]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / FaultTolerantWriteDxe / FaultTolerantWriteDxe.c
1 /** @file
2
3 This is a simple fault tolerant write driver.
4
5 This boot service protocol only provides fault tolerant write capability for
6 block devices. The protocol has internal non-volatile intermediate storage
7 of the data and private information. It should be able to recover
8 automatically from a critical fault, such as power failure.
9
10 The implementation uses an FTW (Fault Tolerant Write) Work Space.
11 This work space is a memory copy of the work space on the Working Block,
12 the size of the work space is the FTW_WORK_SPACE_SIZE bytes.
13
14 The work space stores each write record as EFI_FTW_RECORD structure.
15 The spare block stores the write buffer before write to the target block.
16
17 The write record has three states to specify the different phase of write operation.
18 1) WRITE_ALLOCATED is that the record is allocated in write space.
19 The information of write operation is stored in write record structure.
20 2) SPARE_COMPLETED is that the data from write buffer is writed into the spare block as the backup.
21 3) WRITE_COMPLETED is that the data is copied from the spare block to the target block.
22
23 This driver operates the data as the whole size of spare block.
24 It first read the SpareAreaLength data from the target block into the spare memory buffer.
25 Then copy the write buffer data into the spare memory buffer.
26 Then write the spare memory buffer into the spare block.
27 Final copy the data from the spare block to the target block.
28
29 To make this drive work well, the following conditions must be satisfied:
30 1. The write NumBytes data must be fit within Spare area.
31 Offset + NumBytes <= SpareAreaLength
32 2. The whole flash range has the same block size.
33 3. Working block is an area which contains working space in its last block and has the same size as spare block.
34 4. Working Block area must be in the single one Firmware Volume Block range which FVB protocol is produced on.
35 5. Spare area must be in the single one Firmware Volume Block range which FVB protocol is produced on.
36 6. Any write data area (SpareAreaLength Area) which the data will be written into must be
37 in the single one Firmware Volume Block range which FVB protocol is produced on.
38 7. If write data area (such as Variable range) is enlarged, the spare area range must be enlarged.
39 The spare area must be enough large to store the write data before write them into the target range.
40 If one of them is not satisfied, FtwWrite may fail.
41 Usually, Spare area only takes one block. That's SpareAreaLength = BlockSize, NumberOfSpareBlock = 1.
42
43 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
44 SPDX-License-Identifier: BSD-2-Clause-Patent
45
46 **/
47
48 #include <Library/UefiBootServicesTableLib.h>
49 #include "FaultTolerantWrite.h"
50 EFI_EVENT mFvbRegistration = NULL;
51
52
53 /**
54 Retrieve the FVB protocol interface by HANDLE.
55
56 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
57 reading, writing, and erasing the target block.
58 @param[out] FvBlock The interface of FVB protocol
59
60 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
61 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
62 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
63
64 **/
65 EFI_STATUS
66 FtwGetFvbByHandle (
67 IN EFI_HANDLE FvBlockHandle,
68 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
69 )
70 {
71 //
72 // To get the FVB protocol interface on the handle
73 //
74 return gBS->HandleProtocol (
75 FvBlockHandle,
76 &gEfiFirmwareVolumeBlockProtocolGuid,
77 (VOID **) FvBlock
78 );
79 }
80
81 /**
82 Retrieve the Swap Address Range protocol interface.
83
84 @param[out] SarProtocol The interface of SAR protocol
85
86 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
87 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
88 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
89
90 **/
91 EFI_STATUS
92 FtwGetSarProtocol (
93 OUT VOID **SarProtocol
94 )
95 {
96 EFI_STATUS Status;
97
98 //
99 // Locate Swap Address Range protocol
100 //
101 Status = gBS->LocateProtocol (
102 &gEfiSwapAddressRangeProtocolGuid,
103 NULL,
104 SarProtocol
105 );
106 return Status;
107 }
108
109 /**
110 Function returns an array of handles that support the FVB protocol
111 in a buffer allocated from pool.
112
113 @param[out] NumberHandles The number of handles returned in Buffer.
114 @param[out] Buffer A pointer to the buffer to return the requested
115 array of handles that support FVB protocol.
116
117 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
118 handles in Buffer was returned in NumberHandles.
119 @retval EFI_NOT_FOUND No FVB handle was found.
120 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
121 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
122
123 **/
124 EFI_STATUS
125 GetFvbCountAndBuffer (
126 OUT UINTN *NumberHandles,
127 OUT EFI_HANDLE **Buffer
128 )
129 {
130 EFI_STATUS Status;
131
132 //
133 // Locate all handles of Fvb protocol
134 //
135 Status = gBS->LocateHandleBuffer (
136 ByProtocol,
137 &gEfiFirmwareVolumeBlockProtocolGuid,
138 NULL,
139 NumberHandles,
140 Buffer
141 );
142 return Status;
143 }
144
145
146 /**
147 Firmware Volume Block Protocol notification event handler.
148
149 @param[in] Event Event whose notification function is being invoked.
150 @param[in] Context Pointer to the notification function's context.
151
152 **/
153 VOID
154 EFIAPI
155 FvbNotificationEvent (
156 IN EFI_EVENT Event,
157 IN VOID *Context
158 )
159 {
160 EFI_STATUS Status;
161 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
162 EFI_FTW_DEVICE *FtwDevice;
163
164 //
165 // Just return to avoid installing FaultTolerantWriteProtocol again
166 // if Fault Tolerant Write protocol has been installed.
167 //
168 Status = gBS->LocateProtocol (
169 &gEfiFaultTolerantWriteProtocolGuid,
170 NULL,
171 (VOID **) &FtwProtocol
172 );
173 if (!EFI_ERROR (Status)) {
174 return ;
175 }
176
177 //
178 // Found proper FVB protocol and initialize FtwDevice for protocol installation
179 //
180 FtwDevice = (EFI_FTW_DEVICE *)Context;
181 Status = InitFtwProtocol (FtwDevice);
182 if (EFI_ERROR(Status)) {
183 return ;
184 }
185
186 //
187 // Install protocol interface
188 //
189 Status = gBS->InstallProtocolInterface (
190 &FtwDevice->Handle,
191 &gEfiFaultTolerantWriteProtocolGuid,
192 EFI_NATIVE_INTERFACE,
193 &FtwDevice->FtwInstance
194 );
195 ASSERT_EFI_ERROR (Status);
196
197 Status = gBS->CloseEvent (Event);
198 ASSERT_EFI_ERROR (Status);
199
200 return;
201 }
202
203
204 /**
205 This function is the entry point of the Fault Tolerant Write driver.
206
207 @param[in] ImageHandle A handle for the image that is initializing this driver
208 @param[in] SystemTable A pointer to the EFI system table
209
210 @retval EFI_SUCCESS The initialization finished successfully.
211 @retval EFI_OUT_OF_RESOURCES Allocate memory error
212 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
213
214 **/
215 EFI_STATUS
216 EFIAPI
217 FaultTolerantWriteInitialize (
218 IN EFI_HANDLE ImageHandle,
219 IN EFI_SYSTEM_TABLE *SystemTable
220 )
221 {
222 EFI_STATUS Status;
223 EFI_FTW_DEVICE *FtwDevice;
224
225 FtwDevice = NULL;
226
227 //
228 // Allocate private data structure for FTW protocol and do some initialization
229 //
230 Status = InitFtwDevice (&FtwDevice);
231 if (EFI_ERROR(Status)) {
232 return Status;
233 }
234
235 //
236 // Register FvbNotificationEvent () notify function.
237 //
238 EfiCreateProtocolNotifyEvent (
239 &gEfiFirmwareVolumeBlockProtocolGuid,
240 TPL_CALLBACK,
241 FvbNotificationEvent,
242 (VOID *)FtwDevice,
243 &mFvbRegistration
244 );
245
246 return EFI_SUCCESS;
247 }
248
249 /**
250 Internal implementation of CRC32. Depending on the execution context
251 (traditional SMM or DXE vs standalone MM), this function is implemented
252 via a call to the CalculateCrc32 () boot service, or via a library
253 call.
254
255 If Buffer is NULL, then ASSERT().
256 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
257
258 @param[in] Buffer A pointer to the buffer on which the 32-bit CRC is to be computed.
259 @param[in] Length The number of bytes in the buffer Data.
260
261 @retval Crc32 The 32-bit CRC was computed for the data buffer.
262
263 **/
264 UINT32
265 FtwCalculateCrc32 (
266 IN VOID *Buffer,
267 IN UINTN Length
268 )
269 {
270 EFI_STATUS Status;
271 UINT32 ReturnValue;
272
273 Status = gBS->CalculateCrc32 (Buffer, Length, &ReturnValue);
274 ASSERT_EFI_ERROR (Status);
275
276 return ReturnValue;
277 }