2 EFI Runtime Variable services.
4 Copyright (c) 2006 - 2008, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
19 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
20 EFI_HANDLE mHandle
= NULL
;
24 // This is a temperary function which will be removed
25 // when EfiAcquireLock in UefiLib can handle the
26 // the call in UEFI Runtimer driver in RT phase.
29 AcquireLockOnlyAtBootTime (
33 if (!EfiAtRuntime ()) {
34 EfiAcquireLock (Lock
);
39 // This is a temperary function which will be removed
40 // when EfiAcquireLock in UefiLib can handle the
41 // the call in UEFI Runtimer driver in RT phase.
44 ReleaseLockOnlyAtBootTime (
48 if (!EfiAtRuntime ()) {
49 EfiReleaseLock (Lock
);
54 GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
58 Routine used to track statistical information about variable usage.
59 The data is stored in the EFI system table so it can be accessed later.
60 VariableInfo.efi can dump out the table. Only Boot Services variable
61 accesses are tracked by this code. The PcdVariableCollectStatistics
62 build flag controls if this feature is enabled.
64 A read that hits in the cache will have Read and Cache true for
65 the transaction. Data is allocated by this routine, but never
68 @param[in] VariableName Name of the Variable to track
69 @param[in] VendorGuid Guid of the Variable to track
70 @param[in] Volatile TRUE if volatile FALSE if non-volatile
71 @param[in] Read TRUE if GetVariable() was called
72 @param[in] Write TRUE if SetVariable() was called
73 @param[in] Delete TRUE if deleted via SetVariable()
74 @param[in] Cache TRUE for a cache hit.
79 IN CHAR16
*VariableName
,
80 IN EFI_GUID
*VendorGuid
,
88 VARIABLE_INFO_ENTRY
*Entry
;
90 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
92 if (EfiAtRuntime ()) {
93 // Don't collect statistics at runtime
97 if (gVariableInfo
== NULL
) {
99 // on the first call allocate a entry and place a pointer to it in
100 // the EFI System Table
102 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
103 ASSERT (gVariableInfo
!= NULL
);
105 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
106 gVariableInfo
->Name
= AllocatePool (StrLen (VariableName
));
107 StrCpy (gVariableInfo
->Name
, VariableName
);
108 gVariableInfo
->Volatile
= Volatile
;
110 gBS
->InstallConfigurationTable (&gEfiVariableInfoGuid
, gVariableInfo
);
114 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
115 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
116 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
124 Entry
->DeleteCount
++;
134 if (Entry
->Next
== NULL
) {
136 // If the entry is not in the table add it.
137 // Next iteration of the loop will fill in the data
139 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
140 ASSERT (Entry
->Next
!= NULL
);
142 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
143 Entry
->Next
->Name
= AllocatePool (StrLen (VariableName
));
144 StrCpy (Entry
->Next
->Name
, VariableName
);
145 Entry
->Next
->Volatile
= Volatile
;
154 IsValidVariableHeader (
155 IN VARIABLE_HEADER
*Variable
161 This code checks if variable header is valid or not.
164 Variable Pointer to the Variable Header.
167 TRUE Variable header is valid.
168 FALSE Variable header is not valid.
172 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
181 UpdateVariableStore (
182 IN VARIABLE_GLOBAL
*Global
,
184 IN BOOLEAN SetByIndex
,
186 IN UINTN DataPtrIndex
,
194 This function writes data to the FWH at the correct LBA even if the LBAs
199 Global - Pointer to VARAIBLE_GLOBAL structure
200 Volatile - If the Variable is Volatile or Non-Volatile
201 SetByIndex - TRUE: Target pointer is given as index
202 FALSE: Target pointer is absolute
203 Instance - Instance of FV Block services
204 DataPtrIndex - Pointer to the Data from the end of VARIABLE_STORE_HEADER
206 DataSize - Size of data to be written.
207 Buffer - Pointer to the buffer from which data is written
211 EFI_INVALID_PARAMETER - Parameters not valid
212 EFI_SUCCESS - Variable store successfully updated
216 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
224 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
225 VARIABLE_STORE_HEADER
*VolatileBase
;
226 EFI_PHYSICAL_ADDRESS FvVolHdr
;
227 EFI_PHYSICAL_ADDRESS DataPtr
;
231 DataPtr
= DataPtrIndex
;
234 // Check if the Data is Volatile
237 EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
);
238 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
240 // Data Pointer should point to the actual Address where data is to be
244 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
247 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
248 return EFI_INVALID_PARAMETER
;
252 // Data Pointer should point to the actual Address where data is to be
255 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
257 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
260 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
261 return EFI_INVALID_PARAMETER
;
265 // If Volatile Variable just do a simple mem copy.
267 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
272 // If we are here we are dealing with Non-Volatile Variables
274 LinearOffset
= (UINTN
) FwVolHeader
;
275 CurrWritePtr
= (UINTN
) DataPtr
;
276 CurrWriteSize
= DataSize
;
280 if (CurrWritePtr
< LinearOffset
) {
281 return EFI_INVALID_PARAMETER
;
284 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
285 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
287 // Check to see if the Variable Writes are spanning through multiple
290 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
291 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
292 Status
= EfiFvbWriteBlock (
295 (UINTN
) (CurrWritePtr
- LinearOffset
),
301 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
302 Status
= EfiFvbWriteBlock (
305 (UINTN
) (CurrWritePtr
- LinearOffset
),
309 if (EFI_ERROR (Status
)) {
313 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
314 CurrBuffer
= CurrBuffer
+ Size
;
315 CurrWriteSize
= CurrWriteSize
- Size
;
319 LinearOffset
+= PtrBlockMapEntry
->Length
;
328 VARIABLE_STORE_STATUS
329 GetVariableStoreStatus (
330 IN VARIABLE_STORE_HEADER
*VarStoreHeader
336 This code gets the current status of Variable Store.
340 VarStoreHeader Pointer to the Variable Store Header.
344 EfiRaw Variable store status is raw
345 EfiValid Variable store status is valid
346 EfiInvalid Variable store status is invalid
350 if (VarStoreHeader
->Signature
== VARIABLE_STORE_SIGNATURE
&&
351 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
352 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
356 } else if (VarStoreHeader
->Signature
== 0xffffffff &&
357 VarStoreHeader
->Size
== 0xffffffff &&
358 VarStoreHeader
->Format
== 0xff &&
359 VarStoreHeader
->State
== 0xff
371 IN VARIABLE_HEADER
*Variable
377 This code gets the size of name of variable.
381 Variable Pointer to the Variable Header.
385 UINTN Size of variable in bytes
389 if (Variable
->State
== (UINT8
) (-1) ||
390 Variable
->DataSize
== (UINT32
) -1 ||
391 Variable
->NameSize
== (UINT32
) -1 ||
392 Variable
->Attributes
== (UINT32
) -1) {
395 return (UINTN
) Variable
->NameSize
;
400 IN VARIABLE_HEADER
*Variable
406 This code gets the size of name of variable.
410 Variable Pointer to the Variable Header.
414 UINTN Size of variable in bytes
418 if (Variable
->State
== (UINT8
) -1 ||
419 Variable
->DataSize
== (UINT32
) -1 ||
420 Variable
->NameSize
== (UINT32
) -1 ||
421 Variable
->Attributes
== (UINT32
) -1) {
424 return (UINTN
) Variable
->DataSize
;
429 IN VARIABLE_HEADER
*Variable
435 This code gets the pointer to the variable name.
439 Variable Pointer to the Variable Header.
443 CHAR16* Pointer to Variable Name
448 return (CHAR16
*) (Variable
+ 1);
453 IN VARIABLE_HEADER
*Variable
459 This code gets the pointer to the variable data.
463 Variable Pointer to the Variable Header.
467 UINT8* Pointer to Variable Data
474 // Be careful about pad size for alignment
476 Value
= (UINTN
) GetVariableNamePtr (Variable
);
477 Value
+= NameSizeOfVariable (Variable
);
478 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
480 return (UINT8
*) Value
;
486 IN VARIABLE_HEADER
*Variable
492 This code gets the pointer to the next variable header.
496 Variable Pointer to the Variable Header.
500 VARIABLE_HEADER* Pointer to next variable header.
506 if (!IsValidVariableHeader (Variable
)) {
510 Value
= (UINTN
) GetVariableDataPtr (Variable
);
511 Value
+= DataSizeOfVariable (Variable
);
512 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
515 // Be careful about pad size for alignment
517 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
522 IN VARIABLE_STORE_HEADER
*VarStoreHeader
528 This code gets the pointer to the first variable memory pointer byte
532 VarStoreHeader Pointer to the Variable Store Header.
536 VARIABLE_HEADER* Pointer to last unavailable Variable Header
541 // The end of variable store
543 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
548 IN VARIABLE_STORE_HEADER
*VarStoreHeader
554 This code gets the pointer to the last variable memory pointer byte
558 VarStoreHeader Pointer to the Variable Store Header.
562 VARIABLE_HEADER* Pointer to last unavailable Variable Header
567 // The end of variable store
569 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
575 IN EFI_PHYSICAL_ADDRESS VariableBase
,
576 OUT UINTN
*LastVariableOffset
,
577 IN BOOLEAN IsVolatile
583 Variable store garbage collection and reclaim operation
587 VariableBase Base address of variable store
588 LastVariableOffset Offset of last variable
589 IsVolatile The variable store is volatile or not,
590 if it is non-volatile, need FTW
598 VARIABLE_HEADER
*Variable
;
599 VARIABLE_HEADER
*NextVariable
;
600 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
602 UINTN ValidBufferSize
;
607 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
610 // Start Pointers for the variable.
612 Variable
= GetStartPointer (VariableStoreHeader
);
613 ValidBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
615 while (IsValidVariableHeader (Variable
)) {
616 NextVariable
= GetNextVariablePtr (Variable
);
617 if (Variable
->State
== VAR_ADDED
) {
618 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
619 ValidBufferSize
+= VariableSize
;
622 Variable
= NextVariable
;
625 ValidBuffer
= AllocatePool (ValidBufferSize
);
626 if (ValidBuffer
== NULL
) {
627 return EFI_OUT_OF_RESOURCES
;
630 SetMem (ValidBuffer
, ValidBufferSize
, 0xff);
632 CurrPtr
= ValidBuffer
;
635 // Copy variable store header
637 CopyMem (CurrPtr
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
638 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) CurrPtr
);
641 // Start Pointers for the variable.
643 Variable
= GetStartPointer (VariableStoreHeader
);
645 while (IsValidVariableHeader (Variable
)) {
646 NextVariable
= GetNextVariablePtr (Variable
);
647 if (Variable
->State
== VAR_ADDED
) {
648 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
649 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
650 CurrPtr
+= VariableSize
;
653 Variable
= NextVariable
;
658 // If volatile variable store, just copy valid buffer
660 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
661 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, ValidBufferSize
);
662 *LastVariableOffset
= ValidBufferSize
;
663 Status
= EFI_SUCCESS
;
666 // If non-volatile variable store, perform FTW here.
668 Status
= FtwVariableSpace (
673 if (!EFI_ERROR (Status
)) {
674 *LastVariableOffset
= ValidBufferSize
;
678 FreePool (ValidBuffer
);
680 if (EFI_ERROR (Status
)) {
681 *LastVariableOffset
= 0;
689 // The current Hii implementation accesses this variable a larg # of times on every boot.
690 // Other common variables are only accessed a single time. This is why this cache algorithm
691 // only targets a single variable. Probably to get an performance improvement out of
692 // a Cache you would need a cache that improves the search performance for a variable.
694 VARIABLE_CACHE_ENTRY mVariableCache
[] = {
696 &gEfiGlobalVariableGuid
,
706 Update the Cache with Variable information. These are the same
707 arguments as the EFI Variable services.
709 @param[in] VariableName Name of variable
710 @param[in] VendorGuid Guid of variable
711 @param[in] Attribute Attribue of the variable
712 @param[in] DataSize Size of data. 0 means delete
713 @param[in] Data Variable data
717 UpdateVariableCache (
718 IN CHAR16
*VariableName
,
719 IN EFI_GUID
*VendorGuid
,
720 IN UINT32 Attributes
,
725 VARIABLE_CACHE_ENTRY
*Entry
;
728 if (EfiAtRuntime ()) {
729 // Don't use the cache at runtime
733 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
734 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
735 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
736 Entry
->Attributes
= Attributes
;
739 if (Entry
->DataSize
!= 0) {
740 FreePool (Entry
->Data
);
742 Entry
->DataSize
= DataSize
;
743 } else if (DataSize
== Entry
->DataSize
) {
744 CopyMem (Entry
->Data
, Data
, DataSize
);
746 Entry
->Data
= AllocatePool (DataSize
);
747 Entry
->DataSize
= DataSize
;
748 CopyMem (Entry
->Data
, Data
, DataSize
);
757 Search the cache to see if the variable is in the cache.
759 @param[in] VariableName Name of variable
760 @param[in] VendorGuid Guid of variable
761 @param[in] Attribute Attribue returned
762 @param[in] DataSize Size of data returned
763 @param[in] Data Variable data returned
765 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.
766 @retval other Not found.
770 FindVariableInCache (
771 IN CHAR16
*VariableName
,
772 IN EFI_GUID
*VendorGuid
,
773 OUT UINT32
*Attributes OPTIONAL
,
774 IN OUT UINTN
*DataSize
,
778 VARIABLE_CACHE_ENTRY
*Entry
;
781 if (EfiAtRuntime ()) {
782 // Don't use the cache at runtime
783 return EFI_NOT_FOUND
;
786 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
787 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
788 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
789 if (Entry
->DataSize
== 0) {
790 // Variable was deleted so return not found
791 return EFI_NOT_FOUND
;
792 } else if (Entry
->DataSize
> *DataSize
) {
793 // If the buffer is too small return correct size
794 *DataSize
= Entry
->DataSize
;
795 return EFI_BUFFER_TOO_SMALL
;
797 *DataSize
= Entry
->DataSize
;
799 CopyMem (Data
, Entry
->Data
, Entry
->DataSize
);
800 if (Attributes
!= NULL
) {
801 *Attributes
= Entry
->Attributes
;
809 return EFI_NOT_FOUND
;
815 IN CHAR16
*VariableName
,
816 IN EFI_GUID
*VendorGuid
,
817 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
818 IN VARIABLE_GLOBAL
*Global
824 This code finds variable in storage blocks (Volatile or Non-Volatile)
828 VariableName Name of the variable to be found
829 VendorGuid Vendor GUID to be found.
830 PtrTrack Variable Track Pointer structure that contains
831 Variable Information.
832 Contains the pointer of Variable header.
833 Global VARIABLE_GLOBAL pointer
841 VARIABLE_HEADER
*Variable
[2];
842 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
847 // 0: Volatile, 1: Non-Volatile
848 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
849 // make use of this mapping to implement search algorithme.
851 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
852 VariableStoreHeader
[1] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
855 // Start Pointers for the variable.
856 // Actual Data Pointer where data can be written.
858 Variable
[0] = GetStartPointer (VariableStoreHeader
[0]);
859 Variable
[1] = GetStartPointer (VariableStoreHeader
[1]);
861 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
862 return EFI_INVALID_PARAMETER
;
865 // Find the variable by walk through volatile and then non-volatile variable store
867 for (Index
= 0; Index
< 2; Index
++) {
868 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
869 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
871 while (IsValidVariableHeader (Variable
[Index
]) && (Variable
[Index
] <= GetEndPointer (VariableStoreHeader
[Index
]))) {
872 if (Variable
[Index
]->State
== VAR_ADDED
) {
873 if (!EfiAtRuntime () || (Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
874 if (VariableName
[0] == 0) {
875 PtrTrack
->CurrPtr
= Variable
[Index
];
876 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
879 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
880 Point
= (VOID
*) GetVariableNamePtr (Variable
[Index
]);
882 ASSERT (NameSizeOfVariable (Variable
[Index
]) != 0);
883 if (!CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
[Index
]))) {
884 PtrTrack
->CurrPtr
= Variable
[Index
];
885 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
893 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
896 PtrTrack
->CurrPtr
= NULL
;
897 return EFI_NOT_FOUND
;
906 This code finds variable in storage blocks (Volatile or Non-Volatile)
910 VariableName Name of Variable to be found
911 VendorGuid Variable vendor GUID
912 Attributes OPTIONAL Attribute value of the variable found
913 DataSize Size of Data found. If size is less than the
914 data, this value contains the required size.
916 Global Pointer to VARIABLE_GLOBAL structure
917 Instance Instance of the Firmware Volume.
921 EFI_INVALID_PARAMETER - Invalid parameter
922 EFI_SUCCESS - Find the specified variable
923 EFI_NOT_FOUND - Not found
924 EFI_BUFFER_TO_SMALL - DataSize is too small for the result
930 RuntimeServiceGetVariable (
931 IN CHAR16
*VariableName
,
932 IN EFI_GUID
*VendorGuid
,
933 OUT UINT32
*Attributes OPTIONAL
,
934 IN OUT UINTN
*DataSize
,
939 VARIABLE_POINTER_TRACK Variable
;
942 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
943 return EFI_INVALID_PARAMETER
;
946 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
949 // Find existing variable
951 Status
= FindVariableInCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
952 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_SUCCESS
)){
954 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
);
958 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
959 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
966 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
967 ASSERT (VarDataSize
!= 0);
969 if (*DataSize
>= VarDataSize
) {
971 Status
= EFI_INVALID_PARAMETER
;
975 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
976 if (Attributes
!= NULL
) {
977 *Attributes
= Variable
.CurrPtr
->Attributes
;
980 *DataSize
= VarDataSize
;
981 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
982 UpdateVariableCache (VariableName
, VendorGuid
, Variable
.CurrPtr
->Attributes
, VarDataSize
, Data
);
984 Status
= EFI_SUCCESS
;
987 *DataSize
= VarDataSize
;
988 Status
= EFI_BUFFER_TOO_SMALL
;
993 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1001 Routine Description:
1003 This code Finds the Next available variable
1007 VariableNameSize Size of the variable
1008 VariableName Pointer to variable name
1009 VendorGuid Variable Vendor Guid
1010 Global VARIABLE_GLOBAL structure pointer.
1011 Instance FV instance
1020 RuntimeServiceGetNextVariableName (
1021 IN OUT UINTN
*VariableNameSize
,
1022 IN OUT CHAR16
*VariableName
,
1023 IN OUT EFI_GUID
*VendorGuid
1026 VARIABLE_POINTER_TRACK Variable
;
1030 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1031 return EFI_INVALID_PARAMETER
;
1034 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1036 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1037 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1041 if (VariableName
[0] != 0) {
1043 // If variable name is not NULL, get next variable
1045 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1050 // If both volatile and non-volatile variable store are parsed,
1053 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
1054 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
1055 if (!Variable
.Volatile
) {
1056 Variable
.StartPtr
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1057 Variable
.EndPtr
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
));
1059 Status
= EFI_NOT_FOUND
;
1063 Variable
.CurrPtr
= Variable
.StartPtr
;
1064 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
1069 // Variable is found
1071 if (IsValidVariableHeader (Variable
.CurrPtr
) && Variable
.CurrPtr
->State
== VAR_ADDED
) {
1072 if (!(EfiAtRuntime () && !(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
))) {
1073 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
1074 ASSERT (VarNameSize
!= 0);
1076 if (VarNameSize
<= *VariableNameSize
) {
1079 GetVariableNamePtr (Variable
.CurrPtr
),
1084 &Variable
.CurrPtr
->VendorGuid
,
1087 Status
= EFI_SUCCESS
;
1089 Status
= EFI_BUFFER_TOO_SMALL
;
1092 *VariableNameSize
= VarNameSize
;
1097 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1101 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1108 Routine Description:
1110 This code sets variable in storage blocks (Volatile or Non-Volatile)
1114 VariableName Name of Variable to be found
1115 VendorGuid Variable vendor GUID
1116 Attributes Attribute value of the variable found
1117 DataSize Size of Data found. If size is less than the
1118 data, this value contains the required size.
1120 Global Pointer to VARIABLE_GLOBAL structure
1121 VolatileOffset The offset of last volatile variable
1122 NonVolatileOffset The offset of last non-volatile variable
1123 Instance Instance of the Firmware Volume.
1127 EFI_INVALID_PARAMETER - Invalid parameter
1128 EFI_SUCCESS - Set successfully
1129 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
1130 EFI_NOT_FOUND - Not found
1131 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
1132 EFI_WRITE_PROTECTED - Variable is read-only
1137 RuntimeServiceSetVariable (
1138 IN CHAR16
*VariableName
,
1139 IN EFI_GUID
*VendorGuid
,
1140 IN UINT32 Attributes
,
1145 VARIABLE_POINTER_TRACK Variable
;
1147 VARIABLE_HEADER
*NextVariable
;
1149 UINTN VarNameOffset
;
1150 UINTN VarDataOffset
;
1154 UINTN
*VolatileOffset
;
1155 UINTN
*NonVolatileOffset
;
1158 EFI_PHYSICAL_ADDRESS Point
;
1161 // Check input parameters
1163 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1164 return EFI_INVALID_PARAMETER
;
1167 // Make sure if runtime bit is set, boot service bit is set also
1169 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1170 return EFI_INVALID_PARAMETER
;
1174 // The size of the VariableName, including the Unicode Null in bytes plus
1175 // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)
1176 // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.
1178 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1179 if ((DataSize
> MAX_HARDWARE_ERROR_VARIABLE_SIZE
) ||
1180 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> MAX_HARDWARE_ERROR_VARIABLE_SIZE
)) {
1181 return EFI_INVALID_PARAMETER
;
1185 // The size of the VariableName, including the Unicode Null in bytes plus
1186 // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.
1188 if ((DataSize
> MAX_VARIABLE_SIZE
) ||
1189 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> MAX_VARIABLE_SIZE
)) {
1190 return EFI_INVALID_PARAMETER
;
1194 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1197 Instance
= mVariableModuleGlobal
->FvbInstance
;
1198 VolatileOffset
= &mVariableModuleGlobal
->VolatileLastVariableOffset
;
1201 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
1203 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
1204 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;;
1206 // Parse non-volatile variable data and get last variable offset
1208 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
1209 while (IsValidVariableHeader (NextVariable
)) {
1210 NextVariable
= GetNextVariablePtr (NextVariable
);
1212 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
1215 NonVolatileOffset
= &mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1219 // Check whether the input variable is already existed
1222 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1223 if (Status
== EFI_SUCCESS
&& Variable
.CurrPtr
!= NULL
) {
1225 // Update/Delete existing variable
1227 Volatile
= Variable
.Volatile
;
1229 if (EfiAtRuntime ()) {
1231 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1232 // the volatile is ReadOnly, and SetVariable should be aborted and
1233 // return EFI_WRITE_PROTECTED.
1235 if (Variable
.Volatile
) {
1236 Status
= EFI_WRITE_PROTECTED
;
1240 // Only variable have NV attribute can be updated/deleted in Runtime
1242 if (!(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)) {
1243 Status
= EFI_INVALID_PARAMETER
;
1248 // Setting a data variable with no access, or zero DataSize attributes
1249 // specified causes it to be deleted.
1251 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1252 State
= Variable
.CurrPtr
->State
;
1253 State
&= VAR_DELETED
;
1255 Status
= UpdateVariableStore (
1256 &mVariableModuleGlobal
->VariableGlobal
,
1260 (UINTN
) &Variable
.CurrPtr
->State
,
1264 if (!EFI_ERROR (Status
)) {
1265 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1266 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1271 // If the variable is marked valid and the same data has been passed in
1272 // then return to the caller immediately.
1274 if (DataSizeOfVariable (Variable
.CurrPtr
) == DataSize
&&
1275 (CompareMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), DataSize
) == 0)) {
1277 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1278 Status
= EFI_SUCCESS
;
1280 } else if ((Variable
.CurrPtr
->State
== VAR_ADDED
) ||
1281 (Variable
.CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1283 // Mark the old variable as in delete transition
1285 State
= Variable
.CurrPtr
->State
;
1286 State
&= VAR_IN_DELETED_TRANSITION
;
1288 Status
= UpdateVariableStore (
1289 &mVariableModuleGlobal
->VariableGlobal
,
1293 (UINTN
) &Variable
.CurrPtr
->State
,
1297 if (EFI_ERROR (Status
)) {
1301 } else if (Status
== EFI_NOT_FOUND
) {
1303 // Create a new variable
1307 // Make sure we are trying to create a new variable.
1308 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1310 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1311 Status
= EFI_NOT_FOUND
;
1316 // Only variable have NV|RT attribute can be created in Runtime
1318 if (EfiAtRuntime () &&
1319 (!(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) || !(Attributes
& EFI_VARIABLE_NON_VOLATILE
))) {
1320 Status
= EFI_INVALID_PARAMETER
;
1325 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
1327 ASSERT (Status
== EFI_INVALID_PARAMETER
);
1332 // Function part - create a new variable and copy the data.
1333 // Both update a variable and create a variable will come here.
1335 // Tricky part: Use scratch data area at the end of volatile variable store
1336 // as a temporary storage.
1338 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1340 SetMem (NextVariable
, SCRATCH_SIZE
, 0xff);
1342 NextVariable
->StartId
= VARIABLE_DATA
;
1343 NextVariable
->Attributes
= Attributes
;
1345 // NextVariable->State = VAR_ADDED;
1347 NextVariable
->Reserved
= 0;
1348 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1349 VarNameSize
= StrSize (VariableName
);
1351 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1355 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1357 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1361 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1363 // There will be pad bytes after Data, the NextVariable->NameSize and
1364 // NextVariable->DataSize should not include pad size so that variable
1365 // service can get actual size in GetVariable
1367 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1368 NextVariable
->DataSize
= (UINT32
)DataSize
;
1371 // The actual size of the variable that stores in storage should
1372 // include pad size.
1374 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1375 if (Attributes
& EFI_VARIABLE_NON_VOLATILE
) {
1377 // Create a nonvolatile variable
1381 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1382 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
1384 if (EfiAtRuntime ()) {
1385 Status
= EFI_OUT_OF_RESOURCES
;
1389 // Perform garbage collection & reclaim operation
1391 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
, NonVolatileOffset
, FALSE
);
1392 if (EFI_ERROR (Status
)) {
1396 // If still no enough space, return out of resources
1398 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1399 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
1401 Status
= EFI_OUT_OF_RESOURCES
;
1409 // 1. Write variable header
1410 // 2. Set variable state to header valid
1411 // 3. Write variable data
1412 // 4. Set variable state to valid
1417 Status
= UpdateVariableStore (
1418 &mVariableModuleGlobal
->VariableGlobal
,
1423 sizeof (VARIABLE_HEADER
),
1424 (UINT8
*) NextVariable
1427 if (EFI_ERROR (Status
)) {
1434 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1435 Status
= UpdateVariableStore (
1436 &mVariableModuleGlobal
->VariableGlobal
,
1441 sizeof (VARIABLE_HEADER
),
1442 (UINT8
*) NextVariable
1445 if (EFI_ERROR (Status
)) {
1451 Status
= UpdateVariableStore (
1452 &mVariableModuleGlobal
->VariableGlobal
,
1456 *NonVolatileOffset
+ sizeof (VARIABLE_HEADER
),
1457 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1458 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1461 if (EFI_ERROR (Status
)) {
1467 NextVariable
->State
= VAR_ADDED
;
1468 Status
= UpdateVariableStore (
1469 &mVariableModuleGlobal
->VariableGlobal
,
1474 sizeof (VARIABLE_HEADER
),
1475 (UINT8
*) NextVariable
1478 if (EFI_ERROR (Status
)) {
1482 *NonVolatileOffset
= HEADER_ALIGN (*NonVolatileOffset
+ VarSize
);
1486 // Create a volatile variable
1490 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1491 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1493 // Perform garbage collection & reclaim operation
1495 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
, VolatileOffset
, TRUE
);
1496 if (EFI_ERROR (Status
)) {
1500 // If still no enough space, return out of resources
1502 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1503 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1505 Status
= EFI_OUT_OF_RESOURCES
;
1512 NextVariable
->State
= VAR_ADDED
;
1513 Status
= UpdateVariableStore (
1514 &mVariableModuleGlobal
->VariableGlobal
,
1520 (UINT8
*) NextVariable
1523 if (EFI_ERROR (Status
)) {
1527 *VolatileOffset
= HEADER_ALIGN (*VolatileOffset
+ VarSize
);
1530 // Mark the old variable as deleted
1532 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
.CurrPtr
!= NULL
) {
1533 State
= Variable
.CurrPtr
->State
;
1534 State
&= VAR_DELETED
;
1536 Status
= UpdateVariableStore (
1537 &mVariableModuleGlobal
->VariableGlobal
,
1541 (UINTN
) &Variable
.CurrPtr
->State
,
1546 if (!EFI_ERROR (Status
)) {
1547 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1548 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1553 Status
= EFI_SUCCESS
;
1554 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1555 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1558 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
1559 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1567 Routine Description:
1569 This code returns information about the EFI variables.
1573 Attributes Attributes bitmask to specify the type of variables
1574 on which to return information.
1575 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1576 for the EFI variables associated with the attributes specified.
1577 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1578 for EFI variables associated with the attributes specified.
1579 MaximumVariableSize Pointer to the maximum size of an individual EFI variables
1580 associated with the attributes specified.
1581 Global Pointer to VARIABLE_GLOBAL structure.
1582 Instance Instance of the Firmware Volume.
1587 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
1588 EFI_SUCCESS - Query successfully.
1589 EFI_UNSUPPORTED - The attribute is not supported on this platform.
1594 RuntimeServiceQueryVariableInfo (
1595 IN UINT32 Attributes
,
1596 OUT UINT64
*MaximumVariableStorageSize
,
1597 OUT UINT64
*RemainingVariableStorageSize
,
1598 OUT UINT64
*MaximumVariableSize
1601 VARIABLE_HEADER
*Variable
;
1602 VARIABLE_HEADER
*NextVariable
;
1603 UINT64 VariableSize
;
1604 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1606 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1607 return EFI_INVALID_PARAMETER
;
1610 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1612 // Make sure the Attributes combination is supported by the platform.
1614 return EFI_UNSUPPORTED
;
1615 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1617 // Make sure if runtime bit is set, boot service bit is set also.
1619 return EFI_INVALID_PARAMETER
;
1620 } else if (EfiAtRuntime () && !(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
1622 // Make sure RT Attribute is set if we are in Runtime phase.
1624 return EFI_INVALID_PARAMETER
;
1627 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1629 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1631 // Query is Volatile related.
1633 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
1636 // Query is Non-Volatile related.
1638 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1642 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1643 // with the storage size (excluding the storage header size).
1645 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1646 *RemainingVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1649 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.
1651 *MaximumVariableSize
= MAX_VARIABLE_SIZE
- sizeof (VARIABLE_HEADER
);
1654 // Harware error record variable needs larger size.
1656 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1657 *MaximumVariableSize
= MAX_HARDWARE_ERROR_VARIABLE_SIZE
- sizeof (VARIABLE_HEADER
);
1661 // Point to the starting address of the variables.
1663 Variable
= GetStartPointer (VariableStoreHeader
);
1666 // Now walk through the related variable store.
1668 while (IsValidVariableHeader (Variable
) && (Variable
< GetEndPointer (VariableStoreHeader
))) {
1669 NextVariable
= GetNextVariablePtr (Variable
);
1670 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1672 if (EfiAtRuntime ()) {
1674 // we don't take the state of the variables in mind
1675 // when calculating RemainingVariableStorageSize,
1676 // since the space occupied by variables not marked with
1677 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1679 *RemainingVariableStorageSize
-= VariableSize
;
1682 // Only care about Variables with State VAR_ADDED,because
1683 // the space not marked as VAR_ADDED is reclaimable now.
1685 if (Variable
->State
== VAR_ADDED
) {
1686 *RemainingVariableStorageSize
-= VariableSize
;
1691 // Go to the next one
1693 Variable
= NextVariable
;
1696 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
1697 *MaximumVariableSize
= 0;
1698 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
1699 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
1702 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1716 VarSize
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1717 Status
= EFI_SUCCESS
;
1720 // Check if the free area is blow a threshold
1722 if ((VarSize
- mVariableModuleGlobal
->NonVolatileLastVariableOffset
) < VARIABLE_RECLAIM_THRESHOLD
) {
1724 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1725 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1728 ASSERT_EFI_ERROR (Status
);
1733 VariableCommonInitialize (
1734 IN EFI_HANDLE ImageHandle
,
1735 IN EFI_SYSTEM_TABLE
*SystemTable
1739 Routine Description:
1740 This function does common initialization for variable services
1744 ImageHandle - The firmware allocated handle for the EFI image.
1745 SystemTable - A pointer to the EFI System Table.
1751 EFI_NOT_FOUND - Variable store area not found.
1752 EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.
1753 EFI_SUCCESS - Variable services successfully initialized.
1758 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1760 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
1761 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1762 VARIABLE_HEADER
*NextVariable
;
1764 EFI_PHYSICAL_ADDRESS FvVolHdr
;
1765 UINT64 TempVariableStoreHeader
;
1766 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
1771 UINT64 VariableStoreBase
;
1772 UINT64 VariableStoreLength
;
1773 EFI_EVENT ReadyToBootEvent
;
1775 Status
= EFI_SUCCESS
;
1777 // Allocate runtime memory for variable driver global structure.
1779 mVariableModuleGlobal
= AllocateRuntimePool (sizeof (VARIABLE_MODULE_GLOBAL
));
1780 if (mVariableModuleGlobal
== NULL
) {
1781 return EFI_OUT_OF_RESOURCES
;
1784 EfiInitializeLock(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
1785 mVariableModuleGlobal
->VariableGlobal
.ReentrantState
= 0;
1788 // Allocate memory for volatile variable store
1790 VolatileVariableStore
= AllocateRuntimePool (VARIABLE_STORE_SIZE
+ SCRATCH_SIZE
);
1791 if (VolatileVariableStore
== NULL
) {
1792 FreePool (mVariableModuleGlobal
);
1793 return EFI_OUT_OF_RESOURCES
;
1796 SetMem (VolatileVariableStore
, VARIABLE_STORE_SIZE
+ SCRATCH_SIZE
, 0xff);
1799 // Variable Specific Data
1801 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
1802 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
1804 VolatileVariableStore
->Signature
= VARIABLE_STORE_SIGNATURE
;
1805 VolatileVariableStore
->Size
= VARIABLE_STORE_SIZE
;
1806 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
1807 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
1808 VolatileVariableStore
->Reserved
= 0;
1809 VolatileVariableStore
->Reserved1
= 0;
1812 // Get non volatile varaible store
1815 TempVariableStoreHeader
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableBase
);
1816 VariableStoreBase
= TempVariableStoreHeader
+ \
1817 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1818 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
1819 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1821 // Mark the variable storage region of the FLASH as RUNTIME
1823 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
1824 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
1825 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
1827 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
1828 if (EFI_ERROR (Status
)) {
1832 Status
= gDS
->SetMemorySpaceAttributes (
1835 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
1837 if (EFI_ERROR (Status
)) {
1841 // Get address of non volatile variable store base
1843 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
1849 // Find the Correct Instance of the FV Block Service.
1852 CurrPtr
= (CHAR8
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1853 while (EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
) == EFI_SUCCESS
) {
1854 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
1855 if (CurrPtr
>= (CHAR8
*) FwVolHeader
&& CurrPtr
< (((CHAR8
*) FwVolHeader
) + FwVolHeader
->FvLength
)) {
1856 mVariableModuleGlobal
->FvbInstance
= Instance
;
1863 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) CurrPtr
;
1864 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
1865 if (~VariableStoreHeader
->Size
== 0) {
1866 Status
= UpdateVariableStore (
1867 &mVariableModuleGlobal
->VariableGlobal
,
1870 mVariableModuleGlobal
->FvbInstance
,
1871 (UINTN
) &VariableStoreHeader
->Size
,
1873 (UINT8
*) &VariableStoreLength
1876 // As Variables are stored in NV storage, which are slow devices,such as flash.
1877 // Variable operation may skip checking variable program result to improve performance,
1878 // We can assume Variable program is OK through some check point.
1879 // Variable Store Size Setting should be the first Variable write operation,
1880 // We can assume all Read/Write is OK if we can set Variable store size successfully.
1881 // If write fail, we will assert here
1883 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
1885 if (EFI_ERROR (Status
)) {
1890 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) CurrPtr
);
1892 // Parse non-volatile variable data and get last variable offset
1894 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) CurrPtr
);
1895 Status
= EFI_SUCCESS
;
1897 while (IsValidVariableHeader (NextVariable
)) {
1898 NextVariable
= GetNextVariablePtr (NextVariable
);
1901 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) CurrPtr
;
1904 // Check if the free area is really free.
1906 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
1907 Data
= ((UINT8
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)[Index
];
1910 // There must be something wrong in variable store, do reclaim operation.
1913 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1914 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1918 if (EFI_ERROR (Status
)) {
1927 // Register the event handling function to reclaim variable for OS usage.
1929 Status
= EfiCreateEventReadyToBootEx (
1938 if (EFI_ERROR (Status
)) {
1939 FreePool (mVariableModuleGlobal
);
1940 FreePool (VolatileVariableStore
);
1948 VariableClassAddressChangeEvent (
1955 (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
1959 (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
1961 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
1966 Variable Driver main entry point. The Variable driver places the 4 EFI
1967 runtime services in the EFI System Table and installs arch protocols
1968 for variable read and write services being availible.
1970 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1971 @param[in] SystemTable A pointer to the EFI System Table.
1973 @retval EFI_SUCCESS The entry point is executed successfully.
1974 @retval other Some error occurs when executing this entry point.
1979 VariableServiceInitialize (
1980 IN EFI_HANDLE ImageHandle
,
1981 IN EFI_SYSTEM_TABLE
*SystemTable
1986 Status
= VariableCommonInitialize (ImageHandle
, SystemTable
);
1987 ASSERT_EFI_ERROR (Status
);
1989 SystemTable
->RuntimeServices
->GetVariable
= RuntimeServiceGetVariable
;
1990 SystemTable
->RuntimeServices
->GetNextVariableName
= RuntimeServiceGetNextVariableName
;
1991 SystemTable
->RuntimeServices
->SetVariable
= RuntimeServiceSetVariable
;
1992 SystemTable
->RuntimeServices
->QueryVariableInfo
= RuntimeServiceQueryVariableInfo
;
1995 // Now install the Variable Runtime Architectural Protocol on a new handle
1997 Status
= gBS
->InstallMultipleProtocolInterfaces (
1999 &gEfiVariableArchProtocolGuid
, NULL
,
2000 &gEfiVariableWriteArchProtocolGuid
, NULL
,
2003 ASSERT_EFI_ERROR (Status
);
2005 Status
= gBS
->CreateEvent (
2006 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
,
2008 VariableClassAddressChangeEvent
,
2010 &mVirtualAddressChangeEvent
2012 ASSERT_EFI_ERROR (Status
);