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.
20 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
21 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
22 EFI_HANDLE mHandle
= NULL
;
25 // The current Hii implementation accesses this variable a larg # of times on every boot.
26 // Other common variables are only accessed a single time. This is why this cache algorithm
27 // only targets a single variable. Probably to get an performance improvement out of
28 // a Cache you would need a cache that improves the search performance for a variable.
30 VARIABLE_CACHE_ENTRY mVariableCache
[] = {
32 &gEfiGlobalVariableGuid
,
40 GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
45 // This is a temperary function which will be removed
46 // when EfiAcquireLock in UefiLib can handle the
47 // the call in UEFI Runtimer driver in RT phase.
50 AcquireLockOnlyAtBootTime (
54 if (!EfiAtRuntime ()) {
55 EfiAcquireLock (Lock
);
60 // This is a temperary function which will be removed
61 // when EfiAcquireLock in UefiLib can handle the
62 // the call in UEFI Runtimer driver in RT phase.
65 ReleaseLockOnlyAtBootTime (
69 if (!EfiAtRuntime ()) {
70 EfiReleaseLock (Lock
);
76 Routine used to track statistical information about variable usage.
77 The data is stored in the EFI system table so it can be accessed later.
78 VariableInfo.efi can dump out the table. Only Boot Services variable
79 accesses are tracked by this code. The PcdVariableCollectStatistics
80 build flag controls if this feature is enabled.
82 A read that hits in the cache will have Read and Cache true for
83 the transaction. Data is allocated by this routine, but never
86 @param[in] VariableName Name of the Variable to track
87 @param[in] VendorGuid Guid of the Variable to track
88 @param[in] Volatile TRUE if volatile FALSE if non-volatile
89 @param[in] Read TRUE if GetVariable() was called
90 @param[in] Write TRUE if SetVariable() was called
91 @param[in] Delete TRUE if deleted via SetVariable()
92 @param[in] Cache TRUE for a cache hit.
97 IN CHAR16
*VariableName
,
98 IN EFI_GUID
*VendorGuid
,
106 VARIABLE_INFO_ENTRY
*Entry
;
108 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
110 if (EfiAtRuntime ()) {
111 // Don't collect statistics at runtime
115 if (gVariableInfo
== NULL
) {
117 // on the first call allocate a entry and place a pointer to it in
118 // the EFI System Table
120 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
121 ASSERT (gVariableInfo
!= NULL
);
123 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
124 gVariableInfo
->Name
= AllocatePool (StrLen (VariableName
));
125 StrCpy (gVariableInfo
->Name
, VariableName
);
126 gVariableInfo
->Volatile
= Volatile
;
128 gBS
->InstallConfigurationTable (&gEfiVariableInfoGuid
, gVariableInfo
);
132 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
133 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
134 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
142 Entry
->DeleteCount
++;
152 if (Entry
->Next
== NULL
) {
154 // If the entry is not in the table add it.
155 // Next iteration of the loop will fill in the data
157 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
158 ASSERT (Entry
->Next
!= NULL
);
160 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
161 Entry
->Next
->Name
= AllocatePool (StrLen (VariableName
));
162 StrCpy (Entry
->Next
->Name
, VariableName
);
163 Entry
->Next
->Volatile
= Volatile
;
172 IsValidVariableHeader (
173 IN VARIABLE_HEADER
*Variable
179 This code checks if variable header is valid or not.
182 Variable Pointer to the Variable Header.
185 TRUE Variable header is valid.
186 FALSE Variable header is not valid.
190 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
199 UpdateVariableStore (
200 IN VARIABLE_GLOBAL
*Global
,
202 IN BOOLEAN SetByIndex
,
204 IN UINTN DataPtrIndex
,
212 This function writes data to the FWH at the correct LBA even if the LBAs
217 Global - Pointer to VARAIBLE_GLOBAL structure
218 Volatile - If the Variable is Volatile or Non-Volatile
219 SetByIndex - TRUE: Target pointer is given as index
220 FALSE: Target pointer is absolute
221 Instance - Instance of FV Block services
222 DataPtrIndex - Pointer to the Data from the end of VARIABLE_STORE_HEADER
224 DataSize - Size of data to be written.
225 Buffer - Pointer to the buffer from which data is written
229 EFI_INVALID_PARAMETER - Parameters not valid
230 EFI_SUCCESS - Variable store successfully updated
234 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
242 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
243 VARIABLE_STORE_HEADER
*VolatileBase
;
244 EFI_PHYSICAL_ADDRESS FvVolHdr
;
245 EFI_PHYSICAL_ADDRESS DataPtr
;
249 DataPtr
= DataPtrIndex
;
252 // Check if the Data is Volatile
255 EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
);
256 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
258 // Data Pointer should point to the actual Address where data is to be
262 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
265 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
266 return EFI_INVALID_PARAMETER
;
270 // Data Pointer should point to the actual Address where data is to be
273 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
275 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
278 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
279 return EFI_INVALID_PARAMETER
;
283 // If Volatile Variable just do a simple mem copy.
285 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
290 // If we are here we are dealing with Non-Volatile Variables
292 LinearOffset
= (UINTN
) FwVolHeader
;
293 CurrWritePtr
= (UINTN
) DataPtr
;
294 CurrWriteSize
= DataSize
;
298 if (CurrWritePtr
< LinearOffset
) {
299 return EFI_INVALID_PARAMETER
;
302 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
303 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
305 // Check to see if the Variable Writes are spanning through multiple
308 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
309 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
310 Status
= EfiFvbWriteBlock (
313 (UINTN
) (CurrWritePtr
- LinearOffset
),
319 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
320 Status
= EfiFvbWriteBlock (
323 (UINTN
) (CurrWritePtr
- LinearOffset
),
327 if (EFI_ERROR (Status
)) {
331 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
332 CurrBuffer
= CurrBuffer
+ Size
;
333 CurrWriteSize
= CurrWriteSize
- Size
;
337 LinearOffset
+= PtrBlockMapEntry
->Length
;
346 VARIABLE_STORE_STATUS
347 GetVariableStoreStatus (
348 IN VARIABLE_STORE_HEADER
*VarStoreHeader
354 This code gets the current status of Variable Store.
358 VarStoreHeader Pointer to the Variable Store Header.
362 EfiRaw Variable store status is raw
363 EfiValid Variable store status is valid
364 EfiInvalid Variable store status is invalid
368 if (VarStoreHeader
->Signature
== VARIABLE_STORE_SIGNATURE
&&
369 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
370 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
374 } else if (VarStoreHeader
->Signature
== 0xffffffff &&
375 VarStoreHeader
->Size
== 0xffffffff &&
376 VarStoreHeader
->Format
== 0xff &&
377 VarStoreHeader
->State
== 0xff
389 IN VARIABLE_HEADER
*Variable
395 This code gets the size of name of variable.
399 Variable Pointer to the Variable Header.
403 UINTN Size of variable in bytes
407 if (Variable
->State
== (UINT8
) (-1) ||
408 Variable
->DataSize
== (UINT32
) -1 ||
409 Variable
->NameSize
== (UINT32
) -1 ||
410 Variable
->Attributes
== (UINT32
) -1) {
413 return (UINTN
) Variable
->NameSize
;
418 IN VARIABLE_HEADER
*Variable
424 This code gets the size of name of variable.
428 Variable Pointer to the Variable Header.
432 UINTN Size of variable in bytes
436 if (Variable
->State
== (UINT8
) -1 ||
437 Variable
->DataSize
== (UINT32
) -1 ||
438 Variable
->NameSize
== (UINT32
) -1 ||
439 Variable
->Attributes
== (UINT32
) -1) {
442 return (UINTN
) Variable
->DataSize
;
447 IN VARIABLE_HEADER
*Variable
453 This code gets the pointer to the variable name.
457 Variable Pointer to the Variable Header.
461 CHAR16* Pointer to Variable Name
466 return (CHAR16
*) (Variable
+ 1);
471 IN VARIABLE_HEADER
*Variable
477 This code gets the pointer to the variable data.
481 Variable Pointer to the Variable Header.
485 UINT8* Pointer to Variable Data
492 // Be careful about pad size for alignment
494 Value
= (UINTN
) GetVariableNamePtr (Variable
);
495 Value
+= NameSizeOfVariable (Variable
);
496 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
498 return (UINT8
*) Value
;
504 IN VARIABLE_HEADER
*Variable
510 This code gets the pointer to the next variable header.
514 Variable Pointer to the Variable Header.
518 VARIABLE_HEADER* Pointer to next variable header.
524 if (!IsValidVariableHeader (Variable
)) {
528 Value
= (UINTN
) GetVariableDataPtr (Variable
);
529 Value
+= DataSizeOfVariable (Variable
);
530 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
533 // Be careful about pad size for alignment
535 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
540 IN VARIABLE_STORE_HEADER
*VarStoreHeader
546 This code gets the pointer to the first variable memory pointer byte
550 VarStoreHeader Pointer to the Variable Store Header.
554 VARIABLE_HEADER* Pointer to last unavailable Variable Header
559 // The end of variable store
561 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
566 IN VARIABLE_STORE_HEADER
*VarStoreHeader
572 This code gets the pointer to the last variable memory pointer byte
576 VarStoreHeader Pointer to the Variable Store Header.
580 VARIABLE_HEADER* Pointer to last unavailable Variable Header
585 // The end of variable store
587 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
593 IN EFI_PHYSICAL_ADDRESS VariableBase
,
594 OUT UINTN
*LastVariableOffset
,
595 IN BOOLEAN IsVolatile
,
596 IN VARIABLE_HEADER
*UpdatingVariable
602 Variable store garbage collection and reclaim operation
606 VariableBase Base address of variable store
607 LastVariableOffset Offset of last variable
608 IsVolatile The variable store is volatile or not,
609 if it is non-volatile, need FTW
617 VARIABLE_HEADER
*Variable
;
618 VARIABLE_HEADER
*AddedVariable
;
619 VARIABLE_HEADER
*NextVariable
;
620 VARIABLE_HEADER
*NextAddedVariable
;
621 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
623 UINTN MaximumBufferSize
;
625 UINTN VariableNameSize
;
626 UINTN UpdatingVariableNameSize
;
633 CHAR16
*VariableNamePtr
;
634 CHAR16
*UpdatingVariableNamePtr
;
636 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
639 // Start Pointers for the variable.
641 Variable
= GetStartPointer (VariableStoreHeader
);
642 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
644 while (IsValidVariableHeader (Variable
)) {
645 NextVariable
= GetNextVariablePtr (Variable
);
646 if (Variable
->State
== VAR_ADDED
||
647 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
649 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
650 MaximumBufferSize
+= VariableSize
;
653 Variable
= NextVariable
;
657 // Reserve the 1 Bytes with Oxff to identify the
658 // end of the variable buffer.
660 MaximumBufferSize
+= 1;
661 ValidBuffer
= AllocatePool (MaximumBufferSize
);
662 if (ValidBuffer
== NULL
) {
663 return EFI_OUT_OF_RESOURCES
;
666 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
669 // Copy variable store header
671 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
672 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
675 // Start Pointers for the variable.
679 // Reinstall all ADDED variables as long as they are not identical to Updating Variable
681 Variable
= GetStartPointer (VariableStoreHeader
);
682 while (IsValidVariableHeader (Variable
)) {
683 NextVariable
= GetNextVariablePtr (Variable
);
684 if (Variable
->State
== VAR_ADDED
) {
685 if (UpdatingVariable
!= NULL
) {
686 if (UpdatingVariable
== Variable
) {
687 Variable
= NextVariable
;
691 VariableNameSize
= NameSizeOfVariable(Variable
);
692 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
694 VariableNamePtr
= GetVariableNamePtr (Variable
);
695 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
696 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
697 VariableNameSize
== UpdatingVariableNameSize
&&
698 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
699 Variable
= NextVariable
;
703 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
704 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
705 CurrPtr
+= VariableSize
;
707 Variable
= NextVariable
;
711 // Reinstall the variable being updated if it is not NULL
713 if (UpdatingVariable
!= NULL
) {
714 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
715 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
716 CurrPtr
+= VariableSize
;
720 // Reinstall all in delete transition variables
722 Variable
= GetStartPointer (VariableStoreHeader
);
723 while (IsValidVariableHeader (Variable
)) {
724 NextVariable
= GetNextVariablePtr (Variable
);
725 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
728 // Buffer has cached all ADDED variable.
729 // Per IN_DELETED variable, we have to guarantee that
730 // no ADDED one in previous buffer.
734 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
735 while (IsValidVariableHeader (AddedVariable
)) {
736 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
737 NameSize
= NameSizeOfVariable (AddedVariable
);
738 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
739 NameSize
== NameSizeOfVariable (Variable
)
741 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
742 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
743 if (CompareMem (Point0
, Point1
, NameSizeOfVariable (AddedVariable
)) == 0) {
748 AddedVariable
= NextAddedVariable
;
752 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED
754 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
755 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
756 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
757 CurrPtr
+= VariableSize
;
761 Variable
= NextVariable
;
766 // If volatile variable store, just copy valid buffer
768 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
769 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
770 Status
= EFI_SUCCESS
;
773 // If non-volatile variable store, perform FTW here.
775 Status
= FtwVariableSpace (
778 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
781 if (!EFI_ERROR (Status
)) {
782 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
784 *LastVariableOffset
= 0;
787 FreePool (ValidBuffer
);
794 Update the Cache with Variable information. These are the same
795 arguments as the EFI Variable services.
797 @param[in] VariableName Name of variable
798 @param[in] VendorGuid Guid of variable
799 @param[in] Attribute Attribue of the variable
800 @param[in] DataSize Size of data. 0 means delete
801 @param[in] Data Variable data
805 UpdateVariableCache (
806 IN CHAR16
*VariableName
,
807 IN EFI_GUID
*VendorGuid
,
808 IN UINT32 Attributes
,
813 VARIABLE_CACHE_ENTRY
*Entry
;
816 if (EfiAtRuntime ()) {
817 // Don't use the cache at runtime
821 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
822 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
823 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
824 Entry
->Attributes
= Attributes
;
827 if (Entry
->DataSize
!= 0) {
828 FreePool (Entry
->Data
);
830 Entry
->DataSize
= DataSize
;
831 } else if (DataSize
== Entry
->DataSize
) {
832 CopyMem (Entry
->Data
, Data
, DataSize
);
834 Entry
->Data
= AllocatePool (DataSize
);
835 Entry
->DataSize
= DataSize
;
836 CopyMem (Entry
->Data
, Data
, DataSize
);
845 Search the cache to see if the variable is in the cache.
847 @param[in] VariableName Name of variable
848 @param[in] VendorGuid Guid of variable
849 @param[in] Attribute Attribue returned
850 @param[in] DataSize Size of data returned
851 @param[in] Data Variable data returned
853 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.
854 @retval other Not found.
858 FindVariableInCache (
859 IN CHAR16
*VariableName
,
860 IN EFI_GUID
*VendorGuid
,
861 OUT UINT32
*Attributes OPTIONAL
,
862 IN OUT UINTN
*DataSize
,
866 VARIABLE_CACHE_ENTRY
*Entry
;
869 if (EfiAtRuntime ()) {
870 // Don't use the cache at runtime
871 return EFI_NOT_FOUND
;
874 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
875 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
876 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
877 if (Entry
->DataSize
== 0) {
878 // Variable was deleted so return not found
879 return EFI_NOT_FOUND
;
880 } else if (Entry
->DataSize
> *DataSize
) {
881 // If the buffer is too small return correct size
882 *DataSize
= Entry
->DataSize
;
883 return EFI_BUFFER_TOO_SMALL
;
885 *DataSize
= Entry
->DataSize
;
887 CopyMem (Data
, Entry
->Data
, Entry
->DataSize
);
888 if (Attributes
!= NULL
) {
889 *Attributes
= Entry
->Attributes
;
897 return EFI_NOT_FOUND
;
903 IN CHAR16
*VariableName
,
904 IN EFI_GUID
*VendorGuid
,
905 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
906 IN VARIABLE_GLOBAL
*Global
912 This code finds variable in storage blocks (Volatile or Non-Volatile)
916 VariableName Name of the variable to be found
917 VendorGuid Vendor GUID to be found.
918 PtrTrack Variable Track Pointer structure that contains
919 Variable Information.
920 Contains the pointer of Variable header.
921 Global VARIABLE_GLOBAL pointer
929 VARIABLE_HEADER
*Variable
[2];
930 VARIABLE_HEADER
*InDeletedVariable
;
931 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
932 UINTN InDeletedStorageIndex
;
937 // 0: Volatile, 1: Non-Volatile
938 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
939 // make use of this mapping to implement search algorithme.
941 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
942 VariableStoreHeader
[1] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
945 // Start Pointers for the variable.
946 // Actual Data Pointer where data can be written.
948 Variable
[0] = GetStartPointer (VariableStoreHeader
[0]);
949 Variable
[1] = GetStartPointer (VariableStoreHeader
[1]);
951 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
952 return EFI_INVALID_PARAMETER
;
956 // Find the variable by walk through volatile and then non-volatile variable store
958 InDeletedVariable
= NULL
;
959 InDeletedStorageIndex
= 0;
960 for (Index
= 0; Index
< 2; Index
++) {
961 while (IsValidVariableHeader (Variable
[Index
]) && (Variable
[Index
] <= GetEndPointer (VariableStoreHeader
[Index
]))) {
962 if (Variable
[Index
]->State
== VAR_ADDED
||
963 Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
965 if (!EfiAtRuntime () || (Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
966 if (VariableName
[0] == 0) {
967 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
968 InDeletedVariable
= Variable
[Index
];
969 InDeletedStorageIndex
= Index
;
971 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
972 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
973 PtrTrack
->CurrPtr
= Variable
[Index
];
974 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
979 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
980 Point
= (VOID
*) GetVariableNamePtr (Variable
[Index
]);
982 ASSERT (NameSizeOfVariable (Variable
[Index
]) != 0);
983 if (!CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
[Index
]))) {
984 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
985 InDeletedVariable
= Variable
[Index
];
986 InDeletedStorageIndex
= Index
;
988 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
989 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
990 PtrTrack
->CurrPtr
= Variable
[Index
];
991 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
1001 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
1003 if (InDeletedVariable
!= NULL
) {
1004 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
1005 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
1006 PtrTrack
->CurrPtr
= InDeletedVariable
;
1007 PtrTrack
->Volatile
= (BOOLEAN
)(InDeletedStorageIndex
== 0);
1011 PtrTrack
->CurrPtr
= NULL
;
1012 return EFI_NOT_FOUND
;
1019 Routine Description:
1021 This code finds variable in storage blocks (Volatile or Non-Volatile)
1025 VariableName Name of Variable to be found
1026 VendorGuid Variable vendor GUID
1027 Attributes OPTIONAL Attribute value of the variable found
1028 DataSize Size of Data found. If size is less than the
1029 data, this value contains the required size.
1031 Global Pointer to VARIABLE_GLOBAL structure
1032 Instance Instance of the Firmware Volume.
1036 EFI_INVALID_PARAMETER - Invalid parameter
1037 EFI_SUCCESS - Find the specified variable
1038 EFI_NOT_FOUND - Not found
1039 EFI_BUFFER_TO_SMALL - DataSize is too small for the result
1045 RuntimeServiceGetVariable (
1046 IN CHAR16
*VariableName
,
1047 IN EFI_GUID
*VendorGuid
,
1048 OUT UINT32
*Attributes OPTIONAL
,
1049 IN OUT UINTN
*DataSize
,
1054 VARIABLE_POINTER_TRACK Variable
;
1057 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1058 return EFI_INVALID_PARAMETER
;
1061 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1064 // Find existing variable
1066 Status
= FindVariableInCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1067 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_SUCCESS
)){
1069 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
);
1073 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1074 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1081 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1082 ASSERT (VarDataSize
!= 0);
1084 if (*DataSize
>= VarDataSize
) {
1086 Status
= EFI_INVALID_PARAMETER
;
1090 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1091 if (Attributes
!= NULL
) {
1092 *Attributes
= Variable
.CurrPtr
->Attributes
;
1095 *DataSize
= VarDataSize
;
1096 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1097 UpdateVariableCache (VariableName
, VendorGuid
, Variable
.CurrPtr
->Attributes
, VarDataSize
, Data
);
1099 Status
= EFI_SUCCESS
;
1102 *DataSize
= VarDataSize
;
1103 Status
= EFI_BUFFER_TOO_SMALL
;
1108 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1116 Routine Description:
1118 This code Finds the Next available variable
1122 VariableNameSize Size of the variable
1123 VariableName Pointer to variable name
1124 VendorGuid Variable Vendor Guid
1125 Global VARIABLE_GLOBAL structure pointer.
1126 Instance FV instance
1135 RuntimeServiceGetNextVariableName (
1136 IN OUT UINTN
*VariableNameSize
,
1137 IN OUT CHAR16
*VariableName
,
1138 IN OUT EFI_GUID
*VendorGuid
1141 VARIABLE_POINTER_TRACK Variable
;
1145 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1146 return EFI_INVALID_PARAMETER
;
1149 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1151 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1152 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1156 if (VariableName
[0] != 0) {
1158 // If variable name is not NULL, get next variable
1160 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1165 // If both volatile and non-volatile variable store are parsed,
1168 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
1169 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
1170 if (!Variable
.Volatile
) {
1171 Variable
.StartPtr
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1172 Variable
.EndPtr
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
));
1174 Status
= EFI_NOT_FOUND
;
1178 Variable
.CurrPtr
= Variable
.StartPtr
;
1179 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
1184 // Variable is found
1186 if (IsValidVariableHeader (Variable
.CurrPtr
) && Variable
.CurrPtr
->State
== VAR_ADDED
) {
1187 if (!(EfiAtRuntime () && !(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
))) {
1188 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
1189 ASSERT (VarNameSize
!= 0);
1191 if (VarNameSize
<= *VariableNameSize
) {
1194 GetVariableNamePtr (Variable
.CurrPtr
),
1199 &Variable
.CurrPtr
->VendorGuid
,
1202 Status
= EFI_SUCCESS
;
1204 Status
= EFI_BUFFER_TOO_SMALL
;
1207 *VariableNameSize
= VarNameSize
;
1212 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1216 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1223 Routine Description:
1225 This code sets variable in storage blocks (Volatile or Non-Volatile)
1229 VariableName Name of Variable to be found
1230 VendorGuid Variable vendor GUID
1231 Attributes Attribute value of the variable found
1232 DataSize Size of Data found. If size is less than the
1233 data, this value contains the required size.
1235 Global Pointer to VARIABLE_GLOBAL structure
1236 VolatileOffset The offset of last volatile variable
1237 NonVolatileOffset The offset of last non-volatile variable
1238 Instance Instance of the Firmware Volume.
1242 EFI_INVALID_PARAMETER - Invalid parameter
1243 EFI_SUCCESS - Set successfully
1244 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
1245 EFI_NOT_FOUND - Not found
1246 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
1247 EFI_WRITE_PROTECTED - Variable is read-only
1252 RuntimeServiceSetVariable (
1253 IN CHAR16
*VariableName
,
1254 IN EFI_GUID
*VendorGuid
,
1255 IN UINT32 Attributes
,
1260 VARIABLE_POINTER_TRACK Variable
;
1262 VARIABLE_HEADER
*NextVariable
;
1264 UINTN VarNameOffset
;
1265 UINTN VarDataOffset
;
1269 UINTN
*VolatileOffset
;
1270 UINTN
*NonVolatileOffset
;
1273 EFI_PHYSICAL_ADDRESS Point
;
1276 // Check input parameters
1278 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1279 return EFI_INVALID_PARAMETER
;
1282 // Make sure if runtime bit is set, boot service bit is set also
1284 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1285 return EFI_INVALID_PARAMETER
;
1289 // The size of the VariableName, including the Unicode Null in bytes plus
1290 // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)
1291 // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.
1293 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1294 if ((DataSize
> MAX_HARDWARE_ERROR_VARIABLE_SIZE
) ||
1295 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> MAX_HARDWARE_ERROR_VARIABLE_SIZE
)) {
1296 return EFI_INVALID_PARAMETER
;
1300 // The size of the VariableName, including the Unicode Null in bytes plus
1301 // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.
1303 if ((DataSize
> MAX_VARIABLE_SIZE
) ||
1304 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> MAX_VARIABLE_SIZE
)) {
1305 return EFI_INVALID_PARAMETER
;
1309 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1312 Instance
= mVariableModuleGlobal
->FvbInstance
;
1313 VolatileOffset
= &mVariableModuleGlobal
->VolatileLastVariableOffset
;
1316 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
1318 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
1319 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;;
1321 // Parse non-volatile variable data and get last variable offset
1323 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
1324 while (IsValidVariableHeader (NextVariable
)) {
1325 NextVariable
= GetNextVariablePtr (NextVariable
);
1327 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
1330 NonVolatileOffset
= &mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1334 // Check whether the input variable is already existed
1337 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1338 if (Status
== EFI_SUCCESS
&& Variable
.CurrPtr
!= NULL
) {
1340 // Update/Delete existing variable
1342 Volatile
= Variable
.Volatile
;
1344 if (EfiAtRuntime ()) {
1346 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1347 // the volatile is ReadOnly, and SetVariable should be aborted and
1348 // return EFI_WRITE_PROTECTED.
1350 if (Variable
.Volatile
) {
1351 Status
= EFI_WRITE_PROTECTED
;
1355 // Only variable have NV attribute can be updated/deleted in Runtime
1357 if (!(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)) {
1358 Status
= EFI_INVALID_PARAMETER
;
1363 // Setting a data variable with no access, or zero DataSize attributes
1364 // specified causes it to be deleted.
1366 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1367 State
= Variable
.CurrPtr
->State
;
1368 State
&= VAR_DELETED
;
1370 Status
= UpdateVariableStore (
1371 &mVariableModuleGlobal
->VariableGlobal
,
1375 (UINTN
) &Variable
.CurrPtr
->State
,
1379 if (!EFI_ERROR (Status
)) {
1380 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1381 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1386 // If the variable is marked valid and the same data has been passed in
1387 // then return to the caller immediately.
1389 if (DataSizeOfVariable (Variable
.CurrPtr
) == DataSize
&&
1390 (CompareMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), DataSize
) == 0)) {
1392 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1393 Status
= EFI_SUCCESS
;
1395 } else if ((Variable
.CurrPtr
->State
== VAR_ADDED
) ||
1396 (Variable
.CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1399 // Mark the old variable as in delete transition
1401 State
= Variable
.CurrPtr
->State
;
1402 State
&= VAR_IN_DELETED_TRANSITION
;
1404 Status
= UpdateVariableStore (
1405 &mVariableModuleGlobal
->VariableGlobal
,
1409 (UINTN
) &Variable
.CurrPtr
->State
,
1413 if (EFI_ERROR (Status
)) {
1417 } else if (Status
== EFI_NOT_FOUND
) {
1419 // Create a new variable
1423 // Make sure we are trying to create a new variable.
1424 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1426 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1427 Status
= EFI_NOT_FOUND
;
1432 // Only variable have NV|RT attribute can be created in Runtime
1434 if (EfiAtRuntime () &&
1435 (!(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) || !(Attributes
& EFI_VARIABLE_NON_VOLATILE
))) {
1436 Status
= EFI_INVALID_PARAMETER
;
1441 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
1443 ASSERT (Status
== EFI_INVALID_PARAMETER
);
1448 // Function part - create a new variable and copy the data.
1449 // Both update a variable and create a variable will come here.
1451 // Tricky part: Use scratch data area at the end of volatile variable store
1452 // as a temporary storage.
1454 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1456 SetMem (NextVariable
, SCRATCH_SIZE
, 0xff);
1458 NextVariable
->StartId
= VARIABLE_DATA
;
1459 NextVariable
->Attributes
= Attributes
;
1461 // NextVariable->State = VAR_ADDED;
1463 NextVariable
->Reserved
= 0;
1464 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1465 VarNameSize
= StrSize (VariableName
);
1467 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1471 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1473 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1477 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1479 // There will be pad bytes after Data, the NextVariable->NameSize and
1480 // NextVariable->DataSize should not include pad size so that variable
1481 // service can get actual size in GetVariable
1483 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1484 NextVariable
->DataSize
= (UINT32
)DataSize
;
1487 // The actual size of the variable that stores in storage should
1488 // include pad size.
1490 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1491 if (Attributes
& EFI_VARIABLE_NON_VOLATILE
) {
1493 // Create a nonvolatile variable
1497 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1498 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
1500 if (EfiAtRuntime ()) {
1501 Status
= EFI_OUT_OF_RESOURCES
;
1505 // Perform garbage collection & reclaim operation
1507 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
, NonVolatileOffset
, FALSE
, Variable
.CurrPtr
);
1508 if (EFI_ERROR (Status
)) {
1512 // If still no enough space, return out of resources
1514 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1515 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
1517 Status
= EFI_OUT_OF_RESOURCES
;
1525 // 1. Write variable header
1526 // 2. Set variable state to header valid
1527 // 3. Write variable data
1528 // 4. Set variable state to valid
1533 Status
= UpdateVariableStore (
1534 &mVariableModuleGlobal
->VariableGlobal
,
1539 sizeof (VARIABLE_HEADER
),
1540 (UINT8
*) NextVariable
1543 if (EFI_ERROR (Status
)) {
1550 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1551 Status
= UpdateVariableStore (
1552 &mVariableModuleGlobal
->VariableGlobal
,
1557 sizeof (VARIABLE_HEADER
),
1558 (UINT8
*) NextVariable
1561 if (EFI_ERROR (Status
)) {
1567 Status
= UpdateVariableStore (
1568 &mVariableModuleGlobal
->VariableGlobal
,
1572 *NonVolatileOffset
+ sizeof (VARIABLE_HEADER
),
1573 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1574 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1577 if (EFI_ERROR (Status
)) {
1583 NextVariable
->State
= VAR_ADDED
;
1584 Status
= UpdateVariableStore (
1585 &mVariableModuleGlobal
->VariableGlobal
,
1590 sizeof (VARIABLE_HEADER
),
1591 (UINT8
*) NextVariable
1594 if (EFI_ERROR (Status
)) {
1598 *NonVolatileOffset
= HEADER_ALIGN (*NonVolatileOffset
+ VarSize
);
1602 // Create a volatile variable
1606 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1607 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1609 // Perform garbage collection & reclaim operation
1611 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
, VolatileOffset
, TRUE
, Variable
.CurrPtr
);
1612 if (EFI_ERROR (Status
)) {
1616 // If still no enough space, return out of resources
1618 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1619 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1621 Status
= EFI_OUT_OF_RESOURCES
;
1628 NextVariable
->State
= VAR_ADDED
;
1629 Status
= UpdateVariableStore (
1630 &mVariableModuleGlobal
->VariableGlobal
,
1636 (UINT8
*) NextVariable
1639 if (EFI_ERROR (Status
)) {
1643 *VolatileOffset
= HEADER_ALIGN (*VolatileOffset
+ VarSize
);
1646 // Mark the old variable as deleted
1648 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
.CurrPtr
!= NULL
) {
1649 State
= Variable
.CurrPtr
->State
;
1650 State
&= VAR_DELETED
;
1652 Status
= UpdateVariableStore (
1653 &mVariableModuleGlobal
->VariableGlobal
,
1657 (UINTN
) &Variable
.CurrPtr
->State
,
1662 if (!EFI_ERROR (Status
)) {
1663 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1664 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1669 Status
= EFI_SUCCESS
;
1670 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1671 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1674 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
1675 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1683 Routine Description:
1685 This code returns information about the EFI variables.
1689 Attributes Attributes bitmask to specify the type of variables
1690 on which to return information.
1691 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1692 for the EFI variables associated with the attributes specified.
1693 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1694 for EFI variables associated with the attributes specified.
1695 MaximumVariableSize Pointer to the maximum size of an individual EFI variables
1696 associated with the attributes specified.
1697 Global Pointer to VARIABLE_GLOBAL structure.
1698 Instance Instance of the Firmware Volume.
1703 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
1704 EFI_SUCCESS - Query successfully.
1705 EFI_UNSUPPORTED - The attribute is not supported on this platform.
1710 RuntimeServiceQueryVariableInfo (
1711 IN UINT32 Attributes
,
1712 OUT UINT64
*MaximumVariableStorageSize
,
1713 OUT UINT64
*RemainingVariableStorageSize
,
1714 OUT UINT64
*MaximumVariableSize
1717 VARIABLE_HEADER
*Variable
;
1718 VARIABLE_HEADER
*NextVariable
;
1719 UINT64 VariableSize
;
1720 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1722 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1723 return EFI_INVALID_PARAMETER
;
1726 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1728 // Make sure the Attributes combination is supported by the platform.
1730 return EFI_UNSUPPORTED
;
1731 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1733 // Make sure if runtime bit is set, boot service bit is set also.
1735 return EFI_INVALID_PARAMETER
;
1736 } else if (EfiAtRuntime () && !(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
1738 // Make sure RT Attribute is set if we are in Runtime phase.
1740 return EFI_INVALID_PARAMETER
;
1743 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1745 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1747 // Query is Volatile related.
1749 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
1752 // Query is Non-Volatile related.
1754 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1758 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1759 // with the storage size (excluding the storage header size).
1761 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1762 *RemainingVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1765 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.
1767 *MaximumVariableSize
= MAX_VARIABLE_SIZE
- sizeof (VARIABLE_HEADER
);
1770 // Harware error record variable needs larger size.
1772 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1773 *MaximumVariableSize
= MAX_HARDWARE_ERROR_VARIABLE_SIZE
- sizeof (VARIABLE_HEADER
);
1777 // Point to the starting address of the variables.
1779 Variable
= GetStartPointer (VariableStoreHeader
);
1782 // Now walk through the related variable store.
1784 while (IsValidVariableHeader (Variable
) && (Variable
< GetEndPointer (VariableStoreHeader
))) {
1785 NextVariable
= GetNextVariablePtr (Variable
);
1786 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1788 if (EfiAtRuntime ()) {
1790 // we don't take the state of the variables in mind
1791 // when calculating RemainingVariableStorageSize,
1792 // since the space occupied by variables not marked with
1793 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1795 *RemainingVariableStorageSize
-= VariableSize
;
1798 // Only care about Variables with State VAR_ADDED,because
1799 // the space not marked as VAR_ADDED is reclaimable now.
1801 if (Variable
->State
== VAR_ADDED
) {
1802 *RemainingVariableStorageSize
-= VariableSize
;
1807 // Go to the next one
1809 Variable
= NextVariable
;
1812 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
1813 *MaximumVariableSize
= 0;
1814 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
1815 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
1818 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1832 VarSize
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1833 Status
= EFI_SUCCESS
;
1836 // Check if the free area is blow a threshold
1838 if ((VarSize
- mVariableModuleGlobal
->NonVolatileLastVariableOffset
) < VARIABLE_RECLAIM_THRESHOLD
) {
1840 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1841 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1845 ASSERT_EFI_ERROR (Status
);
1850 VariableCommonInitialize (
1851 IN EFI_HANDLE ImageHandle
,
1852 IN EFI_SYSTEM_TABLE
*SystemTable
1856 Routine Description:
1857 This function does common initialization for variable services
1861 ImageHandle - The firmware allocated handle for the EFI image.
1862 SystemTable - A pointer to the EFI System Table.
1868 EFI_NOT_FOUND - Variable store area not found.
1869 EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.
1870 EFI_SUCCESS - Variable services successfully initialized.
1875 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1877 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
1878 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1879 VARIABLE_HEADER
*NextVariable
;
1881 EFI_PHYSICAL_ADDRESS FvVolHdr
;
1882 UINT64 TempVariableStoreHeader
;
1883 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
1888 UINT64 VariableStoreBase
;
1889 UINT64 VariableStoreLength
;
1890 EFI_EVENT ReadyToBootEvent
;
1892 Status
= EFI_SUCCESS
;
1894 // Allocate runtime memory for variable driver global structure.
1896 mVariableModuleGlobal
= AllocateRuntimePool (sizeof (VARIABLE_MODULE_GLOBAL
));
1897 if (mVariableModuleGlobal
== NULL
) {
1898 return EFI_OUT_OF_RESOURCES
;
1901 EfiInitializeLock(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
1902 mVariableModuleGlobal
->VariableGlobal
.ReentrantState
= 0;
1905 // Allocate memory for volatile variable store
1907 VolatileVariableStore
= AllocateRuntimePool (VARIABLE_STORE_SIZE
+ SCRATCH_SIZE
);
1908 if (VolatileVariableStore
== NULL
) {
1909 FreePool (mVariableModuleGlobal
);
1910 return EFI_OUT_OF_RESOURCES
;
1913 SetMem (VolatileVariableStore
, VARIABLE_STORE_SIZE
+ SCRATCH_SIZE
, 0xff);
1916 // Variable Specific Data
1918 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
1919 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
1921 VolatileVariableStore
->Signature
= VARIABLE_STORE_SIGNATURE
;
1922 VolatileVariableStore
->Size
= VARIABLE_STORE_SIZE
;
1923 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
1924 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
1925 VolatileVariableStore
->Reserved
= 0;
1926 VolatileVariableStore
->Reserved1
= 0;
1929 // Get non volatile varaible store
1932 TempVariableStoreHeader
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableBase
);
1933 VariableStoreBase
= TempVariableStoreHeader
+ \
1934 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1935 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
1936 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1938 // Mark the variable storage region of the FLASH as RUNTIME
1940 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
1941 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
1942 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
1944 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
1945 if (EFI_ERROR (Status
)) {
1949 Status
= gDS
->SetMemorySpaceAttributes (
1952 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
1954 if (EFI_ERROR (Status
)) {
1958 // Get address of non volatile variable store base
1960 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
1966 // Find the Correct Instance of the FV Block Service.
1969 CurrPtr
= (CHAR8
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1970 while (EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
) == EFI_SUCCESS
) {
1971 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
1972 if (CurrPtr
>= (CHAR8
*) FwVolHeader
&& CurrPtr
< (((CHAR8
*) FwVolHeader
) + FwVolHeader
->FvLength
)) {
1973 mVariableModuleGlobal
->FvbInstance
= Instance
;
1980 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) CurrPtr
;
1981 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
1982 if (~VariableStoreHeader
->Size
== 0) {
1983 Status
= UpdateVariableStore (
1984 &mVariableModuleGlobal
->VariableGlobal
,
1987 mVariableModuleGlobal
->FvbInstance
,
1988 (UINTN
) &VariableStoreHeader
->Size
,
1990 (UINT8
*) &VariableStoreLength
1993 // As Variables are stored in NV storage, which are slow devices,such as flash.
1994 // Variable operation may skip checking variable program result to improve performance,
1995 // We can assume Variable program is OK through some check point.
1996 // Variable Store Size Setting should be the first Variable write operation,
1997 // We can assume all Read/Write is OK if we can set Variable store size successfully.
1998 // If write fail, we will assert here
2000 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2002 if (EFI_ERROR (Status
)) {
2007 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) CurrPtr
);
2009 // Parse non-volatile variable data and get last variable offset
2011 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) CurrPtr
);
2012 Status
= EFI_SUCCESS
;
2014 while (IsValidVariableHeader (NextVariable
)) {
2015 NextVariable
= GetNextVariablePtr (NextVariable
);
2018 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) CurrPtr
;
2021 // Check if the free area is really free.
2023 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
2024 Data
= ((UINT8
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)[Index
];
2027 // There must be something wrong in variable store, do reclaim operation.
2030 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2031 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2036 if (EFI_ERROR (Status
)) {
2045 // Register the event handling function to reclaim variable for OS usage.
2047 Status
= EfiCreateEventReadyToBootEx (
2056 if (EFI_ERROR (Status
)) {
2057 FreePool (mVariableModuleGlobal
);
2058 FreePool (VolatileVariableStore
);
2066 VariableClassAddressChangeEvent (
2073 (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
2077 (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
2079 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
2084 Variable Driver main entry point. The Variable driver places the 4 EFI
2085 runtime services in the EFI System Table and installs arch protocols
2086 for variable read and write services being availible.
2088 @param[in] ImageHandle The firmware allocated handle for the EFI image.
2089 @param[in] SystemTable A pointer to the EFI System Table.
2091 @retval EFI_SUCCESS The entry point is executed successfully.
2092 @retval other Some error occurs when executing this entry point.
2097 VariableServiceInitialize (
2098 IN EFI_HANDLE ImageHandle
,
2099 IN EFI_SYSTEM_TABLE
*SystemTable
2104 Status
= VariableCommonInitialize (ImageHandle
, SystemTable
);
2105 ASSERT_EFI_ERROR (Status
);
2107 SystemTable
->RuntimeServices
->GetVariable
= RuntimeServiceGetVariable
;
2108 SystemTable
->RuntimeServices
->GetNextVariableName
= RuntimeServiceGetNextVariableName
;
2109 SystemTable
->RuntimeServices
->SetVariable
= RuntimeServiceSetVariable
;
2110 SystemTable
->RuntimeServices
->QueryVariableInfo
= RuntimeServiceQueryVariableInfo
;
2113 // Now install the Variable Runtime Architectural Protocol on a new handle
2115 Status
= gBS
->InstallMultipleProtocolInterfaces (
2117 &gEfiVariableArchProtocolGuid
, NULL
,
2118 &gEfiVariableWriteArchProtocolGuid
, NULL
,
2121 ASSERT_EFI_ERROR (Status
);
2123 Status
= gBS
->CreateEvent (
2124 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
,
2126 VariableClassAddressChangeEvent
,
2128 &mVirtualAddressChangeEvent
2130 ASSERT_EFI_ERROR (Status
);