3 The common variable operation routines shared by DXE_RINTIME variable
4 module and DXE_SMM variable module.
6 Copyright (c) 2006 - 2011, 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 PtrTrack Variable Track Pointer structure that contains Variable Information.
723 @retval EFI_SUCCESS Variable found successfully
724 @retval EFI_NOT_FOUND Variable not found
728 IN CHAR16
*VariableName
,
729 IN EFI_GUID
*VendorGuid
,
730 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
733 VARIABLE_HEADER
*InDeletedVariable
;
737 // Find the variable by walk through HOB, volatile and non-volatile variable store.
739 InDeletedVariable
= NULL
;
741 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
742 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
743 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
745 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
746 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
748 if (!AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
749 if (VariableName
[0] == 0) {
750 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
751 InDeletedVariable
= PtrTrack
->CurrPtr
;
756 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
757 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
759 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
760 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
761 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
762 InDeletedVariable
= PtrTrack
->CurrPtr
;
773 PtrTrack
->CurrPtr
= InDeletedVariable
;
774 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
779 Finds variable in storage blocks of volatile and non-volatile storage areas.
781 This code finds variable in storage blocks of volatile and non-volatile storage areas.
782 If VariableName is an empty string, then we just return the first
783 qualified variable without comparing VariableName and VendorGuid.
784 Otherwise, VariableName and VendorGuid are compared.
786 @param VariableName Name of the variable to be found.
787 @param VendorGuid Vendor GUID to be found.
788 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
789 including the range searched and the target position.
790 @param Global Pointer to VARIABLE_GLOBAL structure, including
791 base of volatile variable storage area, base of
792 NV variable storage area, and a lock.
794 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
796 @retval EFI_SUCCESS Variable successfully found.
797 @retval EFI_NOT_FOUND Variable not found
802 IN CHAR16
*VariableName
,
803 IN EFI_GUID
*VendorGuid
,
804 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
805 IN VARIABLE_GLOBAL
*Global
809 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
810 VARIABLE_STORE_TYPE Type
;
812 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
813 return EFI_INVALID_PARAMETER
;
817 // 0: Volatile, 1: HOB, 2: Non-Volatile.
818 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
819 // make use of this mapping to implement search algorithm.
821 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
822 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
823 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
826 // Find the variable by walk through HOB, volatile and non-volatile variable store.
828 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
829 if (VariableStoreHeader
[Type
] == NULL
) {
833 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
834 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
835 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
837 Status
= FindVariableEx (VariableName
, VendorGuid
, PtrTrack
);
838 if (!EFI_ERROR (Status
)) {
842 return EFI_NOT_FOUND
;
846 Get index from supported language codes according to language string.
848 This code is used to get corresponding index in supported language codes. It can handle
849 RFC4646 and ISO639 language tags.
850 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
851 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
854 SupportedLang = "engfraengfra"
856 Iso639Language = TRUE
857 The return value is "0".
859 SupportedLang = "en;fr;en-US;fr-FR"
861 Iso639Language = FALSE
862 The return value is "3".
864 @param SupportedLang Platform supported language codes.
865 @param Lang Configured language.
866 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
868 @retval The index of language in the language codes.
872 GetIndexFromSupportedLangCodes(
873 IN CHAR8
*SupportedLang
,
875 IN BOOLEAN Iso639Language
880 UINTN LanguageLength
;
882 if (Iso639Language
) {
883 CompareLength
= ISO_639_2_ENTRY_SIZE
;
884 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
885 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
887 // Successfully find the index of Lang string in SupportedLang string.
889 Index
= Index
/ CompareLength
;
897 // Compare RFC4646 language code
900 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
902 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
904 // Skip ';' characters in SupportedLang
906 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
908 // Determine the length of the next language code in SupportedLang
910 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
912 if ((CompareLength
== LanguageLength
) &&
913 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
915 // Successfully find the index of Lang string in SupportedLang string.
926 Get language string from supported language codes according to index.
928 This code is used to get corresponding language strings in supported language codes. It can handle
929 RFC4646 and ISO639 language tags.
930 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
931 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
934 SupportedLang = "engfraengfra"
936 Iso639Language = TRUE
937 The return value is "fra".
939 SupportedLang = "en;fr;en-US;fr-FR"
941 Iso639Language = FALSE
942 The return value is "fr".
944 @param SupportedLang Platform supported language codes.
945 @param Index The index in supported language codes.
946 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
948 @retval The language string in the language codes.
952 GetLangFromSupportedLangCodes (
953 IN CHAR8
*SupportedLang
,
955 IN BOOLEAN Iso639Language
963 Supported
= SupportedLang
;
964 if (Iso639Language
) {
966 // According to the index of Lang string in SupportedLang string to get the language.
967 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
968 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
970 CompareLength
= ISO_639_2_ENTRY_SIZE
;
971 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
972 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
977 // Take semicolon as delimitation, sequentially traverse supported language codes.
979 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
982 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
984 // Have completed the traverse, but not find corrsponding string.
985 // This case is not allowed to happen.
990 if (SubIndex
== Index
) {
992 // According to the index of Lang string in SupportedLang string to get the language.
993 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
994 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
996 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
997 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1002 // Skip ';' characters in Supported
1004 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1010 Returns a pointer to an allocated buffer that contains the best matching language
1011 from a set of supported languages.
1013 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1014 code types may not be mixed in a single call to this function. This function
1015 supports a variable argument list that allows the caller to pass in a prioritized
1016 list of language codes to test against all the language codes in SupportedLanguages.
1018 If SupportedLanguages is NULL, then ASSERT().
1020 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1021 contains a set of language codes in the format
1022 specified by Iso639Language.
1023 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1024 in ISO 639-2 format. If FALSE, then all language
1025 codes are assumed to be in RFC 4646 language format
1026 @param[in] ... A variable argument list that contains pointers to
1027 Null-terminated ASCII strings that contain one or more
1028 language codes in the format specified by Iso639Language.
1029 The first language code from each of these language
1030 code lists is used to determine if it is an exact or
1031 close match to any of the language codes in
1032 SupportedLanguages. Close matches only apply to RFC 4646
1033 language codes, and the matching algorithm from RFC 4647
1034 is used to determine if a close match is present. If
1035 an exact or close match is found, then the matching
1036 language code from SupportedLanguages is returned. If
1037 no matches are found, then the next variable argument
1038 parameter is evaluated. The variable argument list
1039 is terminated by a NULL.
1041 @retval NULL The best matching language could not be found in SupportedLanguages.
1042 @retval NULL There are not enough resources available to return the best matching
1044 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1045 language in SupportedLanguages.
1050 VariableGetBestLanguage (
1051 IN CONST CHAR8
*SupportedLanguages
,
1052 IN BOOLEAN Iso639Language
,
1058 UINTN CompareLength
;
1059 UINTN LanguageLength
;
1060 CONST CHAR8
*Supported
;
1063 ASSERT (SupportedLanguages
!= NULL
);
1065 VA_START (Args
, Iso639Language
);
1066 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1068 // Default to ISO 639-2 mode
1071 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1074 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1076 if (!Iso639Language
) {
1077 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1081 // Trim back the length of Language used until it is empty
1083 while (LanguageLength
> 0) {
1085 // Loop through all language codes in SupportedLanguages
1087 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1089 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1091 if (!Iso639Language
) {
1093 // Skip ';' characters in Supported
1095 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1097 // Determine the length of the next language code in Supported
1099 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1101 // If Language is longer than the Supported, then skip to the next language
1103 if (LanguageLength
> CompareLength
) {
1108 // See if the first LanguageLength characters in Supported match Language
1110 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1113 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1114 Buffer
[CompareLength
] = '\0';
1115 return CopyMem (Buffer
, Supported
, CompareLength
);
1119 if (Iso639Language
) {
1121 // If ISO 639 mode, then each language can only be tested once
1126 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1128 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1135 // No matches were found
1141 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1143 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1145 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1146 and are read-only. Therefore, in variable driver, only store the original value for other use.
1148 @param[in] VariableName Name of variable.
1150 @param[in] Data Variable data.
1152 @param[in] DataSize Size of data. 0 means delete.
1156 AutoUpdateLangVariable(
1157 IN CHAR16
*VariableName
,
1163 CHAR8
*BestPlatformLang
;
1167 VARIABLE_POINTER_TRACK Variable
;
1168 BOOLEAN SetLanguageCodes
;
1171 // Don't do updates for delete operation
1173 if (DataSize
== 0) {
1177 SetLanguageCodes
= FALSE
;
1179 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1181 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1187 SetLanguageCodes
= TRUE
;
1190 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1191 // Therefore, in variable driver, only store the original value for other use.
1193 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1194 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1196 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1197 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1200 // PlatformLang holds a single language from PlatformLangCodes,
1201 // so the size of PlatformLangCodes is enough for the PlatformLang.
1203 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1204 FreePool (mVariableModuleGlobal
->PlatformLang
);
1206 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1207 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1209 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1211 // LangCodes is a volatile variable, so it can not be updated at runtime.
1217 SetLanguageCodes
= TRUE
;
1220 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1221 // Therefore, in variable driver, only store the original value for other use.
1223 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1224 FreePool (mVariableModuleGlobal
->LangCodes
);
1226 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1227 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1230 if (SetLanguageCodes
1231 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1232 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1234 // Update Lang if PlatformLang is already set
1235 // Update PlatformLang if Lang is already set
1237 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1238 if (!EFI_ERROR (Status
)) {
1242 VariableName
= L
"PlatformLang";
1243 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1244 DataSize
= Variable
.CurrPtr
->DataSize
;
1246 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1247 if (!EFI_ERROR (Status
)) {
1249 // Update PlatformLang
1251 VariableName
= L
"Lang";
1252 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1253 DataSize
= Variable
.CurrPtr
->DataSize
;
1256 // Neither PlatformLang nor Lang is set, directly return
1264 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1266 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1268 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1270 // Update Lang when PlatformLangCodes/LangCodes were set.
1272 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1274 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1276 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1277 if (BestPlatformLang
!= NULL
) {
1279 // Get the corresponding index in language codes.
1281 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1284 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1286 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1289 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1291 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
);
1293 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1294 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
1296 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1298 ASSERT_EFI_ERROR(Status
);
1302 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1304 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1306 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1308 // When setting Lang, firstly get most matched language string from supported language codes.
1310 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1311 if (BestLang
!= NULL
) {
1313 // Get the corresponding index in language codes.
1315 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1318 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1320 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1323 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1325 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1327 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1328 AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
1330 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1331 ASSERT_EFI_ERROR (Status
);
1338 Update the variable region with Variable information. These are the same
1339 arguments as the EFI Variable services.
1341 @param[in] VariableName Name of variable.
1342 @param[in] VendorGuid Guid of variable.
1343 @param[in] Data Variable data.
1344 @param[in] DataSize Size of data. 0 means delete.
1345 @param[in] Attributes Attribues of the variable.
1346 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1348 @retval EFI_SUCCESS The update operation is success.
1349 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1354 IN CHAR16
*VariableName
,
1355 IN EFI_GUID
*VendorGuid
,
1358 IN UINT32 Attributes OPTIONAL
,
1359 IN VARIABLE_POINTER_TRACK
*CacheVariable
1363 VARIABLE_HEADER
*NextVariable
;
1365 UINTN NonVolatileVarableStoreSize
;
1366 UINTN VarNameOffset
;
1367 UINTN VarDataOffset
;
1371 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1374 VARIABLE_POINTER_TRACK
*Variable
;
1375 VARIABLE_POINTER_TRACK NvVariable
;
1376 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1379 if ((mVariableModuleGlobal
->FvbInstance
== NULL
) && ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0)) {
1381 // The FVB protocol is not ready. Trying to update NV variable prior to the installation
1382 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
1384 return EFI_NOT_AVAILABLE_YET
;
1387 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1388 Variable
= CacheVariable
;
1391 // Update/Delete existing NV variable.
1392 // CacheVariable points to the variable in the memory copy of Flash area
1393 // Now let Variable points to the same variable in Flash area.
1395 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1396 Variable
= &NvVariable
;
1397 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1398 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1399 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1400 Variable
->Volatile
= FALSE
;
1403 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1406 if (Variable
->CurrPtr
!= NULL
) {
1408 // Update/Delete existing variable.
1412 // If AtRuntime and the variable is Volatile and Runtime Access,
1413 // the volatile is ReadOnly, and SetVariable should be aborted and
1414 // return EFI_WRITE_PROTECTED.
1416 if (Variable
->Volatile
) {
1417 Status
= EFI_WRITE_PROTECTED
;
1421 // Only variable that have NV attributes can be updated/deleted in Runtime.
1423 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1424 Status
= EFI_INVALID_PARAMETER
;
1430 // Setting a data variable with no access, or zero DataSize attributes
1431 // causes it to be deleted.
1433 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1434 State
= Variable
->CurrPtr
->State
;
1435 State
&= VAR_DELETED
;
1437 Status
= UpdateVariableStore (
1438 &mVariableModuleGlobal
->VariableGlobal
,
1442 (UINTN
) &Variable
->CurrPtr
->State
,
1446 if (!EFI_ERROR (Status
)) {
1447 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1448 if (!Variable
->Volatile
) {
1449 CacheVariable
->CurrPtr
->State
= State
;
1455 // If the variable is marked valid, and the same data has been passed in,
1456 // then return to the caller immediately.
1458 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1459 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0)) {
1461 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1462 Status
= EFI_SUCCESS
;
1464 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1465 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1468 // Mark the old variable as in delete transition.
1470 State
= Variable
->CurrPtr
->State
;
1471 State
&= VAR_IN_DELETED_TRANSITION
;
1473 Status
= UpdateVariableStore (
1474 &mVariableModuleGlobal
->VariableGlobal
,
1478 (UINTN
) &Variable
->CurrPtr
->State
,
1482 if (EFI_ERROR (Status
)) {
1485 if (!Variable
->Volatile
) {
1486 CacheVariable
->CurrPtr
->State
= State
;
1491 // Not found existing variable. Create a new variable.
1495 // Make sure we are trying to create a new variable.
1496 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1498 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1499 Status
= EFI_NOT_FOUND
;
1504 // Only variable have NV|RT attribute can be created in Runtime.
1507 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1508 Status
= EFI_INVALID_PARAMETER
;
1514 // Function part - create a new variable and copy the data.
1515 // Both update a variable and create a variable will come here.
1518 // Tricky part: Use scratch data area at the end of volatile variable store
1519 // as a temporary storage.
1521 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1522 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1524 SetMem (NextVariable
, ScratchSize
, 0xff);
1526 NextVariable
->StartId
= VARIABLE_DATA
;
1527 NextVariable
->Attributes
= Attributes
;
1529 // NextVariable->State = VAR_ADDED;
1531 NextVariable
->Reserved
= 0;
1532 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1533 VarNameSize
= StrSize (VariableName
);
1535 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1539 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1541 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1545 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1547 // There will be pad bytes after Data, the NextVariable->NameSize and
1548 // NextVariable->DataSize should not include pad size so that variable
1549 // service can get actual size in GetVariable.
1551 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1552 NextVariable
->DataSize
= (UINT32
)DataSize
;
1555 // The actual size of the variable that stores in storage should
1556 // include pad size.
1558 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1559 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1561 // Create a nonvolatile variable.
1564 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1565 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1566 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1567 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1568 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1570 Status
= EFI_OUT_OF_RESOURCES
;
1574 // Perform garbage collection & reclaim operation.
1576 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1577 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
->CurrPtr
);
1578 if (EFI_ERROR (Status
)) {
1582 // If still no enough space, return out of resources.
1584 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1585 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1586 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1587 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1588 Status
= EFI_OUT_OF_RESOURCES
;
1595 // 1. Write variable header
1596 // 2. Set variable state to header valid
1597 // 3. Write variable data
1598 // 4. Set variable state to valid
1603 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1604 Status
= UpdateVariableStore (
1605 &mVariableModuleGlobal
->VariableGlobal
,
1609 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1610 sizeof (VARIABLE_HEADER
),
1611 (UINT8
*) NextVariable
1614 if (EFI_ERROR (Status
)) {
1621 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1622 Status
= UpdateVariableStore (
1623 &mVariableModuleGlobal
->VariableGlobal
,
1627 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1629 &NextVariable
->State
1632 if (EFI_ERROR (Status
)) {
1638 Status
= UpdateVariableStore (
1639 &mVariableModuleGlobal
->VariableGlobal
,
1643 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1644 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1645 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1648 if (EFI_ERROR (Status
)) {
1654 NextVariable
->State
= VAR_ADDED
;
1655 Status
= UpdateVariableStore (
1656 &mVariableModuleGlobal
->VariableGlobal
,
1660 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1662 &NextVariable
->State
1665 if (EFI_ERROR (Status
)) {
1669 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1671 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1672 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1674 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1677 // update the memory copy of Flash region.
1679 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1682 // Create a volatile variable.
1686 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1687 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1689 // Perform garbage collection & reclaim operation.
1691 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1692 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
->CurrPtr
);
1693 if (EFI_ERROR (Status
)) {
1697 // If still no enough space, return out of resources.
1699 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1700 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1702 Status
= EFI_OUT_OF_RESOURCES
;
1708 NextVariable
->State
= VAR_ADDED
;
1709 Status
= UpdateVariableStore (
1710 &mVariableModuleGlobal
->VariableGlobal
,
1714 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1716 (UINT8
*) NextVariable
1719 if (EFI_ERROR (Status
)) {
1723 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1727 // Mark the old variable as deleted.
1729 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1730 State
= Variable
->CurrPtr
->State
;
1731 State
&= VAR_DELETED
;
1733 Status
= UpdateVariableStore (
1734 &mVariableModuleGlobal
->VariableGlobal
,
1738 (UINTN
) &Variable
->CurrPtr
->State
,
1742 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1743 CacheVariable
->CurrPtr
->State
= State
;
1747 if (!EFI_ERROR (Status
)) {
1748 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1757 This code finds variable in storage blocks (Volatile or Non-Volatile).
1759 @param VariableName Name of Variable to be found.
1760 @param VendorGuid Variable vendor GUID.
1761 @param Attributes Attribute value of the variable found.
1762 @param DataSize Size of Data found. If size is less than the
1763 data, this value contains the required size.
1764 @param Data Data pointer.
1766 @return EFI_INVALID_PARAMETER Invalid parameter.
1767 @return EFI_SUCCESS Find the specified variable.
1768 @return EFI_NOT_FOUND Not found.
1769 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1774 VariableServiceGetVariable (
1775 IN CHAR16
*VariableName
,
1776 IN EFI_GUID
*VendorGuid
,
1777 OUT UINT32
*Attributes OPTIONAL
,
1778 IN OUT UINTN
*DataSize
,
1783 VARIABLE_POINTER_TRACK Variable
;
1786 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1787 return EFI_INVALID_PARAMETER
;
1790 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1792 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1793 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1800 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1801 ASSERT (VarDataSize
!= 0);
1803 if (*DataSize
>= VarDataSize
) {
1805 Status
= EFI_INVALID_PARAMETER
;
1809 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1810 if (Attributes
!= NULL
) {
1811 *Attributes
= Variable
.CurrPtr
->Attributes
;
1814 *DataSize
= VarDataSize
;
1815 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1817 Status
= EFI_SUCCESS
;
1820 *DataSize
= VarDataSize
;
1821 Status
= EFI_BUFFER_TOO_SMALL
;
1826 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1834 This code Finds the Next available variable.
1836 @param VariableNameSize Size of the variable name.
1837 @param VariableName Pointer to variable name.
1838 @param VendorGuid Variable Vendor Guid.
1840 @return EFI_INVALID_PARAMETER Invalid parameter.
1841 @return EFI_SUCCESS Find the specified variable.
1842 @return EFI_NOT_FOUND Not found.
1843 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1848 VariableServiceGetNextVariableName (
1849 IN OUT UINTN
*VariableNameSize
,
1850 IN OUT CHAR16
*VariableName
,
1851 IN OUT EFI_GUID
*VendorGuid
1854 VARIABLE_STORE_TYPE Type
;
1855 VARIABLE_POINTER_TRACK Variable
;
1856 VARIABLE_POINTER_TRACK VariableInHob
;
1859 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1861 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1862 return EFI_INVALID_PARAMETER
;
1865 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1867 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1868 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1872 if (VariableName
[0] != 0) {
1874 // If variable name is not NULL, get next variable.
1876 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1880 // 0: Volatile, 1: HOB, 2: Non-Volatile.
1881 // The index and attributes mapping must be kept in this order as FindVariable
1882 // makes use of this mapping to implement search algorithm.
1884 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
1885 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
1886 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
1890 // Switch from Volatile to HOB, to Non-Volatile.
1892 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
1893 (Variable
.CurrPtr
== NULL
) ||
1894 !IsValidVariableHeader (Variable
.CurrPtr
)
1897 // Find current storage index
1899 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1900 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
1904 ASSERT (Type
< VariableStoreTypeMax
);
1906 // Switch to next storage
1908 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
1909 if (VariableStoreHeader
[Type
] != NULL
) {
1914 // Capture the case that
1915 // 1. current storage is the last one, or
1916 // 2. no further storage
1918 if (Type
== VariableStoreTypeMax
) {
1919 Status
= EFI_NOT_FOUND
;
1922 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
1923 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
1924 Variable
.CurrPtr
= Variable
.StartPtr
;
1928 // Variable is found
1930 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
1931 if ((AtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
1934 // Don't return NV variable when HOB overrides it
1936 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
1937 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
1939 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
1940 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
1941 Status
= FindVariableEx (
1942 GetVariableNamePtr (Variable
.CurrPtr
),
1943 &Variable
.CurrPtr
->VendorGuid
,
1946 if (!EFI_ERROR (Status
)) {
1947 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1952 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
1953 ASSERT (VarNameSize
!= 0);
1955 if (VarNameSize
<= *VariableNameSize
) {
1956 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
1957 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
1958 Status
= EFI_SUCCESS
;
1960 Status
= EFI_BUFFER_TOO_SMALL
;
1963 *VariableNameSize
= VarNameSize
;
1968 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1972 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1978 This code sets variable in storage blocks (Volatile or Non-Volatile).
1980 @param VariableName Name of Variable to be found.
1981 @param VendorGuid Variable vendor GUID.
1982 @param Attributes Attribute value of the variable found
1983 @param DataSize Size of Data found. If size is less than the
1984 data, this value contains the required size.
1985 @param Data Data pointer.
1987 @return EFI_INVALID_PARAMETER Invalid parameter.
1988 @return EFI_SUCCESS Set successfully.
1989 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
1990 @return EFI_NOT_FOUND Not found.
1991 @return EFI_WRITE_PROTECTED Variable is read-only.
1996 VariableServiceSetVariable (
1997 IN CHAR16
*VariableName
,
1998 IN EFI_GUID
*VendorGuid
,
1999 IN UINT32 Attributes
,
2004 VARIABLE_POINTER_TRACK Variable
;
2006 VARIABLE_HEADER
*NextVariable
;
2007 EFI_PHYSICAL_ADDRESS Point
;
2010 // Check input parameters.
2012 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2013 return EFI_INVALID_PARAMETER
;
2016 if (DataSize
!= 0 && Data
== NULL
) {
2017 return EFI_INVALID_PARAMETER
;
2021 // Not support authenticated variable write yet.
2023 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2024 return EFI_INVALID_PARAMETER
;
2028 // Make sure if runtime bit is set, boot service bit is set also.
2030 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2031 return EFI_INVALID_PARAMETER
;
2035 // The size of the VariableName, including the Unicode Null in bytes plus
2036 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2037 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2039 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2040 if ((DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
2041 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
2042 return EFI_INVALID_PARAMETER
;
2045 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX".
2047 if (StrnCmp(VariableName
, L
"HwErrRec", StrLen(L
"HwErrRec")) != 0) {
2048 return EFI_INVALID_PARAMETER
;
2052 // The size of the VariableName, including the Unicode Null in bytes plus
2053 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2055 if ((DataSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2056 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxVariableSize
))) {
2057 return EFI_INVALID_PARAMETER
;
2061 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2064 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2066 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2067 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2069 // Parse non-volatile variable data and get last variable offset.
2071 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2072 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2073 && IsValidVariableHeader (NextVariable
)) {
2074 NextVariable
= GetNextVariablePtr (NextVariable
);
2076 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2080 // Check whether the input variable is already existed.
2082 FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
2085 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2087 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2089 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
2091 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2092 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2099 This code returns information about the EFI variables.
2101 @param Attributes Attributes bitmask to specify the type of variables
2102 on which to return information.
2103 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2104 for the EFI variables associated with the attributes specified.
2105 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2106 for EFI variables associated with the attributes specified.
2107 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2108 associated with the attributes specified.
2110 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2111 @return EFI_SUCCESS Query successfully.
2112 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2117 VariableServiceQueryVariableInfo (
2118 IN UINT32 Attributes
,
2119 OUT UINT64
*MaximumVariableStorageSize
,
2120 OUT UINT64
*RemainingVariableStorageSize
,
2121 OUT UINT64
*MaximumVariableSize
2124 VARIABLE_HEADER
*Variable
;
2125 VARIABLE_HEADER
*NextVariable
;
2126 UINT64 VariableSize
;
2127 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2128 UINT64 CommonVariableTotalSize
;
2129 UINT64 HwErrVariableTotalSize
;
2131 CommonVariableTotalSize
= 0;
2132 HwErrVariableTotalSize
= 0;
2134 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2135 return EFI_INVALID_PARAMETER
;
2138 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2140 // Make sure the Attributes combination is supported by the platform.
2142 return EFI_UNSUPPORTED
;
2143 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2145 // Make sure if runtime bit is set, boot service bit is set also.
2147 return EFI_INVALID_PARAMETER
;
2148 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2150 // Make sure RT Attribute is set if we are in Runtime phase.
2152 return EFI_INVALID_PARAMETER
;
2153 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2155 // Make sure Hw Attribute is set with NV.
2157 return EFI_INVALID_PARAMETER
;
2158 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2160 // Not support authentiated variable write yet.
2162 return EFI_UNSUPPORTED
;
2165 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2167 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2169 // Query is Volatile related.
2171 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2174 // Query is Non-Volatile related.
2176 VariableStoreHeader
= mNvVariableCache
;
2180 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2181 // with the storage size (excluding the storage header size).
2183 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2186 // Harware error record variable needs larger size.
2188 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2189 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2190 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2192 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2193 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2194 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2198 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2200 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2204 // Point to the starting address of the variables.
2206 Variable
= GetStartPointer (VariableStoreHeader
);
2209 // Now walk through the related variable store.
2211 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2212 NextVariable
= GetNextVariablePtr (Variable
);
2213 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2217 // We don't take the state of the variables in mind
2218 // when calculating RemainingVariableStorageSize,
2219 // since the space occupied by variables not marked with
2220 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2222 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2223 HwErrVariableTotalSize
+= VariableSize
;
2225 CommonVariableTotalSize
+= VariableSize
;
2229 // Only care about Variables with State VAR_ADDED, because
2230 // the space not marked as VAR_ADDED is reclaimable now.
2232 if (Variable
->State
== VAR_ADDED
) {
2233 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2234 HwErrVariableTotalSize
+= VariableSize
;
2236 CommonVariableTotalSize
+= VariableSize
;
2242 // Go to the next one.
2244 Variable
= NextVariable
;
2247 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2248 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2250 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2253 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2254 *MaximumVariableSize
= 0;
2255 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2256 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2259 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2265 This function reclaims variable storage if free size is below the threshold.
2274 UINTN CommonVariableSpace
;
2275 UINTN RemainingCommonVariableSpace
;
2276 UINTN RemainingHwErrVariableSpace
;
2278 Status
= EFI_SUCCESS
;
2280 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2282 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2284 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2286 // Check if the free area is blow a threshold.
2288 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2289 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2290 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2292 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2293 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2297 ASSERT_EFI_ERROR (Status
);
2303 Initializes variable write service after FVB was ready.
2305 @retval EFI_SUCCESS Function successfully executed.
2306 @retval Others Fail to initialize the variable service.
2310 VariableWriteServiceInitialize (
2315 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2318 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2319 VARIABLE_HEADER
*Variable
;
2322 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2323 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2326 // Check if the free area is really free.
2328 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
2329 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2332 // There must be something wrong in variable store, do reclaim operation.
2335 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2336 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2340 if (EFI_ERROR (Status
)) {
2348 // Flush the HOB variable to flash and invalidate HOB variable.
2350 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2352 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB
2354 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2355 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
2357 for ( Variable
= GetStartPointer (VariableStoreHeader
)
2358 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
2359 ; Variable
= GetNextVariablePtr (Variable
)
2361 ASSERT (Variable
->State
== VAR_ADDED
);
2362 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
2363 VariableData
= GetVariableDataPtr (Variable
);
2364 Status
= VariableServiceSetVariable (
2365 GetVariableNamePtr (Variable
),
2366 &Variable
->VendorGuid
,
2367 Variable
->Attributes
,
2371 ASSERT_EFI_ERROR (Status
);
2379 Initializes variable store area for non-volatile and volatile variable.
2381 @retval EFI_SUCCESS Function successfully executed.
2382 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2386 VariableCommonInitialize (
2391 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2392 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2393 VARIABLE_HEADER
*NextVariable
;
2394 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2395 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2396 UINT64 VariableStoreLength
;
2399 EFI_HOB_GUID_TYPE
*GuidHob
;
2402 // Allocate runtime memory for variable driver global structure.
2404 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2405 if (mVariableModuleGlobal
== NULL
) {
2406 return EFI_OUT_OF_RESOURCES
;
2409 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2412 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2413 // is stored with common variable in the same NV region. So the platform integrator should
2414 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2415 // PcdFlashNvStorageVariableSize.
2417 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2420 // Get HOB variable store.
2422 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
2423 if (GuidHob
!= NULL
) {
2424 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) GET_GUID_HOB_DATA (GuidHob
);
2428 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2430 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2431 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2432 if (VolatileVariableStore
== NULL
) {
2433 FreePool (mVariableModuleGlobal
);
2434 return EFI_OUT_OF_RESOURCES
;
2437 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2440 // Initialize Variable Specific Data.
2442 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2443 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2444 mVariableModuleGlobal
->FvbInstance
= NULL
;
2446 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiVariableGuid
);
2447 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2448 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2449 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2450 VolatileVariableStore
->Reserved
= 0;
2451 VolatileVariableStore
->Reserved1
= 0;
2454 // Get non-volatile variable store.
2457 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2458 if (TempVariableStoreHeader
== 0) {
2459 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2461 VariableStoreBase
= TempVariableStoreHeader
+ \
2462 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2463 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2464 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2466 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2467 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2468 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2469 Status
= EFI_VOLUME_CORRUPTED
;
2470 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2473 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2476 // Parse non-volatile variable data and get last variable offset.
2478 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2479 while (IsValidVariableHeader (NextVariable
)) {
2480 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2481 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2482 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2484 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2487 NextVariable
= GetNextVariablePtr (NextVariable
);
2490 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2493 // Allocate runtime memory used for a memory copy of the FLASH region.
2494 // Keep the memory and the FLASH in sync as updates occur
2496 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2497 if (mNvVariableCache
== NULL
) {
2498 Status
= EFI_OUT_OF_RESOURCES
;
2501 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2502 Status
= EFI_SUCCESS
;
2505 if (EFI_ERROR (Status
)) {
2506 FreePool (mVariableModuleGlobal
);
2507 FreePool (VolatileVariableStore
);
2515 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2517 @param[in] Address The Flash address.
2518 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2519 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2523 GetFvbInfoByAddress (
2524 IN EFI_PHYSICAL_ADDRESS Address
,
2525 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2526 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2530 EFI_HANDLE
*HandleBuffer
;
2533 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2534 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2535 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2536 EFI_FVB_ATTRIBUTES_2 Attributes
;
2539 // Get all FVB handles.
2541 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2542 if (EFI_ERROR (Status
)) {
2543 return EFI_NOT_FOUND
;
2547 // Get the FVB to access variable store.
2550 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2551 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2552 if (EFI_ERROR (Status
)) {
2553 Status
= EFI_NOT_FOUND
;
2558 // Ensure this FVB protocol supported Write operation.
2560 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2561 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2566 // Compare the address and select the right one.
2568 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2569 if (EFI_ERROR (Status
)) {
2573 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2574 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
2575 if (FvbHandle
!= NULL
) {
2576 *FvbHandle
= HandleBuffer
[Index
];
2578 if (FvbProtocol
!= NULL
) {
2581 Status
= EFI_SUCCESS
;
2585 FreePool (HandleBuffer
);
2588 Status
= EFI_NOT_FOUND
;