]>
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 |
8d3a5c82 | 740 | //\r |
052ad7e1 A |
741 | VariableStoreHeader[0] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r |
742 | VariableStoreHeader[1] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r | |
8d3a5c82 | 743 | \r |
744 | //\r | |
745 | // Start Pointers for the variable.\r | |
746 | // Actual Data Pointer where data can be written.\r | |
747 | //\r | |
748 | Variable[0] = (VARIABLE_HEADER *) (VariableStoreHeader[0] + 1);\r | |
749 | Variable[1] = (VARIABLE_HEADER *) (VariableStoreHeader[1] + 1);\r | |
750 | \r | |
751 | if (VariableName[0] != 0 && VendorGuid == NULL) {\r | |
752 | return EFI_INVALID_PARAMETER;\r | |
753 | }\r | |
754 | //\r | |
33a5a666 | 755 | // Find the variable by walk through volatile and then non-volatile variable store\r |
8d3a5c82 | 756 | //\r |
757 | for (Index = 0; Index < 2; Index++) {\r | |
758 | PtrTrack->StartPtr = (VARIABLE_HEADER *) (VariableStoreHeader[Index] + 1);\r | |
759 | PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);\r | |
760 | \r | |
761 | while (IsValidVariableHeader (Variable[Index]) && (Variable[Index] <= GetEndPointer (VariableStoreHeader[Index]))) {\r | |
762 | if (Variable[Index]->State == VAR_ADDED) {\r | |
c6492839 | 763 | if (!EfiAtRuntime () || (Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r |
8d3a5c82 | 764 | if (VariableName[0] == 0) {\r |
765 | PtrTrack->CurrPtr = Variable[Index];\r | |
33a5a666 | 766 | PtrTrack->Volatile = (BOOLEAN)(Index == 0);\r |
8d3a5c82 | 767 | return EFI_SUCCESS;\r |
768 | } else {\r | |
769 | if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {\r | |
770 | if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable[Index]), Variable[Index]->NameSize)) {\r | |
771 | PtrTrack->CurrPtr = Variable[Index];\r | |
33a5a666 | 772 | PtrTrack->Volatile = (BOOLEAN)(Index == 0);\r |
8d3a5c82 | 773 | return EFI_SUCCESS;\r |
774 | }\r | |
775 | }\r | |
776 | }\r | |
777 | }\r | |
778 | }\r | |
779 | \r | |
780 | Variable[Index] = GetNextVariablePtr (Variable[Index]);\r | |
781 | }\r | |
8d3a5c82 | 782 | }\r |
8d3a5c82 | 783 | PtrTrack->CurrPtr = NULL;\r |
784 | return EFI_NOT_FOUND;\r | |
785 | }\r | |
786 | \r | |
33a5a666 | 787 | \r |
052ad7e1 | 788 | \r |
8d3a5c82 | 789 | /*++\r |
790 | \r | |
791 | Routine Description:\r | |
792 | \r | |
793 | This code finds variable in storage blocks (Volatile or Non-Volatile)\r | |
794 | \r | |
795 | Arguments:\r | |
796 | \r | |
c6492839 | 797 | VariableName Name of Variable to be found\r |
798 | VendorGuid Variable vendor GUID\r | |
799 | Attributes OPTIONAL Attribute value of the variable found\r | |
800 | DataSize Size of Data found. If size is less than the\r | |
801 | data, this value contains the required size.\r | |
802 | Data Data pointer\r | |
803 | Global Pointer to VARIABLE_GLOBAL structure\r | |
804 | Instance Instance of the Firmware Volume.\r | |
8d3a5c82 | 805 | \r |
806 | Returns:\r | |
807 | \r | |
c6492839 | 808 | EFI_INVALID_PARAMETER - Invalid parameter\r |
809 | EFI_SUCCESS - Find the specified variable\r | |
810 | EFI_NOT_FOUND - Not found\r | |
811 | EFI_BUFFER_TO_SMALL - DataSize is too small for the result\r | |
812 | \r | |
8d3a5c82 | 813 | \r |
814 | --*/\r | |
052ad7e1 A |
815 | EFI_STATUS\r |
816 | EFIAPI\r | |
817 | RuntimeServiceGetVariable (\r | |
818 | IN CHAR16 *VariableName,\r | |
819 | IN EFI_GUID *VendorGuid,\r | |
820 | OUT UINT32 *Attributes OPTIONAL,\r | |
821 | IN OUT UINTN *DataSize,\r | |
822 | OUT VOID *Data\r | |
823 | )\r | |
8d3a5c82 | 824 | {\r |
052ad7e1 | 825 | EFI_STATUS Status;\r |
8d3a5c82 | 826 | VARIABLE_POINTER_TRACK Variable;\r |
827 | UINTN VarDataSize;\r | |
8d3a5c82 | 828 | \r |
829 | if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r | |
830 | return EFI_INVALID_PARAMETER;\r | |
831 | }\r | |
33a5a666 | 832 | \r |
8d3a5c82 | 833 | //\r |
834 | // Find existing variable\r | |
835 | //\r | |
33a5a666 A |
836 | Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r |
837 | if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){\r | |
838 | // Hit in the Cache\r | |
839 | UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);\r | |
840 | return Status;\r | |
841 | }\r | |
842 | \r | |
052ad7e1 | 843 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r |
8d3a5c82 | 844 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r |
845 | goto Done;\r | |
846 | }\r | |
33a5a666 | 847 | \r |
8d3a5c82 | 848 | //\r |
849 | // Get data size\r | |
850 | //\r | |
851 | VarDataSize = Variable.CurrPtr->DataSize;\r | |
852 | if (*DataSize >= VarDataSize) {\r | |
853 | if (Data == NULL) {\r | |
854 | Status = EFI_INVALID_PARAMETER;\r | |
855 | goto Done;\r | |
856 | }\r | |
857 | \r | |
858 | CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r | |
859 | if (Attributes != NULL) {\r | |
860 | *Attributes = Variable.CurrPtr->Attributes;\r | |
861 | }\r | |
862 | \r | |
863 | *DataSize = VarDataSize;\r | |
33a5a666 A |
864 | UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r |
865 | UpdateVariableCache (VariableName, VendorGuid, Variable.CurrPtr->Attributes, VarDataSize, Data);\r | |
866 | \r | |
8d3a5c82 | 867 | Status = EFI_SUCCESS;\r |
868 | goto Done;\r | |
869 | } else {\r | |
870 | *DataSize = VarDataSize;\r | |
871 | Status = EFI_BUFFER_TOO_SMALL;\r | |
872 | goto Done;\r | |
873 | }\r | |
874 | \r | |
875 | Done:\r | |
052ad7e1 | 876 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 877 | return Status;\r |
878 | }\r | |
879 | \r | |
052ad7e1 A |
880 | \r |
881 | \r | |
8d3a5c82 | 882 | /*++\r |
883 | \r | |
884 | Routine Description:\r | |
885 | \r | |
886 | This code Finds the Next available variable\r | |
887 | \r | |
888 | Arguments:\r | |
889 | \r | |
890 | VariableNameSize Size of the variable\r | |
891 | VariableName Pointer to variable name\r | |
892 | VendorGuid Variable Vendor Guid\r | |
893 | Global VARIABLE_GLOBAL structure pointer.\r | |
894 | Instance FV instance\r | |
895 | \r | |
896 | Returns:\r | |
897 | \r | |
898 | EFI STATUS\r | |
899 | \r | |
900 | --*/\r | |
052ad7e1 A |
901 | EFI_STATUS\r |
902 | EFIAPI\r | |
903 | RuntimeServiceGetNextVariableName (\r | |
904 | IN OUT UINTN *VariableNameSize,\r | |
905 | IN OUT CHAR16 *VariableName,\r | |
906 | IN OUT EFI_GUID *VendorGuid\r | |
907 | )\r | |
8d3a5c82 | 908 | {\r |
909 | VARIABLE_POINTER_TRACK Variable;\r | |
910 | UINTN VarNameSize;\r | |
911 | EFI_STATUS Status;\r | |
912 | \r | |
913 | if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r | |
914 | return EFI_INVALID_PARAMETER;\r | |
915 | }\r | |
916 | \r | |
052ad7e1 | 917 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r |
8d3a5c82 | 918 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r |
919 | goto Done;\r | |
920 | }\r | |
921 | \r | |
922 | if (VariableName[0] != 0) {\r | |
923 | //\r | |
924 | // If variable name is not NULL, get next variable\r | |
925 | //\r | |
926 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
927 | }\r | |
928 | \r | |
929 | while (TRUE) {\r | |
930 | //\r | |
931 | // If both volatile and non-volatile variable store are parsed,\r | |
932 | // return not found\r | |
933 | //\r | |
934 | if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r | |
935 | Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));\r | |
936 | if (Variable.Volatile) {\r | |
052ad7e1 A |
937 | Variable.StartPtr = (VARIABLE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase + sizeof (VARIABLE_STORE_HEADER)));\r |
938 | Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r | |
8d3a5c82 | 939 | } else {\r |
940 | Status = EFI_NOT_FOUND;\r | |
941 | goto Done;\r | |
942 | }\r | |
943 | \r | |
944 | Variable.CurrPtr = Variable.StartPtr;\r | |
945 | if (!IsValidVariableHeader (Variable.CurrPtr)) {\r | |
946 | continue;\r | |
947 | }\r | |
948 | }\r | |
949 | //\r | |
950 | // Variable is found\r | |
951 | //\r | |
952 | if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {\r | |
953 | if (!(EfiAtRuntime () && !(Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {\r | |
954 | VarNameSize = Variable.CurrPtr->NameSize;\r | |
955 | if (VarNameSize <= *VariableNameSize) {\r | |
956 | CopyMem (\r | |
957 | VariableName,\r | |
958 | GET_VARIABLE_NAME_PTR (Variable.CurrPtr),\r | |
959 | VarNameSize\r | |
960 | );\r | |
961 | CopyMem (\r | |
962 | VendorGuid,\r | |
963 | &Variable.CurrPtr->VendorGuid,\r | |
964 | sizeof (EFI_GUID)\r | |
965 | );\r | |
966 | Status = EFI_SUCCESS;\r | |
967 | } else {\r | |
968 | Status = EFI_BUFFER_TOO_SMALL;\r | |
969 | }\r | |
970 | \r | |
971 | *VariableNameSize = VarNameSize;\r | |
972 | goto Done;\r | |
973 | }\r | |
974 | }\r | |
975 | \r | |
976 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
977 | }\r | |
978 | \r | |
979 | Done:\r | |
052ad7e1 | 980 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 981 | return Status;\r |
982 | }\r | |
983 | \r | |
052ad7e1 | 984 | \r |
8d3a5c82 | 985 | /*++\r |
986 | \r | |
987 | Routine Description:\r | |
988 | \r | |
989 | This code sets variable in storage blocks (Volatile or Non-Volatile)\r | |
990 | \r | |
991 | Arguments:\r | |
992 | \r | |
993 | VariableName Name of Variable to be found\r | |
994 | VendorGuid Variable vendor GUID\r | |
995 | Attributes Attribute value of the variable found\r | |
996 | DataSize Size of Data found. If size is less than the\r | |
997 | data, this value contains the required size.\r | |
998 | Data Data pointer\r | |
999 | Global Pointer to VARIABLE_GLOBAL structure\r | |
1000 | VolatileOffset The offset of last volatile variable\r | |
1001 | NonVolatileOffset The offset of last non-volatile variable\r | |
1002 | Instance Instance of the Firmware Volume.\r | |
1003 | \r | |
1004 | Returns:\r | |
1005 | \r | |
8d3a5c82 | 1006 | EFI_INVALID_PARAMETER - Invalid parameter\r |
1007 | EFI_SUCCESS - Set successfully\r | |
1008 | EFI_OUT_OF_RESOURCES - Resource not enough to set variable\r | |
1009 | EFI_NOT_FOUND - Not found\r | |
c6492839 | 1010 | EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure\r |
1011 | EFI_WRITE_PROTECTED - Variable is read-only\r | |
8d3a5c82 | 1012 | \r |
1013 | --*/\r | |
052ad7e1 A |
1014 | EFI_STATUS\r |
1015 | EFIAPI\r | |
1016 | RuntimeServiceSetVariable (\r | |
1017 | IN CHAR16 *VariableName,\r | |
1018 | IN EFI_GUID *VendorGuid,\r | |
1019 | IN UINT32 Attributes,\r | |
1020 | IN UINTN DataSize,\r | |
1021 | IN VOID *Data\r | |
1022 | )\r | |
8d3a5c82 | 1023 | {\r |
1024 | VARIABLE_POINTER_TRACK Variable;\r | |
1025 | EFI_STATUS Status;\r | |
1026 | VARIABLE_HEADER *NextVariable;\r | |
1027 | UINTN VarNameSize;\r | |
1028 | UINTN VarNameOffset;\r | |
1029 | UINTN VarDataOffset;\r | |
1030 | UINTN VarSize;\r | |
1031 | UINT8 State;\r | |
1032 | BOOLEAN Reclaimed;\r | |
052ad7e1 A |
1033 | UINTN *VolatileOffset;\r |
1034 | UINTN *NonVolatileOffset;\r | |
1035 | UINT32 Instance;\r | |
8d3a5c82 | 1036 | \r |
052ad7e1 A |
1037 | Reclaimed = FALSE;\r |
1038 | VolatileOffset = &mVariableModuleGlobal->VolatileLastVariableOffset;\r | |
1039 | NonVolatileOffset = &mVariableModuleGlobal->NonVolatileLastVariableOffset;\r | |
1040 | Instance = mVariableModuleGlobal->FvbInstance;\r | |
8d3a5c82 | 1041 | \r |
c6492839 | 1042 | //\r |
1043 | // Check input parameters\r | |
1044 | //\r | |
8d3a5c82 | 1045 | if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r |
1046 | return EFI_INVALID_PARAMETER;\r | |
c6492839 | 1047 | } \r |
1048 | //\r | |
1049 | // Make sure if runtime bit is set, boot service bit is set also\r | |
1050 | //\r | |
1051 | if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r | |
1052 | return EFI_INVALID_PARAMETER;\r | |
8d3a5c82 | 1053 | }\r |
c6492839 | 1054 | //\r |
1055 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
1056 | // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)\r | |
1057 | // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.\r | |
1058 | //\r | |
1059 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
1060 | if ((DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE) || \r | |
1061 | (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE)) {\r | |
1062 | return EFI_INVALID_PARAMETER;\r | |
1063 | } \r | |
1064 | } else {\r | |
1065 | //\r | |
1066 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
1067 | // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.\r | |
1068 | //\r | |
1069 | if ((DataSize > MAX_VARIABLE_SIZE) ||\r | |
1070 | (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE)) {\r | |
1071 | return EFI_INVALID_PARAMETER;\r | |
1072 | } \r | |
1073 | } \r | |
1074 | //\r | |
1075 | // Check whether the input variable is already existed\r | |
1076 | //\r | |
1077 | \r | |
052ad7e1 | 1078 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r |
8d3a5c82 | 1079 | \r |
c6492839 | 1080 | if (Status == EFI_SUCCESS && Variable.CurrPtr != NULL) {\r |
8d3a5c82 | 1081 | //\r |
c6492839 | 1082 | // Update/Delete existing variable\r |
8d3a5c82 | 1083 | //\r |
c6492839 | 1084 | \r |
1085 | if (EfiAtRuntime ()) { \r | |
1086 | //\r | |
1087 | // If EfiAtRuntime and the variable is Volatile and Runtime Access, \r | |
1088 | // the volatile is ReadOnly, and SetVariable should be aborted and \r | |
1089 | // return EFI_WRITE_PROTECTED.\r | |
1090 | //\r | |
1091 | if (Variable.Volatile) {\r | |
1092 | Status = EFI_WRITE_PROTECTED;\r | |
1093 | goto Done;\r | |
1094 | }\r | |
1095 | //\r | |
1096 | // Only variable have NV attribute can be updated/deleted in Runtime\r | |
1097 | //\r | |
1098 | if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r | |
1099 | Status = EFI_INVALID_PARAMETER;\r | |
1100 | goto Done; \r | |
1101 | }\r | |
1102 | }\r | |
8d3a5c82 | 1103 | //\r |
c6492839 | 1104 | // Setting a data variable with no access, or zero DataSize attributes\r |
1105 | // specified causes it to be deleted.\r | |
8d3a5c82 | 1106 | //\r |
c6492839 | 1107 | if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { \r |
1108 | State = Variable.CurrPtr->State;\r | |
1109 | State &= VAR_DELETED;\r | |
1110 | \r | |
1111 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1112 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1113 | Variable.Volatile,\r |
1114 | FALSE,\r | |
1115 | Instance,\r | |
1116 | (UINTN) &Variable.CurrPtr->State,\r | |
1117 | sizeof (UINT8),\r | |
1118 | &State\r | |
1119 | ); \r | |
33a5a666 A |
1120 | if (!EFI_ERROR (Status)) {\r |
1121 | UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, FALSE, FALSE, TRUE, FALSE);\r | |
1122 | UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r | |
1123 | }\r | |
c6492839 | 1124 | goto Done; \r |
1125 | }\r | |
8d3a5c82 | 1126 | //\r |
c6492839 | 1127 | // If the variable is marked valid and the same data has been passed in\r |
1128 | // then return to the caller immediately.\r | |
8d3a5c82 | 1129 | //\r |
c6492839 | 1130 | if (Variable.CurrPtr->DataSize == DataSize &&\r |
1131 | (CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0)) {\r | |
33a5a666 A |
1132 | \r |
1133 | UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, FALSE, TRUE, FALSE, FALSE);\r | |
c6492839 | 1134 | Status = EFI_SUCCESS;\r |
1135 | goto Done;\r | |
1136 | } else if ((Variable.CurrPtr->State == VAR_ADDED) ||\r | |
1137 | (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r | |
1138 | //\r | |
1139 | // Mark the old variable as in delete transition\r | |
1140 | //\r | |
1141 | State = Variable.CurrPtr->State;\r | |
1142 | State &= VAR_IN_DELETED_TRANSITION;\r | |
1143 | \r | |
1144 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1145 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1146 | Variable.Volatile,\r |
1147 | FALSE,\r | |
1148 | Instance,\r | |
1149 | (UINTN) &Variable.CurrPtr->State,\r | |
1150 | sizeof (UINT8),\r | |
1151 | &State\r | |
1152 | ); \r | |
1153 | if (EFI_ERROR (Status)) {\r | |
1154 | goto Done; \r | |
33a5a666 | 1155 | } \r |
c6492839 | 1156 | } \r |
1157 | } else if (Status == EFI_NOT_FOUND) {\r | |
8d3a5c82 | 1158 | //\r |
c6492839 | 1159 | // Create a new variable\r |
1160 | // \r | |
1161 | \r | |
8d3a5c82 | 1162 | //\r |
c6492839 | 1163 | // Make sure we are trying to create a new variable.\r |
1164 | // Setting a data variable with no access, or zero DataSize attributes means to delete it. \r | |
8d3a5c82 | 1165 | //\r |
c6492839 | 1166 | if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r |
1167 | Status = EFI_NOT_FOUND;\r | |
1168 | goto Done;\r | |
1169 | }\r | |
1170 | \r | |
8d3a5c82 | 1171 | //\r |
c6492839 | 1172 | // Only variable have NV|RT attribute can be created in Runtime\r |
1173 | //\r | |
1174 | if (EfiAtRuntime () &&\r | |
1175 | (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {\r | |
1176 | Status = EFI_INVALID_PARAMETER;\r | |
1177 | goto Done;\r | |
1178 | } \r | |
1179 | } else {\r | |
8d3a5c82 | 1180 | //\r |
c6492839 | 1181 | // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().\r |
8d3a5c82 | 1182 | //\r |
c6492839 | 1183 | ASSERT (Status == EFI_INVALID_PARAMETER);\r |
8d3a5c82 | 1184 | goto Done;\r |
c6492839 | 1185 | }\r |
1186 | \r | |
1187 | //\r | |
1188 | // Function part - create a new variable and copy the data.\r | |
1189 | // Both update a variable and create a variable will come here.\r | |
1190 | //\r | |
1191 | // Tricky part: Use scratch data area at the end of volatile variable store\r | |
1192 | // as a temporary storage.\r | |
1193 | //\r | |
052ad7e1 | 1194 | NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r |
c6492839 | 1195 | \r |
1196 | SetMem (NextVariable, SCRATCH_SIZE, 0xff);\r | |
1197 | \r | |
1198 | NextVariable->StartId = VARIABLE_DATA;\r | |
1199 | NextVariable->Attributes = Attributes;\r | |
1200 | //\r | |
1201 | // NextVariable->State = VAR_ADDED;\r | |
1202 | //\r | |
1203 | NextVariable->Reserved = 0;\r | |
1204 | VarNameOffset = sizeof (VARIABLE_HEADER);\r | |
1205 | VarNameSize = StrSize (VariableName);\r | |
1206 | CopyMem (\r | |
1207 | (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r | |
1208 | VariableName,\r | |
1209 | VarNameSize\r | |
1210 | );\r | |
1211 | VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r | |
1212 | CopyMem (\r | |
1213 | (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r | |
1214 | Data,\r | |
1215 | DataSize\r | |
1216 | );\r | |
1217 | CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r | |
1218 | //\r | |
1219 | // There will be pad bytes after Data, the NextVariable->NameSize and\r | |
1220 | // NextVariable->DataSize should not include pad size so that variable\r | |
1221 | // service can get actual size in GetVariable\r | |
1222 | //\r | |
1223 | NextVariable->NameSize = (UINT32)VarNameSize;\r | |
1224 | NextVariable->DataSize = (UINT32)DataSize;\r | |
1225 | \r | |
1226 | //\r | |
1227 | // The actual size of the variable that stores in storage should\r | |
1228 | // include pad size.\r | |
1229 | //\r | |
1230 | VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r | |
1231 | if (Attributes & EFI_VARIABLE_NON_VOLATILE) {\r | |
8d3a5c82 | 1232 | //\r |
c6492839 | 1233 | // Create a nonvolatile variable\r |
8d3a5c82 | 1234 | //\r |
33a5a666 | 1235 | Variable.Volatile = FALSE;\r |
c6492839 | 1236 | \r |
1237 | if ((UINT32) (VarSize +*NonVolatileOffset) >\r | |
052ad7e1 | 1238 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size\r |
c6492839 | 1239 | ) {\r |
1240 | if (EfiAtRuntime ()) {\r | |
1241 | Status = EFI_OUT_OF_RESOURCES;\r | |
1242 | goto Done;\r | |
1243 | }\r | |
1244 | //\r | |
1245 | // Perform garbage collection & reclaim operation\r | |
1246 | //\r | |
052ad7e1 | 1247 | Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, NonVolatileOffset, FALSE);\r |
8d3a5c82 | 1248 | if (EFI_ERROR (Status)) {\r |
1249 | goto Done;\r | |
1250 | }\r | |
8d3a5c82 | 1251 | //\r |
c6492839 | 1252 | // If still no enough space, return out of resources\r |
8d3a5c82 | 1253 | //\r |
c6492839 | 1254 | if ((UINT32) (VarSize +*NonVolatileOffset) >\r |
052ad7e1 | 1255 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size\r |
8d3a5c82 | 1256 | ) {\r |
c6492839 | 1257 | Status = EFI_OUT_OF_RESOURCES;\r |
8d3a5c82 | 1258 | goto Done;\r |
8d3a5c82 | 1259 | }\r |
c6492839 | 1260 | \r |
1261 | Reclaimed = TRUE;\r | |
8d3a5c82 | 1262 | }\r |
1263 | //\r | |
c6492839 | 1264 | // Three steps\r |
1265 | // 1. Write variable header\r | |
1266 | // 2. Write variable data\r | |
1267 | // 3. Set variable state to valid\r | |
8d3a5c82 | 1268 | //\r |
8d3a5c82 | 1269 | //\r |
c6492839 | 1270 | // Step 1:\r |
8d3a5c82 | 1271 | //\r |
c6492839 | 1272 | Status = UpdateVariableStore (\r |
052ad7e1 | 1273 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1274 | FALSE,\r |
1275 | TRUE,\r | |
1276 | Instance,\r | |
1277 | *NonVolatileOffset,\r | |
1278 | sizeof (VARIABLE_HEADER),\r | |
1279 | (UINT8 *) NextVariable\r | |
1280 | );\r | |
1281 | \r | |
1282 | if (EFI_ERROR (Status)) {\r | |
1283 | goto Done;\r | |
1284 | }\r | |
8d3a5c82 | 1285 | //\r |
c6492839 | 1286 | // Step 2:\r |
8d3a5c82 | 1287 | //\r |
c6492839 | 1288 | Status = UpdateVariableStore (\r |
052ad7e1 | 1289 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1290 | FALSE,\r |
1291 | TRUE,\r | |
1292 | Instance,\r | |
1293 | *NonVolatileOffset + sizeof (VARIABLE_HEADER),\r | |
1294 | (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r | |
1295 | (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r | |
1296 | );\r | |
1297 | \r | |
1298 | if (EFI_ERROR (Status)) {\r | |
1299 | goto Done;\r | |
1300 | }\r | |
8d3a5c82 | 1301 | //\r |
c6492839 | 1302 | // Step 3:\r |
8d3a5c82 | 1303 | //\r |
c6492839 | 1304 | NextVariable->State = VAR_ADDED;\r |
1305 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1306 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1307 | FALSE,\r |
1308 | TRUE,\r | |
1309 | Instance,\r | |
1310 | *NonVolatileOffset,\r | |
1311 | sizeof (VARIABLE_HEADER),\r | |
1312 | (UINT8 *) NextVariable\r | |
1313 | );\r | |
1314 | \r | |
1315 | if (EFI_ERROR (Status)) {\r | |
1316 | goto Done;\r | |
1317 | }\r | |
8d3a5c82 | 1318 | \r |
c6492839 | 1319 | *NonVolatileOffset = *NonVolatileOffset + VarSize;\r |
8d3a5c82 | 1320 | \r |
c6492839 | 1321 | } else {\r |
1322 | //\r | |
1323 | // Create a volatile variable\r | |
1324 | // \r | |
33a5a666 | 1325 | Variable.Volatile = TRUE;\r |
c6492839 | 1326 | \r |
1327 | if ((UINT32) (VarSize +*VolatileOffset) >\r | |
052ad7e1 | 1328 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r |
8d3a5c82 | 1329 | //\r |
c6492839 | 1330 | // Perform garbage collection & reclaim operation\r |
8d3a5c82 | 1331 | //\r |
052ad7e1 | 1332 | Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, VolatileOffset, TRUE);\r |
8d3a5c82 | 1333 | if (EFI_ERROR (Status)) {\r |
1334 | goto Done;\r | |
1335 | }\r | |
1336 | //\r | |
c6492839 | 1337 | // If still no enough space, return out of resources\r |
8d3a5c82 | 1338 | //\r |
8d3a5c82 | 1339 | if ((UINT32) (VarSize +*VolatileOffset) >\r |
052ad7e1 | 1340 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size\r |
8d3a5c82 | 1341 | ) {\r |
c6492839 | 1342 | Status = EFI_OUT_OF_RESOURCES;\r |
8d3a5c82 | 1343 | goto Done;\r |
1344 | }\r | |
c6492839 | 1345 | \r |
1346 | Reclaimed = TRUE;\r | |
8d3a5c82 | 1347 | }\r |
8d3a5c82 | 1348 | \r |
c6492839 | 1349 | NextVariable->State = VAR_ADDED;\r |
1350 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1351 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1352 | TRUE,\r |
1353 | TRUE,\r | |
1354 | Instance,\r | |
1355 | *VolatileOffset,\r | |
1356 | (UINT32) VarSize,\r | |
1357 | (UINT8 *) NextVariable\r | |
1358 | );\r | |
1359 | \r | |
1360 | if (EFI_ERROR (Status)) {\r | |
1361 | goto Done;\r | |
8d3a5c82 | 1362 | }\r |
c6492839 | 1363 | \r |
1364 | *VolatileOffset = *VolatileOffset + VarSize;\r | |
1365 | }\r | |
1366 | //\r | |
1367 | // Mark the old variable as deleted\r | |
1368 | //\r | |
1369 | if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {\r | |
1370 | State = Variable.CurrPtr->State;\r | |
1371 | State &= VAR_DELETED;\r | |
1372 | \r | |
1373 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1374 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1375 | Variable.Volatile,\r |
1376 | FALSE,\r | |
1377 | Instance,\r | |
1378 | (UINTN) &Variable.CurrPtr->State,\r | |
1379 | sizeof (UINT8),\r | |
1380 | &State\r | |
1381 | );\r | |
33a5a666 A |
1382 | \r |
1383 | if (!EFI_ERROR (Status)) {\r | |
1384 | UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, FALSE, TRUE, FALSE, FALSE);\r | |
1385 | UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r | |
1386 | }\r | |
c6492839 | 1387 | goto Done; \r |
8d3a5c82 | 1388 | }\r |
1389 | \r | |
1390 | Status = EFI_SUCCESS;\r | |
33a5a666 A |
1391 | UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, FALSE, TRUE, FALSE, FALSE);\r |
1392 | UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r | |
1393 | \r | |
8d3a5c82 | 1394 | Done:\r |
052ad7e1 | 1395 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 1396 | return Status;\r |
1397 | }\r | |
1398 | \r | |
052ad7e1 | 1399 | \r |
8d3a5c82 | 1400 | /*++\r |
1401 | \r | |
1402 | Routine Description:\r | |
1403 | \r | |
1404 | This code returns information about the EFI variables.\r | |
1405 | \r | |
1406 | Arguments:\r | |
1407 | \r | |
1408 | Attributes Attributes bitmask to specify the type of variables\r | |
1409 | on which to return information.\r | |
1410 | MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r | |
1411 | for the EFI variables associated with the attributes specified.\r | |
1412 | RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r | |
c6492839 | 1413 | for EFI variables associated with the attributes specified.\r |
1414 | MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r | |
8d3a5c82 | 1415 | associated with the attributes specified.\r |
1416 | Global Pointer to VARIABLE_GLOBAL structure.\r | |
1417 | Instance Instance of the Firmware Volume.\r | |
1418 | \r | |
1419 | Returns:\r | |
1420 | \r | |
1421 | EFI STATUS\r | |
1422 | EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.\r | |
1423 | EFI_SUCCESS - Query successfully.\r | |
1424 | EFI_UNSUPPORTED - The attribute is not supported on this platform.\r | |
1425 | \r | |
1426 | --*/\r | |
052ad7e1 A |
1427 | EFI_STATUS\r |
1428 | EFIAPI\r | |
1429 | RuntimeServiceQueryVariableInfo (\r | |
1430 | IN UINT32 Attributes,\r | |
1431 | OUT UINT64 *MaximumVariableStorageSize,\r | |
1432 | OUT UINT64 *RemainingVariableStorageSize,\r | |
1433 | OUT UINT64 *MaximumVariableSize\r | |
1434 | )\r | |
8d3a5c82 | 1435 | {\r |
1436 | VARIABLE_HEADER *Variable;\r | |
1437 | VARIABLE_HEADER *NextVariable;\r | |
1438 | UINT64 VariableSize;\r | |
1439 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
1440 | \r | |
c6492839 | 1441 | if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r |
8d3a5c82 | 1442 | return EFI_INVALID_PARAMETER;\r |
1443 | }\r | |
c6492839 | 1444 | \r |
1445 | if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r | |
8d3a5c82 | 1446 | //\r |
1447 | // Make sure the Attributes combination is supported by the platform.\r | |
1448 | //\r | |
c6492839 | 1449 | return EFI_UNSUPPORTED; \r |
8d3a5c82 | 1450 | } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r |
1451 | //\r | |
1452 | // Make sure if runtime bit is set, boot service bit is set also.\r | |
1453 | //\r | |
1454 | return EFI_INVALID_PARAMETER;\r | |
1455 | } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r | |
1456 | //\r | |
1457 | // Make sure RT Attribute is set if we are in Runtime phase.\r | |
1458 | //\r | |
1459 | return EFI_INVALID_PARAMETER;\r | |
1460 | }\r | |
1461 | \r | |
052ad7e1 | 1462 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 1463 | \r |
1464 | if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r | |
1465 | //\r | |
1466 | // Query is Volatile related.\r | |
1467 | //\r | |
052ad7e1 | 1468 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r |
8d3a5c82 | 1469 | } else {\r |
1470 | //\r | |
1471 | // Query is Non-Volatile related.\r | |
1472 | //\r | |
052ad7e1 | 1473 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r |
8d3a5c82 | 1474 | }\r |
1475 | \r | |
1476 | //\r | |
1477 | // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r | |
1478 | // with the storage size (excluding the storage header size).\r | |
1479 | //\r | |
1480 | *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r | |
1481 | *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r | |
1482 | \r | |
1483 | //\r | |
c6492839 | 1484 | // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.\r |
8d3a5c82 | 1485 | //\r |
c6492839 | 1486 | *MaximumVariableSize = MAX_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r |
1487 | \r | |
1488 | //\r | |
1489 | // Harware error record variable needs larger size.\r | |
1490 | //\r | |
1491 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
1492 | *MaximumVariableSize = MAX_HARDWARE_ERROR_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r | |
1493 | }\r | |
8d3a5c82 | 1494 | \r |
1495 | //\r | |
1496 | // Point to the starting address of the variables.\r | |
1497 | //\r | |
1498 | Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r | |
1499 | \r | |
1500 | //\r | |
1501 | // Now walk through the related variable store.\r | |
1502 | //\r | |
1503 | while (IsValidVariableHeader (Variable) && (Variable < GetEndPointer (VariableStoreHeader))) {\r | |
1504 | NextVariable = GetNextVariablePtr (Variable);\r | |
1505 | VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r | |
1506 | \r | |
1507 | if (EfiAtRuntime ()) {\r | |
1508 | //\r | |
1509 | // we don't take the state of the variables in mind\r | |
1510 | // when calculating RemainingVariableStorageSize,\r | |
1511 | // since the space occupied by variables not marked with\r | |
1512 | // VAR_ADDED is not allowed to be reclaimed in Runtime.\r | |
1513 | //\r | |
1514 | *RemainingVariableStorageSize -= VariableSize;\r | |
1515 | } else {\r | |
1516 | //\r | |
1517 | // Only care about Variables with State VAR_ADDED,because\r | |
1518 | // the space not marked as VAR_ADDED is reclaimable now.\r | |
1519 | //\r | |
1520 | if (Variable->State == VAR_ADDED) {\r | |
1521 | *RemainingVariableStorageSize -= VariableSize;\r | |
1522 | }\r | |
1523 | }\r | |
1524 | \r | |
1525 | //\r | |
1526 | // Go to the next one\r | |
1527 | //\r | |
1528 | Variable = NextVariable;\r | |
1529 | }\r | |
1530 | \r | |
c6492839 | 1531 | if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r |
1532 | *MaximumVariableSize = 0;\r | |
1533 | } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r | |
1534 | *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r | |
1535 | }\r | |
1536 | \r | |
052ad7e1 | 1537 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 1538 | return EFI_SUCCESS;\r |
1539 | }\r | |
1540 | \r | |
1541 | EFI_STATUS\r | |
8d3a5c82 | 1542 | VariableCommonInitialize (\r |
1543 | IN EFI_HANDLE ImageHandle,\r | |
1544 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1545 | )\r | |
1546 | /*++\r | |
1547 | \r | |
1548 | Routine Description:\r | |
1549 | This function does common initialization for variable services\r | |
1550 | \r | |
1551 | Arguments:\r | |
1552 | \r | |
1553 | ImageHandle - The firmware allocated handle for the EFI image.\r | |
1554 | SystemTable - A pointer to the EFI System Table.\r | |
1555 | \r | |
1556 | Returns:\r | |
1557 | \r | |
1558 | Status code.\r | |
1559 | \r | |
1560 | EFI_NOT_FOUND - Variable store area not found.\r | |
1561 | EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.\r | |
1562 | EFI_SUCCESS - Variable services successfully initialized.\r | |
1563 | \r | |
1564 | --*/\r | |
1565 | {\r | |
1566 | EFI_STATUS Status;\r | |
1567 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
1568 | CHAR8 *CurrPtr;\r | |
1569 | VARIABLE_STORE_HEADER *VolatileVariableStore;\r | |
1570 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
1571 | VARIABLE_HEADER *NextVariable;\r | |
1572 | UINT32 Instance;\r | |
1573 | EFI_PHYSICAL_ADDRESS FvVolHdr;\r | |
8d3a5c82 | 1574 | UINT64 TempVariableStoreHeader;\r |
8d3a5c82 | 1575 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r |
8d3a5c82 | 1576 | UINT64 BaseAddress;\r |
1577 | UINT64 Length;\r | |
1578 | UINTN Index;\r | |
1579 | UINT8 Data;\r | |
052ad7e1 A |
1580 | UINT64 VariableStoreBase;\r |
1581 | UINT64 VariableStoreLength;\r | |
8d3a5c82 | 1582 | \r |
8d3a5c82 | 1583 | \r |
052ad7e1 | 1584 | EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r |
8d3a5c82 | 1585 | \r |
1586 | //\r | |
1587 | // Allocate memory for volatile variable store\r | |
1588 | //\r | |
1589 | VolatileVariableStore = AllocateRuntimePool (VARIABLE_STORE_SIZE + SCRATCH_SIZE);\r | |
1590 | if (VolatileVariableStore == NULL) {\r | |
1591 | FreePool (mVariableModuleGlobal);\r | |
1592 | return EFI_OUT_OF_RESOURCES;\r | |
1593 | }\r | |
1594 | \r | |
1595 | SetMem (VolatileVariableStore, VARIABLE_STORE_SIZE + SCRATCH_SIZE, 0xff);\r | |
1596 | \r | |
1597 | //\r | |
1598 | // Variable Specific Data\r | |
1599 | //\r | |
052ad7e1 | 1600 | mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r |
8d3a5c82 | 1601 | mVariableModuleGlobal->VolatileLastVariableOffset = sizeof (VARIABLE_STORE_HEADER);\r |
1602 | \r | |
1603 | VolatileVariableStore->Signature = VARIABLE_STORE_SIGNATURE;\r | |
1604 | VolatileVariableStore->Size = VARIABLE_STORE_SIZE;\r | |
1605 | VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r | |
1606 | VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r | |
1607 | VolatileVariableStore->Reserved = 0;\r | |
1608 | VolatileVariableStore->Reserved1 = 0;\r | |
1609 | \r | |
1610 | //\r | |
1611 | // Get non volatile varaible store\r | |
1612 | //\r | |
1613 | \r | |
1614 | TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);\r | |
052ad7e1 | 1615 | VariableStoreBase = TempVariableStoreHeader + \\r |
8d3a5c82 | 1616 | (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r |
052ad7e1 | 1617 | VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r |
8d3a5c82 | 1618 | (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r |
1619 | //\r | |
1620 | // Mark the variable storage region of the FLASH as RUNTIME\r | |
1621 | //\r | |
052ad7e1 A |
1622 | BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r |
1623 | Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r | |
8d3a5c82 | 1624 | Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r |
1625 | \r | |
1626 | Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r | |
1627 | if (EFI_ERROR (Status)) {\r | |
1628 | FreePool (mVariableModuleGlobal);\r | |
1629 | FreePool (VolatileVariableStore);\r | |
1630 | return EFI_UNSUPPORTED;\r | |
1631 | }\r | |
1632 | \r | |
1633 | Status = gDS->SetMemorySpaceAttributes (\r | |
1634 | BaseAddress,\r | |
1635 | Length,\r | |
1636 | GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r | |
1637 | );\r | |
1638 | if (EFI_ERROR (Status)) {\r | |
1639 | FreePool (mVariableModuleGlobal);\r | |
1640 | FreePool (VolatileVariableStore);\r | |
1641 | return EFI_UNSUPPORTED;\r | |
1642 | }\r | |
1643 | //\r | |
1644 | // Get address of non volatile variable store base\r | |
1645 | //\r | |
052ad7e1 | 1646 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r |
8d3a5c82 | 1647 | \r |
1648 | //\r | |
1649 | // Check Integrity\r | |
1650 | //\r | |
1651 | //\r | |
1652 | // Find the Correct Instance of the FV Block Service.\r | |
1653 | //\r | |
1654 | Instance = 0;\r | |
052ad7e1 | 1655 | CurrPtr = (CHAR8 *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r |
8d3a5c82 | 1656 | while (EfiFvbGetPhysicalAddress (Instance, &FvVolHdr) == EFI_SUCCESS) {\r |
1657 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r | |
1658 | if (CurrPtr >= (CHAR8 *) FwVolHeader && CurrPtr < (((CHAR8 *) FwVolHeader) + FwVolHeader->FvLength)) {\r | |
1659 | mVariableModuleGlobal->FvbInstance = Instance;\r | |
1660 | break;\r | |
1661 | }\r | |
1662 | \r | |
1663 | Instance++;\r | |
1664 | }\r | |
1665 | \r | |
1666 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;\r | |
1667 | if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r | |
1668 | if (~VariableStoreHeader->Size == 0) {\r | |
1669 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1670 | &mVariableModuleGlobal->VariableGlobal,\r |
8d3a5c82 | 1671 | FALSE,\r |
1672 | FALSE,\r | |
1673 | mVariableModuleGlobal->FvbInstance,\r | |
1674 | (UINTN) &VariableStoreHeader->Size,\r | |
1675 | sizeof (UINT32),\r | |
052ad7e1 | 1676 | (UINT8 *) &VariableStoreLength\r |
8d3a5c82 | 1677 | );\r |
1678 | //\r | |
1679 | // As Variables are stored in NV storage, which are slow devices,such as flash.\r | |
1680 | // Variable operation may skip checking variable program result to improve performance,\r | |
1681 | // We can assume Variable program is OK through some check point.\r | |
1682 | // Variable Store Size Setting should be the first Variable write operation,\r | |
1683 | // We can assume all Read/Write is OK if we can set Variable store size successfully.\r | |
1684 | // If write fail, we will assert here\r | |
1685 | //\r | |
052ad7e1 | 1686 | ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r |
8d3a5c82 | 1687 | \r |
1688 | if (EFI_ERROR (Status)) {\r | |
1689 | return Status;\r | |
1690 | }\r | |
1691 | }\r | |
1692 | \r | |
052ad7e1 | 1693 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);\r |
8d3a5c82 | 1694 | //\r |
1695 | // Parse non-volatile variable data and get last variable offset\r | |
1696 | //\r | |
1697 | NextVariable = (VARIABLE_HEADER *) (CurrPtr + sizeof (VARIABLE_STORE_HEADER));\r | |
1698 | Status = EFI_SUCCESS;\r | |
1699 | \r | |
1700 | while (IsValidVariableHeader (NextVariable)) {\r | |
1701 | NextVariable = GetNextVariablePtr (NextVariable);\r | |
1702 | }\r | |
1703 | \r | |
1704 | mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) CurrPtr;\r | |
1705 | \r | |
1706 | //\r | |
1707 | // Check if the free area is blow a threshold\r | |
1708 | //\r | |
1709 | if ((((VARIABLE_STORE_HEADER *)((UINTN) CurrPtr))->Size - mVariableModuleGlobal->NonVolatileLastVariableOffset) < VARIABLE_RECLAIM_THRESHOLD) {\r | |
1710 | Status = Reclaim (\r | |
052ad7e1 | 1711 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r |
8d3a5c82 | 1712 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r |
1713 | FALSE\r | |
1714 | );\r | |
1715 | }\r | |
1716 | \r | |
1717 | if (EFI_ERROR (Status)) {\r | |
1718 | FreePool (mVariableModuleGlobal);\r | |
1719 | FreePool (VolatileVariableStore);\r | |
1720 | return Status;\r | |
1721 | }\r | |
1722 | \r | |
1723 | //\r | |
1724 | // Check if the free area is really free.\r | |
1725 | //\r | |
1726 | for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r | |
052ad7e1 | 1727 | Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)[Index];\r |
8d3a5c82 | 1728 | if (Data != 0xff) {\r |
1729 | //\r | |
1730 | // There must be something wrong in variable store, do reclaim operation.\r | |
1731 | //\r | |
1732 | Status = Reclaim (\r | |
052ad7e1 | 1733 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r |
8d3a5c82 | 1734 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r |
1735 | FALSE\r | |
1736 | );\r | |
1737 | break;\r | |
1738 | }\r | |
1739 | }\r | |
1740 | }\r | |
1741 | \r | |
1742 | if (EFI_ERROR (Status)) {\r | |
1743 | FreePool (mVariableModuleGlobal);\r | |
1744 | FreePool (VolatileVariableStore);\r | |
1745 | }\r | |
1746 | \r | |
1747 | return Status;\r | |
1748 | }\r | |
052ad7e1 A |
1749 | \r |
1750 | \r | |
1751 | \r | |
1752 | \r | |
1753 | VOID\r | |
1754 | EFIAPI\r | |
1755 | VariableClassAddressChangeEvent (\r | |
1756 | IN EFI_EVENT Event,\r | |
1757 | IN VOID *Context\r | |
1758 | )\r | |
1759 | {\r | |
1760 | EfiConvertPointer (\r | |
1761 | 0x0,\r | |
1762 | (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase\r | |
1763 | );\r | |
1764 | EfiConvertPointer (\r | |
1765 | 0x0,\r | |
1766 | (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase\r | |
1767 | );\r | |
1768 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);\r | |
1769 | }\r | |
1770 | \r | |
1771 | \r | |
1772 | /**\r | |
1773 | Variable Driver main entry point. The Variable driver places the 4 EFI\r | |
1774 | runtime services in the EFI System Table and installs arch protocols \r | |
1775 | for variable read and write services being availible. \r | |
1776 | \r | |
1777 | @param[in] ImageHandle The firmware allocated handle for the EFI image. \r | |
1778 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
1779 | \r | |
1780 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
1781 | @retval other Some error occurs when executing this entry point.\r | |
1782 | \r | |
1783 | **/\r | |
1784 | EFI_STATUS\r | |
1785 | EFIAPI\r | |
1786 | VariableServiceInitialize (\r | |
1787 | IN EFI_HANDLE ImageHandle,\r | |
1788 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1789 | )\r | |
1790 | {\r | |
1791 | EFI_STATUS Status;\r | |
1792 | \r | |
1793 | Status = VariableCommonInitialize (ImageHandle, SystemTable);\r | |
1794 | ASSERT_EFI_ERROR (Status);\r | |
1795 | \r | |
1796 | SystemTable->RuntimeServices->GetVariable = RuntimeServiceGetVariable;\r | |
1797 | SystemTable->RuntimeServices->GetNextVariableName = RuntimeServiceGetNextVariableName;\r | |
1798 | SystemTable->RuntimeServices->SetVariable = RuntimeServiceSetVariable;\r | |
1799 | SystemTable->RuntimeServices->QueryVariableInfo = RuntimeServiceQueryVariableInfo;\r | |
1800 | \r | |
1801 | //\r | |
1802 | // Now install the Variable Runtime Architectural Protocol on a new handle\r | |
1803 | //\r | |
1804 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
1805 | &mHandle,\r | |
1806 | &gEfiVariableArchProtocolGuid, NULL,\r | |
1807 | &gEfiVariableWriteArchProtocolGuid, NULL,\r | |
1808 | NULL\r | |
1809 | );\r | |
1810 | ASSERT_EFI_ERROR (Status);\r | |
1811 | \r | |
1812 | Status = gBS->CreateEvent (\r | |
1813 | EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,\r | |
1814 | TPL_NOTIFY,\r | |
1815 | VariableClassAddressChangeEvent,\r | |
1816 | NULL,\r | |
1817 | &mVirtualAddressChangeEvent\r | |
1818 | );\r | |
1819 | ASSERT_EFI_ERROR (Status);\r | |
1820 | \r | |
1821 | return EFI_SUCCESS;\r | |
1822 | }\r | |
1823 | \r |