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 (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0))) ||
2113 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0))) {
2115 // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of
2116 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
2118 Status
= AppendSignatureList (
2120 Variable
->CurrPtr
->DataSize
,
2121 MaxDataSize
- Variable
->CurrPtr
->DataSize
,
2126 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2128 // Signature List is too long, Failed to Append.
2130 Status
= EFI_INVALID_PARAMETER
;
2134 if (MergedBufSize
== Variable
->CurrPtr
->DataSize
) {
2135 if ((TimeStamp
== NULL
) || CompareTimeStamp (TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2137 // New EFI_SIGNATURE_DATA is not found and timestamp is not later
2138 // than current timestamp, return EFI_SUCCESS directly.
2140 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2141 Status
= EFI_SUCCESS
;
2147 // For other Variables, append the new data to the end of existing data.
2148 // Max Harware error record variable data size is different from common variable
2150 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2151 MaxDataSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - DataOffset
;
2154 if (Variable
->CurrPtr
->DataSize
+ DataSize
> MaxDataSize
) {
2156 // Existing data size + new data size exceed maximum variable size limitation.
2158 Status
= EFI_INVALID_PARAMETER
;
2161 CopyMem ((UINT8
*) ((UINTN
) BufferForMerge
+ Variable
->CurrPtr
->DataSize
), Data
, DataSize
);
2162 MergedBufSize
= Variable
->CurrPtr
->DataSize
+ DataSize
;
2166 // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.
2168 Data
= BufferForMerge
;
2169 DataSize
= MergedBufSize
;
2174 // Mark the old variable as in delete transition.
2176 State
= Variable
->CurrPtr
->State
;
2177 State
&= VAR_IN_DELETED_TRANSITION
;
2179 Status
= UpdateVariableStore (
2180 &mVariableModuleGlobal
->VariableGlobal
,
2184 (UINTN
) &Variable
->CurrPtr
->State
,
2188 if (EFI_ERROR (Status
)) {
2191 if (!Variable
->Volatile
) {
2192 CacheVariable
->CurrPtr
->State
= State
;
2197 // Not found existing variable. Create a new variable.
2200 if ((DataSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
2201 Status
= EFI_SUCCESS
;
2206 // Make sure we are trying to create a new variable.
2207 // Setting a data variable with zero DataSize or no access attributes means to delete it.
2209 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
2210 Status
= EFI_NOT_FOUND
;
2215 // Only variable have NV|RT attribute can be created in Runtime.
2218 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
2219 Status
= EFI_INVALID_PARAMETER
;
2225 // Function part - create a new variable and copy the data.
2226 // Both update a variable and create a variable will come here.
2228 NextVariable
->StartId
= VARIABLE_DATA
;
2230 // NextVariable->State = VAR_ADDED;
2232 NextVariable
->Reserved
= 0;
2233 NextVariable
->PubKeyIndex
= KeyIndex
;
2234 NextVariable
->MonotonicCount
= MonotonicCount
;
2235 ZeroMem (&NextVariable
->TimeStamp
, sizeof (EFI_TIME
));
2237 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
2238 (TimeStamp
!= NULL
)) {
2239 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
2240 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
2243 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
2244 // when the new TimeStamp value is later than the current timestamp associated
2245 // with the variable, we need associate the new timestamp with the updated value.
2247 if (Variable
->CurrPtr
!= NULL
) {
2248 if (CompareTimeStamp (&Variable
->CurrPtr
->TimeStamp
, TimeStamp
)) {
2249 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
2256 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
2257 // Attributes bitmask parameter of a GetVariable() call.
2259 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
2261 VarNameOffset
= sizeof (VARIABLE_HEADER
);
2262 VarNameSize
= StrSize (VariableName
);
2264 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
2268 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
2271 // If DataReady is TRUE, it means the variable data has been saved into
2272 // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.
2276 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
2282 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
2284 // There will be pad bytes after Data, the NextVariable->NameSize and
2285 // NextVariable->DataSize should not include pad size so that variable
2286 // service can get actual size in GetVariable.
2288 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
2289 NextVariable
->DataSize
= (UINT32
)DataSize
;
2292 // The actual size of the variable that stores in storage should
2293 // include pad size.
2295 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
2296 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2298 // Create a nonvolatile variable.
2301 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
2302 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2303 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
2304 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
2305 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
2307 Status
= EFI_OUT_OF_RESOURCES
;
2311 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2314 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2315 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2319 HEADER_ALIGN (VarSize
),
2322 if (!EFI_ERROR (Status
)) {
2324 // The new variable has been integrated successfully during reclaiming.
2326 if (Variable
->CurrPtr
!= NULL
) {
2327 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2328 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2330 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
);
2331 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2337 // 1. Write variable header
2338 // 2. Set variable state to header valid
2339 // 3. Write variable data
2340 // 4. Set variable state to valid
2345 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
2346 Status
= UpdateVariableStore (
2347 &mVariableModuleGlobal
->VariableGlobal
,
2351 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2352 sizeof (VARIABLE_HEADER
),
2353 (UINT8
*) NextVariable
2356 if (EFI_ERROR (Status
)) {
2363 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
2364 Status
= UpdateVariableStore (
2365 &mVariableModuleGlobal
->VariableGlobal
,
2369 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2371 &NextVariable
->State
2374 if (EFI_ERROR (Status
)) {
2380 Status
= UpdateVariableStore (
2381 &mVariableModuleGlobal
->VariableGlobal
,
2385 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
2386 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
2387 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
2390 if (EFI_ERROR (Status
)) {
2396 NextVariable
->State
= VAR_ADDED
;
2397 Status
= UpdateVariableStore (
2398 &mVariableModuleGlobal
->VariableGlobal
,
2402 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2404 &NextVariable
->State
2407 if (EFI_ERROR (Status
)) {
2411 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2413 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2414 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2416 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2419 // update the memory copy of Flash region.
2421 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
2424 // Create a volatile variable.
2428 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2429 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
2431 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2434 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
2435 &mVariableModuleGlobal
->VolatileLastVariableOffset
,
2439 HEADER_ALIGN (VarSize
),
2442 if (!EFI_ERROR (Status
)) {
2444 // The new variable has been integrated successfully during reclaiming.
2446 if (Variable
->CurrPtr
!= NULL
) {
2447 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2448 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2450 UpdateVariableInfo (VariableName
, VendorGuid
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
);
2455 NextVariable
->State
= VAR_ADDED
;
2456 Status
= UpdateVariableStore (
2457 &mVariableModuleGlobal
->VariableGlobal
,
2461 mVariableModuleGlobal
->VolatileLastVariableOffset
,
2463 (UINT8
*) NextVariable
2466 if (EFI_ERROR (Status
)) {
2470 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2474 // Mark the old variable as deleted.
2476 if (!EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
2477 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2479 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
2480 // set IN_DELETED_TRANSITION one to DELETED state first.
2482 State
= Variable
->InDeletedTransitionPtr
->State
;
2483 State
&= VAR_DELETED
;
2484 Status
= UpdateVariableStore (
2485 &mVariableModuleGlobal
->VariableGlobal
,
2489 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2493 if (!EFI_ERROR (Status
)) {
2494 if (!Variable
->Volatile
) {
2495 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2496 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2503 State
= Variable
->CurrPtr
->State
;
2504 State
&= VAR_DELETED
;
2506 Status
= UpdateVariableStore (
2507 &mVariableModuleGlobal
->VariableGlobal
,
2511 (UINTN
) &Variable
->CurrPtr
->State
,
2515 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
2516 CacheVariable
->CurrPtr
->State
= State
;
2520 if (!EFI_ERROR (Status
)) {
2521 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2523 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2532 Check if a Unicode character is a hexadecimal character.
2534 This function checks if a Unicode character is a
2535 hexadecimal character. The valid hexadecimal character is
2536 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
2539 @param Char The character to check against.
2541 @retval TRUE If the Char is a hexadecmial character.
2542 @retval FALSE If the Char is not a hexadecmial character.
2547 IsHexaDecimalDigitCharacter (
2551 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
2556 This code checks if variable is hardware error record variable or not.
2558 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
2559 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
2561 @param VariableName Pointer to variable name.
2562 @param VendorGuid Variable Vendor Guid.
2564 @retval TRUE Variable is hardware error record variable.
2565 @retval FALSE Variable is not hardware error record variable.
2570 IsHwErrRecVariable (
2571 IN CHAR16
*VariableName
,
2572 IN EFI_GUID
*VendorGuid
2575 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
2576 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
2577 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
2578 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
2579 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
2580 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
2581 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
2589 This code checks if variable guid is global variable guid first.
2590 If yes, further check if variable name is in mGlobalVariableList or mGlobalVariableList2 and attributes matched.
2592 @param[in] VariableName Pointer to variable name.
2593 @param[in] VendorGuid Variable Vendor Guid.
2594 @param[in] Attributes Attributes of the variable.
2596 @retval EFI_SUCCESS Variable is not global variable, or Variable is global variable, variable name is in the lists and attributes matched.
2597 @retval EFI_INVALID_PARAMETER Variable is global variable, but variable name is not in the lists or attributes unmatched.
2602 CheckEfiGlobalVariable (
2603 IN CHAR16
*VariableName
,
2604 IN EFI_GUID
*VendorGuid
,
2605 IN UINT32 Attributes
2611 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)){
2613 // Try list 1, exactly match.
2615 for (Index
= 0; Index
< sizeof (mGlobalVariableList
)/sizeof (mGlobalVariableList
[0]); Index
++) {
2616 if ((StrCmp (mGlobalVariableList
[Index
].Name
, VariableName
) == 0) &&
2617 (Attributes
== 0 || (Attributes
& (~EFI_VARIABLE_APPEND_WRITE
)) == mGlobalVariableList
[Index
].Attributes
)) {
2625 NameLength
= StrLen (VariableName
) - 4;
2626 for (Index
= 0; Index
< sizeof (mGlobalVariableList2
)/sizeof (mGlobalVariableList2
[0]); Index
++) {
2627 if ((StrLen (VariableName
) == StrLen (mGlobalVariableList2
[Index
].Name
)) &&
2628 (StrnCmp (mGlobalVariableList2
[Index
].Name
, VariableName
, NameLength
) == 0) &&
2629 IsHexaDecimalDigitCharacter (VariableName
[NameLength
]) &&
2630 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 1]) &&
2631 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 2]) &&
2632 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 3]) &&
2633 (Attributes
== 0 || (Attributes
& (~EFI_VARIABLE_APPEND_WRITE
)) == mGlobalVariableList2
[Index
].Attributes
)) {
2638 DEBUG ((EFI_D_INFO
, "[Variable]: set global variable with invalid variable name or attributes - %g:%s:%x\n", VendorGuid
, VariableName
, Attributes
));
2639 return EFI_INVALID_PARAMETER
;
2646 Mark a variable that will become read-only after leaving the DXE phase of execution.
2648 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
2649 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
2650 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
2652 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
2653 as pending to be read-only.
2654 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
2655 Or VariableName is an empty string.
2656 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
2657 already been signaled.
2658 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
2662 VariableLockRequestToLock (
2663 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL
*This
,
2664 IN CHAR16
*VariableName
,
2665 IN EFI_GUID
*VendorGuid
2668 VARIABLE_ENTRY
*Entry
;
2670 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2671 return EFI_INVALID_PARAMETER
;
2675 return EFI_ACCESS_DENIED
;
2678 Entry
= AllocateRuntimePool (sizeof (*Entry
) + StrSize (VariableName
));
2679 if (Entry
== NULL
) {
2680 return EFI_OUT_OF_RESOURCES
;
2683 DEBUG ((EFI_D_INFO
, "[Variable] Lock: %g:%s\n", VendorGuid
, VariableName
));
2685 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2687 Entry
->Name
= (CHAR16
*) (Entry
+ 1);
2688 StrCpy (Entry
->Name
, VariableName
);
2689 CopyGuid (&Entry
->Guid
, VendorGuid
);
2690 InsertTailList (&mLockedVariableList
, &Entry
->Link
);
2692 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2698 This code checks if variable should be treated as read-only variable.
2700 @param[in] VariableName Name of the Variable.
2701 @param[in] VendorGuid GUID of the Variable.
2703 @retval TRUE This variable is read-only variable.
2704 @retval FALSE This variable is NOT read-only variable.
2708 IsReadOnlyVariable (
2709 IN CHAR16
*VariableName
,
2710 IN EFI_GUID
*VendorGuid
2713 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)) {
2714 if ((StrCmp (VariableName
, EFI_SETUP_MODE_NAME
) == 0) ||
2715 (StrCmp (VariableName
, EFI_SIGNATURE_SUPPORT_NAME
) == 0) ||
2716 (StrCmp (VariableName
, EFI_SECURE_BOOT_MODE_NAME
) == 0) ||
2717 (StrCmp (VariableName
, EFI_VENDOR_KEYS_VARIABLE_NAME
) == 0) ||
2718 (StrCmp (VariableName
, EFI_KEK_DEFAULT_VARIABLE_NAME
) == 0) ||
2719 (StrCmp (VariableName
, EFI_PK_DEFAULT_VARIABLE_NAME
) == 0) ||
2720 (StrCmp (VariableName
, EFI_DB_DEFAULT_VARIABLE_NAME
) == 0) ||
2721 (StrCmp (VariableName
, EFI_DBX_DEFAULT_VARIABLE_NAME
) == 0) ||
2722 (StrCmp (VariableName
, EFI_DBT_DEFAULT_VARIABLE_NAME
) == 0)) {
2732 This code finds variable in storage blocks (Volatile or Non-Volatile).
2734 Caution: This function may receive untrusted input.
2735 This function may be invoked in SMM mode, and datasize is external input.
2736 This function will do basic validation, before parse the data.
2738 @param VariableName Name of Variable to be found.
2739 @param VendorGuid Variable vendor GUID.
2740 @param Attributes Attribute value of the variable found.
2741 @param DataSize Size of Data found. If size is less than the
2742 data, this value contains the required size.
2743 @param Data Data pointer.
2745 @return EFI_INVALID_PARAMETER Invalid parameter.
2746 @return EFI_SUCCESS Find the specified variable.
2747 @return EFI_NOT_FOUND Not found.
2748 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2753 VariableServiceGetVariable (
2754 IN CHAR16
*VariableName
,
2755 IN EFI_GUID
*VendorGuid
,
2756 OUT UINT32
*Attributes OPTIONAL
,
2757 IN OUT UINTN
*DataSize
,
2762 VARIABLE_POINTER_TRACK Variable
;
2765 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2766 return EFI_INVALID_PARAMETER
;
2769 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2771 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2772 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2779 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2780 ASSERT (VarDataSize
!= 0);
2782 if (*DataSize
>= VarDataSize
) {
2784 Status
= EFI_INVALID_PARAMETER
;
2788 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2789 if (Attributes
!= NULL
) {
2790 *Attributes
= Variable
.CurrPtr
->Attributes
;
2793 *DataSize
= VarDataSize
;
2794 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2796 Status
= EFI_SUCCESS
;
2799 *DataSize
= VarDataSize
;
2800 Status
= EFI_BUFFER_TOO_SMALL
;
2805 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2813 This code Finds the Next available variable.
2815 Caution: This function may receive untrusted input.
2816 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2818 @param VariableNameSize Size of the variable name.
2819 @param VariableName Pointer to variable name.
2820 @param VendorGuid Variable Vendor Guid.
2822 @return EFI_INVALID_PARAMETER Invalid parameter.
2823 @return EFI_SUCCESS Find the specified variable.
2824 @return EFI_NOT_FOUND Not found.
2825 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2830 VariableServiceGetNextVariableName (
2831 IN OUT UINTN
*VariableNameSize
,
2832 IN OUT CHAR16
*VariableName
,
2833 IN OUT EFI_GUID
*VendorGuid
2836 VARIABLE_STORE_TYPE Type
;
2837 VARIABLE_POINTER_TRACK Variable
;
2838 VARIABLE_POINTER_TRACK VariableInHob
;
2839 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2842 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2844 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2845 return EFI_INVALID_PARAMETER
;
2848 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2850 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2851 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2855 if (VariableName
[0] != 0) {
2857 // If variable name is not NULL, get next variable.
2859 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2863 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2864 // The index and attributes mapping must be kept in this order as FindVariable
2865 // makes use of this mapping to implement search algorithm.
2867 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2868 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2869 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2873 // Switch from Volatile to HOB, to Non-Volatile.
2875 while (!IsValidVariableHeader (Variable
.CurrPtr
, Variable
.EndPtr
)) {
2877 // Find current storage index
2879 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2880 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2884 ASSERT (Type
< VariableStoreTypeMax
);
2886 // Switch to next storage
2888 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2889 if (VariableStoreHeader
[Type
] != NULL
) {
2894 // Capture the case that
2895 // 1. current storage is the last one, or
2896 // 2. no further storage
2898 if (Type
== VariableStoreTypeMax
) {
2899 Status
= EFI_NOT_FOUND
;
2902 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2903 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2904 Variable
.CurrPtr
= Variable
.StartPtr
;
2908 // Variable is found
2910 if (Variable
.CurrPtr
->State
== VAR_ADDED
|| Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2911 if (!AtRuntime () || ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
2912 if (Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2914 // If it is a IN_DELETED_TRANSITION variable,
2915 // and there is also a same ADDED one at the same time,
2918 VariablePtrTrack
.StartPtr
= Variable
.StartPtr
;
2919 VariablePtrTrack
.EndPtr
= Variable
.EndPtr
;
2920 Status
= FindVariableEx (
2921 GetVariableNamePtr (Variable
.CurrPtr
),
2922 &Variable
.CurrPtr
->VendorGuid
,
2926 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
== VAR_ADDED
) {
2927 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2933 // Don't return NV variable when HOB overrides it
2935 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2936 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2938 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2939 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2940 Status
= FindVariableEx (
2941 GetVariableNamePtr (Variable
.CurrPtr
),
2942 &Variable
.CurrPtr
->VendorGuid
,
2946 if (!EFI_ERROR (Status
)) {
2947 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2952 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2953 ASSERT (VarNameSize
!= 0);
2955 if (VarNameSize
<= *VariableNameSize
) {
2956 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2957 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2958 Status
= EFI_SUCCESS
;
2960 Status
= EFI_BUFFER_TOO_SMALL
;
2963 *VariableNameSize
= VarNameSize
;
2968 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2972 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2978 This code sets variable in storage blocks (Volatile or Non-Volatile).
2980 Caution: This function may receive untrusted input.
2981 This function may be invoked in SMM mode, and datasize and data are external input.
2982 This function will do basic validation, before parse the data.
2983 This function will parse the authentication carefully to avoid security issues, like
2984 buffer overflow, integer overflow.
2985 This function will check attribute carefully to avoid authentication bypass.
2987 @param VariableName Name of Variable to be found.
2988 @param VendorGuid Variable vendor GUID.
2989 @param Attributes Attribute value of the variable found
2990 @param DataSize Size of Data found. If size is less than the
2991 data, this value contains the required size.
2992 @param Data Data pointer.
2994 @return EFI_INVALID_PARAMETER Invalid parameter.
2995 @return EFI_SUCCESS Set successfully.
2996 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2997 @return EFI_NOT_FOUND Not found.
2998 @return EFI_WRITE_PROTECTED Variable is read-only.
3003 VariableServiceSetVariable (
3004 IN CHAR16
*VariableName
,
3005 IN EFI_GUID
*VendorGuid
,
3006 IN UINT32 Attributes
,
3011 VARIABLE_POINTER_TRACK Variable
;
3013 VARIABLE_HEADER
*NextVariable
;
3014 EFI_PHYSICAL_ADDRESS Point
;
3017 VARIABLE_ENTRY
*Entry
;
3020 // Check input parameters.
3022 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
3023 return EFI_INVALID_PARAMETER
;
3026 if (IsReadOnlyVariable (VariableName
, VendorGuid
)) {
3027 return EFI_WRITE_PROTECTED
;
3030 if (DataSize
!= 0 && Data
== NULL
) {
3031 return EFI_INVALID_PARAMETER
;
3035 // Check for reserverd bit in variable attribute.
3037 if ((Attributes
& (~EFI_VARIABLE_ATTRIBUTES_MASK
)) != 0) {
3038 return EFI_INVALID_PARAMETER
;
3042 // Make sure if runtime bit is set, boot service bit is set also.
3044 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
3045 return EFI_INVALID_PARAMETER
;
3049 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
3050 // cannot be set both.
3052 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
3053 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
3054 return EFI_INVALID_PARAMETER
;
3057 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
3058 if (DataSize
< AUTHINFO_SIZE
) {
3060 // Try to write Authenticated Variable without AuthInfo.
3062 return EFI_SECURITY_VIOLATION
;
3064 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
3065 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
3067 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
3069 if (DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
||
3070 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
)) ||
3071 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) {
3072 return EFI_SECURITY_VIOLATION
;
3074 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
3076 PayloadSize
= DataSize
;
3079 if ((UINTN
)(~0) - PayloadSize
< StrSize(VariableName
)){
3081 // Prevent whole variable size overflow
3083 return EFI_INVALID_PARAMETER
;
3087 // The size of the VariableName, including the Unicode Null in bytes plus
3088 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
3089 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
3091 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3092 if (StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
)) {
3093 return EFI_INVALID_PARAMETER
;
3095 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
3096 return EFI_INVALID_PARAMETER
;
3100 // The size of the VariableName, including the Unicode Null in bytes plus
3101 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
3103 if (StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
)) {
3104 return EFI_INVALID_PARAMETER
;
3108 Status
= CheckEfiGlobalVariable (VariableName
, VendorGuid
, Attributes
);
3109 if (EFI_ERROR (Status
)) {
3113 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3116 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
3118 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
3119 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
3121 // Parse non-volatile variable data and get last variable offset.
3123 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
3124 while (IsValidVariableHeader (NextVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))) {
3125 NextVariable
= GetNextVariablePtr (NextVariable
);
3127 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
3130 if (mEndOfDxe
&& mEnableLocking
) {
3132 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.
3134 for ( Link
= GetFirstNode (&mLockedVariableList
)
3135 ; !IsNull (&mLockedVariableList
, Link
)
3136 ; Link
= GetNextNode (&mLockedVariableList
, Link
)
3138 Entry
= BASE_CR (Link
, VARIABLE_ENTRY
, Link
);
3139 if (CompareGuid (&Entry
->Guid
, VendorGuid
) && (StrCmp (Entry
->Name
, VariableName
) == 0)) {
3140 Status
= EFI_WRITE_PROTECTED
;
3141 DEBUG ((EFI_D_INFO
, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid
, VariableName
));
3148 // Check whether the input variable is already existed.
3150 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
3151 if (!EFI_ERROR (Status
)) {
3152 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
3153 Status
= EFI_WRITE_PROTECTED
;
3156 if (Attributes
!= 0 && (Attributes
& (~EFI_VARIABLE_APPEND_WRITE
)) != Variable
.CurrPtr
->Attributes
) {
3158 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not
3159 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:
3160 // 1. No access attributes specified
3161 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE
3163 Status
= EFI_INVALID_PARAMETER
;
3168 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate
)) {
3170 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
3172 Status
= AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
3173 if (EFI_ERROR (Status
)) {
3175 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.
3182 // Process PK, KEK, Sigdb seperately.
3184 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
3185 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, TRUE
);
3186 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
3187 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
3188 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
3189 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))
3190 || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
)) == 0) {
3191 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
3192 if (EFI_ERROR (Status
)) {
3193 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
3196 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
3200 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
3201 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3203 if (!AtRuntime ()) {
3204 if (!EFI_ERROR (Status
)) {
3217 This code returns information about the EFI variables.
3219 Caution: This function may receive untrusted input.
3220 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3222 @param Attributes Attributes bitmask to specify the type of variables
3223 on which to return information.
3224 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
3225 for the EFI variables associated with the attributes specified.
3226 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
3227 for EFI variables associated with the attributes specified.
3228 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
3229 associated with the attributes specified.
3231 @return EFI_SUCCESS Query successfully.
3236 VariableServiceQueryVariableInfoInternal (
3237 IN UINT32 Attributes
,
3238 OUT UINT64
*MaximumVariableStorageSize
,
3239 OUT UINT64
*RemainingVariableStorageSize
,
3240 OUT UINT64
*MaximumVariableSize
3243 VARIABLE_HEADER
*Variable
;
3244 VARIABLE_HEADER
*NextVariable
;
3245 UINT64 VariableSize
;
3246 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3247 UINT64 CommonVariableTotalSize
;
3248 UINT64 HwErrVariableTotalSize
;
3250 VARIABLE_POINTER_TRACK VariablePtrTrack
;
3252 CommonVariableTotalSize
= 0;
3253 HwErrVariableTotalSize
= 0;
3255 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
3257 // Query is Volatile related.
3259 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
3262 // Query is Non-Volatile related.
3264 VariableStoreHeader
= mNvVariableCache
;
3268 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
3269 // with the storage size (excluding the storage header size).
3271 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
3274 // Harware error record variable needs larger size.
3276 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3277 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
3278 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
3280 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
3281 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
3282 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
3286 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
3288 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
3292 // Point to the starting address of the variables.
3294 Variable
= GetStartPointer (VariableStoreHeader
);
3297 // Now walk through the related variable store.
3299 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
3300 NextVariable
= GetNextVariablePtr (Variable
);
3301 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
3305 // We don't take the state of the variables in mind
3306 // when calculating RemainingVariableStorageSize,
3307 // since the space occupied by variables not marked with
3308 // VAR_ADDED is not allowed to be reclaimed in Runtime.
3310 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3311 HwErrVariableTotalSize
+= VariableSize
;
3313 CommonVariableTotalSize
+= VariableSize
;
3317 // Only care about Variables with State VAR_ADDED, because
3318 // the space not marked as VAR_ADDED is reclaimable now.
3320 if (Variable
->State
== VAR_ADDED
) {
3321 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3322 HwErrVariableTotalSize
+= VariableSize
;
3324 CommonVariableTotalSize
+= VariableSize
;
3326 } else if (Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
3328 // If it is a IN_DELETED_TRANSITION variable,
3329 // and there is not also a same ADDED one at the same time,
3330 // this IN_DELETED_TRANSITION variable is valid.
3332 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
3333 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
3334 Status
= FindVariableEx (
3335 GetVariableNamePtr (Variable
),
3336 &Variable
->VendorGuid
,
3340 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
!= VAR_ADDED
) {
3341 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3342 HwErrVariableTotalSize
+= VariableSize
;
3344 CommonVariableTotalSize
+= VariableSize
;
3351 // Go to the next one.
3353 Variable
= NextVariable
;
3356 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
3357 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
3359 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
3362 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
3363 *MaximumVariableSize
= 0;
3364 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
3365 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
3373 This code returns information about the EFI variables.
3375 Caution: This function may receive untrusted input.
3376 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3378 @param Attributes Attributes bitmask to specify the type of variables
3379 on which to return information.
3380 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
3381 for the EFI variables associated with the attributes specified.
3382 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
3383 for EFI variables associated with the attributes specified.
3384 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
3385 associated with the attributes specified.
3387 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
3388 @return EFI_SUCCESS Query successfully.
3389 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
3394 VariableServiceQueryVariableInfo (
3395 IN UINT32 Attributes
,
3396 OUT UINT64
*MaximumVariableStorageSize
,
3397 OUT UINT64
*RemainingVariableStorageSize
,
3398 OUT UINT64
*MaximumVariableSize
3403 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
3404 return EFI_INVALID_PARAMETER
;
3407 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
3409 // Make sure the Attributes combination is supported by the platform.
3411 return EFI_UNSUPPORTED
;
3412 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
3414 // Make sure if runtime bit is set, boot service bit is set also.
3416 return EFI_INVALID_PARAMETER
;
3417 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
3419 // Make sure RT Attribute is set if we are in Runtime phase.
3421 return EFI_INVALID_PARAMETER
;
3422 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3424 // Make sure Hw Attribute is set with NV.
3426 return EFI_INVALID_PARAMETER
;
3429 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3431 Status
= VariableServiceQueryVariableInfoInternal (
3433 MaximumVariableStorageSize
,
3434 RemainingVariableStorageSize
,
3438 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3443 This function reclaims variable storage if free size is below the threshold.
3445 Caution: This function may be invoked at SMM mode.
3446 Care must be taken to make sure not security issue.
3455 UINTN CommonVariableSpace
;
3456 UINTN RemainingCommonVariableSpace
;
3457 UINTN RemainingHwErrVariableSpace
;
3459 Status
= EFI_SUCCESS
;
3461 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
3463 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
3465 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
3467 // Check if the free area is blow a threshold.
3469 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
3470 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
3471 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
3473 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3474 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3481 ASSERT_EFI_ERROR (Status
);
3486 Init non-volatile variable store.
3488 @retval EFI_SUCCESS Function successfully executed.
3489 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3490 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.
3494 InitNonVolatileVariableStore (
3498 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
3499 VARIABLE_HEADER
*NextVariable
;
3500 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3501 UINT64 VariableStoreLength
;
3503 EFI_HOB_GUID_TYPE
*GuidHob
;
3504 EFI_PHYSICAL_ADDRESS NvStorageBase
;
3505 UINT8
*NvStorageData
;
3506 UINT32 NvStorageSize
;
3507 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*FtwLastWriteData
;
3508 UINT32 BackUpOffset
;
3511 mVariableModuleGlobal
->FvbInstance
= NULL
;
3514 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
3515 // is stored with common variable in the same NV region. So the platform integrator should
3516 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
3517 // PcdFlashNvStorageVariableSize.
3519 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
3522 // Allocate runtime memory used for a memory copy of the FLASH region.
3523 // Keep the memory and the FLASH in sync as updates occur.
3525 NvStorageSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
3526 NvStorageData
= AllocateRuntimeZeroPool (NvStorageSize
);
3527 if (NvStorageData
== NULL
) {
3528 return EFI_OUT_OF_RESOURCES
;
3531 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
3532 if (NvStorageBase
== 0) {
3533 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3536 // Copy NV storage data to the memory buffer.
3538 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) NvStorageBase
, NvStorageSize
);
3541 // Check the FTW last write data hob.
3543 GuidHob
= GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid
);
3544 if (GuidHob
!= NULL
) {
3545 FtwLastWriteData
= (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*) GET_GUID_HOB_DATA (GuidHob
);
3546 if (FtwLastWriteData
->TargetAddress
== NvStorageBase
) {
3547 DEBUG ((EFI_D_INFO
, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN
) FtwLastWriteData
->SpareAddress
));
3549 // Copy the backed up NV storage data to the memory buffer from spare block.
3551 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) (FtwLastWriteData
->SpareAddress
), NvStorageSize
);
3552 } else if ((FtwLastWriteData
->TargetAddress
> NvStorageBase
) &&
3553 (FtwLastWriteData
->TargetAddress
< (NvStorageBase
+ NvStorageSize
))) {
3555 // Flash NV storage from the Offset is backed up in spare block.
3557 BackUpOffset
= (UINT32
) (FtwLastWriteData
->TargetAddress
- NvStorageBase
);
3558 BackUpSize
= NvStorageSize
- BackUpOffset
;
3559 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
));
3561 // Copy the partial backed up NV storage data to the memory buffer from spare block.
3563 CopyMem (NvStorageData
+ BackUpOffset
, (UINT8
*) (UINTN
) FtwLastWriteData
->SpareAddress
, BackUpSize
);
3567 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) NvStorageData
;
3570 // Check if the Firmware Volume is not corrupted
3572 if ((FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) || (!CompareGuid (&gEfiSystemNvDataFvGuid
, &FvHeader
->FileSystemGuid
))) {
3573 FreePool (NvStorageData
);
3574 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
3575 return EFI_VOLUME_CORRUPTED
;
3578 VariableStoreBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) FvHeader
+ FvHeader
->HeaderLength
);
3579 VariableStoreLength
= (UINT64
) (NvStorageSize
- FvHeader
->HeaderLength
);
3581 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3582 mNvVariableCache
= (VARIABLE_STORE_HEADER
*) (UINTN
) VariableStoreBase
;
3583 if (GetVariableStoreStatus (mNvVariableCache
) != EfiValid
) {
3584 FreePool (NvStorageData
);
3585 DEBUG((EFI_D_ERROR
, "Variable Store header is corrupted\n"));
3586 return EFI_VOLUME_CORRUPTED
;
3588 ASSERT(mNvVariableCache
->Size
== VariableStoreLength
);
3591 // The max variable or hardware error variable size should be < variable store size.
3593 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) < VariableStoreLength
);
3596 // Parse non-volatile variable data and get last variable offset.
3598 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
3599 while (IsValidVariableHeader (NextVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
))) {
3600 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
3601 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3602 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3604 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3607 NextVariable
= GetNextVariablePtr (NextVariable
);
3609 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
3615 Flush the HOB variable to flash.
3617 @param[in] VariableName Name of variable has been updated or deleted.
3618 @param[in] VendorGuid Guid of variable has been updated or deleted.
3622 FlushHobVariableToFlash (
3623 IN CHAR16
*VariableName
,
3624 IN EFI_GUID
*VendorGuid
3628 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3629 VARIABLE_HEADER
*Variable
;
3636 // Flush the HOB variable to flash.
3638 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3639 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
3641 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
3643 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
3644 for ( Variable
= GetStartPointer (VariableStoreHeader
)
3645 ; IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))
3646 ; Variable
= GetNextVariablePtr (Variable
)
3648 if (Variable
->State
!= VAR_ADDED
) {
3650 // The HOB variable has been set to DELETED state in local.
3654 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
3655 if (VendorGuid
== NULL
|| VariableName
== NULL
||
3656 !CompareGuid (VendorGuid
, &Variable
->VendorGuid
) ||
3657 StrCmp (VariableName
, GetVariableNamePtr (Variable
)) != 0) {
3658 VariableData
= GetVariableDataPtr (Variable
);
3659 Status
= VariableServiceSetVariable (
3660 GetVariableNamePtr (Variable
),
3661 &Variable
->VendorGuid
,
3662 Variable
->Attributes
,
3666 DEBUG ((EFI_D_INFO
, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
), Status
));
3669 // The updated or deleted variable is matched with the HOB variable.
3670 // Don't break here because we will try to set other HOB variables
3671 // since this variable could be set successfully.
3673 Status
= EFI_SUCCESS
;
3675 if (!EFI_ERROR (Status
)) {
3677 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
3678 // set the HOB variable to DELETED state in local.
3680 DEBUG ((EFI_D_INFO
, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
)));
3681 Variable
->State
&= VAR_DELETED
;
3688 // We still have HOB variable(s) not flushed in flash.
3690 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
3693 // All HOB variables have been flushed in flash.
3695 DEBUG ((EFI_D_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
3696 if (!AtRuntime ()) {
3697 FreePool ((VOID
*) VariableStoreHeader
);
3705 Initializes variable write service after FTW was ready.
3707 @retval EFI_SUCCESS Function successfully executed.
3708 @retval Others Fail to initialize the variable service.
3712 VariableWriteServiceInitialize (
3717 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3720 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3721 EFI_PHYSICAL_ADDRESS NvStorageBase
;
3723 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
3724 if (NvStorageBase
== 0) {
3725 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3727 VariableStoreBase
= NvStorageBase
+ (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(NvStorageBase
))->HeaderLength
);
3730 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
3732 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3733 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
3736 // Check if the free area is really free.
3738 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
3739 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
3742 // There must be something wrong in variable store, do reclaim operation.
3745 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3746 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3753 if (EFI_ERROR (Status
)) {
3760 FlushHobVariableToFlash (NULL
, NULL
);
3763 // Authenticated variable initialize.
3765 Status
= AutenticatedVariableServiceInitialize ();
3772 Initializes variable store area for non-volatile and volatile variable.
3774 @retval EFI_SUCCESS Function successfully executed.
3775 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3779 VariableCommonInitialize (
3784 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
3785 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3786 UINT64 VariableStoreLength
;
3788 EFI_HOB_GUID_TYPE
*GuidHob
;
3791 // Allocate runtime memory for variable driver global structure.
3793 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
3794 if (mVariableModuleGlobal
== NULL
) {
3795 return EFI_OUT_OF_RESOURCES
;
3798 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
3801 // Get HOB variable store.
3803 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
3804 if (GuidHob
!= NULL
) {
3805 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
3806 VariableStoreLength
= (UINT64
) (GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
));
3807 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
3808 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
3809 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
3810 FreePool (mVariableModuleGlobal
);
3811 return EFI_OUT_OF_RESOURCES
;
3814 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
3819 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
3821 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
3822 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
3823 if (VolatileVariableStore
== NULL
) {
3824 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3825 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3827 FreePool (mVariableModuleGlobal
);
3828 return EFI_OUT_OF_RESOURCES
;
3831 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
3834 // Initialize Variable Specific Data.
3836 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
3837 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
3839 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiAuthenticatedVariableGuid
);
3840 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
3841 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
3842 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
3843 VolatileVariableStore
->Reserved
= 0;
3844 VolatileVariableStore
->Reserved1
= 0;
3847 // Init non-volatile variable store.
3849 Status
= InitNonVolatileVariableStore ();
3850 if (EFI_ERROR (Status
)) {
3851 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3852 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3854 FreePool (mVariableModuleGlobal
);
3855 FreePool (VolatileVariableStore
);
3863 Get the proper fvb handle and/or fvb protocol by the given Flash address.
3865 @param[in] Address The Flash address.
3866 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
3867 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
3871 GetFvbInfoByAddress (
3872 IN EFI_PHYSICAL_ADDRESS Address
,
3873 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
3874 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
3878 EFI_HANDLE
*HandleBuffer
;
3881 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
3882 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
3883 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
3884 EFI_FVB_ATTRIBUTES_2 Attributes
;
3886 HandleBuffer
= NULL
;
3888 // Get all FVB handles.
3890 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
3891 if (EFI_ERROR (Status
)) {
3892 return EFI_NOT_FOUND
;
3896 // Get the FVB to access variable store.
3899 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
3900 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
3901 if (EFI_ERROR (Status
)) {
3902 Status
= EFI_NOT_FOUND
;
3907 // Ensure this FVB protocol supported Write operation.
3909 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
3910 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
3915 // Compare the address and select the right one.
3917 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
3918 if (EFI_ERROR (Status
)) {
3922 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
3923 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
3924 if (FvbHandle
!= NULL
) {
3925 *FvbHandle
= HandleBuffer
[Index
];
3927 if (FvbProtocol
!= NULL
) {
3930 Status
= EFI_SUCCESS
;
3934 FreePool (HandleBuffer
);
3937 Status
= EFI_NOT_FOUND
;