]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
Adjust gSimpleTextInExNotifyGuid position in INF file.
[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
504214c4
LG
6Copyright (c) 2006 - 2008, Intel Corporation \r
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
45f6c85b 41EFI_STATUS\r
42FtwVariableSpace (\r
43 IN EFI_PHYSICAL_ADDRESS VariableBaseAddress,\r
44 IN UINT8 *Buffer,\r
45 IN UINTN BufferSize\r
46 );\r
aa79b0b3 47\r
33a5a666 48\r
7c80e839 49/**\r
50 Acquires lock only at boot time. Simply returns at runtime.\r
51\r
52 This is a temperary function which will be removed when\r
53 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
54 Runtimer driver in RT phase.\r
55 It calls EfiAcquireLock() at boot time, and simply returns\r
56 at runtime.\r
57\r
58 @param Lock A pointer to the lock to acquire\r
59\r
60**/\r
8d3a5c82 61VOID\r
62AcquireLockOnlyAtBootTime (\r
63 IN EFI_LOCK *Lock\r
64 )\r
65{\r
66 if (!EfiAtRuntime ()) {\r
67 EfiAcquireLock (Lock);\r
68 }\r
69}\r
70\r
7c80e839 71/**\r
72 Releases lock only at boot time. Simply returns at runtime.\r
73\r
74 This is a temperary function which will be removed when\r
75 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
76 Runtimer driver in RT phase.\r
77 It calls EfiReleaseLock() at boot time, and simply returns\r
78 at runtime.\r
79\r
80 @param Lock A pointer to the lock to release\r
81\r
82**/\r
8d3a5c82 83VOID\r
84ReleaseLockOnlyAtBootTime (\r
85 IN EFI_LOCK *Lock\r
86 )\r
87{\r
88 if (!EfiAtRuntime ()) {\r
89 EfiReleaseLock (Lock);\r
90 }\r
91}\r
92\r
33a5a666 93\r
052ad7e1
A
94/**\r
95 Routine used to track statistical information about variable usage. \r
96 The data is stored in the EFI system table so it can be accessed later.\r
97 VariableInfo.efi can dump out the table. Only Boot Services variable \r
98 accesses are tracked by this code. The PcdVariableCollectStatistics\r
99 build flag controls if this feature is enabled. \r
100\r
101 A read that hits in the cache will have Read and Cache true for \r
102 the transaction. Data is allocated by this routine, but never\r
103 freed.\r
104\r
105 @param[in] VariableName Name of the Variable to track\r
106 @param[in] VendorGuid Guid of the Variable to track\r
107 @param[in] Volatile TRUE if volatile FALSE if non-volatile\r
108 @param[in] Read TRUE if GetVariable() was called\r
109 @param[in] Write TRUE if SetVariable() was called\r
110 @param[in] Delete TRUE if deleted via SetVariable()\r
111 @param[in] Cache TRUE for a cache hit.\r
112\r
113**/\r
33a5a666
A
114VOID\r
115UpdateVariableInfo (\r
116 IN CHAR16 *VariableName,\r
117 IN EFI_GUID *VendorGuid,\r
118 IN BOOLEAN Volatile,\r
119 IN BOOLEAN Read,\r
120 IN BOOLEAN Write,\r
121 IN BOOLEAN Delete,\r
122 IN BOOLEAN Cache\r
123 )\r
124{\r
125 VARIABLE_INFO_ENTRY *Entry;\r
126\r
127 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
128\r
129 if (EfiAtRuntime ()) {\r
130 // Don't collect statistics at runtime\r
131 return;\r
132 }\r
133\r
134 if (gVariableInfo == NULL) {\r
052ad7e1
A
135 //\r
136 // on the first call allocate a entry and place a pointer to it in\r
137 // the EFI System Table\r
138 //\r
33a5a666 139 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
052ad7e1
A
140 ASSERT (gVariableInfo != NULL);\r
141\r
33a5a666
A
142 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
143 gVariableInfo->Name = AllocatePool (StrLen (VariableName));\r
144 StrCpy (gVariableInfo->Name, VariableName);\r
145 gVariableInfo->Volatile = Volatile;\r
146\r
147 gBS->InstallConfigurationTable (&gEfiVariableInfoGuid, gVariableInfo);\r
148 }\r
149\r
150 \r
151 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
152 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
153 if (StrCmp (VariableName, Entry->Name) == 0) {\r
154 if (Read) {\r
155 Entry->ReadCount++;\r
156 }\r
157 if (Write) {\r
158 Entry->WriteCount++;\r
159 }\r
160 if (Delete) {\r
161 Entry->DeleteCount++;\r
162 }\r
163 if (Cache) {\r
164 Entry->CacheCount++;\r
165 }\r
166\r
167 return;\r
168 }\r
169 }\r
170\r
171 if (Entry->Next == NULL) {\r
052ad7e1
A
172 //\r
173 // If the entry is not in the table add it.\r
174 // Next iteration of the loop will fill in the data\r
175 //\r
33a5a666 176 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
052ad7e1 177 ASSERT (Entry->Next != NULL);\r
33a5a666
A
178\r
179 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
180 Entry->Next->Name = AllocatePool (StrLen (VariableName));\r
181 StrCpy (Entry->Next->Name, VariableName);\r
182 Entry->Next->Volatile = Volatile;\r
183 }\r
184\r
185 }\r
186 }\r
187}\r
188\r
189\r
7c80e839 190/**\r
8d3a5c82 191\r
192 This code checks if variable header is valid or not.\r
193\r
7c80e839 194 @param Variable Pointer to the Variable Header.\r
8d3a5c82 195\r
7c80e839 196 @retval TRUE Variable header is valid.\r
197 @retval FALSE Variable header is not valid.\r
8d3a5c82 198\r
7c80e839 199**/\r
200BOOLEAN\r
201IsValidVariableHeader (\r
202 IN VARIABLE_HEADER *Variable\r
203 )\r
8d3a5c82 204{\r
fdb7765f 205 if (Variable == NULL || Variable->StartId != VARIABLE_DATA) {\r
8d3a5c82 206 return FALSE;\r
207 }\r
208\r
209 return TRUE;\r
210}\r
211\r
052ad7e1 212\r
7c80e839 213/**\r
214\r
215 This function writes data to the FWH at the correct LBA even if the LBAs\r
216 are fragmented.\r
217\r
218 @param Global Pointer to VARAIBLE_GLOBAL structure\r
219 @param Volatile Point out the Variable is Volatile or Non-Volatile\r
220 @param SetByIndex TRUE if target pointer is given as index\r
221 FALSE if target pointer is absolute\r
222 @param Instance Instance of FV Block services\r
223 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER\r
224 structure\r
225 @param DataSize Size of data to be written\r
226 @param Buffer Pointer to the buffer from which data is written\r
227\r
228 @retval EFI_INVALID_PARAMETER Parameters not valid\r
229 @retval EFI_SUCCESS Variable store successfully updated\r
230\r
231**/\r
8d3a5c82 232EFI_STATUS\r
8d3a5c82 233UpdateVariableStore (\r
234 IN VARIABLE_GLOBAL *Global,\r
235 IN BOOLEAN Volatile,\r
236 IN BOOLEAN SetByIndex,\r
237 IN UINTN Instance,\r
238 IN UINTN DataPtrIndex,\r
239 IN UINT32 DataSize,\r
240 IN UINT8 *Buffer\r
241 )\r
8d3a5c82 242{\r
243 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r
244 UINTN BlockIndex2;\r
245 UINTN LinearOffset;\r
246 UINTN CurrWriteSize;\r
247 UINTN CurrWritePtr;\r
248 UINT8 *CurrBuffer;\r
249 EFI_LBA LbaNumber;\r
250 UINTN Size;\r
251 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
252 VARIABLE_STORE_HEADER *VolatileBase;\r
253 EFI_PHYSICAL_ADDRESS FvVolHdr;\r
254 EFI_PHYSICAL_ADDRESS DataPtr;\r
255 EFI_STATUS Status;\r
256\r
257 FwVolHeader = NULL;\r
258 DataPtr = DataPtrIndex;\r
259\r
260 //\r
261 // Check if the Data is Volatile\r
262 //\r
263 if (!Volatile) {\r
264 EfiFvbGetPhysicalAddress (Instance, &FvVolHdr);\r
265 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
266 //\r
267 // Data Pointer should point to the actual Address where data is to be\r
268 // written\r
269 //\r
270 if (SetByIndex) {\r
052ad7e1 271 DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
8d3a5c82 272 }\r
273\r
274 if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r
275 return EFI_INVALID_PARAMETER;\r
276 }\r
277 } else {\r
278 //\r
279 // Data Pointer should point to the actual Address where data is to be\r
280 // written\r
281 //\r
052ad7e1 282 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
8d3a5c82 283 if (SetByIndex) {\r
052ad7e1 284 DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
8d3a5c82 285 }\r
286\r
287 if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
288 return EFI_INVALID_PARAMETER;\r
289 }\r
c6492839 290 \r
291 //\r
292 // If Volatile Variable just do a simple mem copy.\r
293 // \r
294 CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);\r
8d3a5c82 295 return EFI_SUCCESS;\r
296 }\r
c6492839 297 \r
8d3a5c82 298 //\r
299 // If we are here we are dealing with Non-Volatile Variables\r
300 //\r
301 LinearOffset = (UINTN) FwVolHeader;\r
302 CurrWritePtr = (UINTN) DataPtr;\r
303 CurrWriteSize = DataSize;\r
304 CurrBuffer = Buffer;\r
305 LbaNumber = 0;\r
306\r
307 if (CurrWritePtr < LinearOffset) {\r
308 return EFI_INVALID_PARAMETER;\r
309 }\r
310\r
311 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
312 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r
313 //\r
314 // Check to see if the Variable Writes are spanning through multiple\r
315 // blocks.\r
316 //\r
317 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r
318 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r
319 Status = EfiFvbWriteBlock (\r
320 Instance,\r
321 LbaNumber,\r
322 (UINTN) (CurrWritePtr - LinearOffset),\r
323 &CurrWriteSize,\r
324 CurrBuffer\r
325 );\r
8d3a5c82 326 return Status;\r
8d3a5c82 327 } else {\r
328 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r
329 Status = EfiFvbWriteBlock (\r
330 Instance,\r
331 LbaNumber,\r
332 (UINTN) (CurrWritePtr - LinearOffset),\r
333 &Size,\r
334 CurrBuffer\r
335 );\r
336 if (EFI_ERROR (Status)) {\r
337 return Status;\r
338 }\r
339\r
340 CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r
341 CurrBuffer = CurrBuffer + Size;\r
342 CurrWriteSize = CurrWriteSize - Size;\r
343 }\r
344 }\r
345\r
346 LinearOffset += PtrBlockMapEntry->Length;\r
347 LbaNumber++;\r
348 }\r
349 }\r
350\r
351 return EFI_SUCCESS;\r
352}\r
353\r
052ad7e1 354\r
7c80e839 355/**\r
8d3a5c82 356\r
357 This code gets the current status of Variable Store.\r
358\r
7c80e839 359 @param VarStoreHeader Pointer to the Variable Store Header.\r
8d3a5c82 360\r
7c80e839 361 @retval EfiRaw Variable store status is raw\r
362 @retval EfiValid Variable store status is valid\r
363 @retval EfiInvalid Variable store status is invalid\r
8d3a5c82 364\r
7c80e839 365**/\r
366VARIABLE_STORE_STATUS\r
367GetVariableStoreStatus (\r
368 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
369 )\r
8d3a5c82 370{\r
371 if (VarStoreHeader->Signature == VARIABLE_STORE_SIGNATURE &&\r
372 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
373 VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
374 ) {\r
375\r
376 return EfiValid;\r
377 } else if (VarStoreHeader->Signature == 0xffffffff &&\r
378 VarStoreHeader->Size == 0xffffffff &&\r
379 VarStoreHeader->Format == 0xff &&\r
380 VarStoreHeader->State == 0xff\r
381 ) {\r
382\r
383 return EfiRaw;\r
384 } else {\r
385 return EfiInvalid;\r
386 }\r
387}\r
388\r
130e2569 389\r
7c80e839 390/**\r
130e2569 391\r
392 This code gets the size of name of variable.\r
393\r
7c80e839 394 @param Variable Pointer to the Variable Header\r
130e2569 395\r
7c80e839 396 @return UINTN Size of variable in bytes\r
130e2569 397\r
7c80e839 398**/\r
399UINTN\r
400NameSizeOfVariable (\r
401 IN VARIABLE_HEADER *Variable\r
402 )\r
130e2569 403{\r
404 if (Variable->State == (UINT8) (-1) ||\r
7c80e839 405 Variable->DataSize == (UINT32) (-1) ||\r
406 Variable->NameSize == (UINT32) (-1) ||\r
407 Variable->Attributes == (UINT32) (-1)) {\r
130e2569 408 return 0;\r
409 }\r
410 return (UINTN) Variable->NameSize;\r
411}\r
412\r
7c80e839 413/**\r
130e2569 414\r
7c80e839 415 This code gets the size of variable data.\r
130e2569 416\r
7c80e839 417 @param Variable Pointer to the Variable Header\r
130e2569 418\r
7c80e839 419 @return Size of variable in bytes\r
130e2569 420\r
7c80e839 421**/\r
422UINTN\r
423DataSizeOfVariable (\r
424 IN VARIABLE_HEADER *Variable\r
425 )\r
130e2569 426{\r
7c80e839 427 if (Variable->State == (UINT8) (-1) ||\r
428 Variable->DataSize == (UINT32) (-1) ||\r
429 Variable->NameSize == (UINT32) (-1) ||\r
430 Variable->Attributes == (UINT32) (-1)) {\r
130e2569 431 return 0;\r
432 }\r
433 return (UINTN) Variable->DataSize;\r
434}\r
435\r
7c80e839 436/**\r
130e2569 437\r
438 This code gets the pointer to the variable name.\r
439\r
7c80e839 440 @param Variable Pointer to the Variable Header\r
130e2569 441\r
7c80e839 442 @return Pointer to Variable Name which is Unicode encoding\r
130e2569 443\r
7c80e839 444**/\r
445CHAR16 *\r
446GetVariableNamePtr (\r
447 IN VARIABLE_HEADER *Variable\r
448 )\r
130e2569 449{\r
450\r
451 return (CHAR16 *) (Variable + 1);\r
452}\r
453\r
7c80e839 454/**\r
8d3a5c82 455\r
456 This code gets the pointer to the variable data.\r
457\r
7c80e839 458 @param Variable Pointer to the Variable Header\r
8d3a5c82 459\r
7c80e839 460 @return Pointer to Variable Data\r
8d3a5c82 461\r
7c80e839 462**/\r
463UINT8 *\r
464GetVariableDataPtr (\r
465 IN VARIABLE_HEADER *Variable\r
466 )\r
8d3a5c82 467{\r
130e2569 468 UINTN Value;\r
469 \r
8d3a5c82 470 //\r
471 // Be careful about pad size for alignment\r
472 //\r
130e2569 473 Value = (UINTN) GetVariableNamePtr (Variable);\r
474 Value += NameSizeOfVariable (Variable);\r
475 Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r
476\r
477 return (UINT8 *) Value;\r
8d3a5c82 478}\r
479\r
052ad7e1 480\r
7c80e839 481/**\r
8d3a5c82 482\r
483 This code gets the pointer to the next variable header.\r
484\r
7c80e839 485 @param Variable Pointer to the Variable Header\r
8d3a5c82 486\r
7c80e839 487 @return Pointer to next variable header\r
8d3a5c82 488\r
7c80e839 489**/\r
490VARIABLE_HEADER *\r
491GetNextVariablePtr (\r
492 IN VARIABLE_HEADER *Variable\r
493 )\r
8d3a5c82 494{\r
130e2569 495 UINTN Value;\r
496\r
8d3a5c82 497 if (!IsValidVariableHeader (Variable)) {\r
498 return NULL;\r
499 }\r
130e2569 500\r
501 Value = (UINTN) GetVariableDataPtr (Variable);\r
502 Value += DataSizeOfVariable (Variable);\r
503 Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r
504\r
8d3a5c82 505 //\r
506 // Be careful about pad size for alignment\r
507 //\r
130e2569 508 return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r
8d3a5c82 509}\r
510\r
7c80e839 511/**\r
9cad030b 512\r
7c80e839 513 Gets the pointer to the first variable header in given variable store area.\r
9cad030b 514\r
7c80e839 515 @param VarStoreHeader Pointer to the Variable Store Header.\r
9cad030b 516\r
7c80e839 517 @return Pointer to the first variable header\r
9cad030b 518\r
7c80e839 519**/\r
520VARIABLE_HEADER *\r
521GetStartPointer (\r
522 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
523 )\r
9cad030b 524{\r
525 //\r
526 // The end of variable store\r
527 //\r
528 return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r
529}\r
052ad7e1 530\r
7c80e839 531/**\r
8d3a5c82 532\r
7c80e839 533 Gets the pointer to the end of the variable storage area.\r
8d3a5c82 534\r
7c80e839 535 This function gets pointer to the end of the variable storage\r
536 area, according to the input variable store header.\r
8d3a5c82 537\r
7c80e839 538 @param VarStoreHeader Pointer to the Variable Store Header\r
8d3a5c82 539\r
7c80e839 540 @return Pointer to the end of the variable storage area \r
8d3a5c82 541\r
7c80e839 542**/\r
543VARIABLE_HEADER *\r
544GetEndPointer (\r
545 IN VARIABLE_STORE_HEADER *VarStoreHeader\r
546 )\r
8d3a5c82 547{\r
548 //\r
549 // The end of variable store\r
550 //\r
9cad030b 551 return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
8d3a5c82 552}\r
553\r
052ad7e1 554\r
7c80e839 555/**\r
556\r
557 Variable store garbage collection and reclaim operation.\r
558\r
559 @param VariableBase Base address of variable store\r
560 @param LastVariableOffset Offset of last variable\r
561 @param IsVolatile The variable store is volatile or not,\r
562 if it is non-volatile, need FTW\r
563 @param UpdatingVariable Pointer to updateing variable.\r
564\r
565 @return EFI_OUT_OF_RESOURCES\r
566 @return EFI_SUCCESS\r
567 @return Others\r
568\r
569**/\r
8d3a5c82 570EFI_STATUS\r
8d3a5c82 571Reclaim (\r
572 IN EFI_PHYSICAL_ADDRESS VariableBase,\r
573 OUT UINTN *LastVariableOffset,\r
814bae52 574 IN BOOLEAN IsVolatile,\r
575 IN VARIABLE_HEADER *UpdatingVariable\r
8d3a5c82 576 )\r
8d3a5c82 577{\r
578 VARIABLE_HEADER *Variable;\r
814bae52 579 VARIABLE_HEADER *AddedVariable;\r
8d3a5c82 580 VARIABLE_HEADER *NextVariable;\r
814bae52 581 VARIABLE_HEADER *NextAddedVariable;\r
8d3a5c82 582 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
583 UINT8 *ValidBuffer;\r
814bae52 584 UINTN MaximumBufferSize;\r
8d3a5c82 585 UINTN VariableSize;\r
49e70927 586 UINTN VariableNameSize;\r
587 UINTN UpdatingVariableNameSize;\r
814bae52 588 UINTN NameSize;\r
8d3a5c82 589 UINT8 *CurrPtr;\r
814bae52 590 VOID *Point0;\r
591 VOID *Point1;\r
592 BOOLEAN FoundAdded;\r
8d3a5c82 593 EFI_STATUS Status;\r
49e70927 594 CHAR16 *VariableNamePtr;\r
595 CHAR16 *UpdatingVariableNamePtr;\r
8d3a5c82 596\r
597 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r
598\r
599 //\r
600 // Start Pointers for the variable.\r
601 //\r
814bae52 602 Variable = GetStartPointer (VariableStoreHeader);\r
603 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
8d3a5c82 604\r
605 while (IsValidVariableHeader (Variable)) {\r
606 NextVariable = GetNextVariablePtr (Variable);\r
814bae52 607 if (Variable->State == VAR_ADDED || \r
608 Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
609 ) {\r
8d3a5c82 610 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
814bae52 611 MaximumBufferSize += VariableSize;\r
8d3a5c82 612 }\r
613\r
614 Variable = NextVariable;\r
615 }\r
616\r
814bae52 617 //\r
618 // Reserve the 1 Bytes with Oxff to identify the \r
619 // end of the variable buffer. \r
620 // \r
621 MaximumBufferSize += 1;\r
622 ValidBuffer = AllocatePool (MaximumBufferSize);\r
8d3a5c82 623 if (ValidBuffer == NULL) {\r
624 return EFI_OUT_OF_RESOURCES;\r
625 }\r
626\r
814bae52 627 SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r
8d3a5c82 628\r
629 //\r
630 // Copy variable store header\r
631 //\r
814bae52 632 CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
633 CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
8d3a5c82 634\r
814bae52 635 //\r
5ead4a07 636 // Reinstall all ADDED variables as long as they are not identical to Updating Variable\r
814bae52 637 // \r
638 Variable = GetStartPointer (VariableStoreHeader);\r
8d3a5c82 639 while (IsValidVariableHeader (Variable)) {\r
640 NextVariable = GetNextVariablePtr (Variable);\r
641 if (Variable->State == VAR_ADDED) {\r
5ead4a07 642 if (UpdatingVariable != NULL) {\r
643 if (UpdatingVariable == Variable) {\r
644 Variable = NextVariable;\r
645 continue;\r
646 }\r
49e70927 647\r
648 VariableNameSize = NameSizeOfVariable(Variable);\r
649 UpdatingVariableNameSize = NameSizeOfVariable(UpdatingVariable);\r
650\r
651 VariableNamePtr = GetVariableNamePtr (Variable);\r
652 UpdatingVariableNamePtr = GetVariableNamePtr (UpdatingVariable);\r
5ead4a07 653 if (CompareGuid (&Variable->VendorGuid, &UpdatingVariable->VendorGuid) &&\r
49e70927 654 VariableNameSize == UpdatingVariableNameSize &&\r
655 CompareMem (VariableNamePtr, UpdatingVariableNamePtr, VariableNameSize) == 0 ) {\r
5ead4a07 656 Variable = NextVariable;\r
657 continue;\r
658 }\r
659 }\r
8d3a5c82 660 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
661 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
662 CurrPtr += VariableSize;\r
663 }\r
8d3a5c82 664 Variable = NextVariable;\r
665 }\r
5ead4a07 666\r
667 //\r
668 // Reinstall the variable being updated if it is not NULL\r
669 //\r
670 if (UpdatingVariable != NULL) {\r
671 VariableSize = (UINTN)(GetNextVariablePtr (UpdatingVariable)) - (UINTN)UpdatingVariable;\r
672 CopyMem (CurrPtr, (UINT8 *) UpdatingVariable, VariableSize);\r
673 CurrPtr += VariableSize;\r
674 }\r
675\r
814bae52 676 //\r
677 // Reinstall all in delete transition variables\r
678 // \r
679 Variable = GetStartPointer (VariableStoreHeader);\r
680 while (IsValidVariableHeader (Variable)) {\r
681 NextVariable = GetNextVariablePtr (Variable);\r
5ead4a07 682 if (Variable != UpdatingVariable && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
814bae52 683\r
684 //\r
685 // Buffer has cached all ADDED variable. \r
686 // Per IN_DELETED variable, we have to guarantee that\r
687 // no ADDED one in previous buffer. \r
688 // \r
689 \r
690 FoundAdded = FALSE;\r
691 AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
692 while (IsValidVariableHeader (AddedVariable)) {\r
693 NextAddedVariable = GetNextVariablePtr (AddedVariable);\r
694 NameSize = NameSizeOfVariable (AddedVariable);\r
695 if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&\r
696 NameSize == NameSizeOfVariable (Variable)\r
697 ) {\r
698 Point0 = (VOID *) GetVariableNamePtr (AddedVariable);\r
699 Point1 = (VOID *) GetVariableNamePtr (Variable);\r
5ead4a07 700 if (CompareMem (Point0, Point1, NameSizeOfVariable (AddedVariable)) == 0) {\r
814bae52 701 FoundAdded = TRUE;\r
702 break;\r
703 }\r
704 }\r
705 AddedVariable = NextAddedVariable;\r
706 }\r
707 if (!FoundAdded) {\r
5ead4a07 708 //\r
709 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED\r
710 //\r
814bae52 711 VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
712 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
5ead4a07 713 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
814bae52 714 CurrPtr += VariableSize;\r
715 }\r
716 }\r
717\r
718 Variable = NextVariable;\r
719 }\r
8d3a5c82 720\r
721 if (IsVolatile) {\r
722 //\r
723 // If volatile variable store, just copy valid buffer\r
724 //\r
725 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r
814bae52 726 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer));\r
8d3a5c82 727 Status = EFI_SUCCESS;\r
728 } else {\r
729 //\r
730 // If non-volatile variable store, perform FTW here.\r
731 //\r
732 Status = FtwVariableSpace (\r
733 VariableBase,\r
734 ValidBuffer,\r
814bae52 735 (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)\r
8d3a5c82 736 );\r
8d3a5c82 737 }\r
814bae52 738 if (!EFI_ERROR (Status)) {\r
739 *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer);\r
740 } else {\r
8d3a5c82 741 *LastVariableOffset = 0;\r
742 }\r
743\r
814bae52 744 FreePool (ValidBuffer);\r
745\r
8d3a5c82 746 return Status;\r
747}\r
748\r
33a5a666 749\r
052ad7e1
A
750/**\r
751 Update the Cache with Variable information. These are the same \r
752 arguments as the EFI Variable services.\r
753\r
754 @param[in] VariableName Name of variable\r
755 @param[in] VendorGuid Guid of variable\r
45f6c85b 756 @param[in] Attributes Attribues of the variable\r
052ad7e1
A
757 @param[in] DataSize Size of data. 0 means delete\r
758 @param[in] Data Variable data\r
759\r
760**/\r
33a5a666
A
761VOID\r
762UpdateVariableCache (\r
763 IN CHAR16 *VariableName,\r
764 IN EFI_GUID *VendorGuid,\r
052ad7e1
A
765 IN UINT32 Attributes,\r
766 IN UINTN DataSize,\r
767 IN VOID *Data\r
33a5a666
A
768 )\r
769{\r
770 VARIABLE_CACHE_ENTRY *Entry;\r
771 UINTN Index;\r
772\r
773 if (EfiAtRuntime ()) {\r
774 // Don't use the cache at runtime\r
775 return;\r
776 }\r
777\r
778 for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {\r
779 if (CompareGuid (VendorGuid, Entry->Guid)) {\r
780 if (StrCmp (VariableName, Entry->Name) == 0) { \r
781 Entry->Attributes = Attributes;\r
782 if (DataSize == 0) {\r
783 // Delete Case\r
784 if (Entry->DataSize != 0) {\r
785 FreePool (Entry->Data);\r
786 }\r
787 Entry->DataSize = DataSize;\r
788 } else if (DataSize == Entry->DataSize) {\r
789 CopyMem (Entry->Data, Data, DataSize);\r
790 } else {\r
791 Entry->Data = AllocatePool (DataSize);\r
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
c6492839 930 if (!EfiAtRuntime () || (Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\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
948 if (!CompareMem (VariableName, Point, NameSizeOfVariable (Variable[Index]))) {\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
1137 if (!(EfiAtRuntime () && !(Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {\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
1295 if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r
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
2064 Status = gBS->CreateEvent (\r
2065 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,\r
2066 TPL_NOTIFY,\r
2067 VariableClassAddressChangeEvent,\r
2068 NULL,\r
2069 &mVirtualAddressChangeEvent\r
2070 );\r
2071 ASSERT_EFI_ERROR (Status);\r
2072\r
2073 return EFI_SUCCESS;\r
2074}\r
2075\r