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 - 2013, 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
;
47 Routine used to track statistical information about variable usage.
48 The data is stored in the EFI system table so it can be accessed later.
49 VariableInfo.efi can dump out the table. Only Boot Services variable
50 accesses are tracked by this code. The PcdVariableCollectStatistics
51 build flag controls if this feature is enabled.
53 A read that hits in the cache will have Read and Cache true for
54 the transaction. Data is allocated by this routine, but never
57 @param[in] VariableName Name of the Variable to track.
58 @param[in] VendorGuid Guid of the Variable to track.
59 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
60 @param[in] Read TRUE if GetVariable() was called.
61 @param[in] Write TRUE if SetVariable() was called.
62 @param[in] Delete TRUE if deleted via SetVariable().
63 @param[in] Cache TRUE for a cache hit.
68 IN CHAR16
*VariableName
,
69 IN EFI_GUID
*VendorGuid
,
77 VARIABLE_INFO_ENTRY
*Entry
;
79 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
82 // Don't collect statistics at runtime.
86 if (gVariableInfo
== NULL
) {
88 // On the first call allocate a entry and place a pointer to it in
89 // the EFI System Table.
91 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
92 ASSERT (gVariableInfo
!= NULL
);
94 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
95 gVariableInfo
->Name
= AllocatePool (StrSize (VariableName
));
96 ASSERT (gVariableInfo
->Name
!= NULL
);
97 StrCpy (gVariableInfo
->Name
, VariableName
);
98 gVariableInfo
->Volatile
= Volatile
;
102 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
103 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
104 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
112 Entry
->DeleteCount
++;
122 if (Entry
->Next
== NULL
) {
124 // If the entry is not in the table add it.
125 // Next iteration of the loop will fill in the data.
127 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
128 ASSERT (Entry
->Next
!= NULL
);
130 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
131 Entry
->Next
->Name
= AllocatePool (StrSize (VariableName
));
132 ASSERT (Entry
->Next
->Name
!= NULL
);
133 StrCpy (Entry
->Next
->Name
, VariableName
);
134 Entry
->Next
->Volatile
= Volatile
;
144 This code checks if variable header is valid or not.
146 @param Variable Pointer to the Variable Header.
148 @retval TRUE Variable header is valid.
149 @retval FALSE Variable header is not valid.
153 IsValidVariableHeader (
154 IN VARIABLE_HEADER
*Variable
157 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
167 This function writes data to the FWH at the correct LBA even if the LBAs
170 @param Global Pointer to VARAIBLE_GLOBAL structure.
171 @param Volatile Point out the Variable is Volatile or Non-Volatile.
172 @param SetByIndex TRUE if target pointer is given as index.
173 FALSE if target pointer is absolute.
174 @param Fvb Pointer to the writable FVB protocol.
175 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
177 @param DataSize Size of data to be written.
178 @param Buffer Pointer to the buffer from which data is written.
180 @retval EFI_INVALID_PARAMETER Parameters not valid.
181 @retval EFI_SUCCESS Variable store successfully updated.
185 UpdateVariableStore (
186 IN VARIABLE_GLOBAL
*Global
,
188 IN BOOLEAN SetByIndex
,
189 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
190 IN UINTN DataPtrIndex
,
195 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
203 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
204 VARIABLE_STORE_HEADER
*VolatileBase
;
205 EFI_PHYSICAL_ADDRESS FvVolHdr
;
206 EFI_PHYSICAL_ADDRESS DataPtr
;
210 DataPtr
= DataPtrIndex
;
213 // Check if the Data is Volatile.
217 return EFI_INVALID_PARAMETER
;
219 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
220 ASSERT_EFI_ERROR (Status
);
222 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
224 // Data Pointer should point to the actual Address where data is to be
228 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
231 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
232 return EFI_INVALID_PARAMETER
;
236 // Data Pointer should point to the actual Address where data is to be
239 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
241 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
244 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
245 return EFI_INVALID_PARAMETER
;
249 // If Volatile Variable just do a simple mem copy.
251 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
256 // If we are here we are dealing with Non-Volatile Variables.
258 LinearOffset
= (UINTN
) FwVolHeader
;
259 CurrWritePtr
= (UINTN
) DataPtr
;
260 CurrWriteSize
= DataSize
;
264 if (CurrWritePtr
< LinearOffset
) {
265 return EFI_INVALID_PARAMETER
;
268 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
269 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
271 // Check to see if the Variable Writes are spanning through multiple
274 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
275 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
276 Status
= Fvb
->Write (
279 (UINTN
) (CurrWritePtr
- LinearOffset
),
285 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
286 Status
= Fvb
->Write (
289 (UINTN
) (CurrWritePtr
- LinearOffset
),
293 if (EFI_ERROR (Status
)) {
297 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
298 CurrBuffer
= CurrBuffer
+ Size
;
299 CurrWriteSize
= CurrWriteSize
- Size
;
303 LinearOffset
+= PtrBlockMapEntry
->Length
;
314 This code gets the current status of Variable Store.
316 @param VarStoreHeader Pointer to the Variable Store Header.
318 @retval EfiRaw Variable store status is raw.
319 @retval EfiValid Variable store status is valid.
320 @retval EfiInvalid Variable store status is invalid.
323 VARIABLE_STORE_STATUS
324 GetVariableStoreStatus (
325 IN VARIABLE_STORE_HEADER
*VarStoreHeader
328 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) &&
329 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
330 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
334 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
335 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
336 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
337 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
338 VarStoreHeader
->Size
== 0xffffffff &&
339 VarStoreHeader
->Format
== 0xff &&
340 VarStoreHeader
->State
== 0xff
352 This code gets the size of name of variable.
354 @param Variable Pointer to the Variable Header.
356 @return UINTN Size of variable in bytes.
361 IN VARIABLE_HEADER
*Variable
364 if (Variable
->State
== (UINT8
) (-1) ||
365 Variable
->DataSize
== (UINT32
) (-1) ||
366 Variable
->NameSize
== (UINT32
) (-1) ||
367 Variable
->Attributes
== (UINT32
) (-1)) {
370 return (UINTN
) Variable
->NameSize
;
375 This code gets the size of variable data.
377 @param Variable Pointer to the Variable Header.
379 @return Size of variable in bytes.
384 IN VARIABLE_HEADER
*Variable
387 if (Variable
->State
== (UINT8
) (-1) ||
388 Variable
->DataSize
== (UINT32
) (-1) ||
389 Variable
->NameSize
== (UINT32
) (-1) ||
390 Variable
->Attributes
== (UINT32
) (-1)) {
393 return (UINTN
) Variable
->DataSize
;
398 This code gets the pointer to the variable name.
400 @param Variable Pointer to the Variable Header.
402 @return Pointer to Variable Name which is Unicode encoding.
407 IN VARIABLE_HEADER
*Variable
411 return (CHAR16
*) (Variable
+ 1);
416 This code gets the pointer to the variable data.
418 @param Variable Pointer to the Variable Header.
420 @return Pointer to Variable Data.
425 IN VARIABLE_HEADER
*Variable
431 // Be careful about pad size for alignment.
433 Value
= (UINTN
) GetVariableNamePtr (Variable
);
434 Value
+= NameSizeOfVariable (Variable
);
435 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
437 return (UINT8
*) Value
;
443 This code gets the pointer to the next variable header.
445 @param Variable Pointer to the Variable Header.
447 @return Pointer to next variable header.
452 IN VARIABLE_HEADER
*Variable
457 if (!IsValidVariableHeader (Variable
)) {
461 Value
= (UINTN
) GetVariableDataPtr (Variable
);
462 Value
+= DataSizeOfVariable (Variable
);
463 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
466 // Be careful about pad size for alignment.
468 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
473 Gets the pointer to the first variable header in given variable store area.
475 @param VarStoreHeader Pointer to the Variable Store Header.
477 @return Pointer to the first variable header.
482 IN VARIABLE_STORE_HEADER
*VarStoreHeader
486 // The end of variable store.
488 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
493 Gets the pointer to the end of the variable storage area.
495 This function gets pointer to the end of the variable storage
496 area, according to the input variable store header.
498 @param VarStoreHeader Pointer to the Variable Store Header.
500 @return Pointer to the end of the variable storage area.
505 IN VARIABLE_STORE_HEADER
*VarStoreHeader
509 // The end of variable store
511 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
516 Check the PubKeyIndex is a valid key or not.
518 This function will iterate the NV storage to see if this PubKeyIndex is still referenced
519 by any valid count-based auth variabe.
521 @param[in] PubKeyIndex Index of the public key in public key store.
523 @retval TRUE The PubKeyIndex is still in use.
524 @retval FALSE The PubKeyIndex is not referenced by any count-based auth variabe.
529 IN UINT32 PubKeyIndex
532 VARIABLE_HEADER
*Variable
;
534 if (PubKeyIndex
> mPubKeyNumber
) {
538 Variable
= GetStartPointer (mNvVariableCache
);
540 while (IsValidVariableHeader (Variable
)) {
541 if ((Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) &&
542 Variable
->PubKeyIndex
== PubKeyIndex
) {
545 Variable
= GetNextVariablePtr (Variable
);
553 Get the number of valid public key in PubKeyStore.
555 @param[in] PubKeyNumber Number of the public key in public key store.
557 @return Number of valid public key in PubKeyStore.
561 GetValidPubKeyNumber (
562 IN UINT32 PubKeyNumber
570 for (PubKeyIndex
= 1; PubKeyIndex
<= PubKeyNumber
; PubKeyIndex
++) {
571 if (IsValidPubKeyIndex (PubKeyIndex
)) {
581 Filter the useless key in public key store.
583 This function will find out all valid public keys in public key database, save them in new allocated
584 buffer NewPubKeyStore, and give the new PubKeyIndex. The caller is responsible for freeing buffer
585 NewPubKeyIndex and NewPubKeyStore with FreePool().
587 @param[in] PubKeyStore Point to the public key database.
588 @param[in] PubKeyNumber Number of the public key in PubKeyStore.
589 @param[out] NewPubKeyIndex Point to an array of new PubKeyIndex corresponds to NewPubKeyStore.
590 @param[out] NewPubKeyStore Saved all valid public keys in PubKeyStore.
591 @param[out] NewPubKeySize Buffer size of the NewPubKeyStore.
593 @retval EFI_SUCCESS Trim operation is complete successfully.
594 @retval EFI_OUT_OF_RESOURCES No enough memory resources, or no useless key in PubKeyStore.
599 IN UINT8
*PubKeyStore
,
600 IN UINT32 PubKeyNumber
,
601 OUT UINT32
**NewPubKeyIndex
,
602 OUT UINT8
**NewPubKeyStore
,
603 OUT UINT32
*NewPubKeySize
608 UINT32 NewPubKeyNumber
;
610 NewPubKeyNumber
= GetValidPubKeyNumber (PubKeyNumber
);
611 if (NewPubKeyNumber
== PubKeyNumber
) {
612 return EFI_OUT_OF_RESOURCES
;
615 if (NewPubKeyNumber
!= 0) {
616 *NewPubKeySize
= NewPubKeyNumber
* EFI_CERT_TYPE_RSA2048_SIZE
;
618 *NewPubKeySize
= sizeof (UINT8
);
621 *NewPubKeyStore
= AllocatePool (*NewPubKeySize
);
622 if (*NewPubKeyStore
== NULL
) {
623 return EFI_OUT_OF_RESOURCES
;
626 *NewPubKeyIndex
= AllocateZeroPool ((PubKeyNumber
+ 1) * sizeof (UINT32
));
627 if (*NewPubKeyIndex
== NULL
) {
628 FreePool (*NewPubKeyStore
);
629 return EFI_OUT_OF_RESOURCES
;
633 for (PubKeyIndex
= 1; PubKeyIndex
<= PubKeyNumber
; PubKeyIndex
++) {
634 if (IsValidPubKeyIndex (PubKeyIndex
)) {
636 *NewPubKeyStore
+ CopiedKey
* EFI_CERT_TYPE_RSA2048_SIZE
,
637 PubKeyStore
+ (PubKeyIndex
- 1) * EFI_CERT_TYPE_RSA2048_SIZE
,
638 EFI_CERT_TYPE_RSA2048_SIZE
640 (*NewPubKeyIndex
)[PubKeyIndex
] = ++CopiedKey
;
648 Variable store garbage collection and reclaim operation.
650 If ReclaimPubKeyStore is FALSE, reclaim variable space by deleting the obsoleted varaibles.
651 If ReclaimPubKeyStore is TRUE, reclaim invalid key in public key database and update the PubKeyIndex
652 for all the count-based authenticate variable in NV storage.
654 @param[in] VariableBase Base address of variable store.
655 @param[out] LastVariableOffset Offset of last variable.
656 @param[in] IsVolatile The variable store is volatile or not;
657 if it is non-volatile, need FTW.
658 @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.
659 @param[in] ReclaimPubKeyStore Reclaim for public key database or not.
660 @param[in] ReclaimAnyway If TRUE, do reclaim anyway.
662 @return EFI_SUCCESS Reclaim operation has finished successfully.
663 @return EFI_OUT_OF_RESOURCES No enough memory resources.
664 @return EFI_DEVICE_ERROR The public key database doesn't exist.
665 @return Others Unexpect error happened during reclaim operation.
670 IN EFI_PHYSICAL_ADDRESS VariableBase
,
671 OUT UINTN
*LastVariableOffset
,
672 IN BOOLEAN IsVolatile
,
673 IN OUT VARIABLE_POINTER_TRACK
*UpdatingPtrTrack
,
674 IN BOOLEAN ReclaimPubKeyStore
,
675 IN BOOLEAN ReclaimAnyway
678 VARIABLE_HEADER
*Variable
;
679 VARIABLE_HEADER
*AddedVariable
;
680 VARIABLE_HEADER
*NextVariable
;
681 VARIABLE_HEADER
*NextAddedVariable
;
682 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
684 UINTN MaximumBufferSize
;
686 UINTN VariableNameSize
;
687 UINTN UpdatingVariableNameSize
;
694 CHAR16
*VariableNamePtr
;
695 CHAR16
*UpdatingVariableNamePtr
;
696 UINTN CommonVariableTotalSize
;
697 UINTN HwErrVariableTotalSize
;
698 UINT32
*NewPubKeyIndex
;
699 UINT8
*NewPubKeyStore
;
700 UINT32 NewPubKeySize
;
701 VARIABLE_HEADER
*PubKeyHeader
;
702 BOOLEAN NeedDoReclaim
;
703 VARIABLE_HEADER
*UpdatingVariable
;
705 UpdatingVariable
= NULL
;
706 if (UpdatingPtrTrack
!= NULL
) {
707 UpdatingVariable
= UpdatingPtrTrack
->CurrPtr
;
710 NeedDoReclaim
= FALSE
;
711 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
713 CommonVariableTotalSize
= 0;
714 HwErrVariableTotalSize
= 0;
715 NewPubKeyIndex
= NULL
;
716 NewPubKeyStore
= NULL
;
721 // Start Pointers for the variable.
723 Variable
= GetStartPointer (VariableStoreHeader
);
724 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
726 while (IsValidVariableHeader (Variable
)) {
727 NextVariable
= GetNextVariablePtr (Variable
);
728 if (Variable
->State
== VAR_ADDED
||
729 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
731 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
732 MaximumBufferSize
+= VariableSize
;
734 NeedDoReclaim
= TRUE
;
737 Variable
= NextVariable
;
740 if (!ReclaimAnyway
&& !NeedDoReclaim
) {
741 DEBUG ((EFI_D_INFO
, "Variable driver: no DELETED variable found, so no variable space could be reclaimed.\n"));
746 // Reserve the 1 Bytes with Oxff to identify the
747 // end of the variable buffer.
749 MaximumBufferSize
+= 1;
750 ValidBuffer
= AllocatePool (MaximumBufferSize
);
751 if (ValidBuffer
== NULL
) {
752 return EFI_OUT_OF_RESOURCES
;
755 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
758 // Copy variable store header.
760 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
761 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
763 if (ReclaimPubKeyStore
) {
765 // Trim the PubKeyStore and get new PubKeyIndex.
767 Status
= PubKeyStoreFilter (
774 if (EFI_ERROR (Status
)) {
775 FreePool (ValidBuffer
);
780 // Refresh the PubKeyIndex for all valid variables (ADDED and IN_DELETED_TRANSITION).
782 Variable
= GetStartPointer (mNvVariableCache
);
783 while (IsValidVariableHeader (Variable
)) {
784 NextVariable
= GetNextVariablePtr (Variable
);
785 if (Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
786 if ((StrCmp (GetVariableNamePtr (Variable
), AUTHVAR_KEYDB_NAME
) == 0) &&
787 (CompareGuid (&Variable
->VendorGuid
, &gEfiAuthenticatedVariableGuid
))) {
789 // Skip the public key database, it will be reinstalled later.
791 PubKeyHeader
= Variable
;
792 Variable
= NextVariable
;
796 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
797 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
798 ((VARIABLE_HEADER
*) CurrPtr
)->PubKeyIndex
= NewPubKeyIndex
[Variable
->PubKeyIndex
];
799 CurrPtr
+= VariableSize
;
800 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
801 HwErrVariableTotalSize
+= VariableSize
;
802 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
803 CommonVariableTotalSize
+= VariableSize
;
806 Variable
= NextVariable
;
810 // Reinstall the new public key database.
812 ASSERT (PubKeyHeader
!= NULL
);
813 if (PubKeyHeader
== NULL
) {
814 FreePool (ValidBuffer
);
815 FreePool (NewPubKeyIndex
);
816 FreePool (NewPubKeyStore
);
817 return EFI_DEVICE_ERROR
;
819 CopyMem (CurrPtr
, (UINT8
*) PubKeyHeader
, sizeof (VARIABLE_HEADER
));
820 Variable
= (VARIABLE_HEADER
*) CurrPtr
;
821 Variable
->DataSize
= NewPubKeySize
;
822 StrCpy (GetVariableNamePtr (Variable
), GetVariableNamePtr (PubKeyHeader
));
823 CopyMem (GetVariableDataPtr (Variable
), NewPubKeyStore
, NewPubKeySize
);
824 CurrPtr
= (UINT8
*) GetNextVariablePtr (Variable
);
825 CommonVariableTotalSize
+= (UINTN
) CurrPtr
- (UINTN
) Variable
;
828 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
830 Variable
= GetStartPointer (VariableStoreHeader
);
831 while (IsValidVariableHeader (Variable
)) {
832 NextVariable
= GetNextVariablePtr (Variable
);
833 if (Variable
->State
== VAR_ADDED
) {
834 if (UpdatingVariable
!= NULL
) {
835 if (UpdatingVariable
== Variable
) {
836 Variable
= NextVariable
;
840 VariableNameSize
= NameSizeOfVariable(Variable
);
841 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
843 VariableNamePtr
= GetVariableNamePtr (Variable
);
844 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
845 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
846 VariableNameSize
== UpdatingVariableNameSize
&&
847 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
848 Variable
= NextVariable
;
852 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
853 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
854 CurrPtr
+= VariableSize
;
855 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
856 HwErrVariableTotalSize
+= VariableSize
;
857 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
858 CommonVariableTotalSize
+= VariableSize
;
861 Variable
= NextVariable
;
865 // Reinstall the variable being updated if it is not NULL.
867 if (UpdatingVariable
!= NULL
) {
868 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
869 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
870 UpdatingPtrTrack
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)UpdatingPtrTrack
->StartPtr
+ ((UINTN
)CurrPtr
- (UINTN
)GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
)));
871 UpdatingPtrTrack
->InDeletedTransitionPtr
= NULL
;
872 CurrPtr
+= VariableSize
;
873 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
874 HwErrVariableTotalSize
+= VariableSize
;
875 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
876 CommonVariableTotalSize
+= VariableSize
;
881 // Reinstall all in delete transition variables.
883 Variable
= GetStartPointer (VariableStoreHeader
);
884 while (IsValidVariableHeader (Variable
)) {
885 NextVariable
= GetNextVariablePtr (Variable
);
886 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
889 // Buffer has cached all ADDED variable.
890 // Per IN_DELETED variable, we have to guarantee that
891 // no ADDED one in previous buffer.
895 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
896 while (IsValidVariableHeader (AddedVariable
)) {
897 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
898 NameSize
= NameSizeOfVariable (AddedVariable
);
899 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
900 NameSize
== NameSizeOfVariable (Variable
)
902 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
903 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
904 if (CompareMem (Point0
, Point1
, NameSize
) == 0) {
909 AddedVariable
= NextAddedVariable
;
913 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
915 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
916 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
917 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
918 CurrPtr
+= VariableSize
;
919 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
920 HwErrVariableTotalSize
+= VariableSize
;
921 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
922 CommonVariableTotalSize
+= VariableSize
;
927 Variable
= NextVariable
;
933 // If volatile variable store, just copy valid buffer.
935 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
936 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
937 Status
= EFI_SUCCESS
;
940 // If non-volatile variable store, perform FTW here.
942 Status
= FtwVariableSpace (
945 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
947 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
949 if (!EFI_ERROR (Status
)) {
950 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
952 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
953 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
956 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
957 while (IsValidVariableHeader (NextVariable
)) {
958 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
959 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
960 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
961 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
962 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
965 NextVariable
= GetNextVariablePtr (NextVariable
);
967 *LastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableBase
;
970 if (NewPubKeyStore
!= NULL
) {
971 FreePool (NewPubKeyStore
);
974 if (NewPubKeyIndex
!= NULL
) {
975 FreePool (NewPubKeyIndex
);
978 FreePool (ValidBuffer
);
984 Find the variable in the specified variable store.
986 @param[in] VariableName Name of the variable to be found
987 @param[in] VendorGuid Vendor GUID to be found.
988 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
989 check at runtime when searching variable.
990 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.
992 @retval EFI_SUCCESS Variable found successfully
993 @retval EFI_NOT_FOUND Variable not found
997 IN CHAR16
*VariableName
,
998 IN EFI_GUID
*VendorGuid
,
999 IN BOOLEAN IgnoreRtCheck
,
1000 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
1003 VARIABLE_HEADER
*InDeletedVariable
;
1006 PtrTrack
->InDeletedTransitionPtr
= NULL
;
1009 // Find the variable by walk through HOB, volatile and non-volatile variable store.
1011 InDeletedVariable
= NULL
;
1013 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
1014 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
1015 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
1017 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
1018 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
1020 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
1021 if (VariableName
[0] == 0) {
1022 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1023 InDeletedVariable
= PtrTrack
->CurrPtr
;
1025 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
1029 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
1030 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
1032 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
1033 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
1034 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
1035 InDeletedVariable
= PtrTrack
->CurrPtr
;
1037 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
1047 PtrTrack
->CurrPtr
= InDeletedVariable
;
1048 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
1053 Finds variable in storage blocks of volatile and non-volatile storage areas.
1055 This code finds variable in storage blocks of volatile and non-volatile storage areas.
1056 If VariableName is an empty string, then we just return the first
1057 qualified variable without comparing VariableName and VendorGuid.
1058 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
1059 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
1060 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
1062 @param[in] VariableName Name of the variable to be found.
1063 @param[in] VendorGuid Vendor GUID to be found.
1064 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
1065 including the range searched and the target position.
1066 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
1067 base of volatile variable storage area, base of
1068 NV variable storage area, and a lock.
1069 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
1070 check at runtime when searching variable.
1072 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
1074 @retval EFI_SUCCESS Variable successfully found.
1075 @retval EFI_NOT_FOUND Variable not found
1080 IN CHAR16
*VariableName
,
1081 IN EFI_GUID
*VendorGuid
,
1082 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
1083 IN VARIABLE_GLOBAL
*Global
,
1084 IN BOOLEAN IgnoreRtCheck
1088 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1089 VARIABLE_STORE_TYPE Type
;
1091 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
1092 return EFI_INVALID_PARAMETER
;
1096 // 0: Volatile, 1: HOB, 2: Non-Volatile.
1097 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
1098 // make use of this mapping to implement search algorithm.
1100 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
1101 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
1102 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
1105 // Find the variable by walk through HOB, volatile and non-volatile variable store.
1107 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1108 if (VariableStoreHeader
[Type
] == NULL
) {
1112 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
1113 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
1114 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
1116 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
1117 if (!EFI_ERROR (Status
)) {
1121 return EFI_NOT_FOUND
;
1125 Get index from supported language codes according to language string.
1127 This code is used to get corresponding index in supported language codes. It can handle
1128 RFC4646 and ISO639 language tags.
1129 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
1130 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
1133 SupportedLang = "engfraengfra"
1135 Iso639Language = TRUE
1136 The return value is "0".
1138 SupportedLang = "en;fr;en-US;fr-FR"
1140 Iso639Language = FALSE
1141 The return value is "3".
1143 @param SupportedLang Platform supported language codes.
1144 @param Lang Configured language.
1145 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1147 @retval The index of language in the language codes.
1151 GetIndexFromSupportedLangCodes(
1152 IN CHAR8
*SupportedLang
,
1154 IN BOOLEAN Iso639Language
1158 UINTN CompareLength
;
1159 UINTN LanguageLength
;
1161 if (Iso639Language
) {
1162 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1163 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
1164 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
1166 // Successfully find the index of Lang string in SupportedLang string.
1168 Index
= Index
/ CompareLength
;
1176 // Compare RFC4646 language code
1179 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
1181 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
1183 // Skip ';' characters in SupportedLang
1185 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
1187 // Determine the length of the next language code in SupportedLang
1189 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
1191 if ((CompareLength
== LanguageLength
) &&
1192 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
1194 // Successfully find the index of Lang string in SupportedLang string.
1205 Get language string from supported language codes according to index.
1207 This code is used to get corresponding language strings in supported language codes. It can handle
1208 RFC4646 and ISO639 language tags.
1209 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
1210 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
1213 SupportedLang = "engfraengfra"
1215 Iso639Language = TRUE
1216 The return value is "fra".
1218 SupportedLang = "en;fr;en-US;fr-FR"
1220 Iso639Language = FALSE
1221 The return value is "fr".
1223 @param SupportedLang Platform supported language codes.
1224 @param Index The index in supported language codes.
1225 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1227 @retval The language string in the language codes.
1231 GetLangFromSupportedLangCodes (
1232 IN CHAR8
*SupportedLang
,
1234 IN BOOLEAN Iso639Language
1238 UINTN CompareLength
;
1242 Supported
= SupportedLang
;
1243 if (Iso639Language
) {
1245 // According to the index of Lang string in SupportedLang string to get the language.
1246 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1247 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1249 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1250 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1251 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1256 // Take semicolon as delimitation, sequentially traverse supported language codes.
1258 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1261 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1263 // Have completed the traverse, but not find corrsponding string.
1264 // This case is not allowed to happen.
1269 if (SubIndex
== Index
) {
1271 // According to the index of Lang string in SupportedLang string to get the language.
1272 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1273 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1275 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1276 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1281 // Skip ';' characters in Supported
1283 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1289 Returns a pointer to an allocated buffer that contains the best matching language
1290 from a set of supported languages.
1292 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1293 code types may not be mixed in a single call to this function. This function
1294 supports a variable argument list that allows the caller to pass in a prioritized
1295 list of language codes to test against all the language codes in SupportedLanguages.
1297 If SupportedLanguages is NULL, then ASSERT().
1299 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1300 contains a set of language codes in the format
1301 specified by Iso639Language.
1302 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1303 in ISO 639-2 format. If FALSE, then all language
1304 codes are assumed to be in RFC 4646 language format
1305 @param[in] ... A variable argument list that contains pointers to
1306 Null-terminated ASCII strings that contain one or more
1307 language codes in the format specified by Iso639Language.
1308 The first language code from each of these language
1309 code lists is used to determine if it is an exact or
1310 close match to any of the language codes in
1311 SupportedLanguages. Close matches only apply to RFC 4646
1312 language codes, and the matching algorithm from RFC 4647
1313 is used to determine if a close match is present. If
1314 an exact or close match is found, then the matching
1315 language code from SupportedLanguages is returned. If
1316 no matches are found, then the next variable argument
1317 parameter is evaluated. The variable argument list
1318 is terminated by a NULL.
1320 @retval NULL The best matching language could not be found in SupportedLanguages.
1321 @retval NULL There are not enough resources available to return the best matching
1323 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1324 language in SupportedLanguages.
1329 VariableGetBestLanguage (
1330 IN CONST CHAR8
*SupportedLanguages
,
1331 IN BOOLEAN Iso639Language
,
1337 UINTN CompareLength
;
1338 UINTN LanguageLength
;
1339 CONST CHAR8
*Supported
;
1342 if (SupportedLanguages
== NULL
) {
1346 VA_START (Args
, Iso639Language
);
1347 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1349 // Default to ISO 639-2 mode
1352 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1355 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1357 if (!Iso639Language
) {
1358 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1362 // Trim back the length of Language used until it is empty
1364 while (LanguageLength
> 0) {
1366 // Loop through all language codes in SupportedLanguages
1368 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1370 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1372 if (!Iso639Language
) {
1374 // Skip ';' characters in Supported
1376 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1378 // Determine the length of the next language code in Supported
1380 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1382 // If Language is longer than the Supported, then skip to the next language
1384 if (LanguageLength
> CompareLength
) {
1389 // See if the first LanguageLength characters in Supported match Language
1391 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1394 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1395 Buffer
[CompareLength
] = '\0';
1396 return CopyMem (Buffer
, Supported
, CompareLength
);
1400 if (Iso639Language
) {
1402 // If ISO 639 mode, then each language can only be tested once
1407 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1409 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1416 // No matches were found
1422 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1424 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1426 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1427 and are read-only. Therefore, in variable driver, only store the original value for other use.
1429 @param[in] VariableName Name of variable.
1431 @param[in] Data Variable data.
1433 @param[in] DataSize Size of data. 0 means delete.
1437 AutoUpdateLangVariable (
1438 IN CHAR16
*VariableName
,
1444 CHAR8
*BestPlatformLang
;
1448 VARIABLE_POINTER_TRACK Variable
;
1449 BOOLEAN SetLanguageCodes
;
1452 // Don't do updates for delete operation
1454 if (DataSize
== 0) {
1458 SetLanguageCodes
= FALSE
;
1460 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1462 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1468 SetLanguageCodes
= TRUE
;
1471 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1472 // Therefore, in variable driver, only store the original value for other use.
1474 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1475 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1477 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1478 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1481 // PlatformLang holds a single language from PlatformLangCodes,
1482 // so the size of PlatformLangCodes is enough for the PlatformLang.
1484 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1485 FreePool (mVariableModuleGlobal
->PlatformLang
);
1487 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1488 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1490 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1492 // LangCodes is a volatile variable, so it can not be updated at runtime.
1498 SetLanguageCodes
= TRUE
;
1501 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1502 // Therefore, in variable driver, only store the original value for other use.
1504 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1505 FreePool (mVariableModuleGlobal
->LangCodes
);
1507 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1508 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1511 if (SetLanguageCodes
1512 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1513 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1515 // Update Lang if PlatformLang is already set
1516 // Update PlatformLang if Lang is already set
1518 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1519 if (!EFI_ERROR (Status
)) {
1523 VariableName
= L
"PlatformLang";
1524 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1525 DataSize
= Variable
.CurrPtr
->DataSize
;
1527 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1528 if (!EFI_ERROR (Status
)) {
1530 // Update PlatformLang
1532 VariableName
= L
"Lang";
1533 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1534 DataSize
= Variable
.CurrPtr
->DataSize
;
1537 // Neither PlatformLang nor Lang is set, directly return
1545 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1547 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1549 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1551 // Update Lang when PlatformLangCodes/LangCodes were set.
1553 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1555 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1557 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1558 if (BestPlatformLang
!= NULL
) {
1560 // Get the corresponding index in language codes.
1562 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1565 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1567 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1570 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1572 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1574 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1575 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, 0, 0, &Variable
, NULL
);
1577 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1579 ASSERT_EFI_ERROR(Status
);
1583 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1585 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1587 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1589 // When setting Lang, firstly get most matched language string from supported language codes.
1591 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1592 if (BestLang
!= NULL
) {
1594 // Get the corresponding index in language codes.
1596 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1599 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1601 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1604 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1606 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1608 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1609 AsciiStrSize (BestPlatformLang
), Attributes
, 0, 0, &Variable
, NULL
);
1611 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1612 ASSERT_EFI_ERROR (Status
);
1619 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
1620 index of associated public key is needed.
1622 @param[in] VariableName Name of variable.
1623 @param[in] VendorGuid Guid of variable.
1624 @param[in] Data Variable data.
1625 @param[in] DataSize Size of data. 0 means delete.
1626 @param[in] Attributes Attributes of the variable.
1627 @param[in] KeyIndex Index of associated public key.
1628 @param[in] MonotonicCount Value of associated monotonic count.
1629 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.
1630 @param[in] TimeStamp Value of associated TimeStamp.
1632 @retval EFI_SUCCESS The update operation is success.
1633 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1638 IN CHAR16
*VariableName
,
1639 IN EFI_GUID
*VendorGuid
,
1642 IN UINT32 Attributes OPTIONAL
,
1643 IN UINT32 KeyIndex OPTIONAL
,
1644 IN UINT64 MonotonicCount OPTIONAL
,
1645 IN OUT VARIABLE_POINTER_TRACK
*CacheVariable
,
1646 IN EFI_TIME
*TimeStamp OPTIONAL
1650 VARIABLE_HEADER
*NextVariable
;
1653 UINTN NonVolatileVarableStoreSize
;
1654 UINTN VarNameOffset
;
1655 UINTN VarDataOffset
;
1659 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1661 VARIABLE_POINTER_TRACK
*Variable
;
1662 VARIABLE_POINTER_TRACK NvVariable
;
1663 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1668 if (mVariableModuleGlobal
->FvbInstance
== NULL
) {
1670 // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
1672 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1674 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1676 return EFI_NOT_AVAILABLE_YET
;
1677 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1679 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1680 // The authenticated variable perhaps is not initialized, just return here.
1682 return EFI_NOT_AVAILABLE_YET
;
1686 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1687 Variable
= CacheVariable
;
1690 // Update/Delete existing NV variable.
1691 // CacheVariable points to the variable in the memory copy of Flash area
1692 // Now let Variable points to the same variable in Flash area.
1694 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1695 Variable
= &NvVariable
;
1696 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1697 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1698 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1699 if (CacheVariable
->InDeletedTransitionPtr
!= NULL
) {
1700 Variable
->InDeletedTransitionPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->InDeletedTransitionPtr
- (UINTN
)CacheVariable
->StartPtr
));
1702 Variable
->InDeletedTransitionPtr
= NULL
;
1704 Variable
->Volatile
= FALSE
;
1707 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1710 // Tricky part: Use scratch data area at the end of volatile variable store
1711 // as a temporary storage.
1713 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1714 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1717 if (Variable
->CurrPtr
!= NULL
) {
1719 // Update/Delete existing variable.
1723 // If AtRuntime and the variable is Volatile and Runtime Access,
1724 // the volatile is ReadOnly, and SetVariable should be aborted and
1725 // return EFI_WRITE_PROTECTED.
1727 if (Variable
->Volatile
) {
1728 Status
= EFI_WRITE_PROTECTED
;
1732 // Only variable that have NV attributes can be updated/deleted in Runtime.
1734 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1735 Status
= EFI_INVALID_PARAMETER
;
1740 // Only variable that have RT attributes can be updated/deleted in Runtime.
1742 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) {
1743 Status
= EFI_INVALID_PARAMETER
;
1749 // Setting a data variable with no access, or zero DataSize attributes
1750 // causes it to be deleted.
1751 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
1752 // not delete the variable.
1754 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0))|| ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
1755 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
1757 // Both ADDED and IN_DELETED_TRANSITION variable are present,
1758 // set IN_DELETED_TRANSITION one to DELETED state first.
1760 State
= Variable
->InDeletedTransitionPtr
->State
;
1761 State
&= VAR_DELETED
;
1762 Status
= UpdateVariableStore (
1763 &mVariableModuleGlobal
->VariableGlobal
,
1767 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
1771 if (!EFI_ERROR (Status
)) {
1772 if (!Variable
->Volatile
) {
1773 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
1774 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
1781 State
= Variable
->CurrPtr
->State
;
1782 State
&= VAR_DELETED
;
1784 Status
= UpdateVariableStore (
1785 &mVariableModuleGlobal
->VariableGlobal
,
1789 (UINTN
) &Variable
->CurrPtr
->State
,
1793 if (!EFI_ERROR (Status
)) {
1794 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1795 if (!Variable
->Volatile
) {
1796 CacheVariable
->CurrPtr
->State
= State
;
1797 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1803 // If the variable is marked valid, and the same data has been passed in,
1804 // then return to the caller immediately.
1806 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1807 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0) &&
1808 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) &&
1809 (TimeStamp
== NULL
)) {
1811 // Variable content unchanged and no need to update timestamp, just return.
1813 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1814 Status
= EFI_SUCCESS
;
1816 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1817 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1820 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
1822 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
1824 // Cache the previous variable data into StorageArea.
1826 DataOffset
= sizeof (VARIABLE_HEADER
) + Variable
->CurrPtr
->NameSize
+ GET_PAD_SIZE (Variable
->CurrPtr
->NameSize
);
1827 CopyMem (mStorageArea
, (UINT8
*)((UINTN
) Variable
->CurrPtr
+ DataOffset
), Variable
->CurrPtr
->DataSize
);
1830 // Set Max Common Variable Data Size as default MaxDataSize
1832 MaxDataSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
) - StrSize (VariableName
) - GET_PAD_SIZE (StrSize (VariableName
));
1835 if ((CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
1836 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) ||
1837 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0))) {
1840 // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of
1841 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
1843 Status
= AppendSignatureList (
1845 Variable
->CurrPtr
->DataSize
,
1846 MaxDataSize
- Variable
->CurrPtr
->DataSize
,
1851 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1853 // Signture List is too long, Failed to Append
1855 Status
= EFI_INVALID_PARAMETER
;
1859 if (BufSize
== Variable
->CurrPtr
->DataSize
) {
1860 if ((TimeStamp
== NULL
) || CompareTimeStamp (TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
1862 // New EFI_SIGNATURE_DATA is not found and timestamp is not later
1863 // than current timestamp, return EFI_SUCCESS directly.
1865 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1866 Status
= EFI_SUCCESS
;
1872 // For other Variables, append the new data to the end of previous data.
1873 // Max Harware error record variable data size is different from common variable
1875 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1876 MaxDataSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
) - StrSize (VariableName
) - GET_PAD_SIZE (StrSize (VariableName
));
1879 if (Variable
->CurrPtr
->DataSize
+ DataSize
> MaxDataSize
) {
1881 // Exsiting data + Appended data exceed maximum variable size limitation
1883 Status
= EFI_INVALID_PARAMETER
;
1886 CopyMem ((UINT8
*)((UINTN
) mStorageArea
+ Variable
->CurrPtr
->DataSize
), Data
, DataSize
);
1887 BufSize
= Variable
->CurrPtr
->DataSize
+ DataSize
;
1891 // Override Data and DataSize which are used for combined data area including previous and new data.
1893 Data
= mStorageArea
;
1898 // Mark the old variable as in delete transition.
1900 State
= Variable
->CurrPtr
->State
;
1901 State
&= VAR_IN_DELETED_TRANSITION
;
1903 Status
= UpdateVariableStore (
1904 &mVariableModuleGlobal
->VariableGlobal
,
1908 (UINTN
) &Variable
->CurrPtr
->State
,
1912 if (EFI_ERROR (Status
)) {
1915 if (!Variable
->Volatile
) {
1916 CacheVariable
->CurrPtr
->State
= State
;
1921 // Not found existing variable. Create a new variable.
1924 if ((DataSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
1925 Status
= EFI_SUCCESS
;
1930 // Make sure we are trying to create a new variable.
1931 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1933 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1934 Status
= EFI_NOT_FOUND
;
1939 // Only variable have NV|RT attribute can be created in Runtime.
1942 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1943 Status
= EFI_INVALID_PARAMETER
;
1949 // Function part - create a new variable and copy the data.
1950 // Both update a variable and create a variable will come here.
1952 SetMem (NextVariable
, ScratchSize
, 0xff);
1954 NextVariable
->StartId
= VARIABLE_DATA
;
1956 // NextVariable->State = VAR_ADDED;
1958 NextVariable
->Reserved
= 0;
1959 NextVariable
->PubKeyIndex
= KeyIndex
;
1960 NextVariable
->MonotonicCount
= MonotonicCount
;
1961 ZeroMem (&NextVariable
->TimeStamp
, sizeof (EFI_TIME
));
1963 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1964 (TimeStamp
!= NULL
)) {
1965 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
1966 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1969 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
1970 // when the new TimeStamp value is later than the current timestamp associated
1971 // with the variable, we need associate the new timestamp with the updated value.
1973 if (Variable
->CurrPtr
!= NULL
) {
1974 if (CompareTimeStamp (&Variable
->CurrPtr
->TimeStamp
, TimeStamp
)) {
1975 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1982 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
1983 // Attributes bitmask parameter of a GetVariable() call.
1985 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
1987 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1988 VarNameSize
= StrSize (VariableName
);
1990 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1994 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1996 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
2000 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
2002 // There will be pad bytes after Data, the NextVariable->NameSize and
2003 // NextVariable->DataSize should not include pad size so that variable
2004 // service can get actual size in GetVariable.
2006 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
2007 NextVariable
->DataSize
= (UINT32
)DataSize
;
2010 // The actual size of the variable that stores in storage should
2011 // include pad size.
2013 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
2014 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2016 // Create a nonvolatile variable.
2019 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
2020 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2021 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
2022 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
2023 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
2025 Status
= EFI_OUT_OF_RESOURCES
;
2029 // Perform garbage collection & reclaim operation.
2032 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2033 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2039 if (EFI_ERROR (Status
)) {
2043 // If still no enough space, return out of resources.
2045 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2046 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
2047 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
2048 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
2049 Status
= EFI_OUT_OF_RESOURCES
;
2052 if (Variable
->CurrPtr
!= NULL
) {
2053 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2054 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2059 // 1. Write variable header
2060 // 2. Set variable state to header valid
2061 // 3. Write variable data
2062 // 4. Set variable state to valid
2067 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
2068 Status
= UpdateVariableStore (
2069 &mVariableModuleGlobal
->VariableGlobal
,
2073 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2074 sizeof (VARIABLE_HEADER
),
2075 (UINT8
*) NextVariable
2078 if (EFI_ERROR (Status
)) {
2085 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
2086 Status
= UpdateVariableStore (
2087 &mVariableModuleGlobal
->VariableGlobal
,
2091 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2093 &NextVariable
->State
2096 if (EFI_ERROR (Status
)) {
2102 Status
= UpdateVariableStore (
2103 &mVariableModuleGlobal
->VariableGlobal
,
2107 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
2108 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
2109 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
2112 if (EFI_ERROR (Status
)) {
2118 NextVariable
->State
= VAR_ADDED
;
2119 Status
= UpdateVariableStore (
2120 &mVariableModuleGlobal
->VariableGlobal
,
2124 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2126 &NextVariable
->State
2129 if (EFI_ERROR (Status
)) {
2133 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2135 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2136 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2138 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2141 // update the memory copy of Flash region.
2143 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
2146 // Create a volatile variable.
2150 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2151 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
2153 // Perform garbage collection & reclaim operation.
2156 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
2157 &mVariableModuleGlobal
->VolatileLastVariableOffset
,
2163 if (EFI_ERROR (Status
)) {
2167 // If still no enough space, return out of resources.
2169 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2170 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
2172 Status
= EFI_OUT_OF_RESOURCES
;
2175 if (Variable
->CurrPtr
!= NULL
) {
2176 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2177 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2181 NextVariable
->State
= VAR_ADDED
;
2182 Status
= UpdateVariableStore (
2183 &mVariableModuleGlobal
->VariableGlobal
,
2187 mVariableModuleGlobal
->VolatileLastVariableOffset
,
2189 (UINT8
*) NextVariable
2192 if (EFI_ERROR (Status
)) {
2196 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2200 // Mark the old variable as deleted.
2202 if (!EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
2203 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2205 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
2206 // set IN_DELETED_TRANSITION one to DELETED state first.
2208 State
= Variable
->InDeletedTransitionPtr
->State
;
2209 State
&= VAR_DELETED
;
2210 Status
= UpdateVariableStore (
2211 &mVariableModuleGlobal
->VariableGlobal
,
2215 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2219 if (!EFI_ERROR (Status
)) {
2220 if (!Variable
->Volatile
) {
2221 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2222 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2229 State
= Variable
->CurrPtr
->State
;
2230 State
&= VAR_DELETED
;
2232 Status
= UpdateVariableStore (
2233 &mVariableModuleGlobal
->VariableGlobal
,
2237 (UINTN
) &Variable
->CurrPtr
->State
,
2241 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
2242 CacheVariable
->CurrPtr
->State
= State
;
2246 if (!EFI_ERROR (Status
)) {
2247 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2249 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2258 Check if a Unicode character is a hexadecimal character.
2260 This function checks if a Unicode character is a
2261 hexadecimal character. The valid hexadecimal character is
2262 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
2265 @param Char The character to check against.
2267 @retval TRUE If the Char is a hexadecmial character.
2268 @retval FALSE If the Char is not a hexadecmial character.
2273 IsHexaDecimalDigitCharacter (
2277 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
2282 This code checks if variable is hardware error record variable or not.
2284 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
2285 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
2287 @param VariableName Pointer to variable name.
2288 @param VendorGuid Variable Vendor Guid.
2290 @retval TRUE Variable is hardware error record variable.
2291 @retval FALSE Variable is not hardware error record variable.
2296 IsHwErrRecVariable (
2297 IN CHAR16
*VariableName
,
2298 IN EFI_GUID
*VendorGuid
2301 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
2302 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
2303 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
2304 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
2305 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
2306 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
2307 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
2315 This code checks if variable should be treated as read-only variable.
2317 @param[in] VariableName Name of the Variable.
2318 @param[in] VendorGuid GUID of the Variable.
2320 @retval TRUE This variable is read-only variable.
2321 @retval FALSE This variable is NOT read-only variable.
2325 IsReadOnlyVariable (
2326 IN CHAR16
*VariableName
,
2327 IN EFI_GUID
*VendorGuid
2330 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)) {
2331 if ((StrCmp (VariableName
, EFI_SETUP_MODE_NAME
) == 0) ||
2332 (StrCmp (VariableName
, EFI_SIGNATURE_SUPPORT_NAME
) == 0) ||
2333 (StrCmp (VariableName
, EFI_SECURE_BOOT_MODE_NAME
) == 0)) {
2343 This code finds variable in storage blocks (Volatile or Non-Volatile).
2345 Caution: This function may receive untrusted input.
2346 This function may be invoked in SMM mode, and datasize is external input.
2347 This function will do basic validation, before parse the data.
2349 @param VariableName Name of Variable to be found.
2350 @param VendorGuid Variable vendor GUID.
2351 @param Attributes Attribute value of the variable found.
2352 @param DataSize Size of Data found. If size is less than the
2353 data, this value contains the required size.
2354 @param Data Data pointer.
2356 @return EFI_INVALID_PARAMETER Invalid parameter.
2357 @return EFI_SUCCESS Find the specified variable.
2358 @return EFI_NOT_FOUND Not found.
2359 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2364 VariableServiceGetVariable (
2365 IN CHAR16
*VariableName
,
2366 IN EFI_GUID
*VendorGuid
,
2367 OUT UINT32
*Attributes OPTIONAL
,
2368 IN OUT UINTN
*DataSize
,
2373 VARIABLE_POINTER_TRACK Variable
;
2376 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2377 return EFI_INVALID_PARAMETER
;
2380 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2382 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2383 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2390 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2391 ASSERT (VarDataSize
!= 0);
2393 if (*DataSize
>= VarDataSize
) {
2395 Status
= EFI_INVALID_PARAMETER
;
2399 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2400 if (Attributes
!= NULL
) {
2401 *Attributes
= Variable
.CurrPtr
->Attributes
;
2404 *DataSize
= VarDataSize
;
2405 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2407 Status
= EFI_SUCCESS
;
2410 *DataSize
= VarDataSize
;
2411 Status
= EFI_BUFFER_TOO_SMALL
;
2416 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2424 This code Finds the Next available variable.
2426 Caution: This function may receive untrusted input.
2427 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2429 @param VariableNameSize Size of the variable name.
2430 @param VariableName Pointer to variable name.
2431 @param VendorGuid Variable Vendor Guid.
2433 @return EFI_INVALID_PARAMETER Invalid parameter.
2434 @return EFI_SUCCESS Find the specified variable.
2435 @return EFI_NOT_FOUND Not found.
2436 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2441 VariableServiceGetNextVariableName (
2442 IN OUT UINTN
*VariableNameSize
,
2443 IN OUT CHAR16
*VariableName
,
2444 IN OUT EFI_GUID
*VendorGuid
2447 VARIABLE_STORE_TYPE Type
;
2448 VARIABLE_POINTER_TRACK Variable
;
2449 VARIABLE_POINTER_TRACK VariableInHob
;
2450 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2453 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2455 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2456 return EFI_INVALID_PARAMETER
;
2459 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2461 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2462 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2466 if (VariableName
[0] != 0) {
2468 // If variable name is not NULL, get next variable.
2470 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2474 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2475 // The index and attributes mapping must be kept in this order as FindVariable
2476 // makes use of this mapping to implement search algorithm.
2478 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2479 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2480 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2484 // Switch from Volatile to HOB, to Non-Volatile.
2486 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
2487 (Variable
.CurrPtr
== NULL
) ||
2488 !IsValidVariableHeader (Variable
.CurrPtr
)
2491 // Find current storage index
2493 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2494 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2498 ASSERT (Type
< VariableStoreTypeMax
);
2500 // Switch to next storage
2502 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2503 if (VariableStoreHeader
[Type
] != NULL
) {
2508 // Capture the case that
2509 // 1. current storage is the last one, or
2510 // 2. no further storage
2512 if (Type
== VariableStoreTypeMax
) {
2513 Status
= EFI_NOT_FOUND
;
2516 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2517 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2518 Variable
.CurrPtr
= Variable
.StartPtr
;
2522 // Variable is found
2524 if (Variable
.CurrPtr
->State
== VAR_ADDED
|| Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2525 if (!AtRuntime () || ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
2526 if (Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2528 // If it is a IN_DELETED_TRANSITION variable,
2529 // and there is also a same ADDED one at the same time,
2532 VariablePtrTrack
.StartPtr
= Variable
.StartPtr
;
2533 VariablePtrTrack
.EndPtr
= Variable
.EndPtr
;
2534 Status
= FindVariableEx (
2535 GetVariableNamePtr (Variable
.CurrPtr
),
2536 &Variable
.CurrPtr
->VendorGuid
,
2540 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
== VAR_ADDED
) {
2541 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2547 // Don't return NV variable when HOB overrides it
2549 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2550 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2552 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2553 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2554 Status
= FindVariableEx (
2555 GetVariableNamePtr (Variable
.CurrPtr
),
2556 &Variable
.CurrPtr
->VendorGuid
,
2560 if (!EFI_ERROR (Status
)) {
2561 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2566 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2567 ASSERT (VarNameSize
!= 0);
2569 if (VarNameSize
<= *VariableNameSize
) {
2570 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2571 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2572 Status
= EFI_SUCCESS
;
2574 Status
= EFI_BUFFER_TOO_SMALL
;
2577 *VariableNameSize
= VarNameSize
;
2582 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2586 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2592 This code sets variable in storage blocks (Volatile or Non-Volatile).
2594 Caution: This function may receive untrusted input.
2595 This function may be invoked in SMM mode, and datasize and data are external input.
2596 This function will do basic validation, before parse the data.
2597 This function will parse the authentication carefully to avoid security issues, like
2598 buffer overflow, integer overflow.
2599 This function will check attribute carefully to avoid authentication bypass.
2601 @param VariableName Name of Variable to be found.
2602 @param VendorGuid Variable vendor GUID.
2603 @param Attributes Attribute value of the variable found
2604 @param DataSize Size of Data found. If size is less than the
2605 data, this value contains the required size.
2606 @param Data Data pointer.
2608 @return EFI_INVALID_PARAMETER Invalid parameter.
2609 @return EFI_SUCCESS Set successfully.
2610 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2611 @return EFI_NOT_FOUND Not found.
2612 @return EFI_WRITE_PROTECTED Variable is read-only.
2617 VariableServiceSetVariable (
2618 IN CHAR16
*VariableName
,
2619 IN EFI_GUID
*VendorGuid
,
2620 IN UINT32 Attributes
,
2625 VARIABLE_POINTER_TRACK Variable
;
2627 VARIABLE_HEADER
*NextVariable
;
2628 EFI_PHYSICAL_ADDRESS Point
;
2632 // Check input parameters.
2634 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2635 return EFI_INVALID_PARAMETER
;
2638 if (IsReadOnlyVariable (VariableName
, VendorGuid
)) {
2639 return EFI_WRITE_PROTECTED
;
2642 if (DataSize
!= 0 && Data
== NULL
) {
2643 return EFI_INVALID_PARAMETER
;
2647 // Check for reserverd bit in variable attribute.
2649 if ((Attributes
& (~EFI_VARIABLE_ATTRIBUTES_MASK
)) != 0) {
2650 return EFI_INVALID_PARAMETER
;
2654 // Make sure if runtime bit is set, boot service bit is set also.
2656 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2657 return EFI_INVALID_PARAMETER
;
2661 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
2662 // cannot be set both.
2664 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
2665 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
2666 return EFI_INVALID_PARAMETER
;
2669 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2670 if (DataSize
< AUTHINFO_SIZE
) {
2672 // Try to write Authenticated Variable without AuthInfo.
2674 return EFI_SECURITY_VIOLATION
;
2676 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2677 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
2679 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2681 if (DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
||
2682 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
)) ||
2683 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) {
2684 return EFI_SECURITY_VIOLATION
;
2686 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
2688 PayloadSize
= DataSize
;
2691 if ((UINTN
)(~0) - PayloadSize
< StrSize(VariableName
)){
2693 // Prevent whole variable size overflow
2695 return EFI_INVALID_PARAMETER
;
2699 // The size of the VariableName, including the Unicode Null in bytes plus
2700 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2701 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2703 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2704 if (StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
)) {
2705 return EFI_INVALID_PARAMETER
;
2707 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2708 return EFI_INVALID_PARAMETER
;
2712 // The size of the VariableName, including the Unicode Null in bytes plus
2713 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2715 if (StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
)) {
2716 return EFI_INVALID_PARAMETER
;
2722 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence
2723 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.
2725 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, L
"HwErrRecSupport") == 0)) {
2726 return EFI_WRITE_PROTECTED
;
2730 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2733 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2735 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2736 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2738 // Parse non-volatile variable data and get last variable offset.
2740 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2741 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2742 && IsValidVariableHeader (NextVariable
)) {
2743 NextVariable
= GetNextVariablePtr (NextVariable
);
2745 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2749 // Check whether the input variable is already existed.
2751 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2752 if (!EFI_ERROR (Status
)) {
2753 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2754 return EFI_WRITE_PROTECTED
;
2759 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2761 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2763 // Process PK, KEK, Sigdb seperately.
2765 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
2766 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, TRUE
);
2767 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
2768 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2769 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
2770 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) {
2771 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2772 if (EFI_ERROR (Status
)) {
2773 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2776 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2779 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2780 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2787 This code returns information about the EFI variables.
2789 Caution: This function may receive untrusted input.
2790 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2792 @param Attributes Attributes bitmask to specify the type of variables
2793 on which to return information.
2794 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2795 for the EFI variables associated with the attributes specified.
2796 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2797 for EFI variables associated with the attributes specified.
2798 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2799 associated with the attributes specified.
2801 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2802 @return EFI_SUCCESS Query successfully.
2803 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2808 VariableServiceQueryVariableInfo (
2809 IN UINT32 Attributes
,
2810 OUT UINT64
*MaximumVariableStorageSize
,
2811 OUT UINT64
*RemainingVariableStorageSize
,
2812 OUT UINT64
*MaximumVariableSize
2815 VARIABLE_HEADER
*Variable
;
2816 VARIABLE_HEADER
*NextVariable
;
2817 UINT64 VariableSize
;
2818 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2819 UINT64 CommonVariableTotalSize
;
2820 UINT64 HwErrVariableTotalSize
;
2822 CommonVariableTotalSize
= 0;
2823 HwErrVariableTotalSize
= 0;
2825 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2826 return EFI_INVALID_PARAMETER
;
2829 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2831 // Make sure the Attributes combination is supported by the platform.
2833 return EFI_UNSUPPORTED
;
2834 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2836 // Make sure if runtime bit is set, boot service bit is set also.
2838 return EFI_INVALID_PARAMETER
;
2839 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2841 // Make sure RT Attribute is set if we are in Runtime phase.
2843 return EFI_INVALID_PARAMETER
;
2844 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2846 // Make sure Hw Attribute is set with NV.
2848 return EFI_INVALID_PARAMETER
;
2851 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2853 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2855 // Query is Volatile related.
2857 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2860 // Query is Non-Volatile related.
2862 VariableStoreHeader
= mNvVariableCache
;
2866 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2867 // with the storage size (excluding the storage header size).
2869 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2872 // Harware error record variable needs larger size.
2874 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2875 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2876 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2878 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2879 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2880 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2884 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2886 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2890 // Point to the starting address of the variables.
2892 Variable
= GetStartPointer (VariableStoreHeader
);
2895 // Now walk through the related variable store.
2897 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2898 NextVariable
= GetNextVariablePtr (Variable
);
2899 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2903 // We don't take the state of the variables in mind
2904 // when calculating RemainingVariableStorageSize,
2905 // since the space occupied by variables not marked with
2906 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2908 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2909 HwErrVariableTotalSize
+= VariableSize
;
2911 CommonVariableTotalSize
+= VariableSize
;
2915 // Only care about Variables with State VAR_ADDED, because
2916 // the space not marked as VAR_ADDED is reclaimable now.
2918 if (Variable
->State
== VAR_ADDED
) {
2919 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2920 HwErrVariableTotalSize
+= VariableSize
;
2922 CommonVariableTotalSize
+= VariableSize
;
2928 // Go to the next one.
2930 Variable
= NextVariable
;
2933 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2934 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2936 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2939 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2940 *MaximumVariableSize
= 0;
2941 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2942 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2945 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2951 This function reclaims variable storage if free size is below the threshold.
2953 Caution: This function may be invoked at SMM mode.
2954 Care must be taken to make sure not security issue.
2963 UINTN CommonVariableSpace
;
2964 UINTN RemainingCommonVariableSpace
;
2965 UINTN RemainingHwErrVariableSpace
;
2967 Status
= EFI_SUCCESS
;
2969 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2971 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2973 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2975 // Check if the free area is blow a threshold.
2977 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2978 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2979 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2981 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2982 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2988 ASSERT_EFI_ERROR (Status
);
2993 Flush the HOB variable to flash.
2995 @param[in] VariableName Name of variable has been updated or deleted.
2996 @param[in] VendorGuid Guid of variable has been updated or deleted.
3000 FlushHobVariableToFlash (
3001 IN CHAR16
*VariableName
,
3002 IN EFI_GUID
*VendorGuid
3006 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3007 VARIABLE_HEADER
*Variable
;
3014 // Flush the HOB variable to flash.
3016 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3017 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
3019 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
3021 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
3022 for ( Variable
= GetStartPointer (VariableStoreHeader
)
3023 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
3024 ; Variable
= GetNextVariablePtr (Variable
)
3026 if (Variable
->State
!= VAR_ADDED
) {
3028 // The HOB variable has been set to DELETED state in local.
3032 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
3033 if (VendorGuid
== NULL
|| VariableName
== NULL
||
3034 !CompareGuid (VendorGuid
, &Variable
->VendorGuid
) ||
3035 StrCmp (VariableName
, GetVariableNamePtr (Variable
)) != 0) {
3036 VariableData
= GetVariableDataPtr (Variable
);
3037 Status
= VariableServiceSetVariable (
3038 GetVariableNamePtr (Variable
),
3039 &Variable
->VendorGuid
,
3040 Variable
->Attributes
,
3044 DEBUG ((EFI_D_INFO
, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
), Status
));
3047 // The updated or deleted variable is matched with the HOB variable.
3048 // Don't break here because we will try to set other HOB variables
3049 // since this variable could be set successfully.
3051 Status
= EFI_SUCCESS
;
3053 if (!EFI_ERROR (Status
)) {
3055 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
3056 // set the HOB variable to DELETED state in local.
3058 DEBUG ((EFI_D_INFO
, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
)));
3059 Variable
->State
&= VAR_DELETED
;
3066 // We still have HOB variable(s) not flushed in flash.
3068 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
3071 // All HOB variables have been flushed in flash.
3073 DEBUG ((EFI_D_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
3074 if (!AtRuntime ()) {
3075 FreePool ((VOID
*) VariableStoreHeader
);
3083 Initializes variable write service after FVB was ready.
3085 @retval EFI_SUCCESS Function successfully executed.
3086 @retval Others Fail to initialize the variable service.
3090 VariableWriteServiceInitialize (
3095 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3098 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3100 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
3101 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
3104 // Check if the free area is really free.
3106 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
3107 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
3110 // There must be something wrong in variable store, do reclaim operation.
3113 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3114 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3120 if (EFI_ERROR (Status
)) {
3127 FlushHobVariableToFlash (NULL
, NULL
);
3130 // Authenticated variable initialize.
3132 Status
= AutenticatedVariableServiceInitialize ();
3139 Initializes variable store area for non-volatile and volatile variable.
3141 @retval EFI_SUCCESS Function successfully executed.
3142 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3146 VariableCommonInitialize (
3151 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
3152 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3153 VARIABLE_HEADER
*NextVariable
;
3154 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
3155 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3156 UINT64 VariableStoreLength
;
3159 EFI_HOB_GUID_TYPE
*GuidHob
;
3162 // Allocate runtime memory for variable driver global structure.
3164 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
3165 if (mVariableModuleGlobal
== NULL
) {
3166 return EFI_OUT_OF_RESOURCES
;
3169 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
3172 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
3173 // is stored with common variable in the same NV region. So the platform integrator should
3174 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
3175 // PcdFlashNvStorageVariableSize.
3177 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
3180 // Get HOB variable store.
3182 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
3183 if (GuidHob
!= NULL
) {
3184 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
3185 VariableStoreLength
= (UINT64
) (GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
));
3186 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
3187 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
3188 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
3189 return EFI_OUT_OF_RESOURCES
;
3192 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
3197 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
3199 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
3200 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
3201 if (VolatileVariableStore
== NULL
) {
3202 FreePool (mVariableModuleGlobal
);
3203 return EFI_OUT_OF_RESOURCES
;
3206 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
3209 // Initialize Variable Specific Data.
3211 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
3212 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
3213 mVariableModuleGlobal
->FvbInstance
= NULL
;
3215 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiAuthenticatedVariableGuid
);
3216 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
3217 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
3218 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
3219 VolatileVariableStore
->Reserved
= 0;
3220 VolatileVariableStore
->Reserved1
= 0;
3223 // Get non-volatile variable store.
3226 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
3227 if (TempVariableStoreHeader
== 0) {
3228 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3232 // Check if the Firmware Volume is not corrupted
3234 if ((((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->Signature
!= EFI_FVH_SIGNATURE
) ||
3235 (!CompareGuid (&gEfiSystemNvDataFvGuid
, &((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->FileSystemGuid
))) {
3236 Status
= EFI_VOLUME_CORRUPTED
;
3237 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
3241 VariableStoreBase
= TempVariableStoreHeader
+ \
3242 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
3243 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
3244 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
3246 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3247 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
3248 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
3249 Status
= EFI_VOLUME_CORRUPTED
;
3250 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
3253 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
3256 // The max variable or hardware error variable size should be < variable store size.
3258 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) < VariableStoreLength
);
3261 // Parse non-volatile variable data and get last variable offset.
3263 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
3264 while (IsValidVariableHeader (NextVariable
)) {
3265 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
3266 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3267 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3269 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3272 NextVariable
= GetNextVariablePtr (NextVariable
);
3275 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
3278 // Allocate runtime memory used for a memory copy of the FLASH region.
3279 // Keep the memory and the FLASH in sync as updates occur
3281 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
3282 if (mNvVariableCache
== NULL
) {
3283 Status
= EFI_OUT_OF_RESOURCES
;
3286 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
3287 Status
= EFI_SUCCESS
;
3290 if (EFI_ERROR (Status
)) {
3291 FreePool (mVariableModuleGlobal
);
3292 FreePool (VolatileVariableStore
);
3300 Get the proper fvb handle and/or fvb protocol by the given Flash address.
3302 @param[in] Address The Flash address.
3303 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
3304 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
3308 GetFvbInfoByAddress (
3309 IN EFI_PHYSICAL_ADDRESS Address
,
3310 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
3311 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
3315 EFI_HANDLE
*HandleBuffer
;
3318 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
3319 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
3320 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
3321 EFI_FVB_ATTRIBUTES_2 Attributes
;
3324 // Get all FVB handles.
3326 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
3327 if (EFI_ERROR (Status
)) {
3328 return EFI_NOT_FOUND
;
3332 // Get the FVB to access variable store.
3335 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
3336 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
3337 if (EFI_ERROR (Status
)) {
3338 Status
= EFI_NOT_FOUND
;
3343 // Ensure this FVB protocol supported Write operation.
3345 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
3346 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
3351 // Compare the address and select the right one.
3353 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
3354 if (EFI_ERROR (Status
)) {
3358 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
3359 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
3360 if (FvbHandle
!= NULL
) {
3361 *FvbHandle
= HandleBuffer
[Index
];
3363 if (FvbProtocol
!= NULL
) {
3366 Status
= EFI_SUCCESS
;
3370 FreePool (HandleBuffer
);
3373 Status
= EFI_NOT_FOUND
;