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