3 The common variable operation routines shared by DXE_RUNTIME variable
4 module and DXE_SMM variable module.
6 Copyright (c) 2006 - 2013, 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
;
32 /// The list to store the variables which cannot be set after the EFI_END_OF_DXE_EVENT_GROUP_GUID
33 /// or EVT_GROUP_READY_TO_BOOT event.
35 LIST_ENTRY mLockedVariableList
= INITIALIZE_LIST_HEAD_VARIABLE (mLockedVariableList
);
38 /// The flag to indicate whether the platform has left the DXE phase of execution.
40 BOOLEAN mEndOfDxe
= FALSE
;
43 /// The flag to indicate whether the variable storage locking is enabled.
45 BOOLEAN mEnableLocking
= TRUE
;
49 Routine used to track statistical information about variable usage.
50 The data is stored in the EFI system table so it can be accessed later.
51 VariableInfo.efi can dump out the table. Only Boot Services variable
52 accesses are tracked by this code. The PcdVariableCollectStatistics
53 build flag controls if this feature is enabled.
55 A read that hits in the cache will have Read and Cache true for
56 the transaction. Data is allocated by this routine, but never
59 @param[in] VariableName Name of the Variable to track.
60 @param[in] VendorGuid Guid of the Variable to track.
61 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
62 @param[in] Read TRUE if GetVariable() was called.
63 @param[in] Write TRUE if SetVariable() was called.
64 @param[in] Delete TRUE if deleted via SetVariable().
65 @param[in] Cache TRUE for a cache hit.
70 IN CHAR16
*VariableName
,
71 IN EFI_GUID
*VendorGuid
,
79 VARIABLE_INFO_ENTRY
*Entry
;
81 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
84 // Don't collect statistics at runtime.
88 if (gVariableInfo
== NULL
) {
90 // On the first call allocate a entry and place a pointer to it in
91 // the EFI System Table.
93 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
94 ASSERT (gVariableInfo
!= NULL
);
96 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
97 gVariableInfo
->Name
= AllocatePool (StrSize (VariableName
));
98 ASSERT (gVariableInfo
->Name
!= NULL
);
99 StrCpy (gVariableInfo
->Name
, VariableName
);
100 gVariableInfo
->Volatile
= Volatile
;
104 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
105 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
106 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
114 Entry
->DeleteCount
++;
124 if (Entry
->Next
== NULL
) {
126 // If the entry is not in the table add it.
127 // Next iteration of the loop will fill in the data.
129 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
130 ASSERT (Entry
->Next
!= NULL
);
132 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
133 Entry
->Next
->Name
= AllocatePool (StrSize (VariableName
));
134 ASSERT (Entry
->Next
->Name
!= NULL
);
135 StrCpy (Entry
->Next
->Name
, VariableName
);
136 Entry
->Next
->Volatile
= Volatile
;
146 This code checks if variable header is valid or not.
148 @param Variable Pointer to the Variable Header.
150 @retval TRUE Variable header is valid.
151 @retval FALSE Variable header is not valid.
155 IsValidVariableHeader (
156 IN VARIABLE_HEADER
*Variable
159 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
169 This function writes data to the FWH at the correct LBA even if the LBAs
172 @param Global Pointer to VARAIBLE_GLOBAL structure.
173 @param Volatile Point out the Variable is Volatile or Non-Volatile.
174 @param SetByIndex TRUE if target pointer is given as index.
175 FALSE if target pointer is absolute.
176 @param Fvb Pointer to the writable FVB protocol.
177 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
179 @param DataSize Size of data to be written.
180 @param Buffer Pointer to the buffer from which data is written.
182 @retval EFI_INVALID_PARAMETER Parameters not valid.
183 @retval EFI_SUCCESS Variable store successfully updated.
187 UpdateVariableStore (
188 IN VARIABLE_GLOBAL
*Global
,
190 IN BOOLEAN SetByIndex
,
191 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
192 IN UINTN DataPtrIndex
,
197 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
205 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
206 VARIABLE_STORE_HEADER
*VolatileBase
;
207 EFI_PHYSICAL_ADDRESS FvVolHdr
;
208 EFI_PHYSICAL_ADDRESS DataPtr
;
212 DataPtr
= DataPtrIndex
;
215 // Check if the Data is Volatile.
218 ASSERT (Fvb
!= NULL
);
219 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
220 ASSERT_EFI_ERROR (Status
);
222 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
224 // Data Pointer should point to the actual Address where data is to be
228 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
231 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
232 return EFI_INVALID_PARAMETER
;
236 // Data Pointer should point to the actual Address where data is to be
239 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
241 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
244 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
245 return EFI_INVALID_PARAMETER
;
249 // If Volatile Variable just do a simple mem copy.
251 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
256 // If we are here we are dealing with Non-Volatile Variables.
258 LinearOffset
= (UINTN
) FwVolHeader
;
259 CurrWritePtr
= (UINTN
) DataPtr
;
260 CurrWriteSize
= DataSize
;
264 if (CurrWritePtr
< LinearOffset
) {
265 return EFI_INVALID_PARAMETER
;
268 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
269 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
271 // Check to see if the Variable Writes are spanning through multiple
274 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
275 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
276 Status
= Fvb
->Write (
279 (UINTN
) (CurrWritePtr
- LinearOffset
),
285 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
286 Status
= Fvb
->Write (
289 (UINTN
) (CurrWritePtr
- LinearOffset
),
293 if (EFI_ERROR (Status
)) {
297 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
298 CurrBuffer
= CurrBuffer
+ Size
;
299 CurrWriteSize
= CurrWriteSize
- Size
;
303 LinearOffset
+= PtrBlockMapEntry
->Length
;
314 This code gets the current status of Variable Store.
316 @param VarStoreHeader Pointer to the Variable Store Header.
318 @retval EfiRaw Variable store status is raw.
319 @retval EfiValid Variable store status is valid.
320 @retval EfiInvalid Variable store status is invalid.
323 VARIABLE_STORE_STATUS
324 GetVariableStoreStatus (
325 IN VARIABLE_STORE_HEADER
*VarStoreHeader
328 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
) &&
329 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
330 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
334 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
335 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
336 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
337 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
338 VarStoreHeader
->Size
== 0xffffffff &&
339 VarStoreHeader
->Format
== 0xff &&
340 VarStoreHeader
->State
== 0xff
352 This code gets the size of name of variable.
354 @param Variable Pointer to the Variable Header.
356 @return UINTN Size of variable in bytes.
361 IN VARIABLE_HEADER
*Variable
364 if (Variable
->State
== (UINT8
) (-1) ||
365 Variable
->DataSize
== (UINT32
) (-1) ||
366 Variable
->NameSize
== (UINT32
) (-1) ||
367 Variable
->Attributes
== (UINT32
) (-1)) {
370 return (UINTN
) Variable
->NameSize
;
375 This code gets the size of variable data.
377 @param Variable Pointer to the Variable Header.
379 @return Size of variable in bytes.
384 IN VARIABLE_HEADER
*Variable
387 if (Variable
->State
== (UINT8
) (-1) ||
388 Variable
->DataSize
== (UINT32
) (-1) ||
389 Variable
->NameSize
== (UINT32
) (-1) ||
390 Variable
->Attributes
== (UINT32
) (-1)) {
393 return (UINTN
) Variable
->DataSize
;
398 This code gets the pointer to the variable name.
400 @param Variable Pointer to the Variable Header.
402 @return Pointer to Variable Name which is Unicode encoding.
407 IN VARIABLE_HEADER
*Variable
411 return (CHAR16
*) (Variable
+ 1);
416 This code gets the pointer to the variable data.
418 @param Variable Pointer to the Variable Header.
420 @return Pointer to Variable Data.
425 IN VARIABLE_HEADER
*Variable
431 // Be careful about pad size for alignment.
433 Value
= (UINTN
) GetVariableNamePtr (Variable
);
434 Value
+= NameSizeOfVariable (Variable
);
435 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
437 return (UINT8
*) Value
;
443 This code gets the pointer to the next variable header.
445 @param Variable Pointer to the Variable Header.
447 @return Pointer to next variable header.
452 IN VARIABLE_HEADER
*Variable
457 if (!IsValidVariableHeader (Variable
)) {
461 Value
= (UINTN
) GetVariableDataPtr (Variable
);
462 Value
+= DataSizeOfVariable (Variable
);
463 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
466 // Be careful about pad size for alignment.
468 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
473 Gets the pointer to the first variable header in given variable store area.
475 @param VarStoreHeader Pointer to the Variable Store Header.
477 @return Pointer to the first variable header.
482 IN VARIABLE_STORE_HEADER
*VarStoreHeader
486 // The end of variable store.
488 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
493 Gets the pointer to the end of the variable storage area.
495 This function gets pointer to the end of the variable storage
496 area, according to the input variable store header.
498 @param VarStoreHeader Pointer to the Variable Store Header.
500 @return Pointer to the end of the variable storage area.
505 IN VARIABLE_STORE_HEADER
*VarStoreHeader
509 // The end of variable store
511 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
517 Variable store garbage collection and reclaim operation.
519 @param VariableBase Base address of variable store.
520 @param LastVariableOffset Offset of last variable.
521 @param IsVolatile The variable store is volatile or not;
522 if it is non-volatile, need FTW.
523 @param UpdatingPtrTrack Pointer to updating variable pointer track structure.
524 @param ReclaimAnyway If TRUE, do reclaim anyway.
526 @return EFI_OUT_OF_RESOURCES
533 IN EFI_PHYSICAL_ADDRESS VariableBase
,
534 OUT UINTN
*LastVariableOffset
,
535 IN BOOLEAN IsVolatile
,
536 IN OUT VARIABLE_POINTER_TRACK
*UpdatingPtrTrack
,
537 IN BOOLEAN ReclaimAnyway
540 VARIABLE_HEADER
*Variable
;
541 VARIABLE_HEADER
*AddedVariable
;
542 VARIABLE_HEADER
*NextVariable
;
543 VARIABLE_HEADER
*NextAddedVariable
;
544 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
546 UINTN MaximumBufferSize
;
548 UINTN VariableNameSize
;
549 UINTN UpdatingVariableNameSize
;
556 CHAR16
*VariableNamePtr
;
557 CHAR16
*UpdatingVariableNamePtr
;
558 UINTN CommonVariableTotalSize
;
559 UINTN HwErrVariableTotalSize
;
560 BOOLEAN NeedDoReclaim
;
561 VARIABLE_HEADER
*UpdatingVariable
;
563 UpdatingVariable
= NULL
;
564 if (UpdatingPtrTrack
!= NULL
) {
565 UpdatingVariable
= UpdatingPtrTrack
->CurrPtr
;
568 NeedDoReclaim
= FALSE
;
569 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
571 CommonVariableTotalSize
= 0;
572 HwErrVariableTotalSize
= 0;
575 // Start Pointers for the variable.
577 Variable
= GetStartPointer (VariableStoreHeader
);
578 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
580 while (IsValidVariableHeader (Variable
)) {
581 NextVariable
= GetNextVariablePtr (Variable
);
582 if (Variable
->State
== VAR_ADDED
||
583 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
585 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
586 MaximumBufferSize
+= VariableSize
;
588 NeedDoReclaim
= TRUE
;
591 Variable
= NextVariable
;
594 if (!ReclaimAnyway
&& !NeedDoReclaim
) {
595 DEBUG ((EFI_D_INFO
, "Variable driver: no DELETED variable found, so no variable space could be reclaimed.\n"));
600 // Reserve the 1 Bytes with Oxff to identify the
601 // end of the variable buffer.
603 MaximumBufferSize
+= 1;
604 ValidBuffer
= AllocatePool (MaximumBufferSize
);
605 if (ValidBuffer
== NULL
) {
606 return EFI_OUT_OF_RESOURCES
;
609 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
612 // Copy variable store header.
614 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
615 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
618 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
620 Variable
= GetStartPointer (VariableStoreHeader
);
621 while (IsValidVariableHeader (Variable
)) {
622 NextVariable
= GetNextVariablePtr (Variable
);
623 if (Variable
->State
== VAR_ADDED
) {
624 if (UpdatingVariable
!= NULL
) {
625 if (UpdatingVariable
== Variable
) {
626 Variable
= NextVariable
;
630 VariableNameSize
= NameSizeOfVariable(Variable
);
631 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
633 VariableNamePtr
= GetVariableNamePtr (Variable
);
634 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
635 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
636 VariableNameSize
== UpdatingVariableNameSize
&&
637 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
638 Variable
= NextVariable
;
642 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
643 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
644 CurrPtr
+= VariableSize
;
645 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
646 HwErrVariableTotalSize
+= VariableSize
;
647 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
648 CommonVariableTotalSize
+= VariableSize
;
651 Variable
= NextVariable
;
655 // Reinstall the variable being updated if it is not NULL.
657 if (UpdatingVariable
!= NULL
) {
658 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
659 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
660 UpdatingPtrTrack
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)UpdatingPtrTrack
->StartPtr
+ ((UINTN
)CurrPtr
- (UINTN
)GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
)));
661 UpdatingPtrTrack
->InDeletedTransitionPtr
= NULL
;
662 CurrPtr
+= VariableSize
;
663 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
664 HwErrVariableTotalSize
+= VariableSize
;
665 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
666 CommonVariableTotalSize
+= VariableSize
;
671 // Reinstall all in delete transition variables.
673 Variable
= GetStartPointer (VariableStoreHeader
);
674 while (IsValidVariableHeader (Variable
)) {
675 NextVariable
= GetNextVariablePtr (Variable
);
676 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
679 // Buffer has cached all ADDED variable.
680 // Per IN_DELETED variable, we have to guarantee that
681 // no ADDED one in previous buffer.
685 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
686 while (IsValidVariableHeader (AddedVariable
)) {
687 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
688 NameSize
= NameSizeOfVariable (AddedVariable
);
689 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
690 NameSize
== NameSizeOfVariable (Variable
)
692 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
693 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
694 if (CompareMem (Point0
, Point1
, NameSize
) == 0) {
699 AddedVariable
= NextAddedVariable
;
703 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
705 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
706 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
707 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
708 CurrPtr
+= VariableSize
;
709 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
710 HwErrVariableTotalSize
+= VariableSize
;
711 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
712 CommonVariableTotalSize
+= VariableSize
;
717 Variable
= NextVariable
;
722 // If volatile variable store, just copy valid buffer.
724 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
725 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
726 Status
= EFI_SUCCESS
;
729 // If non-volatile variable store, perform FTW here.
731 Status
= FtwVariableSpace (
734 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
736 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
738 if (!EFI_ERROR (Status
)) {
739 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
741 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
742 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
745 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
746 while (IsValidVariableHeader (NextVariable
)) {
747 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
748 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
749 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
750 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
751 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
754 NextVariable
= GetNextVariablePtr (NextVariable
);
756 *LastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableBase
;
759 FreePool (ValidBuffer
);
765 Find the variable in the specified variable store.
767 @param VariableName Name of the variable to be found
768 @param VendorGuid Vendor GUID to be found.
769 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
770 check at runtime when searching variable.
771 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
773 @retval EFI_SUCCESS Variable found successfully
774 @retval EFI_NOT_FOUND Variable not found
778 IN CHAR16
*VariableName
,
779 IN EFI_GUID
*VendorGuid
,
780 IN BOOLEAN IgnoreRtCheck
,
781 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
784 VARIABLE_HEADER
*InDeletedVariable
;
787 PtrTrack
->InDeletedTransitionPtr
= NULL
;
790 // Find the variable by walk through HOB, volatile and non-volatile variable store.
792 InDeletedVariable
= NULL
;
794 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
795 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
796 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
798 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
799 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
801 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
802 if (VariableName
[0] == 0) {
803 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
804 InDeletedVariable
= PtrTrack
->CurrPtr
;
806 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
810 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
811 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
813 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
814 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
815 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
816 InDeletedVariable
= PtrTrack
->CurrPtr
;
818 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
828 PtrTrack
->CurrPtr
= InDeletedVariable
;
829 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
834 Finds variable in storage blocks of volatile and non-volatile storage areas.
836 This code finds variable in storage blocks of volatile and non-volatile storage areas.
837 If VariableName is an empty string, then we just return the first
838 qualified variable without comparing VariableName and VendorGuid.
839 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
840 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
841 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
843 @param VariableName Name of the variable to be found.
844 @param VendorGuid Vendor GUID to be found.
845 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
846 including the range searched and the target position.
847 @param Global Pointer to VARIABLE_GLOBAL structure, including
848 base of volatile variable storage area, base of
849 NV variable storage area, and a lock.
850 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
851 check at runtime when searching variable.
853 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
855 @retval EFI_SUCCESS Variable successfully found.
856 @retval EFI_NOT_FOUND Variable not found
861 IN CHAR16
*VariableName
,
862 IN EFI_GUID
*VendorGuid
,
863 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
864 IN VARIABLE_GLOBAL
*Global
,
865 IN BOOLEAN IgnoreRtCheck
869 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
870 VARIABLE_STORE_TYPE Type
;
872 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
873 return EFI_INVALID_PARAMETER
;
877 // 0: Volatile, 1: HOB, 2: Non-Volatile.
878 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
879 // make use of this mapping to implement search algorithm.
881 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
882 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
883 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
886 // Find the variable by walk through HOB, volatile and non-volatile variable store.
888 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
889 if (VariableStoreHeader
[Type
] == NULL
) {
893 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
894 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
895 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
897 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
898 if (!EFI_ERROR (Status
)) {
902 return EFI_NOT_FOUND
;
906 Get index from supported language codes according to language string.
908 This code is used to get corresponding index in supported language codes. It can handle
909 RFC4646 and ISO639 language tags.
910 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
911 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
914 SupportedLang = "engfraengfra"
916 Iso639Language = TRUE
917 The return value is "0".
919 SupportedLang = "en;fr;en-US;fr-FR"
921 Iso639Language = FALSE
922 The return value is "3".
924 @param SupportedLang Platform supported language codes.
925 @param Lang Configured language.
926 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
928 @retval The index of language in the language codes.
932 GetIndexFromSupportedLangCodes(
933 IN CHAR8
*SupportedLang
,
935 IN BOOLEAN Iso639Language
940 UINTN LanguageLength
;
942 if (Iso639Language
) {
943 CompareLength
= ISO_639_2_ENTRY_SIZE
;
944 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
945 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
947 // Successfully find the index of Lang string in SupportedLang string.
949 Index
= Index
/ CompareLength
;
957 // Compare RFC4646 language code
960 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
962 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
964 // Skip ';' characters in SupportedLang
966 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
968 // Determine the length of the next language code in SupportedLang
970 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
972 if ((CompareLength
== LanguageLength
) &&
973 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
975 // Successfully find the index of Lang string in SupportedLang string.
986 Get language string from supported language codes according to index.
988 This code is used to get corresponding language strings in supported language codes. It can handle
989 RFC4646 and ISO639 language tags.
990 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
991 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
994 SupportedLang = "engfraengfra"
996 Iso639Language = TRUE
997 The return value is "fra".
999 SupportedLang = "en;fr;en-US;fr-FR"
1001 Iso639Language = FALSE
1002 The return value is "fr".
1004 @param SupportedLang Platform supported language codes.
1005 @param Index The index in supported language codes.
1006 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1008 @retval The language string in the language codes.
1012 GetLangFromSupportedLangCodes (
1013 IN CHAR8
*SupportedLang
,
1015 IN BOOLEAN Iso639Language
1019 UINTN CompareLength
;
1023 Supported
= SupportedLang
;
1024 if (Iso639Language
) {
1026 // According to the index of Lang string in SupportedLang string to get the language.
1027 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1028 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1030 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1031 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1032 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1037 // Take semicolon as delimitation, sequentially traverse supported language codes.
1039 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1042 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1044 // Have completed the traverse, but not find corrsponding string.
1045 // This case is not allowed to happen.
1050 if (SubIndex
== Index
) {
1052 // According to the index of Lang string in SupportedLang string to get the language.
1053 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1054 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1056 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1057 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1062 // Skip ';' characters in Supported
1064 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1070 Returns a pointer to an allocated buffer that contains the best matching language
1071 from a set of supported languages.
1073 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1074 code types may not be mixed in a single call to this function. This function
1075 supports a variable argument list that allows the caller to pass in a prioritized
1076 list of language codes to test against all the language codes in SupportedLanguages.
1078 If SupportedLanguages is NULL, then ASSERT().
1080 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1081 contains a set of language codes in the format
1082 specified by Iso639Language.
1083 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1084 in ISO 639-2 format. If FALSE, then all language
1085 codes are assumed to be in RFC 4646 language format
1086 @param[in] ... A variable argument list that contains pointers to
1087 Null-terminated ASCII strings that contain one or more
1088 language codes in the format specified by Iso639Language.
1089 The first language code from each of these language
1090 code lists is used to determine if it is an exact or
1091 close match to any of the language codes in
1092 SupportedLanguages. Close matches only apply to RFC 4646
1093 language codes, and the matching algorithm from RFC 4647
1094 is used to determine if a close match is present. If
1095 an exact or close match is found, then the matching
1096 language code from SupportedLanguages is returned. If
1097 no matches are found, then the next variable argument
1098 parameter is evaluated. The variable argument list
1099 is terminated by a NULL.
1101 @retval NULL The best matching language could not be found in SupportedLanguages.
1102 @retval NULL There are not enough resources available to return the best matching
1104 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1105 language in SupportedLanguages.
1110 VariableGetBestLanguage (
1111 IN CONST CHAR8
*SupportedLanguages
,
1112 IN BOOLEAN Iso639Language
,
1118 UINTN CompareLength
;
1119 UINTN LanguageLength
;
1120 CONST CHAR8
*Supported
;
1123 ASSERT (SupportedLanguages
!= NULL
);
1125 VA_START (Args
, Iso639Language
);
1126 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1128 // Default to ISO 639-2 mode
1131 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1134 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1136 if (!Iso639Language
) {
1137 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1141 // Trim back the length of Language used until it is empty
1143 while (LanguageLength
> 0) {
1145 // Loop through all language codes in SupportedLanguages
1147 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1149 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1151 if (!Iso639Language
) {
1153 // Skip ';' characters in Supported
1155 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1157 // Determine the length of the next language code in Supported
1159 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1161 // If Language is longer than the Supported, then skip to the next language
1163 if (LanguageLength
> CompareLength
) {
1168 // See if the first LanguageLength characters in Supported match Language
1170 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1173 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1174 Buffer
[CompareLength
] = '\0';
1175 return CopyMem (Buffer
, Supported
, CompareLength
);
1179 if (Iso639Language
) {
1181 // If ISO 639 mode, then each language can only be tested once
1186 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1188 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1195 // No matches were found
1201 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1203 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1205 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1206 and are read-only. Therefore, in variable driver, only store the original value for other use.
1208 @param[in] VariableName Name of variable.
1210 @param[in] Data Variable data.
1212 @param[in] DataSize Size of data. 0 means delete.
1216 AutoUpdateLangVariable (
1217 IN CHAR16
*VariableName
,
1223 CHAR8
*BestPlatformLang
;
1227 VARIABLE_POINTER_TRACK Variable
;
1228 BOOLEAN SetLanguageCodes
;
1231 // Don't do updates for delete operation
1233 if (DataSize
== 0) {
1237 SetLanguageCodes
= FALSE
;
1239 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1241 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1247 SetLanguageCodes
= TRUE
;
1250 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1251 // Therefore, in variable driver, only store the original value for other use.
1253 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1254 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1256 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1257 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1260 // PlatformLang holds a single language from PlatformLangCodes,
1261 // so the size of PlatformLangCodes is enough for the PlatformLang.
1263 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1264 FreePool (mVariableModuleGlobal
->PlatformLang
);
1266 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1267 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1269 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1271 // LangCodes is a volatile variable, so it can not be updated at runtime.
1277 SetLanguageCodes
= TRUE
;
1280 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1281 // Therefore, in variable driver, only store the original value for other use.
1283 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1284 FreePool (mVariableModuleGlobal
->LangCodes
);
1286 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1287 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1290 if (SetLanguageCodes
1291 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1292 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1294 // Update Lang if PlatformLang is already set
1295 // Update PlatformLang if Lang is already set
1297 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1298 if (!EFI_ERROR (Status
)) {
1302 VariableName
= L
"PlatformLang";
1303 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1304 DataSize
= Variable
.CurrPtr
->DataSize
;
1306 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1307 if (!EFI_ERROR (Status
)) {
1309 // Update PlatformLang
1311 VariableName
= L
"Lang";
1312 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1313 DataSize
= Variable
.CurrPtr
->DataSize
;
1316 // Neither PlatformLang nor Lang is set, directly return
1324 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1326 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1328 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1330 // Update Lang when PlatformLangCodes/LangCodes were set.
1332 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1334 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1336 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1337 if (BestPlatformLang
!= NULL
) {
1339 // Get the corresponding index in language codes.
1341 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1344 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1346 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1349 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1351 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1353 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1354 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
1356 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1358 ASSERT_EFI_ERROR(Status
);
1362 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1364 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1366 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1368 // When setting Lang, firstly get most matched language string from supported language codes.
1370 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1371 if (BestLang
!= NULL
) {
1373 // Get the corresponding index in language codes.
1375 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1378 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1380 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1383 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1385 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1387 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1388 AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
1390 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1391 ASSERT_EFI_ERROR (Status
);
1398 Update the variable region with Variable information. These are the same
1399 arguments as the EFI Variable services.
1401 @param[in] VariableName Name of variable.
1402 @param[in] VendorGuid Guid of variable.
1403 @param[in] Data Variable data.
1404 @param[in] DataSize Size of data. 0 means delete.
1405 @param[in] Attributes Attribues of the variable.
1406 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.
1408 @retval EFI_SUCCESS The update operation is success.
1409 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1414 IN CHAR16
*VariableName
,
1415 IN EFI_GUID
*VendorGuid
,
1418 IN UINT32 Attributes OPTIONAL
,
1419 IN OUT VARIABLE_POINTER_TRACK
*CacheVariable
1423 VARIABLE_HEADER
*NextVariable
;
1425 UINTN NonVolatileVarableStoreSize
;
1426 UINTN VarNameOffset
;
1427 UINTN VarDataOffset
;
1431 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1433 VARIABLE_POINTER_TRACK
*Variable
;
1434 VARIABLE_POINTER_TRACK NvVariable
;
1435 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1438 if ((mVariableModuleGlobal
->FvbInstance
== NULL
) && ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0)) {
1440 // The FVB protocol is not ready. Trying to update NV variable prior to the installation
1441 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
1443 return EFI_NOT_AVAILABLE_YET
;
1446 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1447 Variable
= CacheVariable
;
1450 // Update/Delete existing NV variable.
1451 // CacheVariable points to the variable in the memory copy of Flash area
1452 // Now let Variable points to the same variable in Flash area.
1454 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1455 Variable
= &NvVariable
;
1456 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1457 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1458 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1459 if (CacheVariable
->InDeletedTransitionPtr
!= NULL
) {
1460 Variable
->InDeletedTransitionPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->InDeletedTransitionPtr
- (UINTN
)CacheVariable
->StartPtr
));
1462 Variable
->InDeletedTransitionPtr
= NULL
;
1464 Variable
->Volatile
= FALSE
;
1467 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1469 if (Variable
->CurrPtr
!= NULL
) {
1471 // Update/Delete existing variable.
1475 // If AtRuntime and the variable is Volatile and Runtime Access,
1476 // the volatile is ReadOnly, and SetVariable should be aborted and
1477 // return EFI_WRITE_PROTECTED.
1479 if (Variable
->Volatile
) {
1480 Status
= EFI_WRITE_PROTECTED
;
1484 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.
1486 if (((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0)) {
1487 Status
= EFI_INVALID_PARAMETER
;
1493 // Setting a data variable with no access, or zero DataSize attributes
1494 // causes it to be deleted.
1496 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1497 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
1499 // Both ADDED and IN_DELETED_TRANSITION variable are present,
1500 // set IN_DELETED_TRANSITION one to DELETED state first.
1502 State
= Variable
->InDeletedTransitionPtr
->State
;
1503 State
&= VAR_DELETED
;
1504 Status
= UpdateVariableStore (
1505 &mVariableModuleGlobal
->VariableGlobal
,
1509 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
1513 if (!EFI_ERROR (Status
)) {
1514 if (!Variable
->Volatile
) {
1515 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
1516 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
1523 State
= Variable
->CurrPtr
->State
;
1524 State
&= VAR_DELETED
;
1526 Status
= UpdateVariableStore (
1527 &mVariableModuleGlobal
->VariableGlobal
,
1531 (UINTN
) &Variable
->CurrPtr
->State
,
1535 if (!EFI_ERROR (Status
)) {
1536 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1537 if (!Variable
->Volatile
) {
1538 CacheVariable
->CurrPtr
->State
= State
;
1539 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1545 // If the variable is marked valid, and the same data has been passed in,
1546 // then return to the caller immediately.
1548 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1549 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0)) {
1551 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1552 Status
= EFI_SUCCESS
;
1554 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1555 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1558 // Mark the old variable as in delete transition.
1560 State
= Variable
->CurrPtr
->State
;
1561 State
&= VAR_IN_DELETED_TRANSITION
;
1563 Status
= UpdateVariableStore (
1564 &mVariableModuleGlobal
->VariableGlobal
,
1568 (UINTN
) &Variable
->CurrPtr
->State
,
1572 if (EFI_ERROR (Status
)) {
1575 if (!Variable
->Volatile
) {
1576 CacheVariable
->CurrPtr
->State
= State
;
1581 // Not found existing variable. Create a new variable.
1585 // Make sure we are trying to create a new variable.
1586 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1588 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1589 Status
= EFI_NOT_FOUND
;
1594 // Only variable have NV|RT attribute can be created in Runtime.
1597 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1598 Status
= EFI_INVALID_PARAMETER
;
1604 // Function part - create a new variable and copy the data.
1605 // Both update a variable and create a variable will come here.
1608 // Tricky part: Use scratch data area at the end of volatile variable store
1609 // as a temporary storage.
1611 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1612 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1614 SetMem (NextVariable
, ScratchSize
, 0xff);
1616 NextVariable
->StartId
= VARIABLE_DATA
;
1617 NextVariable
->Attributes
= Attributes
;
1619 // NextVariable->State = VAR_ADDED;
1621 NextVariable
->Reserved
= 0;
1622 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1623 VarNameSize
= StrSize (VariableName
);
1625 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1629 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1631 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1635 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1637 // There will be pad bytes after Data, the NextVariable->NameSize and
1638 // NextVariable->DataSize should not include pad size so that variable
1639 // service can get actual size in GetVariable.
1641 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1642 NextVariable
->DataSize
= (UINT32
)DataSize
;
1645 // The actual size of the variable that stores in storage should
1646 // include pad size.
1648 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1649 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1651 // Create a nonvolatile variable.
1654 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1655 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1656 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1657 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1658 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1660 Status
= EFI_OUT_OF_RESOURCES
;
1664 // Perform garbage collection & reclaim operation.
1666 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1667 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
, FALSE
);
1668 if (EFI_ERROR (Status
)) {
1672 // If still no enough space, return out of resources.
1674 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1675 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1676 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1677 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1678 Status
= EFI_OUT_OF_RESOURCES
;
1681 if (Variable
->CurrPtr
!= NULL
) {
1682 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
1683 CacheVariable
->InDeletedTransitionPtr
= NULL
;
1688 // 1. Write variable header
1689 // 2. Set variable state to header valid
1690 // 3. Write variable data
1691 // 4. Set variable state to valid
1696 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1697 Status
= UpdateVariableStore (
1698 &mVariableModuleGlobal
->VariableGlobal
,
1702 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1703 sizeof (VARIABLE_HEADER
),
1704 (UINT8
*) NextVariable
1707 if (EFI_ERROR (Status
)) {
1714 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1715 Status
= UpdateVariableStore (
1716 &mVariableModuleGlobal
->VariableGlobal
,
1720 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1722 &NextVariable
->State
1725 if (EFI_ERROR (Status
)) {
1731 Status
= UpdateVariableStore (
1732 &mVariableModuleGlobal
->VariableGlobal
,
1736 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1737 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1738 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1741 if (EFI_ERROR (Status
)) {
1747 NextVariable
->State
= VAR_ADDED
;
1748 Status
= UpdateVariableStore (
1749 &mVariableModuleGlobal
->VariableGlobal
,
1753 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1755 &NextVariable
->State
1758 if (EFI_ERROR (Status
)) {
1762 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1764 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1765 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1767 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1770 // update the memory copy of Flash region.
1772 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1775 // Create a volatile variable.
1779 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1780 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1782 // Perform garbage collection & reclaim operation.
1784 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1785 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
, FALSE
);
1786 if (EFI_ERROR (Status
)) {
1790 // If still no enough space, return out of resources.
1792 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1793 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1795 Status
= EFI_OUT_OF_RESOURCES
;
1798 if (Variable
->CurrPtr
!= NULL
) {
1799 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
1800 CacheVariable
->InDeletedTransitionPtr
= NULL
;
1804 NextVariable
->State
= VAR_ADDED
;
1805 Status
= UpdateVariableStore (
1806 &mVariableModuleGlobal
->VariableGlobal
,
1810 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1812 (UINT8
*) NextVariable
1815 if (EFI_ERROR (Status
)) {
1819 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1823 // Mark the old variable as deleted.
1825 if (!EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1826 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
1828 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
1829 // set IN_DELETED_TRANSITION one to DELETED state first.
1831 State
= Variable
->InDeletedTransitionPtr
->State
;
1832 State
&= VAR_DELETED
;
1833 Status
= UpdateVariableStore (
1834 &mVariableModuleGlobal
->VariableGlobal
,
1838 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
1842 if (!EFI_ERROR (Status
)) {
1843 if (!Variable
->Volatile
) {
1844 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
1845 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
1852 State
= Variable
->CurrPtr
->State
;
1853 State
&= VAR_DELETED
;
1855 Status
= UpdateVariableStore (
1856 &mVariableModuleGlobal
->VariableGlobal
,
1860 (UINTN
) &Variable
->CurrPtr
->State
,
1864 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1865 CacheVariable
->CurrPtr
->State
= State
;
1869 if (!EFI_ERROR (Status
)) {
1870 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1872 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1881 Check if a Unicode character is a hexadecimal character.
1883 This function checks if a Unicode character is a
1884 hexadecimal character. The valid hexadecimal character is
1885 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
1888 @param Char The character to check against.
1890 @retval TRUE If the Char is a hexadecmial character.
1891 @retval FALSE If the Char is not a hexadecmial character.
1896 IsHexaDecimalDigitCharacter (
1900 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
1905 This code checks if variable is hardware error record variable or not.
1907 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
1908 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
1910 @param VariableName Pointer to variable name.
1911 @param VendorGuid Variable Vendor Guid.
1913 @retval TRUE Variable is hardware error record variable.
1914 @retval FALSE Variable is not hardware error record variable.
1919 IsHwErrRecVariable (
1920 IN CHAR16
*VariableName
,
1921 IN EFI_GUID
*VendorGuid
1924 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
1925 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
1926 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
1927 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
1928 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
1929 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
1930 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
1938 Mark a variable that will become read-only after leaving the DXE phase of execution.
1940 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
1941 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
1942 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
1944 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
1945 as pending to be read-only.
1946 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
1947 Or VariableName is an empty string.
1948 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
1949 already been signaled.
1950 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
1954 VariableLockRequestToLock (
1955 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL
*This
,
1956 IN CHAR16
*VariableName
,
1957 IN EFI_GUID
*VendorGuid
1960 VARIABLE_ENTRY
*Entry
;
1962 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1963 return EFI_INVALID_PARAMETER
;
1967 return EFI_ACCESS_DENIED
;
1970 Entry
= AllocateRuntimePool (sizeof (*Entry
) + StrSize (VariableName
));
1971 if (Entry
== NULL
) {
1972 return EFI_OUT_OF_RESOURCES
;
1975 DEBUG ((EFI_D_INFO
, "[Variable] Lock: %g:%s\n", VendorGuid
, VariableName
));
1977 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1979 Entry
->Name
= (CHAR16
*) (Entry
+ 1);
1980 StrCpy (Entry
->Name
, VariableName
);
1981 CopyGuid (&Entry
->Guid
, VendorGuid
);
1982 InsertTailList (&mLockedVariableList
, &Entry
->Link
);
1984 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1991 This code finds variable in storage blocks (Volatile or Non-Volatile).
1993 @param VariableName Name of Variable to be found.
1994 @param VendorGuid Variable vendor GUID.
1995 @param Attributes Attribute value of the variable found.
1996 @param DataSize Size of Data found. If size is less than the
1997 data, this value contains the required size.
1998 @param Data Data pointer.
2000 @return EFI_INVALID_PARAMETER Invalid parameter.
2001 @return EFI_SUCCESS Find the specified variable.
2002 @return EFI_NOT_FOUND Not found.
2003 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2008 VariableServiceGetVariable (
2009 IN CHAR16
*VariableName
,
2010 IN EFI_GUID
*VendorGuid
,
2011 OUT UINT32
*Attributes OPTIONAL
,
2012 IN OUT UINTN
*DataSize
,
2017 VARIABLE_POINTER_TRACK Variable
;
2020 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2021 return EFI_INVALID_PARAMETER
;
2024 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2026 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2027 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2034 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2035 ASSERT (VarDataSize
!= 0);
2037 if (*DataSize
>= VarDataSize
) {
2039 Status
= EFI_INVALID_PARAMETER
;
2043 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2044 if (Attributes
!= NULL
) {
2045 *Attributes
= Variable
.CurrPtr
->Attributes
;
2048 *DataSize
= VarDataSize
;
2049 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2051 Status
= EFI_SUCCESS
;
2054 *DataSize
= VarDataSize
;
2055 Status
= EFI_BUFFER_TOO_SMALL
;
2060 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2068 This code Finds the Next available variable.
2070 @param VariableNameSize Size of the variable name.
2071 @param VariableName Pointer to variable name.
2072 @param VendorGuid Variable Vendor Guid.
2074 @return EFI_INVALID_PARAMETER Invalid parameter.
2075 @return EFI_SUCCESS Find the specified variable.
2076 @return EFI_NOT_FOUND Not found.
2077 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2082 VariableServiceGetNextVariableName (
2083 IN OUT UINTN
*VariableNameSize
,
2084 IN OUT CHAR16
*VariableName
,
2085 IN OUT EFI_GUID
*VendorGuid
2088 VARIABLE_STORE_TYPE Type
;
2089 VARIABLE_POINTER_TRACK Variable
;
2090 VARIABLE_POINTER_TRACK VariableInHob
;
2091 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2094 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2096 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2097 return EFI_INVALID_PARAMETER
;
2100 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2102 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2103 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2107 if (VariableName
[0] != 0) {
2109 // If variable name is not NULL, get next variable.
2111 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2115 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2116 // The index and attributes mapping must be kept in this order as FindVariable
2117 // makes use of this mapping to implement search algorithm.
2119 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2120 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2121 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2125 // Switch from Volatile to HOB, to Non-Volatile.
2127 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
2128 (Variable
.CurrPtr
== NULL
) ||
2129 !IsValidVariableHeader (Variable
.CurrPtr
)
2132 // Find current storage index
2134 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2135 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2139 ASSERT (Type
< VariableStoreTypeMax
);
2141 // Switch to next storage
2143 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2144 if (VariableStoreHeader
[Type
] != NULL
) {
2149 // Capture the case that
2150 // 1. current storage is the last one, or
2151 // 2. no further storage
2153 if (Type
== VariableStoreTypeMax
) {
2154 Status
= EFI_NOT_FOUND
;
2157 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2158 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2159 Variable
.CurrPtr
= Variable
.StartPtr
;
2163 // Variable is found
2165 if (Variable
.CurrPtr
->State
== VAR_ADDED
|| Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2166 if (!AtRuntime () || ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
2167 if (Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2169 // If it is a IN_DELETED_TRANSITION variable,
2170 // and there is also a same ADDED one at the same time,
2173 VariablePtrTrack
.StartPtr
= Variable
.StartPtr
;
2174 VariablePtrTrack
.EndPtr
= Variable
.EndPtr
;
2175 Status
= FindVariableEx (
2176 GetVariableNamePtr (Variable
.CurrPtr
),
2177 &Variable
.CurrPtr
->VendorGuid
,
2181 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
== VAR_ADDED
) {
2182 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2188 // Don't return NV variable when HOB overrides it
2190 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2191 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2193 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2194 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2195 Status
= FindVariableEx (
2196 GetVariableNamePtr (Variable
.CurrPtr
),
2197 &Variable
.CurrPtr
->VendorGuid
,
2201 if (!EFI_ERROR (Status
)) {
2202 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2207 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2208 ASSERT (VarNameSize
!= 0);
2210 if (VarNameSize
<= *VariableNameSize
) {
2211 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2212 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2213 Status
= EFI_SUCCESS
;
2215 Status
= EFI_BUFFER_TOO_SMALL
;
2218 *VariableNameSize
= VarNameSize
;
2223 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2227 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2233 This code sets variable in storage blocks (Volatile or Non-Volatile).
2235 @param VariableName Name of Variable to be found.
2236 @param VendorGuid Variable vendor GUID.
2237 @param Attributes Attribute value of the variable found
2238 @param DataSize Size of Data found. If size is less than the
2239 data, this value contains the required size.
2240 @param Data Data pointer.
2242 @return EFI_INVALID_PARAMETER Invalid parameter.
2243 @return EFI_SUCCESS Set successfully.
2244 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2245 @return EFI_NOT_FOUND Not found.
2246 @return EFI_WRITE_PROTECTED Variable is read-only.
2251 VariableServiceSetVariable (
2252 IN CHAR16
*VariableName
,
2253 IN EFI_GUID
*VendorGuid
,
2254 IN UINT32 Attributes
,
2259 VARIABLE_POINTER_TRACK Variable
;
2261 VARIABLE_HEADER
*NextVariable
;
2262 EFI_PHYSICAL_ADDRESS Point
;
2264 VARIABLE_ENTRY
*Entry
;
2267 // Check input parameters.
2269 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2270 return EFI_INVALID_PARAMETER
;
2273 if (DataSize
!= 0 && Data
== NULL
) {
2274 return EFI_INVALID_PARAMETER
;
2278 // Not support authenticated variable write yet.
2280 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2281 return EFI_INVALID_PARAMETER
;
2285 // Make sure if runtime bit is set, boot service bit is set also.
2287 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2288 return EFI_INVALID_PARAMETER
;
2291 if ((UINTN
)(~0) - DataSize
< StrSize(VariableName
)){
2293 // Prevent whole variable size overflow
2295 return EFI_INVALID_PARAMETER
;
2299 // The size of the VariableName, including the Unicode Null in bytes plus
2300 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2301 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2303 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2304 if ( StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
)) {
2305 return EFI_INVALID_PARAMETER
;
2307 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2308 return EFI_INVALID_PARAMETER
;
2312 // The size of the VariableName, including the Unicode Null in bytes plus
2313 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2315 if (StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
)) {
2316 return EFI_INVALID_PARAMETER
;
2320 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2323 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2325 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2326 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2328 // Parse non-volatile variable data and get last variable offset.
2330 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2331 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2332 && IsValidVariableHeader (NextVariable
)) {
2333 NextVariable
= GetNextVariablePtr (NextVariable
);
2335 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2338 if (mEndOfDxe
&& mEnableLocking
) {
2340 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.
2342 for ( Link
= GetFirstNode (&mLockedVariableList
)
2343 ; !IsNull (&mLockedVariableList
, Link
)
2344 ; Link
= GetNextNode (&mLockedVariableList
, Link
)
2346 Entry
= BASE_CR (Link
, VARIABLE_ENTRY
, Link
);
2347 if (CompareGuid (&Entry
->Guid
, VendorGuid
) && (StrCmp (Entry
->Name
, VariableName
) == 0)) {
2348 Status
= EFI_WRITE_PROTECTED
;
2349 DEBUG ((EFI_D_INFO
, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid
, VariableName
));
2356 // Check whether the input variable is already existed.
2358 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2359 if (!EFI_ERROR (Status
)) {
2360 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2361 Status
= EFI_WRITE_PROTECTED
;
2367 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2369 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2371 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
2374 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2375 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2382 This code returns information about the EFI variables.
2384 @param Attributes Attributes bitmask to specify the type of variables
2385 on which to return information.
2386 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2387 for the EFI variables associated with the attributes specified.
2388 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2389 for EFI variables associated with the attributes specified.
2390 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2391 associated with the attributes specified.
2393 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2394 @return EFI_SUCCESS Query successfully.
2395 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2400 VariableServiceQueryVariableInfo (
2401 IN UINT32 Attributes
,
2402 OUT UINT64
*MaximumVariableStorageSize
,
2403 OUT UINT64
*RemainingVariableStorageSize
,
2404 OUT UINT64
*MaximumVariableSize
2407 VARIABLE_HEADER
*Variable
;
2408 VARIABLE_HEADER
*NextVariable
;
2409 UINT64 VariableSize
;
2410 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2411 UINT64 CommonVariableTotalSize
;
2412 UINT64 HwErrVariableTotalSize
;
2414 CommonVariableTotalSize
= 0;
2415 HwErrVariableTotalSize
= 0;
2417 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2418 return EFI_INVALID_PARAMETER
;
2421 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2423 // Make sure the Attributes combination is supported by the platform.
2425 return EFI_UNSUPPORTED
;
2426 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2428 // Make sure if runtime bit is set, boot service bit is set also.
2430 return EFI_INVALID_PARAMETER
;
2431 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2433 // Make sure RT Attribute is set if we are in Runtime phase.
2435 return EFI_INVALID_PARAMETER
;
2436 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2438 // Make sure Hw Attribute is set with NV.
2440 return EFI_INVALID_PARAMETER
;
2441 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2443 // Not support authentiated variable write yet.
2445 return EFI_UNSUPPORTED
;
2448 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2450 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2452 // Query is Volatile related.
2454 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2457 // Query is Non-Volatile related.
2459 VariableStoreHeader
= mNvVariableCache
;
2463 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2464 // with the storage size (excluding the storage header size).
2466 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2469 // Harware error record variable needs larger size.
2471 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2472 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2473 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2475 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2476 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2477 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2481 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2483 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2487 // Point to the starting address of the variables.
2489 Variable
= GetStartPointer (VariableStoreHeader
);
2492 // Now walk through the related variable store.
2494 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2495 NextVariable
= GetNextVariablePtr (Variable
);
2496 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2500 // We don't take the state of the variables in mind
2501 // when calculating RemainingVariableStorageSize,
2502 // since the space occupied by variables not marked with
2503 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2505 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2506 HwErrVariableTotalSize
+= VariableSize
;
2508 CommonVariableTotalSize
+= VariableSize
;
2512 // Only care about Variables with State VAR_ADDED, because
2513 // the space not marked as VAR_ADDED is reclaimable now.
2515 if (Variable
->State
== VAR_ADDED
) {
2516 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2517 HwErrVariableTotalSize
+= VariableSize
;
2519 CommonVariableTotalSize
+= VariableSize
;
2525 // Go to the next one.
2527 Variable
= NextVariable
;
2530 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2531 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2533 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2536 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2537 *MaximumVariableSize
= 0;
2538 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2539 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2542 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2548 This function reclaims variable storage if free size is below the threshold.
2557 UINTN CommonVariableSpace
;
2558 UINTN RemainingCommonVariableSpace
;
2559 UINTN RemainingHwErrVariableSpace
;
2561 Status
= EFI_SUCCESS
;
2563 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2565 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2567 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2569 // Check if the free area is blow a threshold.
2571 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2572 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2573 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2575 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2576 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2581 ASSERT_EFI_ERROR (Status
);
2586 Init non-volatile variable store.
2588 @retval EFI_SUCCESS Function successfully executed.
2589 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2590 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.
2594 InitNonVolatileVariableStore (
2598 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
2599 VARIABLE_HEADER
*NextVariable
;
2600 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2601 UINT64 VariableStoreLength
;
2603 EFI_HOB_GUID_TYPE
*GuidHob
;
2604 EFI_PHYSICAL_ADDRESS NvStorageBase
;
2605 UINT8
*NvStorageData
;
2606 UINT32 NvStorageSize
;
2607 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*FtwLastWriteData
;
2608 UINT32 BackUpOffset
;
2611 mVariableModuleGlobal
->FvbInstance
= NULL
;
2614 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2615 // is stored with common variable in the same NV region. So the platform integrator should
2616 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2617 // PcdFlashNvStorageVariableSize.
2619 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2622 // Allocate runtime memory used for a memory copy of the FLASH region.
2623 // Keep the memory and the FLASH in sync as updates occur.
2625 NvStorageSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
2626 NvStorageData
= AllocateRuntimeZeroPool (NvStorageSize
);
2627 if (NvStorageData
== NULL
) {
2628 return EFI_OUT_OF_RESOURCES
;
2631 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2632 if (NvStorageBase
== 0) {
2633 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2636 // Copy NV storage data to the memory buffer.
2638 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) NvStorageBase
, NvStorageSize
);
2641 // Check the FTW last write data hob.
2643 GuidHob
= GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid
);
2644 if (GuidHob
!= NULL
) {
2645 FtwLastWriteData
= (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*) GET_GUID_HOB_DATA (GuidHob
);
2646 if (FtwLastWriteData
->TargetAddress
== NvStorageBase
) {
2647 DEBUG ((EFI_D_INFO
, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN
) FtwLastWriteData
->SpareAddress
));
2649 // Copy the backed up NV storage data to the memory buffer from spare block.
2651 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) (FtwLastWriteData
->SpareAddress
), NvStorageSize
);
2652 } else if ((FtwLastWriteData
->TargetAddress
> NvStorageBase
) &&
2653 (FtwLastWriteData
->TargetAddress
< (NvStorageBase
+ NvStorageSize
))) {
2655 // Flash NV storage from the offset is backed up in spare block.
2657 BackUpOffset
= (UINT32
) (FtwLastWriteData
->TargetAddress
- NvStorageBase
);
2658 BackUpSize
= NvStorageSize
- BackUpOffset
;
2659 DEBUG ((EFI_D_INFO
, "Variable: High partial NV storage from offset: %x is backed up in spare block: 0x%x\n", BackUpOffset
, (UINTN
) FtwLastWriteData
->SpareAddress
));
2661 // Copy the partial backed up NV storage data to the memory buffer from spare block.
2663 CopyMem (NvStorageData
+ BackUpOffset
, (UINT8
*) (UINTN
) FtwLastWriteData
->SpareAddress
, BackUpSize
);
2667 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) NvStorageData
;
2670 // Check if the Firmware Volume is not corrupted
2672 if ((FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) || (!CompareGuid (&gEfiSystemNvDataFvGuid
, &FvHeader
->FileSystemGuid
))) {
2673 FreePool (NvStorageData
);
2674 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
2675 return EFI_VOLUME_CORRUPTED
;
2678 VariableStoreBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) FvHeader
+ FvHeader
->HeaderLength
);
2679 VariableStoreLength
= (UINT64
) (NvStorageSize
- FvHeader
->HeaderLength
);
2681 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2682 mNvVariableCache
= (VARIABLE_STORE_HEADER
*) (UINTN
) VariableStoreBase
;
2683 if (GetVariableStoreStatus (mNvVariableCache
) != EfiValid
) {
2684 FreePool (NvStorageData
);
2685 DEBUG((EFI_D_ERROR
, "Variable Store header is corrupted\n"));
2686 return EFI_VOLUME_CORRUPTED
;
2688 ASSERT(mNvVariableCache
->Size
== VariableStoreLength
);
2691 // The max variable or hardware error variable size should be < variable store size.
2693 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) < VariableStoreLength
);
2696 // Parse non-volatile variable data and get last variable offset.
2698 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2699 while (IsValidVariableHeader (NextVariable
)) {
2700 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2701 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2702 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2704 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2707 NextVariable
= GetNextVariablePtr (NextVariable
);
2709 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2715 Flush the HOB variable to flash.
2717 @param[in] VariableName Name of variable has been updated or deleted.
2718 @param[in] VendorGuid Guid of variable has been updated or deleted.
2722 FlushHobVariableToFlash (
2723 IN CHAR16
*VariableName
,
2724 IN EFI_GUID
*VendorGuid
2728 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2729 VARIABLE_HEADER
*Variable
;
2736 // Flush the HOB variable to flash.
2738 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2739 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2741 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
2743 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
2744 for ( Variable
= GetStartPointer (VariableStoreHeader
)
2745 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
2746 ; Variable
= GetNextVariablePtr (Variable
)
2748 if (Variable
->State
!= VAR_ADDED
) {
2750 // The HOB variable has been set to DELETED state in local.
2754 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
2755 if (VendorGuid
== NULL
|| VariableName
== NULL
||
2756 !CompareGuid (VendorGuid
, &Variable
->VendorGuid
) ||
2757 StrCmp (VariableName
, GetVariableNamePtr (Variable
)) != 0) {
2758 VariableData
= GetVariableDataPtr (Variable
);
2759 Status
= VariableServiceSetVariable (
2760 GetVariableNamePtr (Variable
),
2761 &Variable
->VendorGuid
,
2762 Variable
->Attributes
,
2766 DEBUG ((EFI_D_INFO
, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
), Status
));
2769 // The updated or deleted variable is matched with the HOB variable.
2770 // Don't break here because we will try to set other HOB variables
2771 // since this variable could be set successfully.
2773 Status
= EFI_SUCCESS
;
2775 if (!EFI_ERROR (Status
)) {
2777 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
2778 // set the HOB variable to DELETED state in local.
2780 DEBUG ((EFI_D_INFO
, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
)));
2781 Variable
->State
&= VAR_DELETED
;
2788 // We still have HOB variable(s) not flushed in flash.
2790 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
2793 // All HOB variables have been flushed in flash.
2795 DEBUG ((EFI_D_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
2796 if (!AtRuntime ()) {
2797 FreePool ((VOID
*) VariableStoreHeader
);
2805 Initializes variable write service after FTW was ready.
2807 @retval EFI_SUCCESS Function successfully executed.
2808 @retval Others Fail to initialize the variable service.
2812 VariableWriteServiceInitialize (
2817 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2820 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2821 EFI_PHYSICAL_ADDRESS NvStorageBase
;
2823 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2824 if (NvStorageBase
== 0) {
2825 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2827 VariableStoreBase
= NvStorageBase
+ (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(NvStorageBase
))->HeaderLength
);
2830 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
2832 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2833 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2836 // Check if the free area is really free.
2838 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
2839 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2842 // There must be something wrong in variable store, do reclaim operation.
2845 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2846 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2851 if (EFI_ERROR (Status
)) {
2858 FlushHobVariableToFlash (NULL
, NULL
);
2865 Initializes variable store area for non-volatile and volatile variable.
2867 @retval EFI_SUCCESS Function successfully executed.
2868 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2872 VariableCommonInitialize (
2877 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2878 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2879 UINT64 VariableStoreLength
;
2881 EFI_HOB_GUID_TYPE
*GuidHob
;
2884 // Allocate runtime memory for variable driver global structure.
2886 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2887 if (mVariableModuleGlobal
== NULL
) {
2888 return EFI_OUT_OF_RESOURCES
;
2891 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2894 // Get HOB variable store.
2896 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
2897 if (GuidHob
!= NULL
) {
2898 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
2899 VariableStoreLength
= (UINT64
) (GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
));
2900 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
2901 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
2902 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
2903 FreePool (mVariableModuleGlobal
);
2904 return EFI_OUT_OF_RESOURCES
;
2907 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
2912 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2914 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2915 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2916 if (VolatileVariableStore
== NULL
) {
2917 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2918 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
2920 FreePool (mVariableModuleGlobal
);
2921 return EFI_OUT_OF_RESOURCES
;
2924 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2927 // Initialize Variable Specific Data.
2929 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2930 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2932 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiVariableGuid
);
2933 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2934 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2935 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2936 VolatileVariableStore
->Reserved
= 0;
2937 VolatileVariableStore
->Reserved1
= 0;
2940 // Init non-volatile variable store.
2942 Status
= InitNonVolatileVariableStore ();
2943 if (EFI_ERROR (Status
)) {
2944 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2945 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
2947 FreePool (mVariableModuleGlobal
);
2948 FreePool (VolatileVariableStore
);
2956 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2958 @param[in] Address The Flash address.
2959 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2960 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2964 GetFvbInfoByAddress (
2965 IN EFI_PHYSICAL_ADDRESS Address
,
2966 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2967 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2971 EFI_HANDLE
*HandleBuffer
;
2974 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2975 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2976 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2977 EFI_FVB_ATTRIBUTES_2 Attributes
;
2980 // Get all FVB handles.
2982 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2983 if (EFI_ERROR (Status
)) {
2984 return EFI_NOT_FOUND
;
2988 // Get the FVB to access variable store.
2991 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2992 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2993 if (EFI_ERROR (Status
)) {
2994 Status
= EFI_NOT_FOUND
;
2999 // Ensure this FVB protocol supported Write operation.
3001 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
3002 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
3007 // Compare the address and select the right one.
3009 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
3010 if (EFI_ERROR (Status
)) {
3014 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
3015 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
3016 if (FvbHandle
!= NULL
) {
3017 *FvbHandle
= HandleBuffer
[Index
];
3019 if (FvbProtocol
!= NULL
) {
3022 Status
= EFI_SUCCESS
;
3026 FreePool (HandleBuffer
);
3029 Status
= EFI_NOT_FOUND
;