2 The common variable operation routines shared by DXE_RUNTIME variable
3 module and DXE_SMM variable module.
5 Caution: This module requires additional review when modified.
6 This driver will have external input - variable data. They may be input in SMM mode.
7 This external input must be validated carefully to avoid security issue like
8 buffer overflow, integer overflow.
10 VariableServiceGetNextVariableName () and VariableServiceQueryVariableInfo() are external API.
11 They need check input parameter.
13 VariableServiceGetVariable() and VariableServiceSetVariable() are external API
14 to receive datasize and data buffer. The size should be checked carefully.
16 VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,
17 integer overflow. It should also check attribute to avoid authentication bypass.
19 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
20 (C) Copyright 2015-2018 Hewlett Packard Enterprise Development LP<BR>
21 This program and the accompanying materials
22 are licensed and made available under the terms and conditions of the BSD License
23 which accompanies this distribution. The full text of the license may be found at
24 http://opensource.org/licenses/bsd-license.php
26 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
27 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
33 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
36 /// Define a memory cache that improves the search performance for a variable.
38 VARIABLE_STORE_HEADER
*mNvVariableCache
= NULL
;
41 /// Memory cache of Fv Header.
43 EFI_FIRMWARE_VOLUME_HEADER
*mNvFvHeaderCache
= NULL
;
46 /// The memory entry used for variable statistics data.
48 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
51 /// The flag to indicate whether the platform has left the DXE phase of execution.
53 BOOLEAN mEndOfDxe
= FALSE
;
56 /// It indicates the var check request source.
57 /// In the implementation, DXE is regarded as untrusted, and SMM is trusted.
59 VAR_CHECK_REQUEST_SOURCE mRequestSource
= VarCheckFromUntrusted
;
62 // It will record the current boot error flag before EndOfDxe.
64 VAR_ERROR_FLAG mCurrentBootVarErrFlag
= VAR_ERROR_FLAG_NO_ERROR
;
66 VARIABLE_ENTRY_PROPERTY mVariableEntryProperty
[] = {
68 &gEdkiiVarErrorFlagGuid
,
71 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
72 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
,
73 VARIABLE_ATTRIBUTE_NV_BS_RT
,
74 sizeof (VAR_ERROR_FLAG
),
75 sizeof (VAR_ERROR_FLAG
)
80 AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn
= {
81 AUTH_VAR_LIB_CONTEXT_IN_STRUCT_VERSION
,
83 // StructSize, TO BE FILLED
87 // MaxAuthVariableSize, TO BE FILLED
90 VariableExLibFindVariable
,
91 VariableExLibFindNextVariable
,
92 VariableExLibUpdateVariable
,
93 VariableExLibGetScratchBuffer
,
94 VariableExLibCheckRemainingSpaceForConsistency
,
95 VariableExLibAtRuntime
,
98 AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut
;
101 Routine used to track statistical information about variable usage.
102 The data is stored in the EFI system table so it can be accessed later.
103 VariableInfo.efi can dump out the table. Only Boot Services variable
104 accesses are tracked by this code. The PcdVariableCollectStatistics
105 build flag controls if this feature is enabled.
107 A read that hits in the cache will have Read and Cache true for
108 the transaction. Data is allocated by this routine, but never
111 @param[in] VariableName Name of the Variable to track.
112 @param[in] VendorGuid Guid of the Variable to track.
113 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
114 @param[in] Read TRUE if GetVariable() was called.
115 @param[in] Write TRUE if SetVariable() was called.
116 @param[in] Delete TRUE if deleted via SetVariable().
117 @param[in] Cache TRUE for a cache hit.
122 IN CHAR16
*VariableName
,
123 IN EFI_GUID
*VendorGuid
,
131 VARIABLE_INFO_ENTRY
*Entry
;
133 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
136 // Don't collect statistics at runtime.
140 if (gVariableInfo
== NULL
) {
142 // On the first call allocate a entry and place a pointer to it in
143 // the EFI System Table.
145 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
146 ASSERT (gVariableInfo
!= NULL
);
148 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
149 gVariableInfo
->Name
= AllocateZeroPool (StrSize (VariableName
));
150 ASSERT (gVariableInfo
->Name
!= NULL
);
151 StrCpyS (gVariableInfo
->Name
, StrSize(VariableName
)/sizeof(CHAR16
), VariableName
);
152 gVariableInfo
->Volatile
= Volatile
;
156 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
157 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
158 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
166 Entry
->DeleteCount
++;
176 if (Entry
->Next
== NULL
) {
178 // If the entry is not in the table add it.
179 // Next iteration of the loop will fill in the data.
181 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
182 ASSERT (Entry
->Next
!= NULL
);
184 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
185 Entry
->Next
->Name
= AllocateZeroPool (StrSize (VariableName
));
186 ASSERT (Entry
->Next
->Name
!= NULL
);
187 StrCpyS (Entry
->Next
->Name
, StrSize(VariableName
)/sizeof(CHAR16
), VariableName
);
188 Entry
->Next
->Volatile
= Volatile
;
198 This code checks if variable header is valid or not.
200 @param Variable Pointer to the Variable Header.
201 @param VariableStoreEnd Pointer to the Variable Store End.
203 @retval TRUE Variable header is valid.
204 @retval FALSE Variable header is not valid.
208 IsValidVariableHeader (
209 IN VARIABLE_HEADER
*Variable
,
210 IN VARIABLE_HEADER
*VariableStoreEnd
213 if ((Variable
== NULL
) || (Variable
>= VariableStoreEnd
) || (Variable
->StartId
!= VARIABLE_DATA
)) {
215 // Variable is NULL or has reached the end of variable store,
216 // or the StartId is not correct.
227 This function writes data to the FWH at the correct LBA even if the LBAs
230 @param Global Pointer to VARAIBLE_GLOBAL structure.
231 @param Volatile Point out the Variable is Volatile or Non-Volatile.
232 @param SetByIndex TRUE if target pointer is given as index.
233 FALSE if target pointer is absolute.
234 @param Fvb Pointer to the writable FVB protocol.
235 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
237 @param DataSize Size of data to be written.
238 @param Buffer Pointer to the buffer from which data is written.
240 @retval EFI_INVALID_PARAMETER Parameters not valid.
241 @retval EFI_UNSUPPORTED Fvb is a NULL for Non-Volatile variable update.
242 @retval EFI_OUT_OF_RESOURCES The remaining size is not enough.
243 @retval EFI_SUCCESS Variable store successfully updated.
247 UpdateVariableStore (
248 IN VARIABLE_GLOBAL
*Global
,
250 IN BOOLEAN SetByIndex
,
251 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
252 IN UINTN DataPtrIndex
,
257 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
265 VARIABLE_STORE_HEADER
*VolatileBase
;
266 EFI_PHYSICAL_ADDRESS FvVolHdr
;
267 EFI_PHYSICAL_ADDRESS DataPtr
;
271 DataPtr
= DataPtrIndex
;
274 // Check if the Data is Volatile.
278 return EFI_UNSUPPORTED
;
280 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
281 ASSERT_EFI_ERROR (Status
);
284 // Data Pointer should point to the actual Address where data is to be
288 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
291 if ((DataPtr
+ DataSize
) > (FvVolHdr
+ mNvFvHeaderCache
->FvLength
)) {
292 return EFI_OUT_OF_RESOURCES
;
296 // Data Pointer should point to the actual Address where data is to be
299 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
301 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
304 if ((DataPtr
+ DataSize
) > ((UINTN
) VolatileBase
+ VolatileBase
->Size
)) {
305 return EFI_OUT_OF_RESOURCES
;
309 // If Volatile Variable just do a simple mem copy.
311 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
316 // If we are here we are dealing with Non-Volatile Variables.
318 LinearOffset
= (UINTN
) FvVolHdr
;
319 CurrWritePtr
= (UINTN
) DataPtr
;
320 CurrWriteSize
= DataSize
;
324 if (CurrWritePtr
< LinearOffset
) {
325 return EFI_INVALID_PARAMETER
;
328 for (PtrBlockMapEntry
= mNvFvHeaderCache
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
329 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
331 // Check to see if the Variable Writes are spanning through multiple
334 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
335 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
336 Status
= Fvb
->Write (
339 (UINTN
) (CurrWritePtr
- LinearOffset
),
345 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
346 Status
= Fvb
->Write (
349 (UINTN
) (CurrWritePtr
- LinearOffset
),
353 if (EFI_ERROR (Status
)) {
357 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
358 CurrBuffer
= CurrBuffer
+ Size
;
359 CurrWriteSize
= CurrWriteSize
- Size
;
363 LinearOffset
+= PtrBlockMapEntry
->Length
;
374 This code gets the current status of Variable Store.
376 @param VarStoreHeader Pointer to the Variable Store Header.
378 @retval EfiRaw Variable store status is raw.
379 @retval EfiValid Variable store status is valid.
380 @retval EfiInvalid Variable store status is invalid.
383 VARIABLE_STORE_STATUS
384 GetVariableStoreStatus (
385 IN VARIABLE_STORE_HEADER
*VarStoreHeader
388 if ((CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) ||
389 CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
)) &&
390 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
391 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
395 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
396 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
397 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
398 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
399 VarStoreHeader
->Size
== 0xffffffff &&
400 VarStoreHeader
->Format
== 0xff &&
401 VarStoreHeader
->State
== 0xff
411 This code gets the size of variable header.
413 @return Size of variable header in bytes in type UINTN.
417 GetVariableHeaderSize (
423 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
424 Value
= sizeof (AUTHENTICATED_VARIABLE_HEADER
);
426 Value
= sizeof (VARIABLE_HEADER
);
434 This code gets the size of name of variable.
436 @param Variable Pointer to the Variable Header.
438 @return UINTN Size of variable in bytes.
443 IN VARIABLE_HEADER
*Variable
446 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
448 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
449 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
450 if (AuthVariable
->State
== (UINT8
) (-1) ||
451 AuthVariable
->DataSize
== (UINT32
) (-1) ||
452 AuthVariable
->NameSize
== (UINT32
) (-1) ||
453 AuthVariable
->Attributes
== (UINT32
) (-1)) {
456 return (UINTN
) AuthVariable
->NameSize
;
458 if (Variable
->State
== (UINT8
) (-1) ||
459 Variable
->DataSize
== (UINT32
) (-1) ||
460 Variable
->NameSize
== (UINT32
) (-1) ||
461 Variable
->Attributes
== (UINT32
) (-1)) {
464 return (UINTN
) Variable
->NameSize
;
469 This code sets the size of name of variable.
471 @param[in] Variable Pointer to the Variable Header.
472 @param[in] NameSize Name size to set.
476 SetNameSizeOfVariable (
477 IN VARIABLE_HEADER
*Variable
,
481 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
483 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
484 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
485 AuthVariable
->NameSize
= (UINT32
) NameSize
;
487 Variable
->NameSize
= (UINT32
) NameSize
;
493 This code gets the size of variable data.
495 @param Variable Pointer to the Variable Header.
497 @return Size of variable in bytes.
502 IN VARIABLE_HEADER
*Variable
505 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
507 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
508 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
509 if (AuthVariable
->State
== (UINT8
) (-1) ||
510 AuthVariable
->DataSize
== (UINT32
) (-1) ||
511 AuthVariable
->NameSize
== (UINT32
) (-1) ||
512 AuthVariable
->Attributes
== (UINT32
) (-1)) {
515 return (UINTN
) AuthVariable
->DataSize
;
517 if (Variable
->State
== (UINT8
) (-1) ||
518 Variable
->DataSize
== (UINT32
) (-1) ||
519 Variable
->NameSize
== (UINT32
) (-1) ||
520 Variable
->Attributes
== (UINT32
) (-1)) {
523 return (UINTN
) Variable
->DataSize
;
528 This code sets the size of variable data.
530 @param[in] Variable Pointer to the Variable Header.
531 @param[in] DataSize Data size to set.
535 SetDataSizeOfVariable (
536 IN VARIABLE_HEADER
*Variable
,
540 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
542 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
543 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
544 AuthVariable
->DataSize
= (UINT32
) DataSize
;
546 Variable
->DataSize
= (UINT32
) DataSize
;
552 This code gets the pointer to the variable name.
554 @param Variable Pointer to the Variable Header.
556 @return Pointer to Variable Name which is Unicode encoding.
561 IN VARIABLE_HEADER
*Variable
564 return (CHAR16
*) ((UINTN
) Variable
+ GetVariableHeaderSize ());
568 This code gets the pointer to the variable guid.
570 @param Variable Pointer to the Variable Header.
572 @return A EFI_GUID* pointer to Vendor Guid.
577 IN VARIABLE_HEADER
*Variable
580 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
582 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
583 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
584 return &AuthVariable
->VendorGuid
;
586 return &Variable
->VendorGuid
;
592 This code gets the pointer to the variable data.
594 @param Variable Pointer to the Variable Header.
596 @return Pointer to Variable Data.
601 IN VARIABLE_HEADER
*Variable
607 // Be careful about pad size for alignment.
609 Value
= (UINTN
) GetVariableNamePtr (Variable
);
610 Value
+= NameSizeOfVariable (Variable
);
611 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
613 return (UINT8
*) Value
;
617 This code gets the variable data offset related to variable header.
619 @param Variable Pointer to the Variable Header.
621 @return Variable Data offset.
625 GetVariableDataOffset (
626 IN VARIABLE_HEADER
*Variable
632 // Be careful about pad size for alignment
634 Value
= GetVariableHeaderSize ();
635 Value
+= NameSizeOfVariable (Variable
);
636 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
643 This code gets the pointer to the next variable header.
645 @param Variable Pointer to the Variable Header.
647 @return Pointer to next variable header.
652 IN VARIABLE_HEADER
*Variable
657 Value
= (UINTN
) GetVariableDataPtr (Variable
);
658 Value
+= DataSizeOfVariable (Variable
);
659 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
662 // Be careful about pad size for alignment.
664 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
669 Gets the pointer to the first variable header in given variable store area.
671 @param VarStoreHeader Pointer to the Variable Store Header.
673 @return Pointer to the first variable header.
678 IN VARIABLE_STORE_HEADER
*VarStoreHeader
682 // The start of variable store.
684 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
689 Gets the pointer to the end of the variable storage area.
691 This function gets pointer to the end of the variable storage
692 area, according to the input variable store header.
694 @param VarStoreHeader Pointer to the Variable Store Header.
696 @return Pointer to the end of the variable storage area.
701 IN VARIABLE_STORE_HEADER
*VarStoreHeader
705 // The end of variable store
707 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
711 Record variable error flag.
713 @param[in] Flag Variable error flag to record.
714 @param[in] VariableName Name of variable.
715 @param[in] VendorGuid Guid of variable.
716 @param[in] Attributes Attributes of the variable.
717 @param[in] VariableSize Size of the variable.
722 IN VAR_ERROR_FLAG Flag
,
723 IN CHAR16
*VariableName
,
724 IN EFI_GUID
*VendorGuid
,
725 IN UINT32 Attributes
,
726 IN UINTN VariableSize
730 VARIABLE_POINTER_TRACK Variable
;
731 VAR_ERROR_FLAG
*VarErrFlag
;
732 VAR_ERROR_FLAG TempFlag
;
735 DEBUG ((EFI_D_ERROR
, "RecordVarErrorFlag (0x%02x) %s:%g - 0x%08x - 0x%x\n", Flag
, VariableName
, VendorGuid
, Attributes
, VariableSize
));
736 if (Flag
== VAR_ERROR_FLAG_SYSTEM_ERROR
) {
738 DEBUG ((EFI_D_ERROR
, "CommonRuntimeVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal
->CommonRuntimeVariableSpace
, mVariableModuleGlobal
->CommonVariableTotalSize
));
740 DEBUG ((EFI_D_ERROR
, "CommonVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal
->CommonVariableSpace
, mVariableModuleGlobal
->CommonVariableTotalSize
));
743 DEBUG ((EFI_D_ERROR
, "CommonMaxUserVariableSpace = 0x%x - CommonUserVariableTotalSize = 0x%x\n", mVariableModuleGlobal
->CommonMaxUserVariableSpace
, mVariableModuleGlobal
->CommonUserVariableTotalSize
));
749 // Before EndOfDxe, just record the current boot variable error flag to local variable,
750 // and leave the variable error flag in NV flash as the last boot variable error flag.
751 // After EndOfDxe in InitializeVarErrorFlag (), the variable error flag in NV flash
752 // will be initialized to this local current boot variable error flag.
754 mCurrentBootVarErrFlag
&= Flag
;
759 // Record error flag (it should have be initialized).
761 Status
= FindVariable (
763 &gEdkiiVarErrorFlagGuid
,
765 &mVariableModuleGlobal
->VariableGlobal
,
768 if (!EFI_ERROR (Status
)) {
769 VarErrFlag
= (VAR_ERROR_FLAG
*) GetVariableDataPtr (Variable
.CurrPtr
);
770 TempFlag
= *VarErrFlag
;
772 if (TempFlag
== *VarErrFlag
) {
775 Status
= UpdateVariableStore (
776 &mVariableModuleGlobal
->VariableGlobal
,
779 mVariableModuleGlobal
->FvbInstance
,
780 (UINTN
) VarErrFlag
- (UINTN
) mNvVariableCache
+ (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
784 if (!EFI_ERROR (Status
)) {
786 // Update the data in NV cache.
788 *VarErrFlag
= TempFlag
;
794 Initialize variable error flag.
796 Before EndOfDxe, the variable indicates the last boot variable error flag,
797 then it means the last boot variable error flag must be got before EndOfDxe.
798 After EndOfDxe, the variable indicates the current boot variable error flag,
799 then it means the current boot variable error flag must be got after EndOfDxe.
803 InitializeVarErrorFlag (
808 VARIABLE_POINTER_TRACK Variable
;
810 VAR_ERROR_FLAG VarErrFlag
;
816 Flag
= mCurrentBootVarErrFlag
;
817 DEBUG ((EFI_D_INFO
, "Initialize variable error flag (%02x)\n", Flag
));
819 Status
= FindVariable (
821 &gEdkiiVarErrorFlagGuid
,
823 &mVariableModuleGlobal
->VariableGlobal
,
826 if (!EFI_ERROR (Status
)) {
827 VarErrFlag
= *((VAR_ERROR_FLAG
*) GetVariableDataPtr (Variable
.CurrPtr
));
828 if (VarErrFlag
== Flag
) {
835 &gEdkiiVarErrorFlagGuid
,
838 VARIABLE_ATTRIBUTE_NV_BS_RT
,
849 @param[in] Variable Pointer to variable header.
851 @retval TRUE User variable.
852 @retval FALSE System variable.
857 IN VARIABLE_HEADER
*Variable
860 VAR_CHECK_VARIABLE_PROPERTY Property
;
863 // Only after End Of Dxe, the variables belong to system variable are fixed.
864 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,
865 // then no need to check if the variable is user variable or not specially.
867 if (mEndOfDxe
&& (mVariableModuleGlobal
->CommonMaxUserVariableSpace
!= mVariableModuleGlobal
->CommonVariableSpace
)) {
868 if (VarCheckLibVariablePropertyGet (GetVariableNamePtr (Variable
), GetVendorGuidPtr (Variable
), &Property
) == EFI_NOT_FOUND
) {
876 Calculate common user variable total size.
880 CalculateCommonUserVariableTotalSize (
884 VARIABLE_HEADER
*Variable
;
885 VARIABLE_HEADER
*NextVariable
;
887 VAR_CHECK_VARIABLE_PROPERTY Property
;
890 // Only after End Of Dxe, the variables belong to system variable are fixed.
891 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,
892 // then no need to calculate the common user variable total size specially.
894 if (mEndOfDxe
&& (mVariableModuleGlobal
->CommonMaxUserVariableSpace
!= mVariableModuleGlobal
->CommonVariableSpace
)) {
895 Variable
= GetStartPointer (mNvVariableCache
);
896 while (IsValidVariableHeader (Variable
, GetEndPointer (mNvVariableCache
))) {
897 NextVariable
= GetNextVariablePtr (Variable
);
898 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
899 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
900 if (VarCheckLibVariablePropertyGet (GetVariableNamePtr (Variable
), GetVendorGuidPtr (Variable
), &Property
) == EFI_NOT_FOUND
) {
902 // No property, it is user variable.
904 mVariableModuleGlobal
->CommonUserVariableTotalSize
+= VariableSize
;
908 Variable
= NextVariable
;
914 Initialize variable quota.
918 InitializeVariableQuota (
926 InitializeVarErrorFlag ();
927 CalculateCommonUserVariableTotalSize ();
932 Variable store garbage collection and reclaim operation.
934 @param[in] VariableBase Base address of variable store.
935 @param[out] LastVariableOffset Offset of last variable.
936 @param[in] IsVolatile The variable store is volatile or not;
937 if it is non-volatile, need FTW.
938 @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.
939 @param[in] NewVariable Pointer to new variable.
940 @param[in] NewVariableSize New variable size.
942 @return EFI_SUCCESS Reclaim operation has finished successfully.
943 @return EFI_OUT_OF_RESOURCES No enough memory resources or variable space.
944 @return Others Unexpect error happened during reclaim operation.
949 IN EFI_PHYSICAL_ADDRESS VariableBase
,
950 OUT UINTN
*LastVariableOffset
,
951 IN BOOLEAN IsVolatile
,
952 IN OUT VARIABLE_POINTER_TRACK
*UpdatingPtrTrack
,
953 IN VARIABLE_HEADER
*NewVariable
,
954 IN UINTN NewVariableSize
957 VARIABLE_HEADER
*Variable
;
958 VARIABLE_HEADER
*AddedVariable
;
959 VARIABLE_HEADER
*NextVariable
;
960 VARIABLE_HEADER
*NextAddedVariable
;
961 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
963 UINTN MaximumBufferSize
;
971 UINTN CommonVariableTotalSize
;
972 UINTN CommonUserVariableTotalSize
;
973 UINTN HwErrVariableTotalSize
;
974 VARIABLE_HEADER
*UpdatingVariable
;
975 VARIABLE_HEADER
*UpdatingInDeletedTransition
;
977 UpdatingVariable
= NULL
;
978 UpdatingInDeletedTransition
= NULL
;
979 if (UpdatingPtrTrack
!= NULL
) {
980 UpdatingVariable
= UpdatingPtrTrack
->CurrPtr
;
981 UpdatingInDeletedTransition
= UpdatingPtrTrack
->InDeletedTransitionPtr
;
984 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
986 CommonVariableTotalSize
= 0;
987 CommonUserVariableTotalSize
= 0;
988 HwErrVariableTotalSize
= 0;
992 // Start Pointers for the variable.
994 Variable
= GetStartPointer (VariableStoreHeader
);
995 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
997 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
998 NextVariable
= GetNextVariablePtr (Variable
);
999 if ((Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) &&
1000 Variable
!= UpdatingVariable
&&
1001 Variable
!= UpdatingInDeletedTransition
1003 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
1004 MaximumBufferSize
+= VariableSize
;
1007 Variable
= NextVariable
;
1010 if (NewVariable
!= NULL
) {
1012 // Add the new variable size.
1014 MaximumBufferSize
+= NewVariableSize
;
1018 // Reserve the 1 Bytes with Oxff to identify the
1019 // end of the variable buffer.
1021 MaximumBufferSize
+= 1;
1022 ValidBuffer
= AllocatePool (MaximumBufferSize
);
1023 if (ValidBuffer
== NULL
) {
1024 return EFI_OUT_OF_RESOURCES
;
1028 // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache
1029 // as the buffer to reduce SMRAM consumption for SMM variable driver.
1031 MaximumBufferSize
= mNvVariableCache
->Size
;
1032 ValidBuffer
= (UINT8
*) mNvVariableCache
;
1035 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
1038 // Copy variable store header.
1040 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
1041 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
1044 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
1046 Variable
= GetStartPointer (VariableStoreHeader
);
1047 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
1048 NextVariable
= GetNextVariablePtr (Variable
);
1049 if (Variable
!= UpdatingVariable
&& Variable
->State
== VAR_ADDED
) {
1050 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
1051 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
1052 CurrPtr
+= VariableSize
;
1053 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1054 HwErrVariableTotalSize
+= VariableSize
;
1055 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1056 CommonVariableTotalSize
+= VariableSize
;
1057 if (IsUserVariable (Variable
)) {
1058 CommonUserVariableTotalSize
+= VariableSize
;
1062 Variable
= NextVariable
;
1066 // Reinstall all in delete transition variables.
1068 Variable
= GetStartPointer (VariableStoreHeader
);
1069 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
1070 NextVariable
= GetNextVariablePtr (Variable
);
1071 if (Variable
!= UpdatingVariable
&& Variable
!= UpdatingInDeletedTransition
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1074 // Buffer has cached all ADDED variable.
1075 // Per IN_DELETED variable, we have to guarantee that
1076 // no ADDED one in previous buffer.
1080 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
1081 while (IsValidVariableHeader (AddedVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
))) {
1082 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
1083 NameSize
= NameSizeOfVariable (AddedVariable
);
1084 if (CompareGuid (GetVendorGuidPtr (AddedVariable
), GetVendorGuidPtr (Variable
)) &&
1085 NameSize
== NameSizeOfVariable (Variable
)
1087 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
1088 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
1089 if (CompareMem (Point0
, Point1
, NameSize
) == 0) {
1094 AddedVariable
= NextAddedVariable
;
1098 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
1100 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
1101 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
1102 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
1103 CurrPtr
+= VariableSize
;
1104 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1105 HwErrVariableTotalSize
+= VariableSize
;
1106 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
1107 CommonVariableTotalSize
+= VariableSize
;
1108 if (IsUserVariable (Variable
)) {
1109 CommonUserVariableTotalSize
+= VariableSize
;
1115 Variable
= NextVariable
;
1119 // Install the new variable if it is not NULL.
1121 if (NewVariable
!= NULL
) {
1122 if (((UINTN
) CurrPtr
- (UINTN
) ValidBuffer
) + NewVariableSize
> VariableStoreHeader
->Size
) {
1124 // No enough space to store the new variable.
1126 Status
= EFI_OUT_OF_RESOURCES
;
1130 if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1131 HwErrVariableTotalSize
+= NewVariableSize
;
1132 } else if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1133 CommonVariableTotalSize
+= NewVariableSize
;
1134 if (IsUserVariable (NewVariable
)) {
1135 CommonUserVariableTotalSize
+= NewVariableSize
;
1138 if ((HwErrVariableTotalSize
> PcdGet32 (PcdHwErrStorageSize
)) ||
1139 (CommonVariableTotalSize
> mVariableModuleGlobal
->CommonVariableSpace
) ||
1140 (CommonUserVariableTotalSize
> mVariableModuleGlobal
->CommonMaxUserVariableSpace
)) {
1142 // No enough space to store the new variable by NV or NV+HR attribute.
1144 Status
= EFI_OUT_OF_RESOURCES
;
1149 CopyMem (CurrPtr
, (UINT8
*) NewVariable
, NewVariableSize
);
1150 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
1151 if (UpdatingVariable
!= NULL
) {
1152 UpdatingPtrTrack
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)UpdatingPtrTrack
->StartPtr
+ ((UINTN
)CurrPtr
- (UINTN
)GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
)));
1153 UpdatingPtrTrack
->InDeletedTransitionPtr
= NULL
;
1155 CurrPtr
+= NewVariableSize
;
1160 // If volatile variable store, just copy valid buffer.
1162 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
1163 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) CurrPtr
- (UINTN
) ValidBuffer
);
1164 *LastVariableOffset
= (UINTN
) CurrPtr
- (UINTN
) ValidBuffer
;
1165 Status
= EFI_SUCCESS
;
1168 // If non-volatile variable store, perform FTW here.
1170 Status
= FtwVariableSpace (
1172 (VARIABLE_STORE_HEADER
*) ValidBuffer
1174 if (!EFI_ERROR (Status
)) {
1175 *LastVariableOffset
= (UINTN
) CurrPtr
- (UINTN
) ValidBuffer
;
1176 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
1177 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
1178 mVariableModuleGlobal
->CommonUserVariableTotalSize
= CommonUserVariableTotalSize
;
1180 mVariableModuleGlobal
->HwErrVariableTotalSize
= 0;
1181 mVariableModuleGlobal
->CommonVariableTotalSize
= 0;
1182 mVariableModuleGlobal
->CommonUserVariableTotalSize
= 0;
1183 Variable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
1184 while (IsValidVariableHeader (Variable
, GetEndPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
))) {
1185 NextVariable
= GetNextVariablePtr (Variable
);
1186 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
1187 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1188 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
1189 } else if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1190 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
1191 if (IsUserVariable (Variable
)) {
1192 mVariableModuleGlobal
->CommonUserVariableTotalSize
+= VariableSize
;
1196 Variable
= NextVariable
;
1198 *LastVariableOffset
= (UINTN
) Variable
- (UINTN
) VariableBase
;
1204 FreePool (ValidBuffer
);
1207 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.
1209 CopyMem (mNvVariableCache
, (UINT8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
1216 Find the variable in the specified variable store.
1218 @param[in] VariableName Name of the variable to be found
1219 @param[in] VendorGuid Vendor GUID to be found.
1220 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
1221 check at runtime when searching variable.
1222 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.
1224 @retval EFI_SUCCESS Variable found successfully
1225 @retval EFI_NOT_FOUND Variable not found
1229 IN CHAR16
*VariableName
,
1230 IN EFI_GUID
*VendorGuid
,
1231 IN BOOLEAN IgnoreRtCheck
,
1232 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
1235 VARIABLE_HEADER
*InDeletedVariable
;
1238 PtrTrack
->InDeletedTransitionPtr
= NULL
;
1241 // Find the variable by walk through HOB, volatile and non-volatile variable store.
1243 InDeletedVariable
= NULL
;
1245 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
1246 ; IsValidVariableHeader (PtrTrack
->CurrPtr
, PtrTrack
->EndPtr
)
1247 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
1249 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
1250 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
1252 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
1253 if (VariableName
[0] == 0) {
1254 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1255 InDeletedVariable
= PtrTrack
->CurrPtr
;
1257 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
1261 if (CompareGuid (VendorGuid
, GetVendorGuidPtr (PtrTrack
->CurrPtr
))) {
1262 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
1264 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
1265 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
1266 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1267 InDeletedVariable
= PtrTrack
->CurrPtr
;
1269 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
1279 PtrTrack
->CurrPtr
= InDeletedVariable
;
1280 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
1285 Finds variable in storage blocks of volatile and non-volatile storage areas.
1287 This code finds variable in storage blocks of volatile and non-volatile storage areas.
1288 If VariableName is an empty string, then we just return the first
1289 qualified variable without comparing VariableName and VendorGuid.
1290 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
1291 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
1292 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
1294 @param[in] VariableName Name of the variable to be found.
1295 @param[in] VendorGuid Vendor GUID to be found.
1296 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
1297 including the range searched and the target position.
1298 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
1299 base of volatile variable storage area, base of
1300 NV variable storage area, and a lock.
1301 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
1302 check at runtime when searching variable.
1304 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
1306 @retval EFI_SUCCESS Variable successfully found.
1307 @retval EFI_NOT_FOUND Variable not found
1312 IN CHAR16
*VariableName
,
1313 IN EFI_GUID
*VendorGuid
,
1314 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
1315 IN VARIABLE_GLOBAL
*Global
,
1316 IN BOOLEAN IgnoreRtCheck
1320 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1321 VARIABLE_STORE_TYPE Type
;
1323 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
1324 return EFI_INVALID_PARAMETER
;
1328 // 0: Volatile, 1: HOB, 2: Non-Volatile.
1329 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
1330 // make use of this mapping to implement search algorithm.
1332 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
1333 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
1334 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
1337 // Find the variable by walk through HOB, volatile and non-volatile variable store.
1339 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1340 if (VariableStoreHeader
[Type
] == NULL
) {
1344 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
1345 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
1346 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
1348 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
1349 if (!EFI_ERROR (Status
)) {
1353 return EFI_NOT_FOUND
;
1357 Get index from supported language codes according to language string.
1359 This code is used to get corresponding index in supported language codes. It can handle
1360 RFC4646 and ISO639 language tags.
1361 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
1362 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
1365 SupportedLang = "engfraengfra"
1367 Iso639Language = TRUE
1368 The return value is "0".
1370 SupportedLang = "en;fr;en-US;fr-FR"
1372 Iso639Language = FALSE
1373 The return value is "3".
1375 @param SupportedLang Platform supported language codes.
1376 @param Lang Configured language.
1377 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1379 @retval The index of language in the language codes.
1383 GetIndexFromSupportedLangCodes(
1384 IN CHAR8
*SupportedLang
,
1386 IN BOOLEAN Iso639Language
1390 UINTN CompareLength
;
1391 UINTN LanguageLength
;
1393 if (Iso639Language
) {
1394 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1395 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
1396 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
1398 // Successfully find the index of Lang string in SupportedLang string.
1400 Index
= Index
/ CompareLength
;
1408 // Compare RFC4646 language code
1411 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
1413 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
1415 // Skip ';' characters in SupportedLang
1417 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
1419 // Determine the length of the next language code in SupportedLang
1421 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
1423 if ((CompareLength
== LanguageLength
) &&
1424 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
1426 // Successfully find the index of Lang string in SupportedLang string.
1437 Get language string from supported language codes according to index.
1439 This code is used to get corresponding language strings in supported language codes. It can handle
1440 RFC4646 and ISO639 language tags.
1441 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
1442 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
1445 SupportedLang = "engfraengfra"
1447 Iso639Language = TRUE
1448 The return value is "fra".
1450 SupportedLang = "en;fr;en-US;fr-FR"
1452 Iso639Language = FALSE
1453 The return value is "fr".
1455 @param SupportedLang Platform supported language codes.
1456 @param Index The index in supported language codes.
1457 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1459 @retval The language string in the language codes.
1463 GetLangFromSupportedLangCodes (
1464 IN CHAR8
*SupportedLang
,
1466 IN BOOLEAN Iso639Language
1470 UINTN CompareLength
;
1474 Supported
= SupportedLang
;
1475 if (Iso639Language
) {
1477 // According to the index of Lang string in SupportedLang string to get the language.
1478 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1479 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1481 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1482 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1483 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1488 // Take semicolon as delimitation, sequentially traverse supported language codes.
1490 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1493 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1495 // Have completed the traverse, but not find corrsponding string.
1496 // This case is not allowed to happen.
1501 if (SubIndex
== Index
) {
1503 // According to the index of Lang string in SupportedLang string to get the language.
1504 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1505 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1507 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1508 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1513 // Skip ';' characters in Supported
1515 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1521 Returns a pointer to an allocated buffer that contains the best matching language
1522 from a set of supported languages.
1524 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1525 code types may not be mixed in a single call to this function. This function
1526 supports a variable argument list that allows the caller to pass in a prioritized
1527 list of language codes to test against all the language codes in SupportedLanguages.
1529 If SupportedLanguages is NULL, then ASSERT().
1531 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1532 contains a set of language codes in the format
1533 specified by Iso639Language.
1534 @param[in] Iso639Language If not zero, then all language codes are assumed to be
1535 in ISO 639-2 format. If zero, then all language
1536 codes are assumed to be in RFC 4646 language format
1537 @param[in] ... A variable argument list that contains pointers to
1538 Null-terminated ASCII strings that contain one or more
1539 language codes in the format specified by Iso639Language.
1540 The first language code from each of these language
1541 code lists is used to determine if it is an exact or
1542 close match to any of the language codes in
1543 SupportedLanguages. Close matches only apply to RFC 4646
1544 language codes, and the matching algorithm from RFC 4647
1545 is used to determine if a close match is present. If
1546 an exact or close match is found, then the matching
1547 language code from SupportedLanguages is returned. If
1548 no matches are found, then the next variable argument
1549 parameter is evaluated. The variable argument list
1550 is terminated by a NULL.
1552 @retval NULL The best matching language could not be found in SupportedLanguages.
1553 @retval NULL There are not enough resources available to return the best matching
1555 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1556 language in SupportedLanguages.
1561 VariableGetBestLanguage (
1562 IN CONST CHAR8
*SupportedLanguages
,
1563 IN UINTN Iso639Language
,
1569 UINTN CompareLength
;
1570 UINTN LanguageLength
;
1571 CONST CHAR8
*Supported
;
1574 if (SupportedLanguages
== NULL
) {
1578 VA_START (Args
, Iso639Language
);
1579 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1581 // Default to ISO 639-2 mode
1584 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1587 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1589 if (Iso639Language
== 0) {
1590 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1594 // Trim back the length of Language used until it is empty
1596 while (LanguageLength
> 0) {
1598 // Loop through all language codes in SupportedLanguages
1600 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1602 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1604 if (Iso639Language
== 0) {
1606 // Skip ';' characters in Supported
1608 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1610 // Determine the length of the next language code in Supported
1612 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1614 // If Language is longer than the Supported, then skip to the next language
1616 if (LanguageLength
> CompareLength
) {
1621 // See if the first LanguageLength characters in Supported match Language
1623 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1626 Buffer
= (Iso639Language
!= 0) ? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1627 Buffer
[CompareLength
] = '\0';
1628 return CopyMem (Buffer
, Supported
, CompareLength
);
1632 if (Iso639Language
!= 0) {
1634 // If ISO 639 mode, then each language can only be tested once
1639 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1641 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1648 // No matches were found
1654 This function is to check if the remaining variable space is enough to set
1655 all Variables from argument list successfully. The purpose of the check
1656 is to keep the consistency of the Variables to be in variable storage.
1658 Note: Variables are assumed to be in same storage.
1659 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1660 so follow the argument sequence to check the Variables.
1662 @param[in] Attributes Variable attributes for Variable entries.
1663 @param[in] Marker VA_LIST style variable argument list.
1664 The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1665 A NULL terminates the list. The VariableSize of
1666 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
1667 It will be changed to variable total size as output.
1669 @retval TRUE Have enough variable space to set the Variables successfully.
1670 @retval FALSE No enough variable space to set the Variables successfully.
1675 CheckRemainingSpaceForConsistencyInternal (
1676 IN UINT32 Attributes
,
1682 VARIABLE_ENTRY_CONSISTENCY
*VariableEntry
;
1683 UINT64 MaximumVariableStorageSize
;
1684 UINT64 RemainingVariableStorageSize
;
1685 UINT64 MaximumVariableSize
;
1686 UINTN TotalNeededSize
;
1687 UINTN OriginalVarSize
;
1688 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1689 VARIABLE_POINTER_TRACK VariablePtrTrack
;
1690 VARIABLE_HEADER
*NextVariable
;
1695 // Non-Volatile related.
1697 VariableStoreHeader
= mNvVariableCache
;
1699 Status
= VariableServiceQueryVariableInfoInternal (
1701 &MaximumVariableStorageSize
,
1702 &RemainingVariableStorageSize
,
1703 &MaximumVariableSize
1705 ASSERT_EFI_ERROR (Status
);
1707 TotalNeededSize
= 0;
1708 VA_COPY (Args
, Marker
);
1709 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1710 while (VariableEntry
!= NULL
) {
1712 // Calculate variable total size.
1714 VarNameSize
= StrSize (VariableEntry
->Name
);
1715 VarNameSize
+= GET_PAD_SIZE (VarNameSize
);
1716 VarDataSize
= VariableEntry
->VariableSize
;
1717 VarDataSize
+= GET_PAD_SIZE (VarDataSize
);
1718 VariableEntry
->VariableSize
= HEADER_ALIGN (GetVariableHeaderSize () + VarNameSize
+ VarDataSize
);
1720 TotalNeededSize
+= VariableEntry
->VariableSize
;
1721 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1725 if (RemainingVariableStorageSize
>= TotalNeededSize
) {
1727 // Already have enough space.
1730 } else if (AtRuntime ()) {
1732 // At runtime, no reclaim.
1733 // The original variable space of Variables can't be reused.
1738 VA_COPY (Args
, Marker
);
1739 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1740 while (VariableEntry
!= NULL
) {
1742 // Check if Variable[Index] has been present and get its size.
1744 OriginalVarSize
= 0;
1745 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
1746 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
1747 Status
= FindVariableEx (
1748 VariableEntry
->Name
,
1749 VariableEntry
->Guid
,
1753 if (!EFI_ERROR (Status
)) {
1755 // Get size of Variable[Index].
1757 NextVariable
= GetNextVariablePtr (VariablePtrTrack
.CurrPtr
);
1758 OriginalVarSize
= (UINTN
) NextVariable
- (UINTN
) VariablePtrTrack
.CurrPtr
;
1760 // Add the original size of Variable[Index] to remaining variable storage size.
1762 RemainingVariableStorageSize
+= OriginalVarSize
;
1764 if (VariableEntry
->VariableSize
> RemainingVariableStorageSize
) {
1766 // No enough space for Variable[Index].
1772 // Sub the (new) size of Variable[Index] from remaining variable storage size.
1774 RemainingVariableStorageSize
-= VariableEntry
->VariableSize
;
1775 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1783 This function is to check if the remaining variable space is enough to set
1784 all Variables from argument list successfully. The purpose of the check
1785 is to keep the consistency of the Variables to be in variable storage.
1787 Note: Variables are assumed to be in same storage.
1788 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1789 so follow the argument sequence to check the Variables.
1791 @param[in] Attributes Variable attributes for Variable entries.
1792 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1793 A NULL terminates the list. The VariableSize of
1794 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
1795 It will be changed to variable total size as output.
1797 @retval TRUE Have enough variable space to set the Variables successfully.
1798 @retval FALSE No enough variable space to set the Variables successfully.
1803 CheckRemainingSpaceForConsistency (
1804 IN UINT32 Attributes
,
1811 VA_START (Marker
, Attributes
);
1813 Return
= CheckRemainingSpaceForConsistencyInternal (Attributes
, Marker
);
1821 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1823 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1825 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1826 and are read-only. Therefore, in variable driver, only store the original value for other use.
1828 @param[in] VariableName Name of variable.
1830 @param[in] Data Variable data.
1832 @param[in] DataSize Size of data. 0 means delete.
1834 @retval EFI_SUCCESS The update operation is successful or ignored.
1835 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.
1836 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.
1837 @retval Others Other errors happened during the update operation.
1841 AutoUpdateLangVariable (
1842 IN CHAR16
*VariableName
,
1848 CHAR8
*BestPlatformLang
;
1852 VARIABLE_POINTER_TRACK Variable
;
1853 BOOLEAN SetLanguageCodes
;
1854 VARIABLE_ENTRY_CONSISTENCY VariableEntry
[2];
1857 // Don't do updates for delete operation
1859 if (DataSize
== 0) {
1863 SetLanguageCodes
= FALSE
;
1865 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
) == 0) {
1867 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1870 return EFI_WRITE_PROTECTED
;
1873 SetLanguageCodes
= TRUE
;
1876 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1877 // Therefore, in variable driver, only store the original value for other use.
1879 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1880 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1882 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1883 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1886 // PlatformLang holds a single language from PlatformLangCodes,
1887 // so the size of PlatformLangCodes is enough for the PlatformLang.
1889 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1890 FreePool (mVariableModuleGlobal
->PlatformLang
);
1892 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1893 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1895 } else if (StrCmp (VariableName
, EFI_LANG_CODES_VARIABLE_NAME
) == 0) {
1897 // LangCodes is a volatile variable, so it can not be updated at runtime.
1900 return EFI_WRITE_PROTECTED
;
1903 SetLanguageCodes
= TRUE
;
1906 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1907 // Therefore, in variable driver, only store the original value for other use.
1909 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1910 FreePool (mVariableModuleGlobal
->LangCodes
);
1912 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1913 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1916 if (SetLanguageCodes
1917 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1918 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1920 // Update Lang if PlatformLang is already set
1921 // Update PlatformLang if Lang is already set
1923 Status
= FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1924 if (!EFI_ERROR (Status
)) {
1928 VariableName
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1929 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1930 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1932 Status
= FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1933 if (!EFI_ERROR (Status
)) {
1935 // Update PlatformLang
1937 VariableName
= EFI_LANG_VARIABLE_NAME
;
1938 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1939 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1942 // Neither PlatformLang nor Lang is set, directly return
1949 Status
= EFI_SUCCESS
;
1952 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1954 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1956 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_VARIABLE_NAME
) == 0) {
1958 // Update Lang when PlatformLangCodes/LangCodes were set.
1960 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1962 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1964 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1965 if (BestPlatformLang
!= NULL
) {
1967 // Get the corresponding index in language codes.
1969 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1972 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1974 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1977 // Check the variable space for both Lang and PlatformLang variable.
1979 VariableEntry
[0].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1980 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1981 VariableEntry
[0].Name
= EFI_LANG_VARIABLE_NAME
;
1983 VariableEntry
[1].VariableSize
= AsciiStrSize (BestPlatformLang
);
1984 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1985 VariableEntry
[1].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1986 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1988 // No enough variable space to set both Lang and PlatformLang successfully.
1990 Status
= EFI_OUT_OF_RESOURCES
;
1993 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1995 FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1997 Status
= UpdateVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestLang
,
1998 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, 0, 0, &Variable
, NULL
);
2001 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang
, BestLang
, Status
));
2005 } else if (StrCmp (VariableName
, EFI_LANG_VARIABLE_NAME
) == 0) {
2007 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
2009 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
2011 // When setting Lang, firstly get most matched language string from supported language codes.
2013 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
2014 if (BestLang
!= NULL
) {
2016 // Get the corresponding index in language codes.
2018 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
2021 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
2023 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
2026 // Check the variable space for both PlatformLang and Lang variable.
2028 VariableEntry
[0].VariableSize
= AsciiStrSize (BestPlatformLang
);
2029 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
2030 VariableEntry
[0].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
2032 VariableEntry
[1].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
2033 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
2034 VariableEntry
[1].Name
= EFI_LANG_VARIABLE_NAME
;
2035 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
2037 // No enough variable space to set both PlatformLang and Lang successfully.
2039 Status
= EFI_OUT_OF_RESOURCES
;
2042 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
2044 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2046 Status
= UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestPlatformLang
,
2047 AsciiStrSize (BestPlatformLang
), Attributes
, 0, 0, &Variable
, NULL
);
2050 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang
, BestPlatformLang
, Status
));
2055 if (SetLanguageCodes
) {
2057 // Continue to set PlatformLangCodes or LangCodes.
2066 Compare two EFI_TIME data.
2069 @param FirstTime A pointer to the first EFI_TIME data.
2070 @param SecondTime A pointer to the second EFI_TIME data.
2072 @retval TRUE The FirstTime is not later than the SecondTime.
2073 @retval FALSE The FirstTime is later than the SecondTime.
2077 VariableCompareTimeStampInternal (
2078 IN EFI_TIME
*FirstTime
,
2079 IN EFI_TIME
*SecondTime
2082 if (FirstTime
->Year
!= SecondTime
->Year
) {
2083 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
2084 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
2085 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
2086 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
2087 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
2088 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
2089 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
2090 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
2091 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
2094 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
2098 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
2099 index of associated public key is needed.
2101 @param[in] VariableName Name of variable.
2102 @param[in] VendorGuid Guid of variable.
2103 @param[in] Data Variable data.
2104 @param[in] DataSize Size of data. 0 means delete.
2105 @param[in] Attributes Attributes of the variable.
2106 @param[in] KeyIndex Index of associated public key.
2107 @param[in] MonotonicCount Value of associated monotonic count.
2108 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.
2109 @param[in] TimeStamp Value of associated TimeStamp.
2111 @retval EFI_SUCCESS The update operation is success.
2112 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
2117 IN CHAR16
*VariableName
,
2118 IN EFI_GUID
*VendorGuid
,
2121 IN UINT32 Attributes OPTIONAL
,
2122 IN UINT32 KeyIndex OPTIONAL
,
2123 IN UINT64 MonotonicCount OPTIONAL
,
2124 IN OUT VARIABLE_POINTER_TRACK
*CacheVariable
,
2125 IN EFI_TIME
*TimeStamp OPTIONAL
2129 VARIABLE_HEADER
*NextVariable
;
2132 UINTN VarNameOffset
;
2133 UINTN VarDataOffset
;
2137 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2139 VARIABLE_POINTER_TRACK
*Variable
;
2140 VARIABLE_POINTER_TRACK NvVariable
;
2141 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2142 UINT8
*BufferForMerge
;
2143 UINTN MergedBufSize
;
2146 BOOLEAN IsCommonVariable
;
2147 BOOLEAN IsCommonUserVariable
;
2148 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
2150 if (mVariableModuleGlobal
->FvbInstance
== NULL
) {
2152 // The FVB protocol is not ready, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
2154 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2156 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
2158 DEBUG ((EFI_D_ERROR
, "Update NV variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET
));
2159 return EFI_NOT_AVAILABLE_YET
;
2160 } else if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
2162 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
2163 // The authenticated variable perhaps is not initialized, just return here.
2165 DEBUG ((EFI_D_ERROR
, "Update AUTH variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET
));
2166 return EFI_NOT_AVAILABLE_YET
;
2171 // Check if CacheVariable points to the variable in variable HOB.
2172 // If yes, let CacheVariable points to the variable in NV variable cache.
2174 if ((CacheVariable
->CurrPtr
!= NULL
) &&
2175 (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) &&
2176 (CacheVariable
->StartPtr
== GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
))
2178 CacheVariable
->StartPtr
= GetStartPointer (mNvVariableCache
);
2179 CacheVariable
->EndPtr
= GetEndPointer (mNvVariableCache
);
2180 CacheVariable
->Volatile
= FALSE
;
2181 Status
= FindVariableEx (VariableName
, VendorGuid
, FALSE
, CacheVariable
);
2182 if (CacheVariable
->CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2184 // There is no matched variable in NV variable cache.
2186 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0)) || (Attributes
== 0)) {
2188 // It is to delete variable,
2189 // go to delete this variable in variable HOB and
2190 // try to flush other variables from HOB to flash.
2192 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
);
2193 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2199 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
2200 Variable
= CacheVariable
;
2203 // Update/Delete existing NV variable.
2204 // CacheVariable points to the variable in the memory copy of Flash area
2205 // Now let Variable points to the same variable in Flash area.
2207 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
2208 Variable
= &NvVariable
;
2209 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
2210 Variable
->EndPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->EndPtr
- (UINTN
)CacheVariable
->StartPtr
));
2212 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
2213 if (CacheVariable
->InDeletedTransitionPtr
!= NULL
) {
2214 Variable
->InDeletedTransitionPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->InDeletedTransitionPtr
- (UINTN
)CacheVariable
->StartPtr
));
2216 Variable
->InDeletedTransitionPtr
= NULL
;
2218 Variable
->Volatile
= FALSE
;
2221 Fvb
= mVariableModuleGlobal
->FvbInstance
;
2224 // Tricky part: Use scratch data area at the end of volatile variable store
2225 // as a temporary storage.
2227 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
2228 ScratchSize
= mVariableModuleGlobal
->ScratchBufferSize
;
2229 SetMem (NextVariable
, ScratchSize
, 0xff);
2232 if (Variable
->CurrPtr
!= NULL
) {
2234 // Update/Delete existing variable.
2238 // If AtRuntime and the variable is Volatile and Runtime Access,
2239 // the volatile is ReadOnly, and SetVariable should be aborted and
2240 // return EFI_WRITE_PROTECTED.
2242 if (Variable
->Volatile
) {
2243 Status
= EFI_WRITE_PROTECTED
;
2247 // Only variable that have NV attributes can be updated/deleted in Runtime.
2249 if ((CacheVariable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2250 Status
= EFI_INVALID_PARAMETER
;
2255 // Only variable that have RT attributes can be updated/deleted in Runtime.
2257 if ((CacheVariable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) {
2258 Status
= EFI_INVALID_PARAMETER
;
2264 // Setting a data variable with no access, or zero DataSize attributes
2265 // causes it to be deleted.
2266 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
2267 // not delete the variable.
2269 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0))|| ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
2270 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2272 // Both ADDED and IN_DELETED_TRANSITION variable are present,
2273 // set IN_DELETED_TRANSITION one to DELETED state first.
2275 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2276 State
= CacheVariable
->InDeletedTransitionPtr
->State
;
2277 State
&= VAR_DELETED
;
2278 Status
= UpdateVariableStore (
2279 &mVariableModuleGlobal
->VariableGlobal
,
2283 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2287 if (!EFI_ERROR (Status
)) {
2288 if (!Variable
->Volatile
) {
2289 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2296 State
= CacheVariable
->CurrPtr
->State
;
2297 State
&= VAR_DELETED
;
2299 Status
= UpdateVariableStore (
2300 &mVariableModuleGlobal
->VariableGlobal
,
2304 (UINTN
) &Variable
->CurrPtr
->State
,
2308 if (!EFI_ERROR (Status
)) {
2309 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
2310 if (!Variable
->Volatile
) {
2311 CacheVariable
->CurrPtr
->State
= State
;
2312 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2318 // If the variable is marked valid, and the same data has been passed in,
2319 // then return to the caller immediately.
2321 if (DataSizeOfVariable (CacheVariable
->CurrPtr
) == DataSize
&&
2322 (CompareMem (Data
, GetVariableDataPtr (CacheVariable
->CurrPtr
), DataSize
) == 0) &&
2323 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) &&
2324 (TimeStamp
== NULL
)) {
2326 // Variable content unchanged and no need to update timestamp, just return.
2328 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2329 Status
= EFI_SUCCESS
;
2331 } else if ((CacheVariable
->CurrPtr
->State
== VAR_ADDED
) ||
2332 (CacheVariable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
2335 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable.
2337 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
2339 // NOTE: From 0 to DataOffset of NextVariable is reserved for Variable Header and Name.
2340 // From DataOffset of NextVariable is to save the existing variable data.
2342 DataOffset
= GetVariableDataOffset (CacheVariable
->CurrPtr
);
2343 BufferForMerge
= (UINT8
*) ((UINTN
) NextVariable
+ DataOffset
);
2344 CopyMem (BufferForMerge
, (UINT8
*) ((UINTN
) CacheVariable
->CurrPtr
+ DataOffset
), DataSizeOfVariable (CacheVariable
->CurrPtr
));
2347 // Set Max Auth/Non-Volatile/Volatile Variable Data Size as default MaxDataSize.
2349 if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
2350 MaxDataSize
= mVariableModuleGlobal
->MaxAuthVariableSize
- DataOffset
;
2351 } else if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2352 MaxDataSize
= mVariableModuleGlobal
->MaxVariableSize
- DataOffset
;
2354 MaxDataSize
= mVariableModuleGlobal
->MaxVolatileVariableSize
- DataOffset
;
2358 // Append the new data to the end of existing data.
2359 // Max Harware error record variable data size is different from common/auth variable.
2361 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2362 MaxDataSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - DataOffset
;
2365 if (DataSizeOfVariable (CacheVariable
->CurrPtr
) + DataSize
> MaxDataSize
) {
2367 // Existing data size + new data size exceed maximum variable size limitation.
2369 Status
= EFI_INVALID_PARAMETER
;
2372 CopyMem ((UINT8
*) ((UINTN
) BufferForMerge
+ DataSizeOfVariable (CacheVariable
->CurrPtr
)), Data
, DataSize
);
2373 MergedBufSize
= DataSizeOfVariable (CacheVariable
->CurrPtr
) + DataSize
;
2376 // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.
2378 Data
= BufferForMerge
;
2379 DataSize
= MergedBufSize
;
2384 // Mark the old variable as in delete transition.
2386 State
= CacheVariable
->CurrPtr
->State
;
2387 State
&= VAR_IN_DELETED_TRANSITION
;
2389 Status
= UpdateVariableStore (
2390 &mVariableModuleGlobal
->VariableGlobal
,
2394 (UINTN
) &Variable
->CurrPtr
->State
,
2398 if (EFI_ERROR (Status
)) {
2401 if (!Variable
->Volatile
) {
2402 CacheVariable
->CurrPtr
->State
= State
;
2407 // Not found existing variable. Create a new variable.
2410 if ((DataSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
2411 Status
= EFI_SUCCESS
;
2416 // Make sure we are trying to create a new variable.
2417 // Setting a data variable with zero DataSize or no access attributes means to delete it.
2419 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
2420 Status
= EFI_NOT_FOUND
;
2425 // Only variable have NV|RT attribute can be created in Runtime.
2428 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
2429 Status
= EFI_INVALID_PARAMETER
;
2435 // Function part - create a new variable and copy the data.
2436 // Both update a variable and create a variable will come here.
2438 NextVariable
->StartId
= VARIABLE_DATA
;
2440 // NextVariable->State = VAR_ADDED;
2442 NextVariable
->Reserved
= 0;
2443 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
2444 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) NextVariable
;
2445 AuthVariable
->PubKeyIndex
= KeyIndex
;
2446 AuthVariable
->MonotonicCount
= MonotonicCount
;
2447 ZeroMem (&AuthVariable
->TimeStamp
, sizeof (EFI_TIME
));
2449 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
2450 (TimeStamp
!= NULL
)) {
2451 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
2452 CopyMem (&AuthVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
2455 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
2456 // when the new TimeStamp value is later than the current timestamp associated
2457 // with the variable, we need associate the new timestamp with the updated value.
2459 if (Variable
->CurrPtr
!= NULL
) {
2460 if (VariableCompareTimeStampInternal (&(((AUTHENTICATED_VARIABLE_HEADER
*) CacheVariable
->CurrPtr
)->TimeStamp
), TimeStamp
)) {
2461 CopyMem (&AuthVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
2463 CopyMem (&AuthVariable
->TimeStamp
, &(((AUTHENTICATED_VARIABLE_HEADER
*) CacheVariable
->CurrPtr
)->TimeStamp
), sizeof (EFI_TIME
));
2471 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
2472 // Attributes bitmask parameter of a GetVariable() call.
2474 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
2476 VarNameOffset
= GetVariableHeaderSize ();
2477 VarNameSize
= StrSize (VariableName
);
2479 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
2483 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
2486 // If DataReady is TRUE, it means the variable data has been saved into
2487 // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.
2491 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
2497 CopyMem (GetVendorGuidPtr (NextVariable
), VendorGuid
, sizeof (EFI_GUID
));
2499 // There will be pad bytes after Data, the NextVariable->NameSize and
2500 // NextVariable->DataSize should not include pad size so that variable
2501 // service can get actual size in GetVariable.
2503 SetNameSizeOfVariable (NextVariable
, VarNameSize
);
2504 SetDataSizeOfVariable (NextVariable
, DataSize
);
2507 // The actual size of the variable that stores in storage should
2508 // include pad size.
2510 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
2511 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2513 // Create a nonvolatile variable.
2517 IsCommonVariable
= FALSE
;
2518 IsCommonUserVariable
= FALSE
;
2519 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0) {
2520 IsCommonVariable
= TRUE
;
2521 IsCommonUserVariable
= IsUserVariable (NextVariable
);
2523 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2524 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
2525 || (IsCommonVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonVariableSpace
))
2526 || (IsCommonVariable
&& AtRuntime () && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonRuntimeVariableSpace
))
2527 || (IsCommonUserVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonUserVariableTotalSize
) > mVariableModuleGlobal
->CommonMaxUserVariableSpace
))) {
2529 if (IsCommonUserVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonUserVariableTotalSize
) > mVariableModuleGlobal
->CommonMaxUserVariableSpace
)) {
2530 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2532 if (IsCommonVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonRuntimeVariableSpace
)) {
2533 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2535 Status
= EFI_OUT_OF_RESOURCES
;
2539 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2542 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2543 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2547 HEADER_ALIGN (VarSize
)
2549 if (!EFI_ERROR (Status
)) {
2551 // The new variable has been integrated successfully during reclaiming.
2553 if (Variable
->CurrPtr
!= NULL
) {
2554 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2555 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2557 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
);
2558 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2560 if (IsCommonUserVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonUserVariableTotalSize
) > mVariableModuleGlobal
->CommonMaxUserVariableSpace
)) {
2561 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2563 if (IsCommonVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonVariableSpace
)) {
2564 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2571 // 1. Write variable header
2572 // 2. Set variable state to header valid
2573 // 3. Write variable data
2574 // 4. Set variable state to valid
2579 Status
= UpdateVariableStore (
2580 &mVariableModuleGlobal
->VariableGlobal
,
2584 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2585 (UINT32
) GetVariableHeaderSize (),
2586 (UINT8
*) NextVariable
2589 if (EFI_ERROR (Status
)) {
2596 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
2597 Status
= UpdateVariableStore (
2598 &mVariableModuleGlobal
->VariableGlobal
,
2602 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2604 &NextVariable
->State
2607 if (EFI_ERROR (Status
)) {
2613 Status
= UpdateVariableStore (
2614 &mVariableModuleGlobal
->VariableGlobal
,
2618 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ GetVariableHeaderSize (),
2619 (UINT32
) (VarSize
- GetVariableHeaderSize ()),
2620 (UINT8
*) NextVariable
+ GetVariableHeaderSize ()
2623 if (EFI_ERROR (Status
)) {
2629 NextVariable
->State
= VAR_ADDED
;
2630 Status
= UpdateVariableStore (
2631 &mVariableModuleGlobal
->VariableGlobal
,
2635 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2637 &NextVariable
->State
2640 if (EFI_ERROR (Status
)) {
2645 // Update the memory copy of Flash region.
2647 CopyMem ((UINT8
*)mNvVariableCache
+ mVariableModuleGlobal
->NonVolatileLastVariableOffset
, (UINT8
*)NextVariable
, VarSize
);
2649 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2651 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2652 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2654 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2655 if (IsCommonUserVariable
) {
2656 mVariableModuleGlobal
->CommonUserVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2661 // Create a volatile variable.
2665 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2666 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
2668 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2671 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
2672 &mVariableModuleGlobal
->VolatileLastVariableOffset
,
2676 HEADER_ALIGN (VarSize
)
2678 if (!EFI_ERROR (Status
)) {
2680 // The new variable has been integrated successfully during reclaiming.
2682 if (Variable
->CurrPtr
!= NULL
) {
2683 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2684 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2686 UpdateVariableInfo (VariableName
, VendorGuid
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
);
2691 NextVariable
->State
= VAR_ADDED
;
2692 Status
= UpdateVariableStore (
2693 &mVariableModuleGlobal
->VariableGlobal
,
2697 mVariableModuleGlobal
->VolatileLastVariableOffset
,
2699 (UINT8
*) NextVariable
2702 if (EFI_ERROR (Status
)) {
2706 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2710 // Mark the old variable as deleted.
2712 if (!EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
2713 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2715 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
2716 // set IN_DELETED_TRANSITION one to DELETED state first.
2718 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2719 State
= CacheVariable
->InDeletedTransitionPtr
->State
;
2720 State
&= VAR_DELETED
;
2721 Status
= UpdateVariableStore (
2722 &mVariableModuleGlobal
->VariableGlobal
,
2726 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2730 if (!EFI_ERROR (Status
)) {
2731 if (!Variable
->Volatile
) {
2732 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2739 State
= CacheVariable
->CurrPtr
->State
;
2740 State
&= VAR_DELETED
;
2742 Status
= UpdateVariableStore (
2743 &mVariableModuleGlobal
->VariableGlobal
,
2747 (UINTN
) &Variable
->CurrPtr
->State
,
2751 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
2752 CacheVariable
->CurrPtr
->State
= State
;
2756 if (!EFI_ERROR (Status
)) {
2757 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2759 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2769 This code finds variable in storage blocks (Volatile or Non-Volatile).
2771 Caution: This function may receive untrusted input.
2772 This function may be invoked in SMM mode, and datasize is external input.
2773 This function will do basic validation, before parse the data.
2775 @param VariableName Name of Variable to be found.
2776 @param VendorGuid Variable vendor GUID.
2777 @param Attributes Attribute value of the variable found.
2778 @param DataSize Size of Data found. If size is less than the
2779 data, this value contains the required size.
2780 @param Data The buffer to return the contents of the variable. May be NULL
2781 with a zero DataSize in order to determine the size buffer needed.
2783 @return EFI_INVALID_PARAMETER Invalid parameter.
2784 @return EFI_SUCCESS Find the specified variable.
2785 @return EFI_NOT_FOUND Not found.
2786 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2791 VariableServiceGetVariable (
2792 IN CHAR16
*VariableName
,
2793 IN EFI_GUID
*VendorGuid
,
2794 OUT UINT32
*Attributes OPTIONAL
,
2795 IN OUT UINTN
*DataSize
,
2796 OUT VOID
*Data OPTIONAL
2800 VARIABLE_POINTER_TRACK Variable
;
2803 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2804 return EFI_INVALID_PARAMETER
;
2807 if (VariableName
[0] == 0) {
2808 return EFI_NOT_FOUND
;
2811 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2813 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2814 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2821 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2822 ASSERT (VarDataSize
!= 0);
2824 if (*DataSize
>= VarDataSize
) {
2826 Status
= EFI_INVALID_PARAMETER
;
2830 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2831 if (Attributes
!= NULL
) {
2832 *Attributes
= Variable
.CurrPtr
->Attributes
;
2835 *DataSize
= VarDataSize
;
2836 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2838 Status
= EFI_SUCCESS
;
2841 *DataSize
= VarDataSize
;
2842 Status
= EFI_BUFFER_TOO_SMALL
;
2847 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2852 This code Finds the Next available variable.
2854 Caution: This function may receive untrusted input.
2855 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2857 @param[in] VariableName Pointer to variable name.
2858 @param[in] VendorGuid Variable Vendor Guid.
2859 @param[out] VariablePtr Pointer to variable header address.
2861 @retval EFI_SUCCESS The function completed successfully.
2862 @retval EFI_NOT_FOUND The next variable was not found.
2863 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.
2864 @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
2865 GUID of an existing variable.
2870 VariableServiceGetNextVariableInternal (
2871 IN CHAR16
*VariableName
,
2872 IN EFI_GUID
*VendorGuid
,
2873 OUT VARIABLE_HEADER
**VariablePtr
2876 VARIABLE_STORE_TYPE Type
;
2877 VARIABLE_POINTER_TRACK Variable
;
2878 VARIABLE_POINTER_TRACK VariableInHob
;
2879 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2881 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2883 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2884 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2886 // For VariableName is an empty string, FindVariable() will try to find and return
2887 // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)
2888 // as no any variable is found, still go to return the error (EFI_NOT_FOUND).
2890 if (VariableName
[0] != 0) {
2892 // For VariableName is not an empty string, and FindVariable() returns error as
2893 // VariableName and VendorGuid are not a name and GUID of an existing variable,
2894 // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.
2896 Status
= EFI_INVALID_PARAMETER
;
2901 if (VariableName
[0] != 0) {
2903 // If variable name is not NULL, get next variable.
2905 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2909 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2910 // The index and attributes mapping must be kept in this order as FindVariable
2911 // makes use of this mapping to implement search algorithm.
2913 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2914 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2915 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2919 // Switch from Volatile to HOB, to Non-Volatile.
2921 while (!IsValidVariableHeader (Variable
.CurrPtr
, Variable
.EndPtr
)) {
2923 // Find current storage index
2925 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2926 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2930 ASSERT (Type
< VariableStoreTypeMax
);
2932 // Switch to next storage
2934 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2935 if (VariableStoreHeader
[Type
] != NULL
) {
2940 // Capture the case that
2941 // 1. current storage is the last one, or
2942 // 2. no further storage
2944 if (Type
== VariableStoreTypeMax
) {
2945 Status
= EFI_NOT_FOUND
;
2948 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2949 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2950 Variable
.CurrPtr
= Variable
.StartPtr
;
2954 // Variable is found
2956 if (Variable
.CurrPtr
->State
== VAR_ADDED
|| Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2957 if (!AtRuntime () || ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
2958 if (Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2960 // If it is a IN_DELETED_TRANSITION variable,
2961 // and there is also a same ADDED one at the same time,
2964 VariablePtrTrack
.StartPtr
= Variable
.StartPtr
;
2965 VariablePtrTrack
.EndPtr
= Variable
.EndPtr
;
2966 Status
= FindVariableEx (
2967 GetVariableNamePtr (Variable
.CurrPtr
),
2968 GetVendorGuidPtr (Variable
.CurrPtr
),
2972 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
== VAR_ADDED
) {
2973 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2979 // Don't return NV variable when HOB overrides it
2981 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2982 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2984 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2985 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2986 Status
= FindVariableEx (
2987 GetVariableNamePtr (Variable
.CurrPtr
),
2988 GetVendorGuidPtr (Variable
.CurrPtr
),
2992 if (!EFI_ERROR (Status
)) {
2993 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2998 *VariablePtr
= Variable
.CurrPtr
;
2999 Status
= EFI_SUCCESS
;
3004 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
3013 This code Finds the Next available variable.
3015 Caution: This function may receive untrusted input.
3016 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3018 @param VariableNameSize The size of the VariableName buffer. The size must be large
3019 enough to fit input string supplied in VariableName buffer.
3020 @param VariableName Pointer to variable name.
3021 @param VendorGuid Variable Vendor Guid.
3023 @retval EFI_SUCCESS The function completed successfully.
3024 @retval EFI_NOT_FOUND The next variable was not found.
3025 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the result.
3026 VariableNameSize has been updated with the size needed to complete the request.
3027 @retval EFI_INVALID_PARAMETER VariableNameSize is NULL.
3028 @retval EFI_INVALID_PARAMETER VariableName is NULL.
3029 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.
3030 @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
3031 GUID of an existing variable.
3032 @retval EFI_INVALID_PARAMETER Null-terminator is not found in the first VariableNameSize bytes of
3033 the input VariableName buffer.
3038 VariableServiceGetNextVariableName (
3039 IN OUT UINTN
*VariableNameSize
,
3040 IN OUT CHAR16
*VariableName
,
3041 IN OUT EFI_GUID
*VendorGuid
3047 VARIABLE_HEADER
*VariablePtr
;
3049 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
3050 return EFI_INVALID_PARAMETER
;
3054 // Calculate the possible maximum length of name string, including the Null terminator.
3056 MaxLen
= *VariableNameSize
/ sizeof (CHAR16
);
3057 if ((MaxLen
== 0) || (StrnLenS (VariableName
, MaxLen
) == MaxLen
)) {
3059 // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,
3060 // follow spec to return EFI_INVALID_PARAMETER.
3062 return EFI_INVALID_PARAMETER
;
3065 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3067 Status
= VariableServiceGetNextVariableInternal (VariableName
, VendorGuid
, &VariablePtr
);
3068 if (!EFI_ERROR (Status
)) {
3069 VarNameSize
= NameSizeOfVariable (VariablePtr
);
3070 ASSERT (VarNameSize
!= 0);
3071 if (VarNameSize
<= *VariableNameSize
) {
3072 CopyMem (VariableName
, GetVariableNamePtr (VariablePtr
), VarNameSize
);
3073 CopyMem (VendorGuid
, GetVendorGuidPtr (VariablePtr
), sizeof (EFI_GUID
));
3074 Status
= EFI_SUCCESS
;
3076 Status
= EFI_BUFFER_TOO_SMALL
;
3079 *VariableNameSize
= VarNameSize
;
3082 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3088 This code sets variable in storage blocks (Volatile or Non-Volatile).
3090 Caution: This function may receive untrusted input.
3091 This function may be invoked in SMM mode, and datasize and data are external input.
3092 This function will do basic validation, before parse the data.
3093 This function will parse the authentication carefully to avoid security issues, like
3094 buffer overflow, integer overflow.
3095 This function will check attribute carefully to avoid authentication bypass.
3097 @param VariableName Name of Variable to be found.
3098 @param VendorGuid Variable vendor GUID.
3099 @param Attributes Attribute value of the variable found
3100 @param DataSize Size of Data found. If size is less than the
3101 data, this value contains the required size.
3102 @param Data Data pointer.
3104 @return EFI_INVALID_PARAMETER Invalid parameter.
3105 @return EFI_SUCCESS Set successfully.
3106 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
3107 @return EFI_NOT_FOUND Not found.
3108 @return EFI_WRITE_PROTECTED Variable is read-only.
3113 VariableServiceSetVariable (
3114 IN CHAR16
*VariableName
,
3115 IN EFI_GUID
*VendorGuid
,
3116 IN UINT32 Attributes
,
3121 VARIABLE_POINTER_TRACK Variable
;
3123 VARIABLE_HEADER
*NextVariable
;
3124 EFI_PHYSICAL_ADDRESS Point
;
3128 // Check input parameters.
3130 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
3131 return EFI_INVALID_PARAMETER
;
3134 if (DataSize
!= 0 && Data
== NULL
) {
3135 return EFI_INVALID_PARAMETER
;
3139 // Check for reserverd bit in variable attribute.
3140 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated but we still allow
3141 // the delete operation of common authenticated variable at user physical presence.
3142 // So leave EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute check to AuthVariableLib
3144 if ((Attributes
& (~(EFI_VARIABLE_ATTRIBUTES_MASK
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
))) != 0) {
3145 return EFI_INVALID_PARAMETER
;
3149 // Make sure if runtime bit is set, boot service bit is set also.
3151 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
3152 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
3153 return EFI_UNSUPPORTED
;
3155 return EFI_INVALID_PARAMETER
;
3157 } else if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
3158 if (!mVariableModuleGlobal
->VariableGlobal
.AuthSupport
) {
3160 // Not support authenticated variable write.
3162 return EFI_INVALID_PARAMETER
;
3164 } else if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
3165 if (PcdGet32 (PcdHwErrStorageSize
) == 0) {
3167 // Not support harware error record variable variable.
3169 return EFI_INVALID_PARAMETER
;
3174 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
3175 // cannot be set both.
3177 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
3178 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
3179 return EFI_UNSUPPORTED
;
3182 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
3184 // If DataSize == AUTHINFO_SIZE and then PayloadSize is 0.
3185 // Maybe it's the delete operation of common authenticated variable at user physical presence.
3187 if (DataSize
!= AUTHINFO_SIZE
) {
3188 return EFI_UNSUPPORTED
;
3190 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
3191 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
3193 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
3195 if (DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
||
3196 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
)) ||
3197 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) {
3198 return EFI_SECURITY_VIOLATION
;
3201 // The VariableSpeculationBarrier() call here is to ensure the above sanity
3202 // check for the EFI_VARIABLE_AUTHENTICATION_2 descriptor has been completed
3203 // before the execution of subsequent codes.
3205 VariableSpeculationBarrier ();
3206 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
3208 PayloadSize
= DataSize
;
3211 if ((UINTN
)(~0) - PayloadSize
< StrSize(VariableName
)){
3213 // Prevent whole variable size overflow
3215 return EFI_INVALID_PARAMETER
;
3219 // The size of the VariableName, including the Unicode Null in bytes plus
3220 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
3221 // bytes for HwErrRec#### variable.
3223 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3224 if (StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
) - GetVariableHeaderSize ()) {
3225 return EFI_INVALID_PARAMETER
;
3229 // The size of the VariableName, including the Unicode Null in bytes plus
3230 // the DataSize is limited to maximum size of Max(Auth|Volatile)VariableSize bytes.
3232 if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
3233 if (StrSize (VariableName
) + PayloadSize
> mVariableModuleGlobal
->MaxAuthVariableSize
- GetVariableHeaderSize ()) {
3234 DEBUG ((DEBUG_ERROR
,
3235 "%a: Failed to set variable '%s' with Guid %g\n",
3236 __FUNCTION__
, VariableName
, VendorGuid
));
3237 DEBUG ((DEBUG_ERROR
,
3238 "NameSize(0x%x) + PayloadSize(0x%x) > "
3239 "MaxAuthVariableSize(0x%x) - HeaderSize(0x%x)\n",
3240 StrSize (VariableName
), PayloadSize
,
3241 mVariableModuleGlobal
->MaxAuthVariableSize
,
3242 GetVariableHeaderSize ()
3244 return EFI_INVALID_PARAMETER
;
3246 } else if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
3247 if (StrSize (VariableName
) + PayloadSize
> mVariableModuleGlobal
->MaxVariableSize
- GetVariableHeaderSize ()) {
3248 DEBUG ((DEBUG_ERROR
,
3249 "%a: Failed to set variable '%s' with Guid %g\n",
3250 __FUNCTION__
, VariableName
, VendorGuid
));
3251 DEBUG ((DEBUG_ERROR
,
3252 "NameSize(0x%x) + PayloadSize(0x%x) > "
3253 "MaxVariableSize(0x%x) - HeaderSize(0x%x)\n",
3254 StrSize (VariableName
), PayloadSize
,
3255 mVariableModuleGlobal
->MaxVariableSize
,
3256 GetVariableHeaderSize ()
3258 return EFI_INVALID_PARAMETER
;
3261 if (StrSize (VariableName
) + PayloadSize
> mVariableModuleGlobal
->MaxVolatileVariableSize
- GetVariableHeaderSize ()) {
3262 DEBUG ((DEBUG_ERROR
,
3263 "%a: Failed to set variable '%s' with Guid %g\n",
3264 __FUNCTION__
, VariableName
, VendorGuid
));
3265 DEBUG ((DEBUG_ERROR
,
3266 "NameSize(0x%x) + PayloadSize(0x%x) > "
3267 "MaxVolatileVariableSize(0x%x) - HeaderSize(0x%x)\n",
3268 StrSize (VariableName
), PayloadSize
,
3269 mVariableModuleGlobal
->MaxVolatileVariableSize
,
3270 GetVariableHeaderSize ()
3272 return EFI_INVALID_PARAMETER
;
3278 // Special Handling for MOR Lock variable.
3280 Status
= SetVariableCheckHandlerMor (VariableName
, VendorGuid
, Attributes
, PayloadSize
, (VOID
*) ((UINTN
) Data
+ DataSize
- PayloadSize
));
3281 if (Status
== EFI_ALREADY_STARTED
) {
3283 // EFI_ALREADY_STARTED means the SetVariable() action is handled inside of SetVariableCheckHandlerMor().
3284 // Variable driver can just return SUCCESS.
3288 if (EFI_ERROR (Status
)) {
3292 Status
= VarCheckLibSetVariableCheck (VariableName
, VendorGuid
, Attributes
, PayloadSize
, (VOID
*) ((UINTN
) Data
+ DataSize
- PayloadSize
), mRequestSource
);
3293 if (EFI_ERROR (Status
)) {
3297 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3300 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
3302 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
3303 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
3305 // Parse non-volatile variable data and get last variable offset.
3307 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
3308 while (IsValidVariableHeader (NextVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))) {
3309 NextVariable
= GetNextVariablePtr (NextVariable
);
3311 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
3315 // Check whether the input variable is already existed.
3317 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
3318 if (!EFI_ERROR (Status
)) {
3319 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
3320 Status
= EFI_WRITE_PROTECTED
;
3323 if (Attributes
!= 0 && (Attributes
& (~EFI_VARIABLE_APPEND_WRITE
)) != Variable
.CurrPtr
->Attributes
) {
3325 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not
3326 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:
3327 // 1. No access attributes specified
3328 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE
3330 Status
= EFI_INVALID_PARAMETER
;
3331 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
));
3336 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate
)) {
3338 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
3340 Status
= AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
3341 if (EFI_ERROR (Status
)) {
3343 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.
3349 if (mVariableModuleGlobal
->VariableGlobal
.AuthSupport
) {
3350 Status
= AuthVariableLibProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
3352 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, &Variable
, NULL
);
3356 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
3357 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3359 if (!AtRuntime ()) {
3360 if (!EFI_ERROR (Status
)) {
3373 This code returns information about the EFI variables.
3375 Caution: This function may receive untrusted input.
3376 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3378 @param Attributes Attributes bitmask to specify the type of variables
3379 on which to return information.
3380 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
3381 for the EFI variables associated with the attributes specified.
3382 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
3383 for EFI variables associated with the attributes specified.
3384 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
3385 associated with the attributes specified.
3387 @return EFI_SUCCESS Query successfully.
3392 VariableServiceQueryVariableInfoInternal (
3393 IN UINT32 Attributes
,
3394 OUT UINT64
*MaximumVariableStorageSize
,
3395 OUT UINT64
*RemainingVariableStorageSize
,
3396 OUT UINT64
*MaximumVariableSize
3399 VARIABLE_HEADER
*Variable
;
3400 VARIABLE_HEADER
*NextVariable
;
3401 UINT64 VariableSize
;
3402 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3403 UINT64 CommonVariableTotalSize
;
3404 UINT64 HwErrVariableTotalSize
;
3406 VARIABLE_POINTER_TRACK VariablePtrTrack
;
3408 CommonVariableTotalSize
= 0;
3409 HwErrVariableTotalSize
= 0;
3411 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
3413 // Query is Volatile related.
3415 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
3418 // Query is Non-Volatile related.
3420 VariableStoreHeader
= mNvVariableCache
;
3424 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
3425 // with the storage size (excluding the storage header size).
3427 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
3430 // Harware error record variable needs larger size.
3432 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3433 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
3434 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - GetVariableHeaderSize ();
3436 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
3438 *MaximumVariableStorageSize
= mVariableModuleGlobal
->CommonRuntimeVariableSpace
;
3440 *MaximumVariableStorageSize
= mVariableModuleGlobal
->CommonVariableSpace
;
3445 // Let *MaximumVariableSize be Max(Auth|Volatile)VariableSize with the exception of the variable header size.
3447 if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
3448 *MaximumVariableSize
= mVariableModuleGlobal
->MaxAuthVariableSize
- GetVariableHeaderSize ();
3449 } else if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
3450 *MaximumVariableSize
= mVariableModuleGlobal
->MaxVariableSize
- GetVariableHeaderSize ();
3452 *MaximumVariableSize
= mVariableModuleGlobal
->MaxVolatileVariableSize
- GetVariableHeaderSize ();
3457 // Point to the starting address of the variables.
3459 Variable
= GetStartPointer (VariableStoreHeader
);
3462 // Now walk through the related variable store.
3464 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
3465 NextVariable
= GetNextVariablePtr (Variable
);
3466 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
3470 // We don't take the state of the variables in mind
3471 // when calculating RemainingVariableStorageSize,
3472 // since the space occupied by variables not marked with
3473 // VAR_ADDED is not allowed to be reclaimed in Runtime.
3475 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3476 HwErrVariableTotalSize
+= VariableSize
;
3478 CommonVariableTotalSize
+= VariableSize
;
3482 // Only care about Variables with State VAR_ADDED, because
3483 // the space not marked as VAR_ADDED is reclaimable now.
3485 if (Variable
->State
== VAR_ADDED
) {
3486 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3487 HwErrVariableTotalSize
+= VariableSize
;
3489 CommonVariableTotalSize
+= VariableSize
;
3491 } else if (Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
3493 // If it is a IN_DELETED_TRANSITION variable,
3494 // and there is not also a same ADDED one at the same time,
3495 // this IN_DELETED_TRANSITION variable is valid.
3497 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
3498 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
3499 Status
= FindVariableEx (
3500 GetVariableNamePtr (Variable
),
3501 GetVendorGuidPtr (Variable
),
3505 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
!= VAR_ADDED
) {
3506 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3507 HwErrVariableTotalSize
+= VariableSize
;
3509 CommonVariableTotalSize
+= VariableSize
;
3516 // Go to the next one.
3518 Variable
= NextVariable
;
3521 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
3522 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
3524 if (*MaximumVariableStorageSize
< CommonVariableTotalSize
) {
3525 *RemainingVariableStorageSize
= 0;
3527 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
3531 if (*RemainingVariableStorageSize
< GetVariableHeaderSize ()) {
3532 *MaximumVariableSize
= 0;
3533 } else if ((*RemainingVariableStorageSize
- GetVariableHeaderSize ()) < *MaximumVariableSize
) {
3534 *MaximumVariableSize
= *RemainingVariableStorageSize
- GetVariableHeaderSize ();
3542 This code returns information about the EFI variables.
3544 Caution: This function may receive untrusted input.
3545 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3547 @param Attributes Attributes bitmask to specify the type of variables
3548 on which to return information.
3549 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
3550 for the EFI variables associated with the attributes specified.
3551 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
3552 for EFI variables associated with the attributes specified.
3553 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
3554 associated with the attributes specified.
3556 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
3557 @return EFI_SUCCESS Query successfully.
3558 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
3563 VariableServiceQueryVariableInfo (
3564 IN UINT32 Attributes
,
3565 OUT UINT64
*MaximumVariableStorageSize
,
3566 OUT UINT64
*RemainingVariableStorageSize
,
3567 OUT UINT64
*MaximumVariableSize
3572 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
3573 return EFI_INVALID_PARAMETER
;
3576 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
3578 // Deprecated attribute, make this check as highest priority.
3580 return EFI_UNSUPPORTED
;
3583 if ((Attributes
& EFI_VARIABLE_ATTRIBUTES_MASK
) == 0) {
3585 // Make sure the Attributes combination is supported by the platform.
3587 return EFI_UNSUPPORTED
;
3588 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
3590 // Make sure if runtime bit is set, boot service bit is set also.
3592 return EFI_INVALID_PARAMETER
;
3593 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
3595 // Make sure RT Attribute is set if we are in Runtime phase.
3597 return EFI_INVALID_PARAMETER
;
3598 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3600 // Make sure Hw Attribute is set with NV.
3602 return EFI_INVALID_PARAMETER
;
3603 } else if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
3604 if (!mVariableModuleGlobal
->VariableGlobal
.AuthSupport
) {
3606 // Not support authenticated variable write.
3608 return EFI_UNSUPPORTED
;
3610 } else if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
3611 if (PcdGet32 (PcdHwErrStorageSize
) == 0) {
3613 // Not support harware error record variable variable.
3615 return EFI_UNSUPPORTED
;
3619 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3621 Status
= VariableServiceQueryVariableInfoInternal (
3623 MaximumVariableStorageSize
,
3624 RemainingVariableStorageSize
,
3628 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3633 This function reclaims variable storage if free size is below the threshold.
3635 Caution: This function may be invoked at SMM mode.
3636 Care must be taken to make sure not security issue.
3645 UINTN RemainingCommonRuntimeVariableSpace
;
3646 UINTN RemainingHwErrVariableSpace
;
3647 STATIC BOOLEAN Reclaimed
;
3650 // This function will be called only once at EndOfDxe or ReadyToBoot event.
3657 Status
= EFI_SUCCESS
;
3659 if (mVariableModuleGlobal
->CommonRuntimeVariableSpace
< mVariableModuleGlobal
->CommonVariableTotalSize
) {
3660 RemainingCommonRuntimeVariableSpace
= 0;
3662 RemainingCommonRuntimeVariableSpace
= mVariableModuleGlobal
->CommonRuntimeVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
3665 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
3668 // Check if the free area is below a threshold.
3670 if (((RemainingCommonRuntimeVariableSpace
< mVariableModuleGlobal
->MaxVariableSize
) ||
3671 (RemainingCommonRuntimeVariableSpace
< mVariableModuleGlobal
->MaxAuthVariableSize
)) ||
3672 ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
3673 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
3675 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3676 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3682 ASSERT_EFI_ERROR (Status
);
3687 Get non-volatile maximum variable size.
3689 @return Non-volatile maximum variable size.
3693 GetNonVolatileMaxVariableSize (
3697 if (PcdGet32 (PcdHwErrStorageSize
) != 0) {
3698 return MAX (MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxAuthVariableSize
)),
3699 PcdGet32 (PcdMaxHardwareErrorVariableSize
));
3701 return MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxAuthVariableSize
));
3706 Get maximum variable size, covering both non-volatile and volatile variables.
3708 @return Maximum variable size.
3712 GetMaxVariableSize (
3716 UINTN MaxVariableSize
;
3718 MaxVariableSize
= GetNonVolatileMaxVariableSize();
3720 // The condition below fails implicitly if PcdMaxVolatileVariableSize equals
3721 // the default zero value.
3723 if (MaxVariableSize
< PcdGet32 (PcdMaxVolatileVariableSize
)) {
3724 MaxVariableSize
= PcdGet32 (PcdMaxVolatileVariableSize
);
3726 return MaxVariableSize
;
3730 Init real non-volatile variable store.
3732 @param[out] VariableStoreBase Output pointer to real non-volatile variable store base.
3734 @retval EFI_SUCCESS Function successfully executed.
3735 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3736 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.
3740 InitRealNonVolatileVariableStore (
3741 OUT EFI_PHYSICAL_ADDRESS
*VariableStoreBase
3744 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
3745 VARIABLE_STORE_HEADER
*VariableStore
;
3746 UINT32 VariableStoreLength
;
3747 EFI_HOB_GUID_TYPE
*GuidHob
;
3748 EFI_PHYSICAL_ADDRESS NvStorageBase
;
3749 UINT8
*NvStorageData
;
3750 UINT32 NvStorageSize
;
3751 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*FtwLastWriteData
;
3752 UINT32 BackUpOffset
;
3754 UINT32 HwErrStorageSize
;
3755 UINT32 MaxUserNvVariableSpaceSize
;
3756 UINT32 BoottimeReservedNvVariableSpaceSize
;
3760 mVariableModuleGlobal
->FvbInstance
= NULL
;
3763 // Allocate runtime memory used for a memory copy of the FLASH region.
3764 // Keep the memory and the FLASH in sync as updates occur.
3766 NvStorageSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
3767 NvStorageData
= AllocateRuntimeZeroPool (NvStorageSize
);
3768 if (NvStorageData
== NULL
) {
3769 return EFI_OUT_OF_RESOURCES
;
3772 NvStorageBase
= NV_STORAGE_VARIABLE_BASE
;
3773 ASSERT (NvStorageBase
!= 0);
3776 // Copy NV storage data to the memory buffer.
3778 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) NvStorageBase
, NvStorageSize
);
3780 Status
= GetFtwProtocol ((VOID
**)&FtwProtocol
);
3782 // If FTW protocol has been installed, no need to check FTW last write data hob.
3784 if (EFI_ERROR (Status
)) {
3786 // Check the FTW last write data hob.
3788 GuidHob
= GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid
);
3789 if (GuidHob
!= NULL
) {
3790 FtwLastWriteData
= (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*) GET_GUID_HOB_DATA (GuidHob
);
3791 if (FtwLastWriteData
->TargetAddress
== NvStorageBase
) {
3792 DEBUG ((EFI_D_INFO
, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN
) FtwLastWriteData
->SpareAddress
));
3794 // Copy the backed up NV storage data to the memory buffer from spare block.
3796 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) (FtwLastWriteData
->SpareAddress
), NvStorageSize
);
3797 } else if ((FtwLastWriteData
->TargetAddress
> NvStorageBase
) &&
3798 (FtwLastWriteData
->TargetAddress
< (NvStorageBase
+ NvStorageSize
))) {
3800 // Flash NV storage from the Offset is backed up in spare block.
3802 BackUpOffset
= (UINT32
) (FtwLastWriteData
->TargetAddress
- NvStorageBase
);
3803 BackUpSize
= NvStorageSize
- BackUpOffset
;
3804 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
));
3806 // Copy the partial backed up NV storage data to the memory buffer from spare block.
3808 CopyMem (NvStorageData
+ BackUpOffset
, (UINT8
*) (UINTN
) FtwLastWriteData
->SpareAddress
, BackUpSize
);
3813 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) NvStorageData
;
3816 // Check if the Firmware Volume is not corrupted
3818 if ((FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) || (!CompareGuid (&gEfiSystemNvDataFvGuid
, &FvHeader
->FileSystemGuid
))) {
3819 FreePool (NvStorageData
);
3820 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
3821 return EFI_VOLUME_CORRUPTED
;
3824 VariableStore
= (VARIABLE_STORE_HEADER
*) ((UINTN
) FvHeader
+ FvHeader
->HeaderLength
);
3825 VariableStoreLength
= NvStorageSize
- FvHeader
->HeaderLength
;
3826 ASSERT (sizeof (VARIABLE_STORE_HEADER
) <= VariableStoreLength
);
3827 ASSERT (VariableStore
->Size
== VariableStoreLength
);
3830 // Check if the Variable Store header is not corrupted
3832 if (GetVariableStoreStatus (VariableStore
) != EfiValid
) {
3833 FreePool (NvStorageData
);
3834 DEBUG((EFI_D_ERROR
, "Variable Store header is corrupted\n"));
3835 return EFI_VOLUME_CORRUPTED
;
3838 mNvFvHeaderCache
= FvHeader
;
3840 *VariableStoreBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStore
;
3842 HwErrStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
3843 MaxUserNvVariableSpaceSize
= PcdGet32 (PcdMaxUserNvVariableSpaceSize
);
3844 BoottimeReservedNvVariableSpaceSize
= PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize
);
3847 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
3848 // is stored with common variable in the same NV region. So the platform integrator should
3849 // ensure that the value of PcdHwErrStorageSize is less than the value of
3850 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).
3852 ASSERT (HwErrStorageSize
< (VariableStoreLength
- sizeof (VARIABLE_STORE_HEADER
)));
3854 // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of
3855 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).
3857 ASSERT (MaxUserNvVariableSpaceSize
< (VariableStoreLength
- sizeof (VARIABLE_STORE_HEADER
) - HwErrStorageSize
));
3859 // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of
3860 // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).
3862 ASSERT (BoottimeReservedNvVariableSpaceSize
< (VariableStoreLength
- sizeof (VARIABLE_STORE_HEADER
) - HwErrStorageSize
));
3864 mVariableModuleGlobal
->CommonVariableSpace
= ((UINTN
) VariableStoreLength
- sizeof (VARIABLE_STORE_HEADER
) - HwErrStorageSize
);
3865 mVariableModuleGlobal
->CommonMaxUserVariableSpace
= ((MaxUserNvVariableSpaceSize
!= 0) ? MaxUserNvVariableSpaceSize
: mVariableModuleGlobal
->CommonVariableSpace
);
3866 mVariableModuleGlobal
->CommonRuntimeVariableSpace
= mVariableModuleGlobal
->CommonVariableSpace
- BoottimeReservedNvVariableSpaceSize
;
3868 DEBUG ((EFI_D_INFO
, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal
->CommonVariableSpace
, mVariableModuleGlobal
->CommonMaxUserVariableSpace
, mVariableModuleGlobal
->CommonRuntimeVariableSpace
));
3871 // The max NV variable size should be < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).
3873 ASSERT (GetNonVolatileMaxVariableSize () < (VariableStoreLength
- sizeof (VARIABLE_STORE_HEADER
)));
3879 Init non-volatile variable store.
3881 @retval EFI_SUCCESS Function successfully executed.
3882 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3883 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.
3887 InitNonVolatileVariableStore (
3891 VARIABLE_HEADER
*Variable
;
3892 VARIABLE_HEADER
*NextVariable
;
3893 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3897 Status
= InitRealNonVolatileVariableStore (&VariableStoreBase
);
3898 if (EFI_ERROR (Status
)) {
3902 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3903 mNvVariableCache
= (VARIABLE_STORE_HEADER
*) (UINTN
) VariableStoreBase
;
3904 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
= (BOOLEAN
)(CompareGuid (&mNvVariableCache
->Signature
, &gEfiAuthenticatedVariableGuid
));
3906 mVariableModuleGlobal
->MaxVariableSize
= PcdGet32 (PcdMaxVariableSize
);
3907 mVariableModuleGlobal
->MaxAuthVariableSize
= ((PcdGet32 (PcdMaxAuthVariableSize
) != 0) ? PcdGet32 (PcdMaxAuthVariableSize
) : mVariableModuleGlobal
->MaxVariableSize
);
3910 // Parse non-volatile variable data and get last variable offset.
3912 Variable
= GetStartPointer (mNvVariableCache
);
3913 while (IsValidVariableHeader (Variable
, GetEndPointer (mNvVariableCache
))) {
3914 NextVariable
= GetNextVariablePtr (Variable
);
3915 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
3916 if ((Variable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3917 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
3919 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
3922 Variable
= NextVariable
;
3924 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) Variable
- (UINTN
) mNvVariableCache
;
3930 Flush the HOB variable to flash.
3932 @param[in] VariableName Name of variable has been updated or deleted.
3933 @param[in] VendorGuid Guid of variable has been updated or deleted.
3937 FlushHobVariableToFlash (
3938 IN CHAR16
*VariableName
,
3939 IN EFI_GUID
*VendorGuid
3943 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3944 VARIABLE_HEADER
*Variable
;
3946 VARIABLE_POINTER_TRACK VariablePtrTrack
;
3952 // Flush the HOB variable to flash.
3954 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3955 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
3957 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
3959 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
3960 for ( Variable
= GetStartPointer (VariableStoreHeader
)
3961 ; IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))
3962 ; Variable
= GetNextVariablePtr (Variable
)
3964 if (Variable
->State
!= VAR_ADDED
) {
3966 // The HOB variable has been set to DELETED state in local.
3970 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
3971 if (VendorGuid
== NULL
|| VariableName
== NULL
||
3972 !CompareGuid (VendorGuid
, GetVendorGuidPtr (Variable
)) ||
3973 StrCmp (VariableName
, GetVariableNamePtr (Variable
)) != 0) {
3974 VariableData
= GetVariableDataPtr (Variable
);
3975 FindVariable (GetVariableNamePtr (Variable
), GetVendorGuidPtr (Variable
), &VariablePtrTrack
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
3976 Status
= UpdateVariable (
3977 GetVariableNamePtr (Variable
),
3978 GetVendorGuidPtr (Variable
),
3980 DataSizeOfVariable (Variable
),
3981 Variable
->Attributes
,
3987 DEBUG ((EFI_D_INFO
, "Variable driver flush the HOB variable to flash: %g %s %r\n", GetVendorGuidPtr (Variable
), GetVariableNamePtr (Variable
), Status
));
3990 // The updated or deleted variable is matched with this HOB variable.
3991 // Don't break here because we will try to set other HOB variables
3992 // since this variable could be set successfully.
3994 Status
= EFI_SUCCESS
;
3996 if (!EFI_ERROR (Status
)) {
3998 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
3999 // set the HOB variable to DELETED state in local.
4001 DEBUG ((EFI_D_INFO
, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", GetVendorGuidPtr (Variable
), GetVariableNamePtr (Variable
)));
4002 Variable
->State
&= VAR_DELETED
;
4009 // We still have HOB variable(s) not flushed in flash.
4011 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
4014 // All HOB variables have been flushed in flash.
4016 DEBUG ((EFI_D_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
4017 if (!AtRuntime ()) {
4018 FreePool ((VOID
*) VariableStoreHeader
);
4026 Initializes variable write service.
4028 @retval EFI_SUCCESS Function successfully executed.
4029 @retval Others Fail to initialize the variable service.
4033 VariableWriteServiceInitialize (
4040 VARIABLE_ENTRY_PROPERTY
*VariableEntry
;
4042 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
4045 // Check if the free area is really free.
4047 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< mNvVariableCache
->Size
; Index
++) {
4048 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
4051 // There must be something wrong in variable store, do reclaim operation.
4054 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
4055 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
4061 if (EFI_ERROR (Status
)) {
4062 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
4069 FlushHobVariableToFlash (NULL
, NULL
);
4071 Status
= EFI_SUCCESS
;
4072 ZeroMem (&mAuthContextOut
, sizeof (mAuthContextOut
));
4073 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
4075 // Authenticated variable initialize.
4077 mAuthContextIn
.StructSize
= sizeof (AUTH_VAR_LIB_CONTEXT_IN
);
4078 mAuthContextIn
.MaxAuthVariableSize
= mVariableModuleGlobal
->MaxAuthVariableSize
- GetVariableHeaderSize ();
4079 Status
= AuthVariableLibInitialize (&mAuthContextIn
, &mAuthContextOut
);
4080 if (!EFI_ERROR (Status
)) {
4081 DEBUG ((EFI_D_INFO
, "Variable driver will work with auth variable support!\n"));
4082 mVariableModuleGlobal
->VariableGlobal
.AuthSupport
= TRUE
;
4083 if (mAuthContextOut
.AuthVarEntry
!= NULL
) {
4084 for (Index
= 0; Index
< mAuthContextOut
.AuthVarEntryCount
; Index
++) {
4085 VariableEntry
= &mAuthContextOut
.AuthVarEntry
[Index
];
4086 Status
= VarCheckLibVariablePropertySet (
4087 VariableEntry
->Name
,
4088 VariableEntry
->Guid
,
4089 &VariableEntry
->VariableProperty
4091 ASSERT_EFI_ERROR (Status
);
4094 } else if (Status
== EFI_UNSUPPORTED
) {
4095 DEBUG ((EFI_D_INFO
, "NOTICE - AuthVariableLibInitialize() returns %r!\n", Status
));
4096 DEBUG ((EFI_D_INFO
, "Variable driver will continue to work without auth variable support!\n"));
4097 mVariableModuleGlobal
->VariableGlobal
.AuthSupport
= FALSE
;
4098 Status
= EFI_SUCCESS
;
4102 if (!EFI_ERROR (Status
)) {
4103 for (Index
= 0; Index
< ARRAY_SIZE (mVariableEntryProperty
); Index
++) {
4104 VariableEntry
= &mVariableEntryProperty
[Index
];
4105 Status
= VarCheckLibVariablePropertySet (VariableEntry
->Name
, VariableEntry
->Guid
, &VariableEntry
->VariableProperty
);
4106 ASSERT_EFI_ERROR (Status
);
4110 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
4113 // Initialize MOR Lock variable.
4121 Convert normal variable storage to the allocated auth variable storage.
4123 @param[in] NormalVarStorage Pointer to the normal variable storage header
4125 @retval the allocated auth variable storage
4128 ConvertNormalVarStorageToAuthVarStorage (
4129 VARIABLE_STORE_HEADER
*NormalVarStorage
4132 VARIABLE_HEADER
*StartPtr
;
4134 VARIABLE_HEADER
*EndPtr
;
4135 UINTN AuthVarStroageSize
;
4136 AUTHENTICATED_VARIABLE_HEADER
*AuthStartPtr
;
4137 VARIABLE_STORE_HEADER
*AuthVarStorage
;
4139 AuthVarStroageSize
= sizeof (VARIABLE_STORE_HEADER
);
4141 // Set AuthFormat as FALSE for normal variable storage
4143 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
= FALSE
;
4146 // Calculate Auth Variable Storage Size
4148 StartPtr
= GetStartPointer (NormalVarStorage
);
4149 EndPtr
= GetEndPointer (NormalVarStorage
);
4150 while (StartPtr
< EndPtr
) {
4151 if (StartPtr
->State
== VAR_ADDED
) {
4152 AuthVarStroageSize
= HEADER_ALIGN (AuthVarStroageSize
);
4153 AuthVarStroageSize
+= sizeof (AUTHENTICATED_VARIABLE_HEADER
);
4154 AuthVarStroageSize
+= StartPtr
->NameSize
+ GET_PAD_SIZE (StartPtr
->NameSize
);
4155 AuthVarStroageSize
+= StartPtr
->DataSize
+ GET_PAD_SIZE (StartPtr
->DataSize
);
4157 StartPtr
= GetNextVariablePtr (StartPtr
);
4161 // Allocate Runtime memory for Auth Variable Storage
4163 AuthVarStorage
= AllocateRuntimeZeroPool (AuthVarStroageSize
);
4164 ASSERT (AuthVarStorage
!= NULL
);
4165 if (AuthVarStorage
== NULL
) {
4170 // Copy Variable from Normal storage to Auth storage
4172 StartPtr
= GetStartPointer (NormalVarStorage
);
4173 EndPtr
= GetEndPointer (NormalVarStorage
);
4174 AuthStartPtr
= (AUTHENTICATED_VARIABLE_HEADER
*) GetStartPointer (AuthVarStorage
);
4175 while (StartPtr
< EndPtr
) {
4176 if (StartPtr
->State
== VAR_ADDED
) {
4177 AuthStartPtr
= (AUTHENTICATED_VARIABLE_HEADER
*) HEADER_ALIGN (AuthStartPtr
);
4179 // Copy Variable Header
4181 AuthStartPtr
->StartId
= StartPtr
->StartId
;
4182 AuthStartPtr
->State
= StartPtr
->State
;
4183 AuthStartPtr
->Attributes
= StartPtr
->Attributes
;
4184 AuthStartPtr
->NameSize
= StartPtr
->NameSize
;
4185 AuthStartPtr
->DataSize
= StartPtr
->DataSize
;
4186 CopyGuid (&AuthStartPtr
->VendorGuid
, &StartPtr
->VendorGuid
);
4188 // Copy Variable Name
4190 NextPtr
= (UINT8
*) (AuthStartPtr
+ 1);
4191 CopyMem (NextPtr
, GetVariableNamePtr (StartPtr
), AuthStartPtr
->NameSize
);
4193 // Copy Variable Data
4195 NextPtr
= NextPtr
+ AuthStartPtr
->NameSize
+ GET_PAD_SIZE (AuthStartPtr
->NameSize
);
4196 CopyMem (NextPtr
, GetVariableDataPtr (StartPtr
), AuthStartPtr
->DataSize
);
4198 // Go to next variable
4200 AuthStartPtr
= (AUTHENTICATED_VARIABLE_HEADER
*) (NextPtr
+ AuthStartPtr
->DataSize
+ GET_PAD_SIZE (AuthStartPtr
->DataSize
));
4202 StartPtr
= GetNextVariablePtr (StartPtr
);
4205 // Update Auth Storage Header
4207 AuthVarStorage
->Format
= NormalVarStorage
->Format
;
4208 AuthVarStorage
->State
= NormalVarStorage
->State
;
4209 AuthVarStorage
->Size
= (UINT32
)((UINTN
)AuthStartPtr
- (UINTN
)AuthVarStorage
);
4210 CopyGuid (&AuthVarStorage
->Signature
, &gEfiAuthenticatedVariableGuid
);
4211 ASSERT (AuthVarStorage
->Size
<= AuthVarStroageSize
);
4214 // Restore AuthFormat
4216 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
= TRUE
;
4217 return AuthVarStorage
;
4221 Get HOB variable store.
4223 @param[in] VariableGuid NV variable store signature.
4225 @retval EFI_SUCCESS Function successfully executed.
4226 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
4230 GetHobVariableStore (
4231 IN EFI_GUID
*VariableGuid
4234 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
4235 UINT64 VariableStoreLength
;
4236 EFI_HOB_GUID_TYPE
*GuidHob
;
4237 BOOLEAN NeedConvertNormalToAuth
;
4240 // Make sure there is no more than one Variable HOB.
4243 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
4244 if (GuidHob
!= NULL
) {
4245 if ((GetNextGuidHob (&gEfiAuthenticatedVariableGuid
, GET_NEXT_HOB (GuidHob
)) != NULL
)) {
4246 DEBUG ((DEBUG_ERROR
, "ERROR: Found two Auth Variable HOBs\n"));
4248 } else if (GetFirstGuidHob (&gEfiVariableGuid
) != NULL
) {
4249 DEBUG ((DEBUG_ERROR
, "ERROR: Found one Auth + one Normal Variable HOBs\n"));
4253 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
4254 if (GuidHob
!= NULL
) {
4255 if ((GetNextGuidHob (&gEfiVariableGuid
, GET_NEXT_HOB (GuidHob
)) != NULL
)) {
4256 DEBUG ((DEBUG_ERROR
, "ERROR: Found two Normal Variable HOBs\n"));
4264 // Combinations supported:
4265 // 1. Normal NV variable store +
4266 // Normal HOB variable store
4267 // 2. Auth NV variable store +
4268 // Auth HOB variable store
4269 // 3. Auth NV variable store +
4270 // Normal HOB variable store (code will convert it to Auth Format)
4272 NeedConvertNormalToAuth
= FALSE
;
4273 GuidHob
= GetFirstGuidHob (VariableGuid
);
4274 if (GuidHob
== NULL
&& VariableGuid
== &gEfiAuthenticatedVariableGuid
) {
4276 // Try getting it from normal variable HOB
4278 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
4279 NeedConvertNormalToAuth
= TRUE
;
4281 if (GuidHob
!= NULL
) {
4282 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
4283 VariableStoreLength
= GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
);
4284 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
4285 if (!NeedConvertNormalToAuth
) {
4286 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
4288 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) ConvertNormalVarStorageToAuthVarStorage ((VOID
*) VariableStoreHeader
);
4290 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
4291 return EFI_OUT_OF_RESOURCES
;
4294 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
4302 Initializes variable store area for non-volatile and volatile variable.
4304 @retval EFI_SUCCESS Function successfully executed.
4305 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
4309 VariableCommonInitialize (
4314 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
4316 EFI_GUID
*VariableGuid
;
4319 // Allocate runtime memory for variable driver global structure.
4321 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
4322 if (mVariableModuleGlobal
== NULL
) {
4323 return EFI_OUT_OF_RESOURCES
;
4326 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
4329 // Init non-volatile variable store.
4331 Status
= InitNonVolatileVariableStore ();
4332 if (EFI_ERROR (Status
)) {
4333 FreePool (mVariableModuleGlobal
);
4338 // mVariableModuleGlobal->VariableGlobal.AuthFormat
4339 // has been initialized in InitNonVolatileVariableStore().
4341 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
4342 DEBUG ((EFI_D_INFO
, "Variable driver will work with auth variable format!\n"));
4344 // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will initialize it.
4346 mVariableModuleGlobal
->VariableGlobal
.AuthSupport
= FALSE
;
4347 VariableGuid
= &gEfiAuthenticatedVariableGuid
;
4349 DEBUG ((EFI_D_INFO
, "Variable driver will work without auth variable support!\n"));
4350 mVariableModuleGlobal
->VariableGlobal
.AuthSupport
= FALSE
;
4351 VariableGuid
= &gEfiVariableGuid
;
4355 // Get HOB variable store.
4357 Status
= GetHobVariableStore (VariableGuid
);
4358 if (EFI_ERROR (Status
)) {
4359 if (mNvFvHeaderCache
!= NULL
) {
4360 FreePool (mNvFvHeaderCache
);
4362 FreePool (mVariableModuleGlobal
);
4366 mVariableModuleGlobal
->MaxVolatileVariableSize
= ((PcdGet32 (PcdMaxVolatileVariableSize
) != 0) ?
4367 PcdGet32 (PcdMaxVolatileVariableSize
) :
4368 mVariableModuleGlobal
->MaxVariableSize
4371 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
4373 ScratchSize
= GetMaxVariableSize ();
4374 mVariableModuleGlobal
->ScratchBufferSize
= ScratchSize
;
4375 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
4376 if (VolatileVariableStore
== NULL
) {
4377 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
4378 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
4380 if (mNvFvHeaderCache
!= NULL
) {
4381 FreePool (mNvFvHeaderCache
);
4383 FreePool (mVariableModuleGlobal
);
4384 return EFI_OUT_OF_RESOURCES
;
4387 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
4390 // Initialize Variable Specific Data.
4392 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
4393 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
4395 CopyGuid (&VolatileVariableStore
->Signature
, VariableGuid
);
4396 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
4397 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
4398 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
4399 VolatileVariableStore
->Reserved
= 0;
4400 VolatileVariableStore
->Reserved1
= 0;
4407 Get the proper fvb handle and/or fvb protocol by the given Flash address.
4409 @param[in] Address The Flash address.
4410 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
4411 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
4415 GetFvbInfoByAddress (
4416 IN EFI_PHYSICAL_ADDRESS Address
,
4417 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
4418 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
4422 EFI_HANDLE
*HandleBuffer
;
4425 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
4426 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
4427 EFI_FVB_ATTRIBUTES_2 Attributes
;
4429 UINTN NumberOfBlocks
;
4431 HandleBuffer
= NULL
;
4433 // Get all FVB handles.
4435 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
4436 if (EFI_ERROR (Status
)) {
4437 return EFI_NOT_FOUND
;
4441 // Get the FVB to access variable store.
4444 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
4445 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
4446 if (EFI_ERROR (Status
)) {
4447 Status
= EFI_NOT_FOUND
;
4452 // Ensure this FVB protocol supported Write operation.
4454 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
4455 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
4460 // Compare the address and select the right one.
4462 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
4463 if (EFI_ERROR (Status
)) {
4468 // Assume one FVB has one type of BlockSize.
4470 Status
= Fvb
->GetBlockSize (Fvb
, 0, &BlockSize
, &NumberOfBlocks
);
4471 if (EFI_ERROR (Status
)) {
4475 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ BlockSize
* NumberOfBlocks
))) {
4476 if (FvbHandle
!= NULL
) {
4477 *FvbHandle
= HandleBuffer
[Index
];
4479 if (FvbProtocol
!= NULL
) {
4482 Status
= EFI_SUCCESS
;
4486 FreePool (HandleBuffer
);
4489 Status
= EFI_NOT_FOUND
;