3 Implement all four UEFI Runtime Variable services for the nonvolatile
4 and volatile storage space and install variable architecture protocol.
6 Copyright (c) 2006 - 2008, Intel Corporation
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
20 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
21 EFI_HANDLE mHandle
= NULL
;
24 /// The current Hii implementation accesses this variable many times on every boot.
25 /// Other common variables are only accessed once. This is why this cache algorithm
26 /// only targets a single variable. Probably to get an performance improvement out of
27 /// a Cache you would need a cache that improves the search performance for a variable.
29 VARIABLE_CACHE_ENTRY mVariableCache
[] = {
31 &gEfiGlobalVariableGuid
,
39 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
42 Acquires lock only at boot time. Simply returns at runtime.
44 This is a temperary function which will be removed when
45 EfiAcquireLock() in UefiLib can handle the call in UEFI
46 Runtimer driver in RT phase.
47 It calls EfiAcquireLock() at boot time, and simply returns
50 @param Lock A pointer to the lock to acquire
54 AcquireLockOnlyAtBootTime (
58 if (!EfiAtRuntime ()) {
59 EfiAcquireLock (Lock
);
64 Releases lock only at boot time. Simply returns at runtime.
66 This is a temperary function which will be removed when
67 EfiReleaseLock() in UefiLib can handle the call in UEFI
68 Runtimer driver in RT phase.
69 It calls EfiReleaseLock() at boot time, and simply returns
72 @param Lock A pointer to the lock to release
76 ReleaseLockOnlyAtBootTime (
80 if (!EfiAtRuntime ()) {
81 EfiReleaseLock (Lock
);
87 Routine used to track statistical information about variable usage.
88 The data is stored in the EFI system table so it can be accessed later.
89 VariableInfo.efi can dump out the table. Only Boot Services variable
90 accesses are tracked by this code. The PcdVariableCollectStatistics
91 build flag controls if this feature is enabled.
93 A read that hits in the cache will have Read and Cache true for
94 the transaction. Data is allocated by this routine, but never
97 @param[in] VariableName Name of the Variable to track
98 @param[in] VendorGuid Guid of the Variable to track
99 @param[in] Volatile TRUE if volatile FALSE if non-volatile
100 @param[in] Read TRUE if GetVariable() was called
101 @param[in] Write TRUE if SetVariable() was called
102 @param[in] Delete TRUE if deleted via SetVariable()
103 @param[in] Cache TRUE for a cache hit.
108 IN CHAR16
*VariableName
,
109 IN EFI_GUID
*VendorGuid
,
117 VARIABLE_INFO_ENTRY
*Entry
;
119 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
121 if (EfiAtRuntime ()) {
122 // Don't collect statistics at runtime
126 if (gVariableInfo
== NULL
) {
128 // on the first call allocate a entry and place a pointer to it in
129 // the EFI System Table
131 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
132 ASSERT (gVariableInfo
!= NULL
);
134 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
135 gVariableInfo
->Name
= AllocatePool (StrLen (VariableName
));
136 ASSERT (gVariableInfo
->Name
!= NULL
);
137 StrCpy (gVariableInfo
->Name
, VariableName
);
138 gVariableInfo
->Volatile
= Volatile
;
140 gBS
->InstallConfigurationTable (&gEfiVariableInfoGuid
, gVariableInfo
);
144 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
145 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
146 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
154 Entry
->DeleteCount
++;
164 if (Entry
->Next
== NULL
) {
166 // If the entry is not in the table add it.
167 // Next iteration of the loop will fill in the data
169 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
170 ASSERT (Entry
->Next
!= NULL
);
172 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
173 Entry
->Next
->Name
= AllocatePool (StrLen (VariableName
));
174 ASSERT (Entry
->Next
->Name
!= NULL
);
175 StrCpy (Entry
->Next
->Name
, VariableName
);
176 Entry
->Next
->Volatile
= Volatile
;
186 This code checks if variable header is valid or not.
188 @param Variable Pointer to the Variable Header.
190 @retval TRUE Variable header is valid.
191 @retval FALSE Variable header is not valid.
195 IsValidVariableHeader (
196 IN VARIABLE_HEADER
*Variable
199 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
209 This function writes data to the FWH at the correct LBA even if the LBAs
212 @param Global Pointer to VARAIBLE_GLOBAL structure
213 @param Volatile Point out the Variable is Volatile or Non-Volatile
214 @param SetByIndex TRUE if target pointer is given as index
215 FALSE if target pointer is absolute
216 @param Instance Instance of FV Block services
217 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
219 @param DataSize Size of data to be written
220 @param Buffer Pointer to the buffer from which data is written
222 @retval EFI_INVALID_PARAMETER Parameters not valid
223 @retval EFI_SUCCESS Variable store successfully updated
227 UpdateVariableStore (
228 IN VARIABLE_GLOBAL
*Global
,
230 IN BOOLEAN SetByIndex
,
232 IN UINTN DataPtrIndex
,
237 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
245 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
246 VARIABLE_STORE_HEADER
*VolatileBase
;
247 EFI_PHYSICAL_ADDRESS FvVolHdr
;
248 EFI_PHYSICAL_ADDRESS DataPtr
;
252 DataPtr
= DataPtrIndex
;
255 // Check if the Data is Volatile
258 EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
);
259 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
261 // Data Pointer should point to the actual Address where data is to be
265 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
268 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
269 return EFI_INVALID_PARAMETER
;
273 // Data Pointer should point to the actual Address where data is to be
276 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
278 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
281 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
282 return EFI_INVALID_PARAMETER
;
286 // If Volatile Variable just do a simple mem copy.
288 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
293 // If we are here we are dealing with Non-Volatile Variables
295 LinearOffset
= (UINTN
) FwVolHeader
;
296 CurrWritePtr
= (UINTN
) DataPtr
;
297 CurrWriteSize
= DataSize
;
301 if (CurrWritePtr
< LinearOffset
) {
302 return EFI_INVALID_PARAMETER
;
305 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
306 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
308 // Check to see if the Variable Writes are spanning through multiple
311 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
312 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
313 Status
= EfiFvbWriteBlock (
316 (UINTN
) (CurrWritePtr
- LinearOffset
),
322 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
323 Status
= EfiFvbWriteBlock (
326 (UINTN
) (CurrWritePtr
- LinearOffset
),
330 if (EFI_ERROR (Status
)) {
334 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
335 CurrBuffer
= CurrBuffer
+ Size
;
336 CurrWriteSize
= CurrWriteSize
- Size
;
340 LinearOffset
+= PtrBlockMapEntry
->Length
;
351 This code gets the current status of Variable Store.
353 @param VarStoreHeader Pointer to the Variable Store Header.
355 @retval EfiRaw Variable store status is raw
356 @retval EfiValid Variable store status is valid
357 @retval EfiInvalid Variable store status is invalid
360 VARIABLE_STORE_STATUS
361 GetVariableStoreStatus (
362 IN VARIABLE_STORE_HEADER
*VarStoreHeader
365 if (VarStoreHeader
->Signature
== VARIABLE_STORE_SIGNATURE
&&
366 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
367 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
371 } else if (VarStoreHeader
->Signature
== 0xffffffff &&
372 VarStoreHeader
->Size
== 0xffffffff &&
373 VarStoreHeader
->Format
== 0xff &&
374 VarStoreHeader
->State
== 0xff
386 This code gets the size of name of variable.
388 @param Variable Pointer to the Variable Header
390 @return UINTN Size of variable in bytes
395 IN VARIABLE_HEADER
*Variable
398 if (Variable
->State
== (UINT8
) (-1) ||
399 Variable
->DataSize
== (UINT32
) (-1) ||
400 Variable
->NameSize
== (UINT32
) (-1) ||
401 Variable
->Attributes
== (UINT32
) (-1)) {
404 return (UINTN
) Variable
->NameSize
;
409 This code gets the size of variable data.
411 @param Variable Pointer to the Variable Header
413 @return Size of variable in bytes
418 IN VARIABLE_HEADER
*Variable
421 if (Variable
->State
== (UINT8
) (-1) ||
422 Variable
->DataSize
== (UINT32
) (-1) ||
423 Variable
->NameSize
== (UINT32
) (-1) ||
424 Variable
->Attributes
== (UINT32
) (-1)) {
427 return (UINTN
) Variable
->DataSize
;
432 This code gets the pointer to the variable name.
434 @param Variable Pointer to the Variable Header
436 @return Pointer to Variable Name which is Unicode encoding
441 IN VARIABLE_HEADER
*Variable
445 return (CHAR16
*) (Variable
+ 1);
450 This code gets the pointer to the variable data.
452 @param Variable Pointer to the Variable Header
454 @return Pointer to Variable Data
459 IN VARIABLE_HEADER
*Variable
465 // Be careful about pad size for alignment
467 Value
= (UINTN
) GetVariableNamePtr (Variable
);
468 Value
+= NameSizeOfVariable (Variable
);
469 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
471 return (UINT8
*) Value
;
477 This code gets the pointer to the next variable header.
479 @param Variable Pointer to the Variable Header
481 @return Pointer to next variable header
486 IN VARIABLE_HEADER
*Variable
491 if (!IsValidVariableHeader (Variable
)) {
495 Value
= (UINTN
) GetVariableDataPtr (Variable
);
496 Value
+= DataSizeOfVariable (Variable
);
497 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
500 // Be careful about pad size for alignment
502 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
507 Gets the pointer to the first variable header in given variable store area.
509 @param VarStoreHeader Pointer to the Variable Store Header.
511 @return Pointer to the first variable header
516 IN VARIABLE_STORE_HEADER
*VarStoreHeader
520 // The end of variable store
522 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
527 Gets the pointer to the end of the variable storage area.
529 This function gets pointer to the end of the variable storage
530 area, according to the input variable store header.
532 @param VarStoreHeader Pointer to the Variable Store Header
534 @return Pointer to the end of the variable storage area
539 IN VARIABLE_STORE_HEADER
*VarStoreHeader
543 // The end of variable store
545 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
551 Variable store garbage collection and reclaim operation.
553 @param VariableBase Base address of variable store
554 @param LastVariableOffset Offset of last variable
555 @param IsVolatile The variable store is volatile or not,
556 if it is non-volatile, need FTW
557 @param UpdatingVariable Pointer to updateing variable.
559 @return EFI_OUT_OF_RESOURCES
566 IN EFI_PHYSICAL_ADDRESS VariableBase
,
567 OUT UINTN
*LastVariableOffset
,
568 IN BOOLEAN IsVolatile
,
569 IN VARIABLE_HEADER
*UpdatingVariable
572 VARIABLE_HEADER
*Variable
;
573 VARIABLE_HEADER
*AddedVariable
;
574 VARIABLE_HEADER
*NextVariable
;
575 VARIABLE_HEADER
*NextAddedVariable
;
576 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
578 UINTN MaximumBufferSize
;
580 UINTN VariableNameSize
;
581 UINTN UpdatingVariableNameSize
;
588 CHAR16
*VariableNamePtr
;
589 CHAR16
*UpdatingVariableNamePtr
;
591 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
594 // Start Pointers for the variable.
596 Variable
= GetStartPointer (VariableStoreHeader
);
597 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
599 while (IsValidVariableHeader (Variable
)) {
600 NextVariable
= GetNextVariablePtr (Variable
);
601 if (Variable
->State
== VAR_ADDED
||
602 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
604 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
605 MaximumBufferSize
+= VariableSize
;
608 Variable
= NextVariable
;
612 // Reserve the 1 Bytes with Oxff to identify the
613 // end of the variable buffer.
615 MaximumBufferSize
+= 1;
616 ValidBuffer
= AllocatePool (MaximumBufferSize
);
617 if (ValidBuffer
== NULL
) {
618 return EFI_OUT_OF_RESOURCES
;
621 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
624 // Copy variable store header
626 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
627 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
630 // Reinstall all ADDED variables as long as they are not identical to Updating Variable
632 Variable
= GetStartPointer (VariableStoreHeader
);
633 while (IsValidVariableHeader (Variable
)) {
634 NextVariable
= GetNextVariablePtr (Variable
);
635 if (Variable
->State
== VAR_ADDED
) {
636 if (UpdatingVariable
!= NULL
) {
637 if (UpdatingVariable
== Variable
) {
638 Variable
= NextVariable
;
642 VariableNameSize
= NameSizeOfVariable(Variable
);
643 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
645 VariableNamePtr
= GetVariableNamePtr (Variable
);
646 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
647 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
648 VariableNameSize
== UpdatingVariableNameSize
&&
649 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
650 Variable
= NextVariable
;
654 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
655 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
656 CurrPtr
+= VariableSize
;
658 Variable
= NextVariable
;
662 // Reinstall the variable being updated if it is not NULL
664 if (UpdatingVariable
!= NULL
) {
665 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
666 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
667 CurrPtr
+= VariableSize
;
671 // Reinstall all in delete transition variables
673 Variable
= GetStartPointer (VariableStoreHeader
);
674 while (IsValidVariableHeader (Variable
)) {
675 NextVariable
= GetNextVariablePtr (Variable
);
676 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
679 // Buffer has cached all ADDED variable.
680 // Per IN_DELETED variable, we have to guarantee that
681 // no ADDED one in previous buffer.
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
)
692 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
693 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
694 if (CompareMem (Point0
, Point1
, NameSizeOfVariable (AddedVariable
)) == 0) {
699 AddedVariable
= NextAddedVariable
;
703 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED
705 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
706 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
707 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
708 CurrPtr
+= VariableSize
;
712 Variable
= NextVariable
;
717 // If volatile variable store, just copy valid buffer
719 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
720 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
721 Status
= EFI_SUCCESS
;
724 // If non-volatile variable store, perform FTW here.
726 Status
= FtwVariableSpace (
729 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
732 if (!EFI_ERROR (Status
)) {
733 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
735 *LastVariableOffset
= 0;
738 FreePool (ValidBuffer
);
745 Update the Cache with Variable information. These are the same
746 arguments as the EFI Variable services.
748 @param[in] VariableName Name of variable
749 @param[in] VendorGuid Guid of variable
750 @param[in] Attributes Attribues of the variable
751 @param[in] DataSize Size of data. 0 means delete
752 @param[in] Data Variable data
756 UpdateVariableCache (
757 IN CHAR16
*VariableName
,
758 IN EFI_GUID
*VendorGuid
,
759 IN UINT32 Attributes
,
764 VARIABLE_CACHE_ENTRY
*Entry
;
767 if (EfiAtRuntime ()) {
768 // Don't use the cache at runtime
772 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
773 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
774 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
775 Entry
->Attributes
= Attributes
;
778 if (Entry
->DataSize
!= 0) {
779 FreePool (Entry
->Data
);
781 Entry
->DataSize
= DataSize
;
782 } else if (DataSize
== Entry
->DataSize
) {
783 CopyMem (Entry
->Data
, Data
, DataSize
);
785 Entry
->Data
= AllocatePool (DataSize
);
786 Entry
->DataSize
= DataSize
;
787 CopyMem (Entry
->Data
, Data
, DataSize
);
796 Search the cache to check if the variable is in it.
798 This function searches the variable cache. If the variable to find exists, return its data
801 @param VariableName A Null-terminated Unicode string that is the name of the vendor's
802 variable. Each VariableName is unique for each
804 @param VendorGuid A unique identifier for the vendor
805 @param Attributes Pointer to the attributes bitmask of the variable for output.
806 @param DataSize On input, size of the buffer of Data.
807 On output, size of the variable's data.
808 @param Data Pointer to the data buffer for output.
810 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.
811 @retval EFI_NOT_FOUND No matching variable found in cache.
812 @retval EFI_BUFFER_TOO_SMALL *DataSize is smaller than size of the variable's data to return.
816 FindVariableInCache (
817 IN CHAR16
*VariableName
,
818 IN EFI_GUID
*VendorGuid
,
819 OUT UINT32
*Attributes OPTIONAL
,
820 IN OUT UINTN
*DataSize
,
824 VARIABLE_CACHE_ENTRY
*Entry
;
827 if (EfiAtRuntime ()) {
828 // Don't use the cache at runtime
829 return EFI_NOT_FOUND
;
832 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
833 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
834 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
835 if (Entry
->DataSize
== 0) {
836 // Variable was deleted so return not found
837 return EFI_NOT_FOUND
;
838 } else if (Entry
->DataSize
> *DataSize
) {
839 // If the buffer is too small return correct size
840 *DataSize
= Entry
->DataSize
;
841 return EFI_BUFFER_TOO_SMALL
;
843 *DataSize
= Entry
->DataSize
;
845 CopyMem (Data
, Entry
->Data
, Entry
->DataSize
);
846 if (Attributes
!= NULL
) {
847 *Attributes
= Entry
->Attributes
;
855 return EFI_NOT_FOUND
;
859 Finds variable in storage blocks of volatile and non-volatile storage areas.
861 This code finds variable in storage blocks of volatile and non-volatile storage areas.
862 If VariableName is an empty string, then we just return the first
863 qualified variable without comparing VariableName and VendorGuid.
864 Otherwise, VariableName and VendorGuid are compared.
866 @param VariableName Name of the variable to be found
867 @param VendorGuid Vendor GUID to be found.
868 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
869 including the range searched and the target position.
870 @param Global Pointer to VARIABLE_GLOBAL structure, including
871 base of volatile variable storage area, base of
872 NV variable storage area, and a lock.
874 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
876 @retval EFI_SUCCESS Variable successfully found
877 @retval EFI_INVALID_PARAMETER Variable not found
882 IN CHAR16
*VariableName
,
883 IN EFI_GUID
*VendorGuid
,
884 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
885 IN VARIABLE_GLOBAL
*Global
888 VARIABLE_HEADER
*Variable
[2];
889 VARIABLE_HEADER
*InDeletedVariable
;
890 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
891 UINTN InDeletedStorageIndex
;
896 // 0: Volatile, 1: Non-Volatile
897 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
898 // make use of this mapping to implement search algorithme.
900 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
901 VariableStoreHeader
[1] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
904 // Start Pointers for the variable.
905 // Actual Data Pointer where data can be written.
907 Variable
[0] = GetStartPointer (VariableStoreHeader
[0]);
908 Variable
[1] = GetStartPointer (VariableStoreHeader
[1]);
910 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
911 return EFI_INVALID_PARAMETER
;
915 // Find the variable by walk through volatile and then non-volatile variable store
917 InDeletedVariable
= NULL
;
918 InDeletedStorageIndex
= 0;
919 for (Index
= 0; Index
< 2; Index
++) {
920 while (IsValidVariableHeader (Variable
[Index
]) && (Variable
[Index
] <= GetEndPointer (VariableStoreHeader
[Index
]))) {
921 if (Variable
[Index
]->State
== VAR_ADDED
||
922 Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
924 if (!EfiAtRuntime () || ((Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
925 if (VariableName
[0] == 0) {
926 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
927 InDeletedVariable
= Variable
[Index
];
928 InDeletedStorageIndex
= Index
;
930 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
931 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
932 PtrTrack
->CurrPtr
= Variable
[Index
];
933 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
938 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
939 Point
= (VOID
*) GetVariableNamePtr (Variable
[Index
]);
941 ASSERT (NameSizeOfVariable (Variable
[Index
]) != 0);
942 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
[Index
])) == 0) {
943 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
944 InDeletedVariable
= Variable
[Index
];
945 InDeletedStorageIndex
= Index
;
947 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
948 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
949 PtrTrack
->CurrPtr
= Variable
[Index
];
950 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
960 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
962 if (InDeletedVariable
!= NULL
) {
963 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
964 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
965 PtrTrack
->CurrPtr
= InDeletedVariable
;
966 PtrTrack
->Volatile
= (BOOLEAN
)(InDeletedStorageIndex
== 0);
970 PtrTrack
->CurrPtr
= NULL
;
971 return EFI_NOT_FOUND
;
977 This code finds variable in storage blocks (Volatile or Non-Volatile).
979 @param VariableName Name of Variable to be found.
980 @param VendorGuid Variable vendor GUID.
981 @param Attributes Attribute value of the variable found.
982 @param DataSize Size of Data found. If size is less than the
983 data, this value contains the required size.
984 @param Data Data pointer.
986 @return EFI_INVALID_PARAMETER Invalid parameter
987 @return EFI_SUCCESS Find the specified variable
988 @return EFI_NOT_FOUND Not found
989 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result
994 RuntimeServiceGetVariable (
995 IN CHAR16
*VariableName
,
996 IN EFI_GUID
*VendorGuid
,
997 OUT UINT32
*Attributes OPTIONAL
,
998 IN OUT UINTN
*DataSize
,
1003 VARIABLE_POINTER_TRACK Variable
;
1006 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1007 return EFI_INVALID_PARAMETER
;
1010 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1013 // Find existing variable
1015 Status
= FindVariableInCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1016 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_SUCCESS
)){
1018 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
);
1022 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1023 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1030 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1031 ASSERT (VarDataSize
!= 0);
1033 if (*DataSize
>= VarDataSize
) {
1035 Status
= EFI_INVALID_PARAMETER
;
1039 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1040 if (Attributes
!= NULL
) {
1041 *Attributes
= Variable
.CurrPtr
->Attributes
;
1044 *DataSize
= VarDataSize
;
1045 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1046 UpdateVariableCache (VariableName
, VendorGuid
, Variable
.CurrPtr
->Attributes
, VarDataSize
, Data
);
1048 Status
= EFI_SUCCESS
;
1051 *DataSize
= VarDataSize
;
1052 Status
= EFI_BUFFER_TOO_SMALL
;
1057 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1065 This code Finds the Next available variable.
1067 @param VariableNameSize Size of the variable name
1068 @param VariableName Pointer to variable name
1069 @param VendorGuid Variable Vendor Guid
1071 @return EFI_INVALID_PARAMETER Invalid parameter
1072 @return EFI_SUCCESS Find the specified variable
1073 @return EFI_NOT_FOUND Not found
1074 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result
1079 RuntimeServiceGetNextVariableName (
1080 IN OUT UINTN
*VariableNameSize
,
1081 IN OUT CHAR16
*VariableName
,
1082 IN OUT EFI_GUID
*VendorGuid
1085 VARIABLE_POINTER_TRACK Variable
;
1089 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1090 return EFI_INVALID_PARAMETER
;
1093 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1095 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1096 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1100 if (VariableName
[0] != 0) {
1102 // If variable name is not NULL, get next variable
1104 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1109 // If both volatile and non-volatile variable store are parsed,
1112 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
1113 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
1114 if (!Variable
.Volatile
) {
1115 Variable
.StartPtr
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1116 Variable
.EndPtr
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
));
1118 Status
= EFI_NOT_FOUND
;
1122 Variable
.CurrPtr
= Variable
.StartPtr
;
1123 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
1128 // Variable is found
1130 if (IsValidVariableHeader (Variable
.CurrPtr
) && Variable
.CurrPtr
->State
== VAR_ADDED
) {
1131 if ((EfiAtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
1132 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
1133 ASSERT (VarNameSize
!= 0);
1135 if (VarNameSize
<= *VariableNameSize
) {
1138 GetVariableNamePtr (Variable
.CurrPtr
),
1143 &Variable
.CurrPtr
->VendorGuid
,
1146 Status
= EFI_SUCCESS
;
1148 Status
= EFI_BUFFER_TOO_SMALL
;
1151 *VariableNameSize
= VarNameSize
;
1156 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1160 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1166 This code sets variable in storage blocks (Volatile or Non-Volatile).
1168 @param VariableName Name of Variable to be found
1169 @param VendorGuid Variable vendor GUID
1170 @param Attributes Attribute value of the variable found
1171 @param DataSize Size of Data found. If size is less than the
1172 data, this value contains the required size.
1173 @param Data Data pointer
1175 @return EFI_INVALID_PARAMETER Invalid parameter
1176 @return EFI_SUCCESS Set successfully
1177 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable
1178 @return EFI_NOT_FOUND Not found
1179 @return EFI_WRITE_PROTECTED Variable is read-only
1184 RuntimeServiceSetVariable (
1185 IN CHAR16
*VariableName
,
1186 IN EFI_GUID
*VendorGuid
,
1187 IN UINT32 Attributes
,
1192 VARIABLE_POINTER_TRACK Variable
;
1194 VARIABLE_HEADER
*NextVariable
;
1196 UINTN VarNameOffset
;
1197 UINTN VarDataOffset
;
1201 UINTN
*VolatileOffset
;
1202 UINTN
*NonVolatileOffset
;
1205 EFI_PHYSICAL_ADDRESS Point
;
1208 // Check input parameters
1210 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1211 return EFI_INVALID_PARAMETER
;
1214 // Make sure if runtime bit is set, boot service bit is set also
1216 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1217 return EFI_INVALID_PARAMETER
;
1221 // The size of the VariableName, including the Unicode Null in bytes plus
1222 // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxHardwareErrorVariableSize)
1223 // bytes for HwErrRec, and FixedPcdGet32(PcdMaxVariableSize) bytes for the others.
1225 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1226 if ((DataSize
> FixedPcdGet32(PcdMaxHardwareErrorVariableSize
)) ||
1227 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> FixedPcdGet32(PcdMaxHardwareErrorVariableSize
))) {
1228 return EFI_INVALID_PARAMETER
;
1232 // The size of the VariableName, including the Unicode Null in bytes plus
1233 // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxVariableSize) bytes.
1235 if ((DataSize
> FixedPcdGet32(PcdMaxVariableSize
)) ||
1236 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> FixedPcdGet32(PcdMaxVariableSize
))) {
1237 return EFI_INVALID_PARAMETER
;
1241 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1244 Instance
= mVariableModuleGlobal
->FvbInstance
;
1245 VolatileOffset
= &mVariableModuleGlobal
->VolatileLastVariableOffset
;
1248 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
1250 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
1251 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;;
1253 // Parse non-volatile variable data and get last variable offset
1255 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
1256 while (IsValidVariableHeader (NextVariable
)) {
1257 NextVariable
= GetNextVariablePtr (NextVariable
);
1259 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
1262 NonVolatileOffset
= &mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1266 // Check whether the input variable is already existed
1269 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1270 if (Status
== EFI_SUCCESS
&& Variable
.CurrPtr
!= NULL
) {
1272 // Update/Delete existing variable
1274 Volatile
= Variable
.Volatile
;
1276 if (EfiAtRuntime ()) {
1278 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1279 // the volatile is ReadOnly, and SetVariable should be aborted and
1280 // return EFI_WRITE_PROTECTED.
1282 if (Variable
.Volatile
) {
1283 Status
= EFI_WRITE_PROTECTED
;
1287 // Only variable have NV attribute can be updated/deleted in Runtime
1289 if ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1290 Status
= EFI_INVALID_PARAMETER
;
1295 // Setting a data variable with no access, or zero DataSize attributes
1296 // specified causes it to be deleted.
1298 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1299 State
= Variable
.CurrPtr
->State
;
1300 State
&= VAR_DELETED
;
1302 Status
= UpdateVariableStore (
1303 &mVariableModuleGlobal
->VariableGlobal
,
1307 (UINTN
) &Variable
.CurrPtr
->State
,
1311 if (!EFI_ERROR (Status
)) {
1312 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1313 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1318 // If the variable is marked valid and the same data has been passed in
1319 // then return to the caller immediately.
1321 if (DataSizeOfVariable (Variable
.CurrPtr
) == DataSize
&&
1322 (CompareMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), DataSize
) == 0)) {
1324 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1325 Status
= EFI_SUCCESS
;
1327 } else if ((Variable
.CurrPtr
->State
== VAR_ADDED
) ||
1328 (Variable
.CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1331 // Mark the old variable as in delete transition
1333 State
= Variable
.CurrPtr
->State
;
1334 State
&= VAR_IN_DELETED_TRANSITION
;
1336 Status
= UpdateVariableStore (
1337 &mVariableModuleGlobal
->VariableGlobal
,
1341 (UINTN
) &Variable
.CurrPtr
->State
,
1345 if (EFI_ERROR (Status
)) {
1349 } else if (Status
== EFI_NOT_FOUND
) {
1351 // Create a new variable
1355 // Make sure we are trying to create a new variable.
1356 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1358 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1359 Status
= EFI_NOT_FOUND
;
1364 // Only variable have NV|RT attribute can be created in Runtime
1366 if (EfiAtRuntime () &&
1367 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1368 Status
= EFI_INVALID_PARAMETER
;
1373 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
1375 ASSERT (Status
== EFI_INVALID_PARAMETER
);
1380 // Function part - create a new variable and copy the data.
1381 // Both update a variable and create a variable will come here.
1383 // Tricky part: Use scratch data area at the end of volatile variable store
1384 // as a temporary storage.
1386 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1388 SetMem (NextVariable
, FixedPcdGet32(PcdMaxVariableSize
), 0xff);
1390 NextVariable
->StartId
= VARIABLE_DATA
;
1391 NextVariable
->Attributes
= Attributes
;
1393 // NextVariable->State = VAR_ADDED;
1395 NextVariable
->Reserved
= 0;
1396 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1397 VarNameSize
= StrSize (VariableName
);
1399 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1403 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1405 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1409 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1411 // There will be pad bytes after Data, the NextVariable->NameSize and
1412 // NextVariable->DataSize should not include pad size so that variable
1413 // service can get actual size in GetVariable
1415 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1416 NextVariable
->DataSize
= (UINT32
)DataSize
;
1419 // The actual size of the variable that stores in storage should
1420 // include pad size.
1422 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1423 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1425 // Create a nonvolatile variable
1429 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1430 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
1432 if (EfiAtRuntime ()) {
1433 Status
= EFI_OUT_OF_RESOURCES
;
1437 // Perform garbage collection & reclaim operation
1439 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
, NonVolatileOffset
, FALSE
, Variable
.CurrPtr
);
1440 if (EFI_ERROR (Status
)) {
1444 // If still no enough space, return out of resources
1446 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1447 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
1449 Status
= EFI_OUT_OF_RESOURCES
;
1457 // 1. Write variable header
1458 // 2. Set variable state to header valid
1459 // 3. Write variable data
1460 // 4. Set variable state to valid
1465 Status
= UpdateVariableStore (
1466 &mVariableModuleGlobal
->VariableGlobal
,
1471 sizeof (VARIABLE_HEADER
),
1472 (UINT8
*) NextVariable
1475 if (EFI_ERROR (Status
)) {
1482 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1483 Status
= UpdateVariableStore (
1484 &mVariableModuleGlobal
->VariableGlobal
,
1489 sizeof (VARIABLE_HEADER
),
1490 (UINT8
*) NextVariable
1493 if (EFI_ERROR (Status
)) {
1499 Status
= UpdateVariableStore (
1500 &mVariableModuleGlobal
->VariableGlobal
,
1504 *NonVolatileOffset
+ sizeof (VARIABLE_HEADER
),
1505 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1506 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1509 if (EFI_ERROR (Status
)) {
1515 NextVariable
->State
= VAR_ADDED
;
1516 Status
= UpdateVariableStore (
1517 &mVariableModuleGlobal
->VariableGlobal
,
1522 sizeof (VARIABLE_HEADER
),
1523 (UINT8
*) NextVariable
1526 if (EFI_ERROR (Status
)) {
1530 *NonVolatileOffset
= HEADER_ALIGN (*NonVolatileOffset
+ VarSize
);
1534 // Create a volatile variable
1538 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1539 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1541 // Perform garbage collection & reclaim operation
1543 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
, VolatileOffset
, TRUE
, Variable
.CurrPtr
);
1544 if (EFI_ERROR (Status
)) {
1548 // If still no enough space, return out of resources
1550 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1551 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1553 Status
= EFI_OUT_OF_RESOURCES
;
1560 NextVariable
->State
= VAR_ADDED
;
1561 Status
= UpdateVariableStore (
1562 &mVariableModuleGlobal
->VariableGlobal
,
1568 (UINT8
*) NextVariable
1571 if (EFI_ERROR (Status
)) {
1575 *VolatileOffset
= HEADER_ALIGN (*VolatileOffset
+ VarSize
);
1578 // Mark the old variable as deleted
1580 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
.CurrPtr
!= NULL
) {
1581 State
= Variable
.CurrPtr
->State
;
1582 State
&= VAR_DELETED
;
1584 Status
= UpdateVariableStore (
1585 &mVariableModuleGlobal
->VariableGlobal
,
1589 (UINTN
) &Variable
.CurrPtr
->State
,
1594 if (!EFI_ERROR (Status
)) {
1595 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1596 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1601 Status
= EFI_SUCCESS
;
1602 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1603 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1606 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
1607 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1614 This code returns information about the EFI variables.
1616 @param Attributes Attributes bitmask to specify the type of variables
1617 on which to return information.
1618 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1619 for the EFI variables associated with the attributes specified.
1620 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1621 for EFI variables associated with the attributes specified.
1622 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
1623 associated with the attributes specified.
1625 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
1626 @return EFI_SUCCESS Query successfully.
1627 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
1632 RuntimeServiceQueryVariableInfo (
1633 IN UINT32 Attributes
,
1634 OUT UINT64
*MaximumVariableStorageSize
,
1635 OUT UINT64
*RemainingVariableStorageSize
,
1636 OUT UINT64
*MaximumVariableSize
1639 VARIABLE_HEADER
*Variable
;
1640 VARIABLE_HEADER
*NextVariable
;
1641 UINT64 VariableSize
;
1642 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1644 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1645 return EFI_INVALID_PARAMETER
;
1648 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1650 // Make sure the Attributes combination is supported by the platform.
1652 return EFI_UNSUPPORTED
;
1653 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1655 // Make sure if runtime bit is set, boot service bit is set also.
1657 return EFI_INVALID_PARAMETER
;
1658 } else if (EfiAtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
1660 // Make sure RT Attribute is set if we are in Runtime phase.
1662 return EFI_INVALID_PARAMETER
;
1665 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1667 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1669 // Query is Volatile related.
1671 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
1674 // Query is Non-Volatile related.
1676 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1680 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1681 // with the storage size (excluding the storage header size).
1683 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1684 *RemainingVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1687 // Let *MaximumVariableSize be FixedPcdGet32(PcdMaxVariableSize) with the exception of the variable header size.
1689 *MaximumVariableSize
= FixedPcdGet32(PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
1692 // Harware error record variable needs larger size.
1694 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1695 *MaximumVariableSize
= FixedPcdGet32(PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
1699 // Point to the starting address of the variables.
1701 Variable
= GetStartPointer (VariableStoreHeader
);
1704 // Now walk through the related variable store.
1706 while (IsValidVariableHeader (Variable
) && (Variable
< GetEndPointer (VariableStoreHeader
))) {
1707 NextVariable
= GetNextVariablePtr (Variable
);
1708 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1710 if (EfiAtRuntime ()) {
1712 // we don't take the state of the variables in mind
1713 // when calculating RemainingVariableStorageSize,
1714 // since the space occupied by variables not marked with
1715 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1717 *RemainingVariableStorageSize
-= VariableSize
;
1720 // Only care about Variables with State VAR_ADDED,because
1721 // the space not marked as VAR_ADDED is reclaimable now.
1723 if (Variable
->State
== VAR_ADDED
) {
1724 *RemainingVariableStorageSize
-= VariableSize
;
1729 // Go to the next one
1731 Variable
= NextVariable
;
1734 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
1735 *MaximumVariableSize
= 0;
1736 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
1737 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
1740 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1746 Notification function of EVT_GROUP_READY_TO_BOOT event group.
1748 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
1749 When the Boot Manager is about to load and execute a boot option, it reclaims variable
1750 storage if free size is below the threshold.
1752 @param Event Event whose notification function is being invoked
1753 @param Context Pointer to the notification function's context
1766 VarSize
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1767 Status
= EFI_SUCCESS
;
1770 // Check if the free area is blow a threshold
1772 if ((VarSize
- mVariableModuleGlobal
->NonVolatileLastVariableOffset
) < VARIABLE_RECLAIM_THRESHOLD
) {
1774 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1775 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1779 ASSERT_EFI_ERROR (Status
);
1784 Initializes variable store area for non-volatile and volatile variable.
1786 @param ImageHandle The Image handle of this driver.
1787 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
1789 @retval EFI_SUCCESS Function successfully executed.
1790 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
1794 VariableCommonInitialize (
1795 IN EFI_HANDLE ImageHandle
,
1796 IN EFI_SYSTEM_TABLE
*SystemTable
1800 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1802 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
1803 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1804 VARIABLE_HEADER
*NextVariable
;
1806 EFI_PHYSICAL_ADDRESS FvVolHdr
;
1807 UINT64 TempVariableStoreHeader
;
1808 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
1813 UINT64 VariableStoreBase
;
1814 UINT64 VariableStoreLength
;
1815 EFI_EVENT ReadyToBootEvent
;
1817 Status
= EFI_SUCCESS
;
1819 // Allocate runtime memory for variable driver global structure.
1821 mVariableModuleGlobal
= AllocateRuntimePool (sizeof (VARIABLE_MODULE_GLOBAL
));
1822 if (mVariableModuleGlobal
== NULL
) {
1823 return EFI_OUT_OF_RESOURCES
;
1826 EfiInitializeLock(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
1827 mVariableModuleGlobal
->VariableGlobal
.ReentrantState
= 0;
1830 // Allocate memory for volatile variable store
1832 VolatileVariableStore
= AllocateRuntimePool (FixedPcdGet32(PcdVariableStoreSize
) + FixedPcdGet32(PcdMaxVariableSize
));
1833 if (VolatileVariableStore
== NULL
) {
1834 FreePool (mVariableModuleGlobal
);
1835 return EFI_OUT_OF_RESOURCES
;
1838 SetMem (VolatileVariableStore
, FixedPcdGet32(PcdVariableStoreSize
) + FixedPcdGet32(PcdMaxVariableSize
), 0xff);
1841 // Variable Specific Data
1843 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
1844 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
1846 VolatileVariableStore
->Signature
= VARIABLE_STORE_SIGNATURE
;
1847 VolatileVariableStore
->Size
= FixedPcdGet32(PcdVariableStoreSize
);
1848 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
1849 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
1850 VolatileVariableStore
->Reserved
= 0;
1851 VolatileVariableStore
->Reserved1
= 0;
1854 // Get non volatile varaible store
1857 TempVariableStoreHeader
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableBase
);
1858 VariableStoreBase
= TempVariableStoreHeader
+ \
1859 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1860 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
1861 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1863 // Mark the variable storage region of the FLASH as RUNTIME
1865 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
1866 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
1867 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
1869 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
1870 if (EFI_ERROR (Status
)) {
1874 Status
= gDS
->SetMemorySpaceAttributes (
1877 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
1879 if (EFI_ERROR (Status
)) {
1883 // Get address of non volatile variable store base
1885 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
1891 // Find the Correct Instance of the FV Block Service.
1894 CurrPtr
= (CHAR8
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1895 while (EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
) == EFI_SUCCESS
) {
1896 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
1897 if (CurrPtr
>= (CHAR8
*) FwVolHeader
&& CurrPtr
< (((CHAR8
*) FwVolHeader
) + FwVolHeader
->FvLength
)) {
1898 mVariableModuleGlobal
->FvbInstance
= Instance
;
1905 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) CurrPtr
;
1906 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
1907 if (~VariableStoreHeader
->Size
== 0) {
1908 Status
= UpdateVariableStore (
1909 &mVariableModuleGlobal
->VariableGlobal
,
1912 mVariableModuleGlobal
->FvbInstance
,
1913 (UINTN
) &VariableStoreHeader
->Size
,
1915 (UINT8
*) &VariableStoreLength
1918 // As Variables are stored in NV storage, which are slow devices,such as flash.
1919 // Variable operation may skip checking variable program result to improve performance,
1920 // We can assume Variable program is OK through some check point.
1921 // Variable Store Size Setting should be the first Variable write operation,
1922 // We can assume all Read/Write is OK if we can set Variable store size successfully.
1923 // If write fail, we will assert here
1925 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
1927 if (EFI_ERROR (Status
)) {
1932 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) CurrPtr
);
1934 // Parse non-volatile variable data and get last variable offset
1936 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) CurrPtr
);
1937 Status
= EFI_SUCCESS
;
1939 while (IsValidVariableHeader (NextVariable
)) {
1940 NextVariable
= GetNextVariablePtr (NextVariable
);
1943 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) CurrPtr
;
1946 // Check if the free area is really free.
1948 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
1949 Data
= ((UINT8
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)[Index
];
1952 // There must be something wrong in variable store, do reclaim operation.
1955 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1956 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1961 if (EFI_ERROR (Status
)) {
1970 // Register the event handling function to reclaim variable for OS usage.
1972 Status
= EfiCreateEventReadyToBootEx (
1981 if (EFI_ERROR (Status
)) {
1982 FreePool (mVariableModuleGlobal
);
1983 FreePool (VolatileVariableStore
);
1990 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
1992 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
1993 It convers pointer to new virtual address.
1995 @param Event Event whose notification function is being invoked
1996 @param Context Pointer to the notification function's context
2001 VariableClassAddressChangeEvent (
2008 (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
2012 (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
2014 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
2019 Variable Driver main entry point. The Variable driver places the 4 EFI
2020 runtime services in the EFI System Table and installs arch protocols
2021 for variable read and write services being availible. It also registers
2022 notification function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
2024 @param[in] ImageHandle The firmware allocated handle for the EFI image.
2025 @param[in] SystemTable A pointer to the EFI System Table.
2027 @retval EFI_SUCCESS Variable service successfully initialized.
2032 VariableServiceInitialize (
2033 IN EFI_HANDLE ImageHandle
,
2034 IN EFI_SYSTEM_TABLE
*SystemTable
2039 Status
= VariableCommonInitialize (ImageHandle
, SystemTable
);
2040 ASSERT_EFI_ERROR (Status
);
2042 SystemTable
->RuntimeServices
->GetVariable
= RuntimeServiceGetVariable
;
2043 SystemTable
->RuntimeServices
->GetNextVariableName
= RuntimeServiceGetNextVariableName
;
2044 SystemTable
->RuntimeServices
->SetVariable
= RuntimeServiceSetVariable
;
2045 SystemTable
->RuntimeServices
->QueryVariableInfo
= RuntimeServiceQueryVariableInfo
;
2048 // Now install the Variable Runtime Architectural Protocol on a new handle
2050 Status
= gBS
->InstallMultipleProtocolInterfaces (
2052 &gEfiVariableArchProtocolGuid
, NULL
,
2053 &gEfiVariableWriteArchProtocolGuid
, NULL
,
2056 ASSERT_EFI_ERROR (Status
);
2058 Status
= gBS
->CreateEventEx (
2061 VariableClassAddressChangeEvent
,
2063 &gEfiEventVirtualAddressChangeGuid
,
2064 &mVirtualAddressChangeEvent
2066 ASSERT_EFI_ERROR (Status
);