3 Handles non-volatile variable store garbage collection, using FTW
4 (Fault Tolerant Write) protocol.
6 Copyright (c) 2006 - 2014, 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.
20 Gets LBA of block and offset by given address.
22 This function gets the Logical Block Address (LBA) of a firmware
23 volume block containing the given address, and the offset of the
26 @param Address Address which should be contained
27 by returned FVB handle.
28 @param Lba Pointer to LBA for output.
29 @param Offset Pointer to offset for output.
31 @retval EFI_SUCCESS LBA and offset successfully returned.
32 @retval EFI_NOT_FOUND Fail to find FVB handle by address.
33 @retval EFI_ABORTED Fail to find valid LBA and offset.
37 GetLbaAndOffsetByAddress (
38 IN EFI_PHYSICAL_ADDRESS Address
,
44 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
45 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
46 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
47 EFI_FV_BLOCK_MAP_ENTRY
*FvbMapEntry
;
51 *Lba
= (EFI_LBA
) (-1);
55 // Get the proper FVB protocol.
57 Status
= GetFvbInfoByAddress (Address
, NULL
, &Fvb
);
58 if (EFI_ERROR (Status
)) {
63 // Get the Base Address of FV.
65 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
66 if (EFI_ERROR (Status
)) {
70 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
73 // Get the (LBA, Offset) of Address.
75 if ((FwVolHeader
->FvLength
) > (FwVolHeader
->HeaderLength
)) {
77 // BUGBUG: Assume one FV has one type of BlockLength.
79 FvbMapEntry
= &FwVolHeader
->BlockMap
[0];
80 for (LbaIndex
= 1; LbaIndex
<= FvbMapEntry
->NumBlocks
; LbaIndex
+= 1) {
81 if (Address
< (FvbBaseAddress
+ FvbMapEntry
->Length
* LbaIndex
)) {
83 // Found the (Lba, Offset).
86 *Offset
= (UINTN
) (Address
- (FvbBaseAddress
+ FvbMapEntry
->Length
* (LbaIndex
- 1)));
96 Writes a buffer to variable storage space, in the working block.
98 This function writes a buffer to variable storage space into a firmware
99 volume block device. The destination is specified by parameter
100 VariableBase. Fault Tolerant Write protocol is used for writing.
102 @param VariableBase Base address of variable to write
103 @param VariableBuffer Point to the variable data buffer.
105 @retval EFI_SUCCESS The function completed successfully.
106 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.
107 @retval EFI_ABORTED The function could not complete successfully.
112 IN EFI_PHYSICAL_ADDRESS VariableBase
,
113 IN VARIABLE_STORE_HEADER
*VariableBuffer
117 EFI_HANDLE FvbHandle
;
121 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
124 // Locate fault tolerant write protocol.
126 Status
= GetFtwProtocol((VOID
**) &FtwProtocol
);
127 if (EFI_ERROR (Status
)) {
128 return EFI_NOT_FOUND
;
131 // Locate Fvb handle by address.
133 Status
= GetFvbInfoByAddress (VariableBase
, &FvbHandle
, NULL
);
134 if (EFI_ERROR (Status
)) {
138 // Get LBA and Offset by address.
140 Status
= GetLbaAndOffsetByAddress (VariableBase
, &VarLba
, &VarOffset
);
141 if (EFI_ERROR (Status
)) {
145 FtwBufferSize
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
))->Size
;
146 ASSERT (FtwBufferSize
== VariableBuffer
->Size
);
151 Status
= FtwProtocol
->Write (
155 FtwBufferSize
, // NumBytes
156 NULL
, // PrivateData NULL
157 FvbHandle
, // Fvb Handle
158 (VOID
*) VariableBuffer
// write buffer