3 The common variable operation routines shared by DXE_RUNTIME variable
4 module and DXE_SMM variable module.
6 Copyright (c) 2006 - 2014, 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
;
48 // To prevent name collisions with possible future globally defined variables,
49 // other internal firmware data variables that are not defined here must be
50 // saved with a unique VendorGuid other than EFI_GLOBAL_VARIABLE or
51 // any other GUID defined by the UEFI Specification. Implementations must
52 // only permit the creation of variables with a UEFI Specification-defined
53 // VendorGuid when these variables are documented in the UEFI Specification.
55 GLOBAL_VARIABLE_ENTRY mGlobalVariableList
[] = {
56 {EFI_LANG_CODES_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
57 {EFI_LANG_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
58 {EFI_TIME_OUT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
59 {EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
60 {EFI_PLATFORM_LANG_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
61 {EFI_CON_IN_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
62 {EFI_CON_OUT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
63 {EFI_ERR_OUT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
64 {EFI_CON_IN_DEV_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
65 {EFI_CON_OUT_DEV_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
66 {EFI_ERR_OUT_DEV_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
67 {EFI_BOOT_ORDER_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
68 {EFI_BOOT_NEXT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
69 {EFI_BOOT_CURRENT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
70 {EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
71 {EFI_DRIVER_ORDER_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
72 {EFI_HW_ERR_REC_SUPPORT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
73 {EFI_SETUP_MODE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
74 {EFI_KEY_EXCHANGE_KEY_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT_AT
},
75 {EFI_PLATFORM_KEY_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT_AT
},
76 {EFI_SIGNATURE_SUPPORT_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
77 {EFI_SECURE_BOOT_MODE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
78 {EFI_KEK_DEFAULT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
79 {EFI_PK_DEFAULT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
80 {EFI_DB_DEFAULT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
81 {EFI_DBX_DEFAULT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
82 {EFI_DBT_DEFAULT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
83 {EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
84 {EFI_OS_INDICATIONS_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_NV_BS_RT
},
85 {EFI_VENDOR_KEYS_VARIABLE_NAME
, VARIABLE_ATTRIBUTE_BS_RT
},
87 GLOBAL_VARIABLE_ENTRY mGlobalVariableList2
[] = {
88 {L
"Boot####", VARIABLE_ATTRIBUTE_NV_BS_RT
},
89 {L
"Driver####", VARIABLE_ATTRIBUTE_NV_BS_RT
},
90 {L
"Key####", VARIABLE_ATTRIBUTE_NV_BS_RT
},
94 Routine used to track statistical information about variable usage.
95 The data is stored in the EFI system table so it can be accessed later.
96 VariableInfo.efi can dump out the table. Only Boot Services variable
97 accesses are tracked by this code. The PcdVariableCollectStatistics
98 build flag controls if this feature is enabled.
100 A read that hits in the cache will have Read and Cache true for
101 the transaction. Data is allocated by this routine, but never
104 @param[in] VariableName Name of the Variable to track.
105 @param[in] VendorGuid Guid of the Variable to track.
106 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
107 @param[in] Read TRUE if GetVariable() was called.
108 @param[in] Write TRUE if SetVariable() was called.
109 @param[in] Delete TRUE if deleted via SetVariable().
110 @param[in] Cache TRUE for a cache hit.
115 IN CHAR16
*VariableName
,
116 IN EFI_GUID
*VendorGuid
,
124 VARIABLE_INFO_ENTRY
*Entry
;
126 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
129 // Don't collect statistics at runtime.
133 if (gVariableInfo
== NULL
) {
135 // On the first call allocate a entry and place a pointer to it in
136 // the EFI System Table.
138 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
139 ASSERT (gVariableInfo
!= NULL
);
141 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
142 gVariableInfo
->Name
= AllocateZeroPool (StrSize (VariableName
));
143 ASSERT (gVariableInfo
->Name
!= NULL
);
144 StrnCpy (gVariableInfo
->Name
, VariableName
, StrLen (VariableName
));
145 gVariableInfo
->Volatile
= Volatile
;
149 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
150 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
151 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
159 Entry
->DeleteCount
++;
169 if (Entry
->Next
== NULL
) {
171 // If the entry is not in the table add it.
172 // Next iteration of the loop will fill in the data.
174 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
175 ASSERT (Entry
->Next
!= NULL
);
177 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
178 Entry
->Next
->Name
= AllocateZeroPool (StrSize (VariableName
));
179 ASSERT (Entry
->Next
->Name
!= NULL
);
180 StrnCpy (Entry
->Next
->Name
, VariableName
, StrLen (VariableName
));
181 Entry
->Next
->Volatile
= Volatile
;
191 This code checks if variable header is valid or not.
193 @param Variable Pointer to the Variable Header.
194 @param VariableStoreEnd Pointer to the Variable Store End.
196 @retval TRUE Variable header is valid.
197 @retval FALSE Variable header is not valid.
201 IsValidVariableHeader (
202 IN VARIABLE_HEADER
*Variable
,
203 IN VARIABLE_HEADER
*VariableStoreEnd
206 if ((Variable
== NULL
) || (Variable
>= VariableStoreEnd
) || (Variable
->StartId
!= VARIABLE_DATA
)) {
208 // Variable is NULL or has reached the end of variable store,
209 // or the StartId is not correct.
220 This function writes data to the FWH at the correct LBA even if the LBAs
223 @param Global Pointer to VARAIBLE_GLOBAL structure.
224 @param Volatile Point out the Variable is Volatile or Non-Volatile.
225 @param SetByIndex TRUE if target pointer is given as index.
226 FALSE if target pointer is absolute.
227 @param Fvb Pointer to the writable FVB protocol.
228 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
230 @param DataSize Size of data to be written.
231 @param Buffer Pointer to the buffer from which data is written.
233 @retval EFI_INVALID_PARAMETER Parameters not valid.
234 @retval EFI_SUCCESS Variable store successfully updated.
238 UpdateVariableStore (
239 IN VARIABLE_GLOBAL
*Global
,
241 IN BOOLEAN SetByIndex
,
242 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
243 IN UINTN DataPtrIndex
,
248 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
256 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
257 VARIABLE_STORE_HEADER
*VolatileBase
;
258 EFI_PHYSICAL_ADDRESS FvVolHdr
;
259 EFI_PHYSICAL_ADDRESS DataPtr
;
263 DataPtr
= DataPtrIndex
;
266 // Check if the Data is Volatile.
269 ASSERT (Fvb
!= NULL
);
270 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
271 ASSERT_EFI_ERROR (Status
);
273 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
275 // Data Pointer should point to the actual Address where data is to be
279 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
282 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
283 return EFI_INVALID_PARAMETER
;
287 // Data Pointer should point to the actual Address where data is to be
290 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
292 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
295 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
296 return EFI_INVALID_PARAMETER
;
300 // If Volatile Variable just do a simple mem copy.
302 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
307 // If we are here we are dealing with Non-Volatile Variables.
309 LinearOffset
= (UINTN
) FwVolHeader
;
310 CurrWritePtr
= (UINTN
) DataPtr
;
311 CurrWriteSize
= DataSize
;
315 if (CurrWritePtr
< LinearOffset
) {
316 return EFI_INVALID_PARAMETER
;
319 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
320 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
322 // Check to see if the Variable Writes are spanning through multiple
325 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
326 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
327 Status
= Fvb
->Write (
330 (UINTN
) (CurrWritePtr
- LinearOffset
),
336 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
337 Status
= Fvb
->Write (
340 (UINTN
) (CurrWritePtr
- LinearOffset
),
344 if (EFI_ERROR (Status
)) {
348 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
349 CurrBuffer
= CurrBuffer
+ Size
;
350 CurrWriteSize
= CurrWriteSize
- Size
;
354 LinearOffset
+= PtrBlockMapEntry
->Length
;
365 This code gets the current status of Variable Store.
367 @param VarStoreHeader Pointer to the Variable Store Header.
369 @retval EfiRaw Variable store status is raw.
370 @retval EfiValid Variable store status is valid.
371 @retval EfiInvalid Variable store status is invalid.
374 VARIABLE_STORE_STATUS
375 GetVariableStoreStatus (
376 IN VARIABLE_STORE_HEADER
*VarStoreHeader
379 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
) &&
380 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
381 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
385 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
386 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
387 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
388 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
389 VarStoreHeader
->Size
== 0xffffffff &&
390 VarStoreHeader
->Format
== 0xff &&
391 VarStoreHeader
->State
== 0xff
403 This code gets the size of name of variable.
405 @param Variable Pointer to the Variable Header.
407 @return UINTN Size of variable in bytes.
412 IN VARIABLE_HEADER
*Variable
415 if (Variable
->State
== (UINT8
) (-1) ||
416 Variable
->DataSize
== (UINT32
) (-1) ||
417 Variable
->NameSize
== (UINT32
) (-1) ||
418 Variable
->Attributes
== (UINT32
) (-1)) {
421 return (UINTN
) Variable
->NameSize
;
426 This code gets the size of variable data.
428 @param Variable Pointer to the Variable Header.
430 @return Size of variable in bytes.
435 IN VARIABLE_HEADER
*Variable
438 if (Variable
->State
== (UINT8
) (-1) ||
439 Variable
->DataSize
== (UINT32
) (-1) ||
440 Variable
->NameSize
== (UINT32
) (-1) ||
441 Variable
->Attributes
== (UINT32
) (-1)) {
444 return (UINTN
) Variable
->DataSize
;
449 This code gets the pointer to the variable name.
451 @param Variable Pointer to the Variable Header.
453 @return Pointer to Variable Name which is Unicode encoding.
458 IN VARIABLE_HEADER
*Variable
462 return (CHAR16
*) (Variable
+ 1);
467 This code gets the pointer to the variable data.
469 @param Variable Pointer to the Variable Header.
471 @return Pointer to Variable Data.
476 IN VARIABLE_HEADER
*Variable
482 // Be careful about pad size for alignment.
484 Value
= (UINTN
) GetVariableNamePtr (Variable
);
485 Value
+= NameSizeOfVariable (Variable
);
486 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
488 return (UINT8
*) Value
;
494 This code gets the pointer to the next variable header.
496 @param Variable Pointer to the Variable Header.
498 @return Pointer to next variable header.
503 IN VARIABLE_HEADER
*Variable
508 Value
= (UINTN
) GetVariableDataPtr (Variable
);
509 Value
+= DataSizeOfVariable (Variable
);
510 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
513 // Be careful about pad size for alignment.
515 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
520 Gets the pointer to the first variable header in given variable store area.
522 @param VarStoreHeader Pointer to the Variable Store Header.
524 @return Pointer to the first variable header.
529 IN VARIABLE_STORE_HEADER
*VarStoreHeader
533 // The end of variable store.
535 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
540 Gets the pointer to the end of the variable storage area.
542 This function gets pointer to the end of the variable storage
543 area, according to the input variable store header.
545 @param VarStoreHeader Pointer to the Variable Store Header.
547 @return Pointer to the end of the variable storage area.
552 IN VARIABLE_STORE_HEADER
*VarStoreHeader
556 // The end of variable store
558 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
564 Variable store garbage collection and reclaim operation.
566 @param VariableBase Base address of variable store.
567 @param LastVariableOffset Offset of last variable.
568 @param IsVolatile The variable store is volatile or not;
569 if it is non-volatile, need FTW.
570 @param UpdatingPtrTrack Pointer to updating variable pointer track structure.
571 @param NewVariable Pointer to new variable.
572 @param NewVariableSize New variable size.
574 @return EFI_OUT_OF_RESOURCES
581 IN EFI_PHYSICAL_ADDRESS VariableBase
,
582 OUT UINTN
*LastVariableOffset
,
583 IN BOOLEAN IsVolatile
,
584 IN OUT VARIABLE_POINTER_TRACK
*UpdatingPtrTrack
,
585 IN VARIABLE_HEADER
*NewVariable
,
586 IN UINTN NewVariableSize
589 VARIABLE_HEADER
*Variable
;
590 VARIABLE_HEADER
*AddedVariable
;
591 VARIABLE_HEADER
*NextVariable
;
592 VARIABLE_HEADER
*NextAddedVariable
;
593 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
595 UINTN MaximumBufferSize
;
603 UINTN CommonVariableTotalSize
;
604 UINTN HwErrVariableTotalSize
;
605 VARIABLE_HEADER
*UpdatingVariable
;
606 VARIABLE_HEADER
*UpdatingInDeletedTransition
;
608 UpdatingVariable
= NULL
;
609 UpdatingInDeletedTransition
= NULL
;
610 if (UpdatingPtrTrack
!= NULL
) {
611 UpdatingVariable
= UpdatingPtrTrack
->CurrPtr
;
612 UpdatingInDeletedTransition
= UpdatingPtrTrack
->InDeletedTransitionPtr
;
615 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
617 CommonVariableTotalSize
= 0;
618 HwErrVariableTotalSize
= 0;
622 // Start Pointers for the variable.
624 Variable
= GetStartPointer (VariableStoreHeader
);
625 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
627 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
628 NextVariable
= GetNextVariablePtr (Variable
);
629 if ((Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) &&
630 Variable
!= UpdatingVariable
&&
631 Variable
!= UpdatingInDeletedTransition
633 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
634 MaximumBufferSize
+= VariableSize
;
637 Variable
= NextVariable
;
640 if (NewVariable
!= NULL
) {
642 // Add the new variable size.
644 MaximumBufferSize
+= NewVariableSize
;
648 // Reserve the 1 Bytes with Oxff to identify the
649 // end of the variable buffer.
651 MaximumBufferSize
+= 1;
652 ValidBuffer
= AllocatePool (MaximumBufferSize
);
653 if (ValidBuffer
== NULL
) {
654 return EFI_OUT_OF_RESOURCES
;
658 // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache
659 // as the buffer to reduce SMRAM consumption for SMM variable driver.
661 MaximumBufferSize
= mNvVariableCache
->Size
;
662 ValidBuffer
= (UINT8
*) mNvVariableCache
;
665 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
668 // Copy variable store header.
670 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
671 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
674 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
676 Variable
= GetStartPointer (VariableStoreHeader
);
677 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
678 NextVariable
= GetNextVariablePtr (Variable
);
679 if (Variable
!= UpdatingVariable
&& Variable
->State
== VAR_ADDED
) {
680 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
681 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
682 CurrPtr
+= VariableSize
;
683 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
684 HwErrVariableTotalSize
+= VariableSize
;
685 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
686 CommonVariableTotalSize
+= VariableSize
;
689 Variable
= NextVariable
;
693 // Reinstall all in delete transition variables.
695 Variable
= GetStartPointer (VariableStoreHeader
);
696 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
697 NextVariable
= GetNextVariablePtr (Variable
);
698 if (Variable
!= UpdatingVariable
&& Variable
!= UpdatingInDeletedTransition
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
701 // Buffer has cached all ADDED variable.
702 // Per IN_DELETED variable, we have to guarantee that
703 // no ADDED one in previous buffer.
707 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
708 while (IsValidVariableHeader (AddedVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
))) {
709 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
710 NameSize
= NameSizeOfVariable (AddedVariable
);
711 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
712 NameSize
== NameSizeOfVariable (Variable
)
714 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
715 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
716 if (CompareMem (Point0
, Point1
, NameSize
) == 0) {
721 AddedVariable
= NextAddedVariable
;
725 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
727 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
728 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
729 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
730 CurrPtr
+= VariableSize
;
731 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
732 HwErrVariableTotalSize
+= VariableSize
;
733 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
734 CommonVariableTotalSize
+= VariableSize
;
739 Variable
= NextVariable
;
743 // Install the new variable if it is not NULL.
745 if (NewVariable
!= NULL
) {
746 if ((UINTN
) (CurrPtr
- ValidBuffer
) + NewVariableSize
> VariableStoreHeader
->Size
) {
748 // No enough space to store the new variable.
750 Status
= EFI_OUT_OF_RESOURCES
;
754 if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
755 HwErrVariableTotalSize
+= NewVariableSize
;
756 } else if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
757 CommonVariableTotalSize
+= NewVariableSize
;
759 if ((HwErrVariableTotalSize
> PcdGet32 (PcdHwErrStorageSize
)) ||
760 (CommonVariableTotalSize
> VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
))) {
762 // No enough space to store the new variable by NV or NV+HR attribute.
764 Status
= EFI_OUT_OF_RESOURCES
;
769 CopyMem (CurrPtr
, (UINT8
*) NewVariable
, NewVariableSize
);
770 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
771 if (UpdatingVariable
!= NULL
) {
772 UpdatingPtrTrack
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)UpdatingPtrTrack
->StartPtr
+ ((UINTN
)CurrPtr
- (UINTN
)GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
)));
773 UpdatingPtrTrack
->InDeletedTransitionPtr
= NULL
;
775 CurrPtr
+= NewVariableSize
;
780 // If volatile variable store, just copy valid buffer.
782 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
783 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- ValidBuffer
));
784 *LastVariableOffset
= (UINTN
) (CurrPtr
- ValidBuffer
);
785 Status
= EFI_SUCCESS
;
788 // If non-volatile variable store, perform FTW here.
790 Status
= FtwVariableSpace (
792 (VARIABLE_STORE_HEADER
*) ValidBuffer
794 if (!EFI_ERROR (Status
)) {
795 *LastVariableOffset
= (UINTN
) (CurrPtr
- ValidBuffer
);
796 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
797 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
799 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
800 while (IsValidVariableHeader (NextVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
))) {
801 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
802 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
803 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
804 } else if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
805 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
808 NextVariable
= GetNextVariablePtr (NextVariable
);
810 *LastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableBase
;
816 FreePool (ValidBuffer
);
819 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.
821 CopyMem (mNvVariableCache
, (UINT8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
828 Find the variable in the specified variable store.
830 @param VariableName Name of the variable to be found
831 @param VendorGuid Vendor GUID to be found.
832 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
833 check at runtime when searching variable.
834 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
836 @retval EFI_SUCCESS Variable found successfully
837 @retval EFI_NOT_FOUND Variable not found
841 IN CHAR16
*VariableName
,
842 IN EFI_GUID
*VendorGuid
,
843 IN BOOLEAN IgnoreRtCheck
,
844 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
847 VARIABLE_HEADER
*InDeletedVariable
;
850 PtrTrack
->InDeletedTransitionPtr
= NULL
;
853 // Find the variable by walk through HOB, volatile and non-volatile variable store.
855 InDeletedVariable
= NULL
;
857 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
858 ; IsValidVariableHeader (PtrTrack
->CurrPtr
, PtrTrack
->EndPtr
)
859 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
861 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
862 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
864 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
865 if (VariableName
[0] == 0) {
866 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
867 InDeletedVariable
= PtrTrack
->CurrPtr
;
869 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
873 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
874 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
876 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
877 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
878 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
879 InDeletedVariable
= PtrTrack
->CurrPtr
;
881 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
891 PtrTrack
->CurrPtr
= InDeletedVariable
;
892 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
897 Finds variable in storage blocks of volatile and non-volatile storage areas.
899 This code finds variable in storage blocks of volatile and non-volatile storage areas.
900 If VariableName is an empty string, then we just return the first
901 qualified variable without comparing VariableName and VendorGuid.
902 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
903 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
904 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
906 @param VariableName Name of the variable to be found.
907 @param VendorGuid Vendor GUID to be found.
908 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
909 including the range searched and the target position.
910 @param Global Pointer to VARIABLE_GLOBAL structure, including
911 base of volatile variable storage area, base of
912 NV variable storage area, and a lock.
913 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
914 check at runtime when searching variable.
916 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
918 @retval EFI_SUCCESS Variable successfully found.
919 @retval EFI_NOT_FOUND Variable not found
924 IN CHAR16
*VariableName
,
925 IN EFI_GUID
*VendorGuid
,
926 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
927 IN VARIABLE_GLOBAL
*Global
,
928 IN BOOLEAN IgnoreRtCheck
932 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
933 VARIABLE_STORE_TYPE Type
;
935 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
936 return EFI_INVALID_PARAMETER
;
940 // 0: Volatile, 1: HOB, 2: Non-Volatile.
941 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
942 // make use of this mapping to implement search algorithm.
944 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
945 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
946 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
949 // Find the variable by walk through HOB, volatile and non-volatile variable store.
951 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
952 if (VariableStoreHeader
[Type
] == NULL
) {
956 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
957 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
958 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
960 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
961 if (!EFI_ERROR (Status
)) {
965 return EFI_NOT_FOUND
;
969 Get index from supported language codes according to language string.
971 This code is used to get corresponding index in supported language codes. It can handle
972 RFC4646 and ISO639 language tags.
973 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
974 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
977 SupportedLang = "engfraengfra"
979 Iso639Language = TRUE
980 The return value is "0".
982 SupportedLang = "en;fr;en-US;fr-FR"
984 Iso639Language = FALSE
985 The return value is "3".
987 @param SupportedLang Platform supported language codes.
988 @param Lang Configured language.
989 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
991 @retval The index of language in the language codes.
995 GetIndexFromSupportedLangCodes(
996 IN CHAR8
*SupportedLang
,
998 IN BOOLEAN Iso639Language
1002 UINTN CompareLength
;
1003 UINTN LanguageLength
;
1005 if (Iso639Language
) {
1006 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1007 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
1008 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
1010 // Successfully find the index of Lang string in SupportedLang string.
1012 Index
= Index
/ CompareLength
;
1020 // Compare RFC4646 language code
1023 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
1025 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
1027 // Skip ';' characters in SupportedLang
1029 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
1031 // Determine the length of the next language code in SupportedLang
1033 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
1035 if ((CompareLength
== LanguageLength
) &&
1036 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
1038 // Successfully find the index of Lang string in SupportedLang string.
1049 Get language string from supported language codes according to index.
1051 This code is used to get corresponding language strings in supported language codes. It can handle
1052 RFC4646 and ISO639 language tags.
1053 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
1054 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
1057 SupportedLang = "engfraengfra"
1059 Iso639Language = TRUE
1060 The return value is "fra".
1062 SupportedLang = "en;fr;en-US;fr-FR"
1064 Iso639Language = FALSE
1065 The return value is "fr".
1067 @param SupportedLang Platform supported language codes.
1068 @param Index The index in supported language codes.
1069 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1071 @retval The language string in the language codes.
1075 GetLangFromSupportedLangCodes (
1076 IN CHAR8
*SupportedLang
,
1078 IN BOOLEAN Iso639Language
1082 UINTN CompareLength
;
1086 Supported
= SupportedLang
;
1087 if (Iso639Language
) {
1089 // According to the index of Lang string in SupportedLang string to get the language.
1090 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1091 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1093 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1094 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1095 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1100 // Take semicolon as delimitation, sequentially traverse supported language codes.
1102 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1105 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1107 // Have completed the traverse, but not find corrsponding string.
1108 // This case is not allowed to happen.
1113 if (SubIndex
== Index
) {
1115 // According to the index of Lang string in SupportedLang string to get the language.
1116 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1117 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1119 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1120 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1125 // Skip ';' characters in Supported
1127 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1133 Returns a pointer to an allocated buffer that contains the best matching language
1134 from a set of supported languages.
1136 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1137 code types may not be mixed in a single call to this function. This function
1138 supports a variable argument list that allows the caller to pass in a prioritized
1139 list of language codes to test against all the language codes in SupportedLanguages.
1141 If SupportedLanguages is NULL, then ASSERT().
1143 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1144 contains a set of language codes in the format
1145 specified by Iso639Language.
1146 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1147 in ISO 639-2 format. If FALSE, then all language
1148 codes are assumed to be in RFC 4646 language format
1149 @param[in] ... A variable argument list that contains pointers to
1150 Null-terminated ASCII strings that contain one or more
1151 language codes in the format specified by Iso639Language.
1152 The first language code from each of these language
1153 code lists is used to determine if it is an exact or
1154 close match to any of the language codes in
1155 SupportedLanguages. Close matches only apply to RFC 4646
1156 language codes, and the matching algorithm from RFC 4647
1157 is used to determine if a close match is present. If
1158 an exact or close match is found, then the matching
1159 language code from SupportedLanguages is returned. If
1160 no matches are found, then the next variable argument
1161 parameter is evaluated. The variable argument list
1162 is terminated by a NULL.
1164 @retval NULL The best matching language could not be found in SupportedLanguages.
1165 @retval NULL There are not enough resources available to return the best matching
1167 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1168 language in SupportedLanguages.
1173 VariableGetBestLanguage (
1174 IN CONST CHAR8
*SupportedLanguages
,
1175 IN BOOLEAN Iso639Language
,
1181 UINTN CompareLength
;
1182 UINTN LanguageLength
;
1183 CONST CHAR8
*Supported
;
1186 ASSERT (SupportedLanguages
!= NULL
);
1188 VA_START (Args
, Iso639Language
);
1189 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1191 // Default to ISO 639-2 mode
1194 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1197 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1199 if (!Iso639Language
) {
1200 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1204 // Trim back the length of Language used until it is empty
1206 while (LanguageLength
> 0) {
1208 // Loop through all language codes in SupportedLanguages
1210 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1212 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1214 if (!Iso639Language
) {
1216 // Skip ';' characters in Supported
1218 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1220 // Determine the length of the next language code in Supported
1222 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1224 // If Language is longer than the Supported, then skip to the next language
1226 if (LanguageLength
> CompareLength
) {
1231 // See if the first LanguageLength characters in Supported match Language
1233 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1236 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1237 Buffer
[CompareLength
] = '\0';
1238 return CopyMem (Buffer
, Supported
, CompareLength
);
1242 if (Iso639Language
) {
1244 // If ISO 639 mode, then each language can only be tested once
1249 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1251 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1258 // No matches were found
1264 This function is to check if the remaining variable space is enough to set
1265 all Variables from argument list successfully. The purpose of the check
1266 is to keep the consistency of the Variables to be in variable storage.
1268 Note: Variables are assumed to be in same storage.
1269 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1270 so follow the argument sequence to check the Variables.
1272 @param[in] Attributes Variable attributes for Variable entries.
1273 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1274 A NULL terminates the list. The VariableSize of
1275 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
1276 It will be changed to variable total size as output.
1278 @retval TRUE Have enough variable space to set the Variables successfully.
1279 @retval FALSE No enough variable space to set the Variables successfully.
1284 CheckRemainingSpaceForConsistency (
1285 IN UINT32 Attributes
,
1291 VARIABLE_ENTRY_CONSISTENCY
*VariableEntry
;
1292 UINT64 MaximumVariableStorageSize
;
1293 UINT64 RemainingVariableStorageSize
;
1294 UINT64 MaximumVariableSize
;
1295 UINTN TotalNeededSize
;
1296 UINTN OriginalVarSize
;
1297 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1298 VARIABLE_POINTER_TRACK VariablePtrTrack
;
1299 VARIABLE_HEADER
*NextVariable
;
1304 // Non-Volatile related.
1306 VariableStoreHeader
= mNvVariableCache
;
1308 Status
= VariableServiceQueryVariableInfoInternal (
1310 &MaximumVariableStorageSize
,
1311 &RemainingVariableStorageSize
,
1312 &MaximumVariableSize
1314 ASSERT_EFI_ERROR (Status
);
1316 TotalNeededSize
= 0;
1317 VA_START (Args
, Attributes
);
1318 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1319 while (VariableEntry
!= NULL
) {
1321 // Calculate variable total size.
1323 VarNameSize
= StrSize (VariableEntry
->Name
);
1324 VarNameSize
+= GET_PAD_SIZE (VarNameSize
);
1325 VarDataSize
= VariableEntry
->VariableSize
;
1326 VarDataSize
+= GET_PAD_SIZE (VarDataSize
);
1327 VariableEntry
->VariableSize
= HEADER_ALIGN (sizeof (VARIABLE_HEADER
) + VarNameSize
+ VarDataSize
);
1329 TotalNeededSize
+= VariableEntry
->VariableSize
;
1330 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1334 if (RemainingVariableStorageSize
>= TotalNeededSize
) {
1336 // Already have enough space.
1339 } else if (AtRuntime ()) {
1341 // At runtime, no reclaim.
1342 // The original variable space of Variables can't be reused.
1347 VA_START (Args
, Attributes
);
1348 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1349 while (VariableEntry
!= NULL
) {
1351 // Check if Variable[Index] has been present and get its size.
1353 OriginalVarSize
= 0;
1354 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
1355 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
1356 Status
= FindVariableEx (
1357 VariableEntry
->Name
,
1358 VariableEntry
->Guid
,
1362 if (!EFI_ERROR (Status
)) {
1364 // Get size of Variable[Index].
1366 NextVariable
= GetNextVariablePtr (VariablePtrTrack
.CurrPtr
);
1367 OriginalVarSize
= (UINTN
) NextVariable
- (UINTN
) VariablePtrTrack
.CurrPtr
;
1369 // Add the original size of Variable[Index] to remaining variable storage size.
1371 RemainingVariableStorageSize
+= OriginalVarSize
;
1373 if (VariableEntry
->VariableSize
> RemainingVariableStorageSize
) {
1375 // No enough space for Variable[Index].
1381 // Sub the (new) size of Variable[Index] from remaining variable storage size.
1383 RemainingVariableStorageSize
-= VariableEntry
->VariableSize
;
1384 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1392 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1394 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1396 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1397 and are read-only. Therefore, in variable driver, only store the original value for other use.
1399 @param[in] VariableName Name of variable.
1401 @param[in] Data Variable data.
1403 @param[in] DataSize Size of data. 0 means delete.
1405 @retval EFI_SUCCESS The update operation is successful or ignored.
1406 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.
1407 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.
1408 @retval Others Other errors happened during the update operation.
1412 AutoUpdateLangVariable (
1413 IN CHAR16
*VariableName
,
1419 CHAR8
*BestPlatformLang
;
1423 VARIABLE_POINTER_TRACK Variable
;
1424 BOOLEAN SetLanguageCodes
;
1425 VARIABLE_ENTRY_CONSISTENCY VariableEntry
[2];
1428 // Don't do updates for delete operation
1430 if (DataSize
== 0) {
1434 SetLanguageCodes
= FALSE
;
1436 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
) == 0) {
1438 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1441 return EFI_WRITE_PROTECTED
;
1444 SetLanguageCodes
= TRUE
;
1447 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1448 // Therefore, in variable driver, only store the original value for other use.
1450 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1451 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1453 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1454 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1457 // PlatformLang holds a single language from PlatformLangCodes,
1458 // so the size of PlatformLangCodes is enough for the PlatformLang.
1460 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1461 FreePool (mVariableModuleGlobal
->PlatformLang
);
1463 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1464 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1466 } else if (StrCmp (VariableName
, EFI_LANG_CODES_VARIABLE_NAME
) == 0) {
1468 // LangCodes is a volatile variable, so it can not be updated at runtime.
1471 return EFI_WRITE_PROTECTED
;
1474 SetLanguageCodes
= TRUE
;
1477 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1478 // Therefore, in variable driver, only store the original value for other use.
1480 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1481 FreePool (mVariableModuleGlobal
->LangCodes
);
1483 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1484 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1487 if (SetLanguageCodes
1488 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1489 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1491 // Update Lang if PlatformLang is already set
1492 // Update PlatformLang if Lang is already set
1494 Status
= FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1495 if (!EFI_ERROR (Status
)) {
1499 VariableName
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1500 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1501 DataSize
= Variable
.CurrPtr
->DataSize
;
1503 Status
= FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1504 if (!EFI_ERROR (Status
)) {
1506 // Update PlatformLang
1508 VariableName
= EFI_LANG_VARIABLE_NAME
;
1509 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1510 DataSize
= Variable
.CurrPtr
->DataSize
;
1513 // Neither PlatformLang nor Lang is set, directly return
1520 Status
= EFI_SUCCESS
;
1523 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1525 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1527 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_VARIABLE_NAME
) == 0) {
1529 // Update Lang when PlatformLangCodes/LangCodes were set.
1531 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1533 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1535 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1536 if (BestPlatformLang
!= NULL
) {
1538 // Get the corresponding index in language codes.
1540 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1543 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1545 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1548 // Check the variable space for both Lang and PlatformLang variable.
1550 VariableEntry
[0].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1551 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1552 VariableEntry
[0].Name
= EFI_LANG_VARIABLE_NAME
;
1554 VariableEntry
[1].VariableSize
= AsciiStrSize (BestPlatformLang
);
1555 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1556 VariableEntry
[1].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1557 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1559 // No enough variable space to set both Lang and PlatformLang successfully.
1561 Status
= EFI_OUT_OF_RESOURCES
;
1564 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1566 FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1568 Status
= UpdateVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestLang
,
1569 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
1572 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang
, BestLang
, Status
));
1576 } else if (StrCmp (VariableName
, EFI_LANG_VARIABLE_NAME
) == 0) {
1578 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1580 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1582 // When setting Lang, firstly get most matched language string from supported language codes.
1584 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1585 if (BestLang
!= NULL
) {
1587 // Get the corresponding index in language codes.
1589 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1592 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1594 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1597 // Check the variable space for both PlatformLang and Lang variable.
1599 VariableEntry
[0].VariableSize
= AsciiStrSize (BestPlatformLang
);
1600 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1601 VariableEntry
[0].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1603 VariableEntry
[1].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1604 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1605 VariableEntry
[1].Name
= EFI_LANG_VARIABLE_NAME
;
1606 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1608 // No enough variable space to set both PlatformLang and Lang successfully.
1610 Status
= EFI_OUT_OF_RESOURCES
;
1613 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1615 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1617 Status
= UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestPlatformLang
,
1618 AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
1621 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang
, BestPlatformLang
, Status
));
1626 if (SetLanguageCodes
) {
1628 // Continue to set PlatformLangCodes or LangCodes.
1637 Update the variable region with Variable information. These are the same
1638 arguments as the EFI Variable services.
1640 @param[in] VariableName Name of variable.
1641 @param[in] VendorGuid Guid of variable.
1642 @param[in] Data Variable data.
1643 @param[in] DataSize Size of data. 0 means delete.
1644 @param[in] Attributes Attribues of the variable.
1645 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.
1647 @retval EFI_SUCCESS The update operation is success.
1648 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1653 IN CHAR16
*VariableName
,
1654 IN EFI_GUID
*VendorGuid
,
1657 IN UINT32 Attributes OPTIONAL
,
1658 IN OUT VARIABLE_POINTER_TRACK
*CacheVariable
1662 VARIABLE_HEADER
*NextVariable
;
1664 UINTN NonVolatileVarableStoreSize
;
1665 UINTN VarNameOffset
;
1666 UINTN VarDataOffset
;
1670 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1672 VARIABLE_POINTER_TRACK
*Variable
;
1673 VARIABLE_POINTER_TRACK NvVariable
;
1674 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1677 if ((mVariableModuleGlobal
->FvbInstance
== NULL
) && ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0)) {
1679 // The FVB protocol is not ready. Trying to update NV variable prior to the installation
1680 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
1682 return EFI_NOT_AVAILABLE_YET
;
1685 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1686 Variable
= CacheVariable
;
1689 // Update/Delete existing NV variable.
1690 // CacheVariable points to the variable in the memory copy of Flash area
1691 // Now let Variable points to the same variable in Flash area.
1693 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1694 Variable
= &NvVariable
;
1695 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1696 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1697 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1698 if (CacheVariable
->InDeletedTransitionPtr
!= NULL
) {
1699 Variable
->InDeletedTransitionPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->InDeletedTransitionPtr
- (UINTN
)CacheVariable
->StartPtr
));
1701 Variable
->InDeletedTransitionPtr
= NULL
;
1703 Variable
->Volatile
= FALSE
;
1706 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1708 if (Variable
->CurrPtr
!= NULL
) {
1710 // Update/Delete existing variable.
1714 // If AtRuntime and the variable is Volatile and Runtime Access,
1715 // the volatile is ReadOnly, and SetVariable should be aborted and
1716 // return EFI_WRITE_PROTECTED.
1718 if (Variable
->Volatile
) {
1719 Status
= EFI_WRITE_PROTECTED
;
1723 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.
1725 if (((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0)) {
1726 Status
= EFI_INVALID_PARAMETER
;
1732 // Setting a data variable with no access, or zero DataSize attributes
1733 // causes it to be deleted.
1735 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1736 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
1738 // Both ADDED and IN_DELETED_TRANSITION variable are present,
1739 // set IN_DELETED_TRANSITION one to DELETED state first.
1741 State
= Variable
->InDeletedTransitionPtr
->State
;
1742 State
&= VAR_DELETED
;
1743 Status
= UpdateVariableStore (
1744 &mVariableModuleGlobal
->VariableGlobal
,
1748 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
1752 if (!EFI_ERROR (Status
)) {
1753 if (!Variable
->Volatile
) {
1754 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
1755 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
1762 State
= Variable
->CurrPtr
->State
;
1763 State
&= VAR_DELETED
;
1765 Status
= UpdateVariableStore (
1766 &mVariableModuleGlobal
->VariableGlobal
,
1770 (UINTN
) &Variable
->CurrPtr
->State
,
1774 if (!EFI_ERROR (Status
)) {
1775 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1776 if (!Variable
->Volatile
) {
1777 CacheVariable
->CurrPtr
->State
= State
;
1778 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1784 // If the variable is marked valid, and the same data has been passed in,
1785 // then return to the caller immediately.
1787 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1788 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0)) {
1790 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1791 Status
= EFI_SUCCESS
;
1793 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1794 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1797 // Mark the old variable as in delete transition.
1799 State
= Variable
->CurrPtr
->State
;
1800 State
&= VAR_IN_DELETED_TRANSITION
;
1802 Status
= UpdateVariableStore (
1803 &mVariableModuleGlobal
->VariableGlobal
,
1807 (UINTN
) &Variable
->CurrPtr
->State
,
1811 if (EFI_ERROR (Status
)) {
1814 if (!Variable
->Volatile
) {
1815 CacheVariable
->CurrPtr
->State
= State
;
1820 // Not found existing variable. Create a new variable.
1824 // Make sure we are trying to create a new variable.
1825 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1827 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1828 Status
= EFI_NOT_FOUND
;
1833 // Only variable have NV|RT attribute can be created in Runtime.
1836 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1837 Status
= EFI_INVALID_PARAMETER
;
1843 // Function part - create a new variable and copy the data.
1844 // Both update a variable and create a variable will come here.
1847 // Tricky part: Use scratch data area at the end of volatile variable store
1848 // as a temporary storage.
1850 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1851 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1853 SetMem (NextVariable
, ScratchSize
, 0xff);
1855 NextVariable
->StartId
= VARIABLE_DATA
;
1856 NextVariable
->Attributes
= Attributes
;
1858 // NextVariable->State = VAR_ADDED;
1860 NextVariable
->Reserved
= 0;
1861 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1862 VarNameSize
= StrSize (VariableName
);
1864 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1868 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1870 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1874 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1876 // There will be pad bytes after Data, the NextVariable->NameSize and
1877 // NextVariable->DataSize should not include pad size so that variable
1878 // service can get actual size in GetVariable.
1880 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1881 NextVariable
->DataSize
= (UINT32
)DataSize
;
1884 // The actual size of the variable that stores in storage should
1885 // include pad size.
1887 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1888 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1890 // Create a nonvolatile variable.
1893 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1894 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1895 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1896 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1897 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1899 Status
= EFI_OUT_OF_RESOURCES
;
1903 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
1905 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1906 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
, NextVariable
, HEADER_ALIGN (VarSize
));
1907 if (!EFI_ERROR (Status
)) {
1909 // The new variable has been integrated successfully during reclaiming.
1911 if (Variable
->CurrPtr
!= NULL
) {
1912 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
1913 CacheVariable
->InDeletedTransitionPtr
= NULL
;
1915 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
);
1916 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1922 // 1. Write variable header
1923 // 2. Set variable state to header valid
1924 // 3. Write variable data
1925 // 4. Set variable state to valid
1930 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1931 Status
= UpdateVariableStore (
1932 &mVariableModuleGlobal
->VariableGlobal
,
1936 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1937 sizeof (VARIABLE_HEADER
),
1938 (UINT8
*) NextVariable
1941 if (EFI_ERROR (Status
)) {
1948 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1949 Status
= UpdateVariableStore (
1950 &mVariableModuleGlobal
->VariableGlobal
,
1954 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1956 &NextVariable
->State
1959 if (EFI_ERROR (Status
)) {
1965 Status
= UpdateVariableStore (
1966 &mVariableModuleGlobal
->VariableGlobal
,
1970 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1971 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1972 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1975 if (EFI_ERROR (Status
)) {
1981 NextVariable
->State
= VAR_ADDED
;
1982 Status
= UpdateVariableStore (
1983 &mVariableModuleGlobal
->VariableGlobal
,
1987 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1989 &NextVariable
->State
1992 if (EFI_ERROR (Status
)) {
1996 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1998 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1999 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2001 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2004 // update the memory copy of Flash region.
2006 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
2009 // Create a volatile variable.
2013 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2014 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
2016 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2018 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
2019 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
, NextVariable
, HEADER_ALIGN (VarSize
));
2020 if (!EFI_ERROR (Status
)) {
2022 // The new variable has been integrated successfully during reclaiming.
2024 if (Variable
->CurrPtr
!= NULL
) {
2025 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2026 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2028 UpdateVariableInfo (VariableName
, VendorGuid
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
);
2033 NextVariable
->State
= VAR_ADDED
;
2034 Status
= UpdateVariableStore (
2035 &mVariableModuleGlobal
->VariableGlobal
,
2039 mVariableModuleGlobal
->VolatileLastVariableOffset
,
2041 (UINT8
*) NextVariable
2044 if (EFI_ERROR (Status
)) {
2048 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2052 // Mark the old variable as deleted.
2054 if (!EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
2055 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2057 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
2058 // set IN_DELETED_TRANSITION one to DELETED state first.
2060 State
= Variable
->InDeletedTransitionPtr
->State
;
2061 State
&= VAR_DELETED
;
2062 Status
= UpdateVariableStore (
2063 &mVariableModuleGlobal
->VariableGlobal
,
2067 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2071 if (!EFI_ERROR (Status
)) {
2072 if (!Variable
->Volatile
) {
2073 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2074 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2081 State
= Variable
->CurrPtr
->State
;
2082 State
&= VAR_DELETED
;
2084 Status
= UpdateVariableStore (
2085 &mVariableModuleGlobal
->VariableGlobal
,
2089 (UINTN
) &Variable
->CurrPtr
->State
,
2093 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
2094 CacheVariable
->CurrPtr
->State
= State
;
2098 if (!EFI_ERROR (Status
)) {
2099 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2101 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2110 Check if a Unicode character is a hexadecimal character.
2112 This function checks if a Unicode character is a
2113 hexadecimal character. The valid hexadecimal character is
2114 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
2117 @param Char The character to check against.
2119 @retval TRUE If the Char is a hexadecmial character.
2120 @retval FALSE If the Char is not a hexadecmial character.
2125 IsHexaDecimalDigitCharacter (
2129 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
2134 This code checks if variable is hardware error record variable or not.
2136 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
2137 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
2139 @param VariableName Pointer to variable name.
2140 @param VendorGuid Variable Vendor Guid.
2142 @retval TRUE Variable is hardware error record variable.
2143 @retval FALSE Variable is not hardware error record variable.
2148 IsHwErrRecVariable (
2149 IN CHAR16
*VariableName
,
2150 IN EFI_GUID
*VendorGuid
2153 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
2154 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
2155 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
2156 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
2157 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
2158 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
2159 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
2167 This code checks if variable guid is global variable guid first.
2168 If yes, further check if variable name is in mGlobalVariableList or mGlobalVariableList2 and attributes matched.
2170 @param[in] VariableName Pointer to variable name.
2171 @param[in] VendorGuid Variable Vendor Guid.
2172 @param[in] Attributes Attributes of the variable.
2174 @retval EFI_SUCCESS Variable is not global variable, or Variable is global variable, variable name is in the lists and attributes matched.
2175 @retval EFI_INVALID_PARAMETER Variable is global variable, but variable name is not in the lists or attributes unmatched.
2180 CheckEfiGlobalVariable (
2181 IN CHAR16
*VariableName
,
2182 IN EFI_GUID
*VendorGuid
,
2183 IN UINT32 Attributes
2189 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)){
2191 // Try list 1, exactly match.
2193 for (Index
= 0; Index
< sizeof (mGlobalVariableList
)/sizeof (mGlobalVariableList
[0]); Index
++) {
2194 if ((StrCmp (mGlobalVariableList
[Index
].Name
, VariableName
) == 0) &&
2195 (Attributes
== 0 || Attributes
== mGlobalVariableList
[Index
].Attributes
)) {
2203 NameLength
= StrLen (VariableName
) - 4;
2204 for (Index
= 0; Index
< sizeof (mGlobalVariableList2
)/sizeof (mGlobalVariableList2
[0]); Index
++) {
2205 if ((StrLen (VariableName
) == StrLen (mGlobalVariableList2
[Index
].Name
)) &&
2206 (StrnCmp (mGlobalVariableList2
[Index
].Name
, VariableName
, NameLength
) == 0) &&
2207 IsHexaDecimalDigitCharacter (VariableName
[NameLength
]) &&
2208 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 1]) &&
2209 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 2]) &&
2210 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 3]) &&
2211 (Attributes
== 0 || Attributes
== mGlobalVariableList2
[Index
].Attributes
)) {
2216 DEBUG ((EFI_D_INFO
, "[Variable]: set global variable with invalid variable name or attributes - %g:%s:%x\n", VendorGuid
, VariableName
, Attributes
));
2217 return EFI_INVALID_PARAMETER
;
2224 Mark a variable that will become read-only after leaving the DXE phase of execution.
2226 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
2227 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
2228 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
2230 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
2231 as pending to be read-only.
2232 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
2233 Or VariableName is an empty string.
2234 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
2235 already been signaled.
2236 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
2240 VariableLockRequestToLock (
2241 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL
*This
,
2242 IN CHAR16
*VariableName
,
2243 IN EFI_GUID
*VendorGuid
2246 VARIABLE_ENTRY
*Entry
;
2248 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2249 return EFI_INVALID_PARAMETER
;
2253 return EFI_ACCESS_DENIED
;
2256 Entry
= AllocateRuntimeZeroPool (sizeof (*Entry
) + StrSize (VariableName
));
2257 if (Entry
== NULL
) {
2258 return EFI_OUT_OF_RESOURCES
;
2261 DEBUG ((EFI_D_INFO
, "[Variable] Lock: %g:%s\n", VendorGuid
, VariableName
));
2263 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2265 Entry
->Name
= (CHAR16
*) (Entry
+ 1);
2266 StrnCpy (Entry
->Name
, VariableName
, StrLen (VariableName
));
2267 CopyGuid (&Entry
->Guid
, VendorGuid
);
2268 InsertTailList (&mLockedVariableList
, &Entry
->Link
);
2270 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2277 This code finds variable in storage blocks (Volatile or Non-Volatile).
2279 @param VariableName Name of Variable to be found.
2280 @param VendorGuid Variable vendor GUID.
2281 @param Attributes Attribute value of the variable found.
2282 @param DataSize Size of Data found. If size is less than the
2283 data, this value contains the required size.
2284 @param Data Data pointer.
2286 @return EFI_INVALID_PARAMETER Invalid parameter.
2287 @return EFI_SUCCESS Find the specified variable.
2288 @return EFI_NOT_FOUND Not found.
2289 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2294 VariableServiceGetVariable (
2295 IN CHAR16
*VariableName
,
2296 IN EFI_GUID
*VendorGuid
,
2297 OUT UINT32
*Attributes OPTIONAL
,
2298 IN OUT UINTN
*DataSize
,
2303 VARIABLE_POINTER_TRACK Variable
;
2306 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2307 return EFI_INVALID_PARAMETER
;
2310 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2312 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2313 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2320 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2321 ASSERT (VarDataSize
!= 0);
2323 if (*DataSize
>= VarDataSize
) {
2325 Status
= EFI_INVALID_PARAMETER
;
2329 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2330 if (Attributes
!= NULL
) {
2331 *Attributes
= Variable
.CurrPtr
->Attributes
;
2334 *DataSize
= VarDataSize
;
2335 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2337 Status
= EFI_SUCCESS
;
2340 *DataSize
= VarDataSize
;
2341 Status
= EFI_BUFFER_TOO_SMALL
;
2346 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2354 This code Finds the Next available variable.
2356 @param VariableNameSize Size of the variable name.
2357 @param VariableName Pointer to variable name.
2358 @param VendorGuid Variable Vendor Guid.
2360 @return EFI_INVALID_PARAMETER Invalid parameter.
2361 @return EFI_SUCCESS Find the specified variable.
2362 @return EFI_NOT_FOUND Not found.
2363 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2368 VariableServiceGetNextVariableName (
2369 IN OUT UINTN
*VariableNameSize
,
2370 IN OUT CHAR16
*VariableName
,
2371 IN OUT EFI_GUID
*VendorGuid
2374 VARIABLE_STORE_TYPE Type
;
2375 VARIABLE_POINTER_TRACK Variable
;
2376 VARIABLE_POINTER_TRACK VariableInHob
;
2377 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2380 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2382 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2383 return EFI_INVALID_PARAMETER
;
2386 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2388 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2389 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2393 if (VariableName
[0] != 0) {
2395 // If variable name is not NULL, get next variable.
2397 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2401 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2402 // The index and attributes mapping must be kept in this order as FindVariable
2403 // makes use of this mapping to implement search algorithm.
2405 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2406 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2407 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2411 // Switch from Volatile to HOB, to Non-Volatile.
2413 while (!IsValidVariableHeader (Variable
.CurrPtr
, Variable
.EndPtr
)) {
2415 // Find current storage index
2417 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2418 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2422 ASSERT (Type
< VariableStoreTypeMax
);
2424 // Switch to next storage
2426 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2427 if (VariableStoreHeader
[Type
] != NULL
) {
2432 // Capture the case that
2433 // 1. current storage is the last one, or
2434 // 2. no further storage
2436 if (Type
== VariableStoreTypeMax
) {
2437 Status
= EFI_NOT_FOUND
;
2440 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2441 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2442 Variable
.CurrPtr
= Variable
.StartPtr
;
2446 // Variable is found
2448 if (Variable
.CurrPtr
->State
== VAR_ADDED
|| Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2449 if (!AtRuntime () || ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
2450 if (Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2452 // If it is a IN_DELETED_TRANSITION variable,
2453 // and there is also a same ADDED one at the same time,
2456 VariablePtrTrack
.StartPtr
= Variable
.StartPtr
;
2457 VariablePtrTrack
.EndPtr
= Variable
.EndPtr
;
2458 Status
= FindVariableEx (
2459 GetVariableNamePtr (Variable
.CurrPtr
),
2460 &Variable
.CurrPtr
->VendorGuid
,
2464 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
== VAR_ADDED
) {
2465 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2471 // Don't return NV variable when HOB overrides it
2473 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2474 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2476 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2477 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2478 Status
= FindVariableEx (
2479 GetVariableNamePtr (Variable
.CurrPtr
),
2480 &Variable
.CurrPtr
->VendorGuid
,
2484 if (!EFI_ERROR (Status
)) {
2485 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2490 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2491 ASSERT (VarNameSize
!= 0);
2493 if (VarNameSize
<= *VariableNameSize
) {
2494 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2495 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2496 Status
= EFI_SUCCESS
;
2498 Status
= EFI_BUFFER_TOO_SMALL
;
2501 *VariableNameSize
= VarNameSize
;
2506 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2510 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2516 This code sets variable in storage blocks (Volatile or Non-Volatile).
2518 @param VariableName Name of Variable to be found.
2519 @param VendorGuid Variable vendor GUID.
2520 @param Attributes Attribute value of the variable found
2521 @param DataSize Size of Data found. If size is less than the
2522 data, this value contains the required size.
2523 @param Data Data pointer.
2525 @return EFI_INVALID_PARAMETER Invalid parameter.
2526 @return EFI_SUCCESS Set successfully.
2527 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2528 @return EFI_NOT_FOUND Not found.
2529 @return EFI_WRITE_PROTECTED Variable is read-only.
2534 VariableServiceSetVariable (
2535 IN CHAR16
*VariableName
,
2536 IN EFI_GUID
*VendorGuid
,
2537 IN UINT32 Attributes
,
2542 VARIABLE_POINTER_TRACK Variable
;
2544 VARIABLE_HEADER
*NextVariable
;
2545 EFI_PHYSICAL_ADDRESS Point
;
2547 VARIABLE_ENTRY
*Entry
;
2550 // Check input parameters.
2552 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2553 return EFI_INVALID_PARAMETER
;
2556 if (DataSize
!= 0 && Data
== NULL
) {
2557 return EFI_INVALID_PARAMETER
;
2561 // Not support authenticated or append variable write yet.
2563 if ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_APPEND_WRITE
)) != 0) {
2564 return EFI_INVALID_PARAMETER
;
2568 // Make sure if runtime bit is set, boot service bit is set also.
2570 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2571 return EFI_INVALID_PARAMETER
;
2574 if ((UINTN
)(~0) - DataSize
< StrSize(VariableName
)){
2576 // Prevent whole variable size overflow
2578 return EFI_INVALID_PARAMETER
;
2582 // The size of the VariableName, including the Unicode Null in bytes plus
2583 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2584 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2586 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2587 if ( StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
)) {
2588 return EFI_INVALID_PARAMETER
;
2590 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2591 return EFI_INVALID_PARAMETER
;
2595 // The size of the VariableName, including the Unicode Null in bytes plus
2596 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2598 if (StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
)) {
2599 return EFI_INVALID_PARAMETER
;
2603 Status
= CheckEfiGlobalVariable (VariableName
, VendorGuid
, Attributes
);
2604 if (EFI_ERROR (Status
)) {
2608 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2611 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2613 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2614 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2616 // Parse non-volatile variable data and get last variable offset.
2618 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2619 while (IsValidVariableHeader (NextVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))) {
2620 NextVariable
= GetNextVariablePtr (NextVariable
);
2622 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2625 if (mEndOfDxe
&& mEnableLocking
) {
2627 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.
2629 for ( Link
= GetFirstNode (&mLockedVariableList
)
2630 ; !IsNull (&mLockedVariableList
, Link
)
2631 ; Link
= GetNextNode (&mLockedVariableList
, Link
)
2633 Entry
= BASE_CR (Link
, VARIABLE_ENTRY
, Link
);
2634 if (CompareGuid (&Entry
->Guid
, VendorGuid
) && (StrCmp (Entry
->Name
, VariableName
) == 0)) {
2635 Status
= EFI_WRITE_PROTECTED
;
2636 DEBUG ((EFI_D_INFO
, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid
, VariableName
));
2643 // Check whether the input variable is already existed.
2645 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2646 if (!EFI_ERROR (Status
)) {
2647 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2648 Status
= EFI_WRITE_PROTECTED
;
2651 if (Attributes
!= 0 && Attributes
!= Variable
.CurrPtr
->Attributes
) {
2653 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not
2654 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:
2655 // 1. No access attributes specified
2656 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE
2658 Status
= EFI_INVALID_PARAMETER
;
2663 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate
)) {
2665 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2667 Status
= AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2668 if (EFI_ERROR (Status
)) {
2670 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.
2676 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
2679 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2680 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2687 This code returns information about the EFI variables.
2689 @param Attributes Attributes bitmask to specify the type of variables
2690 on which to return information.
2691 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2692 for the EFI variables associated with the attributes specified.
2693 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2694 for EFI variables associated with the attributes specified.
2695 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2696 associated with the attributes specified.
2698 @return EFI_SUCCESS Query successfully.
2703 VariableServiceQueryVariableInfoInternal (
2704 IN UINT32 Attributes
,
2705 OUT UINT64
*MaximumVariableStorageSize
,
2706 OUT UINT64
*RemainingVariableStorageSize
,
2707 OUT UINT64
*MaximumVariableSize
2710 VARIABLE_HEADER
*Variable
;
2711 VARIABLE_HEADER
*NextVariable
;
2712 UINT64 VariableSize
;
2713 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2714 UINT64 CommonVariableTotalSize
;
2715 UINT64 HwErrVariableTotalSize
;
2717 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2719 CommonVariableTotalSize
= 0;
2720 HwErrVariableTotalSize
= 0;
2722 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2724 // Query is Volatile related.
2726 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2729 // Query is Non-Volatile related.
2731 VariableStoreHeader
= mNvVariableCache
;
2735 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2736 // with the storage size (excluding the storage header size).
2738 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2741 // Harware error record variable needs larger size.
2743 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2744 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2745 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2747 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2748 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2749 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2753 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2755 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2759 // Point to the starting address of the variables.
2761 Variable
= GetStartPointer (VariableStoreHeader
);
2764 // Now walk through the related variable store.
2766 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
2767 NextVariable
= GetNextVariablePtr (Variable
);
2768 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2772 // We don't take the state of the variables in mind
2773 // when calculating RemainingVariableStorageSize,
2774 // since the space occupied by variables not marked with
2775 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2777 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2778 HwErrVariableTotalSize
+= VariableSize
;
2780 CommonVariableTotalSize
+= VariableSize
;
2784 // Only care about Variables with State VAR_ADDED, because
2785 // the space not marked as VAR_ADDED is reclaimable now.
2787 if (Variable
->State
== VAR_ADDED
) {
2788 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2789 HwErrVariableTotalSize
+= VariableSize
;
2791 CommonVariableTotalSize
+= VariableSize
;
2793 } else if (Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2795 // If it is a IN_DELETED_TRANSITION variable,
2796 // and there is not also a same ADDED one at the same time,
2797 // this IN_DELETED_TRANSITION variable is valid.
2799 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
2800 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
2801 Status
= FindVariableEx (
2802 GetVariableNamePtr (Variable
),
2803 &Variable
->VendorGuid
,
2807 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
!= VAR_ADDED
) {
2808 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2809 HwErrVariableTotalSize
+= VariableSize
;
2811 CommonVariableTotalSize
+= VariableSize
;
2818 // Go to the next one.
2820 Variable
= NextVariable
;
2823 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2824 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2826 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2829 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2830 *MaximumVariableSize
= 0;
2831 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2832 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2840 This code returns information about the EFI variables.
2842 @param Attributes Attributes bitmask to specify the type of variables
2843 on which to return information.
2844 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2845 for the EFI variables associated with the attributes specified.
2846 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2847 for EFI variables associated with the attributes specified.
2848 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2849 associated with the attributes specified.
2851 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2852 @return EFI_SUCCESS Query successfully.
2853 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2858 VariableServiceQueryVariableInfo (
2859 IN UINT32 Attributes
,
2860 OUT UINT64
*MaximumVariableStorageSize
,
2861 OUT UINT64
*RemainingVariableStorageSize
,
2862 OUT UINT64
*MaximumVariableSize
2867 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2868 return EFI_INVALID_PARAMETER
;
2871 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2873 // Make sure the Attributes combination is supported by the platform.
2875 return EFI_UNSUPPORTED
;
2876 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2878 // Make sure if runtime bit is set, boot service bit is set also.
2880 return EFI_INVALID_PARAMETER
;
2881 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2883 // Make sure RT Attribute is set if we are in Runtime phase.
2885 return EFI_INVALID_PARAMETER
;
2886 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2888 // Make sure Hw Attribute is set with NV.
2890 return EFI_INVALID_PARAMETER
;
2891 } else if ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_APPEND_WRITE
)) != 0) {
2893 // Not support authenticated or append variable write yet.
2895 return EFI_UNSUPPORTED
;
2898 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2900 Status
= VariableServiceQueryVariableInfoInternal (
2902 MaximumVariableStorageSize
,
2903 RemainingVariableStorageSize
,
2907 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2912 This function reclaims variable storage if free size is below the threshold.
2921 UINTN CommonVariableSpace
;
2922 UINTN RemainingCommonVariableSpace
;
2923 UINTN RemainingHwErrVariableSpace
;
2925 Status
= EFI_SUCCESS
;
2927 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2929 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2931 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2933 // Check if the free area is blow a threshold.
2935 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2936 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2937 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2939 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2940 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2946 ASSERT_EFI_ERROR (Status
);
2951 Init non-volatile variable store.
2953 @retval EFI_SUCCESS Function successfully executed.
2954 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2955 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.
2959 InitNonVolatileVariableStore (
2963 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
2964 VARIABLE_HEADER
*NextVariable
;
2965 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2966 UINT64 VariableStoreLength
;
2968 EFI_HOB_GUID_TYPE
*GuidHob
;
2969 EFI_PHYSICAL_ADDRESS NvStorageBase
;
2970 UINT8
*NvStorageData
;
2971 UINT32 NvStorageSize
;
2972 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*FtwLastWriteData
;
2973 UINT32 BackUpOffset
;
2976 mVariableModuleGlobal
->FvbInstance
= NULL
;
2979 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2980 // is stored with common variable in the same NV region. So the platform integrator should
2981 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2982 // PcdFlashNvStorageVariableSize.
2984 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2987 // Allocate runtime memory used for a memory copy of the FLASH region.
2988 // Keep the memory and the FLASH in sync as updates occur.
2990 NvStorageSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
2991 NvStorageData
= AllocateRuntimeZeroPool (NvStorageSize
);
2992 if (NvStorageData
== NULL
) {
2993 return EFI_OUT_OF_RESOURCES
;
2996 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2997 if (NvStorageBase
== 0) {
2998 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3001 // Copy NV storage data to the memory buffer.
3003 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) NvStorageBase
, NvStorageSize
);
3006 // Check the FTW last write data hob.
3008 GuidHob
= GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid
);
3009 if (GuidHob
!= NULL
) {
3010 FtwLastWriteData
= (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*) GET_GUID_HOB_DATA (GuidHob
);
3011 if (FtwLastWriteData
->TargetAddress
== NvStorageBase
) {
3012 DEBUG ((EFI_D_INFO
, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN
) FtwLastWriteData
->SpareAddress
));
3014 // Copy the backed up NV storage data to the memory buffer from spare block.
3016 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) (FtwLastWriteData
->SpareAddress
), NvStorageSize
);
3017 } else if ((FtwLastWriteData
->TargetAddress
> NvStorageBase
) &&
3018 (FtwLastWriteData
->TargetAddress
< (NvStorageBase
+ NvStorageSize
))) {
3020 // Flash NV storage from the offset is backed up in spare block.
3022 BackUpOffset
= (UINT32
) (FtwLastWriteData
->TargetAddress
- NvStorageBase
);
3023 BackUpSize
= NvStorageSize
- BackUpOffset
;
3024 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
));
3026 // Copy the partial backed up NV storage data to the memory buffer from spare block.
3028 CopyMem (NvStorageData
+ BackUpOffset
, (UINT8
*) (UINTN
) FtwLastWriteData
->SpareAddress
, BackUpSize
);
3032 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) NvStorageData
;
3035 // Check if the Firmware Volume is not corrupted
3037 if ((FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) || (!CompareGuid (&gEfiSystemNvDataFvGuid
, &FvHeader
->FileSystemGuid
))) {
3038 FreePool (NvStorageData
);
3039 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
3040 return EFI_VOLUME_CORRUPTED
;
3043 VariableStoreBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) FvHeader
+ FvHeader
->HeaderLength
);
3044 VariableStoreLength
= (UINT64
) (NvStorageSize
- FvHeader
->HeaderLength
);
3046 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3047 mNvVariableCache
= (VARIABLE_STORE_HEADER
*) (UINTN
) VariableStoreBase
;
3048 if (GetVariableStoreStatus (mNvVariableCache
) != EfiValid
) {
3049 FreePool (NvStorageData
);
3050 DEBUG((EFI_D_ERROR
, "Variable Store header is corrupted\n"));
3051 return EFI_VOLUME_CORRUPTED
;
3053 ASSERT(mNvVariableCache
->Size
== VariableStoreLength
);
3056 // The max variable or hardware error variable size should be < variable store size.
3058 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) < VariableStoreLength
);
3061 // Parse non-volatile variable data and get last variable offset.
3063 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
3064 while (IsValidVariableHeader (NextVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
))) {
3065 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
3066 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3067 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3069 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3072 NextVariable
= GetNextVariablePtr (NextVariable
);
3074 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
3080 Flush the HOB variable to flash.
3082 @param[in] VariableName Name of variable has been updated or deleted.
3083 @param[in] VendorGuid Guid of variable has been updated or deleted.
3087 FlushHobVariableToFlash (
3088 IN CHAR16
*VariableName
,
3089 IN EFI_GUID
*VendorGuid
3093 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3094 VARIABLE_HEADER
*Variable
;
3101 // Flush the HOB variable to flash.
3103 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3104 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
3106 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
3108 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
3109 for ( Variable
= GetStartPointer (VariableStoreHeader
)
3110 ; IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))
3111 ; Variable
= GetNextVariablePtr (Variable
)
3113 if (Variable
->State
!= VAR_ADDED
) {
3115 // The HOB variable has been set to DELETED state in local.
3119 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
3120 if (VendorGuid
== NULL
|| VariableName
== NULL
||
3121 !CompareGuid (VendorGuid
, &Variable
->VendorGuid
) ||
3122 StrCmp (VariableName
, GetVariableNamePtr (Variable
)) != 0) {
3123 VariableData
= GetVariableDataPtr (Variable
);
3124 Status
= VariableServiceSetVariable (
3125 GetVariableNamePtr (Variable
),
3126 &Variable
->VendorGuid
,
3127 Variable
->Attributes
,
3131 DEBUG ((EFI_D_INFO
, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
), Status
));
3134 // The updated or deleted variable is matched with the HOB variable.
3135 // Don't break here because we will try to set other HOB variables
3136 // since this variable could be set successfully.
3138 Status
= EFI_SUCCESS
;
3140 if (!EFI_ERROR (Status
)) {
3142 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
3143 // set the HOB variable to DELETED state in local.
3145 DEBUG ((EFI_D_INFO
, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
)));
3146 Variable
->State
&= VAR_DELETED
;
3153 // We still have HOB variable(s) not flushed in flash.
3155 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
3158 // All HOB variables have been flushed in flash.
3160 DEBUG ((EFI_D_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
3161 if (!AtRuntime ()) {
3162 FreePool ((VOID
*) VariableStoreHeader
);
3170 Initializes variable write service after FTW was ready.
3172 @retval EFI_SUCCESS Function successfully executed.
3173 @retval Others Fail to initialize the variable service.
3177 VariableWriteServiceInitialize (
3182 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3185 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3186 EFI_PHYSICAL_ADDRESS NvStorageBase
;
3188 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
3189 if (NvStorageBase
== 0) {
3190 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3192 VariableStoreBase
= NvStorageBase
+ (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(NvStorageBase
))->HeaderLength
);
3195 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
3197 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3198 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
3201 // Check if the free area is really free.
3203 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
3204 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
3207 // There must be something wrong in variable store, do reclaim operation.
3210 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3211 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3217 if (EFI_ERROR (Status
)) {
3224 FlushHobVariableToFlash (NULL
, NULL
);
3231 Initializes variable store area for non-volatile and volatile variable.
3233 @retval EFI_SUCCESS Function successfully executed.
3234 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3238 VariableCommonInitialize (
3243 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
3244 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3245 UINT64 VariableStoreLength
;
3247 EFI_HOB_GUID_TYPE
*GuidHob
;
3250 // Allocate runtime memory for variable driver global structure.
3252 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
3253 if (mVariableModuleGlobal
== NULL
) {
3254 return EFI_OUT_OF_RESOURCES
;
3257 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
3260 // Get HOB variable store.
3262 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
3263 if (GuidHob
!= NULL
) {
3264 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
3265 VariableStoreLength
= (UINT64
) (GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
));
3266 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
3267 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
3268 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
3269 FreePool (mVariableModuleGlobal
);
3270 return EFI_OUT_OF_RESOURCES
;
3273 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
3278 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
3280 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
3281 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
3282 if (VolatileVariableStore
== NULL
) {
3283 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3284 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3286 FreePool (mVariableModuleGlobal
);
3287 return EFI_OUT_OF_RESOURCES
;
3290 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
3293 // Initialize Variable Specific Data.
3295 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
3296 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
3298 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiVariableGuid
);
3299 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
3300 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
3301 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
3302 VolatileVariableStore
->Reserved
= 0;
3303 VolatileVariableStore
->Reserved1
= 0;
3306 // Init non-volatile variable store.
3308 Status
= InitNonVolatileVariableStore ();
3309 if (EFI_ERROR (Status
)) {
3310 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3311 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3313 FreePool (mVariableModuleGlobal
);
3314 FreePool (VolatileVariableStore
);
3322 Get the proper fvb handle and/or fvb protocol by the given Flash address.
3324 @param[in] Address The Flash address.
3325 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
3326 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
3330 GetFvbInfoByAddress (
3331 IN EFI_PHYSICAL_ADDRESS Address
,
3332 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
3333 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
3337 EFI_HANDLE
*HandleBuffer
;
3340 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
3341 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
3342 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
3343 EFI_FVB_ATTRIBUTES_2 Attributes
;
3346 HandleBuffer
= NULL
;
3349 // Get all FVB handles.
3351 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
3352 if (EFI_ERROR (Status
)) {
3353 return EFI_NOT_FOUND
;
3357 // Get the FVB to access variable store.
3360 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
3361 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
3362 if (EFI_ERROR (Status
)) {
3363 Status
= EFI_NOT_FOUND
;
3368 // Ensure this FVB protocol supported Write operation.
3370 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
3371 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
3376 // Compare the address and select the right one.
3378 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
3379 if (EFI_ERROR (Status
)) {
3383 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
3384 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
3385 if (FvbHandle
!= NULL
) {
3386 *FvbHandle
= HandleBuffer
[Index
];
3388 if (FvbProtocol
!= NULL
) {
3391 Status
= EFI_SUCCESS
;
3395 FreePool (HandleBuffer
);
3398 Status
= EFI_NOT_FOUND
;