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