3 This is a simple fault tolerant write driver.
4 And it only supports write BufferSize <= SpareAreaLength.
6 This boot service only protocol provides fault tolerant write capability for
7 block devices. The protocol has internal non-volatile intermediate storage
8 of the data and private information. It should be able to recover
9 automatically from a critical fault, such as power failure.
11 The implementation uses an FTW Lite (Fault Tolerant Write) Work Space.
12 This work space is a memory copy of the work space on the Woring Block,
13 the size of the work space is the FTW_WORK_SPACE_SIZE bytes.
15 Copyright (c) 2006 - 2008, Intel Corporation
16 All rights reserved. This program and the accompanying materials
17 are licensed and made available under the terms and conditions of the BSD License
18 which accompanies this distribution. The full text of the license may be found at
19 http://opensource.org/licenses/bsd-license.php
21 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
22 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
29 // In write function, we should check the target range to prevent the user
30 // from writing Spare block and Working space directly.
33 // Fault Tolerant Write Protocol API
38 IN EFI_FTW_LITE_PROTOCOL
*This
,
39 IN EFI_HANDLE FvbHandle
,
42 IN OUT UINTN
*NumBytes
,
48 Starts a target block update. This function will record data about write
49 in fault tolerant storage and will complete the write in a recoverable
50 manner, ensuring at all times that either the original contents or
51 the modified contents are available.
54 This - Calling context
55 FvbHandle - The handle of FVB protocol that provides services for
56 reading, writing, and erasing the target block.
57 Lba - The logical block address of the target block.
58 Offset - The offset within the target block to place the data.
59 NumBytes - The number of bytes to write to the target block.
60 Buffer - The data to write.
63 EFI_SUCCESS - The function completed successfully
64 EFI_BAD_BUFFER_SIZE - The write would span a target block, which is not
66 EFI_ACCESS_DENIED - No writes have been allocated.
67 EFI_NOT_FOUND - Cannot find FVB by handle.
68 EFI_OUT_OF_RESOURCES - Cannot allocate memory.
69 EFI_ABORTED - The function could not complete successfully.
74 EFI_FTW_LITE_DEVICE
*FtwLiteDevice
;
75 EFI_FTW_LITE_RECORD
*Record
;
76 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
77 EFI_PHYSICAL_ADDRESS FvbPhysicalAddress
;
82 UINTN SpareBufferSize
;
86 EFI_DEV_PATH_PTR DevPtr
;
89 // Refresh work space and get last record
91 FtwLiteDevice
= FTW_LITE_CONTEXT_FROM_THIS (This
);
92 Status
= WorkSpaceRefresh (FtwLiteDevice
);
93 if (EFI_ERROR (Status
)) {
97 Record
= FtwLiteDevice
->FtwLastRecord
;
100 // Check the flags of last write record
102 if ((Record
->WriteAllocated
== FTW_VALID_STATE
) || (Record
->SpareCompleted
== FTW_VALID_STATE
)) {
103 return EFI_ACCESS_DENIED
;
106 // IF former record has completed, THEN use next record
108 if (Record
->WriteCompleted
== FTW_VALID_STATE
) {
110 FtwLiteDevice
->FtwLastRecord
= Record
;
113 MyOffset
= (UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
;
116 // Check if the input data can fit within the target block
118 if ((Offset
+*NumBytes
) > FtwLiteDevice
->SpareAreaLength
) {
119 return EFI_BAD_BUFFER_SIZE
;
122 // Check if there is enough free space for allocate a record
124 if ((MyOffset
+ WRITE_TOTAL_SIZE
) > FtwLiteDevice
->FtwWorkSpaceSize
) {
125 Status
= FtwReclaimWorkSpace (FtwLiteDevice
);
126 if (EFI_ERROR (Status
)) {
127 DEBUG ((EFI_D_ERROR
, "FtwLite: Reclaim work space - %r", Status
));
132 // Get the FVB protocol by handle
134 Status
= FtwGetFvbByHandle (FvbHandle
, &Fvb
);
135 if (EFI_ERROR (Status
)) {
136 return EFI_NOT_FOUND
;
139 // Allocate a write record in workspace.
140 // Update Header->WriteAllocated as VALID
142 Status
= FtwUpdateFvState (
143 FtwLiteDevice
->FtwFvBlock
,
144 FtwLiteDevice
->FtwWorkSpaceLba
,
145 FtwLiteDevice
->FtwWorkSpaceBase
+ MyOffset
,
149 if (EFI_ERROR (Status
)) {
150 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Allocate record - %r\n", Status
));
154 Record
->WriteAllocated
= FTW_VALID_STATE
;
157 // Prepare data of write record, filling DevPath with memory mapped address.
159 DevPtr
.MemMap
= (MEMMAP_DEVICE_PATH
*) &Record
->DevPath
;
160 DevPtr
.MemMap
->Header
.Type
= HARDWARE_DEVICE_PATH
;
161 DevPtr
.MemMap
->Header
.SubType
= HW_MEMMAP_DP
;
162 SetDevicePathNodeLength (&DevPtr
.MemMap
->Header
, sizeof (MEMMAP_DEVICE_PATH
));
164 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbPhysicalAddress
);
165 if (EFI_ERROR (Status
)) {
166 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Get FVB physical address - %r\n", Status
));
170 DevPtr
.MemMap
->MemoryType
= EfiMemoryMappedIO
;
171 DevPtr
.MemMap
->StartingAddress
= FvbPhysicalAddress
;
172 DevPtr
.MemMap
->EndingAddress
= FvbPhysicalAddress
+*NumBytes
;
177 Record
->Offset
= Offset
;
178 Record
->NumBytes
= *NumBytes
;
181 // Write the record to the work space.
183 MyOffset
= (UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
;
184 MyLength
= FTW_LITE_RECORD_SIZE
;
186 Status
= FtwLiteDevice
->FtwFvBlock
->Write (
187 FtwLiteDevice
->FtwFvBlock
,
188 FtwLiteDevice
->FtwWorkSpaceLba
,
189 FtwLiteDevice
->FtwWorkSpaceBase
+ MyOffset
,
193 if (EFI_ERROR (Status
)) {
197 // Record has been written to working block, then write data.
200 // Allocate a memory buffer
202 MyBufferSize
= FtwLiteDevice
->SpareAreaLength
;
203 MyBuffer
= AllocatePool (MyBufferSize
);
204 if (MyBuffer
== NULL
) {
205 return EFI_OUT_OF_RESOURCES
;
208 // Starting at Lba, if the number of the rest blocks on Fvb is less
209 // than NumberOfSpareBlock.
212 // Read all original data from target block to memory buffer
214 if (IsInWorkingBlock (FtwLiteDevice
, Fvb
, Lba
)) {
216 // If target block falls into working block, we must follow the process of
217 // updating working block.
220 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
221 MyLength
= FtwLiteDevice
->SizeOfSpareBlock
;
222 Status
= FtwLiteDevice
->FtwFvBlock
->Read (
223 FtwLiteDevice
->FtwFvBlock
,
224 FtwLiteDevice
->FtwWorkBlockLba
+ Index
,
229 if (EFI_ERROR (Status
)) {
237 // Update Offset by adding the offset from the start LBA of working block to
238 // the target LBA. The target block can not span working block!
240 Offset
= (((UINTN
) (Lba
- FtwLiteDevice
->FtwWorkBlockLba
)) * FtwLiteDevice
->SizeOfSpareBlock
+ Offset
);
241 ASSERT ((Offset
+*NumBytes
) <= FtwLiteDevice
->SpareAreaLength
);
246 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
247 MyLength
= FtwLiteDevice
->SizeOfSpareBlock
;
248 Status
= Fvb
->Read (Fvb
, Lba
+ Index
, 0, &MyLength
, Ptr
);
249 if (EFI_ERROR (Status
)) {
258 // Overwrite the updating range data with
259 // the input buffer content
261 CopyMem (MyBuffer
+ Offset
, Buffer
, *NumBytes
);
264 // Try to keep the content of spare block
265 // Save spare block into a spare backup memory buffer (Sparebuffer)
267 SpareBufferSize
= FtwLiteDevice
->SpareAreaLength
;
268 SpareBuffer
= AllocatePool (SpareBufferSize
);
269 if (SpareBuffer
== NULL
) {
271 return EFI_OUT_OF_RESOURCES
;
275 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
276 MyLength
= FtwLiteDevice
->SizeOfSpareBlock
;
277 Status
= FtwLiteDevice
->FtwBackupFvb
->Read (
278 FtwLiteDevice
->FtwBackupFvb
,
279 FtwLiteDevice
->FtwSpareLba
+ Index
,
284 if (EFI_ERROR (Status
)) {
286 FreePool (SpareBuffer
);
293 // Write the memory buffer to spare block
294 // Don't forget to erase Flash first.
296 Status
= FtwEraseSpareBlock (FtwLiteDevice
);
298 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
299 MyLength
= FtwLiteDevice
->SizeOfSpareBlock
;
300 Status
= FtwLiteDevice
->FtwBackupFvb
->Write (
301 FtwLiteDevice
->FtwBackupFvb
,
302 FtwLiteDevice
->FtwSpareLba
+ Index
,
307 if (EFI_ERROR (Status
)) {
309 FreePool (SpareBuffer
);
321 // Set the SpareCompleteD in the FTW record,
323 MyOffset
= (UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
;
324 Status
= FtwUpdateFvState (
325 FtwLiteDevice
->FtwFvBlock
,
326 FtwLiteDevice
->FtwWorkSpaceLba
,
327 FtwLiteDevice
->FtwWorkSpaceBase
+ MyOffset
,
330 if (EFI_ERROR (Status
)) {
331 FreePool (SpareBuffer
);
335 Record
->SpareCompleted
= FTW_VALID_STATE
;
338 // Since the content has already backuped in spare block, the write is
339 // guaranteed to be completed with fault tolerant manner.
341 Status
= FtwWriteRecord (FtwLiteDevice
, Fvb
);
342 if (EFI_ERROR (Status
)) {
343 FreePool (SpareBuffer
);
348 FtwLiteDevice
->FtwLastRecord
= Record
;
351 // Restore spare backup buffer into spare block , if no failure happened during FtwWrite.
353 Status
= FtwEraseSpareBlock (FtwLiteDevice
);
355 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
356 MyLength
= FtwLiteDevice
->SizeOfSpareBlock
;
357 Status
= FtwLiteDevice
->FtwBackupFvb
->Write (
358 FtwLiteDevice
->FtwBackupFvb
,
359 FtwLiteDevice
->FtwSpareLba
+ Index
,
364 if (EFI_ERROR (Status
)) {
365 FreePool (SpareBuffer
);
374 FreePool (SpareBuffer
);
378 "FtwLite: Write() success, (Lba:Offset)=(%lx:0x%x), NumBytes: 0x%x\n",
390 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
391 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
396 Write a record with fault tolerant mannaer.
397 Since the content has already backuped in spare block, the write is
398 guaranteed to be completed with fault tolerant manner.
401 FtwLiteDevice - The private data of FTW_LITE driver
402 Fvb - The FVB protocol that provides services for
403 reading, writing, and erasing the target block.
406 EFI_SUCCESS - The function completed successfully
407 EFI_ABORTED - The function could not complete successfully
412 EFI_FTW_LITE_RECORD
*Record
;
413 EFI_LBA WorkSpaceLbaOffset
;
417 // Spare Complete but Destination not complete,
418 // Recover the targt block with the spare block.
420 Record
= FtwLiteDevice
->FtwLastRecord
;
423 // IF target block is working block, THEN Flush Spare Block To Working Block;
424 // ELSE IF target block is boot block, THEN Flush Spare Block To boot Block;
425 // ELSE flush spare block to normal target block.ENDIF
427 if (IsInWorkingBlock (FtwLiteDevice
, Fvb
, Record
->Lba
)) {
429 // If target block is working block, Attention:
430 // it's required to set SPARE_COMPLETED to spare block.
432 WorkSpaceLbaOffset
= FtwLiteDevice
->FtwWorkSpaceLba
- FtwLiteDevice
->FtwWorkBlockLba
;
433 Offset
= (UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
;
434 Status
= FtwUpdateFvState (
435 FtwLiteDevice
->FtwBackupFvb
,
436 FtwLiteDevice
->FtwSpareLba
+ WorkSpaceLbaOffset
,
437 FtwLiteDevice
->FtwWorkSpaceBase
+ Offset
,
440 ASSERT_EFI_ERROR (Status
);
442 Status
= FlushSpareBlockToWorkingBlock (FtwLiteDevice
);
443 } else if (IsBootBlock (FtwLiteDevice
, Fvb
, Record
->Lba
)) {
447 Status
= FlushSpareBlockToBootBlock (FtwLiteDevice
);
450 // Update blocks other than working block or boot block
452 Status
= FlushSpareBlockToTargetBlock (FtwLiteDevice
, Fvb
, Record
->Lba
);
455 ASSERT_EFI_ERROR (Status
);
458 // Set WriteCompleted flag in record
460 Offset
= (UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
;
461 Status
= FtwUpdateFvState (
462 FtwLiteDevice
->FtwFvBlock
,
463 FtwLiteDevice
->FtwWorkSpaceLba
,
464 FtwLiteDevice
->FtwWorkSpaceBase
+ Offset
,
467 ASSERT_EFI_ERROR (Status
);
469 Record
->WriteCompleted
= FTW_VALID_STATE
;
476 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
481 Restarts a previously interrupted write. The caller must provide the
482 block protocol needed to complete the interrupted write.
485 FtwLiteDevice - The private data of FTW_LITE driver
486 FvbHandle - The handle of FVB protocol that provides services for
487 reading, writing, and erasing the target block.
490 EFI_SUCCESS - The function completed successfully
491 EFI_ACCESS_DENIED - No pending writes exist
492 EFI_NOT_FOUND - FVB protocol not found by the handle
493 EFI_ABORTED - The function could not complete successfully
498 EFI_FTW_LITE_RECORD
*Record
;
499 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
500 EFI_DEV_PATH_PTR DevPathPtr
;
503 // Spare Completed but Destination not complete,
504 // Recover the targt block with the spare block.
506 Record
= FtwLiteDevice
->FtwLastRecord
;
509 // Only support memory mapped FVB device path by now.
511 DevPathPtr
.MemMap
= (MEMMAP_DEVICE_PATH
*) &Record
->DevPath
;
512 if (!((DevPathPtr
.MemMap
->Header
.Type
== HARDWARE_DEVICE_PATH
) && (DevPathPtr
.MemMap
->Header
.SubType
== HW_MEMMAP_DP
))
514 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: FVB Device Path is not memory mapped\n"));
518 Status
= GetFvbByAddress (DevPathPtr
.MemMap
->StartingAddress
, &Fvb
);
519 if (EFI_ERROR (Status
)) {
520 return EFI_NOT_FOUND
;
523 // Since the content has already backuped in spare block, the write is
524 // guaranteed to be completed with fault tolerant manner.
526 Status
= FtwWriteRecord (FtwLiteDevice
, Fvb
);
527 DEBUG ((EFI_D_FTW_INFO
, "FtwLite: Restart() - %r\n", Status
));
530 FtwLiteDevice
->FtwLastRecord
= Record
;
534 // This is restart, no need to keep spareblock content.
536 FtwEraseSpareBlock (FtwLiteDevice
);
544 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
549 Aborts all previous allocated writes.
552 FtwLiteDevice - The private data of FTW_LITE driver
555 EFI_SUCCESS - The function completed successfully
556 EFI_ABORTED - The function could not complete successfully.
557 EFI_NOT_FOUND - No allocated writes exist.
564 if (FtwLiteDevice
->FtwLastRecord
->WriteCompleted
== FTW_VALID_STATE
) {
565 return EFI_NOT_FOUND
;
568 // Update the complete state of the header as VALID and abort.
570 Offset
= (UINT8
*) FtwLiteDevice
->FtwLastRecord
- FtwLiteDevice
->FtwWorkSpace
;
571 Status
= FtwUpdateFvState (
572 FtwLiteDevice
->FtwFvBlock
,
573 FtwLiteDevice
->FtwWorkSpaceLba
,
574 FtwLiteDevice
->FtwWorkSpaceBase
+ Offset
,
577 if (EFI_ERROR (Status
)) {
581 FtwLiteDevice
->FtwLastRecord
->WriteCompleted
= FTW_VALID_STATE
;
583 Status
= FtwGetLastRecord (FtwLiteDevice
, &FtwLiteDevice
->FtwLastRecord
);
586 // Erase the spare block
588 Status
= FtwEraseSpareBlock (FtwLiteDevice
);
590 DEBUG ((EFI_D_FTW_INFO
, "FtwLite: Abort() success \n"));
597 IN EFI_HANDLE ImageHandle
,
598 IN EFI_SYSTEM_TABLE
*SystemTable
602 This function is the entry point of the Fault Tolerant Write driver.
605 ImageHandle - EFI_HANDLE: A handle for the image that is initializing
607 SystemTable - EFI_SYSTEM_TABLE: A pointer to the EFI system table
610 EFI_SUCCESS - FTW has finished the initialization
611 EFI_ABORTED - FTW initialization error
615 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
617 EFI_HANDLE
*HandleBuffer
;
619 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
620 EFI_PHYSICAL_ADDRESS BaseAddress
;
621 EFI_FTW_LITE_DEVICE
*FtwLiteDevice
;
622 EFI_FTW_LITE_RECORD
*Record
;
626 EFI_FV_BLOCK_MAP_ENTRY
*FvbMapEntry
;
630 // Allocate Private data of this driver,
631 // INCLUDING THE FtwWorkSpace[FTW_WORK_SPACE_SIZE].
633 FtwLiteDevice
= NULL
;
634 FtwLiteDevice
= AllocatePool (sizeof (EFI_FTW_LITE_DEVICE
) + FTW_WORK_SPACE_SIZE
);
635 if (FtwLiteDevice
!= NULL
) {
636 Status
= EFI_SUCCESS
;
638 Status
= EFI_OUT_OF_RESOURCES
;
641 ASSERT_EFI_ERROR (Status
);
643 ZeroMem (FtwLiteDevice
, sizeof (EFI_FTW_LITE_DEVICE
));
644 FtwLiteDevice
->Signature
= FTW_LITE_DEVICE_SIGNATURE
;
647 // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.
649 FtwLiteDevice
->FtwWorkSpace
= (UINT8
*) (FtwLiteDevice
+ 1);
650 FtwLiteDevice
->FtwWorkSpaceSize
= FTW_WORK_SPACE_SIZE
;
652 FtwLiteDevice
->FtwWorkSpace
,
653 FtwLiteDevice
->FtwWorkSpaceSize
,
656 FtwLiteDevice
->FtwWorkSpaceHeader
= (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*) FtwLiteDevice
->FtwWorkSpace
;
658 FtwLiteDevice
->FtwLastRecord
= NULL
;
660 FtwLiteDevice
->WorkSpaceAddress
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageFtwWorkingBase
);
661 FtwLiteDevice
->WorkSpaceLength
= (UINTN
) PcdGet32 (PcdFlashNvStorageFtwWorkingSize
);
663 FtwLiteDevice
->SpareAreaAddress
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageFtwSpareBase
);
664 FtwLiteDevice
->SpareAreaLength
= (UINTN
) PcdGet32 (PcdFlashNvStorageFtwSpareSize
);
666 ASSERT ((FtwLiteDevice
->WorkSpaceLength
!= 0) && (FtwLiteDevice
->SpareAreaLength
!= 0));
669 // Locate FVB protocol
671 Status
= gBS
->LocateHandleBuffer (
673 &gEfiFirmwareVolumeBlockProtocolGuid
,
678 ASSERT_EFI_ERROR (Status
);
680 ASSERT (HandleCount
> 0);
682 FtwLiteDevice
->FtwFvBlock
= NULL
;
683 FtwLiteDevice
->FtwBackupFvb
= NULL
;
684 FtwLiteDevice
->FtwWorkSpaceLba
= (EFI_LBA
) (-1);
685 FtwLiteDevice
->FtwSpareLba
= (EFI_LBA
) (-1);
686 for (Index
= 0; Index
< HandleCount
; Index
+= 1) {
687 Status
= gBS
->HandleProtocol (
689 &gEfiFirmwareVolumeBlockProtocolGuid
,
692 ASSERT_EFI_ERROR (Status
);
694 Status
= Fvb
->GetPhysicalAddress (Fvb
, &BaseAddress
);
695 if (EFI_ERROR (Status
)) {
699 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) BaseAddress
);
701 if ((FtwLiteDevice
->WorkSpaceAddress
>= BaseAddress
) &&
702 (FtwLiteDevice
->WorkSpaceAddress
<= (BaseAddress
+ FwVolHeader
->FvLength
))
704 FtwLiteDevice
->FtwFvBlock
= Fvb
;
706 // To get the LBA of work space
708 if ((FwVolHeader
->FvLength
) > (FwVolHeader
->HeaderLength
)) {
710 // FV may have multiple types of BlockLength
712 FvbMapEntry
= &FwVolHeader
->BlockMap
[0];
713 while (!((FvbMapEntry
->NumBlocks
== 0) && (FvbMapEntry
->Length
== 0))) {
714 for (LbaIndex
= 1; LbaIndex
<= FvbMapEntry
->NumBlocks
; LbaIndex
+= 1) {
715 if (FtwLiteDevice
->WorkSpaceAddress
< (BaseAddress
+ FvbMapEntry
->Length
* LbaIndex
)) {
716 FtwLiteDevice
->FtwWorkSpaceLba
= LbaIndex
- 1;
718 // Get the Work space size and Base(Offset)
720 FtwLiteDevice
->FtwWorkSpaceSize
= FtwLiteDevice
->WorkSpaceLength
;
721 FtwLiteDevice
->FtwWorkSpaceBase
= (UINTN
) (FtwLiteDevice
->WorkSpaceAddress
- (BaseAddress
+ FvbMapEntry
->Length
* (LbaIndex
- 1)));
736 if ((FtwLiteDevice
->SpareAreaAddress
>= BaseAddress
) &&
737 (FtwLiteDevice
->SpareAreaAddress
< (BaseAddress
+ FwVolHeader
->FvLength
))
739 FtwLiteDevice
->FtwBackupFvb
= Fvb
;
741 // To get the LBA of spare
743 if ((FwVolHeader
->FvLength
) > (FwVolHeader
->HeaderLength
)) {
745 // FV may have multiple types of BlockLength
747 FvbMapEntry
= &FwVolHeader
->BlockMap
[0];
748 while (!((FvbMapEntry
->NumBlocks
== 0) && (FvbMapEntry
->Length
== 0))) {
749 for (LbaIndex
= 1; LbaIndex
<= FvbMapEntry
->NumBlocks
; LbaIndex
+= 1) {
750 if (FtwLiteDevice
->SpareAreaAddress
< (BaseAddress
+ FvbMapEntry
->Length
* LbaIndex
)) {
752 // Get the NumberOfSpareBlock and SizeOfSpareBlock
754 FtwLiteDevice
->FtwSpareLba
= LbaIndex
- 1;
755 FtwLiteDevice
->SizeOfSpareBlock
= FvbMapEntry
->Length
;
756 FtwLiteDevice
->NumberOfSpareBlock
= FtwLiteDevice
->SpareAreaLength
/ FtwLiteDevice
->SizeOfSpareBlock
;
758 // Check the range of spare area to make sure that it's in FV range
760 ASSERT ((FtwLiteDevice
->FtwSpareLba
+ FtwLiteDevice
->NumberOfSpareBlock
) <= FvbMapEntry
->NumBlocks
);
774 // Calculate the start LBA of working block. Working block is an area which
775 // contains working space in its last block and has the same size as spare
776 // block, unless there are not enough blocks before the block that contains
779 FtwLiteDevice
->FtwWorkBlockLba
= FtwLiteDevice
->FtwWorkSpaceLba
- FtwLiteDevice
->NumberOfSpareBlock
+ 1;
780 if ((INT64
) (FtwLiteDevice
->FtwWorkBlockLba
) < 0) {
781 FtwLiteDevice
->FtwWorkBlockLba
= 0;
784 if ((FtwLiteDevice
->FtwFvBlock
== NULL
) ||
785 (FtwLiteDevice
->FtwBackupFvb
== NULL
) ||
786 (FtwLiteDevice
->FtwWorkSpaceLba
== (EFI_LBA
) (-1)) ||
787 (FtwLiteDevice
->FtwSpareLba
== (EFI_LBA
) (-1))
789 DEBUG ((EFI_D_ERROR
, "FtwLite: Working or spare FVB not ready\n"));
790 ASSERT_EFI_ERROR (Status
);
793 // Refresh workspace data from working block
795 Status
= WorkSpaceRefresh (FtwLiteDevice
);
796 ASSERT_EFI_ERROR (Status
);
799 // If the working block workspace is not valid, try the spare block
801 if (!IsValidWorkSpace (FtwLiteDevice
->FtwWorkSpaceHeader
)) {
802 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Workspace invalid, read from backup\n"));
804 // Read from spare block
806 Length
= FtwLiteDevice
->FtwWorkSpaceSize
;
807 Status
= FtwLiteDevice
->FtwBackupFvb
->Read (
808 FtwLiteDevice
->FtwBackupFvb
,
809 FtwLiteDevice
->FtwSpareLba
,
810 FtwLiteDevice
->FtwWorkSpaceBase
,
812 FtwLiteDevice
->FtwWorkSpace
814 ASSERT_EFI_ERROR (Status
);
817 // If spare block is valid, then replace working block content.
819 if (IsValidWorkSpace (FtwLiteDevice
->FtwWorkSpaceHeader
)) {
820 Status
= FlushSpareBlockToWorkingBlock (FtwLiteDevice
);
821 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Restart working block in Init() - %r\n", Status
));
822 ASSERT_EFI_ERROR (Status
);
824 FtwAbort (FtwLiteDevice
);
826 // Refresh work space.
828 Status
= WorkSpaceRefresh (FtwLiteDevice
);
829 if (EFI_ERROR (Status
)) {
833 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Both are invalid, init workspace\n"));
835 // If both are invalid, then initialize work space.
838 FtwLiteDevice
->FtwWorkSpace
,
839 FtwLiteDevice
->FtwWorkSpaceSize
,
842 InitWorkSpaceHeader (FtwLiteDevice
->FtwWorkSpaceHeader
);
844 // Write to work space on the working block
846 Length
= FtwLiteDevice
->FtwWorkSpaceSize
;
847 Status
= FtwLiteDevice
->FtwFvBlock
->Write (
848 FtwLiteDevice
->FtwFvBlock
,
849 FtwLiteDevice
->FtwWorkSpaceLba
,
850 FtwLiteDevice
->FtwWorkSpaceBase
,
852 FtwLiteDevice
->FtwWorkSpace
854 if (EFI_ERROR (Status
)) {
860 // Hook the protocol API
862 FtwLiteDevice
->FtwLiteInstance
.Write
= FtwLiteWrite
;
865 // Install protocol interface
867 Status
= gBS
->InstallProtocolInterface (
868 &FtwLiteDevice
->Handle
,
869 &gEfiFaultTolerantWriteLiteProtocolGuid
,
870 EFI_NATIVE_INTERFACE
,
871 &FtwLiteDevice
->FtwLiteInstance
873 if (EFI_ERROR (Status
)) {
877 // If (!SpareCompleted) THEN Abort to rollback.
879 if ((FtwLiteDevice
->FtwLastRecord
->WriteAllocated
== FTW_VALID_STATE
) &&
880 (FtwLiteDevice
->FtwLastRecord
->SpareCompleted
!= FTW_VALID_STATE
)
882 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Init.. record not SpareCompleted, abort()\n"));
883 FtwAbort (FtwLiteDevice
);
886 // if (SpareCompleted) THEN Restart to fault tolerant write.
888 if ((FtwLiteDevice
->FtwLastRecord
->SpareCompleted
== FTW_VALID_STATE
) &&
889 (FtwLiteDevice
->FtwLastRecord
->WriteCompleted
!= FTW_VALID_STATE
)
892 Status
= FtwRestart (FtwLiteDevice
);
893 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Restart last write - %r\n", Status
));
894 if (EFI_ERROR (Status
)) {
899 // To check the workspace buffer behind last records is EMPTY or not.
900 // If it's not EMPTY, FTW_LITE also need to call reclaim().
902 Record
= FtwLiteDevice
->FtwLastRecord
;
903 Offset
= (UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
;
904 if (FtwLiteDevice
->FtwWorkSpace
[Offset
] != FTW_ERASED_BYTE
) {
905 Offset
+= WRITE_TOTAL_SIZE
;
908 if (!IsErasedFlashBuffer (
910 FtwLiteDevice
->FtwWorkSpace
+ Offset
,
911 FtwLiteDevice
->FtwWorkSpaceSize
- Offset
913 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Workspace is dirty, call reclaim...\n"));
914 Status
= FtwReclaimWorkSpace (FtwLiteDevice
);
915 if (EFI_ERROR (Status
)) {
916 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Workspace reclaim - %r\n", Status
));