2 Handles non-volatile variable store garbage collection, using FTW
3 (Fault Tolerant Write) protocol.
5 Copyright (c) 2009 - 2010, 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 LBA of block and offset by given address.
21 This function gets the Logical Block Address (LBA) of a firmware
22 volume block containing the given address, and the offset of the
25 @param Address Address which should be contained
26 by returned FVB handle.
27 @param Lba Pointer to LBA for output.
28 @param Offset Pointer to offset for output.
30 @retval EFI_SUCCESS LBA and offset successfully returned.
31 @retval EFI_NOT_FOUND Fail to find FVB handle by address.
32 @retval EFI_ABORTED Fail to find valid LBA and offset.
36 GetLbaAndOffsetByAddress (
37 IN EFI_PHYSICAL_ADDRESS Address
,
43 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
44 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
45 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
46 EFI_FV_BLOCK_MAP_ENTRY
*FvbMapEntry
;
49 *Lba
= (EFI_LBA
) (-1);
53 // Get the proper FVB protocol.
55 Status
= GetFvbInfoByAddress (Address
, NULL
, &Fvb
);
56 if (EFI_ERROR (Status
)) {
61 // Get the Base Address of FV.
63 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
64 if (EFI_ERROR (Status
)) {
68 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
71 // Get the (LBA, Offset) of Address.
73 if ((FwVolHeader
->FvLength
) > (FwVolHeader
->HeaderLength
)) {
75 // BUGBUG: Assume one FV has one type of BlockLength.
77 FvbMapEntry
= &FwVolHeader
->BlockMap
[0];
78 for (LbaIndex
= 1; LbaIndex
<= FvbMapEntry
->NumBlocks
; LbaIndex
+= 1) {
79 if (Address
< (FvbBaseAddress
+ FvbMapEntry
->Length
* LbaIndex
)) {
81 // Found the (Lba, Offset).
84 *Offset
= (UINTN
) (Address
- (FvbBaseAddress
+ FvbMapEntry
->Length
* (LbaIndex
- 1)));
94 Writes a buffer to variable storage space, in the working block.
96 This function writes a buffer to variable storage space into a firmware
97 volume block device. The destination is specified by parameter
98 VariableBase. Fault Tolerant Write protocol is used for writing.
100 @param VariableBase Base address of variable to write
101 @param Buffer Point to the data buffer.
102 @param BufferSize The number of bytes of the data Buffer.
104 @retval EFI_SUCCESS The function completed successfully.
105 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.
106 @retval EFI_ABORTED The function could not complete successfully.
111 IN EFI_PHYSICAL_ADDRESS VariableBase
,
117 EFI_HANDLE FvbHandle
;
122 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
125 // Locate fault tolerant write protocol.
127 Status
= GetFtwProtocol((VOID
**) &FtwProtocol
);
128 if (EFI_ERROR (Status
)) {
129 return EFI_NOT_FOUND
;
132 // Locate Fvb handle by address.
134 Status
= GetFvbInfoByAddress (VariableBase
, &FvbHandle
, NULL
);
135 if (EFI_ERROR (Status
)) {
139 // Get LBA and Offset by address.
141 Status
= GetLbaAndOffsetByAddress (VariableBase
, &VarLba
, &VarOffset
);
142 if (EFI_ERROR (Status
)) {
146 // Prepare for the variable data.
148 FtwBufferSize
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
))->Size
;
149 FtwBuffer
= AllocatePool (FtwBufferSize
);
150 if (FtwBuffer
== NULL
) {
151 return EFI_OUT_OF_RESOURCES
;
154 SetMem (FtwBuffer
, FtwBufferSize
, (UINT8
) 0xff);
155 CopyMem (FtwBuffer
, Buffer
, BufferSize
);
160 Status
= FtwProtocol
->Write (
164 FtwBufferSize
, // NumBytes
165 NULL
, // PrivateData NULL
166 FvbHandle
, // Fvb Handle
167 FtwBuffer
// write buffer
170 FreePool (FtwBuffer
);