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
;
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 UpdatingPtrTrack Pointer to updating variable pointer track structure.
508 @param ReclaimAnyway If TRUE, do reclaim anyway.
510 @return EFI_OUT_OF_RESOURCES
517 IN EFI_PHYSICAL_ADDRESS VariableBase
,
518 OUT UINTN
*LastVariableOffset
,
519 IN BOOLEAN IsVolatile
,
520 IN OUT VARIABLE_POINTER_TRACK
*UpdatingPtrTrack
,
521 IN BOOLEAN ReclaimAnyway
524 VARIABLE_HEADER
*Variable
;
525 VARIABLE_HEADER
*AddedVariable
;
526 VARIABLE_HEADER
*NextVariable
;
527 VARIABLE_HEADER
*NextAddedVariable
;
528 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
530 UINTN MaximumBufferSize
;
532 UINTN VariableNameSize
;
533 UINTN UpdatingVariableNameSize
;
540 CHAR16
*VariableNamePtr
;
541 CHAR16
*UpdatingVariableNamePtr
;
542 UINTN CommonVariableTotalSize
;
543 UINTN HwErrVariableTotalSize
;
544 BOOLEAN NeedDoReclaim
;
545 VARIABLE_HEADER
*UpdatingVariable
;
547 UpdatingVariable
= NULL
;
548 if (UpdatingPtrTrack
!= NULL
) {
549 UpdatingVariable
= UpdatingPtrTrack
->CurrPtr
;
552 NeedDoReclaim
= FALSE
;
553 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
555 CommonVariableTotalSize
= 0;
556 HwErrVariableTotalSize
= 0;
559 // Start Pointers for the variable.
561 Variable
= GetStartPointer (VariableStoreHeader
);
562 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
564 while (IsValidVariableHeader (Variable
)) {
565 NextVariable
= GetNextVariablePtr (Variable
);
566 if (Variable
->State
== VAR_ADDED
||
567 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
569 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
570 MaximumBufferSize
+= VariableSize
;
572 NeedDoReclaim
= TRUE
;
575 Variable
= NextVariable
;
578 if (!ReclaimAnyway
&& !NeedDoReclaim
) {
579 DEBUG ((EFI_D_INFO
, "Variable driver: no DELETED variable found, so no variable space could be reclaimed.\n"));
584 // Reserve the 1 Bytes with Oxff to identify the
585 // end of the variable buffer.
587 MaximumBufferSize
+= 1;
588 ValidBuffer
= AllocatePool (MaximumBufferSize
);
589 if (ValidBuffer
== NULL
) {
590 return EFI_OUT_OF_RESOURCES
;
593 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
596 // Copy variable store header.
598 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
599 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
602 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
604 Variable
= GetStartPointer (VariableStoreHeader
);
605 while (IsValidVariableHeader (Variable
)) {
606 NextVariable
= GetNextVariablePtr (Variable
);
607 if (Variable
->State
== VAR_ADDED
) {
608 if (UpdatingVariable
!= NULL
) {
609 if (UpdatingVariable
== Variable
) {
610 Variable
= NextVariable
;
614 VariableNameSize
= NameSizeOfVariable(Variable
);
615 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
617 VariableNamePtr
= GetVariableNamePtr (Variable
);
618 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
619 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
620 VariableNameSize
== UpdatingVariableNameSize
&&
621 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
622 Variable
= NextVariable
;
626 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
627 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
628 CurrPtr
+= VariableSize
;
629 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
630 HwErrVariableTotalSize
+= VariableSize
;
631 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
632 CommonVariableTotalSize
+= VariableSize
;
635 Variable
= NextVariable
;
639 // Reinstall the variable being updated if it is not NULL.
641 if (UpdatingVariable
!= NULL
) {
642 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
643 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
644 UpdatingPtrTrack
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)UpdatingPtrTrack
->StartPtr
+ ((UINTN
)CurrPtr
- (UINTN
)GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
)));
645 UpdatingPtrTrack
->InDeletedTransitionPtr
= NULL
;
646 CurrPtr
+= VariableSize
;
647 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
648 HwErrVariableTotalSize
+= VariableSize
;
649 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
650 CommonVariableTotalSize
+= VariableSize
;
655 // Reinstall all in delete transition variables.
657 Variable
= GetStartPointer (VariableStoreHeader
);
658 while (IsValidVariableHeader (Variable
)) {
659 NextVariable
= GetNextVariablePtr (Variable
);
660 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
663 // Buffer has cached all ADDED variable.
664 // Per IN_DELETED variable, we have to guarantee that
665 // no ADDED one in previous buffer.
669 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
670 while (IsValidVariableHeader (AddedVariable
)) {
671 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
672 NameSize
= NameSizeOfVariable (AddedVariable
);
673 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
674 NameSize
== NameSizeOfVariable (Variable
)
676 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
677 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
678 if (CompareMem (Point0
, Point1
, NameSize
) == 0) {
683 AddedVariable
= NextAddedVariable
;
687 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
689 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
690 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
691 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
692 CurrPtr
+= VariableSize
;
693 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
694 HwErrVariableTotalSize
+= VariableSize
;
695 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
696 CommonVariableTotalSize
+= VariableSize
;
701 Variable
= NextVariable
;
706 // If volatile variable store, just copy valid buffer.
708 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
709 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
710 Status
= EFI_SUCCESS
;
713 // If non-volatile variable store, perform FTW here.
715 Status
= FtwVariableSpace (
718 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
720 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
722 if (!EFI_ERROR (Status
)) {
723 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
725 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
726 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
729 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
730 while (IsValidVariableHeader (NextVariable
)) {
731 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
732 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
733 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
734 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
735 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
738 NextVariable
= GetNextVariablePtr (NextVariable
);
740 *LastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableBase
;
743 FreePool (ValidBuffer
);
749 Find the variable in the specified variable store.
751 @param VariableName Name of the variable to be found
752 @param VendorGuid Vendor GUID to be found.
753 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
754 check at runtime when searching variable.
755 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
757 @retval EFI_SUCCESS Variable found successfully
758 @retval EFI_NOT_FOUND Variable not found
762 IN CHAR16
*VariableName
,
763 IN EFI_GUID
*VendorGuid
,
764 IN BOOLEAN IgnoreRtCheck
,
765 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
768 VARIABLE_HEADER
*InDeletedVariable
;
771 PtrTrack
->InDeletedTransitionPtr
= NULL
;
774 // Find the variable by walk through HOB, volatile and non-volatile variable store.
776 InDeletedVariable
= NULL
;
778 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
779 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
780 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
782 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
783 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
785 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
786 if (VariableName
[0] == 0) {
787 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
788 InDeletedVariable
= PtrTrack
->CurrPtr
;
790 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
794 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
795 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
797 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
798 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
799 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
800 InDeletedVariable
= PtrTrack
->CurrPtr
;
802 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
812 PtrTrack
->CurrPtr
= InDeletedVariable
;
813 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
818 Finds variable in storage blocks of volatile and non-volatile storage areas.
820 This code finds variable in storage blocks of volatile and non-volatile storage areas.
821 If VariableName is an empty string, then we just return the first
822 qualified variable without comparing VariableName and VendorGuid.
823 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
824 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
825 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
827 @param VariableName Name of the variable to be found.
828 @param VendorGuid Vendor GUID to be found.
829 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
830 including the range searched and the target position.
831 @param Global Pointer to VARIABLE_GLOBAL structure, including
832 base of volatile variable storage area, base of
833 NV variable storage area, and a lock.
834 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
835 check at runtime when searching variable.
837 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
839 @retval EFI_SUCCESS Variable successfully found.
840 @retval EFI_NOT_FOUND Variable not found
845 IN CHAR16
*VariableName
,
846 IN EFI_GUID
*VendorGuid
,
847 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
848 IN VARIABLE_GLOBAL
*Global
,
849 IN BOOLEAN IgnoreRtCheck
853 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
854 VARIABLE_STORE_TYPE Type
;
856 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
857 return EFI_INVALID_PARAMETER
;
861 // 0: Volatile, 1: HOB, 2: Non-Volatile.
862 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
863 // make use of this mapping to implement search algorithm.
865 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
866 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
867 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
870 // Find the variable by walk through HOB, volatile and non-volatile variable store.
872 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
873 if (VariableStoreHeader
[Type
] == NULL
) {
877 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
878 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
879 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
881 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
882 if (!EFI_ERROR (Status
)) {
886 return EFI_NOT_FOUND
;
890 Get index from supported language codes according to language string.
892 This code is used to get corresponding index in supported language codes. It can handle
893 RFC4646 and ISO639 language tags.
894 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
895 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
898 SupportedLang = "engfraengfra"
900 Iso639Language = TRUE
901 The return value is "0".
903 SupportedLang = "en;fr;en-US;fr-FR"
905 Iso639Language = FALSE
906 The return value is "3".
908 @param SupportedLang Platform supported language codes.
909 @param Lang Configured language.
910 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
912 @retval The index of language in the language codes.
916 GetIndexFromSupportedLangCodes(
917 IN CHAR8
*SupportedLang
,
919 IN BOOLEAN Iso639Language
924 UINTN LanguageLength
;
926 if (Iso639Language
) {
927 CompareLength
= ISO_639_2_ENTRY_SIZE
;
928 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
929 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
931 // Successfully find the index of Lang string in SupportedLang string.
933 Index
= Index
/ CompareLength
;
941 // Compare RFC4646 language code
944 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
946 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
948 // Skip ';' characters in SupportedLang
950 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
952 // Determine the length of the next language code in SupportedLang
954 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
956 if ((CompareLength
== LanguageLength
) &&
957 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
959 // Successfully find the index of Lang string in SupportedLang string.
970 Get language string from supported language codes according to index.
972 This code is used to get corresponding language strings in supported language codes. It can handle
973 RFC4646 and ISO639 language tags.
974 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
975 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
978 SupportedLang = "engfraengfra"
980 Iso639Language = TRUE
981 The return value is "fra".
983 SupportedLang = "en;fr;en-US;fr-FR"
985 Iso639Language = FALSE
986 The return value is "fr".
988 @param SupportedLang Platform supported language codes.
989 @param Index The index in supported language codes.
990 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
992 @retval The language string in the language codes.
996 GetLangFromSupportedLangCodes (
997 IN CHAR8
*SupportedLang
,
999 IN BOOLEAN Iso639Language
1003 UINTN CompareLength
;
1007 Supported
= SupportedLang
;
1008 if (Iso639Language
) {
1010 // According to the index of Lang string in SupportedLang string to get the language.
1011 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1012 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1014 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1015 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1016 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1021 // Take semicolon as delimitation, sequentially traverse supported language codes.
1023 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1026 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1028 // Have completed the traverse, but not find corrsponding string.
1029 // This case is not allowed to happen.
1034 if (SubIndex
== Index
) {
1036 // According to the index of Lang string in SupportedLang string to get the language.
1037 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1038 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1040 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1041 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1046 // Skip ';' characters in Supported
1048 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1054 Returns a pointer to an allocated buffer that contains the best matching language
1055 from a set of supported languages.
1057 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1058 code types may not be mixed in a single call to this function. This function
1059 supports a variable argument list that allows the caller to pass in a prioritized
1060 list of language codes to test against all the language codes in SupportedLanguages.
1062 If SupportedLanguages is NULL, then ASSERT().
1064 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1065 contains a set of language codes in the format
1066 specified by Iso639Language.
1067 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1068 in ISO 639-2 format. If FALSE, then all language
1069 codes are assumed to be in RFC 4646 language format
1070 @param[in] ... A variable argument list that contains pointers to
1071 Null-terminated ASCII strings that contain one or more
1072 language codes in the format specified by Iso639Language.
1073 The first language code from each of these language
1074 code lists is used to determine if it is an exact or
1075 close match to any of the language codes in
1076 SupportedLanguages. Close matches only apply to RFC 4646
1077 language codes, and the matching algorithm from RFC 4647
1078 is used to determine if a close match is present. If
1079 an exact or close match is found, then the matching
1080 language code from SupportedLanguages is returned. If
1081 no matches are found, then the next variable argument
1082 parameter is evaluated. The variable argument list
1083 is terminated by a NULL.
1085 @retval NULL The best matching language could not be found in SupportedLanguages.
1086 @retval NULL There are not enough resources available to return the best matching
1088 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1089 language in SupportedLanguages.
1094 VariableGetBestLanguage (
1095 IN CONST CHAR8
*SupportedLanguages
,
1096 IN BOOLEAN Iso639Language
,
1102 UINTN CompareLength
;
1103 UINTN LanguageLength
;
1104 CONST CHAR8
*Supported
;
1107 ASSERT (SupportedLanguages
!= NULL
);
1109 VA_START (Args
, Iso639Language
);
1110 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1112 // Default to ISO 639-2 mode
1115 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1118 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1120 if (!Iso639Language
) {
1121 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1125 // Trim back the length of Language used until it is empty
1127 while (LanguageLength
> 0) {
1129 // Loop through all language codes in SupportedLanguages
1131 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1133 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1135 if (!Iso639Language
) {
1137 // Skip ';' characters in Supported
1139 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1141 // Determine the length of the next language code in Supported
1143 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1145 // If Language is longer than the Supported, then skip to the next language
1147 if (LanguageLength
> CompareLength
) {
1152 // See if the first LanguageLength characters in Supported match Language
1154 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1157 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1158 Buffer
[CompareLength
] = '\0';
1159 return CopyMem (Buffer
, Supported
, CompareLength
);
1163 if (Iso639Language
) {
1165 // If ISO 639 mode, then each language can only be tested once
1170 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1172 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1179 // No matches were found
1185 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1187 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1189 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1190 and are read-only. Therefore, in variable driver, only store the original value for other use.
1192 @param[in] VariableName Name of variable.
1194 @param[in] Data Variable data.
1196 @param[in] DataSize Size of data. 0 means delete.
1200 AutoUpdateLangVariable (
1201 IN CHAR16
*VariableName
,
1207 CHAR8
*BestPlatformLang
;
1211 VARIABLE_POINTER_TRACK Variable
;
1212 BOOLEAN SetLanguageCodes
;
1215 // Don't do updates for delete operation
1217 if (DataSize
== 0) {
1221 SetLanguageCodes
= FALSE
;
1223 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1225 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1231 SetLanguageCodes
= TRUE
;
1234 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1235 // Therefore, in variable driver, only store the original value for other use.
1237 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1238 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1240 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1241 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1244 // PlatformLang holds a single language from PlatformLangCodes,
1245 // so the size of PlatformLangCodes is enough for the PlatformLang.
1247 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1248 FreePool (mVariableModuleGlobal
->PlatformLang
);
1250 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1251 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1253 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1255 // LangCodes is a volatile variable, so it can not be updated at runtime.
1261 SetLanguageCodes
= TRUE
;
1264 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1265 // Therefore, in variable driver, only store the original value for other use.
1267 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1268 FreePool (mVariableModuleGlobal
->LangCodes
);
1270 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1271 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1274 if (SetLanguageCodes
1275 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1276 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1278 // Update Lang if PlatformLang is already set
1279 // Update PlatformLang if Lang is already set
1281 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1282 if (!EFI_ERROR (Status
)) {
1286 VariableName
= L
"PlatformLang";
1287 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1288 DataSize
= Variable
.CurrPtr
->DataSize
;
1290 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1291 if (!EFI_ERROR (Status
)) {
1293 // Update PlatformLang
1295 VariableName
= L
"Lang";
1296 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1297 DataSize
= Variable
.CurrPtr
->DataSize
;
1300 // Neither PlatformLang nor Lang is set, directly return
1308 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1310 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1312 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1314 // Update Lang when PlatformLangCodes/LangCodes were set.
1316 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1318 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1320 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1321 if (BestPlatformLang
!= NULL
) {
1323 // Get the corresponding index in language codes.
1325 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1328 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1330 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1333 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1335 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1337 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1338 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
1340 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1342 ASSERT_EFI_ERROR(Status
);
1346 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1348 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1350 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1352 // When setting Lang, firstly get most matched language string from supported language codes.
1354 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1355 if (BestLang
!= NULL
) {
1357 // Get the corresponding index in language codes.
1359 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1362 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1364 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1367 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1369 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1371 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1372 AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
1374 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1375 ASSERT_EFI_ERROR (Status
);
1382 Update the variable region with Variable information. These are the same
1383 arguments as the EFI Variable services.
1385 @param[in] VariableName Name of variable.
1386 @param[in] VendorGuid Guid of variable.
1387 @param[in] Data Variable data.
1388 @param[in] DataSize Size of data. 0 means delete.
1389 @param[in] Attributes Attribues of the variable.
1390 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.
1392 @retval EFI_SUCCESS The update operation is success.
1393 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1398 IN CHAR16
*VariableName
,
1399 IN EFI_GUID
*VendorGuid
,
1402 IN UINT32 Attributes OPTIONAL
,
1403 IN OUT VARIABLE_POINTER_TRACK
*CacheVariable
1407 VARIABLE_HEADER
*NextVariable
;
1409 UINTN NonVolatileVarableStoreSize
;
1410 UINTN VarNameOffset
;
1411 UINTN VarDataOffset
;
1415 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1417 VARIABLE_POINTER_TRACK
*Variable
;
1418 VARIABLE_POINTER_TRACK NvVariable
;
1419 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1422 if ((mVariableModuleGlobal
->FvbInstance
== NULL
) && ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0)) {
1424 // The FVB protocol is not ready. Trying to update NV variable prior to the installation
1425 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
1427 return EFI_NOT_AVAILABLE_YET
;
1430 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1431 Variable
= CacheVariable
;
1434 // Update/Delete existing NV variable.
1435 // CacheVariable points to the variable in the memory copy of Flash area
1436 // Now let Variable points to the same variable in Flash area.
1438 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1439 Variable
= &NvVariable
;
1440 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1441 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1442 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1443 if (CacheVariable
->InDeletedTransitionPtr
!= NULL
) {
1444 Variable
->InDeletedTransitionPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->InDeletedTransitionPtr
- (UINTN
)CacheVariable
->StartPtr
));
1446 Variable
->InDeletedTransitionPtr
= NULL
;
1448 Variable
->Volatile
= FALSE
;
1451 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1453 if (Variable
->CurrPtr
!= NULL
) {
1455 // Update/Delete existing variable.
1459 // If AtRuntime and the variable is Volatile and Runtime Access,
1460 // the volatile is ReadOnly, and SetVariable should be aborted and
1461 // return EFI_WRITE_PROTECTED.
1463 if (Variable
->Volatile
) {
1464 Status
= EFI_WRITE_PROTECTED
;
1468 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.
1470 if (((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0)) {
1471 Status
= EFI_INVALID_PARAMETER
;
1477 // Setting a data variable with no access, or zero DataSize attributes
1478 // causes it to be deleted.
1480 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1481 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
1483 // Both ADDED and IN_DELETED_TRANSITION variable are present,
1484 // set IN_DELETED_TRANSITION one to DELETED state first.
1486 State
= Variable
->InDeletedTransitionPtr
->State
;
1487 State
&= VAR_DELETED
;
1488 Status
= UpdateVariableStore (
1489 &mVariableModuleGlobal
->VariableGlobal
,
1493 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
1497 if (!EFI_ERROR (Status
)) {
1498 if (!Variable
->Volatile
) {
1499 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
1500 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
1507 State
= Variable
->CurrPtr
->State
;
1508 State
&= VAR_DELETED
;
1510 Status
= UpdateVariableStore (
1511 &mVariableModuleGlobal
->VariableGlobal
,
1515 (UINTN
) &Variable
->CurrPtr
->State
,
1519 if (!EFI_ERROR (Status
)) {
1520 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1521 if (!Variable
->Volatile
) {
1522 CacheVariable
->CurrPtr
->State
= State
;
1523 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1529 // If the variable is marked valid, and the same data has been passed in,
1530 // then return to the caller immediately.
1532 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1533 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0)) {
1535 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1536 Status
= EFI_SUCCESS
;
1538 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1539 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1542 // Mark the old variable as in delete transition.
1544 State
= Variable
->CurrPtr
->State
;
1545 State
&= VAR_IN_DELETED_TRANSITION
;
1547 Status
= UpdateVariableStore (
1548 &mVariableModuleGlobal
->VariableGlobal
,
1552 (UINTN
) &Variable
->CurrPtr
->State
,
1556 if (EFI_ERROR (Status
)) {
1559 if (!Variable
->Volatile
) {
1560 CacheVariable
->CurrPtr
->State
= State
;
1565 // Not found existing variable. Create a new variable.
1569 // Make sure we are trying to create a new variable.
1570 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1572 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1573 Status
= EFI_NOT_FOUND
;
1578 // Only variable have NV|RT attribute can be created in Runtime.
1581 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1582 Status
= EFI_INVALID_PARAMETER
;
1588 // Function part - create a new variable and copy the data.
1589 // Both update a variable and create a variable will come here.
1592 // Tricky part: Use scratch data area at the end of volatile variable store
1593 // as a temporary storage.
1595 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1596 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1598 SetMem (NextVariable
, ScratchSize
, 0xff);
1600 NextVariable
->StartId
= VARIABLE_DATA
;
1601 NextVariable
->Attributes
= Attributes
;
1603 // NextVariable->State = VAR_ADDED;
1605 NextVariable
->Reserved
= 0;
1606 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1607 VarNameSize
= StrSize (VariableName
);
1609 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1613 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1615 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1619 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1621 // There will be pad bytes after Data, the NextVariable->NameSize and
1622 // NextVariable->DataSize should not include pad size so that variable
1623 // service can get actual size in GetVariable.
1625 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1626 NextVariable
->DataSize
= (UINT32
)DataSize
;
1629 // The actual size of the variable that stores in storage should
1630 // include pad size.
1632 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1633 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1635 // Create a nonvolatile variable.
1638 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1639 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1640 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1641 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1642 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1644 Status
= EFI_OUT_OF_RESOURCES
;
1648 // Perform garbage collection & reclaim operation.
1650 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1651 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
, FALSE
);
1652 if (EFI_ERROR (Status
)) {
1656 // If still no enough space, return out of resources.
1658 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1659 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1660 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1661 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1662 Status
= EFI_OUT_OF_RESOURCES
;
1665 if (Variable
->CurrPtr
!= NULL
) {
1666 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
1667 CacheVariable
->InDeletedTransitionPtr
= NULL
;
1672 // 1. Write variable header
1673 // 2. Set variable state to header valid
1674 // 3. Write variable data
1675 // 4. Set variable state to valid
1680 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1681 Status
= UpdateVariableStore (
1682 &mVariableModuleGlobal
->VariableGlobal
,
1686 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1687 sizeof (VARIABLE_HEADER
),
1688 (UINT8
*) NextVariable
1691 if (EFI_ERROR (Status
)) {
1698 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1699 Status
= UpdateVariableStore (
1700 &mVariableModuleGlobal
->VariableGlobal
,
1704 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1706 &NextVariable
->State
1709 if (EFI_ERROR (Status
)) {
1715 Status
= UpdateVariableStore (
1716 &mVariableModuleGlobal
->VariableGlobal
,
1720 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1721 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1722 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1725 if (EFI_ERROR (Status
)) {
1731 NextVariable
->State
= VAR_ADDED
;
1732 Status
= UpdateVariableStore (
1733 &mVariableModuleGlobal
->VariableGlobal
,
1737 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1739 &NextVariable
->State
1742 if (EFI_ERROR (Status
)) {
1746 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1748 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1749 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1751 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1754 // update the memory copy of Flash region.
1756 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1759 // Create a volatile variable.
1763 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1764 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1766 // Perform garbage collection & reclaim operation.
1768 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1769 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
, FALSE
);
1770 if (EFI_ERROR (Status
)) {
1774 // If still no enough space, return out of resources.
1776 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1777 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1779 Status
= EFI_OUT_OF_RESOURCES
;
1782 if (Variable
->CurrPtr
!= NULL
) {
1783 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
1784 CacheVariable
->InDeletedTransitionPtr
= NULL
;
1788 NextVariable
->State
= VAR_ADDED
;
1789 Status
= UpdateVariableStore (
1790 &mVariableModuleGlobal
->VariableGlobal
,
1794 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1796 (UINT8
*) NextVariable
1799 if (EFI_ERROR (Status
)) {
1803 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1807 // Mark the old variable as deleted.
1809 if (!EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1810 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
1812 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
1813 // set IN_DELETED_TRANSITION one to DELETED state first.
1815 State
= Variable
->InDeletedTransitionPtr
->State
;
1816 State
&= VAR_DELETED
;
1817 Status
= UpdateVariableStore (
1818 &mVariableModuleGlobal
->VariableGlobal
,
1822 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
1826 if (!EFI_ERROR (Status
)) {
1827 if (!Variable
->Volatile
) {
1828 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
1829 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
1836 State
= Variable
->CurrPtr
->State
;
1837 State
&= VAR_DELETED
;
1839 Status
= UpdateVariableStore (
1840 &mVariableModuleGlobal
->VariableGlobal
,
1844 (UINTN
) &Variable
->CurrPtr
->State
,
1848 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1849 CacheVariable
->CurrPtr
->State
= State
;
1853 if (!EFI_ERROR (Status
)) {
1854 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1856 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1865 Check if a Unicode character is a hexadecimal character.
1867 This function checks if a Unicode character is a
1868 hexadecimal character. The valid hexadecimal character is
1869 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
1872 @param Char The character to check against.
1874 @retval TRUE If the Char is a hexadecmial character.
1875 @retval FALSE If the Char is not a hexadecmial character.
1880 IsHexaDecimalDigitCharacter (
1884 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
1889 This code checks if variable is hardware error record variable or not.
1891 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
1892 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
1894 @param VariableName Pointer to variable name.
1895 @param VendorGuid Variable Vendor Guid.
1897 @retval TRUE Variable is hardware error record variable.
1898 @retval FALSE Variable is not hardware error record variable.
1903 IsHwErrRecVariable (
1904 IN CHAR16
*VariableName
,
1905 IN EFI_GUID
*VendorGuid
1908 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
1909 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
1910 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
1911 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
1912 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
1913 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
1914 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
1923 This code finds variable in storage blocks (Volatile or Non-Volatile).
1925 @param VariableName Name of Variable to be found.
1926 @param VendorGuid Variable vendor GUID.
1927 @param Attributes Attribute value of the variable found.
1928 @param DataSize Size of Data found. If size is less than the
1929 data, this value contains the required size.
1930 @param Data Data pointer.
1932 @return EFI_INVALID_PARAMETER Invalid parameter.
1933 @return EFI_SUCCESS Find the specified variable.
1934 @return EFI_NOT_FOUND Not found.
1935 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1940 VariableServiceGetVariable (
1941 IN CHAR16
*VariableName
,
1942 IN EFI_GUID
*VendorGuid
,
1943 OUT UINT32
*Attributes OPTIONAL
,
1944 IN OUT UINTN
*DataSize
,
1949 VARIABLE_POINTER_TRACK Variable
;
1952 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1953 return EFI_INVALID_PARAMETER
;
1956 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1958 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1959 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1966 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1967 ASSERT (VarDataSize
!= 0);
1969 if (*DataSize
>= VarDataSize
) {
1971 Status
= EFI_INVALID_PARAMETER
;
1975 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1976 if (Attributes
!= NULL
) {
1977 *Attributes
= Variable
.CurrPtr
->Attributes
;
1980 *DataSize
= VarDataSize
;
1981 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1983 Status
= EFI_SUCCESS
;
1986 *DataSize
= VarDataSize
;
1987 Status
= EFI_BUFFER_TOO_SMALL
;
1992 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2000 This code Finds the Next available variable.
2002 @param VariableNameSize Size of the variable name.
2003 @param VariableName Pointer to variable name.
2004 @param VendorGuid Variable Vendor Guid.
2006 @return EFI_INVALID_PARAMETER Invalid parameter.
2007 @return EFI_SUCCESS Find the specified variable.
2008 @return EFI_NOT_FOUND Not found.
2009 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2014 VariableServiceGetNextVariableName (
2015 IN OUT UINTN
*VariableNameSize
,
2016 IN OUT CHAR16
*VariableName
,
2017 IN OUT EFI_GUID
*VendorGuid
2020 VARIABLE_STORE_TYPE Type
;
2021 VARIABLE_POINTER_TRACK Variable
;
2022 VARIABLE_POINTER_TRACK VariableInHob
;
2023 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2026 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2028 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2029 return EFI_INVALID_PARAMETER
;
2032 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2034 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2035 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2039 if (VariableName
[0] != 0) {
2041 // If variable name is not NULL, get next variable.
2043 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2047 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2048 // The index and attributes mapping must be kept in this order as FindVariable
2049 // makes use of this mapping to implement search algorithm.
2051 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2052 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2053 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2057 // Switch from Volatile to HOB, to Non-Volatile.
2059 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
2060 (Variable
.CurrPtr
== NULL
) ||
2061 !IsValidVariableHeader (Variable
.CurrPtr
)
2064 // Find current storage index
2066 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2067 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2071 ASSERT (Type
< VariableStoreTypeMax
);
2073 // Switch to next storage
2075 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2076 if (VariableStoreHeader
[Type
] != NULL
) {
2081 // Capture the case that
2082 // 1. current storage is the last one, or
2083 // 2. no further storage
2085 if (Type
== VariableStoreTypeMax
) {
2086 Status
= EFI_NOT_FOUND
;
2089 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2090 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2091 Variable
.CurrPtr
= Variable
.StartPtr
;
2095 // Variable is found
2097 if (Variable
.CurrPtr
->State
== VAR_ADDED
|| Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2098 if (!AtRuntime () || ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
2099 if (Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2101 // If it is a IN_DELETED_TRANSITION variable,
2102 // and there is also a same ADDED one at the same time,
2105 VariablePtrTrack
.StartPtr
= Variable
.StartPtr
;
2106 VariablePtrTrack
.EndPtr
= Variable
.EndPtr
;
2107 Status
= FindVariableEx (
2108 GetVariableNamePtr (Variable
.CurrPtr
),
2109 &Variable
.CurrPtr
->VendorGuid
,
2113 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
== VAR_ADDED
) {
2114 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2120 // Don't return NV variable when HOB overrides it
2122 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2123 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2125 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2126 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2127 Status
= FindVariableEx (
2128 GetVariableNamePtr (Variable
.CurrPtr
),
2129 &Variable
.CurrPtr
->VendorGuid
,
2133 if (!EFI_ERROR (Status
)) {
2134 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2139 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2140 ASSERT (VarNameSize
!= 0);
2142 if (VarNameSize
<= *VariableNameSize
) {
2143 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2144 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2145 Status
= EFI_SUCCESS
;
2147 Status
= EFI_BUFFER_TOO_SMALL
;
2150 *VariableNameSize
= VarNameSize
;
2155 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2159 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2165 This code sets variable in storage blocks (Volatile or Non-Volatile).
2167 @param VariableName Name of Variable to be found.
2168 @param VendorGuid Variable vendor GUID.
2169 @param Attributes Attribute value of the variable found
2170 @param DataSize Size of Data found. If size is less than the
2171 data, this value contains the required size.
2172 @param Data Data pointer.
2174 @return EFI_INVALID_PARAMETER Invalid parameter.
2175 @return EFI_SUCCESS Set successfully.
2176 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2177 @return EFI_NOT_FOUND Not found.
2178 @return EFI_WRITE_PROTECTED Variable is read-only.
2183 VariableServiceSetVariable (
2184 IN CHAR16
*VariableName
,
2185 IN EFI_GUID
*VendorGuid
,
2186 IN UINT32 Attributes
,
2191 VARIABLE_POINTER_TRACK Variable
;
2193 VARIABLE_HEADER
*NextVariable
;
2194 EFI_PHYSICAL_ADDRESS Point
;
2197 // Check input parameters.
2199 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2200 return EFI_INVALID_PARAMETER
;
2203 if (DataSize
!= 0 && Data
== NULL
) {
2204 return EFI_INVALID_PARAMETER
;
2208 // Not support authenticated variable write yet.
2210 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2211 return EFI_INVALID_PARAMETER
;
2215 // Make sure if runtime bit is set, boot service bit is set also.
2217 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2218 return EFI_INVALID_PARAMETER
;
2222 // The size of the VariableName, including the Unicode Null in bytes plus
2223 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2224 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2226 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2227 if ((DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
2228 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
2229 return EFI_INVALID_PARAMETER
;
2231 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2232 return EFI_INVALID_PARAMETER
;
2236 // The size of the VariableName, including the Unicode Null in bytes plus
2237 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2239 if ((DataSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2240 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxVariableSize
))) {
2241 return EFI_INVALID_PARAMETER
;
2247 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence
2248 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.
2250 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, L
"HwErrRecSupport") == 0)) {
2251 return EFI_WRITE_PROTECTED
;
2255 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2258 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2260 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2261 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2263 // Parse non-volatile variable data and get last variable offset.
2265 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2266 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2267 && IsValidVariableHeader (NextVariable
)) {
2268 NextVariable
= GetNextVariablePtr (NextVariable
);
2270 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2274 // Check whether the input variable is already existed.
2276 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2277 if (!EFI_ERROR (Status
)) {
2278 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2279 return EFI_WRITE_PROTECTED
;
2284 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2286 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2288 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
2290 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2291 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2298 This code returns information about the EFI variables.
2300 @param Attributes Attributes bitmask to specify the type of variables
2301 on which to return information.
2302 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2303 for the EFI variables associated with the attributes specified.
2304 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2305 for EFI variables associated with the attributes specified.
2306 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2307 associated with the attributes specified.
2309 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2310 @return EFI_SUCCESS Query successfully.
2311 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2316 VariableServiceQueryVariableInfo (
2317 IN UINT32 Attributes
,
2318 OUT UINT64
*MaximumVariableStorageSize
,
2319 OUT UINT64
*RemainingVariableStorageSize
,
2320 OUT UINT64
*MaximumVariableSize
2323 VARIABLE_HEADER
*Variable
;
2324 VARIABLE_HEADER
*NextVariable
;
2325 UINT64 VariableSize
;
2326 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2327 UINT64 CommonVariableTotalSize
;
2328 UINT64 HwErrVariableTotalSize
;
2330 CommonVariableTotalSize
= 0;
2331 HwErrVariableTotalSize
= 0;
2333 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2334 return EFI_INVALID_PARAMETER
;
2337 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2339 // Make sure the Attributes combination is supported by the platform.
2341 return EFI_UNSUPPORTED
;
2342 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2344 // Make sure if runtime bit is set, boot service bit is set also.
2346 return EFI_INVALID_PARAMETER
;
2347 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2349 // Make sure RT Attribute is set if we are in Runtime phase.
2351 return EFI_INVALID_PARAMETER
;
2352 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2354 // Make sure Hw Attribute is set with NV.
2356 return EFI_INVALID_PARAMETER
;
2357 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2359 // Not support authentiated variable write yet.
2361 return EFI_UNSUPPORTED
;
2364 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2366 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2368 // Query is Volatile related.
2370 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2373 // Query is Non-Volatile related.
2375 VariableStoreHeader
= mNvVariableCache
;
2379 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2380 // with the storage size (excluding the storage header size).
2382 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2385 // Harware error record variable needs larger size.
2387 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2388 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2389 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2391 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2392 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2393 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2397 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2399 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2403 // Point to the starting address of the variables.
2405 Variable
= GetStartPointer (VariableStoreHeader
);
2408 // Now walk through the related variable store.
2410 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2411 NextVariable
= GetNextVariablePtr (Variable
);
2412 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2416 // We don't take the state of the variables in mind
2417 // when calculating RemainingVariableStorageSize,
2418 // since the space occupied by variables not marked with
2419 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2421 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2422 HwErrVariableTotalSize
+= VariableSize
;
2424 CommonVariableTotalSize
+= VariableSize
;
2428 // Only care about Variables with State VAR_ADDED, because
2429 // the space not marked as VAR_ADDED is reclaimable now.
2431 if (Variable
->State
== VAR_ADDED
) {
2432 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2433 HwErrVariableTotalSize
+= VariableSize
;
2435 CommonVariableTotalSize
+= VariableSize
;
2441 // Go to the next one.
2443 Variable
= NextVariable
;
2446 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2447 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2449 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2452 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2453 *MaximumVariableSize
= 0;
2454 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2455 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2458 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2464 This function reclaims variable storage if free size is below the threshold.
2473 UINTN CommonVariableSpace
;
2474 UINTN RemainingCommonVariableSpace
;
2475 UINTN RemainingHwErrVariableSpace
;
2477 Status
= EFI_SUCCESS
;
2479 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2481 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2483 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2485 // Check if the free area is blow a threshold.
2487 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2488 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2489 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2491 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2492 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2497 ASSERT_EFI_ERROR (Status
);
2502 Flush the HOB variable to flash.
2504 @param[in] VariableName Name of variable has been updated or deleted.
2505 @param[in] VendorGuid Guid of variable has been updated or deleted.
2509 FlushHobVariableToFlash (
2510 IN CHAR16
*VariableName
,
2511 IN EFI_GUID
*VendorGuid
2515 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2516 VARIABLE_HEADER
*Variable
;
2523 // Flush the HOB variable to flash.
2525 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2526 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2528 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
2530 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
2531 for ( Variable
= GetStartPointer (VariableStoreHeader
)
2532 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
2533 ; Variable
= GetNextVariablePtr (Variable
)
2535 if (Variable
->State
!= VAR_ADDED
) {
2537 // The HOB variable has been set to DELETED state in local.
2541 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
2542 if (VendorGuid
== NULL
|| VariableName
== NULL
||
2543 !CompareGuid (VendorGuid
, &Variable
->VendorGuid
) ||
2544 StrCmp (VariableName
, GetVariableNamePtr (Variable
)) != 0) {
2545 VariableData
= GetVariableDataPtr (Variable
);
2546 Status
= VariableServiceSetVariable (
2547 GetVariableNamePtr (Variable
),
2548 &Variable
->VendorGuid
,
2549 Variable
->Attributes
,
2553 DEBUG ((EFI_D_INFO
, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
), Status
));
2556 // The updated or deleted variable is matched with the HOB variable.
2557 // Don't break here because we will try to set other HOB variables
2558 // since this variable could be set successfully.
2560 Status
= EFI_SUCCESS
;
2562 if (!EFI_ERROR (Status
)) {
2564 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
2565 // set the HOB variable to DELETED state in local.
2567 DEBUG ((EFI_D_INFO
, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
)));
2568 Variable
->State
&= VAR_DELETED
;
2575 // We still have HOB variable(s) not flushed in flash.
2577 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
2580 // All HOB variables have been flushed in flash.
2582 DEBUG ((EFI_D_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
2583 if (!AtRuntime ()) {
2584 FreePool ((VOID
*) VariableStoreHeader
);
2592 Initializes variable write service after FVB was ready.
2594 @retval EFI_SUCCESS Function successfully executed.
2595 @retval Others Fail to initialize the variable service.
2599 VariableWriteServiceInitialize (
2604 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2607 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2609 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2610 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2613 // Check if the free area is really free.
2615 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
2616 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2619 // There must be something wrong in variable store, do reclaim operation.
2622 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2623 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2628 if (EFI_ERROR (Status
)) {
2635 FlushHobVariableToFlash (NULL
, NULL
);
2642 Initializes variable store area for non-volatile and volatile variable.
2644 @retval EFI_SUCCESS Function successfully executed.
2645 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2649 VariableCommonInitialize (
2654 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2655 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2656 VARIABLE_HEADER
*NextVariable
;
2657 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2658 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2659 UINT64 VariableStoreLength
;
2662 EFI_HOB_GUID_TYPE
*GuidHob
;
2665 // Allocate runtime memory for variable driver global structure.
2667 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2668 if (mVariableModuleGlobal
== NULL
) {
2669 return EFI_OUT_OF_RESOURCES
;
2672 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2675 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2676 // is stored with common variable in the same NV region. So the platform integrator should
2677 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2678 // PcdFlashNvStorageVariableSize.
2680 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2683 // Get HOB variable store.
2685 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
2686 if (GuidHob
!= NULL
) {
2687 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
2688 VariableStoreLength
= (UINT64
) (GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
));
2689 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
2690 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
2691 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
2692 return EFI_OUT_OF_RESOURCES
;
2695 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
2700 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2702 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2703 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2704 if (VolatileVariableStore
== NULL
) {
2705 FreePool (mVariableModuleGlobal
);
2706 return EFI_OUT_OF_RESOURCES
;
2709 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2712 // Initialize Variable Specific Data.
2714 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2715 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2716 mVariableModuleGlobal
->FvbInstance
= NULL
;
2718 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiVariableGuid
);
2719 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2720 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2721 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2722 VolatileVariableStore
->Reserved
= 0;
2723 VolatileVariableStore
->Reserved1
= 0;
2726 // Get non-volatile variable store.
2729 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2730 if (TempVariableStoreHeader
== 0) {
2731 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2735 // Check if the Firmware Volume is not corrupted
2737 if ((((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->Signature
!= EFI_FVH_SIGNATURE
) ||
2738 (!CompareGuid (&gEfiSystemNvDataFvGuid
, &((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->FileSystemGuid
))) {
2739 Status
= EFI_VOLUME_CORRUPTED
;
2740 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
2744 VariableStoreBase
= TempVariableStoreHeader
+ \
2745 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2746 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2747 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2749 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2750 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2751 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2752 Status
= EFI_VOLUME_CORRUPTED
;
2753 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2756 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2759 // The max variable or hardware error variable size should be < variable store size.
2761 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) < VariableStoreLength
);
2764 // Parse non-volatile variable data and get last variable offset.
2766 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2767 while (IsValidVariableHeader (NextVariable
)) {
2768 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2769 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2770 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2772 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2775 NextVariable
= GetNextVariablePtr (NextVariable
);
2778 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2781 // Allocate runtime memory used for a memory copy of the FLASH region.
2782 // Keep the memory and the FLASH in sync as updates occur
2784 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2785 if (mNvVariableCache
== NULL
) {
2786 Status
= EFI_OUT_OF_RESOURCES
;
2789 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2790 Status
= EFI_SUCCESS
;
2793 if (EFI_ERROR (Status
)) {
2794 FreePool (mVariableModuleGlobal
);
2795 FreePool (VolatileVariableStore
);
2803 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2805 @param[in] Address The Flash address.
2806 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2807 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2811 GetFvbInfoByAddress (
2812 IN EFI_PHYSICAL_ADDRESS Address
,
2813 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2814 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2818 EFI_HANDLE
*HandleBuffer
;
2821 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2822 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2823 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2824 EFI_FVB_ATTRIBUTES_2 Attributes
;
2827 // Get all FVB handles.
2829 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2830 if (EFI_ERROR (Status
)) {
2831 return EFI_NOT_FOUND
;
2835 // Get the FVB to access variable store.
2838 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2839 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2840 if (EFI_ERROR (Status
)) {
2841 Status
= EFI_NOT_FOUND
;
2846 // Ensure this FVB protocol supported Write operation.
2848 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2849 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2854 // Compare the address and select the right one.
2856 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2857 if (EFI_ERROR (Status
)) {
2861 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2862 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
2863 if (FvbHandle
!= NULL
) {
2864 *FvbHandle
= HandleBuffer
[Index
];
2866 if (FvbProtocol
!= NULL
) {
2869 Status
= EFI_SUCCESS
;
2873 FreePool (HandleBuffer
);
2876 Status
= EFI_NOT_FOUND
;