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>
23 SPDX-License-Identifier: BSD-2-Clause-Patent
28 #include "VariableNonVolatile.h"
29 #include "VariableParsing.h"
30 #include "VariableRuntimeCache.h"
32 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
35 /// Define a memory cache that improves the search performance for a variable.
36 /// For EmuNvMode == TRUE, it will be equal to NonVolatileVariableBase.
38 VARIABLE_STORE_HEADER
*mNvVariableCache
= NULL
;
41 /// Memory cache of Fv Header.
43 EFI_FIRMWARE_VOLUME_HEADER
*mNvFvHeaderCache
= NULL
;
46 /// The memory entry used for variable statistics data.
48 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
51 /// The flag to indicate whether the platform has left the DXE phase of execution.
53 BOOLEAN mEndOfDxe
= FALSE
;
56 /// It indicates the var check request source.
57 /// In the implementation, DXE is regarded as untrusted, and SMM is trusted.
59 VAR_CHECK_REQUEST_SOURCE mRequestSource
= VarCheckFromUntrusted
;
62 // It will record the current boot error flag before EndOfDxe.
64 VAR_ERROR_FLAG mCurrentBootVarErrFlag
= VAR_ERROR_FLAG_NO_ERROR
;
66 VARIABLE_ENTRY_PROPERTY mVariableEntryProperty
[] = {
68 &gEdkiiVarErrorFlagGuid
,
71 VAR_CHECK_VARIABLE_PROPERTY_REVISION
,
72 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
,
73 VARIABLE_ATTRIBUTE_NV_BS_RT
,
74 sizeof (VAR_ERROR_FLAG
),
75 sizeof (VAR_ERROR_FLAG
)
80 AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn
= {
81 AUTH_VAR_LIB_CONTEXT_IN_STRUCT_VERSION
,
83 // StructSize, TO BE FILLED
87 // MaxAuthVariableSize, TO BE FILLED
90 VariableExLibFindVariable
,
91 VariableExLibFindNextVariable
,
92 VariableExLibUpdateVariable
,
93 VariableExLibGetScratchBuffer
,
94 VariableExLibCheckRemainingSpaceForConsistency
,
95 VariableExLibAtRuntime
,
98 AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut
;
102 This function writes data to the FWH at the correct LBA even if the LBAs
105 @param Global Pointer to VARAIBLE_GLOBAL structure.
106 @param Volatile Point out the Variable is Volatile or Non-Volatile.
107 @param SetByIndex TRUE if target pointer is given as index.
108 FALSE if target pointer is absolute.
109 @param Fvb Pointer to the writable FVB protocol.
110 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
112 @param DataSize Size of data to be written.
113 @param Buffer Pointer to the buffer from which data is written.
115 @retval EFI_INVALID_PARAMETER Parameters not valid.
116 @retval EFI_UNSUPPORTED Fvb is a NULL for Non-Volatile variable update.
117 @retval EFI_OUT_OF_RESOURCES The remaining size is not enough.
118 @retval EFI_SUCCESS Variable store successfully updated.
122 UpdateVariableStore (
123 IN VARIABLE_GLOBAL
*Global
,
125 IN BOOLEAN SetByIndex
,
126 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
127 IN UINTN DataPtrIndex
,
132 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
140 VARIABLE_STORE_HEADER
*VolatileBase
;
141 EFI_PHYSICAL_ADDRESS FvVolHdr
;
142 EFI_PHYSICAL_ADDRESS DataPtr
;
146 DataPtr
= DataPtrIndex
;
149 // Check if the Data is Volatile.
151 if (!Volatile
&& !mVariableModuleGlobal
->VariableGlobal
.EmuNvMode
) {
153 return EFI_UNSUPPORTED
;
155 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
156 ASSERT_EFI_ERROR (Status
);
159 // Data Pointer should point to the actual Address where data is to be
163 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
166 if ((DataPtr
+ DataSize
) > (FvVolHdr
+ mNvFvHeaderCache
->FvLength
)) {
167 return EFI_OUT_OF_RESOURCES
;
171 // Data Pointer should point to the actual Address where data is to be
175 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
177 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
180 if ((DataPtr
+ DataSize
) > ((UINTN
) VolatileBase
+ VolatileBase
->Size
)) {
181 return EFI_OUT_OF_RESOURCES
;
185 // Emulated non-volatile variable mode.
188 DataPtr
+= (UINTN
) mNvVariableCache
;
191 if ((DataPtr
+ DataSize
) > ((UINTN
) mNvVariableCache
+ mNvVariableCache
->Size
)) {
192 return EFI_OUT_OF_RESOURCES
;
197 // If Volatile/Emulated Non-volatile Variable just do a simple mem copy.
199 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
204 // If we are here we are dealing with Non-Volatile Variables.
206 LinearOffset
= (UINTN
) FvVolHdr
;
207 CurrWritePtr
= (UINTN
) DataPtr
;
208 CurrWriteSize
= DataSize
;
212 if (CurrWritePtr
< LinearOffset
) {
213 return EFI_INVALID_PARAMETER
;
216 for (PtrBlockMapEntry
= mNvFvHeaderCache
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
217 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
219 // Check to see if the Variable Writes are spanning through multiple
222 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
223 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
224 Status
= Fvb
->Write (
227 (UINTN
) (CurrWritePtr
- LinearOffset
),
233 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
234 Status
= Fvb
->Write (
237 (UINTN
) (CurrWritePtr
- LinearOffset
),
241 if (EFI_ERROR (Status
)) {
245 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
246 CurrBuffer
= CurrBuffer
+ Size
;
247 CurrWriteSize
= CurrWriteSize
- Size
;
251 LinearOffset
+= PtrBlockMapEntry
->Length
;
260 Record variable error flag.
262 @param[in] Flag Variable error flag to record.
263 @param[in] VariableName Name of variable.
264 @param[in] VendorGuid Guid of variable.
265 @param[in] Attributes Attributes of the variable.
266 @param[in] VariableSize Size of the variable.
271 IN VAR_ERROR_FLAG Flag
,
272 IN CHAR16
*VariableName
,
273 IN EFI_GUID
*VendorGuid
,
274 IN UINT32 Attributes
,
275 IN UINTN VariableSize
279 VARIABLE_POINTER_TRACK Variable
;
280 VAR_ERROR_FLAG
*VarErrFlag
;
281 VAR_ERROR_FLAG TempFlag
;
284 DEBUG ((DEBUG_ERROR
, "RecordVarErrorFlag (0x%02x) %s:%g - 0x%08x - 0x%x\n", Flag
, VariableName
, VendorGuid
, Attributes
, VariableSize
));
285 if (Flag
== VAR_ERROR_FLAG_SYSTEM_ERROR
) {
287 DEBUG ((DEBUG_ERROR
, "CommonRuntimeVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal
->CommonRuntimeVariableSpace
, mVariableModuleGlobal
->CommonVariableTotalSize
));
289 DEBUG ((DEBUG_ERROR
, "CommonVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal
->CommonVariableSpace
, mVariableModuleGlobal
->CommonVariableTotalSize
));
292 DEBUG ((DEBUG_ERROR
, "CommonMaxUserVariableSpace = 0x%x - CommonUserVariableTotalSize = 0x%x\n", mVariableModuleGlobal
->CommonMaxUserVariableSpace
, mVariableModuleGlobal
->CommonUserVariableTotalSize
));
298 // Before EndOfDxe, just record the current boot variable error flag to local variable,
299 // and leave the variable error flag in NV flash as the last boot variable error flag.
300 // After EndOfDxe in InitializeVarErrorFlag (), the variable error flag in NV flash
301 // will be initialized to this local current boot variable error flag.
303 mCurrentBootVarErrFlag
&= Flag
;
308 // Record error flag (it should have be initialized).
310 Status
= FindVariable (
312 &gEdkiiVarErrorFlagGuid
,
314 &mVariableModuleGlobal
->VariableGlobal
,
317 if (!EFI_ERROR (Status
)) {
318 VarErrFlag
= (VAR_ERROR_FLAG
*) GetVariableDataPtr (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
319 TempFlag
= *VarErrFlag
;
321 if (TempFlag
== *VarErrFlag
) {
324 Status
= UpdateVariableStore (
325 &mVariableModuleGlobal
->VariableGlobal
,
328 mVariableModuleGlobal
->FvbInstance
,
329 (UINTN
) VarErrFlag
- (UINTN
) mNvVariableCache
+ (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
333 if (!EFI_ERROR (Status
)) {
335 // Update the data in NV cache.
337 *VarErrFlag
= TempFlag
;
338 Status
= SynchronizeRuntimeVariableCache (
339 &mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeNvCache
,
341 mNvVariableCache
->Size
343 ASSERT_EFI_ERROR (Status
);
349 Initialize variable error flag.
351 Before EndOfDxe, the variable indicates the last boot variable error flag,
352 then it means the last boot variable error flag must be got before EndOfDxe.
353 After EndOfDxe, the variable indicates the current boot variable error flag,
354 then it means the current boot variable error flag must be got after EndOfDxe.
358 InitializeVarErrorFlag (
363 VARIABLE_POINTER_TRACK Variable
;
365 VAR_ERROR_FLAG VarErrFlag
;
371 Flag
= mCurrentBootVarErrFlag
;
372 DEBUG ((DEBUG_INFO
, "Initialize variable error flag (%02x)\n", Flag
));
374 Status
= FindVariable (
376 &gEdkiiVarErrorFlagGuid
,
378 &mVariableModuleGlobal
->VariableGlobal
,
381 if (!EFI_ERROR (Status
)) {
382 VarErrFlag
= *((VAR_ERROR_FLAG
*) GetVariableDataPtr (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
));
383 if (VarErrFlag
== Flag
) {
390 &gEdkiiVarErrorFlagGuid
,
393 VARIABLE_ATTRIBUTE_NV_BS_RT
,
404 @param[in] Variable Pointer to variable header.
406 @retval TRUE User variable.
407 @retval FALSE System variable.
412 IN VARIABLE_HEADER
*Variable
415 VAR_CHECK_VARIABLE_PROPERTY Property
;
418 // Only after End Of Dxe, the variables belong to system variable are fixed.
419 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,
420 // then no need to check if the variable is user variable or not specially.
422 if (mEndOfDxe
&& (mVariableModuleGlobal
->CommonMaxUserVariableSpace
!= mVariableModuleGlobal
->CommonVariableSpace
)) {
423 if (VarCheckLibVariablePropertyGet (
424 GetVariableNamePtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
425 GetVendorGuidPtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
427 ) == EFI_NOT_FOUND
) {
435 Calculate common user variable total size.
439 CalculateCommonUserVariableTotalSize (
443 VARIABLE_HEADER
*Variable
;
444 VARIABLE_HEADER
*NextVariable
;
446 VAR_CHECK_VARIABLE_PROPERTY Property
;
449 // Only after End Of Dxe, the variables belong to system variable are fixed.
450 // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,
451 // then no need to calculate the common user variable total size specially.
453 if (mEndOfDxe
&& (mVariableModuleGlobal
->CommonMaxUserVariableSpace
!= mVariableModuleGlobal
->CommonVariableSpace
)) {
454 Variable
= GetStartPointer (mNvVariableCache
);
455 while (IsValidVariableHeader (Variable
, GetEndPointer (mNvVariableCache
))) {
456 NextVariable
= GetNextVariablePtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
457 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
458 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
459 if (VarCheckLibVariablePropertyGet (
460 GetVariableNamePtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
461 GetVendorGuidPtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
463 ) == EFI_NOT_FOUND
) {
465 // No property, it is user variable.
467 mVariableModuleGlobal
->CommonUserVariableTotalSize
+= VariableSize
;
471 Variable
= NextVariable
;
477 Initialize variable quota.
481 InitializeVariableQuota (
489 InitializeVarErrorFlag ();
490 CalculateCommonUserVariableTotalSize ();
495 Variable store garbage collection and reclaim operation.
497 @param[in] VariableBase Base address of variable store.
498 @param[out] LastVariableOffset Offset of last variable.
499 @param[in] IsVolatile The variable store is volatile or not;
500 if it is non-volatile, need FTW.
501 @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.
502 @param[in] NewVariable Pointer to new variable.
503 @param[in] NewVariableSize New variable size.
505 @return EFI_SUCCESS Reclaim operation has finished successfully.
506 @return EFI_OUT_OF_RESOURCES No enough memory resources or variable space.
507 @return Others Unexpect error happened during reclaim operation.
512 IN EFI_PHYSICAL_ADDRESS VariableBase
,
513 OUT UINTN
*LastVariableOffset
,
514 IN BOOLEAN IsVolatile
,
515 IN OUT VARIABLE_POINTER_TRACK
*UpdatingPtrTrack
,
516 IN VARIABLE_HEADER
*NewVariable
,
517 IN UINTN NewVariableSize
520 VARIABLE_HEADER
*Variable
;
521 VARIABLE_HEADER
*AddedVariable
;
522 VARIABLE_HEADER
*NextVariable
;
523 VARIABLE_HEADER
*NextAddedVariable
;
524 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
526 UINTN MaximumBufferSize
;
534 EFI_STATUS DoneStatus
;
535 UINTN CommonVariableTotalSize
;
536 UINTN CommonUserVariableTotalSize
;
537 UINTN HwErrVariableTotalSize
;
538 VARIABLE_HEADER
*UpdatingVariable
;
539 VARIABLE_HEADER
*UpdatingInDeletedTransition
;
542 AuthFormat
= mVariableModuleGlobal
->VariableGlobal
.AuthFormat
;
543 UpdatingVariable
= NULL
;
544 UpdatingInDeletedTransition
= NULL
;
545 if (UpdatingPtrTrack
!= NULL
) {
546 UpdatingVariable
= UpdatingPtrTrack
->CurrPtr
;
547 UpdatingInDeletedTransition
= UpdatingPtrTrack
->InDeletedTransitionPtr
;
550 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
552 CommonVariableTotalSize
= 0;
553 CommonUserVariableTotalSize
= 0;
554 HwErrVariableTotalSize
= 0;
556 if (IsVolatile
|| mVariableModuleGlobal
->VariableGlobal
.EmuNvMode
) {
558 // Start Pointers for the variable.
560 Variable
= GetStartPointer (VariableStoreHeader
);
561 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
563 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
564 NextVariable
= GetNextVariablePtr (Variable
, AuthFormat
);
565 if ((Variable
->State
== VAR_ADDED
|| Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) &&
566 Variable
!= UpdatingVariable
&&
567 Variable
!= UpdatingInDeletedTransition
569 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
570 MaximumBufferSize
+= VariableSize
;
573 Variable
= NextVariable
;
576 if (NewVariable
!= NULL
) {
578 // Add the new variable size.
580 MaximumBufferSize
+= NewVariableSize
;
584 // Reserve the 1 Bytes with Oxff to identify the
585 // end of the variable buffer.
587 MaximumBufferSize
+= 1;
588 ValidBuffer
= AllocatePool (MaximumBufferSize
);
589 if (ValidBuffer
== NULL
) {
590 return EFI_OUT_OF_RESOURCES
;
594 // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache
595 // as the buffer to reduce SMRAM consumption for SMM variable driver.
597 MaximumBufferSize
= mNvVariableCache
->Size
;
598 ValidBuffer
= (UINT8
*) mNvVariableCache
;
601 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
604 // Copy variable store header.
606 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
607 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
610 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
612 Variable
= GetStartPointer (VariableStoreHeader
);
613 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
614 NextVariable
= GetNextVariablePtr (Variable
, AuthFormat
);
615 if (Variable
!= UpdatingVariable
&& Variable
->State
== VAR_ADDED
) {
616 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
617 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
618 CurrPtr
+= VariableSize
;
619 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
620 HwErrVariableTotalSize
+= VariableSize
;
621 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
622 CommonVariableTotalSize
+= VariableSize
;
623 if (IsUserVariable (Variable
)) {
624 CommonUserVariableTotalSize
+= VariableSize
;
628 Variable
= NextVariable
;
632 // Reinstall all in delete transition variables.
634 Variable
= GetStartPointer (VariableStoreHeader
);
635 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
636 NextVariable
= GetNextVariablePtr (Variable
, AuthFormat
);
637 if (Variable
!= UpdatingVariable
&& Variable
!= UpdatingInDeletedTransition
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
640 // Buffer has cached all ADDED variable.
641 // Per IN_DELETED variable, we have to guarantee that
642 // no ADDED one in previous buffer.
646 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
647 while (IsValidVariableHeader (AddedVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
))) {
648 NextAddedVariable
= GetNextVariablePtr (AddedVariable
, AuthFormat
);
649 NameSize
= NameSizeOfVariable (AddedVariable
, AuthFormat
);
651 GetVendorGuidPtr (AddedVariable
, AuthFormat
),
652 GetVendorGuidPtr (Variable
, AuthFormat
)
653 ) && NameSize
== NameSizeOfVariable (Variable
, AuthFormat
)) {
654 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
, AuthFormat
);
655 Point1
= (VOID
*) GetVariableNamePtr (Variable
, AuthFormat
);
656 if (CompareMem (Point0
, Point1
, NameSize
) == 0) {
661 AddedVariable
= NextAddedVariable
;
665 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
667 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
668 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
669 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
670 CurrPtr
+= VariableSize
;
671 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
672 HwErrVariableTotalSize
+= VariableSize
;
673 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
674 CommonVariableTotalSize
+= VariableSize
;
675 if (IsUserVariable (Variable
)) {
676 CommonUserVariableTotalSize
+= VariableSize
;
682 Variable
= NextVariable
;
686 // Install the new variable if it is not NULL.
688 if (NewVariable
!= NULL
) {
689 if (((UINTN
) CurrPtr
- (UINTN
) ValidBuffer
) + NewVariableSize
> VariableStoreHeader
->Size
) {
691 // No enough space to store the new variable.
693 Status
= EFI_OUT_OF_RESOURCES
;
697 if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
698 HwErrVariableTotalSize
+= NewVariableSize
;
699 } else if ((NewVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
700 CommonVariableTotalSize
+= NewVariableSize
;
701 if (IsUserVariable (NewVariable
)) {
702 CommonUserVariableTotalSize
+= NewVariableSize
;
705 if ((HwErrVariableTotalSize
> PcdGet32 (PcdHwErrStorageSize
)) ||
706 (CommonVariableTotalSize
> mVariableModuleGlobal
->CommonVariableSpace
) ||
707 (CommonUserVariableTotalSize
> mVariableModuleGlobal
->CommonMaxUserVariableSpace
)) {
709 // No enough space to store the new variable by NV or NV+HR attribute.
711 Status
= EFI_OUT_OF_RESOURCES
;
716 CopyMem (CurrPtr
, (UINT8
*) NewVariable
, NewVariableSize
);
717 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
718 if (UpdatingVariable
!= NULL
) {
719 UpdatingPtrTrack
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)UpdatingPtrTrack
->StartPtr
+ ((UINTN
)CurrPtr
- (UINTN
)GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
)));
720 UpdatingPtrTrack
->InDeletedTransitionPtr
= NULL
;
722 CurrPtr
+= NewVariableSize
;
725 if (IsVolatile
|| mVariableModuleGlobal
->VariableGlobal
.EmuNvMode
) {
727 // If volatile/emulated non-volatile variable store, just copy valid buffer.
729 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
730 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) CurrPtr
- (UINTN
) ValidBuffer
);
731 *LastVariableOffset
= (UINTN
) CurrPtr
- (UINTN
) ValidBuffer
;
734 // Emulated non-volatile variable mode.
736 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
737 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
738 mVariableModuleGlobal
->CommonUserVariableTotalSize
= CommonUserVariableTotalSize
;
740 Status
= EFI_SUCCESS
;
743 // If non-volatile variable store, perform FTW here.
745 Status
= FtwVariableSpace (
747 (VARIABLE_STORE_HEADER
*) ValidBuffer
749 if (!EFI_ERROR (Status
)) {
750 *LastVariableOffset
= (UINTN
) CurrPtr
- (UINTN
) ValidBuffer
;
751 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
752 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
753 mVariableModuleGlobal
->CommonUserVariableTotalSize
= CommonUserVariableTotalSize
;
755 mVariableModuleGlobal
->HwErrVariableTotalSize
= 0;
756 mVariableModuleGlobal
->CommonVariableTotalSize
= 0;
757 mVariableModuleGlobal
->CommonUserVariableTotalSize
= 0;
758 Variable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
759 while (IsValidVariableHeader (Variable
, GetEndPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
))) {
760 NextVariable
= GetNextVariablePtr (Variable
, AuthFormat
);
761 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
762 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
763 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
764 } else if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
765 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
766 if (IsUserVariable (Variable
)) {
767 mVariableModuleGlobal
->CommonUserVariableTotalSize
+= VariableSize
;
771 Variable
= NextVariable
;
773 *LastVariableOffset
= (UINTN
) Variable
- (UINTN
) VariableBase
;
778 DoneStatus
= EFI_SUCCESS
;
779 if (IsVolatile
|| mVariableModuleGlobal
->VariableGlobal
.EmuNvMode
) {
780 DoneStatus
= SynchronizeRuntimeVariableCache (
781 &mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeVolatileCache
,
783 VariableStoreHeader
->Size
785 ASSERT_EFI_ERROR (DoneStatus
);
786 FreePool (ValidBuffer
);
789 // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.
791 CopyMem (mNvVariableCache
, (UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
);
792 DoneStatus
= SynchronizeRuntimeVariableCache (
793 &mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeNvCache
,
795 VariableStoreHeader
->Size
797 ASSERT_EFI_ERROR (DoneStatus
);
800 if (!EFI_ERROR (Status
) && EFI_ERROR (DoneStatus
)) {
808 Finds variable in storage blocks of volatile and non-volatile storage areas.
810 This code finds variable in storage blocks of volatile and non-volatile storage areas.
811 If VariableName is an empty string, then we just return the first
812 qualified variable without comparing VariableName and VendorGuid.
813 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
814 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
815 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
817 @param[in] VariableName Name of the variable to be found.
818 @param[in] VendorGuid Vendor GUID to be found.
819 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
820 including the range searched and the target position.
821 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
822 base of volatile variable storage area, base of
823 NV variable storage area, and a lock.
824 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
825 check at runtime when searching variable.
827 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
829 @retval EFI_SUCCESS Variable successfully found.
830 @retval EFI_NOT_FOUND Variable not found
835 IN CHAR16
*VariableName
,
836 IN EFI_GUID
*VendorGuid
,
837 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
838 IN VARIABLE_GLOBAL
*Global
,
839 IN BOOLEAN IgnoreRtCheck
843 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
844 VARIABLE_STORE_TYPE Type
;
846 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
847 return EFI_INVALID_PARAMETER
;
851 // 0: Volatile, 1: HOB, 2: Non-Volatile.
852 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
853 // make use of this mapping to implement search algorithm.
855 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
856 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
857 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
860 // Find the variable by walk through HOB, volatile and non-volatile variable store.
862 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
863 if (VariableStoreHeader
[Type
] == NULL
) {
867 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
868 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
869 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
871 Status
= FindVariableEx (
876 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
878 if (!EFI_ERROR (Status
)) {
882 return EFI_NOT_FOUND
;
886 Get index from supported language codes according to language string.
888 This code is used to get corresponding index in supported language codes. It can handle
889 RFC4646 and ISO639 language tags.
890 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
891 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
894 SupportedLang = "engfraengfra"
896 Iso639Language = TRUE
897 The return value is "0".
899 SupportedLang = "en;fr;en-US;fr-FR"
901 Iso639Language = FALSE
902 The return value is "3".
904 @param SupportedLang Platform supported language codes.
905 @param Lang Configured language.
906 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
908 @retval The index of language in the language codes.
912 GetIndexFromSupportedLangCodes(
913 IN CHAR8
*SupportedLang
,
915 IN BOOLEAN Iso639Language
920 UINTN LanguageLength
;
922 if (Iso639Language
) {
923 CompareLength
= ISO_639_2_ENTRY_SIZE
;
924 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
925 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
927 // Successfully find the index of Lang string in SupportedLang string.
929 Index
= Index
/ CompareLength
;
937 // Compare RFC4646 language code
940 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
942 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
944 // Skip ';' characters in SupportedLang
946 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
948 // Determine the length of the next language code in SupportedLang
950 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
952 if ((CompareLength
== LanguageLength
) &&
953 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
955 // Successfully find the index of Lang string in SupportedLang string.
966 Get language string from supported language codes according to index.
968 This code is used to get corresponding language strings in supported language codes. It can handle
969 RFC4646 and ISO639 language tags.
970 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
971 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
974 SupportedLang = "engfraengfra"
976 Iso639Language = TRUE
977 The return value is "fra".
979 SupportedLang = "en;fr;en-US;fr-FR"
981 Iso639Language = FALSE
982 The return value is "fr".
984 @param SupportedLang Platform supported language codes.
985 @param Index The index in supported language codes.
986 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
988 @retval The language string in the language codes.
992 GetLangFromSupportedLangCodes (
993 IN CHAR8
*SupportedLang
,
995 IN BOOLEAN Iso639Language
1003 Supported
= SupportedLang
;
1004 if (Iso639Language
) {
1006 // According to the index of Lang string in SupportedLang string to get the language.
1007 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1008 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1010 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1011 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1012 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1017 // Take semicolon as delimitation, sequentially traverse supported language codes.
1019 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1022 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1024 // Have completed the traverse, but not find corrsponding string.
1025 // This case is not allowed to happen.
1030 if (SubIndex
== Index
) {
1032 // According to the index of Lang string in SupportedLang string to get the language.
1033 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1034 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1036 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1037 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1042 // Skip ';' characters in Supported
1044 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1050 Returns a pointer to an allocated buffer that contains the best matching language
1051 from a set of supported languages.
1053 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1054 code types may not be mixed in a single call to this function. This function
1055 supports a variable argument list that allows the caller to pass in a prioritized
1056 list of language codes to test against all the language codes in SupportedLanguages.
1058 If SupportedLanguages is NULL, then ASSERT().
1060 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1061 contains a set of language codes in the format
1062 specified by Iso639Language.
1063 @param[in] Iso639Language If not zero, then all language codes are assumed to be
1064 in ISO 639-2 format. If zero, then all language
1065 codes are assumed to be in RFC 4646 language format
1066 @param[in] ... A variable argument list that contains pointers to
1067 Null-terminated ASCII strings that contain one or more
1068 language codes in the format specified by Iso639Language.
1069 The first language code from each of these language
1070 code lists is used to determine if it is an exact or
1071 close match to any of the language codes in
1072 SupportedLanguages. Close matches only apply to RFC 4646
1073 language codes, and the matching algorithm from RFC 4647
1074 is used to determine if a close match is present. If
1075 an exact or close match is found, then the matching
1076 language code from SupportedLanguages is returned. If
1077 no matches are found, then the next variable argument
1078 parameter is evaluated. The variable argument list
1079 is terminated by a NULL.
1081 @retval NULL The best matching language could not be found in SupportedLanguages.
1082 @retval NULL There are not enough resources available to return the best matching
1084 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1085 language in SupportedLanguages.
1090 VariableGetBestLanguage (
1091 IN CONST CHAR8
*SupportedLanguages
,
1092 IN UINTN Iso639Language
,
1098 UINTN CompareLength
;
1099 UINTN LanguageLength
;
1100 CONST CHAR8
*Supported
;
1103 if (SupportedLanguages
== NULL
) {
1107 VA_START (Args
, Iso639Language
);
1108 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1110 // Default to ISO 639-2 mode
1113 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1116 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1118 if (Iso639Language
== 0) {
1119 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1123 // Trim back the length of Language used until it is empty
1125 while (LanguageLength
> 0) {
1127 // Loop through all language codes in SupportedLanguages
1129 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1131 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1133 if (Iso639Language
== 0) {
1135 // Skip ';' characters in Supported
1137 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1139 // Determine the length of the next language code in Supported
1141 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1143 // If Language is longer than the Supported, then skip to the next language
1145 if (LanguageLength
> CompareLength
) {
1150 // See if the first LanguageLength characters in Supported match Language
1152 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1155 Buffer
= (Iso639Language
!= 0) ? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1156 Buffer
[CompareLength
] = '\0';
1157 return CopyMem (Buffer
, Supported
, CompareLength
);
1161 if (Iso639Language
!= 0) {
1163 // If ISO 639 mode, then each language can only be tested once
1168 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1170 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1177 // No matches were found
1183 This function is to check if the remaining variable space is enough to set
1184 all Variables from argument list successfully. The purpose of the check
1185 is to keep the consistency of the Variables to be in variable storage.
1187 Note: Variables are assumed to be in same storage.
1188 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1189 so follow the argument sequence to check the Variables.
1191 @param[in] Attributes Variable attributes for Variable entries.
1192 @param[in] Marker VA_LIST style variable argument list.
1193 The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1194 A NULL terminates the list. The VariableSize of
1195 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
1196 It will be changed to variable total size as output.
1198 @retval TRUE Have enough variable space to set the Variables successfully.
1199 @retval FALSE No enough variable space to set the Variables successfully.
1204 CheckRemainingSpaceForConsistencyInternal (
1205 IN UINT32 Attributes
,
1211 VARIABLE_ENTRY_CONSISTENCY
*VariableEntry
;
1212 UINT64 MaximumVariableStorageSize
;
1213 UINT64 RemainingVariableStorageSize
;
1214 UINT64 MaximumVariableSize
;
1215 UINTN TotalNeededSize
;
1216 UINTN OriginalVarSize
;
1217 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1218 VARIABLE_POINTER_TRACK VariablePtrTrack
;
1219 VARIABLE_HEADER
*NextVariable
;
1224 // Non-Volatile related.
1226 VariableStoreHeader
= mNvVariableCache
;
1228 Status
= VariableServiceQueryVariableInfoInternal (
1230 &MaximumVariableStorageSize
,
1231 &RemainingVariableStorageSize
,
1232 &MaximumVariableSize
1234 ASSERT_EFI_ERROR (Status
);
1236 TotalNeededSize
= 0;
1237 VA_COPY (Args
, Marker
);
1238 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1239 while (VariableEntry
!= NULL
) {
1241 // Calculate variable total size.
1243 VarNameSize
= StrSize (VariableEntry
->Name
);
1244 VarNameSize
+= GET_PAD_SIZE (VarNameSize
);
1245 VarDataSize
= VariableEntry
->VariableSize
;
1246 VarDataSize
+= GET_PAD_SIZE (VarDataSize
);
1247 VariableEntry
->VariableSize
= HEADER_ALIGN (
1248 GetVariableHeaderSize (
1249 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
1250 ) + VarNameSize
+ VarDataSize
1253 TotalNeededSize
+= VariableEntry
->VariableSize
;
1254 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1258 if (RemainingVariableStorageSize
>= TotalNeededSize
) {
1260 // Already have enough space.
1263 } else if (AtRuntime ()) {
1265 // At runtime, no reclaim.
1266 // The original variable space of Variables can't be reused.
1271 VA_COPY (Args
, Marker
);
1272 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1273 while (VariableEntry
!= NULL
) {
1275 // Check if Variable[Index] has been present and get its size.
1277 OriginalVarSize
= 0;
1278 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
1279 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
1280 Status
= FindVariableEx (
1281 VariableEntry
->Name
,
1282 VariableEntry
->Guid
,
1285 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
1287 if (!EFI_ERROR (Status
)) {
1289 // Get size of Variable[Index].
1291 NextVariable
= GetNextVariablePtr (VariablePtrTrack
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
1292 OriginalVarSize
= (UINTN
) NextVariable
- (UINTN
) VariablePtrTrack
.CurrPtr
;
1294 // Add the original size of Variable[Index] to remaining variable storage size.
1296 RemainingVariableStorageSize
+= OriginalVarSize
;
1298 if (VariableEntry
->VariableSize
> RemainingVariableStorageSize
) {
1300 // No enough space for Variable[Index].
1306 // Sub the (new) size of Variable[Index] from remaining variable storage size.
1308 RemainingVariableStorageSize
-= VariableEntry
->VariableSize
;
1309 VariableEntry
= VA_ARG (Args
, VARIABLE_ENTRY_CONSISTENCY
*);
1317 This function is to check if the remaining variable space is enough to set
1318 all Variables from argument list successfully. The purpose of the check
1319 is to keep the consistency of the Variables to be in variable storage.
1321 Note: Variables are assumed to be in same storage.
1322 The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
1323 so follow the argument sequence to check the Variables.
1325 @param[in] Attributes Variable attributes for Variable entries.
1326 @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
1327 A NULL terminates the list. The VariableSize of
1328 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
1329 It will be changed to variable total size as output.
1331 @retval TRUE Have enough variable space to set the Variables successfully.
1332 @retval FALSE No enough variable space to set the Variables successfully.
1337 CheckRemainingSpaceForConsistency (
1338 IN UINT32 Attributes
,
1345 VA_START (Marker
, Attributes
);
1347 Return
= CheckRemainingSpaceForConsistencyInternal (Attributes
, Marker
);
1355 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1357 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1359 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1360 and are read-only. Therefore, in variable driver, only store the original value for other use.
1362 @param[in] VariableName Name of variable.
1364 @param[in] Data Variable data.
1366 @param[in] DataSize Size of data. 0 means delete.
1368 @retval EFI_SUCCESS The update operation is successful or ignored.
1369 @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.
1370 @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.
1371 @retval Others Other errors happened during the update operation.
1375 AutoUpdateLangVariable (
1376 IN CHAR16
*VariableName
,
1382 CHAR8
*BestPlatformLang
;
1386 VARIABLE_POINTER_TRACK Variable
;
1387 BOOLEAN SetLanguageCodes
;
1388 VARIABLE_ENTRY_CONSISTENCY VariableEntry
[2];
1391 // Don't do updates for delete operation
1393 if (DataSize
== 0) {
1397 SetLanguageCodes
= FALSE
;
1399 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME
) == 0) {
1401 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1404 return EFI_WRITE_PROTECTED
;
1407 SetLanguageCodes
= TRUE
;
1410 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1411 // Therefore, in variable driver, only store the original value for other use.
1413 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1414 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1416 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1417 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1420 // PlatformLang holds a single language from PlatformLangCodes,
1421 // so the size of PlatformLangCodes is enough for the PlatformLang.
1423 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1424 FreePool (mVariableModuleGlobal
->PlatformLang
);
1426 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1427 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1429 } else if (StrCmp (VariableName
, EFI_LANG_CODES_VARIABLE_NAME
) == 0) {
1431 // LangCodes is a volatile variable, so it can not be updated at runtime.
1434 return EFI_WRITE_PROTECTED
;
1437 SetLanguageCodes
= TRUE
;
1440 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1441 // Therefore, in variable driver, only store the original value for other use.
1443 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1444 FreePool (mVariableModuleGlobal
->LangCodes
);
1446 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1447 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1450 if (SetLanguageCodes
1451 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1452 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1454 // Update Lang if PlatformLang is already set
1455 // Update PlatformLang if Lang is already set
1457 Status
= FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1458 if (!EFI_ERROR (Status
)) {
1462 VariableName
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1463 Data
= GetVariableDataPtr (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
1464 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
1466 Status
= FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1467 if (!EFI_ERROR (Status
)) {
1469 // Update PlatformLang
1471 VariableName
= EFI_LANG_VARIABLE_NAME
;
1472 Data
= GetVariableDataPtr (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
1473 DataSize
= DataSizeOfVariable (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
1476 // Neither PlatformLang nor Lang is set, directly return
1483 Status
= EFI_SUCCESS
;
1486 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1488 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1490 if (StrCmp (VariableName
, EFI_PLATFORM_LANG_VARIABLE_NAME
) == 0) {
1492 // Update Lang when PlatformLangCodes/LangCodes were set.
1494 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1496 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1498 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1499 if (BestPlatformLang
!= NULL
) {
1501 // Get the corresponding index in language codes.
1503 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1506 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1508 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1511 // Check the variable space for both Lang and PlatformLang variable.
1513 VariableEntry
[0].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1514 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1515 VariableEntry
[0].Name
= EFI_LANG_VARIABLE_NAME
;
1517 VariableEntry
[1].VariableSize
= AsciiStrSize (BestPlatformLang
);
1518 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1519 VariableEntry
[1].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1520 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1522 // No enough variable space to set both Lang and PlatformLang successfully.
1524 Status
= EFI_OUT_OF_RESOURCES
;
1527 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1529 FindVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1531 Status
= UpdateVariable (EFI_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestLang
,
1532 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, 0, 0, &Variable
, NULL
);
1535 DEBUG ((DEBUG_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang
, BestLang
, Status
));
1539 } else if (StrCmp (VariableName
, EFI_LANG_VARIABLE_NAME
) == 0) {
1541 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1543 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1545 // When setting Lang, firstly get most matched language string from supported language codes.
1547 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1548 if (BestLang
!= NULL
) {
1550 // Get the corresponding index in language codes.
1552 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1555 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1557 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1560 // Check the variable space for both PlatformLang and Lang variable.
1562 VariableEntry
[0].VariableSize
= AsciiStrSize (BestPlatformLang
);
1563 VariableEntry
[0].Guid
= &gEfiGlobalVariableGuid
;
1564 VariableEntry
[0].Name
= EFI_PLATFORM_LANG_VARIABLE_NAME
;
1566 VariableEntry
[1].VariableSize
= ISO_639_2_ENTRY_SIZE
+ 1;
1567 VariableEntry
[1].Guid
= &gEfiGlobalVariableGuid
;
1568 VariableEntry
[1].Name
= EFI_LANG_VARIABLE_NAME
;
1569 if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT
, &VariableEntry
[0], &VariableEntry
[1], NULL
)) {
1571 // No enough variable space to set both PlatformLang and Lang successfully.
1573 Status
= EFI_OUT_OF_RESOURCES
;
1576 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1578 FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1580 Status
= UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME
, &gEfiGlobalVariableGuid
, BestPlatformLang
,
1581 AsciiStrSize (BestPlatformLang
), Attributes
, 0, 0, &Variable
, NULL
);
1584 DEBUG ((DEBUG_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang
, BestPlatformLang
, Status
));
1589 if (SetLanguageCodes
) {
1591 // Continue to set PlatformLangCodes or LangCodes.
1600 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
1601 index of associated public key is needed.
1603 @param[in] VariableName Name of variable.
1604 @param[in] VendorGuid Guid of variable.
1605 @param[in] Data Variable data.
1606 @param[in] DataSize Size of data. 0 means delete.
1607 @param[in] Attributes Attributes of the variable.
1608 @param[in] KeyIndex Index of associated public key.
1609 @param[in] MonotonicCount Value of associated monotonic count.
1610 @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.
1611 @param[in] TimeStamp Value of associated TimeStamp.
1613 @retval EFI_SUCCESS The update operation is success.
1614 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1619 IN CHAR16
*VariableName
,
1620 IN EFI_GUID
*VendorGuid
,
1623 IN UINT32 Attributes OPTIONAL
,
1624 IN UINT32 KeyIndex OPTIONAL
,
1625 IN UINT64 MonotonicCount OPTIONAL
,
1626 IN OUT VARIABLE_POINTER_TRACK
*CacheVariable
,
1627 IN EFI_TIME
*TimeStamp OPTIONAL
1631 VARIABLE_HEADER
*NextVariable
;
1634 UINTN VarNameOffset
;
1635 UINTN VarDataOffset
;
1639 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1641 VARIABLE_POINTER_TRACK
*Variable
;
1642 VARIABLE_POINTER_TRACK NvVariable
;
1643 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1644 VARIABLE_RUNTIME_CACHE
*VolatileCacheInstance
;
1645 UINT8
*BufferForMerge
;
1646 UINTN MergedBufSize
;
1649 BOOLEAN IsCommonVariable
;
1650 BOOLEAN IsCommonUserVariable
;
1651 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
1654 if (mVariableModuleGlobal
->FvbInstance
== NULL
&& !mVariableModuleGlobal
->VariableGlobal
.EmuNvMode
) {
1656 // The FVB protocol is not ready, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
1658 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1660 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1662 DEBUG ((DEBUG_ERROR
, "Update NV variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET
));
1663 return EFI_NOT_AVAILABLE_YET
;
1664 } else if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
1666 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1667 // The authenticated variable perhaps is not initialized, just return here.
1669 DEBUG ((DEBUG_ERROR
, "Update AUTH variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET
));
1670 return EFI_NOT_AVAILABLE_YET
;
1674 AuthFormat
= mVariableModuleGlobal
->VariableGlobal
.AuthFormat
;
1677 // Check if CacheVariable points to the variable in variable HOB.
1678 // If yes, let CacheVariable points to the variable in NV variable cache.
1680 if ((CacheVariable
->CurrPtr
!= NULL
) &&
1681 (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) &&
1682 (CacheVariable
->StartPtr
== GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
))
1684 CacheVariable
->StartPtr
= GetStartPointer (mNvVariableCache
);
1685 CacheVariable
->EndPtr
= GetEndPointer (mNvVariableCache
);
1686 CacheVariable
->Volatile
= FALSE
;
1687 Status
= FindVariableEx (VariableName
, VendorGuid
, FALSE
, CacheVariable
, AuthFormat
);
1688 if (CacheVariable
->CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1690 // There is no matched variable in NV variable cache.
1692 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0)) || (Attributes
== 0)) {
1694 // It is to delete variable,
1695 // go to delete this variable in variable HOB and
1696 // try to flush other variables from HOB to flash.
1698 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
, &gVariableInfo
);
1699 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1705 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1706 Variable
= CacheVariable
;
1709 // Update/Delete existing NV variable.
1710 // CacheVariable points to the variable in the memory copy of Flash area
1711 // Now let Variable points to the same variable in Flash area.
1713 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1714 Variable
= &NvVariable
;
1715 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1716 Variable
->EndPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->EndPtr
- (UINTN
)CacheVariable
->StartPtr
));
1718 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1719 if (CacheVariable
->InDeletedTransitionPtr
!= NULL
) {
1720 Variable
->InDeletedTransitionPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->InDeletedTransitionPtr
- (UINTN
)CacheVariable
->StartPtr
));
1722 Variable
->InDeletedTransitionPtr
= NULL
;
1724 Variable
->Volatile
= FALSE
;
1727 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1730 // Tricky part: Use scratch data area at the end of volatile variable store
1731 // as a temporary storage.
1733 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1734 ScratchSize
= mVariableModuleGlobal
->ScratchBufferSize
;
1735 SetMem (NextVariable
, ScratchSize
, 0xff);
1738 if (Variable
->CurrPtr
!= NULL
) {
1740 // Update/Delete existing variable.
1744 // If AtRuntime and the variable is Volatile and Runtime Access,
1745 // the volatile is ReadOnly, and SetVariable should be aborted and
1746 // return EFI_WRITE_PROTECTED.
1748 if (Variable
->Volatile
) {
1749 Status
= EFI_WRITE_PROTECTED
;
1753 // Only variable that have NV attributes can be updated/deleted in Runtime.
1755 if ((CacheVariable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1756 Status
= EFI_INVALID_PARAMETER
;
1761 // Only variable that have RT attributes can be updated/deleted in Runtime.
1763 if ((CacheVariable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) {
1764 Status
= EFI_INVALID_PARAMETER
;
1770 // Setting a data variable with no access, or zero DataSize attributes
1771 // causes it to be deleted.
1772 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
1773 // not delete the variable.
1775 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0))|| ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
1776 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
1778 // Both ADDED and IN_DELETED_TRANSITION variable are present,
1779 // set IN_DELETED_TRANSITION one to DELETED state first.
1781 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
1782 State
= CacheVariable
->InDeletedTransitionPtr
->State
;
1783 State
&= VAR_DELETED
;
1784 Status
= UpdateVariableStore (
1785 &mVariableModuleGlobal
->VariableGlobal
,
1789 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
1793 if (!EFI_ERROR (Status
)) {
1794 if (!Variable
->Volatile
) {
1795 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
1802 State
= CacheVariable
->CurrPtr
->State
;
1803 State
&= VAR_DELETED
;
1805 Status
= UpdateVariableStore (
1806 &mVariableModuleGlobal
->VariableGlobal
,
1810 (UINTN
) &Variable
->CurrPtr
->State
,
1814 if (!EFI_ERROR (Status
)) {
1815 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
, &gVariableInfo
);
1816 if (!Variable
->Volatile
) {
1817 CacheVariable
->CurrPtr
->State
= State
;
1818 FlushHobVariableToFlash (VariableName
, VendorGuid
);
1824 // If the variable is marked valid, and the same data has been passed in,
1825 // then return to the caller immediately.
1827 if (DataSizeOfVariable (CacheVariable
->CurrPtr
, AuthFormat
) == DataSize
&&
1828 (CompareMem (Data
, GetVariableDataPtr (CacheVariable
->CurrPtr
, AuthFormat
), DataSize
) == 0) &&
1829 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) &&
1830 (TimeStamp
== NULL
)) {
1832 // Variable content unchanged and no need to update timestamp, just return.
1834 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
, &gVariableInfo
);
1835 Status
= EFI_SUCCESS
;
1837 } else if ((CacheVariable
->CurrPtr
->State
== VAR_ADDED
) ||
1838 (CacheVariable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1841 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable.
1843 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
1845 // NOTE: From 0 to DataOffset of NextVariable is reserved for Variable Header and Name.
1846 // From DataOffset of NextVariable is to save the existing variable data.
1848 DataOffset
= GetVariableDataOffset (CacheVariable
->CurrPtr
, AuthFormat
);
1849 BufferForMerge
= (UINT8
*) ((UINTN
) NextVariable
+ DataOffset
);
1852 (UINT8
*) ((UINTN
) CacheVariable
->CurrPtr
+ DataOffset
),
1853 DataSizeOfVariable (CacheVariable
->CurrPtr
, AuthFormat
)
1857 // Set Max Auth/Non-Volatile/Volatile Variable Data Size as default MaxDataSize.
1859 if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
1860 MaxDataSize
= mVariableModuleGlobal
->MaxAuthVariableSize
- DataOffset
;
1861 } else if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1862 MaxDataSize
= mVariableModuleGlobal
->MaxVariableSize
- DataOffset
;
1864 MaxDataSize
= mVariableModuleGlobal
->MaxVolatileVariableSize
- DataOffset
;
1868 // Append the new data to the end of existing data.
1869 // Max Harware error record variable data size is different from common/auth variable.
1871 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
1872 MaxDataSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - DataOffset
;
1875 if (DataSizeOfVariable (CacheVariable
->CurrPtr
, AuthFormat
) + DataSize
> MaxDataSize
) {
1877 // Existing data size + new data size exceed maximum variable size limitation.
1879 Status
= EFI_INVALID_PARAMETER
;
1884 (UINTN
) BufferForMerge
+ DataSizeOfVariable (CacheVariable
->CurrPtr
, AuthFormat
)
1889 MergedBufSize
= DataSizeOfVariable (CacheVariable
->CurrPtr
, AuthFormat
) +
1893 // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.
1895 Data
= BufferForMerge
;
1896 DataSize
= MergedBufSize
;
1901 // Mark the old variable as in delete transition.
1903 State
= CacheVariable
->CurrPtr
->State
;
1904 State
&= VAR_IN_DELETED_TRANSITION
;
1906 Status
= UpdateVariableStore (
1907 &mVariableModuleGlobal
->VariableGlobal
,
1911 (UINTN
) &Variable
->CurrPtr
->State
,
1915 if (EFI_ERROR (Status
)) {
1918 if (!Variable
->Volatile
) {
1919 CacheVariable
->CurrPtr
->State
= State
;
1924 // Not found existing variable. Create a new variable.
1927 if ((DataSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
1928 Status
= EFI_SUCCESS
;
1933 // Make sure we are trying to create a new variable.
1934 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1936 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1937 Status
= EFI_NOT_FOUND
;
1942 // Only variable have NV|RT attribute can be created in Runtime.
1945 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1946 Status
= EFI_INVALID_PARAMETER
;
1952 // Function part - create a new variable and copy the data.
1953 // Both update a variable and create a variable will come here.
1955 NextVariable
->StartId
= VARIABLE_DATA
;
1957 // NextVariable->State = VAR_ADDED;
1959 NextVariable
->Reserved
= 0;
1960 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
1961 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) NextVariable
;
1962 AuthVariable
->PubKeyIndex
= KeyIndex
;
1963 AuthVariable
->MonotonicCount
= MonotonicCount
;
1964 ZeroMem (&AuthVariable
->TimeStamp
, sizeof (EFI_TIME
));
1966 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1967 (TimeStamp
!= NULL
)) {
1968 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
1969 CopyMem (&AuthVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1972 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
1973 // when the new TimeStamp value is later than the current timestamp associated
1974 // with the variable, we need associate the new timestamp with the updated value.
1976 if (Variable
->CurrPtr
!= NULL
) {
1977 if (VariableCompareTimeStampInternal (&(((AUTHENTICATED_VARIABLE_HEADER
*) CacheVariable
->CurrPtr
)->TimeStamp
), TimeStamp
)) {
1978 CopyMem (&AuthVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1980 CopyMem (&AuthVariable
->TimeStamp
, &(((AUTHENTICATED_VARIABLE_HEADER
*) CacheVariable
->CurrPtr
)->TimeStamp
), sizeof (EFI_TIME
));
1988 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
1989 // Attributes bitmask parameter of a GetVariable() call.
1991 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
1993 VarNameOffset
= GetVariableHeaderSize (AuthFormat
);
1994 VarNameSize
= StrSize (VariableName
);
1996 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
2000 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
2003 // If DataReady is TRUE, it means the variable data has been saved into
2004 // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.
2008 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
2015 GetVendorGuidPtr (NextVariable
, AuthFormat
),
2020 // There will be pad bytes after Data, the NextVariable->NameSize and
2021 // NextVariable->DataSize should not include pad size so that variable
2022 // service can get actual size in GetVariable.
2024 SetNameSizeOfVariable (NextVariable
, VarNameSize
, AuthFormat
);
2025 SetDataSizeOfVariable (NextVariable
, DataSize
, AuthFormat
);
2028 // The actual size of the variable that stores in storage should
2029 // include pad size.
2031 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
2032 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2034 // Create a nonvolatile variable.
2038 IsCommonVariable
= FALSE
;
2039 IsCommonUserVariable
= FALSE
;
2040 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0) {
2041 IsCommonVariable
= TRUE
;
2042 IsCommonUserVariable
= IsUserVariable (NextVariable
);
2044 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
2045 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
2046 || (IsCommonVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonVariableSpace
))
2047 || (IsCommonVariable
&& AtRuntime () && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonRuntimeVariableSpace
))
2048 || (IsCommonUserVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonUserVariableTotalSize
) > mVariableModuleGlobal
->CommonMaxUserVariableSpace
))) {
2050 if (IsCommonUserVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonUserVariableTotalSize
) > mVariableModuleGlobal
->CommonMaxUserVariableSpace
)) {
2051 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2053 if (IsCommonVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonRuntimeVariableSpace
)) {
2054 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2056 Status
= EFI_OUT_OF_RESOURCES
;
2060 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2063 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2064 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2068 HEADER_ALIGN (VarSize
)
2070 if (!EFI_ERROR (Status
)) {
2072 // The new variable has been integrated successfully during reclaiming.
2074 if (Variable
->CurrPtr
!= NULL
) {
2075 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2076 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2078 UpdateVariableInfo (VariableName
, VendorGuid
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, &gVariableInfo
);
2079 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2081 if (IsCommonUserVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonUserVariableTotalSize
) > mVariableModuleGlobal
->CommonMaxUserVariableSpace
)) {
2082 RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2084 if (IsCommonVariable
&& ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > mVariableModuleGlobal
->CommonVariableSpace
)) {
2085 RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR
, VariableName
, VendorGuid
, Attributes
, VarSize
);
2091 if (!mVariableModuleGlobal
->VariableGlobal
.EmuNvMode
) {
2094 // 1. Write variable header
2095 // 2. Set variable state to header valid
2096 // 3. Write variable data
2097 // 4. Set variable state to valid
2102 Status
= UpdateVariableStore (
2103 &mVariableModuleGlobal
->VariableGlobal
,
2107 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2108 (UINT32
) GetVariableHeaderSize (AuthFormat
),
2109 (UINT8
*) NextVariable
2112 if (EFI_ERROR (Status
)) {
2119 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
2120 Status
= UpdateVariableStore (
2121 &mVariableModuleGlobal
->VariableGlobal
,
2125 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2127 &NextVariable
->State
2130 if (EFI_ERROR (Status
)) {
2136 Status
= UpdateVariableStore (
2137 &mVariableModuleGlobal
->VariableGlobal
,
2141 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ GetVariableHeaderSize (AuthFormat
),
2142 (UINT32
) (VarSize
- GetVariableHeaderSize (AuthFormat
)),
2143 (UINT8
*) NextVariable
+ GetVariableHeaderSize (AuthFormat
)
2146 if (EFI_ERROR (Status
)) {
2152 NextVariable
->State
= VAR_ADDED
;
2153 Status
= UpdateVariableStore (
2154 &mVariableModuleGlobal
->VariableGlobal
,
2158 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
2160 &NextVariable
->State
2163 if (EFI_ERROR (Status
)) {
2168 // Update the memory copy of Flash region.
2170 CopyMem ((UINT8
*)mNvVariableCache
+ mVariableModuleGlobal
->NonVolatileLastVariableOffset
, (UINT8
*)NextVariable
, VarSize
);
2173 // Emulated non-volatile variable mode.
2175 NextVariable
->State
= VAR_ADDED
;
2176 Status
= UpdateVariableStore (
2177 &mVariableModuleGlobal
->VariableGlobal
,
2181 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2183 (UINT8
*) NextVariable
2186 if (EFI_ERROR (Status
)) {
2191 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2193 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2194 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2196 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2197 if (IsCommonUserVariable
) {
2198 mVariableModuleGlobal
->CommonUserVariableTotalSize
+= HEADER_ALIGN (VarSize
);
2203 // Create a volatile variable.
2207 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
2208 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
2210 // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
2213 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
2214 &mVariableModuleGlobal
->VolatileLastVariableOffset
,
2218 HEADER_ALIGN (VarSize
)
2220 if (!EFI_ERROR (Status
)) {
2222 // The new variable has been integrated successfully during reclaiming.
2224 if (Variable
->CurrPtr
!= NULL
) {
2225 CacheVariable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
) CacheVariable
->StartPtr
+ ((UINTN
) Variable
->CurrPtr
- (UINTN
) Variable
->StartPtr
));
2226 CacheVariable
->InDeletedTransitionPtr
= NULL
;
2228 UpdateVariableInfo (VariableName
, VendorGuid
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
, &gVariableInfo
);
2233 NextVariable
->State
= VAR_ADDED
;
2234 Status
= UpdateVariableStore (
2235 &mVariableModuleGlobal
->VariableGlobal
,
2239 mVariableModuleGlobal
->VolatileLastVariableOffset
,
2241 (UINT8
*) NextVariable
2244 if (EFI_ERROR (Status
)) {
2248 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
2252 // Mark the old variable as deleted.
2254 if (!EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
2255 if (Variable
->InDeletedTransitionPtr
!= NULL
) {
2257 // Both ADDED and IN_DELETED_TRANSITION old variable are present,
2258 // set IN_DELETED_TRANSITION one to DELETED state first.
2260 ASSERT (CacheVariable
->InDeletedTransitionPtr
!= NULL
);
2261 State
= CacheVariable
->InDeletedTransitionPtr
->State
;
2262 State
&= VAR_DELETED
;
2263 Status
= UpdateVariableStore (
2264 &mVariableModuleGlobal
->VariableGlobal
,
2268 (UINTN
) &Variable
->InDeletedTransitionPtr
->State
,
2272 if (!EFI_ERROR (Status
)) {
2273 if (!Variable
->Volatile
) {
2274 CacheVariable
->InDeletedTransitionPtr
->State
= State
;
2281 State
= CacheVariable
->CurrPtr
->State
;
2282 State
&= VAR_DELETED
;
2284 Status
= UpdateVariableStore (
2285 &mVariableModuleGlobal
->VariableGlobal
,
2289 (UINTN
) &Variable
->CurrPtr
->State
,
2293 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
2294 CacheVariable
->CurrPtr
->State
= State
;
2298 if (!EFI_ERROR (Status
)) {
2299 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
, &gVariableInfo
);
2301 FlushHobVariableToFlash (VariableName
, VendorGuid
);
2306 if (!EFI_ERROR (Status
)) {
2307 if ((Variable
->CurrPtr
!= NULL
&& !Variable
->Volatile
) || (Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2308 VolatileCacheInstance
= &(mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeNvCache
);
2310 VolatileCacheInstance
= &(mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeVolatileCache
);
2313 if (VolatileCacheInstance
->Store
!= NULL
) {
2314 Status
= SynchronizeRuntimeVariableCache (
2315 VolatileCacheInstance
,
2317 VolatileCacheInstance
->Store
->Size
2319 ASSERT_EFI_ERROR (Status
);
2328 This code finds variable in storage blocks (Volatile or Non-Volatile).
2330 Caution: This function may receive untrusted input.
2331 This function may be invoked in SMM mode, and datasize is external input.
2332 This function will do basic validation, before parse the data.
2334 @param VariableName Name of Variable to be found.
2335 @param VendorGuid Variable vendor GUID.
2336 @param Attributes Attribute value of the variable found.
2337 @param DataSize Size of Data found. If size is less than the
2338 data, this value contains the required size.
2339 @param Data The buffer to return the contents of the variable. May be NULL
2340 with a zero DataSize in order to determine the size buffer needed.
2342 @return EFI_INVALID_PARAMETER Invalid parameter.
2343 @return EFI_SUCCESS Find the specified variable.
2344 @return EFI_NOT_FOUND Not found.
2345 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2350 VariableServiceGetVariable (
2351 IN CHAR16
*VariableName
,
2352 IN EFI_GUID
*VendorGuid
,
2353 OUT UINT32
*Attributes OPTIONAL
,
2354 IN OUT UINTN
*DataSize
,
2355 OUT VOID
*Data OPTIONAL
2359 VARIABLE_POINTER_TRACK Variable
;
2362 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2363 return EFI_INVALID_PARAMETER
;
2366 if (VariableName
[0] == 0) {
2367 return EFI_NOT_FOUND
;
2370 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2372 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2373 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2380 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
2381 ASSERT (VarDataSize
!= 0);
2383 if (*DataSize
>= VarDataSize
) {
2385 Status
= EFI_INVALID_PARAMETER
;
2389 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
), VarDataSize
);
2391 *DataSize
= VarDataSize
;
2392 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
, &gVariableInfo
);
2394 Status
= EFI_SUCCESS
;
2397 *DataSize
= VarDataSize
;
2398 Status
= EFI_BUFFER_TOO_SMALL
;
2403 if (Status
== EFI_SUCCESS
|| Status
== EFI_BUFFER_TOO_SMALL
) {
2404 if (Attributes
!= NULL
&& Variable
.CurrPtr
!= NULL
) {
2405 *Attributes
= Variable
.CurrPtr
->Attributes
;
2408 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2414 This code Finds the Next available variable.
2416 Caution: This function may receive untrusted input.
2417 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2419 @param VariableNameSize The size of the VariableName buffer. The size must be large
2420 enough to fit input string supplied in VariableName buffer.
2421 @param VariableName Pointer to variable name.
2422 @param VendorGuid Variable Vendor Guid.
2424 @retval EFI_SUCCESS The function completed successfully.
2425 @retval EFI_NOT_FOUND The next variable was not found.
2426 @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the result.
2427 VariableNameSize has been updated with the size needed to complete the request.
2428 @retval EFI_INVALID_PARAMETER VariableNameSize is NULL.
2429 @retval EFI_INVALID_PARAMETER VariableName is NULL.
2430 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.
2431 @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
2432 GUID of an existing variable.
2433 @retval EFI_INVALID_PARAMETER Null-terminator is not found in the first VariableNameSize bytes of
2434 the input VariableName buffer.
2439 VariableServiceGetNextVariableName (
2440 IN OUT UINTN
*VariableNameSize
,
2441 IN OUT CHAR16
*VariableName
,
2442 IN OUT EFI_GUID
*VendorGuid
2449 VARIABLE_HEADER
*VariablePtr
;
2450 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2452 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2453 return EFI_INVALID_PARAMETER
;
2456 AuthFormat
= mVariableModuleGlobal
->VariableGlobal
.AuthFormat
;
2459 // Calculate the possible maximum length of name string, including the Null terminator.
2461 MaxLen
= *VariableNameSize
/ sizeof (CHAR16
);
2462 if ((MaxLen
== 0) || (StrnLenS (VariableName
, MaxLen
) == MaxLen
)) {
2464 // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,
2465 // follow spec to return EFI_INVALID_PARAMETER.
2467 return EFI_INVALID_PARAMETER
;
2470 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2473 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2474 // The index and attributes mapping must be kept in this order as FindVariable
2475 // makes use of this mapping to implement search algorithm.
2477 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2478 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2479 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2481 Status
= VariableServiceGetNextVariableInternal (
2484 VariableStoreHeader
,
2488 if (!EFI_ERROR (Status
)) {
2489 VarNameSize
= NameSizeOfVariable (VariablePtr
, AuthFormat
);
2490 ASSERT (VarNameSize
!= 0);
2491 if (VarNameSize
<= *VariableNameSize
) {
2494 GetVariableNamePtr (VariablePtr
, AuthFormat
),
2499 GetVendorGuidPtr (VariablePtr
, AuthFormat
),
2502 Status
= EFI_SUCCESS
;
2504 Status
= EFI_BUFFER_TOO_SMALL
;
2507 *VariableNameSize
= VarNameSize
;
2510 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2516 This code sets variable in storage blocks (Volatile or Non-Volatile).
2518 Caution: This function may receive untrusted input.
2519 This function may be invoked in SMM mode, and datasize and data are external input.
2520 This function will do basic validation, before parse the data.
2521 This function will parse the authentication carefully to avoid security issues, like
2522 buffer overflow, integer overflow.
2523 This function will check attribute carefully to avoid authentication bypass.
2525 @param VariableName Name of Variable to be found.
2526 @param VendorGuid Variable vendor GUID.
2527 @param Attributes Attribute value of the variable found
2528 @param DataSize Size of Data found. If size is less than the
2529 data, this value contains the required size.
2530 @param Data Data pointer.
2532 @return EFI_INVALID_PARAMETER Invalid parameter.
2533 @return EFI_SUCCESS Set successfully.
2534 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2535 @return EFI_NOT_FOUND Not found.
2536 @return EFI_WRITE_PROTECTED Variable is read-only.
2541 VariableServiceSetVariable (
2542 IN CHAR16
*VariableName
,
2543 IN EFI_GUID
*VendorGuid
,
2544 IN UINT32 Attributes
,
2549 VARIABLE_POINTER_TRACK Variable
;
2551 VARIABLE_HEADER
*NextVariable
;
2552 EFI_PHYSICAL_ADDRESS Point
;
2556 AuthFormat
= mVariableModuleGlobal
->VariableGlobal
.AuthFormat
;
2559 // Check input parameters.
2561 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2562 return EFI_INVALID_PARAMETER
;
2565 if (DataSize
!= 0 && Data
== NULL
) {
2566 return EFI_INVALID_PARAMETER
;
2570 // Check for reserverd bit in variable attribute.
2571 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated but we still allow
2572 // the delete operation of common authenticated variable at user physical presence.
2573 // So leave EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute check to AuthVariableLib
2575 if ((Attributes
& (~(EFI_VARIABLE_ATTRIBUTES_MASK
| EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
))) != 0) {
2576 return EFI_INVALID_PARAMETER
;
2580 // Make sure if runtime bit is set, boot service bit is set also.
2582 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2583 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2584 return EFI_UNSUPPORTED
;
2586 return EFI_INVALID_PARAMETER
;
2588 } else if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
2589 if (!mVariableModuleGlobal
->VariableGlobal
.AuthSupport
) {
2591 // Not support authenticated variable write.
2593 return EFI_INVALID_PARAMETER
;
2595 } else if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
2596 if (PcdGet32 (PcdHwErrStorageSize
) == 0) {
2598 // Not support harware error record variable variable.
2600 return EFI_INVALID_PARAMETER
;
2605 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
2606 // cannot be set both.
2608 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
2609 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
2610 return EFI_UNSUPPORTED
;
2613 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2615 // If DataSize == AUTHINFO_SIZE and then PayloadSize is 0.
2616 // Maybe it's the delete operation of common authenticated variable at user physical presence.
2618 if (DataSize
!= AUTHINFO_SIZE
) {
2619 return EFI_UNSUPPORTED
;
2621 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2622 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
2624 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2626 if (DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
||
2627 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
)) ||
2628 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) {
2629 return EFI_SECURITY_VIOLATION
;
2632 // The VariableSpeculationBarrier() call here is to ensure the above sanity
2633 // check for the EFI_VARIABLE_AUTHENTICATION_2 descriptor has been completed
2634 // before the execution of subsequent codes.
2636 VariableSpeculationBarrier ();
2637 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
2639 PayloadSize
= DataSize
;
2642 if ((UINTN
)(~0) - PayloadSize
< StrSize(VariableName
)){
2644 // Prevent whole variable size overflow
2646 return EFI_INVALID_PARAMETER
;
2650 // The size of the VariableName, including the Unicode Null in bytes plus
2651 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2652 // bytes for HwErrRec#### variable.
2654 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2655 if (StrSize (VariableName
) + PayloadSize
>
2656 PcdGet32 (PcdMaxHardwareErrorVariableSize
) - GetVariableHeaderSize (AuthFormat
)) {
2657 return EFI_INVALID_PARAMETER
;
2661 // The size of the VariableName, including the Unicode Null in bytes plus
2662 // the DataSize is limited to maximum size of Max(Auth|Volatile)VariableSize bytes.
2664 if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
2665 if (StrSize (VariableName
) + PayloadSize
>
2666 mVariableModuleGlobal
->MaxAuthVariableSize
-
2667 GetVariableHeaderSize (AuthFormat
)) {
2668 DEBUG ((DEBUG_ERROR
,
2669 "%a: Failed to set variable '%s' with Guid %g\n",
2670 __FUNCTION__
, VariableName
, VendorGuid
));
2671 DEBUG ((DEBUG_ERROR
,
2672 "NameSize(0x%x) + PayloadSize(0x%x) > "
2673 "MaxAuthVariableSize(0x%x) - HeaderSize(0x%x)\n",
2674 StrSize (VariableName
), PayloadSize
,
2675 mVariableModuleGlobal
->MaxAuthVariableSize
,
2676 GetVariableHeaderSize (AuthFormat
)
2678 return EFI_INVALID_PARAMETER
;
2680 } else if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2681 if (StrSize (VariableName
) + PayloadSize
>
2682 mVariableModuleGlobal
->MaxVariableSize
- GetVariableHeaderSize (AuthFormat
)) {
2683 DEBUG ((DEBUG_ERROR
,
2684 "%a: Failed to set variable '%s' with Guid %g\n",
2685 __FUNCTION__
, VariableName
, VendorGuid
));
2686 DEBUG ((DEBUG_ERROR
,
2687 "NameSize(0x%x) + PayloadSize(0x%x) > "
2688 "MaxVariableSize(0x%x) - HeaderSize(0x%x)\n",
2689 StrSize (VariableName
), PayloadSize
,
2690 mVariableModuleGlobal
->MaxVariableSize
,
2691 GetVariableHeaderSize (AuthFormat
)
2693 return EFI_INVALID_PARAMETER
;
2696 if (StrSize (VariableName
) + PayloadSize
>
2697 mVariableModuleGlobal
->MaxVolatileVariableSize
- GetVariableHeaderSize (AuthFormat
)) {
2698 DEBUG ((DEBUG_ERROR
,
2699 "%a: Failed to set variable '%s' with Guid %g\n",
2700 __FUNCTION__
, VariableName
, VendorGuid
));
2701 DEBUG ((DEBUG_ERROR
,
2702 "NameSize(0x%x) + PayloadSize(0x%x) > "
2703 "MaxVolatileVariableSize(0x%x) - HeaderSize(0x%x)\n",
2704 StrSize (VariableName
), PayloadSize
,
2705 mVariableModuleGlobal
->MaxVolatileVariableSize
,
2706 GetVariableHeaderSize (AuthFormat
)
2708 return EFI_INVALID_PARAMETER
;
2714 // Special Handling for MOR Lock variable.
2716 Status
= SetVariableCheckHandlerMor (VariableName
, VendorGuid
, Attributes
, PayloadSize
, (VOID
*) ((UINTN
) Data
+ DataSize
- PayloadSize
));
2717 if (Status
== EFI_ALREADY_STARTED
) {
2719 // EFI_ALREADY_STARTED means the SetVariable() action is handled inside of SetVariableCheckHandlerMor().
2720 // Variable driver can just return SUCCESS.
2724 if (EFI_ERROR (Status
)) {
2728 Status
= VarCheckLibSetVariableCheck (VariableName
, VendorGuid
, Attributes
, PayloadSize
, (VOID
*) ((UINTN
) Data
+ DataSize
- PayloadSize
), mRequestSource
);
2729 if (EFI_ERROR (Status
)) {
2733 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2736 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2738 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2739 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2741 // Parse non-volatile variable data and get last variable offset.
2743 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2744 while (IsValidVariableHeader (NextVariable
, GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))) {
2745 NextVariable
= GetNextVariablePtr (NextVariable
, AuthFormat
);
2747 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2751 // Check whether the input variable is already existed.
2753 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2754 if (!EFI_ERROR (Status
)) {
2755 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2756 Status
= EFI_WRITE_PROTECTED
;
2759 if (Attributes
!= 0 && (Attributes
& (~EFI_VARIABLE_APPEND_WRITE
)) != Variable
.CurrPtr
->Attributes
) {
2761 // If a preexisting variable is rewritten with different attributes, SetVariable() shall not
2762 // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:
2763 // 1. No access attributes specified
2764 // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE
2766 Status
= EFI_INVALID_PARAMETER
;
2767 DEBUG ((DEBUG_INFO
, "[Variable]: Rewritten a preexisting variable(0x%08x) with different attributes(0x%08x) - %g:%s\n", Variable
.CurrPtr
->Attributes
, Attributes
, VendorGuid
, VariableName
));
2772 if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate
)) {
2774 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2776 Status
= AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2777 if (EFI_ERROR (Status
)) {
2779 // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.
2785 if (mVariableModuleGlobal
->VariableGlobal
.AuthSupport
) {
2786 Status
= AuthVariableLibProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
);
2788 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, 0, 0, &Variable
, NULL
);
2792 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2793 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2795 if (!AtRuntime ()) {
2796 if (!EFI_ERROR (Status
)) {
2809 This code returns information about the EFI variables.
2811 Caution: This function may receive untrusted input.
2812 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2814 @param Attributes Attributes bitmask to specify the type of variables
2815 on which to return information.
2816 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2817 for the EFI variables associated with the attributes specified.
2818 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2819 for EFI variables associated with the attributes specified.
2820 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2821 associated with the attributes specified.
2823 @return EFI_SUCCESS Query successfully.
2828 VariableServiceQueryVariableInfoInternal (
2829 IN UINT32 Attributes
,
2830 OUT UINT64
*MaximumVariableStorageSize
,
2831 OUT UINT64
*RemainingVariableStorageSize
,
2832 OUT UINT64
*MaximumVariableSize
2835 VARIABLE_HEADER
*Variable
;
2836 VARIABLE_HEADER
*NextVariable
;
2837 UINT64 VariableSize
;
2838 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2839 UINT64 CommonVariableTotalSize
;
2840 UINT64 HwErrVariableTotalSize
;
2842 VARIABLE_POINTER_TRACK VariablePtrTrack
;
2844 CommonVariableTotalSize
= 0;
2845 HwErrVariableTotalSize
= 0;
2847 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2849 // Query is Volatile related.
2851 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2854 // Query is Non-Volatile related.
2856 VariableStoreHeader
= mNvVariableCache
;
2860 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2861 // with the storage size (excluding the storage header size).
2863 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2866 // Harware error record variable needs larger size.
2868 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2869 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2870 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) -
2871 GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
2873 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2875 *MaximumVariableStorageSize
= mVariableModuleGlobal
->CommonRuntimeVariableSpace
;
2877 *MaximumVariableStorageSize
= mVariableModuleGlobal
->CommonVariableSpace
;
2882 // Let *MaximumVariableSize be Max(Auth|Volatile)VariableSize with the exception of the variable header size.
2884 if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
2885 *MaximumVariableSize
= mVariableModuleGlobal
->MaxAuthVariableSize
-
2886 GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
2887 } else if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2888 *MaximumVariableSize
= mVariableModuleGlobal
->MaxVariableSize
-
2889 GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
2891 *MaximumVariableSize
= mVariableModuleGlobal
->MaxVolatileVariableSize
-
2892 GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
2897 // Point to the starting address of the variables.
2899 Variable
= GetStartPointer (VariableStoreHeader
);
2902 // Now walk through the related variable store.
2904 while (IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))) {
2905 NextVariable
= GetNextVariablePtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
2906 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2910 // We don't take the state of the variables in mind
2911 // when calculating RemainingVariableStorageSize,
2912 // since the space occupied by variables not marked with
2913 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2915 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2916 HwErrVariableTotalSize
+= VariableSize
;
2918 CommonVariableTotalSize
+= VariableSize
;
2922 // Only care about Variables with State VAR_ADDED, because
2923 // the space not marked as VAR_ADDED is reclaimable now.
2925 if (Variable
->State
== VAR_ADDED
) {
2926 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2927 HwErrVariableTotalSize
+= VariableSize
;
2929 CommonVariableTotalSize
+= VariableSize
;
2931 } else if (Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
2933 // If it is a IN_DELETED_TRANSITION variable,
2934 // and there is not also a same ADDED one at the same time,
2935 // this IN_DELETED_TRANSITION variable is valid.
2937 VariablePtrTrack
.StartPtr
= GetStartPointer (VariableStoreHeader
);
2938 VariablePtrTrack
.EndPtr
= GetEndPointer (VariableStoreHeader
);
2939 Status
= FindVariableEx (
2940 GetVariableNamePtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
2941 GetVendorGuidPtr (Variable
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
2944 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
2946 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
!= VAR_ADDED
) {
2947 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2948 HwErrVariableTotalSize
+= VariableSize
;
2950 CommonVariableTotalSize
+= VariableSize
;
2957 // Go to the next one.
2959 Variable
= NextVariable
;
2962 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2963 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2965 if (*MaximumVariableStorageSize
< CommonVariableTotalSize
) {
2966 *RemainingVariableStorageSize
= 0;
2968 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2972 if (*RemainingVariableStorageSize
< GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
)) {
2973 *MaximumVariableSize
= 0;
2974 } else if ((*RemainingVariableStorageSize
- GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
)) <
2975 *MaximumVariableSize
2977 *MaximumVariableSize
= *RemainingVariableStorageSize
-
2978 GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
2986 This code returns information about the EFI variables.
2988 Caution: This function may receive untrusted input.
2989 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2991 @param Attributes Attributes bitmask to specify the type of variables
2992 on which to return information.
2993 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2994 for the EFI variables associated with the attributes specified.
2995 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2996 for EFI variables associated with the attributes specified.
2997 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2998 associated with the attributes specified.
3000 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
3001 @return EFI_SUCCESS Query successfully.
3002 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
3007 VariableServiceQueryVariableInfo (
3008 IN UINT32 Attributes
,
3009 OUT UINT64
*MaximumVariableStorageSize
,
3010 OUT UINT64
*RemainingVariableStorageSize
,
3011 OUT UINT64
*MaximumVariableSize
3016 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
3017 return EFI_INVALID_PARAMETER
;
3020 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
3022 // Deprecated attribute, make this check as highest priority.
3024 return EFI_UNSUPPORTED
;
3027 if ((Attributes
& EFI_VARIABLE_ATTRIBUTES_MASK
) == 0) {
3029 // Make sure the Attributes combination is supported by the platform.
3031 return EFI_UNSUPPORTED
;
3032 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
3034 // Make sure if runtime bit is set, boot service bit is set also.
3036 return EFI_INVALID_PARAMETER
;
3037 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
3039 // Make sure RT Attribute is set if we are in Runtime phase.
3041 return EFI_INVALID_PARAMETER
;
3042 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
3044 // Make sure Hw Attribute is set with NV.
3046 return EFI_INVALID_PARAMETER
;
3047 } else if ((Attributes
& VARIABLE_ATTRIBUTE_AT_AW
) != 0) {
3048 if (!mVariableModuleGlobal
->VariableGlobal
.AuthSupport
) {
3050 // Not support authenticated variable write.
3052 return EFI_UNSUPPORTED
;
3054 } else if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
3055 if (PcdGet32 (PcdHwErrStorageSize
) == 0) {
3057 // Not support harware error record variable variable.
3059 return EFI_UNSUPPORTED
;
3063 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3065 Status
= VariableServiceQueryVariableInfoInternal (
3067 MaximumVariableStorageSize
,
3068 RemainingVariableStorageSize
,
3072 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3077 This function reclaims variable storage if free size is below the threshold.
3079 Caution: This function may be invoked at SMM mode.
3080 Care must be taken to make sure not security issue.
3089 UINTN RemainingCommonRuntimeVariableSpace
;
3090 UINTN RemainingHwErrVariableSpace
;
3091 STATIC BOOLEAN Reclaimed
;
3094 // This function will be called only once at EndOfDxe or ReadyToBoot event.
3101 Status
= EFI_SUCCESS
;
3103 if (mVariableModuleGlobal
->CommonRuntimeVariableSpace
< mVariableModuleGlobal
->CommonVariableTotalSize
) {
3104 RemainingCommonRuntimeVariableSpace
= 0;
3106 RemainingCommonRuntimeVariableSpace
= mVariableModuleGlobal
->CommonRuntimeVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
3109 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
3112 // Check if the free area is below a threshold.
3114 if (((RemainingCommonRuntimeVariableSpace
< mVariableModuleGlobal
->MaxVariableSize
) ||
3115 (RemainingCommonRuntimeVariableSpace
< mVariableModuleGlobal
->MaxAuthVariableSize
)) ||
3116 ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
3117 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
3119 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3120 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3126 ASSERT_EFI_ERROR (Status
);
3131 Get maximum variable size, covering both non-volatile and volatile variables.
3133 @return Maximum variable size.
3137 GetMaxVariableSize (
3141 UINTN MaxVariableSize
;
3143 MaxVariableSize
= GetNonVolatileMaxVariableSize();
3145 // The condition below fails implicitly if PcdMaxVolatileVariableSize equals
3146 // the default zero value.
3148 if (MaxVariableSize
< PcdGet32 (PcdMaxVolatileVariableSize
)) {
3149 MaxVariableSize
= PcdGet32 (PcdMaxVolatileVariableSize
);
3151 return MaxVariableSize
;
3155 Flush the HOB variable to flash.
3157 @param[in] VariableName Name of variable has been updated or deleted.
3158 @param[in] VendorGuid Guid of variable has been updated or deleted.
3162 FlushHobVariableToFlash (
3163 IN CHAR16
*VariableName
,
3164 IN EFI_GUID
*VendorGuid
3168 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3169 VARIABLE_HEADER
*Variable
;
3171 VARIABLE_POINTER_TRACK VariablePtrTrack
;
3176 AuthFormat
= mVariableModuleGlobal
->VariableGlobal
.AuthFormat
;
3179 // Flush the HOB variable to flash.
3181 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3182 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
3184 // Set HobVariableBase to 0, it can avoid SetVariable to call back.
3186 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
3187 for ( Variable
= GetStartPointer (VariableStoreHeader
)
3188 ; IsValidVariableHeader (Variable
, GetEndPointer (VariableStoreHeader
))
3189 ; Variable
= GetNextVariablePtr (Variable
, AuthFormat
)
3191 if (Variable
->State
!= VAR_ADDED
) {
3193 // The HOB variable has been set to DELETED state in local.
3197 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
3198 if (VendorGuid
== NULL
|| VariableName
== NULL
||
3199 !CompareGuid (VendorGuid
, GetVendorGuidPtr (Variable
, AuthFormat
)) ||
3200 StrCmp (VariableName
, GetVariableNamePtr (Variable
, AuthFormat
)) != 0) {
3201 VariableData
= GetVariableDataPtr (Variable
, AuthFormat
);
3203 GetVariableNamePtr (Variable
, AuthFormat
),
3204 GetVendorGuidPtr (Variable
, AuthFormat
),
3206 &mVariableModuleGlobal
->VariableGlobal
, FALSE
3208 Status
= UpdateVariable (
3209 GetVariableNamePtr (Variable
, AuthFormat
),
3210 GetVendorGuidPtr (Variable
, AuthFormat
),
3212 DataSizeOfVariable (Variable
, AuthFormat
),
3213 Variable
->Attributes
,
3221 "Variable driver flush the HOB variable to flash: %g %s %r\n",
3222 GetVendorGuidPtr (Variable
, AuthFormat
),
3223 GetVariableNamePtr (Variable
, AuthFormat
),
3228 // The updated or deleted variable is matched with this HOB variable.
3229 // Don't break here because we will try to set other HOB variables
3230 // since this variable could be set successfully.
3232 Status
= EFI_SUCCESS
;
3234 if (!EFI_ERROR (Status
)) {
3236 // If set variable successful, or the updated or deleted variable is matched with the HOB variable,
3237 // set the HOB variable to DELETED state in local.
3241 "Variable driver set the HOB variable to DELETED state in local: %g %s\n",
3242 GetVendorGuidPtr (Variable
, AuthFormat
),
3243 GetVariableNamePtr (Variable
, AuthFormat
)
3245 Variable
->State
&= VAR_DELETED
;
3250 if (mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeHobCache
.Store
!= NULL
) {
3251 Status
= SynchronizeRuntimeVariableCache (
3252 &mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeHobCache
,
3254 mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.VariableRuntimeHobCache
.Store
->Size
3256 ASSERT_EFI_ERROR (Status
);
3260 // We still have HOB variable(s) not flushed in flash.
3262 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
3265 // All HOB variables have been flushed in flash.
3267 DEBUG ((DEBUG_INFO
, "Variable driver: all HOB variables have been flushed in flash.\n"));
3268 if (mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.HobFlushComplete
!= NULL
) {
3269 *(mVariableModuleGlobal
->VariableGlobal
.VariableRuntimeCacheContext
.HobFlushComplete
) = TRUE
;
3271 if (!AtRuntime ()) {
3272 FreePool ((VOID
*) VariableStoreHeader
);
3280 Initializes variable write service.
3282 @retval EFI_SUCCESS Function successfully executed.
3283 @retval Others Fail to initialize the variable service.
3287 VariableWriteServiceInitialize (
3294 VARIABLE_ENTRY_PROPERTY
*VariableEntry
;
3296 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3299 // Check if the free area is really free.
3301 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< mNvVariableCache
->Size
; Index
++) {
3302 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
3305 // There must be something wrong in variable store, do reclaim operation.
3308 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
3309 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
3315 if (EFI_ERROR (Status
)) {
3316 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3323 FlushHobVariableToFlash (NULL
, NULL
);
3325 Status
= EFI_SUCCESS
;
3326 ZeroMem (&mAuthContextOut
, sizeof (mAuthContextOut
));
3327 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
3329 // Authenticated variable initialize.
3331 mAuthContextIn
.StructSize
= sizeof (AUTH_VAR_LIB_CONTEXT_IN
);
3332 mAuthContextIn
.MaxAuthVariableSize
= mVariableModuleGlobal
->MaxAuthVariableSize
-
3333 GetVariableHeaderSize (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
3334 Status
= AuthVariableLibInitialize (&mAuthContextIn
, &mAuthContextOut
);
3335 if (!EFI_ERROR (Status
)) {
3336 DEBUG ((DEBUG_INFO
, "Variable driver will work with auth variable support!\n"));
3337 mVariableModuleGlobal
->VariableGlobal
.AuthSupport
= TRUE
;
3338 if (mAuthContextOut
.AuthVarEntry
!= NULL
) {
3339 for (Index
= 0; Index
< mAuthContextOut
.AuthVarEntryCount
; Index
++) {
3340 VariableEntry
= &mAuthContextOut
.AuthVarEntry
[Index
];
3341 Status
= VarCheckLibVariablePropertySet (
3342 VariableEntry
->Name
,
3343 VariableEntry
->Guid
,
3344 &VariableEntry
->VariableProperty
3346 ASSERT_EFI_ERROR (Status
);
3349 } else if (Status
== EFI_UNSUPPORTED
) {
3350 DEBUG ((DEBUG_INFO
, "NOTICE - AuthVariableLibInitialize() returns %r!\n", Status
));
3351 DEBUG ((DEBUG_INFO
, "Variable driver will continue to work without auth variable support!\n"));
3352 mVariableModuleGlobal
->VariableGlobal
.AuthSupport
= FALSE
;
3353 Status
= EFI_SUCCESS
;
3357 if (!EFI_ERROR (Status
)) {
3358 for (Index
= 0; Index
< ARRAY_SIZE (mVariableEntryProperty
); Index
++) {
3359 VariableEntry
= &mVariableEntryProperty
[Index
];
3360 Status
= VarCheckLibVariablePropertySet (VariableEntry
->Name
, VariableEntry
->Guid
, &VariableEntry
->VariableProperty
);
3361 ASSERT_EFI_ERROR (Status
);
3365 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
3368 // Initialize MOR Lock variable.
3376 Convert normal variable storage to the allocated auth variable storage.
3378 @param[in] NormalVarStorage Pointer to the normal variable storage header
3380 @retval the allocated auth variable storage
3383 ConvertNormalVarStorageToAuthVarStorage (
3384 VARIABLE_STORE_HEADER
*NormalVarStorage
3387 VARIABLE_HEADER
*StartPtr
;
3389 VARIABLE_HEADER
*EndPtr
;
3390 UINTN AuthVarStroageSize
;
3391 AUTHENTICATED_VARIABLE_HEADER
*AuthStartPtr
;
3392 VARIABLE_STORE_HEADER
*AuthVarStorage
;
3394 AuthVarStroageSize
= sizeof (VARIABLE_STORE_HEADER
);
3396 // Set AuthFormat as FALSE for normal variable storage
3398 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
= FALSE
;
3401 // Calculate Auth Variable Storage Size
3403 StartPtr
= GetStartPointer (NormalVarStorage
);
3404 EndPtr
= GetEndPointer (NormalVarStorage
);
3405 while (StartPtr
< EndPtr
) {
3406 if (StartPtr
->State
== VAR_ADDED
) {
3407 AuthVarStroageSize
= HEADER_ALIGN (AuthVarStroageSize
);
3408 AuthVarStroageSize
+= sizeof (AUTHENTICATED_VARIABLE_HEADER
);
3409 AuthVarStroageSize
+= StartPtr
->NameSize
+ GET_PAD_SIZE (StartPtr
->NameSize
);
3410 AuthVarStroageSize
+= StartPtr
->DataSize
+ GET_PAD_SIZE (StartPtr
->DataSize
);
3412 StartPtr
= GetNextVariablePtr (StartPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
3416 // Allocate Runtime memory for Auth Variable Storage
3418 AuthVarStorage
= AllocateRuntimeZeroPool (AuthVarStroageSize
);
3419 ASSERT (AuthVarStorage
!= NULL
);
3420 if (AuthVarStorage
== NULL
) {
3425 // Copy Variable from Normal storage to Auth storage
3427 StartPtr
= GetStartPointer (NormalVarStorage
);
3428 EndPtr
= GetEndPointer (NormalVarStorage
);
3429 AuthStartPtr
= (AUTHENTICATED_VARIABLE_HEADER
*) GetStartPointer (AuthVarStorage
);
3430 while (StartPtr
< EndPtr
) {
3431 if (StartPtr
->State
== VAR_ADDED
) {
3432 AuthStartPtr
= (AUTHENTICATED_VARIABLE_HEADER
*) HEADER_ALIGN (AuthStartPtr
);
3434 // Copy Variable Header
3436 AuthStartPtr
->StartId
= StartPtr
->StartId
;
3437 AuthStartPtr
->State
= StartPtr
->State
;
3438 AuthStartPtr
->Attributes
= StartPtr
->Attributes
;
3439 AuthStartPtr
->NameSize
= StartPtr
->NameSize
;
3440 AuthStartPtr
->DataSize
= StartPtr
->DataSize
;
3441 CopyGuid (&AuthStartPtr
->VendorGuid
, &StartPtr
->VendorGuid
);
3443 // Copy Variable Name
3445 NextPtr
= (UINT8
*) (AuthStartPtr
+ 1);
3448 GetVariableNamePtr (StartPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
),
3449 AuthStartPtr
->NameSize
3452 // Copy Variable Data
3454 NextPtr
= NextPtr
+ AuthStartPtr
->NameSize
+ GET_PAD_SIZE (AuthStartPtr
->NameSize
);
3455 CopyMem (NextPtr
, GetVariableDataPtr (StartPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
), AuthStartPtr
->DataSize
);
3457 // Go to next variable
3459 AuthStartPtr
= (AUTHENTICATED_VARIABLE_HEADER
*) (NextPtr
+ AuthStartPtr
->DataSize
+ GET_PAD_SIZE (AuthStartPtr
->DataSize
));
3461 StartPtr
= GetNextVariablePtr (StartPtr
, mVariableModuleGlobal
->VariableGlobal
.AuthFormat
);
3464 // Update Auth Storage Header
3466 AuthVarStorage
->Format
= NormalVarStorage
->Format
;
3467 AuthVarStorage
->State
= NormalVarStorage
->State
;
3468 AuthVarStorage
->Size
= (UINT32
)((UINTN
)AuthStartPtr
- (UINTN
)AuthVarStorage
);
3469 CopyGuid (&AuthVarStorage
->Signature
, &gEfiAuthenticatedVariableGuid
);
3470 ASSERT (AuthVarStorage
->Size
<= AuthVarStroageSize
);
3473 // Restore AuthFormat
3475 mVariableModuleGlobal
->VariableGlobal
.AuthFormat
= TRUE
;
3476 return AuthVarStorage
;
3480 Get HOB variable store.
3482 @param[in] VariableGuid NV variable store signature.
3484 @retval EFI_SUCCESS Function successfully executed.
3485 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3489 GetHobVariableStore (
3490 IN EFI_GUID
*VariableGuid
3493 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
3494 UINT64 VariableStoreLength
;
3495 EFI_HOB_GUID_TYPE
*GuidHob
;
3496 BOOLEAN NeedConvertNormalToAuth
;
3499 // Make sure there is no more than one Variable HOB.
3502 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
3503 if (GuidHob
!= NULL
) {
3504 if ((GetNextGuidHob (&gEfiAuthenticatedVariableGuid
, GET_NEXT_HOB (GuidHob
)) != NULL
)) {
3505 DEBUG ((DEBUG_ERROR
, "ERROR: Found two Auth Variable HOBs\n"));
3507 } else if (GetFirstGuidHob (&gEfiVariableGuid
) != NULL
) {
3508 DEBUG ((DEBUG_ERROR
, "ERROR: Found one Auth + one Normal Variable HOBs\n"));
3512 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
3513 if (GuidHob
!= NULL
) {
3514 if ((GetNextGuidHob (&gEfiVariableGuid
, GET_NEXT_HOB (GuidHob
)) != NULL
)) {
3515 DEBUG ((DEBUG_ERROR
, "ERROR: Found two Normal Variable HOBs\n"));
3523 // Combinations supported:
3524 // 1. Normal NV variable store +
3525 // Normal HOB variable store
3526 // 2. Auth NV variable store +
3527 // Auth HOB variable store
3528 // 3. Auth NV variable store +
3529 // Normal HOB variable store (code will convert it to Auth Format)
3531 NeedConvertNormalToAuth
= FALSE
;
3532 GuidHob
= GetFirstGuidHob (VariableGuid
);
3533 if (GuidHob
== NULL
&& VariableGuid
== &gEfiAuthenticatedVariableGuid
) {
3535 // Try getting it from normal variable HOB
3537 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
3538 NeedConvertNormalToAuth
= TRUE
;
3540 if (GuidHob
!= NULL
) {
3541 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
3542 VariableStoreLength
= GuidHob
->Header
.HobLength
- sizeof (EFI_HOB_GUID_TYPE
);
3543 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
3544 if (!NeedConvertNormalToAuth
) {
3545 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) AllocateRuntimeCopyPool ((UINTN
) VariableStoreLength
, (VOID
*) VariableStoreHeader
);
3547 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) ConvertNormalVarStorageToAuthVarStorage ((VOID
*) VariableStoreHeader
);
3549 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
== 0) {
3550 return EFI_OUT_OF_RESOURCES
;
3553 DEBUG ((DEBUG_ERROR
, "HOB Variable Store header is corrupted!\n"));
3561 Initializes variable store area for non-volatile and volatile variable.
3563 @retval EFI_SUCCESS Function successfully executed.
3564 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
3568 VariableCommonInitialize (
3573 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
3575 EFI_GUID
*VariableGuid
;
3578 // Allocate runtime memory for variable driver global structure.
3580 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
3581 if (mVariableModuleGlobal
== NULL
) {
3582 return EFI_OUT_OF_RESOURCES
;
3585 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
3588 // Init non-volatile variable store.
3590 Status
= InitNonVolatileVariableStore ();
3591 if (EFI_ERROR (Status
)) {
3592 FreePool (mVariableModuleGlobal
);
3597 // mVariableModuleGlobal->VariableGlobal.AuthFormat
3598 // has been initialized in InitNonVolatileVariableStore().
3600 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
3601 DEBUG ((DEBUG_INFO
, "Variable driver will work with auth variable format!\n"));
3603 // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will initialize it.
3605 mVariableModuleGlobal
->VariableGlobal
.AuthSupport
= FALSE
;
3606 VariableGuid
= &gEfiAuthenticatedVariableGuid
;
3608 DEBUG ((DEBUG_INFO
, "Variable driver will work without auth variable support!\n"));
3609 mVariableModuleGlobal
->VariableGlobal
.AuthSupport
= FALSE
;
3610 VariableGuid
= &gEfiVariableGuid
;
3614 // Get HOB variable store.
3616 Status
= GetHobVariableStore (VariableGuid
);
3617 if (EFI_ERROR (Status
)) {
3618 if (mNvFvHeaderCache
!= NULL
) {
3619 FreePool (mNvFvHeaderCache
);
3621 FreePool (mVariableModuleGlobal
);
3625 mVariableModuleGlobal
->MaxVolatileVariableSize
= ((PcdGet32 (PcdMaxVolatileVariableSize
) != 0) ?
3626 PcdGet32 (PcdMaxVolatileVariableSize
) :
3627 mVariableModuleGlobal
->MaxVariableSize
3630 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
3632 ScratchSize
= GetMaxVariableSize ();
3633 mVariableModuleGlobal
->ScratchBufferSize
= ScratchSize
;
3634 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
3635 if (VolatileVariableStore
== NULL
) {
3636 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
3637 FreePool ((VOID
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
3639 if (mNvFvHeaderCache
!= NULL
) {
3640 FreePool (mNvFvHeaderCache
);
3642 FreePool (mVariableModuleGlobal
);
3643 return EFI_OUT_OF_RESOURCES
;
3646 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
3649 // Initialize Variable Specific Data.
3651 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
3652 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
3654 CopyGuid (&VolatileVariableStore
->Signature
, VariableGuid
);
3655 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
3656 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
3657 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
3658 VolatileVariableStore
->Reserved
= 0;
3659 VolatileVariableStore
->Reserved1
= 0;
3666 Get the proper fvb handle and/or fvb protocol by the given Flash address.
3668 @param[in] Address The Flash address.
3669 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
3670 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
3674 GetFvbInfoByAddress (
3675 IN EFI_PHYSICAL_ADDRESS Address
,
3676 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
3677 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
3681 EFI_HANDLE
*HandleBuffer
;
3684 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
3685 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
3686 EFI_FVB_ATTRIBUTES_2 Attributes
;
3688 UINTN NumberOfBlocks
;
3690 HandleBuffer
= NULL
;
3692 // Get all FVB handles.
3694 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
3695 if (EFI_ERROR (Status
)) {
3696 return EFI_NOT_FOUND
;
3700 // Get the FVB to access variable store.
3703 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
3704 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
3705 if (EFI_ERROR (Status
)) {
3706 Status
= EFI_NOT_FOUND
;
3711 // Ensure this FVB protocol supported Write operation.
3713 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
3714 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
3719 // Compare the address and select the right one.
3721 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
3722 if (EFI_ERROR (Status
)) {
3727 // Assume one FVB has one type of BlockSize.
3729 Status
= Fvb
->GetBlockSize (Fvb
, 0, &BlockSize
, &NumberOfBlocks
);
3730 if (EFI_ERROR (Status
)) {
3734 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ BlockSize
* NumberOfBlocks
))) {
3735 if (FvbHandle
!= NULL
) {
3736 *FvbHandle
= HandleBuffer
[Index
];
3738 if (FvbProtocol
!= NULL
) {
3741 Status
= EFI_SUCCESS
;
3745 FreePool (HandleBuffer
);
3748 Status
= EFI_NOT_FOUND
;