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) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
20 This program and the accompanying materials
21 are licensed and made available under the terms and conditions of the BSD License
22 which accompanies this distribution. The full text of the license may be found at
23 http://opensource.org/licenses/bsd-license.php
25 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
26 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
31 #include "AuthService.h"
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 /// The memory entry used for variable statistics data.
43 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
46 /// The list to store the variables which cannot be set after the EFI_END_OF_DXE_EVENT_GROUP_GUID
47 /// or EVT_GROUP_READY_TO_BOOT event.
49 LIST_ENTRY mLockedVariableList
= INITIALIZE_LIST_HEAD_VARIABLE (mLockedVariableList
);
52 /// The flag to indicate whether the platform has left the DXE phase of execution.
54 BOOLEAN mEndOfDxe
= FALSE
;
57 /// The flag to indicate whether the variable storage locking is enabled.
59 BOOLEAN mEnableLocking
= TRUE
;
62 // To prevent name collisions with possible future globally defined variables,
63 // other internal firmware data variables that are not defined here must be
64 // saved with a unique VendorGuid other than EFI_GLOBAL_VARIABLE or
65 // any other GUID defined by the UEFI Specification. Implementations must
66 // only permit the creation of variables with a UEFI Specification-defined
67 // VendorGuid when these variables are documented in the UEFI Specification.
69 GLOBAL_VARIABLE_ENTRY mGlobalVariableList
[] = {
70 {EFI_LANG_CODES_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
71 {EFI_LANG_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
72 {EFI_TIME_OUT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
73 {EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
74 {EFI_PLATFORM_LANG_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
75 {EFI_CON_IN_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
76 {EFI_CON_OUT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
77 {EFI_ERR_OUT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
78 {EFI_CON_IN_DEV_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
79 {EFI_CON_OUT_DEV_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
80 {EFI_ERR_OUT_DEV_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
81 {EFI_BOOT_ORDER_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
82 {EFI_BOOT_NEXT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
83 {EFI_BOOT_CURRENT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
84 {EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
85 {EFI_DRIVER_ORDER_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
86 {EFI_HW_ERR_REC_SUPPORT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
87 {EFI_SETUP_MODE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
88 {EFI_KEY_EXCHANGE_KEY_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT_AT
},
89 {EFI_PLATFORM_KEY_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT_AT
},
90 {EFI_SIGNATURE_SUPPORT_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
91 {EFI_SECURE_BOOT_MODE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
92 {EFI_KEK_DEFAULT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
93 {EFI_PK_DEFAULT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
94 {EFI_DB_DEFAULT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
95 {EFI_DBX_DEFAULT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
96 {EFI_DBT_DEFAULT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
97 {EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
98 {EFI_OS_INDICATIONS_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
99 {EFI_VENDOR_KEYS_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
101 GLOBAL_VARIABLE_ENTRY mGlobalVariableList2
[] = {
102 {L
"Boot####", VARIABLE_ATTRIBUTE_NV_BS_RT
},
103 {L
"Driver####", VARIABLE_ATTRIBUTE_NV_BS_RT
},
104 {L
"Key####", VARIABLE_ATTRIBUTE_NV_BS_RT
},
109 SecureBoot Hook for auth variable update.
111 @param[in] VariableName Name of Variable to be found.
112 @param[in] VendorGuid Variable vendor GUID.
117 IN CHAR16
*VariableName
,
118 IN EFI_GUID
*VendorGuid
122 Routine used to track statistical information about variable usage.
123 The data is stored in the EFI system table so it can be accessed later.
124 VariableInfo.efi can dump out the table. Only Boot Services variable
125 accesses are tracked by this code. The PcdVariableCollectStatistics
126 build flag controls if this feature is enabled.
128 A read that hits in the cache will have Read and Cache true for
129 the transaction. Data is allocated by this routine, but never
132 @param[in] VariableName Name of the Variable to track.
133 @param[in] VendorGuid Guid of the Variable to track.
134 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
135 @param[in] Read TRUE if GetVariable() was called.
136 @param[in] Write TRUE if SetVariable() was called.
137 @param[in] Delete TRUE if deleted via SetVariable().
138 @param[in] Cache TRUE for a cache hit.
143 IN CHAR16
*VariableName
,
144 IN EFI_GUID
*VendorGuid
,
152 VARIABLE_INFO_ENTRY
*Entry
;
154 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
157 // Don't collect statistics at runtime.
161 if (gVariableInfo
== NULL
) {
163 // On the first call allocate a entry and place a pointer to it in
164 // the EFI System Table.
166 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
167 ASSERT (gVariableInfo
!= NULL
);
169 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
170 gVariableInfo
->Name
= AllocatePool (StrSize (VariableName
));
171 ASSERT (gVariableInfo
->Name
!= NULL
);
172 StrCpy (gVariableInfo
->Name
, VariableName
);
173 gVariableInfo
->Volatile
= Volatile
;
177 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
178 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
179 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
187 Entry
->DeleteCount
++;
197 if (Entry
->Next
== NULL
) {
199 // If the entry is not in the table add it.
200 // Next iteration of the loop will fill in the data.
202 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
203 ASSERT (Entry
->Next
!= NULL
);
205 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
206 Entry
->Next
->Name
= AllocatePool (StrSize (VariableName
));
207 ASSERT (Entry
->Next
->Name
!= NULL
);
208 StrCpy (Entry
->Next
->Name
, VariableName
);
209 Entry
->Next
->Volatile
= Volatile
;
219 This code checks if variable header is valid or not.
221 @param Variable Pointer to the Variable Header.
222 @param VariableStoreEnd Pointer to the Variable Store End.
224 @retval TRUE Variable header is valid.
225 @retval FALSE Variable header is not valid.
229 IsValidVariableHeader (
230 IN VARIABLE_HEADER
*Variable
,
231 IN VARIABLE_HEADER
*VariableStoreEnd
234 if ((Variable
== NULL
) || (Variable
>= VariableStoreEnd
) || (Variable
->StartId
!= VARIABLE_DATA
)) {
236 // Variable is NULL or has reached the end of variable store,
237 // or the StartId is not correct.
248 This function writes data to the FWH at the correct LBA even if the LBAs
251 @param Global Pointer to VARAIBLE_GLOBAL structure.
252 @param Volatile Point out the Variable is Volatile or Non-Volatile.
253 @param SetByIndex TRUE if target pointer is given as index.
254 FALSE if target pointer is absolute.
255 @param Fvb Pointer to the writable FVB protocol.
256 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
258 @param DataSize Size of data to be written.
259 @param Buffer Pointer to the buffer from which data is written.
261 @retval EFI_INVALID_PARAMETER Parameters not valid.
262 @retval EFI_SUCCESS Variable store successfully updated.
266 UpdateVariableStore (
267 IN VARIABLE_GLOBAL
*Global
,
269 IN BOOLEAN SetByIndex
,
270 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
271 IN UINTN DataPtrIndex
,
276 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
284 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
285 VARIABLE_STORE_HEADER
*VolatileBase
;
286 EFI_PHYSICAL_ADDRESS FvVolHdr
;
287 EFI_PHYSICAL_ADDRESS DataPtr
;
291 DataPtr
= DataPtrIndex
;
294 // Check if the Data is Volatile.
298 return EFI_INVALID_PARAMETER
;
300 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
301 ASSERT_EFI_ERROR (Status
);
303 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
305 // Data Pointer should point to the actual Address where data is to be
309 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
312 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
313 return EFI_INVALID_PARAMETER
;
317 // Data Pointer should point to the actual Address where data is to be
320 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
322 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
325 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
326 return EFI_INVALID_PARAMETER
;
330 // If Volatile Variable just do a simple mem copy.
332 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
337 // If we are here we are dealing with Non-Volatile Variables.
339 LinearOffset
= (UINTN
) FwVolHeader
;
340 CurrWritePtr
= (UINTN
) DataPtr
;
341 CurrWriteSize
= DataSize
;
345 if (CurrWritePtr
< LinearOffset
) {
346 return EFI_INVALID_PARAMETER
;
349 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
350 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
352 // Check to see if the Variable Writes are spanning through multiple
355 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
356 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
357 Status
= Fvb
->Write (
360 (UINTN
) (CurrWritePtr
- LinearOffset
),
366 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
367 Status
= Fvb
->Write (
370 (UINTN
) (CurrWritePtr
- LinearOffset
),
374 if (EFI_ERROR (Status
)) {
378 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
379 CurrBuffer
= CurrBuffer
+ Size
;
380 CurrWriteSize
= CurrWriteSize
- Size
;
384 LinearOffset
+= PtrBlockMapEntry
->Length
;
395 This code gets the current status of Variable Store.
397 @param VarStoreHeader Pointer to the Variable Store Header.
399 @retval EfiRaw Variable store status is raw.
400 @retval EfiValid Variable store status is valid.
401 @retval EfiInvalid Variable store status is invalid.
404 VARIABLE_STORE_STATUS
405 GetVariableStoreStatus (
406 IN VARIABLE_STORE_HEADER
*VarStoreHeader
409 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) &&
410 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
411 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
415 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
416 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
417 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
418 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
419 VarStoreHeader
->Size
== 0xffffffff &&
420 VarStoreHeader
->Format
== 0xff &&
421 VarStoreHeader
->State
== 0xff
433 This code gets the size of name of variable.
435 @param Variable Pointer to the Variable Header.
437 @return UINTN Size of variable in bytes.
442 IN VARIABLE_HEADER
*Variable
445 if (Variable
->State
== (UINT8
) (-1) ||
446 Variable
->DataSize
== (UINT32
) (-1) ||
447 Variable
->NameSize
== (UINT32
) (-1) ||
448 Variable
->Attributes
== (UINT32
) (-1)) {
451 return (UINTN
) Variable
->NameSize
;
456 This code gets the size of variable data.
458 @param Variable Pointer to the Variable Header.
460 @return Size of variable in bytes.
465 IN VARIABLE_HEADER
*Variable
468 if (Variable
->State
== (UINT8
) (-1) ||
469 Variable
->DataSize
== (UINT32
) (-1) ||
470 Variable
->NameSize
== (UINT32
) (-1) ||
471 Variable
->Attributes
== (UINT32
) (-1)) {
474 return (UINTN
) Variable
->DataSize
;
479 This code gets the pointer to the variable name.
481 @param Variable Pointer to the Variable Header.
483 @return Pointer to Variable Name which is Unicode encoding.
488 IN VARIABLE_HEADER
*Variable
492 return (CHAR16
*) (Variable
+ 1);
497 This code gets the pointer to the variable data.
499 @param Variable Pointer to the Variable Header.
501 @return Pointer to Variable Data.
506 IN VARIABLE_HEADER
*Variable
512 // Be careful about pad size for alignment.
514 Value
= (UINTN
) GetVariableNamePtr (Variable
);
515 Value
+= NameSizeOfVariable (Variable
);
516 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
518 return (UINT8
*) Value
;
524 This code gets the pointer to the next variable header.
526 @param Variable Pointer to the Variable Header.
528 @return Pointer to next variable header.
533 IN VARIABLE_HEADER
*Variable
538 Value
= (UINTN
) GetVariableDataPtr (Variable
);
539 Value
+= DataSizeOfVariable (Variable
);
540 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
543 // Be careful about pad size for alignment.
545 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
550 Gets the pointer to the first variable header in given variable store area.
552 @param VarStoreHeader Pointer to the Variable Store Header.
554 @return Pointer to the first variable header.
559 IN VARIABLE_STORE_HEADER
*VarStoreHeader
563 // The end of variable store.
565 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
570 Gets the pointer to the end of the variable storage area.
572 This function gets pointer to the end of the variable storage
573 area, according to the input variable store header.
575 @param VarStoreHeader Pointer to the Variable Store Header.
577 @return Pointer to the end of the variable storage area.
582 IN VARIABLE_STORE_HEADER
*VarStoreHeader
586 // The end of variable store
588 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
593 Check the PubKeyIndex is a valid key or not.
595 This function will iterate the NV storage to see if this PubKeyIndex is still referenced
596 by any valid count-based auth variabe.
598 @param[in] PubKeyIndex Index of the public key in public key store.
600 @retval TRUE The PubKeyIndex is still in use.
601 @retval FALSE The PubKeyIndex is not referenced by any count-based auth variabe.
606 IN UINT32 PubKeyIndex
609 VARIABLE_HEADER
*Variable
;
610 VARIABLE_HEADER
*VariableStoreEnd
;
612 if (PubKeyIndex
> mPubKeyNumber
) {
616 Variable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
617 VariableStoreEnd
= GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
619 while (IsValidVariableHeader (Variable
, VariableStoreEnd
)) {
620 if ((Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) &&
621 Variable
->PubKeyIndex
== PubKeyIndex
) {
624 Variable
= GetNextVariablePtr (Variable
);
632 Get the number of valid public key in PubKeyStore.
634 @param[in] PubKeyNumber Number of the public key in public key store.
636 @return Number of valid public key in PubKeyStore.
640 GetValidPubKeyNumber (
641 IN UINT32 PubKeyNumber
649 for (PubKeyIndex
= 1; PubKeyIndex
<= PubKeyNumber
; PubKeyIndex
++) {
650 if (IsValidPubKeyIndex (PubKeyIndex
)) {
660 Filter the useless key in public key store.
662 This function will find out all valid public keys in public key database, save them in new allocated
663 buffer NewPubKeyStore, and give the new PubKeyIndex. The caller is responsible for freeing buffer
664 NewPubKeyIndex and NewPubKeyStore with FreePool().
666 @param[in] PubKeyStore Point to the public key database.
667 @param[in] PubKeyNumber Number of the public key in PubKeyStore.
668 @param[out] NewPubKeyIndex Point to an array of new PubKeyIndex corresponds to NewPubKeyStore.
669 @param[out] NewPubKeyStore Saved all valid public keys in PubKeyStore.
670 @param[out] NewPubKeySize Buffer size of the NewPubKeyStore.
672 @retval EFI_SUCCESS Trim operation is complete successfully.
673 @retval EFI_OUT_OF_RESOURCES No enough memory resources, or no useless key in PubKeyStore.
678 IN UINT8
*PubKeyStore
,
679 IN UINT32 PubKeyNumber
,
680 OUT UINT32
**NewPubKeyIndex
,
681 OUT UINT8
**NewPubKeyStore
,
682 OUT UINT32
*NewPubKeySize
687 UINT32 NewPubKeyNumber
;
689 NewPubKeyNumber
= GetValidPubKeyNumber (PubKeyNumber
);
690 if (NewPubKeyNumber
== PubKeyNumber
) {
691 return EFI_OUT_OF_RESOURCES
;
694 if (NewPubKeyNumber
!= 0) {
695 *NewPubKeySize
= NewPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
;
697 *NewPubKeySize
= sizeof (UINT8
);
700 *NewPubKeyStore
= AllocatePool (*NewPubKeySize
);
701 if (*NewPubKeyStore
== NULL
) {
702 return EFI_OUT_OF_RESOURCES
;
705 *NewPubKeyIndex
= AllocateZeroPool ((PubKeyNumber
+ 1) * sizeof (UINT32
));
706 if (*NewPubKeyIndex
== NULL
) {
707 FreePool (*NewPubKeyStore
);
708 *NewPubKeyStore
= NULL
;
709 return EFI_OUT_OF_RESOURCES
;
713 for (PubKeyIndex
= 1; PubKeyIndex
<= PubKeyNumber
; PubKeyIndex
++) {
714 if (IsValidPubKeyIndex (PubKeyIndex
)) {
716 *NewPubKeyStore
+ CopiedKey
* EFI_CERT_TYPE_RSA2048_SIZE
,
717 PubKeyStore
+ (PubKeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
,
718 EFI_CERT_TYPE_RSA2048_SIZE
720 (*NewPubKeyIndex
)[PubKeyIndex
] = ++CopiedKey
;
728 Variable store garbage collection and reclaim operation.
730 If ReclaimPubKeyStore is FALSE, reclaim variable space by deleting the obsoleted varaibles.
731 If ReclaimPubKeyStore is TRUE, reclaim invalid key in public key database and update the PubKeyIndex
732 for all the count-based authenticate variable in NV storage.
734 @param[in] VariableBase Base address of variable store.
735 @param[out] LastVariableOffset Offset of last variable.
736 @param[in] IsVolatile The variable store is volatile or not;
737 if it is non-volatile, need FTW.
738 @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.
739 @param[in] NewVariable Pointer to new variable.
740 @param[in] NewVariableSize New variable size.
741 @param[in] ReclaimPubKeyStore Reclaim for public key database or not.
743 @return EFI_SUCCESS Reclaim operation has finished successfully.
744 @return EFI_OUT_OF_RESOURCES No enough memory resources or variable space.
745 @return EFI_DEVICE_ERROR The public key database doesn't exist.
746 @return Others Unexpect error happened during reclaim operation.
751 IN EFI_PHYSICAL_ADDRESS VariableBase
,
752 OUT UINTN
*LastVariableOffset
,
753 IN BOOLEAN IsVolatile
,
754 IN OUT VARIABLE_POINTER_TRACK
*UpdatingPtrTrack
,
755 IN VARIABLE_HEADER
*NewVariable
,
756 IN UINTN NewVariableSize
,
757 IN BOOLEAN ReclaimPubKeyStore
760 VARIABLE_HEADER
*Variable
;
761 VARIABLE_HEADER
*AddedVariable
;
762 VARIABLE_HEADER
*NextVariable
;
763 VARIABLE_HEADER
*NextAddedVariable
;
764 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
766 UINTN MaximumBufferSize
;
774 UINTN CommonVariableTotalSize
;
775 UINTN HwErrVariableTotalSize
;
776 UINT32
*NewPubKeyIndex
;
777 UINT8
*NewPubKeyStore
;
778 UINT32 NewPubKeySize
;
779 VARIABLE_HEADER
*PubKeyHeader
;
780 VARIABLE_HEADER
*UpdatingVariable
;
781 VARIABLE_HEADER
*UpdatingInDeletedTransition
;
783 UpdatingVariable
= NULL
;
784 UpdatingInDeletedTransition
= NULL
;
785 if (UpdatingPtrTrack
!= NULL
) {
786 UpdatingVariable
= UpdatingPtrTrack
->CurrPtr
;
787 UpdatingInDeletedTransition
= UpdatingPtrTrack
->InDeletedTransitionPtr
;
790 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
792 CommonVariableTotalSize
= 0;
793 HwErrVariableTotalSize
= 0;
794 NewPubKeyIndex
= NULL
;
795 NewPubKeyStore
= NULL
;
801 // Start Pointers for the variable.
803 Variable
= GetStartPointer (VariableStoreHeader
);
804 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
806 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
807 NextVariable
= GetNextVariablePtr (Variable
);
808 if ((Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) &&
809 Variable
!= UpdatingVariable
&&
810 Variable
!= UpdatingInDeletedTransition
812 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
813 MaximumBufferSize
+= VariableSize
;
816 Variable
= NextVariable
;
819 if (NewVariable
!= NULL
) {
821 // Add the new variable size.
823 MaximumBufferSize
+= NewVariableSize
;
827 // Reserve the 1 Bytes with Oxff to identify the
828 // end of the variable buffer.
830 MaximumBufferSize
+= 1;
831 ValidBuffer
= AllocatePool (MaximumBufferSize
);
832 if (ValidBuffer
== NULL
) {
833 return EFI_OUT_OF_RESOURCES
;
837 // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache
838 // as the buffer to reduce SMRAM consumption for SMM variable driver.
840 MaximumBufferSize
= mNvVariableCache
->Size
;
841 ValidBuffer
= (UINT8
*) mNvVariableCache
;
844 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
847 // Copy variable store header.
849 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
850 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
852 if (ReclaimPubKeyStore
) {
853 ASSERT (IsVolatile
== FALSE
);
855 // Trim the PubKeyStore and get new PubKeyIndex.
857 Status
= PubKeyStoreFilter (
864 if (EFI_ERROR (Status
)) {
867 ASSERT ((NewPubKeyIndex
!= NULL
) && (NewPubKeyStore
!= NULL
));
870 // Refresh the PubKeyIndex for all valid variables (ADDED and IN_DELETED_TRANSITION).
872 Variable
= GetStartPointer (VariableStoreHeader
);
873 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
874 NextVariable
= GetNextVariablePtr (Variable
);
875 if (Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
876 if ((StrCmp (GetVariableNamePtr (Variable
), AUTHVAR_KEYDB_NAME
) == 0) &&
877 (CompareGuid (&Variable
->VendorGuid
, &gEfiAuthenticatedVariableGuid
))) {
879 // Skip the public key database, it will be reinstalled later.
881 PubKeyHeader
= Variable
;
882 Variable
= NextVariable
;
886 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
887 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
888 ((VARIABLE_HEADER
*) CurrPtr
)->PubKeyIndex
= NewPubKeyIndex
[Variable
->PubKeyIndex
];
889 CurrPtr
+= VariableSize
;
890 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
891 HwErrVariableTotalSize
+= VariableSize
;
892 } else if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
893 CommonVariableTotalSize
+= VariableSize
;
896 Variable
= NextVariable
;
900 // Reinstall the new public key database.
902 ASSERT (PubKeyHeader
!= NULL
);
903 if (PubKeyHeader
== NULL
) {
904 Status
= EFI_DEVICE_ERROR
;
907 CopyMem (CurrPtr
, (UINT8
*) PubKeyHeader
, sizeof (VARIABLE_HEADER
));
908 Variable
= (VARIABLE_HEADER
*) CurrPtr
;
909 Variable
->DataSize
= NewPubKeySize
;
910 StrCpy (GetVariableNamePtr (Variable
), GetVariableNamePtr (PubKeyHeader
));
911 CopyMem (GetVariableDataPtr (Variable
), NewPubKeyStore
, NewPubKeySize
);
912 CurrPtr
= (UINT8
*) GetNextVariablePtr (Variable
);
913 CommonVariableTotalSize
+= (UINTN
) CurrPtr
- (UINTN
) Variable
;
916 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
918 Variable
= GetStartPointer (VariableStoreHeader
);
919 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
920 NextVariable
= GetNextVariablePtr (Variable
);
921 if (Variable
!= UpdatingVariable
&& Variable
->State
== VAR_ADDED
) {
922 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
923 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
924 CurrPtr
+= VariableSize
;
925 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
926 HwErrVariableTotalSize
+= VariableSize
;
927 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
928 CommonVariableTotalSize
+= VariableSize
;
931 Variable
= NextVariable
;
935 // Reinstall all in delete transition variables.
937 Variable
= GetStartPointer (VariableStoreHeader
);
938 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
939 NextVariable
= GetNextVariablePtr (Variable
);
940 if (Variable
!= UpdatingVariable
&& Variable
!= UpdatingInDeletedTransition
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
943 // Buffer has cached all ADDED variable.
944 // Per IN_DELETED variable, we have to guarantee that
945 // no ADDED one in previous buffer.
949 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
950 while (IsValidVariableHeader (AddedVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
))) {
951 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
952 NameSize
= NameSizeOfVariable (AddedVariable
);
953 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
954 NameSize
== NameSizeOfVariable (Variable
)
956 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
957 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
958 if (CompareMem (Point0
, Point1
, NameSize
) == 0) {
963 AddedVariable
= NextAddedVariable
;
967 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
969 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
970 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
971 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
972 CurrPtr
+= VariableSize
;
973 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
974 HwErrVariableTotalSize
+= VariableSize
;
975 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
976 CommonVariableTotalSize
+= VariableSize
;
981 Variable
= NextVariable
;
985 // Install the new variable if it is not NULL.
987 if (NewVariable
!= NULL
) {
988 if ((UINTN
) (CurrPtr
- ValidBuffer
) + NewVariableSize
> VariableStoreHeader
->Size
) {
990 // No enough space to store the new variable.
992 Status
= EFI_OUT_OF_RESOURCES
;
996 if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
997 HwErrVariableTotalSize
+= NewVariableSize
;
998 } else if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
999 CommonVariableTotalSize
+= NewVariableSize
;
1001 if ((HwErrVariableTotalSize
> PcdGet32 (PcdHwErrStorageSize
)) ||
1002 (CommonVariableTotalSize
> VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
))) {
1004 // No enough space to store the new variable by NV or NV+HR attribute.
1006 Status
= EFI_OUT_OF_RESOURCES
;
1011 CopyMem (CurrPtr
, (UINT8
*) NewVariable
, NewVariableSize
);
1012 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
1013 if (UpdatingVariable
!= NULL
) {
1014 UpdatingPtrTrack
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)UpdatingPtrTrack
->StartPtr
+ ((UINTN
)CurrPtr
- (UINTN
)GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
)));
1015 UpdatingPtrTrack
->InDeletedTransitionPtr
= NULL
;
1017 CurrPtr
+= NewVariableSize
;
1023 // If volatile variable store, just copy valid buffer.
1025 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
1026 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- ValidBuffer
));
1027 *LastVariableOffset
= (UINTN
) (CurrPtr
- ValidBuffer
);
1028 Status
= EFI_SUCCESS
;
1031 // If non-volatile variable store, perform FTW here.
1033 Status
= FtwVariableSpace (
1035 (VARIABLE_STORE_HEADER
*) ValidBuffer
1037 if (!EFI_ERROR (Status
)) {
1038 *LastVariableOffset
= (UINTN
) (CurrPtr
- ValidBuffer
);
1039 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
1040 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
1042 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
1043 while (IsValidVariableHeader (NextVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
))) {
1044 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
1045 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1046 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
1047 } else if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1048 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
1051 NextVariable
= GetNextVariablePtr (NextVariable
);
1053 *LastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableBase
;
1059 FreePool (ValidBuffer
);
1062 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.
1064 CopyMem (mNvVariableCache
, (UINT8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
1066 if (NewPubKeyStore
!= NULL
) {
1067 FreePool (NewPubKeyStore
);
1070 if (NewPubKeyIndex
!= NULL
) {
1071 FreePool (NewPubKeyIndex
);
1079 Find the variable in the specified variable store.
1081 @param[in] VariableName Name of the variable to be found
1082 @param[in] VendorGuid Vendor GUID to be found.
1083 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
1084 check at runtime when searching variable.
1085 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.
1087 @retval EFI_SUCCESS Variable found successfully
1088 @retval EFI_NOT_FOUND Variable not found
1092 IN CHAR16
*VariableName
,
1093 IN EFI_GUID
*VendorGuid
,
1094 IN BOOLEAN IgnoreRtCheck
,
1095 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
1098 VARIABLE_HEADER
*InDeletedVariable
;
1101 PtrTrack
->InDeletedTransitionPtr
= NULL
;
1104 // Find the variable by walk through HOB, volatile and non-volatile variable store.
1106 InDeletedVariable
= NULL
;
1108 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
1109 ; IsValidVariableHeader (PtrTrack
->CurrPtr
, PtrTrack
->EndPtr
)
1110 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
1112 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
1113 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
1115 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
1116 if (VariableName
[0] == 0) {
1117 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1118 InDeletedVariable
= PtrTrack
->CurrPtr
;
1120 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
1124 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
1125 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
1127 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
1128 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
1129 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1130 InDeletedVariable
= PtrTrack
->CurrPtr
;
1132 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
1142 PtrTrack
->CurrPtr
= InDeletedVariable
;
1143 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
1148 Finds variable in storage blocks of volatile and non-volatile storage areas.
1150 This code finds variable in storage blocks of volatile and non-volatile storage areas.
1151 If VariableName is an empty string, then we just return the first
1152 qualified variable without comparing VariableName and VendorGuid.
1153 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
1154 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
1155 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
1157 @param[in] VariableName Name of the variable to be found.
1158 @param[in] VendorGuid Vendor GUID to be found.
1159 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
1160 including the range searched and the target position.
1161 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
1162 base of volatile variable storage area, base of
1163 NV variable storage area, and a lock.
1164 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
1165 check at runtime when searching variable.
1167 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
1169 @retval EFI_SUCCESS Variable successfully found.
1170 @retval EFI_NOT_FOUND Variable not found
1175 IN CHAR16
*VariableName
,
1176 IN EFI_GUID
*VendorGuid
,
1177 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
1178 IN VARIABLE_GLOBAL
*Global
,
1179 IN BOOLEAN IgnoreRtCheck
1183 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1184 VARIABLE_STORE_TYPE Type
;
1186 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
1187 return EFI_INVALID_PARAMETER
;
1191 // 0: Volatile, 1: HOB, 2: Non-Volatile.
1192 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
1193 // make use of this mapping to implement search algorithm.
1195 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
1196 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
1197 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
1200 // Find the variable by walk through HOB, volatile and non-volatile variable store.
1202 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1203 if (VariableStoreHeader
[Type
] == NULL
) {
1207 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
1208 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
1209 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
1211 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
1212 if (!EFI_ERROR (Status
)) {
1216 return EFI_NOT_FOUND
;
1220 Get index from supported language codes according to language string.
1222 This code is used to get corresponding index in supported language codes. It can handle
1223 RFC4646 and ISO639 language tags.
1224 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
1225 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
1228 SupportedLang = "engfraengfra"
1230 Iso639Language = TRUE
1231 The return value is "0".
1233 SupportedLang = "en;fr;en-US;fr-FR"
1235 Iso639Language = FALSE
1236 The return value is "3".
1238 @param SupportedLang Platform supported language codes.
1239 @param Lang Configured language.
1240 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1242 @retval The index of language in the language codes.
1246 GetIndexFromSupportedLangCodes(
1247 IN CHAR8
*SupportedLang
,
1249 IN BOOLEAN Iso639Language
1253 UINTN CompareLength
;
1254 UINTN LanguageLength
;
1256 if (Iso639Language
) {
1257 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1258 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
1259 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
1261 // Successfully find the index of Lang string in SupportedLang string.
1263 Index
= Index
/ CompareLength
;
1271 // Compare RFC4646 language code
1274 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
1276 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
1278 // Skip ';' characters in SupportedLang
1280 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
1282 // Determine the length of the next language code in SupportedLang
1284 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
1286 if ((CompareLength
== LanguageLength
) &&
1287 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
1289 // Successfully find the index of Lang string in SupportedLang string.
1300 Get language string from supported language codes according to index.
1302 This code is used to get corresponding language strings in supported language codes. It can handle
1303 RFC4646 and ISO639 language tags.
1304 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
1305 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
1308 SupportedLang = "engfraengfra"
1310 Iso639Language = TRUE
1311 The return value is "fra".
1313 SupportedLang = "en;fr;en-US;fr-FR"
1315 Iso639Language = FALSE
1316 The return value is "fr".
1318 @param SupportedLang Platform supported language codes.
1319 @param Index The index in supported language codes.
1320 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1322 @retval The language string in the language codes.
1326 GetLangFromSupportedLangCodes (
1327 IN CHAR8
*SupportedLang
,
1329 IN BOOLEAN Iso639Language
1333 UINTN CompareLength
;
1337 Supported
= SupportedLang
;
1338 if (Iso639Language
) {
1340 // According to the index of Lang string in SupportedLang string to get the language.
1341 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1342 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1344 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1345 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1346 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1351 // Take semicolon as delimitation, sequentially traverse supported language codes.
1353 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1356 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1358 // Have completed the traverse, but not find corrsponding string.
1359 // This case is not allowed to happen.
1364 if (SubIndex
== Index
) {
1366 // According to the index of Lang string in SupportedLang string to get the language.
1367 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1368 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1370 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1371 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1376 // Skip ';' characters in Supported
1378 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1384 Returns a pointer to an allocated buffer that contains the best matching language
1385 from a set of supported languages.
1387 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1388 code types may not be mixed in a single call to this function. This function
1389 supports a variable argument list that allows the caller to pass in a prioritized
1390 list of language codes to test against all the language codes in SupportedLanguages.
1392 If SupportedLanguages is NULL, then ASSERT().
1394 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1395 contains a set of language codes in the format
1396 specified by Iso639Language.
1397 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1398 in ISO 639-2 format. If FALSE, then all language
1399 codes are assumed to be in RFC 4646 language format
1400 @param[in] ... A variable argument list that contains pointers to
1401 Null-terminated ASCII strings that contain one or more
1402 language codes in the format specified by Iso639Language.
1403 The first language code from each of these language
1404 code lists is used to determine if it is an exact or
1405 close match to any of the language codes in
1406 SupportedLanguages. Close matches only apply to RFC 4646
1407 language codes, and the matching algorithm from RFC 4647
1408 is used to determine if a close match is present. If
1409 an exact or close match is found, then the matching
1410 language code from SupportedLanguages is returned. If
1411 no matches are found, then the next variable argument
1412 parameter is evaluated. The variable argument list
1413 is terminated by a NULL.
1415 @retval NULL The best matching language could not be found in SupportedLanguages.
1416 @retval NULL There are not enough resources available to return the best matching
1418 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1419 language in SupportedLanguages.
1424 VariableGetBestLanguage (
1425 IN CONST CHAR8
*SupportedLanguages
,
1426 IN BOOLEAN Iso639Language
,
1432 UINTN CompareLength
;
1433 UINTN LanguageLength
;
1434 CONST CHAR8
*Supported
;
1437 if (SupportedLanguages
== NULL
) {
1441 VA_START (Args
, Iso639Language
);
1442 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1444 // Default to ISO 639-2 mode
1447 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1450 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1452 if (!Iso639Language
) {
1453 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1457 // Trim back the length of Language used until it is empty
1459 while (LanguageLength
> 0) {
1461 // Loop through all language codes in SupportedLanguages
1463 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1465 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1467 if (!Iso639Language
) {
1469 // Skip ';' characters in Supported
1471 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1473 // Determine the length of the next language code in Supported
1475 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1477 // If Language is longer than the Supported, then skip to the next language
1479 if (LanguageLength
> CompareLength
) {
1484 // See if the first LanguageLength characters in Supported match Language
1486 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1489 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1490 Buffer
[CompareLength
] = '\0';
1491 return CopyMem (Buffer
, Supported
, CompareLength
);
1495 if (Iso639Language
) {
1497 // If ISO 639 mode, then each language can only be tested once
1502 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1504 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1511 // No matches were found
1517 This function is to check if the remaining variable space is enough to set
1518 all Variables from argument list successfully. The purpose of the check
1519 is to keep the consistency of the Variables to be in variable storage.
1521 Note: Variables are assumed to be in same storage.
1522 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1523 so follow the argument sequence to check the Variables.
1525 @param[in] Attributes Variable attributes for Variable entries.
1526 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1527 A NULL terminates the list. The VariableSize of
1528 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
1529 It will be changed to variable total size as output.
1531 @retval TRUE Have enough variable space to set the Variables successfully.
1532 @retval FALSE No enough variable space to set the Variables successfully.
1537 CheckRemainingSpaceForConsistency (
1538 IN UINT32 Attributes
,
1544 VARIABLE_ENTRY_CONSISTENCY
*VariableEntry
;
1545 UINT64 MaximumVariableStorageSize
;
1546 UINT64 RemainingVariableStorageSize
;
1547 UINT64 MaximumVariableSize
;
1548 UINTN TotalNeededSize
;
1549 UINTN OriginalVarSize
;
1550 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1551 VARIABLE_POINTER_TRACK VariablePtrTrack
;
1552 VARIABLE_HEADER
*NextVariable
;
1557 // Non-Volatile related.
1559 VariableStoreHeader
= mNvVariableCache
;
1561 Status
= VariableServiceQueryVariableInfoInternal (
1563 &MaximumVariableStorageSize
,
1564 &RemainingVariableStorageSize
,
1565 &MaximumVariableSize
1567 ASSERT_EFI_ERROR (Status
);
1569 TotalNeededSize
= 0;
1570 VA_START (Args
, Attributes
);
1571 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1572 while (VariableEntry
!= NULL
) {
1574 // Calculate variable total size.
1576 VarNameSize
= StrSize (VariableEntry
->Name
);
1577 VarNameSize
+= GET_PAD_SIZE (VarNameSize
);
1578 VarDataSize
= VariableEntry
->VariableSize
;
1579 VarDataSize
+= GET_PAD_SIZE (VarDataSize
);
1580 VariableEntry
->VariableSize
= HEADER_ALIGN (sizeof (VARIABLE_HEADER
) + VarNameSize
+ VarDataSize
);
1582 TotalNeededSize
+= VariableEntry
->VariableSize
;
1583 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1587 if (RemainingVariableStorageSize
>= TotalNeededSize
) {
1589 // Already have enough space.
1592 } else if (AtRuntime ()) {
1594 // At runtime, no reclaim.
1595 // The original variable space of Variables can't be reused.
1600 VA_START (Args
, Attributes
);
1601 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1602 while (VariableEntry
!= NULL
) {
1604 // Check if Variable[Index] has been present and get its size.
1606 OriginalVarSize
= 0;
1607 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
1608 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
1609 Status
= FindVariableEx (
1610 VariableEntry
->Name
,
1611 VariableEntry
->Guid
,
1615 if (!EFI_ERROR (Status
)) {
1617 // Get size of Variable[Index].
1619 NextVariable
= GetNextVariablePtr (VariablePtrTrack
.CurrPtr
);
1620 OriginalVarSize
= (UINTN
) NextVariable
- (UINTN
) VariablePtrTrack
.CurrPtr
;
1622 // Add the original size of Variable[Index] to remaining variable storage size.
1624 RemainingVariableStorageSize
+= OriginalVarSize
;
1626 if (VariableEntry
->VariableSize
> RemainingVariableStorageSize
) {
1628 // No enough space for Variable[Index].
1634 // Sub the (new) size of Variable[Index] from remaining variable storage size.
1636 RemainingVariableStorageSize
-= VariableEntry
->VariableSize
;
1637 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1645 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1647 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1649 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1650 and are read-only. Therefore, in variable driver, only store the original value for other use.
1652 @param[in] VariableName Name of variable.
1654 @param[in] Data Variable data.
1656 @param[in] DataSize Size of data. 0 means delete.
1658 @retval EFI_SUCCESS The update operation is successful or ignored.
1659 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.
1660 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.
1661 @retval Others Other errors happened during the update operation.
1665 AutoUpdateLangVariable (
1666 IN CHAR16
*VariableName
,
1672 CHAR8
*BestPlatformLang
;
1676 VARIABLE_POINTER_TRACK Variable
;
1677 BOOLEAN SetLanguageCodes
;
1678 VARIABLE_ENTRY_CONSISTENCY VariableEntry
[2];
1681 // Don't do updates for delete operation
1683 if (DataSize
== 0) {
1687 SetLanguageCodes
= FALSE
;
1689 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
) == 0) {
1691 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1694 return EFI_WRITE_PROTECTED
;
1697 SetLanguageCodes
= TRUE
;
1700 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1701 // Therefore, in variable driver, only store the original value for other use.
1703 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1704 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1706 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1707 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1710 // PlatformLang holds a single language from PlatformLangCodes,
1711 // so the size of PlatformLangCodes is enough for the PlatformLang.
1713 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1714 FreePool (mVariableModuleGlobal
->PlatformLang
);
1716 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1717 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1719 } else if (StrCmp (VariableName
, EFI_LANG_CODES_VARIABLE_NAME
) == 0) {
1721 // LangCodes is a volatile variable, so it can not be updated at runtime.
1724 return EFI_WRITE_PROTECTED
;
1727 SetLanguageCodes
= TRUE
;
1730 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1731 // Therefore, in variable driver, only store the original value for other use.
1733 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1734 FreePool (mVariableModuleGlobal
->LangCodes
);
1736 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1737 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1740 if (SetLanguageCodes
1741 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1742 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1744 // Update Lang if PlatformLang is already set
1745 // Update PlatformLang if Lang is already set
1747 Status
= FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1748 if (!EFI_ERROR (Status
)) {
1752 VariableName
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1753 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1754 DataSize
= Variable
.CurrPtr
->DataSize
;
1756 Status
= FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1757 if (!EFI_ERROR (Status
)) {
1759 // Update PlatformLang
1761 VariableName
= EFI_LANG_VARIABLE_NAME
;
1762 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1763 DataSize
= Variable
.CurrPtr
->DataSize
;
1766 // Neither PlatformLang nor Lang is set, directly return
1773 Status
= EFI_SUCCESS
;
1776 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1778 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1780 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_VARIABLE_NAME
) == 0) {
1782 // Update Lang when PlatformLangCodes/LangCodes were set.
1784 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1786 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1788 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1789 if (BestPlatformLang
!= NULL
) {
1791 // Get the corresponding index in language codes.
1793 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1796 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1798 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1801 // Check the variable space for both Lang and PlatformLang variable.
1803 VariableEntry
[0].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1804 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1805 VariableEntry
[0].Name
= EFI_LANG_VARIABLE_NAME
;
1807 VariableEntry
[1].VariableSize
= AsciiStrSize (BestPlatformLang
);
1808 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1809 VariableEntry
[1].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1810 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1812 // No enough variable space to set both Lang and PlatformLang successfully.
1814 Status
= EFI_OUT_OF_RESOURCES
;
1817 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1819 FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1821 Status
= UpdateVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestLang
,
1822 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, 0, 0, &Variable
, NULL
);
1825 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang
, BestLang
, Status
));
1829 } else if (StrCmp (VariableName
, EFI_LANG_VARIABLE_NAME
) == 0) {
1831 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1833 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1835 // When setting Lang, firstly get most matched language string from supported language codes.
1837 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1838 if (BestLang
!= NULL
) {
1840 // Get the corresponding index in language codes.
1842 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1845 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1847 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1850 // Check the variable space for both PlatformLang and Lang variable.
1852 VariableEntry
[0].VariableSize
= AsciiStrSize (BestPlatformLang
);
1853 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1854 VariableEntry
[0].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1856 VariableEntry
[1].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1857 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1858 VariableEntry
[1].Name
= EFI_LANG_VARIABLE_NAME
;
1859 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1861 // No enough variable space to set both PlatformLang and Lang successfully.
1863 Status
= EFI_OUT_OF_RESOURCES
;
1866 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1868 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1870 Status
= UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestPlatformLang
,
1871 AsciiStrSize (BestPlatformLang
), Attributes
, 0, 0, &Variable
, NULL
);
1874 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang
, BestPlatformLang
, Status
));
1879 if (SetLanguageCodes
) {
1881 // Continue to set PlatformLangCodes or LangCodes.
1890 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
1891 index of associated public key is needed.
1893 @param[in] VariableName Name of variable.
1894 @param[in] VendorGuid Guid of variable.
1895 @param[in] Data Variable data.
1896 @param[in] DataSize Size of data. 0 means delete.
1897 @param[in] Attributes Attributes of the variable.
1898 @param[in] KeyIndex Index of associated public key.
1899 @param[in] MonotonicCount Value of associated monotonic count.
1900 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.
1901 @param[in] TimeStamp Value of associated TimeStamp.
1903 @retval EFI_SUCCESS The update operation is success.
1904 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1909 IN CHAR16
*VariableName
,
1910 IN EFI_GUID
*VendorGuid
,
1913 IN UINT32 Attributes OPTIONAL
,
1914 IN UINT32 KeyIndex OPTIONAL
,
1915 IN UINT64 MonotonicCount OPTIONAL
,
1916 IN OUT VARIABLE_POINTER_TRACK
*CacheVariable
,
1917 IN EFI_TIME
*TimeStamp OPTIONAL
1921 VARIABLE_HEADER
*NextVariable
;
1924 UINTN NonVolatileVarableStoreSize
;
1925 UINTN VarNameOffset
;
1926 UINTN VarDataOffset
;
1930 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1932 VARIABLE_POINTER_TRACK
*Variable
;
1933 VARIABLE_POINTER_TRACK NvVariable
;
1934 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1936 UINT8
*BufferForMerge
;
1937 UINTN MergedBufSize
;
1941 if (mVariableModuleGlobal
->FvbInstance
== NULL
) {
1943 // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
1945 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1947 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1949 return EFI_NOT_AVAILABLE_YET
;
1950 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1952 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1953 // The authenticated variable perhaps is not initialized, just return here.
1955 return EFI_NOT_AVAILABLE_YET
;
1959 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1960 Variable
= CacheVariable
;
1963 // Update/Delete existing NV variable.
1964 // CacheVariable points to the variable in the memory copy of Flash area
1965 // Now let Variable points to the same variable in Flash area.
1967 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1968 Variable
= &NvVariable
;
1969 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1970 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1971 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1972 if (CacheVariable
->InDeletedTransitionPtr
!= NULL
) {
1973 Variable
->InDeletedTransitionPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->InDeletedTransitionPtr
- (UINTN
)CacheVariable
->StartPtr
));
1975 Variable
->InDeletedTransitionPtr
= NULL
;
1977 Variable
->Volatile
= FALSE
;
1980 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1983 // Tricky part: Use scratch data area at the end of volatile variable store
1984 // as a temporary storage.
1986 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1987 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1988 SetMem (NextVariable
, ScratchSize
, 0xff);
1991 if (Variable
->CurrPtr
!= NULL
) {
1993 // Update/Delete existing variable.
1997 // If AtRuntime and the variable is Volatile and Runtime Access,
1998 // the volatile is ReadOnly, and SetVariable should be aborted and
1999 // return EFI_WRITE_PROTECTED.
2001 if (Variable
->Volatile
) {
2002 Status
= EFI_WRITE_PROTECTED
;
2006 // Only variable that have NV attributes can be updated/deleted in Runtime.
2008 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2009 Status
= EFI_INVALID_PARAMETER
;
2014 // Only variable that have RT attributes can be updated/deleted in Runtime.
2016 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) {
2017 Status
= EFI_INVALID_PARAMETER
;
2023 // Setting a data variable with no access, or zero DataSize attributes
2024 // causes it to be deleted.
2025 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
2026 // not delete the variable.
2028 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0))|| ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
2029 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2031 // Both ADDED and IN_DELETED_TRANSITION variable are present,
2032 // set IN_DELETED_TRANSITION one to DELETED state first.
2034 State
= Variable
->InDeletedTransitionPtr
->State
;
2035 State
&= VAR_DELETED
;
2036 Status
= UpdateVariableStore (
2037 &mVariableModuleGlobal
->VariableGlobal
,
2041 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2045 if (!EFI_ERROR (Status
)) {
2046 if (!Variable
->Volatile
) {
2047 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2048 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2055 State
= Variable
->CurrPtr
->State
;
2056 State
&= VAR_DELETED
;
2058 Status
= UpdateVariableStore (
2059 &mVariableModuleGlobal
->VariableGlobal
,
2063 (UINTN
) &Variable
->CurrPtr
->State
,
2067 if (!EFI_ERROR (Status
)) {
2068 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
2069 if (!Variable
->Volatile
) {
2070 CacheVariable
->CurrPtr
->State
= State
;
2071 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2077 // If the variable is marked valid, and the same data has been passed in,
2078 // then return to the caller immediately.
2080 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
2081 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0) &&
2082 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) &&
2083 (TimeStamp
== NULL
)) {
2085 // Variable content unchanged and no need to update timestamp, just return.
2087 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2088 Status
= EFI_SUCCESS
;
2090 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
2091 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
2094 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
2096 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
2098 // NOTE: From 0 to DataOffset of NextVariable is reserved for Variable Header and Name.
2099 // From DataOffset of NextVariable is to save the existing variable data.
2101 DataOffset
= sizeof (VARIABLE_HEADER
) + Variable
->CurrPtr
->NameSize
+ GET_PAD_SIZE (Variable
->CurrPtr
->NameSize
);
2102 BufferForMerge
= (UINT8
*) ((UINTN
) NextVariable
+ DataOffset
);
2103 CopyMem (BufferForMerge
, (UINT8
*) ((UINTN
) Variable
->CurrPtr
+ DataOffset
), Variable
->CurrPtr
->DataSize
);
2106 // Set Max Common Variable Data Size as default MaxDataSize
2108 MaxDataSize
= PcdGet32 (PcdMaxVariableSize
) - DataOffset
;
2110 if ((CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
2111 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) ||
2112 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0))) {
2114 // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of
2115 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
2117 Status
= AppendSignatureList (
2119 Variable
->CurrPtr
->DataSize
,
2120 MaxDataSize
- Variable
->CurrPtr
->DataSize
,
2125 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2127 // Signature List is too long, Failed to Append.
2129 Status
= EFI_INVALID_PARAMETER
;
2133 if (MergedBufSize
== Variable
->CurrPtr
->DataSize
) {
2134 if ((TimeStamp
== NULL
) || CompareTimeStamp (TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2136 // New EFI_SIGNATURE_DATA is not found and timestamp is not later
2137 // than current timestamp, return EFI_SUCCESS directly.
2139 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2140 Status
= EFI_SUCCESS
;
2146 // For other Variables, append the new data to the end of existing data.
2147 // Max Harware error record variable data size is different from common variable
2149 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2150 MaxDataSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - DataOffset
;
2153 if (Variable
->CurrPtr
->DataSize
+ DataSize
> MaxDataSize
) {
2155 // Existing data size + new data size exceed maximum variable size limitation.
2157 Status
= EFI_INVALID_PARAMETER
;
2160 CopyMem ((UINT8
*) ((UINTN
) BufferForMerge
+ Variable
->CurrPtr
->DataSize
), Data
, DataSize
);
2161 MergedBufSize
= Variable
->CurrPtr
->DataSize
+ DataSize
;
2165 // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.
2167 Data
= BufferForMerge
;
2168 DataSize
= MergedBufSize
;
2173 // Mark the old variable as in delete transition.
2175 State
= Variable
->CurrPtr
->State
;
2176 State
&= VAR_IN_DELETED_TRANSITION
;
2178 Status
= UpdateVariableStore (
2179 &mVariableModuleGlobal
->VariableGlobal
,
2183 (UINTN
) &Variable
->CurrPtr
->State
,
2187 if (EFI_ERROR (Status
)) {
2190 if (!Variable
->Volatile
) {
2191 CacheVariable
->CurrPtr
->State
= State
;
2196 // Not found existing variable. Create a new variable.
2199 if ((DataSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
2200 Status
= EFI_SUCCESS
;
2205 // Make sure we are trying to create a new variable.
2206 // Setting a data variable with zero DataSize or no access attributes means to delete it.
2208 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
2209 Status
= EFI_NOT_FOUND
;
2214 // Only variable have NV|RT attribute can be created in Runtime.
2217 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
2218 Status
= EFI_INVALID_PARAMETER
;
2224 // Function part - create a new variable and copy the data.
2225 // Both update a variable and create a variable will come here.
2227 NextVariable
->StartId
= VARIABLE_DATA
;
2229 // NextVariable->State = VAR_ADDED;
2231 NextVariable
->Reserved
= 0;
2232 NextVariable
->PubKeyIndex
= KeyIndex
;
2233 NextVariable
->MonotonicCount
= MonotonicCount
;
2234 ZeroMem (&NextVariable
->TimeStamp
, sizeof (EFI_TIME
));
2236 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
2237 (TimeStamp
!= NULL
)) {
2238 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
2239 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
2242 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
2243 // when the new TimeStamp value is later than the current timestamp associated
2244 // with the variable, we need associate the new timestamp with the updated value.
2246 if (Variable
->CurrPtr
!= NULL
) {
2247 if (CompareTimeStamp (&Variable
->CurrPtr
->TimeStamp
, TimeStamp
)) {
2248 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
2255 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
2256 // Attributes bitmask parameter of a GetVariable() call.
2258 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
2260 VarNameOffset
= sizeof (VARIABLE_HEADER
);
2261 VarNameSize
= StrSize (VariableName
);
2263 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
2267 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
2270 // If DataReady is TRUE, it means the variable data has been saved into
2271 // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.
2275 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
2281 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
2283 // There will be pad bytes after Data, the NextVariable->NameSize and
2284 // NextVariable->DataSize should not include pad size so that variable
2285 // service can get actual size in GetVariable.
2287 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
2288 NextVariable
->DataSize
= (UINT32
)DataSize
;
2291 // The actual size of the variable that stores in storage should
2292 // include pad size.
2294 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
2295 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2297 // Create a nonvolatile variable.
2300 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
2301 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2302 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
2303 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
2304 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
2306 Status
= EFI_OUT_OF_RESOURCES
;
2310 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2313 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2314 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2318 HEADER_ALIGN (VarSize
),
2321 if (!EFI_ERROR (Status
)) {
2323 // The new variable has been integrated successfully during reclaiming.
2325 if (Variable
->CurrPtr
!= NULL
) {
2326 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2327 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2329 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
);
2330 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2336 // 1. Write variable header
2337 // 2. Set variable state to header valid
2338 // 3. Write variable data
2339 // 4. Set variable state to valid
2344 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
2345 Status
= UpdateVariableStore (
2346 &mVariableModuleGlobal
->VariableGlobal
,
2350 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2351 sizeof (VARIABLE_HEADER
),
2352 (UINT8
*) NextVariable
2355 if (EFI_ERROR (Status
)) {
2362 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
2363 Status
= UpdateVariableStore (
2364 &mVariableModuleGlobal
->VariableGlobal
,
2368 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2370 &NextVariable
->State
2373 if (EFI_ERROR (Status
)) {
2379 Status
= UpdateVariableStore (
2380 &mVariableModuleGlobal
->VariableGlobal
,
2384 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
2385 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
2386 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
2389 if (EFI_ERROR (Status
)) {
2395 NextVariable
->State
= VAR_ADDED
;
2396 Status
= UpdateVariableStore (
2397 &mVariableModuleGlobal
->VariableGlobal
,
2401 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2403 &NextVariable
->State
2406 if (EFI_ERROR (Status
)) {
2410 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2412 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2413 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2415 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2418 // update the memory copy of Flash region.
2420 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
2423 // Create a volatile variable.
2427 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2428 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
2430 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2433 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
2434 &mVariableModuleGlobal
->VolatileLastVariableOffset
,
2438 HEADER_ALIGN (VarSize
),
2441 if (!EFI_ERROR (Status
)) {
2443 // The new variable has been integrated successfully during reclaiming.
2445 if (Variable
->CurrPtr
!= NULL
) {
2446 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2447 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2449 UpdateVariableInfo (VariableName
, VendorGuid
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
);
2454 NextVariable
->State
= VAR_ADDED
;
2455 Status
= UpdateVariableStore (
2456 &mVariableModuleGlobal
->VariableGlobal
,
2460 mVariableModuleGlobal
->VolatileLastVariableOffset
,
2462 (UINT8
*) NextVariable
2465 if (EFI_ERROR (Status
)) {
2469 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2473 // Mark the old variable as deleted.
2475 if (!EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
2476 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2478 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
2479 // set IN_DELETED_TRANSITION one to DELETED state first.
2481 State
= Variable
->InDeletedTransitionPtr
->State
;
2482 State
&= VAR_DELETED
;
2483 Status
= UpdateVariableStore (
2484 &mVariableModuleGlobal
->VariableGlobal
,
2488 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2492 if (!EFI_ERROR (Status
)) {
2493 if (!Variable
->Volatile
) {
2494 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2495 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2502 State
= Variable
->CurrPtr
->State
;
2503 State
&= VAR_DELETED
;
2505 Status
= UpdateVariableStore (
2506 &mVariableModuleGlobal
->VariableGlobal
,
2510 (UINTN
) &Variable
->CurrPtr
->State
,
2514 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
2515 CacheVariable
->CurrPtr
->State
= State
;
2519 if (!EFI_ERROR (Status
)) {
2520 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2522 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2531 Check if a Unicode character is a hexadecimal character.
2533 This function checks if a Unicode character is a
2534 hexadecimal character. The valid hexadecimal character is
2535 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
2538 @param Char The character to check against.
2540 @retval TRUE If the Char is a hexadecmial character.
2541 @retval FALSE If the Char is not a hexadecmial character.
2546 IsHexaDecimalDigitCharacter (
2550 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
2555 This code checks if variable is hardware error record variable or not.
2557 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
2558 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
2560 @param VariableName Pointer to variable name.
2561 @param VendorGuid Variable Vendor Guid.
2563 @retval TRUE Variable is hardware error record variable.
2564 @retval FALSE Variable is not hardware error record variable.
2569 IsHwErrRecVariable (
2570 IN CHAR16
*VariableName
,
2571 IN EFI_GUID
*VendorGuid
2574 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
2575 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
2576 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
2577 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
2578 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
2579 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
2580 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
2588 This code checks if variable guid is global variable guid first.
2589 If yes, further check if variable name is in mGlobalVariableList or mGlobalVariableList2 and attributes matched.
2591 @param[in] VariableName Pointer to variable name.
2592 @param[in] VendorGuid Variable Vendor Guid.
2593 @param[in] Attributes Attributes of the variable.
2595 @retval EFI_SUCCESS Variable is not global variable, or Variable is global variable, variable name is in the lists and attributes matched.
2596 @retval EFI_INVALID_PARAMETER Variable is global variable, but variable name is not in the lists or attributes unmatched.
2601 CheckEfiGlobalVariable (
2602 IN CHAR16
*VariableName
,
2603 IN EFI_GUID
*VendorGuid
,
2604 IN UINT32 Attributes
2610 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)){
2612 // Try list 1, exactly match.
2614 for (Index
= 0; Index
< sizeof (mGlobalVariableList
)/sizeof (mGlobalVariableList
[0]); Index
++) {
2615 if ((StrCmp (mGlobalVariableList
[Index
].Name
, VariableName
) == 0) &&
2616 (Attributes
== 0 || (Attributes
& (~EFI_VARIABLE_APPEND_WRITE
)) == mGlobalVariableList
[Index
].Attributes
)) {
2624 NameLength
= StrLen (VariableName
) - 4;
2625 for (Index
= 0; Index
< sizeof (mGlobalVariableList2
)/sizeof (mGlobalVariableList2
[0]); Index
++) {
2626 if ((StrLen (VariableName
) == StrLen (mGlobalVariableList2
[Index
].Name
)) &&
2627 (StrnCmp (mGlobalVariableList2
[Index
].Name
, VariableName
, NameLength
) == 0) &&
2628 IsHexaDecimalDigitCharacter (VariableName
[NameLength
]) &&
2629 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 1]) &&
2630 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 2]) &&
2631 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 3]) &&
2632 (Attributes
== 0 || (Attributes
& (~EFI_VARIABLE_APPEND_WRITE
)) == mGlobalVariableList2
[Index
].Attributes
)) {
2637 DEBUG ((EFI_D_INFO
, "[Variable]: set global variable with invalid variable name or attributes - %g:%s:%x\n", VendorGuid
, VariableName
, Attributes
));
2638 return EFI_INVALID_PARAMETER
;
2645 Mark a variable that will become read-only after leaving the DXE phase of execution.
2647 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
2648 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
2649 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
2651 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
2652 as pending to be read-only.
2653 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
2654 Or VariableName is an empty string.
2655 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
2656 already been signaled.
2657 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
2661 VariableLockRequestToLock (
2662 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL
*This
,
2663 IN CHAR16
*VariableName
,
2664 IN EFI_GUID
*VendorGuid
2667 VARIABLE_ENTRY
*Entry
;
2669 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2670 return EFI_INVALID_PARAMETER
;
2674 return EFI_ACCESS_DENIED
;
2677 Entry
= AllocateRuntimePool (sizeof (*Entry
) + StrSize (VariableName
));
2678 if (Entry
== NULL
) {
2679 return EFI_OUT_OF_RESOURCES
;
2682 DEBUG ((EFI_D_INFO
, "[Variable] Lock: %g:%s\n", VendorGuid
, VariableName
));
2684 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2686 Entry
->Name
= (CHAR16
*) (Entry
+ 1);
2687 StrCpy (Entry
->Name
, VariableName
);
2688 CopyGuid (&Entry
->Guid
, VendorGuid
);
2689 InsertTailList (&mLockedVariableList
, &Entry
->Link
);
2691 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2697 This code checks if variable should be treated as read-only variable.
2699 @param[in] VariableName Name of the Variable.
2700 @param[in] VendorGuid GUID of the Variable.
2702 @retval TRUE This variable is read-only variable.
2703 @retval FALSE This variable is NOT read-only variable.
2707 IsReadOnlyVariable (
2708 IN CHAR16
*VariableName
,
2709 IN EFI_GUID
*VendorGuid
2712 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)) {
2713 if ((StrCmp (VariableName
, EFI_SETUP_MODE_NAME
) == 0) ||
2714 (StrCmp (VariableName
, EFI_SIGNATURE_SUPPORT_NAME
) == 0) ||
2715 (StrCmp (VariableName
, EFI_SECURE_BOOT_MODE_NAME
) == 0) ||
2716 (StrCmp (VariableName
, EFI_VENDOR_KEYS_VARIABLE_NAME
) == 0) ||
2717 (StrCmp (VariableName
, EFI_KEK_DEFAULT_VARIABLE_NAME
) == 0) ||
2718 (StrCmp (VariableName
, EFI_PK_DEFAULT_VARIABLE_NAME
) == 0) ||
2719 (StrCmp (VariableName
, EFI_DB_DEFAULT_VARIABLE_NAME
) == 0) ||
2720 (StrCmp (VariableName
, EFI_DBX_DEFAULT_VARIABLE_NAME
) == 0) ||
2721 (StrCmp (VariableName
, EFI_DBT_DEFAULT_VARIABLE_NAME
) == 0)) {
2731 This code finds variable in storage blocks (Volatile or Non-Volatile).
2733 Caution: This function may receive untrusted input.
2734 This function may be invoked in SMM mode, and datasize is external input.
2735 This function will do basic validation, before parse the data.
2737 @param VariableName Name of Variable to be found.
2738 @param VendorGuid Variable vendor GUID.
2739 @param Attributes Attribute value of the variable found.
2740 @param DataSize Size of Data found. If size is less than the
2741 data, this value contains the required size.
2742 @param Data Data pointer.
2744 @return EFI_INVALID_PARAMETER Invalid parameter.
2745 @return EFI_SUCCESS Find the specified variable.
2746 @return EFI_NOT_FOUND Not found.
2747 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2752 VariableServiceGetVariable (
2753 IN CHAR16
*VariableName
,
2754 IN EFI_GUID
*VendorGuid
,
2755 OUT UINT32
*Attributes OPTIONAL
,
2756 IN OUT UINTN
*DataSize
,
2761 VARIABLE_POINTER_TRACK Variable
;
2764 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2765 return EFI_INVALID_PARAMETER
;
2768 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2770 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2771 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2778 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2779 ASSERT (VarDataSize
!= 0);
2781 if (*DataSize
>= VarDataSize
) {
2783 Status
= EFI_INVALID_PARAMETER
;
2787 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2788 if (Attributes
!= NULL
) {
2789 *Attributes
= Variable
.CurrPtr
->Attributes
;
2792 *DataSize
= VarDataSize
;
2793 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2795 Status
= EFI_SUCCESS
;
2798 *DataSize
= VarDataSize
;
2799 Status
= EFI_BUFFER_TOO_SMALL
;
2804 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2812 This code Finds the Next available variable.
2814 Caution: This function may receive untrusted input.
2815 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2817 @param VariableNameSize Size of the variable name.
2818 @param VariableName Pointer to variable name.
2819 @param VendorGuid Variable Vendor Guid.
2821 @return EFI_INVALID_PARAMETER Invalid parameter.
2822 @return EFI_SUCCESS Find the specified variable.
2823 @return EFI_NOT_FOUND Not found.
2824 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2829 VariableServiceGetNextVariableName (
2830 IN OUT UINTN
*VariableNameSize
,
2831 IN OUT CHAR16
*VariableName
,
2832 IN OUT EFI_GUID
*VendorGuid
2835 VARIABLE_STORE_TYPE Type
;
2836 VARIABLE_POINTER_TRACK Variable
;
2837 VARIABLE_POINTER_TRACK VariableInHob
;
2838 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2841 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2843 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2844 return EFI_INVALID_PARAMETER
;
2847 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2849 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2850 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2854 if (VariableName
[0] != 0) {
2856 // If variable name is not NULL, get next variable.
2858 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2862 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2863 // The index and attributes mapping must be kept in this order as FindVariable
2864 // makes use of this mapping to implement search algorithm.
2866 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2867 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2868 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2872 // Switch from Volatile to HOB, to Non-Volatile.
2874 while (!IsValidVariableHeader (Variable
.CurrPtr
, Variable
.EndPtr
)) {
2876 // Find current storage index
2878 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2879 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2883 ASSERT (Type
< VariableStoreTypeMax
);
2885 // Switch to next storage
2887 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2888 if (VariableStoreHeader
[Type
] != NULL
) {
2893 // Capture the case that
2894 // 1. current storage is the last one, or
2895 // 2. no further storage
2897 if (Type
== VariableStoreTypeMax
) {
2898 Status
= EFI_NOT_FOUND
;
2901 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2902 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2903 Variable
.CurrPtr
= Variable
.StartPtr
;
2907 // Variable is found
2909 if (Variable
.CurrPtr
->State
== VAR_ADDED
|| Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2910 if (!AtRuntime () || ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
2911 if (Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2913 // If it is a IN_DELETED_TRANSITION variable,
2914 // and there is also a same ADDED one at the same time,
2917 VariablePtrTrack
.StartPtr
= Variable
.StartPtr
;
2918 VariablePtrTrack
.EndPtr
= Variable
.EndPtr
;
2919 Status
= FindVariableEx (
2920 GetVariableNamePtr (Variable
.CurrPtr
),
2921 &Variable
.CurrPtr
->VendorGuid
,
2925 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
== VAR_ADDED
) {
2926 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2932 // Don't return NV variable when HOB overrides it
2934 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2935 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2937 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2938 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2939 Status
= FindVariableEx (
2940 GetVariableNamePtr (Variable
.CurrPtr
),
2941 &Variable
.CurrPtr
->VendorGuid
,
2945 if (!EFI_ERROR (Status
)) {
2946 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2951 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2952 ASSERT (VarNameSize
!= 0);
2954 if (VarNameSize
<= *VariableNameSize
) {
2955 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2956 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2957 Status
= EFI_SUCCESS
;
2959 Status
= EFI_BUFFER_TOO_SMALL
;
2962 *VariableNameSize
= VarNameSize
;
2967 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2971 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2977 This code sets variable in storage blocks (Volatile or Non-Volatile).
2979 Caution: This function may receive untrusted input.
2980 This function may be invoked in SMM mode, and datasize and data are external input.
2981 This function will do basic validation, before parse the data.
2982 This function will parse the authentication carefully to avoid security issues, like
2983 buffer overflow, integer overflow.
2984 This function will check attribute carefully to avoid authentication bypass.
2986 @param VariableName Name of Variable to be found.
2987 @param VendorGuid Variable vendor GUID.
2988 @param Attributes Attribute value of the variable found
2989 @param DataSize Size of Data found. If size is less than the
2990 data, this value contains the required size.
2991 @param Data Data pointer.
2993 @return EFI_INVALID_PARAMETER Invalid parameter.
2994 @return EFI_SUCCESS Set successfully.
2995 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2996 @return EFI_NOT_FOUND Not found.
2997 @return EFI_WRITE_PROTECTED Variable is read-only.
3002 VariableServiceSetVariable (
3003 IN CHAR16
*VariableName
,
3004 IN EFI_GUID
*VendorGuid
,
3005 IN UINT32 Attributes
,
3010 VARIABLE_POINTER_TRACK Variable
;
3012 VARIABLE_HEADER
*NextVariable
;
3013 EFI_PHYSICAL_ADDRESS Point
;
3016 VARIABLE_ENTRY
*Entry
;
3019 // Check input parameters.
3021 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
3022 return EFI_INVALID_PARAMETER
;
3025 if (IsReadOnlyVariable (VariableName
, VendorGuid
)) {
3026 return EFI_WRITE_PROTECTED
;
3029 if (DataSize
!= 0 && Data
== NULL
) {
3030 return EFI_INVALID_PARAMETER
;
3034 // Check for reserverd bit in variable attribute.
3036 if ((Attributes
& (~EFI_VARIABLE_ATTRIBUTES_MASK
)) != 0) {
3037 return EFI_INVALID_PARAMETER
;
3041 // Make sure if runtime bit is set, boot service bit is set also.
3043 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
3044 return EFI_INVALID_PARAMETER
;
3048 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
3049 // cannot be set both.
3051 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
3052 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
3053 return EFI_INVALID_PARAMETER
;
3056 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
3057 if (DataSize
< AUTHINFO_SIZE
) {
3059 // Try to write Authenticated Variable without AuthInfo.
3061 return EFI_SECURITY_VIOLATION
;
3063 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
3064 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
3066 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
3068 if (DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
||
3069 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
)) ||
3070 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) {
3071 return EFI_SECURITY_VIOLATION
;
3073 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
3075 PayloadSize
= DataSize
;
3078 if ((UINTN
)(~0) - PayloadSize
< StrSize(VariableName
)){
3080 // Prevent whole variable size overflow
3082 return EFI_INVALID_PARAMETER
;
3086 // The size of the VariableName, including the Unicode Null in bytes plus
3087 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
3088 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
3090 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3091 if (StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
)) {
3092 return EFI_INVALID_PARAMETER
;
3094 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
3095 return EFI_INVALID_PARAMETER
;
3099 // The size of the VariableName, including the Unicode Null in bytes plus
3100 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
3102 if (StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
)) {
3103 return EFI_INVALID_PARAMETER
;
3107 Status
= CheckEfiGlobalVariable (VariableName
, VendorGuid
, Attributes
);
3108 if (EFI_ERROR (Status
)) {
3112 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3115 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
3117 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
3118 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
3120 // Parse non-volatile variable data and get last variable offset.
3122 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
3123 while (IsValidVariableHeader (NextVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))) {
3124 NextVariable
= GetNextVariablePtr (NextVariable
);
3126 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
3129 if (mEndOfDxe
&& mEnableLocking
) {
3131 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.
3133 for ( Link
= GetFirstNode (&mLockedVariableList
)
3134 ; !IsNull (&mLockedVariableList
, Link
)
3135 ; Link
= GetNextNode (&mLockedVariableList
, Link
)
3137 Entry
= BASE_CR (Link
, VARIABLE_ENTRY
, Link
);
3138 if (CompareGuid (&Entry
->Guid
, VendorGuid
) && (StrCmp (Entry
->Name
, VariableName
) == 0)) {
3139 Status
= EFI_WRITE_PROTECTED
;
3140 DEBUG ((EFI_D_INFO
, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid
, VariableName
));
3147 // Check whether the input variable is already existed.
3149 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
3150 if (!EFI_ERROR (Status
)) {
3151 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
3152 Status
= EFI_WRITE_PROTECTED
;
3155 if (Attributes
!= 0 && (Attributes
& (~EFI_VARIABLE_APPEND_WRITE
)) != Variable
.CurrPtr
->Attributes
) {
3157 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not
3158 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:
3159 // 1. No access attributes specified
3160 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE
3162 Status
= EFI_INVALID_PARAMETER
;
3167 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate
)) {
3169 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
3171 Status
= AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
3172 if (EFI_ERROR (Status
)) {
3174 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.
3181 // Process PK, KEK, Sigdb seperately.
3183 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
3184 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, TRUE
);
3185 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
3186 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
3187 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
3188 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) {
3189 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
3190 if (EFI_ERROR (Status
)) {
3191 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
3194 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
3198 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
3199 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3201 if (!AtRuntime ()) {
3202 if (!EFI_ERROR (Status
)) {
3215 This code returns information about the EFI variables.
3217 Caution: This function may receive untrusted input.
3218 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3220 @param Attributes Attributes bitmask to specify the type of variables
3221 on which to return information.
3222 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
3223 for the EFI variables associated with the attributes specified.
3224 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
3225 for EFI variables associated with the attributes specified.
3226 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
3227 associated with the attributes specified.
3229 @return EFI_SUCCESS Query successfully.
3234 VariableServiceQueryVariableInfoInternal (
3235 IN UINT32 Attributes
,
3236 OUT UINT64
*MaximumVariableStorageSize
,
3237 OUT UINT64
*RemainingVariableStorageSize
,
3238 OUT UINT64
*MaximumVariableSize
3241 VARIABLE_HEADER
*Variable
;
3242 VARIABLE_HEADER
*NextVariable
;
3243 UINT64 VariableSize
;
3244 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3245 UINT64 CommonVariableTotalSize
;
3246 UINT64 HwErrVariableTotalSize
;
3248 VARIABLE_POINTER_TRACK VariablePtrTrack
;
3250 CommonVariableTotalSize
= 0;
3251 HwErrVariableTotalSize
= 0;
3253 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
3255 // Query is Volatile related.
3257 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
3260 // Query is Non-Volatile related.
3262 VariableStoreHeader
= mNvVariableCache
;
3266 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
3267 // with the storage size (excluding the storage header size).
3269 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
3272 // Harware error record variable needs larger size.
3274 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3275 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
3276 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
3278 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
3279 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
3280 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
3284 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
3286 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
3290 // Point to the starting address of the variables.
3292 Variable
= GetStartPointer (VariableStoreHeader
);
3295 // Now walk through the related variable store.
3297 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
3298 NextVariable
= GetNextVariablePtr (Variable
);
3299 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
3303 // We don't take the state of the variables in mind
3304 // when calculating RemainingVariableStorageSize,
3305 // since the space occupied by variables not marked with
3306 // VAR_ADDED is not allowed to be reclaimed in Runtime.
3308 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3309 HwErrVariableTotalSize
+= VariableSize
;
3311 CommonVariableTotalSize
+= VariableSize
;
3315 // Only care about Variables with State VAR_ADDED, because
3316 // the space not marked as VAR_ADDED is reclaimable now.
3318 if (Variable
->State
== VAR_ADDED
) {
3319 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3320 HwErrVariableTotalSize
+= VariableSize
;
3322 CommonVariableTotalSize
+= VariableSize
;
3324 } else if (Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
3326 // If it is a IN_DELETED_TRANSITION variable,
3327 // and there is not also a same ADDED one at the same time,
3328 // this IN_DELETED_TRANSITION variable is valid.
3330 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
3331 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
3332 Status
= FindVariableEx (
3333 GetVariableNamePtr (Variable
),
3334 &Variable
->VendorGuid
,
3338 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
!= VAR_ADDED
) {
3339 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3340 HwErrVariableTotalSize
+= VariableSize
;
3342 CommonVariableTotalSize
+= VariableSize
;
3349 // Go to the next one.
3351 Variable
= NextVariable
;
3354 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
3355 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
3357 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
3360 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
3361 *MaximumVariableSize
= 0;
3362 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
3363 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
3371 This code returns information about the EFI variables.
3373 Caution: This function may receive untrusted input.
3374 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3376 @param Attributes Attributes bitmask to specify the type of variables
3377 on which to return information.
3378 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
3379 for the EFI variables associated with the attributes specified.
3380 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
3381 for EFI variables associated with the attributes specified.
3382 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
3383 associated with the attributes specified.
3385 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
3386 @return EFI_SUCCESS Query successfully.
3387 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
3392 VariableServiceQueryVariableInfo (
3393 IN UINT32 Attributes
,
3394 OUT UINT64
*MaximumVariableStorageSize
,
3395 OUT UINT64
*RemainingVariableStorageSize
,
3396 OUT UINT64
*MaximumVariableSize
3401 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
3402 return EFI_INVALID_PARAMETER
;
3405 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
3407 // Make sure the Attributes combination is supported by the platform.
3409 return EFI_UNSUPPORTED
;
3410 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
3412 // Make sure if runtime bit is set, boot service bit is set also.
3414 return EFI_INVALID_PARAMETER
;
3415 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
3417 // Make sure RT Attribute is set if we are in Runtime phase.
3419 return EFI_INVALID_PARAMETER
;
3420 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3422 // Make sure Hw Attribute is set with NV.
3424 return EFI_INVALID_PARAMETER
;
3427 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3429 Status
= VariableServiceQueryVariableInfoInternal (
3431 MaximumVariableStorageSize
,
3432 RemainingVariableStorageSize
,
3436 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3441 This function reclaims variable storage if free size is below the threshold.
3443 Caution: This function may be invoked at SMM mode.
3444 Care must be taken to make sure not security issue.
3453 UINTN CommonVariableSpace
;
3454 UINTN RemainingCommonVariableSpace
;
3455 UINTN RemainingHwErrVariableSpace
;
3457 Status
= EFI_SUCCESS
;
3459 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
3461 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
3463 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
3465 // Check if the free area is blow a threshold.
3467 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
3468 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
3469 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
3471 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3472 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3479 ASSERT_EFI_ERROR (Status
);
3484 Init non-volatile variable store.
3486 @retval EFI_SUCCESS Function successfully executed.
3487 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3488 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.
3492 InitNonVolatileVariableStore (
3496 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
3497 VARIABLE_HEADER
*NextVariable
;
3498 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3499 UINT64 VariableStoreLength
;
3501 EFI_HOB_GUID_TYPE
*GuidHob
;
3502 EFI_PHYSICAL_ADDRESS NvStorageBase
;
3503 UINT8
*NvStorageData
;
3504 UINT32 NvStorageSize
;
3505 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*FtwLastWriteData
;
3506 UINT32 BackUpOffset
;
3509 mVariableModuleGlobal
->FvbInstance
= NULL
;
3512 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
3513 // is stored with common variable in the same NV region. So the platform integrator should
3514 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
3515 // PcdFlashNvStorageVariableSize.
3517 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
3520 // Allocate runtime memory used for a memory copy of the FLASH region.
3521 // Keep the memory and the FLASH in sync as updates occur.
3523 NvStorageSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
3524 NvStorageData
= AllocateRuntimeZeroPool (NvStorageSize
);
3525 if (NvStorageData
== NULL
) {
3526 return EFI_OUT_OF_RESOURCES
;
3529 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
3530 if (NvStorageBase
== 0) {
3531 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3534 // Copy NV storage data to the memory buffer.
3536 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) NvStorageBase
, NvStorageSize
);
3539 // Check the FTW last write data hob.
3541 GuidHob
= GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid
);
3542 if (GuidHob
!= NULL
) {
3543 FtwLastWriteData
= (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*) GET_GUID_HOB_DATA (GuidHob
);
3544 if (FtwLastWriteData
->TargetAddress
== NvStorageBase
) {
3545 DEBUG ((EFI_D_INFO
, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN
) FtwLastWriteData
->SpareAddress
));
3547 // Copy the backed up NV storage data to the memory buffer from spare block.
3549 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) (FtwLastWriteData
->SpareAddress
), NvStorageSize
);
3550 } else if ((FtwLastWriteData
->TargetAddress
> NvStorageBase
) &&
3551 (FtwLastWriteData
->TargetAddress
< (NvStorageBase
+ NvStorageSize
))) {
3553 // Flash NV storage from the Offset is backed up in spare block.
3555 BackUpOffset
= (UINT32
) (FtwLastWriteData
->TargetAddress
- NvStorageBase
);
3556 BackUpSize
= NvStorageSize
- BackUpOffset
;
3557 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
));
3559 // Copy the partial backed up NV storage data to the memory buffer from spare block.
3561 CopyMem (NvStorageData
+ BackUpOffset
, (UINT8
*) (UINTN
) FtwLastWriteData
->SpareAddress
, BackUpSize
);
3565 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) NvStorageData
;
3568 // Check if the Firmware Volume is not corrupted
3570 if ((FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) || (!CompareGuid (&gEfiSystemNvDataFvGuid
, &FvHeader
->FileSystemGuid
))) {
3571 FreePool (NvStorageData
);
3572 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
3573 return EFI_VOLUME_CORRUPTED
;
3576 VariableStoreBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) FvHeader
+ FvHeader
->HeaderLength
);
3577 VariableStoreLength
= (UINT64
) (NvStorageSize
- FvHeader
->HeaderLength
);
3579 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3580 mNvVariableCache
= (VARIABLE_STORE_HEADER
*) (UINTN
) VariableStoreBase
;
3581 if (GetVariableStoreStatus (mNvVariableCache
) != EfiValid
) {
3582 FreePool (NvStorageData
);
3583 DEBUG((EFI_D_ERROR
, "Variable Store header is corrupted\n"));
3584 return EFI_VOLUME_CORRUPTED
;
3586 ASSERT(mNvVariableCache
->Size
== VariableStoreLength
);
3589 // The max variable or hardware error variable size should be < variable store size.
3591 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) < VariableStoreLength
);
3594 // Parse non-volatile variable data and get last variable offset.
3596 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
3597 while (IsValidVariableHeader (NextVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
))) {
3598 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
3599 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3600 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3602 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3605 NextVariable
= GetNextVariablePtr (NextVariable
);
3607 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
3613 Flush the HOB variable to flash.
3615 @param[in] VariableName Name of variable has been updated or deleted.
3616 @param[in] VendorGuid Guid of variable has been updated or deleted.
3620 FlushHobVariableToFlash (
3621 IN CHAR16
*VariableName
,
3622 IN EFI_GUID
*VendorGuid
3626 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3627 VARIABLE_HEADER
*Variable
;
3634 // Flush the HOB variable to flash.
3636 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3637 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
3639 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
3641 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
3642 for ( Variable
= GetStartPointer (VariableStoreHeader
)
3643 ; IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))
3644 ; Variable
= GetNextVariablePtr (Variable
)
3646 if (Variable
->State
!= VAR_ADDED
) {
3648 // The HOB variable has been set to DELETED state in local.
3652 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
3653 if (VendorGuid
== NULL
|| VariableName
== NULL
||
3654 !CompareGuid (VendorGuid
, &Variable
->VendorGuid
) ||
3655 StrCmp (VariableName
, GetVariableNamePtr (Variable
)) != 0) {
3656 VariableData
= GetVariableDataPtr (Variable
);
3657 Status
= VariableServiceSetVariable (
3658 GetVariableNamePtr (Variable
),
3659 &Variable
->VendorGuid
,
3660 Variable
->Attributes
,
3664 DEBUG ((EFI_D_INFO
, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
), Status
));
3667 // The updated or deleted variable is matched with the HOB variable.
3668 // Don't break here because we will try to set other HOB variables
3669 // since this variable could be set successfully.
3671 Status
= EFI_SUCCESS
;
3673 if (!EFI_ERROR (Status
)) {
3675 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
3676 // set the HOB variable to DELETED state in local.
3678 DEBUG ((EFI_D_INFO
, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
)));
3679 Variable
->State
&= VAR_DELETED
;
3686 // We still have HOB variable(s) not flushed in flash.
3688 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
3691 // All HOB variables have been flushed in flash.
3693 DEBUG ((EFI_D_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
3694 if (!AtRuntime ()) {
3695 FreePool ((VOID
*) VariableStoreHeader
);
3703 Initializes variable write service after FTW was ready.
3705 @retval EFI_SUCCESS Function successfully executed.
3706 @retval Others Fail to initialize the variable service.
3710 VariableWriteServiceInitialize (
3715 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3718 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3719 EFI_PHYSICAL_ADDRESS NvStorageBase
;
3721 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
3722 if (NvStorageBase
== 0) {
3723 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3725 VariableStoreBase
= NvStorageBase
+ (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(NvStorageBase
))->HeaderLength
);
3728 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
3730 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3731 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
3734 // Check if the free area is really free.
3736 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
3737 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
3740 // There must be something wrong in variable store, do reclaim operation.
3743 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3744 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3751 if (EFI_ERROR (Status
)) {
3758 FlushHobVariableToFlash (NULL
, NULL
);
3761 // Authenticated variable initialize.
3763 Status
= AutenticatedVariableServiceInitialize ();
3770 Initializes variable store area for non-volatile and volatile variable.
3772 @retval EFI_SUCCESS Function successfully executed.
3773 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3777 VariableCommonInitialize (
3782 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
3783 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3784 UINT64 VariableStoreLength
;
3786 EFI_HOB_GUID_TYPE
*GuidHob
;
3789 // Allocate runtime memory for variable driver global structure.
3791 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
3792 if (mVariableModuleGlobal
== NULL
) {
3793 return EFI_OUT_OF_RESOURCES
;
3796 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
3799 // Get HOB variable store.
3801 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
3802 if (GuidHob
!= NULL
) {
3803 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
3804 VariableStoreLength
= (UINT64
) (GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
));
3805 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
3806 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
3807 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
3808 FreePool (mVariableModuleGlobal
);
3809 return EFI_OUT_OF_RESOURCES
;
3812 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
3817 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
3819 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
3820 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
3821 if (VolatileVariableStore
== NULL
) {
3822 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3823 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3825 FreePool (mVariableModuleGlobal
);
3826 return EFI_OUT_OF_RESOURCES
;
3829 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
3832 // Initialize Variable Specific Data.
3834 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
3835 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
3837 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiAuthenticatedVariableGuid
);
3838 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
3839 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
3840 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
3841 VolatileVariableStore
->Reserved
= 0;
3842 VolatileVariableStore
->Reserved1
= 0;
3845 // Init non-volatile variable store.
3847 Status
= InitNonVolatileVariableStore ();
3848 if (EFI_ERROR (Status
)) {
3849 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3850 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3852 FreePool (mVariableModuleGlobal
);
3853 FreePool (VolatileVariableStore
);
3861 Get the proper fvb handle and/or fvb protocol by the given Flash address.
3863 @param[in] Address The Flash address.
3864 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
3865 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
3869 GetFvbInfoByAddress (
3870 IN EFI_PHYSICAL_ADDRESS Address
,
3871 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
3872 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
3876 EFI_HANDLE
*HandleBuffer
;
3879 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
3880 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
3881 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
3882 EFI_FVB_ATTRIBUTES_2 Attributes
;
3884 HandleBuffer
= NULL
;
3886 // Get all FVB handles.
3888 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
3889 if (EFI_ERROR (Status
)) {
3890 return EFI_NOT_FOUND
;
3894 // Get the FVB to access variable store.
3897 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
3898 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
3899 if (EFI_ERROR (Status
)) {
3900 Status
= EFI_NOT_FOUND
;
3905 // Ensure this FVB protocol supported Write operation.
3907 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
3908 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
3913 // Compare the address and select the right one.
3915 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
3916 if (EFI_ERROR (Status
)) {
3920 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
3921 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
3922 if (FvbHandle
!= NULL
) {
3923 *FvbHandle
= HandleBuffer
[Index
];
3925 if (FvbProtocol
!= NULL
) {
3928 Status
= EFI_SUCCESS
;
3932 FreePool (HandleBuffer
);
3935 Status
= EFI_NOT_FOUND
;