3 The internal header file includes the common header files, defines
4 internal structure and functions used by FtwLite module.
6 Copyright (c) 2006 - 2013, 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/UefiBootServicesTableLib.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
53 #define FTW_DEVICE_SIGNATURE SIGNATURE_32 ('F', 'T', 'W', 'D')
56 // EFI Fault tolerant protocol private data structure
61 EFI_FAULT_TOLERANT_WRITE_PROTOCOL FtwInstance
;
62 EFI_PHYSICAL_ADDRESS WorkSpaceAddress
; // Base address of working space range in flash.
63 EFI_PHYSICAL_ADDRESS SpareAreaAddress
; // Base address of spare range in flash.
64 UINTN WorkSpaceLength
; // Size of working space range in flash.
65 UINTN SpareAreaLength
; // Size of spare range in flash.
66 UINTN NumberOfSpareBlock
; // Number of the blocks in spare block.
67 UINTN BlockSize
; // Block size in bytes of the blocks in flash
68 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*FtwWorkSpaceHeader
;// Pointer to Working Space Header in memory buffer
69 EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwLastWriteHeader
;// Pointer to last record header in memory buffer
70 EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwLastWriteRecord
;// Pointer to last record in memory buffer
71 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FtwFvBlock
; // FVB of working block
72 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FtwBackupFvb
; // FVB of spare block
73 EFI_LBA FtwSpareLba
; // Start LBA of spare block
74 EFI_LBA FtwWorkBlockLba
; // Start LBA of working block that contains working space in its last 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 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)
91 This function is the entry point of the Fault Tolerant Write driver.
93 @param ImageHandle A handle for the image that is initializing this driver
94 @param SystemTable A pointer to the EFI system table
96 @return EFI_SUCCESS FTW has finished the initialization
97 @retval EFI_NOT_FOUND Locate FVB protocol error
98 @retval EFI_OUT_OF_RESOURCES Allocate memory error
99 @retval EFI_VOLUME_CORRUPTED Firmware volume is error
100 @retval EFI_ABORTED FTW initialization error
105 InitializeFaultTolerantWrite (
106 IN EFI_HANDLE ImageHandle
,
107 IN EFI_SYSTEM_TABLE
*SystemTable
111 // Fault Tolerant Write Protocol API
115 Query the largest block that may be updated in a fault tolerant manner.
118 @param This Indicates a pointer to the calling context.
119 @param BlockSize A pointer to a caller allocated UINTN that is updated to
120 indicate the size of the largest block that can be updated.
122 @return EFI_SUCCESS The function completed successfully
128 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
133 Allocates space for the protocol to maintain information about writes.
134 Since writes must be completed in a fault tolerant manner and multiple
135 updates will require more resources to be successful, this function
136 enables the protocol to ensure that enough space exists to track
137 information about the upcoming writes.
139 All writes must be completed or aborted before another fault tolerant write can occur.
141 @param This Indicates a pointer to the calling context.
142 @param CallerId The GUID identifying the write.
143 @param PrivateDataSize The size of the caller's private data
144 that must be recorded for each write.
145 @param NumberOfWrites The number of fault tolerant block writes
146 that will need to occur.
148 @return EFI_SUCCESS The function completed successfully
149 @retval EFI_ABORTED The function could not complete successfully.
150 @retval EFI_ACCESS_DENIED All allocated writes have not been completed.
156 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
157 IN EFI_GUID
*CallerId
,
158 IN UINTN PrivateDataSize
,
159 IN UINTN NumberOfWrites
163 Starts a target block update. This function will record data about write
164 in fault tolerant storage and will complete the write in a recoverable
165 manner, ensuring at all times that either the original contents or
166 the modified contents are available.
169 @param This Calling context
170 @param Lba The logical block address of the target block.
171 @param Offset The offset within the target block to place the data.
172 @param Length The number of bytes to write to the target block.
173 @param PrivateData A pointer to private data that the caller requires to
174 complete any pending writes in the event of a fault.
175 @param FvBlockHandle The handle of FVB protocol that provides services for
176 reading, writing, and erasing the target block.
177 @param Buffer The data to write.
179 @retval EFI_SUCCESS The function completed successfully
180 @retval EFI_ABORTED The function could not complete successfully.
181 @retval EFI_BAD_BUFFER_SIZE The input data can't fit within the spare block.
182 Offset + *NumBytes > SpareAreaLength.
183 @retval EFI_ACCESS_DENIED No writes have been allocated.
184 @retval EFI_OUT_OF_RESOURCES Cannot allocate enough memory resource.
185 @retval EFI_NOT_FOUND Cannot find FVB protocol by handle.
191 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
195 IN VOID
*PrivateData
,
196 IN EFI_HANDLE FvBlockHandle
,
201 Restarts a previously interrupted write. The caller must provide the
202 block protocol needed to complete the interrupted write.
204 @param This Calling context.
205 @param FvBlockHandle The handle of FVB protocol that provides services for
206 reading, writing, and erasing the target block.
208 @retval EFI_SUCCESS The function completed successfully
209 @retval EFI_ACCESS_DENIED No pending writes exist
210 @retval EFI_NOT_FOUND FVB protocol not found by the handle
211 @retval EFI_ABORTED The function could not complete successfully
217 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
218 IN EFI_HANDLE FvBlockHandle
222 Aborts all previous allocated writes.
224 @param This Calling context
226 @retval EFI_SUCCESS The function completed successfully
227 @retval EFI_ABORTED The function could not complete successfully.
228 @retval EFI_NOT_FOUND No allocated writes exist.
234 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
238 Starts a target block update. This records information about the write
239 in fault tolerant storage and will complete the write in a recoverable
240 manner, ensuring at all times that either the original contents or
241 the modified contents are available.
243 @param This Indicates a pointer to the calling context.
244 @param CallerId The GUID identifying the last write.
245 @param Lba The logical block address of the last write.
246 @param Offset The offset within the block of the last write.
247 @param Length The length of the last write.
248 @param PrivateDataSize bytes from the private data
249 stored for this write.
250 @param PrivateData A pointer to a buffer. The function will copy
251 @param Complete A Boolean value with TRUE indicating
252 that the write was completed.
254 @retval EFI_SUCCESS The function completed successfully
255 @retval EFI_ABORTED The function could not complete successfully
256 @retval EFI_NOT_FOUND No allocated writes exist
257 @retval EFI_BUFFER_TOO_SMALL Input buffer is not larget enough
263 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
264 OUT EFI_GUID
*CallerId
,
268 IN OUT UINTN
*PrivateDataSize
,
269 OUT VOID
*PrivateData
,
270 OUT BOOLEAN
*Complete
276 @param FtwDevice The private data of FTW driver
278 @retval EFI_SUCCESS The erase request was successfully completed.
279 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
280 @retval EFI_DEVICE_ERROR The block device is not functioning
281 correctly and could not be written.
282 The firmware device may have been
284 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed
285 in the variable argument list do
286 not exist in the firmware volume.
292 IN EFI_FTW_DEVICE
*FtwDevice
296 Retrive the proper FVB protocol interface by HANDLE.
299 @param FvBlockHandle The handle of FVB protocol that provides services for
300 reading, writing, and erasing the target block.
301 @param FvBlock The interface of FVB protocol
303 @retval EFI_SUCCESS The function completed successfully
304 @retval EFI_ABORTED The function could not complete successfully
309 IN EFI_HANDLE FvBlockHandle
,
310 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
315 Is it in working block?
317 @param FtwDevice The private data of FTW driver
318 @param FvBlock Fvb protocol instance
319 @param Lba The block specified
321 @return A BOOLEAN value indicating in working block or not.
326 EFI_FTW_DEVICE
*FtwDevice
,
327 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
335 @param FtwDevice The private data of FTW driver
336 @param FvBlock Fvb protocol instance
337 @param Lba The block specified
339 @return A BOOLEAN value indicating in boot block or not.
344 EFI_FTW_DEVICE
*FtwDevice
,
345 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
350 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
351 Spare block is accessed by FTW backup FVB protocol interface. LBA is 1.
352 Target block is accessed by FvbBlock protocol interface. LBA is Lba.
355 @param FtwDevice The private data of FTW driver
356 @param FvBlock FVB Protocol interface to access target block
357 @param Lba Lba of the target block
359 @retval EFI_SUCCESS Spare block content is copied to target block
360 @retval EFI_INVALID_PARAMETER Input parameter error
361 @retval EFI_OUT_OF_RESOURCES Allocate memory error
362 @retval EFI_ABORTED The function could not complete successfully
366 FlushSpareBlockToTargetBlock (
367 EFI_FTW_DEVICE
*FtwDevice
,
368 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
373 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
374 Spare block is accessed by FTW backup FVB protocol interface. LBA is
375 FtwDevice->FtwSpareLba.
376 Working block is accessed by FTW working FVB protocol interface. LBA is
377 FtwDevice->FtwWorkBlockLba.
379 Since the working block header is important when FTW initializes, the
380 state of the operation should be handled carefully. The Crc value is
381 calculated without STATE element.
383 @param FtwDevice The private data of FTW driver
385 @retval EFI_SUCCESS Spare block content is copied to target block
386 @retval EFI_OUT_OF_RESOURCES Allocate memory error
387 @retval EFI_ABORTED The function could not complete successfully
391 FlushSpareBlockToWorkingBlock (
392 EFI_FTW_DEVICE
*FtwDevice
396 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
397 Spare block is accessed by FTW working FVB protocol interface. LBA is 1.
398 Target block is accessed by FvbBlock protocol interface. LBA is Lba.
400 FTW will do extra work on boot block update.
401 FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL,
402 which is produced by a chipset driver.
403 FTW updating boot block steps may be:
404 1. GetRangeLocation(), if the Range is inside the boot block, FTW know
405 that boot block will be update. It shall add a FLAG in the working block.
406 2. When spare block is ready,
407 3. SetSwapState(EFI_SWAPPED)
408 4. erasing boot block,
409 5. programming boot block until the boot block is ok.
410 6. SetSwapState(UNSWAPPED)
411 FTW shall not allow to update boot block when battery state is error.
413 @param FtwDevice The private data of FTW driver
415 @retval EFI_SUCCESS Spare block content is copied to boot block
416 @retval EFI_INVALID_PARAMETER Input parameter error
417 @retval EFI_OUT_OF_RESOURCES Allocate memory error
418 @retval EFI_ABORTED The function could not complete successfully
422 FlushSpareBlockToBootBlock (
423 EFI_FTW_DEVICE
*FtwDevice
427 Update a bit of state on a block device. The location of the bit is
428 calculated by the (Lba, Offset, bit). Here bit is determined by the
429 the name of a certain bit.
432 @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
433 @param Lba Lba of a block
434 @param Offset Offset on the Lba
435 @param NewBit New value that will override the old value if it can be change
437 @retval EFI_SUCCESS A state bit has been updated successfully
438 @retval Others Access block device error.
440 Assume all bits of State are inside the same BYTE.
441 @retval EFI_ABORTED Read block fail
446 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
453 Get the last Write Header pointer.
454 The last write header is the header whose 'complete' state hasn't been set.
455 After all, this header may be a EMPTY header entry for next Allocate.
458 @param FtwWorkSpaceHeader Pointer of the working block header
459 @param FtwWorkSpaceSize Size of the work space
460 @param FtwWriteHeader Pointer to retrieve the last write header
462 @retval EFI_SUCCESS Get the last write record successfully
463 @retval EFI_ABORTED The FTW work space is damaged
467 FtwGetLastWriteHeader (
468 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*FtwWorkSpaceHeader
,
469 IN UINTN FtwWorkSpaceSize
,
470 OUT EFI_FAULT_TOLERANT_WRITE_HEADER
**FtwWriteHeader
474 Get the last Write Record pointer. The last write Record is the Record
475 whose DestinationCompleted state hasn't been set. After all, this Record
476 may be a EMPTY record entry for next write.
479 @param FtwWriteHeader Pointer to the write record header
480 @param FtwWriteRecord Pointer to retrieve the last write record
482 @retval EFI_SUCCESS Get the last write record successfully
483 @retval EFI_ABORTED The FTW work space is damaged
487 FtwGetLastWriteRecord (
488 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwWriteHeader
,
489 OUT EFI_FAULT_TOLERANT_WRITE_RECORD
**FtwWriteRecord
493 To check if FtwRecord is the first record of FtwHeader.
495 @param FtwHeader Pointer to the write record header
496 @param FtwRecord Pointer to the write record
498 @retval TRUE FtwRecord is the first Record of the FtwHeader
499 @retval FALSE FtwRecord is not the first Record of the FtwHeader
503 IsFirstRecordOfWrites (
504 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
505 IN EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwRecord
509 To check if FtwRecord is the last record of FtwHeader. Because the
510 FtwHeader has NumberOfWrites & PrivateDataSize, the FtwRecord can be
511 determined if it is the last record of FtwHeader.
513 @param FtwHeader Pointer to the write record header
514 @param FtwRecord Pointer to the write record
516 @retval TRUE FtwRecord is the last Record of the FtwHeader
517 @retval FALSE FtwRecord is not the last Record of the FtwHeader
521 IsLastRecordOfWrites (
522 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
523 IN EFI_FAULT_TOLERANT_WRITE_RECORD
*FtwRecord
527 To check if FtwRecord is the first record of FtwHeader.
529 @param FtwHeader Pointer to the write record header
530 @param FtwRecord Pointer to retrieve the previous write record
532 @retval EFI_ACCESS_DENIED Input record is the first record, no previous record is return.
533 @retval EFI_SUCCESS The previous write record is found.
537 GetPreviousRecordOfWrites (
538 IN EFI_FAULT_TOLERANT_WRITE_HEADER
*FtwHeader
,
539 IN OUT EFI_FAULT_TOLERANT_WRITE_RECORD
**FtwRecord
544 Check whether a flash buffer is erased.
546 @param Buffer Buffer to check
547 @param BufferSize Size of the buffer
549 @return A BOOLEAN value indicating erased or not.
553 IsErasedFlashBuffer (
558 Initialize a work space when there is no work space.
560 @param WorkingHeader Pointer of working block header
562 @retval EFI_SUCCESS The function completed successfully
563 @retval EFI_ABORTED The function could not complete successfully.
567 InitWorkSpaceHeader (
568 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
571 Read from working block to refresh the work space in memory.
573 @param FtwDevice Point to private data of FTW driver
575 @retval EFI_SUCCESS The function completed successfully
576 @retval EFI_ABORTED The function could not complete successfully.
581 IN EFI_FTW_DEVICE
*FtwDevice
584 Check to see if it is a valid work space.
587 @param WorkingHeader Pointer of working block header
589 @retval EFI_SUCCESS The function completed successfully
590 @retval EFI_ABORTED The function could not complete successfully.
595 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
598 Reclaim the work space on the working block.
600 @param FtwDevice Point to private data of FTW driver
601 @param PreserveRecord Whether to preserve the working record is needed
603 @retval EFI_SUCCESS The function completed successfully
604 @retval EFI_OUT_OF_RESOURCES Allocate memory error
605 @retval EFI_ABORTED The function could not complete successfully
609 FtwReclaimWorkSpace (
610 IN EFI_FTW_DEVICE
*FtwDevice
,
611 IN BOOLEAN PreserveRecord
616 Get firmware block by address.
619 @param Address Address specified the block
620 @param FvBlock The block caller wanted
622 @retval EFI_SUCCESS The protocol instance if found.
623 @retval EFI_NOT_FOUND Block not found
628 IN EFI_PHYSICAL_ADDRESS Address
,
629 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
633 Retrive the proper Swap Address Range protocol interface.
635 @param[out] SarProtocol The interface of SAR protocol
637 @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.
638 @retval EFI_NOT_FOUND The SAR protocol instance was not found.
639 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
644 OUT VOID
**SarProtocol
648 Function returns an array of handles that support the FVB protocol
649 in a buffer allocated from pool.
651 @param[out] NumberHandles The number of handles returned in Buffer.
652 @param[out] Buffer A pointer to the buffer to return the requested
653 array of handles that support FVB protocol.
655 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
656 handles in Buffer was returned in NumberHandles.
657 @retval EFI_NOT_FOUND No FVB handle was found.
658 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
659 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
663 GetFvbCountAndBuffer (
664 OUT UINTN
*NumberHandles
,
665 OUT EFI_HANDLE
**Buffer
670 Allocate private data for FTW driver and initialize it.
672 @param[out] FtwData Pointer to the FTW device structure
674 @retval EFI_SUCCESS Initialize the FTW device successfully.
675 @retval EFI_OUT_OF_RESOURCES Allocate memory error
676 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
681 OUT EFI_FTW_DEVICE
**FtwData
686 Initialization for Fault Tolerant Write is done in this handler.
688 @param[in, out] FtwDevice Pointer to the FTW device structure
690 @retval EFI_SUCCESS Initialize the FTW protocol successfully.
691 @retval EFI_NOT_FOUND No proper FVB protocol was found.
696 IN OUT EFI_FTW_DEVICE
*FtwDevice
700 Initialize a local work space header.
702 Since Signature and WriteQueueSize have been known, Crc can be calculated out,
703 then the work space header will be fixed.
706 InitializeLocalWorkSpaceHeader (