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.
223 @retval TRUE Variable header is valid.
224 @retval FALSE Variable header is not valid.
228 IsValidVariableHeader (
229 IN VARIABLE_HEADER
*Variable
232 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
242 This function writes data to the FWH at the correct LBA even if the LBAs
245 @param Global Pointer to VARAIBLE_GLOBAL structure.
246 @param Volatile Point out the Variable is Volatile or Non-Volatile.
247 @param SetByIndex TRUE if target pointer is given as index.
248 FALSE if target pointer is absolute.
249 @param Fvb Pointer to the writable FVB protocol.
250 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
252 @param DataSize Size of data to be written.
253 @param Buffer Pointer to the buffer from which data is written.
255 @retval EFI_INVALID_PARAMETER Parameters not valid.
256 @retval EFI_SUCCESS Variable store successfully updated.
260 UpdateVariableStore (
261 IN VARIABLE_GLOBAL
*Global
,
263 IN BOOLEAN SetByIndex
,
264 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
265 IN UINTN DataPtrIndex
,
270 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
278 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
279 VARIABLE_STORE_HEADER
*VolatileBase
;
280 EFI_PHYSICAL_ADDRESS FvVolHdr
;
281 EFI_PHYSICAL_ADDRESS DataPtr
;
285 DataPtr
= DataPtrIndex
;
288 // Check if the Data is Volatile.
292 return EFI_INVALID_PARAMETER
;
294 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
295 ASSERT_EFI_ERROR (Status
);
297 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
299 // Data Pointer should point to the actual Address where data is to be
303 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
306 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
307 return EFI_INVALID_PARAMETER
;
311 // Data Pointer should point to the actual Address where data is to be
314 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
316 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
319 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
320 return EFI_INVALID_PARAMETER
;
324 // If Volatile Variable just do a simple mem copy.
326 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
331 // If we are here we are dealing with Non-Volatile Variables.
333 LinearOffset
= (UINTN
) FwVolHeader
;
334 CurrWritePtr
= (UINTN
) DataPtr
;
335 CurrWriteSize
= DataSize
;
339 if (CurrWritePtr
< LinearOffset
) {
340 return EFI_INVALID_PARAMETER
;
343 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
344 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
346 // Check to see if the Variable Writes are spanning through multiple
349 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
350 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
351 Status
= Fvb
->Write (
354 (UINTN
) (CurrWritePtr
- LinearOffset
),
360 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
361 Status
= Fvb
->Write (
364 (UINTN
) (CurrWritePtr
- LinearOffset
),
368 if (EFI_ERROR (Status
)) {
372 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
373 CurrBuffer
= CurrBuffer
+ Size
;
374 CurrWriteSize
= CurrWriteSize
- Size
;
378 LinearOffset
+= PtrBlockMapEntry
->Length
;
389 This code gets the current status of Variable Store.
391 @param VarStoreHeader Pointer to the Variable Store Header.
393 @retval EfiRaw Variable store status is raw.
394 @retval EfiValid Variable store status is valid.
395 @retval EfiInvalid Variable store status is invalid.
398 VARIABLE_STORE_STATUS
399 GetVariableStoreStatus (
400 IN VARIABLE_STORE_HEADER
*VarStoreHeader
403 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) &&
404 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
405 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
409 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
410 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
411 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
412 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
413 VarStoreHeader
->Size
== 0xffffffff &&
414 VarStoreHeader
->Format
== 0xff &&
415 VarStoreHeader
->State
== 0xff
427 This code gets the size of name of variable.
429 @param Variable Pointer to the Variable Header.
431 @return UINTN Size of variable in bytes.
436 IN VARIABLE_HEADER
*Variable
439 if (Variable
->State
== (UINT8
) (-1) ||
440 Variable
->DataSize
== (UINT32
) (-1) ||
441 Variable
->NameSize
== (UINT32
) (-1) ||
442 Variable
->Attributes
== (UINT32
) (-1)) {
445 return (UINTN
) Variable
->NameSize
;
450 This code gets the size of variable data.
452 @param Variable Pointer to the Variable Header.
454 @return Size of variable in bytes.
459 IN VARIABLE_HEADER
*Variable
462 if (Variable
->State
== (UINT8
) (-1) ||
463 Variable
->DataSize
== (UINT32
) (-1) ||
464 Variable
->NameSize
== (UINT32
) (-1) ||
465 Variable
->Attributes
== (UINT32
) (-1)) {
468 return (UINTN
) Variable
->DataSize
;
473 This code gets the pointer to the variable name.
475 @param Variable Pointer to the Variable Header.
477 @return Pointer to Variable Name which is Unicode encoding.
482 IN VARIABLE_HEADER
*Variable
486 return (CHAR16
*) (Variable
+ 1);
491 This code gets the pointer to the variable data.
493 @param Variable Pointer to the Variable Header.
495 @return Pointer to Variable Data.
500 IN VARIABLE_HEADER
*Variable
506 // Be careful about pad size for alignment.
508 Value
= (UINTN
) GetVariableNamePtr (Variable
);
509 Value
+= NameSizeOfVariable (Variable
);
510 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
512 return (UINT8
*) Value
;
518 This code gets the pointer to the next variable header.
520 @param Variable Pointer to the Variable Header.
522 @return Pointer to next variable header.
527 IN VARIABLE_HEADER
*Variable
532 if (!IsValidVariableHeader (Variable
)) {
536 Value
= (UINTN
) GetVariableDataPtr (Variable
);
537 Value
+= DataSizeOfVariable (Variable
);
538 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
541 // Be careful about pad size for alignment.
543 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
548 Gets the pointer to the first variable header in given variable store area.
550 @param VarStoreHeader Pointer to the Variable Store Header.
552 @return Pointer to the first variable header.
557 IN VARIABLE_STORE_HEADER
*VarStoreHeader
561 // The end of variable store.
563 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
568 Gets the pointer to the end of the variable storage area.
570 This function gets pointer to the end of the variable storage
571 area, according to the input variable store header.
573 @param VarStoreHeader Pointer to the Variable Store Header.
575 @return Pointer to the end of the variable storage area.
580 IN VARIABLE_STORE_HEADER
*VarStoreHeader
584 // The end of variable store
586 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
591 Check the PubKeyIndex is a valid key or not.
593 This function will iterate the NV storage to see if this PubKeyIndex is still referenced
594 by any valid count-based auth variabe.
596 @param[in] PubKeyIndex Index of the public key in public key store.
598 @retval TRUE The PubKeyIndex is still in use.
599 @retval FALSE The PubKeyIndex is not referenced by any count-based auth variabe.
604 IN UINT32 PubKeyIndex
607 VARIABLE_HEADER
*Variable
;
609 if (PubKeyIndex
> mPubKeyNumber
) {
613 Variable
= GetStartPointer (mNvVariableCache
);
615 while (IsValidVariableHeader (Variable
)) {
616 if ((Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) &&
617 Variable
->PubKeyIndex
== PubKeyIndex
) {
620 Variable
= GetNextVariablePtr (Variable
);
628 Get the number of valid public key in PubKeyStore.
630 @param[in] PubKeyNumber Number of the public key in public key store.
632 @return Number of valid public key in PubKeyStore.
636 GetValidPubKeyNumber (
637 IN UINT32 PubKeyNumber
645 for (PubKeyIndex
= 1; PubKeyIndex
<= PubKeyNumber
; PubKeyIndex
++) {
646 if (IsValidPubKeyIndex (PubKeyIndex
)) {
656 Filter the useless key in public key store.
658 This function will find out all valid public keys in public key database, save them in new allocated
659 buffer NewPubKeyStore, and give the new PubKeyIndex. The caller is responsible for freeing buffer
660 NewPubKeyIndex and NewPubKeyStore with FreePool().
662 @param[in] PubKeyStore Point to the public key database.
663 @param[in] PubKeyNumber Number of the public key in PubKeyStore.
664 @param[out] NewPubKeyIndex Point to an array of new PubKeyIndex corresponds to NewPubKeyStore.
665 @param[out] NewPubKeyStore Saved all valid public keys in PubKeyStore.
666 @param[out] NewPubKeySize Buffer size of the NewPubKeyStore.
668 @retval EFI_SUCCESS Trim operation is complete successfully.
669 @retval EFI_OUT_OF_RESOURCES No enough memory resources, or no useless key in PubKeyStore.
674 IN UINT8
*PubKeyStore
,
675 IN UINT32 PubKeyNumber
,
676 OUT UINT32
**NewPubKeyIndex
,
677 OUT UINT8
**NewPubKeyStore
,
678 OUT UINT32
*NewPubKeySize
683 UINT32 NewPubKeyNumber
;
685 NewPubKeyNumber
= GetValidPubKeyNumber (PubKeyNumber
);
686 if (NewPubKeyNumber
== PubKeyNumber
) {
687 return EFI_OUT_OF_RESOURCES
;
690 if (NewPubKeyNumber
!= 0) {
691 *NewPubKeySize
= NewPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
;
693 *NewPubKeySize
= sizeof (UINT8
);
696 *NewPubKeyStore
= AllocatePool (*NewPubKeySize
);
697 if (*NewPubKeyStore
== NULL
) {
698 return EFI_OUT_OF_RESOURCES
;
701 *NewPubKeyIndex
= AllocateZeroPool ((PubKeyNumber
+ 1) * sizeof (UINT32
));
702 if (*NewPubKeyIndex
== NULL
) {
703 FreePool (*NewPubKeyStore
);
704 *NewPubKeyStore
= NULL
;
705 return EFI_OUT_OF_RESOURCES
;
709 for (PubKeyIndex
= 1; PubKeyIndex
<= PubKeyNumber
; PubKeyIndex
++) {
710 if (IsValidPubKeyIndex (PubKeyIndex
)) {
712 *NewPubKeyStore
+ CopiedKey
* EFI_CERT_TYPE_RSA2048_SIZE
,
713 PubKeyStore
+ (PubKeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
,
714 EFI_CERT_TYPE_RSA2048_SIZE
716 (*NewPubKeyIndex
)[PubKeyIndex
] = ++CopiedKey
;
724 Variable store garbage collection and reclaim operation.
726 If ReclaimPubKeyStore is FALSE, reclaim variable space by deleting the obsoleted varaibles.
727 If ReclaimPubKeyStore is TRUE, reclaim invalid key in public key database and update the PubKeyIndex
728 for all the count-based authenticate variable in NV storage.
730 @param[in] VariableBase Base address of variable store.
731 @param[out] LastVariableOffset Offset of last variable.
732 @param[in] IsVolatile The variable store is volatile or not;
733 if it is non-volatile, need FTW.
734 @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.
735 @param[in] NewVariable Pointer to new variable.
736 @param[in] NewVariableSize New variable size.
737 @param[in] ReclaimPubKeyStore Reclaim for public key database or not.
739 @return EFI_SUCCESS Reclaim operation has finished successfully.
740 @return EFI_OUT_OF_RESOURCES No enough memory resources or variable space.
741 @return EFI_DEVICE_ERROR The public key database doesn't exist.
742 @return Others Unexpect error happened during reclaim operation.
747 IN EFI_PHYSICAL_ADDRESS VariableBase
,
748 OUT UINTN
*LastVariableOffset
,
749 IN BOOLEAN IsVolatile
,
750 IN OUT VARIABLE_POINTER_TRACK
*UpdatingPtrTrack
,
751 IN VARIABLE_HEADER
*NewVariable
,
752 IN UINTN NewVariableSize
,
753 IN BOOLEAN ReclaimPubKeyStore
756 VARIABLE_HEADER
*Variable
;
757 VARIABLE_HEADER
*AddedVariable
;
758 VARIABLE_HEADER
*NextVariable
;
759 VARIABLE_HEADER
*NextAddedVariable
;
760 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
762 UINTN MaximumBufferSize
;
770 UINTN CommonVariableTotalSize
;
771 UINTN HwErrVariableTotalSize
;
772 UINT32
*NewPubKeyIndex
;
773 UINT8
*NewPubKeyStore
;
774 UINT32 NewPubKeySize
;
775 VARIABLE_HEADER
*PubKeyHeader
;
776 VARIABLE_HEADER
*UpdatingVariable
;
777 VARIABLE_HEADER
*UpdatingInDeletedTransition
;
779 UpdatingVariable
= NULL
;
780 UpdatingInDeletedTransition
= NULL
;
781 if (UpdatingPtrTrack
!= NULL
) {
782 UpdatingVariable
= UpdatingPtrTrack
->CurrPtr
;
783 UpdatingInDeletedTransition
= UpdatingPtrTrack
->InDeletedTransitionPtr
;
786 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
788 CommonVariableTotalSize
= 0;
789 HwErrVariableTotalSize
= 0;
790 NewPubKeyIndex
= NULL
;
791 NewPubKeyStore
= NULL
;
797 // Start Pointers for the variable.
799 Variable
= GetStartPointer (VariableStoreHeader
);
800 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
802 while (IsValidVariableHeader (Variable
)) {
803 NextVariable
= GetNextVariablePtr (Variable
);
804 if ((Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) &&
805 Variable
!= UpdatingVariable
&&
806 Variable
!= UpdatingInDeletedTransition
808 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
809 MaximumBufferSize
+= VariableSize
;
812 Variable
= NextVariable
;
815 if (NewVariable
!= NULL
) {
817 // Add the new variable size.
819 MaximumBufferSize
+= NewVariableSize
;
823 // Reserve the 1 Bytes with Oxff to identify the
824 // end of the variable buffer.
826 MaximumBufferSize
+= 1;
827 ValidBuffer
= AllocatePool (MaximumBufferSize
);
828 if (ValidBuffer
== NULL
) {
829 return EFI_OUT_OF_RESOURCES
;
833 // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache
834 // as the buffer to reduce SMRAM consumption for SMM variable driver.
836 MaximumBufferSize
= mNvVariableCache
->Size
;
837 ValidBuffer
= (UINT8
*) mNvVariableCache
;
840 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
843 // Copy variable store header.
845 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
846 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
848 if (ReclaimPubKeyStore
) {
849 ASSERT (IsVolatile
== FALSE
);
851 // Trim the PubKeyStore and get new PubKeyIndex.
853 Status
= PubKeyStoreFilter (
860 if (EFI_ERROR (Status
)) {
865 // Refresh the PubKeyIndex for all valid variables (ADDED and IN_DELETED_TRANSITION).
867 Variable
= GetStartPointer (VariableStoreHeader
);
868 while (IsValidVariableHeader (Variable
)) {
869 NextVariable
= GetNextVariablePtr (Variable
);
870 if (Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
871 if ((StrCmp (GetVariableNamePtr (Variable
), AUTHVAR_KEYDB_NAME
) == 0) &&
872 (CompareGuid (&Variable
->VendorGuid
, &gEfiAuthenticatedVariableGuid
))) {
874 // Skip the public key database, it will be reinstalled later.
876 PubKeyHeader
= Variable
;
877 Variable
= NextVariable
;
881 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
882 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
883 ((VARIABLE_HEADER
*) CurrPtr
)->PubKeyIndex
= NewPubKeyIndex
[Variable
->PubKeyIndex
];
884 CurrPtr
+= VariableSize
;
885 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
886 HwErrVariableTotalSize
+= VariableSize
;
887 } else if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
888 CommonVariableTotalSize
+= VariableSize
;
891 Variable
= NextVariable
;
895 // Reinstall the new public key database.
897 ASSERT (PubKeyHeader
!= NULL
);
898 if (PubKeyHeader
== NULL
) {
899 Status
= EFI_DEVICE_ERROR
;
902 CopyMem (CurrPtr
, (UINT8
*) PubKeyHeader
, sizeof (VARIABLE_HEADER
));
903 Variable
= (VARIABLE_HEADER
*) CurrPtr
;
904 Variable
->DataSize
= NewPubKeySize
;
905 StrCpy (GetVariableNamePtr (Variable
), GetVariableNamePtr (PubKeyHeader
));
906 CopyMem (GetVariableDataPtr (Variable
), NewPubKeyStore
, NewPubKeySize
);
907 CurrPtr
= (UINT8
*) GetNextVariablePtr (Variable
);
908 CommonVariableTotalSize
+= (UINTN
) CurrPtr
- (UINTN
) Variable
;
911 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
913 Variable
= GetStartPointer (VariableStoreHeader
);
914 while (IsValidVariableHeader (Variable
)) {
915 NextVariable
= GetNextVariablePtr (Variable
);
916 if (Variable
!= UpdatingVariable
&& Variable
->State
== VAR_ADDED
) {
917 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
918 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
919 CurrPtr
+= VariableSize
;
920 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
921 HwErrVariableTotalSize
+= VariableSize
;
922 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
923 CommonVariableTotalSize
+= VariableSize
;
926 Variable
= NextVariable
;
930 // Reinstall all in delete transition variables.
932 Variable
= GetStartPointer (VariableStoreHeader
);
933 while (IsValidVariableHeader (Variable
)) {
934 NextVariable
= GetNextVariablePtr (Variable
);
935 if (Variable
!= UpdatingVariable
&& Variable
!= UpdatingInDeletedTransition
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
938 // Buffer has cached all ADDED variable.
939 // Per IN_DELETED variable, we have to guarantee that
940 // no ADDED one in previous buffer.
944 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
945 while (IsValidVariableHeader (AddedVariable
)) {
946 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
947 NameSize
= NameSizeOfVariable (AddedVariable
);
948 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
949 NameSize
== NameSizeOfVariable (Variable
)
951 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
952 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
953 if (CompareMem (Point0
, Point1
, NameSize
) == 0) {
958 AddedVariable
= NextAddedVariable
;
962 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
964 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
965 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
966 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
967 CurrPtr
+= VariableSize
;
968 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
969 HwErrVariableTotalSize
+= VariableSize
;
970 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
971 CommonVariableTotalSize
+= VariableSize
;
976 Variable
= NextVariable
;
980 // Install the new variable if it is not NULL.
982 if (NewVariable
!= NULL
) {
983 if ((UINTN
) (CurrPtr
- ValidBuffer
) + NewVariableSize
> VariableStoreHeader
->Size
) {
985 // No enough space to store the new variable.
987 Status
= EFI_OUT_OF_RESOURCES
;
991 if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
992 HwErrVariableTotalSize
+= NewVariableSize
;
993 } else if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
994 CommonVariableTotalSize
+= NewVariableSize
;
996 if ((HwErrVariableTotalSize
> PcdGet32 (PcdHwErrStorageSize
)) ||
997 (CommonVariableTotalSize
> VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
))) {
999 // No enough space to store the new variable by NV or NV+HR attribute.
1001 Status
= EFI_OUT_OF_RESOURCES
;
1006 CopyMem (CurrPtr
, (UINT8
*) NewVariable
, NewVariableSize
);
1007 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
1008 if (UpdatingVariable
!= NULL
) {
1009 UpdatingPtrTrack
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)UpdatingPtrTrack
->StartPtr
+ ((UINTN
)CurrPtr
- (UINTN
)GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
)));
1010 UpdatingPtrTrack
->InDeletedTransitionPtr
= NULL
;
1012 CurrPtr
+= NewVariableSize
;
1018 // If volatile variable store, just copy valid buffer.
1020 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
1021 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- ValidBuffer
));
1022 *LastVariableOffset
= (UINTN
) (CurrPtr
- ValidBuffer
);
1023 Status
= EFI_SUCCESS
;
1026 // If non-volatile variable store, perform FTW here.
1028 Status
= FtwVariableSpace (
1030 (VARIABLE_STORE_HEADER
*) ValidBuffer
1032 if (!EFI_ERROR (Status
)) {
1033 *LastVariableOffset
= (UINTN
) (CurrPtr
- ValidBuffer
);
1034 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
1035 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
1037 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
1038 while (IsValidVariableHeader (NextVariable
)) {
1039 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
1040 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1041 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
1042 } else if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1043 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
1046 NextVariable
= GetNextVariablePtr (NextVariable
);
1048 *LastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableBase
;
1054 FreePool (ValidBuffer
);
1057 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.
1059 CopyMem (mNvVariableCache
, (UINT8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
1061 if (NewPubKeyStore
!= NULL
) {
1062 FreePool (NewPubKeyStore
);
1065 if (NewPubKeyIndex
!= NULL
) {
1066 FreePool (NewPubKeyIndex
);
1074 Find the variable in the specified variable store.
1076 @param[in] VariableName Name of the variable to be found
1077 @param[in] VendorGuid Vendor GUID to be found.
1078 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
1079 check at runtime when searching variable.
1080 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.
1082 @retval EFI_SUCCESS Variable found successfully
1083 @retval EFI_NOT_FOUND Variable not found
1087 IN CHAR16
*VariableName
,
1088 IN EFI_GUID
*VendorGuid
,
1089 IN BOOLEAN IgnoreRtCheck
,
1090 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
1093 VARIABLE_HEADER
*InDeletedVariable
;
1096 PtrTrack
->InDeletedTransitionPtr
= NULL
;
1099 // Find the variable by walk through HOB, volatile and non-volatile variable store.
1101 InDeletedVariable
= NULL
;
1103 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
1104 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
1105 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
1107 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
1108 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
1110 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
1111 if (VariableName
[0] == 0) {
1112 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1113 InDeletedVariable
= PtrTrack
->CurrPtr
;
1115 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
1119 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
1120 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
1122 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
1123 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
1124 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1125 InDeletedVariable
= PtrTrack
->CurrPtr
;
1127 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
1137 PtrTrack
->CurrPtr
= InDeletedVariable
;
1138 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
1143 Finds variable in storage blocks of volatile and non-volatile storage areas.
1145 This code finds variable in storage blocks of volatile and non-volatile storage areas.
1146 If VariableName is an empty string, then we just return the first
1147 qualified variable without comparing VariableName and VendorGuid.
1148 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
1149 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
1150 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
1152 @param[in] VariableName Name of the variable to be found.
1153 @param[in] VendorGuid Vendor GUID to be found.
1154 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
1155 including the range searched and the target position.
1156 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
1157 base of volatile variable storage area, base of
1158 NV variable storage area, and a lock.
1159 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
1160 check at runtime when searching variable.
1162 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
1164 @retval EFI_SUCCESS Variable successfully found.
1165 @retval EFI_NOT_FOUND Variable not found
1170 IN CHAR16
*VariableName
,
1171 IN EFI_GUID
*VendorGuid
,
1172 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
1173 IN VARIABLE_GLOBAL
*Global
,
1174 IN BOOLEAN IgnoreRtCheck
1178 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1179 VARIABLE_STORE_TYPE Type
;
1181 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
1182 return EFI_INVALID_PARAMETER
;
1186 // 0: Volatile, 1: HOB, 2: Non-Volatile.
1187 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
1188 // make use of this mapping to implement search algorithm.
1190 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
1191 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
1192 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
1195 // Find the variable by walk through HOB, volatile and non-volatile variable store.
1197 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1198 if (VariableStoreHeader
[Type
] == NULL
) {
1202 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
1203 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
1204 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
1206 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
1207 if (!EFI_ERROR (Status
)) {
1211 return EFI_NOT_FOUND
;
1215 Get index from supported language codes according to language string.
1217 This code is used to get corresponding index in supported language codes. It can handle
1218 RFC4646 and ISO639 language tags.
1219 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
1220 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
1223 SupportedLang = "engfraengfra"
1225 Iso639Language = TRUE
1226 The return value is "0".
1228 SupportedLang = "en;fr;en-US;fr-FR"
1230 Iso639Language = FALSE
1231 The return value is "3".
1233 @param SupportedLang Platform supported language codes.
1234 @param Lang Configured language.
1235 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1237 @retval The index of language in the language codes.
1241 GetIndexFromSupportedLangCodes(
1242 IN CHAR8
*SupportedLang
,
1244 IN BOOLEAN Iso639Language
1248 UINTN CompareLength
;
1249 UINTN LanguageLength
;
1251 if (Iso639Language
) {
1252 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1253 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
1254 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
1256 // Successfully find the index of Lang string in SupportedLang string.
1258 Index
= Index
/ CompareLength
;
1266 // Compare RFC4646 language code
1269 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
1271 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
1273 // Skip ';' characters in SupportedLang
1275 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
1277 // Determine the length of the next language code in SupportedLang
1279 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
1281 if ((CompareLength
== LanguageLength
) &&
1282 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
1284 // Successfully find the index of Lang string in SupportedLang string.
1295 Get language string from supported language codes according to index.
1297 This code is used to get corresponding language strings in supported language codes. It can handle
1298 RFC4646 and ISO639 language tags.
1299 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
1300 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
1303 SupportedLang = "engfraengfra"
1305 Iso639Language = TRUE
1306 The return value is "fra".
1308 SupportedLang = "en;fr;en-US;fr-FR"
1310 Iso639Language = FALSE
1311 The return value is "fr".
1313 @param SupportedLang Platform supported language codes.
1314 @param Index The index in supported language codes.
1315 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1317 @retval The language string in the language codes.
1321 GetLangFromSupportedLangCodes (
1322 IN CHAR8
*SupportedLang
,
1324 IN BOOLEAN Iso639Language
1328 UINTN CompareLength
;
1332 Supported
= SupportedLang
;
1333 if (Iso639Language
) {
1335 // According to the index of Lang string in SupportedLang string to get the language.
1336 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1337 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1339 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1340 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1341 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1346 // Take semicolon as delimitation, sequentially traverse supported language codes.
1348 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1351 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1353 // Have completed the traverse, but not find corrsponding string.
1354 // This case is not allowed to happen.
1359 if (SubIndex
== Index
) {
1361 // According to the index of Lang string in SupportedLang string to get the language.
1362 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1363 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1365 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1366 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1371 // Skip ';' characters in Supported
1373 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1379 Returns a pointer to an allocated buffer that contains the best matching language
1380 from a set of supported languages.
1382 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1383 code types may not be mixed in a single call to this function. This function
1384 supports a variable argument list that allows the caller to pass in a prioritized
1385 list of language codes to test against all the language codes in SupportedLanguages.
1387 If SupportedLanguages is NULL, then ASSERT().
1389 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1390 contains a set of language codes in the format
1391 specified by Iso639Language.
1392 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1393 in ISO 639-2 format. If FALSE, then all language
1394 codes are assumed to be in RFC 4646 language format
1395 @param[in] ... A variable argument list that contains pointers to
1396 Null-terminated ASCII strings that contain one or more
1397 language codes in the format specified by Iso639Language.
1398 The first language code from each of these language
1399 code lists is used to determine if it is an exact or
1400 close match to any of the language codes in
1401 SupportedLanguages. Close matches only apply to RFC 4646
1402 language codes, and the matching algorithm from RFC 4647
1403 is used to determine if a close match is present. If
1404 an exact or close match is found, then the matching
1405 language code from SupportedLanguages is returned. If
1406 no matches are found, then the next variable argument
1407 parameter is evaluated. The variable argument list
1408 is terminated by a NULL.
1410 @retval NULL The best matching language could not be found in SupportedLanguages.
1411 @retval NULL There are not enough resources available to return the best matching
1413 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1414 language in SupportedLanguages.
1419 VariableGetBestLanguage (
1420 IN CONST CHAR8
*SupportedLanguages
,
1421 IN BOOLEAN Iso639Language
,
1427 UINTN CompareLength
;
1428 UINTN LanguageLength
;
1429 CONST CHAR8
*Supported
;
1432 if (SupportedLanguages
== NULL
) {
1436 VA_START (Args
, Iso639Language
);
1437 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1439 // Default to ISO 639-2 mode
1442 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1445 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1447 if (!Iso639Language
) {
1448 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1452 // Trim back the length of Language used until it is empty
1454 while (LanguageLength
> 0) {
1456 // Loop through all language codes in SupportedLanguages
1458 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1460 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1462 if (!Iso639Language
) {
1464 // Skip ';' characters in Supported
1466 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1468 // Determine the length of the next language code in Supported
1470 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1472 // If Language is longer than the Supported, then skip to the next language
1474 if (LanguageLength
> CompareLength
) {
1479 // See if the first LanguageLength characters in Supported match Language
1481 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1484 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1485 Buffer
[CompareLength
] = '\0';
1486 return CopyMem (Buffer
, Supported
, CompareLength
);
1490 if (Iso639Language
) {
1492 // If ISO 639 mode, then each language can only be tested once
1497 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1499 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1506 // No matches were found
1512 This function is to check if the remaining variable space is enough to set
1513 all Variables from argument list successfully. The purpose of the check
1514 is to keep the consistency of the Variables to be in variable storage.
1516 Note: Variables are assumed to be in same storage.
1517 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1518 so follow the argument sequence to check the Variables.
1520 @param[in] Attributes Variable attributes for Variable entries.
1521 @param ... Variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1522 A NULL terminates the list.
1524 @retval TRUE Have enough variable space to set the Variables successfully.
1525 @retval FALSE No enough variable space to set the Variables successfully.
1530 CheckRemainingSpaceForConsistency (
1531 IN UINT32 Attributes
,
1537 VARIABLE_ENTRY_CONSISTENCY
*VariableEntry
;
1538 UINT64 MaximumVariableStorageSize
;
1539 UINT64 RemainingVariableStorageSize
;
1540 UINT64 MaximumVariableSize
;
1541 UINTN TotalNeededSize
;
1542 UINTN OriginalVarSize
;
1543 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1544 VARIABLE_POINTER_TRACK VariablePtrTrack
;
1545 VARIABLE_HEADER
*NextVariable
;
1548 // Non-Volatile related.
1550 VariableStoreHeader
= mNvVariableCache
;
1552 Status
= VariableServiceQueryVariableInfoInternal (
1554 &MaximumVariableStorageSize
,
1555 &RemainingVariableStorageSize
,
1556 &MaximumVariableSize
1558 ASSERT_EFI_ERROR (Status
);
1560 TotalNeededSize
= 0;
1561 VA_START (Args
, Attributes
);
1562 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1563 while (VariableEntry
!= NULL
) {
1564 TotalNeededSize
+= VariableEntry
->VariableSize
;
1565 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1569 if (RemainingVariableStorageSize
>= TotalNeededSize
) {
1571 // Already have enough space.
1574 } else if (AtRuntime ()) {
1576 // At runtime, no reclaim.
1577 // The original variable space of Variables can't be reused.
1582 VA_START (Args
, Attributes
);
1583 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1584 while (VariableEntry
!= NULL
) {
1586 // Check if Variable[Index] has been present and get its size.
1588 OriginalVarSize
= 0;
1589 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
1590 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
1591 Status
= FindVariableEx (
1592 VariableEntry
->Name
,
1593 VariableEntry
->Guid
,
1597 if (!EFI_ERROR (Status
)) {
1599 // Get size of Variable[Index].
1601 NextVariable
= GetNextVariablePtr (VariablePtrTrack
.CurrPtr
);
1602 OriginalVarSize
= (UINTN
) NextVariable
- (UINTN
) VariablePtrTrack
.CurrPtr
;
1604 // Add the original size of Variable[Index] to remaining variable storage size.
1606 RemainingVariableStorageSize
+= OriginalVarSize
;
1608 if (VariableEntry
->VariableSize
> RemainingVariableStorageSize
) {
1610 // No enough space for Variable[Index].
1616 // Sub the (new) size of Variable[Index] from remaining variable storage size.
1618 RemainingVariableStorageSize
-= VariableEntry
->VariableSize
;
1619 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1627 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1629 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1631 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1632 and are read-only. Therefore, in variable driver, only store the original value for other use.
1634 @param[in] VariableName Name of variable.
1636 @param[in] Data Variable data.
1638 @param[in] DataSize Size of data. 0 means delete.
1640 @retval EFI_SUCCESS The update operation is successful or ignored.
1641 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.
1642 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.
1643 @retval Others Other errors happened during the update operation.
1647 AutoUpdateLangVariable (
1648 IN CHAR16
*VariableName
,
1654 CHAR8
*BestPlatformLang
;
1658 VARIABLE_POINTER_TRACK Variable
;
1659 BOOLEAN SetLanguageCodes
;
1662 VARIABLE_ENTRY_CONSISTENCY VariableEntry
[2];
1665 // Don't do updates for delete operation
1667 if (DataSize
== 0) {
1671 SetLanguageCodes
= FALSE
;
1673 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
) == 0) {
1675 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1678 return EFI_WRITE_PROTECTED
;
1681 SetLanguageCodes
= TRUE
;
1684 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1685 // Therefore, in variable driver, only store the original value for other use.
1687 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1688 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1690 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1691 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1694 // PlatformLang holds a single language from PlatformLangCodes,
1695 // so the size of PlatformLangCodes is enough for the PlatformLang.
1697 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1698 FreePool (mVariableModuleGlobal
->PlatformLang
);
1700 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1701 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1703 } else if (StrCmp (VariableName
, EFI_LANG_CODES_VARIABLE_NAME
) == 0) {
1705 // LangCodes is a volatile variable, so it can not be updated at runtime.
1708 return EFI_WRITE_PROTECTED
;
1711 SetLanguageCodes
= TRUE
;
1714 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1715 // Therefore, in variable driver, only store the original value for other use.
1717 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1718 FreePool (mVariableModuleGlobal
->LangCodes
);
1720 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1721 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1724 if (SetLanguageCodes
1725 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1726 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1728 // Update Lang if PlatformLang is already set
1729 // Update PlatformLang if Lang is already set
1731 Status
= FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1732 if (!EFI_ERROR (Status
)) {
1736 VariableName
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1737 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1738 DataSize
= Variable
.CurrPtr
->DataSize
;
1740 Status
= FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1741 if (!EFI_ERROR (Status
)) {
1743 // Update PlatformLang
1745 VariableName
= EFI_LANG_VARIABLE_NAME
;
1746 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1747 DataSize
= Variable
.CurrPtr
->DataSize
;
1750 // Neither PlatformLang nor Lang is set, directly return
1757 Status
= EFI_SUCCESS
;
1760 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1762 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1764 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_VARIABLE_NAME
) == 0) {
1766 // Update Lang when PlatformLangCodes/LangCodes were set.
1768 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1770 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1772 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1773 if (BestPlatformLang
!= NULL
) {
1775 // Get the corresponding index in language codes.
1777 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1780 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1782 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1785 // Calculate the needed variable size for Lang variable.
1787 VarNameSize
= StrSize (EFI_LANG_VARIABLE_NAME
);
1788 VarDataSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1789 VariableEntry
[0].VariableSize
= sizeof (VARIABLE_HEADER
) + VarNameSize
+ GET_PAD_SIZE (VarNameSize
) + VarDataSize
+ GET_PAD_SIZE (VarDataSize
);
1790 VariableEntry
[0].VariableSize
= HEADER_ALIGN (VariableEntry
[0].VariableSize
);
1791 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1792 VariableEntry
[0].Name
= EFI_LANG_VARIABLE_NAME
;
1794 // Calculate the needed variable size for PlatformLang variable.
1796 VarNameSize
= StrSize (EFI_PLATFORM_LANG_VARIABLE_NAME
);
1797 VarDataSize
= AsciiStrSize (BestPlatformLang
);
1798 VariableEntry
[1].VariableSize
= sizeof (VARIABLE_HEADER
) + VarNameSize
+ GET_PAD_SIZE (VarNameSize
) + VarDataSize
+ GET_PAD_SIZE (VarDataSize
);
1799 VariableEntry
[1].VariableSize
= HEADER_ALIGN (VariableEntry
[1].VariableSize
);
1800 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1801 VariableEntry
[1].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1802 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1804 // No enough variable space to set both Lang and PlatformLang successfully.
1806 Status
= EFI_OUT_OF_RESOURCES
;
1809 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1811 FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1813 Status
= UpdateVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestLang
,
1814 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, 0, 0, &Variable
, NULL
);
1817 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang
, BestLang
, Status
));
1821 } else if (StrCmp (VariableName
, EFI_LANG_VARIABLE_NAME
) == 0) {
1823 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1825 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1827 // When setting Lang, firstly get most matched language string from supported language codes.
1829 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1830 if (BestLang
!= NULL
) {
1832 // Get the corresponding index in language codes.
1834 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1837 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1839 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1842 // Calculate the needed variable size for PlatformLang variable.
1844 VarNameSize
= StrSize (EFI_PLATFORM_LANG_VARIABLE_NAME
);
1845 VarDataSize
= AsciiStrSize (BestPlatformLang
);
1846 VariableEntry
[0].VariableSize
= sizeof (VARIABLE_HEADER
) + VarNameSize
+ GET_PAD_SIZE (VarNameSize
) + VarDataSize
+ GET_PAD_SIZE (VarDataSize
);
1847 VariableEntry
[0].VariableSize
= HEADER_ALIGN (VariableEntry
[0].VariableSize
);
1848 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1849 VariableEntry
[0].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1851 // Calculate the needed variable size for Lang variable.
1853 VarNameSize
= StrSize (EFI_LANG_VARIABLE_NAME
);
1854 VarDataSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1855 VariableEntry
[1].VariableSize
= sizeof (VARIABLE_HEADER
) + VarNameSize
+ GET_PAD_SIZE (VarNameSize
) + VarDataSize
+ GET_PAD_SIZE (VarDataSize
);
1856 VariableEntry
[1].VariableSize
= HEADER_ALIGN (VariableEntry
[1].VariableSize
);
1857 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1858 VariableEntry
[1].Name
= EFI_LANG_VARIABLE_NAME
;
1859 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1861 // No enough variable space to set both PlatformLang and Lang successfully.
1863 Status
= EFI_OUT_OF_RESOURCES
;
1866 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1868 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1870 Status
= UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestPlatformLang
,
1871 AsciiStrSize (BestPlatformLang
), Attributes
, 0, 0, &Variable
, NULL
);
1874 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang
, BestPlatformLang
, Status
));
1879 if (SetLanguageCodes
) {
1881 // Continue to set PlatformLangCodes or LangCodes.
1890 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
1891 index of associated public key is needed.
1893 @param[in] VariableName Name of variable.
1894 @param[in] VendorGuid Guid of variable.
1895 @param[in] Data Variable data.
1896 @param[in] DataSize Size of data. 0 means delete.
1897 @param[in] Attributes Attributes of the variable.
1898 @param[in] KeyIndex Index of associated public key.
1899 @param[in] MonotonicCount Value of associated monotonic count.
1900 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.
1901 @param[in] TimeStamp Value of associated TimeStamp.
1903 @retval EFI_SUCCESS The update operation is success.
1904 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1909 IN CHAR16
*VariableName
,
1910 IN EFI_GUID
*VendorGuid
,
1913 IN UINT32 Attributes OPTIONAL
,
1914 IN UINT32 KeyIndex OPTIONAL
,
1915 IN UINT64 MonotonicCount OPTIONAL
,
1916 IN OUT VARIABLE_POINTER_TRACK
*CacheVariable
,
1917 IN EFI_TIME
*TimeStamp OPTIONAL
1921 VARIABLE_HEADER
*NextVariable
;
1924 UINTN NonVolatileVarableStoreSize
;
1925 UINTN VarNameOffset
;
1926 UINTN VarDataOffset
;
1930 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1932 VARIABLE_POINTER_TRACK
*Variable
;
1933 VARIABLE_POINTER_TRACK NvVariable
;
1934 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1936 UINT8
*BufferForMerge
;
1937 UINTN MergedBufSize
;
1941 if (mVariableModuleGlobal
->FvbInstance
== NULL
) {
1943 // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
1945 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1947 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1949 return EFI_NOT_AVAILABLE_YET
;
1950 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1952 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1953 // The authenticated variable perhaps is not initialized, just return here.
1955 return EFI_NOT_AVAILABLE_YET
;
1959 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1960 Variable
= CacheVariable
;
1963 // Update/Delete existing NV variable.
1964 // CacheVariable points to the variable in the memory copy of Flash area
1965 // Now let Variable points to the same variable in Flash area.
1967 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1968 Variable
= &NvVariable
;
1969 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1970 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1971 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1972 if (CacheVariable
->InDeletedTransitionPtr
!= NULL
) {
1973 Variable
->InDeletedTransitionPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->InDeletedTransitionPtr
- (UINTN
)CacheVariable
->StartPtr
));
1975 Variable
->InDeletedTransitionPtr
= NULL
;
1977 Variable
->Volatile
= FALSE
;
1980 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1983 // Tricky part: Use scratch data area at the end of volatile variable store
1984 // as a temporary storage.
1986 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1987 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1988 SetMem (NextVariable
, ScratchSize
, 0xff);
1991 if (Variable
->CurrPtr
!= NULL
) {
1993 // Update/Delete existing variable.
1997 // If AtRuntime and the variable is Volatile and Runtime Access,
1998 // the volatile is ReadOnly, and SetVariable should be aborted and
1999 // return EFI_WRITE_PROTECTED.
2001 if (Variable
->Volatile
) {
2002 Status
= EFI_WRITE_PROTECTED
;
2006 // Only variable that have NV attributes can be updated/deleted in Runtime.
2008 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2009 Status
= EFI_INVALID_PARAMETER
;
2014 // Only variable that have RT attributes can be updated/deleted in Runtime.
2016 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) {
2017 Status
= EFI_INVALID_PARAMETER
;
2023 // Setting a data variable with no access, or zero DataSize attributes
2024 // causes it to be deleted.
2025 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
2026 // not delete the variable.
2028 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0))|| ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
2029 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2031 // Both ADDED and IN_DELETED_TRANSITION variable are present,
2032 // set IN_DELETED_TRANSITION one to DELETED state first.
2034 State
= Variable
->InDeletedTransitionPtr
->State
;
2035 State
&= VAR_DELETED
;
2036 Status
= UpdateVariableStore (
2037 &mVariableModuleGlobal
->VariableGlobal
,
2041 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2045 if (!EFI_ERROR (Status
)) {
2046 if (!Variable
->Volatile
) {
2047 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2048 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2055 State
= Variable
->CurrPtr
->State
;
2056 State
&= VAR_DELETED
;
2058 Status
= UpdateVariableStore (
2059 &mVariableModuleGlobal
->VariableGlobal
,
2063 (UINTN
) &Variable
->CurrPtr
->State
,
2067 if (!EFI_ERROR (Status
)) {
2068 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
2069 if (!Variable
->Volatile
) {
2070 CacheVariable
->CurrPtr
->State
= State
;
2071 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2077 // If the variable is marked valid, and the same data has been passed in,
2078 // then return to the caller immediately.
2080 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
2081 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0) &&
2082 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) &&
2083 (TimeStamp
== NULL
)) {
2085 // Variable content unchanged and no need to update timestamp, just return.
2087 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2088 Status
= EFI_SUCCESS
;
2090 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
2091 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
2094 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
2096 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
2098 // NOTE: From 0 to DataOffset of NextVariable is reserved for Variable Header and Name.
2099 // From DataOffset of NextVariable is to save the existing variable data.
2101 DataOffset
= sizeof (VARIABLE_HEADER
) + Variable
->CurrPtr
->NameSize
+ GET_PAD_SIZE (Variable
->CurrPtr
->NameSize
);
2102 BufferForMerge
= (UINT8
*) ((UINTN
) NextVariable
+ DataOffset
);
2103 CopyMem (BufferForMerge
, (UINT8
*) ((UINTN
) Variable
->CurrPtr
+ DataOffset
), Variable
->CurrPtr
->DataSize
);
2106 // Set Max Common Variable Data Size as default MaxDataSize
2108 MaxDataSize
= PcdGet32 (PcdMaxVariableSize
) - DataOffset
;
2110 if ((CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
2111 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) ||
2112 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0))) {
2114 // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of
2115 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
2117 Status
= AppendSignatureList (
2119 Variable
->CurrPtr
->DataSize
,
2120 MaxDataSize
- Variable
->CurrPtr
->DataSize
,
2125 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2127 // Signature List is too long, Failed to Append.
2129 Status
= EFI_INVALID_PARAMETER
;
2133 if (MergedBufSize
== Variable
->CurrPtr
->DataSize
) {
2134 if ((TimeStamp
== NULL
) || CompareTimeStamp (TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
2136 // New EFI_SIGNATURE_DATA is not found and timestamp is not later
2137 // than current timestamp, return EFI_SUCCESS directly.
2139 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2140 Status
= EFI_SUCCESS
;
2146 // For other Variables, append the new data to the end of existing data.
2147 // Max Harware error record variable data size is different from common variable
2149 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2150 MaxDataSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - DataOffset
;
2153 if (Variable
->CurrPtr
->DataSize
+ DataSize
> MaxDataSize
) {
2155 // Existing data size + new data size exceed maximum variable size limitation.
2157 Status
= EFI_INVALID_PARAMETER
;
2160 CopyMem ((UINT8
*) ((UINTN
) BufferForMerge
+ Variable
->CurrPtr
->DataSize
), Data
, DataSize
);
2161 MergedBufSize
= Variable
->CurrPtr
->DataSize
+ DataSize
;
2165 // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.
2167 Data
= BufferForMerge
;
2168 DataSize
= MergedBufSize
;
2173 // Mark the old variable as in delete transition.
2175 State
= Variable
->CurrPtr
->State
;
2176 State
&= VAR_IN_DELETED_TRANSITION
;
2178 Status
= UpdateVariableStore (
2179 &mVariableModuleGlobal
->VariableGlobal
,
2183 (UINTN
) &Variable
->CurrPtr
->State
,
2187 if (EFI_ERROR (Status
)) {
2190 if (!Variable
->Volatile
) {
2191 CacheVariable
->CurrPtr
->State
= State
;
2196 // Not found existing variable. Create a new variable.
2199 if ((DataSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
2200 Status
= EFI_SUCCESS
;
2205 // Make sure we are trying to create a new variable.
2206 // Setting a data variable with zero DataSize or no access attributes means to delete it.
2208 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
2209 Status
= EFI_NOT_FOUND
;
2214 // Only variable have NV|RT attribute can be created in Runtime.
2217 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
2218 Status
= EFI_INVALID_PARAMETER
;
2224 // Function part - create a new variable and copy the data.
2225 // Both update a variable and create a variable will come here.
2227 NextVariable
->StartId
= VARIABLE_DATA
;
2229 // NextVariable->State = VAR_ADDED;
2231 NextVariable
->Reserved
= 0;
2232 NextVariable
->PubKeyIndex
= KeyIndex
;
2233 NextVariable
->MonotonicCount
= MonotonicCount
;
2234 ZeroMem (&NextVariable
->TimeStamp
, sizeof (EFI_TIME
));
2236 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
2237 (TimeStamp
!= NULL
)) {
2238 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
2239 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
2242 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
2243 // when the new TimeStamp value is later than the current timestamp associated
2244 // with the variable, we need associate the new timestamp with the updated value.
2246 if (Variable
->CurrPtr
!= NULL
) {
2247 if (CompareTimeStamp (&Variable
->CurrPtr
->TimeStamp
, TimeStamp
)) {
2248 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
2255 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
2256 // Attributes bitmask parameter of a GetVariable() call.
2258 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
2260 VarNameOffset
= sizeof (VARIABLE_HEADER
);
2261 VarNameSize
= StrSize (VariableName
);
2263 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
2267 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
2270 // If DataReady is TRUE, it means the variable data has been saved into
2271 // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.
2275 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
2281 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
2283 // There will be pad bytes after Data, the NextVariable->NameSize and
2284 // NextVariable->DataSize should not include pad size so that variable
2285 // service can get actual size in GetVariable.
2287 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
2288 NextVariable
->DataSize
= (UINT32
)DataSize
;
2291 // The actual size of the variable that stores in storage should
2292 // include pad size.
2294 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
2295 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2297 // Create a nonvolatile variable.
2300 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
2301 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2302 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
2303 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
2304 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
2306 Status
= EFI_OUT_OF_RESOURCES
;
2310 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2313 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2314 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2318 HEADER_ALIGN (VarSize
),
2321 if (!EFI_ERROR (Status
)) {
2323 // The new variable has been integrated successfully during reclaiming.
2325 if (Variable
->CurrPtr
!= NULL
) {
2326 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2327 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2329 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
);
2330 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2336 // 1. Write variable header
2337 // 2. Set variable state to header valid
2338 // 3. Write variable data
2339 // 4. Set variable state to valid
2344 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
2345 Status
= UpdateVariableStore (
2346 &mVariableModuleGlobal
->VariableGlobal
,
2350 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2351 sizeof (VARIABLE_HEADER
),
2352 (UINT8
*) NextVariable
2355 if (EFI_ERROR (Status
)) {
2362 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
2363 Status
= UpdateVariableStore (
2364 &mVariableModuleGlobal
->VariableGlobal
,
2368 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2370 &NextVariable
->State
2373 if (EFI_ERROR (Status
)) {
2379 Status
= UpdateVariableStore (
2380 &mVariableModuleGlobal
->VariableGlobal
,
2384 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
2385 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
2386 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
2389 if (EFI_ERROR (Status
)) {
2395 NextVariable
->State
= VAR_ADDED
;
2396 Status
= UpdateVariableStore (
2397 &mVariableModuleGlobal
->VariableGlobal
,
2401 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2403 &NextVariable
->State
2406 if (EFI_ERROR (Status
)) {
2410 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2412 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2413 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2415 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2418 // update the memory copy of Flash region.
2420 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
2423 // Create a volatile variable.
2427 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2428 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
2430 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2433 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
2434 &mVariableModuleGlobal
->VolatileLastVariableOffset
,
2438 HEADER_ALIGN (VarSize
),
2441 if (!EFI_ERROR (Status
)) {
2443 // The new variable has been integrated successfully during reclaiming.
2445 if (Variable
->CurrPtr
!= NULL
) {
2446 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2447 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2449 UpdateVariableInfo (VariableName
, VendorGuid
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
);
2454 NextVariable
->State
= VAR_ADDED
;
2455 Status
= UpdateVariableStore (
2456 &mVariableModuleGlobal
->VariableGlobal
,
2460 mVariableModuleGlobal
->VolatileLastVariableOffset
,
2462 (UINT8
*) NextVariable
2465 if (EFI_ERROR (Status
)) {
2469 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2473 // Mark the old variable as deleted.
2475 if (!EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
2476 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2478 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
2479 // set IN_DELETED_TRANSITION one to DELETED state first.
2481 State
= Variable
->InDeletedTransitionPtr
->State
;
2482 State
&= VAR_DELETED
;
2483 Status
= UpdateVariableStore (
2484 &mVariableModuleGlobal
->VariableGlobal
,
2488 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2492 if (!EFI_ERROR (Status
)) {
2493 if (!Variable
->Volatile
) {
2494 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2495 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2502 State
= Variable
->CurrPtr
->State
;
2503 State
&= VAR_DELETED
;
2505 Status
= UpdateVariableStore (
2506 &mVariableModuleGlobal
->VariableGlobal
,
2510 (UINTN
) &Variable
->CurrPtr
->State
,
2514 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
2515 CacheVariable
->CurrPtr
->State
= State
;
2519 if (!EFI_ERROR (Status
)) {
2520 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2522 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2531 Check if a Unicode character is a hexadecimal character.
2533 This function checks if a Unicode character is a
2534 hexadecimal character. The valid hexadecimal character is
2535 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
2538 @param Char The character to check against.
2540 @retval TRUE If the Char is a hexadecmial character.
2541 @retval FALSE If the Char is not a hexadecmial character.
2546 IsHexaDecimalDigitCharacter (
2550 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
2555 This code checks if variable is hardware error record variable or not.
2557 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
2558 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
2560 @param VariableName Pointer to variable name.
2561 @param VendorGuid Variable Vendor Guid.
2563 @retval TRUE Variable is hardware error record variable.
2564 @retval FALSE Variable is not hardware error record variable.
2569 IsHwErrRecVariable (
2570 IN CHAR16
*VariableName
,
2571 IN EFI_GUID
*VendorGuid
2574 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
2575 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
2576 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
2577 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
2578 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
2579 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
2580 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
2588 This code checks if variable guid is global variable guid first.
2589 If yes, further check if variable name is in mGlobalVariableList or mGlobalVariableList2 and attributes matched.
2591 @param[in] VariableName Pointer to variable name.
2592 @param[in] VendorGuid Variable Vendor Guid.
2593 @param[in] Attributes Attributes of the variable.
2595 @retval EFI_SUCCESS Variable is not global variable, or Variable is global variable, variable name is in the lists and attributes matched.
2596 @retval EFI_INVALID_PARAMETER Variable is global variable, but variable name is not in the lists or attributes unmatched.
2601 CheckEfiGlobalVariable (
2602 IN CHAR16
*VariableName
,
2603 IN EFI_GUID
*VendorGuid
,
2604 IN UINT32 Attributes
2610 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)){
2612 // Try list 1, exactly match.
2614 for (Index
= 0; Index
< sizeof (mGlobalVariableList
)/sizeof (mGlobalVariableList
[0]); Index
++) {
2615 if ((StrCmp (mGlobalVariableList
[Index
].Name
, VariableName
) == 0) &&
2616 (Attributes
== 0 || (Attributes
& (~EFI_VARIABLE_APPEND_WRITE
)) == mGlobalVariableList
[Index
].Attributes
)) {
2624 NameLength
= StrLen (VariableName
) - 4;
2625 for (Index
= 0; Index
< sizeof (mGlobalVariableList2
)/sizeof (mGlobalVariableList2
[0]); Index
++) {
2626 if ((StrLen (VariableName
) == StrLen (mGlobalVariableList2
[Index
].Name
)) &&
2627 (StrnCmp (mGlobalVariableList2
[Index
].Name
, VariableName
, NameLength
) == 0) &&
2628 IsHexaDecimalDigitCharacter (VariableName
[NameLength
]) &&
2629 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 1]) &&
2630 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 2]) &&
2631 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 3]) &&
2632 (Attributes
== 0 || (Attributes
& (~EFI_VARIABLE_APPEND_WRITE
)) == mGlobalVariableList2
[Index
].Attributes
)) {
2637 DEBUG ((EFI_D_INFO
, "[Variable]: set global variable with invalid variable name or attributes - %g:%s:%x\n", VendorGuid
, VariableName
, Attributes
));
2638 return EFI_INVALID_PARAMETER
;
2645 Mark a variable that will become read-only after leaving the DXE phase of execution.
2647 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
2648 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
2649 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
2651 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
2652 as pending to be read-only.
2653 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
2654 Or VariableName is an empty string.
2655 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
2656 already been signaled.
2657 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
2661 VariableLockRequestToLock (
2662 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL
*This
,
2663 IN CHAR16
*VariableName
,
2664 IN EFI_GUID
*VendorGuid
2667 VARIABLE_ENTRY
*Entry
;
2669 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2670 return EFI_INVALID_PARAMETER
;
2674 return EFI_ACCESS_DENIED
;
2677 Entry
= AllocateRuntimePool (sizeof (*Entry
) + StrSize (VariableName
));
2678 if (Entry
== NULL
) {
2679 return EFI_OUT_OF_RESOURCES
;
2682 DEBUG ((EFI_D_INFO
, "[Variable] Lock: %g:%s\n", VendorGuid
, VariableName
));
2684 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2686 Entry
->Name
= (CHAR16
*) (Entry
+ 1);
2687 StrCpy (Entry
->Name
, VariableName
);
2688 CopyGuid (&Entry
->Guid
, VendorGuid
);
2689 InsertTailList (&mLockedVariableList
, &Entry
->Link
);
2691 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2697 This code checks if variable should be treated as read-only variable.
2699 @param[in] VariableName Name of the Variable.
2700 @param[in] VendorGuid GUID of the Variable.
2702 @retval TRUE This variable is read-only variable.
2703 @retval FALSE This variable is NOT read-only variable.
2707 IsReadOnlyVariable (
2708 IN CHAR16
*VariableName
,
2709 IN EFI_GUID
*VendorGuid
2712 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)) {
2713 if ((StrCmp (VariableName
, EFI_SETUP_MODE_NAME
) == 0) ||
2714 (StrCmp (VariableName
, EFI_SIGNATURE_SUPPORT_NAME
) == 0) ||
2715 (StrCmp (VariableName
, EFI_SECURE_BOOT_MODE_NAME
) == 0) ||
2716 (StrCmp (VariableName
, EFI_VENDOR_KEYS_VARIABLE_NAME
) == 0) ||
2717 (StrCmp (VariableName
, EFI_KEK_DEFAULT_VARIABLE_NAME
) == 0) ||
2718 (StrCmp (VariableName
, EFI_PK_DEFAULT_VARIABLE_NAME
) == 0) ||
2719 (StrCmp (VariableName
, EFI_DB_DEFAULT_VARIABLE_NAME
) == 0) ||
2720 (StrCmp (VariableName
, EFI_DBX_DEFAULT_VARIABLE_NAME
) == 0) ||
2721 (StrCmp (VariableName
, EFI_DBT_DEFAULT_VARIABLE_NAME
) == 0)) {
2731 This code finds variable in storage blocks (Volatile or Non-Volatile).
2733 Caution: This function may receive untrusted input.
2734 This function may be invoked in SMM mode, and datasize is external input.
2735 This function will do basic validation, before parse the data.
2737 @param VariableName Name of Variable to be found.
2738 @param VendorGuid Variable vendor GUID.
2739 @param Attributes Attribute value of the variable found.
2740 @param DataSize Size of Data found. If size is less than the
2741 data, this value contains the required size.
2742 @param Data Data pointer.
2744 @return EFI_INVALID_PARAMETER Invalid parameter.
2745 @return EFI_SUCCESS Find the specified variable.
2746 @return EFI_NOT_FOUND Not found.
2747 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2752 VariableServiceGetVariable (
2753 IN CHAR16
*VariableName
,
2754 IN EFI_GUID
*VendorGuid
,
2755 OUT UINT32
*Attributes OPTIONAL
,
2756 IN OUT UINTN
*DataSize
,
2761 VARIABLE_POINTER_TRACK Variable
;
2764 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2765 return EFI_INVALID_PARAMETER
;
2768 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2770 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2771 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2778 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2779 ASSERT (VarDataSize
!= 0);
2781 if (*DataSize
>= VarDataSize
) {
2783 Status
= EFI_INVALID_PARAMETER
;
2787 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2788 if (Attributes
!= NULL
) {
2789 *Attributes
= Variable
.CurrPtr
->Attributes
;
2792 *DataSize
= VarDataSize
;
2793 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2795 Status
= EFI_SUCCESS
;
2798 *DataSize
= VarDataSize
;
2799 Status
= EFI_BUFFER_TOO_SMALL
;
2804 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2812 This code Finds the Next available variable.
2814 Caution: This function may receive untrusted input.
2815 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2817 @param VariableNameSize Size of the variable name.
2818 @param VariableName Pointer to variable name.
2819 @param VendorGuid Variable Vendor Guid.
2821 @return EFI_INVALID_PARAMETER Invalid parameter.
2822 @return EFI_SUCCESS Find the specified variable.
2823 @return EFI_NOT_FOUND Not found.
2824 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2829 VariableServiceGetNextVariableName (
2830 IN OUT UINTN
*VariableNameSize
,
2831 IN OUT CHAR16
*VariableName
,
2832 IN OUT EFI_GUID
*VendorGuid
2835 VARIABLE_STORE_TYPE Type
;
2836 VARIABLE_POINTER_TRACK Variable
;
2837 VARIABLE_POINTER_TRACK VariableInHob
;
2838 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2841 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2843 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2844 return EFI_INVALID_PARAMETER
;
2847 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2849 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2850 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2854 if (VariableName
[0] != 0) {
2856 // If variable name is not NULL, get next variable.
2858 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2862 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2863 // The index and attributes mapping must be kept in this order as FindVariable
2864 // makes use of this mapping to implement search algorithm.
2866 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2867 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2868 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2872 // Switch from Volatile to HOB, to Non-Volatile.
2874 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
2875 (Variable
.CurrPtr
== NULL
) ||
2876 !IsValidVariableHeader (Variable
.CurrPtr
)
2879 // Find current storage index
2881 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2882 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2886 ASSERT (Type
< VariableStoreTypeMax
);
2888 // Switch to next storage
2890 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2891 if (VariableStoreHeader
[Type
] != NULL
) {
2896 // Capture the case that
2897 // 1. current storage is the last one, or
2898 // 2. no further storage
2900 if (Type
== VariableStoreTypeMax
) {
2901 Status
= EFI_NOT_FOUND
;
2904 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2905 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2906 Variable
.CurrPtr
= Variable
.StartPtr
;
2910 // Variable is found
2912 if (Variable
.CurrPtr
->State
== VAR_ADDED
|| Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2913 if (!AtRuntime () || ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
2914 if (Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2916 // If it is a IN_DELETED_TRANSITION variable,
2917 // and there is also a same ADDED one at the same time,
2920 VariablePtrTrack
.StartPtr
= Variable
.StartPtr
;
2921 VariablePtrTrack
.EndPtr
= Variable
.EndPtr
;
2922 Status
= FindVariableEx (
2923 GetVariableNamePtr (Variable
.CurrPtr
),
2924 &Variable
.CurrPtr
->VendorGuid
,
2928 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
== VAR_ADDED
) {
2929 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2935 // Don't return NV variable when HOB overrides it
2937 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2938 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2940 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2941 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2942 Status
= FindVariableEx (
2943 GetVariableNamePtr (Variable
.CurrPtr
),
2944 &Variable
.CurrPtr
->VendorGuid
,
2948 if (!EFI_ERROR (Status
)) {
2949 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2954 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2955 ASSERT (VarNameSize
!= 0);
2957 if (VarNameSize
<= *VariableNameSize
) {
2958 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2959 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2960 Status
= EFI_SUCCESS
;
2962 Status
= EFI_BUFFER_TOO_SMALL
;
2965 *VariableNameSize
= VarNameSize
;
2970 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2974 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2980 This code sets variable in storage blocks (Volatile or Non-Volatile).
2982 Caution: This function may receive untrusted input.
2983 This function may be invoked in SMM mode, and datasize and data are external input.
2984 This function will do basic validation, before parse the data.
2985 This function will parse the authentication carefully to avoid security issues, like
2986 buffer overflow, integer overflow.
2987 This function will check attribute carefully to avoid authentication bypass.
2989 @param VariableName Name of Variable to be found.
2990 @param VendorGuid Variable vendor GUID.
2991 @param Attributes Attribute value of the variable found
2992 @param DataSize Size of Data found. If size is less than the
2993 data, this value contains the required size.
2994 @param Data Data pointer.
2996 @return EFI_INVALID_PARAMETER Invalid parameter.
2997 @return EFI_SUCCESS Set successfully.
2998 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2999 @return EFI_NOT_FOUND Not found.
3000 @return EFI_WRITE_PROTECTED Variable is read-only.
3005 VariableServiceSetVariable (
3006 IN CHAR16
*VariableName
,
3007 IN EFI_GUID
*VendorGuid
,
3008 IN UINT32 Attributes
,
3013 VARIABLE_POINTER_TRACK Variable
;
3015 VARIABLE_HEADER
*NextVariable
;
3016 EFI_PHYSICAL_ADDRESS Point
;
3019 VARIABLE_ENTRY
*Entry
;
3022 // Check input parameters.
3024 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
3025 return EFI_INVALID_PARAMETER
;
3028 if (IsReadOnlyVariable (VariableName
, VendorGuid
)) {
3029 return EFI_WRITE_PROTECTED
;
3032 if (DataSize
!= 0 && Data
== NULL
) {
3033 return EFI_INVALID_PARAMETER
;
3037 // Check for reserverd bit in variable attribute.
3039 if ((Attributes
& (~EFI_VARIABLE_ATTRIBUTES_MASK
)) != 0) {
3040 return EFI_INVALID_PARAMETER
;
3044 // Make sure if runtime bit is set, boot service bit is set also.
3046 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
3047 return EFI_INVALID_PARAMETER
;
3051 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
3052 // cannot be set both.
3054 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
3055 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
3056 return EFI_INVALID_PARAMETER
;
3059 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
3060 if (DataSize
< AUTHINFO_SIZE
) {
3062 // Try to write Authenticated Variable without AuthInfo.
3064 return EFI_SECURITY_VIOLATION
;
3066 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
3067 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
3069 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
3071 if (DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
||
3072 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
)) ||
3073 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) {
3074 return EFI_SECURITY_VIOLATION
;
3076 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
3078 PayloadSize
= DataSize
;
3081 if ((UINTN
)(~0) - PayloadSize
< StrSize(VariableName
)){
3083 // Prevent whole variable size overflow
3085 return EFI_INVALID_PARAMETER
;
3089 // The size of the VariableName, including the Unicode Null in bytes plus
3090 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
3091 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
3093 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3094 if (StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
)) {
3095 return EFI_INVALID_PARAMETER
;
3097 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
3098 return EFI_INVALID_PARAMETER
;
3102 // The size of the VariableName, including the Unicode Null in bytes plus
3103 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
3105 if (StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
)) {
3106 return EFI_INVALID_PARAMETER
;
3110 Status
= CheckEfiGlobalVariable (VariableName
, VendorGuid
, Attributes
);
3111 if (EFI_ERROR (Status
)) {
3115 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3118 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
3120 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
3121 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
3123 // Parse non-volatile variable data and get last variable offset.
3125 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
3126 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
3127 && IsValidVariableHeader (NextVariable
)) {
3128 NextVariable
= GetNextVariablePtr (NextVariable
);
3130 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
3133 if (mEndOfDxe
&& mEnableLocking
) {
3135 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.
3137 for ( Link
= GetFirstNode (&mLockedVariableList
)
3138 ; !IsNull (&mLockedVariableList
, Link
)
3139 ; Link
= GetNextNode (&mLockedVariableList
, Link
)
3141 Entry
= BASE_CR (Link
, VARIABLE_ENTRY
, Link
);
3142 if (CompareGuid (&Entry
->Guid
, VendorGuid
) && (StrCmp (Entry
->Name
, VariableName
) == 0)) {
3143 Status
= EFI_WRITE_PROTECTED
;
3144 DEBUG ((EFI_D_INFO
, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid
, VariableName
));
3151 // Check whether the input variable is already existed.
3153 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
3154 if (!EFI_ERROR (Status
)) {
3155 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
3156 Status
= EFI_WRITE_PROTECTED
;
3159 if (Attributes
!= 0 && (Attributes
& (~EFI_VARIABLE_APPEND_WRITE
)) != Variable
.CurrPtr
->Attributes
) {
3161 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not
3162 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:
3163 // 1. No access attributes specified
3164 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE
3166 Status
= EFI_INVALID_PARAMETER
;
3171 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate
)) {
3173 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
3175 Status
= AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
3176 if (EFI_ERROR (Status
)) {
3178 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.
3185 // Process PK, KEK, Sigdb seperately.
3187 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
3188 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, TRUE
);
3189 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
3190 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
3191 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
3192 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) {
3193 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
3194 if (EFI_ERROR (Status
)) {
3195 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
3198 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
3202 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
3203 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3205 if (!AtRuntime ()) {
3206 if (!EFI_ERROR (Status
)) {
3219 This code returns information about the EFI variables.
3221 Caution: This function may receive untrusted input.
3222 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3224 @param Attributes Attributes bitmask to specify the type of variables
3225 on which to return information.
3226 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
3227 for the EFI variables associated with the attributes specified.
3228 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
3229 for EFI variables associated with the attributes specified.
3230 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
3231 associated with the attributes specified.
3233 @return EFI_SUCCESS Query successfully.
3238 VariableServiceQueryVariableInfoInternal (
3239 IN UINT32 Attributes
,
3240 OUT UINT64
*MaximumVariableStorageSize
,
3241 OUT UINT64
*RemainingVariableStorageSize
,
3242 OUT UINT64
*MaximumVariableSize
3245 VARIABLE_HEADER
*Variable
;
3246 VARIABLE_HEADER
*NextVariable
;
3247 UINT64 VariableSize
;
3248 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3249 UINT64 CommonVariableTotalSize
;
3250 UINT64 HwErrVariableTotalSize
;
3252 VARIABLE_POINTER_TRACK VariablePtrTrack
;
3254 CommonVariableTotalSize
= 0;
3255 HwErrVariableTotalSize
= 0;
3257 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
3259 // Query is Volatile related.
3261 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
3264 // Query is Non-Volatile related.
3266 VariableStoreHeader
= mNvVariableCache
;
3270 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
3271 // with the storage size (excluding the storage header size).
3273 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
3276 // Harware error record variable needs larger size.
3278 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3279 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
3280 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
3282 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
3283 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
3284 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
3288 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
3290 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
3294 // Point to the starting address of the variables.
3296 Variable
= GetStartPointer (VariableStoreHeader
);
3299 // Now walk through the related variable store.
3301 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
3302 NextVariable
= GetNextVariablePtr (Variable
);
3303 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
3307 // We don't take the state of the variables in mind
3308 // when calculating RemainingVariableStorageSize,
3309 // since the space occupied by variables not marked with
3310 // VAR_ADDED is not allowed to be reclaimed in Runtime.
3312 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3313 HwErrVariableTotalSize
+= VariableSize
;
3315 CommonVariableTotalSize
+= VariableSize
;
3319 // Only care about Variables with State VAR_ADDED, because
3320 // the space not marked as VAR_ADDED is reclaimable now.
3322 if (Variable
->State
== VAR_ADDED
) {
3323 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3324 HwErrVariableTotalSize
+= VariableSize
;
3326 CommonVariableTotalSize
+= VariableSize
;
3328 } else if (Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
3330 // If it is a IN_DELETED_TRANSITION variable,
3331 // and there is not also a same ADDED one at the same time,
3332 // this IN_DELETED_TRANSITION variable is valid.
3334 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
3335 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
3336 Status
= FindVariableEx (
3337 GetVariableNamePtr (Variable
),
3338 &Variable
->VendorGuid
,
3342 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
!= VAR_ADDED
) {
3343 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3344 HwErrVariableTotalSize
+= VariableSize
;
3346 CommonVariableTotalSize
+= VariableSize
;
3353 // Go to the next one.
3355 Variable
= NextVariable
;
3358 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
3359 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
3361 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
3364 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
3365 *MaximumVariableSize
= 0;
3366 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
3367 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
3375 This code returns information about the EFI variables.
3377 Caution: This function may receive untrusted input.
3378 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3380 @param Attributes Attributes bitmask to specify the type of variables
3381 on which to return information.
3382 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
3383 for the EFI variables associated with the attributes specified.
3384 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
3385 for EFI variables associated with the attributes specified.
3386 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
3387 associated with the attributes specified.
3389 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
3390 @return EFI_SUCCESS Query successfully.
3391 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
3396 VariableServiceQueryVariableInfo (
3397 IN UINT32 Attributes
,
3398 OUT UINT64
*MaximumVariableStorageSize
,
3399 OUT UINT64
*RemainingVariableStorageSize
,
3400 OUT UINT64
*MaximumVariableSize
3405 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
3406 return EFI_INVALID_PARAMETER
;
3409 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
3411 // Make sure the Attributes combination is supported by the platform.
3413 return EFI_UNSUPPORTED
;
3414 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
3416 // Make sure if runtime bit is set, boot service bit is set also.
3418 return EFI_INVALID_PARAMETER
;
3419 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
3421 // Make sure RT Attribute is set if we are in Runtime phase.
3423 return EFI_INVALID_PARAMETER
;
3424 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3426 // Make sure Hw Attribute is set with NV.
3428 return EFI_INVALID_PARAMETER
;
3431 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3433 Status
= VariableServiceQueryVariableInfoInternal (
3435 MaximumVariableStorageSize
,
3436 RemainingVariableStorageSize
,
3440 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3445 This function reclaims variable storage if free size is below the threshold.
3447 Caution: This function may be invoked at SMM mode.
3448 Care must be taken to make sure not security issue.
3457 UINTN CommonVariableSpace
;
3458 UINTN RemainingCommonVariableSpace
;
3459 UINTN RemainingHwErrVariableSpace
;
3461 Status
= EFI_SUCCESS
;
3463 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
3465 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
3467 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
3469 // Check if the free area is blow a threshold.
3471 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
3472 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
3473 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
3475 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3476 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3483 ASSERT_EFI_ERROR (Status
);
3488 Init non-volatile variable store.
3490 @retval EFI_SUCCESS Function successfully executed.
3491 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3492 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.
3496 InitNonVolatileVariableStore (
3500 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
3501 VARIABLE_HEADER
*NextVariable
;
3502 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3503 UINT64 VariableStoreLength
;
3505 EFI_HOB_GUID_TYPE
*GuidHob
;
3506 EFI_PHYSICAL_ADDRESS NvStorageBase
;
3507 UINT8
*NvStorageData
;
3508 UINT32 NvStorageSize
;
3509 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*FtwLastWriteData
;
3510 UINT32 BackUpOffset
;
3513 mVariableModuleGlobal
->FvbInstance
= NULL
;
3516 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
3517 // is stored with common variable in the same NV region. So the platform integrator should
3518 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
3519 // PcdFlashNvStorageVariableSize.
3521 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
3524 // Allocate runtime memory used for a memory copy of the FLASH region.
3525 // Keep the memory and the FLASH in sync as updates occur.
3527 NvStorageSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
3528 NvStorageData
= AllocateRuntimeZeroPool (NvStorageSize
);
3529 if (NvStorageData
== NULL
) {
3530 return EFI_OUT_OF_RESOURCES
;
3533 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
3534 if (NvStorageBase
== 0) {
3535 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3538 // Copy NV storage data to the memory buffer.
3540 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) NvStorageBase
, NvStorageSize
);
3543 // Check the FTW last write data hob.
3545 GuidHob
= GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid
);
3546 if (GuidHob
!= NULL
) {
3547 FtwLastWriteData
= (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*) GET_GUID_HOB_DATA (GuidHob
);
3548 if (FtwLastWriteData
->TargetAddress
== NvStorageBase
) {
3549 DEBUG ((EFI_D_INFO
, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN
) FtwLastWriteData
->SpareAddress
));
3551 // Copy the backed up NV storage data to the memory buffer from spare block.
3553 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) (FtwLastWriteData
->SpareAddress
), NvStorageSize
);
3554 } else if ((FtwLastWriteData
->TargetAddress
> NvStorageBase
) &&
3555 (FtwLastWriteData
->TargetAddress
< (NvStorageBase
+ NvStorageSize
))) {
3557 // Flash NV storage from the Offset is backed up in spare block.
3559 BackUpOffset
= (UINT32
) (FtwLastWriteData
->TargetAddress
- NvStorageBase
);
3560 BackUpSize
= NvStorageSize
- BackUpOffset
;
3561 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
));
3563 // Copy the partial backed up NV storage data to the memory buffer from spare block.
3565 CopyMem (NvStorageData
+ BackUpOffset
, (UINT8
*) (UINTN
) FtwLastWriteData
->SpareAddress
, BackUpSize
);
3569 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) NvStorageData
;
3572 // Check if the Firmware Volume is not corrupted
3574 if ((FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) || (!CompareGuid (&gEfiSystemNvDataFvGuid
, &FvHeader
->FileSystemGuid
))) {
3575 FreePool (NvStorageData
);
3576 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
3577 return EFI_VOLUME_CORRUPTED
;
3580 VariableStoreBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) FvHeader
+ FvHeader
->HeaderLength
);
3581 VariableStoreLength
= (UINT64
) (NvStorageSize
- FvHeader
->HeaderLength
);
3583 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3584 mNvVariableCache
= (VARIABLE_STORE_HEADER
*) (UINTN
) VariableStoreBase
;
3585 if (GetVariableStoreStatus (mNvVariableCache
) != EfiValid
) {
3586 FreePool (NvStorageData
);
3587 DEBUG((EFI_D_ERROR
, "Variable Store header is corrupted\n"));
3588 return EFI_VOLUME_CORRUPTED
;
3590 ASSERT(mNvVariableCache
->Size
== VariableStoreLength
);
3593 // The max variable or hardware error variable size should be < variable store size.
3595 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) < VariableStoreLength
);
3598 // Parse non-volatile variable data and get last variable offset.
3600 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
3601 while (IsValidVariableHeader (NextVariable
)) {
3602 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
3603 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3604 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3606 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3609 NextVariable
= GetNextVariablePtr (NextVariable
);
3611 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
3617 Flush the HOB variable to flash.
3619 @param[in] VariableName Name of variable has been updated or deleted.
3620 @param[in] VendorGuid Guid of variable has been updated or deleted.
3624 FlushHobVariableToFlash (
3625 IN CHAR16
*VariableName
,
3626 IN EFI_GUID
*VendorGuid
3630 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3631 VARIABLE_HEADER
*Variable
;
3638 // Flush the HOB variable to flash.
3640 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3641 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
3643 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
3645 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
3646 for ( Variable
= GetStartPointer (VariableStoreHeader
)
3647 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
3648 ; Variable
= GetNextVariablePtr (Variable
)
3650 if (Variable
->State
!= VAR_ADDED
) {
3652 // The HOB variable has been set to DELETED state in local.
3656 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
3657 if (VendorGuid
== NULL
|| VariableName
== NULL
||
3658 !CompareGuid (VendorGuid
, &Variable
->VendorGuid
) ||
3659 StrCmp (VariableName
, GetVariableNamePtr (Variable
)) != 0) {
3660 VariableData
= GetVariableDataPtr (Variable
);
3661 Status
= VariableServiceSetVariable (
3662 GetVariableNamePtr (Variable
),
3663 &Variable
->VendorGuid
,
3664 Variable
->Attributes
,
3668 DEBUG ((EFI_D_INFO
, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
), Status
));
3671 // The updated or deleted variable is matched with the HOB variable.
3672 // Don't break here because we will try to set other HOB variables
3673 // since this variable could be set successfully.
3675 Status
= EFI_SUCCESS
;
3677 if (!EFI_ERROR (Status
)) {
3679 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
3680 // set the HOB variable to DELETED state in local.
3682 DEBUG ((EFI_D_INFO
, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
)));
3683 Variable
->State
&= VAR_DELETED
;
3690 // We still have HOB variable(s) not flushed in flash.
3692 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
3695 // All HOB variables have been flushed in flash.
3697 DEBUG ((EFI_D_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
3698 if (!AtRuntime ()) {
3699 FreePool ((VOID
*) VariableStoreHeader
);
3707 Initializes variable write service after FTW was ready.
3709 @retval EFI_SUCCESS Function successfully executed.
3710 @retval Others Fail to initialize the variable service.
3714 VariableWriteServiceInitialize (
3719 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3722 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3723 EFI_PHYSICAL_ADDRESS NvStorageBase
;
3725 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
3726 if (NvStorageBase
== 0) {
3727 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3729 VariableStoreBase
= NvStorageBase
+ (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(NvStorageBase
))->HeaderLength
);
3732 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
3734 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3735 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
3738 // Check if the free area is really free.
3740 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
3741 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
3744 // There must be something wrong in variable store, do reclaim operation.
3747 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3748 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3755 if (EFI_ERROR (Status
)) {
3762 FlushHobVariableToFlash (NULL
, NULL
);
3765 // Authenticated variable initialize.
3767 Status
= AutenticatedVariableServiceInitialize ();
3774 Initializes variable store area for non-volatile and volatile variable.
3776 @retval EFI_SUCCESS Function successfully executed.
3777 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3781 VariableCommonInitialize (
3786 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
3787 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3788 UINT64 VariableStoreLength
;
3790 EFI_HOB_GUID_TYPE
*GuidHob
;
3793 // Allocate runtime memory for variable driver global structure.
3795 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
3796 if (mVariableModuleGlobal
== NULL
) {
3797 return EFI_OUT_OF_RESOURCES
;
3800 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
3803 // Get HOB variable store.
3805 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
3806 if (GuidHob
!= NULL
) {
3807 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
3808 VariableStoreLength
= (UINT64
) (GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
));
3809 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
3810 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
3811 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
3812 FreePool (mVariableModuleGlobal
);
3813 return EFI_OUT_OF_RESOURCES
;
3816 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
3821 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
3823 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
3824 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
3825 if (VolatileVariableStore
== NULL
) {
3826 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3827 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3829 FreePool (mVariableModuleGlobal
);
3830 return EFI_OUT_OF_RESOURCES
;
3833 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
3836 // Initialize Variable Specific Data.
3838 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
3839 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
3841 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiAuthenticatedVariableGuid
);
3842 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
3843 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
3844 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
3845 VolatileVariableStore
->Reserved
= 0;
3846 VolatileVariableStore
->Reserved1
= 0;
3849 // Init non-volatile variable store.
3851 Status
= InitNonVolatileVariableStore ();
3852 if (EFI_ERROR (Status
)) {
3853 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3854 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3856 FreePool (mVariableModuleGlobal
);
3857 FreePool (VolatileVariableStore
);
3865 Get the proper fvb handle and/or fvb protocol by the given Flash address.
3867 @param[in] Address The Flash address.
3868 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
3869 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
3873 GetFvbInfoByAddress (
3874 IN EFI_PHYSICAL_ADDRESS Address
,
3875 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
3876 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
3880 EFI_HANDLE
*HandleBuffer
;
3883 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
3884 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
3885 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
3886 EFI_FVB_ATTRIBUTES_2 Attributes
;
3889 // Get all FVB handles.
3891 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
3892 if (EFI_ERROR (Status
)) {
3893 return EFI_NOT_FOUND
;
3897 // Get the FVB to access variable store.
3900 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
3901 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
3902 if (EFI_ERROR (Status
)) {
3903 Status
= EFI_NOT_FOUND
;
3908 // Ensure this FVB protocol supported Write operation.
3910 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
3911 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
3916 // Compare the address and select the right one.
3918 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
3919 if (EFI_ERROR (Status
)) {
3923 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
3924 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
3925 if (FvbHandle
!= NULL
) {
3926 *FvbHandle
= HandleBuffer
[Index
];
3928 if (FvbProtocol
!= NULL
) {
3931 Status
= EFI_SUCCESS
;
3935 FreePool (HandleBuffer
);
3938 Status
= EFI_NOT_FOUND
;