3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
24 #include <Guid/FlashMapHob.h>
25 #include <Guid/VariableInfo.h>
26 #include <Guid/GlobalVariable.h>
32 // Don't use module globals after the SetVirtualAddress map is signaled
34 ESAL_VARIABLE_GLOBAL
*mVariableModuleGlobal
;
38 // This is a temperary function which will be removed
39 // when EfiAcquireLock in UefiLib can handle the
40 // the call in UEFI Runtimer driver in RT phase.
43 AcquireLockOnlyAtBootTime (
47 if (!EfiAtRuntime ()) {
48 EfiAcquireLock (Lock
);
53 // This is a temperary function which will be removed
54 // when EfiAcquireLock in UefiLib can handle the
55 // the call in UEFI Runtimer driver in RT phase.
58 ReleaseLockOnlyAtBootTime (
62 if (!EfiAtRuntime ()) {
63 EfiReleaseLock (Lock
);
68 GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
73 IN CHAR16
*VariableName
,
74 IN EFI_GUID
*VendorGuid
,
82 VARIABLE_INFO_ENTRY
*Entry
;
84 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
86 if (EfiAtRuntime ()) {
87 // Don't collect statistics at runtime
91 if (gVariableInfo
== NULL
) {
92 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
93 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
94 gVariableInfo
->Name
= AllocatePool (StrLen (VariableName
));
95 StrCpy (gVariableInfo
->Name
, VariableName
);
96 gVariableInfo
->Volatile
= Volatile
;
98 gBS
->InstallConfigurationTable (&gEfiVariableInfoGuid
, gVariableInfo
);
102 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
103 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
104 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
112 Entry
->DeleteCount
++;
122 if (Entry
->Next
== NULL
) {
123 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
125 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
126 Entry
->Next
->Name
= AllocatePool (StrLen (VariableName
));
127 StrCpy (Entry
->Next
->Name
, VariableName
);
128 Entry
->Next
->Volatile
= Volatile
;
140 IsValidVariableHeader (
141 IN VARIABLE_HEADER
*Variable
147 This code checks if variable header is valid or not.
150 Variable Pointer to the Variable Header.
153 TRUE Variable header is valid.
154 FALSE Variable header is not valid.
158 if (Variable
== NULL
||
159 Variable
->StartId
!= VARIABLE_DATA
||
160 (sizeof (VARIABLE_HEADER
) + Variable
->NameSize
+ Variable
->DataSize
) > MAX_VARIABLE_SIZE
171 UpdateVariableStore (
172 IN VARIABLE_GLOBAL
*Global
,
174 IN BOOLEAN SetByIndex
,
176 IN UINTN DataPtrIndex
,
184 This function writes data to the FWH at the correct LBA even if the LBAs
189 Global - Pointer to VARAIBLE_GLOBAL structure
190 Volatile - If the Variable is Volatile or Non-Volatile
191 SetByIndex - TRUE: Target pointer is given as index
192 FALSE: Target pointer is absolute
193 Instance - Instance of FV Block services
194 DataPtrIndex - Pointer to the Data from the end of VARIABLE_STORE_HEADER
196 DataSize - Size of data to be written.
197 Buffer - Pointer to the buffer from which data is written
201 EFI_INVALID_PARAMETER - Parameters not valid
202 EFI_SUCCESS - Variable store successfully updated
206 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
214 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
215 VARIABLE_STORE_HEADER
*VolatileBase
;
216 EFI_PHYSICAL_ADDRESS FvVolHdr
;
217 EFI_PHYSICAL_ADDRESS DataPtr
;
221 DataPtr
= DataPtrIndex
;
224 // Check if the Data is Volatile
227 EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
);
228 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
230 // Data Pointer should point to the actual Address where data is to be
234 DataPtr
+= Global
->NonVolatileVariableBase
;
237 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
238 return EFI_INVALID_PARAMETER
;
242 // Data Pointer should point to the actual Address where data is to be
245 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
);
247 DataPtr
+= Global
->VolatileVariableBase
;
250 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
251 return EFI_INVALID_PARAMETER
;
255 // If Volatile Variable just do a simple mem copy.
257 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
262 // If we are here we are dealing with Non-Volatile Variables
264 LinearOffset
= (UINTN
) FwVolHeader
;
265 CurrWritePtr
= (UINTN
) DataPtr
;
266 CurrWriteSize
= DataSize
;
270 if (CurrWritePtr
< LinearOffset
) {
271 return EFI_INVALID_PARAMETER
;
274 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
275 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
277 // Check to see if the Variable Writes are spanning through multiple
280 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
281 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
282 Status
= EfiFvbWriteBlock (
285 (UINTN
) (CurrWritePtr
- LinearOffset
),
291 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
292 Status
= EfiFvbWriteBlock (
295 (UINTN
) (CurrWritePtr
- LinearOffset
),
299 if (EFI_ERROR (Status
)) {
303 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
304 CurrBuffer
= CurrBuffer
+ Size
;
305 CurrWriteSize
= CurrWriteSize
- Size
;
309 LinearOffset
+= PtrBlockMapEntry
->Length
;
318 VARIABLE_STORE_STATUS
320 GetVariableStoreStatus (
321 IN VARIABLE_STORE_HEADER
*VarStoreHeader
327 This code gets the current status of Variable Store.
331 VarStoreHeader Pointer to the Variable Store Header.
335 EfiRaw Variable store status is raw
336 EfiValid Variable store status is valid
337 EfiInvalid Variable store status is invalid
341 if (VarStoreHeader
->Signature
== VARIABLE_STORE_SIGNATURE
&&
342 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
343 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
347 } else if (VarStoreHeader
->Signature
== 0xffffffff &&
348 VarStoreHeader
->Size
== 0xffffffff &&
349 VarStoreHeader
->Format
== 0xff &&
350 VarStoreHeader
->State
== 0xff
363 IN VARIABLE_HEADER
*Variable
369 This code gets the pointer to the variable data.
373 Variable Pointer to the Variable Header.
377 UINT8* Pointer to Variable Data
382 // Be careful about pad size for alignment
384 return (UINT8
*) ((UINTN
) GET_VARIABLE_NAME_PTR (Variable
) + Variable
->NameSize
+ GET_PAD_SIZE (Variable
->NameSize
));
390 IN VARIABLE_HEADER
*Variable
396 This code gets the pointer to the next variable header.
400 Variable Pointer to the Variable Header.
404 VARIABLE_HEADER* Pointer to next variable header.
408 if (!IsValidVariableHeader (Variable
)) {
412 // Be careful about pad size for alignment
414 return (VARIABLE_HEADER
*) ((UINTN
) GetVariableDataPtr (Variable
) + Variable
->DataSize
+ GET_PAD_SIZE (Variable
->DataSize
));
421 IN VARIABLE_STORE_HEADER
*VarStoreHeader
427 This code gets the pointer to the last variable memory pointer byte
431 VarStoreHeader Pointer to the Variable Store Header.
435 VARIABLE_HEADER* Pointer to last unavailable Variable Header
440 // The end of variable store
442 return (VARIABLE_HEADER
*) ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
449 IN EFI_PHYSICAL_ADDRESS VariableBase
,
450 OUT UINTN
*LastVariableOffset
,
451 IN BOOLEAN IsVolatile
457 Variable store garbage collection and reclaim operation
461 VariableBase Base address of variable store
462 LastVariableOffset Offset of last variable
463 IsVolatile The variable store is volatile or not,
464 if it is non-volatile, need FTW
472 VARIABLE_HEADER
*Variable
;
473 VARIABLE_HEADER
*NextVariable
;
474 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
476 UINTN ValidBufferSize
;
481 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
484 // Start Pointers for the variable.
486 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
488 ValidBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
490 while (IsValidVariableHeader (Variable
)) {
491 NextVariable
= GetNextVariablePtr (Variable
);
492 if (Variable
->State
== VAR_ADDED
) {
493 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
494 ValidBufferSize
+= VariableSize
;
497 Variable
= NextVariable
;
500 ValidBuffer
= AllocatePool (ValidBufferSize
);
501 if (ValidBuffer
== NULL
) {
502 return EFI_OUT_OF_RESOURCES
;
505 SetMem (ValidBuffer
, ValidBufferSize
, 0xff);
507 CurrPtr
= ValidBuffer
;
510 // Copy variable store header
512 CopyMem (CurrPtr
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
513 CurrPtr
+= sizeof (VARIABLE_STORE_HEADER
);
516 // Start Pointers for the variable.
518 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
520 while (IsValidVariableHeader (Variable
)) {
521 NextVariable
= GetNextVariablePtr (Variable
);
522 if (Variable
->State
== VAR_ADDED
) {
523 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
524 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
525 CurrPtr
+= VariableSize
;
528 Variable
= NextVariable
;
533 // If volatile variable store, just copy valid buffer
535 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
536 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, ValidBufferSize
);
537 *LastVariableOffset
= ValidBufferSize
;
538 Status
= EFI_SUCCESS
;
541 // If non-volatile variable store, perform FTW here.
543 Status
= FtwVariableSpace (
548 if (!EFI_ERROR (Status
)) {
549 *LastVariableOffset
= ValidBufferSize
;
553 FreePool (ValidBuffer
);
555 if (EFI_ERROR (Status
)) {
556 *LastVariableOffset
= 0;
568 } VARIABLE_CACHE_ENTRY
;
571 // The current Hii implementation accesses this variable a larg # of times on every boot.
572 // Other common variables are only accessed a single time. This is why this cache algorithm
573 // only targets a single variable. Probably to get an performance improvement out of
574 // a Cache you would need a cache that improves the search performance for a variable.
576 VARIABLE_CACHE_ENTRY mVariableCache
[] = {
578 &gEfiGlobalVariableGuid
,
587 UpdateVariableCache (
588 IN CHAR16
*VariableName
,
589 IN EFI_GUID
*VendorGuid
,
590 OUT UINT32 Attributes
,
591 IN OUT UINTN DataSize
,
595 VARIABLE_CACHE_ENTRY
*Entry
;
598 if (EfiAtRuntime ()) {
599 // Don't use the cache at runtime
603 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
604 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
605 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
606 Entry
->Attributes
= Attributes
;
609 if (Entry
->DataSize
!= 0) {
610 FreePool (Entry
->Data
);
612 Entry
->DataSize
= DataSize
;
613 } else if (DataSize
== Entry
->DataSize
) {
614 CopyMem (Entry
->Data
, Data
, DataSize
);
616 Entry
->Data
= AllocatePool (DataSize
);
617 Entry
->DataSize
= DataSize
;
618 CopyMem (Entry
->Data
, Data
, DataSize
);
627 FindVariableInCache (
628 IN CHAR16
*VariableName
,
629 IN EFI_GUID
*VendorGuid
,
630 OUT UINT32
*Attributes OPTIONAL
,
631 IN OUT UINTN
*DataSize
,
635 VARIABLE_CACHE_ENTRY
*Entry
;
638 if (EfiAtRuntime ()) {
639 // Don't use the cache at runtime
640 return EFI_NOT_FOUND
;
643 for (Index
= 0, Entry
= mVariableCache
; Index
< sizeof (mVariableCache
)/sizeof (VARIABLE_CACHE_ENTRY
); Index
++, Entry
++) {
644 if (CompareGuid (VendorGuid
, Entry
->Guid
)) {
645 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
646 if (Entry
->DataSize
== 0) {
647 return EFI_NOT_FOUND
;
648 } else if (Entry
->DataSize
!= *DataSize
) {
649 *DataSize
= Entry
->DataSize
;
650 return EFI_BUFFER_TOO_SMALL
;
652 CopyMem (Data
, Entry
->Data
, Entry
->DataSize
);
653 if (Attributes
!= NULL
) {
654 *Attributes
= Entry
->Attributes
;
662 return EFI_NOT_FOUND
;
668 IN CHAR16
*VariableName
,
669 IN EFI_GUID
*VendorGuid
,
670 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
671 IN VARIABLE_GLOBAL
*Global
677 This code finds variable in storage blocks (Volatile or Non-Volatile)
681 VariableName Name of the variable to be found
682 VendorGuid Vendor GUID to be found.
683 PtrTrack Variable Track Pointer structure that contains
684 Variable Information.
685 Contains the pointer of Variable header.
686 Global VARIABLE_GLOBAL pointer
694 VARIABLE_HEADER
*Variable
[2];
695 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
699 // We aquire the lock at the entry of FindVariable as GetVariable, GetNextVariableName
700 // SetVariable all call FindVariable at entry point. Please move "Aquire Lock" to
701 // the correct places if this assumption does not hold TRUE anymore.
703 AcquireLockOnlyAtBootTime(&Global
->VariableServicesLock
);
706 // 0: Volatile, 1: Non-Volatile
708 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
);
709 VariableStoreHeader
[1] = (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->NonVolatileVariableBase
);
712 // Start Pointers for the variable.
713 // Actual Data Pointer where data can be written.
715 Variable
[0] = (VARIABLE_HEADER
*) (VariableStoreHeader
[0] + 1);
716 Variable
[1] = (VARIABLE_HEADER
*) (VariableStoreHeader
[1] + 1);
718 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
719 return EFI_INVALID_PARAMETER
;
722 // Find the variable by walk through volatile and then non-volatile variable store
724 for (Index
= 0; Index
< 2; Index
++) {
725 PtrTrack
->StartPtr
= (VARIABLE_HEADER
*) (VariableStoreHeader
[Index
] + 1);
726 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
728 while (IsValidVariableHeader (Variable
[Index
]) && (Variable
[Index
] <= GetEndPointer (VariableStoreHeader
[Index
]))) {
729 if (Variable
[Index
]->State
== VAR_ADDED
) {
730 if (!EfiAtRuntime () || (Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
731 if (VariableName
[0] == 0) {
732 PtrTrack
->CurrPtr
= Variable
[Index
];
733 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
736 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
737 if (!CompareMem (VariableName
, GET_VARIABLE_NAME_PTR (Variable
[Index
]), Variable
[Index
]->NameSize
)) {
738 PtrTrack
->CurrPtr
= Variable
[Index
];
739 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
747 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
750 PtrTrack
->CurrPtr
= NULL
;
751 return EFI_NOT_FOUND
;
758 IN CHAR16
*VariableName
,
759 IN EFI_GUID
*VendorGuid
,
760 OUT UINT32
*Attributes OPTIONAL
,
761 IN OUT UINTN
*DataSize
,
763 IN VARIABLE_GLOBAL
*Global
,
770 This code finds variable in storage blocks (Volatile or Non-Volatile)
774 VariableName Name of Variable to be found
775 VendorGuid Variable vendor GUID
776 Attributes OPTIONAL Attribute value of the variable found
777 DataSize Size of Data found. If size is less than the
778 data, this value contains the required size.
780 Global Pointer to VARIABLE_GLOBAL structure
781 Instance Instance of the Firmware Volume.
785 EFI_INVALID_PARAMETER - Invalid parameter
786 EFI_SUCCESS - Find the specified variable
787 EFI_NOT_FOUND - Not found
788 EFI_BUFFER_TO_SMALL - DataSize is too small for the result
793 VARIABLE_POINTER_TRACK Variable
;
797 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
798 return EFI_INVALID_PARAMETER
;
802 // Find existing variable
804 Status
= FindVariableInCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
805 if ((Status
== EFI_BUFFER_TOO_SMALL
) || (Status
== EFI_SUCCESS
)){
807 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
);
811 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, Global
);
812 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
819 VarDataSize
= Variable
.CurrPtr
->DataSize
;
820 if (*DataSize
>= VarDataSize
) {
822 Status
= EFI_INVALID_PARAMETER
;
826 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
827 if (Attributes
!= NULL
) {
828 *Attributes
= Variable
.CurrPtr
->Attributes
;
831 *DataSize
= VarDataSize
;
832 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
833 UpdateVariableCache (VariableName
, VendorGuid
, Variable
.CurrPtr
->Attributes
, VarDataSize
, Data
);
835 Status
= EFI_SUCCESS
;
838 *DataSize
= VarDataSize
;
839 Status
= EFI_BUFFER_TOO_SMALL
;
844 ReleaseLockOnlyAtBootTime (&Global
->VariableServicesLock
);
850 GetNextVariableName (
851 IN OUT UINTN
*VariableNameSize
,
852 IN OUT CHAR16
*VariableName
,
853 IN OUT EFI_GUID
*VendorGuid
,
854 IN VARIABLE_GLOBAL
*Global
,
861 This code Finds the Next available variable
865 VariableNameSize Size of the variable
866 VariableName Pointer to variable name
867 VendorGuid Variable Vendor Guid
868 Global VARIABLE_GLOBAL structure pointer.
877 VARIABLE_POINTER_TRACK Variable
;
881 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
882 return EFI_INVALID_PARAMETER
;
885 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, Global
);
887 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
891 if (VariableName
[0] != 0) {
893 // If variable name is not NULL, get next variable
895 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
900 // If both volatile and non-volatile variable store are parsed,
903 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
904 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
905 if (Variable
.Volatile
) {
906 Variable
.StartPtr
= (VARIABLE_HEADER
*) ((UINTN
) (Global
->VolatileVariableBase
+ sizeof (VARIABLE_STORE_HEADER
)));
907 Variable
.EndPtr
= (VARIABLE_HEADER
*) GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
));
909 Status
= EFI_NOT_FOUND
;
913 Variable
.CurrPtr
= Variable
.StartPtr
;
914 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
921 if (IsValidVariableHeader (Variable
.CurrPtr
) && Variable
.CurrPtr
->State
== VAR_ADDED
) {
922 if (!(EfiAtRuntime () && !(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
))) {
923 VarNameSize
= Variable
.CurrPtr
->NameSize
;
924 if (VarNameSize
<= *VariableNameSize
) {
927 GET_VARIABLE_NAME_PTR (Variable
.CurrPtr
),
932 &Variable
.CurrPtr
->VendorGuid
,
935 Status
= EFI_SUCCESS
;
937 Status
= EFI_BUFFER_TOO_SMALL
;
940 *VariableNameSize
= VarNameSize
;
945 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
949 ReleaseLockOnlyAtBootTime (&Global
->VariableServicesLock
);
956 IN CHAR16
*VariableName
,
957 IN EFI_GUID
*VendorGuid
,
958 IN UINT32 Attributes
,
961 IN VARIABLE_GLOBAL
*Global
,
962 IN UINTN
*VolatileOffset
,
963 IN UINTN
*NonVolatileOffset
,
970 This code sets variable in storage blocks (Volatile or Non-Volatile)
974 VariableName Name of Variable to be found
975 VendorGuid Variable vendor GUID
976 Attributes Attribute value of the variable found
977 DataSize Size of Data found. If size is less than the
978 data, this value contains the required size.
980 Global Pointer to VARIABLE_GLOBAL structure
981 VolatileOffset The offset of last volatile variable
982 NonVolatileOffset The offset of last non-volatile variable
983 Instance Instance of the Firmware Volume.
987 EFI_INVALID_PARAMETER - Invalid parameter
988 EFI_SUCCESS - Set successfully
989 EFI_OUT_OF_RESOURCES - Resource not enough to set variable
990 EFI_NOT_FOUND - Not found
991 EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure
992 EFI_WRITE_PROTECTED - Variable is read-only
996 VARIABLE_POINTER_TRACK Variable
;
998 VARIABLE_HEADER
*NextVariable
;
1000 UINTN VarNameOffset
;
1001 UINTN VarDataOffset
;
1009 // Check input parameters
1011 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1012 return EFI_INVALID_PARAMETER
;
1015 // Make sure if runtime bit is set, boot service bit is set also
1017 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1018 return EFI_INVALID_PARAMETER
;
1021 // The size of the VariableName, including the Unicode Null in bytes plus
1022 // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)
1023 // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.
1025 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1026 if ((DataSize
> MAX_HARDWARE_ERROR_VARIABLE_SIZE
) ||
1027 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> MAX_HARDWARE_ERROR_VARIABLE_SIZE
)) {
1028 return EFI_INVALID_PARAMETER
;
1032 // The size of the VariableName, including the Unicode Null in bytes plus
1033 // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.
1035 if ((DataSize
> MAX_VARIABLE_SIZE
) ||
1036 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> MAX_VARIABLE_SIZE
)) {
1037 return EFI_INVALID_PARAMETER
;
1041 // Check whether the input variable is already existed
1044 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, Global
);
1046 if (Status
== EFI_SUCCESS
&& Variable
.CurrPtr
!= NULL
) {
1048 // Update/Delete existing variable
1051 if (EfiAtRuntime ()) {
1053 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1054 // the volatile is ReadOnly, and SetVariable should be aborted and
1055 // return EFI_WRITE_PROTECTED.
1057 if (Variable
.Volatile
) {
1058 Status
= EFI_WRITE_PROTECTED
;
1062 // Only variable have NV attribute can be updated/deleted in Runtime
1064 if (!(Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)) {
1065 Status
= EFI_INVALID_PARAMETER
;
1070 // Setting a data variable with no access, or zero DataSize attributes
1071 // specified causes it to be deleted.
1073 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1074 State
= Variable
.CurrPtr
->State
;
1075 State
&= VAR_DELETED
;
1077 Status
= UpdateVariableStore (
1082 (UINTN
) &Variable
.CurrPtr
->State
,
1086 if (!EFI_ERROR (Status
)) {
1087 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1088 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1093 // If the variable is marked valid and the same data has been passed in
1094 // then return to the caller immediately.
1096 if (Variable
.CurrPtr
->DataSize
== DataSize
&&
1097 (CompareMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), DataSize
) == 0)) {
1099 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1100 Status
= EFI_SUCCESS
;
1102 } else if ((Variable
.CurrPtr
->State
== VAR_ADDED
) ||
1103 (Variable
.CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1105 // Mark the old variable as in delete transition
1107 State
= Variable
.CurrPtr
->State
;
1108 State
&= VAR_IN_DELETED_TRANSITION
;
1110 Status
= UpdateVariableStore (
1115 (UINTN
) &Variable
.CurrPtr
->State
,
1119 if (EFI_ERROR (Status
)) {
1123 } else if (Status
== EFI_NOT_FOUND
) {
1125 // Create a new variable
1129 // Make sure we are trying to create a new variable.
1130 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1132 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1133 Status
= EFI_NOT_FOUND
;
1138 // Only variable have NV|RT attribute can be created in Runtime
1140 if (EfiAtRuntime () &&
1141 (!(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) || !(Attributes
& EFI_VARIABLE_NON_VOLATILE
))) {
1142 Status
= EFI_INVALID_PARAMETER
;
1147 // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
1149 ASSERT (Status
== EFI_INVALID_PARAMETER
);
1154 // Function part - create a new variable and copy the data.
1155 // Both update a variable and create a variable will come here.
1157 // Tricky part: Use scratch data area at the end of volatile variable store
1158 // as a temporary storage.
1160 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
));
1162 SetMem (NextVariable
, SCRATCH_SIZE
, 0xff);
1164 NextVariable
->StartId
= VARIABLE_DATA
;
1165 NextVariable
->Attributes
= Attributes
;
1167 // NextVariable->State = VAR_ADDED;
1169 NextVariable
->Reserved
= 0;
1170 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1171 VarNameSize
= StrSize (VariableName
);
1173 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1177 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1179 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1183 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1185 // There will be pad bytes after Data, the NextVariable->NameSize and
1186 // NextVariable->DataSize should not include pad size so that variable
1187 // service can get actual size in GetVariable
1189 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1190 NextVariable
->DataSize
= (UINT32
)DataSize
;
1193 // The actual size of the variable that stores in storage should
1194 // include pad size.
1196 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1197 if (Attributes
& EFI_VARIABLE_NON_VOLATILE
) {
1199 // Create a nonvolatile variable
1201 Variable
.Volatile
= FALSE
;
1203 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1204 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (Global
->NonVolatileVariableBase
)))->Size
1206 if (EfiAtRuntime ()) {
1207 Status
= EFI_OUT_OF_RESOURCES
;
1211 // Perform garbage collection & reclaim operation
1213 Status
= Reclaim (Global
->NonVolatileVariableBase
, NonVolatileOffset
, FALSE
);
1214 if (EFI_ERROR (Status
)) {
1218 // If still no enough space, return out of resources
1220 if ((UINT32
) (VarSize
+*NonVolatileOffset
) >
1221 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (Global
->NonVolatileVariableBase
)))->Size
1223 Status
= EFI_OUT_OF_RESOURCES
;
1231 // 1. Write variable header
1232 // 2. Write variable data
1233 // 3. Set variable state to valid
1238 Status
= UpdateVariableStore (
1244 sizeof (VARIABLE_HEADER
),
1245 (UINT8
*) NextVariable
1248 if (EFI_ERROR (Status
)) {
1254 Status
= UpdateVariableStore (
1259 *NonVolatileOffset
+ sizeof (VARIABLE_HEADER
),
1260 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1261 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1264 if (EFI_ERROR (Status
)) {
1270 NextVariable
->State
= VAR_ADDED
;
1271 Status
= UpdateVariableStore (
1277 sizeof (VARIABLE_HEADER
),
1278 (UINT8
*) NextVariable
1281 if (EFI_ERROR (Status
)) {
1285 *NonVolatileOffset
= *NonVolatileOffset
+ VarSize
;
1289 // Create a volatile variable
1291 Variable
.Volatile
= TRUE
;
1293 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1294 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (Global
->VolatileVariableBase
)))->Size
) {
1296 // Perform garbage collection & reclaim operation
1298 Status
= Reclaim (Global
->VolatileVariableBase
, VolatileOffset
, TRUE
);
1299 if (EFI_ERROR (Status
)) {
1303 // If still no enough space, return out of resources
1305 if ((UINT32
) (VarSize
+*VolatileOffset
) >
1306 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (Global
->VolatileVariableBase
)))->Size
1308 Status
= EFI_OUT_OF_RESOURCES
;
1315 NextVariable
->State
= VAR_ADDED
;
1316 Status
= UpdateVariableStore (
1323 (UINT8
*) NextVariable
1326 if (EFI_ERROR (Status
)) {
1330 *VolatileOffset
= *VolatileOffset
+ VarSize
;
1333 // Mark the old variable as deleted
1335 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
.CurrPtr
!= NULL
) {
1336 State
= Variable
.CurrPtr
->State
;
1337 State
&= VAR_DELETED
;
1339 Status
= UpdateVariableStore (
1344 (UINTN
) &Variable
.CurrPtr
->State
,
1349 if (!EFI_ERROR (Status
)) {
1350 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1351 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1356 Status
= EFI_SUCCESS
;
1357 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1358 UpdateVariableCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
1361 ReleaseLockOnlyAtBootTime (&Global
->VariableServicesLock
);
1368 IN UINT32 Attributes
,
1369 OUT UINT64
*MaximumVariableStorageSize
,
1370 OUT UINT64
*RemainingVariableStorageSize
,
1371 OUT UINT64
*MaximumVariableSize
,
1372 IN VARIABLE_GLOBAL
*Global
,
1377 Routine Description:
1379 This code returns information about the EFI variables.
1383 Attributes Attributes bitmask to specify the type of variables
1384 on which to return information.
1385 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1386 for the EFI variables associated with the attributes specified.
1387 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1388 for EFI variables associated with the attributes specified.
1389 MaximumVariableSize Pointer to the maximum size of an individual EFI variables
1390 associated with the attributes specified.
1391 Global Pointer to VARIABLE_GLOBAL structure.
1392 Instance Instance of the Firmware Volume.
1397 EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.
1398 EFI_SUCCESS - Query successfully.
1399 EFI_UNSUPPORTED - The attribute is not supported on this platform.
1403 VARIABLE_HEADER
*Variable
;
1404 VARIABLE_HEADER
*NextVariable
;
1405 UINT64 VariableSize
;
1406 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1408 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1409 return EFI_INVALID_PARAMETER
;
1412 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
1414 // Make sure the Attributes combination is supported by the platform.
1416 return EFI_UNSUPPORTED
;
1417 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
1419 // Make sure if runtime bit is set, boot service bit is set also.
1421 return EFI_INVALID_PARAMETER
;
1422 } else if (EfiAtRuntime () && !(Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)) {
1424 // Make sure RT Attribute is set if we are in Runtime phase.
1426 return EFI_INVALID_PARAMETER
;
1429 AcquireLockOnlyAtBootTime(&Global
->VariableServicesLock
);
1431 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1433 // Query is Volatile related.
1435 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->VolatileVariableBase
);
1438 // Query is Non-Volatile related.
1440 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) Global
->NonVolatileVariableBase
);
1444 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1445 // with the storage size (excluding the storage header size).
1447 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1448 *RemainingVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
1451 // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.
1453 *MaximumVariableSize
= MAX_VARIABLE_SIZE
- sizeof (VARIABLE_HEADER
);
1456 // Harware error record variable needs larger size.
1458 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1459 *MaximumVariableSize
= MAX_HARDWARE_ERROR_VARIABLE_SIZE
- sizeof (VARIABLE_HEADER
);
1463 // Point to the starting address of the variables.
1465 Variable
= (VARIABLE_HEADER
*) (VariableStoreHeader
+ 1);
1468 // Now walk through the related variable store.
1470 while (IsValidVariableHeader (Variable
) && (Variable
< GetEndPointer (VariableStoreHeader
))) {
1471 NextVariable
= GetNextVariablePtr (Variable
);
1472 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
1474 if (EfiAtRuntime ()) {
1476 // we don't take the state of the variables in mind
1477 // when calculating RemainingVariableStorageSize,
1478 // since the space occupied by variables not marked with
1479 // VAR_ADDED is not allowed to be reclaimed in Runtime.
1481 *RemainingVariableStorageSize
-= VariableSize
;
1484 // Only care about Variables with State VAR_ADDED,because
1485 // the space not marked as VAR_ADDED is reclaimable now.
1487 if (Variable
->State
== VAR_ADDED
) {
1488 *RemainingVariableStorageSize
-= VariableSize
;
1493 // Go to the next one
1495 Variable
= NextVariable
;
1498 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
1499 *MaximumVariableSize
= 0;
1500 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
1501 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
1504 ReleaseLockOnlyAtBootTime (&Global
->VariableServicesLock
);
1510 VariableCommonInitialize (
1511 IN EFI_HANDLE ImageHandle
,
1512 IN EFI_SYSTEM_TABLE
*SystemTable
1516 Routine Description:
1517 This function does common initialization for variable services
1521 ImageHandle - The firmware allocated handle for the EFI image.
1522 SystemTable - A pointer to the EFI System Table.
1528 EFI_NOT_FOUND - Variable store area not found.
1529 EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.
1530 EFI_SUCCESS - Variable services successfully initialized.
1535 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
1537 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
1538 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1539 VARIABLE_HEADER
*NextVariable
;
1541 EFI_PHYSICAL_ADDRESS FvVolHdr
;
1542 UINT64 TempVariableStoreHeader
;
1543 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
1544 EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry
;
1550 mVariableModuleGlobal
= AllocateRuntimePool (sizeof (ESAL_VARIABLE_GLOBAL
));
1551 if (mVariableModuleGlobal
== NULL
) {
1552 return EFI_OUT_OF_RESOURCES
;
1555 EfiInitializeLock(&mVariableModuleGlobal
->VariableGlobal
[Physical
].VariableServicesLock
, TPL_NOTIFY
);
1558 // Allocate memory for volatile variable store
1560 VolatileVariableStore
= AllocateRuntimePool (VARIABLE_STORE_SIZE
+ SCRATCH_SIZE
);
1561 if (VolatileVariableStore
== NULL
) {
1562 FreePool (mVariableModuleGlobal
);
1563 return EFI_OUT_OF_RESOURCES
;
1566 SetMem (VolatileVariableStore
, VARIABLE_STORE_SIZE
+ SCRATCH_SIZE
, 0xff);
1569 // Variable Specific Data
1571 mVariableModuleGlobal
->VariableGlobal
[Physical
].VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
1572 mVariableModuleGlobal
->VolatileLastVariableOffset
= sizeof (VARIABLE_STORE_HEADER
);
1574 VolatileVariableStore
->Signature
= VARIABLE_STORE_SIGNATURE
;
1575 VolatileVariableStore
->Size
= VARIABLE_STORE_SIZE
;
1576 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
1577 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
1578 VolatileVariableStore
->Reserved
= 0;
1579 VolatileVariableStore
->Reserved1
= 0;
1582 // Get non volatile varaible store
1585 TempVariableStoreHeader
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableBase
);
1586 VariableStoreEntry
.Base
= TempVariableStoreHeader
+ \
1587 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1588 VariableStoreEntry
.Length
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
1589 (((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) (TempVariableStoreHeader
)) -> HeaderLength
);
1591 // Mark the variable storage region of the FLASH as RUNTIME
1593 BaseAddress
= VariableStoreEntry
.Base
& (~EFI_PAGE_MASK
);
1594 Length
= VariableStoreEntry
.Length
+ (VariableStoreEntry
.Base
- BaseAddress
);
1595 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
1597 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
1598 if (EFI_ERROR (Status
)) {
1599 FreePool (mVariableModuleGlobal
);
1600 FreePool (VolatileVariableStore
);
1601 return EFI_UNSUPPORTED
;
1604 Status
= gDS
->SetMemorySpaceAttributes (
1607 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
1609 if (EFI_ERROR (Status
)) {
1610 FreePool (mVariableModuleGlobal
);
1611 FreePool (VolatileVariableStore
);
1612 return EFI_UNSUPPORTED
;
1615 // Get address of non volatile variable store base
1617 mVariableModuleGlobal
->VariableGlobal
[Physical
].NonVolatileVariableBase
= VariableStoreEntry
.Base
;
1623 // Find the Correct Instance of the FV Block Service.
1626 CurrPtr
= (CHAR8
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
[Physical
].NonVolatileVariableBase
);
1627 while (EfiFvbGetPhysicalAddress (Instance
, &FvVolHdr
) == EFI_SUCCESS
) {
1628 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
1629 if (CurrPtr
>= (CHAR8
*) FwVolHeader
&& CurrPtr
< (((CHAR8
*) FwVolHeader
) + FwVolHeader
->FvLength
)) {
1630 mVariableModuleGlobal
->FvbInstance
= Instance
;
1637 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) CurrPtr
;
1638 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
1639 if (~VariableStoreHeader
->Size
== 0) {
1640 Status
= UpdateVariableStore (
1641 &mVariableModuleGlobal
->VariableGlobal
[Physical
],
1644 mVariableModuleGlobal
->FvbInstance
,
1645 (UINTN
) &VariableStoreHeader
->Size
,
1647 (UINT8
*) &VariableStoreEntry
.Length
1650 // As Variables are stored in NV storage, which are slow devices,such as flash.
1651 // Variable operation may skip checking variable program result to improve performance,
1652 // We can assume Variable program is OK through some check point.
1653 // Variable Store Size Setting should be the first Variable write operation,
1654 // We can assume all Read/Write is OK if we can set Variable store size successfully.
1655 // If write fail, we will assert here
1657 ASSERT(VariableStoreHeader
->Size
== VariableStoreEntry
.Length
);
1659 if (EFI_ERROR (Status
)) {
1664 mVariableModuleGlobal
->VariableGlobal
[Physical
].NonVolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) CurrPtr
);
1666 // Parse non-volatile variable data and get last variable offset
1668 NextVariable
= (VARIABLE_HEADER
*) (CurrPtr
+ sizeof (VARIABLE_STORE_HEADER
));
1669 Status
= EFI_SUCCESS
;
1671 while (IsValidVariableHeader (NextVariable
)) {
1672 NextVariable
= GetNextVariablePtr (NextVariable
);
1675 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) CurrPtr
;
1678 // Check if the free area is blow a threshold
1680 if ((((VARIABLE_STORE_HEADER
*)((UINTN
) CurrPtr
))->Size
- mVariableModuleGlobal
->NonVolatileLastVariableOffset
) < VARIABLE_RECLAIM_THRESHOLD
) {
1682 mVariableModuleGlobal
->VariableGlobal
[Physical
].NonVolatileVariableBase
,
1683 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1688 if (EFI_ERROR (Status
)) {
1689 FreePool (mVariableModuleGlobal
);
1690 FreePool (VolatileVariableStore
);
1695 // Check if the free area is really free.
1697 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
1698 Data
= ((UINT8
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
[Physical
].NonVolatileVariableBase
)[Index
];
1701 // There must be something wrong in variable store, do reclaim operation.
1704 mVariableModuleGlobal
->VariableGlobal
[Physical
].NonVolatileVariableBase
,
1705 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1713 if (EFI_ERROR (Status
)) {
1714 FreePool (mVariableModuleGlobal
);
1715 FreePool (VolatileVariableStore
);