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