]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/Reclaim.c
Vlv2TbltDevicePkg: Link AuthVariableLib for following merged variable driver deploy
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Reclaim.c
CommitLineData
504214c4
LG
1/** @file\r
2 \r
3 Handles non-volatile variable store garbage collection, using FTW\r
4 (Fault Tolerant Write) protocol.\r
8d3a5c82 5\r
4e1005ec 6Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 7This program and the accompanying materials\r
8d3a5c82 8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
504214c4 15**/\r
8d3a5c82 16\r
3cfb790c 17#include "Variable.h"\r
8d3a5c82 18\r
7c80e839 19/**\r
20 Gets LBA of block and offset by given address.\r
21\r
8a2d4996 22 This function gets the Logical Block Address (LBA) of a firmware\r
23 volume block containing the given address, and the offset of the\r
7c80e839 24 address on the block.\r
25\r
26 @param Address Address which should be contained\r
8a2d4996 27 by returned FVB handle.\r
28 @param Lba Pointer to LBA for output.\r
29 @param Offset Pointer to offset for output.\r
7c80e839 30\r
8a2d4996 31 @retval EFI_SUCCESS LBA and offset successfully returned.\r
32 @retval EFI_NOT_FOUND Fail to find FVB handle by address.\r
33 @retval EFI_ABORTED Fail to find valid LBA and offset.\r
7c80e839 34\r
35**/\r
8d3a5c82 36EFI_STATUS\r
37GetLbaAndOffsetByAddress (\r
38 IN EFI_PHYSICAL_ADDRESS Address,\r
39 OUT EFI_LBA *Lba,\r
40 OUT UINTN *Offset\r
41 )\r
42{\r
43 EFI_STATUS Status;\r
8d3a5c82 44 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
45 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
46 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
47 EFI_FV_BLOCK_MAP_ENTRY *FvbMapEntry;\r
48 UINT32 LbaIndex;\r
49\r
4e1005ec 50 Fvb = NULL;\r
8d3a5c82 51 *Lba = (EFI_LBA) (-1);\r
52 *Offset = 0;\r
8a2d4996 53 \r
8d3a5c82 54 //\r
8a2d4996 55 // Get the proper FVB protocol.\r
8d3a5c82 56 //\r
8a2d4996 57 Status = GetFvbInfoByAddress (Address, NULL, &Fvb);\r
8d3a5c82 58 if (EFI_ERROR (Status)) {\r
59 return Status;\r
60 }\r
61\r
8d3a5c82 62 //\r
8a2d4996 63 // Get the Base Address of FV.\r
8d3a5c82 64 //\r
65 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
66 if (EFI_ERROR (Status)) {\r
67 return Status;\r
68 }\r
69\r
70 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
71\r
72 //\r
8a2d4996 73 // Get the (LBA, Offset) of Address.\r
8d3a5c82 74 //\r
8a2d4996 75 if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
76 //\r
77 // BUGBUG: Assume one FV has one type of BlockLength.\r
78 //\r
79 FvbMapEntry = &FwVolHeader->BlockMap[0];\r
80 for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
81 if (Address < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex)) {\r
82 //\r
83 // Found the (Lba, Offset).\r
84 //\r
85 *Lba = LbaIndex - 1;\r
86 *Offset = (UINTN) (Address - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r
87 return EFI_SUCCESS;\r
88 }\r
8d3a5c82 89 }\r
90 }\r
91\r
92 return EFI_ABORTED;\r
93}\r
94\r
7c80e839 95/**\r
96 Writes a buffer to variable storage space, in the working block.\r
97\r
8a2d4996 98 This function writes a buffer to variable storage space into a firmware\r
7c80e839 99 volume block device. The destination is specified by parameter\r
100 VariableBase. Fault Tolerant Write protocol is used for writing.\r
101\r
102 @param VariableBase Base address of variable to write\r
128ef095 103 @param VariableBuffer Point to the variable data buffer.\r
7c80e839 104\r
8a2d4996 105 @retval EFI_SUCCESS The function completed successfully.\r
106 @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.\r
107 @retval EFI_ABORTED The function could not complete successfully.\r
7c80e839 108\r
109**/\r
8d3a5c82 110EFI_STATUS\r
111FtwVariableSpace (\r
112 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
128ef095 113 IN VARIABLE_STORE_HEADER *VariableBuffer\r
8d3a5c82 114 )\r
8d3a5c82 115{\r
88a5561c
LG
116 EFI_STATUS Status;\r
117 EFI_HANDLE FvbHandle;\r
118 EFI_LBA VarLba;\r
119 UINTN VarOffset;\r
88a5561c
LG
120 UINTN FtwBufferSize;\r
121 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
8d3a5c82 122\r
123 //\r
8a2d4996 124 // Locate fault tolerant write protocol.\r
8d3a5c82 125 //\r
8a2d4996 126 Status = GetFtwProtocol((VOID **) &FtwProtocol);\r
8d3a5c82 127 if (EFI_ERROR (Status)) {\r
128 return EFI_NOT_FOUND;\r
129 }\r
130 //\r
8a2d4996 131 // Locate Fvb handle by address.\r
8d3a5c82 132 //\r
8a2d4996 133 Status = GetFvbInfoByAddress (VariableBase, &FvbHandle, NULL);\r
8d3a5c82 134 if (EFI_ERROR (Status)) {\r
135 return Status;\r
136 }\r
137 //\r
8a2d4996 138 // Get LBA and Offset by address.\r
8d3a5c82 139 //\r
140 Status = GetLbaAndOffsetByAddress (VariableBase, &VarLba, &VarOffset);\r
141 if (EFI_ERROR (Status)) {\r
142 return EFI_ABORTED;\r
143 }\r
8d3a5c82 144\r
128ef095
SZ
145 FtwBufferSize = ((VARIABLE_STORE_HEADER *) ((UINTN) VariableBase))->Size;\r
146 ASSERT (FtwBufferSize == VariableBuffer->Size);\r
8d3a5c82 147\r
148 //\r
8a2d4996 149 // FTW write record.\r
8d3a5c82 150 //\r
88a5561c 151 Status = FtwProtocol->Write (\r
8a2d4996 152 FtwProtocol,\r
153 VarLba, // LBA\r
154 VarOffset, // Offset\r
155 FtwBufferSize, // NumBytes\r
156 NULL, // PrivateData NULL\r
157 FvbHandle, // Fvb Handle\r
128ef095 158 (VOID *) VariableBuffer // write buffer\r
8a2d4996 159 );\r
8d3a5c82 160\r
8d3a5c82 161 return Status;\r
162}\r