]>
Commit | Line | Data |
---|---|---|
052ad7e1 A |
1 | /** @file\r |
2 | EFI Runtime Variable services.\r | |
3 | \r | |
4 | Copyright (c) 2006 - 2007, Intel Corporation \r | |
5 | All rights reserved. This program and the accompanying materials \r | |
6 | are licensed and made available under the terms and conditions of the BSD License \r | |
7 | which accompanies this distribution. The full text of the license may be found at \r | |
8 | http://opensource.org/licenses/bsd-license.php \r | |
8d3a5c82 | 9 | \r |
052ad7e1 A |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
8d3a5c82 | 12 | \r |
052ad7e1 | 13 | **/\r |
8d3a5c82 | 14 | \r |
15 | \r | |
16 | #include "Variable.h"\r | |
33a5a666 A |
17 | \r |
18 | \r | |
052ad7e1 A |
19 | VARIABLE_MODULE_GLOBAL mRuntimeData;\r |
20 | VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal = &mRuntimeData;\r | |
21 | EFI_EVENT mVirtualAddressChangeEvent = NULL;\r | |
22 | EFI_HANDLE mHandle = NULL;\r | |
8d3a5c82 | 23 | \r |
33a5a666 | 24 | \r |
8d3a5c82 | 25 | //\r |
26 | // This is a temperary function which will be removed\r | |
27 | // when EfiAcquireLock in UefiLib can handle the\r | |
28 | // the call in UEFI Runtimer driver in RT phase.\r | |
29 | //\r | |
8d3a5c82 | 30 | VOID\r |
31 | AcquireLockOnlyAtBootTime (\r | |
32 | IN EFI_LOCK *Lock\r | |
33 | )\r | |
34 | {\r | |
35 | if (!EfiAtRuntime ()) {\r | |
36 | EfiAcquireLock (Lock);\r | |
37 | }\r | |
38 | }\r | |
39 | \r | |
40 | //\r | |
41 | // This is a temperary function which will be removed\r | |
42 | // when EfiAcquireLock in UefiLib can handle the\r | |
43 | // the call in UEFI Runtimer driver in RT phase.\r | |
44 | //\r | |
8d3a5c82 | 45 | VOID\r |
46 | ReleaseLockOnlyAtBootTime (\r | |
47 | IN EFI_LOCK *Lock\r | |
48 | )\r | |
49 | {\r | |
50 | if (!EfiAtRuntime ()) {\r | |
51 | EfiReleaseLock (Lock);\r | |
52 | }\r | |
53 | }\r | |
54 | \r | |
33a5a666 A |
55 | \r |
56 | GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r | |
57 | \r | |
58 | \r | |
052ad7e1 A |
59 | /**\r |
60 | Routine used to track statistical information about variable usage. \r | |
61 | The data is stored in the EFI system table so it can be accessed later.\r | |
62 | VariableInfo.efi can dump out the table. Only Boot Services variable \r | |
63 | accesses are tracked by this code. The PcdVariableCollectStatistics\r | |
64 | build flag controls if this feature is enabled. \r | |
65 | \r | |
66 | A read that hits in the cache will have Read and Cache true for \r | |
67 | the transaction. Data is allocated by this routine, but never\r | |
68 | freed.\r | |
69 | \r | |
70 | @param[in] VariableName Name of the Variable to track\r | |
71 | @param[in] VendorGuid Guid of the Variable to track\r | |
72 | @param[in] Volatile TRUE if volatile FALSE if non-volatile\r | |
73 | @param[in] Read TRUE if GetVariable() was called\r | |
74 | @param[in] Write TRUE if SetVariable() was called\r | |
75 | @param[in] Delete TRUE if deleted via SetVariable()\r | |
76 | @param[in] Cache TRUE for a cache hit.\r | |
77 | \r | |
78 | **/\r | |
33a5a666 A |
79 | VOID\r |
80 | UpdateVariableInfo (\r | |
81 | IN CHAR16 *VariableName,\r | |
82 | IN EFI_GUID *VendorGuid,\r | |
83 | IN BOOLEAN Volatile,\r | |
84 | IN BOOLEAN Read,\r | |
85 | IN BOOLEAN Write,\r | |
86 | IN BOOLEAN Delete,\r | |
87 | IN BOOLEAN Cache\r | |
88 | )\r | |
89 | {\r | |
90 | VARIABLE_INFO_ENTRY *Entry;\r | |
91 | \r | |
92 | if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r | |
93 | \r | |
94 | if (EfiAtRuntime ()) {\r | |
95 | // Don't collect statistics at runtime\r | |
96 | return;\r | |
97 | }\r | |
98 | \r | |
99 | if (gVariableInfo == NULL) {\r | |
052ad7e1 A |
100 | //\r |
101 | // on the first call allocate a entry and place a pointer to it in\r | |
102 | // the EFI System Table\r | |
103 | //\r | |
33a5a666 | 104 | gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r |
052ad7e1 A |
105 | ASSERT (gVariableInfo != NULL);\r |
106 | \r | |
33a5a666 A |
107 | CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r |
108 | gVariableInfo->Name = AllocatePool (StrLen (VariableName));\r | |
109 | StrCpy (gVariableInfo->Name, VariableName);\r | |
110 | gVariableInfo->Volatile = Volatile;\r | |
111 | \r | |
112 | gBS->InstallConfigurationTable (&gEfiVariableInfoGuid, gVariableInfo);\r | |
113 | }\r | |
114 | \r | |
115 | \r | |
116 | for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r | |
117 | if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r | |
118 | if (StrCmp (VariableName, Entry->Name) == 0) {\r | |
119 | if (Read) {\r | |
120 | Entry->ReadCount++;\r | |
121 | }\r | |
122 | if (Write) {\r | |
123 | Entry->WriteCount++;\r | |
124 | }\r | |
125 | if (Delete) {\r | |
126 | Entry->DeleteCount++;\r | |
127 | }\r | |
128 | if (Cache) {\r | |
129 | Entry->CacheCount++;\r | |
130 | }\r | |
131 | \r | |
132 | return;\r | |
133 | }\r | |
134 | }\r | |
135 | \r | |
136 | if (Entry->Next == NULL) {\r | |
052ad7e1 A |
137 | //\r |
138 | // If the entry is not in the table add it.\r | |
139 | // Next iteration of the loop will fill in the data\r | |
140 | //\r | |
33a5a666 | 141 | Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r |
052ad7e1 | 142 | ASSERT (Entry->Next != NULL);\r |
33a5a666 A |
143 | \r |
144 | CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r | |
145 | Entry->Next->Name = AllocatePool (StrLen (VariableName));\r | |
146 | StrCpy (Entry->Next->Name, VariableName);\r | |
147 | Entry->Next->Volatile = Volatile;\r | |
148 | }\r | |
149 | \r | |
150 | }\r | |
151 | }\r | |
152 | }\r | |
153 | \r | |
154 | \r | |
155 | \r | |
8d3a5c82 | 156 | BOOLEAN\r |
8d3a5c82 | 157 | IsValidVariableHeader (\r |
158 | IN VARIABLE_HEADER *Variable\r | |
159 | )\r | |
160 | /*++\r | |
161 | \r | |
162 | Routine Description:\r | |
163 | \r | |
164 | This code checks if variable header is valid or not.\r | |
165 | \r | |
166 | Arguments:\r | |
167 | Variable Pointer to the Variable Header.\r | |
168 | \r | |
169 | Returns:\r | |
170 | TRUE Variable header is valid.\r | |
171 | FALSE Variable header is not valid.\r | |
172 | \r | |
173 | --*/\r | |
174 | {\r | |
175 | if (Variable == NULL ||\r | |
176 | Variable->StartId != VARIABLE_DATA ||\r | |
177 | (sizeof (VARIABLE_HEADER) + Variable->NameSize + Variable->DataSize) > MAX_VARIABLE_SIZE\r | |
178 | ) {\r | |
179 | return FALSE;\r | |
180 | }\r | |
181 | \r | |
182 | return TRUE;\r | |
183 | }\r | |
184 | \r | |
052ad7e1 | 185 | \r |
8d3a5c82 | 186 | EFI_STATUS\r |
8d3a5c82 | 187 | UpdateVariableStore (\r |
188 | IN VARIABLE_GLOBAL *Global,\r | |
189 | IN BOOLEAN Volatile,\r | |
190 | IN BOOLEAN SetByIndex,\r | |
191 | IN UINTN Instance,\r | |
192 | IN UINTN DataPtrIndex,\r | |
193 | IN UINT32 DataSize,\r | |
194 | IN UINT8 *Buffer\r | |
195 | )\r | |
196 | /*++\r | |
197 | \r | |
198 | Routine Description:\r | |
199 | \r | |
200 | This function writes data to the FWH at the correct LBA even if the LBAs\r | |
201 | are fragmented.\r | |
202 | \r | |
203 | Arguments:\r | |
204 | \r | |
c6492839 | 205 | Global - Pointer to VARAIBLE_GLOBAL structure\r |
206 | Volatile - If the Variable is Volatile or Non-Volatile\r | |
207 | SetByIndex - TRUE: Target pointer is given as index\r | |
208 | FALSE: Target pointer is absolute\r | |
209 | Instance - Instance of FV Block services\r | |
210 | DataPtrIndex - Pointer to the Data from the end of VARIABLE_STORE_HEADER\r | |
211 | structure\r | |
212 | DataSize - Size of data to be written.\r | |
213 | Buffer - Pointer to the buffer from which data is written\r | |
8d3a5c82 | 214 | \r |
215 | Returns:\r | |
216 | \r | |
c6492839 | 217 | EFI_INVALID_PARAMETER - Parameters not valid\r |
218 | EFI_SUCCESS - Variable store successfully updated\r | |
8d3a5c82 | 219 | \r |
220 | --*/\r | |
221 | {\r | |
222 | EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r | |
223 | UINTN BlockIndex2;\r | |
224 | UINTN LinearOffset;\r | |
225 | UINTN CurrWriteSize;\r | |
226 | UINTN CurrWritePtr;\r | |
227 | UINT8 *CurrBuffer;\r | |
228 | EFI_LBA LbaNumber;\r | |
229 | UINTN Size;\r | |
230 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
231 | VARIABLE_STORE_HEADER *VolatileBase;\r | |
232 | EFI_PHYSICAL_ADDRESS FvVolHdr;\r | |
233 | EFI_PHYSICAL_ADDRESS DataPtr;\r | |
234 | EFI_STATUS Status;\r | |
235 | \r | |
236 | FwVolHeader = NULL;\r | |
237 | DataPtr = DataPtrIndex;\r | |
238 | \r | |
239 | //\r | |
240 | // Check if the Data is Volatile\r | |
241 | //\r | |
242 | if (!Volatile) {\r | |
243 | EfiFvbGetPhysicalAddress (Instance, &FvVolHdr);\r | |
244 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r | |
245 | //\r | |
246 | // Data Pointer should point to the actual Address where data is to be\r | |
247 | // written\r | |
248 | //\r | |
249 | if (SetByIndex) {\r | |
052ad7e1 | 250 | DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r |
8d3a5c82 | 251 | }\r |
252 | \r | |
253 | if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r | |
254 | return EFI_INVALID_PARAMETER;\r | |
255 | }\r | |
256 | } else {\r | |
257 | //\r | |
258 | // Data Pointer should point to the actual Address where data is to be\r | |
259 | // written\r | |
260 | //\r | |
052ad7e1 | 261 | VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r |
8d3a5c82 | 262 | if (SetByIndex) {\r |
052ad7e1 | 263 | DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r |
8d3a5c82 | 264 | }\r |
265 | \r | |
266 | if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r | |
267 | return EFI_INVALID_PARAMETER;\r | |
268 | }\r | |
c6492839 | 269 | \r |
270 | //\r | |
271 | // If Volatile Variable just do a simple mem copy.\r | |
272 | // \r | |
273 | CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);\r | |
8d3a5c82 | 274 | return EFI_SUCCESS;\r |
275 | }\r | |
c6492839 | 276 | \r |
8d3a5c82 | 277 | //\r |
278 | // If we are here we are dealing with Non-Volatile Variables\r | |
279 | //\r | |
280 | LinearOffset = (UINTN) FwVolHeader;\r | |
281 | CurrWritePtr = (UINTN) DataPtr;\r | |
282 | CurrWriteSize = DataSize;\r | |
283 | CurrBuffer = Buffer;\r | |
284 | LbaNumber = 0;\r | |
285 | \r | |
286 | if (CurrWritePtr < LinearOffset) {\r | |
287 | return EFI_INVALID_PARAMETER;\r | |
288 | }\r | |
289 | \r | |
290 | for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r | |
291 | for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r | |
292 | //\r | |
293 | // Check to see if the Variable Writes are spanning through multiple\r | |
294 | // blocks.\r | |
295 | //\r | |
296 | if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r | |
297 | if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r | |
298 | Status = EfiFvbWriteBlock (\r | |
299 | Instance,\r | |
300 | LbaNumber,\r | |
301 | (UINTN) (CurrWritePtr - LinearOffset),\r | |
302 | &CurrWriteSize,\r | |
303 | CurrBuffer\r | |
304 | );\r | |
8d3a5c82 | 305 | return Status;\r |
8d3a5c82 | 306 | } else {\r |
307 | Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r | |
308 | Status = EfiFvbWriteBlock (\r | |
309 | Instance,\r | |
310 | LbaNumber,\r | |
311 | (UINTN) (CurrWritePtr - LinearOffset),\r | |
312 | &Size,\r | |
313 | CurrBuffer\r | |
314 | );\r | |
315 | if (EFI_ERROR (Status)) {\r | |
316 | return Status;\r | |
317 | }\r | |
318 | \r | |
319 | CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r | |
320 | CurrBuffer = CurrBuffer + Size;\r | |
321 | CurrWriteSize = CurrWriteSize - Size;\r | |
322 | }\r | |
323 | }\r | |
324 | \r | |
325 | LinearOffset += PtrBlockMapEntry->Length;\r | |
326 | LbaNumber++;\r | |
327 | }\r | |
328 | }\r | |
329 | \r | |
330 | return EFI_SUCCESS;\r | |
331 | }\r | |
332 | \r | |
052ad7e1 | 333 | \r |
8d3a5c82 | 334 | VARIABLE_STORE_STATUS\r |
8d3a5c82 | 335 | GetVariableStoreStatus (\r |
336 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
337 | )\r | |
338 | /*++\r | |
339 | \r | |
340 | Routine Description:\r | |
341 | \r | |
342 | This code gets the current status of Variable Store.\r | |
343 | \r | |
344 | Arguments:\r | |
345 | \r | |
346 | VarStoreHeader Pointer to the Variable Store Header.\r | |
347 | \r | |
348 | Returns:\r | |
349 | \r | |
350 | EfiRaw Variable store status is raw\r | |
351 | EfiValid Variable store status is valid\r | |
352 | EfiInvalid Variable store status is invalid\r | |
353 | \r | |
354 | --*/\r | |
355 | {\r | |
356 | if (VarStoreHeader->Signature == VARIABLE_STORE_SIGNATURE &&\r | |
357 | VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r | |
358 | VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r | |
359 | ) {\r | |
360 | \r | |
361 | return EfiValid;\r | |
362 | } else if (VarStoreHeader->Signature == 0xffffffff &&\r | |
363 | VarStoreHeader->Size == 0xffffffff &&\r | |
364 | VarStoreHeader->Format == 0xff &&\r | |
365 | VarStoreHeader->State == 0xff\r | |
366 | ) {\r | |
367 | \r | |
368 | return EfiRaw;\r | |
369 | } else {\r | |
370 | return EfiInvalid;\r | |
371 | }\r | |
372 | }\r | |
373 | \r | |
052ad7e1 | 374 | \r |
8d3a5c82 | 375 | UINT8 *\r |
8d3a5c82 | 376 | GetVariableDataPtr (\r |
377 | IN VARIABLE_HEADER *Variable\r | |
378 | )\r | |
379 | /*++\r | |
380 | \r | |
381 | Routine Description:\r | |
382 | \r | |
383 | This code gets the pointer to the variable data.\r | |
384 | \r | |
385 | Arguments:\r | |
386 | \r | |
387 | Variable Pointer to the Variable Header.\r | |
388 | \r | |
389 | Returns:\r | |
390 | \r | |
391 | UINT8* Pointer to Variable Data\r | |
392 | \r | |
393 | --*/\r | |
394 | {\r | |
395 | //\r | |
396 | // Be careful about pad size for alignment\r | |
397 | //\r | |
398 | return (UINT8 *) ((UINTN) GET_VARIABLE_NAME_PTR (Variable) + Variable->NameSize + GET_PAD_SIZE (Variable->NameSize));\r | |
399 | }\r | |
400 | \r | |
052ad7e1 | 401 | \r |
8d3a5c82 | 402 | VARIABLE_HEADER *\r |
8d3a5c82 | 403 | GetNextVariablePtr (\r |
404 | IN VARIABLE_HEADER *Variable\r | |
405 | )\r | |
406 | /*++\r | |
407 | \r | |
408 | Routine Description:\r | |
409 | \r | |
410 | This code gets the pointer to the next variable header.\r | |
411 | \r | |
412 | Arguments:\r | |
413 | \r | |
414 | Variable Pointer to the Variable Header.\r | |
415 | \r | |
416 | Returns:\r | |
417 | \r | |
418 | VARIABLE_HEADER* Pointer to next variable header.\r | |
419 | \r | |
420 | --*/\r | |
421 | {\r | |
422 | if (!IsValidVariableHeader (Variable)) {\r | |
423 | return NULL;\r | |
424 | }\r | |
425 | //\r | |
426 | // Be careful about pad size for alignment\r | |
427 | //\r | |
428 | return (VARIABLE_HEADER *) ((UINTN) GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));\r | |
429 | }\r | |
430 | \r | |
052ad7e1 | 431 | \r |
8d3a5c82 | 432 | VARIABLE_HEADER *\r |
8d3a5c82 | 433 | GetEndPointer (\r |
434 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
435 | )\r | |
436 | /*++\r | |
437 | \r | |
438 | Routine Description:\r | |
439 | \r | |
440 | This code gets the pointer to the last variable memory pointer byte\r | |
441 | \r | |
442 | Arguments:\r | |
443 | \r | |
444 | VarStoreHeader Pointer to the Variable Store Header.\r | |
445 | \r | |
446 | Returns:\r | |
447 | \r | |
448 | VARIABLE_HEADER* Pointer to last unavailable Variable Header\r | |
449 | \r | |
450 | --*/\r | |
451 | {\r | |
452 | //\r | |
453 | // The end of variable store\r | |
454 | //\r | |
455 | return (VARIABLE_HEADER *) ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r | |
456 | }\r | |
457 | \r | |
052ad7e1 | 458 | \r |
8d3a5c82 | 459 | EFI_STATUS\r |
8d3a5c82 | 460 | Reclaim (\r |
461 | IN EFI_PHYSICAL_ADDRESS VariableBase,\r | |
462 | OUT UINTN *LastVariableOffset,\r | |
463 | IN BOOLEAN IsVolatile\r | |
464 | )\r | |
465 | /*++\r | |
466 | \r | |
467 | Routine Description:\r | |
468 | \r | |
469 | Variable store garbage collection and reclaim operation\r | |
470 | \r | |
471 | Arguments:\r | |
472 | \r | |
473 | VariableBase Base address of variable store\r | |
474 | LastVariableOffset Offset of last variable\r | |
475 | IsVolatile The variable store is volatile or not,\r | |
476 | if it is non-volatile, need FTW\r | |
477 | \r | |
478 | Returns:\r | |
479 | \r | |
480 | EFI STATUS\r | |
481 | \r | |
482 | --*/\r | |
483 | {\r | |
484 | VARIABLE_HEADER *Variable;\r | |
485 | VARIABLE_HEADER *NextVariable;\r | |
486 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
487 | UINT8 *ValidBuffer;\r | |
488 | UINTN ValidBufferSize;\r | |
489 | UINTN VariableSize;\r | |
490 | UINT8 *CurrPtr;\r | |
491 | EFI_STATUS Status;\r | |
492 | \r | |
493 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r | |
494 | \r | |
495 | //\r | |
496 | // Start Pointers for the variable.\r | |
497 | //\r | |
498 | Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r | |
499 | \r | |
500 | ValidBufferSize = sizeof (VARIABLE_STORE_HEADER);\r | |
501 | \r | |
502 | while (IsValidVariableHeader (Variable)) {\r | |
503 | NextVariable = GetNextVariablePtr (Variable);\r | |
504 | if (Variable->State == VAR_ADDED) {\r | |
505 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
506 | ValidBufferSize += VariableSize;\r | |
507 | }\r | |
508 | \r | |
509 | Variable = NextVariable;\r | |
510 | }\r | |
511 | \r | |
512 | ValidBuffer = AllocatePool (ValidBufferSize);\r | |
513 | if (ValidBuffer == NULL) {\r | |
514 | return EFI_OUT_OF_RESOURCES;\r | |
515 | }\r | |
516 | \r | |
517 | SetMem (ValidBuffer, ValidBufferSize, 0xff);\r | |
518 | \r | |
519 | CurrPtr = ValidBuffer;\r | |
520 | \r | |
521 | //\r | |
522 | // Copy variable store header\r | |
523 | //\r | |
524 | CopyMem (CurrPtr, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r | |
525 | CurrPtr += sizeof (VARIABLE_STORE_HEADER);\r | |
526 | \r | |
527 | //\r | |
528 | // Start Pointers for the variable.\r | |
529 | //\r | |
530 | Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r | |
531 | \r | |
532 | while (IsValidVariableHeader (Variable)) {\r | |
533 | NextVariable = GetNextVariablePtr (Variable);\r | |
534 | if (Variable->State == VAR_ADDED) {\r | |
535 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
536 | CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r | |
537 | CurrPtr += VariableSize;\r | |
538 | }\r | |
539 | \r | |
540 | Variable = NextVariable;\r | |
541 | }\r | |
542 | \r | |
543 | if (IsVolatile) {\r | |
544 | //\r | |
545 | // If volatile variable store, just copy valid buffer\r | |
546 | //\r | |
547 | SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r | |
548 | CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, ValidBufferSize);\r | |
549 | *LastVariableOffset = ValidBufferSize;\r | |
550 | Status = EFI_SUCCESS;\r | |
551 | } else {\r | |
552 | //\r | |
553 | // If non-volatile variable store, perform FTW here.\r | |
554 | //\r | |
555 | Status = FtwVariableSpace (\r | |
556 | VariableBase,\r | |
557 | ValidBuffer,\r | |
558 | ValidBufferSize\r | |
559 | );\r | |
560 | if (!EFI_ERROR (Status)) {\r | |
561 | *LastVariableOffset = ValidBufferSize;\r | |
562 | }\r | |
563 | }\r | |
564 | \r | |
565 | FreePool (ValidBuffer);\r | |
566 | \r | |
567 | if (EFI_ERROR (Status)) {\r | |
568 | *LastVariableOffset = 0;\r | |
569 | }\r | |
570 | \r | |
571 | return Status;\r | |
572 | }\r | |
573 | \r | |
33a5a666 A |
574 | \r |
575 | //\r | |
576 | // The current Hii implementation accesses this variable a larg # of times on every boot.\r | |
577 | // Other common variables are only accessed a single time. This is why this cache algorithm\r | |
578 | // only targets a single variable. Probably to get an performance improvement out of\r | |
579 | // a Cache you would need a cache that improves the search performance for a variable.\r | |
580 | //\r | |
581 | VARIABLE_CACHE_ENTRY mVariableCache[] = {\r | |
582 | {\r | |
583 | &gEfiGlobalVariableGuid,\r | |
584 | L"Lang",\r | |
585 | 0x00000000,\r | |
586 | 0x00,\r | |
587 | NULL\r | |
588 | }\r | |
589 | };\r | |
590 | \r | |
052ad7e1 A |
591 | \r |
592 | /**\r | |
593 | Update the Cache with Variable information. These are the same \r | |
594 | arguments as the EFI Variable services.\r | |
595 | \r | |
596 | @param[in] VariableName Name of variable\r | |
597 | @param[in] VendorGuid Guid of variable\r | |
598 | @param[in] Attribute Attribue of the variable\r | |
599 | @param[in] DataSize Size of data. 0 means delete\r | |
600 | @param[in] Data Variable data\r | |
601 | \r | |
602 | **/\r | |
33a5a666 A |
603 | VOID\r |
604 | UpdateVariableCache (\r | |
605 | IN CHAR16 *VariableName,\r | |
606 | IN EFI_GUID *VendorGuid,\r | |
052ad7e1 A |
607 | IN UINT32 Attributes,\r |
608 | IN UINTN DataSize,\r | |
609 | IN VOID *Data\r | |
33a5a666 A |
610 | )\r |
611 | {\r | |
612 | VARIABLE_CACHE_ENTRY *Entry;\r | |
613 | UINTN Index;\r | |
614 | \r | |
615 | if (EfiAtRuntime ()) {\r | |
616 | // Don't use the cache at runtime\r | |
617 | return;\r | |
618 | }\r | |
619 | \r | |
620 | for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {\r | |
621 | if (CompareGuid (VendorGuid, Entry->Guid)) {\r | |
622 | if (StrCmp (VariableName, Entry->Name) == 0) { \r | |
623 | Entry->Attributes = Attributes;\r | |
624 | if (DataSize == 0) {\r | |
625 | // Delete Case\r | |
626 | if (Entry->DataSize != 0) {\r | |
627 | FreePool (Entry->Data);\r | |
628 | }\r | |
629 | Entry->DataSize = DataSize;\r | |
630 | } else if (DataSize == Entry->DataSize) {\r | |
631 | CopyMem (Entry->Data, Data, DataSize);\r | |
632 | } else {\r | |
633 | Entry->Data = AllocatePool (DataSize);\r | |
634 | Entry->DataSize = DataSize;\r | |
635 | CopyMem (Entry->Data, Data, DataSize);\r | |
636 | }\r | |
637 | }\r | |
638 | }\r | |
639 | }\r | |
640 | }\r | |
641 | \r | |
642 | \r | |
052ad7e1 A |
643 | /**\r |
644 | Search the cache to see if the variable is in the cache.\r | |
645 | \r | |
646 | @param[in] VariableName Name of variable\r | |
647 | @param[in] VendorGuid Guid of variable\r | |
648 | @param[in] Attribute Attribue returned \r | |
649 | @param[in] DataSize Size of data returned\r | |
650 | @param[in] Data Variable data returned\r | |
651 | \r | |
652 | @retval EFI_SUCCESS VariableGuid & VariableName data was returned.\r | |
653 | @retval other Not found.\r | |
654 | \r | |
655 | **/\r | |
33a5a666 A |
656 | EFI_STATUS\r |
657 | FindVariableInCache (\r | |
658 | IN CHAR16 *VariableName,\r | |
659 | IN EFI_GUID *VendorGuid,\r | |
660 | OUT UINT32 *Attributes OPTIONAL,\r | |
661 | IN OUT UINTN *DataSize,\r | |
662 | OUT VOID *Data\r | |
663 | )\r | |
664 | {\r | |
665 | VARIABLE_CACHE_ENTRY *Entry;\r | |
666 | UINTN Index;\r | |
667 | \r | |
668 | if (EfiAtRuntime ()) {\r | |
669 | // Don't use the cache at runtime\r | |
670 | return EFI_NOT_FOUND;\r | |
671 | }\r | |
672 | \r | |
673 | for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {\r | |
674 | if (CompareGuid (VendorGuid, Entry->Guid)) {\r | |
675 | if (StrCmp (VariableName, Entry->Name) == 0) {\r | |
676 | if (Entry->DataSize == 0) {\r | |
052ad7e1 | 677 | // Variable was deleted so return not found\r |
33a5a666 A |
678 | return EFI_NOT_FOUND;\r |
679 | } else if (Entry->DataSize != *DataSize) {\r | |
052ad7e1 | 680 | // If the buffer is too small return correct size\r |
33a5a666 A |
681 | *DataSize = Entry->DataSize;\r |
682 | return EFI_BUFFER_TOO_SMALL;\r | |
683 | } else {\r | |
052ad7e1 | 684 | // Return the data\r |
33a5a666 A |
685 | CopyMem (Data, Entry->Data, Entry->DataSize);\r |
686 | if (Attributes != NULL) {\r | |
687 | *Attributes = Entry->Attributes;\r | |
688 | }\r | |
689 | return EFI_SUCCESS;\r | |
690 | }\r | |
691 | }\r | |
692 | }\r | |
693 | }\r | |
694 | \r | |
695 | return EFI_NOT_FOUND;\r | |
696 | }\r | |
697 | \r | |
698 | \r | |
8d3a5c82 | 699 | EFI_STATUS\r |
8d3a5c82 | 700 | FindVariable (\r |
701 | IN CHAR16 *VariableName,\r | |
702 | IN EFI_GUID *VendorGuid,\r | |
703 | OUT VARIABLE_POINTER_TRACK *PtrTrack,\r | |
704 | IN VARIABLE_GLOBAL *Global\r | |
705 | )\r | |
706 | /*++\r | |
707 | \r | |
708 | Routine Description:\r | |
709 | \r | |
710 | This code finds variable in storage blocks (Volatile or Non-Volatile)\r | |
711 | \r | |
712 | Arguments:\r | |
713 | \r | |
714 | VariableName Name of the variable to be found\r | |
715 | VendorGuid Vendor GUID to be found.\r | |
716 | PtrTrack Variable Track Pointer structure that contains\r | |
717 | Variable Information.\r | |
718 | Contains the pointer of Variable header.\r | |
719 | Global VARIABLE_GLOBAL pointer\r | |
720 | \r | |
721 | Returns:\r | |
722 | \r | |
723 | EFI STATUS\r | |
724 | \r | |
725 | --*/\r | |
726 | {\r | |
727 | VARIABLE_HEADER *Variable[2];\r | |
728 | VARIABLE_STORE_HEADER *VariableStoreHeader[2];\r | |
729 | UINTN Index;\r | |
730 | \r | |
731 | //\r | |
732 | // We aquire the lock at the entry of FindVariable as GetVariable, GetNextVariableName\r | |
733 | // SetVariable all call FindVariable at entry point. Please move "Aquire Lock" to\r | |
734 | // the correct places if this assumption does not hold TRUE anymore.\r | |
735 | //\r | |
052ad7e1 | 736 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 737 | \r |
738 | //\r | |
33a5a666 | 739 | // 0: Volatile, 1: Non-Volatile\r |
36873a61 | 740 | // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r |
741 | // make use of this mapping to implement search algorithme.\r | |
8d3a5c82 | 742 | //\r |
052ad7e1 A |
743 | VariableStoreHeader[0] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r |
744 | VariableStoreHeader[1] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r | |
8d3a5c82 | 745 | \r |
746 | //\r | |
747 | // Start Pointers for the variable.\r | |
748 | // Actual Data Pointer where data can be written.\r | |
749 | //\r | |
750 | Variable[0] = (VARIABLE_HEADER *) (VariableStoreHeader[0] + 1);\r | |
751 | Variable[1] = (VARIABLE_HEADER *) (VariableStoreHeader[1] + 1);\r | |
752 | \r | |
753 | if (VariableName[0] != 0 && VendorGuid == NULL) {\r | |
754 | return EFI_INVALID_PARAMETER;\r | |
755 | }\r | |
756 | //\r | |
33a5a666 | 757 | // Find the variable by walk through volatile and then non-volatile variable store\r |
8d3a5c82 | 758 | //\r |
759 | for (Index = 0; Index < 2; Index++) {\r | |
760 | PtrTrack->StartPtr = (VARIABLE_HEADER *) (VariableStoreHeader[Index] + 1);\r | |
761 | PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);\r | |
762 | \r | |
763 | while (IsValidVariableHeader (Variable[Index]) && (Variable[Index] <= GetEndPointer (VariableStoreHeader[Index]))) {\r | |
764 | if (Variable[Index]->State == VAR_ADDED) {\r | |
c6492839 | 765 | if (!EfiAtRuntime () || (Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r |
8d3a5c82 | 766 | if (VariableName[0] == 0) {\r |
767 | PtrTrack->CurrPtr = Variable[Index];\r | |
33a5a666 | 768 | PtrTrack->Volatile = (BOOLEAN)(Index == 0);\r |
8d3a5c82 | 769 | return EFI_SUCCESS;\r |
770 | } else {\r | |
771 | if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {\r | |
772 | if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable[Index]), Variable[Index]->NameSize)) {\r | |
773 | PtrTrack->CurrPtr = Variable[Index];\r | |
33a5a666 | 774 | PtrTrack->Volatile = (BOOLEAN)(Index == 0);\r |
8d3a5c82 | 775 | return EFI_SUCCESS;\r |
776 | }\r | |
777 | }\r | |
778 | }\r | |
779 | }\r | |
780 | }\r | |
781 | \r | |
782 | Variable[Index] = GetNextVariablePtr (Variable[Index]);\r | |
783 | }\r | |
8d3a5c82 | 784 | }\r |
8d3a5c82 | 785 | PtrTrack->CurrPtr = NULL;\r |
786 | return EFI_NOT_FOUND;\r | |
787 | }\r | |
788 | \r | |
33a5a666 | 789 | \r |
052ad7e1 | 790 | \r |
8d3a5c82 | 791 | /*++\r |
792 | \r | |
793 | Routine Description:\r | |
794 | \r | |
795 | This code finds variable in storage blocks (Volatile or Non-Volatile)\r | |
796 | \r | |
797 | Arguments:\r | |
798 | \r | |
c6492839 | 799 | VariableName Name of Variable to be found\r |
800 | VendorGuid Variable vendor GUID\r | |
801 | Attributes OPTIONAL Attribute value of the variable found\r | |
802 | DataSize Size of Data found. If size is less than the\r | |
803 | data, this value contains the required size.\r | |
804 | Data Data pointer\r | |
805 | Global Pointer to VARIABLE_GLOBAL structure\r | |
806 | Instance Instance of the Firmware Volume.\r | |
8d3a5c82 | 807 | \r |
808 | Returns:\r | |
809 | \r | |
c6492839 | 810 | EFI_INVALID_PARAMETER - Invalid parameter\r |
811 | EFI_SUCCESS - Find the specified variable\r | |
812 | EFI_NOT_FOUND - Not found\r | |
813 | EFI_BUFFER_TO_SMALL - DataSize is too small for the result\r | |
814 | \r | |
8d3a5c82 | 815 | \r |
816 | --*/\r | |
052ad7e1 A |
817 | EFI_STATUS\r |
818 | EFIAPI\r | |
819 | RuntimeServiceGetVariable (\r | |
820 | IN CHAR16 *VariableName,\r | |
821 | IN EFI_GUID *VendorGuid,\r | |
822 | OUT UINT32 *Attributes OPTIONAL,\r | |
823 | IN OUT UINTN *DataSize,\r | |
824 | OUT VOID *Data\r | |
825 | )\r | |
8d3a5c82 | 826 | {\r |
052ad7e1 | 827 | EFI_STATUS Status;\r |
8d3a5c82 | 828 | VARIABLE_POINTER_TRACK Variable;\r |
829 | UINTN VarDataSize;\r | |
8d3a5c82 | 830 | \r |
831 | if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r | |
832 | return EFI_INVALID_PARAMETER;\r | |
833 | }\r | |
33a5a666 | 834 | \r |
8d3a5c82 | 835 | //\r |
836 | // Find existing variable\r | |
837 | //\r | |
33a5a666 A |
838 | Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r |
839 | if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){\r | |
840 | // Hit in the Cache\r | |
841 | UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);\r | |
842 | return Status;\r | |
843 | }\r | |
844 | \r | |
052ad7e1 | 845 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r |
8d3a5c82 | 846 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r |
847 | goto Done;\r | |
848 | }\r | |
33a5a666 | 849 | \r |
8d3a5c82 | 850 | //\r |
851 | // Get data size\r | |
852 | //\r | |
853 | VarDataSize = Variable.CurrPtr->DataSize;\r | |
854 | if (*DataSize >= VarDataSize) {\r | |
855 | if (Data == NULL) {\r | |
856 | Status = EFI_INVALID_PARAMETER;\r | |
857 | goto Done;\r | |
858 | }\r | |
859 | \r | |
860 | CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r | |
861 | if (Attributes != NULL) {\r | |
862 | *Attributes = Variable.CurrPtr->Attributes;\r | |
863 | }\r | |
864 | \r | |
865 | *DataSize = VarDataSize;\r | |
33a5a666 A |
866 | UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r |
867 | UpdateVariableCache (VariableName, VendorGuid, Variable.CurrPtr->Attributes, VarDataSize, Data);\r | |
868 | \r | |
8d3a5c82 | 869 | Status = EFI_SUCCESS;\r |
870 | goto Done;\r | |
871 | } else {\r | |
872 | *DataSize = VarDataSize;\r | |
873 | Status = EFI_BUFFER_TOO_SMALL;\r | |
874 | goto Done;\r | |
875 | }\r | |
876 | \r | |
877 | Done:\r | |
052ad7e1 | 878 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 879 | return Status;\r |
880 | }\r | |
881 | \r | |
052ad7e1 A |
882 | \r |
883 | \r | |
8d3a5c82 | 884 | /*++\r |
885 | \r | |
886 | Routine Description:\r | |
887 | \r | |
888 | This code Finds the Next available variable\r | |
889 | \r | |
890 | Arguments:\r | |
891 | \r | |
892 | VariableNameSize Size of the variable\r | |
893 | VariableName Pointer to variable name\r | |
894 | VendorGuid Variable Vendor Guid\r | |
895 | Global VARIABLE_GLOBAL structure pointer.\r | |
896 | Instance FV instance\r | |
897 | \r | |
898 | Returns:\r | |
899 | \r | |
900 | EFI STATUS\r | |
901 | \r | |
902 | --*/\r | |
052ad7e1 A |
903 | EFI_STATUS\r |
904 | EFIAPI\r | |
905 | RuntimeServiceGetNextVariableName (\r | |
906 | IN OUT UINTN *VariableNameSize,\r | |
907 | IN OUT CHAR16 *VariableName,\r | |
908 | IN OUT EFI_GUID *VendorGuid\r | |
909 | )\r | |
8d3a5c82 | 910 | {\r |
911 | VARIABLE_POINTER_TRACK Variable;\r | |
912 | UINTN VarNameSize;\r | |
913 | EFI_STATUS Status;\r | |
914 | \r | |
915 | if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r | |
916 | return EFI_INVALID_PARAMETER;\r | |
917 | }\r | |
918 | \r | |
052ad7e1 | 919 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r |
8d3a5c82 | 920 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r |
921 | goto Done;\r | |
922 | }\r | |
923 | \r | |
924 | if (VariableName[0] != 0) {\r | |
925 | //\r | |
926 | // If variable name is not NULL, get next variable\r | |
927 | //\r | |
928 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
929 | }\r | |
930 | \r | |
931 | while (TRUE) {\r | |
932 | //\r | |
933 | // If both volatile and non-volatile variable store are parsed,\r | |
934 | // return not found\r | |
935 | //\r | |
936 | if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r | |
937 | Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));\r | |
36873a61 | 938 | if (!Variable.Volatile) {\r |
939 | Variable.StartPtr = (VARIABLE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase + sizeof (VARIABLE_STORE_HEADER)));\r | |
940 | Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase));\r | |
8d3a5c82 | 941 | } else {\r |
942 | Status = EFI_NOT_FOUND;\r | |
943 | goto Done;\r | |
944 | }\r | |
945 | \r | |
946 | Variable.CurrPtr = Variable.StartPtr;\r | |
947 | if (!IsValidVariableHeader (Variable.CurrPtr)) {\r | |
948 | continue;\r | |
949 | }\r | |
950 | }\r | |
951 | //\r | |
952 | // Variable is found\r | |
953 | //\r | |
954 | if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {\r | |
955 | if (!(EfiAtRuntime () && !(Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {\r | |
956 | VarNameSize = Variable.CurrPtr->NameSize;\r | |
957 | if (VarNameSize <= *VariableNameSize) {\r | |
958 | CopyMem (\r | |
959 | VariableName,\r | |
960 | GET_VARIABLE_NAME_PTR (Variable.CurrPtr),\r | |
961 | VarNameSize\r | |
962 | );\r | |
963 | CopyMem (\r | |
964 | VendorGuid,\r | |
965 | &Variable.CurrPtr->VendorGuid,\r | |
966 | sizeof (EFI_GUID)\r | |
967 | );\r | |
968 | Status = EFI_SUCCESS;\r | |
969 | } else {\r | |
970 | Status = EFI_BUFFER_TOO_SMALL;\r | |
971 | }\r | |
972 | \r | |
973 | *VariableNameSize = VarNameSize;\r | |
974 | goto Done;\r | |
975 | }\r | |
976 | }\r | |
977 | \r | |
978 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
979 | }\r | |
980 | \r | |
981 | Done:\r | |
052ad7e1 | 982 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 983 | return Status;\r |
984 | }\r | |
985 | \r | |
052ad7e1 | 986 | \r |
8d3a5c82 | 987 | /*++\r |
988 | \r | |
989 | Routine Description:\r | |
990 | \r | |
991 | This code sets variable in storage blocks (Volatile or Non-Volatile)\r | |
992 | \r | |
993 | Arguments:\r | |
994 | \r | |
995 | VariableName Name of Variable to be found\r | |
996 | VendorGuid Variable vendor GUID\r | |
997 | Attributes Attribute value of the variable found\r | |
998 | DataSize Size of Data found. If size is less than the\r | |
999 | data, this value contains the required size.\r | |
1000 | Data Data pointer\r | |
1001 | Global Pointer to VARIABLE_GLOBAL structure\r | |
1002 | VolatileOffset The offset of last volatile variable\r | |
1003 | NonVolatileOffset The offset of last non-volatile variable\r | |
1004 | Instance Instance of the Firmware Volume.\r | |
1005 | \r | |
1006 | Returns:\r | |
1007 | \r | |
8d3a5c82 | 1008 | EFI_INVALID_PARAMETER - Invalid parameter\r |
1009 | EFI_SUCCESS - Set successfully\r | |
1010 | EFI_OUT_OF_RESOURCES - Resource not enough to set variable\r | |
1011 | EFI_NOT_FOUND - Not found\r | |
c6492839 | 1012 | EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure\r |
1013 | EFI_WRITE_PROTECTED - Variable is read-only\r | |
8d3a5c82 | 1014 | \r |
1015 | --*/\r | |
052ad7e1 A |
1016 | EFI_STATUS\r |
1017 | EFIAPI\r | |
1018 | RuntimeServiceSetVariable (\r | |
1019 | IN CHAR16 *VariableName,\r | |
1020 | IN EFI_GUID *VendorGuid,\r | |
1021 | IN UINT32 Attributes,\r | |
1022 | IN UINTN DataSize,\r | |
1023 | IN VOID *Data\r | |
1024 | )\r | |
8d3a5c82 | 1025 | {\r |
1026 | VARIABLE_POINTER_TRACK Variable;\r | |
1027 | EFI_STATUS Status;\r | |
1028 | VARIABLE_HEADER *NextVariable;\r | |
1029 | UINTN VarNameSize;\r | |
1030 | UINTN VarNameOffset;\r | |
1031 | UINTN VarDataOffset;\r | |
1032 | UINTN VarSize;\r | |
1033 | UINT8 State;\r | |
1034 | BOOLEAN Reclaimed;\r | |
052ad7e1 A |
1035 | UINTN *VolatileOffset;\r |
1036 | UINTN *NonVolatileOffset;\r | |
1037 | UINT32 Instance;\r | |
fd51bf70 | 1038 | BOOLEAN Volatile;\r |
8d3a5c82 | 1039 | \r |
052ad7e1 A |
1040 | Reclaimed = FALSE;\r |
1041 | VolatileOffset = &mVariableModuleGlobal->VolatileLastVariableOffset;\r | |
1042 | NonVolatileOffset = &mVariableModuleGlobal->NonVolatileLastVariableOffset;\r | |
1043 | Instance = mVariableModuleGlobal->FvbInstance;\r | |
8d3a5c82 | 1044 | \r |
c6492839 | 1045 | //\r |
1046 | // Check input parameters\r | |
1047 | //\r | |
8d3a5c82 | 1048 | if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r |
1049 | return EFI_INVALID_PARAMETER;\r | |
c6492839 | 1050 | } \r |
1051 | //\r | |
1052 | // Make sure if runtime bit is set, boot service bit is set also\r | |
1053 | //\r | |
1054 | if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r | |
1055 | return EFI_INVALID_PARAMETER;\r | |
8d3a5c82 | 1056 | }\r |
c6492839 | 1057 | //\r |
1058 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
1059 | // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)\r | |
1060 | // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.\r | |
1061 | //\r | |
1062 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
1063 | if ((DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE) || \r | |
1064 | (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE)) {\r | |
1065 | return EFI_INVALID_PARAMETER;\r | |
1066 | } \r | |
1067 | } else {\r | |
1068 | //\r | |
1069 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
1070 | // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.\r | |
1071 | //\r | |
1072 | if ((DataSize > MAX_VARIABLE_SIZE) ||\r | |
1073 | (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE)) {\r | |
1074 | return EFI_INVALID_PARAMETER;\r | |
1075 | } \r | |
1076 | } \r | |
1077 | //\r | |
1078 | // Check whether the input variable is already existed\r | |
1079 | //\r | |
1080 | \r | |
052ad7e1 | 1081 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r |
c6492839 | 1082 | if (Status == EFI_SUCCESS && Variable.CurrPtr != NULL) {\r |
8d3a5c82 | 1083 | //\r |
c6492839 | 1084 | // Update/Delete existing variable\r |
8d3a5c82 | 1085 | //\r |
fd51bf70 | 1086 | Volatile = Variable.Volatile;\r |
c6492839 | 1087 | \r |
1088 | if (EfiAtRuntime ()) { \r | |
1089 | //\r | |
1090 | // If EfiAtRuntime and the variable is Volatile and Runtime Access, \r | |
1091 | // the volatile is ReadOnly, and SetVariable should be aborted and \r | |
1092 | // return EFI_WRITE_PROTECTED.\r | |
1093 | //\r | |
1094 | if (Variable.Volatile) {\r | |
1095 | Status = EFI_WRITE_PROTECTED;\r | |
1096 | goto Done;\r | |
1097 | }\r | |
1098 | //\r | |
1099 | // Only variable have NV attribute can be updated/deleted in Runtime\r | |
1100 | //\r | |
1101 | if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r | |
1102 | Status = EFI_INVALID_PARAMETER;\r | |
1103 | goto Done; \r | |
1104 | }\r | |
1105 | }\r | |
8d3a5c82 | 1106 | //\r |
c6492839 | 1107 | // Setting a data variable with no access, or zero DataSize attributes\r |
1108 | // specified causes it to be deleted.\r | |
8d3a5c82 | 1109 | //\r |
c6492839 | 1110 | if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { \r |
1111 | State = Variable.CurrPtr->State;\r | |
1112 | State &= VAR_DELETED;\r | |
1113 | \r | |
1114 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1115 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1116 | Variable.Volatile,\r |
1117 | FALSE,\r | |
1118 | Instance,\r | |
1119 | (UINTN) &Variable.CurrPtr->State,\r | |
1120 | sizeof (UINT8),\r | |
1121 | &State\r | |
1122 | ); \r | |
33a5a666 | 1123 | if (!EFI_ERROR (Status)) {\r |
fd51bf70 | 1124 | UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, FALSE, TRUE, FALSE);\r |
33a5a666 A |
1125 | UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r |
1126 | }\r | |
c6492839 | 1127 | goto Done; \r |
1128 | }\r | |
8d3a5c82 | 1129 | //\r |
c6492839 | 1130 | // If the variable is marked valid and the same data has been passed in\r |
1131 | // then return to the caller immediately.\r | |
8d3a5c82 | 1132 | //\r |
c6492839 | 1133 | if (Variable.CurrPtr->DataSize == DataSize &&\r |
1134 | (CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0)) {\r | |
33a5a666 | 1135 | \r |
fd51bf70 | 1136 | UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r |
c6492839 | 1137 | Status = EFI_SUCCESS;\r |
1138 | goto Done;\r | |
1139 | } else if ((Variable.CurrPtr->State == VAR_ADDED) ||\r | |
1140 | (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r | |
1141 | //\r | |
1142 | // Mark the old variable as in delete transition\r | |
1143 | //\r | |
1144 | State = Variable.CurrPtr->State;\r | |
1145 | State &= VAR_IN_DELETED_TRANSITION;\r | |
1146 | \r | |
1147 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1148 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1149 | Variable.Volatile,\r |
1150 | FALSE,\r | |
1151 | Instance,\r | |
1152 | (UINTN) &Variable.CurrPtr->State,\r | |
1153 | sizeof (UINT8),\r | |
1154 | &State\r | |
1155 | ); \r | |
1156 | if (EFI_ERROR (Status)) {\r | |
1157 | goto Done; \r | |
33a5a666 | 1158 | } \r |
c6492839 | 1159 | } \r |
1160 | } else if (Status == EFI_NOT_FOUND) {\r | |
8d3a5c82 | 1161 | //\r |
c6492839 | 1162 | // Create a new variable\r |
1163 | // \r | |
1164 | \r | |
8d3a5c82 | 1165 | //\r |
c6492839 | 1166 | // Make sure we are trying to create a new variable.\r |
1167 | // Setting a data variable with no access, or zero DataSize attributes means to delete it. \r | |
8d3a5c82 | 1168 | //\r |
c6492839 | 1169 | if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r |
1170 | Status = EFI_NOT_FOUND;\r | |
1171 | goto Done;\r | |
1172 | }\r | |
1173 | \r | |
8d3a5c82 | 1174 | //\r |
c6492839 | 1175 | // Only variable have NV|RT attribute can be created in Runtime\r |
1176 | //\r | |
1177 | if (EfiAtRuntime () &&\r | |
1178 | (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {\r | |
1179 | Status = EFI_INVALID_PARAMETER;\r | |
1180 | goto Done;\r | |
1181 | } \r | |
1182 | } else {\r | |
8d3a5c82 | 1183 | //\r |
c6492839 | 1184 | // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().\r |
8d3a5c82 | 1185 | //\r |
c6492839 | 1186 | ASSERT (Status == EFI_INVALID_PARAMETER);\r |
8d3a5c82 | 1187 | goto Done;\r |
c6492839 | 1188 | }\r |
1189 | \r | |
1190 | //\r | |
1191 | // Function part - create a new variable and copy the data.\r | |
1192 | // Both update a variable and create a variable will come here.\r | |
1193 | //\r | |
1194 | // Tricky part: Use scratch data area at the end of volatile variable store\r | |
1195 | // as a temporary storage.\r | |
1196 | //\r | |
052ad7e1 | 1197 | NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r |
c6492839 | 1198 | \r |
1199 | SetMem (NextVariable, SCRATCH_SIZE, 0xff);\r | |
1200 | \r | |
1201 | NextVariable->StartId = VARIABLE_DATA;\r | |
1202 | NextVariable->Attributes = Attributes;\r | |
1203 | //\r | |
1204 | // NextVariable->State = VAR_ADDED;\r | |
1205 | //\r | |
1206 | NextVariable->Reserved = 0;\r | |
1207 | VarNameOffset = sizeof (VARIABLE_HEADER);\r | |
1208 | VarNameSize = StrSize (VariableName);\r | |
1209 | CopyMem (\r | |
1210 | (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r | |
1211 | VariableName,\r | |
1212 | VarNameSize\r | |
1213 | );\r | |
1214 | VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r | |
1215 | CopyMem (\r | |
1216 | (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r | |
1217 | Data,\r | |
1218 | DataSize\r | |
1219 | );\r | |
1220 | CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r | |
1221 | //\r | |
1222 | // There will be pad bytes after Data, the NextVariable->NameSize and\r | |
1223 | // NextVariable->DataSize should not include pad size so that variable\r | |
1224 | // service can get actual size in GetVariable\r | |
1225 | //\r | |
1226 | NextVariable->NameSize = (UINT32)VarNameSize;\r | |
1227 | NextVariable->DataSize = (UINT32)DataSize;\r | |
1228 | \r | |
1229 | //\r | |
1230 | // The actual size of the variable that stores in storage should\r | |
1231 | // include pad size.\r | |
1232 | //\r | |
1233 | VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r | |
1234 | if (Attributes & EFI_VARIABLE_NON_VOLATILE) {\r | |
8d3a5c82 | 1235 | //\r |
c6492839 | 1236 | // Create a nonvolatile variable\r |
8d3a5c82 | 1237 | //\r |
fd51bf70 | 1238 | Volatile = FALSE;\r |
c6492839 | 1239 | \r |
1240 | if ((UINT32) (VarSize +*NonVolatileOffset) >\r | |
052ad7e1 | 1241 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size\r |
c6492839 | 1242 | ) {\r |
1243 | if (EfiAtRuntime ()) {\r | |
1244 | Status = EFI_OUT_OF_RESOURCES;\r | |
1245 | goto Done;\r | |
1246 | }\r | |
1247 | //\r | |
1248 | // Perform garbage collection & reclaim operation\r | |
1249 | //\r | |
052ad7e1 | 1250 | Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, NonVolatileOffset, FALSE);\r |
8d3a5c82 | 1251 | if (EFI_ERROR (Status)) {\r |
1252 | goto Done;\r | |
1253 | }\r | |
8d3a5c82 | 1254 | //\r |
c6492839 | 1255 | // If still no enough space, return out of resources\r |
8d3a5c82 | 1256 | //\r |
c6492839 | 1257 | if ((UINT32) (VarSize +*NonVolatileOffset) >\r |
052ad7e1 | 1258 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size\r |
8d3a5c82 | 1259 | ) {\r |
c6492839 | 1260 | Status = EFI_OUT_OF_RESOURCES;\r |
8d3a5c82 | 1261 | goto Done;\r |
8d3a5c82 | 1262 | }\r |
c6492839 | 1263 | \r |
1264 | Reclaimed = TRUE;\r | |
8d3a5c82 | 1265 | }\r |
1266 | //\r | |
c6492839 | 1267 | // Three steps\r |
1268 | // 1. Write variable header\r | |
1269 | // 2. Write variable data\r | |
1270 | // 3. Set variable state to valid\r | |
8d3a5c82 | 1271 | //\r |
8d3a5c82 | 1272 | //\r |
c6492839 | 1273 | // Step 1:\r |
8d3a5c82 | 1274 | //\r |
c6492839 | 1275 | Status = UpdateVariableStore (\r |
052ad7e1 | 1276 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1277 | FALSE,\r |
1278 | TRUE,\r | |
1279 | Instance,\r | |
1280 | *NonVolatileOffset,\r | |
1281 | sizeof (VARIABLE_HEADER),\r | |
1282 | (UINT8 *) NextVariable\r | |
1283 | );\r | |
1284 | \r | |
1285 | if (EFI_ERROR (Status)) {\r | |
1286 | goto Done;\r | |
1287 | }\r | |
8d3a5c82 | 1288 | //\r |
c6492839 | 1289 | // Step 2:\r |
8d3a5c82 | 1290 | //\r |
c6492839 | 1291 | Status = UpdateVariableStore (\r |
052ad7e1 | 1292 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1293 | FALSE,\r |
1294 | TRUE,\r | |
1295 | Instance,\r | |
1296 | *NonVolatileOffset + sizeof (VARIABLE_HEADER),\r | |
1297 | (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r | |
1298 | (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r | |
1299 | );\r | |
1300 | \r | |
1301 | if (EFI_ERROR (Status)) {\r | |
1302 | goto Done;\r | |
1303 | }\r | |
8d3a5c82 | 1304 | //\r |
c6492839 | 1305 | // Step 3:\r |
8d3a5c82 | 1306 | //\r |
c6492839 | 1307 | NextVariable->State = VAR_ADDED;\r |
1308 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1309 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1310 | FALSE,\r |
1311 | TRUE,\r | |
1312 | Instance,\r | |
1313 | *NonVolatileOffset,\r | |
1314 | sizeof (VARIABLE_HEADER),\r | |
1315 | (UINT8 *) NextVariable\r | |
1316 | );\r | |
1317 | \r | |
1318 | if (EFI_ERROR (Status)) {\r | |
1319 | goto Done;\r | |
1320 | }\r | |
8d3a5c82 | 1321 | \r |
c6492839 | 1322 | *NonVolatileOffset = *NonVolatileOffset + VarSize;\r |
8d3a5c82 | 1323 | \r |
c6492839 | 1324 | } else {\r |
1325 | //\r | |
1326 | // Create a volatile variable\r | |
1327 | // \r | |
fd51bf70 | 1328 | Volatile = TRUE;\r |
c6492839 | 1329 | \r |
1330 | if ((UINT32) (VarSize +*VolatileOffset) >\r | |
052ad7e1 | 1331 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r |
8d3a5c82 | 1332 | //\r |
c6492839 | 1333 | // Perform garbage collection & reclaim operation\r |
8d3a5c82 | 1334 | //\r |
052ad7e1 | 1335 | Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, VolatileOffset, TRUE);\r |
8d3a5c82 | 1336 | if (EFI_ERROR (Status)) {\r |
1337 | goto Done;\r | |
1338 | }\r | |
1339 | //\r | |
c6492839 | 1340 | // If still no enough space, return out of resources\r |
8d3a5c82 | 1341 | //\r |
8d3a5c82 | 1342 | if ((UINT32) (VarSize +*VolatileOffset) >\r |
052ad7e1 | 1343 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size\r |
8d3a5c82 | 1344 | ) {\r |
c6492839 | 1345 | Status = EFI_OUT_OF_RESOURCES;\r |
8d3a5c82 | 1346 | goto Done;\r |
1347 | }\r | |
c6492839 | 1348 | \r |
1349 | Reclaimed = TRUE;\r | |
8d3a5c82 | 1350 | }\r |
8d3a5c82 | 1351 | \r |
c6492839 | 1352 | NextVariable->State = VAR_ADDED;\r |
1353 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1354 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1355 | TRUE,\r |
1356 | TRUE,\r | |
1357 | Instance,\r | |
1358 | *VolatileOffset,\r | |
1359 | (UINT32) VarSize,\r | |
1360 | (UINT8 *) NextVariable\r | |
1361 | );\r | |
1362 | \r | |
1363 | if (EFI_ERROR (Status)) {\r | |
1364 | goto Done;\r | |
8d3a5c82 | 1365 | }\r |
c6492839 | 1366 | \r |
1367 | *VolatileOffset = *VolatileOffset + VarSize;\r | |
1368 | }\r | |
1369 | //\r | |
1370 | // Mark the old variable as deleted\r | |
1371 | //\r | |
1372 | if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {\r | |
1373 | State = Variable.CurrPtr->State;\r | |
1374 | State &= VAR_DELETED;\r | |
1375 | \r | |
1376 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1377 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1378 | Variable.Volatile,\r |
1379 | FALSE,\r | |
1380 | Instance,\r | |
1381 | (UINTN) &Variable.CurrPtr->State,\r | |
1382 | sizeof (UINT8),\r | |
1383 | &State\r | |
1384 | );\r | |
33a5a666 A |
1385 | \r |
1386 | if (!EFI_ERROR (Status)) {\r | |
fd51bf70 | 1387 | UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r |
33a5a666 A |
1388 | UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r |
1389 | }\r | |
c6492839 | 1390 | goto Done; \r |
8d3a5c82 | 1391 | }\r |
1392 | \r | |
1393 | Status = EFI_SUCCESS;\r | |
fd51bf70 | 1394 | UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r |
33a5a666 A |
1395 | UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r |
1396 | \r | |
8d3a5c82 | 1397 | Done:\r |
052ad7e1 | 1398 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 1399 | return Status;\r |
1400 | }\r | |
1401 | \r | |
052ad7e1 | 1402 | \r |
8d3a5c82 | 1403 | /*++\r |
1404 | \r | |
1405 | Routine Description:\r | |
1406 | \r | |
1407 | This code returns information about the EFI variables.\r | |
1408 | \r | |
1409 | Arguments:\r | |
1410 | \r | |
1411 | Attributes Attributes bitmask to specify the type of variables\r | |
1412 | on which to return information.\r | |
1413 | MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r | |
1414 | for the EFI variables associated with the attributes specified.\r | |
1415 | RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r | |
c6492839 | 1416 | for EFI variables associated with the attributes specified.\r |
1417 | MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r | |
8d3a5c82 | 1418 | associated with the attributes specified.\r |
1419 | Global Pointer to VARIABLE_GLOBAL structure.\r | |
1420 | Instance Instance of the Firmware Volume.\r | |
1421 | \r | |
1422 | Returns:\r | |
1423 | \r | |
1424 | EFI STATUS\r | |
1425 | EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.\r | |
1426 | EFI_SUCCESS - Query successfully.\r | |
1427 | EFI_UNSUPPORTED - The attribute is not supported on this platform.\r | |
1428 | \r | |
1429 | --*/\r | |
052ad7e1 A |
1430 | EFI_STATUS\r |
1431 | EFIAPI\r | |
1432 | RuntimeServiceQueryVariableInfo (\r | |
1433 | IN UINT32 Attributes,\r | |
1434 | OUT UINT64 *MaximumVariableStorageSize,\r | |
1435 | OUT UINT64 *RemainingVariableStorageSize,\r | |
1436 | OUT UINT64 *MaximumVariableSize\r | |
1437 | )\r | |
8d3a5c82 | 1438 | {\r |
1439 | VARIABLE_HEADER *Variable;\r | |
1440 | VARIABLE_HEADER *NextVariable;\r | |
1441 | UINT64 VariableSize;\r | |
1442 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
1443 | \r | |
c6492839 | 1444 | if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r |
8d3a5c82 | 1445 | return EFI_INVALID_PARAMETER;\r |
1446 | }\r | |
c6492839 | 1447 | \r |
1448 | if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r | |
8d3a5c82 | 1449 | //\r |
1450 | // Make sure the Attributes combination is supported by the platform.\r | |
1451 | //\r | |
c6492839 | 1452 | return EFI_UNSUPPORTED; \r |
8d3a5c82 | 1453 | } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r |
1454 | //\r | |
1455 | // Make sure if runtime bit is set, boot service bit is set also.\r | |
1456 | //\r | |
1457 | return EFI_INVALID_PARAMETER;\r | |
1458 | } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r | |
1459 | //\r | |
1460 | // Make sure RT Attribute is set if we are in Runtime phase.\r | |
1461 | //\r | |
1462 | return EFI_INVALID_PARAMETER;\r | |
1463 | }\r | |
1464 | \r | |
052ad7e1 | 1465 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 1466 | \r |
1467 | if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r | |
1468 | //\r | |
1469 | // Query is Volatile related.\r | |
1470 | //\r | |
052ad7e1 | 1471 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r |
8d3a5c82 | 1472 | } else {\r |
1473 | //\r | |
1474 | // Query is Non-Volatile related.\r | |
1475 | //\r | |
052ad7e1 | 1476 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r |
8d3a5c82 | 1477 | }\r |
1478 | \r | |
1479 | //\r | |
1480 | // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r | |
1481 | // with the storage size (excluding the storage header size).\r | |
1482 | //\r | |
1483 | *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r | |
1484 | *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r | |
1485 | \r | |
1486 | //\r | |
c6492839 | 1487 | // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.\r |
8d3a5c82 | 1488 | //\r |
c6492839 | 1489 | *MaximumVariableSize = MAX_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r |
1490 | \r | |
1491 | //\r | |
1492 | // Harware error record variable needs larger size.\r | |
1493 | //\r | |
1494 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
1495 | *MaximumVariableSize = MAX_HARDWARE_ERROR_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r | |
1496 | }\r | |
8d3a5c82 | 1497 | \r |
1498 | //\r | |
1499 | // Point to the starting address of the variables.\r | |
1500 | //\r | |
1501 | Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r | |
1502 | \r | |
1503 | //\r | |
1504 | // Now walk through the related variable store.\r | |
1505 | //\r | |
1506 | while (IsValidVariableHeader (Variable) && (Variable < GetEndPointer (VariableStoreHeader))) {\r | |
1507 | NextVariable = GetNextVariablePtr (Variable);\r | |
1508 | VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r | |
1509 | \r | |
1510 | if (EfiAtRuntime ()) {\r | |
1511 | //\r | |
1512 | // we don't take the state of the variables in mind\r | |
1513 | // when calculating RemainingVariableStorageSize,\r | |
1514 | // since the space occupied by variables not marked with\r | |
1515 | // VAR_ADDED is not allowed to be reclaimed in Runtime.\r | |
1516 | //\r | |
1517 | *RemainingVariableStorageSize -= VariableSize;\r | |
1518 | } else {\r | |
1519 | //\r | |
1520 | // Only care about Variables with State VAR_ADDED,because\r | |
1521 | // the space not marked as VAR_ADDED is reclaimable now.\r | |
1522 | //\r | |
1523 | if (Variable->State == VAR_ADDED) {\r | |
1524 | *RemainingVariableStorageSize -= VariableSize;\r | |
1525 | }\r | |
1526 | }\r | |
1527 | \r | |
1528 | //\r | |
1529 | // Go to the next one\r | |
1530 | //\r | |
1531 | Variable = NextVariable;\r | |
1532 | }\r | |
1533 | \r | |
c6492839 | 1534 | if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r |
1535 | *MaximumVariableSize = 0;\r | |
1536 | } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r | |
1537 | *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r | |
1538 | }\r | |
1539 | \r | |
052ad7e1 | 1540 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 1541 | return EFI_SUCCESS;\r |
1542 | }\r | |
1543 | \r | |
1544 | EFI_STATUS\r | |
8d3a5c82 | 1545 | VariableCommonInitialize (\r |
1546 | IN EFI_HANDLE ImageHandle,\r | |
1547 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1548 | )\r | |
1549 | /*++\r | |
1550 | \r | |
1551 | Routine Description:\r | |
1552 | This function does common initialization for variable services\r | |
1553 | \r | |
1554 | Arguments:\r | |
1555 | \r | |
1556 | ImageHandle - The firmware allocated handle for the EFI image.\r | |
1557 | SystemTable - A pointer to the EFI System Table.\r | |
1558 | \r | |
1559 | Returns:\r | |
1560 | \r | |
1561 | Status code.\r | |
1562 | \r | |
1563 | EFI_NOT_FOUND - Variable store area not found.\r | |
1564 | EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.\r | |
1565 | EFI_SUCCESS - Variable services successfully initialized.\r | |
1566 | \r | |
1567 | --*/\r | |
1568 | {\r | |
1569 | EFI_STATUS Status;\r | |
1570 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
1571 | CHAR8 *CurrPtr;\r | |
1572 | VARIABLE_STORE_HEADER *VolatileVariableStore;\r | |
1573 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
1574 | VARIABLE_HEADER *NextVariable;\r | |
1575 | UINT32 Instance;\r | |
1576 | EFI_PHYSICAL_ADDRESS FvVolHdr;\r | |
8d3a5c82 | 1577 | UINT64 TempVariableStoreHeader;\r |
8d3a5c82 | 1578 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r |
8d3a5c82 | 1579 | UINT64 BaseAddress;\r |
1580 | UINT64 Length;\r | |
1581 | UINTN Index;\r | |
1582 | UINT8 Data;\r | |
052ad7e1 A |
1583 | UINT64 VariableStoreBase;\r |
1584 | UINT64 VariableStoreLength;\r | |
8d3a5c82 | 1585 | \r |
8d3a5c82 | 1586 | \r |
052ad7e1 | 1587 | EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r |
8d3a5c82 | 1588 | \r |
1589 | //\r | |
1590 | // Allocate memory for volatile variable store\r | |
1591 | //\r | |
1592 | VolatileVariableStore = AllocateRuntimePool (VARIABLE_STORE_SIZE + SCRATCH_SIZE);\r | |
1593 | if (VolatileVariableStore == NULL) {\r | |
1594 | FreePool (mVariableModuleGlobal);\r | |
1595 | return EFI_OUT_OF_RESOURCES;\r | |
1596 | }\r | |
1597 | \r | |
1598 | SetMem (VolatileVariableStore, VARIABLE_STORE_SIZE + SCRATCH_SIZE, 0xff);\r | |
1599 | \r | |
1600 | //\r | |
1601 | // Variable Specific Data\r | |
1602 | //\r | |
052ad7e1 | 1603 | mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r |
8d3a5c82 | 1604 | mVariableModuleGlobal->VolatileLastVariableOffset = sizeof (VARIABLE_STORE_HEADER);\r |
1605 | \r | |
1606 | VolatileVariableStore->Signature = VARIABLE_STORE_SIGNATURE;\r | |
1607 | VolatileVariableStore->Size = VARIABLE_STORE_SIZE;\r | |
1608 | VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r | |
1609 | VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r | |
1610 | VolatileVariableStore->Reserved = 0;\r | |
1611 | VolatileVariableStore->Reserved1 = 0;\r | |
1612 | \r | |
1613 | //\r | |
1614 | // Get non volatile varaible store\r | |
1615 | //\r | |
1616 | \r | |
1617 | TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);\r | |
052ad7e1 | 1618 | VariableStoreBase = TempVariableStoreHeader + \\r |
8d3a5c82 | 1619 | (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r |
052ad7e1 | 1620 | VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r |
8d3a5c82 | 1621 | (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r |
1622 | //\r | |
1623 | // Mark the variable storage region of the FLASH as RUNTIME\r | |
1624 | //\r | |
052ad7e1 A |
1625 | BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r |
1626 | Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r | |
8d3a5c82 | 1627 | Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r |
1628 | \r | |
1629 | Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r | |
1630 | if (EFI_ERROR (Status)) {\r | |
1631 | FreePool (mVariableModuleGlobal);\r | |
1632 | FreePool (VolatileVariableStore);\r | |
1633 | return EFI_UNSUPPORTED;\r | |
1634 | }\r | |
1635 | \r | |
1636 | Status = gDS->SetMemorySpaceAttributes (\r | |
1637 | BaseAddress,\r | |
1638 | Length,\r | |
1639 | GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r | |
1640 | );\r | |
1641 | if (EFI_ERROR (Status)) {\r | |
1642 | FreePool (mVariableModuleGlobal);\r | |
1643 | FreePool (VolatileVariableStore);\r | |
1644 | return EFI_UNSUPPORTED;\r | |
1645 | }\r | |
1646 | //\r | |
1647 | // Get address of non volatile variable store base\r | |
1648 | //\r | |
052ad7e1 | 1649 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r |
8d3a5c82 | 1650 | \r |
1651 | //\r | |
1652 | // Check Integrity\r | |
1653 | //\r | |
1654 | //\r | |
1655 | // Find the Correct Instance of the FV Block Service.\r | |
1656 | //\r | |
1657 | Instance = 0;\r | |
052ad7e1 | 1658 | CurrPtr = (CHAR8 *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r |
8d3a5c82 | 1659 | while (EfiFvbGetPhysicalAddress (Instance, &FvVolHdr) == EFI_SUCCESS) {\r |
1660 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r | |
1661 | if (CurrPtr >= (CHAR8 *) FwVolHeader && CurrPtr < (((CHAR8 *) FwVolHeader) + FwVolHeader->FvLength)) {\r | |
1662 | mVariableModuleGlobal->FvbInstance = Instance;\r | |
1663 | break;\r | |
1664 | }\r | |
1665 | \r | |
1666 | Instance++;\r | |
1667 | }\r | |
1668 | \r | |
1669 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;\r | |
1670 | if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r | |
1671 | if (~VariableStoreHeader->Size == 0) {\r | |
1672 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1673 | &mVariableModuleGlobal->VariableGlobal,\r |
8d3a5c82 | 1674 | FALSE,\r |
1675 | FALSE,\r | |
1676 | mVariableModuleGlobal->FvbInstance,\r | |
1677 | (UINTN) &VariableStoreHeader->Size,\r | |
1678 | sizeof (UINT32),\r | |
052ad7e1 | 1679 | (UINT8 *) &VariableStoreLength\r |
8d3a5c82 | 1680 | );\r |
1681 | //\r | |
1682 | // As Variables are stored in NV storage, which are slow devices,such as flash.\r | |
1683 | // Variable operation may skip checking variable program result to improve performance,\r | |
1684 | // We can assume Variable program is OK through some check point.\r | |
1685 | // Variable Store Size Setting should be the first Variable write operation,\r | |
1686 | // We can assume all Read/Write is OK if we can set Variable store size successfully.\r | |
1687 | // If write fail, we will assert here\r | |
1688 | //\r | |
052ad7e1 | 1689 | ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r |
8d3a5c82 | 1690 | \r |
1691 | if (EFI_ERROR (Status)) {\r | |
1692 | return Status;\r | |
1693 | }\r | |
1694 | }\r | |
1695 | \r | |
052ad7e1 | 1696 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);\r |
8d3a5c82 | 1697 | //\r |
1698 | // Parse non-volatile variable data and get last variable offset\r | |
1699 | //\r | |
1700 | NextVariable = (VARIABLE_HEADER *) (CurrPtr + sizeof (VARIABLE_STORE_HEADER));\r | |
1701 | Status = EFI_SUCCESS;\r | |
1702 | \r | |
1703 | while (IsValidVariableHeader (NextVariable)) {\r | |
1704 | NextVariable = GetNextVariablePtr (NextVariable);\r | |
1705 | }\r | |
1706 | \r | |
1707 | mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) CurrPtr;\r | |
1708 | \r | |
1709 | //\r | |
1710 | // Check if the free area is blow a threshold\r | |
1711 | //\r | |
1712 | if ((((VARIABLE_STORE_HEADER *)((UINTN) CurrPtr))->Size - mVariableModuleGlobal->NonVolatileLastVariableOffset) < VARIABLE_RECLAIM_THRESHOLD) {\r | |
1713 | Status = Reclaim (\r | |
052ad7e1 | 1714 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r |
8d3a5c82 | 1715 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r |
1716 | FALSE\r | |
1717 | );\r | |
1718 | }\r | |
1719 | \r | |
1720 | if (EFI_ERROR (Status)) {\r | |
1721 | FreePool (mVariableModuleGlobal);\r | |
1722 | FreePool (VolatileVariableStore);\r | |
1723 | return Status;\r | |
1724 | }\r | |
1725 | \r | |
1726 | //\r | |
1727 | // Check if the free area is really free.\r | |
1728 | //\r | |
1729 | for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r | |
052ad7e1 | 1730 | Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)[Index];\r |
8d3a5c82 | 1731 | if (Data != 0xff) {\r |
1732 | //\r | |
1733 | // There must be something wrong in variable store, do reclaim operation.\r | |
1734 | //\r | |
1735 | Status = Reclaim (\r | |
052ad7e1 | 1736 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r |
8d3a5c82 | 1737 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r |
1738 | FALSE\r | |
1739 | );\r | |
1740 | break;\r | |
1741 | }\r | |
1742 | }\r | |
1743 | }\r | |
1744 | \r | |
1745 | if (EFI_ERROR (Status)) {\r | |
1746 | FreePool (mVariableModuleGlobal);\r | |
1747 | FreePool (VolatileVariableStore);\r | |
1748 | }\r | |
1749 | \r | |
1750 | return Status;\r | |
1751 | }\r | |
052ad7e1 A |
1752 | \r |
1753 | \r | |
1754 | \r | |
1755 | \r | |
1756 | VOID\r | |
1757 | EFIAPI\r | |
1758 | VariableClassAddressChangeEvent (\r | |
1759 | IN EFI_EVENT Event,\r | |
1760 | IN VOID *Context\r | |
1761 | )\r | |
1762 | {\r | |
1763 | EfiConvertPointer (\r | |
1764 | 0x0,\r | |
1765 | (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase\r | |
1766 | );\r | |
1767 | EfiConvertPointer (\r | |
1768 | 0x0,\r | |
1769 | (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase\r | |
1770 | );\r | |
1771 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);\r | |
1772 | }\r | |
1773 | \r | |
1774 | \r | |
1775 | /**\r | |
1776 | Variable Driver main entry point. The Variable driver places the 4 EFI\r | |
1777 | runtime services in the EFI System Table and installs arch protocols \r | |
1778 | for variable read and write services being availible. \r | |
1779 | \r | |
1780 | @param[in] ImageHandle The firmware allocated handle for the EFI image. \r | |
1781 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
1782 | \r | |
1783 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
1784 | @retval other Some error occurs when executing this entry point.\r | |
1785 | \r | |
1786 | **/\r | |
1787 | EFI_STATUS\r | |
1788 | EFIAPI\r | |
1789 | VariableServiceInitialize (\r | |
1790 | IN EFI_HANDLE ImageHandle,\r | |
1791 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1792 | )\r | |
1793 | {\r | |
1794 | EFI_STATUS Status;\r | |
1795 | \r | |
1796 | Status = VariableCommonInitialize (ImageHandle, SystemTable);\r | |
1797 | ASSERT_EFI_ERROR (Status);\r | |
1798 | \r | |
1799 | SystemTable->RuntimeServices->GetVariable = RuntimeServiceGetVariable;\r | |
1800 | SystemTable->RuntimeServices->GetNextVariableName = RuntimeServiceGetNextVariableName;\r | |
1801 | SystemTable->RuntimeServices->SetVariable = RuntimeServiceSetVariable;\r | |
1802 | SystemTable->RuntimeServices->QueryVariableInfo = RuntimeServiceQueryVariableInfo;\r | |
1803 | \r | |
1804 | //\r | |
1805 | // Now install the Variable Runtime Architectural Protocol on a new handle\r | |
1806 | //\r | |
1807 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
1808 | &mHandle,\r | |
1809 | &gEfiVariableArchProtocolGuid, NULL,\r | |
1810 | &gEfiVariableWriteArchProtocolGuid, NULL,\r | |
1811 | NULL\r | |
1812 | );\r | |
1813 | ASSERT_EFI_ERROR (Status);\r | |
1814 | \r | |
1815 | Status = gBS->CreateEvent (\r | |
1816 | EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,\r | |
1817 | TPL_NOTIFY,\r | |
1818 | VariableClassAddressChangeEvent,\r | |
1819 | NULL,\r | |
1820 | &mVirtualAddressChangeEvent\r | |
1821 | );\r | |
1822 | ASSERT_EFI_ERROR (Status);\r | |
1823 | \r | |
1824 | return EFI_SUCCESS;\r | |
1825 | }\r | |
1826 | \r |