3 The internal header file includes the common header files, defines
4 internal structure and functions used by FtwLite module.
6 Copyright (c) 2006 - 2008, Intel Corporation
7 All rights reserved. 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_LITE_H_
18 #define _EFI_FAULT_TOLERANT_WRITE_LITE_H_
23 #include <Guid/SystemNvDataGuid.h>
24 #include <Protocol/FaultTolerantWriteLite.h>
25 #include <Protocol/FirmwareVolumeBlock.h>
27 #include <Library/PcdLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/UefiDriverEntryPoint.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/UefiBootServicesTableLib.h>
33 #include <Library/DevicePathLib.h>
35 #include <WorkingBlockHeader.h>
37 #define EFI_D_FTW_LITE EFI_D_ERROR
38 #define EFI_D_FTW_INFO EFI_D_INFO
41 // Flash erase polarity is 1
43 #define FTW_ERASE_POLARITY 1
45 #define FTW_VALID_STATE 0
46 #define FTW_INVALID_STATE 1
48 #define FTW_ERASED_BYTE ((UINT8) (255))
49 #define FTW_POLARITY_REVERT ((UINT8) (255))
52 UINT8 WriteAllocated
: 1;
53 UINT8 SpareCompleted
: 1;
54 UINT8 WriteCompleted
: 1;
56 #define WRITE_ALLOCATED 0x1
57 #define SPARE_COMPLETED 0x2
58 #define WRITE_COMPLETED 0x4
65 // UINTN SpareAreaOffset;
67 } EFI_FTW_LITE_RECORD
;
69 #define FTW_LITE_DEVICE_SIGNATURE SIGNATURE_32 ('F', 'T', 'W', 'L')
72 // MACRO for FTW WORK SPACE Base & Size
74 #ifdef EFI_FTW_WORKING_OFFSET
75 #define FTW_WORK_SPACE_BASE EFI_FTW_WORKING_OFFSET
77 #define FTW_WORK_SPACE_BASE 0x00E000
80 #ifdef EFI_FTW_WORKING_LENGTH
81 #define FTW_WORK_SPACE_SIZE EFI_FTW_WORKING_LENGTH
83 #define FTW_WORK_SPACE_SIZE 0x002000
86 // MACRO for FTW header and record
88 #define FTW_LITE_RECORD_SIZE (sizeof (EFI_FTW_LITE_RECORD))
91 // EFI Fault tolerant protocol private data structure
96 EFI_FTW_LITE_PROTOCOL FtwLiteInstance
;
97 EFI_PHYSICAL_ADDRESS WorkSpaceAddress
;
98 UINTN WorkSpaceLength
;
99 EFI_PHYSICAL_ADDRESS SpareAreaAddress
;
100 UINTN SpareAreaLength
;
101 UINTN NumberOfSpareBlock
; // Number of the blocks in spare block
102 UINTN SizeOfSpareBlock
; // Block size in bytes of the blocks in spare block
103 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*FtwWorkSpaceHeader
;
104 EFI_FTW_LITE_RECORD
*FtwLastRecord
;
105 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FtwFvBlock
; // FVB of working block
106 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FtwBackupFvb
; // FVB of spare block
108 EFI_LBA FtwWorkBlockLba
; // Start LBA of working block
109 EFI_LBA FtwWorkSpaceLba
; // Start LBA of working space
110 UINTN FtwWorkSpaceBase
; // Offset from LBA start addr
111 UINTN FtwWorkSpaceSize
;
114 // Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE],
115 // Allocated with EFI_FTW_LITE_DEVICE.
117 } EFI_FTW_LITE_DEVICE
;
119 #define FTW_LITE_CONTEXT_FROM_THIS(a) CR (a, EFI_FTW_LITE_DEVICE, FtwLiteInstance, FTW_LITE_DEVICE_SIGNATURE)
122 // Driver entry point
125 This function is the entry point of the Fault Tolerant Write driver.
128 @param ImageHandle A handle for the image that is initializing
130 @param SystemTable A pointer to the EFI system table
132 @retval EFI_SUCCESS FTW has finished the initialization
133 @retval EFI_ABORTED FTW initialization error
139 IN EFI_HANDLE ImageHandle
,
140 IN EFI_SYSTEM_TABLE
*SystemTable
144 // Fault Tolerant Write Protocol API
147 Starts a target block update. This function will record data about write
148 in fault tolerant storage and will complete the write in a recoverable
149 manner, ensuring at all times that either the original contents or
150 the modified contents are available.
153 @param This Calling context
154 @param FvbHandle The handle of FVB protocol that provides services for
155 reading, writing, and erasing the target block.
156 @param Lba The logical block address of the target block.
157 @param Offset The offset within the target block to place the data.
158 @param NumBytes The number of bytes to write to the target block.
159 @param Buffer The data to write.
161 @retval EFI_SUCCESS The function completed successfully
162 @retval EFI_BAD_BUFFER_SIZE The write would span a target block, which is not
164 @retval EFI_ACCESS_DENIED No writes have been allocated.
165 @retval EFI_NOT_FOUND Cannot find FVB by handle.
166 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
167 @retval EFI_ABORTED The function could not complete successfully.
173 IN EFI_FTW_LITE_PROTOCOL
*This
,
174 IN EFI_HANDLE FvbHandle
,
177 IN OUT UINTN
*NumBytes
,
182 // Internal functions
185 Restarts a previously interrupted write. The caller must provide the
186 block protocol needed to complete the interrupted write.
189 @param FtwLiteDevice The private data of FTW_LITE driver
190 FvbHandle - The handle of FVB protocol that provides services for
191 reading, writing, and erasing the target block.
193 @retval EFI_SUCCESS The function completed successfully
194 @retval EFI_ACCESS_DENIED No pending writes exist
195 @retval EFI_NOT_FOUND FVB protocol not found by the handle
196 @retval EFI_ABORTED The function could not complete successfully
201 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
205 Aborts all previous allocated writes.
208 @param FtwLiteDevice The private data of FTW_LITE driver
210 @retval EFI_SUCCESS The function completed successfully
211 @retval EFI_ABORTED The function could not complete successfully.
212 @retval EFI_NOT_FOUND No allocated writes exist.
217 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
222 Write a record with fault tolerant mannaer.
223 Since the content has already backuped in spare block, the write is
224 guaranteed to be completed with fault tolerant manner.
227 @param FtwLiteDevice The private data of FTW_LITE driver
228 @param Fvb The FVB protocol that provides services for
229 reading, writing, and erasing the target block.
231 @retval EFI_SUCCESS The function completed successfully
232 @retval EFI_ABORTED The function could not complete successfully
237 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
238 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
242 To erase the block with the spare block size.
245 @param FtwLiteDevice Calling context
246 @param FvBlock FVB Protocol interface
247 @param Lba Lba of the firmware block
249 @retval EFI_SUCCESS Block LBA is Erased successfully
250 @retval Others Error occurs
255 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
256 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
265 @param FtwLiteDevice Calling context
267 @retval EFI_SUCCESS The erase request was successfully
270 @retval EFI_ACCESS_DENIED The firmware volume is in the
272 @retval EFI_DEVICE_ERROR The block device is not functioning
273 correctly and could not be written.
274 The firmware device may have been
276 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed
277 in the variable argument list do
278 not exist in the firmware volume.
283 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
287 Retrive the proper FVB protocol interface by HANDLE.
290 @param FvBlockHandle The handle of FVB protocol that provides services for
291 reading, writing, and erasing the target block.
292 @param FvBlock The interface of FVB protocol
294 @retval EFI_SUCCESS The function completed successfully
295 @retval EFI_ABORTED The function could not complete successfully
300 IN EFI_HANDLE FvBlockHandle
,
301 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
306 Get firmware block by address.
309 @param Address Address specified the block
310 @param FvBlock The block caller wanted
312 @retval EFI_SUCCESS The protocol instance if found.
313 @retval EFI_NOT_FOUND Block not found
318 IN EFI_PHYSICAL_ADDRESS Address
,
319 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
324 Is it in working block?
327 @param FtwLiteDevice Calling context
328 @param FvBlock Fvb protocol instance
329 @param Lba The block specified
331 @return A BOOLEAN value indicating in working block or not.
336 EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
337 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
342 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
343 Spare block is accessed by FTW backup FVB protocol interface. LBA is
344 FtwLiteDevice->FtwSpareLba.
345 Target block is accessed by FvBlock protocol interface. LBA is Lba.
348 @param FtwLiteDevice The private data of FTW_LITE driver
349 @param FvBlock FVB Protocol interface to access target block
350 @param Lba Lba of the target block
352 @retval EFI_SUCCESS Spare block content is copied to target block
353 @retval EFI_INVALID_PARAMETER Input parameter error
354 @retval EFI_OUT_OF_RESOURCES Allocate memory error
355 @retval EFI_ABORTED The function could not complete successfully
359 FlushSpareBlockToTargetBlock (
360 EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
361 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
366 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
367 Spare block is accessed by FTW backup FVB protocol interface. LBA is
368 FtwLiteDevice->FtwSpareLba.
369 Working block is accessed by FTW working FVB protocol interface. LBA is
370 FtwLiteDevice->FtwWorkBlockLba.
373 @param FtwLiteDevice The private data of FTW_LITE driver
375 @retval EFI_SUCCESS Spare block content is copied to target block
376 @retval EFI_OUT_OF_RESOURCES Allocate memory error
377 @retval EFI_ABORTED The function could not complete successfully
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.
385 FlushSpareBlockToWorkingBlock (
386 EFI_FTW_LITE_DEVICE
*FtwLiteDevice
390 Update a bit of state on a block device. The location of the bit is
391 calculated by the (Lba, Offset, bit). Here bit is determined by the
392 the name of a certain bit.
395 @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
396 @param Lba Lba of a block
397 @param Offset Offset on the Lba
398 @param NewBit New value that will override the old value if it can be change
400 @retval EFI_SUCCESS A state bit has been updated successfully
401 @retval Others Access block device error.
403 Assume all bits of State are inside the same BYTE.
404 @retval EFI_ABORTED Read block fail
409 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvBlock
,
416 Get the last Write record pointer.
417 The last record is the record whose 'complete' state hasn't been set.
418 After all, this header may be a EMPTY header entry for next Allocate.
421 @param FtwLiteDevice Private data of this driver
422 @param FtwLastRecord Pointer to retrieve the last write record
424 @retval EFI_SUCCESS Get the last write record successfully
425 @retval EFI_ABORTED The FTW work space is damaged
430 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
431 OUT EFI_FTW_LITE_RECORD
**FtwLastRecord
436 Check whether a flash buffer is erased.
439 @param Polarity All 1 or all 0
440 @param Buffer Buffer to check
441 @param BufferSize Size of the buffer
443 @return A BOOLEAN value indicating erased or not.
447 IsErasedFlashBuffer (
454 Initialize a work space when there is no work space.
457 @param WorkingHeader Pointer of working block header
459 @retval EFI_SUCCESS The function completed successfully
460 @retval EFI_ABORTED The function could not complete successfully.
464 InitWorkSpaceHeader (
465 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
469 Read from working block to refresh the work space in memory.
472 @param FtwLiteDevice Point to private data of FTW driver
474 @retval EFI_SUCCESS The function completed successfully
475 @retval EFI_ABORTED The function could not complete successfully.
480 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
484 Check to see if it is a valid work space.
487 @param WorkingHeader Pointer of working block header
489 @retval EFI_SUCCESS The function completed successfully
490 @retval EFI_ABORTED The function could not complete successfully.
495 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
*WorkingHeader
499 Reclaim the work space on the working block.
502 @param FtwLiteDevice Point to private data of FTW driver
503 @param PreserveRecord Whether to preserve the working record is needed
505 @retval EFI_SUCCESS The function completed successfully
506 @retval EFI_OUT_OF_RESOURCES Allocate memory error
507 @retval EFI_ABORTED The function could not complete successfully
511 FtwReclaimWorkSpace (
512 IN EFI_FTW_LITE_DEVICE
*FtwLiteDevice
,
513 IN BOOLEAN PreserveRecord