2 EFI Runtime Variable services.
4 Copyright (c) 2006 - 2007, 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.
19 VARIABLE_MODULE_GLOBAL mRuntimeData
;
20 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
= &mRuntimeData
;
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
;
157 IsValidVariableHeader (
158 IN VARIABLE_HEADER
*Variable
164 This code checks if variable header is valid or not.
167 Variable Pointer to the Variable Header.
170 TRUE Variable header is valid.
171 FALSE Variable header is not valid.
175 if (Variable
== NULL
||
176 Variable
->StartId
!= VARIABLE_DATA
||
177 (sizeof (VARIABLE_HEADER
) + Variable
->NameSize
+ Variable
->DataSize
) > MAX_VARIABLE_SIZE
187 UpdateVariableStore (
188 IN VARIABLE_GLOBAL
*Global
,
190 IN BOOLEAN SetByIndex
,
192 IN UINTN DataPtrIndex
,
200 This function writes data to the FWH at the correct LBA even if the LBAs
205 Global - Pointer to VARAIBLE_GLOBAL structure
206 Volatile - If the Variable is Volatile or Non-Volatile
207 SetByIndex - TRUE: Target pointer is given as index
208 FALSE: Target pointer is absolute
209 Instance - Instance of FV Block services
210 DataPtrIndex - Pointer to the Data from the end of VARIABLE_STORE_HEADER
212 DataSize - Size of data to be written.
213 Buffer - Pointer to the buffer from which data is written
217 EFI_INVALID_PARAMETER - Parameters not valid
218 EFI_SUCCESS - Variable store successfully updated
222 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
230 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
231 VARIABLE_STORE_HEADER
*VolatileBase
;
232 EFI_PHYSICAL_ADDRESS FvVolHdr
;
233 EFI_PHYSICAL_ADDRESS DataPtr
;
237 DataPtr
= DataPtrIndex
;
240 // Check if the Data is Volatile
243 EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
);
244 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
246 // Data Pointer should point to the actual Address where data is to be
250 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
253 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
254 return EFI_INVALID_PARAMETER
;
258 // Data Pointer should point to the actual Address where data is to be
261 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
263 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
266 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
267 return EFI_INVALID_PARAMETER
;
271 // If Volatile Variable just do a simple mem copy.
273 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
278 // If we are here we are dealing with Non-Volatile Variables
280 LinearOffset
= (UINTN
) FwVolHeader
;
281 CurrWritePtr
= (UINTN
) DataPtr
;
282 CurrWriteSize
= DataSize
;
286 if (CurrWritePtr
< LinearOffset
) {
287 return EFI_INVALID_PARAMETER
;
290 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
291 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
293 // Check to see if the Variable Writes are spanning through multiple
296 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
297 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
298 Status
= EfiFvbWriteBlock (
301 (UINTN
) (CurrWritePtr
- LinearOffset
),
307 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
308 Status
= EfiFvbWriteBlock (
311 (UINTN
) (CurrWritePtr
- LinearOffset
),
315 if (EFI_ERROR (Status
)) {
319 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
320 CurrBuffer
= CurrBuffer
+ Size
;
321 CurrWriteSize
= CurrWriteSize
- Size
;
325 LinearOffset
+= PtrBlockMapEntry
->Length
;
334 VARIABLE_STORE_STATUS
335 GetVariableStoreStatus (
336 IN VARIABLE_STORE_HEADER
*VarStoreHeader
342 This code gets the current status of Variable Store.
346 VarStoreHeader Pointer to the Variable Store Header.
350 EfiRaw Variable store status is raw
351 EfiValid Variable store status is valid
352 EfiInvalid Variable store status is invalid
356 if (VarStoreHeader
->Signature
== VARIABLE_STORE_SIGNATURE
&&
357 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
358 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
362 } else if (VarStoreHeader
->Signature
== 0xffffffff &&
363 VarStoreHeader
->Size
== 0xffffffff &&
364 VarStoreHeader
->Format
== 0xff &&
365 VarStoreHeader
->State
== 0xff
377 IN VARIABLE_HEADER
*Variable
383 This code gets the pointer to the variable data.
387 Variable Pointer to the Variable Header.
391 UINT8* Pointer to Variable Data
396 // Be careful about pad size for alignment
398 return (UINT8
*) ((UINTN
) GET_VARIABLE_NAME_PTR (Variable
) + Variable
->NameSize
+ GET_PAD_SIZE (Variable
->NameSize
));
404 IN VARIABLE_HEADER
*Variable
410 This code gets the pointer to the next variable header.
414 Variable Pointer to the Variable Header.
418 VARIABLE_HEADER* Pointer to next variable header.
422 if (!IsValidVariableHeader (Variable
)) {
426 // Be careful about pad size for alignment
428 return (VARIABLE_HEADER
*) ((UINTN
) GetVariableDataPtr (Variable
) + Variable
->DataSize
+ GET_PAD_SIZE (Variable
->DataSize
));
434 IN VARIABLE_STORE_HEADER
*VarStoreHeader
440 This code gets the pointer to the last variable memory pointer byte
444 VarStoreHeader Pointer to the Variable Store Header.
448 VARIABLE_HEADER* Pointer to last unavailable Variable Header
453 // The end of variable store
455 return (VARIABLE_HEADER
*) ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
461 IN EFI_PHYSICAL_ADDRESS VariableBase
,
462 OUT UINTN
*LastVariableOffset
,
463 IN BOOLEAN IsVolatile
469 Variable store garbage collection and reclaim operation
473 VariableBase Base address of variable store
474 LastVariableOffset Offset of last variable
475 IsVolatile The variable store is volatile or not,
476 if it is non-volatile, need FTW
484 VARIABLE_HEADER
*Variable
;
485 VARIABLE_HEADER
*NextVariable
;
486 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
488 UINTN ValidBufferSize
;
493 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
496 // Start Pointers for the variable.
498 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
500 ValidBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
502 while (IsValidVariableHeader (Variable
)) {
503 NextVariable
= GetNextVariablePtr (Variable
);
504 if (Variable
->State
== VAR_ADDED
) {
505 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
506 ValidBufferSize
+= VariableSize
;
509 Variable
= NextVariable
;
512 ValidBuffer
= AllocatePool (ValidBufferSize
);
513 if (ValidBuffer
== NULL
) {
514 return EFI_OUT_OF_RESOURCES
;
517 SetMem (ValidBuffer
, ValidBufferSize
, 0xff);
519 CurrPtr
= ValidBuffer
;
522 // Copy variable store header
524 CopyMem (CurrPtr
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
525 CurrPtr
+= sizeof (VARIABLE_STORE_HEADER
);
528 // Start Pointers for the variable.
530 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
532 while (IsValidVariableHeader (Variable
)) {
533 NextVariable
= GetNextVariablePtr (Variable
);
534 if (Variable
->State
== VAR_ADDED
) {
535 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
536 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
537 CurrPtr
+= VariableSize
;
540 Variable
= NextVariable
;
545 // If volatile variable store, just copy valid buffer
547 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
548 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, ValidBufferSize
);
549 *LastVariableOffset
= ValidBufferSize
;
550 Status
= EFI_SUCCESS
;
553 // If non-volatile variable store, perform FTW here.
555 Status
= FtwVariableSpace (
560 if (!EFI_ERROR (Status
)) {
561 *LastVariableOffset
= ValidBufferSize
;
565 FreePool (ValidBuffer
);
567 if (EFI_ERROR (Status
)) {
568 *LastVariableOffset
= 0;
576 // The current Hii implementation accesses this variable a larg # of times on every boot.
577 // Other common variables are only accessed a single time. This is why this cache algorithm
578 // only targets a single variable. Probably to get an performance improvement out of
579 // a Cache you would need a cache that improves the search performance for a variable.
581 VARIABLE_CACHE_ENTRY mVariableCache
[] = {
583 &gEfiGlobalVariableGuid
,
593 Update the Cache with Variable information. These are the same
594 arguments as the EFI Variable services.
596 @param[in] VariableName Name of variable
597 @param[in] VendorGuid Guid of variable
598 @param[in] Attribute Attribue of the variable
599 @param[in] DataSize Size of data. 0 means delete
600 @param[in] Data Variable data
604 UpdateVariableCache (
605 IN CHAR16
*VariableName
,
606 IN EFI_GUID
*VendorGuid
,
607 IN UINT32 Attributes
,
612 VARIABLE_CACHE_ENTRY
*Entry
;
615 if (EfiAtRuntime ()) {
616 // Don't use the cache at runtime
620 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
621 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
622 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
623 Entry
->Attributes
= Attributes
;
626 if (Entry
->DataSize
!= 0) {
627 FreePool (Entry
->Data
);
629 Entry
->DataSize
= DataSize
;
630 } else if (DataSize
== Entry
->DataSize
) {
631 CopyMem (Entry
->Data
, Data
, DataSize
);
633 Entry
->Data
= AllocatePool (DataSize
);
634 Entry
->DataSize
= DataSize
;
635 CopyMem (Entry
->Data
, Data
, DataSize
);
644 Search the cache to see if the variable is in the cache.
646 @param[in] VariableName Name of variable
647 @param[in] VendorGuid Guid of variable
648 @param[in] Attribute Attribue returned
649 @param[in] DataSize Size of data returned
650 @param[in] Data Variable data returned
652 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.
653 @retval other Not found.
657 FindVariableInCache (
658 IN CHAR16
*VariableName
,
659 IN EFI_GUID
*VendorGuid
,
660 OUT UINT32
*Attributes OPTIONAL
,
661 IN OUT UINTN
*DataSize
,
665 VARIABLE_CACHE_ENTRY
*Entry
;
668 if (EfiAtRuntime ()) {
669 // Don't use the cache at runtime
670 return EFI_NOT_FOUND
;
673 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
674 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
675 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
676 if (Entry
->DataSize
== 0) {
677 // Variable was deleted so return not found
678 return EFI_NOT_FOUND
;
679 } else if (Entry
->DataSize
!= *DataSize
) {
680 // If the buffer is too small return correct size
681 *DataSize
= Entry
->DataSize
;
682 return EFI_BUFFER_TOO_SMALL
;
685 CopyMem (Data
, Entry
->Data
, Entry
->DataSize
);
686 if (Attributes
!= NULL
) {
687 *Attributes
= Entry
->Attributes
;
695 return EFI_NOT_FOUND
;
701 IN CHAR16
*VariableName
,
702 IN EFI_GUID
*VendorGuid
,
703 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
704 IN VARIABLE_GLOBAL
*Global
710 This code finds variable in storage blocks (Volatile or Non-Volatile)
714 VariableName Name of the variable to be found
715 VendorGuid Vendor GUID to be found.
716 PtrTrack Variable Track Pointer structure that contains
717 Variable Information.
718 Contains the pointer of Variable header.
719 Global VARIABLE_GLOBAL pointer
727 VARIABLE_HEADER
*Variable
[2];
728 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
732 // We aquire the lock at the entry of FindVariable as GetVariable, GetNextVariableName
733 // SetVariable all call FindVariable at entry point. Please move "Aquire Lock" to
734 // the correct places if this assumption does not hold TRUE anymore.
736 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
739 // 0: Volatile, 1: Non-Volatile
740 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
741 // make use of this mapping to implement search algorithme.
743 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
744 VariableStoreHeader
[1] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
747 // Start Pointers for the variable.
748 // Actual Data Pointer where data can be written.
750 Variable
[0] = (VARIABLE_HEADER
*) (VariableStoreHeader
[0] + 1);
751 Variable
[1] = (VARIABLE_HEADER
*) (VariableStoreHeader
[1] + 1);
753 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
754 return EFI_INVALID_PARAMETER
;
757 // Find the variable by walk through volatile and then non-volatile variable store
759 for (Index
= 0; Index
< 2; Index
++) {
760 PtrTrack
->StartPtr
= (VARIABLE_HEADER
*) (VariableStoreHeader
[Index
] + 1);
761 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
763 while (IsValidVariableHeader (Variable
[Index
]) && (Variable
[Index
] <= GetEndPointer (VariableStoreHeader
[Index
]))) {
764 if (Variable
[Index
]->State
== VAR_ADDED
) {
765 if (!EfiAtRuntime () || (Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
766 if (VariableName
[0] == 0) {
767 PtrTrack
->CurrPtr
= Variable
[Index
];
768 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
771 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
772 if (!CompareMem (VariableName
, GET_VARIABLE_NAME_PTR (Variable
[Index
]), Variable
[Index
]->NameSize
)) {
773 PtrTrack
->CurrPtr
= Variable
[Index
];
774 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
782 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
785 PtrTrack
->CurrPtr
= NULL
;
786 return EFI_NOT_FOUND
;
795 This code finds variable in storage blocks (Volatile or Non-Volatile)
799 VariableName Name of Variable to be found
800 VendorGuid Variable vendor GUID
801 Attributes OPTIONAL Attribute value of the variable found
802 DataSize Size of Data found. If size is less than the
803 data, this value contains the required size.
805 Global Pointer to VARIABLE_GLOBAL structure
806 Instance Instance of the Firmware Volume.
810 EFI_INVALID_PARAMETER - Invalid parameter
811 EFI_SUCCESS - Find the specified variable
812 EFI_NOT_FOUND - Not found
813 EFI_BUFFER_TO_SMALL - DataSize is too small for the result
819 RuntimeServiceGetVariable (
820 IN CHAR16
*VariableName
,
821 IN EFI_GUID
*VendorGuid
,
822 OUT UINT32
*Attributes OPTIONAL
,
823 IN OUT UINTN
*DataSize
,
828 VARIABLE_POINTER_TRACK Variable
;
831 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
832 return EFI_INVALID_PARAMETER
;
836 // Find existing variable
838 Status
= FindVariableInCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
839 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_SUCCESS
)){
841 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
);
845 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
846 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
853 VarDataSize
= Variable
.CurrPtr
->DataSize
;
854 if (*DataSize
>= VarDataSize
) {
856 Status
= EFI_INVALID_PARAMETER
;
860 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
861 if (Attributes
!= NULL
) {
862 *Attributes
= Variable
.CurrPtr
->Attributes
;
865 *DataSize
= VarDataSize
;
866 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
867 UpdateVariableCache (VariableName
, VendorGuid
, Variable
.CurrPtr
->Attributes
, VarDataSize
, Data
);
869 Status
= EFI_SUCCESS
;
872 *DataSize
= VarDataSize
;
873 Status
= EFI_BUFFER_TOO_SMALL
;
878 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
888 This code Finds the Next available variable
892 VariableNameSize Size of the variable
893 VariableName Pointer to variable name
894 VendorGuid Variable Vendor Guid
895 Global VARIABLE_GLOBAL structure pointer.
905 RuntimeServiceGetNextVariableName (
906 IN OUT UINTN
*VariableNameSize
,
907 IN OUT CHAR16
*VariableName
,
908 IN OUT EFI_GUID
*VendorGuid
911 VARIABLE_POINTER_TRACK Variable
;
915 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
916 return EFI_INVALID_PARAMETER
;
919 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
920 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
924 if (VariableName
[0] != 0) {
926 // If variable name is not NULL, get next variable
928 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
933 // If both volatile and non-volatile variable store are parsed,
936 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
937 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
938 if (!Variable
.Volatile
) {
939 Variable
.StartPtr
= (VARIABLE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
+ sizeof (VARIABLE_STORE_HEADER
)));
940 Variable
.EndPtr
= (VARIABLE_HEADER
*) GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
));
942 Status
= EFI_NOT_FOUND
;
946 Variable
.CurrPtr
= Variable
.StartPtr
;
947 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
954 if (IsValidVariableHeader (Variable
.CurrPtr
) && Variable
.CurrPtr
->State
== VAR_ADDED
) {
955 if (!(EfiAtRuntime () && !(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
))) {
956 VarNameSize
= Variable
.CurrPtr
->NameSize
;
957 if (VarNameSize
<= *VariableNameSize
) {
960 GET_VARIABLE_NAME_PTR (Variable
.CurrPtr
),
965 &Variable
.CurrPtr
->VendorGuid
,
968 Status
= EFI_SUCCESS
;
970 Status
= EFI_BUFFER_TOO_SMALL
;
973 *VariableNameSize
= VarNameSize
;
978 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
982 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
991 This code sets variable in storage blocks (Volatile or Non-Volatile)
995 VariableName Name of Variable to be found
996 VendorGuid Variable vendor GUID
997 Attributes Attribute value of the variable found
998 DataSize Size of Data found. If size is less than the
999 data, this value contains the required size.
1001 Global Pointer to VARIABLE_GLOBAL structure
1002 VolatileOffset The offset of last volatile variable
1003 NonVolatileOffset The offset of last non-volatile variable
1004 Instance Instance of the Firmware Volume.
1008 EFI_INVALID_PARAMETER - Invalid parameter
1009 EFI_SUCCESS - Set successfully
1010 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
1011 EFI_NOT_FOUND - Not found
1012 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
1013 EFI_WRITE_PROTECTED - Variable is read-only
1018 RuntimeServiceSetVariable (
1019 IN CHAR16
*VariableName
,
1020 IN EFI_GUID
*VendorGuid
,
1021 IN UINT32 Attributes
,
1026 VARIABLE_POINTER_TRACK Variable
;
1028 VARIABLE_HEADER
*NextVariable
;
1030 UINTN VarNameOffset
;
1031 UINTN VarDataOffset
;
1035 UINTN
*VolatileOffset
;
1036 UINTN
*NonVolatileOffset
;
1041 VolatileOffset
= &mVariableModuleGlobal
->VolatileLastVariableOffset
;
1042 NonVolatileOffset
= &mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1043 Instance
= mVariableModuleGlobal
->FvbInstance
;
1046 // Check input parameters
1048 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1049 return EFI_INVALID_PARAMETER
;
1052 // Make sure if runtime bit is set, boot service bit is set also
1054 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1055 return EFI_INVALID_PARAMETER
;
1058 // The size of the VariableName, including the Unicode Null in bytes plus
1059 // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)
1060 // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.
1062 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1063 if ((DataSize
> MAX_HARDWARE_ERROR_VARIABLE_SIZE
) ||
1064 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> MAX_HARDWARE_ERROR_VARIABLE_SIZE
)) {
1065 return EFI_INVALID_PARAMETER
;
1069 // The size of the VariableName, including the Unicode Null in bytes plus
1070 // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.
1072 if ((DataSize
> MAX_VARIABLE_SIZE
) ||
1073 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> MAX_VARIABLE_SIZE
)) {
1074 return EFI_INVALID_PARAMETER
;
1078 // Check whether the input variable is already existed
1081 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1082 if (Status
== EFI_SUCCESS
&& Variable
.CurrPtr
!= NULL
) {
1084 // Update/Delete existing variable
1086 Volatile
= Variable
.Volatile
;
1088 if (EfiAtRuntime ()) {
1090 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1091 // the volatile is ReadOnly, and SetVariable should be aborted and
1092 // return EFI_WRITE_PROTECTED.
1094 if (Variable
.Volatile
) {
1095 Status
= EFI_WRITE_PROTECTED
;
1099 // Only variable have NV attribute can be updated/deleted in Runtime
1101 if (!(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)) {
1102 Status
= EFI_INVALID_PARAMETER
;
1107 // Setting a data variable with no access, or zero DataSize attributes
1108 // specified causes it to be deleted.
1110 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1111 State
= Variable
.CurrPtr
->State
;
1112 State
&= VAR_DELETED
;
1114 Status
= UpdateVariableStore (
1115 &mVariableModuleGlobal
->VariableGlobal
,
1119 (UINTN
) &Variable
.CurrPtr
->State
,
1123 if (!EFI_ERROR (Status
)) {
1124 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1125 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1130 // If the variable is marked valid and the same data has been passed in
1131 // then return to the caller immediately.
1133 if (Variable
.CurrPtr
->DataSize
== DataSize
&&
1134 (CompareMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), DataSize
) == 0)) {
1136 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1137 Status
= EFI_SUCCESS
;
1139 } else if ((Variable
.CurrPtr
->State
== VAR_ADDED
) ||
1140 (Variable
.CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1142 // Mark the old variable as in delete transition
1144 State
= Variable
.CurrPtr
->State
;
1145 State
&= VAR_IN_DELETED_TRANSITION
;
1147 Status
= UpdateVariableStore (
1148 &mVariableModuleGlobal
->VariableGlobal
,
1152 (UINTN
) &Variable
.CurrPtr
->State
,
1156 if (EFI_ERROR (Status
)) {
1160 } else if (Status
== EFI_NOT_FOUND
) {
1162 // Create a new variable
1166 // Make sure we are trying to create a new variable.
1167 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1169 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1170 Status
= EFI_NOT_FOUND
;
1175 // Only variable have NV|RT attribute can be created in Runtime
1177 if (EfiAtRuntime () &&
1178 (!(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) || !(Attributes
& EFI_VARIABLE_NON_VOLATILE
))) {
1179 Status
= EFI_INVALID_PARAMETER
;
1184 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
1186 ASSERT (Status
== EFI_INVALID_PARAMETER
);
1191 // Function part - create a new variable and copy the data.
1192 // Both update a variable and create a variable will come here.
1194 // Tricky part: Use scratch data area at the end of volatile variable store
1195 // as a temporary storage.
1197 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1199 SetMem (NextVariable
, SCRATCH_SIZE
, 0xff);
1201 NextVariable
->StartId
= VARIABLE_DATA
;
1202 NextVariable
->Attributes
= Attributes
;
1204 // NextVariable->State = VAR_ADDED;
1206 NextVariable
->Reserved
= 0;
1207 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1208 VarNameSize
= StrSize (VariableName
);
1210 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1214 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1216 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1220 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1222 // There will be pad bytes after Data, the NextVariable->NameSize and
1223 // NextVariable->DataSize should not include pad size so that variable
1224 // service can get actual size in GetVariable
1226 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1227 NextVariable
->DataSize
= (UINT32
)DataSize
;
1230 // The actual size of the variable that stores in storage should
1231 // include pad size.
1233 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1234 if (Attributes
& EFI_VARIABLE_NON_VOLATILE
) {
1236 // Create a nonvolatile variable
1240 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1241 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
1243 if (EfiAtRuntime ()) {
1244 Status
= EFI_OUT_OF_RESOURCES
;
1248 // Perform garbage collection & reclaim operation
1250 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
, NonVolatileOffset
, FALSE
);
1251 if (EFI_ERROR (Status
)) {
1255 // If still no enough space, return out of resources
1257 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1258 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
1260 Status
= EFI_OUT_OF_RESOURCES
;
1268 // 1. Write variable header
1269 // 2. Write variable data
1270 // 3. Set variable state to valid
1275 Status
= UpdateVariableStore (
1276 &mVariableModuleGlobal
->VariableGlobal
,
1281 sizeof (VARIABLE_HEADER
),
1282 (UINT8
*) NextVariable
1285 if (EFI_ERROR (Status
)) {
1291 Status
= UpdateVariableStore (
1292 &mVariableModuleGlobal
->VariableGlobal
,
1296 *NonVolatileOffset
+ sizeof (VARIABLE_HEADER
),
1297 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1298 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1301 if (EFI_ERROR (Status
)) {
1307 NextVariable
->State
= VAR_ADDED
;
1308 Status
= UpdateVariableStore (
1309 &mVariableModuleGlobal
->VariableGlobal
,
1314 sizeof (VARIABLE_HEADER
),
1315 (UINT8
*) NextVariable
1318 if (EFI_ERROR (Status
)) {
1322 *NonVolatileOffset
= *NonVolatileOffset
+ VarSize
;
1326 // Create a volatile variable
1330 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1331 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1333 // Perform garbage collection & reclaim operation
1335 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
, VolatileOffset
, TRUE
);
1336 if (EFI_ERROR (Status
)) {
1340 // If still no enough space, return out of resources
1342 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1343 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1345 Status
= EFI_OUT_OF_RESOURCES
;
1352 NextVariable
->State
= VAR_ADDED
;
1353 Status
= UpdateVariableStore (
1354 &mVariableModuleGlobal
->VariableGlobal
,
1360 (UINT8
*) NextVariable
1363 if (EFI_ERROR (Status
)) {
1367 *VolatileOffset
= *VolatileOffset
+ VarSize
;
1370 // Mark the old variable as deleted
1372 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
.CurrPtr
!= NULL
) {
1373 State
= Variable
.CurrPtr
->State
;
1374 State
&= VAR_DELETED
;
1376 Status
= UpdateVariableStore (
1377 &mVariableModuleGlobal
->VariableGlobal
,
1381 (UINTN
) &Variable
.CurrPtr
->State
,
1386 if (!EFI_ERROR (Status
)) {
1387 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1388 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1393 Status
= EFI_SUCCESS
;
1394 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1395 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1398 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1405 Routine Description:
1407 This code returns information about the EFI variables.
1411 Attributes Attributes bitmask to specify the type of variables
1412 on which to return information.
1413 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1414 for the EFI variables associated with the attributes specified.
1415 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1416 for EFI variables associated with the attributes specified.
1417 MaximumVariableSize Pointer to the maximum size of an individual EFI variables
1418 associated with the attributes specified.
1419 Global Pointer to VARIABLE_GLOBAL structure.
1420 Instance Instance of the Firmware Volume.
1425 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
1426 EFI_SUCCESS - Query successfully.
1427 EFI_UNSUPPORTED - The attribute is not supported on this platform.
1432 RuntimeServiceQueryVariableInfo (
1433 IN UINT32 Attributes
,
1434 OUT UINT64
*MaximumVariableStorageSize
,
1435 OUT UINT64
*RemainingVariableStorageSize
,
1436 OUT UINT64
*MaximumVariableSize
1439 VARIABLE_HEADER
*Variable
;
1440 VARIABLE_HEADER
*NextVariable
;
1441 UINT64 VariableSize
;
1442 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1444 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1445 return EFI_INVALID_PARAMETER
;
1448 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1450 // Make sure the Attributes combination is supported by the platform.
1452 return EFI_UNSUPPORTED
;
1453 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1455 // Make sure if runtime bit is set, boot service bit is set also.
1457 return EFI_INVALID_PARAMETER
;
1458 } else if (EfiAtRuntime () && !(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
1460 // Make sure RT Attribute is set if we are in Runtime phase.
1462 return EFI_INVALID_PARAMETER
;
1465 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1467 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1469 // Query is Volatile related.
1471 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
1474 // Query is Non-Volatile related.
1476 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1480 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1481 // with the storage size (excluding the storage header size).
1483 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1484 *RemainingVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1487 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.
1489 *MaximumVariableSize
= MAX_VARIABLE_SIZE
- sizeof (VARIABLE_HEADER
);
1492 // Harware error record variable needs larger size.
1494 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1495 *MaximumVariableSize
= MAX_HARDWARE_ERROR_VARIABLE_SIZE
- sizeof (VARIABLE_HEADER
);
1499 // Point to the starting address of the variables.
1501 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
1504 // Now walk through the related variable store.
1506 while (IsValidVariableHeader (Variable
) && (Variable
< GetEndPointer (VariableStoreHeader
))) {
1507 NextVariable
= GetNextVariablePtr (Variable
);
1508 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1510 if (EfiAtRuntime ()) {
1512 // we don't take the state of the variables in mind
1513 // when calculating RemainingVariableStorageSize,
1514 // since the space occupied by variables not marked with
1515 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1517 *RemainingVariableStorageSize
-= VariableSize
;
1520 // Only care about Variables with State VAR_ADDED,because
1521 // the space not marked as VAR_ADDED is reclaimable now.
1523 if (Variable
->State
== VAR_ADDED
) {
1524 *RemainingVariableStorageSize
-= VariableSize
;
1529 // Go to the next one
1531 Variable
= NextVariable
;
1534 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
1535 *MaximumVariableSize
= 0;
1536 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
1537 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
1540 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1545 VariableCommonInitialize (
1546 IN EFI_HANDLE ImageHandle
,
1547 IN EFI_SYSTEM_TABLE
*SystemTable
1551 Routine Description:
1552 This function does common initialization for variable services
1556 ImageHandle - The firmware allocated handle for the EFI image.
1557 SystemTable - A pointer to the EFI System Table.
1563 EFI_NOT_FOUND - Variable store area not found.
1564 EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.
1565 EFI_SUCCESS - Variable services successfully initialized.
1570 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1572 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
1573 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1574 VARIABLE_HEADER
*NextVariable
;
1576 EFI_PHYSICAL_ADDRESS FvVolHdr
;
1577 UINT64 TempVariableStoreHeader
;
1578 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
1583 UINT64 VariableStoreBase
;
1584 UINT64 VariableStoreLength
;
1587 EfiInitializeLock(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
1590 // Allocate memory for volatile variable store
1592 VolatileVariableStore
= AllocateRuntimePool (VARIABLE_STORE_SIZE
+ SCRATCH_SIZE
);
1593 if (VolatileVariableStore
== NULL
) {
1594 FreePool (mVariableModuleGlobal
);
1595 return EFI_OUT_OF_RESOURCES
;
1598 SetMem (VolatileVariableStore
, VARIABLE_STORE_SIZE
+ SCRATCH_SIZE
, 0xff);
1601 // Variable Specific Data
1603 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
1604 mVariableModuleGlobal
->VolatileLastVariableOffset
= sizeof (VARIABLE_STORE_HEADER
);
1606 VolatileVariableStore
->Signature
= VARIABLE_STORE_SIGNATURE
;
1607 VolatileVariableStore
->Size
= VARIABLE_STORE_SIZE
;
1608 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
1609 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
1610 VolatileVariableStore
->Reserved
= 0;
1611 VolatileVariableStore
->Reserved1
= 0;
1614 // Get non volatile varaible store
1617 TempVariableStoreHeader
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableBase
);
1618 VariableStoreBase
= TempVariableStoreHeader
+ \
1619 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1620 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
1621 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1623 // Mark the variable storage region of the FLASH as RUNTIME
1625 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
1626 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
1627 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
1629 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
1630 if (EFI_ERROR (Status
)) {
1631 FreePool (mVariableModuleGlobal
);
1632 FreePool (VolatileVariableStore
);
1633 return EFI_UNSUPPORTED
;
1636 Status
= gDS
->SetMemorySpaceAttributes (
1639 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
1641 if (EFI_ERROR (Status
)) {
1642 FreePool (mVariableModuleGlobal
);
1643 FreePool (VolatileVariableStore
);
1644 return EFI_UNSUPPORTED
;
1647 // Get address of non volatile variable store base
1649 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
1655 // Find the Correct Instance of the FV Block Service.
1658 CurrPtr
= (CHAR8
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1659 while (EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
) == EFI_SUCCESS
) {
1660 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
1661 if (CurrPtr
>= (CHAR8
*) FwVolHeader
&& CurrPtr
< (((CHAR8
*) FwVolHeader
) + FwVolHeader
->FvLength
)) {
1662 mVariableModuleGlobal
->FvbInstance
= Instance
;
1669 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) CurrPtr
;
1670 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
1671 if (~VariableStoreHeader
->Size
== 0) {
1672 Status
= UpdateVariableStore (
1673 &mVariableModuleGlobal
->VariableGlobal
,
1676 mVariableModuleGlobal
->FvbInstance
,
1677 (UINTN
) &VariableStoreHeader
->Size
,
1679 (UINT8
*) &VariableStoreLength
1682 // As Variables are stored in NV storage, which are slow devices,such as flash.
1683 // Variable operation may skip checking variable program result to improve performance,
1684 // We can assume Variable program is OK through some check point.
1685 // Variable Store Size Setting should be the first Variable write operation,
1686 // We can assume all Read/Write is OK if we can set Variable store size successfully.
1687 // If write fail, we will assert here
1689 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
1691 if (EFI_ERROR (Status
)) {
1696 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) CurrPtr
);
1698 // Parse non-volatile variable data and get last variable offset
1700 NextVariable
= (VARIABLE_HEADER
*) (CurrPtr
+ sizeof (VARIABLE_STORE_HEADER
));
1701 Status
= EFI_SUCCESS
;
1703 while (IsValidVariableHeader (NextVariable
)) {
1704 NextVariable
= GetNextVariablePtr (NextVariable
);
1707 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) CurrPtr
;
1710 // Check if the free area is blow a threshold
1712 if ((((VARIABLE_STORE_HEADER
*)((UINTN
) CurrPtr
))->Size
- mVariableModuleGlobal
->NonVolatileLastVariableOffset
) < VARIABLE_RECLAIM_THRESHOLD
) {
1714 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1715 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1720 if (EFI_ERROR (Status
)) {
1721 FreePool (mVariableModuleGlobal
);
1722 FreePool (VolatileVariableStore
);
1727 // Check if the free area is really free.
1729 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
1730 Data
= ((UINT8
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)[Index
];
1733 // There must be something wrong in variable store, do reclaim operation.
1736 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1737 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1745 if (EFI_ERROR (Status
)) {
1746 FreePool (mVariableModuleGlobal
);
1747 FreePool (VolatileVariableStore
);
1758 VariableClassAddressChangeEvent (
1765 (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
1769 (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
1771 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
1776 Variable Driver main entry point. The Variable driver places the 4 EFI
1777 runtime services in the EFI System Table and installs arch protocols
1778 for variable read and write services being availible.
1780 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1781 @param[in] SystemTable A pointer to the EFI System Table.
1783 @retval EFI_SUCCESS The entry point is executed successfully.
1784 @retval other Some error occurs when executing this entry point.
1789 VariableServiceInitialize (
1790 IN EFI_HANDLE ImageHandle
,
1791 IN EFI_SYSTEM_TABLE
*SystemTable
1796 Status
= VariableCommonInitialize (ImageHandle
, SystemTable
);
1797 ASSERT_EFI_ERROR (Status
);
1799 SystemTable
->RuntimeServices
->GetVariable
= RuntimeServiceGetVariable
;
1800 SystemTable
->RuntimeServices
->GetNextVariableName
= RuntimeServiceGetNextVariableName
;
1801 SystemTable
->RuntimeServices
->SetVariable
= RuntimeServiceSetVariable
;
1802 SystemTable
->RuntimeServices
->QueryVariableInfo
= RuntimeServiceQueryVariableInfo
;
1805 // Now install the Variable Runtime Architectural Protocol on a new handle
1807 Status
= gBS
->InstallMultipleProtocolInterfaces (
1809 &gEfiVariableArchProtocolGuid
, NULL
,
1810 &gEfiVariableWriteArchProtocolGuid
, NULL
,
1813 ASSERT_EFI_ERROR (Status
);
1815 Status
= gBS
->CreateEvent (
1816 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
,
1818 VariableClassAddressChangeEvent
,
1820 &mVirtualAddressChangeEvent
1822 ASSERT_EFI_ERROR (Status
);