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