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])) {
1959 This code checks if variable should be treated as read-only variable.
1961 @param[in] VariableName Name of the Variable.
1962 @param[in] VendorGuid GUID of the Variable.
1964 @retval TRUE This variable is read-only variable.
1965 @retval FALSE This variable is NOT read-only variable.
1969 IsReadOnlyVariable (
1970 IN CHAR16
*VariableName
,
1971 IN EFI_GUID
*VendorGuid
1974 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)) {
1975 if ((StrCmp (VariableName
, EFI_SETUP_MODE_NAME
) == 0) ||
1976 (StrCmp (VariableName
, EFI_SIGNATURE_SUPPORT_NAME
) == 0) ||
1977 (StrCmp (VariableName
, EFI_SECURE_BOOT_MODE_NAME
) == 0)) {
1987 This code finds variable in storage blocks (Volatile or Non-Volatile).
1989 Caution: This function may receive untrusted input.
1990 This function may be invoked in SMM mode, and datasize is external input.
1991 This function will do basic validation, before parse the data.
1993 @param VariableName Name of Variable to be found.
1994 @param VendorGuid Variable vendor GUID.
1995 @param Attributes Attribute value of the variable found.
1996 @param DataSize Size of Data found. If size is less than the
1997 data, this value contains the required size.
1998 @param Data Data pointer.
2000 @return EFI_INVALID_PARAMETER Invalid parameter.
2001 @return EFI_SUCCESS Find the specified variable.
2002 @return EFI_NOT_FOUND Not found.
2003 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2008 VariableServiceGetVariable (
2009 IN CHAR16
*VariableName
,
2010 IN EFI_GUID
*VendorGuid
,
2011 OUT UINT32
*Attributes OPTIONAL
,
2012 IN OUT UINTN
*DataSize
,
2017 VARIABLE_POINTER_TRACK Variable
;
2020 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2021 return EFI_INVALID_PARAMETER
;
2024 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2026 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2027 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2034 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2035 ASSERT (VarDataSize
!= 0);
2037 if (*DataSize
>= VarDataSize
) {
2039 Status
= EFI_INVALID_PARAMETER
;
2043 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2044 if (Attributes
!= NULL
) {
2045 *Attributes
= Variable
.CurrPtr
->Attributes
;
2048 *DataSize
= VarDataSize
;
2049 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2051 Status
= EFI_SUCCESS
;
2054 *DataSize
= VarDataSize
;
2055 Status
= EFI_BUFFER_TOO_SMALL
;
2060 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2068 This code Finds the Next available variable.
2070 Caution: This function may receive untrusted input.
2071 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2073 @param VariableNameSize Size of the variable name.
2074 @param VariableName Pointer to variable name.
2075 @param VendorGuid Variable Vendor Guid.
2077 @return EFI_INVALID_PARAMETER Invalid parameter.
2078 @return EFI_SUCCESS Find the specified variable.
2079 @return EFI_NOT_FOUND Not found.
2080 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2085 VariableServiceGetNextVariableName (
2086 IN OUT UINTN
*VariableNameSize
,
2087 IN OUT CHAR16
*VariableName
,
2088 IN OUT EFI_GUID
*VendorGuid
2091 VARIABLE_STORE_TYPE Type
;
2092 VARIABLE_POINTER_TRACK Variable
;
2093 VARIABLE_POINTER_TRACK VariableInHob
;
2096 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2098 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2099 return EFI_INVALID_PARAMETER
;
2102 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2104 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2105 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2109 if (VariableName
[0] != 0) {
2111 // If variable name is not NULL, get next variable.
2113 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2117 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2118 // The index and attributes mapping must be kept in this order as FindVariable
2119 // makes use of this mapping to implement search algorithm.
2121 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2122 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2123 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2127 // Switch from Volatile to HOB, to Non-Volatile.
2129 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
2130 (Variable
.CurrPtr
== NULL
) ||
2131 !IsValidVariableHeader (Variable
.CurrPtr
)
2134 // Find current storage index
2136 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2137 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2141 ASSERT (Type
< VariableStoreTypeMax
);
2143 // Switch to next storage
2145 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2146 if (VariableStoreHeader
[Type
] != NULL
) {
2151 // Capture the case that
2152 // 1. current storage is the last one, or
2153 // 2. no further storage
2155 if (Type
== VariableStoreTypeMax
) {
2156 Status
= EFI_NOT_FOUND
;
2159 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2160 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2161 Variable
.CurrPtr
= Variable
.StartPtr
;
2165 // Variable is found
2167 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
2168 if ((AtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
2171 // Don't return NV variable when HOB overrides it
2173 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2174 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2176 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2177 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2178 Status
= FindVariableEx (
2179 GetVariableNamePtr (Variable
.CurrPtr
),
2180 &Variable
.CurrPtr
->VendorGuid
,
2184 if (!EFI_ERROR (Status
)) {
2185 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2190 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2191 ASSERT (VarNameSize
!= 0);
2193 if (VarNameSize
<= *VariableNameSize
) {
2194 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2195 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2196 Status
= EFI_SUCCESS
;
2198 Status
= EFI_BUFFER_TOO_SMALL
;
2201 *VariableNameSize
= VarNameSize
;
2206 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2210 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2216 This code sets variable in storage blocks (Volatile or Non-Volatile).
2218 Caution: This function may receive untrusted input.
2219 This function may be invoked in SMM mode, and datasize and data are external input.
2220 This function will do basic validation, before parse the data.
2221 This function will parse the authentication carefully to avoid security issues, like
2222 buffer overflow, integer overflow.
2223 This function will check attribute carefully to avoid authentication bypass.
2225 @param VariableName Name of Variable to be found.
2226 @param VendorGuid Variable vendor GUID.
2227 @param Attributes Attribute value of the variable found
2228 @param DataSize Size of Data found. If size is less than the
2229 data, this value contains the required size.
2230 @param Data Data pointer.
2232 @return EFI_INVALID_PARAMETER Invalid parameter.
2233 @return EFI_SUCCESS Set successfully.
2234 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2235 @return EFI_NOT_FOUND Not found.
2236 @return EFI_WRITE_PROTECTED Variable is read-only.
2241 VariableServiceSetVariable (
2242 IN CHAR16
*VariableName
,
2243 IN EFI_GUID
*VendorGuid
,
2244 IN UINT32 Attributes
,
2249 VARIABLE_POINTER_TRACK Variable
;
2251 VARIABLE_HEADER
*NextVariable
;
2252 EFI_PHYSICAL_ADDRESS Point
;
2256 // Check input parameters.
2258 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2259 return EFI_INVALID_PARAMETER
;
2262 if (IsReadOnlyVariable (VariableName
, VendorGuid
)) {
2263 return EFI_WRITE_PROTECTED
;
2266 if (DataSize
!= 0 && Data
== NULL
) {
2267 return EFI_INVALID_PARAMETER
;
2271 // Make sure if runtime bit is set, boot service bit is set also.
2273 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2274 return EFI_INVALID_PARAMETER
;
2278 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
2279 // cannot be set both.
2281 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
2282 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
2283 return EFI_INVALID_PARAMETER
;
2286 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2287 if (DataSize
< AUTHINFO_SIZE
) {
2289 // Try to write Authenticated Variable without AuthInfo.
2291 return EFI_SECURITY_VIOLATION
;
2293 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2294 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
2296 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2298 if (DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
||
2299 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
)) ||
2300 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) {
2301 return EFI_SECURITY_VIOLATION
;
2303 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
2305 PayloadSize
= DataSize
;
2309 // The size of the VariableName, including the Unicode Null in bytes plus
2310 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2311 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2313 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2314 if ((PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
2315 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
2316 return EFI_INVALID_PARAMETER
;
2318 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2319 return EFI_INVALID_PARAMETER
;
2323 // The size of the VariableName, including the Unicode Null in bytes plus
2324 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2326 if ((PayloadSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2327 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxVariableSize
))) {
2328 return EFI_INVALID_PARAMETER
;
2334 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence
2335 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.
2337 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, L
"HwErrRecSupport") == 0)) {
2338 return EFI_WRITE_PROTECTED
;
2342 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2345 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2347 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2348 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2350 // Parse non-volatile variable data and get last variable offset.
2352 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2353 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2354 && IsValidVariableHeader (NextVariable
)) {
2355 NextVariable
= GetNextVariablePtr (NextVariable
);
2357 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2361 // Check whether the input variable is already existed.
2363 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2364 if (!EFI_ERROR (Status
)) {
2365 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2366 return EFI_WRITE_PROTECTED
;
2371 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2373 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2375 // Process PK, KEK, Sigdb seperately.
2377 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
2378 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, TRUE
);
2379 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
2380 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2381 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
2382 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) {
2383 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2384 if (EFI_ERROR (Status
)) {
2385 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2388 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2391 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2392 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2399 This code returns information about the EFI variables.
2401 Caution: This function may receive untrusted input.
2402 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2404 @param Attributes Attributes bitmask to specify the type of variables
2405 on which to return information.
2406 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2407 for the EFI variables associated with the attributes specified.
2408 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2409 for EFI variables associated with the attributes specified.
2410 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2411 associated with the attributes specified.
2413 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2414 @return EFI_SUCCESS Query successfully.
2415 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2420 VariableServiceQueryVariableInfo (
2421 IN UINT32 Attributes
,
2422 OUT UINT64
*MaximumVariableStorageSize
,
2423 OUT UINT64
*RemainingVariableStorageSize
,
2424 OUT UINT64
*MaximumVariableSize
2427 VARIABLE_HEADER
*Variable
;
2428 VARIABLE_HEADER
*NextVariable
;
2429 UINT64 VariableSize
;
2430 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2431 UINT64 CommonVariableTotalSize
;
2432 UINT64 HwErrVariableTotalSize
;
2434 CommonVariableTotalSize
= 0;
2435 HwErrVariableTotalSize
= 0;
2437 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2438 return EFI_INVALID_PARAMETER
;
2441 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2443 // Make sure the Attributes combination is supported by the platform.
2445 return EFI_UNSUPPORTED
;
2446 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2448 // Make sure if runtime bit is set, boot service bit is set also.
2450 return EFI_INVALID_PARAMETER
;
2451 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2453 // Make sure RT Attribute is set if we are in Runtime phase.
2455 return EFI_INVALID_PARAMETER
;
2456 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2458 // Make sure Hw Attribute is set with NV.
2460 return EFI_INVALID_PARAMETER
;
2463 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2465 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2467 // Query is Volatile related.
2469 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2472 // Query is Non-Volatile related.
2474 VariableStoreHeader
= mNvVariableCache
;
2478 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2479 // with the storage size (excluding the storage header size).
2481 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2484 // Harware error record variable needs larger size.
2486 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2487 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2488 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2490 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2491 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2492 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2496 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2498 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2502 // Point to the starting address of the variables.
2504 Variable
= GetStartPointer (VariableStoreHeader
);
2507 // Now walk through the related variable store.
2509 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2510 NextVariable
= GetNextVariablePtr (Variable
);
2511 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2515 // We don't take the state of the variables in mind
2516 // when calculating RemainingVariableStorageSize,
2517 // since the space occupied by variables not marked with
2518 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2520 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2521 HwErrVariableTotalSize
+= VariableSize
;
2523 CommonVariableTotalSize
+= VariableSize
;
2527 // Only care about Variables with State VAR_ADDED, because
2528 // the space not marked as VAR_ADDED is reclaimable now.
2530 if (Variable
->State
== VAR_ADDED
) {
2531 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2532 HwErrVariableTotalSize
+= VariableSize
;
2534 CommonVariableTotalSize
+= VariableSize
;
2540 // Go to the next one.
2542 Variable
= NextVariable
;
2545 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2546 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2548 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2551 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2552 *MaximumVariableSize
= 0;
2553 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2554 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2557 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2563 This function reclaims variable storage if free size is below the threshold.
2565 Caution: This function may be invoked at SMM mode.
2566 Care must be taken to make sure not security issue.
2575 UINTN CommonVariableSpace
;
2576 UINTN RemainingCommonVariableSpace
;
2577 UINTN RemainingHwErrVariableSpace
;
2579 Status
= EFI_SUCCESS
;
2581 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2583 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2585 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2587 // Check if the free area is blow a threshold.
2589 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2590 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2591 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2593 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2594 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2598 ASSERT_EFI_ERROR (Status
);
2604 Initializes variable write service after FVB was ready.
2606 @retval EFI_SUCCESS Function successfully executed.
2607 @retval Others Fail to initialize the variable service.
2611 VariableWriteServiceInitialize (
2616 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2619 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2620 VARIABLE_HEADER
*Variable
;
2623 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2624 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2627 // Check if the free area is really free.
2629 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
2630 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2633 // There must be something wrong in variable store, do reclaim operation.
2636 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2637 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2641 if (EFI_ERROR (Status
)) {
2650 // Flush the HOB variable to flash and invalidate HOB variable.
2652 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2654 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB
2656 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2657 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
2659 for ( Variable
= GetStartPointer (VariableStoreHeader
)
2660 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
2661 ; Variable
= GetNextVariablePtr (Variable
)
2663 ASSERT (Variable
->State
== VAR_ADDED
);
2664 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
2665 VariableData
= GetVariableDataPtr (Variable
);
2666 Status
= VariableServiceSetVariable (
2667 GetVariableNamePtr (Variable
),
2668 &Variable
->VendorGuid
,
2669 Variable
->Attributes
,
2673 ASSERT_EFI_ERROR (Status
);
2678 // Authenticated variable initialize.
2680 Status
= AutenticatedVariableServiceInitialize ();
2687 Initializes variable store area for non-volatile and volatile variable.
2689 @retval EFI_SUCCESS Function successfully executed.
2690 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2694 VariableCommonInitialize (
2699 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2700 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2701 VARIABLE_HEADER
*NextVariable
;
2702 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2703 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2704 UINT64 VariableStoreLength
;
2707 EFI_HOB_GUID_TYPE
*GuidHob
;
2710 // Allocate runtime memory for variable driver global structure.
2712 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2713 if (mVariableModuleGlobal
== NULL
) {
2714 return EFI_OUT_OF_RESOURCES
;
2717 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2720 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2721 // is stored with common variable in the same NV region. So the platform integrator should
2722 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2723 // PcdFlashNvStorageVariableSize.
2725 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2728 // Get HOB variable store.
2730 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
2731 if (GuidHob
!= NULL
) {
2732 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
2733 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
2734 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
2736 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
2741 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2743 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2744 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2745 if (VolatileVariableStore
== NULL
) {
2746 FreePool (mVariableModuleGlobal
);
2747 return EFI_OUT_OF_RESOURCES
;
2750 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2753 // Initialize Variable Specific Data.
2755 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2756 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2757 mVariableModuleGlobal
->FvbInstance
= NULL
;
2759 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiAuthenticatedVariableGuid
);
2760 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2761 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2762 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2763 VolatileVariableStore
->Reserved
= 0;
2764 VolatileVariableStore
->Reserved1
= 0;
2767 // Get non-volatile variable store.
2770 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2771 if (TempVariableStoreHeader
== 0) {
2772 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2776 // Check if the Firmware Volume is not corrupted
2778 if ((((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->Signature
!= EFI_FVH_SIGNATURE
) ||
2779 (!CompareGuid (&gEfiSystemNvDataFvGuid
, &((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->FileSystemGuid
))) {
2780 Status
= EFI_VOLUME_CORRUPTED
;
2781 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
2785 VariableStoreBase
= TempVariableStoreHeader
+ \
2786 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2787 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2788 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2790 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2791 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2792 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2793 Status
= EFI_VOLUME_CORRUPTED
;
2794 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2797 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2800 // Parse non-volatile variable data and get last variable offset.
2802 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2803 while (IsValidVariableHeader (NextVariable
)) {
2804 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2805 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2806 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2808 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2811 NextVariable
= GetNextVariablePtr (NextVariable
);
2814 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2817 // Allocate runtime memory used for a memory copy of the FLASH region.
2818 // Keep the memory and the FLASH in sync as updates occur
2820 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2821 if (mNvVariableCache
== NULL
) {
2822 Status
= EFI_OUT_OF_RESOURCES
;
2825 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2826 Status
= EFI_SUCCESS
;
2829 if (EFI_ERROR (Status
)) {
2830 FreePool (mVariableModuleGlobal
);
2831 FreePool (VolatileVariableStore
);
2839 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2841 @param[in] Address The Flash address.
2842 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2843 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2847 GetFvbInfoByAddress (
2848 IN EFI_PHYSICAL_ADDRESS Address
,
2849 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2850 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2854 EFI_HANDLE
*HandleBuffer
;
2857 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2858 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2859 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2860 EFI_FVB_ATTRIBUTES_2 Attributes
;
2863 // Get all FVB handles.
2865 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2866 if (EFI_ERROR (Status
)) {
2867 return EFI_NOT_FOUND
;
2871 // Get the FVB to access variable store.
2874 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2875 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2876 if (EFI_ERROR (Status
)) {
2877 Status
= EFI_NOT_FOUND
;
2882 // Ensure this FVB protocol supported Write operation.
2884 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2885 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2890 // Compare the address and select the right one.
2892 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2893 if (EFI_ERROR (Status
)) {
2897 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2898 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
2899 if (FvbHandle
!= NULL
) {
2900 *FvbHandle
= HandleBuffer
[Index
];
2902 if (FvbProtocol
!= NULL
) {
2905 Status
= EFI_SUCCESS
;
2909 FreePool (HandleBuffer
);
2912 Status
= EFI_NOT_FOUND
;