3 Handles non-volatile variable store garbage collection, using FTW
4 (Fault Tolerant Write) protocol.
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.
21 Gets firmware volume block handle by given address.
23 This function gets firmware volume block handle whose
24 address range contains the parameter Address.
26 @param Address Address which should be contained
27 by returned FVB handle
28 @param FvbHandle Pointer to FVB handle for output
30 @retval EFI_SUCCESS FVB handle successfully returned
31 @retval EFI_NOT_FOUND Fail to find FVB handle by address
35 GetFvbHandleByAddress (
36 IN EFI_PHYSICAL_ADDRESS Address
,
37 OUT EFI_HANDLE
*FvbHandle
41 EFI_HANDLE
*HandleBuffer
;
44 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
45 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
46 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
50 // Locate all handles of Fvb protocol
52 Status
= gBS
->LocateHandleBuffer (
54 &gEfiFirmwareVolumeBlockProtocolGuid
,
59 if (EFI_ERROR (Status
)) {
63 // Get the FVB to access variable store
65 for (Index
= 0; Index
< HandleCount
; Index
+= 1) {
66 Status
= gBS
->HandleProtocol (
68 &gEfiFirmwareVolumeBlockProtocolGuid
,
71 if (EFI_ERROR (Status
)) {
72 Status
= EFI_NOT_FOUND
;
76 // Compare the address and select the right one
78 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
79 if (EFI_ERROR (Status
)) {
83 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
84 if ((Address
>= FvbBaseAddress
) && (Address
<= (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
85 *FvbHandle
= HandleBuffer
[Index
];
91 FreePool (HandleBuffer
);
96 Gets LBA of block and offset by given address.
98 This function gets the Logical Block Address (LBA) of firmware
99 volume block containing the given address, and the offset of
100 address on the block.
102 @param Address Address which should be contained
103 by returned FVB handle
104 @param Lba Pointer to LBA for output
105 @param Offset Pointer to offset for output
107 @retval EFI_SUCCESS LBA and offset successfully returned
108 @retval EFI_NOT_FOUND Fail to find FVB handle by address
109 @retval EFI_ABORTED Fail to find valid LBA and offset
113 GetLbaAndOffsetByAddress (
114 IN EFI_PHYSICAL_ADDRESS Address
,
120 EFI_HANDLE FvbHandle
;
121 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
122 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
123 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
124 EFI_FV_BLOCK_MAP_ENTRY
*FvbMapEntry
;
127 *Lba
= (EFI_LBA
) (-1);
131 // Get the proper FVB
133 Status
= GetFvbHandleByAddress (Address
, &FvbHandle
);
134 if (EFI_ERROR (Status
)) {
138 Status
= gBS
->HandleProtocol (
140 &gEfiFirmwareVolumeBlockProtocolGuid
,
143 if (EFI_ERROR (Status
)) {
147 // Get the Base Address of FV
149 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
150 if (EFI_ERROR (Status
)) {
154 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
157 // Get the (LBA, Offset) of Address
159 if ((Address
>= FvbBaseAddress
) && (Address
<= (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
160 if ((FwVolHeader
->FvLength
) > (FwVolHeader
->HeaderLength
)) {
162 // BUGBUG: Assume one FV has one type of BlockLength
164 FvbMapEntry
= &FwVolHeader
->BlockMap
[0];
165 for (LbaIndex
= 1; LbaIndex
<= FvbMapEntry
->NumBlocks
; LbaIndex
+= 1) {
166 if (Address
< (FvbBaseAddress
+ FvbMapEntry
->Length
* LbaIndex
)) {
168 // Found the (Lba, Offset)
171 *Offset
= (UINTN
) (Address
- (FvbBaseAddress
+ FvbMapEntry
->Length
* (LbaIndex
- 1)));
182 Writes a buffer to variable storage space, in the working block.
184 This function writes a buffer to variable storage space into firmware
185 volume block device. The destination is specified by parameter
186 VariableBase. Fault Tolerant Write protocol is used for writing.
188 @param VariableBase Base address of variable to write
189 @param Buffer Point to the data buffer
190 @param BufferSize The number of bytes of the data Buffer
192 @retval EFI_SUCCESS The function completed successfully
193 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol
194 @retval EFI_ABORTED The function could not complete successfully
199 IN EFI_PHYSICAL_ADDRESS VariableBase
,
205 EFI_HANDLE FvbHandle
;
206 EFI_FTW_LITE_PROTOCOL
*FtwLiteProtocol
;
213 // Locate fault tolerant write protocol
215 Status
= gBS
->LocateProtocol (
216 &gEfiFaultTolerantWriteLiteProtocolGuid
,
218 (VOID
**) &FtwLiteProtocol
220 if (EFI_ERROR (Status
)) {
221 return EFI_NOT_FOUND
;
224 // Locate Fvb handle by address
226 Status
= GetFvbHandleByAddress (VariableBase
, &FvbHandle
);
227 if (EFI_ERROR (Status
)) {
231 // Get LBA and Offset by address
233 Status
= GetLbaAndOffsetByAddress (VariableBase
, &VarLba
, &VarOffset
);
234 if (EFI_ERROR (Status
)) {
238 // Prepare for the variable data
240 FtwBufferSize
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
))->Size
;
241 FtwBuffer
= AllocateRuntimePool (FtwBufferSize
);
242 if (FtwBuffer
== NULL
) {
243 return EFI_OUT_OF_RESOURCES
;
246 SetMem (FtwBuffer
, FtwBufferSize
, (UINT8
) 0xff);
247 CopyMem (FtwBuffer
, Buffer
, BufferSize
);
252 Status
= FtwLiteProtocol
->Write (
257 &FtwBufferSize
, // NumBytes
261 FreePool (FtwBuffer
);