Fix a bug with changing the NVRAM properties of a Variable
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Variable.c
CommitLineData
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
19VARIABLE_MODULE_GLOBAL mRuntimeData;\r
20VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal = &mRuntimeData;\r
21EFI_EVENT mVirtualAddressChangeEvent = NULL;\r
22EFI_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 30VOID\r
31AcquireLockOnlyAtBootTime (\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 45VOID\r
46ReleaseLockOnlyAtBootTime (\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
56GLOBAL_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
79VOID\r
80UpdateVariableInfo (\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 156BOOLEAN\r
8d3a5c82 157IsValidVariableHeader (\r
158 IN VARIABLE_HEADER *Variable\r
159 )\r
160/*++\r
161\r
162Routine Description:\r
163\r
164 This code checks if variable header is valid or not.\r
165\r
166Arguments:\r
167 Variable Pointer to the Variable Header.\r
168\r
169Returns:\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 186EFI_STATUS\r
8d3a5c82 187UpdateVariableStore (\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
198Routine 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
203Arguments:\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
215Returns:\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 334VARIABLE_STORE_STATUS\r
8d3a5c82 335GetVariableStoreStatus (\r
336 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
337 )\r
338/*++\r
339\r
340Routine Description:\r
341\r
342 This code gets the current status of Variable Store.\r
343\r
344Arguments:\r
345\r
346 VarStoreHeader Pointer to the Variable Store Header.\r
347\r
348Returns:\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 375UINT8 *\r
8d3a5c82 376GetVariableDataPtr (\r
377 IN VARIABLE_HEADER *Variable\r
378 )\r
379/*++\r
380\r
381Routine Description:\r
382\r
383 This code gets the pointer to the variable data.\r
384\r
385Arguments:\r
386\r
387 Variable Pointer to the Variable Header.\r
388\r
389Returns:\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 402VARIABLE_HEADER *\r
8d3a5c82 403GetNextVariablePtr (\r
404 IN VARIABLE_HEADER *Variable\r
405 )\r
406/*++\r
407\r
408Routine Description:\r
409\r
410 This code gets the pointer to the next variable header.\r
411\r
412Arguments:\r
413\r
414 Variable Pointer to the Variable Header.\r
415\r
416Returns:\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 432VARIABLE_HEADER *\r
8d3a5c82 433GetEndPointer (\r
434 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
435 )\r
436/*++\r
437\r
438Routine Description:\r
439\r
440 This code gets the pointer to the last variable memory pointer byte\r
441\r
442Arguments:\r
443\r
444 VarStoreHeader Pointer to the Variable Store Header.\r
445\r
446Returns:\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 459EFI_STATUS\r
8d3a5c82 460Reclaim (\r
461 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
462 OUT UINTN *LastVariableOffset,\r
463 IN BOOLEAN IsVolatile\r
464 )\r
465/*++\r
466\r
467Routine Description:\r
468\r
469 Variable store garbage collection and reclaim operation\r
470\r
471Arguments:\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
478Returns:\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
581VARIABLE_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
603VOID\r
604UpdateVariableCache (\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
656EFI_STATUS\r
657FindVariableInCache (\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 699EFI_STATUS\r
8d3a5c82 700FindVariable (\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
708Routine Description:\r
709\r
710 This code finds variable in storage blocks (Volatile or Non-Volatile)\r
711\r
712Arguments:\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
721Returns:\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
791Routine Description:\r
792\r
793 This code finds variable in storage blocks (Volatile or Non-Volatile)\r
794\r
795Arguments:\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
806Returns:\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
815EFI_STATUS\r
816EFIAPI\r
817RuntimeServiceGetVariable (\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
875Done:\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
884Routine Description:\r
885\r
886 This code Finds the Next available variable\r
887\r
888Arguments:\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
896Returns:\r
897\r
898 EFI STATUS\r
899\r
900--*/\r
052ad7e1
A
901EFI_STATUS\r
902EFIAPI\r
903RuntimeServiceGetNextVariableName (\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
979Done:\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
987Routine Description:\r
988\r
989 This code sets variable in storage blocks (Volatile or Non-Volatile)\r
990\r
991Arguments:\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
1004Returns:\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
1014EFI_STATUS\r
1015EFIAPI\r
1016RuntimeServiceSetVariable (\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
fd51bf70 1036 BOOLEAN Volatile;\r
8d3a5c82 1037\r
052ad7e1
A
1038 Reclaimed = FALSE;\r
1039 VolatileOffset = &mVariableModuleGlobal->VolatileLastVariableOffset;\r
1040 NonVolatileOffset = &mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
1041 Instance = mVariableModuleGlobal->FvbInstance;\r
8d3a5c82 1042\r
c6492839 1043 //\r
1044 // Check input parameters\r
1045 //\r
8d3a5c82 1046 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
1047 return EFI_INVALID_PARAMETER;\r
c6492839 1048 } \r
1049 //\r
1050 // Make sure if runtime bit is set, boot service bit is set also\r
1051 //\r
1052 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
1053 return EFI_INVALID_PARAMETER;\r
8d3a5c82 1054 }\r
c6492839 1055 //\r
1056 // The size of the VariableName, including the Unicode Null in bytes plus\r
1057 // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)\r
1058 // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.\r
1059 //\r
1060 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1061 if ((DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE) || \r
1062 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE)) {\r
1063 return EFI_INVALID_PARAMETER;\r
1064 } \r
1065 } else {\r
1066 //\r
1067 // The size of the VariableName, including the Unicode Null in bytes plus\r
1068 // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.\r
1069 //\r
1070 if ((DataSize > MAX_VARIABLE_SIZE) ||\r
1071 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE)) {\r
1072 return EFI_INVALID_PARAMETER;\r
1073 } \r
1074 } \r
1075 //\r
1076 // Check whether the input variable is already existed\r
1077 //\r
1078 \r
052ad7e1 1079 Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r
c6492839 1080 if (Status == EFI_SUCCESS && Variable.CurrPtr != NULL) {\r
8d3a5c82 1081 //\r
c6492839 1082 // Update/Delete existing variable\r
8d3a5c82 1083 //\r
fd51bf70 1084 Volatile = Variable.Volatile;\r
c6492839 1085 \r
1086 if (EfiAtRuntime ()) { \r
1087 //\r
1088 // If EfiAtRuntime and the variable is Volatile and Runtime Access, \r
1089 // the volatile is ReadOnly, and SetVariable should be aborted and \r
1090 // return EFI_WRITE_PROTECTED.\r
1091 //\r
1092 if (Variable.Volatile) {\r
1093 Status = EFI_WRITE_PROTECTED;\r
1094 goto Done;\r
1095 }\r
1096 //\r
1097 // Only variable have NV attribute can be updated/deleted in Runtime\r
1098 //\r
1099 if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r
1100 Status = EFI_INVALID_PARAMETER;\r
1101 goto Done; \r
1102 }\r
1103 }\r
8d3a5c82 1104 //\r
c6492839 1105 // Setting a data variable with no access, or zero DataSize attributes\r
1106 // specified causes it to be deleted.\r
8d3a5c82 1107 //\r
c6492839 1108 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { \r
1109 State = Variable.CurrPtr->State;\r
1110 State &= VAR_DELETED;\r
1111\r
1112 Status = UpdateVariableStore (\r
052ad7e1 1113 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1114 Variable.Volatile,\r
1115 FALSE,\r
1116 Instance,\r
1117 (UINTN) &Variable.CurrPtr->State,\r
1118 sizeof (UINT8),\r
1119 &State\r
1120 ); \r
33a5a666 1121 if (!EFI_ERROR (Status)) {\r
fd51bf70 1122 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, FALSE, TRUE, FALSE);\r
33a5a666
A
1123 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
1124 }\r
c6492839 1125 goto Done; \r
1126 }\r
8d3a5c82 1127 //\r
c6492839 1128 // If the variable is marked valid and the same data has been passed in\r
1129 // then return to the caller immediately.\r
8d3a5c82 1130 //\r
c6492839 1131 if (Variable.CurrPtr->DataSize == DataSize &&\r
1132 (CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0)) {\r
33a5a666 1133 \r
fd51bf70 1134 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
c6492839 1135 Status = EFI_SUCCESS;\r
1136 goto Done;\r
1137 } else if ((Variable.CurrPtr->State == VAR_ADDED) ||\r
1138 (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
1139 //\r
1140 // Mark the old variable as in delete transition\r
1141 //\r
1142 State = Variable.CurrPtr->State;\r
1143 State &= VAR_IN_DELETED_TRANSITION;\r
1144\r
1145 Status = UpdateVariableStore (\r
052ad7e1 1146 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1147 Variable.Volatile,\r
1148 FALSE,\r
1149 Instance,\r
1150 (UINTN) &Variable.CurrPtr->State,\r
1151 sizeof (UINT8),\r
1152 &State\r
1153 ); \r
1154 if (EFI_ERROR (Status)) {\r
1155 goto Done; \r
33a5a666 1156 } \r
c6492839 1157 } \r
1158 } else if (Status == EFI_NOT_FOUND) {\r
8d3a5c82 1159 //\r
c6492839 1160 // Create a new variable\r
1161 // \r
1162 \r
8d3a5c82 1163 //\r
c6492839 1164 // Make sure we are trying to create a new variable.\r
1165 // Setting a data variable with no access, or zero DataSize attributes means to delete it. \r
8d3a5c82 1166 //\r
c6492839 1167 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
1168 Status = EFI_NOT_FOUND;\r
1169 goto Done;\r
1170 }\r
1171 \r
8d3a5c82 1172 //\r
c6492839 1173 // Only variable have NV|RT attribute can be created in Runtime\r
1174 //\r
1175 if (EfiAtRuntime () &&\r
1176 (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {\r
1177 Status = EFI_INVALID_PARAMETER;\r
1178 goto Done;\r
1179 } \r
1180 } else {\r
8d3a5c82 1181 //\r
c6492839 1182 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().\r
8d3a5c82 1183 //\r
c6492839 1184 ASSERT (Status == EFI_INVALID_PARAMETER);\r
8d3a5c82 1185 goto Done;\r
c6492839 1186 }\r
1187\r
1188 //\r
1189 // Function part - create a new variable and copy the data.\r
1190 // Both update a variable and create a variable will come here.\r
1191 //\r
1192 // Tricky part: Use scratch data area at the end of volatile variable store\r
1193 // as a temporary storage.\r
1194 //\r
052ad7e1 1195 NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
c6492839 1196\r
1197 SetMem (NextVariable, SCRATCH_SIZE, 0xff);\r
1198\r
1199 NextVariable->StartId = VARIABLE_DATA;\r
1200 NextVariable->Attributes = Attributes;\r
1201 //\r
1202 // NextVariable->State = VAR_ADDED;\r
1203 //\r
1204 NextVariable->Reserved = 0;\r
1205 VarNameOffset = sizeof (VARIABLE_HEADER);\r
1206 VarNameSize = StrSize (VariableName);\r
1207 CopyMem (\r
1208 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
1209 VariableName,\r
1210 VarNameSize\r
1211 );\r
1212 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
1213 CopyMem (\r
1214 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
1215 Data,\r
1216 DataSize\r
1217 );\r
1218 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
1219 //\r
1220 // There will be pad bytes after Data, the NextVariable->NameSize and\r
1221 // NextVariable->DataSize should not include pad size so that variable\r
1222 // service can get actual size in GetVariable\r
1223 //\r
1224 NextVariable->NameSize = (UINT32)VarNameSize;\r
1225 NextVariable->DataSize = (UINT32)DataSize;\r
1226\r
1227 //\r
1228 // The actual size of the variable that stores in storage should\r
1229 // include pad size.\r
1230 //\r
1231 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
1232 if (Attributes & EFI_VARIABLE_NON_VOLATILE) {\r
8d3a5c82 1233 //\r
c6492839 1234 // Create a nonvolatile variable\r
8d3a5c82 1235 //\r
fd51bf70 1236 Volatile = FALSE;\r
c6492839 1237 \r
1238 if ((UINT32) (VarSize +*NonVolatileOffset) >\r
052ad7e1 1239 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size\r
c6492839 1240 ) {\r
1241 if (EfiAtRuntime ()) {\r
1242 Status = EFI_OUT_OF_RESOURCES;\r
1243 goto Done;\r
1244 }\r
1245 //\r
1246 // Perform garbage collection & reclaim operation\r
1247 //\r
052ad7e1 1248 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, NonVolatileOffset, FALSE);\r
8d3a5c82 1249 if (EFI_ERROR (Status)) {\r
1250 goto Done;\r
1251 }\r
8d3a5c82 1252 //\r
c6492839 1253 // If still no enough space, return out of resources\r
8d3a5c82 1254 //\r
c6492839 1255 if ((UINT32) (VarSize +*NonVolatileOffset) >\r
052ad7e1 1256 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size\r
8d3a5c82 1257 ) {\r
c6492839 1258 Status = EFI_OUT_OF_RESOURCES;\r
8d3a5c82 1259 goto Done;\r
8d3a5c82 1260 }\r
c6492839 1261 \r
1262 Reclaimed = TRUE;\r
8d3a5c82 1263 }\r
1264 //\r
c6492839 1265 // Three steps\r
1266 // 1. Write variable header\r
1267 // 2. Write variable data\r
1268 // 3. Set variable state to valid\r
8d3a5c82 1269 //\r
8d3a5c82 1270 //\r
c6492839 1271 // Step 1:\r
8d3a5c82 1272 //\r
c6492839 1273 Status = UpdateVariableStore (\r
052ad7e1 1274 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1275 FALSE,\r
1276 TRUE,\r
1277 Instance,\r
1278 *NonVolatileOffset,\r
1279 sizeof (VARIABLE_HEADER),\r
1280 (UINT8 *) NextVariable\r
1281 );\r
1282\r
1283 if (EFI_ERROR (Status)) {\r
1284 goto Done;\r
1285 }\r
8d3a5c82 1286 //\r
c6492839 1287 // Step 2:\r
8d3a5c82 1288 //\r
c6492839 1289 Status = UpdateVariableStore (\r
052ad7e1 1290 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1291 FALSE,\r
1292 TRUE,\r
1293 Instance,\r
1294 *NonVolatileOffset + sizeof (VARIABLE_HEADER),\r
1295 (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
1296 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
1297 );\r
1298\r
1299 if (EFI_ERROR (Status)) {\r
1300 goto Done;\r
1301 }\r
8d3a5c82 1302 //\r
c6492839 1303 // Step 3:\r
8d3a5c82 1304 //\r
c6492839 1305 NextVariable->State = VAR_ADDED;\r
1306 Status = UpdateVariableStore (\r
052ad7e1 1307 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1308 FALSE,\r
1309 TRUE,\r
1310 Instance,\r
1311 *NonVolatileOffset,\r
1312 sizeof (VARIABLE_HEADER),\r
1313 (UINT8 *) NextVariable\r
1314 );\r
1315\r
1316 if (EFI_ERROR (Status)) {\r
1317 goto Done;\r
1318 }\r
8d3a5c82 1319\r
c6492839 1320 *NonVolatileOffset = *NonVolatileOffset + VarSize;\r
8d3a5c82 1321\r
c6492839 1322 } else {\r
1323 //\r
1324 // Create a volatile variable\r
1325 // \r
fd51bf70 1326 Volatile = TRUE;\r
c6492839 1327\r
1328 if ((UINT32) (VarSize +*VolatileOffset) >\r
052ad7e1 1329 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
8d3a5c82 1330 //\r
c6492839 1331 // Perform garbage collection & reclaim operation\r
8d3a5c82 1332 //\r
052ad7e1 1333 Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, VolatileOffset, TRUE);\r
8d3a5c82 1334 if (EFI_ERROR (Status)) {\r
1335 goto Done;\r
1336 }\r
1337 //\r
c6492839 1338 // If still no enough space, return out of resources\r
8d3a5c82 1339 //\r
8d3a5c82 1340 if ((UINT32) (VarSize +*VolatileOffset) >\r
052ad7e1 1341 ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size\r
8d3a5c82 1342 ) {\r
c6492839 1343 Status = EFI_OUT_OF_RESOURCES;\r
8d3a5c82 1344 goto Done;\r
1345 }\r
c6492839 1346 \r
1347 Reclaimed = TRUE;\r
8d3a5c82 1348 }\r
8d3a5c82 1349\r
c6492839 1350 NextVariable->State = VAR_ADDED;\r
1351 Status = UpdateVariableStore (\r
052ad7e1 1352 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1353 TRUE,\r
1354 TRUE,\r
1355 Instance,\r
1356 *VolatileOffset,\r
1357 (UINT32) VarSize,\r
1358 (UINT8 *) NextVariable\r
1359 );\r
1360\r
1361 if (EFI_ERROR (Status)) {\r
1362 goto Done;\r
8d3a5c82 1363 }\r
c6492839 1364\r
1365 *VolatileOffset = *VolatileOffset + VarSize;\r
1366 }\r
1367 //\r
1368 // Mark the old variable as deleted\r
1369 //\r
1370 if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {\r
1371 State = Variable.CurrPtr->State;\r
1372 State &= VAR_DELETED;\r
1373\r
1374 Status = UpdateVariableStore (\r
052ad7e1 1375 &mVariableModuleGlobal->VariableGlobal,\r
c6492839 1376 Variable.Volatile,\r
1377 FALSE,\r
1378 Instance,\r
1379 (UINTN) &Variable.CurrPtr->State,\r
1380 sizeof (UINT8),\r
1381 &State\r
1382 );\r
33a5a666
A
1383 \r
1384 if (!EFI_ERROR (Status)) {\r
fd51bf70 1385 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
33a5a666
A
1386 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
1387 }\r
c6492839 1388 goto Done; \r
8d3a5c82 1389 }\r
1390\r
1391 Status = EFI_SUCCESS;\r
fd51bf70 1392 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
33a5a666
A
1393 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
1394\r
8d3a5c82 1395Done:\r
052ad7e1 1396 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 1397 return Status;\r
1398}\r
1399\r
052ad7e1 1400\r
8d3a5c82 1401/*++\r
1402\r
1403Routine Description:\r
1404\r
1405 This code returns information about the EFI variables.\r
1406\r
1407Arguments:\r
1408\r
1409 Attributes Attributes bitmask to specify the type of variables\r
1410 on which to return information.\r
1411 MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r
1412 for the EFI variables associated with the attributes specified.\r
1413 RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
c6492839 1414 for EFI variables associated with the attributes specified.\r
1415 MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r
8d3a5c82 1416 associated with the attributes specified.\r
1417 Global Pointer to VARIABLE_GLOBAL structure.\r
1418 Instance Instance of the Firmware Volume.\r
1419\r
1420Returns:\r
1421\r
1422 EFI STATUS\r
1423 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.\r
1424 EFI_SUCCESS - Query successfully.\r
1425 EFI_UNSUPPORTED - The attribute is not supported on this platform.\r
1426\r
1427--*/\r
052ad7e1
A
1428EFI_STATUS\r
1429EFIAPI\r
1430RuntimeServiceQueryVariableInfo (\r
1431 IN UINT32 Attributes,\r
1432 OUT UINT64 *MaximumVariableStorageSize,\r
1433 OUT UINT64 *RemainingVariableStorageSize,\r
1434 OUT UINT64 *MaximumVariableSize\r
1435 )\r
8d3a5c82 1436{\r
1437 VARIABLE_HEADER *Variable;\r
1438 VARIABLE_HEADER *NextVariable;\r
1439 UINT64 VariableSize;\r
1440 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1441\r
c6492839 1442 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
8d3a5c82 1443 return EFI_INVALID_PARAMETER;\r
1444 }\r
c6492839 1445 \r
1446 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
8d3a5c82 1447 //\r
1448 // Make sure the Attributes combination is supported by the platform.\r
1449 //\r
c6492839 1450 return EFI_UNSUPPORTED; \r
8d3a5c82 1451 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
1452 //\r
1453 // Make sure if runtime bit is set, boot service bit is set also.\r
1454 //\r
1455 return EFI_INVALID_PARAMETER;\r
1456 } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
1457 //\r
1458 // Make sure RT Attribute is set if we are in Runtime phase.\r
1459 //\r
1460 return EFI_INVALID_PARAMETER;\r
1461 }\r
1462\r
052ad7e1 1463 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 1464\r
1465 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
1466 //\r
1467 // Query is Volatile related.\r
1468 //\r
052ad7e1 1469 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 1470 } else {\r
1471 //\r
1472 // Query is Non-Volatile related.\r
1473 //\r
052ad7e1 1474 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
8d3a5c82 1475 }\r
1476\r
1477 //\r
1478 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
1479 // with the storage size (excluding the storage header size).\r
1480 //\r
1481 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
1482 *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
1483\r
1484 //\r
c6492839 1485 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.\r
8d3a5c82 1486 //\r
c6492839 1487 *MaximumVariableSize = MAX_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r
1488\r
1489 //\r
1490 // Harware error record variable needs larger size.\r
1491 //\r
1492 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1493 *MaximumVariableSize = MAX_HARDWARE_ERROR_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r
1494 }\r
8d3a5c82 1495\r
1496 //\r
1497 // Point to the starting address of the variables.\r
1498 //\r
1499 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
1500\r
1501 //\r
1502 // Now walk through the related variable store.\r
1503 //\r
1504 while (IsValidVariableHeader (Variable) && (Variable < GetEndPointer (VariableStoreHeader))) {\r
1505 NextVariable = GetNextVariablePtr (Variable);\r
1506 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
1507\r
1508 if (EfiAtRuntime ()) {\r
1509 //\r
1510 // we don't take the state of the variables in mind\r
1511 // when calculating RemainingVariableStorageSize,\r
1512 // since the space occupied by variables not marked with\r
1513 // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
1514 //\r
1515 *RemainingVariableStorageSize -= VariableSize;\r
1516 } else {\r
1517 //\r
1518 // Only care about Variables with State VAR_ADDED,because\r
1519 // the space not marked as VAR_ADDED is reclaimable now.\r
1520 //\r
1521 if (Variable->State == VAR_ADDED) {\r
1522 *RemainingVariableStorageSize -= VariableSize;\r
1523 }\r
1524 }\r
1525\r
1526 //\r
1527 // Go to the next one\r
1528 //\r
1529 Variable = NextVariable;\r
1530 }\r
1531\r
c6492839 1532 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
1533 *MaximumVariableSize = 0;\r
1534 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
1535 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
1536 }\r
1537\r
052ad7e1 1538 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
8d3a5c82 1539 return EFI_SUCCESS;\r
1540}\r
1541\r
1542EFI_STATUS\r
8d3a5c82 1543VariableCommonInitialize (\r
1544 IN EFI_HANDLE ImageHandle,\r
1545 IN EFI_SYSTEM_TABLE *SystemTable\r
1546 )\r
1547/*++\r
1548\r
1549Routine Description:\r
1550 This function does common initialization for variable services\r
1551\r
1552Arguments:\r
1553\r
1554 ImageHandle - The firmware allocated handle for the EFI image.\r
1555 SystemTable - A pointer to the EFI System Table.\r
1556\r
1557Returns:\r
1558\r
1559 Status code.\r
1560\r
1561 EFI_NOT_FOUND - Variable store area not found.\r
1562 EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.\r
1563 EFI_SUCCESS - Variable services successfully initialized.\r
1564\r
1565--*/\r
1566{\r
1567 EFI_STATUS Status;\r
1568 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
1569 CHAR8 *CurrPtr;\r
1570 VARIABLE_STORE_HEADER *VolatileVariableStore;\r
1571 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
1572 VARIABLE_HEADER *NextVariable;\r
1573 UINT32 Instance;\r
1574 EFI_PHYSICAL_ADDRESS FvVolHdr;\r
8d3a5c82 1575 UINT64 TempVariableStoreHeader;\r
8d3a5c82 1576 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
8d3a5c82 1577 UINT64 BaseAddress;\r
1578 UINT64 Length;\r
1579 UINTN Index;\r
1580 UINT8 Data;\r
052ad7e1
A
1581 UINT64 VariableStoreBase;\r
1582 UINT64 VariableStoreLength;\r
8d3a5c82 1583\r
8d3a5c82 1584\r
052ad7e1 1585 EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
8d3a5c82 1586\r
1587 //\r
1588 // Allocate memory for volatile variable store\r
1589 //\r
1590 VolatileVariableStore = AllocateRuntimePool (VARIABLE_STORE_SIZE + SCRATCH_SIZE);\r
1591 if (VolatileVariableStore == NULL) {\r
1592 FreePool (mVariableModuleGlobal);\r
1593 return EFI_OUT_OF_RESOURCES;\r
1594 }\r
1595\r
1596 SetMem (VolatileVariableStore, VARIABLE_STORE_SIZE + SCRATCH_SIZE, 0xff);\r
1597\r
1598 //\r
1599 // Variable Specific Data\r
1600 //\r
052ad7e1 1601 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
8d3a5c82 1602 mVariableModuleGlobal->VolatileLastVariableOffset = sizeof (VARIABLE_STORE_HEADER);\r
1603\r
1604 VolatileVariableStore->Signature = VARIABLE_STORE_SIGNATURE;\r
1605 VolatileVariableStore->Size = VARIABLE_STORE_SIZE;\r
1606 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
1607 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
1608 VolatileVariableStore->Reserved = 0;\r
1609 VolatileVariableStore->Reserved1 = 0;\r
1610\r
1611 //\r
1612 // Get non volatile varaible store\r
1613 //\r
1614\r
1615 TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);\r
052ad7e1 1616 VariableStoreBase = TempVariableStoreHeader + \\r
8d3a5c82 1617 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
052ad7e1 1618 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
8d3a5c82 1619 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
1620 //\r
1621 // Mark the variable storage region of the FLASH as RUNTIME\r
1622 //\r
052ad7e1
A
1623 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
1624 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
8d3a5c82 1625 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
1626\r
1627 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
1628 if (EFI_ERROR (Status)) {\r
1629 FreePool (mVariableModuleGlobal);\r
1630 FreePool (VolatileVariableStore);\r
1631 return EFI_UNSUPPORTED;\r
1632 }\r
1633\r
1634 Status = gDS->SetMemorySpaceAttributes (\r
1635 BaseAddress,\r
1636 Length,\r
1637 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
1638 );\r
1639 if (EFI_ERROR (Status)) {\r
1640 FreePool (mVariableModuleGlobal);\r
1641 FreePool (VolatileVariableStore);\r
1642 return EFI_UNSUPPORTED;\r
1643 }\r
1644 //\r
1645 // Get address of non volatile variable store base\r
1646 //\r
052ad7e1 1647 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
8d3a5c82 1648\r
1649 //\r
1650 // Check Integrity\r
1651 //\r
1652 //\r
1653 // Find the Correct Instance of the FV Block Service.\r
1654 //\r
1655 Instance = 0;\r
052ad7e1 1656 CurrPtr = (CHAR8 *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
8d3a5c82 1657 while (EfiFvbGetPhysicalAddress (Instance, &FvVolHdr) == EFI_SUCCESS) {\r
1658 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
1659 if (CurrPtr >= (CHAR8 *) FwVolHeader && CurrPtr < (((CHAR8 *) FwVolHeader) + FwVolHeader->FvLength)) {\r
1660 mVariableModuleGlobal->FvbInstance = Instance;\r
1661 break;\r
1662 }\r
1663\r
1664 Instance++;\r
1665 }\r
1666\r
1667 VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;\r
1668 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
1669 if (~VariableStoreHeader->Size == 0) {\r
1670 Status = UpdateVariableStore (\r
052ad7e1 1671 &mVariableModuleGlobal->VariableGlobal,\r
8d3a5c82 1672 FALSE,\r
1673 FALSE,\r
1674 mVariableModuleGlobal->FvbInstance,\r
1675 (UINTN) &VariableStoreHeader->Size,\r
1676 sizeof (UINT32),\r
052ad7e1 1677 (UINT8 *) &VariableStoreLength\r
8d3a5c82 1678 );\r
1679 //\r
1680 // As Variables are stored in NV storage, which are slow devices,such as flash.\r
1681 // Variable operation may skip checking variable program result to improve performance,\r
1682 // We can assume Variable program is OK through some check point.\r
1683 // Variable Store Size Setting should be the first Variable write operation,\r
1684 // We can assume all Read/Write is OK if we can set Variable store size successfully.\r
1685 // If write fail, we will assert here\r
1686 //\r
052ad7e1 1687 ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r
8d3a5c82 1688\r
1689 if (EFI_ERROR (Status)) {\r
1690 return Status;\r
1691 }\r
1692 }\r
1693\r
052ad7e1 1694 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);\r
8d3a5c82 1695 //\r
1696 // Parse non-volatile variable data and get last variable offset\r
1697 //\r
1698 NextVariable = (VARIABLE_HEADER *) (CurrPtr + sizeof (VARIABLE_STORE_HEADER));\r
1699 Status = EFI_SUCCESS;\r
1700\r
1701 while (IsValidVariableHeader (NextVariable)) {\r
1702 NextVariable = GetNextVariablePtr (NextVariable);\r
1703 }\r
1704\r
1705 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) CurrPtr;\r
1706\r
1707 //\r
1708 // Check if the free area is blow a threshold\r
1709 //\r
1710 if ((((VARIABLE_STORE_HEADER *)((UINTN) CurrPtr))->Size - mVariableModuleGlobal->NonVolatileLastVariableOffset) < VARIABLE_RECLAIM_THRESHOLD) {\r
1711 Status = Reclaim (\r
052ad7e1 1712 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
8d3a5c82 1713 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
1714 FALSE\r
1715 );\r
1716 }\r
1717\r
1718 if (EFI_ERROR (Status)) {\r
1719 FreePool (mVariableModuleGlobal);\r
1720 FreePool (VolatileVariableStore);\r
1721 return Status;\r
1722 }\r
1723\r
1724 //\r
1725 // Check if the free area is really free.\r
1726 //\r
1727 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
052ad7e1 1728 Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)[Index];\r
8d3a5c82 1729 if (Data != 0xff) {\r
1730 //\r
1731 // There must be something wrong in variable store, do reclaim operation.\r
1732 //\r
1733 Status = Reclaim (\r
052ad7e1 1734 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
8d3a5c82 1735 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
1736 FALSE\r
1737 );\r
1738 break;\r
1739 }\r
1740 }\r
1741 }\r
1742\r
1743 if (EFI_ERROR (Status)) {\r
1744 FreePool (mVariableModuleGlobal);\r
1745 FreePool (VolatileVariableStore);\r
1746 }\r
1747\r
1748 return Status;\r
1749}\r
052ad7e1
A
1750\r
1751\r
1752\r
1753\r
1754VOID\r
1755EFIAPI\r
1756VariableClassAddressChangeEvent (\r
1757 IN EFI_EVENT Event,\r
1758 IN VOID *Context\r
1759 )\r
1760{\r
1761 EfiConvertPointer (\r
1762 0x0,\r
1763 (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase\r
1764 );\r
1765 EfiConvertPointer (\r
1766 0x0,\r
1767 (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase\r
1768 );\r
1769 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);\r
1770}\r
1771\r
1772\r
1773/**\r
1774 Variable Driver main entry point. The Variable driver places the 4 EFI\r
1775 runtime services in the EFI System Table and installs arch protocols \r
1776 for variable read and write services being availible. \r
1777\r
1778 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
1779 @param[in] SystemTable A pointer to the EFI System Table.\r
1780 \r
1781 @retval EFI_SUCCESS The entry point is executed successfully.\r
1782 @retval other Some error occurs when executing this entry point.\r
1783\r
1784**/\r
1785EFI_STATUS\r
1786EFIAPI\r
1787VariableServiceInitialize (\r
1788 IN EFI_HANDLE ImageHandle,\r
1789 IN EFI_SYSTEM_TABLE *SystemTable\r
1790 )\r
1791{\r
1792 EFI_STATUS Status;\r
1793\r
1794 Status = VariableCommonInitialize (ImageHandle, SystemTable);\r
1795 ASSERT_EFI_ERROR (Status);\r
1796\r
1797 SystemTable->RuntimeServices->GetVariable = RuntimeServiceGetVariable;\r
1798 SystemTable->RuntimeServices->GetNextVariableName = RuntimeServiceGetNextVariableName;\r
1799 SystemTable->RuntimeServices->SetVariable = RuntimeServiceSetVariable;\r
1800 SystemTable->RuntimeServices->QueryVariableInfo = RuntimeServiceQueryVariableInfo;\r
1801\r
1802 //\r
1803 // Now install the Variable Runtime Architectural Protocol on a new handle\r
1804 //\r
1805 Status = gBS->InstallMultipleProtocolInterfaces (\r
1806 &mHandle,\r
1807 &gEfiVariableArchProtocolGuid, NULL,\r
1808 &gEfiVariableWriteArchProtocolGuid, NULL,\r
1809 NULL\r
1810 );\r
1811 ASSERT_EFI_ERROR (Status);\r
1812\r
1813 Status = gBS->CreateEvent (\r
1814 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,\r
1815 TPL_NOTIFY,\r
1816 VariableClassAddressChangeEvent,\r
1817 NULL,\r
1818 &mVirtualAddressChangeEvent\r
1819 );\r
1820 ASSERT_EFI_ERROR (Status);\r
1821\r
1822 return EFI_SUCCESS;\r
1823}\r
1824\r