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.
524 @param ReclaimAnyway If TRUE, do reclaim anyway.
526 @return EFI_OUT_OF_RESOURCES
533 IN EFI_PHYSICAL_ADDRESS VariableBase
,
534 OUT UINTN
*LastVariableOffset
,
535 IN BOOLEAN IsVolatile
,
536 IN VARIABLE_HEADER
*UpdatingVariable
,
537 IN BOOLEAN ReclaimAnyway
540 VARIABLE_HEADER
*Variable
;
541 VARIABLE_HEADER
*AddedVariable
;
542 VARIABLE_HEADER
*NextVariable
;
543 VARIABLE_HEADER
*NextAddedVariable
;
544 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
546 UINTN MaximumBufferSize
;
548 UINTN VariableNameSize
;
549 UINTN UpdatingVariableNameSize
;
556 CHAR16
*VariableNamePtr
;
557 CHAR16
*UpdatingVariableNamePtr
;
558 UINTN CommonVariableTotalSize
;
559 UINTN HwErrVariableTotalSize
;
560 BOOLEAN NeedDoReclaim
;
562 NeedDoReclaim
= FALSE
;
563 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
565 CommonVariableTotalSize
= 0;
566 HwErrVariableTotalSize
= 0;
569 // Start Pointers for the variable.
571 Variable
= GetStartPointer (VariableStoreHeader
);
572 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
574 while (IsValidVariableHeader (Variable
)) {
575 NextVariable
= GetNextVariablePtr (Variable
);
576 if (Variable
->State
== VAR_ADDED
||
577 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
579 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
580 MaximumBufferSize
+= VariableSize
;
582 NeedDoReclaim
= TRUE
;
585 Variable
= NextVariable
;
588 if (!ReclaimAnyway
&& !NeedDoReclaim
) {
589 DEBUG ((EFI_D_INFO
, "Variable driver: no DELETED variable found, so no variable space could be reclaimed.\n"));
594 // Reserve the 1 Bytes with Oxff to identify the
595 // end of the variable buffer.
597 MaximumBufferSize
+= 1;
598 ValidBuffer
= AllocatePool (MaximumBufferSize
);
599 if (ValidBuffer
== NULL
) {
600 return EFI_OUT_OF_RESOURCES
;
603 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
606 // Copy variable store header.
608 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
609 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
612 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
614 Variable
= GetStartPointer (VariableStoreHeader
);
615 while (IsValidVariableHeader (Variable
)) {
616 NextVariable
= GetNextVariablePtr (Variable
);
617 if (Variable
->State
== VAR_ADDED
) {
618 if (UpdatingVariable
!= NULL
) {
619 if (UpdatingVariable
== Variable
) {
620 Variable
= NextVariable
;
624 VariableNameSize
= NameSizeOfVariable(Variable
);
625 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
627 VariableNamePtr
= GetVariableNamePtr (Variable
);
628 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
629 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
630 VariableNameSize
== UpdatingVariableNameSize
&&
631 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
632 Variable
= NextVariable
;
636 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
637 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
638 CurrPtr
+= VariableSize
;
639 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
640 HwErrVariableTotalSize
+= VariableSize
;
641 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
642 CommonVariableTotalSize
+= VariableSize
;
645 Variable
= NextVariable
;
649 // Reinstall the variable being updated if it is not NULL.
651 if (UpdatingVariable
!= NULL
) {
652 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
653 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
654 CurrPtr
+= VariableSize
;
655 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
656 HwErrVariableTotalSize
+= VariableSize
;
657 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
658 CommonVariableTotalSize
+= VariableSize
;
663 // Reinstall all in delete transition variables.
665 Variable
= GetStartPointer (VariableStoreHeader
);
666 while (IsValidVariableHeader (Variable
)) {
667 NextVariable
= GetNextVariablePtr (Variable
);
668 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
671 // Buffer has cached all ADDED variable.
672 // Per IN_DELETED variable, we have to guarantee that
673 // no ADDED one in previous buffer.
677 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
678 while (IsValidVariableHeader (AddedVariable
)) {
679 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
680 NameSize
= NameSizeOfVariable (AddedVariable
);
681 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
682 NameSize
== NameSizeOfVariable (Variable
)
684 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
685 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
686 if (CompareMem (Point0
, Point1
, NameSizeOfVariable (AddedVariable
)) == 0) {
691 AddedVariable
= NextAddedVariable
;
695 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
697 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
698 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
699 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
700 CurrPtr
+= VariableSize
;
701 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
702 HwErrVariableTotalSize
+= VariableSize
;
703 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
704 CommonVariableTotalSize
+= VariableSize
;
709 Variable
= NextVariable
;
714 // If volatile variable store, just copy valid buffer.
716 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
717 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
718 Status
= EFI_SUCCESS
;
721 // If non-volatile variable store, perform FTW here.
723 Status
= FtwVariableSpace (
726 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
728 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
730 if (!EFI_ERROR (Status
)) {
731 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
733 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
734 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
737 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
738 while (IsValidVariableHeader (NextVariable
)) {
739 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
740 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
741 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
742 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
743 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
746 NextVariable
= GetNextVariablePtr (NextVariable
);
748 *LastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableBase
;
751 FreePool (ValidBuffer
);
757 Find the variable in the specified variable store.
759 @param[in] VariableName Name of the variable to be found
760 @param[in] VendorGuid Vendor GUID to be found.
761 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
762 check at runtime when searching variable.
763 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.
765 @retval EFI_SUCCESS Variable found successfully
766 @retval EFI_NOT_FOUND Variable not found
770 IN CHAR16
*VariableName
,
771 IN EFI_GUID
*VendorGuid
,
772 IN BOOLEAN IgnoreRtCheck
,
773 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
776 VARIABLE_HEADER
*InDeletedVariable
;
780 // Find the variable by walk through HOB, volatile and non-volatile variable store.
782 InDeletedVariable
= NULL
;
784 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
785 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
786 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
788 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
789 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
791 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
792 if (VariableName
[0] == 0) {
793 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
794 InDeletedVariable
= PtrTrack
->CurrPtr
;
799 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
800 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
802 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
803 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
804 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
805 InDeletedVariable
= PtrTrack
->CurrPtr
;
816 PtrTrack
->CurrPtr
= InDeletedVariable
;
817 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
822 Finds variable in storage blocks of volatile and non-volatile storage areas.
824 This code finds variable in storage blocks of volatile and non-volatile storage areas.
825 If VariableName is an empty string, then we just return the first
826 qualified variable without comparing VariableName and VendorGuid.
827 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
828 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
829 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
831 @param[in] VariableName Name of the variable to be found.
832 @param[in] VendorGuid Vendor GUID to be found.
833 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
834 including the range searched and the target position.
835 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
836 base of volatile variable storage area, base of
837 NV variable storage area, and a lock.
838 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
839 check at runtime when searching variable.
841 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
843 @retval EFI_SUCCESS Variable successfully found.
844 @retval EFI_NOT_FOUND Variable not found
849 IN CHAR16
*VariableName
,
850 IN EFI_GUID
*VendorGuid
,
851 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
852 IN VARIABLE_GLOBAL
*Global
,
853 IN BOOLEAN IgnoreRtCheck
857 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
858 VARIABLE_STORE_TYPE Type
;
860 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
861 return EFI_INVALID_PARAMETER
;
865 // 0: Volatile, 1: HOB, 2: Non-Volatile.
866 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
867 // make use of this mapping to implement search algorithm.
869 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
870 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
871 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
874 // Find the variable by walk through HOB, volatile and non-volatile variable store.
876 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
877 if (VariableStoreHeader
[Type
] == NULL
) {
881 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
882 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
883 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
885 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
886 if (!EFI_ERROR (Status
)) {
890 return EFI_NOT_FOUND
;
894 Get index from supported language codes according to language string.
896 This code is used to get corresponding index in supported language codes. It can handle
897 RFC4646 and ISO639 language tags.
898 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
899 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
902 SupportedLang = "engfraengfra"
904 Iso639Language = TRUE
905 The return value is "0".
907 SupportedLang = "en;fr;en-US;fr-FR"
909 Iso639Language = FALSE
910 The return value is "3".
912 @param SupportedLang Platform supported language codes.
913 @param Lang Configured language.
914 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
916 @retval The index of language in the language codes.
920 GetIndexFromSupportedLangCodes(
921 IN CHAR8
*SupportedLang
,
923 IN BOOLEAN Iso639Language
928 UINTN LanguageLength
;
930 if (Iso639Language
) {
931 CompareLength
= ISO_639_2_ENTRY_SIZE
;
932 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
933 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
935 // Successfully find the index of Lang string in SupportedLang string.
937 Index
= Index
/ CompareLength
;
945 // Compare RFC4646 language code
948 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
950 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
952 // Skip ';' characters in SupportedLang
954 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
956 // Determine the length of the next language code in SupportedLang
958 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
960 if ((CompareLength
== LanguageLength
) &&
961 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
963 // Successfully find the index of Lang string in SupportedLang string.
974 Get language string from supported language codes according to index.
976 This code is used to get corresponding language strings in supported language codes. It can handle
977 RFC4646 and ISO639 language tags.
978 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
979 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
982 SupportedLang = "engfraengfra"
984 Iso639Language = TRUE
985 The return value is "fra".
987 SupportedLang = "en;fr;en-US;fr-FR"
989 Iso639Language = FALSE
990 The return value is "fr".
992 @param SupportedLang Platform supported language codes.
993 @param Index The index in supported language codes.
994 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
996 @retval The language string in the language codes.
1000 GetLangFromSupportedLangCodes (
1001 IN CHAR8
*SupportedLang
,
1003 IN BOOLEAN Iso639Language
1007 UINTN CompareLength
;
1011 Supported
= SupportedLang
;
1012 if (Iso639Language
) {
1014 // According to the index of Lang string in SupportedLang string to get the language.
1015 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1016 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1018 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1019 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1020 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1025 // Take semicolon as delimitation, sequentially traverse supported language codes.
1027 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1030 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1032 // Have completed the traverse, but not find corrsponding string.
1033 // This case is not allowed to happen.
1038 if (SubIndex
== Index
) {
1040 // According to the index of Lang string in SupportedLang string to get the language.
1041 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1042 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1044 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1045 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1050 // Skip ';' characters in Supported
1052 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1058 Returns a pointer to an allocated buffer that contains the best matching language
1059 from a set of supported languages.
1061 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1062 code types may not be mixed in a single call to this function. This function
1063 supports a variable argument list that allows the caller to pass in a prioritized
1064 list of language codes to test against all the language codes in SupportedLanguages.
1066 If SupportedLanguages is NULL, then ASSERT().
1068 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1069 contains a set of language codes in the format
1070 specified by Iso639Language.
1071 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1072 in ISO 639-2 format. If FALSE, then all language
1073 codes are assumed to be in RFC 4646 language format
1074 @param[in] ... A variable argument list that contains pointers to
1075 Null-terminated ASCII strings that contain one or more
1076 language codes in the format specified by Iso639Language.
1077 The first language code from each of these language
1078 code lists is used to determine if it is an exact or
1079 close match to any of the language codes in
1080 SupportedLanguages. Close matches only apply to RFC 4646
1081 language codes, and the matching algorithm from RFC 4647
1082 is used to determine if a close match is present. If
1083 an exact or close match is found, then the matching
1084 language code from SupportedLanguages is returned. If
1085 no matches are found, then the next variable argument
1086 parameter is evaluated. The variable argument list
1087 is terminated by a NULL.
1089 @retval NULL The best matching language could not be found in SupportedLanguages.
1090 @retval NULL There are not enough resources available to return the best matching
1092 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1093 language in SupportedLanguages.
1098 VariableGetBestLanguage (
1099 IN CONST CHAR8
*SupportedLanguages
,
1100 IN BOOLEAN Iso639Language
,
1106 UINTN CompareLength
;
1107 UINTN LanguageLength
;
1108 CONST CHAR8
*Supported
;
1111 if (SupportedLanguages
== NULL
) {
1115 VA_START (Args
, Iso639Language
);
1116 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1118 // Default to ISO 639-2 mode
1121 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1124 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1126 if (!Iso639Language
) {
1127 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1131 // Trim back the length of Language used until it is empty
1133 while (LanguageLength
> 0) {
1135 // Loop through all language codes in SupportedLanguages
1137 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1139 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1141 if (!Iso639Language
) {
1143 // Skip ';' characters in Supported
1145 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1147 // Determine the length of the next language code in Supported
1149 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1151 // If Language is longer than the Supported, then skip to the next language
1153 if (LanguageLength
> CompareLength
) {
1158 // See if the first LanguageLength characters in Supported match Language
1160 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1163 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1164 Buffer
[CompareLength
] = '\0';
1165 return CopyMem (Buffer
, Supported
, CompareLength
);
1169 if (Iso639Language
) {
1171 // If ISO 639 mode, then each language can only be tested once
1176 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1178 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1185 // No matches were found
1191 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1193 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1195 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1196 and are read-only. Therefore, in variable driver, only store the original value for other use.
1198 @param[in] VariableName Name of variable.
1200 @param[in] Data Variable data.
1202 @param[in] DataSize Size of data. 0 means delete.
1206 AutoUpdateLangVariable (
1207 IN CHAR16
*VariableName
,
1213 CHAR8
*BestPlatformLang
;
1217 VARIABLE_POINTER_TRACK Variable
;
1218 BOOLEAN SetLanguageCodes
;
1221 // Don't do updates for delete operation
1223 if (DataSize
== 0) {
1227 SetLanguageCodes
= FALSE
;
1229 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1231 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1237 SetLanguageCodes
= TRUE
;
1240 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1241 // Therefore, in variable driver, only store the original value for other use.
1243 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1244 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1246 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1247 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1250 // PlatformLang holds a single language from PlatformLangCodes,
1251 // so the size of PlatformLangCodes is enough for the PlatformLang.
1253 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1254 FreePool (mVariableModuleGlobal
->PlatformLang
);
1256 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1257 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1259 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1261 // LangCodes is a volatile variable, so it can not be updated at runtime.
1267 SetLanguageCodes
= TRUE
;
1270 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1271 // Therefore, in variable driver, only store the original value for other use.
1273 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1274 FreePool (mVariableModuleGlobal
->LangCodes
);
1276 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1277 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1280 if (SetLanguageCodes
1281 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1282 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1284 // Update Lang if PlatformLang is already set
1285 // Update PlatformLang if Lang is already set
1287 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1288 if (!EFI_ERROR (Status
)) {
1292 VariableName
= L
"PlatformLang";
1293 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1294 DataSize
= Variable
.CurrPtr
->DataSize
;
1296 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1297 if (!EFI_ERROR (Status
)) {
1299 // Update PlatformLang
1301 VariableName
= L
"Lang";
1302 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1303 DataSize
= Variable
.CurrPtr
->DataSize
;
1306 // Neither PlatformLang nor Lang is set, directly return
1314 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1316 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1318 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1320 // Update Lang when PlatformLangCodes/LangCodes were set.
1322 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1324 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1326 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1327 if (BestPlatformLang
!= NULL
) {
1329 // Get the corresponding index in language codes.
1331 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1334 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1336 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1339 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1341 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1343 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1344 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, 0, 0, &Variable
, NULL
);
1346 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1348 ASSERT_EFI_ERROR(Status
);
1352 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1354 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1356 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1358 // When setting Lang, firstly get most matched language string from supported language codes.
1360 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1361 if (BestLang
!= NULL
) {
1363 // Get the corresponding index in language codes.
1365 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1368 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1370 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1373 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1375 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1377 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1378 AsciiStrSize (BestPlatformLang
), Attributes
, 0, 0, &Variable
, NULL
);
1380 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1381 ASSERT_EFI_ERROR (Status
);
1388 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
1389 index of associated public key is needed.
1391 @param[in] VariableName Name of variable.
1392 @param[in] VendorGuid Guid of variable.
1393 @param[in] Data Variable data.
1394 @param[in] DataSize Size of data. 0 means delete.
1395 @param[in] Attributes Attributes of the variable.
1396 @param[in] KeyIndex Index of associated public key.
1397 @param[in] MonotonicCount Value of associated monotonic count.
1398 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1399 @param[in] TimeStamp Value of associated TimeStamp.
1401 @retval EFI_SUCCESS The update operation is success.
1402 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1407 IN CHAR16
*VariableName
,
1408 IN EFI_GUID
*VendorGuid
,
1411 IN UINT32 Attributes OPTIONAL
,
1412 IN UINT32 KeyIndex OPTIONAL
,
1413 IN UINT64 MonotonicCount OPTIONAL
,
1414 IN VARIABLE_POINTER_TRACK
*CacheVariable
,
1415 IN EFI_TIME
*TimeStamp OPTIONAL
1419 VARIABLE_HEADER
*NextVariable
;
1421 UINTN ScratchDataSize
;
1422 UINTN NonVolatileVarableStoreSize
;
1423 UINTN VarNameOffset
;
1424 UINTN VarDataOffset
;
1428 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1431 VARIABLE_POINTER_TRACK
*Variable
;
1432 VARIABLE_POINTER_TRACK NvVariable
;
1433 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1439 if (mVariableModuleGlobal
->FvbInstance
== NULL
) {
1441 // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
1443 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1445 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1447 return EFI_NOT_AVAILABLE_YET
;
1448 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1450 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1451 // The authenticated variable perhaps is not initialized, just return here.
1453 return EFI_NOT_AVAILABLE_YET
;
1457 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1458 Variable
= CacheVariable
;
1461 // Update/Delete existing NV variable.
1462 // CacheVariable points to the variable in the memory copy of Flash area
1463 // Now let Variable points to the same variable in Flash area.
1465 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1466 Variable
= &NvVariable
;
1467 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1468 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1469 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1470 Variable
->Volatile
= FALSE
;
1473 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1477 // Tricky part: Use scratch data area at the end of volatile variable store
1478 // as a temporary storage.
1480 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1481 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1482 ScratchDataSize
= ScratchSize
- sizeof (VARIABLE_HEADER
) - StrSize (VariableName
) - GET_PAD_SIZE (StrSize (VariableName
));
1484 if (Variable
->CurrPtr
!= NULL
) {
1486 // Update/Delete existing variable.
1490 // If AtRuntime and the variable is Volatile and Runtime Access,
1491 // the volatile is ReadOnly, and SetVariable should be aborted and
1492 // return EFI_WRITE_PROTECTED.
1494 if (Variable
->Volatile
) {
1495 Status
= EFI_WRITE_PROTECTED
;
1499 // Only variable that have NV attributes can be updated/deleted in Runtime.
1501 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1502 Status
= EFI_INVALID_PARAMETER
;
1507 // Only variable that have RT attributes can be updated/deleted in Runtime.
1509 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) {
1510 Status
= EFI_INVALID_PARAMETER
;
1516 // Setting a data variable with no access, or zero DataSize attributes
1517 // causes it to be deleted.
1518 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
1519 // not delete the variable.
1521 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0))|| ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
1522 State
= Variable
->CurrPtr
->State
;
1523 State
&= VAR_DELETED
;
1525 Status
= UpdateVariableStore (
1526 &mVariableModuleGlobal
->VariableGlobal
,
1530 (UINTN
) &Variable
->CurrPtr
->State
,
1534 if (!EFI_ERROR (Status
)) {
1535 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1536 if (!Variable
->Volatile
) {
1537 CacheVariable
->CurrPtr
->State
= State
;
1538 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1544 // If the variable is marked valid, and the same data has been passed in,
1545 // then return to the caller immediately.
1547 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1548 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0) &&
1549 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) &&
1550 (TimeStamp
== NULL
)) {
1552 // Variable content unchanged and no need to update timestamp, just return.
1554 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1555 Status
= EFI_SUCCESS
;
1557 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1558 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1561 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
1563 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
1565 // Cache the previous variable data into StorageArea.
1567 DataOffset
= sizeof (VARIABLE_HEADER
) + Variable
->CurrPtr
->NameSize
+ GET_PAD_SIZE (Variable
->CurrPtr
->NameSize
);
1568 CopyMem (mStorageArea
, (UINT8
*)((UINTN
) Variable
->CurrPtr
+ DataOffset
), Variable
->CurrPtr
->DataSize
);
1570 if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) ||
1571 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0))) {
1573 // For variables with the GUID EFI_IMAGE_SECURITY_DATABASE_GUID (i.e. where the data
1574 // buffer is formatted as EFI_SIGNATURE_LIST), the driver shall not perform an append of
1575 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
1577 BufSize
= AppendSignatureList (mStorageArea
, Variable
->CurrPtr
->DataSize
, Data
, DataSize
);
1578 if (BufSize
== Variable
->CurrPtr
->DataSize
) {
1579 if ((TimeStamp
== NULL
) || CompareTimeStamp (TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
1581 // New EFI_SIGNATURE_DATA is not found and timestamp is not later
1582 // than current timestamp, return EFI_SUCCESS directly.
1584 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1585 Status
= EFI_SUCCESS
;
1591 // For other Variables, append the new data to the end of previous data.
1593 CopyMem ((UINT8
*)((UINTN
) mStorageArea
+ Variable
->CurrPtr
->DataSize
), Data
, DataSize
);
1594 BufSize
= Variable
->CurrPtr
->DataSize
+ DataSize
;
1597 RevBufSize
= MIN (PcdGet32 (PcdMaxVariableSize
), ScratchDataSize
);
1598 if (BufSize
> RevBufSize
) {
1600 // If variable size (previous + current) is bigger than reserved buffer in runtime,
1601 // return EFI_OUT_OF_RESOURCES.
1603 return EFI_OUT_OF_RESOURCES
;
1607 // Override Data and DataSize which are used for combined data area including previous and new data.
1609 Data
= mStorageArea
;
1614 // Mark the old variable as in delete transition.
1616 State
= Variable
->CurrPtr
->State
;
1617 State
&= VAR_IN_DELETED_TRANSITION
;
1619 Status
= UpdateVariableStore (
1620 &mVariableModuleGlobal
->VariableGlobal
,
1624 (UINTN
) &Variable
->CurrPtr
->State
,
1628 if (EFI_ERROR (Status
)) {
1631 if (!Variable
->Volatile
) {
1632 CacheVariable
->CurrPtr
->State
= State
;
1637 // Not found existing variable. Create a new variable.
1640 if ((DataSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
1641 Status
= EFI_SUCCESS
;
1646 // Make sure we are trying to create a new variable.
1647 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1649 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1650 Status
= EFI_NOT_FOUND
;
1655 // Only variable have NV|RT attribute can be created in Runtime.
1658 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1659 Status
= EFI_INVALID_PARAMETER
;
1665 // Function part - create a new variable and copy the data.
1666 // Both update a variable and create a variable will come here.
1668 SetMem (NextVariable
, ScratchSize
, 0xff);
1670 NextVariable
->StartId
= VARIABLE_DATA
;
1672 // NextVariable->State = VAR_ADDED;
1674 NextVariable
->Reserved
= 0;
1675 NextVariable
->PubKeyIndex
= KeyIndex
;
1676 NextVariable
->MonotonicCount
= MonotonicCount
;
1677 ZeroMem (&NextVariable
->TimeStamp
, sizeof (EFI_TIME
));
1679 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1680 (TimeStamp
!= NULL
)) {
1681 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
1682 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1685 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
1686 // when the new TimeStamp value is later than the current timestamp associated
1687 // with the variable, we need associate the new timestamp with the updated value.
1689 if (Variable
->CurrPtr
!= NULL
) {
1690 if (CompareTimeStamp (&Variable
->CurrPtr
->TimeStamp
, TimeStamp
)) {
1691 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1698 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
1699 // Attributes bitmask parameter of a GetVariable() call.
1701 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
1703 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1704 VarNameSize
= StrSize (VariableName
);
1706 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1710 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1712 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1716 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1718 // There will be pad bytes after Data, the NextVariable->NameSize and
1719 // NextVariable->DataSize should not include pad size so that variable
1720 // service can get actual size in GetVariable.
1722 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1723 NextVariable
->DataSize
= (UINT32
)DataSize
;
1726 // The actual size of the variable that stores in storage should
1727 // include pad size.
1729 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1730 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1732 // Create a nonvolatile variable.
1735 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1736 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1737 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1738 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1739 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1741 Status
= EFI_OUT_OF_RESOURCES
;
1745 // Perform garbage collection & reclaim operation.
1747 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1748 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
->CurrPtr
, FALSE
);
1749 if (EFI_ERROR (Status
)) {
1753 // If still no enough space, return out of resources.
1755 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1756 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1757 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1758 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1759 Status
= EFI_OUT_OF_RESOURCES
;
1766 // 1. Write variable header
1767 // 2. Set variable state to header valid
1768 // 3. Write variable data
1769 // 4. Set variable state to valid
1774 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1775 Status
= UpdateVariableStore (
1776 &mVariableModuleGlobal
->VariableGlobal
,
1780 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1781 sizeof (VARIABLE_HEADER
),
1782 (UINT8
*) NextVariable
1785 if (EFI_ERROR (Status
)) {
1792 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1793 Status
= UpdateVariableStore (
1794 &mVariableModuleGlobal
->VariableGlobal
,
1798 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1800 &NextVariable
->State
1803 if (EFI_ERROR (Status
)) {
1809 Status
= UpdateVariableStore (
1810 &mVariableModuleGlobal
->VariableGlobal
,
1814 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1815 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1816 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1819 if (EFI_ERROR (Status
)) {
1825 NextVariable
->State
= VAR_ADDED
;
1826 Status
= UpdateVariableStore (
1827 &mVariableModuleGlobal
->VariableGlobal
,
1831 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1833 &NextVariable
->State
1836 if (EFI_ERROR (Status
)) {
1840 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1842 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1843 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1845 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1848 // update the memory copy of Flash region.
1850 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1853 // Create a volatile variable.
1857 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1858 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1860 // Perform garbage collection & reclaim operation.
1862 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1863 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
->CurrPtr
, FALSE
);
1864 if (EFI_ERROR (Status
)) {
1868 // If still no enough space, return out of resources.
1870 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1871 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1873 Status
= EFI_OUT_OF_RESOURCES
;
1879 NextVariable
->State
= VAR_ADDED
;
1880 Status
= UpdateVariableStore (
1881 &mVariableModuleGlobal
->VariableGlobal
,
1885 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1887 (UINT8
*) NextVariable
1890 if (EFI_ERROR (Status
)) {
1894 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1898 // Mark the old variable as deleted.
1900 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1901 State
= Variable
->CurrPtr
->State
;
1902 State
&= VAR_DELETED
;
1904 Status
= UpdateVariableStore (
1905 &mVariableModuleGlobal
->VariableGlobal
,
1909 (UINTN
) &Variable
->CurrPtr
->State
,
1913 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1914 CacheVariable
->CurrPtr
->State
= State
;
1918 if (!EFI_ERROR (Status
)) {
1919 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1921 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1930 Check if a Unicode character is a hexadecimal character.
1932 This function checks if a Unicode character is a
1933 hexadecimal character. The valid hexadecimal character is
1934 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
1937 @param Char The character to check against.
1939 @retval TRUE If the Char is a hexadecmial character.
1940 @retval FALSE If the Char is not a hexadecmial character.
1945 IsHexaDecimalDigitCharacter (
1949 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
1954 This code checks if variable is hardware error record variable or not.
1956 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
1957 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
1959 @param VariableName Pointer to variable name.
1960 @param VendorGuid Variable Vendor Guid.
1962 @retval TRUE Variable is hardware error record variable.
1963 @retval FALSE Variable is not hardware error record variable.
1968 IsHwErrRecVariable (
1969 IN CHAR16
*VariableName
,
1970 IN EFI_GUID
*VendorGuid
1973 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
1974 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
1975 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
1976 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
1977 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
1978 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
1979 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
1987 This code checks if variable should be treated as read-only variable.
1989 @param[in] VariableName Name of the Variable.
1990 @param[in] VendorGuid GUID of the Variable.
1992 @retval TRUE This variable is read-only variable.
1993 @retval FALSE This variable is NOT read-only variable.
1997 IsReadOnlyVariable (
1998 IN CHAR16
*VariableName
,
1999 IN EFI_GUID
*VendorGuid
2002 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)) {
2003 if ((StrCmp (VariableName
, EFI_SETUP_MODE_NAME
) == 0) ||
2004 (StrCmp (VariableName
, EFI_SIGNATURE_SUPPORT_NAME
) == 0) ||
2005 (StrCmp (VariableName
, EFI_SECURE_BOOT_MODE_NAME
) == 0)) {
2015 This code finds variable in storage blocks (Volatile or Non-Volatile).
2017 Caution: This function may receive untrusted input.
2018 This function may be invoked in SMM mode, and datasize is external input.
2019 This function will do basic validation, before parse the data.
2021 @param VariableName Name of Variable to be found.
2022 @param VendorGuid Variable vendor GUID.
2023 @param Attributes Attribute value of the variable found.
2024 @param DataSize Size of Data found. If size is less than the
2025 data, this value contains the required size.
2026 @param Data Data pointer.
2028 @return EFI_INVALID_PARAMETER Invalid parameter.
2029 @return EFI_SUCCESS Find the specified variable.
2030 @return EFI_NOT_FOUND Not found.
2031 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2036 VariableServiceGetVariable (
2037 IN CHAR16
*VariableName
,
2038 IN EFI_GUID
*VendorGuid
,
2039 OUT UINT32
*Attributes OPTIONAL
,
2040 IN OUT UINTN
*DataSize
,
2045 VARIABLE_POINTER_TRACK Variable
;
2048 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2049 return EFI_INVALID_PARAMETER
;
2052 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2054 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2055 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2062 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2063 ASSERT (VarDataSize
!= 0);
2065 if (*DataSize
>= VarDataSize
) {
2067 Status
= EFI_INVALID_PARAMETER
;
2071 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2072 if (Attributes
!= NULL
) {
2073 *Attributes
= Variable
.CurrPtr
->Attributes
;
2076 *DataSize
= VarDataSize
;
2077 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2079 Status
= EFI_SUCCESS
;
2082 *DataSize
= VarDataSize
;
2083 Status
= EFI_BUFFER_TOO_SMALL
;
2088 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2096 This code Finds the Next available variable.
2098 Caution: This function may receive untrusted input.
2099 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2101 @param VariableNameSize Size of the variable name.
2102 @param VariableName Pointer to variable name.
2103 @param VendorGuid Variable Vendor Guid.
2105 @return EFI_INVALID_PARAMETER Invalid parameter.
2106 @return EFI_SUCCESS Find the specified variable.
2107 @return EFI_NOT_FOUND Not found.
2108 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2113 VariableServiceGetNextVariableName (
2114 IN OUT UINTN
*VariableNameSize
,
2115 IN OUT CHAR16
*VariableName
,
2116 IN OUT EFI_GUID
*VendorGuid
2119 VARIABLE_STORE_TYPE Type
;
2120 VARIABLE_POINTER_TRACK Variable
;
2121 VARIABLE_POINTER_TRACK VariableInHob
;
2124 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2126 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2127 return EFI_INVALID_PARAMETER
;
2130 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2132 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2133 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2137 if (VariableName
[0] != 0) {
2139 // If variable name is not NULL, get next variable.
2141 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2145 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2146 // The index and attributes mapping must be kept in this order as FindVariable
2147 // makes use of this mapping to implement search algorithm.
2149 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2150 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2151 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2155 // Switch from Volatile to HOB, to Non-Volatile.
2157 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
2158 (Variable
.CurrPtr
== NULL
) ||
2159 !IsValidVariableHeader (Variable
.CurrPtr
)
2162 // Find current storage index
2164 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2165 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2169 ASSERT (Type
< VariableStoreTypeMax
);
2171 // Switch to next storage
2173 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2174 if (VariableStoreHeader
[Type
] != NULL
) {
2179 // Capture the case that
2180 // 1. current storage is the last one, or
2181 // 2. no further storage
2183 if (Type
== VariableStoreTypeMax
) {
2184 Status
= EFI_NOT_FOUND
;
2187 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2188 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2189 Variable
.CurrPtr
= Variable
.StartPtr
;
2193 // Variable is found
2195 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
2196 if ((AtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
2199 // Don't return NV variable when HOB overrides it
2201 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2202 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2204 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2205 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2206 Status
= FindVariableEx (
2207 GetVariableNamePtr (Variable
.CurrPtr
),
2208 &Variable
.CurrPtr
->VendorGuid
,
2212 if (!EFI_ERROR (Status
)) {
2213 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2218 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2219 ASSERT (VarNameSize
!= 0);
2221 if (VarNameSize
<= *VariableNameSize
) {
2222 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2223 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2224 Status
= EFI_SUCCESS
;
2226 Status
= EFI_BUFFER_TOO_SMALL
;
2229 *VariableNameSize
= VarNameSize
;
2234 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2238 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2244 This code sets variable in storage blocks (Volatile or Non-Volatile).
2246 Caution: This function may receive untrusted input.
2247 This function may be invoked in SMM mode, and datasize and data are external input.
2248 This function will do basic validation, before parse the data.
2249 This function will parse the authentication carefully to avoid security issues, like
2250 buffer overflow, integer overflow.
2251 This function will check attribute carefully to avoid authentication bypass.
2253 @param VariableName Name of Variable to be found.
2254 @param VendorGuid Variable vendor GUID.
2255 @param Attributes Attribute value of the variable found
2256 @param DataSize Size of Data found. If size is less than the
2257 data, this value contains the required size.
2258 @param Data Data pointer.
2260 @return EFI_INVALID_PARAMETER Invalid parameter.
2261 @return EFI_SUCCESS Set successfully.
2262 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2263 @return EFI_NOT_FOUND Not found.
2264 @return EFI_WRITE_PROTECTED Variable is read-only.
2269 VariableServiceSetVariable (
2270 IN CHAR16
*VariableName
,
2271 IN EFI_GUID
*VendorGuid
,
2272 IN UINT32 Attributes
,
2277 VARIABLE_POINTER_TRACK Variable
;
2279 VARIABLE_HEADER
*NextVariable
;
2280 EFI_PHYSICAL_ADDRESS Point
;
2284 // Check input parameters.
2286 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2287 return EFI_INVALID_PARAMETER
;
2290 if (IsReadOnlyVariable (VariableName
, VendorGuid
)) {
2291 return EFI_WRITE_PROTECTED
;
2294 if (DataSize
!= 0 && Data
== NULL
) {
2295 return EFI_INVALID_PARAMETER
;
2299 // Check for reserverd bit in variable attribute.
2301 if ((Attributes
& (~EFI_VARIABLE_ATTRIBUTES_MASK
)) != 0) {
2302 return EFI_INVALID_PARAMETER
;
2306 // Make sure if runtime bit is set, boot service bit is set also.
2308 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2309 return EFI_INVALID_PARAMETER
;
2313 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
2314 // cannot be set both.
2316 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
2317 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
2318 return EFI_INVALID_PARAMETER
;
2321 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2322 if (DataSize
< AUTHINFO_SIZE
) {
2324 // Try to write Authenticated Variable without AuthInfo.
2326 return EFI_SECURITY_VIOLATION
;
2328 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2329 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
2331 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2333 if (DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
||
2334 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
)) ||
2335 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) {
2336 return EFI_SECURITY_VIOLATION
;
2338 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
2340 PayloadSize
= DataSize
;
2344 // The size of the VariableName, including the Unicode Null in bytes plus
2345 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2346 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2348 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2349 if ((PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
2350 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
2351 return EFI_INVALID_PARAMETER
;
2353 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2354 return EFI_INVALID_PARAMETER
;
2358 // The size of the VariableName, including the Unicode Null in bytes plus
2359 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2361 if ((PayloadSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2362 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxVariableSize
))) {
2363 return EFI_INVALID_PARAMETER
;
2369 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence
2370 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.
2372 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, L
"HwErrRecSupport") == 0)) {
2373 return EFI_WRITE_PROTECTED
;
2377 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2380 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2382 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2383 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2385 // Parse non-volatile variable data and get last variable offset.
2387 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2388 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2389 && IsValidVariableHeader (NextVariable
)) {
2390 NextVariable
= GetNextVariablePtr (NextVariable
);
2392 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2396 // Check whether the input variable is already existed.
2398 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2399 if (!EFI_ERROR (Status
)) {
2400 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2401 return EFI_WRITE_PROTECTED
;
2406 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2408 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2410 // Process PK, KEK, Sigdb seperately.
2412 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
2413 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, TRUE
);
2414 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
2415 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2416 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
2417 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) {
2418 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2419 if (EFI_ERROR (Status
)) {
2420 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2423 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2426 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2427 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2434 This code returns information about the EFI variables.
2436 Caution: This function may receive untrusted input.
2437 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2439 @param Attributes Attributes bitmask to specify the type of variables
2440 on which to return information.
2441 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2442 for the EFI variables associated with the attributes specified.
2443 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2444 for EFI variables associated with the attributes specified.
2445 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2446 associated with the attributes specified.
2448 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2449 @return EFI_SUCCESS Query successfully.
2450 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2455 VariableServiceQueryVariableInfo (
2456 IN UINT32 Attributes
,
2457 OUT UINT64
*MaximumVariableStorageSize
,
2458 OUT UINT64
*RemainingVariableStorageSize
,
2459 OUT UINT64
*MaximumVariableSize
2462 VARIABLE_HEADER
*Variable
;
2463 VARIABLE_HEADER
*NextVariable
;
2464 UINT64 VariableSize
;
2465 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2466 UINT64 CommonVariableTotalSize
;
2467 UINT64 HwErrVariableTotalSize
;
2469 CommonVariableTotalSize
= 0;
2470 HwErrVariableTotalSize
= 0;
2472 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2473 return EFI_INVALID_PARAMETER
;
2476 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2478 // Make sure the Attributes combination is supported by the platform.
2480 return EFI_UNSUPPORTED
;
2481 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2483 // Make sure if runtime bit is set, boot service bit is set also.
2485 return EFI_INVALID_PARAMETER
;
2486 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2488 // Make sure RT Attribute is set if we are in Runtime phase.
2490 return EFI_INVALID_PARAMETER
;
2491 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2493 // Make sure Hw Attribute is set with NV.
2495 return EFI_INVALID_PARAMETER
;
2498 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2500 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2502 // Query is Volatile related.
2504 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2507 // Query is Non-Volatile related.
2509 VariableStoreHeader
= mNvVariableCache
;
2513 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2514 // with the storage size (excluding the storage header size).
2516 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2519 // Harware error record variable needs larger size.
2521 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2522 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2523 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2525 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2526 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2527 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2531 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2533 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2537 // Point to the starting address of the variables.
2539 Variable
= GetStartPointer (VariableStoreHeader
);
2542 // Now walk through the related variable store.
2544 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2545 NextVariable
= GetNextVariablePtr (Variable
);
2546 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2550 // We don't take the state of the variables in mind
2551 // when calculating RemainingVariableStorageSize,
2552 // since the space occupied by variables not marked with
2553 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2555 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2556 HwErrVariableTotalSize
+= VariableSize
;
2558 CommonVariableTotalSize
+= VariableSize
;
2562 // Only care about Variables with State VAR_ADDED, because
2563 // the space not marked as VAR_ADDED is reclaimable now.
2565 if (Variable
->State
== VAR_ADDED
) {
2566 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2567 HwErrVariableTotalSize
+= VariableSize
;
2569 CommonVariableTotalSize
+= VariableSize
;
2575 // Go to the next one.
2577 Variable
= NextVariable
;
2580 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2581 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2583 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2586 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2587 *MaximumVariableSize
= 0;
2588 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2589 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2592 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2598 This function reclaims variable storage if free size is below the threshold.
2600 Caution: This function may be invoked at SMM mode.
2601 Care must be taken to make sure not security issue.
2610 UINTN CommonVariableSpace
;
2611 UINTN RemainingCommonVariableSpace
;
2612 UINTN RemainingHwErrVariableSpace
;
2614 Status
= EFI_SUCCESS
;
2616 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2618 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2620 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2622 // Check if the free area is blow a threshold.
2624 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2625 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2626 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2628 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2629 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2634 ASSERT_EFI_ERROR (Status
);
2639 Flush the HOB variable to flash.
2641 @param[in] VariableName Name of variable has been updated or deleted.
2642 @param[in] VendorGuid Guid of variable has been updated or deleted.
2646 FlushHobVariableToFlash (
2647 IN CHAR16
*VariableName
,
2648 IN EFI_GUID
*VendorGuid
2652 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2653 VARIABLE_HEADER
*Variable
;
2660 // Flush the HOB variable to flash.
2662 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2663 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2665 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
2667 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
2668 for ( Variable
= GetStartPointer (VariableStoreHeader
)
2669 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
2670 ; Variable
= GetNextVariablePtr (Variable
)
2672 if (Variable
->State
!= VAR_ADDED
) {
2674 // The HOB variable has been set to DELETED state in local.
2678 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
2679 if (VendorGuid
== NULL
|| VariableName
== NULL
||
2680 !CompareGuid (VendorGuid
, &Variable
->VendorGuid
) ||
2681 StrCmp (VariableName
, GetVariableNamePtr (Variable
)) != 0) {
2682 VariableData
= GetVariableDataPtr (Variable
);
2683 Status
= VariableServiceSetVariable (
2684 GetVariableNamePtr (Variable
),
2685 &Variable
->VendorGuid
,
2686 Variable
->Attributes
,
2690 DEBUG ((EFI_D_INFO
, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
), Status
));
2693 // The updated or deleted variable is matched with the HOB variable.
2694 // Don't break here because we will try to set other HOB variables
2695 // since this variable could be set successfully.
2697 Status
= EFI_SUCCESS
;
2699 if (!EFI_ERROR (Status
)) {
2701 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
2702 // set the HOB variable to DELETED state in local.
2704 DEBUG ((EFI_D_INFO
, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
)));
2705 Variable
->State
&= VAR_DELETED
;
2712 // We still have HOB variable(s) not flushed in flash.
2714 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
2717 // All HOB variables have been flushed in flash.
2719 DEBUG ((EFI_D_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
2720 if (!AtRuntime ()) {
2721 FreePool ((VOID
*) VariableStoreHeader
);
2729 Initializes variable write service after FVB was ready.
2731 @retval EFI_SUCCESS Function successfully executed.
2732 @retval Others Fail to initialize the variable service.
2736 VariableWriteServiceInitialize (
2741 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2744 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2746 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2747 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2750 // Check if the free area is really free.
2752 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
2753 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2756 // There must be something wrong in variable store, do reclaim operation.
2759 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2760 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2765 if (EFI_ERROR (Status
)) {
2772 FlushHobVariableToFlash (NULL
, NULL
);
2775 // Authenticated variable initialize.
2777 Status
= AutenticatedVariableServiceInitialize ();
2784 Initializes variable store area for non-volatile and volatile variable.
2786 @retval EFI_SUCCESS Function successfully executed.
2787 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2791 VariableCommonInitialize (
2796 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2797 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2798 VARIABLE_HEADER
*NextVariable
;
2799 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2800 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2801 UINT64 VariableStoreLength
;
2804 EFI_HOB_GUID_TYPE
*GuidHob
;
2807 // Allocate runtime memory for variable driver global structure.
2809 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2810 if (mVariableModuleGlobal
== NULL
) {
2811 return EFI_OUT_OF_RESOURCES
;
2814 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2817 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2818 // is stored with common variable in the same NV region. So the platform integrator should
2819 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2820 // PcdFlashNvStorageVariableSize.
2822 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2825 // Get HOB variable store.
2827 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
2828 if (GuidHob
!= NULL
) {
2829 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
2830 VariableStoreLength
= (UINT64
) (GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
));
2831 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
2832 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
2833 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
2834 return EFI_OUT_OF_RESOURCES
;
2837 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
2842 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2844 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2845 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2846 if (VolatileVariableStore
== NULL
) {
2847 FreePool (mVariableModuleGlobal
);
2848 return EFI_OUT_OF_RESOURCES
;
2851 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2854 // Initialize Variable Specific Data.
2856 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2857 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2858 mVariableModuleGlobal
->FvbInstance
= NULL
;
2860 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiAuthenticatedVariableGuid
);
2861 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2862 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2863 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2864 VolatileVariableStore
->Reserved
= 0;
2865 VolatileVariableStore
->Reserved1
= 0;
2868 // Get non-volatile variable store.
2871 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2872 if (TempVariableStoreHeader
== 0) {
2873 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2877 // Check if the Firmware Volume is not corrupted
2879 if ((((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->Signature
!= EFI_FVH_SIGNATURE
) ||
2880 (!CompareGuid (&gEfiSystemNvDataFvGuid
, &((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->FileSystemGuid
))) {
2881 Status
= EFI_VOLUME_CORRUPTED
;
2882 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
2886 VariableStoreBase
= TempVariableStoreHeader
+ \
2887 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2888 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2889 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2891 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2892 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2893 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2894 Status
= EFI_VOLUME_CORRUPTED
;
2895 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2898 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2901 // Parse non-volatile variable data and get last variable offset.
2903 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2904 while (IsValidVariableHeader (NextVariable
)) {
2905 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2906 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2907 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2909 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2912 NextVariable
= GetNextVariablePtr (NextVariable
);
2915 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2918 // Allocate runtime memory used for a memory copy of the FLASH region.
2919 // Keep the memory and the FLASH in sync as updates occur
2921 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2922 if (mNvVariableCache
== NULL
) {
2923 Status
= EFI_OUT_OF_RESOURCES
;
2926 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2927 Status
= EFI_SUCCESS
;
2930 if (EFI_ERROR (Status
)) {
2931 FreePool (mVariableModuleGlobal
);
2932 FreePool (VolatileVariableStore
);
2940 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2942 @param[in] Address The Flash address.
2943 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2944 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2948 GetFvbInfoByAddress (
2949 IN EFI_PHYSICAL_ADDRESS Address
,
2950 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2951 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2955 EFI_HANDLE
*HandleBuffer
;
2958 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2959 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2960 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2961 EFI_FVB_ATTRIBUTES_2 Attributes
;
2964 // Get all FVB handles.
2966 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2967 if (EFI_ERROR (Status
)) {
2968 return EFI_NOT_FOUND
;
2972 // Get the FVB to access variable store.
2975 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2976 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2977 if (EFI_ERROR (Status
)) {
2978 Status
= EFI_NOT_FOUND
;
2983 // Ensure this FVB protocol supported Write operation.
2985 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2986 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2991 // Compare the address and select the right one.
2993 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2994 if (EFI_ERROR (Status
)) {
2998 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2999 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
3000 if (FvbHandle
!= NULL
) {
3001 *FvbHandle
= HandleBuffer
[Index
];
3003 if (FvbProtocol
!= NULL
) {
3006 Status
= EFI_SUCCESS
;
3010 FreePool (HandleBuffer
);
3013 Status
= EFI_NOT_FOUND
;