3 The internal header file includes the common header files, defines
4 internal structure and functions used by Ftw module.
6 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #ifndef _EFI_FAULT_TOLERANT_WRITE_H_
12 #define _EFI_FAULT_TOLERANT_WRITE_H_
16 #include <Guid/SystemNvDataGuid.h>
17 #include <Guid/ZeroGuid.h>
18 #include <Protocol/FaultTolerantWrite.h>
19 #include <Protocol/FirmwareVolumeBlock.h>
20 #include <Protocol/SwapAddressRange.h>
22 #include <Library/PcdLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/UefiDriverEntryPoint.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/ReportStatusCodeLib.h>
29 #include <Library/SafeIntLib.h>
30 #include <Library/VariableFlashInfoLib.h>
33 // Flash erase polarity is 1
35 #define FTW_ERASE_POLARITY 1
37 #define FTW_ERASED_BYTE ((UINT8) (255))
38 #define FTW_POLARITY_REVERT ((UINT8) (255))
40 #define HEADER_ALLOCATED 0x1
41 #define WRITES_ALLOCATED 0x2
42 #define WRITES_COMPLETED 0x4
44 #define BOOT_BLOCK_UPDATE 0x1
45 #define SPARE_COMPLETED 0x2
46 #define DEST_COMPLETED 0x4
48 #define FTW_BLOCKS(Length, BlockSize) ((UINTN) ((Length) / (BlockSize) + (((Length) & ((BlockSize) - 1)) ? 1 : 0)))
50 #define FTW_DEVICE_SIGNATURE SIGNATURE_32 ('F', 'T', 'W', 'D')
53 // EFI Fault tolerant protocol private data structure
58 EFI_FAULT_TOLERANT_WRITE_PROTOCOL FtwInstance
;
59 EFI_PHYSICAL_ADDRESS WorkSpaceAddress
; // Base address of working space range in flash.
60 EFI_PHYSICAL_ADDRESS SpareAreaAddress
; // Base address of spare range in flash.
61 UINTN WorkSpaceLength
; // Size of working space range in flash.
62 UINTN NumberOfWorkSpaceBlock
; // Number of the blocks in work block for work space.
63 UINTN WorkBlockSize
; // Block size in bytes of the work blocks in flash
64 UINTN SpareAreaLength
; // Size of spare range in flash.
65 UINTN NumberOfSpareBlock
; // Number of the blocks in spare block.
66 UINTN SpareBlockSize
; // Block size in bytes of the spare blocks in flash
67 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*FtwWorkSpaceHeader
; // Pointer to Working Space Header in memory buffer
68 EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwLastWriteHeader
; // Pointer to last record header in memory buffer
69 EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwLastWriteRecord
; // Pointer to last record in memory buffer
70 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FtwFvBlock
; // FVB of working block
71 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FtwBackupFvb
; // FVB of spare block
72 EFI_LBA FtwSpareLba
; // Start LBA of spare block
73 EFI_LBA FtwWorkBlockLba
; // Start LBA of working block that contains working space in its last block.
74 UINTN NumberOfWorkBlock
; // Number of the blocks in work block.
75 EFI_LBA FtwWorkSpaceLba
; // Start LBA of working space
76 UINTN FtwWorkSpaceBase
; // Offset into the FtwWorkSpaceLba block.
77 UINTN FtwWorkSpaceSize
; // Size of working space range that stores write record.
78 EFI_LBA FtwWorkSpaceLbaInSpare
; // Start LBA of working space in spare block.
79 UINTN FtwWorkSpaceBaseInSpare
; // Offset into the FtwWorkSpaceLbaInSpare block.
80 UINT8
*FtwWorkSpace
; // Point to Work Space in memory buffer
82 // Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE],
83 // Allocated with EFI_FTW_DEVICE.
87 #define FTW_CONTEXT_FROM_THIS(a) CR (a, EFI_FTW_DEVICE, FtwInstance, FTW_DEVICE_SIGNATURE)
94 This function is the entry point of the Fault Tolerant Write driver.
96 @param ImageHandle A handle for the image that is initializing this driver
97 @param SystemTable A pointer to the EFI system table
99 @return EFI_SUCCESS FTW has finished the initialization
100 @retval EFI_NOT_FOUND Locate FVB protocol error
101 @retval EFI_OUT_OF_RESOURCES Allocate memory error
102 @retval EFI_VOLUME_CORRUPTED Firmware volume is error
103 @retval EFI_ABORTED FTW initialization error
108 InitializeFaultTolerantWrite (
109 IN EFI_HANDLE ImageHandle
,
110 IN EFI_SYSTEM_TABLE
*SystemTable
114 // Fault Tolerant Write Protocol API
118 Query the largest block that may be updated in a fault tolerant manner.
121 @param This Indicates a pointer to the calling context.
122 @param BlockSize A pointer to a caller allocated UINTN that is updated to
123 indicate the size of the largest block that can be updated.
125 @return EFI_SUCCESS The function completed successfully
131 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
136 Allocates space for the protocol to maintain information about writes.
137 Since writes must be completed in a fault tolerant manner and multiple
138 updates will require more resources to be successful, this function
139 enables the protocol to ensure that enough space exists to track
140 information about the upcoming writes.
142 All writes must be completed or aborted before another fault tolerant write can occur.
144 @param This Indicates a pointer to the calling context.
145 @param CallerId The GUID identifying the write.
146 @param PrivateDataSize The size of the caller's private data
147 that must be recorded for each write.
148 @param NumberOfWrites The number of fault tolerant block writes
149 that will need to occur.
151 @return EFI_SUCCESS The function completed successfully
152 @retval EFI_ABORTED The function could not complete successfully.
153 @retval EFI_ACCESS_DENIED All allocated writes have not been completed.
159 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
160 IN EFI_GUID
*CallerId
,
161 IN UINTN PrivateDataSize
,
162 IN UINTN NumberOfWrites
166 Starts a target block update. This function will record data about write
167 in fault tolerant storage and will complete the write in a recoverable
168 manner, ensuring at all times that either the original contents or
169 the modified contents are available.
172 @param This Calling context
173 @param Lba The logical block address of the target block.
174 @param Offset The offset within the target block to place the data.
175 @param Length The number of bytes to write to the target block.
176 @param PrivateData A pointer to private data that the caller requires to
177 complete any pending writes in the event of a fault.
178 @param FvBlockHandle The handle of FVB protocol that provides services for
179 reading, writing, and erasing the target block.
180 @param Buffer The data to write.
182 @retval EFI_SUCCESS The function completed successfully
183 @retval EFI_ABORTED The function could not complete successfully.
184 @retval EFI_BAD_BUFFER_SIZE The input data can't fit within the spare block.
185 Offset + *NumBytes > SpareAreaLength.
186 @retval EFI_ACCESS_DENIED No writes have been allocated.
187 @retval EFI_OUT_OF_RESOURCES Cannot allocate enough memory resource.
188 @retval EFI_NOT_FOUND Cannot find FVB protocol by handle.
194 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
198 IN VOID
*PrivateData
,
199 IN EFI_HANDLE FvBlockHandle
,
204 Restarts a previously interrupted write. The caller must provide the
205 block protocol needed to complete the interrupted write.
207 @param This Calling context.
208 @param FvBlockHandle The handle of FVB protocol that provides services for
209 reading, writing, and erasing the target block.
211 @retval EFI_SUCCESS The function completed successfully
212 @retval EFI_ACCESS_DENIED No pending writes exist
213 @retval EFI_NOT_FOUND FVB protocol not found by the handle
214 @retval EFI_ABORTED The function could not complete successfully
220 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
221 IN EFI_HANDLE FvBlockHandle
225 Aborts all previous allocated writes.
227 @param This Calling context
229 @retval EFI_SUCCESS The function completed successfully
230 @retval EFI_ABORTED The function could not complete successfully.
231 @retval EFI_NOT_FOUND No allocated writes exist.
237 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
241 Starts a target block update. This records information about the write
242 in fault tolerant storage and will complete the write in a recoverable
243 manner, ensuring at all times that either the original contents or
244 the modified contents are available.
246 @param This Indicates a pointer to the calling context.
247 @param CallerId The GUID identifying the last write.
248 @param Lba The logical block address of the last write.
249 @param Offset The offset within the block of the last write.
250 @param Length The length of the last write.
251 @param PrivateDataSize bytes from the private data
252 stored for this write.
253 @param PrivateData A pointer to a buffer. The function will copy
254 @param Complete A Boolean value with TRUE indicating
255 that the write was completed.
257 @retval EFI_SUCCESS The function completed successfully
258 @retval EFI_ABORTED The function could not complete successfully
259 @retval EFI_NOT_FOUND No allocated writes exist
260 @retval EFI_BUFFER_TOO_SMALL Input buffer is not larget enough
266 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
267 OUT EFI_GUID
*CallerId
,
271 IN OUT UINTN
*PrivateDataSize
,
272 OUT VOID
*PrivateData
,
273 OUT BOOLEAN
*Complete
279 @param FtwDevice The private data of FTW driver
281 @retval EFI_SUCCESS The erase request was successfully completed.
282 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
283 @retval EFI_DEVICE_ERROR The block device is not functioning
284 correctly and could not be written.
285 The firmware device may have been
287 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed
288 in the variable argument list do
289 not exist in the firmware volume.
295 IN EFI_FTW_DEVICE
*FtwDevice
299 Retrieve the proper FVB protocol interface by HANDLE.
302 @param FvBlockHandle The handle of FVB protocol that provides services for
303 reading, writing, and erasing the target block.
304 @param FvBlock The interface of FVB protocol
306 @retval EFI_SUCCESS The function completed successfully
307 @retval EFI_ABORTED The function could not complete successfully
312 IN EFI_HANDLE FvBlockHandle
,
313 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
318 Is it in working block?
320 @param FtwDevice The private data of FTW driver
321 @param FvBlock Fvb protocol instance
322 @param Lba The block specified
324 @return A BOOLEAN value indicating in working block or not.
329 EFI_FTW_DEVICE
*FtwDevice
,
330 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
338 @param FtwDevice The private data of FTW driver
339 @param FvBlock Fvb protocol instance
341 @return A BOOLEAN value indicating in boot block or not.
346 EFI_FTW_DEVICE
*FtwDevice
,
347 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
351 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
352 Spare block is accessed by FTW backup FVB protocol interface.
353 Target block is accessed by FvBlock protocol interface.
356 @param FtwDevice The private data of FTW driver
357 @param FvBlock FVB Protocol interface to access target block
358 @param Lba Lba of the target block
359 @param BlockSize The size of the block
360 @param NumberOfBlocks The number of consecutive blocks starting with Lba
362 @retval EFI_SUCCESS Spare block content is copied to target block
363 @retval EFI_INVALID_PARAMETER Input parameter error
364 @retval EFI_OUT_OF_RESOURCES Allocate memory error
365 @retval EFI_ABORTED The function could not complete successfully
369 FlushSpareBlockToTargetBlock (
370 EFI_FTW_DEVICE
*FtwDevice
,
371 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
378 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
379 Spare block is accessed by FTW backup FVB protocol interface. LBA is
380 FtwDevice->FtwSpareLba.
381 Working block is accessed by FTW working FVB protocol interface. LBA is
382 FtwDevice->FtwWorkBlockLba.
384 Since the working block header is important when FTW initializes, the
385 state of the operation should be handled carefully. The Crc value is
386 calculated without STATE element.
388 @param FtwDevice The private data of FTW driver
390 @retval EFI_SUCCESS Spare block content is copied to target block
391 @retval EFI_OUT_OF_RESOURCES Allocate memory error
392 @retval EFI_ABORTED The function could not complete successfully
396 FlushSpareBlockToWorkingBlock (
397 EFI_FTW_DEVICE
*FtwDevice
401 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
402 Spare block is accessed by FTW working FVB protocol interface.
403 Target block is accessed by FvBlock protocol interface.
405 FTW will do extra work on boot block update.
406 FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL,
407 which is produced by a chipset driver.
408 FTW updating boot block steps may be:
409 1. GetRangeLocation(), if the Range is inside the boot block, FTW know
410 that boot block will be update. It shall add a FLAG in the working block.
411 2. When spare block is ready,
412 3. SetSwapState(SWAPPED)
413 4. erasing boot block,
414 5. programming boot block until the boot block is ok.
415 6. SetSwapState(UNSWAPPED)
416 FTW shall not allow to update boot block when battery state is error.
418 @param FtwDevice The private data of FTW driver
420 @retval EFI_SUCCESS Spare block content is copied to boot block
421 @retval EFI_INVALID_PARAMETER Input parameter error
422 @retval EFI_OUT_OF_RESOURCES Allocate memory error
423 @retval EFI_ABORTED The function could not complete successfully
427 FlushSpareBlockToBootBlock (
428 EFI_FTW_DEVICE
*FtwDevice
432 Update a bit of state on a block device. The location of the bit is
433 calculated by the (Lba, Offset, bit). Here bit is determined by the
434 the name of a certain bit.
437 @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
438 @param BlockSize The size of the block
439 @param Lba Lba of a block
440 @param Offset Offset on the Lba
441 @param NewBit New value that will override the old value if it can be change
443 @retval EFI_SUCCESS A state bit has been updated successfully
444 @retval Others Access block device error.
446 Assume all bits of State are inside the same BYTE.
447 @retval EFI_ABORTED Read block fail
452 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
460 Get the last Write Header pointer.
461 The last write header is the header whose 'complete' state hasn't been set.
462 After all, this header may be a EMPTY header entry for next Allocate.
465 @param FtwWorkSpaceHeader Pointer of the working block header
466 @param FtwWorkSpaceSize Size of the work space
467 @param FtwWriteHeader Pointer to retrieve the last write header
469 @retval EFI_SUCCESS Get the last write record successfully
470 @retval EFI_ABORTED The FTW work space is damaged
474 FtwGetLastWriteHeader (
475 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*FtwWorkSpaceHeader
,
476 IN UINTN FtwWorkSpaceSize
,
477 OUT EFI_FAULT_TOLERANT_WRITE_HEADER
**FtwWriteHeader
481 Get the last Write Record pointer. The last write Record is the Record
482 whose DestinationCompleted state hasn't been set. After all, this Record
483 may be a EMPTY record entry for next write.
486 @param FtwWriteHeader Pointer to the write record header
487 @param FtwWriteRecord Pointer to retrieve the last write record
489 @retval EFI_SUCCESS Get the last write record successfully
490 @retval EFI_ABORTED The FTW work space is damaged
494 FtwGetLastWriteRecord (
495 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwWriteHeader
,
496 OUT EFI_FAULT_TOLERANT_WRITE_RECORD
**FtwWriteRecord
500 To check if FtwRecord is the first record of FtwHeader.
502 @param FtwHeader Pointer to the write record header
503 @param FtwRecord Pointer to the write record
505 @retval TRUE FtwRecord is the first Record of the FtwHeader
506 @retval FALSE FtwRecord is not the first Record of the FtwHeader
510 IsFirstRecordOfWrites (
511 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
512 IN EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwRecord
516 To check if FtwRecord is the last record of FtwHeader. Because the
517 FtwHeader has NumberOfWrites & PrivateDataSize, the FtwRecord can be
518 determined if it is the last record of FtwHeader.
520 @param FtwHeader Pointer to the write record header
521 @param FtwRecord Pointer to the write record
523 @retval TRUE FtwRecord is the last Record of the FtwHeader
524 @retval FALSE FtwRecord is not the last Record of the FtwHeader
528 IsLastRecordOfWrites (
529 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
530 IN EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwRecord
534 To check if FtwRecord is the first record of FtwHeader.
536 @param FtwHeader Pointer to the write record header
537 @param FtwRecord Pointer to retrieve the previous write record
539 @retval EFI_ACCESS_DENIED Input record is the first record, no previous record is return.
540 @retval EFI_SUCCESS The previous write record is found.
544 GetPreviousRecordOfWrites (
545 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
546 IN OUT EFI_FAULT_TOLERANT_WRITE_RECORD
**FtwRecord
551 Check whether a flash buffer is erased.
553 @param Buffer Buffer to check
554 @param BufferSize Size of the buffer
556 @return A BOOLEAN value indicating erased or not.
560 IsErasedFlashBuffer (
566 Initialize a work space when there is no work space.
568 @param WorkingHeader Pointer of working block header
570 @retval EFI_SUCCESS The function completed successfully
571 @retval EFI_ABORTED The function could not complete successfully.
575 InitWorkSpaceHeader (
576 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
580 Read from working block to refresh the work space in memory.
582 @param FtwDevice Point to private data of FTW driver
584 @retval EFI_SUCCESS The function completed successfully
585 @retval EFI_ABORTED The function could not complete successfully.
590 IN EFI_FTW_DEVICE
*FtwDevice
594 Check to see if it is a valid work space.
597 @param WorkingHeader Pointer of working block header
599 @retval EFI_SUCCESS The function completed successfully
600 @retval EFI_ABORTED The function could not complete successfully.
605 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
609 Reclaim the work space on the working block.
611 @param FtwDevice Point to private data of FTW driver
612 @param PreserveRecord Whether to preserve the working record is needed
614 @retval EFI_SUCCESS The function completed successfully
615 @retval EFI_OUT_OF_RESOURCES Allocate memory error
616 @retval EFI_ABORTED The function could not complete successfully
620 FtwReclaimWorkSpace (
621 IN EFI_FTW_DEVICE
*FtwDevice
,
622 IN BOOLEAN PreserveRecord
627 Get firmware volume block by address.
630 @param Address Address specified the block
631 @param FvBlock The block caller wanted
633 @retval EFI_SUCCESS The protocol instance if found.
634 @retval EFI_NOT_FOUND Block not found
639 IN EFI_PHYSICAL_ADDRESS Address
,
640 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
644 Retrieve the proper Swap Address Range protocol interface.
646 @param[out] SarProtocol The interface of SAR protocol
648 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
649 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
650 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
655 OUT VOID
**SarProtocol
659 Function returns an array of handles that support the FVB protocol
660 in a buffer allocated from pool.
662 @param[out] NumberHandles The number of handles returned in Buffer.
663 @param[out] Buffer A pointer to the buffer to return the requested
664 array of handles that support FVB protocol.
666 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
667 handles in Buffer was returned in NumberHandles.
668 @retval EFI_NOT_FOUND No FVB handle was found.
669 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
670 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
674 GetFvbCountAndBuffer (
675 OUT UINTN
*NumberHandles
,
676 OUT EFI_HANDLE
**Buffer
680 Allocate private data for FTW driver and initialize it.
682 @param[out] FtwData Pointer to the FTW device structure
684 @retval EFI_SUCCESS Initialize the FTW device successfully.
685 @retval EFI_OUT_OF_RESOURCES Allocate memory error
686 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
691 OUT EFI_FTW_DEVICE
**FtwData
695 Initialization for Fault Tolerant Write is done in this handler.
697 @param[in, out] FtwDevice Pointer to the FTW device structure
699 @retval EFI_SUCCESS Initialize the FTW protocol successfully.
700 @retval EFI_NOT_FOUND No proper FVB protocol was found.
705 IN OUT EFI_FTW_DEVICE
*FtwDevice
709 Initialize a local work space header.
711 Since Signature and WriteQueueSize have been known, Crc can be calculated out,
712 then the work space header will be fixed.
714 @param[in] WorkSpaceLength Length in bytes of the FTW workspace area.
718 InitializeLocalWorkSpaceHeader (
719 IN UINTN WorkSpaceLength
723 Read work space data from work block or spare block.
725 @param FvBlock FVB Protocol interface to access the block.
726 @param BlockSize The size of the block.
727 @param Lba Lba of the block.
728 @param Offset The offset within the block.
729 @param Length The number of bytes to read from the block.
730 @param Buffer The data is read.
732 @retval EFI_SUCCESS The function completed successfully.
733 @retval EFI_ABORTED The function could not complete successfully.
738 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
747 Write data to work block.
749 @param FvBlock FVB Protocol interface to access the block.
750 @param BlockSize The size of the block.
751 @param Lba Lba of the block.
752 @param Offset The offset within the block to place the data.
753 @param Length The number of bytes to write to the block.
754 @param Buffer The data to write.
756 @retval EFI_SUCCESS The function completed successfully.
757 @retval EFI_ABORTED The function could not complete successfully.
762 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
771 Internal implementation of CRC32. Depending on the execution context
772 (traditional SMM or DXE vs standalone MM), this function is implemented
773 via a call to the CalculateCrc32 () boot service, or via a library
776 If Buffer is NULL, then ASSERT().
777 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
779 @param[in] Buffer A pointer to the buffer on which the 32-bit CRC is
781 @param[in] Length The number of bytes in the buffer Data.
783 @retval Crc32 The 32-bit CRC was computed for the data buffer.