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
;
26 // This is a temperary function which will be removed
27 // when EfiAcquireLock in UefiLib can handle the
28 // the call in UEFI Runtimer driver in RT phase.
31 AcquireLockOnlyAtBootTime (
35 if (!EfiAtRuntime ()) {
36 EfiAcquireLock (Lock
);
41 // This is a temperary function which will be removed
42 // when EfiAcquireLock in UefiLib can handle the
43 // the call in UEFI Runtimer driver in RT phase.
46 ReleaseLockOnlyAtBootTime (
50 if (!EfiAtRuntime ()) {
51 EfiReleaseLock (Lock
);
56 GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
60 Routine used to track statistical information about variable usage.
61 The data is stored in the EFI system table so it can be accessed later.
62 VariableInfo.efi can dump out the table. Only Boot Services variable
63 accesses are tracked by this code. The PcdVariableCollectStatistics
64 build flag controls if this feature is enabled.
66 A read that hits in the cache will have Read and Cache true for
67 the transaction. Data is allocated by this routine, but never
70 @param[in] VariableName Name of the Variable to track
71 @param[in] VendorGuid Guid of the Variable to track
72 @param[in] Volatile TRUE if volatile FALSE if non-volatile
73 @param[in] Read TRUE if GetVariable() was called
74 @param[in] Write TRUE if SetVariable() was called
75 @param[in] Delete TRUE if deleted via SetVariable()
76 @param[in] Cache TRUE for a cache hit.
81 IN CHAR16
*VariableName
,
82 IN EFI_GUID
*VendorGuid
,
90 VARIABLE_INFO_ENTRY
*Entry
;
92 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
94 if (EfiAtRuntime ()) {
95 // Don't collect statistics at runtime
99 if (gVariableInfo
== NULL
) {
101 // on the first call allocate a entry and place a pointer to it in
102 // the EFI System Table
104 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
105 ASSERT (gVariableInfo
!= NULL
);
107 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
108 gVariableInfo
->Name
= AllocatePool (StrLen (VariableName
));
109 StrCpy (gVariableInfo
->Name
, VariableName
);
110 gVariableInfo
->Volatile
= Volatile
;
112 gBS
->InstallConfigurationTable (&gEfiVariableInfoGuid
, gVariableInfo
);
116 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
117 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
118 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
126 Entry
->DeleteCount
++;
136 if (Entry
->Next
== NULL
) {
138 // If the entry is not in the table add it.
139 // Next iteration of the loop will fill in the data
141 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
142 ASSERT (Entry
->Next
!= NULL
);
144 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
145 Entry
->Next
->Name
= AllocatePool (StrLen (VariableName
));
146 StrCpy (Entry
->Next
->Name
, VariableName
);
147 Entry
->Next
->Volatile
= Volatile
;
156 IsValidVariableHeader (
157 IN VARIABLE_HEADER
*Variable
163 This code checks if variable header is valid or not.
166 Variable Pointer to the Variable Header.
169 TRUE Variable header is valid.
170 FALSE Variable header is not valid.
174 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
183 UpdateVariableStore (
184 IN VARIABLE_GLOBAL
*Global
,
186 IN BOOLEAN SetByIndex
,
188 IN UINTN DataPtrIndex
,
196 This function writes data to the FWH at the correct LBA even if the LBAs
201 Global - Pointer to VARAIBLE_GLOBAL structure
202 Volatile - If the Variable is Volatile or Non-Volatile
203 SetByIndex - TRUE: Target pointer is given as index
204 FALSE: Target pointer is absolute
205 Instance - Instance of FV Block services
206 DataPtrIndex - Pointer to the Data from the end of VARIABLE_STORE_HEADER
208 DataSize - Size of data to be written.
209 Buffer - Pointer to the buffer from which data is written
213 EFI_INVALID_PARAMETER - Parameters not valid
214 EFI_SUCCESS - Variable store successfully updated
218 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
226 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
227 VARIABLE_STORE_HEADER
*VolatileBase
;
228 EFI_PHYSICAL_ADDRESS FvVolHdr
;
229 EFI_PHYSICAL_ADDRESS DataPtr
;
233 DataPtr
= DataPtrIndex
;
236 // Check if the Data is Volatile
239 EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
);
240 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
242 // Data Pointer should point to the actual Address where data is to be
246 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
249 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
250 return EFI_INVALID_PARAMETER
;
254 // Data Pointer should point to the actual Address where data is to be
257 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
259 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
262 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
263 return EFI_INVALID_PARAMETER
;
267 // If Volatile Variable just do a simple mem copy.
269 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
274 // If we are here we are dealing with Non-Volatile Variables
276 LinearOffset
= (UINTN
) FwVolHeader
;
277 CurrWritePtr
= (UINTN
) DataPtr
;
278 CurrWriteSize
= DataSize
;
282 if (CurrWritePtr
< LinearOffset
) {
283 return EFI_INVALID_PARAMETER
;
286 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
287 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
289 // Check to see if the Variable Writes are spanning through multiple
292 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
293 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
294 Status
= EfiFvbWriteBlock (
297 (UINTN
) (CurrWritePtr
- LinearOffset
),
303 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
304 Status
= EfiFvbWriteBlock (
307 (UINTN
) (CurrWritePtr
- LinearOffset
),
311 if (EFI_ERROR (Status
)) {
315 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
316 CurrBuffer
= CurrBuffer
+ Size
;
317 CurrWriteSize
= CurrWriteSize
- Size
;
321 LinearOffset
+= PtrBlockMapEntry
->Length
;
330 VARIABLE_STORE_STATUS
331 GetVariableStoreStatus (
332 IN VARIABLE_STORE_HEADER
*VarStoreHeader
338 This code gets the current status of Variable Store.
342 VarStoreHeader Pointer to the Variable Store Header.
346 EfiRaw Variable store status is raw
347 EfiValid Variable store status is valid
348 EfiInvalid Variable store status is invalid
352 if (VarStoreHeader
->Signature
== VARIABLE_STORE_SIGNATURE
&&
353 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
354 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
358 } else if (VarStoreHeader
->Signature
== 0xffffffff &&
359 VarStoreHeader
->Size
== 0xffffffff &&
360 VarStoreHeader
->Format
== 0xff &&
361 VarStoreHeader
->State
== 0xff
373 IN VARIABLE_HEADER
*Variable
379 This code gets the size of name of variable.
383 Variable Pointer to the Variable Header.
387 UINTN Size of variable in bytes
391 if (Variable
->State
== (UINT8
) (-1) ||
392 Variable
->DataSize
== (UINT32
) -1 ||
393 Variable
->NameSize
== (UINT32
) -1 ||
394 Variable
->Attributes
== (UINT32
) -1) {
397 return (UINTN
) Variable
->NameSize
;
402 IN VARIABLE_HEADER
*Variable
408 This code gets the size of name of variable.
412 Variable Pointer to the Variable Header.
416 UINTN Size of variable in bytes
420 if (Variable
->State
== (UINT8
) -1 ||
421 Variable
->DataSize
== (UINT32
) -1 ||
422 Variable
->NameSize
== (UINT32
) -1 ||
423 Variable
->Attributes
== (UINT32
) -1) {
426 return (UINTN
) Variable
->DataSize
;
431 IN VARIABLE_HEADER
*Variable
437 This code gets the pointer to the variable name.
441 Variable Pointer to the Variable Header.
445 CHAR16* Pointer to Variable Name
450 return (CHAR16
*) (Variable
+ 1);
455 IN VARIABLE_HEADER
*Variable
461 This code gets the pointer to the variable data.
465 Variable Pointer to the Variable Header.
469 UINT8* Pointer to Variable Data
476 // Be careful about pad size for alignment
478 Value
= (UINTN
) GetVariableNamePtr (Variable
);
479 Value
+= NameSizeOfVariable (Variable
);
480 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
482 return (UINT8
*) Value
;
488 IN VARIABLE_HEADER
*Variable
494 This code gets the pointer to the next variable header.
498 Variable Pointer to the Variable Header.
502 VARIABLE_HEADER* Pointer to next variable header.
508 if (!IsValidVariableHeader (Variable
)) {
512 Value
= (UINTN
) GetVariableDataPtr (Variable
);
513 Value
+= DataSizeOfVariable (Variable
);
514 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
517 // Be careful about pad size for alignment
519 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
524 IN VARIABLE_STORE_HEADER
*VarStoreHeader
530 This code gets the pointer to the first variable memory pointer byte
534 VarStoreHeader Pointer to the Variable Store Header.
538 VARIABLE_HEADER* Pointer to last unavailable Variable Header
543 // The end of variable store
545 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
550 IN VARIABLE_STORE_HEADER
*VarStoreHeader
556 This code gets the pointer to the last variable memory pointer byte
560 VarStoreHeader Pointer to the Variable Store Header.
564 VARIABLE_HEADER* Pointer to last unavailable Variable Header
569 // The end of variable store
571 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
577 IN EFI_PHYSICAL_ADDRESS VariableBase
,
578 OUT UINTN
*LastVariableOffset
,
579 IN BOOLEAN IsVolatile
,
580 IN VARIABLE_HEADER
*UpdatingVariable
586 Variable store garbage collection and reclaim operation
590 VariableBase Base address of variable store
591 LastVariableOffset Offset of last variable
592 IsVolatile The variable store is volatile or not,
593 if it is non-volatile, need FTW
601 VARIABLE_HEADER
*Variable
;
602 VARIABLE_HEADER
*AddedVariable
;
603 VARIABLE_HEADER
*NextVariable
;
604 VARIABLE_HEADER
*NextAddedVariable
;
605 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
607 UINTN MaximumBufferSize
;
616 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
619 // Start Pointers for the variable.
621 Variable
= GetStartPointer (VariableStoreHeader
);
622 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
624 while (IsValidVariableHeader (Variable
)) {
625 NextVariable
= GetNextVariablePtr (Variable
);
626 if (Variable
->State
== VAR_ADDED
||
627 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
629 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
630 MaximumBufferSize
+= VariableSize
;
633 Variable
= NextVariable
;
637 // Reserve the 1 Bytes with Oxff to identify the
638 // end of the variable buffer.
640 MaximumBufferSize
+= 1;
641 ValidBuffer
= AllocatePool (MaximumBufferSize
);
642 if (ValidBuffer
== NULL
) {
643 return EFI_OUT_OF_RESOURCES
;
646 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
649 // Copy variable store header
651 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
652 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
655 // Start Pointers for the variable.
659 // Reinstall all ADDED variables
661 Variable
= GetStartPointer (VariableStoreHeader
);
662 while (IsValidVariableHeader (Variable
)) {
663 NextVariable
= GetNextVariablePtr (Variable
);
664 if (Variable
->State
== VAR_ADDED
) {
665 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
666 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
667 CurrPtr
+= VariableSize
;
670 Variable
= NextVariable
;
673 // Reinstall all in delete transition variables
675 Variable
= GetStartPointer (VariableStoreHeader
);
676 while (IsValidVariableHeader (Variable
)) {
677 NextVariable
= GetNextVariablePtr (Variable
);
678 if (Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
681 // Buffer has cached all ADDED variable.
682 // Per IN_DELETED variable, we have to guarantee that
683 // no ADDED one in previous buffer.
687 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
688 while (IsValidVariableHeader (AddedVariable
)) {
689 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
690 NameSize
= NameSizeOfVariable (AddedVariable
);
691 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
692 NameSize
== NameSizeOfVariable (Variable
)
694 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
695 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
699 NameSizeOfVariable (AddedVariable
)
706 AddedVariable
= NextAddedVariable
;
709 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
710 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
711 if (Variable
!= UpdatingVariable
) {
712 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
714 CurrPtr
+= VariableSize
;
718 Variable
= NextVariable
;
723 // If volatile variable store, just copy valid buffer
725 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
726 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
727 Status
= EFI_SUCCESS
;
730 // If non-volatile variable store, perform FTW here.
732 Status
= FtwVariableSpace (
735 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
738 if (!EFI_ERROR (Status
)) {
739 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
741 *LastVariableOffset
= 0;
744 FreePool (ValidBuffer
);
751 // The current Hii implementation accesses this variable a larg # of times on every boot.
752 // Other common variables are only accessed a single time. This is why this cache algorithm
753 // only targets a single variable. Probably to get an performance improvement out of
754 // a Cache you would need a cache that improves the search performance for a variable.
756 VARIABLE_CACHE_ENTRY mVariableCache
[] = {
758 &gEfiGlobalVariableGuid
,
768 Update the Cache with Variable information. These are the same
769 arguments as the EFI Variable services.
771 @param[in] VariableName Name of variable
772 @param[in] VendorGuid Guid of variable
773 @param[in] Attribute Attribue of the variable
774 @param[in] DataSize Size of data. 0 means delete
775 @param[in] Data Variable data
779 UpdateVariableCache (
780 IN CHAR16
*VariableName
,
781 IN EFI_GUID
*VendorGuid
,
782 IN UINT32 Attributes
,
787 VARIABLE_CACHE_ENTRY
*Entry
;
790 if (EfiAtRuntime ()) {
791 // Don't use the cache at runtime
795 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
796 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
797 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
798 Entry
->Attributes
= Attributes
;
801 if (Entry
->DataSize
!= 0) {
802 FreePool (Entry
->Data
);
804 Entry
->DataSize
= DataSize
;
805 } else if (DataSize
== Entry
->DataSize
) {
806 CopyMem (Entry
->Data
, Data
, DataSize
);
808 Entry
->Data
= AllocatePool (DataSize
);
809 Entry
->DataSize
= DataSize
;
810 CopyMem (Entry
->Data
, Data
, DataSize
);
819 Search the cache to see if the variable is in the cache.
821 @param[in] VariableName Name of variable
822 @param[in] VendorGuid Guid of variable
823 @param[in] Attribute Attribue returned
824 @param[in] DataSize Size of data returned
825 @param[in] Data Variable data returned
827 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.
828 @retval other Not found.
832 FindVariableInCache (
833 IN CHAR16
*VariableName
,
834 IN EFI_GUID
*VendorGuid
,
835 OUT UINT32
*Attributes OPTIONAL
,
836 IN OUT UINTN
*DataSize
,
840 VARIABLE_CACHE_ENTRY
*Entry
;
843 if (EfiAtRuntime ()) {
844 // Don't use the cache at runtime
845 return EFI_NOT_FOUND
;
848 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
849 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
850 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
851 if (Entry
->DataSize
== 0) {
852 // Variable was deleted so return not found
853 return EFI_NOT_FOUND
;
854 } else if (Entry
->DataSize
> *DataSize
) {
855 // If the buffer is too small return correct size
856 *DataSize
= Entry
->DataSize
;
857 return EFI_BUFFER_TOO_SMALL
;
859 *DataSize
= Entry
->DataSize
;
861 CopyMem (Data
, Entry
->Data
, Entry
->DataSize
);
862 if (Attributes
!= NULL
) {
863 *Attributes
= Entry
->Attributes
;
871 return EFI_NOT_FOUND
;
877 IN CHAR16
*VariableName
,
878 IN EFI_GUID
*VendorGuid
,
879 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
880 IN VARIABLE_GLOBAL
*Global
886 This code finds variable in storage blocks (Volatile or Non-Volatile)
890 VariableName Name of the variable to be found
891 VendorGuid Vendor GUID to be found.
892 PtrTrack Variable Track Pointer structure that contains
893 Variable Information.
894 Contains the pointer of Variable header.
895 Global VARIABLE_GLOBAL pointer
903 VARIABLE_HEADER
*Variable
[2];
904 VARIABLE_HEADER
*InDeletedVariable
;
905 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
906 UINTN InDeletedStorageIndex
;
911 // 0: Volatile, 1: Non-Volatile
912 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
913 // make use of this mapping to implement search algorithme.
915 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
916 VariableStoreHeader
[1] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
919 // Start Pointers for the variable.
920 // Actual Data Pointer where data can be written.
922 Variable
[0] = GetStartPointer (VariableStoreHeader
[0]);
923 Variable
[1] = GetStartPointer (VariableStoreHeader
[1]);
925 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
926 return EFI_INVALID_PARAMETER
;
930 // Find the variable by walk through volatile and then non-volatile variable store
932 InDeletedVariable
= NULL
;
933 InDeletedStorageIndex
= 0;
934 for (Index
= 0; Index
< 2; Index
++) {
935 while (IsValidVariableHeader (Variable
[Index
]) && (Variable
[Index
] <= GetEndPointer (VariableStoreHeader
[Index
]))) {
936 if (Variable
[Index
]->State
== VAR_ADDED
||
937 Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
939 if (!EfiAtRuntime () || (Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
940 if (VariableName
[0] == 0) {
941 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
942 InDeletedVariable
= Variable
[Index
];
943 InDeletedStorageIndex
= Index
;
945 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
946 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
947 PtrTrack
->CurrPtr
= Variable
[Index
];
948 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
953 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
954 Point
= (VOID
*) GetVariableNamePtr (Variable
[Index
]);
956 ASSERT (NameSizeOfVariable (Variable
[Index
]) != 0);
957 if (!CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
[Index
]))) {
958 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
959 InDeletedVariable
= Variable
[Index
];
960 InDeletedStorageIndex
= Index
;
962 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
963 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
964 PtrTrack
->CurrPtr
= Variable
[Index
];
965 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
975 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
977 if (InDeletedVariable
!= NULL
) {
978 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
979 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
980 PtrTrack
->CurrPtr
= InDeletedVariable
;
981 PtrTrack
->Volatile
= (BOOLEAN
)(InDeletedStorageIndex
== 0);
985 PtrTrack
->CurrPtr
= NULL
;
986 return EFI_NOT_FOUND
;
995 This code finds variable in storage blocks (Volatile or Non-Volatile)
999 VariableName Name of Variable to be found
1000 VendorGuid Variable vendor GUID
1001 Attributes OPTIONAL Attribute value of the variable found
1002 DataSize Size of Data found. If size is less than the
1003 data, this value contains the required size.
1005 Global Pointer to VARIABLE_GLOBAL structure
1006 Instance Instance of the Firmware Volume.
1010 EFI_INVALID_PARAMETER - Invalid parameter
1011 EFI_SUCCESS - Find the specified variable
1012 EFI_NOT_FOUND - Not found
1013 EFI_BUFFER_TO_SMALL - DataSize is too small for the result
1019 RuntimeServiceGetVariable (
1020 IN CHAR16
*VariableName
,
1021 IN EFI_GUID
*VendorGuid
,
1022 OUT UINT32
*Attributes OPTIONAL
,
1023 IN OUT UINTN
*DataSize
,
1028 VARIABLE_POINTER_TRACK Variable
;
1031 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1032 return EFI_INVALID_PARAMETER
;
1035 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1038 // Find existing variable
1040 Status
= FindVariableInCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1041 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_SUCCESS
)){
1043 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
);
1047 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1048 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1055 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1056 ASSERT (VarDataSize
!= 0);
1058 if (*DataSize
>= VarDataSize
) {
1060 Status
= EFI_INVALID_PARAMETER
;
1064 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1065 if (Attributes
!= NULL
) {
1066 *Attributes
= Variable
.CurrPtr
->Attributes
;
1069 *DataSize
= VarDataSize
;
1070 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1071 UpdateVariableCache (VariableName
, VendorGuid
, Variable
.CurrPtr
->Attributes
, VarDataSize
, Data
);
1073 Status
= EFI_SUCCESS
;
1076 *DataSize
= VarDataSize
;
1077 Status
= EFI_BUFFER_TOO_SMALL
;
1082 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1090 Routine Description:
1092 This code Finds the Next available variable
1096 VariableNameSize Size of the variable
1097 VariableName Pointer to variable name
1098 VendorGuid Variable Vendor Guid
1099 Global VARIABLE_GLOBAL structure pointer.
1100 Instance FV instance
1109 RuntimeServiceGetNextVariableName (
1110 IN OUT UINTN
*VariableNameSize
,
1111 IN OUT CHAR16
*VariableName
,
1112 IN OUT EFI_GUID
*VendorGuid
1115 VARIABLE_POINTER_TRACK Variable
;
1119 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1120 return EFI_INVALID_PARAMETER
;
1123 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1125 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1126 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1130 if (VariableName
[0] != 0) {
1132 // If variable name is not NULL, get next variable
1134 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1139 // If both volatile and non-volatile variable store are parsed,
1142 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
1143 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
1144 if (!Variable
.Volatile
) {
1145 Variable
.StartPtr
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1146 Variable
.EndPtr
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
));
1148 Status
= EFI_NOT_FOUND
;
1152 Variable
.CurrPtr
= Variable
.StartPtr
;
1153 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
1158 // Variable is found
1160 if (IsValidVariableHeader (Variable
.CurrPtr
) && Variable
.CurrPtr
->State
== VAR_ADDED
) {
1161 if (!(EfiAtRuntime () && !(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
))) {
1162 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
1163 ASSERT (VarNameSize
!= 0);
1165 if (VarNameSize
<= *VariableNameSize
) {
1168 GetVariableNamePtr (Variable
.CurrPtr
),
1173 &Variable
.CurrPtr
->VendorGuid
,
1176 Status
= EFI_SUCCESS
;
1178 Status
= EFI_BUFFER_TOO_SMALL
;
1181 *VariableNameSize
= VarNameSize
;
1186 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1190 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1197 Routine Description:
1199 This code sets variable in storage blocks (Volatile or Non-Volatile)
1203 VariableName Name of Variable to be found
1204 VendorGuid Variable vendor GUID
1205 Attributes Attribute value of the variable found
1206 DataSize Size of Data found. If size is less than the
1207 data, this value contains the required size.
1209 Global Pointer to VARIABLE_GLOBAL structure
1210 VolatileOffset The offset of last volatile variable
1211 NonVolatileOffset The offset of last non-volatile variable
1212 Instance Instance of the Firmware Volume.
1216 EFI_INVALID_PARAMETER - Invalid parameter
1217 EFI_SUCCESS - Set successfully
1218 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
1219 EFI_NOT_FOUND - Not found
1220 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
1221 EFI_WRITE_PROTECTED - Variable is read-only
1226 RuntimeServiceSetVariable (
1227 IN CHAR16
*VariableName
,
1228 IN EFI_GUID
*VendorGuid
,
1229 IN UINT32 Attributes
,
1234 VARIABLE_POINTER_TRACK Variable
;
1236 VARIABLE_HEADER
*NextVariable
;
1238 UINTN VarNameOffset
;
1239 UINTN VarDataOffset
;
1243 UINTN
*VolatileOffset
;
1244 UINTN
*NonVolatileOffset
;
1247 EFI_PHYSICAL_ADDRESS Point
;
1250 // Check input parameters
1252 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1253 return EFI_INVALID_PARAMETER
;
1256 // Make sure if runtime bit is set, boot service bit is set also
1258 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1259 return EFI_INVALID_PARAMETER
;
1263 // The size of the VariableName, including the Unicode Null in bytes plus
1264 // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)
1265 // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.
1267 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1268 if ((DataSize
> MAX_HARDWARE_ERROR_VARIABLE_SIZE
) ||
1269 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> MAX_HARDWARE_ERROR_VARIABLE_SIZE
)) {
1270 return EFI_INVALID_PARAMETER
;
1274 // The size of the VariableName, including the Unicode Null in bytes plus
1275 // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.
1277 if ((DataSize
> MAX_VARIABLE_SIZE
) ||
1278 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> MAX_VARIABLE_SIZE
)) {
1279 return EFI_INVALID_PARAMETER
;
1283 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1286 Instance
= mVariableModuleGlobal
->FvbInstance
;
1287 VolatileOffset
= &mVariableModuleGlobal
->VolatileLastVariableOffset
;
1290 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
1292 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
1293 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;;
1295 // Parse non-volatile variable data and get last variable offset
1297 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
1298 while (IsValidVariableHeader (NextVariable
)) {
1299 NextVariable
= GetNextVariablePtr (NextVariable
);
1301 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
1304 NonVolatileOffset
= &mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1308 // Check whether the input variable is already existed
1311 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1312 if (Status
== EFI_SUCCESS
&& Variable
.CurrPtr
!= NULL
) {
1314 // Update/Delete existing variable
1316 Volatile
= Variable
.Volatile
;
1318 if (EfiAtRuntime ()) {
1320 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1321 // the volatile is ReadOnly, and SetVariable should be aborted and
1322 // return EFI_WRITE_PROTECTED.
1324 if (Variable
.Volatile
) {
1325 Status
= EFI_WRITE_PROTECTED
;
1329 // Only variable have NV attribute can be updated/deleted in Runtime
1331 if (!(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)) {
1332 Status
= EFI_INVALID_PARAMETER
;
1337 // Setting a data variable with no access, or zero DataSize attributes
1338 // specified causes it to be deleted.
1340 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1341 State
= Variable
.CurrPtr
->State
;
1342 State
&= VAR_DELETED
;
1344 Status
= UpdateVariableStore (
1345 &mVariableModuleGlobal
->VariableGlobal
,
1349 (UINTN
) &Variable
.CurrPtr
->State
,
1353 if (!EFI_ERROR (Status
)) {
1354 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1355 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1360 // If the variable is marked valid and the same data has been passed in
1361 // then return to the caller immediately.
1363 if (DataSizeOfVariable (Variable
.CurrPtr
) == DataSize
&&
1364 (CompareMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), DataSize
) == 0)) {
1366 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1367 Status
= EFI_SUCCESS
;
1369 } else if ((Variable
.CurrPtr
->State
== VAR_ADDED
) ||
1370 (Variable
.CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1373 // Mark the old variable as in delete transition
1375 State
= Variable
.CurrPtr
->State
;
1376 State
&= VAR_IN_DELETED_TRANSITION
;
1378 Status
= UpdateVariableStore (
1379 &mVariableModuleGlobal
->VariableGlobal
,
1383 (UINTN
) &Variable
.CurrPtr
->State
,
1387 if (EFI_ERROR (Status
)) {
1391 } else if (Status
== EFI_NOT_FOUND
) {
1393 // Create a new variable
1397 // Make sure we are trying to create a new variable.
1398 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1400 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1401 Status
= EFI_NOT_FOUND
;
1406 // Only variable have NV|RT attribute can be created in Runtime
1408 if (EfiAtRuntime () &&
1409 (!(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) || !(Attributes
& EFI_VARIABLE_NON_VOLATILE
))) {
1410 Status
= EFI_INVALID_PARAMETER
;
1415 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
1417 ASSERT (Status
== EFI_INVALID_PARAMETER
);
1422 // Function part - create a new variable and copy the data.
1423 // Both update a variable and create a variable will come here.
1425 // Tricky part: Use scratch data area at the end of volatile variable store
1426 // as a temporary storage.
1428 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1430 SetMem (NextVariable
, SCRATCH_SIZE
, 0xff);
1432 NextVariable
->StartId
= VARIABLE_DATA
;
1433 NextVariable
->Attributes
= Attributes
;
1435 // NextVariable->State = VAR_ADDED;
1437 NextVariable
->Reserved
= 0;
1438 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1439 VarNameSize
= StrSize (VariableName
);
1441 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1445 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1447 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1451 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1453 // There will be pad bytes after Data, the NextVariable->NameSize and
1454 // NextVariable->DataSize should not include pad size so that variable
1455 // service can get actual size in GetVariable
1457 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1458 NextVariable
->DataSize
= (UINT32
)DataSize
;
1461 // The actual size of the variable that stores in storage should
1462 // include pad size.
1464 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1465 if (Attributes
& EFI_VARIABLE_NON_VOLATILE
) {
1467 // Create a nonvolatile variable
1471 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1472 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
1474 if (EfiAtRuntime ()) {
1475 Status
= EFI_OUT_OF_RESOURCES
;
1479 // Perform garbage collection & reclaim operation
1481 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
, NonVolatileOffset
, FALSE
, Variable
.CurrPtr
);
1482 if (EFI_ERROR (Status
)) {
1486 // If still no enough space, return out of resources
1488 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1489 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
1491 Status
= EFI_OUT_OF_RESOURCES
;
1499 // 1. Write variable header
1500 // 2. Set variable state to header valid
1501 // 3. Write variable data
1502 // 4. Set variable state to valid
1507 Status
= UpdateVariableStore (
1508 &mVariableModuleGlobal
->VariableGlobal
,
1513 sizeof (VARIABLE_HEADER
),
1514 (UINT8
*) NextVariable
1517 if (EFI_ERROR (Status
)) {
1524 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1525 Status
= UpdateVariableStore (
1526 &mVariableModuleGlobal
->VariableGlobal
,
1531 sizeof (VARIABLE_HEADER
),
1532 (UINT8
*) NextVariable
1535 if (EFI_ERROR (Status
)) {
1541 Status
= UpdateVariableStore (
1542 &mVariableModuleGlobal
->VariableGlobal
,
1546 *NonVolatileOffset
+ sizeof (VARIABLE_HEADER
),
1547 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1548 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1551 if (EFI_ERROR (Status
)) {
1557 NextVariable
->State
= VAR_ADDED
;
1558 Status
= UpdateVariableStore (
1559 &mVariableModuleGlobal
->VariableGlobal
,
1564 sizeof (VARIABLE_HEADER
),
1565 (UINT8
*) NextVariable
1568 if (EFI_ERROR (Status
)) {
1572 *NonVolatileOffset
= HEADER_ALIGN (*NonVolatileOffset
+ VarSize
);
1576 // Create a volatile variable
1580 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1581 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1583 // Perform garbage collection & reclaim operation
1585 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
, VolatileOffset
, TRUE
, Variable
.CurrPtr
);
1586 if (EFI_ERROR (Status
)) {
1590 // If still no enough space, return out of resources
1592 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1593 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1595 Status
= EFI_OUT_OF_RESOURCES
;
1602 NextVariable
->State
= VAR_ADDED
;
1603 Status
= UpdateVariableStore (
1604 &mVariableModuleGlobal
->VariableGlobal
,
1610 (UINT8
*) NextVariable
1613 if (EFI_ERROR (Status
)) {
1617 *VolatileOffset
= HEADER_ALIGN (*VolatileOffset
+ VarSize
);
1620 // Mark the old variable as deleted
1622 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
.CurrPtr
!= NULL
) {
1623 State
= Variable
.CurrPtr
->State
;
1624 State
&= VAR_DELETED
;
1626 Status
= UpdateVariableStore (
1627 &mVariableModuleGlobal
->VariableGlobal
,
1631 (UINTN
) &Variable
.CurrPtr
->State
,
1636 if (!EFI_ERROR (Status
)) {
1637 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1638 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1643 Status
= EFI_SUCCESS
;
1644 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1645 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1648 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
1649 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1657 Routine Description:
1659 This code returns information about the EFI variables.
1663 Attributes Attributes bitmask to specify the type of variables
1664 on which to return information.
1665 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1666 for the EFI variables associated with the attributes specified.
1667 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1668 for EFI variables associated with the attributes specified.
1669 MaximumVariableSize Pointer to the maximum size of an individual EFI variables
1670 associated with the attributes specified.
1671 Global Pointer to VARIABLE_GLOBAL structure.
1672 Instance Instance of the Firmware Volume.
1677 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
1678 EFI_SUCCESS - Query successfully.
1679 EFI_UNSUPPORTED - The attribute is not supported on this platform.
1684 RuntimeServiceQueryVariableInfo (
1685 IN UINT32 Attributes
,
1686 OUT UINT64
*MaximumVariableStorageSize
,
1687 OUT UINT64
*RemainingVariableStorageSize
,
1688 OUT UINT64
*MaximumVariableSize
1691 VARIABLE_HEADER
*Variable
;
1692 VARIABLE_HEADER
*NextVariable
;
1693 UINT64 VariableSize
;
1694 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1696 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1697 return EFI_INVALID_PARAMETER
;
1700 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1702 // Make sure the Attributes combination is supported by the platform.
1704 return EFI_UNSUPPORTED
;
1705 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1707 // Make sure if runtime bit is set, boot service bit is set also.
1709 return EFI_INVALID_PARAMETER
;
1710 } else if (EfiAtRuntime () && !(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
1712 // Make sure RT Attribute is set if we are in Runtime phase.
1714 return EFI_INVALID_PARAMETER
;
1717 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1719 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1721 // Query is Volatile related.
1723 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
1726 // Query is Non-Volatile related.
1728 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1732 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1733 // with the storage size (excluding the storage header size).
1735 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1736 *RemainingVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1739 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.
1741 *MaximumVariableSize
= MAX_VARIABLE_SIZE
- sizeof (VARIABLE_HEADER
);
1744 // Harware error record variable needs larger size.
1746 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1747 *MaximumVariableSize
= MAX_HARDWARE_ERROR_VARIABLE_SIZE
- sizeof (VARIABLE_HEADER
);
1751 // Point to the starting address of the variables.
1753 Variable
= GetStartPointer (VariableStoreHeader
);
1756 // Now walk through the related variable store.
1758 while (IsValidVariableHeader (Variable
) && (Variable
< GetEndPointer (VariableStoreHeader
))) {
1759 NextVariable
= GetNextVariablePtr (Variable
);
1760 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1762 if (EfiAtRuntime ()) {
1764 // we don't take the state of the variables in mind
1765 // when calculating RemainingVariableStorageSize,
1766 // since the space occupied by variables not marked with
1767 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1769 *RemainingVariableStorageSize
-= VariableSize
;
1772 // Only care about Variables with State VAR_ADDED,because
1773 // the space not marked as VAR_ADDED is reclaimable now.
1775 if (Variable
->State
== VAR_ADDED
) {
1776 *RemainingVariableStorageSize
-= VariableSize
;
1781 // Go to the next one
1783 Variable
= NextVariable
;
1786 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
1787 *MaximumVariableSize
= 0;
1788 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
1789 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
1792 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1806 VarSize
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1807 Status
= EFI_SUCCESS
;
1810 // Check if the free area is blow a threshold
1812 if ((VarSize
- mVariableModuleGlobal
->NonVolatileLastVariableOffset
) < VARIABLE_RECLAIM_THRESHOLD
) {
1814 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1815 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1819 ASSERT_EFI_ERROR (Status
);
1824 VariableCommonInitialize (
1825 IN EFI_HANDLE ImageHandle
,
1826 IN EFI_SYSTEM_TABLE
*SystemTable
1830 Routine Description:
1831 This function does common initialization for variable services
1835 ImageHandle - The firmware allocated handle for the EFI image.
1836 SystemTable - A pointer to the EFI System Table.
1842 EFI_NOT_FOUND - Variable store area not found.
1843 EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.
1844 EFI_SUCCESS - Variable services successfully initialized.
1849 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1851 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
1852 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1853 VARIABLE_HEADER
*NextVariable
;
1855 EFI_PHYSICAL_ADDRESS FvVolHdr
;
1856 UINT64 TempVariableStoreHeader
;
1857 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
1862 UINT64 VariableStoreBase
;
1863 UINT64 VariableStoreLength
;
1864 EFI_EVENT ReadyToBootEvent
;
1866 Status
= EFI_SUCCESS
;
1868 // Allocate runtime memory for variable driver global structure.
1870 mVariableModuleGlobal
= AllocateRuntimePool (sizeof (VARIABLE_MODULE_GLOBAL
));
1871 if (mVariableModuleGlobal
== NULL
) {
1872 return EFI_OUT_OF_RESOURCES
;
1875 EfiInitializeLock(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
1876 mVariableModuleGlobal
->VariableGlobal
.ReentrantState
= 0;
1879 // Allocate memory for volatile variable store
1881 VolatileVariableStore
= AllocateRuntimePool (VARIABLE_STORE_SIZE
+ SCRATCH_SIZE
);
1882 if (VolatileVariableStore
== NULL
) {
1883 FreePool (mVariableModuleGlobal
);
1884 return EFI_OUT_OF_RESOURCES
;
1887 SetMem (VolatileVariableStore
, VARIABLE_STORE_SIZE
+ SCRATCH_SIZE
, 0xff);
1890 // Variable Specific Data
1892 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
1893 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
1895 VolatileVariableStore
->Signature
= VARIABLE_STORE_SIGNATURE
;
1896 VolatileVariableStore
->Size
= VARIABLE_STORE_SIZE
;
1897 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
1898 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
1899 VolatileVariableStore
->Reserved
= 0;
1900 VolatileVariableStore
->Reserved1
= 0;
1903 // Get non volatile varaible store
1906 TempVariableStoreHeader
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableBase
);
1907 VariableStoreBase
= TempVariableStoreHeader
+ \
1908 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1909 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
1910 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1912 // Mark the variable storage region of the FLASH as RUNTIME
1914 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
1915 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
1916 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
1918 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
1919 if (EFI_ERROR (Status
)) {
1923 Status
= gDS
->SetMemorySpaceAttributes (
1926 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
1928 if (EFI_ERROR (Status
)) {
1932 // Get address of non volatile variable store base
1934 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
1940 // Find the Correct Instance of the FV Block Service.
1943 CurrPtr
= (CHAR8
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1944 while (EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
) == EFI_SUCCESS
) {
1945 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
1946 if (CurrPtr
>= (CHAR8
*) FwVolHeader
&& CurrPtr
< (((CHAR8
*) FwVolHeader
) + FwVolHeader
->FvLength
)) {
1947 mVariableModuleGlobal
->FvbInstance
= Instance
;
1954 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) CurrPtr
;
1955 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
1956 if (~VariableStoreHeader
->Size
== 0) {
1957 Status
= UpdateVariableStore (
1958 &mVariableModuleGlobal
->VariableGlobal
,
1961 mVariableModuleGlobal
->FvbInstance
,
1962 (UINTN
) &VariableStoreHeader
->Size
,
1964 (UINT8
*) &VariableStoreLength
1967 // As Variables are stored in NV storage, which are slow devices,such as flash.
1968 // Variable operation may skip checking variable program result to improve performance,
1969 // We can assume Variable program is OK through some check point.
1970 // Variable Store Size Setting should be the first Variable write operation,
1971 // We can assume all Read/Write is OK if we can set Variable store size successfully.
1972 // If write fail, we will assert here
1974 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
1976 if (EFI_ERROR (Status
)) {
1981 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) CurrPtr
);
1983 // Parse non-volatile variable data and get last variable offset
1985 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) CurrPtr
);
1986 Status
= EFI_SUCCESS
;
1988 while (IsValidVariableHeader (NextVariable
)) {
1989 NextVariable
= GetNextVariablePtr (NextVariable
);
1992 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) CurrPtr
;
1995 // Check if the free area is really free.
1997 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
1998 Data
= ((UINT8
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)[Index
];
2001 // There must be something wrong in variable store, do reclaim operation.
2004 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2005 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2010 if (EFI_ERROR (Status
)) {
2019 // Register the event handling function to reclaim variable for OS usage.
2021 Status
= EfiCreateEventReadyToBootEx (
2030 if (EFI_ERROR (Status
)) {
2031 FreePool (mVariableModuleGlobal
);
2032 FreePool (VolatileVariableStore
);
2040 VariableClassAddressChangeEvent (
2047 (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
2051 (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
2053 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
2058 Variable Driver main entry point. The Variable driver places the 4 EFI
2059 runtime services in the EFI System Table and installs arch protocols
2060 for variable read and write services being availible.
2062 @param[in] ImageHandle The firmware allocated handle for the EFI image.
2063 @param[in] SystemTable A pointer to the EFI System Table.
2065 @retval EFI_SUCCESS The entry point is executed successfully.
2066 @retval other Some error occurs when executing this entry point.
2071 VariableServiceInitialize (
2072 IN EFI_HANDLE ImageHandle
,
2073 IN EFI_SYSTEM_TABLE
*SystemTable
2078 Status
= VariableCommonInitialize (ImageHandle
, SystemTable
);
2079 ASSERT_EFI_ERROR (Status
);
2081 SystemTable
->RuntimeServices
->GetVariable
= RuntimeServiceGetVariable
;
2082 SystemTable
->RuntimeServices
->GetNextVariableName
= RuntimeServiceGetNextVariableName
;
2083 SystemTable
->RuntimeServices
->SetVariable
= RuntimeServiceSetVariable
;
2084 SystemTable
->RuntimeServices
->QueryVariableInfo
= RuntimeServiceQueryVariableInfo
;
2087 // Now install the Variable Runtime Architectural Protocol on a new handle
2089 Status
= gBS
->InstallMultipleProtocolInterfaces (
2091 &gEfiVariableArchProtocolGuid
, NULL
,
2092 &gEfiVariableWriteArchProtocolGuid
, NULL
,
2095 ASSERT_EFI_ERROR (Status
);
2097 Status
= gBS
->CreateEvent (
2098 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
,
2100 VariableClassAddressChangeEvent
,
2102 &mVirtualAddressChangeEvent
2104 ASSERT_EFI_ERROR (Status
);