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 - 2009, 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 ()) {
769 // Don't use the cache at runtime
774 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
775 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
776 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
777 Entry
->Attributes
= Attributes
;
782 if (Entry
->DataSize
!= 0) {
783 FreePool (Entry
->Data
);
785 Entry
->DataSize
= DataSize
;
786 } else if (DataSize
== Entry
->DataSize
) {
787 CopyMem (Entry
->Data
, Data
, DataSize
);
789 Entry
->Data
= AllocatePool (DataSize
);
790 ASSERT (Entry
->Data
!= NULL
);
792 Entry
->DataSize
= DataSize
;
793 CopyMem (Entry
->Data
, Data
, DataSize
);
802 Search the cache to check if the variable is in it.
804 This function searches the variable cache. If the variable to find exists, return its data
807 @param VariableName A Null-terminated Unicode string that is the name of the vendor's
808 variable. Each VariableName is unique for each
810 @param VendorGuid A unique identifier for the vendor
811 @param Attributes Pointer to the attributes bitmask of the variable for output.
812 @param DataSize On input, size of the buffer of Data.
813 On output, size of the variable's data.
814 @param Data Pointer to the data buffer for output.
816 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.
817 @retval EFI_NOT_FOUND No matching variable found in cache.
818 @retval EFI_BUFFER_TOO_SMALL *DataSize is smaller than size of the variable's data to return.
822 FindVariableInCache (
823 IN CHAR16
*VariableName
,
824 IN EFI_GUID
*VendorGuid
,
825 OUT UINT32
*Attributes OPTIONAL
,
826 IN OUT UINTN
*DataSize
,
830 VARIABLE_CACHE_ENTRY
*Entry
;
833 if (EfiAtRuntime ()) {
834 // Don't use the cache at runtime
835 return EFI_NOT_FOUND
;
838 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
839 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
840 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
841 if (Entry
->DataSize
== 0) {
842 // Variable was deleted so return not found
843 return EFI_NOT_FOUND
;
844 } else if (Entry
->DataSize
> *DataSize
) {
845 // If the buffer is too small return correct size
846 *DataSize
= Entry
->DataSize
;
847 return EFI_BUFFER_TOO_SMALL
;
849 *DataSize
= Entry
->DataSize
;
851 CopyMem (Data
, Entry
->Data
, Entry
->DataSize
);
852 if (Attributes
!= NULL
) {
853 *Attributes
= Entry
->Attributes
;
861 return EFI_NOT_FOUND
;
865 Finds variable in storage blocks of volatile and non-volatile storage areas.
867 This code finds variable in storage blocks of volatile and non-volatile storage areas.
868 If VariableName is an empty string, then we just return the first
869 qualified variable without comparing VariableName and VendorGuid.
870 Otherwise, VariableName and VendorGuid are compared.
872 @param VariableName Name of the variable to be found
873 @param VendorGuid Vendor GUID to be found.
874 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
875 including the range searched and the target position.
876 @param Global Pointer to VARIABLE_GLOBAL structure, including
877 base of volatile variable storage area, base of
878 NV variable storage area, and a lock.
880 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
882 @retval EFI_SUCCESS Variable successfully found
883 @retval EFI_INVALID_PARAMETER Variable not found
888 IN CHAR16
*VariableName
,
889 IN EFI_GUID
*VendorGuid
,
890 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
891 IN VARIABLE_GLOBAL
*Global
894 VARIABLE_HEADER
*Variable
[2];
895 VARIABLE_HEADER
*InDeletedVariable
;
896 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
897 UINTN InDeletedStorageIndex
;
902 // 0: Volatile, 1: Non-Volatile
903 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
904 // make use of this mapping to implement search algorithme.
906 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
907 VariableStoreHeader
[1] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
910 // Start Pointers for the variable.
911 // Actual Data Pointer where data can be written.
913 Variable
[0] = GetStartPointer (VariableStoreHeader
[0]);
914 Variable
[1] = GetStartPointer (VariableStoreHeader
[1]);
916 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
917 return EFI_INVALID_PARAMETER
;
921 // Find the variable by walk through volatile and then non-volatile variable store
923 InDeletedVariable
= NULL
;
924 InDeletedStorageIndex
= 0;
925 for (Index
= 0; Index
< 2; Index
++) {
926 while (IsValidVariableHeader (Variable
[Index
]) && (Variable
[Index
] <= GetEndPointer (VariableStoreHeader
[Index
]))) {
927 if (Variable
[Index
]->State
== VAR_ADDED
||
928 Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
930 if (!EfiAtRuntime () || ((Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
931 if (VariableName
[0] == 0) {
932 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
933 InDeletedVariable
= Variable
[Index
];
934 InDeletedStorageIndex
= Index
;
936 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
937 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
938 PtrTrack
->CurrPtr
= Variable
[Index
];
939 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
944 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
945 Point
= (VOID
*) GetVariableNamePtr (Variable
[Index
]);
947 ASSERT (NameSizeOfVariable (Variable
[Index
]) != 0);
948 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
[Index
])) == 0) {
949 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
950 InDeletedVariable
= Variable
[Index
];
951 InDeletedStorageIndex
= Index
;
953 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
954 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
955 PtrTrack
->CurrPtr
= Variable
[Index
];
956 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
966 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
968 if (InDeletedVariable
!= NULL
) {
969 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
970 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
971 PtrTrack
->CurrPtr
= InDeletedVariable
;
972 PtrTrack
->Volatile
= (BOOLEAN
)(InDeletedStorageIndex
== 0);
976 PtrTrack
->CurrPtr
= NULL
;
977 return EFI_NOT_FOUND
;
983 This code finds variable in storage blocks (Volatile or Non-Volatile).
985 @param VariableName Name of Variable to be found.
986 @param VendorGuid Variable vendor GUID.
987 @param Attributes Attribute value of the variable found.
988 @param DataSize Size of Data found. If size is less than the
989 data, this value contains the required size.
990 @param Data Data pointer.
992 @return EFI_INVALID_PARAMETER Invalid parameter
993 @return EFI_SUCCESS Find the specified variable
994 @return EFI_NOT_FOUND Not found
995 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result
1000 RuntimeServiceGetVariable (
1001 IN CHAR16
*VariableName
,
1002 IN EFI_GUID
*VendorGuid
,
1003 OUT UINT32
*Attributes OPTIONAL
,
1004 IN OUT UINTN
*DataSize
,
1009 VARIABLE_POINTER_TRACK Variable
;
1012 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1013 return EFI_INVALID_PARAMETER
;
1016 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1019 // Find existing variable
1021 Status
= FindVariableInCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1022 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_SUCCESS
)){
1024 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
);
1028 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1029 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1036 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1037 ASSERT (VarDataSize
!= 0);
1039 if (*DataSize
>= VarDataSize
) {
1041 Status
= EFI_INVALID_PARAMETER
;
1045 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1046 if (Attributes
!= NULL
) {
1047 *Attributes
= Variable
.CurrPtr
->Attributes
;
1050 *DataSize
= VarDataSize
;
1051 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1052 UpdateVariableCache (VariableName
, VendorGuid
, Variable
.CurrPtr
->Attributes
, VarDataSize
, Data
);
1054 Status
= EFI_SUCCESS
;
1057 *DataSize
= VarDataSize
;
1058 Status
= EFI_BUFFER_TOO_SMALL
;
1063 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1071 This code Finds the Next available variable.
1073 @param VariableNameSize Size of the variable name
1074 @param VariableName Pointer to variable name
1075 @param VendorGuid Variable Vendor Guid
1077 @return EFI_INVALID_PARAMETER Invalid parameter
1078 @return EFI_SUCCESS Find the specified variable
1079 @return EFI_NOT_FOUND Not found
1080 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result
1085 RuntimeServiceGetNextVariableName (
1086 IN OUT UINTN
*VariableNameSize
,
1087 IN OUT CHAR16
*VariableName
,
1088 IN OUT EFI_GUID
*VendorGuid
1091 VARIABLE_POINTER_TRACK Variable
;
1095 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1096 return EFI_INVALID_PARAMETER
;
1099 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1101 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1102 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1106 if (VariableName
[0] != 0) {
1108 // If variable name is not NULL, get next variable
1110 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1115 // If both volatile and non-volatile variable store are parsed,
1118 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
1119 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
1120 if (!Variable
.Volatile
) {
1121 Variable
.StartPtr
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1122 Variable
.EndPtr
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
));
1124 Status
= EFI_NOT_FOUND
;
1128 Variable
.CurrPtr
= Variable
.StartPtr
;
1129 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
1134 // Variable is found
1136 if (IsValidVariableHeader (Variable
.CurrPtr
) && Variable
.CurrPtr
->State
== VAR_ADDED
) {
1137 if ((EfiAtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
1138 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
1139 ASSERT (VarNameSize
!= 0);
1141 if (VarNameSize
<= *VariableNameSize
) {
1144 GetVariableNamePtr (Variable
.CurrPtr
),
1149 &Variable
.CurrPtr
->VendorGuid
,
1152 Status
= EFI_SUCCESS
;
1154 Status
= EFI_BUFFER_TOO_SMALL
;
1157 *VariableNameSize
= VarNameSize
;
1162 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1166 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1172 This code sets variable in storage blocks (Volatile or Non-Volatile).
1174 @param VariableName Name of Variable to be found
1175 @param VendorGuid Variable vendor GUID
1176 @param Attributes Attribute value of the variable found
1177 @param DataSize Size of Data found. If size is less than the
1178 data, this value contains the required size.
1179 @param Data Data pointer
1181 @return EFI_INVALID_PARAMETER Invalid parameter
1182 @return EFI_SUCCESS Set successfully
1183 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable
1184 @return EFI_NOT_FOUND Not found
1185 @return EFI_WRITE_PROTECTED Variable is read-only
1190 RuntimeServiceSetVariable (
1191 IN CHAR16
*VariableName
,
1192 IN EFI_GUID
*VendorGuid
,
1193 IN UINT32 Attributes
,
1198 VARIABLE_POINTER_TRACK Variable
;
1200 VARIABLE_HEADER
*NextVariable
;
1202 UINTN VarNameOffset
;
1203 UINTN VarDataOffset
;
1207 UINTN
*VolatileOffset
;
1208 UINTN
*NonVolatileOffset
;
1211 EFI_PHYSICAL_ADDRESS Point
;
1214 // Check input parameters
1216 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1217 return EFI_INVALID_PARAMETER
;
1220 // Make sure if runtime bit is set, boot service bit is set also
1222 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1223 return EFI_INVALID_PARAMETER
;
1227 // The size of the VariableName, including the Unicode Null in bytes plus
1228 // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxHardwareErrorVariableSize)
1229 // bytes for HwErrRec, and FixedPcdGet32(PcdMaxVariableSize) bytes for the others.
1231 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1232 if ((DataSize
> FixedPcdGet32(PcdMaxHardwareErrorVariableSize
)) ||
1233 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> FixedPcdGet32(PcdMaxHardwareErrorVariableSize
))) {
1234 return EFI_INVALID_PARAMETER
;
1238 // The size of the VariableName, including the Unicode Null in bytes plus
1239 // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxVariableSize) bytes.
1241 if ((DataSize
> FixedPcdGet32(PcdMaxVariableSize
)) ||
1242 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> FixedPcdGet32(PcdMaxVariableSize
))) {
1243 return EFI_INVALID_PARAMETER
;
1247 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1250 Instance
= mVariableModuleGlobal
->FvbInstance
;
1251 VolatileOffset
= &mVariableModuleGlobal
->VolatileLastVariableOffset
;
1254 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
1256 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
1257 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;;
1259 // Parse non-volatile variable data and get last variable offset
1261 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
1262 while (IsValidVariableHeader (NextVariable
)) {
1263 NextVariable
= GetNextVariablePtr (NextVariable
);
1265 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
1268 NonVolatileOffset
= &mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1272 // Check whether the input variable is already existed
1275 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1276 if (Status
== EFI_SUCCESS
&& Variable
.CurrPtr
!= NULL
) {
1278 // Update/Delete existing variable
1280 Volatile
= Variable
.Volatile
;
1282 if (EfiAtRuntime ()) {
1284 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1285 // the volatile is ReadOnly, and SetVariable should be aborted and
1286 // return EFI_WRITE_PROTECTED.
1288 if (Variable
.Volatile
) {
1289 Status
= EFI_WRITE_PROTECTED
;
1293 // Only variable have NV attribute can be updated/deleted in Runtime
1295 if ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1296 Status
= EFI_INVALID_PARAMETER
;
1301 // Setting a data variable with no access, or zero DataSize attributes
1302 // specified causes it to be deleted.
1304 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1305 State
= Variable
.CurrPtr
->State
;
1306 State
&= VAR_DELETED
;
1308 Status
= UpdateVariableStore (
1309 &mVariableModuleGlobal
->VariableGlobal
,
1313 (UINTN
) &Variable
.CurrPtr
->State
,
1317 if (!EFI_ERROR (Status
)) {
1318 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1319 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1324 // If the variable is marked valid and the same data has been passed in
1325 // then return to the caller immediately.
1327 if (DataSizeOfVariable (Variable
.CurrPtr
) == DataSize
&&
1328 (CompareMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), DataSize
) == 0)) {
1330 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1331 Status
= EFI_SUCCESS
;
1333 } else if ((Variable
.CurrPtr
->State
== VAR_ADDED
) ||
1334 (Variable
.CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1337 // Mark the old variable as in delete transition
1339 State
= Variable
.CurrPtr
->State
;
1340 State
&= VAR_IN_DELETED_TRANSITION
;
1342 Status
= UpdateVariableStore (
1343 &mVariableModuleGlobal
->VariableGlobal
,
1347 (UINTN
) &Variable
.CurrPtr
->State
,
1351 if (EFI_ERROR (Status
)) {
1355 } else if (Status
== EFI_NOT_FOUND
) {
1357 // Create a new variable
1361 // Make sure we are trying to create a new variable.
1362 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1364 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1365 Status
= EFI_NOT_FOUND
;
1370 // Only variable have NV|RT attribute can be created in Runtime
1372 if (EfiAtRuntime () &&
1373 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1374 Status
= EFI_INVALID_PARAMETER
;
1379 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
1381 ASSERT (Status
== EFI_INVALID_PARAMETER
);
1386 // Function part - create a new variable and copy the data.
1387 // Both update a variable and create a variable will come here.
1389 // Tricky part: Use scratch data area at the end of volatile variable store
1390 // as a temporary storage.
1392 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1394 SetMem (NextVariable
, FixedPcdGet32(PcdMaxVariableSize
), 0xff);
1396 NextVariable
->StartId
= VARIABLE_DATA
;
1397 NextVariable
->Attributes
= Attributes
;
1399 // NextVariable->State = VAR_ADDED;
1401 NextVariable
->Reserved
= 0;
1402 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1403 VarNameSize
= StrSize (VariableName
);
1405 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1409 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1411 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1415 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1417 // There will be pad bytes after Data, the NextVariable->NameSize and
1418 // NextVariable->DataSize should not include pad size so that variable
1419 // service can get actual size in GetVariable
1421 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1422 NextVariable
->DataSize
= (UINT32
)DataSize
;
1425 // The actual size of the variable that stores in storage should
1426 // include pad size.
1428 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1429 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1431 // Create a nonvolatile variable
1435 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1436 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
1438 if (EfiAtRuntime ()) {
1439 Status
= EFI_OUT_OF_RESOURCES
;
1443 // Perform garbage collection & reclaim operation
1445 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
, NonVolatileOffset
, FALSE
, Variable
.CurrPtr
);
1446 if (EFI_ERROR (Status
)) {
1450 // If still no enough space, return out of resources
1452 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1453 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
1455 Status
= EFI_OUT_OF_RESOURCES
;
1463 // 1. Write variable header
1464 // 2. Set variable state to header valid
1465 // 3. Write variable data
1466 // 4. Set variable state to valid
1471 Status
= UpdateVariableStore (
1472 &mVariableModuleGlobal
->VariableGlobal
,
1477 sizeof (VARIABLE_HEADER
),
1478 (UINT8
*) NextVariable
1481 if (EFI_ERROR (Status
)) {
1488 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1489 Status
= UpdateVariableStore (
1490 &mVariableModuleGlobal
->VariableGlobal
,
1495 sizeof (VARIABLE_HEADER
),
1496 (UINT8
*) NextVariable
1499 if (EFI_ERROR (Status
)) {
1505 Status
= UpdateVariableStore (
1506 &mVariableModuleGlobal
->VariableGlobal
,
1510 *NonVolatileOffset
+ sizeof (VARIABLE_HEADER
),
1511 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1512 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1515 if (EFI_ERROR (Status
)) {
1521 NextVariable
->State
= VAR_ADDED
;
1522 Status
= UpdateVariableStore (
1523 &mVariableModuleGlobal
->VariableGlobal
,
1528 sizeof (VARIABLE_HEADER
),
1529 (UINT8
*) NextVariable
1532 if (EFI_ERROR (Status
)) {
1536 *NonVolatileOffset
= HEADER_ALIGN (*NonVolatileOffset
+ VarSize
);
1540 // Create a volatile variable
1544 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1545 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1547 // Perform garbage collection & reclaim operation
1549 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
, VolatileOffset
, TRUE
, Variable
.CurrPtr
);
1550 if (EFI_ERROR (Status
)) {
1554 // If still no enough space, return out of resources
1556 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1557 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1559 Status
= EFI_OUT_OF_RESOURCES
;
1566 NextVariable
->State
= VAR_ADDED
;
1567 Status
= UpdateVariableStore (
1568 &mVariableModuleGlobal
->VariableGlobal
,
1574 (UINT8
*) NextVariable
1577 if (EFI_ERROR (Status
)) {
1581 *VolatileOffset
= HEADER_ALIGN (*VolatileOffset
+ VarSize
);
1584 // Mark the old variable as deleted
1586 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
.CurrPtr
!= NULL
) {
1587 State
= Variable
.CurrPtr
->State
;
1588 State
&= VAR_DELETED
;
1590 Status
= UpdateVariableStore (
1591 &mVariableModuleGlobal
->VariableGlobal
,
1595 (UINTN
) &Variable
.CurrPtr
->State
,
1600 if (!EFI_ERROR (Status
)) {
1601 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1602 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1607 Status
= EFI_SUCCESS
;
1608 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1609 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1612 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
1613 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1620 This code returns information about the EFI variables.
1622 @param Attributes Attributes bitmask to specify the type of variables
1623 on which to return information.
1624 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1625 for the EFI variables associated with the attributes specified.
1626 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1627 for EFI variables associated with the attributes specified.
1628 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
1629 associated with the attributes specified.
1631 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
1632 @return EFI_SUCCESS Query successfully.
1633 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
1638 RuntimeServiceQueryVariableInfo (
1639 IN UINT32 Attributes
,
1640 OUT UINT64
*MaximumVariableStorageSize
,
1641 OUT UINT64
*RemainingVariableStorageSize
,
1642 OUT UINT64
*MaximumVariableSize
1645 VARIABLE_HEADER
*Variable
;
1646 VARIABLE_HEADER
*NextVariable
;
1647 UINT64 VariableSize
;
1648 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1650 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1651 return EFI_INVALID_PARAMETER
;
1654 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1656 // Make sure the Attributes combination is supported by the platform.
1658 return EFI_UNSUPPORTED
;
1659 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1661 // Make sure if runtime bit is set, boot service bit is set also.
1663 return EFI_INVALID_PARAMETER
;
1664 } else if (EfiAtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
1666 // Make sure RT Attribute is set if we are in Runtime phase.
1668 return EFI_INVALID_PARAMETER
;
1671 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1673 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1675 // Query is Volatile related.
1677 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
1680 // Query is Non-Volatile related.
1682 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1686 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1687 // with the storage size (excluding the storage header size).
1689 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1690 *RemainingVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1693 // Let *MaximumVariableSize be FixedPcdGet32(PcdMaxVariableSize) with the exception of the variable header size.
1695 *MaximumVariableSize
= FixedPcdGet32(PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
1698 // Harware error record variable needs larger size.
1700 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1701 *MaximumVariableSize
= FixedPcdGet32(PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
1705 // Point to the starting address of the variables.
1707 Variable
= GetStartPointer (VariableStoreHeader
);
1710 // Now walk through the related variable store.
1712 while (IsValidVariableHeader (Variable
) && (Variable
< GetEndPointer (VariableStoreHeader
))) {
1713 NextVariable
= GetNextVariablePtr (Variable
);
1714 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1716 if (EfiAtRuntime ()) {
1718 // we don't take the state of the variables in mind
1719 // when calculating RemainingVariableStorageSize,
1720 // since the space occupied by variables not marked with
1721 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1723 *RemainingVariableStorageSize
-= VariableSize
;
1726 // Only care about Variables with State VAR_ADDED,because
1727 // the space not marked as VAR_ADDED is reclaimable now.
1729 if (Variable
->State
== VAR_ADDED
) {
1730 *RemainingVariableStorageSize
-= VariableSize
;
1735 // Go to the next one
1737 Variable
= NextVariable
;
1740 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
1741 *MaximumVariableSize
= 0;
1742 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
1743 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
1746 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1752 Notification function of EVT_GROUP_READY_TO_BOOT event group.
1754 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
1755 When the Boot Manager is about to load and execute a boot option, it reclaims variable
1756 storage if free size is below the threshold.
1758 @param Event Event whose notification function is being invoked
1759 @param Context Pointer to the notification function's context
1772 VarSize
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1773 Status
= EFI_SUCCESS
;
1776 // Check if the free area is blow a threshold
1778 if ((VarSize
- mVariableModuleGlobal
->NonVolatileLastVariableOffset
) < VARIABLE_RECLAIM_THRESHOLD
) {
1780 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1781 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1785 ASSERT_EFI_ERROR (Status
);
1790 Initializes variable store area for non-volatile and volatile variable.
1792 @param ImageHandle The Image handle of this driver.
1793 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
1795 @retval EFI_SUCCESS Function successfully executed.
1796 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
1800 VariableCommonInitialize (
1801 IN EFI_HANDLE ImageHandle
,
1802 IN EFI_SYSTEM_TABLE
*SystemTable
1806 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1808 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
1809 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1810 VARIABLE_HEADER
*NextVariable
;
1812 EFI_PHYSICAL_ADDRESS FvVolHdr
;
1813 UINT64 TempVariableStoreHeader
;
1814 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
1819 UINT64 VariableStoreBase
;
1820 UINT64 VariableStoreLength
;
1821 EFI_EVENT ReadyToBootEvent
;
1823 Status
= EFI_SUCCESS
;
1825 // Allocate runtime memory for variable driver global structure.
1827 mVariableModuleGlobal
= AllocateRuntimePool (sizeof (VARIABLE_MODULE_GLOBAL
));
1828 if (mVariableModuleGlobal
== NULL
) {
1829 return EFI_OUT_OF_RESOURCES
;
1832 EfiInitializeLock(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
1833 mVariableModuleGlobal
->VariableGlobal
.ReentrantState
= 0;
1836 // Allocate memory for volatile variable store
1838 VolatileVariableStore
= AllocateRuntimePool (FixedPcdGet32(PcdVariableStoreSize
) + FixedPcdGet32(PcdMaxVariableSize
));
1839 if (VolatileVariableStore
== NULL
) {
1840 FreePool (mVariableModuleGlobal
);
1841 return EFI_OUT_OF_RESOURCES
;
1844 SetMem (VolatileVariableStore
, FixedPcdGet32(PcdVariableStoreSize
) + FixedPcdGet32(PcdMaxVariableSize
), 0xff);
1847 // Variable Specific Data
1849 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
1850 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
1852 VolatileVariableStore
->Signature
= VARIABLE_STORE_SIGNATURE
;
1853 VolatileVariableStore
->Size
= FixedPcdGet32(PcdVariableStoreSize
);
1854 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
1855 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
1856 VolatileVariableStore
->Reserved
= 0;
1857 VolatileVariableStore
->Reserved1
= 0;
1860 // Get non volatile varaible store
1863 TempVariableStoreHeader
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableBase
);
1864 VariableStoreBase
= TempVariableStoreHeader
+ \
1865 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1866 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
1867 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1869 // Mark the variable storage region of the FLASH as RUNTIME
1871 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
1872 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
1873 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
1875 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
1876 if (EFI_ERROR (Status
)) {
1880 Status
= gDS
->SetMemorySpaceAttributes (
1883 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
1885 if (EFI_ERROR (Status
)) {
1889 // Get address of non volatile variable store base
1891 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
1897 // Find the Correct Instance of the FV Block Service.
1900 CurrPtr
= (CHAR8
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1901 while (EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
) == EFI_SUCCESS
) {
1902 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
1903 if (CurrPtr
>= (CHAR8
*) FwVolHeader
&& CurrPtr
< (((CHAR8
*) FwVolHeader
) + FwVolHeader
->FvLength
)) {
1904 mVariableModuleGlobal
->FvbInstance
= Instance
;
1911 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) CurrPtr
;
1912 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
1913 if (~VariableStoreHeader
->Size
== 0) {
1914 Status
= UpdateVariableStore (
1915 &mVariableModuleGlobal
->VariableGlobal
,
1918 mVariableModuleGlobal
->FvbInstance
,
1919 (UINTN
) &VariableStoreHeader
->Size
,
1921 (UINT8
*) &VariableStoreLength
1924 // As Variables are stored in NV storage, which are slow devices,such as flash.
1925 // Variable operation may skip checking variable program result to improve performance,
1926 // We can assume Variable program is OK through some check point.
1927 // Variable Store Size Setting should be the first Variable write operation,
1928 // We can assume all Read/Write is OK if we can set Variable store size successfully.
1929 // If write fail, we will assert here
1931 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
1933 if (EFI_ERROR (Status
)) {
1938 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) CurrPtr
);
1940 // Parse non-volatile variable data and get last variable offset
1942 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) CurrPtr
);
1943 Status
= EFI_SUCCESS
;
1945 while (IsValidVariableHeader (NextVariable
)) {
1946 NextVariable
= GetNextVariablePtr (NextVariable
);
1949 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) CurrPtr
;
1952 // Check if the free area is really free.
1954 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
1955 Data
= ((UINT8
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)[Index
];
1958 // There must be something wrong in variable store, do reclaim operation.
1961 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1962 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1967 if (EFI_ERROR (Status
)) {
1976 // Register the event handling function to reclaim variable for OS usage.
1978 Status
= EfiCreateEventReadyToBootEx (
1987 if (EFI_ERROR (Status
)) {
1988 FreePool (mVariableModuleGlobal
);
1989 FreePool (VolatileVariableStore
);
1996 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
1998 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
1999 It convers pointer to new virtual address.
2001 @param Event Event whose notification function is being invoked
2002 @param Context Pointer to the notification function's context
2007 VariableClassAddressChangeEvent (
2014 (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
2018 (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
2020 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
2025 Variable Driver main entry point. The Variable driver places the 4 EFI
2026 runtime services in the EFI System Table and installs arch protocols
2027 for variable read and write services being availible. It also registers
2028 notification function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
2030 @param[in] ImageHandle The firmware allocated handle for the EFI image.
2031 @param[in] SystemTable A pointer to the EFI System Table.
2033 @retval EFI_SUCCESS Variable service successfully initialized.
2038 VariableServiceInitialize (
2039 IN EFI_HANDLE ImageHandle
,
2040 IN EFI_SYSTEM_TABLE
*SystemTable
2045 Status
= VariableCommonInitialize (ImageHandle
, SystemTable
);
2046 ASSERT_EFI_ERROR (Status
);
2048 SystemTable
->RuntimeServices
->GetVariable
= RuntimeServiceGetVariable
;
2049 SystemTable
->RuntimeServices
->GetNextVariableName
= RuntimeServiceGetNextVariableName
;
2050 SystemTable
->RuntimeServices
->SetVariable
= RuntimeServiceSetVariable
;
2051 SystemTable
->RuntimeServices
->QueryVariableInfo
= RuntimeServiceQueryVariableInfo
;
2054 // Now install the Variable Runtime Architectural Protocol on a new handle
2056 Status
= gBS
->InstallMultipleProtocolInterfaces (
2058 &gEfiVariableArchProtocolGuid
, NULL
,
2059 &gEfiVariableWriteArchProtocolGuid
, NULL
,
2062 ASSERT_EFI_ERROR (Status
);
2064 Status
= gBS
->CreateEventEx (
2067 VariableClassAddressChangeEvent
,
2069 &gEfiEventVirtualAddressChangeGuid
,
2070 &mVirtualAddressChangeEvent
2072 ASSERT_EFI_ERROR (Status
);