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
;
1652 UINTN ScratchDataSize
;
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
;
1669 if (mVariableModuleGlobal
->FvbInstance
== NULL
) {
1671 // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
1673 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1675 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1677 return EFI_NOT_AVAILABLE_YET
;
1678 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1680 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1681 // The authenticated variable perhaps is not initialized, just return here.
1683 return EFI_NOT_AVAILABLE_YET
;
1687 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1688 Variable
= CacheVariable
;
1691 // Update/Delete existing NV variable.
1692 // CacheVariable points to the variable in the memory copy of Flash area
1693 // Now let Variable points to the same variable in Flash area.
1695 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1696 Variable
= &NvVariable
;
1697 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1698 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1699 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1700 if (CacheVariable
->InDeletedTransitionPtr
!= NULL
) {
1701 Variable
->InDeletedTransitionPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->InDeletedTransitionPtr
- (UINTN
)CacheVariable
->StartPtr
));
1703 Variable
->InDeletedTransitionPtr
= NULL
;
1705 Variable
->Volatile
= FALSE
;
1708 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1711 // Tricky part: Use scratch data area at the end of volatile variable store
1712 // as a temporary storage.
1714 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1715 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1716 ScratchDataSize
= ScratchSize
- sizeof (VARIABLE_HEADER
) - StrSize (VariableName
) - GET_PAD_SIZE (StrSize (VariableName
));
1718 if (Variable
->CurrPtr
!= NULL
) {
1720 // Update/Delete existing variable.
1724 // If AtRuntime and the variable is Volatile and Runtime Access,
1725 // the volatile is ReadOnly, and SetVariable should be aborted and
1726 // return EFI_WRITE_PROTECTED.
1728 if (Variable
->Volatile
) {
1729 Status
= EFI_WRITE_PROTECTED
;
1733 // Only variable that have NV attributes can be updated/deleted in Runtime.
1735 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1736 Status
= EFI_INVALID_PARAMETER
;
1741 // Only variable that have RT attributes can be updated/deleted in Runtime.
1743 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) {
1744 Status
= EFI_INVALID_PARAMETER
;
1750 // Setting a data variable with no access, or zero DataSize attributes
1751 // causes it to be deleted.
1752 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
1753 // not delete the variable.
1755 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0))|| ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
1756 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
1758 // Both ADDED and IN_DELETED_TRANSITION variable are present,
1759 // set IN_DELETED_TRANSITION one to DELETED state first.
1761 State
= Variable
->InDeletedTransitionPtr
->State
;
1762 State
&= VAR_DELETED
;
1763 Status
= UpdateVariableStore (
1764 &mVariableModuleGlobal
->VariableGlobal
,
1768 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
1772 if (!EFI_ERROR (Status
)) {
1773 if (!Variable
->Volatile
) {
1774 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
1775 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
1782 State
= Variable
->CurrPtr
->State
;
1783 State
&= VAR_DELETED
;
1785 Status
= UpdateVariableStore (
1786 &mVariableModuleGlobal
->VariableGlobal
,
1790 (UINTN
) &Variable
->CurrPtr
->State
,
1794 if (!EFI_ERROR (Status
)) {
1795 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1796 if (!Variable
->Volatile
) {
1797 CacheVariable
->CurrPtr
->State
= State
;
1798 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1804 // If the variable is marked valid, and the same data has been passed in,
1805 // then return to the caller immediately.
1807 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1808 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0) &&
1809 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) &&
1810 (TimeStamp
== NULL
)) {
1812 // Variable content unchanged and no need to update timestamp, just return.
1814 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1815 Status
= EFI_SUCCESS
;
1817 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1818 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1821 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
1823 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
1825 // Cache the previous variable data into StorageArea.
1827 DataOffset
= sizeof (VARIABLE_HEADER
) + Variable
->CurrPtr
->NameSize
+ GET_PAD_SIZE (Variable
->CurrPtr
->NameSize
);
1828 CopyMem (mStorageArea
, (UINT8
*)((UINTN
) Variable
->CurrPtr
+ DataOffset
), Variable
->CurrPtr
->DataSize
);
1830 if ((CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
1831 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) ||
1832 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0))) {
1834 // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of
1835 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
1837 BufSize
= AppendSignatureList (mStorageArea
, Variable
->CurrPtr
->DataSize
, Data
, DataSize
);
1838 if (BufSize
== Variable
->CurrPtr
->DataSize
) {
1839 if ((TimeStamp
== NULL
) || CompareTimeStamp (TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
1841 // New EFI_SIGNATURE_DATA is not found and timestamp is not later
1842 // than current timestamp, return EFI_SUCCESS directly.
1844 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1845 Status
= EFI_SUCCESS
;
1851 // For other Variables, append the new data to the end of previous data.
1853 CopyMem ((UINT8
*)((UINTN
) mStorageArea
+ Variable
->CurrPtr
->DataSize
), Data
, DataSize
);
1854 BufSize
= Variable
->CurrPtr
->DataSize
+ DataSize
;
1857 RevBufSize
= MIN (PcdGet32 (PcdMaxVariableSize
), ScratchDataSize
);
1858 if (BufSize
> RevBufSize
) {
1860 // If variable size (previous + current) is bigger than reserved buffer in runtime,
1861 // return EFI_OUT_OF_RESOURCES.
1863 return EFI_OUT_OF_RESOURCES
;
1867 // Override Data and DataSize which are used for combined data area including previous and new data.
1869 Data
= mStorageArea
;
1874 // Mark the old variable as in delete transition.
1876 State
= Variable
->CurrPtr
->State
;
1877 State
&= VAR_IN_DELETED_TRANSITION
;
1879 Status
= UpdateVariableStore (
1880 &mVariableModuleGlobal
->VariableGlobal
,
1884 (UINTN
) &Variable
->CurrPtr
->State
,
1888 if (EFI_ERROR (Status
)) {
1891 if (!Variable
->Volatile
) {
1892 CacheVariable
->CurrPtr
->State
= State
;
1897 // Not found existing variable. Create a new variable.
1900 if ((DataSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
1901 Status
= EFI_SUCCESS
;
1906 // Make sure we are trying to create a new variable.
1907 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1909 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1910 Status
= EFI_NOT_FOUND
;
1915 // Only variable have NV|RT attribute can be created in Runtime.
1918 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1919 Status
= EFI_INVALID_PARAMETER
;
1925 // Function part - create a new variable and copy the data.
1926 // Both update a variable and create a variable will come here.
1928 SetMem (NextVariable
, ScratchSize
, 0xff);
1930 NextVariable
->StartId
= VARIABLE_DATA
;
1932 // NextVariable->State = VAR_ADDED;
1934 NextVariable
->Reserved
= 0;
1935 NextVariable
->PubKeyIndex
= KeyIndex
;
1936 NextVariable
->MonotonicCount
= MonotonicCount
;
1937 ZeroMem (&NextVariable
->TimeStamp
, sizeof (EFI_TIME
));
1939 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1940 (TimeStamp
!= NULL
)) {
1941 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
1942 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1945 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
1946 // when the new TimeStamp value is later than the current timestamp associated
1947 // with the variable, we need associate the new timestamp with the updated value.
1949 if (Variable
->CurrPtr
!= NULL
) {
1950 if (CompareTimeStamp (&Variable
->CurrPtr
->TimeStamp
, TimeStamp
)) {
1951 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1958 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
1959 // Attributes bitmask parameter of a GetVariable() call.
1961 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
1963 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1964 VarNameSize
= StrSize (VariableName
);
1966 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1970 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1972 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1976 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1978 // There will be pad bytes after Data, the NextVariable->NameSize and
1979 // NextVariable->DataSize should not include pad size so that variable
1980 // service can get actual size in GetVariable.
1982 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1983 NextVariable
->DataSize
= (UINT32
)DataSize
;
1986 // The actual size of the variable that stores in storage should
1987 // include pad size.
1989 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1990 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1992 // Create a nonvolatile variable.
1995 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1996 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1997 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1998 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1999 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
2001 Status
= EFI_OUT_OF_RESOURCES
;
2005 // Perform garbage collection & reclaim operation.
2008 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2009 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2015 if (EFI_ERROR (Status
)) {
2019 // If still no enough space, return out of resources.
2021 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2022 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
2023 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
2024 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
2025 Status
= EFI_OUT_OF_RESOURCES
;
2028 if (Variable
->CurrPtr
!= NULL
) {
2029 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2030 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2035 // 1. Write variable header
2036 // 2. Set variable state to header valid
2037 // 3. Write variable data
2038 // 4. Set variable state to valid
2043 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
2044 Status
= UpdateVariableStore (
2045 &mVariableModuleGlobal
->VariableGlobal
,
2049 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2050 sizeof (VARIABLE_HEADER
),
2051 (UINT8
*) NextVariable
2054 if (EFI_ERROR (Status
)) {
2061 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
2062 Status
= UpdateVariableStore (
2063 &mVariableModuleGlobal
->VariableGlobal
,
2067 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2069 &NextVariable
->State
2072 if (EFI_ERROR (Status
)) {
2078 Status
= UpdateVariableStore (
2079 &mVariableModuleGlobal
->VariableGlobal
,
2083 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
2084 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
2085 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
2088 if (EFI_ERROR (Status
)) {
2094 NextVariable
->State
= VAR_ADDED
;
2095 Status
= UpdateVariableStore (
2096 &mVariableModuleGlobal
->VariableGlobal
,
2100 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2102 &NextVariable
->State
2105 if (EFI_ERROR (Status
)) {
2109 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2111 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2112 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2114 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2117 // update the memory copy of Flash region.
2119 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
2122 // Create a volatile variable.
2126 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2127 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
2129 // Perform garbage collection & reclaim operation.
2132 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
2133 &mVariableModuleGlobal
->VolatileLastVariableOffset
,
2139 if (EFI_ERROR (Status
)) {
2143 // If still no enough space, return out of resources.
2145 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2146 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
2148 Status
= EFI_OUT_OF_RESOURCES
;
2151 if (Variable
->CurrPtr
!= NULL
) {
2152 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2153 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2157 NextVariable
->State
= VAR_ADDED
;
2158 Status
= UpdateVariableStore (
2159 &mVariableModuleGlobal
->VariableGlobal
,
2163 mVariableModuleGlobal
->VolatileLastVariableOffset
,
2165 (UINT8
*) NextVariable
2168 if (EFI_ERROR (Status
)) {
2172 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2176 // Mark the old variable as deleted.
2178 if (!EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
2179 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2181 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
2182 // set IN_DELETED_TRANSITION one to DELETED state first.
2184 State
= Variable
->InDeletedTransitionPtr
->State
;
2185 State
&= VAR_DELETED
;
2186 Status
= UpdateVariableStore (
2187 &mVariableModuleGlobal
->VariableGlobal
,
2191 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2195 if (!EFI_ERROR (Status
)) {
2196 if (!Variable
->Volatile
) {
2197 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2198 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2205 State
= Variable
->CurrPtr
->State
;
2206 State
&= VAR_DELETED
;
2208 Status
= UpdateVariableStore (
2209 &mVariableModuleGlobal
->VariableGlobal
,
2213 (UINTN
) &Variable
->CurrPtr
->State
,
2217 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
2218 CacheVariable
->CurrPtr
->State
= State
;
2222 if (!EFI_ERROR (Status
)) {
2223 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2225 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2234 Check if a Unicode character is a hexadecimal character.
2236 This function checks if a Unicode character is a
2237 hexadecimal character. The valid hexadecimal character is
2238 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
2241 @param Char The character to check against.
2243 @retval TRUE If the Char is a hexadecmial character.
2244 @retval FALSE If the Char is not a hexadecmial character.
2249 IsHexaDecimalDigitCharacter (
2253 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
2258 This code checks if variable is hardware error record variable or not.
2260 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
2261 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
2263 @param VariableName Pointer to variable name.
2264 @param VendorGuid Variable Vendor Guid.
2266 @retval TRUE Variable is hardware error record variable.
2267 @retval FALSE Variable is not hardware error record variable.
2272 IsHwErrRecVariable (
2273 IN CHAR16
*VariableName
,
2274 IN EFI_GUID
*VendorGuid
2277 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
2278 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
2279 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
2280 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
2281 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
2282 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
2283 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
2291 This code checks if variable should be treated as read-only variable.
2293 @param[in] VariableName Name of the Variable.
2294 @param[in] VendorGuid GUID of the Variable.
2296 @retval TRUE This variable is read-only variable.
2297 @retval FALSE This variable is NOT read-only variable.
2301 IsReadOnlyVariable (
2302 IN CHAR16
*VariableName
,
2303 IN EFI_GUID
*VendorGuid
2306 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)) {
2307 if ((StrCmp (VariableName
, EFI_SETUP_MODE_NAME
) == 0) ||
2308 (StrCmp (VariableName
, EFI_SIGNATURE_SUPPORT_NAME
) == 0) ||
2309 (StrCmp (VariableName
, EFI_SECURE_BOOT_MODE_NAME
) == 0)) {
2319 This code finds variable in storage blocks (Volatile or Non-Volatile).
2321 Caution: This function may receive untrusted input.
2322 This function may be invoked in SMM mode, and datasize is external input.
2323 This function will do basic validation, before parse the data.
2325 @param VariableName Name of Variable to be found.
2326 @param VendorGuid Variable vendor GUID.
2327 @param Attributes Attribute value of the variable found.
2328 @param DataSize Size of Data found. If size is less than the
2329 data, this value contains the required size.
2330 @param Data Data pointer.
2332 @return EFI_INVALID_PARAMETER Invalid parameter.
2333 @return EFI_SUCCESS Find the specified variable.
2334 @return EFI_NOT_FOUND Not found.
2335 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2340 VariableServiceGetVariable (
2341 IN CHAR16
*VariableName
,
2342 IN EFI_GUID
*VendorGuid
,
2343 OUT UINT32
*Attributes OPTIONAL
,
2344 IN OUT UINTN
*DataSize
,
2349 VARIABLE_POINTER_TRACK Variable
;
2352 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2353 return EFI_INVALID_PARAMETER
;
2356 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2358 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2359 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2366 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2367 ASSERT (VarDataSize
!= 0);
2369 if (*DataSize
>= VarDataSize
) {
2371 Status
= EFI_INVALID_PARAMETER
;
2375 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2376 if (Attributes
!= NULL
) {
2377 *Attributes
= Variable
.CurrPtr
->Attributes
;
2380 *DataSize
= VarDataSize
;
2381 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2383 Status
= EFI_SUCCESS
;
2386 *DataSize
= VarDataSize
;
2387 Status
= EFI_BUFFER_TOO_SMALL
;
2392 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2400 This code Finds the Next available variable.
2402 Caution: This function may receive untrusted input.
2403 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2405 @param VariableNameSize Size of the variable name.
2406 @param VariableName Pointer to variable name.
2407 @param VendorGuid Variable Vendor Guid.
2409 @return EFI_INVALID_PARAMETER Invalid parameter.
2410 @return EFI_SUCCESS Find the specified variable.
2411 @return EFI_NOT_FOUND Not found.
2412 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2417 VariableServiceGetNextVariableName (
2418 IN OUT UINTN
*VariableNameSize
,
2419 IN OUT CHAR16
*VariableName
,
2420 IN OUT EFI_GUID
*VendorGuid
2423 VARIABLE_STORE_TYPE Type
;
2424 VARIABLE_POINTER_TRACK Variable
;
2425 VARIABLE_POINTER_TRACK VariableInHob
;
2426 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2429 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2431 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2432 return EFI_INVALID_PARAMETER
;
2435 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2437 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2438 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2442 if (VariableName
[0] != 0) {
2444 // If variable name is not NULL, get next variable.
2446 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2450 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2451 // The index and attributes mapping must be kept in this order as FindVariable
2452 // makes use of this mapping to implement search algorithm.
2454 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2455 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2456 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2460 // Switch from Volatile to HOB, to Non-Volatile.
2462 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
2463 (Variable
.CurrPtr
== NULL
) ||
2464 !IsValidVariableHeader (Variable
.CurrPtr
)
2467 // Find current storage index
2469 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2470 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2474 ASSERT (Type
< VariableStoreTypeMax
);
2476 // Switch to next storage
2478 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2479 if (VariableStoreHeader
[Type
] != NULL
) {
2484 // Capture the case that
2485 // 1. current storage is the last one, or
2486 // 2. no further storage
2488 if (Type
== VariableStoreTypeMax
) {
2489 Status
= EFI_NOT_FOUND
;
2492 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2493 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2494 Variable
.CurrPtr
= Variable
.StartPtr
;
2498 // Variable is found
2500 if (Variable
.CurrPtr
->State
== VAR_ADDED
|| Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2501 if (!AtRuntime () || ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
2502 if (Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2504 // If it is a IN_DELETED_TRANSITION variable,
2505 // and there is also a same ADDED one at the same time,
2508 VariablePtrTrack
.StartPtr
= Variable
.StartPtr
;
2509 VariablePtrTrack
.EndPtr
= Variable
.EndPtr
;
2510 Status
= FindVariableEx (
2511 GetVariableNamePtr (Variable
.CurrPtr
),
2512 &Variable
.CurrPtr
->VendorGuid
,
2516 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
== VAR_ADDED
) {
2517 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2523 // Don't return NV variable when HOB overrides it
2525 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2526 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2528 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2529 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2530 Status
= FindVariableEx (
2531 GetVariableNamePtr (Variable
.CurrPtr
),
2532 &Variable
.CurrPtr
->VendorGuid
,
2536 if (!EFI_ERROR (Status
)) {
2537 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2542 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2543 ASSERT (VarNameSize
!= 0);
2545 if (VarNameSize
<= *VariableNameSize
) {
2546 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2547 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2548 Status
= EFI_SUCCESS
;
2550 Status
= EFI_BUFFER_TOO_SMALL
;
2553 *VariableNameSize
= VarNameSize
;
2558 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2562 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2568 This code sets variable in storage blocks (Volatile or Non-Volatile).
2570 Caution: This function may receive untrusted input.
2571 This function may be invoked in SMM mode, and datasize and data are external input.
2572 This function will do basic validation, before parse the data.
2573 This function will parse the authentication carefully to avoid security issues, like
2574 buffer overflow, integer overflow.
2575 This function will check attribute carefully to avoid authentication bypass.
2577 @param VariableName Name of Variable to be found.
2578 @param VendorGuid Variable vendor GUID.
2579 @param Attributes Attribute value of the variable found
2580 @param DataSize Size of Data found. If size is less than the
2581 data, this value contains the required size.
2582 @param Data Data pointer.
2584 @return EFI_INVALID_PARAMETER Invalid parameter.
2585 @return EFI_SUCCESS Set successfully.
2586 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2587 @return EFI_NOT_FOUND Not found.
2588 @return EFI_WRITE_PROTECTED Variable is read-only.
2593 VariableServiceSetVariable (
2594 IN CHAR16
*VariableName
,
2595 IN EFI_GUID
*VendorGuid
,
2596 IN UINT32 Attributes
,
2601 VARIABLE_POINTER_TRACK Variable
;
2603 VARIABLE_HEADER
*NextVariable
;
2604 EFI_PHYSICAL_ADDRESS Point
;
2608 // Check input parameters.
2610 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2611 return EFI_INVALID_PARAMETER
;
2614 if (IsReadOnlyVariable (VariableName
, VendorGuid
)) {
2615 return EFI_WRITE_PROTECTED
;
2618 if (DataSize
!= 0 && Data
== NULL
) {
2619 return EFI_INVALID_PARAMETER
;
2623 // Check for reserverd bit in variable attribute.
2625 if ((Attributes
& (~EFI_VARIABLE_ATTRIBUTES_MASK
)) != 0) {
2626 return EFI_INVALID_PARAMETER
;
2630 // Make sure if runtime bit is set, boot service bit is set also.
2632 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2633 return EFI_INVALID_PARAMETER
;
2637 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
2638 // cannot be set both.
2640 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
2641 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
2642 return EFI_INVALID_PARAMETER
;
2645 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2646 if (DataSize
< AUTHINFO_SIZE
) {
2648 // Try to write Authenticated Variable without AuthInfo.
2650 return EFI_SECURITY_VIOLATION
;
2652 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2653 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
2655 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2657 if (DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
||
2658 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
)) ||
2659 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) {
2660 return EFI_SECURITY_VIOLATION
;
2662 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
2664 PayloadSize
= DataSize
;
2668 // The size of the VariableName, including the Unicode Null in bytes plus
2669 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2670 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2672 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2673 if ((PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
2674 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
2675 return EFI_INVALID_PARAMETER
;
2677 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2678 return EFI_INVALID_PARAMETER
;
2682 // The size of the VariableName, including the Unicode Null in bytes plus
2683 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2685 if ((PayloadSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2686 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxVariableSize
))) {
2687 return EFI_INVALID_PARAMETER
;
2693 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence
2694 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.
2696 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, L
"HwErrRecSupport") == 0)) {
2697 return EFI_WRITE_PROTECTED
;
2701 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2704 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2706 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2707 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2709 // Parse non-volatile variable data and get last variable offset.
2711 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2712 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2713 && IsValidVariableHeader (NextVariable
)) {
2714 NextVariable
= GetNextVariablePtr (NextVariable
);
2716 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2720 // Check whether the input variable is already existed.
2722 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2723 if (!EFI_ERROR (Status
)) {
2724 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2725 return EFI_WRITE_PROTECTED
;
2730 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2732 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2734 // Process PK, KEK, Sigdb seperately.
2736 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
2737 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, TRUE
);
2738 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
2739 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2740 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
2741 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) {
2742 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2743 if (EFI_ERROR (Status
)) {
2744 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2747 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2750 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2751 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2758 This code returns information about the EFI variables.
2760 Caution: This function may receive untrusted input.
2761 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2763 @param Attributes Attributes bitmask to specify the type of variables
2764 on which to return information.
2765 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2766 for the EFI variables associated with the attributes specified.
2767 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2768 for EFI variables associated with the attributes specified.
2769 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2770 associated with the attributes specified.
2772 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2773 @return EFI_SUCCESS Query successfully.
2774 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2779 VariableServiceQueryVariableInfo (
2780 IN UINT32 Attributes
,
2781 OUT UINT64
*MaximumVariableStorageSize
,
2782 OUT UINT64
*RemainingVariableStorageSize
,
2783 OUT UINT64
*MaximumVariableSize
2786 VARIABLE_HEADER
*Variable
;
2787 VARIABLE_HEADER
*NextVariable
;
2788 UINT64 VariableSize
;
2789 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2790 UINT64 CommonVariableTotalSize
;
2791 UINT64 HwErrVariableTotalSize
;
2793 CommonVariableTotalSize
= 0;
2794 HwErrVariableTotalSize
= 0;
2796 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2797 return EFI_INVALID_PARAMETER
;
2800 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2802 // Make sure the Attributes combination is supported by the platform.
2804 return EFI_UNSUPPORTED
;
2805 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2807 // Make sure if runtime bit is set, boot service bit is set also.
2809 return EFI_INVALID_PARAMETER
;
2810 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2812 // Make sure RT Attribute is set if we are in Runtime phase.
2814 return EFI_INVALID_PARAMETER
;
2815 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2817 // Make sure Hw Attribute is set with NV.
2819 return EFI_INVALID_PARAMETER
;
2822 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2824 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2826 // Query is Volatile related.
2828 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2831 // Query is Non-Volatile related.
2833 VariableStoreHeader
= mNvVariableCache
;
2837 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2838 // with the storage size (excluding the storage header size).
2840 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2843 // Harware error record variable needs larger size.
2845 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2846 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2847 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2849 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2850 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2851 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2855 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2857 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2861 // Point to the starting address of the variables.
2863 Variable
= GetStartPointer (VariableStoreHeader
);
2866 // Now walk through the related variable store.
2868 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2869 NextVariable
= GetNextVariablePtr (Variable
);
2870 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2874 // We don't take the state of the variables in mind
2875 // when calculating RemainingVariableStorageSize,
2876 // since the space occupied by variables not marked with
2877 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2879 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2880 HwErrVariableTotalSize
+= VariableSize
;
2882 CommonVariableTotalSize
+= VariableSize
;
2886 // Only care about Variables with State VAR_ADDED, because
2887 // the space not marked as VAR_ADDED is reclaimable now.
2889 if (Variable
->State
== VAR_ADDED
) {
2890 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2891 HwErrVariableTotalSize
+= VariableSize
;
2893 CommonVariableTotalSize
+= VariableSize
;
2899 // Go to the next one.
2901 Variable
= NextVariable
;
2904 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2905 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2907 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2910 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2911 *MaximumVariableSize
= 0;
2912 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2913 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2916 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2922 This function reclaims variable storage if free size is below the threshold.
2924 Caution: This function may be invoked at SMM mode.
2925 Care must be taken to make sure not security issue.
2934 UINTN CommonVariableSpace
;
2935 UINTN RemainingCommonVariableSpace
;
2936 UINTN RemainingHwErrVariableSpace
;
2938 Status
= EFI_SUCCESS
;
2940 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2942 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2944 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2946 // Check if the free area is blow a threshold.
2948 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2949 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2950 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2952 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2953 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2959 ASSERT_EFI_ERROR (Status
);
2964 Flush the HOB variable to flash.
2966 @param[in] VariableName Name of variable has been updated or deleted.
2967 @param[in] VendorGuid Guid of variable has been updated or deleted.
2971 FlushHobVariableToFlash (
2972 IN CHAR16
*VariableName
,
2973 IN EFI_GUID
*VendorGuid
2977 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2978 VARIABLE_HEADER
*Variable
;
2985 // Flush the HOB variable to flash.
2987 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2988 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2990 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
2992 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
2993 for ( Variable
= GetStartPointer (VariableStoreHeader
)
2994 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
2995 ; Variable
= GetNextVariablePtr (Variable
)
2997 if (Variable
->State
!= VAR_ADDED
) {
2999 // The HOB variable has been set to DELETED state in local.
3003 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
3004 if (VendorGuid
== NULL
|| VariableName
== NULL
||
3005 !CompareGuid (VendorGuid
, &Variable
->VendorGuid
) ||
3006 StrCmp (VariableName
, GetVariableNamePtr (Variable
)) != 0) {
3007 VariableData
= GetVariableDataPtr (Variable
);
3008 Status
= VariableServiceSetVariable (
3009 GetVariableNamePtr (Variable
),
3010 &Variable
->VendorGuid
,
3011 Variable
->Attributes
,
3015 DEBUG ((EFI_D_INFO
, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
), Status
));
3018 // The updated or deleted variable is matched with the HOB variable.
3019 // Don't break here because we will try to set other HOB variables
3020 // since this variable could be set successfully.
3022 Status
= EFI_SUCCESS
;
3024 if (!EFI_ERROR (Status
)) {
3026 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
3027 // set the HOB variable to DELETED state in local.
3029 DEBUG ((EFI_D_INFO
, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
)));
3030 Variable
->State
&= VAR_DELETED
;
3037 // We still have HOB variable(s) not flushed in flash.
3039 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
3042 // All HOB variables have been flushed in flash.
3044 DEBUG ((EFI_D_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
3045 if (!AtRuntime ()) {
3046 FreePool ((VOID
*) VariableStoreHeader
);
3054 Initializes variable write service after FVB was ready.
3056 @retval EFI_SUCCESS Function successfully executed.
3057 @retval Others Fail to initialize the variable service.
3061 VariableWriteServiceInitialize (
3066 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3069 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3071 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
3072 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
3075 // Check if the free area is really free.
3077 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
3078 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
3081 // There must be something wrong in variable store, do reclaim operation.
3084 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3085 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3091 if (EFI_ERROR (Status
)) {
3098 FlushHobVariableToFlash (NULL
, NULL
);
3101 // Authenticated variable initialize.
3103 Status
= AutenticatedVariableServiceInitialize ();
3110 Initializes variable store area for non-volatile and volatile variable.
3112 @retval EFI_SUCCESS Function successfully executed.
3113 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3117 VariableCommonInitialize (
3122 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
3123 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3124 VARIABLE_HEADER
*NextVariable
;
3125 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
3126 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3127 UINT64 VariableStoreLength
;
3130 EFI_HOB_GUID_TYPE
*GuidHob
;
3133 // Allocate runtime memory for variable driver global structure.
3135 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
3136 if (mVariableModuleGlobal
== NULL
) {
3137 return EFI_OUT_OF_RESOURCES
;
3140 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
3143 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
3144 // is stored with common variable in the same NV region. So the platform integrator should
3145 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
3146 // PcdFlashNvStorageVariableSize.
3148 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
3151 // Get HOB variable store.
3153 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
3154 if (GuidHob
!= NULL
) {
3155 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
3156 VariableStoreLength
= (UINT64
) (GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
));
3157 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
3158 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
3159 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
3160 return EFI_OUT_OF_RESOURCES
;
3163 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
3168 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
3170 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
3171 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
3172 if (VolatileVariableStore
== NULL
) {
3173 FreePool (mVariableModuleGlobal
);
3174 return EFI_OUT_OF_RESOURCES
;
3177 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
3180 // Initialize Variable Specific Data.
3182 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
3183 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
3184 mVariableModuleGlobal
->FvbInstance
= NULL
;
3186 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiAuthenticatedVariableGuid
);
3187 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
3188 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
3189 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
3190 VolatileVariableStore
->Reserved
= 0;
3191 VolatileVariableStore
->Reserved1
= 0;
3194 // Get non-volatile variable store.
3197 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
3198 if (TempVariableStoreHeader
== 0) {
3199 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3203 // Check if the Firmware Volume is not corrupted
3205 if ((((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->Signature
!= EFI_FVH_SIGNATURE
) ||
3206 (!CompareGuid (&gEfiSystemNvDataFvGuid
, &((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->FileSystemGuid
))) {
3207 Status
= EFI_VOLUME_CORRUPTED
;
3208 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
3212 VariableStoreBase
= TempVariableStoreHeader
+ \
3213 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
3214 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
3215 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
3217 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3218 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
3219 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
3220 Status
= EFI_VOLUME_CORRUPTED
;
3221 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
3224 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
3227 // The max variable or hardware error variable size should be < variable store size.
3229 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) < VariableStoreLength
);
3232 // Parse non-volatile variable data and get last variable offset.
3234 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
3235 while (IsValidVariableHeader (NextVariable
)) {
3236 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
3237 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3238 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3240 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3243 NextVariable
= GetNextVariablePtr (NextVariable
);
3246 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
3249 // Allocate runtime memory used for a memory copy of the FLASH region.
3250 // Keep the memory and the FLASH in sync as updates occur
3252 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
3253 if (mNvVariableCache
== NULL
) {
3254 Status
= EFI_OUT_OF_RESOURCES
;
3257 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
3258 Status
= EFI_SUCCESS
;
3261 if (EFI_ERROR (Status
)) {
3262 FreePool (mVariableModuleGlobal
);
3263 FreePool (VolatileVariableStore
);
3271 Get the proper fvb handle and/or fvb protocol by the given Flash address.
3273 @param[in] Address The Flash address.
3274 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
3275 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
3279 GetFvbInfoByAddress (
3280 IN EFI_PHYSICAL_ADDRESS Address
,
3281 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
3282 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
3286 EFI_HANDLE
*HandleBuffer
;
3289 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
3290 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
3291 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
3292 EFI_FVB_ATTRIBUTES_2 Attributes
;
3295 // Get all FVB handles.
3297 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
3298 if (EFI_ERROR (Status
)) {
3299 return EFI_NOT_FOUND
;
3303 // Get the FVB to access variable store.
3306 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
3307 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
3308 if (EFI_ERROR (Status
)) {
3309 Status
= EFI_NOT_FOUND
;
3314 // Ensure this FVB protocol supported Write operation.
3316 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
3317 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
3322 // Compare the address and select the right one.
3324 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
3325 if (EFI_ERROR (Status
)) {
3329 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
3330 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
3331 if (FvbHandle
!= NULL
) {
3332 *FvbHandle
= HandleBuffer
[Index
];
3334 if (FvbProtocol
!= NULL
) {
3337 Status
= EFI_SUCCESS
;
3341 FreePool (HandleBuffer
);
3344 Status
= EFI_NOT_FOUND
;