2 Handles non-volatile variable store garbage collection, using FTW
3 (Fault Tolerant Write) protocol.
5 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Gets firmware volume block handle by given address.
21 This function gets firmware volume block handle whose
22 address range contains the parameter Address.
24 @param[in] Address Address which should be contained
25 by returned FVB handle.
26 @param[out] FvbHandle Pointer to FVB handle for output.
28 @retval EFI_SUCCESS FVB handle successfully returned.
29 @retval EFI_NOT_FOUND Failed to find FVB handle by address.
33 GetFvbHandleByAddress (
34 IN EFI_PHYSICAL_ADDRESS Address
,
35 OUT EFI_HANDLE
*FvbHandle
39 EFI_HANDLE
*HandleBuffer
;
42 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
43 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
44 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
48 // Locate all handles with Firmware Volume Block protocol
50 Status
= gBS
->LocateHandleBuffer (
52 &gEfiFirmwareVolumeBlockProtocolGuid
,
57 if (EFI_ERROR (Status
)) {
61 // Traverse all the handles, searching for the one containing parameter Address
63 for (Index
= 0; Index
< HandleCount
; Index
+= 1) {
64 Status
= gBS
->HandleProtocol (
66 &gEfiFirmwareVolumeBlockProtocolGuid
,
69 if (EFI_ERROR (Status
)) {
70 Status
= EFI_NOT_FOUND
;
74 // Checks if the address range of this handle contains parameter Address
76 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
77 if (EFI_ERROR (Status
)) {
81 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
82 if ((Address
>= FvbBaseAddress
) && (Address
<= (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
83 *FvbHandle
= HandleBuffer
[Index
];
89 FreePool (HandleBuffer
);
94 Gets LBA of block and offset by given address.
96 This function gets the Logical Block Address (LBA) of firmware
97 volume block containing the given address, and the offset of
100 @param[in] Address Address which should be contained
101 by returned FVB handle.
102 @param[out] Lba The pointer to LBA for output.
103 @param[out] Offset The pointer to offset for output.
105 @retval EFI_SUCCESS LBA and offset successfully returned.
106 @retval EFI_NOT_FOUND Failed to find FVB handle by address.
107 @retval EFI_ABORTED Failed to find valid LBA and offset.
111 GetLbaAndOffsetByAddress (
112 IN EFI_PHYSICAL_ADDRESS Address
,
118 EFI_HANDLE FvbHandle
;
119 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
120 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
121 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
122 EFI_FV_BLOCK_MAP_ENTRY
*FvbMapEntry
;
125 *Lba
= (EFI_LBA
) (-1);
129 // Gets firmware volume block handle by given address.
131 Status
= GetFvbHandleByAddress (Address
, &FvbHandle
);
132 if (EFI_ERROR (Status
)) {
136 Status
= gBS
->HandleProtocol (
138 &gEfiFirmwareVolumeBlockProtocolGuid
,
141 if (EFI_ERROR (Status
)) {
145 // Get the Base Address of FV
147 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
148 if (EFI_ERROR (Status
)) {
152 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
155 // Get the (LBA, Offset) of Address
157 if ((Address
>= FvbBaseAddress
) && (Address
<= (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
158 if ((FwVolHeader
->FvLength
) > (FwVolHeader
->HeaderLength
)) {
160 // BUGBUG: Assume one FV has one type of BlockLength
162 FvbMapEntry
= &FwVolHeader
->BlockMap
[0];
163 for (LbaIndex
= 1; LbaIndex
<= FvbMapEntry
->NumBlocks
; LbaIndex
+= 1) {
164 if (Address
< (FvbBaseAddress
+ FvbMapEntry
->Length
* LbaIndex
)) {
166 // Found the (Lba, Offset)
169 *Offset
= (UINTN
) (Address
- (FvbBaseAddress
+ FvbMapEntry
->Length
* (LbaIndex
- 1)));
180 Writes a buffer to variable storage space.
182 This function writes a buffer to variable storage space into firmware
183 volume block device. The destination is specified by parameter
184 VariableBase. Fault Tolerant Write protocol is used for writing.
186 @param[in] VariableBase The base address of the variable to write.
187 @param[in] Buffer Points to the data buffer.
188 @param[in] BufferSize The number of bytes of the data Buffer.
190 @retval EFI_SUCCESS The function completed successfully.
191 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.
192 @retval Other The function could not complete successfully.
197 IN EFI_PHYSICAL_ADDRESS VariableBase
,
203 EFI_HANDLE FvbHandle
;
208 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
211 // Locate Fault Tolerant Write protocol
213 Status
= gBS
->LocateProtocol (
214 &gEfiFaultTolerantWriteProtocolGuid
,
216 (VOID
**) &FtwProtocol
218 if (EFI_ERROR (Status
)) {
219 return EFI_NOT_FOUND
;
222 // Gets firmware volume block handle by VariableBase.
224 Status
= GetFvbHandleByAddress (VariableBase
, &FvbHandle
);
225 if (EFI_ERROR (Status
)) {
229 // Gets LBA of block and offset by VariableBase.
231 Status
= GetLbaAndOffsetByAddress (VariableBase
, &VarLba
, &VarOffset
);
232 if (EFI_ERROR (Status
)) {
236 // Prepare for the variable data
238 FtwBufferSize
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
))->Size
;
239 FtwBuffer
= AllocatePool (FtwBufferSize
);
240 if (FtwBuffer
== NULL
) {
241 return EFI_OUT_OF_RESOURCES
;
244 SetMem (FtwBuffer
, FtwBufferSize
, (UINT8
) 0xff);
245 CopyMem (FtwBuffer
, Buffer
, BufferSize
);
250 Status
= FtwProtocol
->Write (
254 FtwBufferSize
, // NumBytes,
260 FreePool (FtwBuffer
);