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