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
= AllocatePool (StrSize (VariableName
));
143 ASSERT (gVariableInfo
->Name
!= NULL
);
144 StrCpy (gVariableInfo
->Name
, 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
= AllocatePool (StrSize (VariableName
));
179 ASSERT (Entry
->Next
->Name
!= NULL
);
180 StrCpy (Entry
->Next
->Name
, 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.
195 @retval TRUE Variable header is valid.
196 @retval FALSE Variable header is not valid.
200 IsValidVariableHeader (
201 IN VARIABLE_HEADER
*Variable
204 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
214 This function writes data to the FWH at the correct LBA even if the LBAs
217 @param Global Pointer to VARAIBLE_GLOBAL structure.
218 @param Volatile Point out the Variable is Volatile or Non-Volatile.
219 @param SetByIndex TRUE if target pointer is given as index.
220 FALSE if target pointer is absolute.
221 @param Fvb Pointer to the writable FVB protocol.
222 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
224 @param DataSize Size of data to be written.
225 @param Buffer Pointer to the buffer from which data is written.
227 @retval EFI_INVALID_PARAMETER Parameters not valid.
228 @retval EFI_SUCCESS Variable store successfully updated.
232 UpdateVariableStore (
233 IN VARIABLE_GLOBAL
*Global
,
235 IN BOOLEAN SetByIndex
,
236 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
237 IN UINTN DataPtrIndex
,
242 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
250 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
251 VARIABLE_STORE_HEADER
*VolatileBase
;
252 EFI_PHYSICAL_ADDRESS FvVolHdr
;
253 EFI_PHYSICAL_ADDRESS DataPtr
;
257 DataPtr
= DataPtrIndex
;
260 // Check if the Data is Volatile.
263 ASSERT (Fvb
!= NULL
);
264 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
265 ASSERT_EFI_ERROR (Status
);
267 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
269 // Data Pointer should point to the actual Address where data is to be
273 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
276 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
277 return EFI_INVALID_PARAMETER
;
281 // Data Pointer should point to the actual Address where data is to be
284 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
286 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
289 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
290 return EFI_INVALID_PARAMETER
;
294 // If Volatile Variable just do a simple mem copy.
296 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
301 // If we are here we are dealing with Non-Volatile Variables.
303 LinearOffset
= (UINTN
) FwVolHeader
;
304 CurrWritePtr
= (UINTN
) DataPtr
;
305 CurrWriteSize
= DataSize
;
309 if (CurrWritePtr
< LinearOffset
) {
310 return EFI_INVALID_PARAMETER
;
313 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
314 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
316 // Check to see if the Variable Writes are spanning through multiple
319 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
320 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
321 Status
= Fvb
->Write (
324 (UINTN
) (CurrWritePtr
- LinearOffset
),
330 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
331 Status
= Fvb
->Write (
334 (UINTN
) (CurrWritePtr
- LinearOffset
),
338 if (EFI_ERROR (Status
)) {
342 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
343 CurrBuffer
= CurrBuffer
+ Size
;
344 CurrWriteSize
= CurrWriteSize
- Size
;
348 LinearOffset
+= PtrBlockMapEntry
->Length
;
359 This code gets the current status of Variable Store.
361 @param VarStoreHeader Pointer to the Variable Store Header.
363 @retval EfiRaw Variable store status is raw.
364 @retval EfiValid Variable store status is valid.
365 @retval EfiInvalid Variable store status is invalid.
368 VARIABLE_STORE_STATUS
369 GetVariableStoreStatus (
370 IN VARIABLE_STORE_HEADER
*VarStoreHeader
373 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
) &&
374 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
375 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
379 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
380 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
381 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
382 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
383 VarStoreHeader
->Size
== 0xffffffff &&
384 VarStoreHeader
->Format
== 0xff &&
385 VarStoreHeader
->State
== 0xff
397 This code gets the size of name of variable.
399 @param Variable Pointer to the Variable Header.
401 @return UINTN Size of variable in bytes.
406 IN VARIABLE_HEADER
*Variable
409 if (Variable
->State
== (UINT8
) (-1) ||
410 Variable
->DataSize
== (UINT32
) (-1) ||
411 Variable
->NameSize
== (UINT32
) (-1) ||
412 Variable
->Attributes
== (UINT32
) (-1)) {
415 return (UINTN
) Variable
->NameSize
;
420 This code gets the size of variable data.
422 @param Variable Pointer to the Variable Header.
424 @return Size of variable in bytes.
429 IN VARIABLE_HEADER
*Variable
432 if (Variable
->State
== (UINT8
) (-1) ||
433 Variable
->DataSize
== (UINT32
) (-1) ||
434 Variable
->NameSize
== (UINT32
) (-1) ||
435 Variable
->Attributes
== (UINT32
) (-1)) {
438 return (UINTN
) Variable
->DataSize
;
443 This code gets the pointer to the variable name.
445 @param Variable Pointer to the Variable Header.
447 @return Pointer to Variable Name which is Unicode encoding.
452 IN VARIABLE_HEADER
*Variable
456 return (CHAR16
*) (Variable
+ 1);
461 This code gets the pointer to the variable data.
463 @param Variable Pointer to the Variable Header.
465 @return Pointer to Variable Data.
470 IN VARIABLE_HEADER
*Variable
476 // Be careful about pad size for alignment.
478 Value
= (UINTN
) GetVariableNamePtr (Variable
);
479 Value
+= NameSizeOfVariable (Variable
);
480 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
482 return (UINT8
*) Value
;
488 This code gets the pointer to the next variable header.
490 @param Variable Pointer to the Variable Header.
492 @return Pointer to next variable header.
497 IN VARIABLE_HEADER
*Variable
502 if (!IsValidVariableHeader (Variable
)) {
506 Value
= (UINTN
) GetVariableDataPtr (Variable
);
507 Value
+= DataSizeOfVariable (Variable
);
508 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
511 // Be careful about pad size for alignment.
513 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
518 Gets the pointer to the first variable header in given variable store area.
520 @param VarStoreHeader Pointer to the Variable Store Header.
522 @return Pointer to the first variable header.
527 IN VARIABLE_STORE_HEADER
*VarStoreHeader
531 // The end of variable store.
533 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
538 Gets the pointer to the end of the variable storage area.
540 This function gets pointer to the end of the variable storage
541 area, according to the input variable store header.
543 @param VarStoreHeader Pointer to the Variable Store Header.
545 @return Pointer to the end of the variable storage area.
550 IN VARIABLE_STORE_HEADER
*VarStoreHeader
554 // The end of variable store
556 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
562 Variable store garbage collection and reclaim operation.
564 @param VariableBase Base address of variable store.
565 @param LastVariableOffset Offset of last variable.
566 @param IsVolatile The variable store is volatile or not;
567 if it is non-volatile, need FTW.
568 @param UpdatingPtrTrack Pointer to updating variable pointer track structure.
569 @param NewVariable Pointer to new variable.
570 @param NewVariableSize New variable size.
572 @return EFI_OUT_OF_RESOURCES
579 IN EFI_PHYSICAL_ADDRESS VariableBase
,
580 OUT UINTN
*LastVariableOffset
,
581 IN BOOLEAN IsVolatile
,
582 IN OUT VARIABLE_POINTER_TRACK
*UpdatingPtrTrack
,
583 IN VARIABLE_HEADER
*NewVariable
,
584 IN UINTN NewVariableSize
587 VARIABLE_HEADER
*Variable
;
588 VARIABLE_HEADER
*AddedVariable
;
589 VARIABLE_HEADER
*NextVariable
;
590 VARIABLE_HEADER
*NextAddedVariable
;
591 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
593 UINTN MaximumBufferSize
;
601 UINTN CommonVariableTotalSize
;
602 UINTN HwErrVariableTotalSize
;
603 VARIABLE_HEADER
*UpdatingVariable
;
604 VARIABLE_HEADER
*UpdatingInDeletedTransition
;
606 UpdatingVariable
= NULL
;
607 UpdatingInDeletedTransition
= NULL
;
608 if (UpdatingPtrTrack
!= NULL
) {
609 UpdatingVariable
= UpdatingPtrTrack
->CurrPtr
;
610 UpdatingInDeletedTransition
= UpdatingPtrTrack
->InDeletedTransitionPtr
;
613 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
615 CommonVariableTotalSize
= 0;
616 HwErrVariableTotalSize
= 0;
620 // Start Pointers for the variable.
622 Variable
= GetStartPointer (VariableStoreHeader
);
623 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
625 while (IsValidVariableHeader (Variable
)) {
626 NextVariable
= GetNextVariablePtr (Variable
);
627 if ((Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) &&
628 Variable
!= UpdatingVariable
&&
629 Variable
!= UpdatingInDeletedTransition
631 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
632 MaximumBufferSize
+= VariableSize
;
635 Variable
= NextVariable
;
638 if (NewVariable
!= NULL
) {
640 // Add the new variable size.
642 MaximumBufferSize
+= NewVariableSize
;
646 // Reserve the 1 Bytes with Oxff to identify the
647 // end of the variable buffer.
649 MaximumBufferSize
+= 1;
650 ValidBuffer
= AllocatePool (MaximumBufferSize
);
651 if (ValidBuffer
== NULL
) {
652 return EFI_OUT_OF_RESOURCES
;
656 // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache
657 // as the buffer to reduce SMRAM consumption for SMM variable driver.
659 MaximumBufferSize
= mNvVariableCache
->Size
;
660 ValidBuffer
= (UINT8
*) mNvVariableCache
;
663 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
666 // Copy variable store header.
668 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
669 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
672 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
674 Variable
= GetStartPointer (VariableStoreHeader
);
675 while (IsValidVariableHeader (Variable
)) {
676 NextVariable
= GetNextVariablePtr (Variable
);
677 if (Variable
!= UpdatingVariable
&& Variable
->State
== VAR_ADDED
) {
678 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
679 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
680 CurrPtr
+= VariableSize
;
681 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
682 HwErrVariableTotalSize
+= VariableSize
;
683 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
684 CommonVariableTotalSize
+= VariableSize
;
687 Variable
= NextVariable
;
691 // Reinstall all in delete transition variables.
693 Variable
= GetStartPointer (VariableStoreHeader
);
694 while (IsValidVariableHeader (Variable
)) {
695 NextVariable
= GetNextVariablePtr (Variable
);
696 if (Variable
!= UpdatingVariable
&& Variable
!= UpdatingInDeletedTransition
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
699 // Buffer has cached all ADDED variable.
700 // Per IN_DELETED variable, we have to guarantee that
701 // no ADDED one in previous buffer.
705 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
706 while (IsValidVariableHeader (AddedVariable
)) {
707 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
708 NameSize
= NameSizeOfVariable (AddedVariable
);
709 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
710 NameSize
== NameSizeOfVariable (Variable
)
712 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
713 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
714 if (CompareMem (Point0
, Point1
, NameSize
) == 0) {
719 AddedVariable
= NextAddedVariable
;
723 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
725 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
726 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
727 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
728 CurrPtr
+= VariableSize
;
729 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
730 HwErrVariableTotalSize
+= VariableSize
;
731 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
732 CommonVariableTotalSize
+= VariableSize
;
737 Variable
= NextVariable
;
741 // Install the new variable if it is not NULL.
743 if (NewVariable
!= NULL
) {
744 if ((UINTN
) (CurrPtr
- ValidBuffer
) + NewVariableSize
> VariableStoreHeader
->Size
) {
746 // No enough space to store the new variable.
748 Status
= EFI_OUT_OF_RESOURCES
;
752 if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
753 HwErrVariableTotalSize
+= NewVariableSize
;
754 } else if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
755 CommonVariableTotalSize
+= NewVariableSize
;
757 if ((HwErrVariableTotalSize
> PcdGet32 (PcdHwErrStorageSize
)) ||
758 (CommonVariableTotalSize
> VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
))) {
760 // No enough space to store the new variable by NV or NV+HR attribute.
762 Status
= EFI_OUT_OF_RESOURCES
;
767 CopyMem (CurrPtr
, (UINT8
*) NewVariable
, NewVariableSize
);
768 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
769 if (UpdatingVariable
!= NULL
) {
770 UpdatingPtrTrack
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)UpdatingPtrTrack
->StartPtr
+ ((UINTN
)CurrPtr
- (UINTN
)GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
)));
771 UpdatingPtrTrack
->InDeletedTransitionPtr
= NULL
;
773 CurrPtr
+= NewVariableSize
;
778 // If volatile variable store, just copy valid buffer.
780 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
781 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- ValidBuffer
));
782 *LastVariableOffset
= (UINTN
) (CurrPtr
- ValidBuffer
);
783 Status
= EFI_SUCCESS
;
786 // If non-volatile variable store, perform FTW here.
788 Status
= FtwVariableSpace (
790 (VARIABLE_STORE_HEADER
*) ValidBuffer
792 if (!EFI_ERROR (Status
)) {
793 *LastVariableOffset
= (UINTN
) (CurrPtr
- ValidBuffer
);
794 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
795 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
797 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
798 while (IsValidVariableHeader (NextVariable
)) {
799 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
800 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
801 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
802 } else if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
803 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
806 NextVariable
= GetNextVariablePtr (NextVariable
);
808 *LastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableBase
;
814 FreePool (ValidBuffer
);
817 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.
819 CopyMem (mNvVariableCache
, (UINT8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
826 Find the variable in the specified variable store.
828 @param VariableName Name of the variable to be found
829 @param VendorGuid Vendor GUID to be found.
830 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
831 check at runtime when searching variable.
832 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
834 @retval EFI_SUCCESS Variable found successfully
835 @retval EFI_NOT_FOUND Variable not found
839 IN CHAR16
*VariableName
,
840 IN EFI_GUID
*VendorGuid
,
841 IN BOOLEAN IgnoreRtCheck
,
842 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
845 VARIABLE_HEADER
*InDeletedVariable
;
848 PtrTrack
->InDeletedTransitionPtr
= NULL
;
851 // Find the variable by walk through HOB, volatile and non-volatile variable store.
853 InDeletedVariable
= NULL
;
855 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
856 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
857 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
859 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
860 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
862 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
863 if (VariableName
[0] == 0) {
864 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
865 InDeletedVariable
= PtrTrack
->CurrPtr
;
867 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
871 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
872 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
874 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
875 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
876 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
877 InDeletedVariable
= PtrTrack
->CurrPtr
;
879 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
889 PtrTrack
->CurrPtr
= InDeletedVariable
;
890 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
895 Finds variable in storage blocks of volatile and non-volatile storage areas.
897 This code finds variable in storage blocks of volatile and non-volatile storage areas.
898 If VariableName is an empty string, then we just return the first
899 qualified variable without comparing VariableName and VendorGuid.
900 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
901 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
902 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
904 @param VariableName Name of the variable to be found.
905 @param VendorGuid Vendor GUID to be found.
906 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
907 including the range searched and the target position.
908 @param Global Pointer to VARIABLE_GLOBAL structure, including
909 base of volatile variable storage area, base of
910 NV variable storage area, and a lock.
911 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
912 check at runtime when searching variable.
914 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
916 @retval EFI_SUCCESS Variable successfully found.
917 @retval EFI_NOT_FOUND Variable not found
922 IN CHAR16
*VariableName
,
923 IN EFI_GUID
*VendorGuid
,
924 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
925 IN VARIABLE_GLOBAL
*Global
,
926 IN BOOLEAN IgnoreRtCheck
930 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
931 VARIABLE_STORE_TYPE Type
;
933 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
934 return EFI_INVALID_PARAMETER
;
938 // 0: Volatile, 1: HOB, 2: Non-Volatile.
939 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
940 // make use of this mapping to implement search algorithm.
942 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
943 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
944 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
947 // Find the variable by walk through HOB, volatile and non-volatile variable store.
949 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
950 if (VariableStoreHeader
[Type
] == NULL
) {
954 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
955 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
956 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
958 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
959 if (!EFI_ERROR (Status
)) {
963 return EFI_NOT_FOUND
;
967 Get index from supported language codes according to language string.
969 This code is used to get corresponding index in supported language codes. It can handle
970 RFC4646 and ISO639 language tags.
971 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
972 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
975 SupportedLang = "engfraengfra"
977 Iso639Language = TRUE
978 The return value is "0".
980 SupportedLang = "en;fr;en-US;fr-FR"
982 Iso639Language = FALSE
983 The return value is "3".
985 @param SupportedLang Platform supported language codes.
986 @param Lang Configured language.
987 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
989 @retval The index of language in the language codes.
993 GetIndexFromSupportedLangCodes(
994 IN CHAR8
*SupportedLang
,
996 IN BOOLEAN Iso639Language
1000 UINTN CompareLength
;
1001 UINTN LanguageLength
;
1003 if (Iso639Language
) {
1004 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1005 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
1006 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
1008 // Successfully find the index of Lang string in SupportedLang string.
1010 Index
= Index
/ CompareLength
;
1018 // Compare RFC4646 language code
1021 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
1023 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
1025 // Skip ';' characters in SupportedLang
1027 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
1029 // Determine the length of the next language code in SupportedLang
1031 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
1033 if ((CompareLength
== LanguageLength
) &&
1034 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
1036 // Successfully find the index of Lang string in SupportedLang string.
1047 Get language string from supported language codes according to index.
1049 This code is used to get corresponding language strings in supported language codes. It can handle
1050 RFC4646 and ISO639 language tags.
1051 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
1052 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
1055 SupportedLang = "engfraengfra"
1057 Iso639Language = TRUE
1058 The return value is "fra".
1060 SupportedLang = "en;fr;en-US;fr-FR"
1062 Iso639Language = FALSE
1063 The return value is "fr".
1065 @param SupportedLang Platform supported language codes.
1066 @param Index The index in supported language codes.
1067 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1069 @retval The language string in the language codes.
1073 GetLangFromSupportedLangCodes (
1074 IN CHAR8
*SupportedLang
,
1076 IN BOOLEAN Iso639Language
1080 UINTN CompareLength
;
1084 Supported
= SupportedLang
;
1085 if (Iso639Language
) {
1087 // According to the index of Lang string in SupportedLang string to get the language.
1088 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1089 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1091 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1092 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1093 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1098 // Take semicolon as delimitation, sequentially traverse supported language codes.
1100 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1103 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1105 // Have completed the traverse, but not find corrsponding string.
1106 // This case is not allowed to happen.
1111 if (SubIndex
== Index
) {
1113 // According to the index of Lang string in SupportedLang string to get the language.
1114 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1115 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1117 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1118 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1123 // Skip ';' characters in Supported
1125 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1131 Returns a pointer to an allocated buffer that contains the best matching language
1132 from a set of supported languages.
1134 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1135 code types may not be mixed in a single call to this function. This function
1136 supports a variable argument list that allows the caller to pass in a prioritized
1137 list of language codes to test against all the language codes in SupportedLanguages.
1139 If SupportedLanguages is NULL, then ASSERT().
1141 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1142 contains a set of language codes in the format
1143 specified by Iso639Language.
1144 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1145 in ISO 639-2 format. If FALSE, then all language
1146 codes are assumed to be in RFC 4646 language format
1147 @param[in] ... A variable argument list that contains pointers to
1148 Null-terminated ASCII strings that contain one or more
1149 language codes in the format specified by Iso639Language.
1150 The first language code from each of these language
1151 code lists is used to determine if it is an exact or
1152 close match to any of the language codes in
1153 SupportedLanguages. Close matches only apply to RFC 4646
1154 language codes, and the matching algorithm from RFC 4647
1155 is used to determine if a close match is present. If
1156 an exact or close match is found, then the matching
1157 language code from SupportedLanguages is returned. If
1158 no matches are found, then the next variable argument
1159 parameter is evaluated. The variable argument list
1160 is terminated by a NULL.
1162 @retval NULL The best matching language could not be found in SupportedLanguages.
1163 @retval NULL There are not enough resources available to return the best matching
1165 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1166 language in SupportedLanguages.
1171 VariableGetBestLanguage (
1172 IN CONST CHAR8
*SupportedLanguages
,
1173 IN BOOLEAN Iso639Language
,
1179 UINTN CompareLength
;
1180 UINTN LanguageLength
;
1181 CONST CHAR8
*Supported
;
1184 ASSERT (SupportedLanguages
!= NULL
);
1186 VA_START (Args
, Iso639Language
);
1187 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1189 // Default to ISO 639-2 mode
1192 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1195 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1197 if (!Iso639Language
) {
1198 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1202 // Trim back the length of Language used until it is empty
1204 while (LanguageLength
> 0) {
1206 // Loop through all language codes in SupportedLanguages
1208 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1210 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1212 if (!Iso639Language
) {
1214 // Skip ';' characters in Supported
1216 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1218 // Determine the length of the next language code in Supported
1220 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1222 // If Language is longer than the Supported, then skip to the next language
1224 if (LanguageLength
> CompareLength
) {
1229 // See if the first LanguageLength characters in Supported match Language
1231 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1234 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1235 Buffer
[CompareLength
] = '\0';
1236 return CopyMem (Buffer
, Supported
, CompareLength
);
1240 if (Iso639Language
) {
1242 // If ISO 639 mode, then each language can only be tested once
1247 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1249 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1256 // No matches were found
1262 This function is to check if the remaining variable space is enough to set
1263 all Variables from argument list successfully. The purpose of the check
1264 is to keep the consistency of the Variables to be in variable storage.
1266 Note: Variables are assumed to be in same storage.
1267 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1268 so follow the argument sequence to check the Variables.
1270 @param[in] Attributes Variable attributes for Variable entries.
1271 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1272 A NULL terminates the list. The VariableSize of
1273 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
1274 It will be changed to variable total size as output.
1276 @retval TRUE Have enough variable space to set the Variables successfully.
1277 @retval FALSE No enough variable space to set the Variables successfully.
1282 CheckRemainingSpaceForConsistency (
1283 IN UINT32 Attributes
,
1289 VARIABLE_ENTRY_CONSISTENCY
*VariableEntry
;
1290 UINT64 MaximumVariableStorageSize
;
1291 UINT64 RemainingVariableStorageSize
;
1292 UINT64 MaximumVariableSize
;
1293 UINTN TotalNeededSize
;
1294 UINTN OriginalVarSize
;
1295 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1296 VARIABLE_POINTER_TRACK VariablePtrTrack
;
1297 VARIABLE_HEADER
*NextVariable
;
1302 // Non-Volatile related.
1304 VariableStoreHeader
= mNvVariableCache
;
1306 Status
= VariableServiceQueryVariableInfoInternal (
1308 &MaximumVariableStorageSize
,
1309 &RemainingVariableStorageSize
,
1310 &MaximumVariableSize
1312 ASSERT_EFI_ERROR (Status
);
1314 TotalNeededSize
= 0;
1315 VA_START (Args
, Attributes
);
1316 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1317 while (VariableEntry
!= NULL
) {
1319 // Calculate variable total size.
1321 VarNameSize
= StrSize (VariableEntry
->Name
);
1322 VarNameSize
+= GET_PAD_SIZE (VarNameSize
);
1323 VarDataSize
= VariableEntry
->VariableSize
;
1324 VarDataSize
+= GET_PAD_SIZE (VarDataSize
);
1325 VariableEntry
->VariableSize
= HEADER_ALIGN (sizeof (VARIABLE_HEADER
) + VarNameSize
+ VarDataSize
);
1327 TotalNeededSize
+= VariableEntry
->VariableSize
;
1328 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1332 if (RemainingVariableStorageSize
>= TotalNeededSize
) {
1334 // Already have enough space.
1337 } else if (AtRuntime ()) {
1339 // At runtime, no reclaim.
1340 // The original variable space of Variables can't be reused.
1345 VA_START (Args
, Attributes
);
1346 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1347 while (VariableEntry
!= NULL
) {
1349 // Check if Variable[Index] has been present and get its size.
1351 OriginalVarSize
= 0;
1352 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
1353 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
1354 Status
= FindVariableEx (
1355 VariableEntry
->Name
,
1356 VariableEntry
->Guid
,
1360 if (!EFI_ERROR (Status
)) {
1362 // Get size of Variable[Index].
1364 NextVariable
= GetNextVariablePtr (VariablePtrTrack
.CurrPtr
);
1365 OriginalVarSize
= (UINTN
) NextVariable
- (UINTN
) VariablePtrTrack
.CurrPtr
;
1367 // Add the original size of Variable[Index] to remaining variable storage size.
1369 RemainingVariableStorageSize
+= OriginalVarSize
;
1371 if (VariableEntry
->VariableSize
> RemainingVariableStorageSize
) {
1373 // No enough space for Variable[Index].
1379 // Sub the (new) size of Variable[Index] from remaining variable storage size.
1381 RemainingVariableStorageSize
-= VariableEntry
->VariableSize
;
1382 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1390 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1392 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1394 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1395 and are read-only. Therefore, in variable driver, only store the original value for other use.
1397 @param[in] VariableName Name of variable.
1399 @param[in] Data Variable data.
1401 @param[in] DataSize Size of data. 0 means delete.
1403 @retval EFI_SUCCESS The update operation is successful or ignored.
1404 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.
1405 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.
1406 @retval Others Other errors happened during the update operation.
1410 AutoUpdateLangVariable (
1411 IN CHAR16
*VariableName
,
1417 CHAR8
*BestPlatformLang
;
1421 VARIABLE_POINTER_TRACK Variable
;
1422 BOOLEAN SetLanguageCodes
;
1423 VARIABLE_ENTRY_CONSISTENCY VariableEntry
[2];
1426 // Don't do updates for delete operation
1428 if (DataSize
== 0) {
1432 SetLanguageCodes
= FALSE
;
1434 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
) == 0) {
1436 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1439 return EFI_WRITE_PROTECTED
;
1442 SetLanguageCodes
= TRUE
;
1445 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1446 // Therefore, in variable driver, only store the original value for other use.
1448 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1449 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1451 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1452 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1455 // PlatformLang holds a single language from PlatformLangCodes,
1456 // so the size of PlatformLangCodes is enough for the PlatformLang.
1458 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1459 FreePool (mVariableModuleGlobal
->PlatformLang
);
1461 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1462 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1464 } else if (StrCmp (VariableName
, EFI_LANG_CODES_VARIABLE_NAME
) == 0) {
1466 // LangCodes is a volatile variable, so it can not be updated at runtime.
1469 return EFI_WRITE_PROTECTED
;
1472 SetLanguageCodes
= TRUE
;
1475 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1476 // Therefore, in variable driver, only store the original value for other use.
1478 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1479 FreePool (mVariableModuleGlobal
->LangCodes
);
1481 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1482 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1485 if (SetLanguageCodes
1486 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1487 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1489 // Update Lang if PlatformLang is already set
1490 // Update PlatformLang if Lang is already set
1492 Status
= FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1493 if (!EFI_ERROR (Status
)) {
1497 VariableName
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1498 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1499 DataSize
= Variable
.CurrPtr
->DataSize
;
1501 Status
= FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1502 if (!EFI_ERROR (Status
)) {
1504 // Update PlatformLang
1506 VariableName
= EFI_LANG_VARIABLE_NAME
;
1507 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1508 DataSize
= Variable
.CurrPtr
->DataSize
;
1511 // Neither PlatformLang nor Lang is set, directly return
1518 Status
= EFI_SUCCESS
;
1521 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1523 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1525 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_VARIABLE_NAME
) == 0) {
1527 // Update Lang when PlatformLangCodes/LangCodes were set.
1529 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1531 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1533 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1534 if (BestPlatformLang
!= NULL
) {
1536 // Get the corresponding index in language codes.
1538 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1541 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1543 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1546 // Check the variable space for both Lang and PlatformLang variable.
1548 VariableEntry
[0].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1549 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1550 VariableEntry
[0].Name
= EFI_LANG_VARIABLE_NAME
;
1552 VariableEntry
[1].VariableSize
= AsciiStrSize (BestPlatformLang
);
1553 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1554 VariableEntry
[1].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1555 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1557 // No enough variable space to set both Lang and PlatformLang successfully.
1559 Status
= EFI_OUT_OF_RESOURCES
;
1562 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1564 FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1566 Status
= UpdateVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestLang
,
1567 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
1570 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang
, BestLang
, Status
));
1574 } else if (StrCmp (VariableName
, EFI_LANG_VARIABLE_NAME
) == 0) {
1576 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1578 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1580 // When setting Lang, firstly get most matched language string from supported language codes.
1582 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1583 if (BestLang
!= NULL
) {
1585 // Get the corresponding index in language codes.
1587 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1590 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1592 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1595 // Check the variable space for both PlatformLang and Lang variable.
1597 VariableEntry
[0].VariableSize
= AsciiStrSize (BestPlatformLang
);
1598 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1599 VariableEntry
[0].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1601 VariableEntry
[1].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1602 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1603 VariableEntry
[1].Name
= EFI_LANG_VARIABLE_NAME
;
1604 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1606 // No enough variable space to set both PlatformLang and Lang successfully.
1608 Status
= EFI_OUT_OF_RESOURCES
;
1611 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1613 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1615 Status
= UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestPlatformLang
,
1616 AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
1619 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang
, BestPlatformLang
, Status
));
1624 if (SetLanguageCodes
) {
1626 // Continue to set PlatformLangCodes or LangCodes.
1635 Update the variable region with Variable information. These are the same
1636 arguments as the EFI Variable services.
1638 @param[in] VariableName Name of variable.
1639 @param[in] VendorGuid Guid of variable.
1640 @param[in] Data Variable data.
1641 @param[in] DataSize Size of data. 0 means delete.
1642 @param[in] Attributes Attribues of the variable.
1643 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.
1645 @retval EFI_SUCCESS The update operation is success.
1646 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1651 IN CHAR16
*VariableName
,
1652 IN EFI_GUID
*VendorGuid
,
1655 IN UINT32 Attributes OPTIONAL
,
1656 IN OUT VARIABLE_POINTER_TRACK
*CacheVariable
1660 VARIABLE_HEADER
*NextVariable
;
1662 UINTN NonVolatileVarableStoreSize
;
1663 UINTN VarNameOffset
;
1664 UINTN VarDataOffset
;
1668 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1670 VARIABLE_POINTER_TRACK
*Variable
;
1671 VARIABLE_POINTER_TRACK NvVariable
;
1672 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1675 if ((mVariableModuleGlobal
->FvbInstance
== NULL
) && ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0)) {
1677 // The FVB protocol is not ready. Trying to update NV variable prior to the installation
1678 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
1680 return EFI_NOT_AVAILABLE_YET
;
1683 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1684 Variable
= CacheVariable
;
1687 // Update/Delete existing NV variable.
1688 // CacheVariable points to the variable in the memory copy of Flash area
1689 // Now let Variable points to the same variable in Flash area.
1691 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1692 Variable
= &NvVariable
;
1693 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1694 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1695 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1696 if (CacheVariable
->InDeletedTransitionPtr
!= NULL
) {
1697 Variable
->InDeletedTransitionPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->InDeletedTransitionPtr
- (UINTN
)CacheVariable
->StartPtr
));
1699 Variable
->InDeletedTransitionPtr
= NULL
;
1701 Variable
->Volatile
= FALSE
;
1704 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1706 if (Variable
->CurrPtr
!= NULL
) {
1708 // Update/Delete existing variable.
1712 // If AtRuntime and the variable is Volatile and Runtime Access,
1713 // the volatile is ReadOnly, and SetVariable should be aborted and
1714 // return EFI_WRITE_PROTECTED.
1716 if (Variable
->Volatile
) {
1717 Status
= EFI_WRITE_PROTECTED
;
1721 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.
1723 if (((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0)) {
1724 Status
= EFI_INVALID_PARAMETER
;
1730 // Setting a data variable with no access, or zero DataSize attributes
1731 // causes it to be deleted.
1733 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1734 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
1736 // Both ADDED and IN_DELETED_TRANSITION variable are present,
1737 // set IN_DELETED_TRANSITION one to DELETED state first.
1739 State
= Variable
->InDeletedTransitionPtr
->State
;
1740 State
&= VAR_DELETED
;
1741 Status
= UpdateVariableStore (
1742 &mVariableModuleGlobal
->VariableGlobal
,
1746 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
1750 if (!EFI_ERROR (Status
)) {
1751 if (!Variable
->Volatile
) {
1752 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
1753 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
1760 State
= Variable
->CurrPtr
->State
;
1761 State
&= VAR_DELETED
;
1763 Status
= UpdateVariableStore (
1764 &mVariableModuleGlobal
->VariableGlobal
,
1768 (UINTN
) &Variable
->CurrPtr
->State
,
1772 if (!EFI_ERROR (Status
)) {
1773 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1774 if (!Variable
->Volatile
) {
1775 CacheVariable
->CurrPtr
->State
= State
;
1776 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1782 // If the variable is marked valid, and the same data has been passed in,
1783 // then return to the caller immediately.
1785 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1786 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0)) {
1788 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1789 Status
= EFI_SUCCESS
;
1791 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1792 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1795 // Mark the old variable as in delete transition.
1797 State
= Variable
->CurrPtr
->State
;
1798 State
&= VAR_IN_DELETED_TRANSITION
;
1800 Status
= UpdateVariableStore (
1801 &mVariableModuleGlobal
->VariableGlobal
,
1805 (UINTN
) &Variable
->CurrPtr
->State
,
1809 if (EFI_ERROR (Status
)) {
1812 if (!Variable
->Volatile
) {
1813 CacheVariable
->CurrPtr
->State
= State
;
1818 // Not found existing variable. Create a new variable.
1822 // Make sure we are trying to create a new variable.
1823 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1825 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1826 Status
= EFI_NOT_FOUND
;
1831 // Only variable have NV|RT attribute can be created in Runtime.
1834 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1835 Status
= EFI_INVALID_PARAMETER
;
1841 // Function part - create a new variable and copy the data.
1842 // Both update a variable and create a variable will come here.
1845 // Tricky part: Use scratch data area at the end of volatile variable store
1846 // as a temporary storage.
1848 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1849 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1851 SetMem (NextVariable
, ScratchSize
, 0xff);
1853 NextVariable
->StartId
= VARIABLE_DATA
;
1854 NextVariable
->Attributes
= Attributes
;
1856 // NextVariable->State = VAR_ADDED;
1858 NextVariable
->Reserved
= 0;
1859 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1860 VarNameSize
= StrSize (VariableName
);
1862 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1866 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1868 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1872 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1874 // There will be pad bytes after Data, the NextVariable->NameSize and
1875 // NextVariable->DataSize should not include pad size so that variable
1876 // service can get actual size in GetVariable.
1878 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1879 NextVariable
->DataSize
= (UINT32
)DataSize
;
1882 // The actual size of the variable that stores in storage should
1883 // include pad size.
1885 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1886 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1888 // Create a nonvolatile variable.
1891 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1892 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1893 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1894 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1895 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1897 Status
= EFI_OUT_OF_RESOURCES
;
1901 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
1903 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1904 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
, NextVariable
, HEADER_ALIGN (VarSize
));
1905 if (!EFI_ERROR (Status
)) {
1907 // The new variable has been integrated successfully during reclaiming.
1909 if (Variable
->CurrPtr
!= NULL
) {
1910 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
1911 CacheVariable
->InDeletedTransitionPtr
= NULL
;
1913 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
);
1914 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1920 // 1. Write variable header
1921 // 2. Set variable state to header valid
1922 // 3. Write variable data
1923 // 4. Set variable state to valid
1928 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1929 Status
= UpdateVariableStore (
1930 &mVariableModuleGlobal
->VariableGlobal
,
1934 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1935 sizeof (VARIABLE_HEADER
),
1936 (UINT8
*) NextVariable
1939 if (EFI_ERROR (Status
)) {
1946 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1947 Status
= UpdateVariableStore (
1948 &mVariableModuleGlobal
->VariableGlobal
,
1952 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1954 &NextVariable
->State
1957 if (EFI_ERROR (Status
)) {
1963 Status
= UpdateVariableStore (
1964 &mVariableModuleGlobal
->VariableGlobal
,
1968 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1969 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1970 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1973 if (EFI_ERROR (Status
)) {
1979 NextVariable
->State
= VAR_ADDED
;
1980 Status
= UpdateVariableStore (
1981 &mVariableModuleGlobal
->VariableGlobal
,
1985 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1987 &NextVariable
->State
1990 if (EFI_ERROR (Status
)) {
1994 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1996 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1997 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1999 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2002 // update the memory copy of Flash region.
2004 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
2007 // Create a volatile variable.
2011 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2012 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
2014 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2016 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
2017 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
, NextVariable
, HEADER_ALIGN (VarSize
));
2018 if (!EFI_ERROR (Status
)) {
2020 // The new variable has been integrated successfully during reclaiming.
2022 if (Variable
->CurrPtr
!= NULL
) {
2023 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2024 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2026 UpdateVariableInfo (VariableName
, VendorGuid
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
);
2031 NextVariable
->State
= VAR_ADDED
;
2032 Status
= UpdateVariableStore (
2033 &mVariableModuleGlobal
->VariableGlobal
,
2037 mVariableModuleGlobal
->VolatileLastVariableOffset
,
2039 (UINT8
*) NextVariable
2042 if (EFI_ERROR (Status
)) {
2046 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2050 // Mark the old variable as deleted.
2052 if (!EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
2053 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2055 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
2056 // set IN_DELETED_TRANSITION one to DELETED state first.
2058 State
= Variable
->InDeletedTransitionPtr
->State
;
2059 State
&= VAR_DELETED
;
2060 Status
= UpdateVariableStore (
2061 &mVariableModuleGlobal
->VariableGlobal
,
2065 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2069 if (!EFI_ERROR (Status
)) {
2070 if (!Variable
->Volatile
) {
2071 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2072 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2079 State
= Variable
->CurrPtr
->State
;
2080 State
&= VAR_DELETED
;
2082 Status
= UpdateVariableStore (
2083 &mVariableModuleGlobal
->VariableGlobal
,
2087 (UINTN
) &Variable
->CurrPtr
->State
,
2091 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
2092 CacheVariable
->CurrPtr
->State
= State
;
2096 if (!EFI_ERROR (Status
)) {
2097 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
2099 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2108 Check if a Unicode character is a hexadecimal character.
2110 This function checks if a Unicode character is a
2111 hexadecimal character. The valid hexadecimal character is
2112 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
2115 @param Char The character to check against.
2117 @retval TRUE If the Char is a hexadecmial character.
2118 @retval FALSE If the Char is not a hexadecmial character.
2123 IsHexaDecimalDigitCharacter (
2127 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
2132 This code checks if variable is hardware error record variable or not.
2134 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
2135 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
2137 @param VariableName Pointer to variable name.
2138 @param VendorGuid Variable Vendor Guid.
2140 @retval TRUE Variable is hardware error record variable.
2141 @retval FALSE Variable is not hardware error record variable.
2146 IsHwErrRecVariable (
2147 IN CHAR16
*VariableName
,
2148 IN EFI_GUID
*VendorGuid
2151 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
2152 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
2153 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
2154 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
2155 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
2156 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
2157 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
2165 This code checks if variable guid is global variable guid first.
2166 If yes, further check if variable name is in mGlobalVariableList or mGlobalVariableList2 and attributes matched.
2168 @param[in] VariableName Pointer to variable name.
2169 @param[in] VendorGuid Variable Vendor Guid.
2170 @param[in] Attributes Attributes of the variable.
2172 @retval EFI_SUCCESS Variable is not global variable, or Variable is global variable, variable name is in the lists and attributes matched.
2173 @retval EFI_INVALID_PARAMETER Variable is global variable, but variable name is not in the lists or attributes unmatched.
2178 CheckEfiGlobalVariable (
2179 IN CHAR16
*VariableName
,
2180 IN EFI_GUID
*VendorGuid
,
2181 IN UINT32 Attributes
2187 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)){
2189 // Try list 1, exactly match.
2191 for (Index
= 0; Index
< sizeof (mGlobalVariableList
)/sizeof (mGlobalVariableList
[0]); Index
++) {
2192 if ((StrCmp (mGlobalVariableList
[Index
].Name
, VariableName
) == 0) &&
2193 (Attributes
== 0 || Attributes
== mGlobalVariableList
[Index
].Attributes
)) {
2201 NameLength
= StrLen (VariableName
) - 4;
2202 for (Index
= 0; Index
< sizeof (mGlobalVariableList2
)/sizeof (mGlobalVariableList2
[0]); Index
++) {
2203 if ((StrLen (VariableName
) == StrLen (mGlobalVariableList2
[Index
].Name
)) &&
2204 (StrnCmp (mGlobalVariableList2
[Index
].Name
, VariableName
, NameLength
) == 0) &&
2205 IsHexaDecimalDigitCharacter (VariableName
[NameLength
]) &&
2206 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 1]) &&
2207 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 2]) &&
2208 IsHexaDecimalDigitCharacter (VariableName
[NameLength
+ 3]) &&
2209 (Attributes
== 0 || Attributes
== mGlobalVariableList2
[Index
].Attributes
)) {
2214 DEBUG ((EFI_D_INFO
, "[Variable]: set global variable with invalid variable name or attributes - %g:%s:%x\n", VendorGuid
, VariableName
, Attributes
));
2215 return EFI_INVALID_PARAMETER
;
2222 Mark a variable that will become read-only after leaving the DXE phase of execution.
2224 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
2225 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
2226 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
2228 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
2229 as pending to be read-only.
2230 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
2231 Or VariableName is an empty string.
2232 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
2233 already been signaled.
2234 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
2238 VariableLockRequestToLock (
2239 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL
*This
,
2240 IN CHAR16
*VariableName
,
2241 IN EFI_GUID
*VendorGuid
2244 VARIABLE_ENTRY
*Entry
;
2246 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2247 return EFI_INVALID_PARAMETER
;
2251 return EFI_ACCESS_DENIED
;
2254 Entry
= AllocateRuntimePool (sizeof (*Entry
) + StrSize (VariableName
));
2255 if (Entry
== NULL
) {
2256 return EFI_OUT_OF_RESOURCES
;
2259 DEBUG ((EFI_D_INFO
, "[Variable] Lock: %g:%s\n", VendorGuid
, VariableName
));
2261 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2263 Entry
->Name
= (CHAR16
*) (Entry
+ 1);
2264 StrCpy (Entry
->Name
, VariableName
);
2265 CopyGuid (&Entry
->Guid
, VendorGuid
);
2266 InsertTailList (&mLockedVariableList
, &Entry
->Link
);
2268 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2275 This code finds variable in storage blocks (Volatile or Non-Volatile).
2277 @param VariableName Name of Variable to be found.
2278 @param VendorGuid Variable vendor GUID.
2279 @param Attributes Attribute value of the variable found.
2280 @param DataSize Size of Data found. If size is less than the
2281 data, this value contains the required size.
2282 @param Data Data pointer.
2284 @return EFI_INVALID_PARAMETER Invalid parameter.
2285 @return EFI_SUCCESS Find the specified variable.
2286 @return EFI_NOT_FOUND Not found.
2287 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2292 VariableServiceGetVariable (
2293 IN CHAR16
*VariableName
,
2294 IN EFI_GUID
*VendorGuid
,
2295 OUT UINT32
*Attributes OPTIONAL
,
2296 IN OUT UINTN
*DataSize
,
2301 VARIABLE_POINTER_TRACK Variable
;
2304 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2305 return EFI_INVALID_PARAMETER
;
2308 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2310 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2311 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2318 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2319 ASSERT (VarDataSize
!= 0);
2321 if (*DataSize
>= VarDataSize
) {
2323 Status
= EFI_INVALID_PARAMETER
;
2327 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2328 if (Attributes
!= NULL
) {
2329 *Attributes
= Variable
.CurrPtr
->Attributes
;
2332 *DataSize
= VarDataSize
;
2333 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2335 Status
= EFI_SUCCESS
;
2338 *DataSize
= VarDataSize
;
2339 Status
= EFI_BUFFER_TOO_SMALL
;
2344 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2352 This code Finds the Next available variable.
2354 @param VariableNameSize Size of the variable name.
2355 @param VariableName Pointer to variable name.
2356 @param VendorGuid Variable Vendor Guid.
2358 @return EFI_INVALID_PARAMETER Invalid parameter.
2359 @return EFI_SUCCESS Find the specified variable.
2360 @return EFI_NOT_FOUND Not found.
2361 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2366 VariableServiceGetNextVariableName (
2367 IN OUT UINTN
*VariableNameSize
,
2368 IN OUT CHAR16
*VariableName
,
2369 IN OUT EFI_GUID
*VendorGuid
2372 VARIABLE_STORE_TYPE Type
;
2373 VARIABLE_POINTER_TRACK Variable
;
2374 VARIABLE_POINTER_TRACK VariableInHob
;
2375 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2378 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2380 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2381 return EFI_INVALID_PARAMETER
;
2384 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2386 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2387 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2391 if (VariableName
[0] != 0) {
2393 // If variable name is not NULL, get next variable.
2395 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2399 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2400 // The index and attributes mapping must be kept in this order as FindVariable
2401 // makes use of this mapping to implement search algorithm.
2403 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2404 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2405 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2409 // Switch from Volatile to HOB, to Non-Volatile.
2411 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
2412 (Variable
.CurrPtr
== NULL
) ||
2413 !IsValidVariableHeader (Variable
.CurrPtr
)
2416 // Find current storage index
2418 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2419 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2423 ASSERT (Type
< VariableStoreTypeMax
);
2425 // Switch to next storage
2427 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2428 if (VariableStoreHeader
[Type
] != NULL
) {
2433 // Capture the case that
2434 // 1. current storage is the last one, or
2435 // 2. no further storage
2437 if (Type
== VariableStoreTypeMax
) {
2438 Status
= EFI_NOT_FOUND
;
2441 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2442 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2443 Variable
.CurrPtr
= Variable
.StartPtr
;
2447 // Variable is found
2449 if (Variable
.CurrPtr
->State
== VAR_ADDED
|| Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2450 if (!AtRuntime () || ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
2451 if (Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2453 // If it is a IN_DELETED_TRANSITION variable,
2454 // and there is also a same ADDED one at the same time,
2457 VariablePtrTrack
.StartPtr
= Variable
.StartPtr
;
2458 VariablePtrTrack
.EndPtr
= Variable
.EndPtr
;
2459 Status
= FindVariableEx (
2460 GetVariableNamePtr (Variable
.CurrPtr
),
2461 &Variable
.CurrPtr
->VendorGuid
,
2465 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
== VAR_ADDED
) {
2466 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2472 // Don't return NV variable when HOB overrides it
2474 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2475 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2477 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2478 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2479 Status
= FindVariableEx (
2480 GetVariableNamePtr (Variable
.CurrPtr
),
2481 &Variable
.CurrPtr
->VendorGuid
,
2485 if (!EFI_ERROR (Status
)) {
2486 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2491 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2492 ASSERT (VarNameSize
!= 0);
2494 if (VarNameSize
<= *VariableNameSize
) {
2495 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2496 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2497 Status
= EFI_SUCCESS
;
2499 Status
= EFI_BUFFER_TOO_SMALL
;
2502 *VariableNameSize
= VarNameSize
;
2507 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2511 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2517 This code sets variable in storage blocks (Volatile or Non-Volatile).
2519 @param VariableName Name of Variable to be found.
2520 @param VendorGuid Variable vendor GUID.
2521 @param Attributes Attribute value of the variable found
2522 @param DataSize Size of Data found. If size is less than the
2523 data, this value contains the required size.
2524 @param Data Data pointer.
2526 @return EFI_INVALID_PARAMETER Invalid parameter.
2527 @return EFI_SUCCESS Set successfully.
2528 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2529 @return EFI_NOT_FOUND Not found.
2530 @return EFI_WRITE_PROTECTED Variable is read-only.
2535 VariableServiceSetVariable (
2536 IN CHAR16
*VariableName
,
2537 IN EFI_GUID
*VendorGuid
,
2538 IN UINT32 Attributes
,
2543 VARIABLE_POINTER_TRACK Variable
;
2545 VARIABLE_HEADER
*NextVariable
;
2546 EFI_PHYSICAL_ADDRESS Point
;
2548 VARIABLE_ENTRY
*Entry
;
2551 // Check input parameters.
2553 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2554 return EFI_INVALID_PARAMETER
;
2557 if (DataSize
!= 0 && Data
== NULL
) {
2558 return EFI_INVALID_PARAMETER
;
2562 // Not support authenticated or append variable write yet.
2564 if ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_APPEND_WRITE
)) != 0) {
2565 return EFI_INVALID_PARAMETER
;
2569 // Make sure if runtime bit is set, boot service bit is set also.
2571 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2572 return EFI_INVALID_PARAMETER
;
2575 if ((UINTN
)(~0) - DataSize
< StrSize(VariableName
)){
2577 // Prevent whole variable size overflow
2579 return EFI_INVALID_PARAMETER
;
2583 // The size of the VariableName, including the Unicode Null in bytes plus
2584 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2585 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2587 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2588 if ( StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
)) {
2589 return EFI_INVALID_PARAMETER
;
2591 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2592 return EFI_INVALID_PARAMETER
;
2596 // The size of the VariableName, including the Unicode Null in bytes plus
2597 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2599 if (StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
)) {
2600 return EFI_INVALID_PARAMETER
;
2604 Status
= CheckEfiGlobalVariable (VariableName
, VendorGuid
, Attributes
);
2605 if (EFI_ERROR (Status
)) {
2609 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2612 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2614 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2615 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2617 // Parse non-volatile variable data and get last variable offset.
2619 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2620 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2621 && IsValidVariableHeader (NextVariable
)) {
2622 NextVariable
= GetNextVariablePtr (NextVariable
);
2624 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2627 if (mEndOfDxe
&& mEnableLocking
) {
2629 // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.
2631 for ( Link
= GetFirstNode (&mLockedVariableList
)
2632 ; !IsNull (&mLockedVariableList
, Link
)
2633 ; Link
= GetNextNode (&mLockedVariableList
, Link
)
2635 Entry
= BASE_CR (Link
, VARIABLE_ENTRY
, Link
);
2636 if (CompareGuid (&Entry
->Guid
, VendorGuid
) && (StrCmp (Entry
->Name
, VariableName
) == 0)) {
2637 Status
= EFI_WRITE_PROTECTED
;
2638 DEBUG ((EFI_D_INFO
, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid
, VariableName
));
2645 // Check whether the input variable is already existed.
2647 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2648 if (!EFI_ERROR (Status
)) {
2649 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2650 Status
= EFI_WRITE_PROTECTED
;
2653 if (Attributes
!= 0 && Attributes
!= Variable
.CurrPtr
->Attributes
) {
2655 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not
2656 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:
2657 // 1. No access attributes specified
2658 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE
2660 Status
= EFI_INVALID_PARAMETER
;
2665 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate
)) {
2667 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2669 Status
= AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2670 if (EFI_ERROR (Status
)) {
2672 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.
2678 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
2681 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2682 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2689 This code returns information about the EFI variables.
2691 @param Attributes Attributes bitmask to specify the type of variables
2692 on which to return information.
2693 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2694 for the EFI variables associated with the attributes specified.
2695 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2696 for EFI variables associated with the attributes specified.
2697 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2698 associated with the attributes specified.
2700 @return EFI_SUCCESS Query successfully.
2705 VariableServiceQueryVariableInfoInternal (
2706 IN UINT32 Attributes
,
2707 OUT UINT64
*MaximumVariableStorageSize
,
2708 OUT UINT64
*RemainingVariableStorageSize
,
2709 OUT UINT64
*MaximumVariableSize
2712 VARIABLE_HEADER
*Variable
;
2713 VARIABLE_HEADER
*NextVariable
;
2714 UINT64 VariableSize
;
2715 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2716 UINT64 CommonVariableTotalSize
;
2717 UINT64 HwErrVariableTotalSize
;
2719 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2721 CommonVariableTotalSize
= 0;
2722 HwErrVariableTotalSize
= 0;
2724 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2726 // Query is Volatile related.
2728 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2731 // Query is Non-Volatile related.
2733 VariableStoreHeader
= mNvVariableCache
;
2737 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2738 // with the storage size (excluding the storage header size).
2740 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2743 // Harware error record variable needs larger size.
2745 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2746 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2747 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2749 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2750 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2751 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2755 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2757 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2761 // Point to the starting address of the variables.
2763 Variable
= GetStartPointer (VariableStoreHeader
);
2766 // Now walk through the related variable store.
2768 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2769 NextVariable
= GetNextVariablePtr (Variable
);
2770 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2774 // We don't take the state of the variables in mind
2775 // when calculating RemainingVariableStorageSize,
2776 // since the space occupied by variables not marked with
2777 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2779 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2780 HwErrVariableTotalSize
+= VariableSize
;
2782 CommonVariableTotalSize
+= VariableSize
;
2786 // Only care about Variables with State VAR_ADDED, because
2787 // the space not marked as VAR_ADDED is reclaimable now.
2789 if (Variable
->State
== VAR_ADDED
) {
2790 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2791 HwErrVariableTotalSize
+= VariableSize
;
2793 CommonVariableTotalSize
+= VariableSize
;
2795 } else if (Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2797 // If it is a IN_DELETED_TRANSITION variable,
2798 // and there is not also a same ADDED one at the same time,
2799 // this IN_DELETED_TRANSITION variable is valid.
2801 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
2802 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
2803 Status
= FindVariableEx (
2804 GetVariableNamePtr (Variable
),
2805 &Variable
->VendorGuid
,
2809 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
!= VAR_ADDED
) {
2810 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2811 HwErrVariableTotalSize
+= VariableSize
;
2813 CommonVariableTotalSize
+= VariableSize
;
2820 // Go to the next one.
2822 Variable
= NextVariable
;
2825 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2826 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2828 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2831 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2832 *MaximumVariableSize
= 0;
2833 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2834 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2842 This code returns information about the EFI variables.
2844 @param Attributes Attributes bitmask to specify the type of variables
2845 on which to return information.
2846 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2847 for the EFI variables associated with the attributes specified.
2848 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2849 for EFI variables associated with the attributes specified.
2850 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2851 associated with the attributes specified.
2853 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2854 @return EFI_SUCCESS Query successfully.
2855 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2860 VariableServiceQueryVariableInfo (
2861 IN UINT32 Attributes
,
2862 OUT UINT64
*MaximumVariableStorageSize
,
2863 OUT UINT64
*RemainingVariableStorageSize
,
2864 OUT UINT64
*MaximumVariableSize
2869 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2870 return EFI_INVALID_PARAMETER
;
2873 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2875 // Make sure the Attributes combination is supported by the platform.
2877 return EFI_UNSUPPORTED
;
2878 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2880 // Make sure if runtime bit is set, boot service bit is set also.
2882 return EFI_INVALID_PARAMETER
;
2883 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2885 // Make sure RT Attribute is set if we are in Runtime phase.
2887 return EFI_INVALID_PARAMETER
;
2888 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2890 // Make sure Hw Attribute is set with NV.
2892 return EFI_INVALID_PARAMETER
;
2893 } else if ((Attributes
& (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
| EFI_VARIABLE_APPEND_WRITE
)) != 0) {
2895 // Not support authenticated or append variable write yet.
2897 return EFI_UNSUPPORTED
;
2900 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2902 Status
= VariableServiceQueryVariableInfoInternal (
2904 MaximumVariableStorageSize
,
2905 RemainingVariableStorageSize
,
2909 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2914 This function reclaims variable storage if free size is below the threshold.
2923 UINTN CommonVariableSpace
;
2924 UINTN RemainingCommonVariableSpace
;
2925 UINTN RemainingHwErrVariableSpace
;
2927 Status
= EFI_SUCCESS
;
2929 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2931 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2933 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2935 // Check if the free area is blow a threshold.
2937 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2938 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2939 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2941 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2942 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2948 ASSERT_EFI_ERROR (Status
);
2953 Init non-volatile variable store.
2955 @retval EFI_SUCCESS Function successfully executed.
2956 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2957 @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.
2961 InitNonVolatileVariableStore (
2965 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
2966 VARIABLE_HEADER
*NextVariable
;
2967 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2968 UINT64 VariableStoreLength
;
2970 EFI_HOB_GUID_TYPE
*GuidHob
;
2971 EFI_PHYSICAL_ADDRESS NvStorageBase
;
2972 UINT8
*NvStorageData
;
2973 UINT32 NvStorageSize
;
2974 FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*FtwLastWriteData
;
2975 UINT32 BackUpOffset
;
2978 mVariableModuleGlobal
->FvbInstance
= NULL
;
2981 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2982 // is stored with common variable in the same NV region. So the platform integrator should
2983 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2984 // PcdFlashNvStorageVariableSize.
2986 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2989 // Allocate runtime memory used for a memory copy of the FLASH region.
2990 // Keep the memory and the FLASH in sync as updates occur.
2992 NvStorageSize
= PcdGet32 (PcdFlashNvStorageVariableSize
);
2993 NvStorageData
= AllocateRuntimeZeroPool (NvStorageSize
);
2994 if (NvStorageData
== NULL
) {
2995 return EFI_OUT_OF_RESOURCES
;
2998 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2999 if (NvStorageBase
== 0) {
3000 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3003 // Copy NV storage data to the memory buffer.
3005 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) NvStorageBase
, NvStorageSize
);
3008 // Check the FTW last write data hob.
3010 GuidHob
= GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid
);
3011 if (GuidHob
!= NULL
) {
3012 FtwLastWriteData
= (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA
*) GET_GUID_HOB_DATA (GuidHob
);
3013 if (FtwLastWriteData
->TargetAddress
== NvStorageBase
) {
3014 DEBUG ((EFI_D_INFO
, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN
) FtwLastWriteData
->SpareAddress
));
3016 // Copy the backed up NV storage data to the memory buffer from spare block.
3018 CopyMem (NvStorageData
, (UINT8
*) (UINTN
) (FtwLastWriteData
->SpareAddress
), NvStorageSize
);
3019 } else if ((FtwLastWriteData
->TargetAddress
> NvStorageBase
) &&
3020 (FtwLastWriteData
->TargetAddress
< (NvStorageBase
+ NvStorageSize
))) {
3022 // Flash NV storage from the offset is backed up in spare block.
3024 BackUpOffset
= (UINT32
) (FtwLastWriteData
->TargetAddress
- NvStorageBase
);
3025 BackUpSize
= NvStorageSize
- BackUpOffset
;
3026 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
));
3028 // Copy the partial backed up NV storage data to the memory buffer from spare block.
3030 CopyMem (NvStorageData
+ BackUpOffset
, (UINT8
*) (UINTN
) FtwLastWriteData
->SpareAddress
, BackUpSize
);
3034 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) NvStorageData
;
3037 // Check if the Firmware Volume is not corrupted
3039 if ((FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) || (!CompareGuid (&gEfiSystemNvDataFvGuid
, &FvHeader
->FileSystemGuid
))) {
3040 FreePool (NvStorageData
);
3041 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
3042 return EFI_VOLUME_CORRUPTED
;
3045 VariableStoreBase
= (EFI_PHYSICAL_ADDRESS
) ((UINTN
) FvHeader
+ FvHeader
->HeaderLength
);
3046 VariableStoreLength
= (UINT64
) (NvStorageSize
- FvHeader
->HeaderLength
);
3048 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3049 mNvVariableCache
= (VARIABLE_STORE_HEADER
*) (UINTN
) VariableStoreBase
;
3050 if (GetVariableStoreStatus (mNvVariableCache
) != EfiValid
) {
3051 FreePool (NvStorageData
);
3052 DEBUG((EFI_D_ERROR
, "Variable Store header is corrupted\n"));
3053 return EFI_VOLUME_CORRUPTED
;
3055 ASSERT(mNvVariableCache
->Size
== VariableStoreLength
);
3058 // The max variable or hardware error variable size should be < variable store size.
3060 ASSERT(MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) < VariableStoreLength
);
3063 // Parse non-volatile variable data and get last variable offset.
3065 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
3066 while (IsValidVariableHeader (NextVariable
)) {
3067 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
3068 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
3069 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3071 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
3074 NextVariable
= GetNextVariablePtr (NextVariable
);
3076 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
3082 Flush the HOB variable to flash.
3084 @param[in] VariableName Name of variable has been updated or deleted.
3085 @param[in] VendorGuid Guid of variable has been updated or deleted.
3089 FlushHobVariableToFlash (
3090 IN CHAR16
*VariableName
,
3091 IN EFI_GUID
*VendorGuid
3095 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3096 VARIABLE_HEADER
*Variable
;
3103 // Flush the HOB variable to flash.
3105 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3106 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
3108 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
3110 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
3111 for ( Variable
= GetStartPointer (VariableStoreHeader
)
3112 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
3113 ; Variable
= GetNextVariablePtr (Variable
)
3115 if (Variable
->State
!= VAR_ADDED
) {
3117 // The HOB variable has been set to DELETED state in local.
3121 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
3122 if (VendorGuid
== NULL
|| VariableName
== NULL
||
3123 !CompareGuid (VendorGuid
, &Variable
->VendorGuid
) ||
3124 StrCmp (VariableName
, GetVariableNamePtr (Variable
)) != 0) {
3125 VariableData
= GetVariableDataPtr (Variable
);
3126 Status
= VariableServiceSetVariable (
3127 GetVariableNamePtr (Variable
),
3128 &Variable
->VendorGuid
,
3129 Variable
->Attributes
,
3133 DEBUG ((EFI_D_INFO
, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
), Status
));
3136 // The updated or deleted variable is matched with the HOB variable.
3137 // Don't break here because we will try to set other HOB variables
3138 // since this variable could be set successfully.
3140 Status
= EFI_SUCCESS
;
3142 if (!EFI_ERROR (Status
)) {
3144 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
3145 // set the HOB variable to DELETED state in local.
3147 DEBUG ((EFI_D_INFO
, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable
->VendorGuid
, GetVariableNamePtr (Variable
)));
3148 Variable
->State
&= VAR_DELETED
;
3155 // We still have HOB variable(s) not flushed in flash.
3157 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
3160 // All HOB variables have been flushed in flash.
3162 DEBUG ((EFI_D_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
3163 if (!AtRuntime ()) {
3164 FreePool ((VOID
*) VariableStoreHeader
);
3172 Initializes variable write service after FTW was ready.
3174 @retval EFI_SUCCESS Function successfully executed.
3175 @retval Others Fail to initialize the variable service.
3179 VariableWriteServiceInitialize (
3184 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3187 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
3188 EFI_PHYSICAL_ADDRESS NvStorageBase
;
3190 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
3191 if (NvStorageBase
== 0) {
3192 NvStorageBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
3194 VariableStoreBase
= NvStorageBase
+ (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(NvStorageBase
))->HeaderLength
);
3197 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
3199 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
3200 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
3203 // Check if the free area is really free.
3205 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
3206 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
3209 // There must be something wrong in variable store, do reclaim operation.
3212 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3213 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3219 if (EFI_ERROR (Status
)) {
3226 FlushHobVariableToFlash (NULL
, NULL
);
3233 Initializes variable store area for non-volatile and volatile variable.
3235 @retval EFI_SUCCESS Function successfully executed.
3236 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3240 VariableCommonInitialize (
3245 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
3246 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3247 UINT64 VariableStoreLength
;
3249 EFI_HOB_GUID_TYPE
*GuidHob
;
3252 // Allocate runtime memory for variable driver global structure.
3254 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
3255 if (mVariableModuleGlobal
== NULL
) {
3256 return EFI_OUT_OF_RESOURCES
;
3259 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
3262 // Get HOB variable store.
3264 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
3265 if (GuidHob
!= NULL
) {
3266 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
3267 VariableStoreLength
= (UINT64
) (GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
));
3268 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
3269 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
3270 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
3271 FreePool (mVariableModuleGlobal
);
3272 return EFI_OUT_OF_RESOURCES
;
3275 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
3280 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
3282 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
3283 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
3284 if (VolatileVariableStore
== NULL
) {
3285 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3286 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3288 FreePool (mVariableModuleGlobal
);
3289 return EFI_OUT_OF_RESOURCES
;
3292 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
3295 // Initialize Variable Specific Data.
3297 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
3298 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
3300 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiVariableGuid
);
3301 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
3302 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
3303 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
3304 VolatileVariableStore
->Reserved
= 0;
3305 VolatileVariableStore
->Reserved1
= 0;
3308 // Init non-volatile variable store.
3310 Status
= InitNonVolatileVariableStore ();
3311 if (EFI_ERROR (Status
)) {
3312 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3313 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3315 FreePool (mVariableModuleGlobal
);
3316 FreePool (VolatileVariableStore
);
3324 Get the proper fvb handle and/or fvb protocol by the given Flash address.
3326 @param[in] Address The Flash address.
3327 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
3328 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
3332 GetFvbInfoByAddress (
3333 IN EFI_PHYSICAL_ADDRESS Address
,
3334 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
3335 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
3339 EFI_HANDLE
*HandleBuffer
;
3342 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
3343 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
3344 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
3345 EFI_FVB_ATTRIBUTES_2 Attributes
;
3348 // Get all FVB handles.
3350 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
3351 if (EFI_ERROR (Status
)) {
3352 return EFI_NOT_FOUND
;
3356 // Get the FVB to access variable store.
3359 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
3360 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
3361 if (EFI_ERROR (Status
)) {
3362 Status
= EFI_NOT_FOUND
;
3367 // Ensure this FVB protocol supported Write operation.
3369 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
3370 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
3375 // Compare the address and select the right one.
3377 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
3378 if (EFI_ERROR (Status
)) {
3382 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
3383 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
3384 if (FvbHandle
!= NULL
) {
3385 *FvbHandle
= HandleBuffer
[Index
];
3387 if (FvbProtocol
!= NULL
) {
3390 Status
= EFI_SUCCESS
;
3394 FreePool (HandleBuffer
);
3397 Status
= EFI_NOT_FOUND
;