2 The common variable operation routines shared by DXE_RUNTIME variable
3 module and DXE_SMM variable module.
5 Caution: This module requires additional review when modified.
6 This driver will have external input - variable data. They may be input in SMM mode.
7 This external input must be validated carefully to avoid security issue like
8 buffer overflow, integer overflow.
10 VariableServiceGetNextVariableName () and VariableServiceQueryVariableInfo() are external API.
11 They need check input parameter.
13 VariableServiceGetVariable() and VariableServiceSetVariable() are external API
14 to receive datasize and data buffer. The size should be checked carefully.
16 VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,
17 integer overflow. It should also check attribute to avoid authentication bypass.
19 Copyright (c) 2006 - 2020, Intel Corporation. All rights reserved.<BR>
20 (C) Copyright 2015-2018 Hewlett Packard Enterprise Development LP<BR>
21 Copyright (c) Microsoft Corporation.<BR>
22 Copyright (c) 2022, ARM Limited. All rights reserved.<BR>
24 SPDX-License-Identifier: BSD-2-Clause-Patent
29 #include "VariableNonVolatile.h"
30 #include "VariableParsing.h"
31 #include "VariableRuntimeCache.h"
33 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
36 /// Define a memory cache that improves the search performance for a variable.
37 /// For EmuNvMode == TRUE, it will be equal to NonVolatileVariableBase.
39 VARIABLE_STORE_HEADER
*mNvVariableCache
= NULL
;
42 /// Memory cache of Fv Header.
44 EFI_FIRMWARE_VOLUME_HEADER
*mNvFvHeaderCache
= NULL
;
47 /// The memory entry used for variable statistics data.
49 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
52 /// The flag to indicate whether the platform has left the DXE phase of execution.
54 BOOLEAN mEndOfDxe
= FALSE
;
57 /// It indicates the var check request source.
58 /// In the implementation, DXE is regarded as untrusted, and SMM is trusted.
60 VAR_CHECK_REQUEST_SOURCE mRequestSource
= VarCheckFromUntrusted
;
63 // It will record the current boot error flag before EndOfDxe.
65 VAR_ERROR_FLAG mCurrentBootVarErrFlag
= VAR_ERROR_FLAG_NO_ERROR
;
67 VARIABLE_ENTRY_PROPERTY mVariableEntryProperty
[] = {
69 &gEdkiiVarErrorFlagGuid
,
72 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
73 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
,
74 VARIABLE_ATTRIBUTE_NV_BS_RT
,
75 sizeof (VAR_ERROR_FLAG
),
76 sizeof (VAR_ERROR_FLAG
)
81 AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn
= {
82 AUTH_VAR_LIB_CONTEXT_IN_STRUCT_VERSION
,
84 // StructSize, TO BE FILLED
88 // MaxAuthVariableSize, TO BE FILLED
91 VariableExLibFindVariable
,
92 VariableExLibFindNextVariable
,
93 VariableExLibUpdateVariable
,
94 VariableExLibGetScratchBuffer
,
95 VariableExLibCheckRemainingSpaceForConsistency
,
96 VariableExLibAtRuntime
,
99 AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut
;
103 This function writes data to the FWH at the correct LBA even if the LBAs
106 @param Global Pointer to VARAIBLE_GLOBAL structure.
107 @param Volatile Point out the Variable is Volatile or Non-Volatile.
108 @param SetByIndex TRUE if target pointer is given as index.
109 FALSE if target pointer is absolute.
110 @param Fvb Pointer to the writable FVB protocol.
111 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
113 @param DataSize Size of data to be written.
114 @param Buffer Pointer to the buffer from which data is written.
116 @retval EFI_INVALID_PARAMETER Parameters not valid.
117 @retval EFI_UNSUPPORTED Fvb is a NULL for Non-Volatile variable update.
118 @retval EFI_OUT_OF_RESOURCES The remaining size is not enough.
119 @retval EFI_SUCCESS Variable store successfully updated.
123 UpdateVariableStore (
124 IN VARIABLE_GLOBAL
*Global
,
126 IN BOOLEAN SetByIndex
,
127 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
128 IN UINTN DataPtrIndex
,
133 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
141 VARIABLE_STORE_HEADER
*VolatileBase
;
142 EFI_PHYSICAL_ADDRESS FvVolHdr
;
143 EFI_PHYSICAL_ADDRESS DataPtr
;
147 DataPtr
= DataPtrIndex
;
150 // Check if the Data is Volatile.
152 if (!Volatile
&& !mVariableModuleGlobal
->VariableGlobal
.EmuNvMode
) {
154 return EFI_UNSUPPORTED
;
157 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvVolHdr
);
158 ASSERT_EFI_ERROR (Status
);
161 // Data Pointer should point to the actual Address where data is to be
165 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
168 if ((DataPtr
+ DataSize
) > (FvVolHdr
+ mNvFvHeaderCache
->FvLength
)) {
169 return EFI_OUT_OF_RESOURCES
;
173 // Data Pointer should point to the actual Address where data is to be
177 VolatileBase
= (VARIABLE_STORE_HEADER
*)((UINTN
)mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
179 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
182 if ((DataPtr
+ DataSize
) > ((UINTN
)VolatileBase
+ VolatileBase
->Size
)) {
183 return EFI_OUT_OF_RESOURCES
;
187 // Emulated non-volatile variable mode.
190 DataPtr
+= (UINTN
)mNvVariableCache
;
193 if ((DataPtr
+ DataSize
) > ((UINTN
)mNvVariableCache
+ mNvVariableCache
->Size
)) {
194 return EFI_OUT_OF_RESOURCES
;
199 // If Volatile/Emulated Non-volatile Variable just do a simple mem copy.
201 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
206 // If we are here we are dealing with Non-Volatile Variables.
208 LinearOffset
= (UINTN
)FvVolHdr
;
209 CurrWritePtr
= (UINTN
)DataPtr
;
210 CurrWriteSize
= DataSize
;
214 if (CurrWritePtr
< LinearOffset
) {
215 return EFI_INVALID_PARAMETER
;
218 for (PtrBlockMapEntry
= mNvFvHeaderCache
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
219 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
221 // Check to see if the Variable Writes are spanning through multiple
224 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
225 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
226 Status
= Fvb
->Write (
229 (UINTN
)(CurrWritePtr
- LinearOffset
),
235 Size
= (UINT32
)(LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
236 Status
= Fvb
->Write (
239 (UINTN
)(CurrWritePtr
- LinearOffset
),
243 if (EFI_ERROR (Status
)) {
247 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
248 CurrBuffer
= CurrBuffer
+ Size
;
249 CurrWriteSize
= CurrWriteSize
- Size
;
253 LinearOffset
+= PtrBlockMapEntry
->Length
;
262 Record variable error flag.
264 @param[in] Flag Variable error flag to record.
265 @param[in] VariableName Name of variable.
266 @param[in] VendorGuid Guid of variable.
267 @param[in] Attributes Attributes of the variable.
268 @param[in] VariableSize Size of the variable.
273 IN VAR_ERROR_FLAG Flag
,
274 IN CHAR16
*VariableName
,
275 IN EFI_GUID
*VendorGuid
,
276 IN UINT32 Attributes
,
277 IN UINTN VariableSize
281 VARIABLE_POINTER_TRACK Variable
;
282 VAR_ERROR_FLAG
*VarErrFlag
;
283 VAR_ERROR_FLAG TempFlag
;
286 DEBUG ((DEBUG_ERROR
, "RecordVarErrorFlag (0x%02x) %s:%g - 0x%08x - 0x%x\n", Flag
, VariableName
, VendorGuid
, Attributes
, VariableSize
));
287 if (Flag
== VAR_ERROR_FLAG_SYSTEM_ERROR
) {
289 DEBUG ((DEBUG_ERROR
, "CommonRuntimeVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal
->CommonRuntimeVariableSpace
, mVariableModuleGlobal
->CommonVariableTotalSize
));
291 DEBUG ((DEBUG_ERROR
, "CommonVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal
->CommonVariableSpace
, mVariableModuleGlobal
->CommonVariableTotalSize
));
294 DEBUG ((DEBUG_ERROR
, "CommonMaxUserVariableSpace = 0x%x - CommonUserVariableTotalSize = 0x%x\n", mVariableModuleGlobal
->CommonMaxUserVariableSpace
, mVariableModuleGlobal
->CommonUserVariableTotalSize
));
301 // Before EndOfDxe, just record the current boot variable error flag to local variable,
302 // and leave the variable error flag in NV flash as the last boot variable error flag.
303 // After EndOfDxe in InitializeVarErrorFlag (), the variable error flag in NV flash
304 // will be initialized to this local current boot variable error flag.
306 mCurrentBootVarErrFlag
&= Flag
;
311 // Record error flag (it should have be initialized).
313 Status
= FindVariable (
315 &gEdkiiVarErrorFlagGuid
,
317 &mVariableModuleGlobal
->VariableGlobal
,
320 if (!EFI_ERROR (Status
)) {
321 VarErrFlag
= (VAR_ERROR_FLAG
*)GetVariableDataPtr (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
322 TempFlag
= *VarErrFlag
;
324 if (TempFlag
== *VarErrFlag
) {
328 Status
= UpdateVariableStore (
329 &mVariableModuleGlobal
->VariableGlobal
,
332 mVariableModuleGlobal
->FvbInstance
,
333 (UINTN
)VarErrFlag
- (UINTN
)mNvVariableCache
+ (UINTN
)mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
337 if (!EFI_ERROR (Status
)) {
339 // Update the data in NV cache.
341 *VarErrFlag
= TempFlag
;
342 Status
= SynchronizeRuntimeVariableCache (
343 &mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeNvCache
,
345 mNvVariableCache
->Size
347 ASSERT_EFI_ERROR (Status
);
353 Initialize variable error flag.
355 Before EndOfDxe, the variable indicates the last boot variable error flag,
356 then it means the last boot variable error flag must be got before EndOfDxe.
357 After EndOfDxe, the variable indicates the current boot variable error flag,
358 then it means the current boot variable error flag must be got after EndOfDxe.
362 InitializeVarErrorFlag (
367 VARIABLE_POINTER_TRACK Variable
;
369 VAR_ERROR_FLAG VarErrFlag
;
375 Flag
= mCurrentBootVarErrFlag
;
376 DEBUG ((DEBUG_INFO
, "Initialize variable error flag (%02x)\n", Flag
));
378 Status
= FindVariable (
380 &gEdkiiVarErrorFlagGuid
,
382 &mVariableModuleGlobal
->VariableGlobal
,
385 if (!EFI_ERROR (Status
)) {
386 VarErrFlag
= *((VAR_ERROR_FLAG
*)GetVariableDataPtr (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
));
387 if (VarErrFlag
== Flag
) {
394 &gEdkiiVarErrorFlagGuid
,
397 VARIABLE_ATTRIBUTE_NV_BS_RT
,
408 @param[in] Variable Pointer to variable header.
410 @retval TRUE User variable.
411 @retval FALSE System variable.
416 IN VARIABLE_HEADER
*Variable
419 VAR_CHECK_VARIABLE_PROPERTY Property
;
422 // Only after End Of Dxe, the variables belong to system variable are fixed.
423 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,
424 // then no need to check if the variable is user variable or not specially.
426 if (mEndOfDxe
&& (mVariableModuleGlobal
->CommonMaxUserVariableSpace
!= mVariableModuleGlobal
->CommonVariableSpace
)) {
427 if (VarCheckLibVariablePropertyGet (
428 GetVariableNamePtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
429 GetVendorGuidPtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
441 Calculate common user variable total size.
445 CalculateCommonUserVariableTotalSize (
449 VARIABLE_HEADER
*Variable
;
450 VARIABLE_HEADER
*NextVariable
;
452 VAR_CHECK_VARIABLE_PROPERTY Property
;
455 // Only after End Of Dxe, the variables belong to system variable are fixed.
456 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,
457 // then no need to calculate the common user variable total size specially.
459 if (mEndOfDxe
&& (mVariableModuleGlobal
->CommonMaxUserVariableSpace
!= mVariableModuleGlobal
->CommonVariableSpace
)) {
460 Variable
= GetStartPointer (mNvVariableCache
);
461 while (IsValidVariableHeader (Variable
, GetEndPointer (mNvVariableCache
))) {
462 NextVariable
= GetNextVariablePtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
463 VariableSize
= (UINTN
)NextVariable
- (UINTN
)Variable
;
464 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
465 if (VarCheckLibVariablePropertyGet (
466 GetVariableNamePtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
467 GetVendorGuidPtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
472 // No property, it is user variable.
474 mVariableModuleGlobal
->CommonUserVariableTotalSize
+= VariableSize
;
478 Variable
= NextVariable
;
484 Initialize variable quota.
488 InitializeVariableQuota (
496 InitializeVarErrorFlag ();
497 CalculateCommonUserVariableTotalSize ();
502 Variable store garbage collection and reclaim operation.
504 @param[in] VariableBase Base address of variable store.
505 @param[out] LastVariableOffset Offset of last variable.
506 @param[in] IsVolatile The variable store is volatile or not;
507 if it is non-volatile, need FTW.
508 @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.
509 @param[in] NewVariable Pointer to new variable.
510 @param[in] NewVariableSize New variable size.
512 @return EFI_SUCCESS Reclaim operation has finished successfully.
513 @return EFI_OUT_OF_RESOURCES No enough memory resources or variable space.
514 @return Others Unexpect error happened during reclaim operation.
519 IN EFI_PHYSICAL_ADDRESS VariableBase
,
520 OUT UINTN
*LastVariableOffset
,
521 IN BOOLEAN IsVolatile
,
522 IN OUT VARIABLE_POINTER_TRACK
*UpdatingPtrTrack
,
523 IN VARIABLE_HEADER
*NewVariable
,
524 IN UINTN NewVariableSize
527 VARIABLE_HEADER
*Variable
;
528 VARIABLE_HEADER
*AddedVariable
;
529 VARIABLE_HEADER
*NextVariable
;
530 VARIABLE_HEADER
*NextAddedVariable
;
531 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
533 UINTN MaximumBufferSize
;
541 EFI_STATUS DoneStatus
;
542 UINTN CommonVariableTotalSize
;
543 UINTN CommonUserVariableTotalSize
;
544 UINTN HwErrVariableTotalSize
;
545 VARIABLE_HEADER
*UpdatingVariable
;
546 VARIABLE_HEADER
*UpdatingInDeletedTransition
;
549 AuthFormat
= mVariableModuleGlobal
->VariableGlobal
.AuthFormat
;
550 UpdatingVariable
= NULL
;
551 UpdatingInDeletedTransition
= NULL
;
552 if (UpdatingPtrTrack
!= NULL
) {
553 UpdatingVariable
= UpdatingPtrTrack
->CurrPtr
;
554 UpdatingInDeletedTransition
= UpdatingPtrTrack
->InDeletedTransitionPtr
;
557 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINTN
)VariableBase
);
559 CommonVariableTotalSize
= 0;
560 CommonUserVariableTotalSize
= 0;
561 HwErrVariableTotalSize
= 0;
563 if (IsVolatile
|| mVariableModuleGlobal
->VariableGlobal
.EmuNvMode
) {
565 // Start Pointers for the variable.
567 Variable
= GetStartPointer (VariableStoreHeader
);
568 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
570 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
571 NextVariable
= GetNextVariablePtr (Variable
, AuthFormat
);
572 if (((Variable
->State
== VAR_ADDED
) || (Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
))) &&
573 (Variable
!= UpdatingVariable
) &&
574 (Variable
!= UpdatingInDeletedTransition
)
577 VariableSize
= (UINTN
)NextVariable
- (UINTN
)Variable
;
578 MaximumBufferSize
+= VariableSize
;
581 Variable
= NextVariable
;
584 if (NewVariable
!= NULL
) {
586 // Add the new variable size.
588 MaximumBufferSize
+= NewVariableSize
;
592 // Reserve the 1 Bytes with Oxff to identify the
593 // end of the variable buffer.
595 MaximumBufferSize
+= 1;
596 ValidBuffer
= AllocatePool (MaximumBufferSize
);
597 if (ValidBuffer
== NULL
) {
598 return EFI_OUT_OF_RESOURCES
;
602 // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache
603 // as the buffer to reduce SMRAM consumption for SMM variable driver.
605 MaximumBufferSize
= mNvVariableCache
->Size
;
606 ValidBuffer
= (UINT8
*)mNvVariableCache
;
609 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
612 // Copy variable store header.
614 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
615 CurrPtr
= (UINT8
*)GetStartPointer ((VARIABLE_STORE_HEADER
*)ValidBuffer
);
618 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
620 Variable
= GetStartPointer (VariableStoreHeader
);
621 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
622 NextVariable
= GetNextVariablePtr (Variable
, AuthFormat
);
623 if ((Variable
!= UpdatingVariable
) && (Variable
->State
== VAR_ADDED
)) {
624 VariableSize
= (UINTN
)NextVariable
- (UINTN
)Variable
;
625 CopyMem (CurrPtr
, (UINT8
*)Variable
, VariableSize
);
626 CurrPtr
+= VariableSize
;
627 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
628 HwErrVariableTotalSize
+= VariableSize
;
629 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
630 CommonVariableTotalSize
+= VariableSize
;
631 if (IsUserVariable (Variable
)) {
632 CommonUserVariableTotalSize
+= VariableSize
;
637 Variable
= NextVariable
;
641 // Reinstall all in delete transition variables.
643 Variable
= GetStartPointer (VariableStoreHeader
);
644 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
645 NextVariable
= GetNextVariablePtr (Variable
, AuthFormat
);
646 if ((Variable
!= UpdatingVariable
) && (Variable
!= UpdatingInDeletedTransition
) && (Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
))) {
648 // Buffer has cached all ADDED variable.
649 // Per IN_DELETED variable, we have to guarantee that
650 // no ADDED one in previous buffer.
654 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)ValidBuffer
);
655 while (IsValidVariableHeader (AddedVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*)ValidBuffer
))) {
656 NextAddedVariable
= GetNextVariablePtr (AddedVariable
, AuthFormat
);
657 NameSize
= NameSizeOfVariable (AddedVariable
, AuthFormat
);
659 GetVendorGuidPtr (AddedVariable
, AuthFormat
),
660 GetVendorGuidPtr (Variable
, AuthFormat
)
661 ) && (NameSize
== NameSizeOfVariable (Variable
, AuthFormat
)))
663 Point0
= (VOID
*)GetVariableNamePtr (AddedVariable
, AuthFormat
);
664 Point1
= (VOID
*)GetVariableNamePtr (Variable
, AuthFormat
);
665 if (CompareMem (Point0
, Point1
, NameSize
) == 0) {
671 AddedVariable
= NextAddedVariable
;
676 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
678 VariableSize
= (UINTN
)NextVariable
- (UINTN
)Variable
;
679 CopyMem (CurrPtr
, (UINT8
*)Variable
, VariableSize
);
680 ((VARIABLE_HEADER
*)CurrPtr
)->State
= VAR_ADDED
;
681 CurrPtr
+= VariableSize
;
682 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
683 HwErrVariableTotalSize
+= VariableSize
;
684 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
685 CommonVariableTotalSize
+= VariableSize
;
686 if (IsUserVariable (Variable
)) {
687 CommonUserVariableTotalSize
+= VariableSize
;
693 Variable
= NextVariable
;
697 // Install the new variable if it is not NULL.
699 if (NewVariable
!= NULL
) {
700 if (((UINTN
)CurrPtr
- (UINTN
)ValidBuffer
) + NewVariableSize
> VariableStoreHeader
->Size
) {
702 // No enough space to store the new variable.
704 Status
= EFI_OUT_OF_RESOURCES
;
709 if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
710 HwErrVariableTotalSize
+= NewVariableSize
;
711 } else if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
712 CommonVariableTotalSize
+= NewVariableSize
;
713 if (IsUserVariable (NewVariable
)) {
714 CommonUserVariableTotalSize
+= NewVariableSize
;
718 if ((HwErrVariableTotalSize
> PcdGet32 (PcdHwErrStorageSize
)) ||
719 (CommonVariableTotalSize
> mVariableModuleGlobal
->CommonVariableSpace
) ||
720 (CommonUserVariableTotalSize
> mVariableModuleGlobal
->CommonMaxUserVariableSpace
))
723 // No enough space to store the new variable by NV or NV+HR attribute.
725 Status
= EFI_OUT_OF_RESOURCES
;
730 CopyMem (CurrPtr
, (UINT8
*)NewVariable
, NewVariableSize
);
731 ((VARIABLE_HEADER
*)CurrPtr
)->State
= VAR_ADDED
;
732 if (UpdatingVariable
!= NULL
) {
733 UpdatingPtrTrack
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)UpdatingPtrTrack
->StartPtr
+ ((UINTN
)CurrPtr
- (UINTN
)GetStartPointer ((VARIABLE_STORE_HEADER
*)ValidBuffer
)));
734 UpdatingPtrTrack
->InDeletedTransitionPtr
= NULL
;
737 CurrPtr
+= NewVariableSize
;
740 if (IsVolatile
|| mVariableModuleGlobal
->VariableGlobal
.EmuNvMode
) {
742 // If volatile/emulated non-volatile variable store, just copy valid buffer.
744 SetMem ((UINT8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
, 0xff);
745 CopyMem ((UINT8
*)(UINTN
)VariableBase
, ValidBuffer
, (UINTN
)CurrPtr
- (UINTN
)ValidBuffer
);
746 *LastVariableOffset
= (UINTN
)CurrPtr
- (UINTN
)ValidBuffer
;
749 // Emulated non-volatile variable mode.
751 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
752 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
753 mVariableModuleGlobal
->CommonUserVariableTotalSize
= CommonUserVariableTotalSize
;
756 Status
= EFI_SUCCESS
;
759 // If non-volatile variable store, perform FTW here.
761 Status
= FtwVariableSpace (
763 (VARIABLE_STORE_HEADER
*)ValidBuffer
765 if (!EFI_ERROR (Status
)) {
766 *LastVariableOffset
= (UINTN
)CurrPtr
- (UINTN
)ValidBuffer
;
767 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
768 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
769 mVariableModuleGlobal
->CommonUserVariableTotalSize
= CommonUserVariableTotalSize
;
771 mVariableModuleGlobal
->HwErrVariableTotalSize
= 0;
772 mVariableModuleGlobal
->CommonVariableTotalSize
= 0;
773 mVariableModuleGlobal
->CommonUserVariableTotalSize
= 0;
774 Variable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
775 while (IsValidVariableHeader (Variable
, GetEndPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
))) {
776 NextVariable
= GetNextVariablePtr (Variable
, AuthFormat
);
777 VariableSize
= (UINTN
)NextVariable
- (UINTN
)Variable
;
778 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
779 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
780 } else if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
781 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
782 if (IsUserVariable (Variable
)) {
783 mVariableModuleGlobal
->CommonUserVariableTotalSize
+= VariableSize
;
787 Variable
= NextVariable
;
790 *LastVariableOffset
= (UINTN
)Variable
- (UINTN
)VariableBase
;
795 DoneStatus
= EFI_SUCCESS
;
796 if (IsVolatile
|| mVariableModuleGlobal
->VariableGlobal
.EmuNvMode
) {
797 DoneStatus
= SynchronizeRuntimeVariableCache (
798 &mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeVolatileCache
,
800 VariableStoreHeader
->Size
802 ASSERT_EFI_ERROR (DoneStatus
);
803 FreePool (ValidBuffer
);
806 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.
808 CopyMem (mNvVariableCache
, (UINT8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
809 DoneStatus
= SynchronizeRuntimeVariableCache (
810 &mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeNvCache
,
812 VariableStoreHeader
->Size
814 ASSERT_EFI_ERROR (DoneStatus
);
817 if (!EFI_ERROR (Status
) && EFI_ERROR (DoneStatus
)) {
825 Finds variable in storage blocks of volatile and non-volatile storage areas.
827 This code finds variable in storage blocks of volatile and non-volatile storage areas.
828 If VariableName is an empty string, then we just return the first
829 qualified variable without comparing VariableName and VendorGuid.
830 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
831 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
832 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
834 @param[in] VariableName Name of the variable to be found.
835 @param[in] VendorGuid Vendor GUID to be found.
836 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
837 including the range searched and the target position.
838 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
839 base of volatile variable storage area, base of
840 NV variable storage area, and a lock.
841 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
842 check at runtime when searching variable.
844 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
846 @retval EFI_SUCCESS Variable successfully found.
847 @retval EFI_NOT_FOUND Variable not found
852 IN CHAR16
*VariableName
,
853 IN EFI_GUID
*VendorGuid
,
854 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
855 IN VARIABLE_GLOBAL
*Global
,
856 IN BOOLEAN IgnoreRtCheck
860 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
861 VARIABLE_STORE_TYPE Type
;
863 if ((VariableName
[0] != 0) && (VendorGuid
== NULL
)) {
864 return EFI_INVALID_PARAMETER
;
868 // 0: Volatile, 1: HOB, 2: Non-Volatile.
869 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
870 // make use of this mapping to implement search algorithm.
872 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*)(UINTN
)Global
->VolatileVariableBase
;
873 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*)(UINTN
)Global
->HobVariableBase
;
874 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
877 // Find the variable by walk through HOB, volatile and non-volatile variable store.
879 for (Type
= (VARIABLE_STORE_TYPE
)0; Type
< VariableStoreTypeMax
; Type
++) {
880 if (VariableStoreHeader
[Type
] == NULL
) {
884 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
885 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
886 PtrTrack
->Volatile
= (BOOLEAN
)(Type
== VariableStoreTypeVolatile
);
888 Status
= FindVariableEx (
893 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
895 if (!EFI_ERROR (Status
)) {
900 return EFI_NOT_FOUND
;
904 Get index from supported language codes according to language string.
906 This code is used to get corresponding index in supported language codes. It can handle
907 RFC4646 and ISO639 language tags.
908 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
909 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
912 SupportedLang = "engfraengfra"
914 Iso639Language = TRUE
915 The return value is "0".
917 SupportedLang = "en;fr;en-US;fr-FR"
919 Iso639Language = FALSE
920 The return value is "3".
922 @param SupportedLang Platform supported language codes.
923 @param Lang Configured language.
924 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
926 @retval The index of language in the language codes.
930 GetIndexFromSupportedLangCodes (
931 IN CHAR8
*SupportedLang
,
933 IN BOOLEAN Iso639Language
938 UINTN LanguageLength
;
940 if (Iso639Language
) {
941 CompareLength
= ISO_639_2_ENTRY_SIZE
;
942 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
943 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
945 // Successfully find the index of Lang string in SupportedLang string.
947 Index
= Index
/ CompareLength
;
956 // Compare RFC4646 language code
959 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++) {
962 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
964 // Skip ';' characters in SupportedLang
966 for ( ; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++) {
970 // Determine the length of the next language code in SupportedLang
972 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++) {
975 if ((CompareLength
== LanguageLength
) &&
976 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0))
979 // Successfully find the index of Lang string in SupportedLang string.
991 Get language string from supported language codes according to index.
993 This code is used to get corresponding language strings in supported language codes. It can handle
994 RFC4646 and ISO639 language tags.
995 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
996 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
999 SupportedLang = "engfraengfra"
1001 Iso639Language = TRUE
1002 The return value is "fra".
1004 SupportedLang = "en;fr;en-US;fr-FR"
1006 Iso639Language = FALSE
1007 The return value is "fr".
1009 @param SupportedLang Platform supported language codes.
1010 @param Index The index in supported language codes.
1011 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1013 @retval The language string in the language codes.
1017 GetLangFromSupportedLangCodes (
1018 IN CHAR8
*SupportedLang
,
1020 IN BOOLEAN Iso639Language
1024 UINTN CompareLength
;
1028 Supported
= SupportedLang
;
1029 if (Iso639Language
) {
1031 // According to the index of Lang string in SupportedLang string to get the language.
1032 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1033 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1035 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1036 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1037 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1041 // Take semicolon as delimitation, sequentially traverse supported language codes.
1043 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1047 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1049 // Have completed the traverse, but not find corrsponding string.
1050 // This case is not allowed to happen.
1056 if (SubIndex
== Index
) {
1058 // According to the index of Lang string in SupportedLang string to get the language.
1059 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1060 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1062 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1063 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1069 // Skip ';' characters in Supported
1071 for ( ; *Supported
!= '\0' && *Supported
== ';'; Supported
++) {
1078 Returns a pointer to an allocated buffer that contains the best matching language
1079 from a set of supported languages.
1081 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1082 code types may not be mixed in a single call to this function. This function
1083 supports a variable argument list that allows the caller to pass in a prioritized
1084 list of language codes to test against all the language codes in SupportedLanguages.
1086 If SupportedLanguages is NULL, then ASSERT().
1088 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1089 contains a set of language codes in the format
1090 specified by Iso639Language.
1091 @param[in] Iso639Language If not zero, then all language codes are assumed to be
1092 in ISO 639-2 format. If zero, then all language
1093 codes are assumed to be in RFC 4646 language format
1094 @param[in] ... A variable argument list that contains pointers to
1095 Null-terminated ASCII strings that contain one or more
1096 language codes in the format specified by Iso639Language.
1097 The first language code from each of these language
1098 code lists is used to determine if it is an exact or
1099 close match to any of the language codes in
1100 SupportedLanguages. Close matches only apply to RFC 4646
1101 language codes, and the matching algorithm from RFC 4647
1102 is used to determine if a close match is present. If
1103 an exact or close match is found, then the matching
1104 language code from SupportedLanguages is returned. If
1105 no matches are found, then the next variable argument
1106 parameter is evaluated. The variable argument list
1107 is terminated by a NULL.
1109 @retval NULL The best matching language could not be found in SupportedLanguages.
1110 @retval NULL There are not enough resources available to return the best matching
1112 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1113 language in SupportedLanguages.
1118 VariableGetBestLanguage (
1119 IN CONST CHAR8
*SupportedLanguages
,
1120 IN UINTN Iso639Language
,
1126 UINTN CompareLength
;
1127 UINTN LanguageLength
;
1128 CONST CHAR8
*Supported
;
1131 if (SupportedLanguages
== NULL
) {
1135 VA_START (Args
, Iso639Language
);
1136 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1138 // Default to ISO 639-2 mode
1141 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1144 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1146 if (Iso639Language
== 0) {
1147 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++) {
1152 // Trim back the length of Language used until it is empty
1154 while (LanguageLength
> 0) {
1156 // Loop through all language codes in SupportedLanguages
1158 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1160 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1162 if (Iso639Language
== 0) {
1164 // Skip ';' characters in Supported
1166 for ( ; *Supported
!= '\0' && *Supported
== ';'; Supported
++) {
1170 // Determine the length of the next language code in Supported
1172 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++) {
1176 // If Language is longer than the Supported, then skip to the next language
1178 if (LanguageLength
> CompareLength
) {
1184 // See if the first LanguageLength characters in Supported match Language
1186 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1189 Buffer
= (Iso639Language
!= 0) ? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1190 Buffer
[CompareLength
] = '\0';
1191 return CopyMem (Buffer
, Supported
, CompareLength
);
1195 if (Iso639Language
!= 0) {
1197 // If ISO 639 mode, then each language can only be tested once
1202 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1204 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--) {
1213 // No matches were found
1219 This function is to check if the remaining variable space is enough to set
1220 all Variables from argument list successfully. The purpose of the check
1221 is to keep the consistency of the Variables to be in variable storage.
1223 Note: Variables are assumed to be in same storage.
1224 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1225 so follow the argument sequence to check the Variables.
1227 @param[in] Attributes Variable attributes for Variable entries.
1228 @param[in] Marker VA_LIST style variable argument list.
1229 The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1230 A NULL terminates the list. The VariableSize of
1231 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
1232 It will be changed to variable total size as output.
1234 @retval TRUE Have enough variable space to set the Variables successfully.
1235 @retval FALSE No enough variable space to set the Variables successfully.
1240 CheckRemainingSpaceForConsistencyInternal (
1241 IN UINT32 Attributes
,
1247 VARIABLE_ENTRY_CONSISTENCY
*VariableEntry
;
1248 UINT64 MaximumVariableStorageSize
;
1249 UINT64 RemainingVariableStorageSize
;
1250 UINT64 MaximumVariableSize
;
1251 UINTN TotalNeededSize
;
1252 UINTN OriginalVarSize
;
1253 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1254 VARIABLE_POINTER_TRACK VariablePtrTrack
;
1255 VARIABLE_HEADER
*NextVariable
;
1260 // Non-Volatile related.
1262 VariableStoreHeader
= mNvVariableCache
;
1264 Status
= VariableServiceQueryVariableInfoInternal (
1266 &MaximumVariableStorageSize
,
1267 &RemainingVariableStorageSize
,
1268 &MaximumVariableSize
1270 ASSERT_EFI_ERROR (Status
);
1272 TotalNeededSize
= 0;
1273 VA_COPY (Args
, Marker
);
1274 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1275 while (VariableEntry
!= NULL
) {
1277 // Calculate variable total size.
1279 VarNameSize
= StrSize (VariableEntry
->Name
);
1280 VarNameSize
+= GET_PAD_SIZE (VarNameSize
);
1281 VarDataSize
= VariableEntry
->VariableSize
;
1282 VarDataSize
+= GET_PAD_SIZE (VarDataSize
);
1283 VariableEntry
->VariableSize
= HEADER_ALIGN (
1284 GetVariableHeaderSize (
1285 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
1286 ) + VarNameSize
+ VarDataSize
1289 TotalNeededSize
+= VariableEntry
->VariableSize
;
1290 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1295 if (RemainingVariableStorageSize
>= TotalNeededSize
) {
1297 // Already have enough space.
1300 } else if (AtRuntime ()) {
1302 // At runtime, no reclaim.
1303 // The original variable space of Variables can't be reused.
1308 VA_COPY (Args
, Marker
);
1309 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1310 while (VariableEntry
!= NULL
) {
1312 // Check if Variable[Index] has been present and get its size.
1314 OriginalVarSize
= 0;
1315 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
1316 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
1317 Status
= FindVariableEx (
1318 VariableEntry
->Name
,
1319 VariableEntry
->Guid
,
1322 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
1324 if (!EFI_ERROR (Status
)) {
1326 // Get size of Variable[Index].
1328 NextVariable
= GetNextVariablePtr (VariablePtrTrack
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
1329 OriginalVarSize
= (UINTN
)NextVariable
- (UINTN
)VariablePtrTrack
.CurrPtr
;
1331 // Add the original size of Variable[Index] to remaining variable storage size.
1333 RemainingVariableStorageSize
+= OriginalVarSize
;
1336 if (VariableEntry
->VariableSize
> RemainingVariableStorageSize
) {
1338 // No enough space for Variable[Index].
1345 // Sub the (new) size of Variable[Index] from remaining variable storage size.
1347 RemainingVariableStorageSize
-= VariableEntry
->VariableSize
;
1348 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1357 This function is to check if the remaining variable space is enough to set
1358 all Variables from argument list successfully. The purpose of the check
1359 is to keep the consistency of the Variables to be in variable storage.
1361 Note: Variables are assumed to be in same storage.
1362 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1363 so follow the argument sequence to check the Variables.
1365 @param[in] Attributes Variable attributes for Variable entries.
1366 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1367 A NULL terminates the list. The VariableSize of
1368 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
1369 It will be changed to variable total size as output.
1371 @retval TRUE Have enough variable space to set the Variables successfully.
1372 @retval FALSE No enough variable space to set the Variables successfully.
1377 CheckRemainingSpaceForConsistency (
1378 IN UINT32 Attributes
,
1385 VA_START (Marker
, Attributes
);
1387 Return
= CheckRemainingSpaceForConsistencyInternal (Attributes
, Marker
);
1395 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1397 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1399 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1400 and are read-only. Therefore, in variable driver, only store the original value for other use.
1402 @param[in] VariableName Name of variable.
1404 @param[in] Data Variable data.
1406 @param[in] DataSize Size of data. 0 means delete.
1408 @retval EFI_SUCCESS The update operation is successful or ignored.
1409 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.
1410 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.
1411 @retval Others Other errors happened during the update operation.
1415 AutoUpdateLangVariable (
1416 IN CHAR16
*VariableName
,
1422 CHAR8
*BestPlatformLang
;
1426 VARIABLE_POINTER_TRACK Variable
;
1427 BOOLEAN SetLanguageCodes
;
1428 VARIABLE_ENTRY_CONSISTENCY VariableEntry
[2];
1431 // Don't do updates for delete operation
1433 if (DataSize
== 0) {
1437 SetLanguageCodes
= FALSE
;
1439 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
) == 0) {
1441 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1444 return EFI_WRITE_PROTECTED
;
1447 SetLanguageCodes
= TRUE
;
1450 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1451 // Therefore, in variable driver, only store the original value for other use.
1453 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1454 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1457 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1458 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1461 // PlatformLang holds a single language from PlatformLangCodes,
1462 // so the size of PlatformLangCodes is enough for the PlatformLang.
1464 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1465 FreePool (mVariableModuleGlobal
->PlatformLang
);
1468 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1469 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1470 } else if (StrCmp (VariableName
, EFI_LANG_CODES_VARIABLE_NAME
) == 0) {
1472 // LangCodes is a volatile variable, so it can not be updated at runtime.
1475 return EFI_WRITE_PROTECTED
;
1478 SetLanguageCodes
= TRUE
;
1481 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1482 // Therefore, in variable driver, only store the original value for other use.
1484 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1485 FreePool (mVariableModuleGlobal
->LangCodes
);
1488 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1489 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1492 if ( SetLanguageCodes
1493 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1494 && (mVariableModuleGlobal
->LangCodes
!= NULL
))
1497 // Update Lang if PlatformLang is already set
1498 // Update PlatformLang if Lang is already set
1500 Status
= FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1501 if (!EFI_ERROR (Status
)) {
1505 VariableName
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1506 Data
= GetVariableDataPtr (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
1507 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
1509 Status
= FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1510 if (!EFI_ERROR (Status
)) {
1512 // Update PlatformLang
1514 VariableName
= EFI_LANG_VARIABLE_NAME
;
1515 Data
= GetVariableDataPtr (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
1516 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
1519 // Neither PlatformLang nor Lang is set, directly return
1526 Status
= EFI_SUCCESS
;
1529 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1531 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1533 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_VARIABLE_NAME
) == 0) {
1535 // Update Lang when PlatformLangCodes/LangCodes were set.
1537 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1539 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1541 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1542 if (BestPlatformLang
!= NULL
) {
1544 // Get the corresponding index in language codes.
1546 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1549 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1551 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1554 // Check the variable space for both Lang and PlatformLang variable.
1556 VariableEntry
[0].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1557 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1558 VariableEntry
[0].Name
= EFI_LANG_VARIABLE_NAME
;
1560 VariableEntry
[1].VariableSize
= AsciiStrSize (BestPlatformLang
);
1561 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1562 VariableEntry
[1].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1563 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1565 // No enough variable space to set both Lang and PlatformLang successfully.
1567 Status
= EFI_OUT_OF_RESOURCES
;
1570 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1572 FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1574 Status
= UpdateVariable (
1575 EFI_LANG_VARIABLE_NAME
,
1576 &gEfiGlobalVariableGuid
,
1578 ISO_639_2_ENTRY_SIZE
+ 1,
1587 DEBUG ((DEBUG_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang
, BestLang
, Status
));
1590 } else if (StrCmp (VariableName
, EFI_LANG_VARIABLE_NAME
) == 0) {
1592 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1594 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1596 // When setting Lang, firstly get most matched language string from supported language codes.
1598 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1599 if (BestLang
!= NULL
) {
1601 // Get the corresponding index in language codes.
1603 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1606 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1608 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1611 // Check the variable space for both PlatformLang and Lang variable.
1613 VariableEntry
[0].VariableSize
= AsciiStrSize (BestPlatformLang
);
1614 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1615 VariableEntry
[0].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1617 VariableEntry
[1].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1618 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1619 VariableEntry
[1].Name
= EFI_LANG_VARIABLE_NAME
;
1620 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1622 // No enough variable space to set both PlatformLang and Lang successfully.
1624 Status
= EFI_OUT_OF_RESOURCES
;
1627 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1629 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1631 Status
= UpdateVariable (
1632 EFI_PLATFORM_LANG_VARIABLE_NAME
,
1633 &gEfiGlobalVariableGuid
,
1635 AsciiStrSize (BestPlatformLang
),
1644 DEBUG ((DEBUG_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang
, BestPlatformLang
, Status
));
1649 if (SetLanguageCodes
) {
1651 // Continue to set PlatformLangCodes or LangCodes.
1660 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
1661 index of associated public key is needed.
1663 @param[in] VariableName Name of variable.
1664 @param[in] VendorGuid Guid of variable.
1665 @param[in] Data Variable data.
1666 @param[in] DataSize Size of data. 0 means delete.
1667 @param[in] Attributes Attributes of the variable.
1668 @param[in] KeyIndex Index of associated public key.
1669 @param[in] MonotonicCount Value of associated monotonic count.
1670 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.
1671 @param[in] TimeStamp Value of associated TimeStamp.
1673 @retval EFI_SUCCESS The update operation is success.
1674 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1679 IN CHAR16
*VariableName
,
1680 IN EFI_GUID
*VendorGuid
,
1683 IN UINT32 Attributes OPTIONAL
,
1684 IN UINT32 KeyIndex OPTIONAL
,
1685 IN UINT64 MonotonicCount OPTIONAL
,
1686 IN OUT VARIABLE_POINTER_TRACK
*CacheVariable
,
1687 IN EFI_TIME
*TimeStamp OPTIONAL
1691 VARIABLE_HEADER
*NextVariable
;
1694 UINTN VarNameOffset
;
1695 UINTN VarDataOffset
;
1699 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1701 VARIABLE_POINTER_TRACK
*Variable
;
1702 VARIABLE_POINTER_TRACK NvVariable
;
1703 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1704 VARIABLE_RUNTIME_CACHE
*VolatileCacheInstance
;
1705 UINT8
*BufferForMerge
;
1706 UINTN MergedBufSize
;
1709 BOOLEAN IsCommonVariable
;
1710 BOOLEAN IsCommonUserVariable
;
1711 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
1714 if ((mVariableModuleGlobal
->FvbInstance
== NULL
) && !mVariableModuleGlobal
->VariableGlobal
.EmuNvMode
) {
1716 // The FVB protocol is not ready, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
1718 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1720 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1722 DEBUG ((DEBUG_ERROR
, "Update NV variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET
));
1723 return EFI_NOT_AVAILABLE_YET
;
1724 } else if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
1726 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1727 // The authenticated variable perhaps is not initialized, just return here.
1729 DEBUG ((DEBUG_ERROR
, "Update AUTH variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET
));
1730 return EFI_NOT_AVAILABLE_YET
;
1734 AuthFormat
= mVariableModuleGlobal
->VariableGlobal
.AuthFormat
;
1737 // Check if CacheVariable points to the variable in variable HOB.
1738 // If yes, let CacheVariable points to the variable in NV variable cache.
1740 if ((CacheVariable
->CurrPtr
!= NULL
) &&
1741 (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) &&
1742 (CacheVariable
->StartPtr
== GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
))
1745 CacheVariable
->StartPtr
= GetStartPointer (mNvVariableCache
);
1746 CacheVariable
->EndPtr
= GetEndPointer (mNvVariableCache
);
1747 CacheVariable
->Volatile
= FALSE
;
1748 Status
= FindVariableEx (VariableName
, VendorGuid
, FALSE
, CacheVariable
, AuthFormat
);
1749 if ((CacheVariable
->CurrPtr
== NULL
) || EFI_ERROR (Status
)) {
1751 // There is no matched variable in NV variable cache.
1753 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0)) || (Attributes
== 0)) {
1755 // It is to delete variable,
1756 // go to delete this variable in variable HOB and
1757 // try to flush other variables from HOB to flash.
1759 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, &gVariableInfo
);
1760 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1766 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1767 Variable
= CacheVariable
;
1770 // Update/Delete existing NV variable.
1771 // CacheVariable points to the variable in the memory copy of Flash area
1772 // Now let Variable points to the same variable in Flash area.
1774 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINTN
)mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1775 Variable
= &NvVariable
;
1776 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1777 Variable
->EndPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->EndPtr
- (UINTN
)CacheVariable
->StartPtr
));
1779 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1780 if (CacheVariable
->InDeletedTransitionPtr
!= NULL
) {
1781 Variable
->InDeletedTransitionPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->InDeletedTransitionPtr
- (UINTN
)CacheVariable
->StartPtr
));
1783 Variable
->InDeletedTransitionPtr
= NULL
;
1786 Variable
->Volatile
= FALSE
;
1789 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1792 // Tricky part: Use scratch data area at the end of volatile variable store
1793 // as a temporary storage.
1795 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*)((UINTN
)mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1796 ScratchSize
= mVariableModuleGlobal
->ScratchBufferSize
;
1797 SetMem (NextVariable
, ScratchSize
, 0xff);
1800 if (Variable
->CurrPtr
!= NULL
) {
1802 // Update/Delete existing variable.
1806 // If AtRuntime and the variable is Volatile and Runtime Access,
1807 // the volatile is ReadOnly, and SetVariable should be aborted and
1808 // return EFI_WRITE_PROTECTED.
1810 if (Variable
->Volatile
) {
1811 Status
= EFI_WRITE_PROTECTED
;
1816 // Only variable that have NV attributes can be updated/deleted in Runtime.
1818 if ((CacheVariable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1819 Status
= EFI_INVALID_PARAMETER
;
1824 // Only variable that have RT attributes can be updated/deleted in Runtime.
1826 if ((CacheVariable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) {
1827 Status
= EFI_INVALID_PARAMETER
;
1833 // Setting a data variable with no access, or zero DataSize attributes
1834 // causes it to be deleted.
1835 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
1836 // not delete the variable.
1838 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0)) || ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
1839 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
1841 // Both ADDED and IN_DELETED_TRANSITION variable are present,
1842 // set IN_DELETED_TRANSITION one to DELETED state first.
1844 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
1845 State
= CacheVariable
->InDeletedTransitionPtr
->State
;
1846 State
&= VAR_DELETED
;
1847 Status
= UpdateVariableStore (
1848 &mVariableModuleGlobal
->VariableGlobal
,
1852 (UINTN
)&Variable
->InDeletedTransitionPtr
->State
,
1856 if (!EFI_ERROR (Status
)) {
1857 if (!Variable
->Volatile
) {
1858 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
1865 State
= CacheVariable
->CurrPtr
->State
;
1866 State
&= VAR_DELETED
;
1868 Status
= UpdateVariableStore (
1869 &mVariableModuleGlobal
->VariableGlobal
,
1873 (UINTN
)&Variable
->CurrPtr
->State
,
1877 if (!EFI_ERROR (Status
)) {
1878 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
, &gVariableInfo
);
1879 if (!Variable
->Volatile
) {
1880 CacheVariable
->CurrPtr
->State
= State
;
1881 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1889 // If the variable is marked valid, and the same data has been passed in,
1890 // then return to the caller immediately.
1892 if ((DataSizeOfVariable (CacheVariable
->CurrPtr
, AuthFormat
) == DataSize
) &&
1893 (CompareMem (Data
, GetVariableDataPtr (CacheVariable
->CurrPtr
, AuthFormat
), DataSize
) == 0) &&
1894 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) &&
1895 (TimeStamp
== NULL
))
1898 // Variable content unchanged and no need to update timestamp, just return.
1900 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
, &gVariableInfo
);
1901 Status
= EFI_SUCCESS
;
1903 } else if ((CacheVariable
->CurrPtr
->State
== VAR_ADDED
) ||
1904 (CacheVariable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
)))
1907 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable.
1909 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
1911 // NOTE: From 0 to DataOffset of NextVariable is reserved for Variable Header and Name.
1912 // From DataOffset of NextVariable is to save the existing variable data.
1914 DataOffset
= GetVariableDataOffset (CacheVariable
->CurrPtr
, AuthFormat
);
1915 BufferForMerge
= (UINT8
*)((UINTN
)NextVariable
+ DataOffset
);
1918 (UINT8
*)((UINTN
)CacheVariable
->CurrPtr
+ DataOffset
),
1919 DataSizeOfVariable (CacheVariable
->CurrPtr
, AuthFormat
)
1923 // Set Max Auth/Non-Volatile/Volatile Variable Data Size as default MaxDataSize.
1925 if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
1926 MaxDataSize
= mVariableModuleGlobal
->MaxAuthVariableSize
- DataOffset
;
1927 } else if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1928 MaxDataSize
= mVariableModuleGlobal
->MaxVariableSize
- DataOffset
;
1930 MaxDataSize
= mVariableModuleGlobal
->MaxVolatileVariableSize
- DataOffset
;
1934 // Append the new data to the end of existing data.
1935 // Max Harware error record variable data size is different from common/auth variable.
1937 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1938 MaxDataSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - DataOffset
;
1941 if (DataSizeOfVariable (CacheVariable
->CurrPtr
, AuthFormat
) + DataSize
> MaxDataSize
) {
1943 // Existing data size + new data size exceed maximum variable size limitation.
1945 Status
= EFI_INVALID_PARAMETER
;
1951 (UINTN
)BufferForMerge
+ DataSizeOfVariable (CacheVariable
->CurrPtr
, AuthFormat
)
1956 MergedBufSize
= DataSizeOfVariable (CacheVariable
->CurrPtr
, AuthFormat
) +
1960 // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.
1962 Data
= BufferForMerge
;
1963 DataSize
= MergedBufSize
;
1968 // Mark the old variable as in delete transition.
1970 State
= CacheVariable
->CurrPtr
->State
;
1971 State
&= VAR_IN_DELETED_TRANSITION
;
1973 Status
= UpdateVariableStore (
1974 &mVariableModuleGlobal
->VariableGlobal
,
1978 (UINTN
)&Variable
->CurrPtr
->State
,
1982 if (EFI_ERROR (Status
)) {
1986 if (!Variable
->Volatile
) {
1987 CacheVariable
->CurrPtr
->State
= State
;
1992 // Not found existing variable. Create a new variable.
1995 if ((DataSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
1996 Status
= EFI_SUCCESS
;
2001 // Make sure we are trying to create a new variable.
2002 // Setting a data variable with zero DataSize or no access attributes means to delete it.
2004 if ((DataSize
== 0) || ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
2005 Status
= EFI_NOT_FOUND
;
2010 // Only variable have NV|RT attribute can be created in Runtime.
2013 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0)))
2015 Status
= EFI_INVALID_PARAMETER
;
2021 // Function part - create a new variable and copy the data.
2022 // Both update a variable and create a variable will come here.
2024 NextVariable
->StartId
= VARIABLE_DATA
;
2026 // NextVariable->State = VAR_ADDED;
2028 NextVariable
->Reserved
= 0;
2029 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
2030 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*)NextVariable
;
2031 AuthVariable
->PubKeyIndex
= KeyIndex
;
2032 AuthVariable
->MonotonicCount
= MonotonicCount
;
2033 ZeroMem (&AuthVariable
->TimeStamp
, sizeof (EFI_TIME
));
2035 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
2036 (TimeStamp
!= NULL
))
2038 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
2039 CopyMem (&AuthVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
2042 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
2043 // when the new TimeStamp value is later than the current timestamp associated
2044 // with the variable, we need associate the new timestamp with the updated value.
2046 if (Variable
->CurrPtr
!= NULL
) {
2047 if (VariableCompareTimeStampInternal (&(((AUTHENTICATED_VARIABLE_HEADER
*)CacheVariable
->CurrPtr
)->TimeStamp
), TimeStamp
)) {
2048 CopyMem (&AuthVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
2050 CopyMem (&AuthVariable
->TimeStamp
, &(((AUTHENTICATED_VARIABLE_HEADER
*)CacheVariable
->CurrPtr
)->TimeStamp
), sizeof (EFI_TIME
));
2058 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
2059 // Attributes bitmask parameter of a GetVariable() call.
2061 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
2063 VarNameOffset
= GetVariableHeaderSize (AuthFormat
);
2064 VarNameSize
= StrSize (VariableName
);
2066 (UINT8
*)((UINTN
)NextVariable
+ VarNameOffset
),
2070 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
2073 // If DataReady is TRUE, it means the variable data has been saved into
2074 // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.
2078 (UINT8
*)((UINTN
)NextVariable
+ VarDataOffset
),
2085 GetVendorGuidPtr (NextVariable
, AuthFormat
),
2090 // There will be pad bytes after Data, the NextVariable->NameSize and
2091 // NextVariable->DataSize should not include pad size so that variable
2092 // service can get actual size in GetVariable.
2094 SetNameSizeOfVariable (NextVariable
, VarNameSize
, AuthFormat
);
2095 SetDataSizeOfVariable (NextVariable
, DataSize
, AuthFormat
);
2098 // The actual size of the variable that stores in storage should
2099 // include pad size.
2101 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
2102 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2104 // Create a nonvolatile variable.
2108 IsCommonVariable
= FALSE
;
2109 IsCommonUserVariable
= FALSE
;
2110 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0) {
2111 IsCommonVariable
= TRUE
;
2112 IsCommonUserVariable
= IsUserVariable (NextVariable
);
2115 if ( ( ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2116 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
2117 || (IsCommonVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonVariableSpace
))
2118 || (IsCommonVariable
&& AtRuntime () && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonRuntimeVariableSpace
))
2119 || (IsCommonUserVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonUserVariableTotalSize
) > mVariableModuleGlobal
->CommonMaxUserVariableSpace
)))
2122 if (IsCommonUserVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonUserVariableTotalSize
) > mVariableModuleGlobal
->CommonMaxUserVariableSpace
)) {
2123 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2126 if (IsCommonVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonRuntimeVariableSpace
)) {
2127 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2130 Status
= EFI_OUT_OF_RESOURCES
;
2135 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2138 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2139 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2143 HEADER_ALIGN (VarSize
)
2145 if (!EFI_ERROR (Status
)) {
2147 // The new variable has been integrated successfully during reclaiming.
2149 if (Variable
->CurrPtr
!= NULL
) {
2150 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)CacheVariable
->StartPtr
+ ((UINTN
)Variable
->CurrPtr
- (UINTN
)Variable
->StartPtr
));
2151 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2154 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, &gVariableInfo
);
2155 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2157 if (IsCommonUserVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonUserVariableTotalSize
) > mVariableModuleGlobal
->CommonMaxUserVariableSpace
)) {
2158 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2161 if (IsCommonVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonVariableSpace
)) {
2162 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2169 if (!mVariableModuleGlobal
->VariableGlobal
.EmuNvMode
) {
2172 // 1. Write variable header
2173 // 2. Set variable state to header valid
2174 // 3. Write variable data
2175 // 4. Set variable state to valid
2180 Status
= UpdateVariableStore (
2181 &mVariableModuleGlobal
->VariableGlobal
,
2185 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2186 (UINT32
)GetVariableHeaderSize (AuthFormat
),
2187 (UINT8
*)NextVariable
2190 if (EFI_ERROR (Status
)) {
2197 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
2198 Status
= UpdateVariableStore (
2199 &mVariableModuleGlobal
->VariableGlobal
,
2203 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2205 &NextVariable
->State
2208 if (EFI_ERROR (Status
)) {
2215 Status
= UpdateVariableStore (
2216 &mVariableModuleGlobal
->VariableGlobal
,
2220 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ GetVariableHeaderSize (AuthFormat
),
2221 (UINT32
)(VarSize
- GetVariableHeaderSize (AuthFormat
)),
2222 (UINT8
*)NextVariable
+ GetVariableHeaderSize (AuthFormat
)
2225 if (EFI_ERROR (Status
)) {
2232 NextVariable
->State
= VAR_ADDED
;
2233 Status
= UpdateVariableStore (
2234 &mVariableModuleGlobal
->VariableGlobal
,
2238 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2240 &NextVariable
->State
2243 if (EFI_ERROR (Status
)) {
2248 // Update the memory copy of Flash region.
2250 CopyMem ((UINT8
*)mNvVariableCache
+ mVariableModuleGlobal
->NonVolatileLastVariableOffset
, (UINT8
*)NextVariable
, VarSize
);
2253 // Emulated non-volatile variable mode.
2255 NextVariable
->State
= VAR_ADDED
;
2256 Status
= UpdateVariableStore (
2257 &mVariableModuleGlobal
->VariableGlobal
,
2261 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2263 (UINT8
*)NextVariable
2266 if (EFI_ERROR (Status
)) {
2271 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2273 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2274 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2276 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2277 if (IsCommonUserVariable
) {
2278 mVariableModuleGlobal
->CommonUserVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2283 // Create a volatile variable.
2287 if ((UINT32
)(VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2288 ((VARIABLE_STORE_HEADER
*)((UINTN
)(mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
)
2291 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2294 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
2295 &mVariableModuleGlobal
->VolatileLastVariableOffset
,
2299 HEADER_ALIGN (VarSize
)
2301 if (!EFI_ERROR (Status
)) {
2303 // The new variable has been integrated successfully during reclaiming.
2305 if (Variable
->CurrPtr
!= NULL
) {
2306 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)CacheVariable
->StartPtr
+ ((UINTN
)Variable
->CurrPtr
- (UINTN
)Variable
->StartPtr
));
2307 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2310 UpdateVariableInfo (VariableName
, VendorGuid
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
, &gVariableInfo
);
2316 NextVariable
->State
= VAR_ADDED
;
2317 Status
= UpdateVariableStore (
2318 &mVariableModuleGlobal
->VariableGlobal
,
2322 mVariableModuleGlobal
->VolatileLastVariableOffset
,
2324 (UINT8
*)NextVariable
2327 if (EFI_ERROR (Status
)) {
2331 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2335 // Mark the old variable as deleted.
2337 if (!EFI_ERROR (Status
) && (Variable
->CurrPtr
!= NULL
)) {
2338 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2340 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
2341 // set IN_DELETED_TRANSITION one to DELETED state first.
2343 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2344 State
= CacheVariable
->InDeletedTransitionPtr
->State
;
2345 State
&= VAR_DELETED
;
2346 Status
= UpdateVariableStore (
2347 &mVariableModuleGlobal
->VariableGlobal
,
2351 (UINTN
)&Variable
->InDeletedTransitionPtr
->State
,
2355 if (!EFI_ERROR (Status
)) {
2356 if (!Variable
->Volatile
) {
2357 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2364 State
= CacheVariable
->CurrPtr
->State
;
2365 State
&= VAR_DELETED
;
2367 Status
= UpdateVariableStore (
2368 &mVariableModuleGlobal
->VariableGlobal
,
2372 (UINTN
)&Variable
->CurrPtr
->State
,
2376 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
2377 CacheVariable
->CurrPtr
->State
= State
;
2381 if (!EFI_ERROR (Status
)) {
2382 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
, &gVariableInfo
);
2384 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2389 if (!EFI_ERROR (Status
)) {
2390 if (((Variable
->CurrPtr
!= NULL
) && !Variable
->Volatile
) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0)) {
2391 VolatileCacheInstance
= &(mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeNvCache
);
2393 VolatileCacheInstance
= &(mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeVolatileCache
);
2396 if (VolatileCacheInstance
->Store
!= NULL
) {
2397 Status
= SynchronizeRuntimeVariableCache (
2398 VolatileCacheInstance
,
2400 VolatileCacheInstance
->Store
->Size
2402 ASSERT_EFI_ERROR (Status
);
2411 This code finds variable in storage blocks (Volatile or Non-Volatile).
2413 Caution: This function may receive untrusted input.
2414 This function may be invoked in SMM mode, and datasize is external input.
2415 This function will do basic validation, before parse the data.
2417 @param VariableName Name of Variable to be found.
2418 @param VendorGuid Variable vendor GUID.
2419 @param Attributes Attribute value of the variable found.
2420 @param DataSize Size of Data found. If size is less than the
2421 data, this value contains the required size.
2422 @param Data The buffer to return the contents of the variable. May be NULL
2423 with a zero DataSize in order to determine the size buffer needed.
2425 @return EFI_INVALID_PARAMETER Invalid parameter.
2426 @return EFI_SUCCESS Find the specified variable.
2427 @return EFI_NOT_FOUND Not found.
2428 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2433 VariableServiceGetVariable (
2434 IN CHAR16
*VariableName
,
2435 IN EFI_GUID
*VendorGuid
,
2436 OUT UINT32
*Attributes OPTIONAL
,
2437 IN OUT UINTN
*DataSize
,
2438 OUT VOID
*Data OPTIONAL
2442 VARIABLE_POINTER_TRACK Variable
;
2445 if ((VariableName
== NULL
) || (VendorGuid
== NULL
) || (DataSize
== NULL
)) {
2446 return EFI_INVALID_PARAMETER
;
2449 if (VariableName
[0] == 0) {
2450 return EFI_NOT_FOUND
;
2453 AcquireLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2455 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2456 if ((Variable
.CurrPtr
== NULL
) || EFI_ERROR (Status
)) {
2463 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
2464 ASSERT (VarDataSize
!= 0);
2466 if (*DataSize
>= VarDataSize
) {
2468 Status
= EFI_INVALID_PARAMETER
;
2472 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
), VarDataSize
);
2474 *DataSize
= VarDataSize
;
2475 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
, &gVariableInfo
);
2477 Status
= EFI_SUCCESS
;
2480 *DataSize
= VarDataSize
;
2481 Status
= EFI_BUFFER_TOO_SMALL
;
2486 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_BUFFER_TOO_SMALL
)) {
2487 if ((Attributes
!= NULL
) && (Variable
.CurrPtr
!= NULL
)) {
2488 *Attributes
= Variable
.CurrPtr
->Attributes
;
2492 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2498 This code Finds the Next available variable.
2500 Caution: This function may receive untrusted input.
2501 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2503 @param VariableNameSize The size of the VariableName buffer. The size must be large
2504 enough to fit input string supplied in VariableName buffer.
2505 @param VariableName Pointer to variable name.
2506 @param VendorGuid Variable Vendor Guid.
2508 @retval EFI_SUCCESS The function completed successfully.
2509 @retval EFI_NOT_FOUND The next variable was not found.
2510 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the result.
2511 VariableNameSize has been updated with the size needed to complete the request.
2512 @retval EFI_INVALID_PARAMETER VariableNameSize is NULL.
2513 @retval EFI_INVALID_PARAMETER VariableName is NULL.
2514 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.
2515 @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
2516 GUID of an existing variable.
2517 @retval EFI_INVALID_PARAMETER Null-terminator is not found in the first VariableNameSize bytes of
2518 the input VariableName buffer.
2523 VariableServiceGetNextVariableName (
2524 IN OUT UINTN
*VariableNameSize
,
2525 IN OUT CHAR16
*VariableName
,
2526 IN OUT EFI_GUID
*VendorGuid
2533 VARIABLE_HEADER
*VariablePtr
;
2534 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2536 if ((VariableNameSize
== NULL
) || (VariableName
== NULL
) || (VendorGuid
== NULL
)) {
2537 return EFI_INVALID_PARAMETER
;
2540 AuthFormat
= mVariableModuleGlobal
->VariableGlobal
.AuthFormat
;
2543 // Calculate the possible maximum length of name string, including the Null terminator.
2545 MaxLen
= *VariableNameSize
/ sizeof (CHAR16
);
2546 if ((MaxLen
== 0) || (StrnLenS (VariableName
, MaxLen
) == MaxLen
)) {
2548 // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,
2549 // follow spec to return EFI_INVALID_PARAMETER.
2551 return EFI_INVALID_PARAMETER
;
2554 AcquireLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2557 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2558 // The index and attributes mapping must be kept in this order as FindVariable
2559 // makes use of this mapping to implement search algorithm.
2561 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*)(UINTN
)mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2562 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*)(UINTN
)mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2563 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2565 Status
= VariableServiceGetNextVariableInternal (
2568 VariableStoreHeader
,
2572 if (!EFI_ERROR (Status
)) {
2573 VarNameSize
= NameSizeOfVariable (VariablePtr
, AuthFormat
);
2574 ASSERT (VarNameSize
!= 0);
2575 if (VarNameSize
<= *VariableNameSize
) {
2578 GetVariableNamePtr (VariablePtr
, AuthFormat
),
2583 GetVendorGuidPtr (VariablePtr
, AuthFormat
),
2586 Status
= EFI_SUCCESS
;
2588 Status
= EFI_BUFFER_TOO_SMALL
;
2591 *VariableNameSize
= VarNameSize
;
2594 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2600 This code sets variable in storage blocks (Volatile or Non-Volatile).
2602 Caution: This function may receive untrusted input.
2603 This function may be invoked in SMM mode, and datasize and data are external input.
2604 This function will do basic validation, before parse the data.
2605 This function will parse the authentication carefully to avoid security issues, like
2606 buffer overflow, integer overflow.
2607 This function will check attribute carefully to avoid authentication bypass.
2609 @param VariableName Name of Variable to be found.
2610 @param VendorGuid Variable vendor GUID.
2611 @param Attributes Attribute value of the variable found
2612 @param DataSize Size of Data found. If size is less than the
2613 data, this value contains the required size.
2614 @param Data Data pointer.
2616 @return EFI_INVALID_PARAMETER Invalid parameter.
2617 @return EFI_SUCCESS Set successfully.
2618 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2619 @return EFI_NOT_FOUND Not found.
2620 @return EFI_WRITE_PROTECTED Variable is read-only.
2625 VariableServiceSetVariable (
2626 IN CHAR16
*VariableName
,
2627 IN EFI_GUID
*VendorGuid
,
2628 IN UINT32 Attributes
,
2633 VARIABLE_POINTER_TRACK Variable
;
2635 VARIABLE_HEADER
*NextVariable
;
2636 EFI_PHYSICAL_ADDRESS Point
;
2640 AuthFormat
= mVariableModuleGlobal
->VariableGlobal
.AuthFormat
;
2643 // Check input parameters.
2645 if ((VariableName
== NULL
) || (VariableName
[0] == 0) || (VendorGuid
== NULL
)) {
2646 return EFI_INVALID_PARAMETER
;
2649 if ((DataSize
!= 0) && (Data
== NULL
)) {
2650 return EFI_INVALID_PARAMETER
;
2654 // Check for reserverd bit in variable attribute.
2655 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated but we still allow
2656 // the delete operation of common authenticated variable at user physical presence.
2657 // So leave EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute check to AuthVariableLib
2659 if ((Attributes
& (~(EFI_VARIABLE_ATTRIBUTES_MASK
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
))) != 0) {
2660 return EFI_INVALID_PARAMETER
;
2664 // Check if the combination of attribute bits is valid.
2666 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2668 // Make sure if runtime bit is set, boot service bit is set also.
2670 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2671 return EFI_UNSUPPORTED
;
2673 return EFI_INVALID_PARAMETER
;
2675 } else if ((Attributes
& EFI_VARIABLE_ATTRIBUTES_MASK
) == EFI_VARIABLE_NON_VOLATILE
) {
2677 // Only EFI_VARIABLE_NON_VOLATILE attribute is invalid
2679 return EFI_INVALID_PARAMETER
;
2680 } else if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
2681 if (!mVariableModuleGlobal
->VariableGlobal
.AuthSupport
) {
2683 // Not support authenticated variable write.
2685 return EFI_INVALID_PARAMETER
;
2687 } else if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2688 if (PcdGet32 (PcdHwErrStorageSize
) == 0) {
2690 // Not support harware error record variable variable.
2692 return EFI_INVALID_PARAMETER
;
2697 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
2698 // cannot be set both.
2700 if ( ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
2701 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
))
2703 return EFI_UNSUPPORTED
;
2706 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2708 // If DataSize == AUTHINFO_SIZE and then PayloadSize is 0.
2709 // Maybe it's the delete operation of common authenticated variable at user physical presence.
2711 if (DataSize
!= AUTHINFO_SIZE
) {
2712 return EFI_UNSUPPORTED
;
2715 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2716 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
2718 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2720 if ((DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
) ||
2721 (((EFI_VARIABLE_AUTHENTICATION_2
*)Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
))) ||
2722 (((EFI_VARIABLE_AUTHENTICATION_2
*)Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)))
2724 return EFI_SECURITY_VIOLATION
;
2728 // The VariableSpeculationBarrier() call here is to ensure the above sanity
2729 // check for the EFI_VARIABLE_AUTHENTICATION_2 descriptor has been completed
2730 // before the execution of subsequent codes.
2732 VariableSpeculationBarrier ();
2733 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
2735 PayloadSize
= DataSize
;
2738 if ((UINTN
)(~0) - PayloadSize
< StrSize (VariableName
)) {
2740 // Prevent whole variable size overflow
2742 return EFI_INVALID_PARAMETER
;
2746 // The size of the VariableName, including the Unicode Null in bytes plus
2747 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2748 // bytes for HwErrRec#### variable.
2750 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2751 if (StrSize (VariableName
) + PayloadSize
>
2752 PcdGet32 (PcdMaxHardwareErrorVariableSize
) - GetVariableHeaderSize (AuthFormat
))
2754 return EFI_INVALID_PARAMETER
;
2758 // The size of the VariableName, including the Unicode Null in bytes plus
2759 // the DataSize is limited to maximum size of Max(Auth|Volatile)VariableSize bytes.
2761 if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
2762 if (StrSize (VariableName
) + PayloadSize
>
2763 mVariableModuleGlobal
->MaxAuthVariableSize
-
2764 GetVariableHeaderSize (AuthFormat
))
2768 "%a: Failed to set variable '%s' with Guid %g\n",
2775 "NameSize(0x%x) + PayloadSize(0x%x) > "
2776 "MaxAuthVariableSize(0x%x) - HeaderSize(0x%x)\n",
2777 StrSize (VariableName
),
2779 mVariableModuleGlobal
->MaxAuthVariableSize
,
2780 GetVariableHeaderSize (AuthFormat
)
2782 return EFI_INVALID_PARAMETER
;
2784 } else if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2785 if (StrSize (VariableName
) + PayloadSize
>
2786 mVariableModuleGlobal
->MaxVariableSize
- GetVariableHeaderSize (AuthFormat
))
2790 "%a: Failed to set variable '%s' with Guid %g\n",
2797 "NameSize(0x%x) + PayloadSize(0x%x) > "
2798 "MaxVariableSize(0x%x) - HeaderSize(0x%x)\n",
2799 StrSize (VariableName
),
2801 mVariableModuleGlobal
->MaxVariableSize
,
2802 GetVariableHeaderSize (AuthFormat
)
2804 return EFI_INVALID_PARAMETER
;
2807 if (StrSize (VariableName
) + PayloadSize
>
2808 mVariableModuleGlobal
->MaxVolatileVariableSize
- GetVariableHeaderSize (AuthFormat
))
2812 "%a: Failed to set variable '%s' with Guid %g\n",
2819 "NameSize(0x%x) + PayloadSize(0x%x) > "
2820 "MaxVolatileVariableSize(0x%x) - HeaderSize(0x%x)\n",
2821 StrSize (VariableName
),
2823 mVariableModuleGlobal
->MaxVolatileVariableSize
,
2824 GetVariableHeaderSize (AuthFormat
)
2826 return EFI_INVALID_PARAMETER
;
2832 // Special Handling for MOR Lock variable.
2834 Status
= SetVariableCheckHandlerMor (VariableName
, VendorGuid
, Attributes
, PayloadSize
, (VOID
*)((UINTN
)Data
+ DataSize
- PayloadSize
));
2835 if (Status
== EFI_ALREADY_STARTED
) {
2837 // EFI_ALREADY_STARTED means the SetVariable() action is handled inside of SetVariableCheckHandlerMor().
2838 // Variable driver can just return SUCCESS.
2843 if (EFI_ERROR (Status
)) {
2847 Status
= VarCheckLibSetVariableCheck (VariableName
, VendorGuid
, Attributes
, PayloadSize
, (VOID
*)((UINTN
)Data
+ DataSize
- PayloadSize
), mRequestSource
);
2848 if (EFI_ERROR (Status
)) {
2852 AcquireLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2855 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2857 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2858 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2860 // Parse non-volatile variable data and get last variable offset.
2862 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)Point
);
2863 while (IsValidVariableHeader (NextVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)Point
))) {
2864 NextVariable
= GetNextVariablePtr (NextVariable
, AuthFormat
);
2867 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
)NextVariable
- (UINTN
)Point
;
2871 // Check whether the input variable is already existed.
2873 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2874 if (!EFI_ERROR (Status
)) {
2875 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2876 Status
= EFI_WRITE_PROTECTED
;
2880 if ((Attributes
!= 0) && ((Attributes
& (~EFI_VARIABLE_APPEND_WRITE
)) != Variable
.CurrPtr
->Attributes
)) {
2882 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not
2883 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:
2884 // 1. No access attributes specified
2885 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE
2887 Status
= EFI_INVALID_PARAMETER
;
2888 DEBUG ((DEBUG_INFO
, "[Variable]: Rewritten a preexisting variable(0x%08x) with different attributes(0x%08x) - %g:%s\n", Variable
.CurrPtr
->Attributes
, Attributes
, VendorGuid
, VariableName
));
2893 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate
)) {
2895 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2897 Status
= AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2898 if (EFI_ERROR (Status
)) {
2900 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.
2906 if (mVariableModuleGlobal
->VariableGlobal
.AuthSupport
) {
2907 Status
= AuthVariableLibProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
2909 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, &Variable
, NULL
);
2913 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2914 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2916 if (!AtRuntime ()) {
2917 if (!EFI_ERROR (Status
)) {
2930 This code returns information about the EFI variables.
2932 Caution: This function may receive untrusted input.
2933 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2935 @param Attributes Attributes bitmask to specify the type of variables
2936 on which to return information.
2937 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2938 for the EFI variables associated with the attributes specified.
2939 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2940 for EFI variables associated with the attributes specified.
2941 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2942 associated with the attributes specified.
2944 @return EFI_SUCCESS Query successfully.
2949 VariableServiceQueryVariableInfoInternal (
2950 IN UINT32 Attributes
,
2951 OUT UINT64
*MaximumVariableStorageSize
,
2952 OUT UINT64
*RemainingVariableStorageSize
,
2953 OUT UINT64
*MaximumVariableSize
2956 VARIABLE_HEADER
*Variable
;
2957 VARIABLE_HEADER
*NextVariable
;
2958 UINT64 VariableSize
;
2959 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2960 UINT64 CommonVariableTotalSize
;
2961 UINT64 HwErrVariableTotalSize
;
2963 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2965 CommonVariableTotalSize
= 0;
2966 HwErrVariableTotalSize
= 0;
2968 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2970 // Query is Volatile related.
2972 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)((UINTN
)mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2975 // Query is Non-Volatile related.
2977 VariableStoreHeader
= mNvVariableCache
;
2981 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2982 // with the storage size (excluding the storage header size).
2984 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2987 // Harware error record variable needs larger size.
2989 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2990 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2991 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) -
2992 GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
2994 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2996 *MaximumVariableStorageSize
= mVariableModuleGlobal
->CommonRuntimeVariableSpace
;
2998 *MaximumVariableStorageSize
= mVariableModuleGlobal
->CommonVariableSpace
;
3003 // Let *MaximumVariableSize be Max(Auth|Volatile)VariableSize with the exception of the variable header size.
3005 if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
3006 *MaximumVariableSize
= mVariableModuleGlobal
->MaxAuthVariableSize
-
3007 GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
3008 } else if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
3009 *MaximumVariableSize
= mVariableModuleGlobal
->MaxVariableSize
-
3010 GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
3012 *MaximumVariableSize
= mVariableModuleGlobal
->MaxVolatileVariableSize
-
3013 GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
3018 // Point to the starting address of the variables.
3020 Variable
= GetStartPointer (VariableStoreHeader
);
3023 // Now walk through the related variable store.
3025 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
3026 NextVariable
= GetNextVariablePtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
3027 VariableSize
= (UINT64
)(UINTN
)NextVariable
- (UINT64
)(UINTN
)Variable
;
3031 // We don't take the state of the variables in mind
3032 // when calculating RemainingVariableStorageSize,
3033 // since the space occupied by variables not marked with
3034 // VAR_ADDED is not allowed to be reclaimed in Runtime.
3036 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3037 HwErrVariableTotalSize
+= VariableSize
;
3039 CommonVariableTotalSize
+= VariableSize
;
3043 // Only care about Variables with State VAR_ADDED, because
3044 // the space not marked as VAR_ADDED is reclaimable now.
3046 if (Variable
->State
== VAR_ADDED
) {
3047 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3048 HwErrVariableTotalSize
+= VariableSize
;
3050 CommonVariableTotalSize
+= VariableSize
;
3052 } else if (Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
3054 // If it is a IN_DELETED_TRANSITION variable,
3055 // and there is not also a same ADDED one at the same time,
3056 // this IN_DELETED_TRANSITION variable is valid.
3058 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
3059 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
3060 Status
= FindVariableEx (
3061 GetVariableNamePtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
3062 GetVendorGuidPtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
3065 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
3067 if (!EFI_ERROR (Status
) && (VariablePtrTrack
.CurrPtr
->State
!= VAR_ADDED
)) {
3068 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3069 HwErrVariableTotalSize
+= VariableSize
;
3071 CommonVariableTotalSize
+= VariableSize
;
3078 // Go to the next one.
3080 Variable
= NextVariable
;
3083 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3084 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
3086 if (*MaximumVariableStorageSize
< CommonVariableTotalSize
) {
3087 *RemainingVariableStorageSize
= 0;
3089 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
3093 if (*RemainingVariableStorageSize
< GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
)) {
3094 *MaximumVariableSize
= 0;
3095 } else if ((*RemainingVariableStorageSize
- GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
)) <
3096 *MaximumVariableSize
3099 *MaximumVariableSize
= *RemainingVariableStorageSize
-
3100 GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
3108 This code returns information about the EFI variables.
3110 Caution: This function may receive untrusted input.
3111 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
3113 @param Attributes Attributes bitmask to specify the type of variables
3114 on which to return information.
3115 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
3116 for the EFI variables associated with the attributes specified.
3117 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
3118 for EFI variables associated with the attributes specified.
3119 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
3120 associated with the attributes specified.
3122 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
3123 @return EFI_SUCCESS Query successfully.
3124 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
3129 VariableServiceQueryVariableInfo (
3130 IN UINT32 Attributes
,
3131 OUT UINT64
*MaximumVariableStorageSize
,
3132 OUT UINT64
*RemainingVariableStorageSize
,
3133 OUT UINT64
*MaximumVariableSize
3138 if ((MaximumVariableStorageSize
== NULL
) || (RemainingVariableStorageSize
== NULL
) || (MaximumVariableSize
== NULL
) || (Attributes
== 0)) {
3139 return EFI_INVALID_PARAMETER
;
3142 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
3144 // Deprecated attribute, make this check as highest priority.
3146 return EFI_UNSUPPORTED
;
3149 if ((Attributes
& EFI_VARIABLE_ATTRIBUTES_MASK
) == 0) {
3151 // Make sure the Attributes combination is supported by the platform.
3153 return EFI_UNSUPPORTED
;
3154 } else if ((Attributes
& EFI_VARIABLE_ATTRIBUTES_MASK
) == EFI_VARIABLE_NON_VOLATILE
) {
3156 // Only EFI_VARIABLE_NON_VOLATILE attribute is invalid
3158 return EFI_INVALID_PARAMETER
;
3159 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
3161 // Make sure if runtime bit is set, boot service bit is set also.
3163 return EFI_INVALID_PARAMETER
;
3164 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
3166 // Make sure RT Attribute is set if we are in Runtime phase.
3168 return EFI_INVALID_PARAMETER
;
3169 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3171 // Make sure Hw Attribute is set with NV.
3173 return EFI_INVALID_PARAMETER
;
3174 } else if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
3175 if (!mVariableModuleGlobal
->VariableGlobal
.AuthSupport
) {
3177 // Not support authenticated variable write.
3179 return EFI_UNSUPPORTED
;
3181 } else if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
3182 if (PcdGet32 (PcdHwErrStorageSize
) == 0) {
3184 // Not support harware error record variable variable.
3186 return EFI_UNSUPPORTED
;
3190 AcquireLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3192 Status
= VariableServiceQueryVariableInfoInternal (
3194 MaximumVariableStorageSize
,
3195 RemainingVariableStorageSize
,
3199 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3204 This function reclaims variable storage if free size is below the threshold.
3206 Caution: This function may be invoked at SMM mode.
3207 Care must be taken to make sure not security issue.
3216 UINTN RemainingCommonRuntimeVariableSpace
;
3217 UINTN RemainingHwErrVariableSpace
;
3218 STATIC BOOLEAN Reclaimed
;
3221 // This function will be called only once at EndOfDxe or ReadyToBoot event.
3229 Status
= EFI_SUCCESS
;
3231 if (mVariableModuleGlobal
->CommonRuntimeVariableSpace
< mVariableModuleGlobal
->CommonVariableTotalSize
) {
3232 RemainingCommonRuntimeVariableSpace
= 0;
3234 RemainingCommonRuntimeVariableSpace
= mVariableModuleGlobal
->CommonRuntimeVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
3237 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
3240 // Check if the free area is below a threshold.
3242 if (((RemainingCommonRuntimeVariableSpace
< mVariableModuleGlobal
->MaxVariableSize
) ||
3243 (RemainingCommonRuntimeVariableSpace
< mVariableModuleGlobal
->MaxAuthVariableSize
)) ||
3244 ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
3245 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
))))
3248 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3249 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3255 ASSERT_EFI_ERROR (Status
);
3260 Get maximum variable size, covering both non-volatile and volatile variables.
3262 @return Maximum variable size.
3266 GetMaxVariableSize (
3270 UINTN MaxVariableSize
;
3272 MaxVariableSize
= GetNonVolatileMaxVariableSize ();
3274 // The condition below fails implicitly if PcdMaxVolatileVariableSize equals
3275 // the default zero value.
3277 if (MaxVariableSize
< PcdGet32 (PcdMaxVolatileVariableSize
)) {
3278 MaxVariableSize
= PcdGet32 (PcdMaxVolatileVariableSize
);
3281 return MaxVariableSize
;
3285 Flush the HOB variable to flash.
3287 @param[in] VariableName Name of variable has been updated or deleted.
3288 @param[in] VendorGuid Guid of variable has been updated or deleted.
3292 FlushHobVariableToFlash (
3293 IN CHAR16
*VariableName
,
3294 IN EFI_GUID
*VendorGuid
3298 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3299 VARIABLE_HEADER
*Variable
;
3301 VARIABLE_POINTER_TRACK VariablePtrTrack
;
3306 AuthFormat
= mVariableModuleGlobal
->VariableGlobal
.AuthFormat
;
3309 // Flush the HOB variable to flash.
3311 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3312 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
3314 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
3316 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
3317 for ( Variable
= GetStartPointer (VariableStoreHeader
)
3318 ; IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))
3319 ; Variable
= GetNextVariablePtr (Variable
, AuthFormat
)
3322 if (Variable
->State
!= VAR_ADDED
) {
3324 // The HOB variable has been set to DELETED state in local.
3329 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
3330 if ((VendorGuid
== NULL
) || (VariableName
== NULL
) ||
3331 !CompareGuid (VendorGuid
, GetVendorGuidPtr (Variable
, AuthFormat
)) ||
3332 (StrCmp (VariableName
, GetVariableNamePtr (Variable
, AuthFormat
)) != 0))
3334 VariableData
= GetVariableDataPtr (Variable
, AuthFormat
);
3336 GetVariableNamePtr (Variable
, AuthFormat
),
3337 GetVendorGuidPtr (Variable
, AuthFormat
),
3339 &mVariableModuleGlobal
->VariableGlobal
,
3342 Status
= UpdateVariable (
3343 GetVariableNamePtr (Variable
, AuthFormat
),
3344 GetVendorGuidPtr (Variable
, AuthFormat
),
3346 DataSizeOfVariable (Variable
, AuthFormat
),
3347 Variable
->Attributes
,
3355 "Variable driver flush the HOB variable to flash: %g %s %r\n",
3356 GetVendorGuidPtr (Variable
, AuthFormat
),
3357 GetVariableNamePtr (Variable
, AuthFormat
),
3362 // The updated or deleted variable is matched with this HOB variable.
3363 // Don't break here because we will try to set other HOB variables
3364 // since this variable could be set successfully.
3366 Status
= EFI_SUCCESS
;
3369 if (!EFI_ERROR (Status
)) {
3371 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
3372 // set the HOB variable to DELETED state in local.
3376 "Variable driver set the HOB variable to DELETED state in local: %g %s\n",
3377 GetVendorGuidPtr (Variable
, AuthFormat
),
3378 GetVariableNamePtr (Variable
, AuthFormat
)
3380 Variable
->State
&= VAR_DELETED
;
3386 if (mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeHobCache
.Store
!= NULL
) {
3387 Status
= SynchronizeRuntimeVariableCache (
3388 &mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeHobCache
,
3390 mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeHobCache
.Store
->Size
3392 ASSERT_EFI_ERROR (Status
);
3397 // We still have HOB variable(s) not flushed in flash.
3399 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)VariableStoreHeader
;
3402 // All HOB variables have been flushed in flash.
3404 DEBUG ((DEBUG_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
3405 if (mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.HobFlushComplete
!= NULL
) {
3406 *(mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.HobFlushComplete
) = TRUE
;
3409 if (!AtRuntime ()) {
3410 FreePool ((VOID
*)VariableStoreHeader
);
3417 Initializes variable write service.
3419 @retval EFI_SUCCESS Function successfully executed.
3420 @retval Others Fail to initialize the variable service.
3424 VariableWriteServiceInitialize (
3431 VARIABLE_ENTRY_PROPERTY
*VariableEntry
;
3433 AcquireLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3436 // Check if the free area is really free.
3438 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< mNvVariableCache
->Size
; Index
++) {
3439 Data
= ((UINT8
*)mNvVariableCache
)[Index
];
3442 // There must be something wrong in variable store, do reclaim operation.
3445 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3446 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3452 if (EFI_ERROR (Status
)) {
3453 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3461 FlushHobVariableToFlash (NULL
, NULL
);
3463 Status
= EFI_SUCCESS
;
3464 ZeroMem (&mAuthContextOut
, sizeof (mAuthContextOut
));
3465 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
3467 // Authenticated variable initialize.
3469 mAuthContextIn
.StructSize
= sizeof (AUTH_VAR_LIB_CONTEXT_IN
);
3470 mAuthContextIn
.MaxAuthVariableSize
= mVariableModuleGlobal
->MaxAuthVariableSize
-
3471 GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
3472 Status
= AuthVariableLibInitialize (&mAuthContextIn
, &mAuthContextOut
);
3473 if (!EFI_ERROR (Status
)) {
3474 DEBUG ((DEBUG_INFO
, "Variable driver will work with auth variable support!\n"));
3475 mVariableModuleGlobal
->VariableGlobal
.AuthSupport
= TRUE
;
3476 if (mAuthContextOut
.AuthVarEntry
!= NULL
) {
3477 for (Index
= 0; Index
< mAuthContextOut
.AuthVarEntryCount
; Index
++) {
3478 VariableEntry
= &mAuthContextOut
.AuthVarEntry
[Index
];
3479 Status
= VarCheckLibVariablePropertySet (
3480 VariableEntry
->Name
,
3481 VariableEntry
->Guid
,
3482 &VariableEntry
->VariableProperty
3484 ASSERT_EFI_ERROR (Status
);
3487 } else if (Status
== EFI_UNSUPPORTED
) {
3488 DEBUG ((DEBUG_INFO
, "NOTICE - AuthVariableLibInitialize() returns %r!\n", Status
));
3489 DEBUG ((DEBUG_INFO
, "Variable driver will continue to work without auth variable support!\n"));
3490 mVariableModuleGlobal
->VariableGlobal
.AuthSupport
= FALSE
;
3491 Status
= EFI_SUCCESS
;
3495 if (!EFI_ERROR (Status
)) {
3496 for (Index
= 0; Index
< ARRAY_SIZE (mVariableEntryProperty
); Index
++) {
3497 VariableEntry
= &mVariableEntryProperty
[Index
];
3498 Status
= VarCheckLibVariablePropertySet (VariableEntry
->Name
, VariableEntry
->Guid
, &VariableEntry
->VariableProperty
);
3499 ASSERT_EFI_ERROR (Status
);
3503 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3506 // Initialize MOR Lock variable.
3514 Convert normal variable storage to the allocated auth variable storage.
3516 @param[in] NormalVarStorage Pointer to the normal variable storage header
3518 @retval the allocated auth variable storage
3521 ConvertNormalVarStorageToAuthVarStorage (
3522 VARIABLE_STORE_HEADER
*NormalVarStorage
3525 VARIABLE_HEADER
*StartPtr
;
3527 VARIABLE_HEADER
*EndPtr
;
3528 UINTN AuthVarStroageSize
;
3529 AUTHENTICATED_VARIABLE_HEADER
*AuthStartPtr
;
3530 VARIABLE_STORE_HEADER
*AuthVarStorage
;
3532 AuthVarStroageSize
= sizeof (VARIABLE_STORE_HEADER
);
3534 // Set AuthFormat as FALSE for normal variable storage
3536 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
= FALSE
;
3539 // Calculate Auth Variable Storage Size
3541 StartPtr
= GetStartPointer (NormalVarStorage
);
3542 EndPtr
= GetEndPointer (NormalVarStorage
);
3543 while (StartPtr
< EndPtr
) {
3544 if (StartPtr
->State
== VAR_ADDED
) {
3545 AuthVarStroageSize
= HEADER_ALIGN (AuthVarStroageSize
);
3546 AuthVarStroageSize
+= sizeof (AUTHENTICATED_VARIABLE_HEADER
);
3547 AuthVarStroageSize
+= StartPtr
->NameSize
+ GET_PAD_SIZE (StartPtr
->NameSize
);
3548 AuthVarStroageSize
+= StartPtr
->DataSize
+ GET_PAD_SIZE (StartPtr
->DataSize
);
3551 StartPtr
= GetNextVariablePtr (StartPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
3555 // Allocate Runtime memory for Auth Variable Storage
3557 AuthVarStorage
= AllocateRuntimeZeroPool (AuthVarStroageSize
);
3558 ASSERT (AuthVarStorage
!= NULL
);
3559 if (AuthVarStorage
== NULL
) {
3564 // Copy Variable from Normal storage to Auth storage
3566 StartPtr
= GetStartPointer (NormalVarStorage
);
3567 EndPtr
= GetEndPointer (NormalVarStorage
);
3568 AuthStartPtr
= (AUTHENTICATED_VARIABLE_HEADER
*)GetStartPointer (AuthVarStorage
);
3569 while (StartPtr
< EndPtr
) {
3570 if (StartPtr
->State
== VAR_ADDED
) {
3571 AuthStartPtr
= (AUTHENTICATED_VARIABLE_HEADER
*)HEADER_ALIGN (AuthStartPtr
);
3573 // Copy Variable Header
3575 AuthStartPtr
->StartId
= StartPtr
->StartId
;
3576 AuthStartPtr
->State
= StartPtr
->State
;
3577 AuthStartPtr
->Attributes
= StartPtr
->Attributes
;
3578 AuthStartPtr
->NameSize
= StartPtr
->NameSize
;
3579 AuthStartPtr
->DataSize
= StartPtr
->DataSize
;
3580 CopyGuid (&AuthStartPtr
->VendorGuid
, &StartPtr
->VendorGuid
);
3582 // Copy Variable Name
3584 NextPtr
= (UINT8
*)(AuthStartPtr
+ 1);
3587 GetVariableNamePtr (StartPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
3588 AuthStartPtr
->NameSize
3591 // Copy Variable Data
3593 NextPtr
= NextPtr
+ AuthStartPtr
->NameSize
+ GET_PAD_SIZE (AuthStartPtr
->NameSize
);
3594 CopyMem (NextPtr
, GetVariableDataPtr (StartPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
), AuthStartPtr
->DataSize
);
3596 // Go to next variable
3598 AuthStartPtr
= (AUTHENTICATED_VARIABLE_HEADER
*)(NextPtr
+ AuthStartPtr
->DataSize
+ GET_PAD_SIZE (AuthStartPtr
->DataSize
));
3601 StartPtr
= GetNextVariablePtr (StartPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
3605 // Update Auth Storage Header
3607 AuthVarStorage
->Format
= NormalVarStorage
->Format
;
3608 AuthVarStorage
->State
= NormalVarStorage
->State
;
3609 AuthVarStorage
->Size
= (UINT32
)((UINTN
)AuthStartPtr
- (UINTN
)AuthVarStorage
);
3610 CopyGuid (&AuthVarStorage
->Signature
, &gEfiAuthenticatedVariableGuid
);
3611 ASSERT (AuthVarStorage
->Size
<= AuthVarStroageSize
);
3614 // Restore AuthFormat
3616 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
= TRUE
;
3617 return AuthVarStorage
;
3621 Get HOB variable store.
3623 @param[in] VariableGuid NV variable store signature.
3625 @retval EFI_SUCCESS Function successfully executed.
3626 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3630 GetHobVariableStore (
3631 IN EFI_GUID
*VariableGuid
3634 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3635 UINT64 VariableStoreLength
;
3636 EFI_HOB_GUID_TYPE
*GuidHob
;
3637 BOOLEAN NeedConvertNormalToAuth
;
3640 // Make sure there is no more than one Variable HOB.
3642 DEBUG_CODE_BEGIN ();
3643 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
3644 if (GuidHob
!= NULL
) {
3645 if ((GetNextGuidHob (&gEfiAuthenticatedVariableGuid
, GET_NEXT_HOB (GuidHob
)) != NULL
)) {
3646 DEBUG ((DEBUG_ERROR
, "ERROR: Found two Auth Variable HOBs\n"));
3648 } else if (GetFirstGuidHob (&gEfiVariableGuid
) != NULL
) {
3649 DEBUG ((DEBUG_ERROR
, "ERROR: Found one Auth + one Normal Variable HOBs\n"));
3653 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
3654 if (GuidHob
!= NULL
) {
3655 if ((GetNextGuidHob (&gEfiVariableGuid
, GET_NEXT_HOB (GuidHob
)) != NULL
)) {
3656 DEBUG ((DEBUG_ERROR
, "ERROR: Found two Normal Variable HOBs\n"));
3665 // Combinations supported:
3666 // 1. Normal NV variable store +
3667 // Normal HOB variable store
3668 // 2. Auth NV variable store +
3669 // Auth HOB variable store
3670 // 3. Auth NV variable store +
3671 // Normal HOB variable store (code will convert it to Auth Format)
3673 NeedConvertNormalToAuth
= FALSE
;
3674 GuidHob
= GetFirstGuidHob (VariableGuid
);
3675 if ((GuidHob
== NULL
) && (VariableGuid
== &gEfiAuthenticatedVariableGuid
)) {
3677 // Try getting it from normal variable HOB
3679 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
3680 NeedConvertNormalToAuth
= TRUE
;
3683 if (GuidHob
!= NULL
) {
3684 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
3685 VariableStoreLength
= GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
);
3686 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
3687 if (!NeedConvertNormalToAuth
) {
3688 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocateRuntimeCopyPool ((UINTN
)VariableStoreLength
, (VOID
*)VariableStoreHeader
);
3690 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)ConvertNormalVarStorageToAuthVarStorage ((VOID
*)VariableStoreHeader
);
3693 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
3694 return EFI_OUT_OF_RESOURCES
;
3697 DEBUG ((DEBUG_ERROR
, "HOB Variable Store header is corrupted!\n"));
3705 Initializes variable store area for non-volatile and volatile variable.
3707 @retval EFI_SUCCESS Function successfully executed.
3708 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3712 VariableCommonInitialize (
3717 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
3719 EFI_GUID
*VariableGuid
;
3722 // Allocate runtime memory for variable driver global structure.
3724 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
3725 if (mVariableModuleGlobal
== NULL
) {
3726 return EFI_OUT_OF_RESOURCES
;
3729 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
3732 // Init non-volatile variable store.
3734 Status
= InitNonVolatileVariableStore ();
3735 if (EFI_ERROR (Status
)) {
3736 FreePool (mVariableModuleGlobal
);
3741 // mVariableModuleGlobal->VariableGlobal.AuthFormat
3742 // has been initialized in InitNonVolatileVariableStore().
3744 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
3745 DEBUG ((DEBUG_INFO
, "Variable driver will work with auth variable format!\n"));
3747 // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will initialize it.
3749 mVariableModuleGlobal
->VariableGlobal
.AuthSupport
= FALSE
;
3750 VariableGuid
= &gEfiAuthenticatedVariableGuid
;
3752 DEBUG ((DEBUG_INFO
, "Variable driver will work without auth variable support!\n"));
3753 mVariableModuleGlobal
->VariableGlobal
.AuthSupport
= FALSE
;
3754 VariableGuid
= &gEfiVariableGuid
;
3758 // Get HOB variable store.
3760 Status
= GetHobVariableStore (VariableGuid
);
3761 if (EFI_ERROR (Status
)) {
3762 if (mNvFvHeaderCache
!= NULL
) {
3763 FreePool (mNvFvHeaderCache
);
3766 FreePool (mVariableModuleGlobal
);
3770 mVariableModuleGlobal
->MaxVolatileVariableSize
= ((PcdGet32 (PcdMaxVolatileVariableSize
) != 0) ?
3771 PcdGet32 (PcdMaxVolatileVariableSize
) :
3772 mVariableModuleGlobal
->MaxVariableSize
3775 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
3777 ScratchSize
= GetMaxVariableSize ();
3778 mVariableModuleGlobal
->ScratchBufferSize
= ScratchSize
;
3779 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
3780 if (VolatileVariableStore
== NULL
) {
3781 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3782 FreePool ((VOID
*)(UINTN
)mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3785 if (mNvFvHeaderCache
!= NULL
) {
3786 FreePool (mNvFvHeaderCache
);
3789 FreePool (mVariableModuleGlobal
);
3790 return EFI_OUT_OF_RESOURCES
;
3793 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
3796 // Initialize Variable Specific Data.
3798 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)VolatileVariableStore
;
3799 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
)GetStartPointer (VolatileVariableStore
) - (UINTN
)VolatileVariableStore
;
3801 CopyGuid (&VolatileVariableStore
->Signature
, VariableGuid
);
3802 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
3803 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
3804 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
3805 VolatileVariableStore
->Reserved
= 0;
3806 VolatileVariableStore
->Reserved1
= 0;
3812 Get the proper fvb handle and/or fvb protocol by the given Flash address.
3814 @param[in] Address The Flash address.
3815 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
3816 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
3820 GetFvbInfoByAddress (
3821 IN EFI_PHYSICAL_ADDRESS Address
,
3822 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
3823 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
3827 EFI_HANDLE
*HandleBuffer
;
3830 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
3831 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
3832 EFI_FVB_ATTRIBUTES_2 Attributes
;
3834 UINTN NumberOfBlocks
;
3836 HandleBuffer
= NULL
;
3838 // Get all FVB handles.
3840 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
3841 if (EFI_ERROR (Status
)) {
3842 return EFI_NOT_FOUND
;
3846 // Get the FVB to access variable store.
3849 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
3850 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
3851 if (EFI_ERROR (Status
)) {
3852 Status
= EFI_NOT_FOUND
;
3857 // Ensure this FVB protocol supported Write operation.
3859 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
3860 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
3865 // Compare the address and select the right one.
3867 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
3868 if (EFI_ERROR (Status
)) {
3873 // Assume one FVB has one type of BlockSize.
3875 Status
= Fvb
->GetBlockSize (Fvb
, 0, &BlockSize
, &NumberOfBlocks
);
3876 if (EFI_ERROR (Status
)) {
3880 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ BlockSize
* NumberOfBlocks
))) {
3881 if (FvbHandle
!= NULL
) {
3882 *FvbHandle
= HandleBuffer
[Index
];
3885 if (FvbProtocol
!= NULL
) {
3889 Status
= EFI_SUCCESS
;
3894 FreePool (HandleBuffer
);
3897 Status
= EFI_NOT_FOUND
;