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 - 2012, 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
);
517 Variable store garbage collection and reclaim operation.
519 @param VariableBase Base address of variable store.
520 @param LastVariableOffset Offset of last variable.
521 @param IsVolatile The variable store is volatile or not;
522 if it is non-volatile, need FTW.
523 @param UpdatingVariable Pointer to updating variable.
525 @return EFI_OUT_OF_RESOURCES
532 IN EFI_PHYSICAL_ADDRESS VariableBase
,
533 OUT UINTN
*LastVariableOffset
,
534 IN BOOLEAN IsVolatile
,
535 IN VARIABLE_HEADER
*UpdatingVariable
538 VARIABLE_HEADER
*Variable
;
539 VARIABLE_HEADER
*AddedVariable
;
540 VARIABLE_HEADER
*NextVariable
;
541 VARIABLE_HEADER
*NextAddedVariable
;
542 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
544 UINTN MaximumBufferSize
;
546 UINTN VariableNameSize
;
547 UINTN UpdatingVariableNameSize
;
554 CHAR16
*VariableNamePtr
;
555 CHAR16
*UpdatingVariableNamePtr
;
557 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
559 // Recalculate the total size of Common/HwErr type variables in non-volatile area.
562 mVariableModuleGlobal
->CommonVariableTotalSize
= 0;
563 mVariableModuleGlobal
->HwErrVariableTotalSize
= 0;
567 // Start Pointers for the variable.
569 Variable
= GetStartPointer (VariableStoreHeader
);
570 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
572 while (IsValidVariableHeader (Variable
)) {
573 NextVariable
= GetNextVariablePtr (Variable
);
574 if (Variable
->State
== VAR_ADDED
||
575 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
577 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
578 MaximumBufferSize
+= VariableSize
;
581 Variable
= NextVariable
;
585 // Reserve the 1 Bytes with Oxff to identify the
586 // end of the variable buffer.
588 MaximumBufferSize
+= 1;
589 ValidBuffer
= AllocatePool (MaximumBufferSize
);
590 if (ValidBuffer
== NULL
) {
591 return EFI_OUT_OF_RESOURCES
;
594 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
597 // Copy variable store header.
599 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
600 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
603 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
605 Variable
= GetStartPointer (VariableStoreHeader
);
606 while (IsValidVariableHeader (Variable
)) {
607 NextVariable
= GetNextVariablePtr (Variable
);
608 if (Variable
->State
== VAR_ADDED
) {
609 if (UpdatingVariable
!= NULL
) {
610 if (UpdatingVariable
== Variable
) {
611 Variable
= NextVariable
;
615 VariableNameSize
= NameSizeOfVariable(Variable
);
616 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
618 VariableNamePtr
= GetVariableNamePtr (Variable
);
619 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
620 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
621 VariableNameSize
== UpdatingVariableNameSize
&&
622 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
623 Variable
= NextVariable
;
627 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
628 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
629 CurrPtr
+= VariableSize
;
630 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
631 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
632 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
633 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
636 Variable
= NextVariable
;
640 // Reinstall the variable being updated if it is not NULL.
642 if (UpdatingVariable
!= NULL
) {
643 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
644 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
645 CurrPtr
+= VariableSize
;
646 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
647 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
648 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
649 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
654 // Reinstall all in delete transition variables.
656 Variable
= GetStartPointer (VariableStoreHeader
);
657 while (IsValidVariableHeader (Variable
)) {
658 NextVariable
= GetNextVariablePtr (Variable
);
659 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
662 // Buffer has cached all ADDED variable.
663 // Per IN_DELETED variable, we have to guarantee that
664 // no ADDED one in previous buffer.
668 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
669 while (IsValidVariableHeader (AddedVariable
)) {
670 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
671 NameSize
= NameSizeOfVariable (AddedVariable
);
672 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
673 NameSize
== NameSizeOfVariable (Variable
)
675 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
676 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
677 if (CompareMem (Point0
, Point1
, NameSizeOfVariable (AddedVariable
)) == 0) {
682 AddedVariable
= NextAddedVariable
;
686 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
688 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
689 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
690 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
691 CurrPtr
+= VariableSize
;
692 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
693 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
694 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
695 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
700 Variable
= NextVariable
;
705 // If volatile variable store, just copy valid buffer.
707 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
708 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
709 Status
= EFI_SUCCESS
;
712 // If non-volatile variable store, perform FTW here.
714 Status
= FtwVariableSpace (
717 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
719 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
721 if (!EFI_ERROR (Status
)) {
722 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
724 *LastVariableOffset
= 0;
727 FreePool (ValidBuffer
);
733 Find the variable in the specified variable store.
735 @param[in] VariableName Name of the variable to be found
736 @param[in] VendorGuid Vendor GUID to be found.
737 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
738 check at runtime when searching variable.
739 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.
741 @retval EFI_SUCCESS Variable found successfully
742 @retval EFI_NOT_FOUND Variable not found
746 IN CHAR16
*VariableName
,
747 IN EFI_GUID
*VendorGuid
,
748 IN BOOLEAN IgnoreRtCheck
,
749 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
752 VARIABLE_HEADER
*InDeletedVariable
;
756 // Find the variable by walk through HOB, volatile and non-volatile variable store.
758 InDeletedVariable
= NULL
;
760 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
761 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
762 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
764 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
765 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
767 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
768 if (VariableName
[0] == 0) {
769 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
770 InDeletedVariable
= PtrTrack
->CurrPtr
;
775 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
776 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
778 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
779 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
780 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
781 InDeletedVariable
= PtrTrack
->CurrPtr
;
792 PtrTrack
->CurrPtr
= InDeletedVariable
;
793 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
798 Finds variable in storage blocks of volatile and non-volatile storage areas.
800 This code finds variable in storage blocks of volatile and non-volatile storage areas.
801 If VariableName is an empty string, then we just return the first
802 qualified variable without comparing VariableName and VendorGuid.
803 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
804 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
805 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
807 @param[in] VariableName Name of the variable to be found.
808 @param[in] VendorGuid Vendor GUID to be found.
809 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
810 including the range searched and the target position.
811 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
812 base of volatile variable storage area, base of
813 NV variable storage area, and a lock.
814 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
815 check at runtime when searching variable.
817 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
819 @retval EFI_SUCCESS Variable successfully found.
820 @retval EFI_NOT_FOUND Variable not found
825 IN CHAR16
*VariableName
,
826 IN EFI_GUID
*VendorGuid
,
827 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
828 IN VARIABLE_GLOBAL
*Global
,
829 IN BOOLEAN IgnoreRtCheck
833 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
834 VARIABLE_STORE_TYPE Type
;
836 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
837 return EFI_INVALID_PARAMETER
;
841 // 0: Volatile, 1: HOB, 2: Non-Volatile.
842 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
843 // make use of this mapping to implement search algorithm.
845 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
846 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
847 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
850 // Find the variable by walk through HOB, volatile and non-volatile variable store.
852 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
853 if (VariableStoreHeader
[Type
] == NULL
) {
857 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
858 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
859 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
861 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
862 if (!EFI_ERROR (Status
)) {
866 return EFI_NOT_FOUND
;
870 Get index from supported language codes according to language string.
872 This code is used to get corresponding index in supported language codes. It can handle
873 RFC4646 and ISO639 language tags.
874 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
875 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
878 SupportedLang = "engfraengfra"
880 Iso639Language = TRUE
881 The return value is "0".
883 SupportedLang = "en;fr;en-US;fr-FR"
885 Iso639Language = FALSE
886 The return value is "3".
888 @param SupportedLang Platform supported language codes.
889 @param Lang Configured language.
890 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
892 @retval The index of language in the language codes.
896 GetIndexFromSupportedLangCodes(
897 IN CHAR8
*SupportedLang
,
899 IN BOOLEAN Iso639Language
904 UINTN LanguageLength
;
906 if (Iso639Language
) {
907 CompareLength
= ISO_639_2_ENTRY_SIZE
;
908 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
909 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
911 // Successfully find the index of Lang string in SupportedLang string.
913 Index
= Index
/ CompareLength
;
921 // Compare RFC4646 language code
924 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
926 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
928 // Skip ';' characters in SupportedLang
930 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
932 // Determine the length of the next language code in SupportedLang
934 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
936 if ((CompareLength
== LanguageLength
) &&
937 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
939 // Successfully find the index of Lang string in SupportedLang string.
950 Get language string from supported language codes according to index.
952 This code is used to get corresponding language strings in supported language codes. It can handle
953 RFC4646 and ISO639 language tags.
954 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
955 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
958 SupportedLang = "engfraengfra"
960 Iso639Language = TRUE
961 The return value is "fra".
963 SupportedLang = "en;fr;en-US;fr-FR"
965 Iso639Language = FALSE
966 The return value is "fr".
968 @param SupportedLang Platform supported language codes.
969 @param Index The index in supported language codes.
970 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
972 @retval The language string in the language codes.
976 GetLangFromSupportedLangCodes (
977 IN CHAR8
*SupportedLang
,
979 IN BOOLEAN Iso639Language
987 Supported
= SupportedLang
;
988 if (Iso639Language
) {
990 // According to the index of Lang string in SupportedLang string to get the language.
991 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
992 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
994 CompareLength
= ISO_639_2_ENTRY_SIZE
;
995 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
996 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1001 // Take semicolon as delimitation, sequentially traverse supported language codes.
1003 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1006 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1008 // Have completed the traverse, but not find corrsponding string.
1009 // This case is not allowed to happen.
1014 if (SubIndex
== Index
) {
1016 // According to the index of Lang string in SupportedLang string to get the language.
1017 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1018 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1020 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1021 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1026 // Skip ';' characters in Supported
1028 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1034 Returns a pointer to an allocated buffer that contains the best matching language
1035 from a set of supported languages.
1037 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1038 code types may not be mixed in a single call to this function. This function
1039 supports a variable argument list that allows the caller to pass in a prioritized
1040 list of language codes to test against all the language codes in SupportedLanguages.
1042 If SupportedLanguages is NULL, then ASSERT().
1044 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1045 contains a set of language codes in the format
1046 specified by Iso639Language.
1047 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1048 in ISO 639-2 format. If FALSE, then all language
1049 codes are assumed to be in RFC 4646 language format
1050 @param[in] ... A variable argument list that contains pointers to
1051 Null-terminated ASCII strings that contain one or more
1052 language codes in the format specified by Iso639Language.
1053 The first language code from each of these language
1054 code lists is used to determine if it is an exact or
1055 close match to any of the language codes in
1056 SupportedLanguages. Close matches only apply to RFC 4646
1057 language codes, and the matching algorithm from RFC 4647
1058 is used to determine if a close match is present. If
1059 an exact or close match is found, then the matching
1060 language code from SupportedLanguages is returned. If
1061 no matches are found, then the next variable argument
1062 parameter is evaluated. The variable argument list
1063 is terminated by a NULL.
1065 @retval NULL The best matching language could not be found in SupportedLanguages.
1066 @retval NULL There are not enough resources available to return the best matching
1068 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1069 language in SupportedLanguages.
1074 VariableGetBestLanguage (
1075 IN CONST CHAR8
*SupportedLanguages
,
1076 IN BOOLEAN Iso639Language
,
1082 UINTN CompareLength
;
1083 UINTN LanguageLength
;
1084 CONST CHAR8
*Supported
;
1087 if (SupportedLanguages
== NULL
) {
1091 VA_START (Args
, Iso639Language
);
1092 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1094 // Default to ISO 639-2 mode
1097 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1100 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1102 if (!Iso639Language
) {
1103 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1107 // Trim back the length of Language used until it is empty
1109 while (LanguageLength
> 0) {
1111 // Loop through all language codes in SupportedLanguages
1113 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1115 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1117 if (!Iso639Language
) {
1119 // Skip ';' characters in Supported
1121 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1123 // Determine the length of the next language code in Supported
1125 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1127 // If Language is longer than the Supported, then skip to the next language
1129 if (LanguageLength
> CompareLength
) {
1134 // See if the first LanguageLength characters in Supported match Language
1136 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1139 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1140 Buffer
[CompareLength
] = '\0';
1141 return CopyMem (Buffer
, Supported
, CompareLength
);
1145 if (Iso639Language
) {
1147 // If ISO 639 mode, then each language can only be tested once
1152 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1154 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1161 // No matches were found
1167 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1169 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1171 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1172 and are read-only. Therefore, in variable driver, only store the original value for other use.
1174 @param[in] VariableName Name of variable.
1176 @param[in] Data Variable data.
1178 @param[in] DataSize Size of data. 0 means delete.
1182 AutoUpdateLangVariable (
1183 IN CHAR16
*VariableName
,
1189 CHAR8
*BestPlatformLang
;
1193 VARIABLE_POINTER_TRACK Variable
;
1194 BOOLEAN SetLanguageCodes
;
1197 // Don't do updates for delete operation
1199 if (DataSize
== 0) {
1203 SetLanguageCodes
= FALSE
;
1205 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1207 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1213 SetLanguageCodes
= TRUE
;
1216 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1217 // Therefore, in variable driver, only store the original value for other use.
1219 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1220 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1222 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1223 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1226 // PlatformLang holds a single language from PlatformLangCodes,
1227 // so the size of PlatformLangCodes is enough for the PlatformLang.
1229 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1230 FreePool (mVariableModuleGlobal
->PlatformLang
);
1232 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1233 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1235 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1237 // LangCodes is a volatile variable, so it can not be updated at runtime.
1243 SetLanguageCodes
= TRUE
;
1246 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1247 // Therefore, in variable driver, only store the original value for other use.
1249 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1250 FreePool (mVariableModuleGlobal
->LangCodes
);
1252 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1253 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1256 if (SetLanguageCodes
1257 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1258 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1260 // Update Lang if PlatformLang is already set
1261 // Update PlatformLang if Lang is already set
1263 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1264 if (!EFI_ERROR (Status
)) {
1268 VariableName
= L
"PlatformLang";
1269 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1270 DataSize
= Variable
.CurrPtr
->DataSize
;
1272 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1273 if (!EFI_ERROR (Status
)) {
1275 // Update PlatformLang
1277 VariableName
= L
"Lang";
1278 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1279 DataSize
= Variable
.CurrPtr
->DataSize
;
1282 // Neither PlatformLang nor Lang is set, directly return
1290 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1292 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1294 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1296 // Update Lang when PlatformLangCodes/LangCodes were set.
1298 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1300 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1302 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1303 if (BestPlatformLang
!= NULL
) {
1305 // Get the corresponding index in language codes.
1307 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1310 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1312 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1315 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1317 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1319 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1320 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, 0, 0, &Variable
, NULL
);
1322 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1324 ASSERT_EFI_ERROR(Status
);
1328 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1330 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1332 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1334 // When setting Lang, firstly get most matched language string from supported language codes.
1336 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1337 if (BestLang
!= NULL
) {
1339 // Get the corresponding index in language codes.
1341 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1344 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1346 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1349 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1351 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1353 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1354 AsciiStrSize (BestPlatformLang
), Attributes
, 0, 0, &Variable
, NULL
);
1356 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1357 ASSERT_EFI_ERROR (Status
);
1364 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
1365 index of associated public key is needed.
1367 @param[in] VariableName Name of variable.
1368 @param[in] VendorGuid Guid of variable.
1369 @param[in] Data Variable data.
1370 @param[in] DataSize Size of data. 0 means delete.
1371 @param[in] Attributes Attributes of the variable.
1372 @param[in] KeyIndex Index of associated public key.
1373 @param[in] MonotonicCount Value of associated monotonic count.
1374 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1375 @param[in] TimeStamp Value of associated TimeStamp.
1377 @retval EFI_SUCCESS The update operation is success.
1378 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1383 IN CHAR16
*VariableName
,
1384 IN EFI_GUID
*VendorGuid
,
1387 IN UINT32 Attributes OPTIONAL
,
1388 IN UINT32 KeyIndex OPTIONAL
,
1389 IN UINT64 MonotonicCount OPTIONAL
,
1390 IN VARIABLE_POINTER_TRACK
*CacheVariable
,
1391 IN EFI_TIME
*TimeStamp OPTIONAL
1395 VARIABLE_HEADER
*NextVariable
;
1397 UINTN ScratchDataSize
;
1398 UINTN NonVolatileVarableStoreSize
;
1399 UINTN VarNameOffset
;
1400 UINTN VarDataOffset
;
1404 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1407 VARIABLE_POINTER_TRACK
*Variable
;
1408 VARIABLE_POINTER_TRACK NvVariable
;
1409 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1415 if (mVariableModuleGlobal
->FvbInstance
== NULL
) {
1417 // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
1419 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1421 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1423 return EFI_NOT_AVAILABLE_YET
;
1424 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1426 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1427 // The authenticated variable perhaps is not initialized, just return here.
1429 return EFI_NOT_AVAILABLE_YET
;
1433 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1434 Variable
= CacheVariable
;
1437 // Update/Delete existing NV variable.
1438 // CacheVariable points to the variable in the memory copy of Flash area
1439 // Now let Variable points to the same variable in Flash area.
1441 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1442 Variable
= &NvVariable
;
1443 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1444 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1445 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1446 Variable
->Volatile
= FALSE
;
1449 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1453 // Tricky part: Use scratch data area at the end of volatile variable store
1454 // as a temporary storage.
1456 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1457 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1458 ScratchDataSize
= ScratchSize
- sizeof (VARIABLE_HEADER
) - StrSize (VariableName
) - GET_PAD_SIZE (StrSize (VariableName
));
1460 if (Variable
->CurrPtr
!= NULL
) {
1462 // Update/Delete existing variable.
1466 // If AtRuntime and the variable is Volatile and Runtime Access,
1467 // the volatile is ReadOnly, and SetVariable should be aborted and
1468 // return EFI_WRITE_PROTECTED.
1470 if (Variable
->Volatile
) {
1471 Status
= EFI_WRITE_PROTECTED
;
1475 // Only variable that have NV attributes can be updated/deleted in Runtime.
1477 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1478 Status
= EFI_INVALID_PARAMETER
;
1483 // Only variable that have RT attributes can be updated/deleted in Runtime.
1485 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) {
1486 Status
= EFI_INVALID_PARAMETER
;
1492 // Setting a data variable with no access, or zero DataSize attributes
1493 // causes it to be deleted.
1494 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
1495 // not delete the variable.
1497 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0))|| ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
1498 State
= Variable
->CurrPtr
->State
;
1499 State
&= VAR_DELETED
;
1501 Status
= UpdateVariableStore (
1502 &mVariableModuleGlobal
->VariableGlobal
,
1506 (UINTN
) &Variable
->CurrPtr
->State
,
1510 if (!EFI_ERROR (Status
)) {
1511 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1512 if (!Variable
->Volatile
) {
1513 CacheVariable
->CurrPtr
->State
= State
;
1519 // If the variable is marked valid, and the same data has been passed in,
1520 // then return to the caller immediately.
1522 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1523 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0) &&
1524 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) &&
1525 (TimeStamp
== NULL
)) {
1527 // Variable content unchanged and no need to update timestamp, just return.
1529 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1530 Status
= EFI_SUCCESS
;
1532 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1533 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1536 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
1538 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
1540 // Cache the previous variable data into StorageArea.
1542 DataOffset
= sizeof (VARIABLE_HEADER
) + Variable
->CurrPtr
->NameSize
+ GET_PAD_SIZE (Variable
->CurrPtr
->NameSize
);
1543 CopyMem (mStorageArea
, (UINT8
*)((UINTN
) Variable
->CurrPtr
+ DataOffset
), Variable
->CurrPtr
->DataSize
);
1545 if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) ||
1546 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0))) {
1548 // For variables with the GUID EFI_IMAGE_SECURITY_DATABASE_GUID (i.e. where the data
1549 // buffer is formatted as EFI_SIGNATURE_LIST), the driver shall not perform an append of
1550 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
1552 BufSize
= AppendSignatureList (mStorageArea
, Variable
->CurrPtr
->DataSize
, Data
, DataSize
);
1553 if (BufSize
== Variable
->CurrPtr
->DataSize
) {
1554 if ((TimeStamp
== NULL
) || CompareTimeStamp (TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
1556 // New EFI_SIGNATURE_DATA is not found and timestamp is not later
1557 // than current timestamp, return EFI_SUCCESS directly.
1559 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1560 Status
= EFI_SUCCESS
;
1566 // For other Variables, append the new data to the end of previous data.
1568 CopyMem ((UINT8
*)((UINTN
) mStorageArea
+ Variable
->CurrPtr
->DataSize
), Data
, DataSize
);
1569 BufSize
= Variable
->CurrPtr
->DataSize
+ DataSize
;
1572 RevBufSize
= MIN (PcdGet32 (PcdMaxVariableSize
), ScratchDataSize
);
1573 if (BufSize
> RevBufSize
) {
1575 // If variable size (previous + current) is bigger than reserved buffer in runtime,
1576 // return EFI_OUT_OF_RESOURCES.
1578 return EFI_OUT_OF_RESOURCES
;
1582 // Override Data and DataSize which are used for combined data area including previous and new data.
1584 Data
= mStorageArea
;
1589 // Mark the old variable as in delete transition.
1591 State
= Variable
->CurrPtr
->State
;
1592 State
&= VAR_IN_DELETED_TRANSITION
;
1594 Status
= UpdateVariableStore (
1595 &mVariableModuleGlobal
->VariableGlobal
,
1599 (UINTN
) &Variable
->CurrPtr
->State
,
1603 if (EFI_ERROR (Status
)) {
1606 if (!Variable
->Volatile
) {
1607 CacheVariable
->CurrPtr
->State
= State
;
1612 // Not found existing variable. Create a new variable.
1615 if ((DataSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
1616 Status
= EFI_SUCCESS
;
1621 // Make sure we are trying to create a new variable.
1622 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1624 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1625 Status
= EFI_NOT_FOUND
;
1630 // Only variable have NV|RT attribute can be created in Runtime.
1633 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1634 Status
= EFI_INVALID_PARAMETER
;
1640 // Function part - create a new variable and copy the data.
1641 // Both update a variable and create a variable will come here.
1643 SetMem (NextVariable
, ScratchSize
, 0xff);
1645 NextVariable
->StartId
= VARIABLE_DATA
;
1647 // NextVariable->State = VAR_ADDED;
1649 NextVariable
->Reserved
= 0;
1650 NextVariable
->PubKeyIndex
= KeyIndex
;
1651 NextVariable
->MonotonicCount
= MonotonicCount
;
1652 ZeroMem (&NextVariable
->TimeStamp
, sizeof (EFI_TIME
));
1654 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1655 (TimeStamp
!= NULL
)) {
1656 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
1657 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1660 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
1661 // when the new TimeStamp value is later than the current timestamp associated
1662 // with the variable, we need associate the new timestamp with the updated value.
1664 if (Variable
->CurrPtr
!= NULL
) {
1665 if (CompareTimeStamp (&Variable
->CurrPtr
->TimeStamp
, TimeStamp
)) {
1666 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1673 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
1674 // Attributes bitmask parameter of a GetVariable() call.
1676 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
1678 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1679 VarNameSize
= StrSize (VariableName
);
1681 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1685 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1687 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1691 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1693 // There will be pad bytes after Data, the NextVariable->NameSize and
1694 // NextVariable->DataSize should not include pad size so that variable
1695 // service can get actual size in GetVariable.
1697 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1698 NextVariable
->DataSize
= (UINT32
)DataSize
;
1701 // The actual size of the variable that stores in storage should
1702 // include pad size.
1704 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1705 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1707 // Create a nonvolatile variable.
1710 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1711 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1712 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1713 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1714 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1716 Status
= EFI_OUT_OF_RESOURCES
;
1720 // Perform garbage collection & reclaim operation.
1722 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1723 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
->CurrPtr
);
1724 if (EFI_ERROR (Status
)) {
1728 // If still no enough space, return out of resources.
1730 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1731 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1732 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1733 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1734 Status
= EFI_OUT_OF_RESOURCES
;
1741 // 1. Write variable header
1742 // 2. Set variable state to header valid
1743 // 3. Write variable data
1744 // 4. Set variable state to valid
1749 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1750 Status
= UpdateVariableStore (
1751 &mVariableModuleGlobal
->VariableGlobal
,
1755 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1756 sizeof (VARIABLE_HEADER
),
1757 (UINT8
*) NextVariable
1760 if (EFI_ERROR (Status
)) {
1767 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1768 Status
= UpdateVariableStore (
1769 &mVariableModuleGlobal
->VariableGlobal
,
1773 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1775 &NextVariable
->State
1778 if (EFI_ERROR (Status
)) {
1784 Status
= UpdateVariableStore (
1785 &mVariableModuleGlobal
->VariableGlobal
,
1789 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1790 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1791 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1794 if (EFI_ERROR (Status
)) {
1800 NextVariable
->State
= VAR_ADDED
;
1801 Status
= UpdateVariableStore (
1802 &mVariableModuleGlobal
->VariableGlobal
,
1806 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1808 &NextVariable
->State
1811 if (EFI_ERROR (Status
)) {
1815 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1817 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1818 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1820 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1823 // update the memory copy of Flash region.
1825 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1828 // Create a volatile variable.
1832 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1833 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1835 // Perform garbage collection & reclaim operation.
1837 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1838 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
->CurrPtr
);
1839 if (EFI_ERROR (Status
)) {
1843 // If still no enough space, return out of resources.
1845 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1846 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1848 Status
= EFI_OUT_OF_RESOURCES
;
1854 NextVariable
->State
= VAR_ADDED
;
1855 Status
= UpdateVariableStore (
1856 &mVariableModuleGlobal
->VariableGlobal
,
1860 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1862 (UINT8
*) NextVariable
1865 if (EFI_ERROR (Status
)) {
1869 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1873 // Mark the old variable as deleted.
1875 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1876 State
= Variable
->CurrPtr
->State
;
1877 State
&= VAR_DELETED
;
1879 Status
= UpdateVariableStore (
1880 &mVariableModuleGlobal
->VariableGlobal
,
1884 (UINTN
) &Variable
->CurrPtr
->State
,
1888 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1889 CacheVariable
->CurrPtr
->State
= State
;
1893 if (!EFI_ERROR (Status
)) {
1894 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1902 Check if a Unicode character is a hexadecimal character.
1904 This function checks if a Unicode character is a
1905 hexadecimal character. The valid hexadecimal character is
1906 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
1909 @param Char The character to check against.
1911 @retval TRUE If the Char is a hexadecmial character.
1912 @retval FALSE If the Char is not a hexadecmial character.
1917 IsHexaDecimalDigitCharacter (
1921 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
1926 This code checks if variable is hardware error record variable or not.
1928 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
1929 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
1931 @param VariableName Pointer to variable name.
1932 @param VendorGuid Variable Vendor Guid.
1934 @retval TRUE Variable is hardware error record variable.
1935 @retval FALSE Variable is not hardware error record variable.
1940 IsHwErrRecVariable (
1941 IN CHAR16
*VariableName
,
1942 IN EFI_GUID
*VendorGuid
1945 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
1946 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
1947 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
1948 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
1949 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
1950 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
1951 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
1960 This code finds variable in storage blocks (Volatile or Non-Volatile).
1962 Caution: This function may receive untrusted input.
1963 This function may be invoked in SMM mode, and datasize is external input.
1964 This function will do basic validation, before parse the data.
1966 @param VariableName Name of Variable to be found.
1967 @param VendorGuid Variable vendor GUID.
1968 @param Attributes Attribute value of the variable found.
1969 @param DataSize Size of Data found. If size is less than the
1970 data, this value contains the required size.
1971 @param Data Data pointer.
1973 @return EFI_INVALID_PARAMETER Invalid parameter.
1974 @return EFI_SUCCESS Find the specified variable.
1975 @return EFI_NOT_FOUND Not found.
1976 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1981 VariableServiceGetVariable (
1982 IN CHAR16
*VariableName
,
1983 IN EFI_GUID
*VendorGuid
,
1984 OUT UINT32
*Attributes OPTIONAL
,
1985 IN OUT UINTN
*DataSize
,
1990 VARIABLE_POINTER_TRACK Variable
;
1993 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1994 return EFI_INVALID_PARAMETER
;
1997 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1999 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2000 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2007 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2008 ASSERT (VarDataSize
!= 0);
2010 if (*DataSize
>= VarDataSize
) {
2012 Status
= EFI_INVALID_PARAMETER
;
2016 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2017 if (Attributes
!= NULL
) {
2018 *Attributes
= Variable
.CurrPtr
->Attributes
;
2021 *DataSize
= VarDataSize
;
2022 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2024 Status
= EFI_SUCCESS
;
2027 *DataSize
= VarDataSize
;
2028 Status
= EFI_BUFFER_TOO_SMALL
;
2033 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2041 This code Finds the Next available variable.
2043 Caution: This function may receive untrusted input.
2044 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2046 @param VariableNameSize Size of the variable name.
2047 @param VariableName Pointer to variable name.
2048 @param VendorGuid Variable Vendor Guid.
2050 @return EFI_INVALID_PARAMETER Invalid parameter.
2051 @return EFI_SUCCESS Find the specified variable.
2052 @return EFI_NOT_FOUND Not found.
2053 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2058 VariableServiceGetNextVariableName (
2059 IN OUT UINTN
*VariableNameSize
,
2060 IN OUT CHAR16
*VariableName
,
2061 IN OUT EFI_GUID
*VendorGuid
2064 VARIABLE_STORE_TYPE Type
;
2065 VARIABLE_POINTER_TRACK Variable
;
2066 VARIABLE_POINTER_TRACK VariableInHob
;
2069 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2071 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2072 return EFI_INVALID_PARAMETER
;
2075 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2077 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2078 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2082 if (VariableName
[0] != 0) {
2084 // If variable name is not NULL, get next variable.
2086 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2090 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2091 // The index and attributes mapping must be kept in this order as FindVariable
2092 // makes use of this mapping to implement search algorithm.
2094 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2095 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2096 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2100 // Switch from Volatile to HOB, to Non-Volatile.
2102 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
2103 (Variable
.CurrPtr
== NULL
) ||
2104 !IsValidVariableHeader (Variable
.CurrPtr
)
2107 // Find current storage index
2109 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2110 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2114 ASSERT (Type
< VariableStoreTypeMax
);
2116 // Switch to next storage
2118 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2119 if (VariableStoreHeader
[Type
] != NULL
) {
2124 // Capture the case that
2125 // 1. current storage is the last one, or
2126 // 2. no further storage
2128 if (Type
== VariableStoreTypeMax
) {
2129 Status
= EFI_NOT_FOUND
;
2132 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2133 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2134 Variable
.CurrPtr
= Variable
.StartPtr
;
2138 // Variable is found
2140 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
2141 if ((AtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
2144 // Don't return NV variable when HOB overrides it
2146 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2147 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2149 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2150 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2151 Status
= FindVariableEx (
2152 GetVariableNamePtr (Variable
.CurrPtr
),
2153 &Variable
.CurrPtr
->VendorGuid
,
2157 if (!EFI_ERROR (Status
)) {
2158 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2163 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2164 ASSERT (VarNameSize
!= 0);
2166 if (VarNameSize
<= *VariableNameSize
) {
2167 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2168 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2169 Status
= EFI_SUCCESS
;
2171 Status
= EFI_BUFFER_TOO_SMALL
;
2174 *VariableNameSize
= VarNameSize
;
2179 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2183 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2189 This code sets variable in storage blocks (Volatile or Non-Volatile).
2191 Caution: This function may receive untrusted input.
2192 This function may be invoked in SMM mode, and datasize and data are external input.
2193 This function will do basic validation, before parse the data.
2194 This function will parse the authentication carefully to avoid security issues, like
2195 buffer overflow, integer overflow.
2196 This function will check attribute carefully to avoid authentication bypass.
2198 @param VariableName Name of Variable to be found.
2199 @param VendorGuid Variable vendor GUID.
2200 @param Attributes Attribute value of the variable found
2201 @param DataSize Size of Data found. If size is less than the
2202 data, this value contains the required size.
2203 @param Data Data pointer.
2205 @return EFI_INVALID_PARAMETER Invalid parameter.
2206 @return EFI_SUCCESS Set successfully.
2207 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2208 @return EFI_NOT_FOUND Not found.
2209 @return EFI_WRITE_PROTECTED Variable is read-only.
2214 VariableServiceSetVariable (
2215 IN CHAR16
*VariableName
,
2216 IN EFI_GUID
*VendorGuid
,
2217 IN UINT32 Attributes
,
2222 VARIABLE_POINTER_TRACK Variable
;
2224 VARIABLE_HEADER
*NextVariable
;
2225 EFI_PHYSICAL_ADDRESS Point
;
2229 // Check input parameters.
2231 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2232 return EFI_INVALID_PARAMETER
;
2235 if (DataSize
!= 0 && Data
== NULL
) {
2236 return EFI_INVALID_PARAMETER
;
2240 // Make sure if runtime bit is set, boot service bit is set also.
2242 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2243 return EFI_INVALID_PARAMETER
;
2247 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
2248 // cannot be set both.
2250 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
2251 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
2252 return EFI_INVALID_PARAMETER
;
2255 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2256 if (DataSize
< AUTHINFO_SIZE
) {
2258 // Try to write Authenticated Variable without AuthInfo.
2260 return EFI_SECURITY_VIOLATION
;
2262 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2263 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
2265 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2267 if (DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
||
2268 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
)) ||
2269 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) {
2270 return EFI_SECURITY_VIOLATION
;
2272 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
2274 PayloadSize
= DataSize
;
2278 // The size of the VariableName, including the Unicode Null in bytes plus
2279 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2280 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2282 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2283 if ((PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
2284 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
2285 return EFI_INVALID_PARAMETER
;
2287 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2288 return EFI_INVALID_PARAMETER
;
2292 // The size of the VariableName, including the Unicode Null in bytes plus
2293 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2295 if ((PayloadSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2296 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxVariableSize
))) {
2297 return EFI_INVALID_PARAMETER
;
2303 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence
2304 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.
2306 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, L
"HwErrRecSupport") == 0)) {
2307 return EFI_WRITE_PROTECTED
;
2311 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2314 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2316 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2317 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2319 // Parse non-volatile variable data and get last variable offset.
2321 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2322 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2323 && IsValidVariableHeader (NextVariable
)) {
2324 NextVariable
= GetNextVariablePtr (NextVariable
);
2326 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2330 // Check whether the input variable is already existed.
2332 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2333 if (!EFI_ERROR (Status
)) {
2334 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2335 return EFI_WRITE_PROTECTED
;
2340 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2342 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2344 // Process PK, KEK, Sigdb seperately.
2346 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
2347 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, TRUE
);
2348 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
2349 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2350 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
2351 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) {
2352 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2354 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2357 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2358 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2365 This code returns information about the EFI variables.
2367 Caution: This function may receive untrusted input.
2368 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2370 @param Attributes Attributes bitmask to specify the type of variables
2371 on which to return information.
2372 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2373 for the EFI variables associated with the attributes specified.
2374 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2375 for EFI variables associated with the attributes specified.
2376 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2377 associated with the attributes specified.
2379 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2380 @return EFI_SUCCESS Query successfully.
2381 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2386 VariableServiceQueryVariableInfo (
2387 IN UINT32 Attributes
,
2388 OUT UINT64
*MaximumVariableStorageSize
,
2389 OUT UINT64
*RemainingVariableStorageSize
,
2390 OUT UINT64
*MaximumVariableSize
2393 VARIABLE_HEADER
*Variable
;
2394 VARIABLE_HEADER
*NextVariable
;
2395 UINT64 VariableSize
;
2396 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2397 UINT64 CommonVariableTotalSize
;
2398 UINT64 HwErrVariableTotalSize
;
2400 CommonVariableTotalSize
= 0;
2401 HwErrVariableTotalSize
= 0;
2403 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2404 return EFI_INVALID_PARAMETER
;
2407 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2409 // Make sure the Attributes combination is supported by the platform.
2411 return EFI_UNSUPPORTED
;
2412 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2414 // Make sure if runtime bit is set, boot service bit is set also.
2416 return EFI_INVALID_PARAMETER
;
2417 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2419 // Make sure RT Attribute is set if we are in Runtime phase.
2421 return EFI_INVALID_PARAMETER
;
2422 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2424 // Make sure Hw Attribute is set with NV.
2426 return EFI_INVALID_PARAMETER
;
2429 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2431 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2433 // Query is Volatile related.
2435 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2438 // Query is Non-Volatile related.
2440 VariableStoreHeader
= mNvVariableCache
;
2444 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2445 // with the storage size (excluding the storage header size).
2447 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2450 // Harware error record variable needs larger size.
2452 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2453 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2454 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2456 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2457 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2458 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2462 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2464 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2468 // Point to the starting address of the variables.
2470 Variable
= GetStartPointer (VariableStoreHeader
);
2473 // Now walk through the related variable store.
2475 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2476 NextVariable
= GetNextVariablePtr (Variable
);
2477 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2481 // We don't take the state of the variables in mind
2482 // when calculating RemainingVariableStorageSize,
2483 // since the space occupied by variables not marked with
2484 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2486 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2487 HwErrVariableTotalSize
+= VariableSize
;
2489 CommonVariableTotalSize
+= VariableSize
;
2493 // Only care about Variables with State VAR_ADDED, because
2494 // the space not marked as VAR_ADDED is reclaimable now.
2496 if (Variable
->State
== VAR_ADDED
) {
2497 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2498 HwErrVariableTotalSize
+= VariableSize
;
2500 CommonVariableTotalSize
+= VariableSize
;
2506 // Go to the next one.
2508 Variable
= NextVariable
;
2511 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2512 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2514 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2517 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2518 *MaximumVariableSize
= 0;
2519 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2520 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2523 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2529 This function reclaims variable storage if free size is below the threshold.
2531 Caution: This function may be invoked at SMM mode.
2532 Care must be taken to make sure not security issue.
2541 UINTN CommonVariableSpace
;
2542 UINTN RemainingCommonVariableSpace
;
2543 UINTN RemainingHwErrVariableSpace
;
2545 Status
= EFI_SUCCESS
;
2547 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2549 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2551 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2553 // Check if the free area is blow a threshold.
2555 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2556 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2557 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2559 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2560 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2564 ASSERT_EFI_ERROR (Status
);
2570 Initializes variable write service after FVB was ready.
2572 @retval EFI_SUCCESS Function successfully executed.
2573 @retval Others Fail to initialize the variable service.
2577 VariableWriteServiceInitialize (
2582 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2585 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2586 VARIABLE_HEADER
*Variable
;
2589 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2590 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2593 // Check if the free area is really free.
2595 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
2596 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2599 // There must be something wrong in variable store, do reclaim operation.
2602 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2603 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2607 if (EFI_ERROR (Status
)) {
2616 // Flush the HOB variable to flash and invalidate HOB variable.
2618 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2620 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB
2622 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2623 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
2625 for ( Variable
= GetStartPointer (VariableStoreHeader
)
2626 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
2627 ; Variable
= GetNextVariablePtr (Variable
)
2629 ASSERT (Variable
->State
== VAR_ADDED
);
2630 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
2631 VariableData
= GetVariableDataPtr (Variable
);
2632 Status
= VariableServiceSetVariable (
2633 GetVariableNamePtr (Variable
),
2634 &Variable
->VendorGuid
,
2635 Variable
->Attributes
,
2639 ASSERT_EFI_ERROR (Status
);
2644 // Authenticated variable initialize.
2646 Status
= AutenticatedVariableServiceInitialize ();
2653 Initializes variable store area for non-volatile and volatile variable.
2655 @retval EFI_SUCCESS Function successfully executed.
2656 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2660 VariableCommonInitialize (
2665 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2666 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2667 VARIABLE_HEADER
*NextVariable
;
2668 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2669 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2670 UINT64 VariableStoreLength
;
2673 EFI_HOB_GUID_TYPE
*GuidHob
;
2676 // Allocate runtime memory for variable driver global structure.
2678 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2679 if (mVariableModuleGlobal
== NULL
) {
2680 return EFI_OUT_OF_RESOURCES
;
2683 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2686 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2687 // is stored with common variable in the same NV region. So the platform integrator should
2688 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2689 // PcdFlashNvStorageVariableSize.
2691 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2694 // Get HOB variable store.
2696 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
2697 if (GuidHob
!= NULL
) {
2698 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
2699 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
2700 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
2702 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
2707 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2709 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2710 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2711 if (VolatileVariableStore
== NULL
) {
2712 FreePool (mVariableModuleGlobal
);
2713 return EFI_OUT_OF_RESOURCES
;
2716 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2719 // Initialize Variable Specific Data.
2721 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2722 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2723 mVariableModuleGlobal
->FvbInstance
= NULL
;
2725 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiAuthenticatedVariableGuid
);
2726 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2727 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2728 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2729 VolatileVariableStore
->Reserved
= 0;
2730 VolatileVariableStore
->Reserved1
= 0;
2733 // Get non-volatile variable store.
2736 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2737 if (TempVariableStoreHeader
== 0) {
2738 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2742 // Check if the Firmware Volume is not corrupted
2744 if ((((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->Signature
!= EFI_FVH_SIGNATURE
) ||
2745 (!CompareGuid (&gEfiSystemNvDataFvGuid
, &((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->FileSystemGuid
))) {
2746 Status
= EFI_VOLUME_CORRUPTED
;
2747 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
2751 VariableStoreBase
= TempVariableStoreHeader
+ \
2752 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2753 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2754 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2756 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2757 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2758 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2759 Status
= EFI_VOLUME_CORRUPTED
;
2760 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2763 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2766 // Parse non-volatile variable data and get last variable offset.
2768 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2769 while (IsValidVariableHeader (NextVariable
)) {
2770 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2771 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2772 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2774 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2777 NextVariable
= GetNextVariablePtr (NextVariable
);
2780 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2783 // Allocate runtime memory used for a memory copy of the FLASH region.
2784 // Keep the memory and the FLASH in sync as updates occur
2786 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2787 if (mNvVariableCache
== NULL
) {
2788 Status
= EFI_OUT_OF_RESOURCES
;
2791 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2792 Status
= EFI_SUCCESS
;
2795 if (EFI_ERROR (Status
)) {
2796 FreePool (mVariableModuleGlobal
);
2797 FreePool (VolatileVariableStore
);
2805 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2807 @param[in] Address The Flash address.
2808 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2809 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2813 GetFvbInfoByAddress (
2814 IN EFI_PHYSICAL_ADDRESS Address
,
2815 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2816 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2820 EFI_HANDLE
*HandleBuffer
;
2823 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2824 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2825 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2826 EFI_FVB_ATTRIBUTES_2 Attributes
;
2829 // Get all FVB handles.
2831 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2832 if (EFI_ERROR (Status
)) {
2833 return EFI_NOT_FOUND
;
2837 // Get the FVB to access variable store.
2840 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2841 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2842 if (EFI_ERROR (Status
)) {
2843 Status
= EFI_NOT_FOUND
;
2848 // Ensure this FVB protocol supported Write operation.
2850 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2851 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2856 // Compare the address and select the right one.
2858 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2859 if (EFI_ERROR (Status
)) {
2863 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2864 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
2865 if (FvbHandle
!= NULL
) {
2866 *FvbHandle
= HandleBuffer
[Index
];
2868 if (FvbProtocol
!= NULL
) {
2871 Status
= EFI_SUCCESS
;
2875 FreePool (HandleBuffer
);
2878 Status
= EFI_NOT_FOUND
;