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