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 - 2014, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "FaultTolerantWrite.h"
20 // Fault Tolerant Write Protocol API
23 Query the largest block that may be updated in a fault tolerant manner.
26 @param This The pointer to this protocol instance.
27 @param BlockSize A pointer to a caller allocated UINTN that is updated to
28 indicate the size of the largest block that can be updated.
30 @return EFI_SUCCESS The function completed successfully
36 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
40 EFI_FTW_DEVICE
*FtwDevice
;
42 if (!FeaturePcdGet(PcdFullFtwServiceEnable
)) {
43 return EFI_UNSUPPORTED
;
46 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
48 *BlockSize
= FtwDevice
->SpareAreaLength
;
54 Allocates space for the protocol to maintain information about writes.
55 Since writes must be completed in a fault tolerant manner and multiple
56 updates will require more resources to be successful, this function
57 enables the protocol to ensure that enough space exists to track
58 information about the upcoming writes.
60 All writes must be completed or aborted before another fault tolerant write can occur.
62 @param This The pointer to this protocol instance.
63 @param CallerId The GUID identifying the write.
64 @param PrivateDataSize The size of the caller's private data
65 that must be recorded for each write.
66 @param NumberOfWrites The number of fault tolerant block writes
67 that will need to occur.
69 @return EFI_SUCCESS The function completed successfully
70 @retval EFI_ABORTED The function could not complete successfully.
71 @retval EFI_ACCESS_DENIED All allocated writes have not been completed.
77 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
78 IN EFI_GUID
*CallerId
,
79 IN UINTN PrivateDataSize
,
80 IN UINTN NumberOfWrites
85 EFI_FTW_DEVICE
*FtwDevice
;
86 EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
;
88 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
90 Status
= WorkSpaceRefresh (FtwDevice
);
91 if (EFI_ERROR (Status
)) {
95 // Check if there is enough space for the coming allocation
97 if (FTW_WRITE_TOTAL_SIZE (NumberOfWrites
, PrivateDataSize
) > FtwDevice
->FtwWorkSpaceHeader
->WriteQueueSize
) {
98 DEBUG ((EFI_D_ERROR
, "Ftw: Allocate() request exceed Workspace, Caller: %g\n", CallerId
));
99 return EFI_BUFFER_TOO_SMALL
;
102 // Find the last write header and record.
103 // If the FtwHeader is complete, skip the completed last write header/records
105 FtwHeader
= FtwDevice
->FtwLastWriteHeader
;
108 // Previous write has not completed, access denied.
110 if ((FtwHeader
->HeaderAllocated
== FTW_VALID_STATE
) || (FtwHeader
->WritesAllocated
== FTW_VALID_STATE
)) {
111 return EFI_ACCESS_DENIED
;
114 // If workspace is not enough, then reclaim workspace
116 Offset
= (UINT8
*) FtwHeader
- (UINT8
*) FtwDevice
->FtwWorkSpace
;
117 if (Offset
+ FTW_WRITE_TOTAL_SIZE (NumberOfWrites
, PrivateDataSize
) > FtwDevice
->FtwWorkSpaceSize
) {
118 Status
= FtwReclaimWorkSpace (FtwDevice
, TRUE
);
119 if (EFI_ERROR (Status
)) {
123 FtwHeader
= FtwDevice
->FtwLastWriteHeader
;
126 // Prepare FTW write header,
127 // overwrite the buffer and write to workspace.
129 FtwHeader
->WritesAllocated
= FTW_INVALID_STATE
;
130 FtwHeader
->Complete
= FTW_INVALID_STATE
;
131 CopyMem (&FtwHeader
->CallerId
, CallerId
, sizeof (EFI_GUID
));
132 FtwHeader
->NumberOfWrites
= NumberOfWrites
;
133 FtwHeader
->PrivateDataSize
= PrivateDataSize
;
134 FtwHeader
->HeaderAllocated
= FTW_VALID_STATE
;
136 Status
= WriteWorkSpaceData (
137 FtwDevice
->FtwFvBlock
,
138 FtwDevice
->WorkBlockSize
,
139 FtwDevice
->FtwWorkSpaceLba
,
140 FtwDevice
->FtwWorkSpaceBase
+ Offset
,
141 sizeof (EFI_FAULT_TOLERANT_WRITE_HEADER
),
144 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",
173 Write a record with fault tolerant manner.
174 Since the content has already backuped in spare block, the write is
175 guaranteed to be completed with fault tolerant manner.
177 @param This The pointer to this protocol instance.
178 @param Fvb The FVB protocol that provides services for
179 reading, writing, and erasing the target block.
180 @param BlockSize The size of the block.
182 @retval EFI_SUCCESS The function completed successfully
183 @retval EFI_ABORTED The function could not complete successfully
188 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
189 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
194 EFI_FTW_DEVICE
*FtwDevice
;
195 EFI_FAULT_TOLERANT_WRITE_HEADER
*Header
;
196 EFI_FAULT_TOLERANT_WRITE_RECORD
*Record
;
198 UINTN NumberOfWriteBlocks
;
200 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
203 // Spare Complete but Destination not complete,
204 // Recover the target block with the spare block.
206 Header
= FtwDevice
->FtwLastWriteHeader
;
207 Record
= FtwDevice
->FtwLastWriteRecord
;
210 // IF target block is working block, THEN Flush Spare Block To Working Block;
211 // ELSE flush spare block to target block, which may be boot block after all.
213 if (IsWorkingBlock (FtwDevice
, Fvb
, Record
->Lba
)) {
215 // If target block is working block,
216 // it also need to set SPARE_COMPLETED to spare block.
218 Offset
= (UINT8
*) Record
- FtwDevice
->FtwWorkSpace
;
219 Status
= FtwUpdateFvState (
220 FtwDevice
->FtwBackupFvb
,
221 FtwDevice
->SpareBlockSize
,
222 FtwDevice
->FtwSpareLba
+ FtwDevice
->FtwWorkSpaceLbaInSpare
,
223 FtwDevice
->FtwWorkSpaceBaseInSpare
+ Offset
,
226 if (EFI_ERROR (Status
)) {
230 Status
= FlushSpareBlockToWorkingBlock (FtwDevice
);
231 } else if (IsBootBlock (FtwDevice
, Fvb
)) {
235 Status
= FlushSpareBlockToBootBlock (FtwDevice
);
238 // Update blocks other than working block or boot block
240 NumberOfWriteBlocks
= FTW_BLOCKS ((UINTN
) (Record
->Offset
+ Record
->Length
), BlockSize
);
241 Status
= FlushSpareBlockToTargetBlock (FtwDevice
, Fvb
, Record
->Lba
, BlockSize
, NumberOfWriteBlocks
);
244 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 ((EFI_D_ERROR
, "Ftw: no allocates space for write record!\n"));
368 DEBUG ((EFI_D_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
)((UINT8
*) Record
- (UINT8
*) 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 ((EFI_D_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 ((EFI_D_ERROR
, "Ftw: Write(), Get block size - %r\n", Status
));
418 NumberOfWriteBlocks
= FTW_BLOCKS (Offset
+ Length
, BlockSize
);
419 DEBUG ((EFI_D_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
));
440 // Write the record to the work space.
443 Record
->Offset
= Offset
;
444 Record
->Length
= Length
;
445 Record
->RelativeOffset
= (INT64
) (FvbPhysicalAddress
+ (UINTN
) Lba
* BlockSize
) - (INT64
) FtwDevice
->SpareAreaAddress
;
446 if (PrivateData
!= NULL
) {
447 CopyMem ((Record
+ 1), PrivateData
, (UINTN
) Header
->PrivateDataSize
);
450 MyOffset
= (UINT8
*) Record
- FtwDevice
->FtwWorkSpace
;
451 MyLength
= FTW_RECORD_SIZE (Header
->PrivateDataSize
);
453 Status
= WriteWorkSpaceData (
454 FtwDevice
->FtwFvBlock
,
455 FtwDevice
->WorkBlockSize
,
456 FtwDevice
->FtwWorkSpaceLba
,
457 FtwDevice
->FtwWorkSpaceBase
+ MyOffset
,
461 if (EFI_ERROR (Status
)) {
465 // Record has written to working block, then do the data.
468 // Allocate a memory buffer
470 MyBufferSize
= WriteLength
;
471 MyBuffer
= AllocatePool (MyBufferSize
);
472 if (MyBuffer
== NULL
) {
473 return EFI_OUT_OF_RESOURCES
;
476 // Read all original data from target block to memory buffer
479 for (Index
= 0; Index
< NumberOfWriteBlocks
; Index
+= 1) {
480 MyLength
= BlockSize
;
481 Status
= Fvb
->Read (Fvb
, Lba
+ Index
, 0, &MyLength
, Ptr
);
482 if (EFI_ERROR (Status
)) {
490 // Overwrite the updating range data with
491 // the input buffer content
493 CopyMem (MyBuffer
+ Offset
, Buffer
, Length
);
496 // Try to keep the content of spare block
497 // Save spare block into a spare backup memory buffer (Sparebuffer)
499 SpareBufferSize
= FtwDevice
->SpareAreaLength
;
500 SpareBuffer
= AllocatePool (SpareBufferSize
);
501 if (SpareBuffer
== NULL
) {
503 return EFI_OUT_OF_RESOURCES
;
507 for (Index
= 0; Index
< FtwDevice
->NumberOfSpareBlock
; Index
+= 1) {
508 MyLength
= FtwDevice
->SpareBlockSize
;
509 Status
= FtwDevice
->FtwBackupFvb
->Read (
510 FtwDevice
->FtwBackupFvb
,
511 FtwDevice
->FtwSpareLba
+ Index
,
516 if (EFI_ERROR (Status
)) {
518 FreePool (SpareBuffer
);
525 // Write the memory buffer to spare block
526 // Do not assume Spare Block and Target Block have same block size
528 Status
= FtwEraseSpareBlock (FtwDevice
);
530 for (Index
= 0; MyBufferSize
> 0; Index
+= 1) {
531 if (MyBufferSize
> FtwDevice
->SpareBlockSize
) {
532 MyLength
= FtwDevice
->SpareBlockSize
;
534 MyLength
= MyBufferSize
;
536 Status
= FtwDevice
->FtwBackupFvb
->Write (
537 FtwDevice
->FtwBackupFvb
,
538 FtwDevice
->FtwSpareLba
+ Index
,
543 if (EFI_ERROR (Status
)) {
545 FreePool (SpareBuffer
);
550 MyBufferSize
-= MyLength
;
558 // Set the SpareComplete in the FTW record,
560 MyOffset
= (UINT8
*) Record
- FtwDevice
->FtwWorkSpace
;
561 Status
= FtwUpdateFvState (
562 FtwDevice
->FtwFvBlock
,
563 FtwDevice
->WorkBlockSize
,
564 FtwDevice
->FtwWorkSpaceLba
,
565 FtwDevice
->FtwWorkSpaceBase
+ MyOffset
,
568 if (EFI_ERROR (Status
)) {
569 FreePool (SpareBuffer
);
573 Record
->SpareComplete
= FTW_VALID_STATE
;
576 // Since the content has already backuped in spare block, the write is
577 // guaranteed to be completed with fault tolerant manner.
579 Status
= FtwWriteRecord (This
, Fvb
, BlockSize
);
580 if (EFI_ERROR (Status
)) {
581 FreePool (SpareBuffer
);
585 // Restore spare backup buffer into spare block , if no failure happened during FtwWrite.
587 Status
= FtwEraseSpareBlock (FtwDevice
);
589 for (Index
= 0; Index
< FtwDevice
->NumberOfSpareBlock
; Index
+= 1) {
590 MyLength
= FtwDevice
->SpareBlockSize
;
591 Status
= FtwDevice
->FtwBackupFvb
->Write (
592 FtwDevice
->FtwBackupFvb
,
593 FtwDevice
->FtwSpareLba
+ Index
,
598 if (EFI_ERROR (Status
)) {
599 FreePool (SpareBuffer
);
608 FreePool (SpareBuffer
);
612 "Ftw: Write() success, (Lba:Offset)=(%lx:0x%x), Length: 0x%x\n",
622 Restarts a previously interrupted write. The caller must provide the
623 block protocol needed to complete the interrupted write.
625 @param This The pointer to this protocol instance.
626 @param FvBlockHandle The handle of FVB protocol that provides services for
627 reading, writing, and erasing the target block.
629 @retval EFI_SUCCESS The function completed successfully
630 @retval EFI_ACCESS_DENIED No pending writes exist
631 @retval EFI_NOT_FOUND FVB protocol not found by the handle
632 @retval EFI_ABORTED The function could not complete successfully
638 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
639 IN EFI_HANDLE FvBlockHandle
643 EFI_FTW_DEVICE
*FtwDevice
;
644 EFI_FAULT_TOLERANT_WRITE_HEADER
*Header
;
645 EFI_FAULT_TOLERANT_WRITE_RECORD
*Record
;
646 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
648 UINTN NumberOfBlocks
;
650 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
652 Status
= WorkSpaceRefresh (FtwDevice
);
653 if (EFI_ERROR (Status
)) {
657 Header
= FtwDevice
->FtwLastWriteHeader
;
658 Record
= FtwDevice
->FtwLastWriteRecord
;
661 // Spare Complete but Destination not complete,
662 // Recover the targt block with the spare block.
664 Status
= FtwGetFvbByHandle (FvBlockHandle
, &Fvb
);
665 if (EFI_ERROR (Status
)) {
666 return EFI_NOT_FOUND
;
670 // Now, one FVB has one type of BlockSize
672 Status
= Fvb
->GetBlockSize (Fvb
, 0, &BlockSize
, &NumberOfBlocks
);
673 if (EFI_ERROR (Status
)) {
674 DEBUG ((EFI_D_ERROR
, "Ftw: Restart(), Get block size - %r\n", Status
));
679 // Check the COMPLETE flag of last write header
681 if (Header
->Complete
== FTW_VALID_STATE
) {
682 return EFI_ACCESS_DENIED
;
686 // Check the flags of last write record
688 if (Record
->DestinationComplete
== FTW_VALID_STATE
) {
689 return EFI_ACCESS_DENIED
;
692 if ((Record
->SpareComplete
!= FTW_VALID_STATE
)) {
697 // Since the content has already backuped in spare block, the write is
698 // guaranteed to be completed with fault tolerant manner.
700 Status
= FtwWriteRecord (This
, Fvb
, BlockSize
);
701 if (EFI_ERROR (Status
)) {
707 // This is restart, no need to keep spareblock content.
709 FtwEraseSpareBlock (FtwDevice
);
711 DEBUG ((EFI_D_ERROR
, "Ftw: Restart() success \n"));
716 Aborts all previous allocated writes.
718 @param This The pointer to this protocol instance.
720 @retval EFI_SUCCESS The function completed successfully
721 @retval EFI_ABORTED The function could not complete successfully.
722 @retval EFI_NOT_FOUND No allocated writes exist.
728 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
733 EFI_FTW_DEVICE
*FtwDevice
;
735 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
737 Status
= WorkSpaceRefresh (FtwDevice
);
738 if (EFI_ERROR (Status
)) {
742 if (FtwDevice
->FtwLastWriteHeader
->HeaderAllocated
!= FTW_VALID_STATE
) {
743 return EFI_NOT_FOUND
;
746 if (FtwDevice
->FtwLastWriteHeader
->Complete
== FTW_VALID_STATE
) {
747 return EFI_NOT_FOUND
;
750 // Update the complete state of the header as VALID and abort.
752 Offset
= (UINT8
*) FtwDevice
->FtwLastWriteHeader
- FtwDevice
->FtwWorkSpace
;
753 Status
= FtwUpdateFvState (
754 FtwDevice
->FtwFvBlock
,
755 FtwDevice
->WorkBlockSize
,
756 FtwDevice
->FtwWorkSpaceLba
,
757 FtwDevice
->FtwWorkSpaceBase
+ Offset
,
760 if (EFI_ERROR (Status
)) {
764 FtwDevice
->FtwLastWriteHeader
->Complete
= FTW_VALID_STATE
;
766 DEBUG ((EFI_D_ERROR
, "Ftw: Abort() success \n"));
771 Starts a target block update. This records information about the write
772 in fault tolerant storage and will complete the write in a recoverable
773 manner, ensuring at all times that either the original contents or
774 the modified contents are available.
776 @param This The pointer to this protocol instance.
777 @param CallerId The GUID identifying the last write.
778 @param Lba The logical block address of the last write.
779 @param Offset The offset within the block of the last write.
780 @param Length The length of the last write.
781 @param PrivateDataSize bytes from the private data
782 stored for this write.
783 @param PrivateData A pointer to a buffer. The function will copy
784 @param Complete A Boolean value with TRUE indicating
785 that the write was completed.
787 @retval EFI_SUCCESS The function completed successfully
788 @retval EFI_ABORTED The function could not complete successfully
789 @retval EFI_NOT_FOUND No allocated writes exist
790 @retval EFI_BUFFER_TOO_SMALL Input buffer is not larget enough
796 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
797 OUT EFI_GUID
*CallerId
,
801 IN OUT UINTN
*PrivateDataSize
,
802 OUT VOID
*PrivateData
,
803 OUT BOOLEAN
*Complete
807 EFI_FTW_DEVICE
*FtwDevice
;
808 EFI_FAULT_TOLERANT_WRITE_HEADER
*Header
;
809 EFI_FAULT_TOLERANT_WRITE_RECORD
*Record
;
811 if (!FeaturePcdGet(PcdFullFtwServiceEnable
)) {
812 return EFI_UNSUPPORTED
;
815 FtwDevice
= FTW_CONTEXT_FROM_THIS (This
);
817 Status
= WorkSpaceRefresh (FtwDevice
);
818 if (EFI_ERROR (Status
)) {
822 Header
= FtwDevice
->FtwLastWriteHeader
;
823 Record
= FtwDevice
->FtwLastWriteRecord
;
826 // If Header is incompleted and the last record has completed, then
827 // call Abort() to set the Header->Complete FLAG.
829 if ((Header
->Complete
!= FTW_VALID_STATE
) &&
830 (Record
->DestinationComplete
== FTW_VALID_STATE
) &&
831 IsLastRecordOfWrites (Header
, Record
)
834 Status
= FtwAbort (This
);
836 return EFI_NOT_FOUND
;
839 // If there is no write header/record, return not found.
841 if (Header
->HeaderAllocated
!= FTW_VALID_STATE
) {
843 return EFI_NOT_FOUND
;
846 // If this record SpareComplete has not set, then it can not restart.
848 if (Record
->SpareComplete
!= FTW_VALID_STATE
) {
849 Status
= GetPreviousRecordOfWrites (Header
, &Record
);
850 if (EFI_ERROR (Status
)) {
853 return EFI_NOT_FOUND
;
855 ASSERT (Record
!= NULL
);
859 // Fill all the requested values
861 CopyMem (CallerId
, &Header
->CallerId
, sizeof (EFI_GUID
));
863 *Offset
= (UINTN
) Record
->Offset
;
864 *Length
= (UINTN
) Record
->Length
;
865 *Complete
= (BOOLEAN
) (Record
->DestinationComplete
== FTW_VALID_STATE
);
867 if (*PrivateDataSize
< Header
->PrivateDataSize
) {
868 *PrivateDataSize
= (UINTN
) Header
->PrivateDataSize
;
870 Status
= EFI_BUFFER_TOO_SMALL
;
872 *PrivateDataSize
= (UINTN
) Header
->PrivateDataSize
;
873 CopyMem (PrivateData
, Record
+ 1, *PrivateDataSize
);
874 Status
= EFI_SUCCESS
;
877 DEBUG ((EFI_D_ERROR
, "Ftw: GetLasetWrite() success\n"));