3 The internal header file includes the common header files, defines
4 internal structure and functions used by FtwLite module.
6 Copyright (c) 2006 - 2010, 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 <Protocol/FaultTolerantWrite.h>
24 #include <Protocol/FirmwareVolumeBlock.h>
25 #include <Protocol/SwapAddressRange.h>
27 #include <Library/PcdLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/UefiLib.h>
30 #include <Library/UefiDriverEntryPoint.h>
31 #include <Library/BaseMemoryLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/UefiBootServicesTableLib.h>
36 // Flash erase polarity is 1
38 #define FTW_ERASE_POLARITY 1
40 #define FTW_VALID_STATE 0
41 #define FTW_INVALID_STATE 1
43 #define FTW_ERASED_BYTE ((UINT8) (255))
44 #define FTW_POLARITY_REVERT ((UINT8) (255))
47 // EFI Fault tolerant block update write queue entry
50 UINT8 HeaderAllocated
: 1;
51 UINT8 WritesAllocated
: 1;
53 #define HEADER_ALLOCATED 0x1
54 #define WRITES_ALLOCATED 0x2
55 #define WRITES_COMPLETED 0x4
59 UINTN PrivateDataSize
;
60 } EFI_FAULT_TOLERANT_WRITE_HEADER
;
63 // EFI Fault tolerant block update write queue record
66 UINT8 BootBlockUpdate
: 1;
67 UINT8 SpareComplete
: 1;
68 UINT8 DestinationComplete
: 1;
69 #define BOOT_BLOCK_UPDATE 0x1
70 #define SPARE_COMPLETED 0x2
71 #define DEST_COMPLETED 0x4
76 EFI_PHYSICAL_ADDRESS FvBaseAddress
;
78 // UINT8 PrivateData[PrivateDataSize]
80 } EFI_FAULT_TOLERANT_WRITE_RECORD
;
83 #define RECORD_SIZE(PrivateDataSize) (sizeof (EFI_FAULT_TOLERANT_WRITE_RECORD) + PrivateDataSize)
85 #define RECORD_TOTAL_SIZE(NumberOfWrites, PrivateDataSize) \
86 ((NumberOfWrites) * (sizeof (EFI_FAULT_TOLERANT_WRITE_RECORD) + PrivateDataSize))
88 #define WRITE_TOTAL_SIZE(NumberOfWrites, PrivateDataSize) \
90 sizeof (EFI_FAULT_TOLERANT_WRITE_HEADER) + (NumberOfWrites) * \
91 (sizeof (EFI_FAULT_TOLERANT_WRITE_RECORD) + PrivateDataSize) \
94 #define FTW_DEVICE_SIGNATURE SIGNATURE_32 ('F', 'T', 'W', 'D')
97 // EFI Fault tolerant protocol private data structure
102 EFI_FAULT_TOLERANT_WRITE_PROTOCOL FtwInstance
;
103 EFI_PHYSICAL_ADDRESS WorkSpaceAddress
; // Base address of working space range in flash.
104 EFI_PHYSICAL_ADDRESS SpareAreaAddress
; // Base address of spare range in flash.
105 UINTN WorkSpaceLength
; // Size of working space range in flash.
106 UINTN SpareAreaLength
; // Size of spare range in flash.
107 UINTN NumberOfSpareBlock
; // Number of the blocks in spare block.
108 UINTN BlockSize
; // Block size in bytes of the blocks in flash
109 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*FtwWorkSpaceHeader
;// Pointer to Working Space Header in memory buffer
110 EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwLastWriteHeader
;// Pointer to last record header in memory buffer
111 EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwLastWriteRecord
;// Pointer to last record in memory buffer
112 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FtwFvBlock
; // FVB of working block
113 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FtwBackupFvb
; // FVB of spare block
114 EFI_LBA FtwSpareLba
; // Start LBA of spare block
115 EFI_LBA FtwWorkBlockLba
; // Start LBA of working block that contains working space in its last block.
116 EFI_LBA FtwWorkSpaceLba
; // Start LBA of working space
117 UINTN FtwWorkSpaceBase
; // Offset into the FtwWorkSpaceLba block.
118 UINTN FtwWorkSpaceSize
; // Size of working space range that stores write record.
119 UINT8
*FtwWorkSpace
; // Point to Work Space in memory buffer
121 // Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE],
122 // Allocated with EFI_FTW_DEVICE.
126 #define FTW_CONTEXT_FROM_THIS(a) CR (a, EFI_FTW_DEVICE, FtwInstance, FTW_DEVICE_SIGNATURE)
129 // Driver entry point
132 This function is the entry point of the Fault Tolerant Write driver.
134 @param ImageHandle A handle for the image that is initializing this driver
135 @param SystemTable A pointer to the EFI system table
137 @return EFI_SUCCESS FTW has finished the initialization
138 @retval EFI_NOT_FOUND Locate FVB protocol error
139 @retval EFI_OUT_OF_RESOURCES Allocate memory error
140 @retval EFI_VOLUME_CORRUPTED Firmware volume is error
141 @retval EFI_ABORTED FTW initialization error
146 InitializeFaultTolerantWrite (
147 IN EFI_HANDLE ImageHandle
,
148 IN EFI_SYSTEM_TABLE
*SystemTable
152 // Fault Tolerant Write Protocol API
156 Query the largest block that may be updated in a fault tolerant manner.
159 @param This Indicates a pointer to the calling context.
160 @param BlockSize A pointer to a caller allocated UINTN that is updated to
161 indicate the size of the largest block that can be updated.
163 @return EFI_SUCCESS The function completed successfully
169 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
174 Allocates space for the protocol to maintain information about writes.
175 Since writes must be completed in a fault tolerant manner and multiple
176 updates will require more resources to be successful, this function
177 enables the protocol to ensure that enough space exists to track
178 information about the upcoming writes.
180 All writes must be completed or aborted before another fault tolerant write can occur.
182 @param This Indicates a pointer to the calling context.
183 @param CallerId The GUID identifying the write.
184 @param PrivateDataSize The size of the caller's private data
185 that must be recorded for each write.
186 @param NumberOfWrites The number of fault tolerant block writes
187 that will need to occur.
189 @return EFI_SUCCESS The function completed successfully
190 @retval EFI_ABORTED The function could not complete successfully.
191 @retval EFI_ACCESS_DENIED All allocated writes have not been completed.
197 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
198 IN EFI_GUID
*CallerId
,
199 IN UINTN PrivateDataSize
,
200 IN UINTN NumberOfWrites
204 Starts a target block update. This function will record data about write
205 in fault tolerant storage and will complete the write in a recoverable
206 manner, ensuring at all times that either the original contents or
207 the modified contents are available.
210 @param This Calling context
211 @param Lba The logical block address of the target block.
212 @param Offset The offset within the target block to place the data.
213 @param Length The number of bytes to write to the target block.
214 @param PrivateData A pointer to private data that the caller requires to
215 complete any pending writes in the event of a fault.
216 @param FvBlockHandle The handle of FVB protocol that provides services for
217 reading, writing, and erasing the target block.
218 @param Buffer The data to write.
220 @retval EFI_SUCCESS The function completed successfully
221 @retval EFI_ABORTED The function could not complete successfully.
222 @retval EFI_BAD_BUFFER_SIZE The input data can't fit within the spare block.
223 Offset + *NumBytes > SpareAreaLength.
224 @retval EFI_ACCESS_DENIED No writes have been allocated.
225 @retval EFI_OUT_OF_RESOURCES Cannot allocate enough memory resource.
226 @retval EFI_NOT_FOUND Cannot find FVB protocol by handle.
232 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
236 IN VOID
*PrivateData
,
237 IN EFI_HANDLE FvBlockHandle
,
242 Restarts a previously interrupted write. The caller must provide the
243 block protocol needed to complete the interrupted write.
245 @param This Calling context.
246 @param FvBlockHandle The handle of FVB protocol that provides services for
247 reading, writing, and erasing the target block.
249 @retval EFI_SUCCESS The function completed successfully
250 @retval EFI_ACCESS_DENIED No pending writes exist
251 @retval EFI_NOT_FOUND FVB protocol not found by the handle
252 @retval EFI_ABORTED The function could not complete successfully
258 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
259 IN EFI_HANDLE FvBlockHandle
263 Aborts all previous allocated writes.
265 @param This Calling context
267 @retval EFI_SUCCESS The function completed successfully
268 @retval EFI_ABORTED The function could not complete successfully.
269 @retval EFI_NOT_FOUND No allocated writes exist.
275 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
279 Starts a target block update. This records information about the write
280 in fault tolerant storage and will complete the write in a recoverable
281 manner, ensuring at all times that either the original contents or
282 the modified contents are available.
284 @param This Indicates a pointer to the calling context.
285 @param CallerId The GUID identifying the last write.
286 @param Lba The logical block address of the last write.
287 @param Offset The offset within the block of the last write.
288 @param Length The length of the last write.
289 @param PrivateDataSize bytes from the private data
290 stored for this write.
291 @param PrivateData A pointer to a buffer. The function will copy
292 @param Complete A Boolean value with TRUE indicating
293 that the write was completed.
295 @retval EFI_SUCCESS The function completed successfully
296 @retval EFI_ABORTED The function could not complete successfully
297 @retval EFI_NOT_FOUND No allocated writes exist
298 @retval EFI_BUFFER_TOO_SMALL Input buffer is not larget enough
304 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
305 OUT EFI_GUID
*CallerId
,
309 IN OUT UINTN
*PrivateDataSize
,
310 OUT VOID
*PrivateData
,
311 OUT BOOLEAN
*Complete
317 @param FtwDevice The private data of FTW driver
319 @retval EFI_SUCCESS The erase request was successfully completed.
320 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
321 @retval EFI_DEVICE_ERROR The block device is not functioning
322 correctly and could not be written.
323 The firmware device may have been
325 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed
326 in the variable argument list do
327 not exist in the firmware volume.
333 IN EFI_FTW_DEVICE
*FtwDevice
337 Retrive the proper FVB protocol interface by HANDLE.
340 @param FvBlockHandle The handle of FVB protocol that provides services for
341 reading, writing, and erasing the target block.
342 @param FvBlock The interface of FVB protocol
344 @retval EFI_SUCCESS The function completed successfully
345 @retval EFI_ABORTED The function could not complete successfully
350 IN EFI_HANDLE FvBlockHandle
,
351 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
356 Is it in working block?
358 @param FtwDevice The private data of FTW driver
359 @param FvBlock Fvb protocol instance
360 @param Lba The block specified
362 @return A BOOLEAN value indicating in working block or not.
367 EFI_FTW_DEVICE
*FtwDevice
,
368 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
376 @param FtwDevice The private data of FTW driver
377 @param FvBlock Fvb protocol instance
378 @param Lba The block specified
380 @return A BOOLEAN value indicating in boot block or not.
385 EFI_FTW_DEVICE
*FtwDevice
,
386 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
391 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
392 Spare block is accessed by FTW backup FVB protocol interface. LBA is 1.
393 Target block is accessed by FvbBlock protocol interface. LBA is Lba.
396 @param FtwDevice The private data of FTW driver
397 @param FvBlock FVB Protocol interface to access target block
398 @param Lba Lba of the target block
400 @retval EFI_SUCCESS Spare block content is copied to target block
401 @retval EFI_INVALID_PARAMETER Input parameter error
402 @retval EFI_OUT_OF_RESOURCES Allocate memory error
403 @retval EFI_ABORTED The function could not complete successfully
407 FlushSpareBlockToTargetBlock (
408 EFI_FTW_DEVICE
*FtwDevice
,
409 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
414 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
415 Spare block is accessed by FTW backup FVB protocol interface. LBA is
416 FtwDevice->FtwSpareLba.
417 Working block is accessed by FTW working FVB protocol interface. LBA is
418 FtwDevice->FtwWorkBlockLba.
420 Since the working block header is important when FTW initializes, the
421 state of the operation should be handled carefully. The Crc value is
422 calculated without STATE element.
424 @param FtwDevice The private data of FTW driver
426 @retval EFI_SUCCESS Spare block content is copied to target block
427 @retval EFI_OUT_OF_RESOURCES Allocate memory error
428 @retval EFI_ABORTED The function could not complete successfully
432 FlushSpareBlockToWorkingBlock (
433 EFI_FTW_DEVICE
*FtwDevice
437 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
438 Spare block is accessed by FTW working FVB protocol interface. LBA is 1.
439 Target block is accessed by FvbBlock protocol interface. LBA is Lba.
441 FTW will do extra work on boot block update.
442 FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL,
443 which is produced by a chipset driver.
444 FTW updating boot block steps may be:
445 1. GetRangeLocation(), if the Range is inside the boot block, FTW know
446 that boot block will be update. It shall add a FLAG in the working block.
447 2. When spare block is ready,
448 3. SetSwapState(EFI_SWAPPED)
449 4. erasing boot block,
450 5. programming boot block until the boot block is ok.
451 6. SetSwapState(UNSWAPPED)
452 FTW shall not allow to update boot block when battery state is error.
454 @param FtwDevice The private data of FTW driver
456 @retval EFI_SUCCESS Spare block content is copied to boot block
457 @retval EFI_INVALID_PARAMETER Input parameter error
458 @retval EFI_OUT_OF_RESOURCES Allocate memory error
459 @retval EFI_ABORTED The function could not complete successfully
463 FlushSpareBlockToBootBlock (
464 EFI_FTW_DEVICE
*FtwDevice
468 Update a bit of state on a block device. The location of the bit is
469 calculated by the (Lba, Offset, bit). Here bit is determined by the
470 the name of a certain bit.
473 @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
474 @param Lba Lba of a block
475 @param Offset Offset on the Lba
476 @param NewBit New value that will override the old value if it can be change
478 @retval EFI_SUCCESS A state bit has been updated successfully
479 @retval Others Access block device error.
481 Assume all bits of State are inside the same BYTE.
482 @retval EFI_ABORTED Read block fail
487 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
494 Get the last Write Header pointer.
495 The last write header is the header whose 'complete' state hasn't been set.
496 After all, this header may be a EMPTY header entry for next Allocate.
499 @param FtwWorkSpaceHeader Pointer of the working block header
500 @param FtwWorkSpaceSize Size of the work space
501 @param FtwWriteHeader Pointer to retrieve the last write header
503 @retval EFI_SUCCESS Get the last write record successfully
504 @retval EFI_ABORTED The FTW work space is damaged
508 FtwGetLastWriteHeader (
509 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*FtwWorkSpaceHeader
,
510 IN UINTN FtwWorkSpaceSize
,
511 OUT EFI_FAULT_TOLERANT_WRITE_HEADER
**FtwWriteHeader
515 Get the last Write Record pointer. The last write Record is the Record
516 whose DestinationCompleted state hasn't been set. After all, this Record
517 may be a EMPTY record entry for next write.
520 @param FtwWriteHeader Pointer to the write record header
521 @param FtwWriteRecord Pointer to retrieve the last write record
523 @retval EFI_SUCCESS Get the last write record successfully
524 @retval EFI_ABORTED The FTW work space is damaged
528 FtwGetLastWriteRecord (
529 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwWriteHeader
,
530 OUT EFI_FAULT_TOLERANT_WRITE_RECORD
**FtwWriteRecord
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 the write record
539 @retval TRUE FtwRecord is the first Record of the FtwHeader
540 @retval FALSE FtwRecord is not the first Record of the FtwHeader
544 IsFirstRecordOfWrites (
545 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
546 IN EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwRecord
550 To check if FtwRecord is the last record of FtwHeader. Because the
551 FtwHeader has NumberOfWrites & PrivateDataSize, the FtwRecord can be
552 determined if it is the last record of FtwHeader.
554 @param FtwHeader Pointer to the write record header
555 @param FtwRecord Pointer to the write record
557 @retval TRUE FtwRecord is the last Record of the FtwHeader
558 @retval FALSE FtwRecord is not the last Record of the FtwHeader
562 IsLastRecordOfWrites (
563 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
564 IN EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwRecord
568 To check if FtwRecord is the first record of FtwHeader.
570 @param FtwHeader Pointer to the write record header
571 @param FtwRecord Pointer to retrieve the previous write record
573 @retval EFI_ACCESS_DENIED Input record is the first record, no previous record is return.
574 @retval EFI_SUCCESS The previous write record is found.
578 GetPreviousRecordOfWrites (
579 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
580 IN OUT EFI_FAULT_TOLERANT_WRITE_RECORD
**FtwRecord
585 Check whether a flash buffer is erased.
587 @param Buffer Buffer to check
588 @param BufferSize Size of the buffer
590 @return A BOOLEAN value indicating erased or not.
594 IsErasedFlashBuffer (
599 Initialize a work space when there is no work space.
601 @param WorkingHeader Pointer of working block header
603 @retval EFI_SUCCESS The function completed successfully
604 @retval EFI_ABORTED The function could not complete successfully.
608 InitWorkSpaceHeader (
609 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
612 Read from working block to refresh the work space in memory.
614 @param FtwDevice Point to private data of FTW driver
616 @retval EFI_SUCCESS The function completed successfully
617 @retval EFI_ABORTED The function could not complete successfully.
622 IN EFI_FTW_DEVICE
*FtwDevice
625 Check to see if it is a valid work space.
628 @param WorkingHeader Pointer of working block header
630 @retval EFI_SUCCESS The function completed successfully
631 @retval EFI_ABORTED The function could not complete successfully.
636 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
639 Reclaim the work space on the working block.
641 @param FtwDevice Point to private data of FTW driver
642 @param PreserveRecord Whether to preserve the working record is needed
644 @retval EFI_SUCCESS The function completed successfully
645 @retval EFI_OUT_OF_RESOURCES Allocate memory error
646 @retval EFI_ABORTED The function could not complete successfully
650 FtwReclaimWorkSpace (
651 IN EFI_FTW_DEVICE
*FtwDevice
,
652 IN BOOLEAN PreserveRecord
657 Get firmware block by address.
660 @param Address Address specified the block
661 @param FvBlock The block caller wanted
663 @retval EFI_SUCCESS The protocol instance if found.
664 @retval EFI_NOT_FOUND Block not found
669 IN EFI_PHYSICAL_ADDRESS Address
,
670 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
674 Retrive the proper Swap Address Range protocol interface.
676 @param[out] SarProtocol The interface of SAR protocol
678 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
679 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
680 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
685 OUT VOID
**SarProtocol
689 Function returns an array of handles that support the FVB protocol
690 in a buffer allocated from pool.
692 @param[out] NumberHandles The number of handles returned in Buffer.
693 @param[out] Buffer A pointer to the buffer to return the requested
694 array of handles that support FVB protocol.
696 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
697 handles in Buffer was returned in NumberHandles.
698 @retval EFI_NOT_FOUND No FVB handle was found.
699 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
700 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
704 GetFvbCountAndBuffer (
705 OUT UINTN
*NumberHandles
,
706 OUT EFI_HANDLE
**Buffer
711 Allocate private data for FTW driver and initialize it.
713 @param[out] FtwData Pointer to the FTW device structure
715 @retval EFI_SUCCESS Initialize the FTW device successfully.
716 @retval EFI_OUT_OF_RESOURCES Allocate memory error
717 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
722 OUT EFI_FTW_DEVICE
**FtwData
727 Initialization for Fault Tolerant Write is done in this handler.
729 @param[in, out] FtwDevice Pointer to the FTW device structure
731 @retval EFI_SUCCESS Initialize the FTW protocol successfully.
732 @retval EFI_NOT_FOUND No proper FVB protocol was found.
737 IN OUT EFI_FTW_DEVICE
*FtwDevice