]>
Commit | Line | Data |
---|---|---|
0c18794e | 1 | /** @file\r |
2 | Handles non-volatile variable store garbage collection, using FTW\r | |
3 | (Fault Tolerant Write) protocol.\r | |
4 | \r | |
5 | Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r | |
6 | This program and the accompanying materials \r | |
7 | are licensed and made available under the terms and conditions of the BSD License \r | |
8 | which accompanies this distribution. The full text of the license may be found at \r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include "Variable.h"\r | |
17 | \r | |
18 | /**\r | |
19 | Gets firmware volume block handle by given address.\r | |
20 | \r | |
21 | This function gets firmware volume block handle whose\r | |
22 | address range contains the parameter Address.\r | |
23 | \r | |
24 | @param[in] Address Address which should be contained\r | |
25 | by returned FVB handle.\r | |
26 | @param[out] FvbHandle Pointer to FVB handle for output.\r | |
27 | \r | |
28 | @retval EFI_SUCCESS FVB handle successfully returned.\r | |
29 | @retval EFI_NOT_FOUND Failed to find FVB handle by address.\r | |
30 | \r | |
31 | **/\r | |
32 | EFI_STATUS\r | |
33 | GetFvbHandleByAddress (\r | |
34 | IN EFI_PHYSICAL_ADDRESS Address,\r | |
35 | OUT EFI_HANDLE *FvbHandle\r | |
36 | )\r | |
37 | {\r | |
38 | EFI_STATUS Status;\r | |
39 | EFI_HANDLE *HandleBuffer;\r | |
40 | UINTN HandleCount;\r | |
41 | UINTN Index;\r | |
42 | EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r | |
43 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r | |
44 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
45 | \r | |
46 | *FvbHandle = NULL;\r | |
47 | //\r | |
48 | // Locate all handles with Firmware Volume Block protocol\r | |
49 | //\r | |
50 | Status = gBS->LocateHandleBuffer (\r | |
51 | ByProtocol,\r | |
52 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
53 | NULL,\r | |
54 | &HandleCount,\r | |
55 | &HandleBuffer\r | |
56 | );\r | |
57 | if (EFI_ERROR (Status)) {\r | |
58 | return EFI_NOT_FOUND;\r | |
59 | }\r | |
60 | //\r | |
61 | // Traverse all the handles, searching for the one containing parameter Address\r | |
62 | //\r | |
63 | for (Index = 0; Index < HandleCount; Index += 1) {\r | |
64 | Status = gBS->HandleProtocol (\r | |
65 | HandleBuffer[Index],\r | |
66 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
67 | (VOID **) &Fvb\r | |
68 | );\r | |
69 | if (EFI_ERROR (Status)) {\r | |
70 | Status = EFI_NOT_FOUND;\r | |
71 | break;\r | |
72 | }\r | |
73 | //\r | |
74 | // Checks if the address range of this handle contains parameter Address\r | |
75 | //\r | |
76 | Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r | |
77 | if (EFI_ERROR (Status)) {\r | |
78 | continue;\r | |
79 | }\r | |
80 | \r | |
81 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r | |
82 | if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + FwVolHeader->FvLength))) {\r | |
83 | *FvbHandle = HandleBuffer[Index];\r | |
84 | Status = EFI_SUCCESS;\r | |
85 | break;\r | |
86 | }\r | |
87 | }\r | |
88 | \r | |
89 | FreePool (HandleBuffer);\r | |
90 | return Status;\r | |
91 | }\r | |
92 | \r | |
93 | /**\r | |
94 | Gets LBA of block and offset by given address.\r | |
95 | \r | |
96 | This function gets the Logical Block Address (LBA) of firmware\r | |
97 | volume block containing the given address, and the offset of\r | |
98 | address on the block.\r | |
99 | \r | |
100 | @param[in] Address Address which should be contained\r | |
101 | by returned FVB handle.\r | |
102 | @param[out] Lba The pointer to LBA for output.\r | |
103 | @param[out] Offset The pointer to offset for output.\r | |
104 | \r | |
105 | @retval EFI_SUCCESS LBA and offset successfully returned.\r | |
106 | @retval EFI_NOT_FOUND Failed to find FVB handle by address.\r | |
107 | @retval EFI_ABORTED Failed to find valid LBA and offset.\r | |
108 | \r | |
109 | **/\r | |
110 | EFI_STATUS\r | |
111 | GetLbaAndOffsetByAddress (\r | |
112 | IN EFI_PHYSICAL_ADDRESS Address,\r | |
113 | OUT EFI_LBA *Lba,\r | |
114 | OUT UINTN *Offset\r | |
115 | )\r | |
116 | {\r | |
117 | EFI_STATUS Status;\r | |
118 | EFI_HANDLE FvbHandle;\r | |
119 | EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r | |
120 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r | |
121 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
122 | EFI_FV_BLOCK_MAP_ENTRY *FvbMapEntry;\r | |
123 | UINT32 LbaIndex;\r | |
124 | \r | |
125 | *Lba = (EFI_LBA) (-1);\r | |
126 | *Offset = 0;\r | |
127 | \r | |
128 | //\r | |
129 | // Gets firmware volume block handle by given address.\r | |
130 | //\r | |
131 | Status = GetFvbHandleByAddress (Address, &FvbHandle);\r | |
132 | if (EFI_ERROR (Status)) {\r | |
133 | return Status;\r | |
134 | }\r | |
135 | \r | |
136 | Status = gBS->HandleProtocol (\r | |
137 | FvbHandle,\r | |
138 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
139 | (VOID **) &Fvb\r | |
140 | );\r | |
141 | if (EFI_ERROR (Status)) {\r | |
142 | return Status;\r | |
143 | }\r | |
144 | //\r | |
145 | // Get the Base Address of FV\r | |
146 | //\r | |
147 | Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r | |
148 | if (EFI_ERROR (Status)) {\r | |
149 | return Status;\r | |
150 | }\r | |
151 | \r | |
152 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r | |
153 | \r | |
154 | //\r | |
155 | // Get the (LBA, Offset) of Address\r | |
156 | //\r | |
157 | if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + FwVolHeader->FvLength))) {\r | |
158 | if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r | |
159 | //\r | |
160 | // BUGBUG: Assume one FV has one type of BlockLength\r | |
161 | //\r | |
162 | FvbMapEntry = &FwVolHeader->BlockMap[0];\r | |
163 | for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r | |
164 | if (Address < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex)) {\r | |
165 | //\r | |
166 | // Found the (Lba, Offset)\r | |
167 | //\r | |
168 | *Lba = LbaIndex - 1;\r | |
169 | *Offset = (UINTN) (Address - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r | |
170 | return EFI_SUCCESS;\r | |
171 | }\r | |
172 | }\r | |
173 | }\r | |
174 | }\r | |
175 | \r | |
176 | return EFI_ABORTED;\r | |
177 | }\r | |
178 | \r | |
179 | /**\r | |
180 | Writes a buffer to variable storage space.\r | |
181 | \r | |
182 | This function writes a buffer to variable storage space into firmware\r | |
183 | volume block device. The destination is specified by parameter\r | |
184 | VariableBase. Fault Tolerant Write protocol is used for writing.\r | |
185 | \r | |
186 | @param[in] VariableBase The base address of the variable to write.\r | |
187 | @param[in] Buffer Points to the data buffer.\r | |
188 | @param[in] BufferSize The number of bytes of the data Buffer.\r | |
189 | \r | |
190 | @retval EFI_SUCCESS The function completed successfully.\r | |
191 | @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.\r | |
192 | @retval Other The function could not complete successfully.\r | |
193 | \r | |
194 | **/\r | |
195 | EFI_STATUS\r | |
196 | FtwVariableSpace (\r | |
197 | IN EFI_PHYSICAL_ADDRESS VariableBase,\r | |
198 | IN UINT8 *Buffer,\r | |
199 | IN UINTN BufferSize\r | |
200 | )\r | |
201 | {\r | |
202 | EFI_STATUS Status;\r | |
203 | EFI_HANDLE FvbHandle;\r | |
204 | EFI_LBA VarLba;\r | |
205 | UINTN VarOffset;\r | |
206 | UINT8 *FtwBuffer;\r | |
207 | UINTN FtwBufferSize;\r | |
208 | EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r | |
209 | \r | |
210 | //\r | |
211 | // Locate Fault Tolerant Write protocol\r | |
212 | //\r | |
213 | Status = gBS->LocateProtocol (\r | |
214 | &gEfiFaultTolerantWriteProtocolGuid,\r | |
215 | NULL,\r | |
216 | (VOID **) &FtwProtocol\r | |
217 | );\r | |
218 | if (EFI_ERROR (Status)) {\r | |
219 | return EFI_NOT_FOUND;\r | |
220 | }\r | |
221 | //\r | |
222 | // Gets firmware volume block handle by VariableBase.\r | |
223 | //\r | |
224 | Status = GetFvbHandleByAddress (VariableBase, &FvbHandle);\r | |
225 | if (EFI_ERROR (Status)) {\r | |
226 | return Status;\r | |
227 | }\r | |
228 | //\r | |
229 | // Gets LBA of block and offset by VariableBase.\r | |
230 | //\r | |
231 | Status = GetLbaAndOffsetByAddress (VariableBase, &VarLba, &VarOffset);\r | |
232 | if (EFI_ERROR (Status)) {\r | |
233 | return EFI_ABORTED;\r | |
234 | }\r | |
235 | //\r | |
236 | // Prepare for the variable data\r | |
237 | //\r | |
238 | FtwBufferSize = ((VARIABLE_STORE_HEADER *) ((UINTN) VariableBase))->Size;\r | |
239 | FtwBuffer = AllocatePool (FtwBufferSize);\r | |
240 | if (FtwBuffer == NULL) {\r | |
241 | return EFI_OUT_OF_RESOURCES;\r | |
242 | }\r | |
243 | \r | |
244 | SetMem (FtwBuffer, FtwBufferSize, (UINT8) 0xff);\r | |
245 | CopyMem (FtwBuffer, Buffer, BufferSize);\r | |
246 | \r | |
247 | //\r | |
248 | // FTW write record\r | |
249 | //\r | |
250 | Status = FtwProtocol->Write (\r | |
251 | FtwProtocol,\r | |
252 | VarLba, // LBA\r | |
253 | VarOffset, // Offset\r | |
254 | FtwBufferSize, // NumBytes,\r | |
255 | NULL,\r | |
256 | FvbHandle,\r | |
257 | FtwBuffer\r | |
258 | );\r | |
259 | \r | |
260 | FreePool (FtwBuffer);\r | |
261 | return Status;\r | |
262 | }\r |