]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.c
MdeModulePkg/FaultTolerantWriteDxe: implement standalone MM version
[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 This program and the accompanying materials
45 are licensed and made available under the terms and conditions of the BSD License
46 which accompanies this distribution. The full text of the license may be found at
47 http://opensource.org/licenses/bsd-license.php
48
49 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
50 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
51
52 **/
53
54 #include <Library/UefiBootServicesTableLib.h>
55 #include "FaultTolerantWrite.h"
56 EFI_EVENT mFvbRegistration = NULL;
57
58
59 /**
60 Retrieve the FVB protocol interface by HANDLE.
61
62 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
63 reading, writing, and erasing the target block.
64 @param[out] FvBlock The interface of FVB protocol
65
66 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
67 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
68 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
69
70 **/
71 EFI_STATUS
72 FtwGetFvbByHandle (
73 IN EFI_HANDLE FvBlockHandle,
74 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
75 )
76 {
77 //
78 // To get the FVB protocol interface on the handle
79 //
80 return gBS->HandleProtocol (
81 FvBlockHandle,
82 &gEfiFirmwareVolumeBlockProtocolGuid,
83 (VOID **) FvBlock
84 );
85 }
86
87 /**
88 Retrieve the Swap Address Range protocol interface.
89
90 @param[out] SarProtocol The interface of SAR protocol
91
92 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
93 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
94 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
95
96 **/
97 EFI_STATUS
98 FtwGetSarProtocol (
99 OUT VOID **SarProtocol
100 )
101 {
102 EFI_STATUS Status;
103
104 //
105 // Locate Swap Address Range protocol
106 //
107 Status = gBS->LocateProtocol (
108 &gEfiSwapAddressRangeProtocolGuid,
109 NULL,
110 SarProtocol
111 );
112 return Status;
113 }
114
115 /**
116 Function returns an array of handles that support the FVB protocol
117 in a buffer allocated from pool.
118
119 @param[out] NumberHandles The number of handles returned in Buffer.
120 @param[out] Buffer A pointer to the buffer to return the requested
121 array of handles that support FVB protocol.
122
123 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
124 handles in Buffer was returned in NumberHandles.
125 @retval EFI_NOT_FOUND No FVB handle was found.
126 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
127 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
128
129 **/
130 EFI_STATUS
131 GetFvbCountAndBuffer (
132 OUT UINTN *NumberHandles,
133 OUT EFI_HANDLE **Buffer
134 )
135 {
136 EFI_STATUS Status;
137
138 //
139 // Locate all handles of Fvb protocol
140 //
141 Status = gBS->LocateHandleBuffer (
142 ByProtocol,
143 &gEfiFirmwareVolumeBlockProtocolGuid,
144 NULL,
145 NumberHandles,
146 Buffer
147 );
148 return Status;
149 }
150
151
152 /**
153 Firmware Volume Block Protocol notification event handler.
154
155 @param[in] Event Event whose notification function is being invoked.
156 @param[in] Context Pointer to the notification function's context.
157
158 **/
159 VOID
160 EFIAPI
161 FvbNotificationEvent (
162 IN EFI_EVENT Event,
163 IN VOID *Context
164 )
165 {
166 EFI_STATUS Status;
167 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
168 EFI_FTW_DEVICE *FtwDevice;
169
170 //
171 // Just return to avoid installing FaultTolerantWriteProtocol again
172 // if Fault Tolerant Write protocol has been installed.
173 //
174 Status = gBS->LocateProtocol (
175 &gEfiFaultTolerantWriteProtocolGuid,
176 NULL,
177 (VOID **) &FtwProtocol
178 );
179 if (!EFI_ERROR (Status)) {
180 return ;
181 }
182
183 //
184 // Found proper FVB protocol and initialize FtwDevice for protocol installation
185 //
186 FtwDevice = (EFI_FTW_DEVICE *)Context;
187 Status = InitFtwProtocol (FtwDevice);
188 if (EFI_ERROR(Status)) {
189 return ;
190 }
191
192 //
193 // Install protocol interface
194 //
195 Status = gBS->InstallProtocolInterface (
196 &FtwDevice->Handle,
197 &gEfiFaultTolerantWriteProtocolGuid,
198 EFI_NATIVE_INTERFACE,
199 &FtwDevice->FtwInstance
200 );
201 ASSERT_EFI_ERROR (Status);
202
203 Status = gBS->CloseEvent (Event);
204 ASSERT_EFI_ERROR (Status);
205
206 return;
207 }
208
209
210 /**
211 This function is the entry point of the Fault Tolerant Write driver.
212
213 @param[in] ImageHandle A handle for the image that is initializing this driver
214 @param[in] SystemTable A pointer to the EFI system table
215
216 @retval EFI_SUCCESS The initialization finished successfully.
217 @retval EFI_OUT_OF_RESOURCES Allocate memory error
218 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
219
220 **/
221 EFI_STATUS
222 EFIAPI
223 FaultTolerantWriteInitialize (
224 IN EFI_HANDLE ImageHandle,
225 IN EFI_SYSTEM_TABLE *SystemTable
226 )
227 {
228 EFI_STATUS Status;
229 EFI_FTW_DEVICE *FtwDevice;
230
231 FtwDevice = NULL;
232
233 //
234 // Allocate private data structure for FTW protocol and do some initialization
235 //
236 Status = InitFtwDevice (&FtwDevice);
237 if (EFI_ERROR(Status)) {
238 return Status;
239 }
240
241 //
242 // Register FvbNotificationEvent () notify function.
243 //
244 EfiCreateProtocolNotifyEvent (
245 &gEfiFirmwareVolumeBlockProtocolGuid,
246 TPL_CALLBACK,
247 FvbNotificationEvent,
248 (VOID *)FtwDevice,
249 &mFvbRegistration
250 );
251
252 return EFI_SUCCESS;
253 }
254
255 /**
256 Internal implementation of CRC32. Depending on the execution context
257 (traditional SMM or DXE vs standalone MM), this function is implemented
258 via a call to the CalculateCrc32 () boot service, or via a library
259 call.
260
261 If Buffer is NULL, then ASSERT().
262 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
263
264 @param[in] Buffer A pointer to the buffer on which the 32-bit CRC is to be computed.
265 @param[in] Length The number of bytes in the buffer Data.
266
267 @retval Crc32 The 32-bit CRC was computed for the data buffer.
268
269 **/
270 UINT32
271 FtwCalculateCrc32 (
272 IN VOID *Buffer,
273 IN UINTN Length
274 )
275 {
276 EFI_STATUS Status;
277 UINT32 ReturnValue;
278
279 Status = gBS->CalculateCrc32 (Buffer, Length, &ReturnValue);
280 ASSERT_EFI_ERROR (Status);
281
282 return ReturnValue;
283 }