3 The common variable operation routines shared by DXE_RUNTIME variable
4 module and DXE_SMM variable module.
6 Caution: This module requires additional review when modified.
7 This driver will have external input - variable data. They may be input in SMM mode.
8 This external input must be validated carefully to avoid security issue like
9 buffer overflow, integer overflow.
11 VariableServiceGetNextVariableName () and VariableServiceQueryVariableInfo() are external API.
12 They need check input parameter.
14 VariableServiceGetVariable() and VariableServiceSetVariable() are external API
15 to receive datasize and data buffer. The size should be checked carefully.
17 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
18 This program and the accompanying materials
19 are licensed and made available under the terms and conditions of the BSD License
20 which accompanies this distribution. The full text of the license may be found at
21 http://opensource.org/licenses/bsd-license.php
23 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
24 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
30 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
33 /// Define a memory cache that improves the search performance for a variable.
35 VARIABLE_STORE_HEADER
*mNvVariableCache
= NULL
;
38 /// The memory entry used for variable statistics data.
40 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
43 /// The list to store the variables which cannot be set after the EFI_END_OF_DXE_EVENT_GROUP_GUID
44 /// or EVT_GROUP_READY_TO_BOOT event.
46 LIST_ENTRY mLockedVariableList
= INITIALIZE_LIST_HEAD_VARIABLE (mLockedVariableList
);
49 /// The flag to indicate whether the platform has left the DXE phase of execution.
51 BOOLEAN mEndOfDxe
= FALSE
;
54 /// The flag to indicate whether the variable storage locking is enabled.
56 BOOLEAN mEnableLocking
= TRUE
;
59 // It will record the current boot error flag before EndOfDxe.
61 VAR_ERROR_FLAG mCurrentBootVarErrFlag
= VAR_ERROR_FLAG_NO_ERROR
;
64 Routine used to track statistical information about variable usage.
65 The data is stored in the EFI system table so it can be accessed later.
66 VariableInfo.efi can dump out the table. Only Boot Services variable
67 accesses are tracked by this code. The PcdVariableCollectStatistics
68 build flag controls if this feature is enabled.
70 A read that hits in the cache will have Read and Cache true for
71 the transaction. Data is allocated by this routine, but never
74 @param[in] VariableName Name of the Variable to track.
75 @param[in] VendorGuid Guid of the Variable to track.
76 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
77 @param[in] Read TRUE if GetVariable() was called.
78 @param[in] Write TRUE if SetVariable() was called.
79 @param[in] Delete TRUE if deleted via SetVariable().
80 @param[in] Cache TRUE for a cache hit.
85 IN CHAR16
*VariableName
,
86 IN EFI_GUID
*VendorGuid
,
94 VARIABLE_INFO_ENTRY
*Entry
;
96 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
99 // Don't collect statistics at runtime.
103 if (gVariableInfo
== NULL
) {
105 // On the first call allocate a entry and place a pointer to it in
106 // the EFI System Table.
108 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
109 ASSERT (gVariableInfo
!= NULL
);
111 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
112 gVariableInfo
->Name
= AllocateZeroPool (StrSize (VariableName
));
113 ASSERT (gVariableInfo
->Name
!= NULL
);
114 StrnCpy (gVariableInfo
->Name
, VariableName
, StrLen (VariableName
));
115 gVariableInfo
->Volatile
= Volatile
;
119 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
120 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
121 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
129 Entry
->DeleteCount
++;
139 if (Entry
->Next
== NULL
) {
141 // If the entry is not in the table add it.
142 // Next iteration of the loop will fill in the data.
144 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
145 ASSERT (Entry
->Next
!= NULL
);
147 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
148 Entry
->Next
->Name
= AllocateZeroPool (StrSize (VariableName
));
149 ASSERT (Entry
->Next
->Name
!= NULL
);
150 StrnCpy (Entry
->Next
->Name
, VariableName
, StrLen (VariableName
));
151 Entry
->Next
->Volatile
= Volatile
;
161 This code checks if variable header is valid or not.
163 @param Variable Pointer to the Variable Header.
164 @param VariableStoreEnd Pointer to the Variable Store End.
166 @retval TRUE Variable header is valid.
167 @retval FALSE Variable header is not valid.
171 IsValidVariableHeader (
172 IN VARIABLE_HEADER
*Variable
,
173 IN VARIABLE_HEADER
*VariableStoreEnd
176 if ((Variable
== NULL
) || (Variable
>= VariableStoreEnd
) || (Variable
->StartId
!= VARIABLE_DATA
)) {
178 // Variable is NULL or has reached the end of variable store,
179 // or the StartId is not correct.
190 This function writes data to the FWH at the correct LBA even if the LBAs
193 @param Global Pointer to VARAIBLE_GLOBAL structure.
194 @param Volatile Point out the Variable is Volatile or Non-Volatile.
195 @param SetByIndex TRUE if target pointer is given as index.
196 FALSE if target pointer is absolute.
197 @param Fvb Pointer to the writable FVB protocol.
198 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
200 @param DataSize Size of data to be written.
201 @param Buffer Pointer to the buffer from which data is written.
203 @retval EFI_INVALID_PARAMETER Parameters not valid.
204 @retval EFI_SUCCESS Variable store successfully updated.
208 UpdateVariableStore (
209 IN VARIABLE_GLOBAL
*Global
,
211 IN BOOLEAN SetByIndex
,
212 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
213 IN UINTN DataPtrIndex
,
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 ASSERT (Fvb
!= NULL
);
240 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
241 ASSERT_EFI_ERROR (Status
);
243 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
245 // Data Pointer should point to the actual Address where data is to be
249 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
252 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
253 return EFI_INVALID_PARAMETER
;
257 // Data Pointer should point to the actual Address where data is to be
260 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
262 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
265 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
266 return EFI_INVALID_PARAMETER
;
270 // If Volatile Variable just do a simple mem copy.
272 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
277 // If we are here we are dealing with Non-Volatile Variables.
279 LinearOffset
= (UINTN
) FwVolHeader
;
280 CurrWritePtr
= (UINTN
) DataPtr
;
281 CurrWriteSize
= DataSize
;
285 if (CurrWritePtr
< LinearOffset
) {
286 return EFI_INVALID_PARAMETER
;
289 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
290 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
292 // Check to see if the Variable Writes are spanning through multiple
295 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
296 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
297 Status
= Fvb
->Write (
300 (UINTN
) (CurrWritePtr
- LinearOffset
),
306 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
307 Status
= Fvb
->Write (
310 (UINTN
) (CurrWritePtr
- LinearOffset
),
314 if (EFI_ERROR (Status
)) {
318 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
319 CurrBuffer
= CurrBuffer
+ Size
;
320 CurrWriteSize
= CurrWriteSize
- Size
;
324 LinearOffset
+= PtrBlockMapEntry
->Length
;
335 This code gets the current status of Variable Store.
337 @param VarStoreHeader Pointer to the Variable Store Header.
339 @retval EfiRaw Variable store status is raw.
340 @retval EfiValid Variable store status is valid.
341 @retval EfiInvalid Variable store status is invalid.
344 VARIABLE_STORE_STATUS
345 GetVariableStoreStatus (
346 IN VARIABLE_STORE_HEADER
*VarStoreHeader
349 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
) &&
350 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
351 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
355 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
356 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
357 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
358 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
359 VarStoreHeader
->Size
== 0xffffffff &&
360 VarStoreHeader
->Format
== 0xff &&
361 VarStoreHeader
->State
== 0xff
373 This code gets the size of name of variable.
375 @param Variable Pointer to the Variable Header.
377 @return UINTN Size of variable in bytes.
382 IN VARIABLE_HEADER
*Variable
385 if (Variable
->State
== (UINT8
) (-1) ||
386 Variable
->DataSize
== (UINT32
) (-1) ||
387 Variable
->NameSize
== (UINT32
) (-1) ||
388 Variable
->Attributes
== (UINT32
) (-1)) {
391 return (UINTN
) Variable
->NameSize
;
396 This code gets the size of variable data.
398 @param Variable Pointer to the Variable Header.
400 @return Size of variable in bytes.
405 IN VARIABLE_HEADER
*Variable
408 if (Variable
->State
== (UINT8
) (-1) ||
409 Variable
->DataSize
== (UINT32
) (-1) ||
410 Variable
->NameSize
== (UINT32
) (-1) ||
411 Variable
->Attributes
== (UINT32
) (-1)) {
414 return (UINTN
) Variable
->DataSize
;
419 This code gets the pointer to the variable name.
421 @param Variable Pointer to the Variable Header.
423 @return Pointer to Variable Name which is Unicode encoding.
428 IN VARIABLE_HEADER
*Variable
432 return (CHAR16
*) (Variable
+ 1);
437 This code gets the pointer to the variable data.
439 @param Variable Pointer to the Variable Header.
441 @return Pointer to Variable Data.
446 IN VARIABLE_HEADER
*Variable
452 // Be careful about pad size for alignment.
454 Value
= (UINTN
) GetVariableNamePtr (Variable
);
455 Value
+= NameSizeOfVariable (Variable
);
456 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
458 return (UINT8
*) Value
;
464 This code gets the pointer to the next variable header.
466 @param Variable Pointer to the Variable Header.
468 @return Pointer to next variable header.
473 IN VARIABLE_HEADER
*Variable
478 Value
= (UINTN
) GetVariableDataPtr (Variable
);
479 Value
+= DataSizeOfVariable (Variable
);
480 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
483 // Be careful about pad size for alignment.
485 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
490 Gets the pointer to the first variable header in given variable store area.
492 @param VarStoreHeader Pointer to the Variable Store Header.
494 @return Pointer to the first variable header.
499 IN VARIABLE_STORE_HEADER
*VarStoreHeader
503 // The end of variable store.
505 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
510 Gets the pointer to the end of the variable storage area.
512 This function gets pointer to the end of the variable storage
513 area, according to the input variable store header.
515 @param VarStoreHeader Pointer to the Variable Store Header.
517 @return Pointer to the end of the variable storage area.
522 IN VARIABLE_STORE_HEADER
*VarStoreHeader
526 // The end of variable store
528 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
532 Record variable error flag.
534 @param[in] Flag Variable error flag to record.
535 @param[in] VariableName Name of variable.
536 @param[in] VendorGuid Guid of variable.
537 @param[in] Attributes Attributes of the variable.
538 @param[in] VariableSize Size of the variable.
543 IN VAR_ERROR_FLAG Flag
,
544 IN CHAR16
*VariableName
,
545 IN EFI_GUID
*VendorGuid
,
546 IN UINT32 Attributes
,
547 IN UINTN VariableSize
551 VARIABLE_POINTER_TRACK Variable
;
552 VAR_ERROR_FLAG
*VarErrFlag
;
553 VAR_ERROR_FLAG TempFlag
;
556 DEBUG ((EFI_D_ERROR
, "RecordVarErrorFlag (0x%02x) %s:%g - 0x%08x - 0x%x\n", Flag
, VariableName
, VendorGuid
, Attributes
, VariableSize
));
557 if (Flag
== VAR_ERROR_FLAG_SYSTEM_ERROR
) {
559 DEBUG ((EFI_D_ERROR
, "CommonRuntimeVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal
->CommonRuntimeVariableSpace
, mVariableModuleGlobal
->CommonVariableTotalSize
));
561 DEBUG ((EFI_D_ERROR
, "CommonVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal
->CommonVariableSpace
, mVariableModuleGlobal
->CommonVariableTotalSize
));
564 DEBUG ((EFI_D_ERROR
, "CommonMaxUserVariableSpace = 0x%x - CommonUserVariableTotalSize = 0x%x\n", mVariableModuleGlobal
->CommonMaxUserVariableSpace
, mVariableModuleGlobal
->CommonUserVariableTotalSize
));
570 // Before EndOfDxe, just record the current boot variable error flag to local variable,
571 // and leave the variable error flag in NV flash as the last boot variable error flag.
572 // After EndOfDxe in InitializeVarErrorFlag (), the variable error flag in NV flash
573 // will be initialized to this local current boot variable error flag.
575 mCurrentBootVarErrFlag
&= Flag
;
580 // Record error flag (it should have be initialized).
582 Status
= FindVariable (
584 &gEdkiiVarErrorFlagGuid
,
586 &mVariableModuleGlobal
->VariableGlobal
,
589 if (!EFI_ERROR (Status
)) {
590 VarErrFlag
= (VAR_ERROR_FLAG
*) GetVariableDataPtr (Variable
.CurrPtr
);
591 TempFlag
= *VarErrFlag
;
593 if (TempFlag
== *VarErrFlag
) {
596 Status
= UpdateVariableStore (
597 &mVariableModuleGlobal
->VariableGlobal
,
600 mVariableModuleGlobal
->FvbInstance
,
601 (UINTN
) VarErrFlag
- (UINTN
) mNvVariableCache
+ (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
605 if (!EFI_ERROR (Status
)) {
607 // Update the data in NV cache.
615 Initialize variable error flag.
617 Before EndOfDxe, the variable indicates the last boot variable error flag,
618 then it means the last boot variable error flag must be got before EndOfDxe.
619 After EndOfDxe, the variable indicates the current boot variable error flag,
620 then it means the current boot variable error flag must be got after EndOfDxe.
624 InitializeVarErrorFlag (
629 VARIABLE_POINTER_TRACK Variable
;
631 VAR_ERROR_FLAG VarErrFlag
;
637 Flag
= mCurrentBootVarErrFlag
;
638 DEBUG ((EFI_D_INFO
, "Initialize variable error flag (%02x)\n", Flag
));
640 Status
= FindVariable (
642 &gEdkiiVarErrorFlagGuid
,
644 &mVariableModuleGlobal
->VariableGlobal
,
647 if (!EFI_ERROR (Status
)) {
648 VarErrFlag
= *((VAR_ERROR_FLAG
*) GetVariableDataPtr (Variable
.CurrPtr
));
649 if (VarErrFlag
== Flag
) {
656 &gEdkiiVarErrorFlagGuid
,
659 VARIABLE_ATTRIBUTE_NV_BS_RT
,
667 @param[in] Variable Pointer to variable header.
669 @retval TRUE User variable.
670 @retval FALSE System variable.
675 IN VARIABLE_HEADER
*Variable
678 VAR_CHECK_VARIABLE_PROPERTY Property
;
681 // Only after End Of Dxe, the variables belong to system variable are fixed.
682 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,
683 // then no need to check if the variable is user variable or not specially.
685 if (mEndOfDxe
&& (mVariableModuleGlobal
->CommonMaxUserVariableSpace
!= mVariableModuleGlobal
->CommonVariableSpace
)) {
686 if (InternalVarCheckVariablePropertyGet (GetVariableNamePtr (Variable
), &Variable
->VendorGuid
, &Property
) == EFI_NOT_FOUND
) {
694 Calculate common user variable total size.
698 CalculateCommonUserVariableTotalSize (
702 VARIABLE_HEADER
*Variable
;
703 VARIABLE_HEADER
*NextVariable
;
705 VAR_CHECK_VARIABLE_PROPERTY Property
;
708 // Only after End Of Dxe, the variables belong to system variable are fixed.
709 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,
710 // then no need to calculate the common user variable total size specially.
712 if (mEndOfDxe
&& (mVariableModuleGlobal
->CommonMaxUserVariableSpace
!= mVariableModuleGlobal
->CommonVariableSpace
)) {
713 Variable
= GetStartPointer (mNvVariableCache
);
714 while (IsValidVariableHeader (Variable
, GetEndPointer (mNvVariableCache
))) {
715 NextVariable
= GetNextVariablePtr (Variable
);
716 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
717 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
718 if (InternalVarCheckVariablePropertyGet (GetVariableNamePtr (Variable
), &Variable
->VendorGuid
, &Property
) == EFI_NOT_FOUND
) {
720 // No property, it is user variable.
722 mVariableModuleGlobal
->CommonUserVariableTotalSize
+= VariableSize
;
726 Variable
= NextVariable
;
732 Initialize variable quota.
736 InitializeVariableQuota (
740 STATIC BOOLEAN Initialized
;
742 if (!mEndOfDxe
|| Initialized
) {
747 InitializeVarErrorFlag ();
748 CalculateCommonUserVariableTotalSize ();
753 Variable store garbage collection and reclaim operation.
755 @param VariableBase Base address of variable store.
756 @param LastVariableOffset Offset of last variable.
757 @param IsVolatile The variable store is volatile or not;
758 if it is non-volatile, need FTW.
759 @param UpdatingPtrTrack Pointer to updating variable pointer track structure.
760 @param NewVariable Pointer to new variable.
761 @param NewVariableSize New variable size.
763 @return EFI_OUT_OF_RESOURCES
770 IN EFI_PHYSICAL_ADDRESS VariableBase
,
771 OUT UINTN
*LastVariableOffset
,
772 IN BOOLEAN IsVolatile
,
773 IN OUT VARIABLE_POINTER_TRACK
*UpdatingPtrTrack
,
774 IN VARIABLE_HEADER
*NewVariable
,
775 IN UINTN NewVariableSize
778 VARIABLE_HEADER
*Variable
;
779 VARIABLE_HEADER
*AddedVariable
;
780 VARIABLE_HEADER
*NextVariable
;
781 VARIABLE_HEADER
*NextAddedVariable
;
782 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
784 UINTN MaximumBufferSize
;
792 UINTN CommonVariableTotalSize
;
793 UINTN CommonUserVariableTotalSize
;
794 UINTN HwErrVariableTotalSize
;
795 VARIABLE_HEADER
*UpdatingVariable
;
796 VARIABLE_HEADER
*UpdatingInDeletedTransition
;
798 UpdatingVariable
= NULL
;
799 UpdatingInDeletedTransition
= NULL
;
800 if (UpdatingPtrTrack
!= NULL
) {
801 UpdatingVariable
= UpdatingPtrTrack
->CurrPtr
;
802 UpdatingInDeletedTransition
= UpdatingPtrTrack
->InDeletedTransitionPtr
;
805 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
807 CommonVariableTotalSize
= 0;
808 CommonUserVariableTotalSize
= 0;
809 HwErrVariableTotalSize
= 0;
813 // Start Pointers for the variable.
815 Variable
= GetStartPointer (VariableStoreHeader
);
816 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
818 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
819 NextVariable
= GetNextVariablePtr (Variable
);
820 if ((Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) &&
821 Variable
!= UpdatingVariable
&&
822 Variable
!= UpdatingInDeletedTransition
824 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
825 MaximumBufferSize
+= VariableSize
;
828 Variable
= NextVariable
;
831 if (NewVariable
!= NULL
) {
833 // Add the new variable size.
835 MaximumBufferSize
+= NewVariableSize
;
839 // Reserve the 1 Bytes with Oxff to identify the
840 // end of the variable buffer.
842 MaximumBufferSize
+= 1;
843 ValidBuffer
= AllocatePool (MaximumBufferSize
);
844 if (ValidBuffer
== NULL
) {
845 return EFI_OUT_OF_RESOURCES
;
849 // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache
850 // as the buffer to reduce SMRAM consumption for SMM variable driver.
852 MaximumBufferSize
= mNvVariableCache
->Size
;
853 ValidBuffer
= (UINT8
*) mNvVariableCache
;
856 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
859 // Copy variable store header.
861 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
862 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
865 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
867 Variable
= GetStartPointer (VariableStoreHeader
);
868 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
869 NextVariable
= GetNextVariablePtr (Variable
);
870 if (Variable
!= UpdatingVariable
&& Variable
->State
== VAR_ADDED
) {
871 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
872 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
873 CurrPtr
+= VariableSize
;
874 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
875 HwErrVariableTotalSize
+= VariableSize
;
876 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
877 CommonVariableTotalSize
+= VariableSize
;
878 if (IsUserVariable (Variable
)) {
879 CommonUserVariableTotalSize
+= VariableSize
;
883 Variable
= NextVariable
;
887 // Reinstall all in delete transition variables.
889 Variable
= GetStartPointer (VariableStoreHeader
);
890 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
891 NextVariable
= GetNextVariablePtr (Variable
);
892 if (Variable
!= UpdatingVariable
&& Variable
!= UpdatingInDeletedTransition
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
895 // Buffer has cached all ADDED variable.
896 // Per IN_DELETED variable, we have to guarantee that
897 // no ADDED one in previous buffer.
901 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
902 while (IsValidVariableHeader (AddedVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
))) {
903 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
904 NameSize
= NameSizeOfVariable (AddedVariable
);
905 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
906 NameSize
== NameSizeOfVariable (Variable
)
908 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
909 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
910 if (CompareMem (Point0
, Point1
, NameSize
) == 0) {
915 AddedVariable
= NextAddedVariable
;
919 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
921 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
922 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
923 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
924 CurrPtr
+= VariableSize
;
925 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
926 HwErrVariableTotalSize
+= VariableSize
;
927 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
928 CommonVariableTotalSize
+= VariableSize
;
929 if (IsUserVariable (Variable
)) {
930 CommonUserVariableTotalSize
+= VariableSize
;
936 Variable
= NextVariable
;
940 // Install the new variable if it is not NULL.
942 if (NewVariable
!= NULL
) {
943 if ((UINTN
) (CurrPtr
- ValidBuffer
) + NewVariableSize
> VariableStoreHeader
->Size
) {
945 // No enough space to store the new variable.
947 Status
= EFI_OUT_OF_RESOURCES
;
951 if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
952 HwErrVariableTotalSize
+= NewVariableSize
;
953 } else if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
954 CommonVariableTotalSize
+= NewVariableSize
;
955 if (IsUserVariable (NewVariable
)) {
956 CommonUserVariableTotalSize
+= NewVariableSize
;
959 if ((HwErrVariableTotalSize
> PcdGet32 (PcdHwErrStorageSize
)) ||
960 (CommonVariableTotalSize
> mVariableModuleGlobal
->CommonVariableSpace
) ||
961 (CommonUserVariableTotalSize
> mVariableModuleGlobal
->CommonMaxUserVariableSpace
)) {
963 // No enough space to store the new variable by NV or NV+HR attribute.
965 Status
= EFI_OUT_OF_RESOURCES
;
970 CopyMem (CurrPtr
, (UINT8
*) NewVariable
, NewVariableSize
);
971 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
972 if (UpdatingVariable
!= NULL
) {
973 UpdatingPtrTrack
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)UpdatingPtrTrack
->StartPtr
+ ((UINTN
)CurrPtr
- (UINTN
)GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
)));
974 UpdatingPtrTrack
->InDeletedTransitionPtr
= NULL
;
976 CurrPtr
+= NewVariableSize
;
981 // If volatile variable store, just copy valid buffer.
983 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
984 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- ValidBuffer
));
985 *LastVariableOffset
= (UINTN
) (CurrPtr
- ValidBuffer
);
986 Status
= EFI_SUCCESS
;
989 // If non-volatile variable store, perform FTW here.
991 Status
= FtwVariableSpace (
993 (VARIABLE_STORE_HEADER
*) ValidBuffer
995 if (!EFI_ERROR (Status
)) {
996 *LastVariableOffset
= (UINTN
) (CurrPtr
- ValidBuffer
);
997 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
998 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
999 mVariableModuleGlobal
->CommonUserVariableTotalSize
= CommonUserVariableTotalSize
;
1001 Variable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
1002 while (IsValidVariableHeader (Variable
, GetEndPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
))) {
1003 NextVariable
= GetNextVariablePtr (Variable
);
1004 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
1005 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1006 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
1007 } else if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1008 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
1009 if (IsUserVariable (Variable
)) {
1010 mVariableModuleGlobal
->CommonUserVariableTotalSize
+= VariableSize
;
1014 Variable
= NextVariable
;
1016 *LastVariableOffset
= (UINTN
) Variable
- (UINTN
) VariableBase
;
1022 FreePool (ValidBuffer
);
1025 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.
1027 CopyMem (mNvVariableCache
, (UINT8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
1034 Find the variable in the specified variable store.
1036 @param VariableName Name of the variable to be found
1037 @param VendorGuid Vendor GUID to be found.
1038 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
1039 check at runtime when searching variable.
1040 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
1042 @retval EFI_SUCCESS Variable found successfully
1043 @retval EFI_NOT_FOUND Variable not found
1047 IN CHAR16
*VariableName
,
1048 IN EFI_GUID
*VendorGuid
,
1049 IN BOOLEAN IgnoreRtCheck
,
1050 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
1053 VARIABLE_HEADER
*InDeletedVariable
;
1056 PtrTrack
->InDeletedTransitionPtr
= NULL
;
1059 // Find the variable by walk through HOB, volatile and non-volatile variable store.
1061 InDeletedVariable
= NULL
;
1063 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
1064 ; IsValidVariableHeader (PtrTrack
->CurrPtr
, PtrTrack
->EndPtr
)
1065 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
1067 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
1068 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
1070 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
1071 if (VariableName
[0] == 0) {
1072 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1073 InDeletedVariable
= PtrTrack
->CurrPtr
;
1075 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
1079 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
1080 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
1082 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
1083 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
1084 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1085 InDeletedVariable
= PtrTrack
->CurrPtr
;
1087 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
1097 PtrTrack
->CurrPtr
= InDeletedVariable
;
1098 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
1103 Finds variable in storage blocks of volatile and non-volatile storage areas.
1105 This code finds variable in storage blocks of volatile and non-volatile storage areas.
1106 If VariableName is an empty string, then we just return the first
1107 qualified variable without comparing VariableName and VendorGuid.
1108 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
1109 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
1110 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
1112 @param VariableName Name of the variable to be found.
1113 @param VendorGuid Vendor GUID to be found.
1114 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
1115 including the range searched and the target position.
1116 @param Global Pointer to VARIABLE_GLOBAL structure, including
1117 base of volatile variable storage area, base of
1118 NV variable storage area, and a lock.
1119 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
1120 check at runtime when searching variable.
1122 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
1124 @retval EFI_SUCCESS Variable successfully found.
1125 @retval EFI_NOT_FOUND Variable not found
1130 IN CHAR16
*VariableName
,
1131 IN EFI_GUID
*VendorGuid
,
1132 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
1133 IN VARIABLE_GLOBAL
*Global
,
1134 IN BOOLEAN IgnoreRtCheck
1138 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1139 VARIABLE_STORE_TYPE Type
;
1141 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
1142 return EFI_INVALID_PARAMETER
;
1146 // 0: Volatile, 1: HOB, 2: Non-Volatile.
1147 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
1148 // make use of this mapping to implement search algorithm.
1150 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
1151 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
1152 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
1155 // Find the variable by walk through HOB, volatile and non-volatile variable store.
1157 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1158 if (VariableStoreHeader
[Type
] == NULL
) {
1162 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
1163 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
1164 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
1166 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
1167 if (!EFI_ERROR (Status
)) {
1171 return EFI_NOT_FOUND
;
1175 Get index from supported language codes according to language string.
1177 This code is used to get corresponding index in supported language codes. It can handle
1178 RFC4646 and ISO639 language tags.
1179 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
1180 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
1183 SupportedLang = "engfraengfra"
1185 Iso639Language = TRUE
1186 The return value is "0".
1188 SupportedLang = "en;fr;en-US;fr-FR"
1190 Iso639Language = FALSE
1191 The return value is "3".
1193 @param SupportedLang Platform supported language codes.
1194 @param Lang Configured language.
1195 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1197 @retval The index of language in the language codes.
1201 GetIndexFromSupportedLangCodes(
1202 IN CHAR8
*SupportedLang
,
1204 IN BOOLEAN Iso639Language
1208 UINTN CompareLength
;
1209 UINTN LanguageLength
;
1211 if (Iso639Language
) {
1212 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1213 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
1214 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
1216 // Successfully find the index of Lang string in SupportedLang string.
1218 Index
= Index
/ CompareLength
;
1226 // Compare RFC4646 language code
1229 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
1231 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
1233 // Skip ';' characters in SupportedLang
1235 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
1237 // Determine the length of the next language code in SupportedLang
1239 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
1241 if ((CompareLength
== LanguageLength
) &&
1242 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
1244 // Successfully find the index of Lang string in SupportedLang string.
1255 Get language string from supported language codes according to index.
1257 This code is used to get corresponding language strings in supported language codes. It can handle
1258 RFC4646 and ISO639 language tags.
1259 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
1260 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
1263 SupportedLang = "engfraengfra"
1265 Iso639Language = TRUE
1266 The return value is "fra".
1268 SupportedLang = "en;fr;en-US;fr-FR"
1270 Iso639Language = FALSE
1271 The return value is "fr".
1273 @param SupportedLang Platform supported language codes.
1274 @param Index The index in supported language codes.
1275 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1277 @retval The language string in the language codes.
1281 GetLangFromSupportedLangCodes (
1282 IN CHAR8
*SupportedLang
,
1284 IN BOOLEAN Iso639Language
1288 UINTN CompareLength
;
1292 Supported
= SupportedLang
;
1293 if (Iso639Language
) {
1295 // According to the index of Lang string in SupportedLang string to get the language.
1296 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1297 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1299 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1300 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1301 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1306 // Take semicolon as delimitation, sequentially traverse supported language codes.
1308 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1311 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1313 // Have completed the traverse, but not find corrsponding string.
1314 // This case is not allowed to happen.
1319 if (SubIndex
== Index
) {
1321 // According to the index of Lang string in SupportedLang string to get the language.
1322 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1323 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1325 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1326 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1331 // Skip ';' characters in Supported
1333 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1339 Returns a pointer to an allocated buffer that contains the best matching language
1340 from a set of supported languages.
1342 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1343 code types may not be mixed in a single call to this function. This function
1344 supports a variable argument list that allows the caller to pass in a prioritized
1345 list of language codes to test against all the language codes in SupportedLanguages.
1347 If SupportedLanguages is NULL, then ASSERT().
1349 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1350 contains a set of language codes in the format
1351 specified by Iso639Language.
1352 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1353 in ISO 639-2 format. If FALSE, then all language
1354 codes are assumed to be in RFC 4646 language format
1355 @param[in] ... A variable argument list that contains pointers to
1356 Null-terminated ASCII strings that contain one or more
1357 language codes in the format specified by Iso639Language.
1358 The first language code from each of these language
1359 code lists is used to determine if it is an exact or
1360 close match to any of the language codes in
1361 SupportedLanguages. Close matches only apply to RFC 4646
1362 language codes, and the matching algorithm from RFC 4647
1363 is used to determine if a close match is present. If
1364 an exact or close match is found, then the matching
1365 language code from SupportedLanguages is returned. If
1366 no matches are found, then the next variable argument
1367 parameter is evaluated. The variable argument list
1368 is terminated by a NULL.
1370 @retval NULL The best matching language could not be found in SupportedLanguages.
1371 @retval NULL There are not enough resources available to return the best matching
1373 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1374 language in SupportedLanguages.
1379 VariableGetBestLanguage (
1380 IN CONST CHAR8
*SupportedLanguages
,
1381 IN BOOLEAN Iso639Language
,
1387 UINTN CompareLength
;
1388 UINTN LanguageLength
;
1389 CONST CHAR8
*Supported
;
1392 ASSERT (SupportedLanguages
!= NULL
);
1394 VA_START (Args
, Iso639Language
);
1395 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1397 // Default to ISO 639-2 mode
1400 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1403 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1405 if (!Iso639Language
) {
1406 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1410 // Trim back the length of Language used until it is empty
1412 while (LanguageLength
> 0) {
1414 // Loop through all language codes in SupportedLanguages
1416 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1418 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1420 if (!Iso639Language
) {
1422 // Skip ';' characters in Supported
1424 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1426 // Determine the length of the next language code in Supported
1428 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1430 // If Language is longer than the Supported, then skip to the next language
1432 if (LanguageLength
> CompareLength
) {
1437 // See if the first LanguageLength characters in Supported match Language
1439 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1442 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1443 Buffer
[CompareLength
] = '\0';
1444 return CopyMem (Buffer
, Supported
, CompareLength
);
1448 if (Iso639Language
) {
1450 // If ISO 639 mode, then each language can only be tested once
1455 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1457 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1464 // No matches were found
1470 This function is to check if the remaining variable space is enough to set
1471 all Variables from argument list successfully. The purpose of the check
1472 is to keep the consistency of the Variables to be in variable storage.
1474 Note: Variables are assumed to be in same storage.
1475 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1476 so follow the argument sequence to check the Variables.
1478 @param[in] Attributes Variable attributes for Variable entries.
1479 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1480 A NULL terminates the list. The VariableSize of
1481 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
1482 It will be changed to variable total size as output.
1484 @retval TRUE Have enough variable space to set the Variables successfully.
1485 @retval FALSE No enough variable space to set the Variables successfully.
1490 CheckRemainingSpaceForConsistency (
1491 IN UINT32 Attributes
,
1497 VARIABLE_ENTRY_CONSISTENCY
*VariableEntry
;
1498 UINT64 MaximumVariableStorageSize
;
1499 UINT64 RemainingVariableStorageSize
;
1500 UINT64 MaximumVariableSize
;
1501 UINTN TotalNeededSize
;
1502 UINTN OriginalVarSize
;
1503 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1504 VARIABLE_POINTER_TRACK VariablePtrTrack
;
1505 VARIABLE_HEADER
*NextVariable
;
1510 // Non-Volatile related.
1512 VariableStoreHeader
= mNvVariableCache
;
1514 Status
= VariableServiceQueryVariableInfoInternal (
1516 &MaximumVariableStorageSize
,
1517 &RemainingVariableStorageSize
,
1518 &MaximumVariableSize
1520 ASSERT_EFI_ERROR (Status
);
1522 TotalNeededSize
= 0;
1523 VA_START (Args
, Attributes
);
1524 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1525 while (VariableEntry
!= NULL
) {
1527 // Calculate variable total size.
1529 VarNameSize
= StrSize (VariableEntry
->Name
);
1530 VarNameSize
+= GET_PAD_SIZE (VarNameSize
);
1531 VarDataSize
= VariableEntry
->VariableSize
;
1532 VarDataSize
+= GET_PAD_SIZE (VarDataSize
);
1533 VariableEntry
->VariableSize
= HEADER_ALIGN (sizeof (VARIABLE_HEADER
) + VarNameSize
+ VarDataSize
);
1535 TotalNeededSize
+= VariableEntry
->VariableSize
;
1536 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1540 if (RemainingVariableStorageSize
>= TotalNeededSize
) {
1542 // Already have enough space.
1545 } else if (AtRuntime ()) {
1547 // At runtime, no reclaim.
1548 // The original variable space of Variables can't be reused.
1553 VA_START (Args
, Attributes
);
1554 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1555 while (VariableEntry
!= NULL
) {
1557 // Check if Variable[Index] has been present and get its size.
1559 OriginalVarSize
= 0;
1560 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
1561 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
1562 Status
= FindVariableEx (
1563 VariableEntry
->Name
,
1564 VariableEntry
->Guid
,
1568 if (!EFI_ERROR (Status
)) {
1570 // Get size of Variable[Index].
1572 NextVariable
= GetNextVariablePtr (VariablePtrTrack
.CurrPtr
);
1573 OriginalVarSize
= (UINTN
) NextVariable
- (UINTN
) VariablePtrTrack
.CurrPtr
;
1575 // Add the original size of Variable[Index] to remaining variable storage size.
1577 RemainingVariableStorageSize
+= OriginalVarSize
;
1579 if (VariableEntry
->VariableSize
> RemainingVariableStorageSize
) {
1581 // No enough space for Variable[Index].
1587 // Sub the (new) size of Variable[Index] from remaining variable storage size.
1589 RemainingVariableStorageSize
-= VariableEntry
->VariableSize
;
1590 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1598 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1600 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1602 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1603 and are read-only. Therefore, in variable driver, only store the original value for other use.
1605 @param[in] VariableName Name of variable.
1607 @param[in] Data Variable data.
1609 @param[in] DataSize Size of data. 0 means delete.
1611 @retval EFI_SUCCESS The update operation is successful or ignored.
1612 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.
1613 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.
1614 @retval Others Other errors happened during the update operation.
1618 AutoUpdateLangVariable (
1619 IN CHAR16
*VariableName
,
1625 CHAR8
*BestPlatformLang
;
1629 VARIABLE_POINTER_TRACK Variable
;
1630 BOOLEAN SetLanguageCodes
;
1631 VARIABLE_ENTRY_CONSISTENCY VariableEntry
[2];
1634 // Don't do updates for delete operation
1636 if (DataSize
== 0) {
1640 SetLanguageCodes
= FALSE
;
1642 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
) == 0) {
1644 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1647 return EFI_WRITE_PROTECTED
;
1650 SetLanguageCodes
= TRUE
;
1653 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1654 // Therefore, in variable driver, only store the original value for other use.
1656 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1657 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1659 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1660 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1663 // PlatformLang holds a single language from PlatformLangCodes,
1664 // so the size of PlatformLangCodes is enough for the PlatformLang.
1666 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1667 FreePool (mVariableModuleGlobal
->PlatformLang
);
1669 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1670 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1672 } else if (StrCmp (VariableName
, EFI_LANG_CODES_VARIABLE_NAME
) == 0) {
1674 // LangCodes is a volatile variable, so it can not be updated at runtime.
1677 return EFI_WRITE_PROTECTED
;
1680 SetLanguageCodes
= TRUE
;
1683 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1684 // Therefore, in variable driver, only store the original value for other use.
1686 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1687 FreePool (mVariableModuleGlobal
->LangCodes
);
1689 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1690 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1693 if (SetLanguageCodes
1694 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1695 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1697 // Update Lang if PlatformLang is already set
1698 // Update PlatformLang if Lang is already set
1700 Status
= FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1701 if (!EFI_ERROR (Status
)) {
1705 VariableName
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1706 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1707 DataSize
= Variable
.CurrPtr
->DataSize
;
1709 Status
= FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1710 if (!EFI_ERROR (Status
)) {
1712 // Update PlatformLang
1714 VariableName
= EFI_LANG_VARIABLE_NAME
;
1715 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1716 DataSize
= Variable
.CurrPtr
->DataSize
;
1719 // Neither PlatformLang nor Lang is set, directly return
1726 Status
= EFI_SUCCESS
;
1729 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1731 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1733 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_VARIABLE_NAME
) == 0) {
1735 // Update Lang when PlatformLangCodes/LangCodes were set.
1737 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1739 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1741 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1742 if (BestPlatformLang
!= NULL
) {
1744 // Get the corresponding index in language codes.
1746 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1749 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1751 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1754 // Check the variable space for both Lang and PlatformLang variable.
1756 VariableEntry
[0].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1757 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1758 VariableEntry
[0].Name
= EFI_LANG_VARIABLE_NAME
;
1760 VariableEntry
[1].VariableSize
= AsciiStrSize (BestPlatformLang
);
1761 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1762 VariableEntry
[1].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1763 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1765 // No enough variable space to set both Lang and PlatformLang successfully.
1767 Status
= EFI_OUT_OF_RESOURCES
;
1770 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1772 FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1774 Status
= UpdateVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestLang
,
1775 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
1778 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang
, BestLang
, Status
));
1782 } else if (StrCmp (VariableName
, EFI_LANG_VARIABLE_NAME
) == 0) {
1784 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1786 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1788 // When setting Lang, firstly get most matched language string from supported language codes.
1790 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1791 if (BestLang
!= NULL
) {
1793 // Get the corresponding index in language codes.
1795 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1798 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1800 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1803 // Check the variable space for both PlatformLang and Lang variable.
1805 VariableEntry
[0].VariableSize
= AsciiStrSize (BestPlatformLang
);
1806 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1807 VariableEntry
[0].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1809 VariableEntry
[1].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1810 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1811 VariableEntry
[1].Name
= EFI_LANG_VARIABLE_NAME
;
1812 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1814 // No enough variable space to set both PlatformLang and Lang successfully.
1816 Status
= EFI_OUT_OF_RESOURCES
;
1819 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1821 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1823 Status
= UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestPlatformLang
,
1824 AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
1827 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang
, BestPlatformLang
, Status
));
1832 if (SetLanguageCodes
) {
1834 // Continue to set PlatformLangCodes or LangCodes.
1843 Update the variable region with Variable information. These are the same
1844 arguments as the EFI Variable services.
1846 @param[in] VariableName Name of variable.
1847 @param[in] VendorGuid Guid of variable.
1848 @param[in] Data Variable data.
1849 @param[in] DataSize Size of data. 0 means delete.
1850 @param[in] Attributes Attribues of the variable.
1851 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.
1853 @retval EFI_SUCCESS The update operation is success.
1854 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1859 IN CHAR16
*VariableName
,
1860 IN EFI_GUID
*VendorGuid
,
1863 IN UINT32 Attributes OPTIONAL
,
1864 IN OUT VARIABLE_POINTER_TRACK
*CacheVariable
1868 VARIABLE_HEADER
*NextVariable
;
1870 UINTN VarNameOffset
;
1871 UINTN VarDataOffset
;
1875 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1877 VARIABLE_POINTER_TRACK
*Variable
;
1878 VARIABLE_POINTER_TRACK NvVariable
;
1879 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1881 BOOLEAN IsCommonVariable
;
1882 BOOLEAN IsCommonUserVariable
;
1884 if ((mVariableModuleGlobal
->FvbInstance
== NULL
) && ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0)) {
1886 // The FVB protocol is not ready. Trying to update NV variable prior to the installation
1887 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
1889 return EFI_NOT_AVAILABLE_YET
;
1892 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1893 Variable
= CacheVariable
;
1896 // Update/Delete existing NV variable.
1897 // CacheVariable points to the variable in the memory copy of Flash area
1898 // Now let Variable points to the same variable in Flash area.
1900 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1901 Variable
= &NvVariable
;
1902 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1903 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1904 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1905 if (CacheVariable
->InDeletedTransitionPtr
!= NULL
) {
1906 Variable
->InDeletedTransitionPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->InDeletedTransitionPtr
- (UINTN
)CacheVariable
->StartPtr
));
1908 Variable
->InDeletedTransitionPtr
= NULL
;
1910 Variable
->Volatile
= FALSE
;
1913 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1915 if (Variable
->CurrPtr
!= NULL
) {
1917 // Update/Delete existing variable.
1921 // If AtRuntime and the variable is Volatile and Runtime Access,
1922 // the volatile is ReadOnly, and SetVariable should be aborted and
1923 // return EFI_WRITE_PROTECTED.
1925 if (Variable
->Volatile
) {
1926 Status
= EFI_WRITE_PROTECTED
;
1930 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.
1932 if (((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0)) {
1933 Status
= EFI_INVALID_PARAMETER
;
1939 // Setting a data variable with no access, or zero DataSize attributes
1940 // causes it to be deleted.
1942 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1943 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
1945 // Both ADDED and IN_DELETED_TRANSITION variable are present,
1946 // set IN_DELETED_TRANSITION one to DELETED state first.
1948 State
= Variable
->InDeletedTransitionPtr
->State
;
1949 State
&= VAR_DELETED
;
1950 Status
= UpdateVariableStore (
1951 &mVariableModuleGlobal
->VariableGlobal
,
1955 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
1959 if (!EFI_ERROR (Status
)) {
1960 if (!Variable
->Volatile
) {
1961 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
1962 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
1969 State
= Variable
->CurrPtr
->State
;
1970 State
&= VAR_DELETED
;
1972 Status
= UpdateVariableStore (
1973 &mVariableModuleGlobal
->VariableGlobal
,
1977 (UINTN
) &Variable
->CurrPtr
->State
,
1981 if (!EFI_ERROR (Status
)) {
1982 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1983 if (!Variable
->Volatile
) {
1984 CacheVariable
->CurrPtr
->State
= State
;
1985 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1991 // If the variable is marked valid, and the same data has been passed in,
1992 // then return to the caller immediately.
1994 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1995 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0)) {
1997 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1998 Status
= EFI_SUCCESS
;
2000 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
2001 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
2004 // Mark the old variable as in delete transition.
2006 State
= Variable
->CurrPtr
->State
;
2007 State
&= VAR_IN_DELETED_TRANSITION
;
2009 Status
= UpdateVariableStore (
2010 &mVariableModuleGlobal
->VariableGlobal
,
2014 (UINTN
) &Variable
->CurrPtr
->State
,
2018 if (EFI_ERROR (Status
)) {
2021 if (!Variable
->Volatile
) {
2022 CacheVariable
->CurrPtr
->State
= State
;
2027 // Not found existing variable. Create a new variable.
2031 // Make sure we are trying to create a new variable.
2032 // Setting a data variable with zero DataSize or no access attributes means to delete it.
2034 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
2035 Status
= EFI_NOT_FOUND
;
2040 // Only variable have NV|RT attribute can be created in Runtime.
2043 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
2044 Status
= EFI_INVALID_PARAMETER
;
2050 // Function part - create a new variable and copy the data.
2051 // Both update a variable and create a variable will come here.
2054 // Tricky part: Use scratch data area at the end of volatile variable store
2055 // as a temporary storage.
2057 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
2058 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2060 SetMem (NextVariable
, ScratchSize
, 0xff);
2062 NextVariable
->StartId
= VARIABLE_DATA
;
2063 NextVariable
->Attributes
= Attributes
;
2065 // NextVariable->State = VAR_ADDED;
2067 NextVariable
->Reserved
= 0;
2068 VarNameOffset
= sizeof (VARIABLE_HEADER
);
2069 VarNameSize
= StrSize (VariableName
);
2071 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
2075 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
2077 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
2081 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
2083 // There will be pad bytes after Data, the NextVariable->NameSize and
2084 // NextVariable->DataSize should not include pad size so that variable
2085 // service can get actual size in GetVariable.
2087 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
2088 NextVariable
->DataSize
= (UINT32
)DataSize
;
2091 // The actual size of the variable that stores in storage should
2092 // include pad size.
2094 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
2095 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2097 // Create a nonvolatile variable.
2101 IsCommonVariable
= FALSE
;
2102 IsCommonUserVariable
= FALSE
;
2103 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0) {
2104 IsCommonVariable
= TRUE
;
2105 IsCommonUserVariable
= IsUserVariable (NextVariable
);
2107 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2108 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
2109 || (IsCommonVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonVariableSpace
))
2110 || (IsCommonVariable
&& AtRuntime () && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonRuntimeVariableSpace
))
2111 || (IsCommonUserVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonUserVariableTotalSize
) > mVariableModuleGlobal
->CommonMaxUserVariableSpace
))) {
2113 if (IsCommonUserVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonUserVariableTotalSize
) > mVariableModuleGlobal
->CommonMaxUserVariableSpace
)) {
2114 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2116 if (IsCommonVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonRuntimeVariableSpace
)) {
2117 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2119 Status
= EFI_OUT_OF_RESOURCES
;
2123 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2125 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2126 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
, NextVariable
, HEADER_ALIGN (VarSize
));
2127 if (!EFI_ERROR (Status
)) {
2129 // The new variable has been integrated successfully during reclaiming.
2131 if (Variable
->CurrPtr
!= NULL
) {
2132 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2133 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2135 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
);
2136 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2138 if (IsCommonUserVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonUserVariableTotalSize
) > mVariableModuleGlobal
->CommonMaxUserVariableSpace
)) {
2139 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2141 if (IsCommonVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonVariableSpace
)) {
2142 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2149 // 1. Write variable header
2150 // 2. Set variable state to header valid
2151 // 3. Write variable data
2152 // 4. Set variable state to valid
2157 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
2158 Status
= UpdateVariableStore (
2159 &mVariableModuleGlobal
->VariableGlobal
,
2163 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2164 sizeof (VARIABLE_HEADER
),
2165 (UINT8
*) NextVariable
2168 if (EFI_ERROR (Status
)) {
2175 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
2176 Status
= UpdateVariableStore (
2177 &mVariableModuleGlobal
->VariableGlobal
,
2181 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2183 &NextVariable
->State
2186 if (EFI_ERROR (Status
)) {
2192 Status
= UpdateVariableStore (
2193 &mVariableModuleGlobal
->VariableGlobal
,
2197 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
2198 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
2199 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
2202 if (EFI_ERROR (Status
)) {
2208 NextVariable
->State
= VAR_ADDED
;
2209 Status
= UpdateVariableStore (
2210 &mVariableModuleGlobal
->VariableGlobal
,
2214 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2216 &NextVariable
->State
2219 if (EFI_ERROR (Status
)) {
2223 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2225 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2226 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2228 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2229 if (IsCommonUserVariable
) {
2230 mVariableModuleGlobal
->CommonUserVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2234 // update the memory copy of Flash region.
2236 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
2239 // Create a volatile variable.
2243 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2244 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
2246 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2248 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
2249 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
, NextVariable
, HEADER_ALIGN (VarSize
));
2250 if (!EFI_ERROR (Status
)) {
2252 // The new variable has been integrated successfully during reclaiming.
2254 if (Variable
->CurrPtr
!= NULL
) {
2255 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2256 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2258 UpdateVariableInfo (VariableName
, VendorGuid
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
);
2263 NextVariable
->State
= VAR_ADDED
;
2264 Status
= UpdateVariableStore (
2265 &mVariableModuleGlobal
->VariableGlobal
,
2269 mVariableModuleGlobal
->VolatileLastVariableOffset
,
2271 (UINT8
*) NextVariable
2274 if (EFI_ERROR (Status
)) {
2278 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2282 // Mark the old variable as deleted.
2284 if (!EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
2285 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2287 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
2288 // set IN_DELETED_TRANSITION one to DELETED state first.
2290 State
= Variable
->InDeletedTransitionPtr
->State
;
2291 State
&= VAR_DELETED
;
2292 Status
= UpdateVariableStore (
2293 &mVariableModuleGlobal
->VariableGlobal
,
2297 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2301 if (!EFI_ERROR (Status
)) {
2302 if (!Variable
->Volatile
) {
2303 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2304 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2311 State
= Variable
->CurrPtr
->State
;
2312 State
&= VAR_DELETED
;
2314 Status
= UpdateVariableStore (
2315 &mVariableModuleGlobal
->VariableGlobal
,
2319 (UINTN
) &Variable
->CurrPtr
->State
,
2323 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
2324 CacheVariable
->CurrPtr
->State
= State
;
2328 if (!EFI_ERROR (Status
)) {
2329 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2331 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2340 Check if a Unicode character is a hexadecimal character.
2342 This function checks if a Unicode character is a
2343 hexadecimal character. The valid hexadecimal character is
2344 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
2347 @param Char The character to check against.
2349 @retval TRUE If the Char is a hexadecmial character.
2350 @retval FALSE If the Char is not a hexadecmial character.
2355 IsHexaDecimalDigitCharacter (
2359 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
2364 This code checks if variable is hardware error record variable or not.
2366 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
2367 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
2369 @param VariableName Pointer to variable name.
2370 @param VendorGuid Variable Vendor Guid.
2372 @retval TRUE Variable is hardware error record variable.
2373 @retval FALSE Variable is not hardware error record variable.
2378 IsHwErrRecVariable (
2379 IN CHAR16
*VariableName
,
2380 IN EFI_GUID
*VendorGuid
2383 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
2384 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
2385 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
2386 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
2387 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
2388 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
2389 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
2397 Mark a variable that will become read-only after leaving the DXE phase of execution.
2399 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
2400 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
2401 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
2403 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
2404 as pending to be read-only.
2405 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
2406 Or VariableName is an empty string.
2407 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
2408 already been signaled.
2409 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
2413 VariableLockRequestToLock (
2414 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL
*This
,
2415 IN CHAR16
*VariableName
,
2416 IN EFI_GUID
*VendorGuid
2419 VARIABLE_ENTRY
*Entry
;
2422 VARIABLE_ENTRY
*LockedEntry
;
2424 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2425 return EFI_INVALID_PARAMETER
;
2429 return EFI_ACCESS_DENIED
;
2432 Entry
= AllocateRuntimeZeroPool (sizeof (*Entry
) + StrSize (VariableName
));
2433 if (Entry
== NULL
) {
2434 return EFI_OUT_OF_RESOURCES
;
2437 DEBUG ((EFI_D_INFO
, "[Variable] Lock: %g:%s\n", VendorGuid
, VariableName
));
2439 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2441 for ( Link
= GetFirstNode (&mLockedVariableList
)
2442 ; !IsNull (&mLockedVariableList
, Link
)
2443 ; Link
= GetNextNode (&mLockedVariableList
, Link
)
2445 LockedEntry
= BASE_CR (Link
, VARIABLE_ENTRY
, Link
);
2446 Name
= (CHAR16
*) ((UINTN
) LockedEntry
+ sizeof (*LockedEntry
));
2447 if (CompareGuid (&LockedEntry
->Guid
, VendorGuid
) && (StrCmp (Name
, VariableName
) == 0)) {
2452 Name
= (CHAR16
*) ((UINTN
) Entry
+ sizeof (*Entry
));
2453 StrnCpy (Name
, VariableName
, StrLen (VariableName
));
2454 CopyGuid (&Entry
->Guid
, VendorGuid
);
2455 InsertTailList (&mLockedVariableList
, &Entry
->Link
);
2458 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2465 This code finds variable in storage blocks (Volatile or Non-Volatile).
2467 Caution: This function may receive untrusted input.
2468 This function may be invoked in SMM mode, and datasize is external input.
2469 This function will do basic validation, before parse the data.
2471 @param VariableName Name of Variable to be found.
2472 @param VendorGuid Variable vendor GUID.
2473 @param Attributes Attribute value of the variable found.
2474 @param DataSize Size of Data found. If size is less than the
2475 data, this value contains the required size.
2476 @param Data Data pointer.
2478 @return EFI_INVALID_PARAMETER Invalid parameter.
2479 @return EFI_SUCCESS Find the specified variable.
2480 @return EFI_NOT_FOUND Not found.
2481 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2486 VariableServiceGetVariable (
2487 IN CHAR16
*VariableName
,
2488 IN EFI_GUID
*VendorGuid
,
2489 OUT UINT32
*Attributes OPTIONAL
,
2490 IN OUT UINTN
*DataSize
,
2495 VARIABLE_POINTER_TRACK Variable
;
2498 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2499 return EFI_INVALID_PARAMETER
;
2502 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2504 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2505 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2512 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2513 ASSERT (VarDataSize
!= 0);
2515 if (*DataSize
>= VarDataSize
) {
2517 Status
= EFI_INVALID_PARAMETER
;
2521 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2522 if (Attributes
!= NULL
) {
2523 *Attributes
= Variable
.CurrPtr
->Attributes
;
2526 *DataSize
= VarDataSize
;
2527 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2529 Status
= EFI_SUCCESS
;
2532 *DataSize
= VarDataSize
;
2533 Status
= EFI_BUFFER_TOO_SMALL
;
2538 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2546 This code Finds the Next available variable.
2548 Caution: This function may receive untrusted input.
2549 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2551 @param VariableNameSize Size of the variable name.
2552 @param VariableName Pointer to variable name.
2553 @param VendorGuid Variable Vendor Guid.
2555 @return EFI_INVALID_PARAMETER Invalid parameter.
2556 @return EFI_SUCCESS Find the specified variable.
2557 @return EFI_NOT_FOUND Not found.
2558 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2563 VariableServiceGetNextVariableName (
2564 IN OUT UINTN
*VariableNameSize
,
2565 IN OUT CHAR16
*VariableName
,
2566 IN OUT EFI_GUID
*VendorGuid
2569 VARIABLE_STORE_TYPE Type
;
2570 VARIABLE_POINTER_TRACK Variable
;
2571 VARIABLE_POINTER_TRACK VariableInHob
;
2572 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2575 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2577 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2578 return EFI_INVALID_PARAMETER
;
2581 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2583 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2584 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2588 if (VariableName
[0] != 0) {
2590 // If variable name is not NULL, get next variable.
2592 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2596 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2597 // The index and attributes mapping must be kept in this order as FindVariable
2598 // makes use of this mapping to implement search algorithm.
2600 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2601 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2602 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2606 // Switch from Volatile to HOB, to Non-Volatile.
2608 while (!IsValidVariableHeader (Variable
.CurrPtr
, Variable
.EndPtr
)) {
2610 // Find current storage index
2612 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2613 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2617 ASSERT (Type
< VariableStoreTypeMax
);
2619 // Switch to next storage
2621 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2622 if (VariableStoreHeader
[Type
] != NULL
) {
2627 // Capture the case that
2628 // 1. current storage is the last one, or
2629 // 2. no further storage
2631 if (Type
== VariableStoreTypeMax
) {
2632 Status
= EFI_NOT_FOUND
;
2635 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2636 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2637 Variable
.CurrPtr
= Variable
.StartPtr
;
2641 // Variable is found
2643 if (Variable
.CurrPtr
->State
== VAR_ADDED
|| Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2644 if (!AtRuntime () || ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
2645 if (Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2647 // If it is a IN_DELETED_TRANSITION variable,
2648 // and there is also a same ADDED one at the same time,
2651 VariablePtrTrack
.StartPtr
= Variable
.StartPtr
;
2652 VariablePtrTrack
.EndPtr
= Variable
.EndPtr
;
2653 Status
= FindVariableEx (
2654 GetVariableNamePtr (Variable
.CurrPtr
),
2655 &Variable
.CurrPtr
->VendorGuid
,
2659 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
== VAR_ADDED
) {
2660 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2666 // Don't return NV variable when HOB overrides it
2668 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2669 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2671 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2672 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2673 Status
= FindVariableEx (
2674 GetVariableNamePtr (Variable
.CurrPtr
),
2675 &Variable
.CurrPtr
->VendorGuid
,
2679 if (!EFI_ERROR (Status
)) {
2680 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2685 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2686 ASSERT (VarNameSize
!= 0);
2688 if (VarNameSize
<= *VariableNameSize
) {
2689 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2690 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2691 Status
= EFI_SUCCESS
;
2693 Status
= EFI_BUFFER_TOO_SMALL
;
2696 *VariableNameSize
= VarNameSize
;
2701 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2705 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2711 This code sets variable in storage blocks (Volatile or Non-Volatile).
2713 Caution: This function may receive untrusted input.
2714 This function may be invoked in SMM mode, and datasize and data are external input.
2715 This function will do basic validation, before parse the data.
2717 @param VariableName Name of Variable to be found.
2718 @param VendorGuid Variable vendor GUID.
2719 @param Attributes Attribute value of the variable found
2720 @param DataSize Size of Data found. If size is less than the
2721 data, this value contains the required size.
2722 @param Data Data pointer.
2724 @return EFI_INVALID_PARAMETER Invalid parameter.
2725 @return EFI_SUCCESS Set successfully.
2726 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2727 @return EFI_NOT_FOUND Not found.
2728 @return EFI_WRITE_PROTECTED Variable is read-only.
2733 VariableServiceSetVariable (
2734 IN CHAR16
*VariableName
,
2735 IN EFI_GUID
*VendorGuid
,
2736 IN UINT32 Attributes
,
2741 VARIABLE_POINTER_TRACK Variable
;
2743 VARIABLE_HEADER
*NextVariable
;
2744 EFI_PHYSICAL_ADDRESS Point
;
2746 VARIABLE_ENTRY
*Entry
;
2750 // Check input parameters.
2752 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2753 return EFI_INVALID_PARAMETER
;
2756 if (DataSize
!= 0 && Data
== NULL
) {
2757 return EFI_INVALID_PARAMETER
;
2761 // Not support authenticated or append variable write yet.
2763 if ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_APPEND_WRITE
)) != 0) {
2764 return EFI_INVALID_PARAMETER
;
2768 // Make sure if runtime bit is set, boot service bit is set also.
2770 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2771 return EFI_INVALID_PARAMETER
;
2774 if ((UINTN
)(~0) - DataSize
< StrSize(VariableName
)){
2776 // Prevent whole variable size overflow
2778 return EFI_INVALID_PARAMETER
;
2782 // The size of the VariableName, including the Unicode Null in bytes plus
2783 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2784 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2786 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2787 if ( StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
)) {
2788 return EFI_INVALID_PARAMETER
;
2790 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2791 return EFI_INVALID_PARAMETER
;
2795 // The size of the VariableName, including the Unicode Null in bytes plus
2796 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2798 if (StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
)) {
2799 return EFI_INVALID_PARAMETER
;
2803 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2806 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2808 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2809 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2811 // Parse non-volatile variable data and get last variable offset.
2813 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2814 while (IsValidVariableHeader (NextVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))) {
2815 NextVariable
= GetNextVariablePtr (NextVariable
);
2817 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2820 if (mEndOfDxe
&& mEnableLocking
) {
2822 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.
2824 for ( Link
= GetFirstNode (&mLockedVariableList
)
2825 ; !IsNull (&mLockedVariableList
, Link
)
2826 ; Link
= GetNextNode (&mLockedVariableList
, Link
)
2828 Entry
= BASE_CR (Link
, VARIABLE_ENTRY
, Link
);
2829 Name
= (CHAR16
*) ((UINTN
) Entry
+ sizeof (*Entry
));
2830 if (CompareGuid (&Entry
->Guid
, VendorGuid
) && (StrCmp (Name
, VariableName
) == 0)) {
2831 Status
= EFI_WRITE_PROTECTED
;
2832 DEBUG ((EFI_D_INFO
, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid
, VariableName
));
2838 Status
= InternalVarCheckSetVariableCheck (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
2839 if (EFI_ERROR (Status
)) {
2844 // Check whether the input variable is already existed.
2846 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2847 if (!EFI_ERROR (Status
)) {
2848 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2849 Status
= EFI_WRITE_PROTECTED
;
2852 if (Attributes
!= 0 && Attributes
!= Variable
.CurrPtr
->Attributes
) {
2854 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not
2855 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:
2856 // 1. No access attributes specified
2857 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE
2859 Status
= EFI_INVALID_PARAMETER
;
2860 DEBUG ((EFI_D_INFO
, "[Variable]: Rewritten a preexisting variable(0x%08x) with different attributes(0x%08x) - %g:%s\n", Variable
.CurrPtr
->Attributes
, Attributes
, VendorGuid
, VariableName
));
2865 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate
)) {
2867 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2869 Status
= AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2870 if (EFI_ERROR (Status
)) {
2872 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.
2878 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
2881 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2882 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2889 This code returns information about the EFI variables.
2891 Caution: This function may receive untrusted input.
2892 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2894 @param Attributes Attributes bitmask to specify the type of variables
2895 on which to return information.
2896 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2897 for the EFI variables associated with the attributes specified.
2898 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2899 for EFI variables associated with the attributes specified.
2900 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2901 associated with the attributes specified.
2903 @return EFI_SUCCESS Query successfully.
2908 VariableServiceQueryVariableInfoInternal (
2909 IN UINT32 Attributes
,
2910 OUT UINT64
*MaximumVariableStorageSize
,
2911 OUT UINT64
*RemainingVariableStorageSize
,
2912 OUT UINT64
*MaximumVariableSize
2915 VARIABLE_HEADER
*Variable
;
2916 VARIABLE_HEADER
*NextVariable
;
2917 UINT64 VariableSize
;
2918 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2919 UINT64 CommonVariableTotalSize
;
2920 UINT64 HwErrVariableTotalSize
;
2922 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2924 CommonVariableTotalSize
= 0;
2925 HwErrVariableTotalSize
= 0;
2927 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2929 // Query is Volatile related.
2931 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2934 // Query is Non-Volatile related.
2936 VariableStoreHeader
= mNvVariableCache
;
2940 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2941 // with the storage size (excluding the storage header size).
2943 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2946 // Harware error record variable needs larger size.
2948 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2949 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2950 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2952 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2954 *MaximumVariableStorageSize
= mVariableModuleGlobal
->CommonRuntimeVariableSpace
;
2956 *MaximumVariableStorageSize
= mVariableModuleGlobal
->CommonVariableSpace
;
2961 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2963 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2967 // Point to the starting address of the variables.
2969 Variable
= GetStartPointer (VariableStoreHeader
);
2972 // Now walk through the related variable store.
2974 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
2975 NextVariable
= GetNextVariablePtr (Variable
);
2976 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2980 // We don't take the state of the variables in mind
2981 // when calculating RemainingVariableStorageSize,
2982 // since the space occupied by variables not marked with
2983 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2985 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2986 HwErrVariableTotalSize
+= VariableSize
;
2988 CommonVariableTotalSize
+= VariableSize
;
2992 // Only care about Variables with State VAR_ADDED, because
2993 // the space not marked as VAR_ADDED is reclaimable now.
2995 if (Variable
->State
== VAR_ADDED
) {
2996 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2997 HwErrVariableTotalSize
+= VariableSize
;
2999 CommonVariableTotalSize
+= VariableSize
;
3001 } else if (Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
3003 // If it is a IN_DELETED_TRANSITION variable,
3004 // and there is not also a same ADDED one at the same time,
3005 // this IN_DELETED_TRANSITION variable is valid.
3007 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
3008 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
3009 Status
= FindVariableEx (
3010 GetVariableNamePtr (Variable
),
3011 &Variable
->VendorGuid
,
3015 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
!= VAR_ADDED
) {
3016 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3017 HwErrVariableTotalSize
+= VariableSize
;
3019 CommonVariableTotalSize
+= VariableSize
;
3026 // Go to the next one.
3028 Variable
= NextVariable
;
3031 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
3032 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
3034 if (*MaximumVariableStorageSize
< CommonVariableTotalSize
) {
3035 *RemainingVariableStorageSize
= 0;
3037 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
3041 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
3042 *MaximumVariableSize
= 0;
3043 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
3044 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
3052 This code returns information about the EFI variables.
3054 Caution: This function may receive untrusted input.
3055 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3057 @param Attributes Attributes bitmask to specify the type of variables
3058 on which to return information.
3059 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
3060 for the EFI variables associated with the attributes specified.
3061 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
3062 for EFI variables associated with the attributes specified.
3063 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
3064 associated with the attributes specified.
3066 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
3067 @return EFI_SUCCESS Query successfully.
3068 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
3073 VariableServiceQueryVariableInfo (
3074 IN UINT32 Attributes
,
3075 OUT UINT64
*MaximumVariableStorageSize
,
3076 OUT UINT64
*RemainingVariableStorageSize
,
3077 OUT UINT64
*MaximumVariableSize
3082 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
3083 return EFI_INVALID_PARAMETER
;
3086 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
3088 // Make sure the Attributes combination is supported by the platform.
3090 return EFI_UNSUPPORTED
;
3091 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
3093 // Make sure if runtime bit is set, boot service bit is set also.
3095 return EFI_INVALID_PARAMETER
;
3096 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
3098 // Make sure RT Attribute is set if we are in Runtime phase.
3100 return EFI_INVALID_PARAMETER
;
3101 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3103 // Make sure Hw Attribute is set with NV.
3105 return EFI_INVALID_PARAMETER
;
3106 } else if ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_APPEND_WRITE
)) != 0) {
3108 // Not support authenticated or append variable write yet.
3110 return EFI_UNSUPPORTED
;
3113 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3115 Status
= VariableServiceQueryVariableInfoInternal (
3117 MaximumVariableStorageSize
,
3118 RemainingVariableStorageSize
,
3122 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3127 This function reclaims variable storage if free size is below the threshold.
3129 Caution: This function may be invoked at SMM mode.
3130 Care must be taken to make sure not security issue.
3139 UINTN RemainingCommonRuntimeVariableSpace
;
3140 UINTN RemainingHwErrVariableSpace
;
3141 STATIC BOOLEAN Reclaimed
;
3144 // This function will be called only once at EndOfDxe or ReadyToBoot event.
3151 Status
= EFI_SUCCESS
;
3153 if (mVariableModuleGlobal
->CommonRuntimeVariableSpace
< mVariableModuleGlobal
->CommonVariableTotalSize
) {
3154 RemainingCommonRuntimeVariableSpace
= 0;
3156 RemainingCommonRuntimeVariableSpace
= mVariableModuleGlobal
->CommonRuntimeVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
3159 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
3161 // Check if the free area is below a threshold.
3163 if ((RemainingCommonRuntimeVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
3164 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
3165 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
3167 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3168 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3174 ASSERT_EFI_ERROR (Status
);
3179 Init non-volatile variable store.
3181 @retval EFI_SUCCESS Function successfully executed.
3182 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3183 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.
3187 InitNonVolatileVariableStore (
3191 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
3192 VARIABLE_HEADER
*Variable
;
3193 VARIABLE_HEADER
*NextVariable
;
3194 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3195 UINT64 VariableStoreLength
;
3197 EFI_HOB_GUID_TYPE
*GuidHob
;
3198 EFI_PHYSICAL_ADDRESS NvStorageBase
;
3199 UINT8
*NvStorageData
;
3200 UINT32 NvStorageSize
;
3201 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*FtwLastWriteData
;
3202 UINT32 BackUpOffset
;
3204 UINT32 HwErrStorageSize
;
3205 UINT32 MaxUserNvVariableSpaceSize
;
3206 UINT32 BoottimeReservedNvVariableSpaceSize
;
3208 mVariableModuleGlobal
->FvbInstance
= NULL
;
3211 // Allocate runtime memory used for a memory copy of the FLASH region.
3212 // Keep the memory and the FLASH in sync as updates occur.
3214 NvStorageSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
3215 NvStorageData
= AllocateRuntimeZeroPool (NvStorageSize
);
3216 if (NvStorageData
== NULL
) {
3217 return EFI_OUT_OF_RESOURCES
;
3220 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
3221 if (NvStorageBase
== 0) {
3222 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3225 // Copy NV storage data to the memory buffer.
3227 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) NvStorageBase
, NvStorageSize
);
3230 // Check the FTW last write data hob.
3232 GuidHob
= GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid
);
3233 if (GuidHob
!= NULL
) {
3234 FtwLastWriteData
= (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*) GET_GUID_HOB_DATA (GuidHob
);
3235 if (FtwLastWriteData
->TargetAddress
== NvStorageBase
) {
3236 DEBUG ((EFI_D_INFO
, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN
) FtwLastWriteData
->SpareAddress
));
3238 // Copy the backed up NV storage data to the memory buffer from spare block.
3240 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) (FtwLastWriteData
->SpareAddress
), NvStorageSize
);
3241 } else if ((FtwLastWriteData
->TargetAddress
> NvStorageBase
) &&
3242 (FtwLastWriteData
->TargetAddress
< (NvStorageBase
+ NvStorageSize
))) {
3244 // Flash NV storage from the offset is backed up in spare block.
3246 BackUpOffset
= (UINT32
) (FtwLastWriteData
->TargetAddress
- NvStorageBase
);
3247 BackUpSize
= NvStorageSize
- BackUpOffset
;
3248 DEBUG ((EFI_D_INFO
, "Variable: High partial NV storage from offset: %x is backed up in spare block: 0x%x\n", BackUpOffset
, (UINTN
) FtwLastWriteData
->SpareAddress
));
3250 // Copy the partial backed up NV storage data to the memory buffer from spare block.
3252 CopyMem (NvStorageData
+ BackUpOffset
, (UINT8
*) (UINTN
) FtwLastWriteData
->SpareAddress
, BackUpSize
);
3256 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) NvStorageData
;
3259 // Check if the Firmware Volume is not corrupted
3261 if ((FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) || (!CompareGuid (&gEfiSystemNvDataFvGuid
, &FvHeader
->FileSystemGuid
))) {
3262 FreePool (NvStorageData
);
3263 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
3264 return EFI_VOLUME_CORRUPTED
;
3267 VariableStoreBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) FvHeader
+ FvHeader
->HeaderLength
);
3268 VariableStoreLength
= (UINT64
) (NvStorageSize
- FvHeader
->HeaderLength
);
3270 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3271 mNvVariableCache
= (VARIABLE_STORE_HEADER
*) (UINTN
) VariableStoreBase
;
3272 if (GetVariableStoreStatus (mNvVariableCache
) != EfiValid
) {
3273 FreePool (NvStorageData
);
3274 DEBUG((EFI_D_ERROR
, "Variable Store header is corrupted\n"));
3275 return EFI_VOLUME_CORRUPTED
;
3277 ASSERT(mNvVariableCache
->Size
== VariableStoreLength
);
3280 ASSERT (sizeof (VARIABLE_STORE_HEADER
) <= VariableStoreLength
);
3282 HwErrStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
3283 MaxUserNvVariableSpaceSize
= PcdGet32 (PcdMaxUserNvVariableSpaceSize
);
3284 BoottimeReservedNvVariableSpaceSize
= PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize
);
3287 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
3288 // is stored with common variable in the same NV region. So the platform integrator should
3289 // ensure that the value of PcdHwErrStorageSize is less than the value of
3290 // VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).
3292 ASSERT (HwErrStorageSize
< (VariableStoreLength
- sizeof (VARIABLE_STORE_HEADER
)));
3294 // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of
3295 // VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).
3297 ASSERT (MaxUserNvVariableSpaceSize
< (VariableStoreLength
- sizeof (VARIABLE_STORE_HEADER
) - HwErrStorageSize
));
3299 // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of
3300 // VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).
3302 ASSERT (BoottimeReservedNvVariableSpaceSize
< (VariableStoreLength
- sizeof (VARIABLE_STORE_HEADER
) - HwErrStorageSize
));
3304 mVariableModuleGlobal
->CommonVariableSpace
= ((UINTN
) VariableStoreLength
- sizeof (VARIABLE_STORE_HEADER
) - HwErrStorageSize
);
3305 mVariableModuleGlobal
->CommonMaxUserVariableSpace
= ((MaxUserNvVariableSpaceSize
!= 0) ? MaxUserNvVariableSpaceSize
: mVariableModuleGlobal
->CommonVariableSpace
);
3306 mVariableModuleGlobal
->CommonRuntimeVariableSpace
= mVariableModuleGlobal
->CommonVariableSpace
- BoottimeReservedNvVariableSpaceSize
;
3308 DEBUG ((EFI_D_INFO
, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal
->CommonVariableSpace
, mVariableModuleGlobal
->CommonMaxUserVariableSpace
, mVariableModuleGlobal
->CommonRuntimeVariableSpace
));
3311 // The max variable or hardware error variable size should be < variable store size.
3313 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) < VariableStoreLength
);
3316 // Parse non-volatile variable data and get last variable offset.
3318 Variable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
3319 while (IsValidVariableHeader (Variable
, GetEndPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
))) {
3320 NextVariable
= GetNextVariablePtr (Variable
);
3321 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
3322 if ((Variable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3323 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
3325 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
3328 Variable
= NextVariable
;
3330 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) Variable
- (UINTN
) VariableStoreBase
;
3336 Flush the HOB variable to flash.
3338 @param[in] VariableName Name of variable has been updated or deleted.
3339 @param[in] VendorGuid Guid of variable has been updated or deleted.
3343 FlushHobVariableToFlash (
3344 IN CHAR16
*VariableName
,
3345 IN EFI_GUID
*VendorGuid
3349 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3350 VARIABLE_HEADER
*Variable
;
3357 // Flush the HOB variable to flash.
3359 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3360 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
3362 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
3364 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
3365 for ( Variable
= GetStartPointer (VariableStoreHeader
)
3366 ; IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))
3367 ; Variable
= GetNextVariablePtr (Variable
)
3369 if (Variable
->State
!= VAR_ADDED
) {
3371 // The HOB variable has been set to DELETED state in local.
3375 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
3376 if (VendorGuid
== NULL
|| VariableName
== NULL
||
3377 !CompareGuid (VendorGuid
, &Variable
->VendorGuid
) ||
3378 StrCmp (VariableName
, GetVariableNamePtr (Variable
)) != 0) {
3379 VariableData
= GetVariableDataPtr (Variable
);
3380 Status
= VariableServiceSetVariable (
3381 GetVariableNamePtr (Variable
),
3382 &Variable
->VendorGuid
,
3383 Variable
->Attributes
,
3387 DEBUG ((EFI_D_INFO
, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
), Status
));
3390 // The updated or deleted variable is matched with the HOB variable.
3391 // Don't break here because we will try to set other HOB variables
3392 // since this variable could be set successfully.
3394 Status
= EFI_SUCCESS
;
3396 if (!EFI_ERROR (Status
)) {
3398 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
3399 // set the HOB variable to DELETED state in local.
3401 DEBUG ((EFI_D_INFO
, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
)));
3402 Variable
->State
&= VAR_DELETED
;
3409 // We still have HOB variable(s) not flushed in flash.
3411 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
3414 // All HOB variables have been flushed in flash.
3416 DEBUG ((EFI_D_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
3417 if (!AtRuntime ()) {
3418 FreePool ((VOID
*) VariableStoreHeader
);
3426 Initializes variable write service after FTW was ready.
3428 @retval EFI_SUCCESS Function successfully executed.
3429 @retval Others Fail to initialize the variable service.
3433 VariableWriteServiceInitialize (
3438 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3441 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3442 EFI_PHYSICAL_ADDRESS NvStorageBase
;
3444 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
3445 if (NvStorageBase
== 0) {
3446 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3448 VariableStoreBase
= NvStorageBase
+ (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(NvStorageBase
))->HeaderLength
);
3451 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
3453 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3454 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
3457 // Check if the free area is really free.
3459 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
3460 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
3463 // There must be something wrong in variable store, do reclaim operation.
3466 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3467 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3473 if (EFI_ERROR (Status
)) {
3480 FlushHobVariableToFlash (NULL
, NULL
);
3487 Initializes variable store area for non-volatile and volatile variable.
3489 @retval EFI_SUCCESS Function successfully executed.
3490 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3494 VariableCommonInitialize (
3499 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
3500 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3501 UINT64 VariableStoreLength
;
3503 EFI_HOB_GUID_TYPE
*GuidHob
;
3506 // Allocate runtime memory for variable driver global structure.
3508 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
3509 if (mVariableModuleGlobal
== NULL
) {
3510 return EFI_OUT_OF_RESOURCES
;
3513 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
3516 // Get HOB variable store.
3518 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
3519 if (GuidHob
!= NULL
) {
3520 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
3521 VariableStoreLength
= (UINT64
) (GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
));
3522 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
3523 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
3524 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
3525 FreePool (mVariableModuleGlobal
);
3526 return EFI_OUT_OF_RESOURCES
;
3529 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
3534 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
3536 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
3537 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
3538 if (VolatileVariableStore
== NULL
) {
3539 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3540 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3542 FreePool (mVariableModuleGlobal
);
3543 return EFI_OUT_OF_RESOURCES
;
3546 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
3549 // Initialize Variable Specific Data.
3551 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
3552 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
3554 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiVariableGuid
);
3555 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
3556 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
3557 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
3558 VolatileVariableStore
->Reserved
= 0;
3559 VolatileVariableStore
->Reserved1
= 0;
3562 // Init non-volatile variable store.
3564 Status
= InitNonVolatileVariableStore ();
3565 if (EFI_ERROR (Status
)) {
3566 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3567 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3569 FreePool (mVariableModuleGlobal
);
3570 FreePool (VolatileVariableStore
);
3578 Get the proper fvb handle and/or fvb protocol by the given Flash address.
3580 @param[in] Address The Flash address.
3581 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
3582 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
3586 GetFvbInfoByAddress (
3587 IN EFI_PHYSICAL_ADDRESS Address
,
3588 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
3589 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
3593 EFI_HANDLE
*HandleBuffer
;
3596 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
3597 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
3598 EFI_FVB_ATTRIBUTES_2 Attributes
;
3600 UINTN NumberOfBlocks
;
3602 HandleBuffer
= NULL
;
3605 // Get all FVB handles.
3607 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
3608 if (EFI_ERROR (Status
)) {
3609 return EFI_NOT_FOUND
;
3613 // Get the FVB to access variable store.
3616 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
3617 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
3618 if (EFI_ERROR (Status
)) {
3619 Status
= EFI_NOT_FOUND
;
3624 // Ensure this FVB protocol supported Write operation.
3626 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
3627 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
3632 // Compare the address and select the right one.
3634 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
3635 if (EFI_ERROR (Status
)) {
3640 // Assume one FVB has one type of BlockSize.
3642 Status
= Fvb
->GetBlockSize (Fvb
, 0, &BlockSize
, &NumberOfBlocks
);
3643 if (EFI_ERROR (Status
)) {
3647 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ BlockSize
* NumberOfBlocks
))) {
3648 if (FvbHandle
!= NULL
) {
3649 *FvbHandle
= HandleBuffer
[Index
];
3651 if (FvbProtocol
!= NULL
) {
3654 Status
= EFI_SUCCESS
;
3658 FreePool (HandleBuffer
);
3661 Status
= EFI_NOT_FOUND
;