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 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 #ifndef _EFI_FAULT_TOLERANT_WRITE_H_
18 #define _EFI_FAULT_TOLERANT_WRITE_H_
22 #include <Guid/SystemNvDataGuid.h>
23 #include <Guid/ZeroGuid.h>
24 #include <Protocol/FaultTolerantWrite.h>
25 #include <Protocol/FirmwareVolumeBlock.h>
26 #include <Protocol/SwapAddressRange.h>
28 #include <Library/PcdLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/UefiLib.h>
31 #include <Library/UefiDriverEntryPoint.h>
32 #include <Library/BaseMemoryLib.h>
33 #include <Library/MemoryAllocationLib.h>
34 #include <Library/ReportStatusCodeLib.h>
37 // Flash erase polarity is 1
39 #define FTW_ERASE_POLARITY 1
41 #define FTW_ERASED_BYTE ((UINT8) (255))
42 #define FTW_POLARITY_REVERT ((UINT8) (255))
44 #define HEADER_ALLOCATED 0x1
45 #define WRITES_ALLOCATED 0x2
46 #define WRITES_COMPLETED 0x4
48 #define BOOT_BLOCK_UPDATE 0x1
49 #define SPARE_COMPLETED 0x2
50 #define DEST_COMPLETED 0x4
52 #define FTW_BLOCKS(Length, BlockSize) ((UINTN) ((Length) / (BlockSize) + (((Length) & ((BlockSize) - 1)) ? 1 : 0)))
54 #define FTW_DEVICE_SIGNATURE SIGNATURE_32 ('F', 'T', 'W', 'D')
57 // EFI Fault tolerant protocol private data structure
62 EFI_FAULT_TOLERANT_WRITE_PROTOCOL FtwInstance
;
63 EFI_PHYSICAL_ADDRESS WorkSpaceAddress
; // Base address of working space range in flash.
64 EFI_PHYSICAL_ADDRESS SpareAreaAddress
; // Base address of spare range in flash.
65 UINTN WorkSpaceLength
; // Size of working space range in flash.
66 UINTN NumberOfWorkSpaceBlock
; // Number of the blocks in work block for work space.
67 UINTN WorkBlockSize
; // Block size in bytes of the work blocks in flash
68 UINTN SpareAreaLength
; // Size of spare range in flash.
69 UINTN NumberOfSpareBlock
; // Number of the blocks in spare block.
70 UINTN SpareBlockSize
; // Block size in bytes of the spare blocks in flash
71 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*FtwWorkSpaceHeader
;// Pointer to Working Space Header in memory buffer
72 EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwLastWriteHeader
;// Pointer to last record header in memory buffer
73 EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwLastWriteRecord
;// Pointer to last record in memory buffer
74 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FtwFvBlock
; // FVB of working block
75 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FtwBackupFvb
; // FVB of spare block
76 EFI_LBA FtwSpareLba
; // Start LBA of spare block
77 EFI_LBA FtwWorkBlockLba
; // Start LBA of working block that contains working space in its last block.
78 UINTN NumberOfWorkBlock
; // Number of the blocks in work block.
79 EFI_LBA FtwWorkSpaceLba
; // Start LBA of working space
80 UINTN FtwWorkSpaceBase
; // Offset into the FtwWorkSpaceLba block.
81 UINTN FtwWorkSpaceSize
; // Size of working space range that stores write record.
82 EFI_LBA FtwWorkSpaceLbaInSpare
; // Start LBA of working space in spare block.
83 UINTN FtwWorkSpaceBaseInSpare
;// Offset into the FtwWorkSpaceLbaInSpare block.
84 UINT8
*FtwWorkSpace
; // Point to Work Space in memory buffer
86 // Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE],
87 // Allocated with EFI_FTW_DEVICE.
91 #define FTW_CONTEXT_FROM_THIS(a) CR (a, EFI_FTW_DEVICE, FtwInstance, FTW_DEVICE_SIGNATURE)
97 This function is the entry point of the Fault Tolerant Write driver.
99 @param ImageHandle A handle for the image that is initializing this driver
100 @param SystemTable A pointer to the EFI system table
102 @return EFI_SUCCESS FTW has finished the initialization
103 @retval EFI_NOT_FOUND Locate FVB protocol error
104 @retval EFI_OUT_OF_RESOURCES Allocate memory error
105 @retval EFI_VOLUME_CORRUPTED Firmware volume is error
106 @retval EFI_ABORTED FTW initialization error
111 InitializeFaultTolerantWrite (
112 IN EFI_HANDLE ImageHandle
,
113 IN EFI_SYSTEM_TABLE
*SystemTable
117 // Fault Tolerant Write Protocol API
121 Query the largest block that may be updated in a fault tolerant manner.
124 @param This Indicates a pointer to the calling context.
125 @param BlockSize A pointer to a caller allocated UINTN that is updated to
126 indicate the size of the largest block that can be updated.
128 @return EFI_SUCCESS The function completed successfully
134 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
139 Allocates space for the protocol to maintain information about writes.
140 Since writes must be completed in a fault tolerant manner and multiple
141 updates will require more resources to be successful, this function
142 enables the protocol to ensure that enough space exists to track
143 information about the upcoming writes.
145 All writes must be completed or aborted before another fault tolerant write can occur.
147 @param This Indicates a pointer to the calling context.
148 @param CallerId The GUID identifying the write.
149 @param PrivateDataSize The size of the caller's private data
150 that must be recorded for each write.
151 @param NumberOfWrites The number of fault tolerant block writes
152 that will need to occur.
154 @return EFI_SUCCESS The function completed successfully
155 @retval EFI_ABORTED The function could not complete successfully.
156 @retval EFI_ACCESS_DENIED All allocated writes have not been completed.
162 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
163 IN EFI_GUID
*CallerId
,
164 IN UINTN PrivateDataSize
,
165 IN UINTN NumberOfWrites
169 Starts a target block update. This function will record data about write
170 in fault tolerant storage and will complete the write in a recoverable
171 manner, ensuring at all times that either the original contents or
172 the modified contents are available.
175 @param This Calling context
176 @param Lba The logical block address of the target block.
177 @param Offset The offset within the target block to place the data.
178 @param Length The number of bytes to write to the target block.
179 @param PrivateData A pointer to private data that the caller requires to
180 complete any pending writes in the event of a fault.
181 @param FvBlockHandle The handle of FVB protocol that provides services for
182 reading, writing, and erasing the target block.
183 @param Buffer The data to write.
185 @retval EFI_SUCCESS The function completed successfully
186 @retval EFI_ABORTED The function could not complete successfully.
187 @retval EFI_BAD_BUFFER_SIZE The input data can't fit within the spare block.
188 Offset + *NumBytes > SpareAreaLength.
189 @retval EFI_ACCESS_DENIED No writes have been allocated.
190 @retval EFI_OUT_OF_RESOURCES Cannot allocate enough memory resource.
191 @retval EFI_NOT_FOUND Cannot find FVB protocol by handle.
197 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
201 IN VOID
*PrivateData
,
202 IN EFI_HANDLE FvBlockHandle
,
207 Restarts a previously interrupted write. The caller must provide the
208 block protocol needed to complete the interrupted write.
210 @param This Calling context.
211 @param FvBlockHandle The handle of FVB protocol that provides services for
212 reading, writing, and erasing the target block.
214 @retval EFI_SUCCESS The function completed successfully
215 @retval EFI_ACCESS_DENIED No pending writes exist
216 @retval EFI_NOT_FOUND FVB protocol not found by the handle
217 @retval EFI_ABORTED The function could not complete successfully
223 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
224 IN EFI_HANDLE FvBlockHandle
228 Aborts all previous allocated writes.
230 @param This Calling context
232 @retval EFI_SUCCESS The function completed successfully
233 @retval EFI_ABORTED The function could not complete successfully.
234 @retval EFI_NOT_FOUND No allocated writes exist.
240 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
244 Starts a target block update. This records information about the write
245 in fault tolerant storage and will complete the write in a recoverable
246 manner, ensuring at all times that either the original contents or
247 the modified contents are available.
249 @param This Indicates a pointer to the calling context.
250 @param CallerId The GUID identifying the last write.
251 @param Lba The logical block address of the last write.
252 @param Offset The offset within the block of the last write.
253 @param Length The length of the last write.
254 @param PrivateDataSize bytes from the private data
255 stored for this write.
256 @param PrivateData A pointer to a buffer. The function will copy
257 @param Complete A Boolean value with TRUE indicating
258 that the write was completed.
260 @retval EFI_SUCCESS The function completed successfully
261 @retval EFI_ABORTED The function could not complete successfully
262 @retval EFI_NOT_FOUND No allocated writes exist
263 @retval EFI_BUFFER_TOO_SMALL Input buffer is not larget enough
269 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
270 OUT EFI_GUID
*CallerId
,
274 IN OUT UINTN
*PrivateDataSize
,
275 OUT VOID
*PrivateData
,
276 OUT BOOLEAN
*Complete
282 @param FtwDevice The private data of FTW driver
284 @retval EFI_SUCCESS The erase request was successfully completed.
285 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
286 @retval EFI_DEVICE_ERROR The block device is not functioning
287 correctly and could not be written.
288 The firmware device may have been
290 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed
291 in the variable argument list do
292 not exist in the firmware volume.
298 IN EFI_FTW_DEVICE
*FtwDevice
302 Retrieve the proper FVB protocol interface by HANDLE.
305 @param FvBlockHandle The handle of FVB protocol that provides services for
306 reading, writing, and erasing the target block.
307 @param FvBlock The interface of FVB protocol
309 @retval EFI_SUCCESS The function completed successfully
310 @retval EFI_ABORTED The function could not complete successfully
315 IN EFI_HANDLE FvBlockHandle
,
316 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
321 Is it in working block?
323 @param FtwDevice The private data of FTW driver
324 @param FvBlock Fvb protocol instance
325 @param Lba The block specified
327 @return A BOOLEAN value indicating in working block or not.
332 EFI_FTW_DEVICE
*FtwDevice
,
333 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
341 @param FtwDevice The private data of FTW driver
342 @param FvBlock Fvb protocol instance
344 @return A BOOLEAN value indicating in boot block or not.
349 EFI_FTW_DEVICE
*FtwDevice
,
350 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
354 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
355 Spare block is accessed by FTW backup FVB protocol interface.
356 Target block is accessed by FvBlock protocol interface.
359 @param FtwDevice The private data of FTW driver
360 @param FvBlock FVB Protocol interface to access target block
361 @param Lba Lba of the target block
362 @param BlockSize The size of the block
363 @param NumberOfBlocks The number of consecutive blocks starting with Lba
365 @retval EFI_SUCCESS Spare block content is copied to target block
366 @retval EFI_INVALID_PARAMETER Input parameter error
367 @retval EFI_OUT_OF_RESOURCES Allocate memory error
368 @retval EFI_ABORTED The function could not complete successfully
372 FlushSpareBlockToTargetBlock (
373 EFI_FTW_DEVICE
*FtwDevice
,
374 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
381 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
382 Spare block is accessed by FTW backup FVB protocol interface. LBA is
383 FtwDevice->FtwSpareLba.
384 Working block is accessed by FTW working FVB protocol interface. LBA is
385 FtwDevice->FtwWorkBlockLba.
387 Since the working block header is important when FTW initializes, the
388 state of the operation should be handled carefully. The Crc value is
389 calculated without STATE element.
391 @param FtwDevice The private data of FTW driver
393 @retval EFI_SUCCESS Spare block content is copied to target block
394 @retval EFI_OUT_OF_RESOURCES Allocate memory error
395 @retval EFI_ABORTED The function could not complete successfully
399 FlushSpareBlockToWorkingBlock (
400 EFI_FTW_DEVICE
*FtwDevice
404 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
405 Spare block is accessed by FTW working FVB protocol interface.
406 Target block is accessed by FvBlock protocol interface.
408 FTW will do extra work on boot block update.
409 FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL,
410 which is produced by a chipset driver.
411 FTW updating boot block steps may be:
412 1. GetRangeLocation(), if the Range is inside the boot block, FTW know
413 that boot block will be update. It shall add a FLAG in the working block.
414 2. When spare block is ready,
415 3. SetSwapState(SWAPPED)
416 4. erasing boot block,
417 5. programming boot block until the boot block is ok.
418 6. SetSwapState(UNSWAPPED)
419 FTW shall not allow to update boot block when battery state is error.
421 @param FtwDevice The private data of FTW driver
423 @retval EFI_SUCCESS Spare block content is copied to boot block
424 @retval EFI_INVALID_PARAMETER Input parameter error
425 @retval EFI_OUT_OF_RESOURCES Allocate memory error
426 @retval EFI_ABORTED The function could not complete successfully
430 FlushSpareBlockToBootBlock (
431 EFI_FTW_DEVICE
*FtwDevice
435 Update a bit of state on a block device. The location of the bit is
436 calculated by the (Lba, Offset, bit). Here bit is determined by the
437 the name of a certain bit.
440 @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
441 @param BlockSize The size of the block
442 @param Lba Lba of a block
443 @param Offset Offset on the Lba
444 @param NewBit New value that will override the old value if it can be change
446 @retval EFI_SUCCESS A state bit has been updated successfully
447 @retval Others Access block device error.
449 Assume all bits of State are inside the same BYTE.
450 @retval EFI_ABORTED Read block fail
455 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
463 Get the last Write Header pointer.
464 The last write header is the header whose 'complete' state hasn't been set.
465 After all, this header may be a EMPTY header entry for next Allocate.
468 @param FtwWorkSpaceHeader Pointer of the working block header
469 @param FtwWorkSpaceSize Size of the work space
470 @param FtwWriteHeader Pointer to retrieve the last write header
472 @retval EFI_SUCCESS Get the last write record successfully
473 @retval EFI_ABORTED The FTW work space is damaged
477 FtwGetLastWriteHeader (
478 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*FtwWorkSpaceHeader
,
479 IN UINTN FtwWorkSpaceSize
,
480 OUT EFI_FAULT_TOLERANT_WRITE_HEADER
**FtwWriteHeader
484 Get the last Write Record pointer. The last write Record is the Record
485 whose DestinationCompleted state hasn't been set. After all, this Record
486 may be a EMPTY record entry for next write.
489 @param FtwWriteHeader Pointer to the write record header
490 @param FtwWriteRecord Pointer to retrieve the last write record
492 @retval EFI_SUCCESS Get the last write record successfully
493 @retval EFI_ABORTED The FTW work space is damaged
497 FtwGetLastWriteRecord (
498 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwWriteHeader
,
499 OUT EFI_FAULT_TOLERANT_WRITE_RECORD
**FtwWriteRecord
503 To check if FtwRecord is the first record of FtwHeader.
505 @param FtwHeader Pointer to the write record header
506 @param FtwRecord Pointer to the write record
508 @retval TRUE FtwRecord is the first Record of the FtwHeader
509 @retval FALSE FtwRecord is not the first Record of the FtwHeader
513 IsFirstRecordOfWrites (
514 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
515 IN EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwRecord
519 To check if FtwRecord is the last record of FtwHeader. Because the
520 FtwHeader has NumberOfWrites & PrivateDataSize, the FtwRecord can be
521 determined if it is the last record of FtwHeader.
523 @param FtwHeader Pointer to the write record header
524 @param FtwRecord Pointer to the write record
526 @retval TRUE FtwRecord is the last Record of the FtwHeader
527 @retval FALSE FtwRecord is not the last Record of the FtwHeader
531 IsLastRecordOfWrites (
532 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
533 IN EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwRecord
537 To check if FtwRecord is the first record of FtwHeader.
539 @param FtwHeader Pointer to the write record header
540 @param FtwRecord Pointer to retrieve the previous write record
542 @retval EFI_ACCESS_DENIED Input record is the first record, no previous record is return.
543 @retval EFI_SUCCESS The previous write record is found.
547 GetPreviousRecordOfWrites (
548 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
549 IN OUT EFI_FAULT_TOLERANT_WRITE_RECORD
**FtwRecord
554 Check whether a flash buffer is erased.
556 @param Buffer Buffer to check
557 @param BufferSize Size of the buffer
559 @return A BOOLEAN value indicating erased or not.
563 IsErasedFlashBuffer (
568 Initialize a work space when there is no work space.
570 @param WorkingHeader Pointer of working block header
572 @retval EFI_SUCCESS The function completed successfully
573 @retval EFI_ABORTED The function could not complete successfully.
577 InitWorkSpaceHeader (
578 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
581 Read from working block to refresh the work space in memory.
583 @param FtwDevice Point to private data of FTW driver
585 @retval EFI_SUCCESS The function completed successfully
586 @retval EFI_ABORTED The function could not complete successfully.
591 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
608 Reclaim the work space on the working block.
610 @param FtwDevice Point to private data of FTW driver
611 @param PreserveRecord Whether to preserve the working record is needed
613 @retval EFI_SUCCESS The function completed successfully
614 @retval EFI_OUT_OF_RESOURCES Allocate memory error
615 @retval EFI_ABORTED The function could not complete successfully
619 FtwReclaimWorkSpace (
620 IN EFI_FTW_DEVICE
*FtwDevice
,
621 IN BOOLEAN PreserveRecord
626 Get firmware volume block by address.
629 @param Address Address specified the block
630 @param FvBlock The block caller wanted
632 @retval EFI_SUCCESS The protocol instance if found.
633 @retval EFI_NOT_FOUND Block not found
638 IN EFI_PHYSICAL_ADDRESS Address
,
639 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
643 Retrieve the proper Swap Address Range protocol interface.
645 @param[out] SarProtocol The interface of SAR protocol
647 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
648 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
649 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
654 OUT VOID
**SarProtocol
658 Function returns an array of handles that support the FVB protocol
659 in a buffer allocated from pool.
661 @param[out] NumberHandles The number of handles returned in Buffer.
662 @param[out] Buffer A pointer to the buffer to return the requested
663 array of handles that support FVB protocol.
665 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
666 handles in Buffer was returned in NumberHandles.
667 @retval EFI_NOT_FOUND No FVB handle was found.
668 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
669 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
673 GetFvbCountAndBuffer (
674 OUT UINTN
*NumberHandles
,
675 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
696 Initialization for Fault Tolerant Write is done in this handler.
698 @param[in, out] FtwDevice Pointer to the FTW device structure
700 @retval EFI_SUCCESS Initialize the FTW protocol successfully.
701 @retval EFI_NOT_FOUND No proper FVB protocol was found.
706 IN OUT EFI_FTW_DEVICE
*FtwDevice
710 Initialize a local work space header.
712 Since Signature and WriteQueueSize have been known, Crc can be calculated out,
713 then the work space header will be fixed.
716 InitializeLocalWorkSpaceHeader (
721 Read work space data from work block or spare block.
723 @param FvBlock FVB Protocol interface to access the block.
724 @param BlockSize The size of the block.
725 @param Lba Lba of the block.
726 @param Offset The offset within the block.
727 @param Length The number of bytes to read from the block.
728 @param Buffer The data is read.
730 @retval EFI_SUCCESS The function completed successfully.
731 @retval EFI_ABORTED The function could not complete successfully.
736 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
745 Write data to work block.
747 @param FvBlock FVB Protocol interface to access the block.
748 @param BlockSize The size of the block.
749 @param Lba Lba of the block.
750 @param Offset The offset within the block to place the data.
751 @param Length The number of bytes to write to the block.
752 @param Buffer The data to write.
754 @retval EFI_SUCCESS The function completed successfully.
755 @retval EFI_ABORTED The function could not complete successfully.
760 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
769 Internal implementation of CRC32. Depending on the execution context
770 (traditional SMM or DXE vs standalone MM), this function is implemented
771 via a call to the CalculateCrc32 () boot service, or via a library
774 If Buffer is NULL, then ASSERT().
775 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
777 @param[in] Buffer A pointer to the buffer on which the 32-bit CRC is
779 @param[in] Length The number of bytes in the buffer Data.
781 @retval Crc32 The 32-bit CRC was computed for the data buffer.