3 These are the common Fault Tolerant Write (FTW) functions that are shared
4 by DXE FTW driver and SMM FTW driver.
6 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "FaultTolerantWrite.h"
14 // Fault Tolerant Write Protocol API
18 Query the largest block that may be updated in a fault tolerant manner.
21 @param This The pointer to this protocol instance.
22 @param BlockSize A pointer to a caller allocated UINTN that is updated to
23 indicate the size of the largest block that can be updated.
25 @return EFI_SUCCESS The function completed successfully
31 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
35 EFI_FTW_DEVICE
*FtwDevice
;
37 if (!FeaturePcdGet (PcdFullFtwServiceEnable
)) {
38 return EFI_UNSUPPORTED
;
41 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
43 *BlockSize
= FtwDevice
->SpareAreaLength
;
49 Allocates space for the protocol to maintain information about writes.
50 Since writes must be completed in a fault tolerant manner and multiple
51 updates will require more resources to be successful, this function
52 enables the protocol to ensure that enough space exists to track
53 information about the upcoming writes.
55 All writes must be completed or aborted before another fault tolerant write can occur.
57 @param This The pointer to this protocol instance.
58 @param CallerId The GUID identifying the write.
59 @param PrivateDataSize The size of the caller's private data
60 that must be recorded for each write.
61 @param NumberOfWrites The number of fault tolerant block writes
62 that will need to occur.
64 @return EFI_SUCCESS The function completed successfully
65 @retval EFI_ABORTED The function could not complete successfully.
66 @retval EFI_ACCESS_DENIED All allocated writes have not been completed.
72 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
73 IN EFI_GUID
*CallerId
,
74 IN UINTN PrivateDataSize
,
75 IN UINTN NumberOfWrites
80 EFI_FTW_DEVICE
*FtwDevice
;
81 EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
;
83 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
85 Status
= WorkSpaceRefresh (FtwDevice
);
86 if (EFI_ERROR (Status
)) {
91 // Check if there is enough space for the coming allocation
93 if (FTW_WRITE_TOTAL_SIZE (NumberOfWrites
, PrivateDataSize
) > FtwDevice
->FtwWorkSpaceHeader
->WriteQueueSize
) {
94 DEBUG ((DEBUG_ERROR
, "Ftw: Allocate() request exceed Workspace, Caller: %g\n", CallerId
));
95 return EFI_BUFFER_TOO_SMALL
;
99 // Find the last write header and record.
100 // If the FtwHeader is complete, skip the completed last write header/records
102 FtwHeader
= FtwDevice
->FtwLastWriteHeader
;
105 // Previous write has not completed, access denied.
107 if ((FtwHeader
->HeaderAllocated
== FTW_VALID_STATE
) || (FtwHeader
->WritesAllocated
== FTW_VALID_STATE
)) {
108 return EFI_ACCESS_DENIED
;
112 // If workspace is not enough, then reclaim workspace
114 Offset
= (UINT8
*)FtwHeader
- (UINT8
*)FtwDevice
->FtwWorkSpace
;
115 if (Offset
+ FTW_WRITE_TOTAL_SIZE (NumberOfWrites
, PrivateDataSize
) > FtwDevice
->FtwWorkSpaceSize
) {
116 Status
= FtwReclaimWorkSpace (FtwDevice
, TRUE
);
117 if (EFI_ERROR (Status
)) {
121 FtwHeader
= FtwDevice
->FtwLastWriteHeader
;
125 // Prepare FTW write header,
126 // overwrite the buffer and write to workspace.
128 FtwHeader
->WritesAllocated
= FTW_INVALID_STATE
;
129 FtwHeader
->Complete
= FTW_INVALID_STATE
;
130 CopyMem (&FtwHeader
->CallerId
, CallerId
, sizeof (EFI_GUID
));
131 FtwHeader
->NumberOfWrites
= NumberOfWrites
;
132 FtwHeader
->PrivateDataSize
= PrivateDataSize
;
133 FtwHeader
->HeaderAllocated
= FTW_VALID_STATE
;
135 Status
= WriteWorkSpaceData (
136 FtwDevice
->FtwFvBlock
,
137 FtwDevice
->WorkBlockSize
,
138 FtwDevice
->FtwWorkSpaceLba
,
139 FtwDevice
->FtwWorkSpaceBase
+ Offset
,
140 sizeof (EFI_FAULT_TOLERANT_WRITE_HEADER
),
143 if (EFI_ERROR (Status
)) {
148 // Update Header->WriteAllocated as VALID
150 Status
= FtwUpdateFvState (
151 FtwDevice
->FtwFvBlock
,
152 FtwDevice
->WorkBlockSize
,
153 FtwDevice
->FtwWorkSpaceLba
,
154 FtwDevice
->FtwWorkSpaceBase
+ Offset
,
157 if (EFI_ERROR (Status
)) {
163 "Ftw: Allocate() success, Caller:%g, # %d\n",
172 Write a record with fault tolerant manner.
173 Since the content has already backuped in spare block, the write is
174 guaranteed to be completed with fault tolerant manner.
176 @param This The pointer to this protocol instance.
177 @param Fvb The FVB protocol that provides services for
178 reading, writing, and erasing the target block.
179 @param BlockSize The size of the block.
181 @retval EFI_SUCCESS The function completed successfully
182 @retval EFI_ABORTED The function could not complete successfully
187 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
188 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
193 EFI_FTW_DEVICE
*FtwDevice
;
194 EFI_FAULT_TOLERANT_WRITE_HEADER
*Header
;
195 EFI_FAULT_TOLERANT_WRITE_RECORD
*Record
;
197 UINTN NumberOfWriteBlocks
;
199 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
202 // Spare Complete but Destination not complete,
203 // Recover the target block with the spare block.
205 Header
= FtwDevice
->FtwLastWriteHeader
;
206 Record
= FtwDevice
->FtwLastWriteRecord
;
209 // IF target block is working block, THEN Flush Spare Block To Working Block;
210 // ELSE flush spare block to target block, which may be boot block after all.
212 if (IsWorkingBlock (FtwDevice
, Fvb
, Record
->Lba
)) {
214 // If target block is working block,
215 // it also need to set SPARE_COMPLETED to spare block.
217 Offset
= (UINT8
*)Record
- FtwDevice
->FtwWorkSpace
;
218 Status
= FtwUpdateFvState (
219 FtwDevice
->FtwBackupFvb
,
220 FtwDevice
->SpareBlockSize
,
221 FtwDevice
->FtwSpareLba
+ FtwDevice
->FtwWorkSpaceLbaInSpare
,
222 FtwDevice
->FtwWorkSpaceBaseInSpare
+ Offset
,
225 if (EFI_ERROR (Status
)) {
229 Status
= FlushSpareBlockToWorkingBlock (FtwDevice
);
230 } else if (IsBootBlock (FtwDevice
, Fvb
)) {
234 Status
= FlushSpareBlockToBootBlock (FtwDevice
);
237 // Update blocks other than working block or boot block
239 NumberOfWriteBlocks
= FTW_BLOCKS ((UINTN
)(Record
->Offset
+ Record
->Length
), BlockSize
);
240 Status
= FlushSpareBlockToTargetBlock (FtwDevice
, Fvb
, Record
->Lba
, BlockSize
, NumberOfWriteBlocks
);
243 if (EFI_ERROR (Status
)) {
248 // Record the DestionationComplete in record
250 Offset
= (UINT8
*)Record
- FtwDevice
->FtwWorkSpace
;
251 Status
= FtwUpdateFvState (
252 FtwDevice
->FtwFvBlock
,
253 FtwDevice
->WorkBlockSize
,
254 FtwDevice
->FtwWorkSpaceLba
,
255 FtwDevice
->FtwWorkSpaceBase
+ Offset
,
258 if (EFI_ERROR (Status
)) {
262 Record
->DestinationComplete
= FTW_VALID_STATE
;
265 // If this is the last Write in these write sequence,
266 // set the complete flag of write header.
268 if (IsLastRecordOfWrites (Header
, Record
)) {
269 Offset
= (UINT8
*)Header
- FtwDevice
->FtwWorkSpace
;
270 Status
= FtwUpdateFvState (
271 FtwDevice
->FtwFvBlock
,
272 FtwDevice
->WorkBlockSize
,
273 FtwDevice
->FtwWorkSpaceLba
,
274 FtwDevice
->FtwWorkSpaceBase
+ Offset
,
277 Header
->Complete
= FTW_VALID_STATE
;
278 if (EFI_ERROR (Status
)) {
287 Starts a target block update. This function will record data about write
288 in fault tolerant storage and will complete the write in a recoverable
289 manner, ensuring at all times that either the original contents or
290 the modified contents are available.
292 @param This The pointer to this protocol instance.
293 @param Lba The logical block address of the target block.
294 @param Offset The offset within the target block to place the data.
295 @param Length The number of bytes to write to the target block.
296 @param PrivateData A pointer to private data that the caller requires to
297 complete any pending writes in the event of a fault.
298 @param FvBlockHandle The handle of FVB protocol that provides services for
299 reading, writing, and erasing the target block.
300 @param Buffer The data to write.
302 @retval EFI_SUCCESS The function completed successfully
303 @retval EFI_ABORTED The function could not complete successfully.
304 @retval EFI_BAD_BUFFER_SIZE The input data can't fit within the spare block.
305 Offset + *NumBytes > SpareAreaLength.
306 @retval EFI_ACCESS_DENIED No writes have been allocated.
307 @retval EFI_OUT_OF_RESOURCES Cannot allocate enough memory resource.
308 @retval EFI_NOT_FOUND Cannot find FVB protocol by handle.
314 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
318 IN VOID
*PrivateData
,
319 IN EFI_HANDLE FvBlockHandle
,
324 EFI_FTW_DEVICE
*FtwDevice
;
325 EFI_FAULT_TOLERANT_WRITE_HEADER
*Header
;
326 EFI_FAULT_TOLERANT_WRITE_RECORD
*Record
;
327 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
332 UINTN SpareBufferSize
;
336 EFI_PHYSICAL_ADDRESS FvbPhysicalAddress
;
338 UINTN NumberOfBlocks
;
339 UINTN NumberOfWriteBlocks
;
342 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
344 Status
= WorkSpaceRefresh (FtwDevice
);
345 if (EFI_ERROR (Status
)) {
349 Header
= FtwDevice
->FtwLastWriteHeader
;
350 Record
= FtwDevice
->FtwLastWriteRecord
;
352 if (IsErasedFlashBuffer ((UINT8
*)Header
, sizeof (EFI_FAULT_TOLERANT_WRITE_HEADER
))) {
353 if (PrivateData
== NULL
) {
355 // Ftw Write Header is not allocated.
356 // No additional private data, the private data size is zero. Number of record can be set to 1.
358 Status
= FtwAllocate (This
, &gEfiCallerIdGuid
, 0, 1);
359 if (EFI_ERROR (Status
)) {
364 // Ftw Write Header is not allocated
365 // Additional private data is not NULL, the private data size can't be determined.
367 DEBUG ((DEBUG_ERROR
, "Ftw: no allocates space for write record!\n"));
368 DEBUG ((DEBUG_ERROR
, "Ftw: Allocate service should be called before Write service!\n"));
369 return EFI_NOT_READY
;
374 // If Record is out of the range of Header, return access denied.
376 if (((UINTN
)Record
- (UINTN
)Header
) > FTW_WRITE_TOTAL_SIZE (Header
->NumberOfWrites
- 1, Header
->PrivateDataSize
)) {
377 return EFI_ACCESS_DENIED
;
381 // Check the COMPLETE flag of last write header
383 if (Header
->Complete
== FTW_VALID_STATE
) {
384 return EFI_ACCESS_DENIED
;
387 if (Record
->DestinationComplete
== FTW_VALID_STATE
) {
388 return EFI_ACCESS_DENIED
;
391 if ((Record
->SpareComplete
== FTW_VALID_STATE
) && (Record
->DestinationComplete
!= FTW_VALID_STATE
)) {
392 return EFI_NOT_READY
;
396 // Get the FVB protocol by handle
398 Status
= FtwGetFvbByHandle (FvBlockHandle
, &Fvb
);
399 if (EFI_ERROR (Status
)) {
400 return EFI_NOT_FOUND
;
403 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbPhysicalAddress
);
404 if (EFI_ERROR (Status
)) {
405 DEBUG ((DEBUG_ERROR
, "Ftw: Write(), Get FVB physical address - %r\n", Status
));
410 // Now, one FVB has one type of BlockSize.
412 Status
= Fvb
->GetBlockSize (Fvb
, 0, &BlockSize
, &NumberOfBlocks
);
413 if (EFI_ERROR (Status
)) {
414 DEBUG ((DEBUG_ERROR
, "Ftw: Write(), Get block size - %r\n", Status
));
418 NumberOfWriteBlocks
= FTW_BLOCKS (Offset
+ Length
, BlockSize
);
419 DEBUG ((DEBUG_INFO
, "Ftw: Write(), BlockSize - 0x%x, NumberOfWriteBlock - 0x%x\n", BlockSize
, NumberOfWriteBlocks
));
420 WriteLength
= NumberOfWriteBlocks
* BlockSize
;
423 // Check if the input data can fit within the spare block.
425 if (WriteLength
> FtwDevice
->SpareAreaLength
) {
426 return EFI_BAD_BUFFER_SIZE
;
430 // Set BootBlockUpdate FLAG if it's updating boot block.
432 if (IsBootBlock (FtwDevice
, Fvb
)) {
433 Record
->BootBlockUpdate
= FTW_VALID_STATE
;
435 // Boot Block and Spare Block should have same block size and block numbers.
437 ASSERT ((BlockSize
== FtwDevice
->SpareBlockSize
) && (NumberOfWriteBlocks
== FtwDevice
->NumberOfSpareBlock
));
441 // Write the record to the work space.
444 Record
->Offset
= Offset
;
445 Record
->Length
= Length
;
446 Record
->RelativeOffset
= (INT64
)(FvbPhysicalAddress
+ (UINTN
)Lba
* BlockSize
) - (INT64
)FtwDevice
->SpareAreaAddress
;
447 if (PrivateData
!= NULL
) {
448 CopyMem ((Record
+ 1), PrivateData
, (UINTN
)Header
->PrivateDataSize
);
451 MyOffset
= (UINT8
*)Record
- FtwDevice
->FtwWorkSpace
;
452 MyLength
= FTW_RECORD_SIZE (Header
->PrivateDataSize
);
454 Status
= WriteWorkSpaceData (
455 FtwDevice
->FtwFvBlock
,
456 FtwDevice
->WorkBlockSize
,
457 FtwDevice
->FtwWorkSpaceLba
,
458 FtwDevice
->FtwWorkSpaceBase
+ MyOffset
,
462 if (EFI_ERROR (Status
)) {
467 // Record has written to working block, then do the data.
470 // Allocate a memory buffer
472 MyBufferSize
= WriteLength
;
473 MyBuffer
= AllocatePool (MyBufferSize
);
474 if (MyBuffer
== NULL
) {
475 return EFI_OUT_OF_RESOURCES
;
479 // Read all original data from target block to memory buffer
482 for (Index
= 0; Index
< NumberOfWriteBlocks
; Index
+= 1) {
483 MyLength
= BlockSize
;
484 Status
= Fvb
->Read (Fvb
, Lba
+ Index
, 0, &MyLength
, Ptr
);
485 if (EFI_ERROR (Status
)) {
494 // Overwrite the updating range data with
495 // the input buffer content
497 CopyMem (MyBuffer
+ Offset
, Buffer
, Length
);
500 // Try to keep the content of spare block
501 // Save spare block into a spare backup memory buffer (Sparebuffer)
503 SpareBufferSize
= FtwDevice
->SpareAreaLength
;
504 SpareBuffer
= AllocatePool (SpareBufferSize
);
505 if (SpareBuffer
== NULL
) {
507 return EFI_OUT_OF_RESOURCES
;
511 for (Index
= 0; Index
< FtwDevice
->NumberOfSpareBlock
; Index
+= 1) {
512 MyLength
= FtwDevice
->SpareBlockSize
;
513 Status
= FtwDevice
->FtwBackupFvb
->Read (
514 FtwDevice
->FtwBackupFvb
,
515 FtwDevice
->FtwSpareLba
+ Index
,
520 if (EFI_ERROR (Status
)) {
522 FreePool (SpareBuffer
);
530 // Write the memory buffer to spare block
531 // Do not assume Spare Block and Target Block have same block size
533 Status
= FtwEraseSpareBlock (FtwDevice
);
534 if (EFI_ERROR (Status
)) {
536 FreePool (SpareBuffer
);
541 for (Index
= 0; MyBufferSize
> 0; Index
+= 1) {
542 if (MyBufferSize
> FtwDevice
->SpareBlockSize
) {
543 MyLength
= FtwDevice
->SpareBlockSize
;
545 MyLength
= MyBufferSize
;
548 Status
= FtwDevice
->FtwBackupFvb
->Write (
549 FtwDevice
->FtwBackupFvb
,
550 FtwDevice
->FtwSpareLba
+ Index
,
555 if (EFI_ERROR (Status
)) {
557 FreePool (SpareBuffer
);
562 MyBufferSize
-= MyLength
;
571 // Set the SpareComplete in the FTW record,
573 MyOffset
= (UINT8
*)Record
- FtwDevice
->FtwWorkSpace
;
574 Status
= FtwUpdateFvState (
575 FtwDevice
->FtwFvBlock
,
576 FtwDevice
->WorkBlockSize
,
577 FtwDevice
->FtwWorkSpaceLba
,
578 FtwDevice
->FtwWorkSpaceBase
+ MyOffset
,
581 if (EFI_ERROR (Status
)) {
582 FreePool (SpareBuffer
);
586 Record
->SpareComplete
= FTW_VALID_STATE
;
589 // Since the content has already backuped in spare block, the write is
590 // guaranteed to be completed with fault tolerant manner.
592 Status
= FtwWriteRecord (This
, Fvb
, BlockSize
);
593 if (EFI_ERROR (Status
)) {
594 FreePool (SpareBuffer
);
599 // Restore spare backup buffer into spare block , if no failure happened during FtwWrite.
601 Status
= FtwEraseSpareBlock (FtwDevice
);
602 if (EFI_ERROR (Status
)) {
603 FreePool (SpareBuffer
);
608 for (Index
= 0; Index
< FtwDevice
->NumberOfSpareBlock
; Index
+= 1) {
609 MyLength
= FtwDevice
->SpareBlockSize
;
610 Status
= FtwDevice
->FtwBackupFvb
->Write (
611 FtwDevice
->FtwBackupFvb
,
612 FtwDevice
->FtwSpareLba
+ Index
,
617 if (EFI_ERROR (Status
)) {
618 FreePool (SpareBuffer
);
628 FreePool (SpareBuffer
);
632 "Ftw: Write() success, (Lba:Offset)=(%lx:0x%x), Length: 0x%x\n",
642 Restarts a previously interrupted write. The caller must provide the
643 block protocol needed to complete the interrupted write.
645 @param This The pointer to this protocol instance.
646 @param FvBlockHandle The handle of FVB protocol that provides services for
647 reading, writing, and erasing the target block.
649 @retval EFI_SUCCESS The function completed successfully
650 @retval EFI_ACCESS_DENIED No pending writes exist
651 @retval EFI_NOT_FOUND FVB protocol not found by the handle
652 @retval EFI_ABORTED The function could not complete successfully
658 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
659 IN EFI_HANDLE FvBlockHandle
663 EFI_FTW_DEVICE
*FtwDevice
;
664 EFI_FAULT_TOLERANT_WRITE_HEADER
*Header
;
665 EFI_FAULT_TOLERANT_WRITE_RECORD
*Record
;
666 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
668 UINTN NumberOfBlocks
;
670 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
672 Status
= WorkSpaceRefresh (FtwDevice
);
673 if (EFI_ERROR (Status
)) {
677 Header
= FtwDevice
->FtwLastWriteHeader
;
678 Record
= FtwDevice
->FtwLastWriteRecord
;
681 // Spare Complete but Destination not complete,
682 // Recover the targt block with the spare block.
684 Status
= FtwGetFvbByHandle (FvBlockHandle
, &Fvb
);
685 if (EFI_ERROR (Status
)) {
686 return EFI_NOT_FOUND
;
690 // Now, one FVB has one type of BlockSize
692 Status
= Fvb
->GetBlockSize (Fvb
, 0, &BlockSize
, &NumberOfBlocks
);
693 if (EFI_ERROR (Status
)) {
694 DEBUG ((DEBUG_ERROR
, "Ftw: Restart(), Get block size - %r\n", Status
));
699 // Check the COMPLETE flag of last write header
701 if (Header
->Complete
== FTW_VALID_STATE
) {
702 return EFI_ACCESS_DENIED
;
706 // Check the flags of last write record
708 if (Record
->DestinationComplete
== FTW_VALID_STATE
) {
709 return EFI_ACCESS_DENIED
;
712 if ((Record
->SpareComplete
!= FTW_VALID_STATE
)) {
717 // Since the content has already backuped in spare block, the write is
718 // guaranteed to be completed with fault tolerant manner.
720 Status
= FtwWriteRecord (This
, Fvb
, BlockSize
);
721 if (EFI_ERROR (Status
)) {
727 // This is restart, no need to keep spareblock content.
729 Status
= FtwEraseSpareBlock (FtwDevice
);
730 if (EFI_ERROR (Status
)) {
734 DEBUG ((DEBUG_INFO
, "%a(): success\n", __FUNCTION__
));
739 Aborts all previous allocated writes.
741 @param This The pointer to this protocol instance.
743 @retval EFI_SUCCESS The function completed successfully
744 @retval EFI_ABORTED The function could not complete successfully.
745 @retval EFI_NOT_FOUND No allocated writes exist.
751 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
756 EFI_FTW_DEVICE
*FtwDevice
;
758 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
760 Status
= WorkSpaceRefresh (FtwDevice
);
761 if (EFI_ERROR (Status
)) {
765 if (FtwDevice
->FtwLastWriteHeader
->HeaderAllocated
!= FTW_VALID_STATE
) {
766 return EFI_NOT_FOUND
;
769 if (FtwDevice
->FtwLastWriteHeader
->Complete
== FTW_VALID_STATE
) {
770 return EFI_NOT_FOUND
;
774 // Update the complete state of the header as VALID and abort.
776 Offset
= (UINT8
*)FtwDevice
->FtwLastWriteHeader
- FtwDevice
->FtwWorkSpace
;
777 Status
= FtwUpdateFvState (
778 FtwDevice
->FtwFvBlock
,
779 FtwDevice
->WorkBlockSize
,
780 FtwDevice
->FtwWorkSpaceLba
,
781 FtwDevice
->FtwWorkSpaceBase
+ Offset
,
784 if (EFI_ERROR (Status
)) {
788 FtwDevice
->FtwLastWriteHeader
->Complete
= FTW_VALID_STATE
;
790 DEBUG ((DEBUG_INFO
, "%a(): success\n", __FUNCTION__
));
795 Starts a target block update. This records information about the write
796 in fault tolerant storage and will complete the write in a recoverable
797 manner, ensuring at all times that either the original contents or
798 the modified contents are available.
800 @param This The pointer to this protocol instance.
801 @param CallerId The GUID identifying the last write.
802 @param Lba The logical block address of the last write.
803 @param Offset The offset within the block of the last write.
804 @param Length The length of the last write.
805 @param PrivateDataSize bytes from the private data
806 stored for this write.
807 @param PrivateData A pointer to a buffer. The function will copy
808 @param Complete A Boolean value with TRUE indicating
809 that the write was completed.
811 @retval EFI_SUCCESS The function completed successfully
812 @retval EFI_ABORTED The function could not complete successfully
813 @retval EFI_NOT_FOUND No allocated writes exist
814 @retval EFI_BUFFER_TOO_SMALL Input buffer is not larget enough
820 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
821 OUT EFI_GUID
*CallerId
,
825 IN OUT UINTN
*PrivateDataSize
,
826 OUT VOID
*PrivateData
,
827 OUT BOOLEAN
*Complete
831 EFI_FTW_DEVICE
*FtwDevice
;
832 EFI_FAULT_TOLERANT_WRITE_HEADER
*Header
;
833 EFI_FAULT_TOLERANT_WRITE_RECORD
*Record
;
835 if (!FeaturePcdGet (PcdFullFtwServiceEnable
)) {
836 return EFI_UNSUPPORTED
;
839 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
841 Status
= WorkSpaceRefresh (FtwDevice
);
842 if (EFI_ERROR (Status
)) {
846 Header
= FtwDevice
->FtwLastWriteHeader
;
847 Record
= FtwDevice
->FtwLastWriteRecord
;
850 // If Header is incompleted and the last record has completed, then
851 // call Abort() to set the Header->Complete FLAG.
853 if ((Header
->Complete
!= FTW_VALID_STATE
) &&
854 (Record
->DestinationComplete
== FTW_VALID_STATE
) &&
855 IsLastRecordOfWrites (Header
, Record
)
858 Status
= FtwAbort (This
);
860 return EFI_NOT_FOUND
;
864 // If there is no write header/record, return not found.
866 if (Header
->HeaderAllocated
!= FTW_VALID_STATE
) {
868 return EFI_NOT_FOUND
;
872 // If this record SpareComplete has not set, then it can not restart.
874 if (Record
->SpareComplete
!= FTW_VALID_STATE
) {
875 Status
= GetPreviousRecordOfWrites (Header
, &Record
);
876 if (EFI_ERROR (Status
)) {
879 return EFI_NOT_FOUND
;
882 ASSERT (Record
!= NULL
);
886 // Fill all the requested values
888 CopyMem (CallerId
, &Header
->CallerId
, sizeof (EFI_GUID
));
890 *Offset
= (UINTN
)Record
->Offset
;
891 *Length
= (UINTN
)Record
->Length
;
892 *Complete
= (BOOLEAN
)(Record
->DestinationComplete
== FTW_VALID_STATE
);
894 if (*PrivateDataSize
< Header
->PrivateDataSize
) {
895 *PrivateDataSize
= (UINTN
)Header
->PrivateDataSize
;
897 Status
= EFI_BUFFER_TOO_SMALL
;
899 *PrivateDataSize
= (UINTN
)Header
->PrivateDataSize
;
900 CopyMem (PrivateData
, Record
+ 1, *PrivateDataSize
);
901 Status
= EFI_SUCCESS
;
904 DEBUG ((DEBUG_INFO
, "%a(): success\n", __FUNCTION__
));