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>
31 // Flash erase polarity is 1
33 #define FTW_ERASE_POLARITY 1
35 #define FTW_ERASED_BYTE ((UINT8) (255))
36 #define FTW_POLARITY_REVERT ((UINT8) (255))
38 #define HEADER_ALLOCATED 0x1
39 #define WRITES_ALLOCATED 0x2
40 #define WRITES_COMPLETED 0x4
42 #define BOOT_BLOCK_UPDATE 0x1
43 #define SPARE_COMPLETED 0x2
44 #define DEST_COMPLETED 0x4
46 #define FTW_BLOCKS(Length, BlockSize) ((UINTN) ((Length) / (BlockSize) + (((Length) & ((BlockSize) - 1)) ? 1 : 0)))
48 #define FTW_DEVICE_SIGNATURE SIGNATURE_32 ('F', 'T', 'W', 'D')
51 // EFI Fault tolerant protocol private data structure
56 EFI_FAULT_TOLERANT_WRITE_PROTOCOL FtwInstance
;
57 EFI_PHYSICAL_ADDRESS WorkSpaceAddress
; // Base address of working space range in flash.
58 EFI_PHYSICAL_ADDRESS SpareAreaAddress
; // Base address of spare range in flash.
59 UINTN WorkSpaceLength
; // Size of working space range in flash.
60 UINTN NumberOfWorkSpaceBlock
; // Number of the blocks in work block for work space.
61 UINTN WorkBlockSize
; // Block size in bytes of the work blocks in flash
62 UINTN SpareAreaLength
; // Size of spare range in flash.
63 UINTN NumberOfSpareBlock
; // Number of the blocks in spare block.
64 UINTN SpareBlockSize
; // Block size in bytes of the spare blocks in flash
65 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*FtwWorkSpaceHeader
; // Pointer to Working Space Header in memory buffer
66 EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwLastWriteHeader
; // Pointer to last record header in memory buffer
67 EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwLastWriteRecord
; // Pointer to last record in memory buffer
68 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FtwFvBlock
; // FVB of working block
69 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FtwBackupFvb
; // FVB of spare block
70 EFI_LBA FtwSpareLba
; // Start LBA of spare block
71 EFI_LBA FtwWorkBlockLba
; // Start LBA of working block that contains working space in its last block.
72 UINTN NumberOfWorkBlock
; // Number of the blocks in work block.
73 EFI_LBA FtwWorkSpaceLba
; // Start LBA of working space
74 UINTN FtwWorkSpaceBase
; // Offset into the FtwWorkSpaceLba block.
75 UINTN FtwWorkSpaceSize
; // Size of working space range that stores write record.
76 EFI_LBA FtwWorkSpaceLbaInSpare
; // Start LBA of working space in spare block.
77 UINTN FtwWorkSpaceBaseInSpare
; // Offset into the FtwWorkSpaceLbaInSpare block.
78 UINT8
*FtwWorkSpace
; // Point to Work Space in memory buffer
80 // Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE],
81 // Allocated with EFI_FTW_DEVICE.
85 #define FTW_CONTEXT_FROM_THIS(a) CR (a, EFI_FTW_DEVICE, FtwInstance, FTW_DEVICE_SIGNATURE)
92 This function is the entry point of the Fault Tolerant Write driver.
94 @param ImageHandle A handle for the image that is initializing this driver
95 @param SystemTable A pointer to the EFI system table
97 @return EFI_SUCCESS FTW has finished the initialization
98 @retval EFI_NOT_FOUND Locate FVB protocol error
99 @retval EFI_OUT_OF_RESOURCES Allocate memory error
100 @retval EFI_VOLUME_CORRUPTED Firmware volume is error
101 @retval EFI_ABORTED FTW initialization error
106 InitializeFaultTolerantWrite (
107 IN EFI_HANDLE ImageHandle
,
108 IN EFI_SYSTEM_TABLE
*SystemTable
112 // Fault Tolerant Write Protocol API
116 Query the largest block that may be updated in a fault tolerant manner.
119 @param This Indicates a pointer to the calling context.
120 @param BlockSize A pointer to a caller allocated UINTN that is updated to
121 indicate the size of the largest block that can be updated.
123 @return EFI_SUCCESS The function completed successfully
129 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
134 Allocates space for the protocol to maintain information about writes.
135 Since writes must be completed in a fault tolerant manner and multiple
136 updates will require more resources to be successful, this function
137 enables the protocol to ensure that enough space exists to track
138 information about the upcoming writes.
140 All writes must be completed or aborted before another fault tolerant write can occur.
142 @param This Indicates a pointer to the calling context.
143 @param CallerId The GUID identifying the write.
144 @param PrivateDataSize The size of the caller's private data
145 that must be recorded for each write.
146 @param NumberOfWrites The number of fault tolerant block writes
147 that will need to occur.
149 @return EFI_SUCCESS The function completed successfully
150 @retval EFI_ABORTED The function could not complete successfully.
151 @retval EFI_ACCESS_DENIED All allocated writes have not been completed.
157 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
158 IN EFI_GUID
*CallerId
,
159 IN UINTN PrivateDataSize
,
160 IN UINTN NumberOfWrites
164 Starts a target block update. This function will record data about write
165 in fault tolerant storage and will complete the write in a recoverable
166 manner, ensuring at all times that either the original contents or
167 the modified contents are available.
170 @param This Calling context
171 @param Lba The logical block address of the target block.
172 @param Offset The offset within the target block to place the data.
173 @param Length The number of bytes to write to the target block.
174 @param PrivateData A pointer to private data that the caller requires to
175 complete any pending writes in the event of a fault.
176 @param FvBlockHandle The handle of FVB protocol that provides services for
177 reading, writing, and erasing the target block.
178 @param Buffer The data to write.
180 @retval EFI_SUCCESS The function completed successfully
181 @retval EFI_ABORTED The function could not complete successfully.
182 @retval EFI_BAD_BUFFER_SIZE The input data can't fit within the spare block.
183 Offset + *NumBytes > SpareAreaLength.
184 @retval EFI_ACCESS_DENIED No writes have been allocated.
185 @retval EFI_OUT_OF_RESOURCES Cannot allocate enough memory resource.
186 @retval EFI_NOT_FOUND Cannot find FVB protocol by handle.
192 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
196 IN VOID
*PrivateData
,
197 IN EFI_HANDLE FvBlockHandle
,
202 Restarts a previously interrupted write. The caller must provide the
203 block protocol needed to complete the interrupted write.
205 @param This Calling context.
206 @param FvBlockHandle The handle of FVB protocol that provides services for
207 reading, writing, and erasing the target block.
209 @retval EFI_SUCCESS The function completed successfully
210 @retval EFI_ACCESS_DENIED No pending writes exist
211 @retval EFI_NOT_FOUND FVB protocol not found by the handle
212 @retval EFI_ABORTED The function could not complete successfully
218 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
219 IN EFI_HANDLE FvBlockHandle
223 Aborts all previous allocated writes.
225 @param This Calling context
227 @retval EFI_SUCCESS The function completed successfully
228 @retval EFI_ABORTED The function could not complete successfully.
229 @retval EFI_NOT_FOUND No allocated writes exist.
235 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
239 Starts a target block update. This records information about the write
240 in fault tolerant storage and will complete the write in a recoverable
241 manner, ensuring at all times that either the original contents or
242 the modified contents are available.
244 @param This Indicates a pointer to the calling context.
245 @param CallerId The GUID identifying the last write.
246 @param Lba The logical block address of the last write.
247 @param Offset The offset within the block of the last write.
248 @param Length The length of the last write.
249 @param PrivateDataSize bytes from the private data
250 stored for this write.
251 @param PrivateData A pointer to a buffer. The function will copy
252 @param Complete A Boolean value with TRUE indicating
253 that the write was completed.
255 @retval EFI_SUCCESS The function completed successfully
256 @retval EFI_ABORTED The function could not complete successfully
257 @retval EFI_NOT_FOUND No allocated writes exist
258 @retval EFI_BUFFER_TOO_SMALL Input buffer is not larget enough
264 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
265 OUT EFI_GUID
*CallerId
,
269 IN OUT UINTN
*PrivateDataSize
,
270 OUT VOID
*PrivateData
,
271 OUT BOOLEAN
*Complete
277 @param FtwDevice The private data of FTW driver
279 @retval EFI_SUCCESS The erase request was successfully completed.
280 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
281 @retval EFI_DEVICE_ERROR The block device is not functioning
282 correctly and could not be written.
283 The firmware device may have been
285 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed
286 in the variable argument list do
287 not exist in the firmware volume.
293 IN EFI_FTW_DEVICE
*FtwDevice
297 Retrieve the proper FVB protocol interface by HANDLE.
300 @param FvBlockHandle The handle of FVB protocol that provides services for
301 reading, writing, and erasing the target block.
302 @param FvBlock The interface of FVB protocol
304 @retval EFI_SUCCESS The function completed successfully
305 @retval EFI_ABORTED The function could not complete successfully
310 IN EFI_HANDLE FvBlockHandle
,
311 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
316 Is it in working block?
318 @param FtwDevice The private data of FTW driver
319 @param FvBlock Fvb protocol instance
320 @param Lba The block specified
322 @return A BOOLEAN value indicating in working block or not.
327 EFI_FTW_DEVICE
*FtwDevice
,
328 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
336 @param FtwDevice The private data of FTW driver
337 @param FvBlock Fvb protocol instance
339 @return A BOOLEAN value indicating in boot block or not.
344 EFI_FTW_DEVICE
*FtwDevice
,
345 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
349 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
350 Spare block is accessed by FTW backup FVB protocol interface.
351 Target block is accessed by FvBlock protocol interface.
354 @param FtwDevice The private data of FTW driver
355 @param FvBlock FVB Protocol interface to access target block
356 @param Lba Lba of the target block
357 @param BlockSize The size of the block
358 @param NumberOfBlocks The number of consecutive blocks starting with Lba
360 @retval EFI_SUCCESS Spare block content is copied to target block
361 @retval EFI_INVALID_PARAMETER Input parameter error
362 @retval EFI_OUT_OF_RESOURCES Allocate memory error
363 @retval EFI_ABORTED The function could not complete successfully
367 FlushSpareBlockToTargetBlock (
368 EFI_FTW_DEVICE
*FtwDevice
,
369 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
376 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
377 Spare block is accessed by FTW backup FVB protocol interface. LBA is
378 FtwDevice->FtwSpareLba.
379 Working block is accessed by FTW working FVB protocol interface. LBA is
380 FtwDevice->FtwWorkBlockLba.
382 Since the working block header is important when FTW initializes, the
383 state of the operation should be handled carefully. The Crc value is
384 calculated without STATE element.
386 @param FtwDevice The private data of FTW driver
388 @retval EFI_SUCCESS Spare block content is copied to target block
389 @retval EFI_OUT_OF_RESOURCES Allocate memory error
390 @retval EFI_ABORTED The function could not complete successfully
394 FlushSpareBlockToWorkingBlock (
395 EFI_FTW_DEVICE
*FtwDevice
399 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
400 Spare block is accessed by FTW working FVB protocol interface.
401 Target block is accessed by FvBlock protocol interface.
403 FTW will do extra work on boot block update.
404 FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL,
405 which is produced by a chipset driver.
406 FTW updating boot block steps may be:
407 1. GetRangeLocation(), if the Range is inside the boot block, FTW know
408 that boot block will be update. It shall add a FLAG in the working block.
409 2. When spare block is ready,
410 3. SetSwapState(SWAPPED)
411 4. erasing boot block,
412 5. programming boot block until the boot block is ok.
413 6. SetSwapState(UNSWAPPED)
414 FTW shall not allow to update boot block when battery state is error.
416 @param FtwDevice The private data of FTW driver
418 @retval EFI_SUCCESS Spare block content is copied to boot block
419 @retval EFI_INVALID_PARAMETER Input parameter error
420 @retval EFI_OUT_OF_RESOURCES Allocate memory error
421 @retval EFI_ABORTED The function could not complete successfully
425 FlushSpareBlockToBootBlock (
426 EFI_FTW_DEVICE
*FtwDevice
430 Update a bit of state on a block device. The location of the bit is
431 calculated by the (Lba, Offset, bit). Here bit is determined by the
432 the name of a certain bit.
435 @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
436 @param BlockSize The size of the block
437 @param Lba Lba of a block
438 @param Offset Offset on the Lba
439 @param NewBit New value that will override the old value if it can be change
441 @retval EFI_SUCCESS A state bit has been updated successfully
442 @retval Others Access block device error.
444 Assume all bits of State are inside the same BYTE.
445 @retval EFI_ABORTED Read block fail
450 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
458 Get the last Write Header pointer.
459 The last write header is the header whose 'complete' state hasn't been set.
460 After all, this header may be a EMPTY header entry for next Allocate.
463 @param FtwWorkSpaceHeader Pointer of the working block header
464 @param FtwWorkSpaceSize Size of the work space
465 @param FtwWriteHeader Pointer to retrieve the last write header
467 @retval EFI_SUCCESS Get the last write record successfully
468 @retval EFI_ABORTED The FTW work space is damaged
472 FtwGetLastWriteHeader (
473 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*FtwWorkSpaceHeader
,
474 IN UINTN FtwWorkSpaceSize
,
475 OUT EFI_FAULT_TOLERANT_WRITE_HEADER
**FtwWriteHeader
479 Get the last Write Record pointer. The last write Record is the Record
480 whose DestinationCompleted state hasn't been set. After all, this Record
481 may be a EMPTY record entry for next write.
484 @param FtwWriteHeader Pointer to the write record header
485 @param FtwWriteRecord Pointer to retrieve the last write record
487 @retval EFI_SUCCESS Get the last write record successfully
488 @retval EFI_ABORTED The FTW work space is damaged
492 FtwGetLastWriteRecord (
493 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwWriteHeader
,
494 OUT EFI_FAULT_TOLERANT_WRITE_RECORD
**FtwWriteRecord
498 To check if FtwRecord is the first record of FtwHeader.
500 @param FtwHeader Pointer to the write record header
501 @param FtwRecord Pointer to the write record
503 @retval TRUE FtwRecord is the first Record of the FtwHeader
504 @retval FALSE FtwRecord is not the first Record of the FtwHeader
508 IsFirstRecordOfWrites (
509 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
510 IN EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwRecord
514 To check if FtwRecord is the last record of FtwHeader. Because the
515 FtwHeader has NumberOfWrites & PrivateDataSize, the FtwRecord can be
516 determined if it is the last record of FtwHeader.
518 @param FtwHeader Pointer to the write record header
519 @param FtwRecord Pointer to the write record
521 @retval TRUE FtwRecord is the last Record of the FtwHeader
522 @retval FALSE FtwRecord is not the last Record of the FtwHeader
526 IsLastRecordOfWrites (
527 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
528 IN EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwRecord
532 To check if FtwRecord is the first record of FtwHeader.
534 @param FtwHeader Pointer to the write record header
535 @param FtwRecord Pointer to retrieve the previous write record
537 @retval EFI_ACCESS_DENIED Input record is the first record, no previous record is return.
538 @retval EFI_SUCCESS The previous write record is found.
542 GetPreviousRecordOfWrites (
543 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
544 IN OUT EFI_FAULT_TOLERANT_WRITE_RECORD
**FtwRecord
549 Check whether a flash buffer is erased.
551 @param Buffer Buffer to check
552 @param BufferSize Size of the buffer
554 @return A BOOLEAN value indicating erased or not.
558 IsErasedFlashBuffer (
564 Initialize a work space when there is no work space.
566 @param WorkingHeader Pointer of working block header
568 @retval EFI_SUCCESS The function completed successfully
569 @retval EFI_ABORTED The function could not complete successfully.
573 InitWorkSpaceHeader (
574 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
578 Read from working block to refresh the work space in memory.
580 @param FtwDevice Point to private data of FTW driver
582 @retval EFI_SUCCESS The function completed successfully
583 @retval EFI_ABORTED The function could not complete successfully.
588 IN EFI_FTW_DEVICE
*FtwDevice
592 Check to see if it is a valid work space.
595 @param WorkingHeader Pointer of working block header
597 @retval EFI_SUCCESS The function completed successfully
598 @retval EFI_ABORTED The function could not complete successfully.
603 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
607 Reclaim the work space on the working block.
609 @param FtwDevice Point to private data of FTW driver
610 @param PreserveRecord Whether to preserve the working record is needed
612 @retval EFI_SUCCESS The function completed successfully
613 @retval EFI_OUT_OF_RESOURCES Allocate memory error
614 @retval EFI_ABORTED The function could not complete successfully
618 FtwReclaimWorkSpace (
619 IN EFI_FTW_DEVICE
*FtwDevice
,
620 IN BOOLEAN PreserveRecord
625 Get firmware volume block by address.
628 @param Address Address specified the block
629 @param FvBlock The block caller wanted
631 @retval EFI_SUCCESS The protocol instance if found.
632 @retval EFI_NOT_FOUND Block not found
637 IN EFI_PHYSICAL_ADDRESS Address
,
638 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
642 Retrieve the proper Swap Address Range protocol interface.
644 @param[out] SarProtocol The interface of SAR protocol
646 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
647 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
648 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
653 OUT VOID
**SarProtocol
657 Function returns an array of handles that support the FVB protocol
658 in a buffer allocated from pool.
660 @param[out] NumberHandles The number of handles returned in Buffer.
661 @param[out] Buffer A pointer to the buffer to return the requested
662 array of handles that support FVB protocol.
664 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
665 handles in Buffer was returned in NumberHandles.
666 @retval EFI_NOT_FOUND No FVB handle was found.
667 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
668 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
672 GetFvbCountAndBuffer (
673 OUT UINTN
*NumberHandles
,
674 OUT EFI_HANDLE
**Buffer
678 Allocate private data for FTW driver and initialize it.
680 @param[out] FtwData Pointer to the FTW device structure
682 @retval EFI_SUCCESS Initialize the FTW device successfully.
683 @retval EFI_OUT_OF_RESOURCES Allocate memory error
684 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
689 OUT EFI_FTW_DEVICE
**FtwData
693 Initialization for Fault Tolerant Write is done in this handler.
695 @param[in, out] FtwDevice Pointer to the FTW device structure
697 @retval EFI_SUCCESS Initialize the FTW protocol successfully.
698 @retval EFI_NOT_FOUND No proper FVB protocol was found.
703 IN OUT EFI_FTW_DEVICE
*FtwDevice
707 Initialize a local work space header.
709 Since Signature and WriteQueueSize have been known, Crc can be calculated out,
710 then the work space header will be fixed.
713 InitializeLocalWorkSpaceHeader (
718 Read work space data from work block or spare block.
720 @param FvBlock FVB Protocol interface to access the block.
721 @param BlockSize The size of the block.
722 @param Lba Lba of the block.
723 @param Offset The offset within the block.
724 @param Length The number of bytes to read from the block.
725 @param Buffer The data is read.
727 @retval EFI_SUCCESS The function completed successfully.
728 @retval EFI_ABORTED The function could not complete successfully.
733 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
742 Write data to work block.
744 @param FvBlock FVB Protocol interface to access the block.
745 @param BlockSize The size of the block.
746 @param Lba Lba of the block.
747 @param Offset The offset within the block to place the data.
748 @param Length The number of bytes to write to the block.
749 @param Buffer The data to write.
751 @retval EFI_SUCCESS The function completed successfully.
752 @retval EFI_ABORTED The function could not complete successfully.
757 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
766 Internal implementation of CRC32. Depending on the execution context
767 (traditional SMM or DXE vs standalone MM), this function is implemented
768 via a call to the CalculateCrc32 () boot service, or via a library
771 If Buffer is NULL, then ASSERT().
772 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
774 @param[in] Buffer A pointer to the buffer on which the 32-bit CRC is
776 @param[in] Length The number of bytes in the buffer Data.
778 @retval Crc32 The 32-bit CRC was computed for the data buffer.