2 The common variable operation routines shared by DXE_RINTIME variable
3 module and DXE_SMM variable module.
5 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "AuthService.h"
19 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
22 /// Define a memory cache that improves the search performance for a variable.
24 VARIABLE_STORE_HEADER
*mNvVariableCache
= NULL
;
27 /// The memory entry used for variable statistics data.
29 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
33 Routine used to track statistical information about variable usage.
34 The data is stored in the EFI system table so it can be accessed later.
35 VariableInfo.efi can dump out the table. Only Boot Services variable
36 accesses are tracked by this code. The PcdVariableCollectStatistics
37 build flag controls if this feature is enabled.
39 A read that hits in the cache will have Read and Cache true for
40 the transaction. Data is allocated by this routine, but never
43 @param[in] VariableName Name of the Variable to track.
44 @param[in] VendorGuid Guid of the Variable to track.
45 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
46 @param[in] Read TRUE if GetVariable() was called.
47 @param[in] Write TRUE if SetVariable() was called.
48 @param[in] Delete TRUE if deleted via SetVariable().
49 @param[in] Cache TRUE for a cache hit.
54 IN CHAR16
*VariableName
,
55 IN EFI_GUID
*VendorGuid
,
63 VARIABLE_INFO_ENTRY
*Entry
;
65 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
68 // Don't collect statistics at runtime.
72 if (gVariableInfo
== NULL
) {
74 // On the first call allocate a entry and place a pointer to it in
75 // the EFI System Table.
77 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
78 ASSERT (gVariableInfo
!= NULL
);
80 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
81 gVariableInfo
->Name
= AllocatePool (StrSize (VariableName
));
82 ASSERT (gVariableInfo
->Name
!= NULL
);
83 StrCpy (gVariableInfo
->Name
, VariableName
);
84 gVariableInfo
->Volatile
= Volatile
;
88 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
89 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
90 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
108 if (Entry
->Next
== NULL
) {
110 // If the entry is not in the table add it.
111 // Next iteration of the loop will fill in the data.
113 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
114 ASSERT (Entry
->Next
!= NULL
);
116 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
117 Entry
->Next
->Name
= AllocatePool (StrSize (VariableName
));
118 ASSERT (Entry
->Next
->Name
!= NULL
);
119 StrCpy (Entry
->Next
->Name
, VariableName
);
120 Entry
->Next
->Volatile
= Volatile
;
130 This code checks if variable header is valid or not.
132 @param Variable Pointer to the Variable Header.
134 @retval TRUE Variable header is valid.
135 @retval FALSE Variable header is not valid.
139 IsValidVariableHeader (
140 IN VARIABLE_HEADER
*Variable
143 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
153 This function writes data to the FWH at the correct LBA even if the LBAs
156 @param Global Pointer to VARAIBLE_GLOBAL structure.
157 @param Volatile Point out the Variable is Volatile or Non-Volatile.
158 @param SetByIndex TRUE if target pointer is given as index.
159 FALSE if target pointer is absolute.
160 @param Fvb Pointer to the writable FVB protocol.
161 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
163 @param DataSize Size of data to be written.
164 @param Buffer Pointer to the buffer from which data is written.
166 @retval EFI_INVALID_PARAMETER Parameters not valid.
167 @retval EFI_SUCCESS Variable store successfully updated.
171 UpdateVariableStore (
172 IN VARIABLE_GLOBAL
*Global
,
174 IN BOOLEAN SetByIndex
,
175 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
176 IN UINTN DataPtrIndex
,
181 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
189 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
190 VARIABLE_STORE_HEADER
*VolatileBase
;
191 EFI_PHYSICAL_ADDRESS FvVolHdr
;
192 EFI_PHYSICAL_ADDRESS DataPtr
;
196 DataPtr
= DataPtrIndex
;
199 // Check if the Data is Volatile.
203 return EFI_INVALID_PARAMETER
;
205 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
206 ASSERT_EFI_ERROR (Status
);
208 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
210 // Data Pointer should point to the actual Address where data is to be
214 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
217 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
218 return EFI_INVALID_PARAMETER
;
222 // Data Pointer should point to the actual Address where data is to be
225 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
227 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
230 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
231 return EFI_INVALID_PARAMETER
;
235 // If Volatile Variable just do a simple mem copy.
237 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
242 // If we are here we are dealing with Non-Volatile Variables.
244 LinearOffset
= (UINTN
) FwVolHeader
;
245 CurrWritePtr
= (UINTN
) DataPtr
;
246 CurrWriteSize
= DataSize
;
250 if (CurrWritePtr
< LinearOffset
) {
251 return EFI_INVALID_PARAMETER
;
254 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
255 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
257 // Check to see if the Variable Writes are spanning through multiple
260 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
261 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
262 Status
= Fvb
->Write (
265 (UINTN
) (CurrWritePtr
- LinearOffset
),
271 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
272 Status
= Fvb
->Write (
275 (UINTN
) (CurrWritePtr
- LinearOffset
),
279 if (EFI_ERROR (Status
)) {
283 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
284 CurrBuffer
= CurrBuffer
+ Size
;
285 CurrWriteSize
= CurrWriteSize
- Size
;
289 LinearOffset
+= PtrBlockMapEntry
->Length
;
300 This code gets the current status of Variable Store.
302 @param VarStoreHeader Pointer to the Variable Store Header.
304 @retval EfiRaw Variable store status is raw.
305 @retval EfiValid Variable store status is valid.
306 @retval EfiInvalid Variable store status is invalid.
309 VARIABLE_STORE_STATUS
310 GetVariableStoreStatus (
311 IN VARIABLE_STORE_HEADER
*VarStoreHeader
314 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) &&
315 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
316 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
320 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
321 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
322 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
323 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
324 VarStoreHeader
->Size
== 0xffffffff &&
325 VarStoreHeader
->Format
== 0xff &&
326 VarStoreHeader
->State
== 0xff
338 This code gets the size of name of variable.
340 @param Variable Pointer to the Variable Header.
342 @return UINTN Size of variable in bytes.
347 IN VARIABLE_HEADER
*Variable
350 if (Variable
->State
== (UINT8
) (-1) ||
351 Variable
->DataSize
== (UINT32
) (-1) ||
352 Variable
->NameSize
== (UINT32
) (-1) ||
353 Variable
->Attributes
== (UINT32
) (-1)) {
356 return (UINTN
) Variable
->NameSize
;
361 This code gets the size of variable data.
363 @param Variable Pointer to the Variable Header.
365 @return Size of variable in bytes.
370 IN VARIABLE_HEADER
*Variable
373 if (Variable
->State
== (UINT8
) (-1) ||
374 Variable
->DataSize
== (UINT32
) (-1) ||
375 Variable
->NameSize
== (UINT32
) (-1) ||
376 Variable
->Attributes
== (UINT32
) (-1)) {
379 return (UINTN
) Variable
->DataSize
;
384 This code gets the pointer to the variable name.
386 @param Variable Pointer to the Variable Header.
388 @return Pointer to Variable Name which is Unicode encoding.
393 IN VARIABLE_HEADER
*Variable
397 return (CHAR16
*) (Variable
+ 1);
402 This code gets the pointer to the variable data.
404 @param Variable Pointer to the Variable Header.
406 @return Pointer to Variable Data.
411 IN VARIABLE_HEADER
*Variable
417 // Be careful about pad size for alignment.
419 Value
= (UINTN
) GetVariableNamePtr (Variable
);
420 Value
+= NameSizeOfVariable (Variable
);
421 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
423 return (UINT8
*) Value
;
429 This code gets the pointer to the next variable header.
431 @param Variable Pointer to the Variable Header.
433 @return Pointer to next variable header.
438 IN VARIABLE_HEADER
*Variable
443 if (!IsValidVariableHeader (Variable
)) {
447 Value
= (UINTN
) GetVariableDataPtr (Variable
);
448 Value
+= DataSizeOfVariable (Variable
);
449 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
452 // Be careful about pad size for alignment.
454 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
459 Gets the pointer to the first variable header in given variable store area.
461 @param VarStoreHeader Pointer to the Variable Store Header.
463 @return Pointer to the first variable header.
468 IN VARIABLE_STORE_HEADER
*VarStoreHeader
472 // The end of variable store.
474 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
479 Gets the pointer to the end of the variable storage area.
481 This function gets pointer to the end of the variable storage
482 area, according to the input variable store header.
484 @param VarStoreHeader Pointer to the Variable Store Header.
486 @return Pointer to the end of the variable storage area.
491 IN VARIABLE_STORE_HEADER
*VarStoreHeader
495 // The end of variable store
497 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
503 Variable store garbage collection and reclaim operation.
505 @param VariableBase Base address of variable store.
506 @param LastVariableOffset Offset of last variable.
507 @param IsVolatile The variable store is volatile or not;
508 if it is non-volatile, need FTW.
509 @param UpdatingVariable Pointer to updating variable.
511 @return EFI_OUT_OF_RESOURCES
518 IN EFI_PHYSICAL_ADDRESS VariableBase
,
519 OUT UINTN
*LastVariableOffset
,
520 IN BOOLEAN IsVolatile
,
521 IN VARIABLE_HEADER
*UpdatingVariable
524 VARIABLE_HEADER
*Variable
;
525 VARIABLE_HEADER
*AddedVariable
;
526 VARIABLE_HEADER
*NextVariable
;
527 VARIABLE_HEADER
*NextAddedVariable
;
528 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
530 UINTN MaximumBufferSize
;
532 UINTN VariableNameSize
;
533 UINTN UpdatingVariableNameSize
;
540 CHAR16
*VariableNamePtr
;
541 CHAR16
*UpdatingVariableNamePtr
;
543 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
545 // Recalculate the total size of Common/HwErr type variables in non-volatile area.
548 mVariableModuleGlobal
->CommonVariableTotalSize
= 0;
549 mVariableModuleGlobal
->HwErrVariableTotalSize
= 0;
553 // Start Pointers for the variable.
555 Variable
= GetStartPointer (VariableStoreHeader
);
556 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
558 while (IsValidVariableHeader (Variable
)) {
559 NextVariable
= GetNextVariablePtr (Variable
);
560 if (Variable
->State
== VAR_ADDED
||
561 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
563 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
564 MaximumBufferSize
+= VariableSize
;
567 Variable
= NextVariable
;
571 // Reserve the 1 Bytes with Oxff to identify the
572 // end of the variable buffer.
574 MaximumBufferSize
+= 1;
575 ValidBuffer
= AllocatePool (MaximumBufferSize
);
576 if (ValidBuffer
== NULL
) {
577 return EFI_OUT_OF_RESOURCES
;
580 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
583 // Copy variable store header.
585 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
586 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
589 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
591 Variable
= GetStartPointer (VariableStoreHeader
);
592 while (IsValidVariableHeader (Variable
)) {
593 NextVariable
= GetNextVariablePtr (Variable
);
594 if (Variable
->State
== VAR_ADDED
) {
595 if (UpdatingVariable
!= NULL
) {
596 if (UpdatingVariable
== Variable
) {
597 Variable
= NextVariable
;
601 VariableNameSize
= NameSizeOfVariable(Variable
);
602 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
604 VariableNamePtr
= GetVariableNamePtr (Variable
);
605 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
606 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
607 VariableNameSize
== UpdatingVariableNameSize
&&
608 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
609 Variable
= NextVariable
;
613 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
614 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
615 CurrPtr
+= VariableSize
;
616 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
617 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
618 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
619 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
622 Variable
= NextVariable
;
626 // Reinstall the variable being updated if it is not NULL.
628 if (UpdatingVariable
!= NULL
) {
629 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
630 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
631 CurrPtr
+= VariableSize
;
632 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
633 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
634 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
635 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
640 // Reinstall all in delete transition variables.
642 Variable
= GetStartPointer (VariableStoreHeader
);
643 while (IsValidVariableHeader (Variable
)) {
644 NextVariable
= GetNextVariablePtr (Variable
);
645 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
648 // Buffer has cached all ADDED variable.
649 // Per IN_DELETED variable, we have to guarantee that
650 // no ADDED one in previous buffer.
654 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
655 while (IsValidVariableHeader (AddedVariable
)) {
656 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
657 NameSize
= NameSizeOfVariable (AddedVariable
);
658 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
659 NameSize
== NameSizeOfVariable (Variable
)
661 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
662 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
663 if (CompareMem (Point0
, Point1
, NameSizeOfVariable (AddedVariable
)) == 0) {
668 AddedVariable
= NextAddedVariable
;
672 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
674 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
675 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
676 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
677 CurrPtr
+= VariableSize
;
678 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
679 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
680 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
681 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
686 Variable
= NextVariable
;
691 // If volatile variable store, just copy valid buffer.
693 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
694 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
695 Status
= EFI_SUCCESS
;
698 // If non-volatile variable store, perform FTW here.
700 Status
= FtwVariableSpace (
703 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
705 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
707 if (!EFI_ERROR (Status
)) {
708 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
710 *LastVariableOffset
= 0;
713 FreePool (ValidBuffer
);
719 Find the variable in the specified variable store.
721 @param[in] VariableName Name of the variable to be found
722 @param[in] VendorGuid Vendor GUID to be found.
723 @param[in] IgnoreRtAttribute Ignore RUNTIME_ACCESS attribute when searching variable.
724 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.
726 @retval EFI_SUCCESS Variable found successfully
727 @retval EFI_NOT_FOUND Variable not found
731 IN CHAR16
*VariableName
,
732 IN EFI_GUID
*VendorGuid
,
733 IN BOOLEAN IgnoreRtAttribute
,
734 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
737 VARIABLE_HEADER
*InDeletedVariable
;
741 // Find the variable by walk through HOB, volatile and non-volatile variable store.
743 InDeletedVariable
= NULL
;
745 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
746 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
747 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
749 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
750 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
752 if (IgnoreRtAttribute
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
753 if (VariableName
[0] == 0) {
754 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
755 InDeletedVariable
= PtrTrack
->CurrPtr
;
760 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
761 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
763 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
764 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
765 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
766 InDeletedVariable
= PtrTrack
->CurrPtr
;
777 PtrTrack
->CurrPtr
= InDeletedVariable
;
778 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
783 Finds variable in storage blocks of volatile and non-volatile storage areas.
785 This code finds variable in storage blocks of volatile and non-volatile storage areas.
786 If VariableName is an empty string, then we just return the first
787 qualified variable without comparing VariableName and VendorGuid.
788 If IgnoreRtAttribute is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS Attribute
789 when searching existing variable, only VariableName and VendorGuid are compared.
790 Otherwise, variables with EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
792 @param[in] VariableName Name of the variable to be found.
793 @param[in] VendorGuid Vendor GUID to be found.
794 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
795 including the range searched and the target position.
796 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
797 base of volatile variable storage area, base of
798 NV variable storage area, and a lock.
799 @param[in] IgnoreRtAttribute Ignore RUNTIME_ACCESS attribute when searching variable.
801 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
803 @retval EFI_SUCCESS Variable successfully found.
804 @retval EFI_NOT_FOUND Variable not found
809 IN CHAR16
*VariableName
,
810 IN EFI_GUID
*VendorGuid
,
811 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
812 IN VARIABLE_GLOBAL
*Global
,
813 IN BOOLEAN IgnoreRtAttribute
817 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
818 VARIABLE_STORE_TYPE Type
;
820 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
821 return EFI_INVALID_PARAMETER
;
825 // 0: Volatile, 1: HOB, 2: Non-Volatile.
826 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
827 // make use of this mapping to implement search algorithm.
829 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
830 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
831 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
834 // Find the variable by walk through HOB, volatile and non-volatile variable store.
836 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
837 if (VariableStoreHeader
[Type
] == NULL
) {
841 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
842 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
843 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
845 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtAttribute
, PtrTrack
);
846 if (!EFI_ERROR (Status
)) {
850 return EFI_NOT_FOUND
;
854 Get index from supported language codes according to language string.
856 This code is used to get corresponding index in supported language codes. It can handle
857 RFC4646 and ISO639 language tags.
858 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
859 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
862 SupportedLang = "engfraengfra"
864 Iso639Language = TRUE
865 The return value is "0".
867 SupportedLang = "en;fr;en-US;fr-FR"
869 Iso639Language = FALSE
870 The return value is "3".
872 @param SupportedLang Platform supported language codes.
873 @param Lang Configured language.
874 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
876 @retval The index of language in the language codes.
880 GetIndexFromSupportedLangCodes(
881 IN CHAR8
*SupportedLang
,
883 IN BOOLEAN Iso639Language
888 UINTN LanguageLength
;
890 if (Iso639Language
) {
891 CompareLength
= ISO_639_2_ENTRY_SIZE
;
892 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
893 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
895 // Successfully find the index of Lang string in SupportedLang string.
897 Index
= Index
/ CompareLength
;
905 // Compare RFC4646 language code
908 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
910 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
912 // Skip ';' characters in SupportedLang
914 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
916 // Determine the length of the next language code in SupportedLang
918 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
920 if ((CompareLength
== LanguageLength
) &&
921 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
923 // Successfully find the index of Lang string in SupportedLang string.
934 Get language string from supported language codes according to index.
936 This code is used to get corresponding language strings in supported language codes. It can handle
937 RFC4646 and ISO639 language tags.
938 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
939 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
942 SupportedLang = "engfraengfra"
944 Iso639Language = TRUE
945 The return value is "fra".
947 SupportedLang = "en;fr;en-US;fr-FR"
949 Iso639Language = FALSE
950 The return value is "fr".
952 @param SupportedLang Platform supported language codes.
953 @param Index The index in supported language codes.
954 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
956 @retval The language string in the language codes.
960 GetLangFromSupportedLangCodes (
961 IN CHAR8
*SupportedLang
,
963 IN BOOLEAN Iso639Language
971 Supported
= SupportedLang
;
972 if (Iso639Language
) {
974 // According to the index of Lang string in SupportedLang string to get the language.
975 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
976 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
978 CompareLength
= ISO_639_2_ENTRY_SIZE
;
979 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
980 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
985 // Take semicolon as delimitation, sequentially traverse supported language codes.
987 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
990 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
992 // Have completed the traverse, but not find corrsponding string.
993 // This case is not allowed to happen.
998 if (SubIndex
== Index
) {
1000 // According to the index of Lang string in SupportedLang string to get the language.
1001 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1002 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1004 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1005 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1010 // Skip ';' characters in Supported
1012 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1018 Returns a pointer to an allocated buffer that contains the best matching language
1019 from a set of supported languages.
1021 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1022 code types may not be mixed in a single call to this function. This function
1023 supports a variable argument list that allows the caller to pass in a prioritized
1024 list of language codes to test against all the language codes in SupportedLanguages.
1026 If SupportedLanguages is NULL, then ASSERT().
1028 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1029 contains a set of language codes in the format
1030 specified by Iso639Language.
1031 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1032 in ISO 639-2 format. If FALSE, then all language
1033 codes are assumed to be in RFC 4646 language format
1034 @param[in] ... A variable argument list that contains pointers to
1035 Null-terminated ASCII strings that contain one or more
1036 language codes in the format specified by Iso639Language.
1037 The first language code from each of these language
1038 code lists is used to determine if it is an exact or
1039 close match to any of the language codes in
1040 SupportedLanguages. Close matches only apply to RFC 4646
1041 language codes, and the matching algorithm from RFC 4647
1042 is used to determine if a close match is present. If
1043 an exact or close match is found, then the matching
1044 language code from SupportedLanguages is returned. If
1045 no matches are found, then the next variable argument
1046 parameter is evaluated. The variable argument list
1047 is terminated by a NULL.
1049 @retval NULL The best matching language could not be found in SupportedLanguages.
1050 @retval NULL There are not enough resources available to return the best matching
1052 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1053 language in SupportedLanguages.
1058 VariableGetBestLanguage (
1059 IN CONST CHAR8
*SupportedLanguages
,
1060 IN BOOLEAN Iso639Language
,
1066 UINTN CompareLength
;
1067 UINTN LanguageLength
;
1068 CONST CHAR8
*Supported
;
1071 if (SupportedLanguages
== NULL
) {
1075 VA_START (Args
, Iso639Language
);
1076 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1078 // Default to ISO 639-2 mode
1081 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1084 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1086 if (!Iso639Language
) {
1087 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1091 // Trim back the length of Language used until it is empty
1093 while (LanguageLength
> 0) {
1095 // Loop through all language codes in SupportedLanguages
1097 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1099 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1101 if (!Iso639Language
) {
1103 // Skip ';' characters in Supported
1105 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1107 // Determine the length of the next language code in Supported
1109 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1111 // If Language is longer than the Supported, then skip to the next language
1113 if (LanguageLength
> CompareLength
) {
1118 // See if the first LanguageLength characters in Supported match Language
1120 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1123 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1124 Buffer
[CompareLength
] = '\0';
1125 return CopyMem (Buffer
, Supported
, CompareLength
);
1129 if (Iso639Language
) {
1131 // If ISO 639 mode, then each language can only be tested once
1136 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1138 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1145 // No matches were found
1151 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1153 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1155 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1156 and are read-only. Therefore, in variable driver, only store the original value for other use.
1158 @param[in] VariableName Name of variable.
1160 @param[in] Data Variable data.
1162 @param[in] DataSize Size of data. 0 means delete.
1166 AutoUpdateLangVariable (
1167 IN CHAR16
*VariableName
,
1173 CHAR8
*BestPlatformLang
;
1177 VARIABLE_POINTER_TRACK Variable
;
1178 BOOLEAN SetLanguageCodes
;
1181 // Don't do updates for delete operation
1183 if (DataSize
== 0) {
1187 SetLanguageCodes
= FALSE
;
1189 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1191 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1197 SetLanguageCodes
= TRUE
;
1200 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1201 // Therefore, in variable driver, only store the original value for other use.
1203 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1204 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1206 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1207 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1210 // PlatformLang holds a single language from PlatformLangCodes,
1211 // so the size of PlatformLangCodes is enough for the PlatformLang.
1213 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1214 FreePool (mVariableModuleGlobal
->PlatformLang
);
1216 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1217 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1219 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1221 // LangCodes is a volatile variable, so it can not be updated at runtime.
1227 SetLanguageCodes
= TRUE
;
1230 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1231 // Therefore, in variable driver, only store the original value for other use.
1233 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1234 FreePool (mVariableModuleGlobal
->LangCodes
);
1236 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1237 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1240 if (SetLanguageCodes
1241 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1242 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1244 // Update Lang if PlatformLang is already set
1245 // Update PlatformLang if Lang is already set
1247 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1248 if (!EFI_ERROR (Status
)) {
1252 VariableName
= L
"PlatformLang";
1253 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1254 DataSize
= Variable
.CurrPtr
->DataSize
;
1256 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1257 if (!EFI_ERROR (Status
)) {
1259 // Update PlatformLang
1261 VariableName
= L
"Lang";
1262 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1263 DataSize
= Variable
.CurrPtr
->DataSize
;
1266 // Neither PlatformLang nor Lang is set, directly return
1274 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1276 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1278 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1280 // Update Lang when PlatformLangCodes/LangCodes were set.
1282 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1284 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1286 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1287 if (BestPlatformLang
!= NULL
) {
1289 // Get the corresponding index in language codes.
1291 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1294 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1296 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1299 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1301 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1303 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1304 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, 0, 0, &Variable
, NULL
);
1306 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1308 ASSERT_EFI_ERROR(Status
);
1312 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1314 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1316 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1318 // When setting Lang, firstly get most matched language string from supported language codes.
1320 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1321 if (BestLang
!= NULL
) {
1323 // Get the corresponding index in language codes.
1325 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1328 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1330 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1333 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1335 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1337 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1338 AsciiStrSize (BestPlatformLang
), Attributes
, 0, 0, &Variable
, NULL
);
1340 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1341 ASSERT_EFI_ERROR (Status
);
1348 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
1349 index of associated public key is needed.
1351 @param[in] VariableName Name of variable.
1352 @param[in] VendorGuid Guid of variable.
1353 @param[in] Data Variable data.
1354 @param[in] DataSize Size of data. 0 means delete.
1355 @param[in] Attributes Attributes of the variable.
1356 @param[in] KeyIndex Index of associated public key.
1357 @param[in] MonotonicCount Value of associated monotonic count.
1358 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1359 @param[in] TimeStamp Value of associated TimeStamp.
1361 @retval EFI_SUCCESS The update operation is success.
1362 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1367 IN CHAR16
*VariableName
,
1368 IN EFI_GUID
*VendorGuid
,
1371 IN UINT32 Attributes OPTIONAL
,
1372 IN UINT32 KeyIndex OPTIONAL
,
1373 IN UINT64 MonotonicCount OPTIONAL
,
1374 IN VARIABLE_POINTER_TRACK
*CacheVariable
,
1375 IN EFI_TIME
*TimeStamp OPTIONAL
1379 VARIABLE_HEADER
*NextVariable
;
1381 UINTN ScratchDataSize
;
1382 UINTN NonVolatileVarableStoreSize
;
1383 UINTN VarNameOffset
;
1384 UINTN VarDataOffset
;
1388 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1391 VARIABLE_POINTER_TRACK
*Variable
;
1392 VARIABLE_POINTER_TRACK NvVariable
;
1393 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1399 if (mVariableModuleGlobal
->FvbInstance
== NULL
) {
1401 // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
1403 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1405 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1407 return EFI_NOT_AVAILABLE_YET
;
1408 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1410 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1411 // The authenticated variable perhaps is not initialized, just return here.
1413 return EFI_NOT_AVAILABLE_YET
;
1417 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1418 Variable
= CacheVariable
;
1421 // Update/Delete existing NV variable.
1422 // CacheVariable points to the variable in the memory copy of Flash area
1423 // Now let Variable points to the same variable in Flash area.
1425 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1426 Variable
= &NvVariable
;
1427 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1428 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1429 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1430 Variable
->Volatile
= FALSE
;
1433 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1437 // Tricky part: Use scratch data area at the end of volatile variable store
1438 // as a temporary storage.
1440 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1441 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1442 ScratchDataSize
= ScratchSize
- sizeof (VARIABLE_HEADER
) - StrSize (VariableName
) - GET_PAD_SIZE (StrSize (VariableName
));
1444 if (Variable
->CurrPtr
!= NULL
) {
1446 // Update/Delete existing variable.
1450 // If AtRuntime and the variable is Volatile and Runtime Access,
1451 // the volatile is ReadOnly, and SetVariable should be aborted and
1452 // return EFI_WRITE_PROTECTED.
1454 if (Variable
->Volatile
) {
1455 Status
= EFI_WRITE_PROTECTED
;
1459 // Only variable that have NV attributes can be updated/deleted in Runtime.
1461 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1462 Status
= EFI_INVALID_PARAMETER
;
1467 // Only variable that have RT attributes can be updated/deleted in Runtime.
1469 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) {
1470 Status
= EFI_INVALID_PARAMETER
;
1476 // Setting a data variable with no access, or zero DataSize attributes
1477 // causes it to be deleted.
1478 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
1479 // not delete the variable.
1481 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0))|| ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
1482 State
= Variable
->CurrPtr
->State
;
1483 State
&= VAR_DELETED
;
1485 Status
= UpdateVariableStore (
1486 &mVariableModuleGlobal
->VariableGlobal
,
1490 (UINTN
) &Variable
->CurrPtr
->State
,
1494 if (!EFI_ERROR (Status
)) {
1495 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1496 if (!Variable
->Volatile
) {
1497 CacheVariable
->CurrPtr
->State
= State
;
1503 // If the variable is marked valid, and the same data has been passed in,
1504 // then return to the caller immediately.
1506 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1507 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0) &&
1508 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) &&
1509 (TimeStamp
== NULL
)) {
1511 // Variable content unchanged and no need to update timestamp, just return.
1513 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1514 Status
= EFI_SUCCESS
;
1516 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1517 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1520 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
1522 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
1524 // Cache the previous variable data into StorageArea.
1526 DataOffset
= sizeof (VARIABLE_HEADER
) + Variable
->CurrPtr
->NameSize
+ GET_PAD_SIZE (Variable
->CurrPtr
->NameSize
);
1527 CopyMem (mStorageArea
, (UINT8
*)((UINTN
) Variable
->CurrPtr
+ DataOffset
), Variable
->CurrPtr
->DataSize
);
1529 if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) ||
1530 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0))) {
1532 // For variables with the GUID EFI_IMAGE_SECURITY_DATABASE_GUID (i.e. where the data
1533 // buffer is formatted as EFI_SIGNATURE_LIST), the driver shall not perform an append of
1534 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
1536 BufSize
= AppendSignatureList (mStorageArea
, Variable
->CurrPtr
->DataSize
, Data
, DataSize
);
1537 if (BufSize
== Variable
->CurrPtr
->DataSize
) {
1538 if ((TimeStamp
== NULL
) || CompareTimeStamp (TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
1540 // New EFI_SIGNATURE_DATA is not found and timestamp is not later
1541 // than current timestamp, return EFI_SUCCESS directly.
1543 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1544 Status
= EFI_SUCCESS
;
1550 // For other Variables, append the new data to the end of previous data.
1552 CopyMem ((UINT8
*)((UINTN
) mStorageArea
+ Variable
->CurrPtr
->DataSize
), Data
, DataSize
);
1553 BufSize
= Variable
->CurrPtr
->DataSize
+ DataSize
;
1556 RevBufSize
= MIN (PcdGet32 (PcdMaxVariableSize
), ScratchDataSize
);
1557 if (BufSize
> RevBufSize
) {
1559 // If variable size (previous + current) is bigger than reserved buffer in runtime,
1560 // return EFI_OUT_OF_RESOURCES.
1562 return EFI_OUT_OF_RESOURCES
;
1566 // Override Data and DataSize which are used for combined data area including previous and new data.
1568 Data
= mStorageArea
;
1573 // Mark the old variable as in delete transition.
1575 State
= Variable
->CurrPtr
->State
;
1576 State
&= VAR_IN_DELETED_TRANSITION
;
1578 Status
= UpdateVariableStore (
1579 &mVariableModuleGlobal
->VariableGlobal
,
1583 (UINTN
) &Variable
->CurrPtr
->State
,
1587 if (EFI_ERROR (Status
)) {
1590 if (!Variable
->Volatile
) {
1591 CacheVariable
->CurrPtr
->State
= State
;
1596 // Not found existing variable. Create a new variable.
1599 if ((DataSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
1600 Status
= EFI_SUCCESS
;
1605 // Make sure we are trying to create a new variable.
1606 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1608 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1609 Status
= EFI_NOT_FOUND
;
1614 // Only variable have NV|RT attribute can be created in Runtime.
1617 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1618 Status
= EFI_INVALID_PARAMETER
;
1624 // Function part - create a new variable and copy the data.
1625 // Both update a variable and create a variable will come here.
1627 SetMem (NextVariable
, ScratchSize
, 0xff);
1629 NextVariable
->StartId
= VARIABLE_DATA
;
1631 // NextVariable->State = VAR_ADDED;
1633 NextVariable
->Reserved
= 0;
1634 NextVariable
->PubKeyIndex
= KeyIndex
;
1635 NextVariable
->MonotonicCount
= MonotonicCount
;
1636 ZeroMem (&NextVariable
->TimeStamp
, sizeof (EFI_TIME
));
1638 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1639 (TimeStamp
!= NULL
)) {
1640 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
1641 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1644 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
1645 // when the new TimeStamp value is later than the current timestamp associated
1646 // with the variable, we need associate the new timestamp with the updated value.
1648 if (Variable
->CurrPtr
!= NULL
) {
1649 if (CompareTimeStamp (&Variable
->CurrPtr
->TimeStamp
, TimeStamp
)) {
1650 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1657 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
1658 // Attributes bitmask parameter of a GetVariable() call.
1660 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
1662 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1663 VarNameSize
= StrSize (VariableName
);
1665 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1669 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1671 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1675 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1677 // There will be pad bytes after Data, the NextVariable->NameSize and
1678 // NextVariable->DataSize should not include pad size so that variable
1679 // service can get actual size in GetVariable.
1681 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1682 NextVariable
->DataSize
= (UINT32
)DataSize
;
1685 // The actual size of the variable that stores in storage should
1686 // include pad size.
1688 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1689 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1691 // Create a nonvolatile variable.
1694 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1695 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1696 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1697 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1698 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1700 Status
= EFI_OUT_OF_RESOURCES
;
1704 // Perform garbage collection & reclaim operation.
1706 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1707 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
->CurrPtr
);
1708 if (EFI_ERROR (Status
)) {
1712 // If still no enough space, return out of resources.
1714 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1715 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1716 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1717 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1718 Status
= EFI_OUT_OF_RESOURCES
;
1725 // 1. Write variable header
1726 // 2. Set variable state to header valid
1727 // 3. Write variable data
1728 // 4. Set variable state to valid
1733 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1734 Status
= UpdateVariableStore (
1735 &mVariableModuleGlobal
->VariableGlobal
,
1739 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1740 sizeof (VARIABLE_HEADER
),
1741 (UINT8
*) NextVariable
1744 if (EFI_ERROR (Status
)) {
1751 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1752 Status
= UpdateVariableStore (
1753 &mVariableModuleGlobal
->VariableGlobal
,
1757 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1759 &NextVariable
->State
1762 if (EFI_ERROR (Status
)) {
1768 Status
= UpdateVariableStore (
1769 &mVariableModuleGlobal
->VariableGlobal
,
1773 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1774 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1775 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1778 if (EFI_ERROR (Status
)) {
1784 NextVariable
->State
= VAR_ADDED
;
1785 Status
= UpdateVariableStore (
1786 &mVariableModuleGlobal
->VariableGlobal
,
1790 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1792 &NextVariable
->State
1795 if (EFI_ERROR (Status
)) {
1799 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1801 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1802 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1804 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1807 // update the memory copy of Flash region.
1809 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1812 // Create a volatile variable.
1816 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1817 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1819 // Perform garbage collection & reclaim operation.
1821 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1822 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
->CurrPtr
);
1823 if (EFI_ERROR (Status
)) {
1827 // If still no enough space, return out of resources.
1829 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1830 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1832 Status
= EFI_OUT_OF_RESOURCES
;
1838 NextVariable
->State
= VAR_ADDED
;
1839 Status
= UpdateVariableStore (
1840 &mVariableModuleGlobal
->VariableGlobal
,
1844 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1846 (UINT8
*) NextVariable
1849 if (EFI_ERROR (Status
)) {
1853 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1857 // Mark the old variable as deleted.
1859 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1860 State
= Variable
->CurrPtr
->State
;
1861 State
&= VAR_DELETED
;
1863 Status
= UpdateVariableStore (
1864 &mVariableModuleGlobal
->VariableGlobal
,
1868 (UINTN
) &Variable
->CurrPtr
->State
,
1872 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1873 CacheVariable
->CurrPtr
->State
= State
;
1877 if (!EFI_ERROR (Status
)) {
1878 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1887 This code finds variable in storage blocks (Volatile or Non-Volatile).
1889 @param VariableName Name of Variable to be found.
1890 @param VendorGuid Variable vendor GUID.
1891 @param Attributes Attribute value of the variable found.
1892 @param DataSize Size of Data found. If size is less than the
1893 data, this value contains the required size.
1894 @param Data Data pointer.
1896 @return EFI_INVALID_PARAMETER Invalid parameter.
1897 @return EFI_SUCCESS Find the specified variable.
1898 @return EFI_NOT_FOUND Not found.
1899 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1904 VariableServiceGetVariable (
1905 IN CHAR16
*VariableName
,
1906 IN EFI_GUID
*VendorGuid
,
1907 OUT UINT32
*Attributes OPTIONAL
,
1908 IN OUT UINTN
*DataSize
,
1913 VARIABLE_POINTER_TRACK Variable
;
1916 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1917 return EFI_INVALID_PARAMETER
;
1920 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1922 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1923 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1930 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1931 ASSERT (VarDataSize
!= 0);
1933 if (*DataSize
>= VarDataSize
) {
1935 Status
= EFI_INVALID_PARAMETER
;
1939 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1940 if (Attributes
!= NULL
) {
1941 *Attributes
= Variable
.CurrPtr
->Attributes
;
1944 *DataSize
= VarDataSize
;
1945 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1947 Status
= EFI_SUCCESS
;
1950 *DataSize
= VarDataSize
;
1951 Status
= EFI_BUFFER_TOO_SMALL
;
1956 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1964 This code Finds the Next available variable.
1966 @param VariableNameSize Size of the variable name.
1967 @param VariableName Pointer to variable name.
1968 @param VendorGuid Variable Vendor Guid.
1970 @return EFI_INVALID_PARAMETER Invalid parameter.
1971 @return EFI_SUCCESS Find the specified variable.
1972 @return EFI_NOT_FOUND Not found.
1973 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1978 VariableServiceGetNextVariableName (
1979 IN OUT UINTN
*VariableNameSize
,
1980 IN OUT CHAR16
*VariableName
,
1981 IN OUT EFI_GUID
*VendorGuid
1984 VARIABLE_STORE_TYPE Type
;
1985 VARIABLE_POINTER_TRACK Variable
;
1986 VARIABLE_POINTER_TRACK VariableInHob
;
1989 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1991 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1992 return EFI_INVALID_PARAMETER
;
1995 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1997 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1998 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2002 if (VariableName
[0] != 0) {
2004 // If variable name is not NULL, get next variable.
2006 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2010 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2011 // The index and attributes mapping must be kept in this order as FindVariable
2012 // makes use of this mapping to implement search algorithm.
2014 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2015 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2016 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2020 // Switch from Volatile to HOB, to Non-Volatile.
2022 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
2023 (Variable
.CurrPtr
== NULL
) ||
2024 !IsValidVariableHeader (Variable
.CurrPtr
)
2027 // Find current storage index
2029 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2030 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2034 ASSERT (Type
< VariableStoreTypeMax
);
2036 // Switch to next storage
2038 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2039 if (VariableStoreHeader
[Type
] != NULL
) {
2044 // Capture the case that
2045 // 1. current storage is the last one, or
2046 // 2. no further storage
2048 if (Type
== VariableStoreTypeMax
) {
2049 Status
= EFI_NOT_FOUND
;
2052 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2053 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2054 Variable
.CurrPtr
= Variable
.StartPtr
;
2058 // Variable is found
2060 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
2061 if ((AtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
2064 // Don't return NV variable when HOB overrides it
2066 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2067 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2069 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2070 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2071 Status
= FindVariableEx (
2072 GetVariableNamePtr (Variable
.CurrPtr
),
2073 &Variable
.CurrPtr
->VendorGuid
,
2077 if (!EFI_ERROR (Status
)) {
2078 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2083 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2084 ASSERT (VarNameSize
!= 0);
2086 if (VarNameSize
<= *VariableNameSize
) {
2087 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2088 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2089 Status
= EFI_SUCCESS
;
2091 Status
= EFI_BUFFER_TOO_SMALL
;
2094 *VariableNameSize
= VarNameSize
;
2099 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2103 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2109 This code sets variable in storage blocks (Volatile or Non-Volatile).
2111 @param VariableName Name of Variable to be found.
2112 @param VendorGuid Variable vendor GUID.
2113 @param Attributes Attribute value of the variable found
2114 @param DataSize Size of Data found. If size is less than the
2115 data, this value contains the required size.
2116 @param Data Data pointer.
2118 @return EFI_INVALID_PARAMETER Invalid parameter.
2119 @return EFI_SUCCESS Set successfully.
2120 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2121 @return EFI_NOT_FOUND Not found.
2122 @return EFI_WRITE_PROTECTED Variable is read-only.
2127 VariableServiceSetVariable (
2128 IN CHAR16
*VariableName
,
2129 IN EFI_GUID
*VendorGuid
,
2130 IN UINT32 Attributes
,
2135 VARIABLE_POINTER_TRACK Variable
;
2137 VARIABLE_HEADER
*NextVariable
;
2138 EFI_PHYSICAL_ADDRESS Point
;
2142 // Check input parameters.
2144 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2145 return EFI_INVALID_PARAMETER
;
2148 if (DataSize
!= 0 && Data
== NULL
) {
2149 return EFI_INVALID_PARAMETER
;
2153 // Make sure if runtime bit is set, boot service bit is set also.
2155 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2156 return EFI_INVALID_PARAMETER
;
2160 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
2161 // cannot be set both.
2163 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
2164 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
2165 return EFI_INVALID_PARAMETER
;
2168 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2169 if (DataSize
< AUTHINFO_SIZE
) {
2171 // Try to write Authenticated Variable without AuthInfo.
2173 return EFI_SECURITY_VIOLATION
;
2175 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2176 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
2178 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2180 if (DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
||
2181 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
)) ||
2182 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) {
2183 return EFI_SECURITY_VIOLATION
;
2185 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
2187 PayloadSize
= DataSize
;
2191 // The size of the VariableName, including the Unicode Null in bytes plus
2192 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2193 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2195 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2196 if ((PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
2197 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
2198 return EFI_INVALID_PARAMETER
;
2201 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX".
2203 if (StrnCmp(VariableName
, L
"HwErrRec", StrLen(L
"HwErrRec")) != 0) {
2204 return EFI_INVALID_PARAMETER
;
2208 // The size of the VariableName, including the Unicode Null in bytes plus
2209 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2211 if ((PayloadSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2212 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxVariableSize
))) {
2213 return EFI_INVALID_PARAMETER
;
2217 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2220 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2222 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2223 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2225 // Parse non-volatile variable data and get last variable offset.
2227 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2228 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2229 && IsValidVariableHeader (NextVariable
)) {
2230 NextVariable
= GetNextVariablePtr (NextVariable
);
2232 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2236 // Check whether the input variable is already existed.
2238 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2239 if (!EFI_ERROR (Status
)) {
2240 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2241 return EFI_WRITE_PROTECTED
;
2246 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2248 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2250 // Process PK, KEK, Sigdb seperately.
2252 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
2253 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, TRUE
);
2254 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
2255 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2256 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
2257 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) {
2258 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2260 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2263 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2264 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2271 This code returns information about the EFI variables.
2273 @param Attributes Attributes bitmask to specify the type of variables
2274 on which to return information.
2275 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2276 for the EFI variables associated with the attributes specified.
2277 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2278 for EFI variables associated with the attributes specified.
2279 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2280 associated with the attributes specified.
2282 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2283 @return EFI_SUCCESS Query successfully.
2284 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2289 VariableServiceQueryVariableInfo (
2290 IN UINT32 Attributes
,
2291 OUT UINT64
*MaximumVariableStorageSize
,
2292 OUT UINT64
*RemainingVariableStorageSize
,
2293 OUT UINT64
*MaximumVariableSize
2296 VARIABLE_HEADER
*Variable
;
2297 VARIABLE_HEADER
*NextVariable
;
2298 UINT64 VariableSize
;
2299 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2300 UINT64 CommonVariableTotalSize
;
2301 UINT64 HwErrVariableTotalSize
;
2303 CommonVariableTotalSize
= 0;
2304 HwErrVariableTotalSize
= 0;
2306 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2307 return EFI_INVALID_PARAMETER
;
2310 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2312 // Make sure the Attributes combination is supported by the platform.
2314 return EFI_UNSUPPORTED
;
2315 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2317 // Make sure if runtime bit is set, boot service bit is set also.
2319 return EFI_INVALID_PARAMETER
;
2320 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2322 // Make sure RT Attribute is set if we are in Runtime phase.
2324 return EFI_INVALID_PARAMETER
;
2325 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2327 // Make sure Hw Attribute is set with NV.
2329 return EFI_INVALID_PARAMETER
;
2332 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2334 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2336 // Query is Volatile related.
2338 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2341 // Query is Non-Volatile related.
2343 VariableStoreHeader
= mNvVariableCache
;
2347 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2348 // with the storage size (excluding the storage header size).
2350 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2353 // Harware error record variable needs larger size.
2355 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2356 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2357 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2359 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2360 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2361 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2365 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2367 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2371 // Point to the starting address of the variables.
2373 Variable
= GetStartPointer (VariableStoreHeader
);
2376 // Now walk through the related variable store.
2378 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2379 NextVariable
= GetNextVariablePtr (Variable
);
2380 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2384 // We don't take the state of the variables in mind
2385 // when calculating RemainingVariableStorageSize,
2386 // since the space occupied by variables not marked with
2387 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2389 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2390 HwErrVariableTotalSize
+= VariableSize
;
2392 CommonVariableTotalSize
+= VariableSize
;
2396 // Only care about Variables with State VAR_ADDED, because
2397 // the space not marked as VAR_ADDED is reclaimable now.
2399 if (Variable
->State
== VAR_ADDED
) {
2400 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2401 HwErrVariableTotalSize
+= VariableSize
;
2403 CommonVariableTotalSize
+= VariableSize
;
2409 // Go to the next one.
2411 Variable
= NextVariable
;
2414 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2415 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2417 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2420 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2421 *MaximumVariableSize
= 0;
2422 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2423 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2426 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2432 This function reclaims variable storage if free size is below the threshold.
2441 UINTN CommonVariableSpace
;
2442 UINTN RemainingCommonVariableSpace
;
2443 UINTN RemainingHwErrVariableSpace
;
2445 Status
= EFI_SUCCESS
;
2447 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2449 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2451 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2453 // Check if the free area is blow a threshold.
2455 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2456 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2457 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2459 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2460 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2464 ASSERT_EFI_ERROR (Status
);
2470 Initializes variable write service after FVB was ready.
2472 @retval EFI_SUCCESS Function successfully executed.
2473 @retval Others Fail to initialize the variable service.
2477 VariableWriteServiceInitialize (
2482 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2485 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2486 VARIABLE_HEADER
*Variable
;
2489 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2490 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2493 // Check if the free area is really free.
2495 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
2496 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2499 // There must be something wrong in variable store, do reclaim operation.
2502 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2503 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2507 if (EFI_ERROR (Status
)) {
2516 // Flush the HOB variable to flash and invalidate HOB variable.
2518 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2520 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB
2522 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2523 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
2525 for ( Variable
= GetStartPointer (VariableStoreHeader
)
2526 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
2527 ; Variable
= GetNextVariablePtr (Variable
)
2529 ASSERT (Variable
->State
== VAR_ADDED
);
2530 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
2531 VariableData
= GetVariableDataPtr (Variable
);
2532 Status
= VariableServiceSetVariable (
2533 GetVariableNamePtr (Variable
),
2534 &Variable
->VendorGuid
,
2535 Variable
->Attributes
,
2539 ASSERT_EFI_ERROR (Status
);
2544 // Authenticated variable initialize.
2546 Status
= AutenticatedVariableServiceInitialize ();
2553 Initializes variable store area for non-volatile and volatile variable.
2555 @retval EFI_SUCCESS Function successfully executed.
2556 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2560 VariableCommonInitialize (
2565 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2566 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2567 VARIABLE_HEADER
*NextVariable
;
2568 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2569 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2570 UINT64 VariableStoreLength
;
2573 EFI_HOB_GUID_TYPE
*GuidHob
;
2576 // Allocate runtime memory for variable driver global structure.
2578 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2579 if (mVariableModuleGlobal
== NULL
) {
2580 return EFI_OUT_OF_RESOURCES
;
2583 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2586 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2587 // is stored with common variable in the same NV region. So the platform integrator should
2588 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2589 // PcdFlashNvStorageVariableSize.
2591 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2594 // Get HOB variable store.
2596 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
2597 if (GuidHob
!= NULL
) {
2598 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
2599 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
2600 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
2602 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
2607 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2609 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2610 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2611 if (VolatileVariableStore
== NULL
) {
2612 FreePool (mVariableModuleGlobal
);
2613 return EFI_OUT_OF_RESOURCES
;
2616 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2619 // Initialize Variable Specific Data.
2621 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2622 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2623 mVariableModuleGlobal
->FvbInstance
= NULL
;
2625 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiAuthenticatedVariableGuid
);
2626 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2627 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2628 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2629 VolatileVariableStore
->Reserved
= 0;
2630 VolatileVariableStore
->Reserved1
= 0;
2633 // Get non-volatile variable store.
2636 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2637 if (TempVariableStoreHeader
== 0) {
2638 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2642 // Check if the Firmware Volume is not corrupted
2644 if ((((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->Signature
!= EFI_FVH_SIGNATURE
) ||
2645 (!CompareGuid (&gEfiSystemNvDataFvGuid
, &((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->FileSystemGuid
))) {
2646 Status
= EFI_VOLUME_CORRUPTED
;
2647 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
2651 VariableStoreBase
= TempVariableStoreHeader
+ \
2652 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2653 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2654 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2656 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2657 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2658 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2659 Status
= EFI_VOLUME_CORRUPTED
;
2660 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2663 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2666 // Parse non-volatile variable data and get last variable offset.
2668 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2669 while (IsValidVariableHeader (NextVariable
)) {
2670 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2671 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2672 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2674 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2677 NextVariable
= GetNextVariablePtr (NextVariable
);
2680 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2683 // Allocate runtime memory used for a memory copy of the FLASH region.
2684 // Keep the memory and the FLASH in sync as updates occur
2686 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2687 if (mNvVariableCache
== NULL
) {
2688 Status
= EFI_OUT_OF_RESOURCES
;
2691 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2692 Status
= EFI_SUCCESS
;
2695 if (EFI_ERROR (Status
)) {
2696 FreePool (mVariableModuleGlobal
);
2697 FreePool (VolatileVariableStore
);
2705 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2707 @param[in] Address The Flash address.
2708 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2709 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2713 GetFvbInfoByAddress (
2714 IN EFI_PHYSICAL_ADDRESS Address
,
2715 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2716 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2720 EFI_HANDLE
*HandleBuffer
;
2723 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2724 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2725 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2726 EFI_FVB_ATTRIBUTES_2 Attributes
;
2729 // Get all FVB handles.
2731 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2732 if (EFI_ERROR (Status
)) {
2733 return EFI_NOT_FOUND
;
2737 // Get the FVB to access variable store.
2740 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2741 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2742 if (EFI_ERROR (Status
)) {
2743 Status
= EFI_NOT_FOUND
;
2748 // Ensure this FVB protocol supported Write operation.
2750 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2751 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2756 // Compare the address and select the right one.
2758 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2759 if (EFI_ERROR (Status
)) {
2763 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2764 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
2765 if (FvbHandle
!= NULL
) {
2766 *FvbHandle
= HandleBuffer
[Index
];
2768 if (FvbProtocol
!= NULL
) {
2771 Status
= EFI_SUCCESS
;
2775 FreePool (HandleBuffer
);
2778 Status
= EFI_NOT_FOUND
;