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
36 Starts a target block update. This function will record data about write
37 in fault tolerant storage and will complete the write in a recoverable
38 manner, ensuring at all times that either the original contents or
39 the modified contents are available.
42 @param This Calling context
43 @param FvbHandle The handle of FVB protocol that provides services for
44 reading, writing, and erasing the target block.
45 @param Lba The logical block address of the target block.
46 @param Offset The offset within the target block to place the data.
47 @param NumBytes The number of bytes to write to the target block.
48 @param Buffer The data to write.
50 @retval EFI_SUCCESS The function completed successfully
51 @retval EFI_BAD_BUFFER_SIZE The write would span a target block, which is not
53 @retval EFI_ACCESS_DENIED No writes have been allocated.
54 @retval EFI_NOT_FOUND Cannot find FVB by handle.
55 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
56 @retval EFI_ABORTED The function could not complete successfully.
62 IN EFI_FTW_LITE_PROTOCOL
*This
,
63 IN EFI_HANDLE FvbHandle
,
66 IN OUT UINTN
*NumBytes
,
71 EFI_FTW_LITE_DEVICE
*FtwLiteDevice
;
72 EFI_FTW_LITE_RECORD
*Record
;
73 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
74 EFI_PHYSICAL_ADDRESS FvbPhysicalAddress
;
79 UINTN SpareBufferSize
;
83 EFI_DEV_PATH_PTR DevPtr
;
86 // Refresh work space and get last record
88 FtwLiteDevice
= FTW_LITE_CONTEXT_FROM_THIS (This
);
89 Status
= WorkSpaceRefresh (FtwLiteDevice
);
90 if (EFI_ERROR (Status
)) {
94 Record
= FtwLiteDevice
->FtwLastRecord
;
97 // Check the flags of last write record
99 if ((Record
->WriteAllocated
== FTW_VALID_STATE
) || (Record
->SpareCompleted
== FTW_VALID_STATE
)) {
100 return EFI_ACCESS_DENIED
;
103 // IF former record has completed, THEN use next record
105 if (Record
->WriteCompleted
== FTW_VALID_STATE
) {
107 FtwLiteDevice
->FtwLastRecord
= Record
;
110 MyOffset
= (UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
;
113 // Check if the input data can fit within the target block
115 if ((Offset
+*NumBytes
) > FtwLiteDevice
->SpareAreaLength
) {
116 return EFI_BAD_BUFFER_SIZE
;
119 // Check if there is enough free space for allocate a record
121 if ((MyOffset
+ WRITE_TOTAL_SIZE
) > FtwLiteDevice
->FtwWorkSpaceSize
) {
122 Status
= FtwReclaimWorkSpace (FtwLiteDevice
);
123 if (EFI_ERROR (Status
)) {
124 DEBUG ((EFI_D_ERROR
, "FtwLite: Reclaim work space - %r", Status
));
129 // Get the FVB protocol by handle
131 Status
= FtwGetFvbByHandle (FvbHandle
, &Fvb
);
132 if (EFI_ERROR (Status
)) {
133 return EFI_NOT_FOUND
;
136 // Allocate a write record in workspace.
137 // Update Header->WriteAllocated as VALID
139 Status
= FtwUpdateFvState (
140 FtwLiteDevice
->FtwFvBlock
,
141 FtwLiteDevice
->FtwWorkSpaceLba
,
142 FtwLiteDevice
->FtwWorkSpaceBase
+ MyOffset
,
146 if (EFI_ERROR (Status
)) {
147 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Allocate record - %r\n", Status
));
151 Record
->WriteAllocated
= FTW_VALID_STATE
;
154 // Prepare data of write record, filling DevPath with memory mapped address.
156 DevPtr
.MemMap
= (MEMMAP_DEVICE_PATH
*) &Record
->DevPath
;
157 DevPtr
.MemMap
->Header
.Type
= HARDWARE_DEVICE_PATH
;
158 DevPtr
.MemMap
->Header
.SubType
= HW_MEMMAP_DP
;
159 SetDevicePathNodeLength (&DevPtr
.MemMap
->Header
, sizeof (MEMMAP_DEVICE_PATH
));
161 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbPhysicalAddress
);
162 if (EFI_ERROR (Status
)) {
163 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Get FVB physical address - %r\n", Status
));
167 DevPtr
.MemMap
->MemoryType
= EfiMemoryMappedIO
;
168 DevPtr
.MemMap
->StartingAddress
= FvbPhysicalAddress
;
169 DevPtr
.MemMap
->EndingAddress
= FvbPhysicalAddress
+*NumBytes
;
174 Record
->Offset
= Offset
;
175 Record
->NumBytes
= *NumBytes
;
178 // Write the record to the work space.
180 MyOffset
= (UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
;
181 MyLength
= FTW_LITE_RECORD_SIZE
;
183 Status
= FtwLiteDevice
->FtwFvBlock
->Write (
184 FtwLiteDevice
->FtwFvBlock
,
185 FtwLiteDevice
->FtwWorkSpaceLba
,
186 FtwLiteDevice
->FtwWorkSpaceBase
+ MyOffset
,
190 if (EFI_ERROR (Status
)) {
194 // Record has been written to working block, then write data.
197 // Allocate a memory buffer
199 MyBufferSize
= FtwLiteDevice
->SpareAreaLength
;
200 MyBuffer
= AllocatePool (MyBufferSize
);
201 if (MyBuffer
== NULL
) {
202 return EFI_OUT_OF_RESOURCES
;
205 // Starting at Lba, if the number of the rest blocks on Fvb is less
206 // than NumberOfSpareBlock.
209 // Read all original data from target block to memory buffer
211 if (IsInWorkingBlock (FtwLiteDevice
, Fvb
, Lba
)) {
213 // If target block falls into working block, we must follow the process of
214 // updating working block.
217 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
218 MyLength
= FtwLiteDevice
->SizeOfSpareBlock
;
219 Status
= FtwLiteDevice
->FtwFvBlock
->Read (
220 FtwLiteDevice
->FtwFvBlock
,
221 FtwLiteDevice
->FtwWorkBlockLba
+ Index
,
226 if (EFI_ERROR (Status
)) {
234 // Update Offset by adding the offset from the start LBA of working block to
235 // the target LBA. The target block can not span working block!
237 Offset
= (((UINTN
) (Lba
- FtwLiteDevice
->FtwWorkBlockLba
)) * FtwLiteDevice
->SizeOfSpareBlock
+ Offset
);
238 ASSERT ((Offset
+*NumBytes
) <= FtwLiteDevice
->SpareAreaLength
);
243 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
244 MyLength
= FtwLiteDevice
->SizeOfSpareBlock
;
245 Status
= Fvb
->Read (Fvb
, Lba
+ Index
, 0, &MyLength
, Ptr
);
246 if (EFI_ERROR (Status
)) {
255 // Overwrite the updating range data with
256 // the input buffer content
258 CopyMem (MyBuffer
+ Offset
, Buffer
, *NumBytes
);
261 // Try to keep the content of spare block
262 // Save spare block into a spare backup memory buffer (Sparebuffer)
264 SpareBufferSize
= FtwLiteDevice
->SpareAreaLength
;
265 SpareBuffer
= AllocatePool (SpareBufferSize
);
266 if (SpareBuffer
== NULL
) {
268 return EFI_OUT_OF_RESOURCES
;
272 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
273 MyLength
= FtwLiteDevice
->SizeOfSpareBlock
;
274 Status
= FtwLiteDevice
->FtwBackupFvb
->Read (
275 FtwLiteDevice
->FtwBackupFvb
,
276 FtwLiteDevice
->FtwSpareLba
+ Index
,
281 if (EFI_ERROR (Status
)) {
283 FreePool (SpareBuffer
);
290 // Write the memory buffer to spare block
291 // Don't forget to erase Flash first.
293 Status
= FtwEraseSpareBlock (FtwLiteDevice
);
295 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
296 MyLength
= FtwLiteDevice
->SizeOfSpareBlock
;
297 Status
= FtwLiteDevice
->FtwBackupFvb
->Write (
298 FtwLiteDevice
->FtwBackupFvb
,
299 FtwLiteDevice
->FtwSpareLba
+ Index
,
304 if (EFI_ERROR (Status
)) {
306 FreePool (SpareBuffer
);
318 // Set the SpareCompleteD in the FTW record,
320 MyOffset
= (UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
;
321 Status
= FtwUpdateFvState (
322 FtwLiteDevice
->FtwFvBlock
,
323 FtwLiteDevice
->FtwWorkSpaceLba
,
324 FtwLiteDevice
->FtwWorkSpaceBase
+ MyOffset
,
327 if (EFI_ERROR (Status
)) {
328 FreePool (SpareBuffer
);
332 Record
->SpareCompleted
= FTW_VALID_STATE
;
335 // Since the content has already backuped in spare block, the write is
336 // guaranteed to be completed with fault tolerant manner.
338 Status
= FtwWriteRecord (FtwLiteDevice
, Fvb
);
339 if (EFI_ERROR (Status
)) {
340 FreePool (SpareBuffer
);
345 FtwLiteDevice
->FtwLastRecord
= Record
;
348 // Restore spare backup buffer into spare block , if no failure happened during FtwWrite.
350 Status
= FtwEraseSpareBlock (FtwLiteDevice
);
352 for (Index
= 0; Index
< FtwLiteDevice
->NumberOfSpareBlock
; Index
+= 1) {
353 MyLength
= FtwLiteDevice
->SizeOfSpareBlock
;
354 Status
= FtwLiteDevice
->FtwBackupFvb
->Write (
355 FtwLiteDevice
->FtwBackupFvb
,
356 FtwLiteDevice
->FtwSpareLba
+ Index
,
361 if (EFI_ERROR (Status
)) {
362 FreePool (SpareBuffer
);
371 FreePool (SpareBuffer
);
375 "FtwLite: Write() success, (Lba:Offset)=(%lx:0x%x), NumBytes: 0x%x\n",
386 Write a record with fault tolerant mannaer.
387 Since the content has already backuped in spare block, the write is
388 guaranteed to be completed with fault tolerant manner.
391 @param FtwLiteDevice The private data of FTW_LITE driver
392 @param Fvb The FVB protocol that provides services for
393 reading, writing, and erasing the target block.
395 @retval EFI_SUCCESS The function completed successfully
396 @retval EFI_ABORTED The function could not complete successfully
401 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
402 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
406 EFI_FTW_LITE_RECORD
*Record
;
407 EFI_LBA WorkSpaceLbaOffset
;
411 // Spare Complete but Destination not complete,
412 // Recover the targt block with the spare block.
414 Record
= FtwLiteDevice
->FtwLastRecord
;
417 // IF target block is working block, THEN Flush Spare Block To Working Block;
418 // ELSE IF target block is boot block, THEN Flush Spare Block To boot Block;
419 // ELSE flush spare block to normal target block.ENDIF
421 if (IsInWorkingBlock (FtwLiteDevice
, Fvb
, Record
->Lba
)) {
423 // If target block is working block, Attention:
424 // it's required to set SPARE_COMPLETED to spare block.
426 WorkSpaceLbaOffset
= FtwLiteDevice
->FtwWorkSpaceLba
- FtwLiteDevice
->FtwWorkBlockLba
;
427 Offset
= (UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
;
428 Status
= FtwUpdateFvState (
429 FtwLiteDevice
->FtwBackupFvb
,
430 FtwLiteDevice
->FtwSpareLba
+ WorkSpaceLbaOffset
,
431 FtwLiteDevice
->FtwWorkSpaceBase
+ Offset
,
434 ASSERT_EFI_ERROR (Status
);
436 Status
= FlushSpareBlockToWorkingBlock (FtwLiteDevice
);
437 } else if (IsBootBlock (FtwLiteDevice
, Fvb
, Record
->Lba
)) {
441 Status
= FlushSpareBlockToBootBlock (FtwLiteDevice
);
444 // Update blocks other than working block or boot block
446 Status
= FlushSpareBlockToTargetBlock (FtwLiteDevice
, Fvb
, Record
->Lba
);
449 ASSERT_EFI_ERROR (Status
);
452 // Set WriteCompleted flag in record
454 Offset
= (UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
;
455 Status
= FtwUpdateFvState (
456 FtwLiteDevice
->FtwFvBlock
,
457 FtwLiteDevice
->FtwWorkSpaceLba
,
458 FtwLiteDevice
->FtwWorkSpaceBase
+ Offset
,
461 ASSERT_EFI_ERROR (Status
);
463 Record
->WriteCompleted
= FTW_VALID_STATE
;
469 Restarts a previously interrupted write. The caller must provide the
470 block protocol needed to complete the interrupted write.
473 @param FtwLiteDevice The private data of FTW_LITE driver
474 FvbHandle - The handle of FVB protocol that provides services for
475 reading, writing, and erasing the target block.
477 @retval EFI_SUCCESS The function completed successfully
478 @retval EFI_ACCESS_DENIED No pending writes exist
479 @retval EFI_NOT_FOUND FVB protocol not found by the handle
480 @retval EFI_ABORTED The function could not complete successfully
485 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
489 EFI_FTW_LITE_RECORD
*Record
;
490 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
491 EFI_DEV_PATH_PTR DevPathPtr
;
494 // Spare Completed but Destination not complete,
495 // Recover the targt block with the spare block.
497 Record
= FtwLiteDevice
->FtwLastRecord
;
500 // Only support memory mapped FVB device path by now.
502 DevPathPtr
.MemMap
= (MEMMAP_DEVICE_PATH
*) &Record
->DevPath
;
503 if (!((DevPathPtr
.MemMap
->Header
.Type
== HARDWARE_DEVICE_PATH
) && (DevPathPtr
.MemMap
->Header
.SubType
== HW_MEMMAP_DP
))
505 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: FVB Device Path is not memory mapped\n"));
509 Status
= GetFvbByAddress (DevPathPtr
.MemMap
->StartingAddress
, &Fvb
);
510 if (EFI_ERROR (Status
)) {
511 return EFI_NOT_FOUND
;
514 // Since the content has already backuped in spare block, the write is
515 // guaranteed to be completed with fault tolerant manner.
517 Status
= FtwWriteRecord (FtwLiteDevice
, Fvb
);
518 DEBUG ((EFI_D_FTW_INFO
, "FtwLite: Restart() - %r\n", Status
));
521 FtwLiteDevice
->FtwLastRecord
= Record
;
525 // This is restart, no need to keep spareblock content.
527 FtwEraseSpareBlock (FtwLiteDevice
);
534 Aborts all previous allocated writes.
537 @param FtwLiteDevice The private data of FTW_LITE driver
539 @retval EFI_SUCCESS The function completed successfully
540 @retval EFI_ABORTED The function could not complete successfully.
541 @retval EFI_NOT_FOUND No allocated writes exist.
546 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
552 if (FtwLiteDevice
->FtwLastRecord
->WriteCompleted
== FTW_VALID_STATE
) {
553 return EFI_NOT_FOUND
;
556 // Update the complete state of the header as VALID and abort.
558 Offset
= (UINT8
*) FtwLiteDevice
->FtwLastRecord
- FtwLiteDevice
->FtwWorkSpace
;
559 Status
= FtwUpdateFvState (
560 FtwLiteDevice
->FtwFvBlock
,
561 FtwLiteDevice
->FtwWorkSpaceLba
,
562 FtwLiteDevice
->FtwWorkSpaceBase
+ Offset
,
565 if (EFI_ERROR (Status
)) {
569 FtwLiteDevice
->FtwLastRecord
->WriteCompleted
= FTW_VALID_STATE
;
571 Status
= FtwGetLastRecord (FtwLiteDevice
, &FtwLiteDevice
->FtwLastRecord
);
574 // Erase the spare block
576 Status
= FtwEraseSpareBlock (FtwLiteDevice
);
578 DEBUG ((EFI_D_FTW_INFO
, "FtwLite: Abort() success \n"));
583 This function is the entry point of the Fault Tolerant Write driver.
586 @param ImageHandle EFI_HANDLE: A handle for the image that is initializing
588 @param SystemTable EFI_SYSTEM_TABLE: A pointer to the EFI system table
590 @retval EFI_SUCCESS FTW has finished the initialization
591 @retval EFI_ABORTED FTW initialization error
597 IN EFI_HANDLE ImageHandle
,
598 IN EFI_SYSTEM_TABLE
*SystemTable
601 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
603 EFI_HANDLE
*HandleBuffer
;
605 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
606 EFI_PHYSICAL_ADDRESS BaseAddress
;
607 EFI_FTW_LITE_DEVICE
*FtwLiteDevice
;
608 EFI_FTW_LITE_RECORD
*Record
;
612 EFI_FV_BLOCK_MAP_ENTRY
*FvbMapEntry
;
616 // Allocate Private data of this driver,
617 // INCLUDING THE FtwWorkSpace[FTW_WORK_SPACE_SIZE].
619 FtwLiteDevice
= NULL
;
620 FtwLiteDevice
= AllocatePool (sizeof (EFI_FTW_LITE_DEVICE
) + FTW_WORK_SPACE_SIZE
);
621 if (FtwLiteDevice
!= NULL
) {
622 Status
= EFI_SUCCESS
;
624 Status
= EFI_OUT_OF_RESOURCES
;
627 ASSERT_EFI_ERROR (Status
);
629 ZeroMem (FtwLiteDevice
, sizeof (EFI_FTW_LITE_DEVICE
));
630 FtwLiteDevice
->Signature
= FTW_LITE_DEVICE_SIGNATURE
;
633 // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.
635 FtwLiteDevice
->FtwWorkSpace
= (UINT8
*) (FtwLiteDevice
+ 1);
636 FtwLiteDevice
->FtwWorkSpaceSize
= FTW_WORK_SPACE_SIZE
;
638 FtwLiteDevice
->FtwWorkSpace
,
639 FtwLiteDevice
->FtwWorkSpaceSize
,
642 FtwLiteDevice
->FtwWorkSpaceHeader
= (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*) FtwLiteDevice
->FtwWorkSpace
;
644 FtwLiteDevice
->FtwLastRecord
= NULL
;
646 FtwLiteDevice
->WorkSpaceAddress
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageFtwWorkingBase
);
647 FtwLiteDevice
->WorkSpaceLength
= (UINTN
) PcdGet32 (PcdFlashNvStorageFtwWorkingSize
);
649 FtwLiteDevice
->SpareAreaAddress
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageFtwSpareBase
);
650 FtwLiteDevice
->SpareAreaLength
= (UINTN
) PcdGet32 (PcdFlashNvStorageFtwSpareSize
);
652 ASSERT ((FtwLiteDevice
->WorkSpaceLength
!= 0) && (FtwLiteDevice
->SpareAreaLength
!= 0));
655 // Locate FVB protocol
657 Status
= gBS
->LocateHandleBuffer (
659 &gEfiFirmwareVolumeBlockProtocolGuid
,
664 ASSERT_EFI_ERROR (Status
);
666 ASSERT (HandleCount
> 0);
668 FtwLiteDevice
->FtwFvBlock
= NULL
;
669 FtwLiteDevice
->FtwBackupFvb
= NULL
;
670 FtwLiteDevice
->FtwWorkSpaceLba
= (EFI_LBA
) (-1);
671 FtwLiteDevice
->FtwSpareLba
= (EFI_LBA
) (-1);
672 for (Index
= 0; Index
< HandleCount
; Index
+= 1) {
673 Status
= gBS
->HandleProtocol (
675 &gEfiFirmwareVolumeBlockProtocolGuid
,
678 ASSERT_EFI_ERROR (Status
);
680 Status
= Fvb
->GetPhysicalAddress (Fvb
, &BaseAddress
);
681 if (EFI_ERROR (Status
)) {
685 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) BaseAddress
);
687 if ((FtwLiteDevice
->WorkSpaceAddress
>= BaseAddress
) &&
688 (FtwLiteDevice
->WorkSpaceAddress
<= (BaseAddress
+ FwVolHeader
->FvLength
))
690 FtwLiteDevice
->FtwFvBlock
= Fvb
;
692 // To get the LBA of work space
694 if ((FwVolHeader
->FvLength
) > (FwVolHeader
->HeaderLength
)) {
696 // FV may have multiple types of BlockLength
698 FvbMapEntry
= &FwVolHeader
->BlockMap
[0];
699 while (!((FvbMapEntry
->NumBlocks
== 0) && (FvbMapEntry
->Length
== 0))) {
700 for (LbaIndex
= 1; LbaIndex
<= FvbMapEntry
->NumBlocks
; LbaIndex
+= 1) {
701 if (FtwLiteDevice
->WorkSpaceAddress
< (BaseAddress
+ FvbMapEntry
->Length
* LbaIndex
)) {
702 FtwLiteDevice
->FtwWorkSpaceLba
= LbaIndex
- 1;
704 // Get the Work space size and Base(Offset)
706 FtwLiteDevice
->FtwWorkSpaceSize
= FtwLiteDevice
->WorkSpaceLength
;
707 FtwLiteDevice
->FtwWorkSpaceBase
= (UINTN
) (FtwLiteDevice
->WorkSpaceAddress
- (BaseAddress
+ FvbMapEntry
->Length
* (LbaIndex
- 1)));
722 if ((FtwLiteDevice
->SpareAreaAddress
>= BaseAddress
) &&
723 (FtwLiteDevice
->SpareAreaAddress
< (BaseAddress
+ FwVolHeader
->FvLength
))
725 FtwLiteDevice
->FtwBackupFvb
= Fvb
;
727 // To get the LBA of spare
729 if ((FwVolHeader
->FvLength
) > (FwVolHeader
->HeaderLength
)) {
731 // FV may have multiple types of BlockLength
733 FvbMapEntry
= &FwVolHeader
->BlockMap
[0];
734 while (!((FvbMapEntry
->NumBlocks
== 0) && (FvbMapEntry
->Length
== 0))) {
735 for (LbaIndex
= 1; LbaIndex
<= FvbMapEntry
->NumBlocks
; LbaIndex
+= 1) {
736 if (FtwLiteDevice
->SpareAreaAddress
< (BaseAddress
+ FvbMapEntry
->Length
* LbaIndex
)) {
738 // Get the NumberOfSpareBlock and SizeOfSpareBlock
740 FtwLiteDevice
->FtwSpareLba
= LbaIndex
- 1;
741 FtwLiteDevice
->SizeOfSpareBlock
= FvbMapEntry
->Length
;
742 FtwLiteDevice
->NumberOfSpareBlock
= FtwLiteDevice
->SpareAreaLength
/ FtwLiteDevice
->SizeOfSpareBlock
;
744 // Check the range of spare area to make sure that it's in FV range
746 ASSERT ((FtwLiteDevice
->FtwSpareLba
+ FtwLiteDevice
->NumberOfSpareBlock
) <= FvbMapEntry
->NumBlocks
);
760 // Calculate the start LBA of working block. Working block is an area which
761 // contains working space in its last block and has the same size as spare
762 // block, unless there are not enough blocks before the block that contains
765 FtwLiteDevice
->FtwWorkBlockLba
= FtwLiteDevice
->FtwWorkSpaceLba
- FtwLiteDevice
->NumberOfSpareBlock
+ 1;
766 if ((INT64
) (FtwLiteDevice
->FtwWorkBlockLba
) < 0) {
767 FtwLiteDevice
->FtwWorkBlockLba
= 0;
770 if ((FtwLiteDevice
->FtwFvBlock
== NULL
) ||
771 (FtwLiteDevice
->FtwBackupFvb
== NULL
) ||
772 (FtwLiteDevice
->FtwWorkSpaceLba
== (EFI_LBA
) (-1)) ||
773 (FtwLiteDevice
->FtwSpareLba
== (EFI_LBA
) (-1))
775 DEBUG ((EFI_D_ERROR
, "FtwLite: Working or spare FVB not ready\n"));
776 ASSERT_EFI_ERROR (Status
);
779 // Refresh workspace data from working block
781 Status
= WorkSpaceRefresh (FtwLiteDevice
);
782 ASSERT_EFI_ERROR (Status
);
785 // If the working block workspace is not valid, try the spare block
787 if (!IsValidWorkSpace (FtwLiteDevice
->FtwWorkSpaceHeader
)) {
788 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Workspace invalid, read from backup\n"));
790 // Read from spare block
792 Length
= FtwLiteDevice
->FtwWorkSpaceSize
;
793 Status
= FtwLiteDevice
->FtwBackupFvb
->Read (
794 FtwLiteDevice
->FtwBackupFvb
,
795 FtwLiteDevice
->FtwSpareLba
,
796 FtwLiteDevice
->FtwWorkSpaceBase
,
798 FtwLiteDevice
->FtwWorkSpace
800 ASSERT_EFI_ERROR (Status
);
803 // If spare block is valid, then replace working block content.
805 if (IsValidWorkSpace (FtwLiteDevice
->FtwWorkSpaceHeader
)) {
806 Status
= FlushSpareBlockToWorkingBlock (FtwLiteDevice
);
807 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Restart working block in Init() - %r\n", Status
));
808 ASSERT_EFI_ERROR (Status
);
810 FtwAbort (FtwLiteDevice
);
812 // Refresh work space.
814 Status
= WorkSpaceRefresh (FtwLiteDevice
);
815 if (EFI_ERROR (Status
)) {
819 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Both are invalid, init workspace\n"));
821 // If both are invalid, then initialize work space.
824 FtwLiteDevice
->FtwWorkSpace
,
825 FtwLiteDevice
->FtwWorkSpaceSize
,
828 InitWorkSpaceHeader (FtwLiteDevice
->FtwWorkSpaceHeader
);
830 // Write to work space on the working block
832 Length
= FtwLiteDevice
->FtwWorkSpaceSize
;
833 Status
= FtwLiteDevice
->FtwFvBlock
->Write (
834 FtwLiteDevice
->FtwFvBlock
,
835 FtwLiteDevice
->FtwWorkSpaceLba
,
836 FtwLiteDevice
->FtwWorkSpaceBase
,
838 FtwLiteDevice
->FtwWorkSpace
840 if (EFI_ERROR (Status
)) {
846 // Hook the protocol API
848 FtwLiteDevice
->FtwLiteInstance
.Write
= FtwLiteWrite
;
851 // Install protocol interface
853 Status
= gBS
->InstallProtocolInterface (
854 &FtwLiteDevice
->Handle
,
855 &gEfiFaultTolerantWriteLiteProtocolGuid
,
856 EFI_NATIVE_INTERFACE
,
857 &FtwLiteDevice
->FtwLiteInstance
859 if (EFI_ERROR (Status
)) {
863 // If (!SpareCompleted) THEN Abort to rollback.
865 if ((FtwLiteDevice
->FtwLastRecord
->WriteAllocated
== FTW_VALID_STATE
) &&
866 (FtwLiteDevice
->FtwLastRecord
->SpareCompleted
!= FTW_VALID_STATE
)
868 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Init.. record not SpareCompleted, abort()\n"));
869 FtwAbort (FtwLiteDevice
);
872 // if (SpareCompleted) THEN Restart to fault tolerant write.
874 if ((FtwLiteDevice
->FtwLastRecord
->SpareCompleted
== FTW_VALID_STATE
) &&
875 (FtwLiteDevice
->FtwLastRecord
->WriteCompleted
!= FTW_VALID_STATE
)
878 Status
= FtwRestart (FtwLiteDevice
);
879 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Restart last write - %r\n", Status
));
880 if (EFI_ERROR (Status
)) {
885 // To check the workspace buffer behind last records is EMPTY or not.
886 // If it's not EMPTY, FTW_LITE also need to call reclaim().
888 Record
= FtwLiteDevice
->FtwLastRecord
;
889 Offset
= (UINT8
*) Record
- FtwLiteDevice
->FtwWorkSpace
;
890 if (FtwLiteDevice
->FtwWorkSpace
[Offset
] != FTW_ERASED_BYTE
) {
891 Offset
+= WRITE_TOTAL_SIZE
;
894 if (!IsErasedFlashBuffer (
896 FtwLiteDevice
->FtwWorkSpace
+ Offset
,
897 FtwLiteDevice
->FtwWorkSpaceSize
- Offset
899 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Workspace is dirty, call reclaim...\n"));
900 Status
= FtwReclaimWorkSpace (FtwLiteDevice
);
901 if (EFI_ERROR (Status
)) {
902 DEBUG ((EFI_D_FTW_LITE
, "FtwLite: Workspace reclaim - %r\n", Status
));