3 The common variable operation routines shared by DXE_RUNTIME variable
4 module and DXE_SMM variable module.
6 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
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.
202 ASSERT (Fvb
!= NULL
);
203 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
204 ASSERT_EFI_ERROR (Status
);
206 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
208 // Data Pointer should point to the actual Address where data is to be
212 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
215 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
216 return EFI_INVALID_PARAMETER
;
220 // Data Pointer should point to the actual Address where data is to be
223 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
225 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
228 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
229 return EFI_INVALID_PARAMETER
;
233 // If Volatile Variable just do a simple mem copy.
235 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
240 // If we are here we are dealing with Non-Volatile Variables.
242 LinearOffset
= (UINTN
) FwVolHeader
;
243 CurrWritePtr
= (UINTN
) DataPtr
;
244 CurrWriteSize
= DataSize
;
248 if (CurrWritePtr
< LinearOffset
) {
249 return EFI_INVALID_PARAMETER
;
252 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
253 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
255 // Check to see if the Variable Writes are spanning through multiple
258 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
259 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
260 Status
= Fvb
->Write (
263 (UINTN
) (CurrWritePtr
- LinearOffset
),
269 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
270 Status
= Fvb
->Write (
273 (UINTN
) (CurrWritePtr
- LinearOffset
),
277 if (EFI_ERROR (Status
)) {
281 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
282 CurrBuffer
= CurrBuffer
+ Size
;
283 CurrWriteSize
= CurrWriteSize
- Size
;
287 LinearOffset
+= PtrBlockMapEntry
->Length
;
298 This code gets the current status of Variable Store.
300 @param VarStoreHeader Pointer to the Variable Store Header.
302 @retval EfiRaw Variable store status is raw.
303 @retval EfiValid Variable store status is valid.
304 @retval EfiInvalid Variable store status is invalid.
307 VARIABLE_STORE_STATUS
308 GetVariableStoreStatus (
309 IN VARIABLE_STORE_HEADER
*VarStoreHeader
312 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
) &&
313 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
314 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
318 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
319 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
320 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
321 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
322 VarStoreHeader
->Size
== 0xffffffff &&
323 VarStoreHeader
->Format
== 0xff &&
324 VarStoreHeader
->State
== 0xff
336 This code gets the size of name of variable.
338 @param Variable Pointer to the Variable Header.
340 @return UINTN Size of variable in bytes.
345 IN VARIABLE_HEADER
*Variable
348 if (Variable
->State
== (UINT8
) (-1) ||
349 Variable
->DataSize
== (UINT32
) (-1) ||
350 Variable
->NameSize
== (UINT32
) (-1) ||
351 Variable
->Attributes
== (UINT32
) (-1)) {
354 return (UINTN
) Variable
->NameSize
;
359 This code gets the size of variable data.
361 @param Variable Pointer to the Variable Header.
363 @return Size of variable in bytes.
368 IN VARIABLE_HEADER
*Variable
371 if (Variable
->State
== (UINT8
) (-1) ||
372 Variable
->DataSize
== (UINT32
) (-1) ||
373 Variable
->NameSize
== (UINT32
) (-1) ||
374 Variable
->Attributes
== (UINT32
) (-1)) {
377 return (UINTN
) Variable
->DataSize
;
382 This code gets the pointer to the variable name.
384 @param Variable Pointer to the Variable Header.
386 @return Pointer to Variable Name which is Unicode encoding.
391 IN VARIABLE_HEADER
*Variable
395 return (CHAR16
*) (Variable
+ 1);
400 This code gets the pointer to the variable data.
402 @param Variable Pointer to the Variable Header.
404 @return Pointer to Variable Data.
409 IN VARIABLE_HEADER
*Variable
415 // Be careful about pad size for alignment.
417 Value
= (UINTN
) GetVariableNamePtr (Variable
);
418 Value
+= NameSizeOfVariable (Variable
);
419 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
421 return (UINT8
*) Value
;
427 This code gets the pointer to the next variable header.
429 @param Variable Pointer to the Variable Header.
431 @return Pointer to next variable header.
436 IN VARIABLE_HEADER
*Variable
441 if (!IsValidVariableHeader (Variable
)) {
445 Value
= (UINTN
) GetVariableDataPtr (Variable
);
446 Value
+= DataSizeOfVariable (Variable
);
447 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
450 // Be careful about pad size for alignment.
452 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
457 Gets the pointer to the first variable header in given variable store area.
459 @param VarStoreHeader Pointer to the Variable Store Header.
461 @return Pointer to the first variable header.
466 IN VARIABLE_STORE_HEADER
*VarStoreHeader
470 // The end of variable store.
472 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
477 Gets the pointer to the end of the variable storage area.
479 This function gets pointer to the end of the variable storage
480 area, according to the input variable store header.
482 @param VarStoreHeader Pointer to the Variable Store Header.
484 @return Pointer to the end of the variable storage area.
489 IN VARIABLE_STORE_HEADER
*VarStoreHeader
493 // The end of variable store
495 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
501 Variable store garbage collection and reclaim operation.
503 @param VariableBase Base address of variable store.
504 @param LastVariableOffset Offset of last variable.
505 @param IsVolatile The variable store is volatile or not;
506 if it is non-volatile, need FTW.
507 @param UpdatingVariable Pointer to updating variable.
509 @return EFI_OUT_OF_RESOURCES
516 IN EFI_PHYSICAL_ADDRESS VariableBase
,
517 OUT UINTN
*LastVariableOffset
,
518 IN BOOLEAN IsVolatile
,
519 IN VARIABLE_HEADER
*UpdatingVariable
522 VARIABLE_HEADER
*Variable
;
523 VARIABLE_HEADER
*AddedVariable
;
524 VARIABLE_HEADER
*NextVariable
;
525 VARIABLE_HEADER
*NextAddedVariable
;
526 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
528 UINTN MaximumBufferSize
;
530 UINTN VariableNameSize
;
531 UINTN UpdatingVariableNameSize
;
538 CHAR16
*VariableNamePtr
;
539 CHAR16
*UpdatingVariableNamePtr
;
541 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
543 // Recalculate the total size of Common/HwErr type variables in non-volatile area.
546 mVariableModuleGlobal
->CommonVariableTotalSize
= 0;
547 mVariableModuleGlobal
->HwErrVariableTotalSize
= 0;
551 // Start Pointers for the variable.
553 Variable
= GetStartPointer (VariableStoreHeader
);
554 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
556 while (IsValidVariableHeader (Variable
)) {
557 NextVariable
= GetNextVariablePtr (Variable
);
558 if (Variable
->State
== VAR_ADDED
||
559 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
561 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
562 MaximumBufferSize
+= VariableSize
;
565 Variable
= NextVariable
;
569 // Reserve the 1 Bytes with Oxff to identify the
570 // end of the variable buffer.
572 MaximumBufferSize
+= 1;
573 ValidBuffer
= AllocatePool (MaximumBufferSize
);
574 if (ValidBuffer
== NULL
) {
575 return EFI_OUT_OF_RESOURCES
;
578 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
581 // Copy variable store header.
583 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
584 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
587 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
589 Variable
= GetStartPointer (VariableStoreHeader
);
590 while (IsValidVariableHeader (Variable
)) {
591 NextVariable
= GetNextVariablePtr (Variable
);
592 if (Variable
->State
== VAR_ADDED
) {
593 if (UpdatingVariable
!= NULL
) {
594 if (UpdatingVariable
== Variable
) {
595 Variable
= NextVariable
;
599 VariableNameSize
= NameSizeOfVariable(Variable
);
600 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
602 VariableNamePtr
= GetVariableNamePtr (Variable
);
603 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
604 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
605 VariableNameSize
== UpdatingVariableNameSize
&&
606 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
607 Variable
= NextVariable
;
611 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
612 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
613 CurrPtr
+= VariableSize
;
614 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
615 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
616 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
617 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
620 Variable
= NextVariable
;
624 // Reinstall the variable being updated if it is not NULL.
626 if (UpdatingVariable
!= NULL
) {
627 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
628 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
629 CurrPtr
+= VariableSize
;
630 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
631 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
632 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
633 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
638 // Reinstall all in delete transition variables.
640 Variable
= GetStartPointer (VariableStoreHeader
);
641 while (IsValidVariableHeader (Variable
)) {
642 NextVariable
= GetNextVariablePtr (Variable
);
643 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
646 // Buffer has cached all ADDED variable.
647 // Per IN_DELETED variable, we have to guarantee that
648 // no ADDED one in previous buffer.
652 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
653 while (IsValidVariableHeader (AddedVariable
)) {
654 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
655 NameSize
= NameSizeOfVariable (AddedVariable
);
656 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
657 NameSize
== NameSizeOfVariable (Variable
)
659 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
660 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
661 if (CompareMem (Point0
, Point1
, NameSizeOfVariable (AddedVariable
)) == 0) {
666 AddedVariable
= NextAddedVariable
;
670 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
672 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
673 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
674 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
675 CurrPtr
+= VariableSize
;
676 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
677 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
678 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
679 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
684 Variable
= NextVariable
;
689 // If volatile variable store, just copy valid buffer.
691 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
692 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
693 Status
= EFI_SUCCESS
;
696 // If non-volatile variable store, perform FTW here.
698 Status
= FtwVariableSpace (
701 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
703 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
705 if (!EFI_ERROR (Status
)) {
706 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
708 *LastVariableOffset
= 0;
711 FreePool (ValidBuffer
);
717 Find the variable in the specified variable store.
719 @param VariableName Name of the variable to be found
720 @param VendorGuid Vendor GUID to be found.
721 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
722 check at runtime when searching variable.
723 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
725 @retval EFI_SUCCESS Variable found successfully
726 @retval EFI_NOT_FOUND Variable not found
730 IN CHAR16
*VariableName
,
731 IN EFI_GUID
*VendorGuid
,
732 IN BOOLEAN IgnoreRtCheck
,
733 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
736 VARIABLE_HEADER
*InDeletedVariable
;
740 // Find the variable by walk through HOB, volatile and non-volatile variable store.
742 InDeletedVariable
= NULL
;
744 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
745 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
746 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
748 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
749 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
751 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
752 if (VariableName
[0] == 0) {
753 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
754 InDeletedVariable
= PtrTrack
->CurrPtr
;
759 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
760 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
762 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
763 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
764 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
765 InDeletedVariable
= PtrTrack
->CurrPtr
;
776 PtrTrack
->CurrPtr
= InDeletedVariable
;
777 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
782 Finds variable in storage blocks of volatile and non-volatile storage areas.
784 This code finds variable in storage blocks of volatile and non-volatile storage areas.
785 If VariableName is an empty string, then we just return the first
786 qualified variable without comparing VariableName and VendorGuid.
787 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
788 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
789 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
791 @param VariableName Name of the variable to be found.
792 @param VendorGuid Vendor GUID to be found.
793 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
794 including the range searched and the target position.
795 @param Global Pointer to VARIABLE_GLOBAL structure, including
796 base of volatile variable storage area, base of
797 NV variable storage area, and a lock.
798 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
799 check at runtime 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 IgnoreRtCheck
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
, IgnoreRtCheck
, 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 ASSERT (SupportedLanguages
!= NULL
);
1073 VA_START (Args
, Iso639Language
);
1074 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1076 // Default to ISO 639-2 mode
1079 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1082 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1084 if (!Iso639Language
) {
1085 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1089 // Trim back the length of Language used until it is empty
1091 while (LanguageLength
> 0) {
1093 // Loop through all language codes in SupportedLanguages
1095 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1097 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1099 if (!Iso639Language
) {
1101 // Skip ';' characters in Supported
1103 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1105 // Determine the length of the next language code in Supported
1107 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1109 // If Language is longer than the Supported, then skip to the next language
1111 if (LanguageLength
> CompareLength
) {
1116 // See if the first LanguageLength characters in Supported match Language
1118 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1121 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1122 Buffer
[CompareLength
] = '\0';
1123 return CopyMem (Buffer
, Supported
, CompareLength
);
1127 if (Iso639Language
) {
1129 // If ISO 639 mode, then each language can only be tested once
1134 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1136 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1143 // No matches were found
1149 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1151 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1153 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1154 and are read-only. Therefore, in variable driver, only store the original value for other use.
1156 @param[in] VariableName Name of variable.
1158 @param[in] Data Variable data.
1160 @param[in] DataSize Size of data. 0 means delete.
1164 AutoUpdateLangVariable (
1165 IN CHAR16
*VariableName
,
1171 CHAR8
*BestPlatformLang
;
1175 VARIABLE_POINTER_TRACK Variable
;
1176 BOOLEAN SetLanguageCodes
;
1179 // Don't do updates for delete operation
1181 if (DataSize
== 0) {
1185 SetLanguageCodes
= FALSE
;
1187 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1189 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1195 SetLanguageCodes
= TRUE
;
1198 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1199 // Therefore, in variable driver, only store the original value for other use.
1201 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1202 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1204 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1205 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1208 // PlatformLang holds a single language from PlatformLangCodes,
1209 // so the size of PlatformLangCodes is enough for the PlatformLang.
1211 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1212 FreePool (mVariableModuleGlobal
->PlatformLang
);
1214 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1215 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1217 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1219 // LangCodes is a volatile variable, so it can not be updated at runtime.
1225 SetLanguageCodes
= TRUE
;
1228 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1229 // Therefore, in variable driver, only store the original value for other use.
1231 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1232 FreePool (mVariableModuleGlobal
->LangCodes
);
1234 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1235 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1238 if (SetLanguageCodes
1239 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1240 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1242 // Update Lang if PlatformLang is already set
1243 // Update PlatformLang if Lang is already set
1245 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1246 if (!EFI_ERROR (Status
)) {
1250 VariableName
= L
"PlatformLang";
1251 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1252 DataSize
= Variable
.CurrPtr
->DataSize
;
1254 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1255 if (!EFI_ERROR (Status
)) {
1257 // Update PlatformLang
1259 VariableName
= L
"Lang";
1260 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1261 DataSize
= Variable
.CurrPtr
->DataSize
;
1264 // Neither PlatformLang nor Lang is set, directly return
1272 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1274 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1276 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1278 // Update Lang when PlatformLangCodes/LangCodes were set.
1280 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1282 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1284 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1285 if (BestPlatformLang
!= NULL
) {
1287 // Get the corresponding index in language codes.
1289 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1292 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1294 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1297 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1299 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
, FALSE
);
1301 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1302 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
1304 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1306 ASSERT_EFI_ERROR(Status
);
1310 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1312 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1314 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1316 // When setting Lang, firstly get most matched language string from supported language codes.
1318 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1319 if (BestLang
!= NULL
) {
1321 // Get the corresponding index in language codes.
1323 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1326 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1328 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1331 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1333 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1335 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1336 AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
1338 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1339 ASSERT_EFI_ERROR (Status
);
1346 Update the variable region with Variable information. These are the same
1347 arguments as the EFI Variable services.
1349 @param[in] VariableName Name of variable.
1350 @param[in] VendorGuid Guid of variable.
1351 @param[in] Data Variable data.
1352 @param[in] DataSize Size of data. 0 means delete.
1353 @param[in] Attributes Attribues of the variable.
1354 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1356 @retval EFI_SUCCESS The update operation is success.
1357 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1362 IN CHAR16
*VariableName
,
1363 IN EFI_GUID
*VendorGuid
,
1366 IN UINT32 Attributes OPTIONAL
,
1367 IN VARIABLE_POINTER_TRACK
*CacheVariable
1371 VARIABLE_HEADER
*NextVariable
;
1373 UINTN NonVolatileVarableStoreSize
;
1374 UINTN VarNameOffset
;
1375 UINTN VarDataOffset
;
1379 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1382 VARIABLE_POINTER_TRACK
*Variable
;
1383 VARIABLE_POINTER_TRACK NvVariable
;
1384 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1387 if ((mVariableModuleGlobal
->FvbInstance
== NULL
) && ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0)) {
1389 // The FVB protocol is not ready. Trying to update NV variable prior to the installation
1390 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
1392 return EFI_NOT_AVAILABLE_YET
;
1395 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1396 Variable
= CacheVariable
;
1399 // Update/Delete existing NV variable.
1400 // CacheVariable points to the variable in the memory copy of Flash area
1401 // Now let Variable points to the same variable in Flash area.
1403 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1404 Variable
= &NvVariable
;
1405 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1406 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1407 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1408 Variable
->Volatile
= FALSE
;
1411 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1414 if (Variable
->CurrPtr
!= NULL
) {
1416 // Update/Delete existing variable.
1420 // If AtRuntime and the variable is Volatile and Runtime Access,
1421 // the volatile is ReadOnly, and SetVariable should be aborted and
1422 // return EFI_WRITE_PROTECTED.
1424 if (Variable
->Volatile
) {
1425 Status
= EFI_WRITE_PROTECTED
;
1429 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.
1431 if (((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0)) {
1432 Status
= EFI_INVALID_PARAMETER
;
1438 // Setting a data variable with no access, or zero DataSize attributes
1439 // causes it to be deleted.
1441 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1442 State
= Variable
->CurrPtr
->State
;
1443 State
&= VAR_DELETED
;
1445 Status
= UpdateVariableStore (
1446 &mVariableModuleGlobal
->VariableGlobal
,
1450 (UINTN
) &Variable
->CurrPtr
->State
,
1454 if (!EFI_ERROR (Status
)) {
1455 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1456 if (!Variable
->Volatile
) {
1457 CacheVariable
->CurrPtr
->State
= State
;
1463 // If the variable is marked valid, and the same data has been passed in,
1464 // then return to the caller immediately.
1466 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1467 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0)) {
1469 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1470 Status
= EFI_SUCCESS
;
1472 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1473 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1476 // Mark the old variable as in delete transition.
1478 State
= Variable
->CurrPtr
->State
;
1479 State
&= VAR_IN_DELETED_TRANSITION
;
1481 Status
= UpdateVariableStore (
1482 &mVariableModuleGlobal
->VariableGlobal
,
1486 (UINTN
) &Variable
->CurrPtr
->State
,
1490 if (EFI_ERROR (Status
)) {
1493 if (!Variable
->Volatile
) {
1494 CacheVariable
->CurrPtr
->State
= State
;
1499 // Not found existing variable. Create a new variable.
1503 // Make sure we are trying to create a new variable.
1504 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1506 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1507 Status
= EFI_NOT_FOUND
;
1512 // Only variable have NV|RT attribute can be created in Runtime.
1515 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1516 Status
= EFI_INVALID_PARAMETER
;
1522 // Function part - create a new variable and copy the data.
1523 // Both update a variable and create a variable will come here.
1526 // Tricky part: Use scratch data area at the end of volatile variable store
1527 // as a temporary storage.
1529 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1530 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1532 SetMem (NextVariable
, ScratchSize
, 0xff);
1534 NextVariable
->StartId
= VARIABLE_DATA
;
1535 NextVariable
->Attributes
= Attributes
;
1537 // NextVariable->State = VAR_ADDED;
1539 NextVariable
->Reserved
= 0;
1540 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1541 VarNameSize
= StrSize (VariableName
);
1543 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1547 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1549 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1553 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1555 // There will be pad bytes after Data, the NextVariable->NameSize and
1556 // NextVariable->DataSize should not include pad size so that variable
1557 // service can get actual size in GetVariable.
1559 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1560 NextVariable
->DataSize
= (UINT32
)DataSize
;
1563 // The actual size of the variable that stores in storage should
1564 // include pad size.
1566 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1567 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1569 // Create a nonvolatile variable.
1572 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1573 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1574 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1575 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1576 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1578 Status
= EFI_OUT_OF_RESOURCES
;
1582 // Perform garbage collection & reclaim operation.
1584 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1585 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
->CurrPtr
);
1586 if (EFI_ERROR (Status
)) {
1590 // If still no enough space, return out of resources.
1592 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1593 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1594 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1595 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1596 Status
= EFI_OUT_OF_RESOURCES
;
1603 // 1. Write variable header
1604 // 2. Set variable state to header valid
1605 // 3. Write variable data
1606 // 4. Set variable state to valid
1611 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1612 Status
= UpdateVariableStore (
1613 &mVariableModuleGlobal
->VariableGlobal
,
1617 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1618 sizeof (VARIABLE_HEADER
),
1619 (UINT8
*) NextVariable
1622 if (EFI_ERROR (Status
)) {
1629 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1630 Status
= UpdateVariableStore (
1631 &mVariableModuleGlobal
->VariableGlobal
,
1635 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1637 &NextVariable
->State
1640 if (EFI_ERROR (Status
)) {
1646 Status
= UpdateVariableStore (
1647 &mVariableModuleGlobal
->VariableGlobal
,
1651 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1652 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1653 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1656 if (EFI_ERROR (Status
)) {
1662 NextVariable
->State
= VAR_ADDED
;
1663 Status
= UpdateVariableStore (
1664 &mVariableModuleGlobal
->VariableGlobal
,
1668 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1670 &NextVariable
->State
1673 if (EFI_ERROR (Status
)) {
1677 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1679 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1680 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1682 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1685 // update the memory copy of Flash region.
1687 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1690 // Create a volatile variable.
1694 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1695 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1697 // Perform garbage collection & reclaim operation.
1699 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1700 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
->CurrPtr
);
1701 if (EFI_ERROR (Status
)) {
1705 // If still no enough space, return out of resources.
1707 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1708 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1710 Status
= EFI_OUT_OF_RESOURCES
;
1716 NextVariable
->State
= VAR_ADDED
;
1717 Status
= UpdateVariableStore (
1718 &mVariableModuleGlobal
->VariableGlobal
,
1722 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1724 (UINT8
*) NextVariable
1727 if (EFI_ERROR (Status
)) {
1731 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1735 // Mark the old variable as deleted.
1737 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1738 State
= Variable
->CurrPtr
->State
;
1739 State
&= VAR_DELETED
;
1741 Status
= UpdateVariableStore (
1742 &mVariableModuleGlobal
->VariableGlobal
,
1746 (UINTN
) &Variable
->CurrPtr
->State
,
1750 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1751 CacheVariable
->CurrPtr
->State
= State
;
1755 if (!EFI_ERROR (Status
)) {
1756 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1764 Check if a Unicode character is a hexadecimal character.
1766 This function checks if a Unicode character is a
1767 hexadecimal character. The valid hexadecimal character is
1768 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
1771 @param Char The character to check against.
1773 @retval TRUE If the Char is a hexadecmial character.
1774 @retval FALSE If the Char is not a hexadecmial character.
1779 IsHexaDecimalDigitCharacter (
1783 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
1788 This code checks if variable is hardware error record variable or not.
1790 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
1791 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
1793 @param VariableName Pointer to variable name.
1794 @param VendorGuid Variable Vendor Guid.
1796 @retval TRUE Variable is hardware error record variable.
1797 @retval FALSE Variable is not hardware error record variable.
1802 IsHwErrRecVariable (
1803 IN CHAR16
*VariableName
,
1804 IN EFI_GUID
*VendorGuid
1807 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
1808 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
1809 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
1810 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
1811 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
1812 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
1813 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
1822 This code finds variable in storage blocks (Volatile or Non-Volatile).
1824 @param VariableName Name of Variable to be found.
1825 @param VendorGuid Variable vendor GUID.
1826 @param Attributes Attribute value of the variable found.
1827 @param DataSize Size of Data found. If size is less than the
1828 data, this value contains the required size.
1829 @param Data Data pointer.
1831 @return EFI_INVALID_PARAMETER Invalid parameter.
1832 @return EFI_SUCCESS Find the specified variable.
1833 @return EFI_NOT_FOUND Not found.
1834 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1839 VariableServiceGetVariable (
1840 IN CHAR16
*VariableName
,
1841 IN EFI_GUID
*VendorGuid
,
1842 OUT UINT32
*Attributes OPTIONAL
,
1843 IN OUT UINTN
*DataSize
,
1848 VARIABLE_POINTER_TRACK Variable
;
1851 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1852 return EFI_INVALID_PARAMETER
;
1855 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1857 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1858 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1865 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1866 ASSERT (VarDataSize
!= 0);
1868 if (*DataSize
>= VarDataSize
) {
1870 Status
= EFI_INVALID_PARAMETER
;
1874 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1875 if (Attributes
!= NULL
) {
1876 *Attributes
= Variable
.CurrPtr
->Attributes
;
1879 *DataSize
= VarDataSize
;
1880 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1882 Status
= EFI_SUCCESS
;
1885 *DataSize
= VarDataSize
;
1886 Status
= EFI_BUFFER_TOO_SMALL
;
1891 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1899 This code Finds the Next available variable.
1901 @param VariableNameSize Size of the variable name.
1902 @param VariableName Pointer to variable name.
1903 @param VendorGuid Variable Vendor Guid.
1905 @return EFI_INVALID_PARAMETER Invalid parameter.
1906 @return EFI_SUCCESS Find the specified variable.
1907 @return EFI_NOT_FOUND Not found.
1908 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1913 VariableServiceGetNextVariableName (
1914 IN OUT UINTN
*VariableNameSize
,
1915 IN OUT CHAR16
*VariableName
,
1916 IN OUT EFI_GUID
*VendorGuid
1919 VARIABLE_STORE_TYPE Type
;
1920 VARIABLE_POINTER_TRACK Variable
;
1921 VARIABLE_POINTER_TRACK VariableInHob
;
1924 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1926 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1927 return EFI_INVALID_PARAMETER
;
1930 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1932 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1933 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1937 if (VariableName
[0] != 0) {
1939 // If variable name is not NULL, get next variable.
1941 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1945 // 0: Volatile, 1: HOB, 2: Non-Volatile.
1946 // The index and attributes mapping must be kept in this order as FindVariable
1947 // makes use of this mapping to implement search algorithm.
1949 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
1950 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
1951 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
1955 // Switch from Volatile to HOB, to Non-Volatile.
1957 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
1958 (Variable
.CurrPtr
== NULL
) ||
1959 !IsValidVariableHeader (Variable
.CurrPtr
)
1962 // Find current storage index
1964 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1965 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
1969 ASSERT (Type
< VariableStoreTypeMax
);
1971 // Switch to next storage
1973 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
1974 if (VariableStoreHeader
[Type
] != NULL
) {
1979 // Capture the case that
1980 // 1. current storage is the last one, or
1981 // 2. no further storage
1983 if (Type
== VariableStoreTypeMax
) {
1984 Status
= EFI_NOT_FOUND
;
1987 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
1988 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
1989 Variable
.CurrPtr
= Variable
.StartPtr
;
1993 // Variable is found
1995 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
1996 if ((AtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
1999 // Don't return NV variable when HOB overrides it
2001 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2002 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2004 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2005 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2006 Status
= FindVariableEx (
2007 GetVariableNamePtr (Variable
.CurrPtr
),
2008 &Variable
.CurrPtr
->VendorGuid
,
2012 if (!EFI_ERROR (Status
)) {
2013 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2018 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2019 ASSERT (VarNameSize
!= 0);
2021 if (VarNameSize
<= *VariableNameSize
) {
2022 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2023 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2024 Status
= EFI_SUCCESS
;
2026 Status
= EFI_BUFFER_TOO_SMALL
;
2029 *VariableNameSize
= VarNameSize
;
2034 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2038 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2044 This code sets variable in storage blocks (Volatile or Non-Volatile).
2046 @param VariableName Name of Variable to be found.
2047 @param VendorGuid Variable vendor GUID.
2048 @param Attributes Attribute value of the variable found
2049 @param DataSize Size of Data found. If size is less than the
2050 data, this value contains the required size.
2051 @param Data Data pointer.
2053 @return EFI_INVALID_PARAMETER Invalid parameter.
2054 @return EFI_SUCCESS Set successfully.
2055 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2056 @return EFI_NOT_FOUND Not found.
2057 @return EFI_WRITE_PROTECTED Variable is read-only.
2062 VariableServiceSetVariable (
2063 IN CHAR16
*VariableName
,
2064 IN EFI_GUID
*VendorGuid
,
2065 IN UINT32 Attributes
,
2070 VARIABLE_POINTER_TRACK Variable
;
2072 VARIABLE_HEADER
*NextVariable
;
2073 EFI_PHYSICAL_ADDRESS Point
;
2076 // Check input parameters.
2078 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2079 return EFI_INVALID_PARAMETER
;
2082 if (DataSize
!= 0 && Data
== NULL
) {
2083 return EFI_INVALID_PARAMETER
;
2087 // Not support authenticated variable write yet.
2089 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2090 return EFI_INVALID_PARAMETER
;
2094 // Make sure if runtime bit is set, boot service bit is set also.
2096 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2097 return EFI_INVALID_PARAMETER
;
2101 // The size of the VariableName, including the Unicode Null in bytes plus
2102 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2103 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2105 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2106 if ((DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
2107 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
2108 return EFI_INVALID_PARAMETER
;
2110 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2111 return EFI_INVALID_PARAMETER
;
2115 // The size of the VariableName, including the Unicode Null in bytes plus
2116 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2118 if ((DataSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2119 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxVariableSize
))) {
2120 return EFI_INVALID_PARAMETER
;
2126 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence
2127 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.
2129 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, L
"HwErrRecSupport") == 0)) {
2130 return EFI_WRITE_PROTECTED
;
2134 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2137 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2139 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2140 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2142 // Parse non-volatile variable data and get last variable offset.
2144 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2145 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2146 && IsValidVariableHeader (NextVariable
)) {
2147 NextVariable
= GetNextVariablePtr (NextVariable
);
2149 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2153 // Check whether the input variable is already existed.
2155 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2156 if (!EFI_ERROR (Status
)) {
2157 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2158 return EFI_WRITE_PROTECTED
;
2163 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2165 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2167 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
2169 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2170 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2177 This code returns information about the EFI variables.
2179 @param Attributes Attributes bitmask to specify the type of variables
2180 on which to return information.
2181 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2182 for the EFI variables associated with the attributes specified.
2183 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2184 for EFI variables associated with the attributes specified.
2185 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2186 associated with the attributes specified.
2188 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2189 @return EFI_SUCCESS Query successfully.
2190 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2195 VariableServiceQueryVariableInfo (
2196 IN UINT32 Attributes
,
2197 OUT UINT64
*MaximumVariableStorageSize
,
2198 OUT UINT64
*RemainingVariableStorageSize
,
2199 OUT UINT64
*MaximumVariableSize
2202 VARIABLE_HEADER
*Variable
;
2203 VARIABLE_HEADER
*NextVariable
;
2204 UINT64 VariableSize
;
2205 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2206 UINT64 CommonVariableTotalSize
;
2207 UINT64 HwErrVariableTotalSize
;
2209 CommonVariableTotalSize
= 0;
2210 HwErrVariableTotalSize
= 0;
2212 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2213 return EFI_INVALID_PARAMETER
;
2216 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2218 // Make sure the Attributes combination is supported by the platform.
2220 return EFI_UNSUPPORTED
;
2221 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2223 // Make sure if runtime bit is set, boot service bit is set also.
2225 return EFI_INVALID_PARAMETER
;
2226 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2228 // Make sure RT Attribute is set if we are in Runtime phase.
2230 return EFI_INVALID_PARAMETER
;
2231 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2233 // Make sure Hw Attribute is set with NV.
2235 return EFI_INVALID_PARAMETER
;
2236 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2238 // Not support authentiated variable write yet.
2240 return EFI_UNSUPPORTED
;
2243 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2245 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2247 // Query is Volatile related.
2249 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2252 // Query is Non-Volatile related.
2254 VariableStoreHeader
= mNvVariableCache
;
2258 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2259 // with the storage size (excluding the storage header size).
2261 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2264 // Harware error record variable needs larger size.
2266 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2267 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2268 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2270 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2271 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2272 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2276 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2278 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2282 // Point to the starting address of the variables.
2284 Variable
= GetStartPointer (VariableStoreHeader
);
2287 // Now walk through the related variable store.
2289 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2290 NextVariable
= GetNextVariablePtr (Variable
);
2291 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2295 // We don't take the state of the variables in mind
2296 // when calculating RemainingVariableStorageSize,
2297 // since the space occupied by variables not marked with
2298 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2300 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2301 HwErrVariableTotalSize
+= VariableSize
;
2303 CommonVariableTotalSize
+= VariableSize
;
2307 // Only care about Variables with State VAR_ADDED, because
2308 // the space not marked as VAR_ADDED is reclaimable now.
2310 if (Variable
->State
== VAR_ADDED
) {
2311 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2312 HwErrVariableTotalSize
+= VariableSize
;
2314 CommonVariableTotalSize
+= VariableSize
;
2320 // Go to the next one.
2322 Variable
= NextVariable
;
2325 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2326 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2328 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2331 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2332 *MaximumVariableSize
= 0;
2333 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2334 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2337 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2343 This function reclaims variable storage if free size is below the threshold.
2352 UINTN CommonVariableSpace
;
2353 UINTN RemainingCommonVariableSpace
;
2354 UINTN RemainingHwErrVariableSpace
;
2356 Status
= EFI_SUCCESS
;
2358 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2360 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2362 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2364 // Check if the free area is blow a threshold.
2366 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2367 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2368 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2370 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2371 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2375 ASSERT_EFI_ERROR (Status
);
2381 Initializes variable write service after FVB was ready.
2383 @retval EFI_SUCCESS Function successfully executed.
2384 @retval Others Fail to initialize the variable service.
2388 VariableWriteServiceInitialize (
2393 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2396 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2397 VARIABLE_HEADER
*Variable
;
2400 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2401 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2404 // Check if the free area is really free.
2406 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
2407 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2410 // There must be something wrong in variable store, do reclaim operation.
2413 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2414 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2418 if (EFI_ERROR (Status
)) {
2426 // Flush the HOB variable to flash and invalidate HOB variable.
2428 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2430 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB
2432 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2433 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
2435 for ( Variable
= GetStartPointer (VariableStoreHeader
)
2436 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
2437 ; Variable
= GetNextVariablePtr (Variable
)
2439 ASSERT (Variable
->State
== VAR_ADDED
);
2440 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
2441 VariableData
= GetVariableDataPtr (Variable
);
2442 Status
= VariableServiceSetVariable (
2443 GetVariableNamePtr (Variable
),
2444 &Variable
->VendorGuid
,
2445 Variable
->Attributes
,
2449 ASSERT_EFI_ERROR (Status
);
2457 Initializes variable store area for non-volatile and volatile variable.
2459 @retval EFI_SUCCESS Function successfully executed.
2460 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2464 VariableCommonInitialize (
2469 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2470 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2471 VARIABLE_HEADER
*NextVariable
;
2472 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2473 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2474 UINT64 VariableStoreLength
;
2477 EFI_HOB_GUID_TYPE
*GuidHob
;
2480 // Allocate runtime memory for variable driver global structure.
2482 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2483 if (mVariableModuleGlobal
== NULL
) {
2484 return EFI_OUT_OF_RESOURCES
;
2487 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2490 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2491 // is stored with common variable in the same NV region. So the platform integrator should
2492 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2493 // PcdFlashNvStorageVariableSize.
2495 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2498 // Get HOB variable store.
2500 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
2501 if (GuidHob
!= NULL
) {
2502 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
2503 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
2504 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
2506 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
2511 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2513 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2514 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2515 if (VolatileVariableStore
== NULL
) {
2516 FreePool (mVariableModuleGlobal
);
2517 return EFI_OUT_OF_RESOURCES
;
2520 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2523 // Initialize Variable Specific Data.
2525 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2526 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2527 mVariableModuleGlobal
->FvbInstance
= NULL
;
2529 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiVariableGuid
);
2530 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2531 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2532 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2533 VolatileVariableStore
->Reserved
= 0;
2534 VolatileVariableStore
->Reserved1
= 0;
2537 // Get non-volatile variable store.
2540 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2541 if (TempVariableStoreHeader
== 0) {
2542 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2546 // Check if the Firmware Volume is not corrupted
2548 if ((((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->Signature
!= EFI_FVH_SIGNATURE
) ||
2549 (!CompareGuid (&gEfiSystemNvDataFvGuid
, &((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->FileSystemGuid
))) {
2550 Status
= EFI_VOLUME_CORRUPTED
;
2551 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
2555 VariableStoreBase
= TempVariableStoreHeader
+ \
2556 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2557 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2558 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2560 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2561 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2562 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2563 Status
= EFI_VOLUME_CORRUPTED
;
2564 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2567 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2570 // Parse non-volatile variable data and get last variable offset.
2572 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2573 while (IsValidVariableHeader (NextVariable
)) {
2574 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2575 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2576 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2578 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2581 NextVariable
= GetNextVariablePtr (NextVariable
);
2584 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2587 // Allocate runtime memory used for a memory copy of the FLASH region.
2588 // Keep the memory and the FLASH in sync as updates occur
2590 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2591 if (mNvVariableCache
== NULL
) {
2592 Status
= EFI_OUT_OF_RESOURCES
;
2595 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2596 Status
= EFI_SUCCESS
;
2599 if (EFI_ERROR (Status
)) {
2600 FreePool (mVariableModuleGlobal
);
2601 FreePool (VolatileVariableStore
);
2609 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2611 @param[in] Address The Flash address.
2612 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2613 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2617 GetFvbInfoByAddress (
2618 IN EFI_PHYSICAL_ADDRESS Address
,
2619 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2620 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2624 EFI_HANDLE
*HandleBuffer
;
2627 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2628 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2629 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2630 EFI_FVB_ATTRIBUTES_2 Attributes
;
2633 // Get all FVB handles.
2635 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2636 if (EFI_ERROR (Status
)) {
2637 return EFI_NOT_FOUND
;
2641 // Get the FVB to access variable store.
2644 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2645 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2646 if (EFI_ERROR (Status
)) {
2647 Status
= EFI_NOT_FOUND
;
2652 // Ensure this FVB protocol supported Write operation.
2654 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2655 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2660 // Compare the address and select the right one.
2662 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2663 if (EFI_ERROR (Status
)) {
2667 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2668 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
2669 if (FvbHandle
!= NULL
) {
2670 *FvbHandle
= HandleBuffer
[Index
];
2672 if (FvbProtocol
!= NULL
) {
2675 Status
= EFI_SUCCESS
;
2679 FreePool (HandleBuffer
);
2682 Status
= EFI_NOT_FOUND
;