2 Handles non-volatile variable store garbage collection, using FTW
3 (Fault Tolerant Write) protocol.
5 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 Gets LBA of block and offset by given address.
15 This function gets the Logical Block Address (LBA) of a firmware
16 volume block containing the given address, and the offset of the
19 @param Address Address which should be contained
20 by returned FVB handle.
21 @param Lba Pointer to LBA for output.
22 @param Offset Pointer to offset for output.
24 @retval EFI_SUCCESS LBA and offset successfully returned.
25 @retval EFI_NOT_FOUND Fail to find FVB handle by address.
26 @retval EFI_ABORTED Fail to find valid LBA and offset.
30 GetLbaAndOffsetByAddress (
31 IN EFI_PHYSICAL_ADDRESS Address
,
37 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
38 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
39 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
40 EFI_FV_BLOCK_MAP_ENTRY
*FvbMapEntry
;
44 *Lba
= (EFI_LBA
) (-1);
48 // Get the proper FVB protocol.
50 Status
= GetFvbInfoByAddress (Address
, NULL
, &Fvb
);
51 if (EFI_ERROR (Status
)) {
56 // Get the Base Address of FV.
58 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
59 if (EFI_ERROR (Status
)) {
63 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
66 // Get the (LBA, Offset) of Address.
68 if ((FwVolHeader
->FvLength
) > (FwVolHeader
->HeaderLength
)) {
70 // BUGBUG: Assume one FV has one type of BlockLength.
72 FvbMapEntry
= &FwVolHeader
->BlockMap
[0];
73 for (LbaIndex
= 1; LbaIndex
<= FvbMapEntry
->NumBlocks
; LbaIndex
+= 1) {
74 if (Address
< (FvbBaseAddress
+ FvbMapEntry
->Length
* LbaIndex
)) {
76 // Found the (Lba, Offset).
79 *Offset
= (UINTN
) (Address
- (FvbBaseAddress
+ FvbMapEntry
->Length
* (LbaIndex
- 1)));
89 Writes a buffer to variable storage space, in the working block.
91 This function writes a buffer to variable storage space into a firmware
92 volume block device. The destination is specified by parameter
93 VariableBase. Fault Tolerant Write protocol is used for writing.
95 @param VariableBase Base address of variable to write
96 @param VariableBuffer Point to the variable data buffer.
98 @retval EFI_SUCCESS The function completed successfully.
99 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.
100 @retval EFI_ABORTED The function could not complete successfully.
105 IN EFI_PHYSICAL_ADDRESS VariableBase
,
106 IN VARIABLE_STORE_HEADER
*VariableBuffer
110 EFI_HANDLE FvbHandle
;
114 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
117 // Locate fault tolerant write protocol.
119 Status
= GetFtwProtocol((VOID
**) &FtwProtocol
);
120 if (EFI_ERROR (Status
)) {
121 return EFI_NOT_FOUND
;
124 // Locate Fvb handle by address.
126 Status
= GetFvbInfoByAddress (VariableBase
, &FvbHandle
, NULL
);
127 if (EFI_ERROR (Status
)) {
131 // Get LBA and Offset by address.
133 Status
= GetLbaAndOffsetByAddress (VariableBase
, &VarLba
, &VarOffset
);
134 if (EFI_ERROR (Status
)) {
138 FtwBufferSize
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
))->Size
;
139 ASSERT (FtwBufferSize
== VariableBuffer
->Size
);
144 Status
= FtwProtocol
->Write (
148 FtwBufferSize
, // NumBytes
149 NULL
, // PrivateData NULL
150 FvbHandle
, // Fvb Handle
151 (VOID
*) VariableBuffer
// write buffer