]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwMisc.c
Add full version FaultTolerantWrite Dxe driver.
[mirror_edk2.git] / MdeModulePkg / Universal / FirmwareVolume / FaultTolerantWriteDxe / FtwMisc.c
CommitLineData
6cc9ca32
LG
1/** @file\r
2\r
5944a83b 3 Internal generic functions to operate flash block.\r
d7dec593 4\r
6cc9ca32 5Copyright (c) 2006 - 2008, Intel Corporation \r
d7dec593 6All rights reserved. This program and the accompanying materials \r
7are licensed and made available under the terms and conditions of the BSD License \r
8which accompanies this distribution. The full text of the license may be found at \r
9http://opensource.org/licenses/bsd-license.php \r
10 \r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
13\r
6cc9ca32 14**/\r
d7dec593 15\r
e87a334f 16#include "FtwLite.h"\r
d7dec593 17\r
6aab8214 18/**\r
d7dec593 19\r
20 Check whether a flash buffer is erased.\r
21\r
d7dec593 22\r
6aab8214 23 @param Polarity All 1 or all 0\r
24 @param Buffer Buffer to check\r
25 @param BufferSize Size of the buffer\r
d7dec593 26\r
6aab8214 27 @return A BOOLEAN value indicating erased or not.\r
d7dec593 28\r
6aab8214 29**/\r
30BOOLEAN\r
31IsErasedFlashBuffer (\r
32 IN BOOLEAN Polarity,\r
33 IN UINT8 *Buffer,\r
34 IN UINTN BufferSize\r
35 )\r
d7dec593 36{\r
37 UINT8 ErasedValue;\r
38 UINT8 *Ptr;\r
39\r
40 if (Polarity) {\r
41 ErasedValue = 0xFF;\r
42 } else {\r
43 ErasedValue = 0;\r
44 }\r
45\r
46 Ptr = Buffer;\r
6aab8214 47 while ((BufferSize--) != 0) {\r
d7dec593 48 if (*Ptr++ != ErasedValue) {\r
49 return FALSE;\r
50 }\r
51 }\r
52\r
53 return TRUE;\r
54}\r
55\r
6aab8214 56/**\r
5944a83b 57 To erase the block with the spare block size.\r
6aab8214 58\r
59\r
60 @param FtwLiteDevice Calling context\r
61 @param FvBlock FVB Protocol interface\r
62 @param Lba Lba of the firmware block\r
63\r
64 @retval EFI_SUCCESS Block LBA is Erased successfully\r
65 @retval Others Error occurs\r
66\r
67**/\r
d7dec593 68EFI_STATUS\r
69FtwEraseBlock (\r
70 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,\r
71 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,\r
72 EFI_LBA Lba\r
73 )\r
d7dec593 74{\r
75 return FvBlock->EraseBlocks (\r
76 FvBlock,\r
77 Lba,\r
78 FtwLiteDevice->NumberOfSpareBlock,\r
79 EFI_LBA_LIST_TERMINATOR\r
80 );\r
81}\r
82\r
6aab8214 83/**\r
d7dec593 84\r
85 Erase spare block.\r
86\r
d7dec593 87\r
6aab8214 88 @param FtwLiteDevice Calling context\r
d7dec593 89\r
6aab8214 90 @retval EFI_SUCCESS The erase request was successfully\r
91 completed.\r
92 \r
93 @retval EFI_ACCESS_DENIED The firmware volume is in the\r
94 WriteDisabled state.\r
95 @retval EFI_DEVICE_ERROR The block device is not functioning\r
96 correctly and could not be written.\r
97 The firmware device may have been\r
98 partially erased.\r
99 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed\r
100 in the variable argument list do\r
101 not exist in the firmware volume. \r
d7dec593 102\r
d7dec593 103\r
6aab8214 104**/\r
105EFI_STATUS\r
106FtwEraseSpareBlock (\r
107 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice\r
108 )\r
d7dec593 109{\r
110 return FtwLiteDevice->FtwBackupFvb->EraseBlocks (\r
111 FtwLiteDevice->FtwBackupFvb,\r
112 FtwLiteDevice->FtwSpareLba,\r
113 FtwLiteDevice->NumberOfSpareBlock,\r
114 EFI_LBA_LIST_TERMINATOR\r
115 );\r
116}\r
117\r
6aab8214 118/**\r
119 Retrive the proper FVB protocol interface by HANDLE.\r
120\r
121\r
122 @param FvBlockHandle The handle of FVB protocol that provides services for\r
123 reading, writing, and erasing the target block.\r
124 @param FvBlock The interface of FVB protocol\r
125\r
126 @retval EFI_SUCCESS The function completed successfully\r
127 @retval EFI_ABORTED The function could not complete successfully\r
128\r
129**/\r
d7dec593 130EFI_STATUS\r
131FtwGetFvbByHandle (\r
132 IN EFI_HANDLE FvBlockHandle,\r
133 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
134 )\r
d7dec593 135{\r
136 //\r
137 // To get the FVB protocol interface on the handle\r
138 //\r
139 return gBS->HandleProtocol (\r
140 FvBlockHandle,\r
141 &gEfiFirmwareVolumeBlockProtocolGuid,\r
142 (VOID **) FvBlock\r
143 );\r
144}\r
145\r
6aab8214 146/**\r
d7dec593 147\r
148 Get firmware block by address.\r
149\r
d7dec593 150\r
6aab8214 151 @param Address Address specified the block\r
152 @param FvBlock The block caller wanted\r
d7dec593 153\r
6aab8214 154 @retval EFI_SUCCESS The protocol instance if found.\r
155 @retval EFI_NOT_FOUND Block not found\r
d7dec593 156\r
6aab8214 157**/\r
158EFI_STATUS\r
159GetFvbByAddress (\r
160 IN EFI_PHYSICAL_ADDRESS Address,\r
161 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
162 )\r
d7dec593 163{\r
164 EFI_STATUS Status;\r
165 EFI_HANDLE *HandleBuffer;\r
166 UINTN HandleCount;\r
167 UINTN Index;\r
168 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
169 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
170 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
171\r
172 *FvBlock = NULL;\r
173 //\r
174 // Locate all handles of Fvb protocol\r
175 //\r
176 Status = gBS->LocateHandleBuffer (\r
177 ByProtocol,\r
178 &gEfiFirmwareVolumeBlockProtocolGuid,\r
179 NULL,\r
180 &HandleCount,\r
181 &HandleBuffer\r
182 );\r
183 if (EFI_ERROR (Status)) {\r
184 return EFI_NOT_FOUND;\r
185 }\r
186 //\r
187 // Search all FVB until find the right one\r
188 //\r
189 for (Index = 0; Index < HandleCount; Index += 1) {\r
190 Status = gBS->HandleProtocol (\r
191 HandleBuffer[Index],\r
192 &gEfiFirmwareVolumeBlockProtocolGuid,\r
193 (VOID **) &Fvb\r
194 );\r
195 if (EFI_ERROR (Status)) {\r
196 Status = EFI_NOT_FOUND;\r
197 break;\r
198 }\r
199 //\r
200 // Compare the address and select the right one\r
201 //\r
202 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
203 if (EFI_ERROR (Status)) {\r
204 continue;\r
205 }\r
206\r
207 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
208 if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + (FwVolHeader->FvLength - 1)))) {\r
209 *FvBlock = Fvb;\r
210 Status = EFI_SUCCESS;\r
211 break;\r
212 }\r
213 }\r
214\r
215 FreePool (HandleBuffer);\r
216 return Status;\r
217}\r
218\r
6aab8214 219/**\r
d7dec593 220\r
221 Is it in working block?\r
222\r
d7dec593 223\r
6aab8214 224 @param FtwLiteDevice Calling context\r
225 @param FvBlock Fvb protocol instance\r
226 @param Lba The block specified\r
d7dec593 227\r
6aab8214 228 @return A BOOLEAN value indicating in working block or not.\r
d7dec593 229\r
6aab8214 230**/\r
231BOOLEAN\r
232IsInWorkingBlock (\r
233 EFI_FTW_LITE_DEVICE *FtwLiteDevice,\r
234 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,\r
235 EFI_LBA Lba\r
236 )\r
d7dec593 237{\r
238 //\r
239 // If matching the following condition, the target block is in working block.\r
240 // 1. Target block is on the FV of working block (Using the same FVB protocol instance).\r
241 // 2. Lba falls into the range of working block.\r
242 //\r
243 return (BOOLEAN)\r
244 (\r
245 (FvBlock == FtwLiteDevice->FtwFvBlock) &&\r
246 (Lba >= FtwLiteDevice->FtwWorkBlockLba) &&\r
247 (Lba <= FtwLiteDevice->FtwWorkSpaceLba)\r
248 );\r
249}\r
250\r
6aab8214 251/**\r
252 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.\r
253 Spare block is accessed by FTW backup FVB protocol interface. LBA is\r
254 FtwLiteDevice->FtwSpareLba.\r
255 Target block is accessed by FvBlock protocol interface. LBA is Lba.\r
256\r
257\r
258 @param FtwLiteDevice The private data of FTW_LITE driver\r
259 @param FvBlock FVB Protocol interface to access target block\r
260 @param Lba Lba of the target block\r
261\r
262 @retval EFI_SUCCESS Spare block content is copied to target block\r
263 @retval EFI_INVALID_PARAMETER Input parameter error\r
264 @retval EFI_OUT_OF_RESOURCES Allocate memory error\r
265 @retval EFI_ABORTED The function could not complete successfully\r
266\r
267**/\r
d7dec593 268EFI_STATUS\r
269FlushSpareBlockToTargetBlock (\r
270 EFI_FTW_LITE_DEVICE *FtwLiteDevice,\r
271 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,\r
272 EFI_LBA Lba\r
273 )\r
d7dec593 274{\r
275 EFI_STATUS Status;\r
276 UINTN Length;\r
277 UINT8 *Buffer;\r
278 UINTN Count;\r
279 UINT8 *Ptr;\r
280 UINTN Index;\r
281\r
282 if ((FtwLiteDevice == NULL) || (FvBlock == NULL)) {\r
283 return EFI_INVALID_PARAMETER;\r
284 }\r
285 //\r
286 // Allocate a memory buffer\r
287 //\r
288 Length = FtwLiteDevice->SpareAreaLength;\r
289 Buffer = AllocatePool (Length);\r
290 if (Buffer == NULL) {\r
291 return EFI_OUT_OF_RESOURCES;\r
292 }\r
293 //\r
294 // Read all content of spare block to memory buffer\r
295 //\r
296 Ptr = Buffer;\r
297 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
276a49b6 298 Count = FtwLiteDevice->BlockSize;\r
d7dec593 299 Status = FtwLiteDevice->FtwBackupFvb->Read (\r
300 FtwLiteDevice->FtwBackupFvb,\r
301 FtwLiteDevice->FtwSpareLba + Index,\r
302 0,\r
303 &Count,\r
304 Ptr\r
305 );\r
306 if (EFI_ERROR (Status)) {\r
307 FreePool (Buffer);\r
308 return Status;\r
309 }\r
310\r
311 Ptr += Count;\r
312 }\r
313 //\r
314 // Erase the target block\r
315 //\r
316 Status = FtwEraseBlock (FtwLiteDevice, FvBlock, Lba);\r
317 if (EFI_ERROR (Status)) {\r
318 FreePool (Buffer);\r
319 return EFI_ABORTED;\r
320 }\r
321 //\r
322 // Write memory buffer to block, using the FvbBlock protocol interface\r
323 //\r
324 Ptr = Buffer;\r
325 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
276a49b6 326 Count = FtwLiteDevice->BlockSize;\r
d7dec593 327 Status = FvBlock->Write (FvBlock, Lba + Index, 0, &Count, Ptr);\r
328 if (EFI_ERROR (Status)) {\r
276a49b6 329 DEBUG ((EFI_D_ERROR, "FtwLite: FVB Write block - %r\n", Status));\r
d7dec593 330 FreePool (Buffer);\r
331 return Status;\r
332 }\r
333\r
334 Ptr += Count;\r
335 }\r
336\r
337 FreePool (Buffer);\r
338\r
339 return Status;\r
340}\r
341\r
6aab8214 342/**\r
343 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.\r
344 Spare block is accessed by FTW backup FVB protocol interface. LBA is\r
345 FtwLiteDevice->FtwSpareLba.\r
346 Working block is accessed by FTW working FVB protocol interface. LBA is\r
347 FtwLiteDevice->FtwWorkBlockLba.\r
d7dec593 348\r
d7dec593 349\r
6aab8214 350 @param FtwLiteDevice The private data of FTW_LITE driver\r
d7dec593 351\r
6aab8214 352 @retval EFI_SUCCESS Spare block content is copied to target block\r
353 @retval EFI_OUT_OF_RESOURCES Allocate memory error\r
354 @retval EFI_ABORTED The function could not complete successfully\r
355 Notes:\r
356 Since the working block header is important when FTW initializes, the\r
357 state of the operation should be handled carefully. The Crc value is\r
358 calculated without STATE element.\r
d7dec593 359\r
6aab8214 360**/\r
361EFI_STATUS\r
362FlushSpareBlockToWorkingBlock (\r
363 EFI_FTW_LITE_DEVICE *FtwLiteDevice\r
364 )\r
d7dec593 365{\r
366 EFI_STATUS Status;\r
367 UINTN Length;\r
368 UINT8 *Buffer;\r
369 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingBlockHeader;\r
370 EFI_LBA WorkSpaceLbaOffset;\r
371 UINTN Count;\r
372 UINT8 *Ptr;\r
373 UINTN Index;\r
374\r
375 //\r
376 // Allocate a memory buffer\r
377 //\r
378 Length = FtwLiteDevice->SpareAreaLength;\r
379 Buffer = AllocatePool (Length);\r
380 if (Buffer == NULL) {\r
381 return EFI_OUT_OF_RESOURCES;\r
382 }\r
383 //\r
276a49b6
LG
384 // To guarantee that the WorkingBlockValid is set on spare block\r
385 //\r
386 // Offset = OFFSET_OF(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,\r
387 // WorkingBlockValid);\r
388 // To skip Signature and Crc: sizeof(EFI_GUID)+sizeof(UINT32).\r
d7dec593 389 //\r
390 WorkSpaceLbaOffset = FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->FtwWorkBlockLba;\r
391 FtwUpdateFvState (\r
392 FtwLiteDevice->FtwBackupFvb,\r
393 FtwLiteDevice->FtwSpareLba + WorkSpaceLbaOffset,\r
394 FtwLiteDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),\r
395 WORKING_BLOCK_VALID\r
396 );\r
397 //\r
398 // Read from spare block to memory buffer\r
399 //\r
400 Ptr = Buffer;\r
401 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
276a49b6 402 Count = FtwLiteDevice->BlockSize;\r
d7dec593 403 Status = FtwLiteDevice->FtwBackupFvb->Read (\r
404 FtwLiteDevice->FtwBackupFvb,\r
405 FtwLiteDevice->FtwSpareLba + Index,\r
406 0,\r
407 &Count,\r
408 Ptr\r
409 );\r
410 if (EFI_ERROR (Status)) {\r
411 FreePool (Buffer);\r
412 return Status;\r
413 }\r
414\r
415 Ptr += Count;\r
416 }\r
417 //\r
418 // Clear the CRC and STATE, copy data from spare to working block.\r
419 //\r
276a49b6 420 WorkingBlockHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) (Buffer + (UINTN) WorkSpaceLbaOffset * FtwLiteDevice->BlockSize + FtwLiteDevice->FtwWorkSpaceBase);\r
d7dec593 421 InitWorkSpaceHeader (WorkingBlockHeader);\r
422 WorkingBlockHeader->WorkingBlockValid = FTW_ERASE_POLARITY;\r
423 WorkingBlockHeader->WorkingBlockInvalid = FTW_ERASE_POLARITY;\r
424\r
425 //\r
426 // target block is working block, then\r
427 // Set WorkingBlockInvalid in EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER\r
428 // before erase the working block.\r
429 //\r
f3f2e05d 430 // Offset = OFFSET_OF(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,\r
d7dec593 431 // WorkingBlockInvalid);\r
432 // To skip Signature and Crc: sizeof(EFI_GUID)+sizeof(UINT32).\r
433 //\r
434 Status = FtwUpdateFvState (\r
435 FtwLiteDevice->FtwFvBlock,\r
436 FtwLiteDevice->FtwWorkSpaceLba,\r
437 FtwLiteDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),\r
438 WORKING_BLOCK_INVALID\r
439 );\r
440 if (EFI_ERROR (Status)) {\r
441 FreePool (Buffer);\r
442 return EFI_ABORTED;\r
443 }\r
444\r
445 FtwLiteDevice->FtwWorkSpaceHeader->WorkingBlockInvalid = FTW_VALID_STATE;\r
446\r
447 //\r
448 // Erase the working block\r
449 //\r
450 Status = FtwEraseBlock (\r
451 FtwLiteDevice,\r
452 FtwLiteDevice->FtwFvBlock,\r
453 FtwLiteDevice->FtwWorkBlockLba\r
454 );\r
455 if (EFI_ERROR (Status)) {\r
456 FreePool (Buffer);\r
457 return EFI_ABORTED;\r
458 }\r
459 //\r
460 // Write memory buffer to working block, using the FvbBlock protocol interface\r
461 //\r
462 Ptr = Buffer;\r
463 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
276a49b6 464 Count = FtwLiteDevice->BlockSize;\r
d7dec593 465 Status = FtwLiteDevice->FtwFvBlock->Write (\r
466 FtwLiteDevice->FtwFvBlock,\r
467 FtwLiteDevice->FtwWorkBlockLba + Index,\r
468 0,\r
469 &Count,\r
470 Ptr\r
471 );\r
472 if (EFI_ERROR (Status)) {\r
276a49b6 473 DEBUG ((EFI_D_ERROR, "FtwLite: FVB Write block - %r\n", Status));\r
d7dec593 474 FreePool (Buffer);\r
475 return Status;\r
476 }\r
477\r
478 Ptr += Count;\r
479 }\r
480 //\r
481 // Since the memory buffer will not be used, free memory Buffer.\r
482 //\r
483 FreePool (Buffer);\r
484\r
485 //\r
486 // Update the VALID of the working block\r
487 //\r
f3f2e05d 488 // Offset = OFFSET_OF(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,\r
d7dec593 489 // WorkingBlockValid);\r
490 // Hardcode offset sizeof(EFI_GUID)+sizeof(UINT32), to skip Signature and Crc\r
491 //\r
492 Status = FtwUpdateFvState (\r
493 FtwLiteDevice->FtwFvBlock,\r
494 FtwLiteDevice->FtwWorkSpaceLba,\r
495 FtwLiteDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),\r
496 WORKING_BLOCK_VALID\r
497 );\r
498 if (EFI_ERROR (Status)) {\r
499 return EFI_ABORTED;\r
500 }\r
501\r
502 FtwLiteDevice->FtwWorkSpaceHeader->WorkingBlockValid = FTW_VALID_STATE;\r
503\r
504 return EFI_SUCCESS;\r
505}\r