2 The common variable operation routines shared by DXE_RINTIME variable
3 module and DXE_SMM variable module.
5 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "AuthService.h"
19 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
22 /// Define a memory cache that improves the search performance for a variable.
24 VARIABLE_STORE_HEADER
*mNvVariableCache
= NULL
;
27 /// The memory entry used for variable statistics data.
29 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
33 Routine used to track statistical information about variable usage.
34 The data is stored in the EFI system table so it can be accessed later.
35 VariableInfo.efi can dump out the table. Only Boot Services variable
36 accesses are tracked by this code. The PcdVariableCollectStatistics
37 build flag controls if this feature is enabled.
39 A read that hits in the cache will have Read and Cache true for
40 the transaction. Data is allocated by this routine, but never
43 @param[in] VariableName Name of the Variable to track.
44 @param[in] VendorGuid Guid of the Variable to track.
45 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
46 @param[in] Read TRUE if GetVariable() was called.
47 @param[in] Write TRUE if SetVariable() was called.
48 @param[in] Delete TRUE if deleted via SetVariable().
49 @param[in] Cache TRUE for a cache hit.
54 IN CHAR16
*VariableName
,
55 IN EFI_GUID
*VendorGuid
,
63 VARIABLE_INFO_ENTRY
*Entry
;
65 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
68 // Don't collect statistics at runtime.
72 if (gVariableInfo
== NULL
) {
74 // On the first call allocate a entry and place a pointer to it in
75 // the EFI System Table.
77 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
78 ASSERT (gVariableInfo
!= NULL
);
80 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
81 gVariableInfo
->Name
= AllocatePool (StrSize (VariableName
));
82 ASSERT (gVariableInfo
->Name
!= NULL
);
83 StrCpy (gVariableInfo
->Name
, VariableName
);
84 gVariableInfo
->Volatile
= Volatile
;
88 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
89 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
90 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
108 if (Entry
->Next
== NULL
) {
110 // If the entry is not in the table add it.
111 // Next iteration of the loop will fill in the data.
113 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
114 ASSERT (Entry
->Next
!= NULL
);
116 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
117 Entry
->Next
->Name
= AllocatePool (StrSize (VariableName
));
118 ASSERT (Entry
->Next
->Name
!= NULL
);
119 StrCpy (Entry
->Next
->Name
, VariableName
);
120 Entry
->Next
->Volatile
= Volatile
;
130 This code checks if variable header is valid or not.
132 @param Variable Pointer to the Variable Header.
134 @retval TRUE Variable header is valid.
135 @retval FALSE Variable header is not valid.
139 IsValidVariableHeader (
140 IN VARIABLE_HEADER
*Variable
143 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
153 This function writes data to the FWH at the correct LBA even if the LBAs
156 @param Global Pointer to VARAIBLE_GLOBAL structure.
157 @param Volatile Point out the Variable is Volatile or Non-Volatile.
158 @param SetByIndex TRUE if target pointer is given as index.
159 FALSE if target pointer is absolute.
160 @param Fvb Pointer to the writable FVB protocol.
161 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
163 @param DataSize Size of data to be written.
164 @param Buffer Pointer to the buffer from which data is written.
166 @retval EFI_INVALID_PARAMETER Parameters not valid.
167 @retval EFI_SUCCESS Variable store successfully updated.
171 UpdateVariableStore (
172 IN VARIABLE_GLOBAL
*Global
,
174 IN BOOLEAN SetByIndex
,
175 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
176 IN UINTN DataPtrIndex
,
181 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
189 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
190 VARIABLE_STORE_HEADER
*VolatileBase
;
191 EFI_PHYSICAL_ADDRESS FvVolHdr
;
192 EFI_PHYSICAL_ADDRESS DataPtr
;
196 DataPtr
= DataPtrIndex
;
199 // Check if the Data is Volatile.
203 return EFI_INVALID_PARAMETER
;
205 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
206 ASSERT_EFI_ERROR (Status
);
208 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
210 // Data Pointer should point to the actual Address where data is to be
214 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
217 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
218 return EFI_INVALID_PARAMETER
;
222 // Data Pointer should point to the actual Address where data is to be
225 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
227 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
230 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
231 return EFI_INVALID_PARAMETER
;
235 // If Volatile Variable just do a simple mem copy.
237 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
242 // If we are here we are dealing with Non-Volatile Variables.
244 LinearOffset
= (UINTN
) FwVolHeader
;
245 CurrWritePtr
= (UINTN
) DataPtr
;
246 CurrWriteSize
= DataSize
;
250 if (CurrWritePtr
< LinearOffset
) {
251 return EFI_INVALID_PARAMETER
;
254 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
255 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
257 // Check to see if the Variable Writes are spanning through multiple
260 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
261 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
262 Status
= Fvb
->Write (
265 (UINTN
) (CurrWritePtr
- LinearOffset
),
271 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
272 Status
= Fvb
->Write (
275 (UINTN
) (CurrWritePtr
- LinearOffset
),
279 if (EFI_ERROR (Status
)) {
283 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
284 CurrBuffer
= CurrBuffer
+ Size
;
285 CurrWriteSize
= CurrWriteSize
- Size
;
289 LinearOffset
+= PtrBlockMapEntry
->Length
;
300 This code gets the current status of Variable Store.
302 @param VarStoreHeader Pointer to the Variable Store Header.
304 @retval EfiRaw Variable store status is raw.
305 @retval EfiValid Variable store status is valid.
306 @retval EfiInvalid Variable store status is invalid.
309 VARIABLE_STORE_STATUS
310 GetVariableStoreStatus (
311 IN VARIABLE_STORE_HEADER
*VarStoreHeader
314 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) &&
315 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
316 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
320 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
321 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
322 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
323 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
324 VarStoreHeader
->Size
== 0xffffffff &&
325 VarStoreHeader
->Format
== 0xff &&
326 VarStoreHeader
->State
== 0xff
338 This code gets the size of name of variable.
340 @param Variable Pointer to the Variable Header.
342 @return UINTN Size of variable in bytes.
347 IN VARIABLE_HEADER
*Variable
350 if (Variable
->State
== (UINT8
) (-1) ||
351 Variable
->DataSize
== (UINT32
) (-1) ||
352 Variable
->NameSize
== (UINT32
) (-1) ||
353 Variable
->Attributes
== (UINT32
) (-1)) {
356 return (UINTN
) Variable
->NameSize
;
361 This code gets the size of variable data.
363 @param Variable Pointer to the Variable Header.
365 @return Size of variable in bytes.
370 IN VARIABLE_HEADER
*Variable
373 if (Variable
->State
== (UINT8
) (-1) ||
374 Variable
->DataSize
== (UINT32
) (-1) ||
375 Variable
->NameSize
== (UINT32
) (-1) ||
376 Variable
->Attributes
== (UINT32
) (-1)) {
379 return (UINTN
) Variable
->DataSize
;
384 This code gets the pointer to the variable name.
386 @param Variable Pointer to the Variable Header.
388 @return Pointer to Variable Name which is Unicode encoding.
393 IN VARIABLE_HEADER
*Variable
397 return (CHAR16
*) (Variable
+ 1);
402 This code gets the pointer to the variable data.
404 @param Variable Pointer to the Variable Header.
406 @return Pointer to Variable Data.
411 IN VARIABLE_HEADER
*Variable
417 // Be careful about pad size for alignment.
419 Value
= (UINTN
) GetVariableNamePtr (Variable
);
420 Value
+= NameSizeOfVariable (Variable
);
421 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
423 return (UINT8
*) Value
;
429 This code gets the pointer to the next variable header.
431 @param Variable Pointer to the Variable Header.
433 @return Pointer to next variable header.
438 IN VARIABLE_HEADER
*Variable
443 if (!IsValidVariableHeader (Variable
)) {
447 Value
= (UINTN
) GetVariableDataPtr (Variable
);
448 Value
+= DataSizeOfVariable (Variable
);
449 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
452 // Be careful about pad size for alignment.
454 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
459 Gets the pointer to the first variable header in given variable store area.
461 @param VarStoreHeader Pointer to the Variable Store Header.
463 @return Pointer to the first variable header.
468 IN VARIABLE_STORE_HEADER
*VarStoreHeader
472 // The end of variable store.
474 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
479 Gets the pointer to the end of the variable storage area.
481 This function gets pointer to the end of the variable storage
482 area, according to the input variable store header.
484 @param VarStoreHeader Pointer to the Variable Store Header.
486 @return Pointer to the end of the variable storage area.
491 IN VARIABLE_STORE_HEADER
*VarStoreHeader
495 // The end of variable store
497 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
503 Variable store garbage collection and reclaim operation.
505 @param VariableBase Base address of variable store.
506 @param LastVariableOffset Offset of last variable.
507 @param IsVolatile The variable store is volatile or not;
508 if it is non-volatile, need FTW.
509 @param UpdatingVariable Pointer to updating variable.
511 @return EFI_OUT_OF_RESOURCES
518 IN EFI_PHYSICAL_ADDRESS VariableBase
,
519 OUT UINTN
*LastVariableOffset
,
520 IN BOOLEAN IsVolatile
,
521 IN VARIABLE_HEADER
*UpdatingVariable
524 VARIABLE_HEADER
*Variable
;
525 VARIABLE_HEADER
*AddedVariable
;
526 VARIABLE_HEADER
*NextVariable
;
527 VARIABLE_HEADER
*NextAddedVariable
;
528 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
530 UINTN MaximumBufferSize
;
532 UINTN VariableNameSize
;
533 UINTN UpdatingVariableNameSize
;
540 CHAR16
*VariableNamePtr
;
541 CHAR16
*UpdatingVariableNamePtr
;
543 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
545 // Recalculate the total size of Common/HwErr type variables in non-volatile area.
548 mVariableModuleGlobal
->CommonVariableTotalSize
= 0;
549 mVariableModuleGlobal
->HwErrVariableTotalSize
= 0;
553 // Start Pointers for the variable.
555 Variable
= GetStartPointer (VariableStoreHeader
);
556 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
558 while (IsValidVariableHeader (Variable
)) {
559 NextVariable
= GetNextVariablePtr (Variable
);
560 if (Variable
->State
== VAR_ADDED
||
561 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
563 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
564 MaximumBufferSize
+= VariableSize
;
567 Variable
= NextVariable
;
571 // Reserve the 1 Bytes with Oxff to identify the
572 // end of the variable buffer.
574 MaximumBufferSize
+= 1;
575 ValidBuffer
= AllocatePool (MaximumBufferSize
);
576 if (ValidBuffer
== NULL
) {
577 return EFI_OUT_OF_RESOURCES
;
580 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
583 // Copy variable store header.
585 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
586 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
589 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
591 Variable
= GetStartPointer (VariableStoreHeader
);
592 while (IsValidVariableHeader (Variable
)) {
593 NextVariable
= GetNextVariablePtr (Variable
);
594 if (Variable
->State
== VAR_ADDED
) {
595 if (UpdatingVariable
!= NULL
) {
596 if (UpdatingVariable
== Variable
) {
597 Variable
= NextVariable
;
601 VariableNameSize
= NameSizeOfVariable(Variable
);
602 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
604 VariableNamePtr
= GetVariableNamePtr (Variable
);
605 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
606 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
607 VariableNameSize
== UpdatingVariableNameSize
&&
608 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
609 Variable
= NextVariable
;
613 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
614 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
615 CurrPtr
+= VariableSize
;
616 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
617 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
618 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
619 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
622 Variable
= NextVariable
;
626 // Reinstall the variable being updated if it is not NULL.
628 if (UpdatingVariable
!= NULL
) {
629 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
630 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
631 CurrPtr
+= VariableSize
;
632 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
633 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
634 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
635 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
640 // Reinstall all in delete transition variables.
642 Variable
= GetStartPointer (VariableStoreHeader
);
643 while (IsValidVariableHeader (Variable
)) {
644 NextVariable
= GetNextVariablePtr (Variable
);
645 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
648 // Buffer has cached all ADDED variable.
649 // Per IN_DELETED variable, we have to guarantee that
650 // no ADDED one in previous buffer.
654 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
655 while (IsValidVariableHeader (AddedVariable
)) {
656 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
657 NameSize
= NameSizeOfVariable (AddedVariable
);
658 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
659 NameSize
== NameSizeOfVariable (Variable
)
661 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
662 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
663 if (CompareMem (Point0
, Point1
, NameSizeOfVariable (AddedVariable
)) == 0) {
668 AddedVariable
= NextAddedVariable
;
672 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
674 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
675 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
676 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
677 CurrPtr
+= VariableSize
;
678 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
679 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
680 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
681 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
686 Variable
= NextVariable
;
691 // If volatile variable store, just copy valid buffer.
693 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
694 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
695 Status
= EFI_SUCCESS
;
698 // If non-volatile variable store, perform FTW here.
700 Status
= FtwVariableSpace (
703 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
705 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
707 if (!EFI_ERROR (Status
)) {
708 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
710 *LastVariableOffset
= 0;
713 FreePool (ValidBuffer
);
719 Find the variable in the specified variable store.
721 @param VariableName Name of the variable to be found
722 @param VendorGuid Vendor GUID to be found.
723 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
725 @retval EFI_SUCCESS Variable found successfully
726 @retval EFI_NOT_FOUND Variable not found
730 IN CHAR16
*VariableName
,
731 IN EFI_GUID
*VendorGuid
,
732 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
735 VARIABLE_HEADER
*InDeletedVariable
;
739 // Find the variable by walk through HOB, volatile and non-volatile variable store.
741 InDeletedVariable
= NULL
;
743 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
744 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
745 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
747 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
748 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
750 if (!AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
751 if (VariableName
[0] == 0) {
752 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
753 InDeletedVariable
= PtrTrack
->CurrPtr
;
758 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
759 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
761 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
762 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
763 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
764 InDeletedVariable
= PtrTrack
->CurrPtr
;
775 PtrTrack
->CurrPtr
= InDeletedVariable
;
776 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
781 Finds variable in storage blocks of volatile and non-volatile storage areas.
783 This code finds variable in storage blocks of volatile and non-volatile storage areas.
784 If VariableName is an empty string, then we just return the first
785 qualified variable without comparing VariableName and VendorGuid.
786 Otherwise, VariableName and VendorGuid are compared.
788 @param VariableName Name of the variable to be found.
789 @param VendorGuid Vendor GUID to be found.
790 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
791 including the range searched and the target position.
792 @param Global Pointer to VARIABLE_GLOBAL structure, including
793 base of volatile variable storage area, base of
794 NV variable storage area, and a lock.
796 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
798 @retval EFI_SUCCESS Variable successfully found.
799 @retval EFI_NOT_FOUND Variable not found
804 IN CHAR16
*VariableName
,
805 IN EFI_GUID
*VendorGuid
,
806 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
807 IN VARIABLE_GLOBAL
*Global
811 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
812 VARIABLE_STORE_TYPE Type
;
814 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
815 return EFI_INVALID_PARAMETER
;
819 // 0: Volatile, 1: HOB, 2: Non-Volatile.
820 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
821 // make use of this mapping to implement search algorithm.
823 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
824 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
825 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
828 // Find the variable by walk through HOB, volatile and non-volatile variable store.
830 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
831 if (VariableStoreHeader
[Type
] == NULL
) {
835 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
836 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
837 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
839 Status
= FindVariableEx (VariableName
, VendorGuid
, PtrTrack
);
840 if (!EFI_ERROR (Status
)) {
844 return EFI_NOT_FOUND
;
848 Get index from supported language codes according to language string.
850 This code is used to get corresponding index in supported language codes. It can handle
851 RFC4646 and ISO639 language tags.
852 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
853 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
856 SupportedLang = "engfraengfra"
858 Iso639Language = TRUE
859 The return value is "0".
861 SupportedLang = "en;fr;en-US;fr-FR"
863 Iso639Language = FALSE
864 The return value is "3".
866 @param SupportedLang Platform supported language codes.
867 @param Lang Configured language.
868 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
870 @retval The index of language in the language codes.
874 GetIndexFromSupportedLangCodes(
875 IN CHAR8
*SupportedLang
,
877 IN BOOLEAN Iso639Language
882 UINTN LanguageLength
;
884 if (Iso639Language
) {
885 CompareLength
= ISO_639_2_ENTRY_SIZE
;
886 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
887 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
889 // Successfully find the index of Lang string in SupportedLang string.
891 Index
= Index
/ CompareLength
;
899 // Compare RFC4646 language code
902 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
904 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
906 // Skip ';' characters in SupportedLang
908 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
910 // Determine the length of the next language code in SupportedLang
912 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
914 if ((CompareLength
== LanguageLength
) &&
915 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
917 // Successfully find the index of Lang string in SupportedLang string.
928 Get language string from supported language codes according to index.
930 This code is used to get corresponding language strings in supported language codes. It can handle
931 RFC4646 and ISO639 language tags.
932 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
933 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
936 SupportedLang = "engfraengfra"
938 Iso639Language = TRUE
939 The return value is "fra".
941 SupportedLang = "en;fr;en-US;fr-FR"
943 Iso639Language = FALSE
944 The return value is "fr".
946 @param SupportedLang Platform supported language codes.
947 @param Index The index in supported language codes.
948 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
950 @retval The language string in the language codes.
954 GetLangFromSupportedLangCodes (
955 IN CHAR8
*SupportedLang
,
957 IN BOOLEAN Iso639Language
965 Supported
= SupportedLang
;
966 if (Iso639Language
) {
968 // According to the index of Lang string in SupportedLang string to get the language.
969 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
970 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
972 CompareLength
= ISO_639_2_ENTRY_SIZE
;
973 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
974 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
979 // Take semicolon as delimitation, sequentially traverse supported language codes.
981 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
984 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
986 // Have completed the traverse, but not find corrsponding string.
987 // This case is not allowed to happen.
992 if (SubIndex
== Index
) {
994 // According to the index of Lang string in SupportedLang string to get the language.
995 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
996 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
998 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
999 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1004 // Skip ';' characters in Supported
1006 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1012 Returns a pointer to an allocated buffer that contains the best matching language
1013 from a set of supported languages.
1015 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1016 code types may not be mixed in a single call to this function. This function
1017 supports a variable argument list that allows the caller to pass in a prioritized
1018 list of language codes to test against all the language codes in SupportedLanguages.
1020 If SupportedLanguages is NULL, then ASSERT().
1022 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1023 contains a set of language codes in the format
1024 specified by Iso639Language.
1025 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1026 in ISO 639-2 format. If FALSE, then all language
1027 codes are assumed to be in RFC 4646 language format
1028 @param[in] ... A variable argument list that contains pointers to
1029 Null-terminated ASCII strings that contain one or more
1030 language codes in the format specified by Iso639Language.
1031 The first language code from each of these language
1032 code lists is used to determine if it is an exact or
1033 close match to any of the language codes in
1034 SupportedLanguages. Close matches only apply to RFC 4646
1035 language codes, and the matching algorithm from RFC 4647
1036 is used to determine if a close match is present. If
1037 an exact or close match is found, then the matching
1038 language code from SupportedLanguages is returned. If
1039 no matches are found, then the next variable argument
1040 parameter is evaluated. The variable argument list
1041 is terminated by a NULL.
1043 @retval NULL The best matching language could not be found in SupportedLanguages.
1044 @retval NULL There are not enough resources available to return the best matching
1046 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1047 language in SupportedLanguages.
1052 VariableGetBestLanguage (
1053 IN CONST CHAR8
*SupportedLanguages
,
1054 IN BOOLEAN Iso639Language
,
1060 UINTN CompareLength
;
1061 UINTN LanguageLength
;
1062 CONST CHAR8
*Supported
;
1065 if (SupportedLanguages
== NULL
) {
1069 VA_START (Args
, Iso639Language
);
1070 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1072 // Default to ISO 639-2 mode
1075 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1078 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1080 if (!Iso639Language
) {
1081 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1085 // Trim back the length of Language used until it is empty
1087 while (LanguageLength
> 0) {
1089 // Loop through all language codes in SupportedLanguages
1091 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1093 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1095 if (!Iso639Language
) {
1097 // Skip ';' characters in Supported
1099 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1101 // Determine the length of the next language code in Supported
1103 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1105 // If Language is longer than the Supported, then skip to the next language
1107 if (LanguageLength
> CompareLength
) {
1112 // See if the first LanguageLength characters in Supported match Language
1114 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1117 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1118 Buffer
[CompareLength
] = '\0';
1119 return CopyMem (Buffer
, Supported
, CompareLength
);
1123 if (Iso639Language
) {
1125 // If ISO 639 mode, then each language can only be tested once
1130 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1132 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1139 // No matches were found
1145 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1147 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1149 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1150 and are read-only. Therefore, in variable driver, only store the original value for other use.
1152 @param[in] VariableName Name of variable.
1154 @param[in] Data Variable data.
1156 @param[in] DataSize Size of data. 0 means delete.
1160 AutoUpdateLangVariable(
1161 IN CHAR16
*VariableName
,
1167 CHAR8
*BestPlatformLang
;
1171 VARIABLE_POINTER_TRACK Variable
;
1172 BOOLEAN SetLanguageCodes
;
1175 // Don't do updates for delete operation
1177 if (DataSize
== 0) {
1181 SetLanguageCodes
= FALSE
;
1183 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1185 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1191 SetLanguageCodes
= TRUE
;
1194 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1195 // Therefore, in variable driver, only store the original value for other use.
1197 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1198 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1200 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1201 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1204 // PlatformLang holds a single language from PlatformLangCodes,
1205 // so the size of PlatformLangCodes is enough for the PlatformLang.
1207 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1208 FreePool (mVariableModuleGlobal
->PlatformLang
);
1210 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1211 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1213 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1215 // LangCodes is a volatile variable, so it can not be updated at runtime.
1221 SetLanguageCodes
= TRUE
;
1224 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1225 // Therefore, in variable driver, only store the original value for other use.
1227 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1228 FreePool (mVariableModuleGlobal
->LangCodes
);
1230 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1231 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1234 if (SetLanguageCodes
1235 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1236 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1238 // Update Lang if PlatformLang is already set
1239 // Update PlatformLang if Lang is already set
1241 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1242 if (!EFI_ERROR (Status
)) {
1246 VariableName
= L
"PlatformLang";
1247 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1248 DataSize
= Variable
.CurrPtr
->DataSize
;
1250 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1251 if (!EFI_ERROR (Status
)) {
1253 // Update PlatformLang
1255 VariableName
= L
"Lang";
1256 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1257 DataSize
= Variable
.CurrPtr
->DataSize
;
1260 // Neither PlatformLang nor Lang is set, directly return
1268 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1270 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1272 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1274 // Update Lang when PlatformLangCodes/LangCodes were set.
1276 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1278 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1280 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1281 if (BestPlatformLang
!= NULL
) {
1283 // Get the corresponding index in language codes.
1285 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1288 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1290 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1293 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1295 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1297 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1298 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, 0, 0, &Variable
, NULL
);
1300 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1302 ASSERT_EFI_ERROR(Status
);
1306 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1308 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1310 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1312 // When setting Lang, firstly get most matched language string from supported language codes.
1314 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1315 if (BestLang
!= NULL
) {
1317 // Get the corresponding index in language codes.
1319 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1322 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1324 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1327 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1329 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1331 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1332 AsciiStrSize (BestPlatformLang
), Attributes
, 0, 0, &Variable
, NULL
);
1334 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1335 ASSERT_EFI_ERROR (Status
);
1342 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
1343 index of associated public key is needed.
1345 @param[in] VariableName Name of variable.
1346 @param[in] VendorGuid Guid of variable.
1347 @param[in] Data Variable data.
1348 @param[in] DataSize Size of data. 0 means delete.
1349 @param[in] Attributes Attributes of the variable.
1350 @param[in] KeyIndex Index of associated public key.
1351 @param[in] MonotonicCount Value of associated monotonic count.
1352 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1353 @param[in] TimeStamp Value of associated TimeStamp.
1355 @retval EFI_SUCCESS The update operation is success.
1356 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1361 IN CHAR16
*VariableName
,
1362 IN EFI_GUID
*VendorGuid
,
1365 IN UINT32 Attributes OPTIONAL
,
1366 IN UINT32 KeyIndex OPTIONAL
,
1367 IN UINT64 MonotonicCount OPTIONAL
,
1368 IN VARIABLE_POINTER_TRACK
*CacheVariable
,
1369 IN EFI_TIME
*TimeStamp OPTIONAL
1373 VARIABLE_HEADER
*NextVariable
;
1375 UINTN ScratchDataSize
;
1376 UINTN NonVolatileVarableStoreSize
;
1377 UINTN VarNameOffset
;
1378 UINTN VarDataOffset
;
1382 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1385 VARIABLE_POINTER_TRACK
*Variable
;
1386 VARIABLE_POINTER_TRACK NvVariable
;
1387 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1393 if (mVariableModuleGlobal
->FvbInstance
== NULL
) {
1395 // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
1397 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1399 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1401 return EFI_NOT_AVAILABLE_YET
;
1402 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1404 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1405 // The authenticated variable perhaps is not initialized, just return here.
1407 return EFI_NOT_AVAILABLE_YET
;
1411 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1412 Variable
= CacheVariable
;
1415 // Update/Delete existing NV variable.
1416 // CacheVariable points to the variable in the memory copy of Flash area
1417 // Now let Variable points to the same variable in Flash area.
1419 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1420 Variable
= &NvVariable
;
1421 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1422 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1423 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1424 Variable
->Volatile
= FALSE
;
1427 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1431 // Tricky part: Use scratch data area at the end of volatile variable store
1432 // as a temporary storage.
1434 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1435 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1436 ScratchDataSize
= ScratchSize
- sizeof (VARIABLE_HEADER
) - StrSize (VariableName
) - GET_PAD_SIZE (StrSize (VariableName
));
1438 if (Variable
->CurrPtr
!= NULL
) {
1440 // Update/Delete existing variable.
1444 // If AtRuntime and the variable is Volatile and Runtime Access,
1445 // the volatile is ReadOnly, and SetVariable should be aborted and
1446 // return EFI_WRITE_PROTECTED.
1448 if (Variable
->Volatile
) {
1449 Status
= EFI_WRITE_PROTECTED
;
1453 // Only variable that have NV attributes can be updated/deleted in Runtime.
1455 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1456 Status
= EFI_INVALID_PARAMETER
;
1462 // Setting a data variable with no access, or zero DataSize attributes
1463 // causes it to be deleted.
1464 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
1465 // not delete the variable.
1467 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0))|| ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
1468 State
= Variable
->CurrPtr
->State
;
1469 State
&= VAR_DELETED
;
1471 Status
= UpdateVariableStore (
1472 &mVariableModuleGlobal
->VariableGlobal
,
1476 (UINTN
) &Variable
->CurrPtr
->State
,
1480 if (!EFI_ERROR (Status
)) {
1481 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1482 if (!Variable
->Volatile
) {
1483 CacheVariable
->CurrPtr
->State
= State
;
1489 // If the variable is marked valid, and the same data has been passed in,
1490 // then return to the caller immediately.
1492 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1493 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0) &&
1494 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
1496 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1497 Status
= EFI_SUCCESS
;
1499 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1500 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1503 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
1505 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
1507 BufSize
= Variable
->CurrPtr
->DataSize
+ DataSize
;
1508 RevBufSize
= MIN (PcdGet32 (PcdMaxAppendVariableSize
), ScratchDataSize
);
1510 if (BufSize
> RevBufSize
) {
1512 // If variable size (previous + current) is bigger than reserved buffer in runtime,
1513 // return EFI_OUT_OF_RESOURCES.
1515 return EFI_OUT_OF_RESOURCES
;
1518 SetMem (mStorageArea
, PcdGet32 (PcdMaxAppendVariableSize
), 0xff);
1520 // Cache the previous variable data into StorageArea.
1522 DataOffset
= sizeof (VARIABLE_HEADER
) + Variable
->CurrPtr
->NameSize
+ GET_PAD_SIZE (Variable
->CurrPtr
->NameSize
);
1523 CopyMem (mStorageArea
, (UINT8
*)((UINTN
)Variable
->CurrPtr
+ DataOffset
), Variable
->CurrPtr
->DataSize
);
1526 // Append the new data to the end of previous data.
1528 CopyMem ((UINT8
*)((UINTN
)mStorageArea
+ Variable
->CurrPtr
->DataSize
), Data
, DataSize
);
1531 // Override Data and DataSize which are used for combined data area including previous and new data.
1533 Data
= mStorageArea
;
1538 // Mark the old variable as in delete transition.
1540 State
= Variable
->CurrPtr
->State
;
1541 State
&= VAR_IN_DELETED_TRANSITION
;
1543 Status
= UpdateVariableStore (
1544 &mVariableModuleGlobal
->VariableGlobal
,
1548 (UINTN
) &Variable
->CurrPtr
->State
,
1552 if (EFI_ERROR (Status
)) {
1555 if (!Variable
->Volatile
) {
1556 CacheVariable
->CurrPtr
->State
= State
;
1561 // Not found existing variable. Create a new variable.
1565 // EFI_VARIABLE_APPEND_WRITE attribute only set for existing variable
1567 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
1568 Status
= EFI_INVALID_PARAMETER
;
1573 // Make sure we are trying to create a new variable.
1574 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1576 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1577 Status
= EFI_NOT_FOUND
;
1582 // Only variable have NV|RT attribute can be created in Runtime.
1585 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1586 Status
= EFI_INVALID_PARAMETER
;
1592 // Function part - create a new variable and copy the data.
1593 // Both update a variable and create a variable will come here.
1595 SetMem (NextVariable
, ScratchSize
, 0xff);
1597 NextVariable
->StartId
= VARIABLE_DATA
;
1599 // NextVariable->State = VAR_ADDED;
1601 NextVariable
->Reserved
= 0;
1602 NextVariable
->PubKeyIndex
= KeyIndex
;
1603 NextVariable
->MonotonicCount
= MonotonicCount
;
1604 SetMem (&NextVariable
->TimeStamp
, sizeof (EFI_TIME
), 0);
1606 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1607 TimeStamp
!= NULL
) {
1608 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
1609 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1612 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
1613 // when the new TimeStamp value is later than the current timestamp associated
1614 // with the variable, we need associate the new timestamp with the updated value.
1616 if (CompareTimeStamp (&Variable
->CurrPtr
->TimeStamp
, TimeStamp
)) {
1617 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1623 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
1624 // Attributes bitmask parameter of a GetVariable() call.
1626 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
1628 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1629 VarNameSize
= StrSize (VariableName
);
1631 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1635 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1637 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1641 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1643 // There will be pad bytes after Data, the NextVariable->NameSize and
1644 // NextVariable->DataSize should not include pad size so that variable
1645 // service can get actual size in GetVariable.
1647 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1648 NextVariable
->DataSize
= (UINT32
)DataSize
;
1651 // The actual size of the variable that stores in storage should
1652 // include pad size.
1654 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1655 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1657 // Create a nonvolatile variable.
1660 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1661 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1662 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1663 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1664 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1666 Status
= EFI_OUT_OF_RESOURCES
;
1670 // Perform garbage collection & reclaim operation.
1672 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1673 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
->CurrPtr
);
1674 if (EFI_ERROR (Status
)) {
1678 // If still no enough space, return out of resources.
1680 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1681 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1682 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1683 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1684 Status
= EFI_OUT_OF_RESOURCES
;
1691 // 1. Write variable header
1692 // 2. Set variable state to header valid
1693 // 3. Write variable data
1694 // 4. Set variable state to valid
1699 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1700 Status
= UpdateVariableStore (
1701 &mVariableModuleGlobal
->VariableGlobal
,
1705 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1706 sizeof (VARIABLE_HEADER
),
1707 (UINT8
*) NextVariable
1710 if (EFI_ERROR (Status
)) {
1717 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1718 Status
= UpdateVariableStore (
1719 &mVariableModuleGlobal
->VariableGlobal
,
1723 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1725 &NextVariable
->State
1728 if (EFI_ERROR (Status
)) {
1734 Status
= UpdateVariableStore (
1735 &mVariableModuleGlobal
->VariableGlobal
,
1739 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1740 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1741 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1744 if (EFI_ERROR (Status
)) {
1750 NextVariable
->State
= VAR_ADDED
;
1751 Status
= UpdateVariableStore (
1752 &mVariableModuleGlobal
->VariableGlobal
,
1756 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1758 &NextVariable
->State
1761 if (EFI_ERROR (Status
)) {
1765 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1767 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1768 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1770 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1773 // update the memory copy of Flash region.
1775 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1778 // Create a volatile variable.
1782 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1783 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1785 // Perform garbage collection & reclaim operation.
1787 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1788 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
->CurrPtr
);
1789 if (EFI_ERROR (Status
)) {
1793 // If still no enough space, return out of resources.
1795 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1796 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1798 Status
= EFI_OUT_OF_RESOURCES
;
1804 NextVariable
->State
= VAR_ADDED
;
1805 Status
= UpdateVariableStore (
1806 &mVariableModuleGlobal
->VariableGlobal
,
1810 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1812 (UINT8
*) NextVariable
1815 if (EFI_ERROR (Status
)) {
1819 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1823 // Mark the old variable as deleted.
1825 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1826 State
= Variable
->CurrPtr
->State
;
1827 State
&= VAR_DELETED
;
1829 Status
= UpdateVariableStore (
1830 &mVariableModuleGlobal
->VariableGlobal
,
1834 (UINTN
) &Variable
->CurrPtr
->State
,
1838 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1839 CacheVariable
->CurrPtr
->State
= State
;
1843 if (!EFI_ERROR (Status
)) {
1844 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1853 This code finds variable in storage blocks (Volatile or Non-Volatile).
1855 @param VariableName Name of Variable to be found.
1856 @param VendorGuid Variable vendor GUID.
1857 @param Attributes Attribute value of the variable found.
1858 @param DataSize Size of Data found. If size is less than the
1859 data, this value contains the required size.
1860 @param Data Data pointer.
1862 @return EFI_INVALID_PARAMETER Invalid parameter.
1863 @return EFI_SUCCESS Find the specified variable.
1864 @return EFI_NOT_FOUND Not found.
1865 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1870 VariableServiceGetVariable (
1871 IN CHAR16
*VariableName
,
1872 IN EFI_GUID
*VendorGuid
,
1873 OUT UINT32
*Attributes OPTIONAL
,
1874 IN OUT UINTN
*DataSize
,
1879 VARIABLE_POINTER_TRACK Variable
;
1882 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1883 return EFI_INVALID_PARAMETER
;
1886 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1888 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1889 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1896 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1897 ASSERT (VarDataSize
!= 0);
1899 if (*DataSize
>= VarDataSize
) {
1901 Status
= EFI_INVALID_PARAMETER
;
1905 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1906 if (Attributes
!= NULL
) {
1907 *Attributes
= Variable
.CurrPtr
->Attributes
;
1910 *DataSize
= VarDataSize
;
1911 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1913 Status
= EFI_SUCCESS
;
1916 *DataSize
= VarDataSize
;
1917 Status
= EFI_BUFFER_TOO_SMALL
;
1922 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1930 This code Finds the Next available variable.
1932 @param VariableNameSize Size of the variable name.
1933 @param VariableName Pointer to variable name.
1934 @param VendorGuid Variable Vendor Guid.
1936 @return EFI_INVALID_PARAMETER Invalid parameter.
1937 @return EFI_SUCCESS Find the specified variable.
1938 @return EFI_NOT_FOUND Not found.
1939 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1944 VariableServiceGetNextVariableName (
1945 IN OUT UINTN
*VariableNameSize
,
1946 IN OUT CHAR16
*VariableName
,
1947 IN OUT EFI_GUID
*VendorGuid
1950 VARIABLE_STORE_TYPE Type
;
1951 VARIABLE_POINTER_TRACK Variable
;
1952 VARIABLE_POINTER_TRACK VariableInHob
;
1955 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1957 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1958 return EFI_INVALID_PARAMETER
;
1961 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1963 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1964 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1968 if (VariableName
[0] != 0) {
1970 // If variable name is not NULL, get next variable.
1972 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1976 // 0: Volatile, 1: HOB, 2: Non-Volatile.
1977 // The index and attributes mapping must be kept in this order as FindVariable
1978 // makes use of this mapping to implement search algorithm.
1980 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
1981 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
1982 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
1986 // Switch from Volatile to HOB, to Non-Volatile.
1988 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
1989 (Variable
.CurrPtr
== NULL
) ||
1990 !IsValidVariableHeader (Variable
.CurrPtr
)
1993 // Find current storage index
1995 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1996 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2000 ASSERT (Type
< VariableStoreTypeMax
);
2002 // Switch to next storage
2004 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2005 if (VariableStoreHeader
[Type
] != NULL
) {
2010 // Capture the case that
2011 // 1. current storage is the last one, or
2012 // 2. no further storage
2014 if (Type
== VariableStoreTypeMax
) {
2015 Status
= EFI_NOT_FOUND
;
2018 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2019 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2020 Variable
.CurrPtr
= Variable
.StartPtr
;
2024 // Variable is found
2026 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
2027 if ((AtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
2030 // Don't return NV variable when HOB overrides it
2032 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2033 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2035 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2036 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2037 Status
= FindVariableEx (
2038 GetVariableNamePtr (Variable
.CurrPtr
),
2039 &Variable
.CurrPtr
->VendorGuid
,
2042 if (!EFI_ERROR (Status
)) {
2043 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2048 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2049 ASSERT (VarNameSize
!= 0);
2051 if (VarNameSize
<= *VariableNameSize
) {
2052 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2053 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2054 Status
= EFI_SUCCESS
;
2056 Status
= EFI_BUFFER_TOO_SMALL
;
2059 *VariableNameSize
= VarNameSize
;
2064 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2068 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2074 This code sets variable in storage blocks (Volatile or Non-Volatile).
2076 @param VariableName Name of Variable to be found.
2077 @param VendorGuid Variable vendor GUID.
2078 @param Attributes Attribute value of the variable found
2079 @param DataSize Size of Data found. If size is less than the
2080 data, this value contains the required size.
2081 @param Data Data pointer.
2083 @return EFI_INVALID_PARAMETER Invalid parameter.
2084 @return EFI_SUCCESS Set successfully.
2085 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2086 @return EFI_NOT_FOUND Not found.
2087 @return EFI_WRITE_PROTECTED Variable is read-only.
2092 VariableServiceSetVariable (
2093 IN CHAR16
*VariableName
,
2094 IN EFI_GUID
*VendorGuid
,
2095 IN UINT32 Attributes
,
2100 VARIABLE_POINTER_TRACK Variable
;
2102 VARIABLE_HEADER
*NextVariable
;
2103 EFI_PHYSICAL_ADDRESS Point
;
2107 // Check input parameters.
2109 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2110 return EFI_INVALID_PARAMETER
;
2113 if (DataSize
!= 0 && Data
== NULL
) {
2114 return EFI_INVALID_PARAMETER
;
2118 // Make sure if runtime bit is set, boot service bit is set also.
2120 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2121 return EFI_INVALID_PARAMETER
;
2125 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
2126 // cannot be set both.
2128 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) \
2129 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
2130 return EFI_INVALID_PARAMETER
;
2133 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2134 if (DataSize
< AUTHINFO_SIZE
) {
2136 // Try to write Authencated Variable without AuthInfo.
2138 return EFI_SECURITY_VIOLATION
;
2140 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2142 PayloadSize
= DataSize
;
2145 // The size of the VariableName, including the Unicode Null in bytes plus
2146 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2147 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2149 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2150 if ((PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
2151 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
2152 return EFI_INVALID_PARAMETER
;
2155 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX".
2157 if (StrnCmp(VariableName
, L
"HwErrRec", StrLen(L
"HwErrRec")) != 0) {
2158 return EFI_INVALID_PARAMETER
;
2162 // The size of the VariableName, including the Unicode Null in bytes plus
2163 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2165 if ((PayloadSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2166 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxVariableSize
))) {
2167 return EFI_INVALID_PARAMETER
;
2171 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2174 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2176 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2177 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2179 // Parse non-volatile variable data and get last variable offset.
2181 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2182 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2183 && IsValidVariableHeader (NextVariable
)) {
2184 NextVariable
= GetNextVariablePtr (NextVariable
);
2186 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2190 // Check whether the input variable is already existed.
2192 FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
2195 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2197 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2199 // Process PK, KEK, Sigdb seperately.
2201 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
2202 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, TRUE
);
2203 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
2204 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2205 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0)) {
2206 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2208 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2211 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2212 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2219 This code returns information about the EFI variables.
2221 @param Attributes Attributes bitmask to specify the type of variables
2222 on which to return information.
2223 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2224 for the EFI variables associated with the attributes specified.
2225 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2226 for EFI variables associated with the attributes specified.
2227 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2228 associated with the attributes specified.
2230 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2231 @return EFI_SUCCESS Query successfully.
2232 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2237 VariableServiceQueryVariableInfo (
2238 IN UINT32 Attributes
,
2239 OUT UINT64
*MaximumVariableStorageSize
,
2240 OUT UINT64
*RemainingVariableStorageSize
,
2241 OUT UINT64
*MaximumVariableSize
2244 VARIABLE_HEADER
*Variable
;
2245 VARIABLE_HEADER
*NextVariable
;
2246 UINT64 VariableSize
;
2247 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2248 UINT64 CommonVariableTotalSize
;
2249 UINT64 HwErrVariableTotalSize
;
2251 CommonVariableTotalSize
= 0;
2252 HwErrVariableTotalSize
= 0;
2254 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2255 return EFI_INVALID_PARAMETER
;
2258 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2260 // Make sure the Attributes combination is supported by the platform.
2262 return EFI_UNSUPPORTED
;
2263 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2265 // Make sure if runtime bit is set, boot service bit is set also.
2267 return EFI_INVALID_PARAMETER
;
2268 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2270 // Make sure RT Attribute is set if we are in Runtime phase.
2272 return EFI_INVALID_PARAMETER
;
2273 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2275 // Make sure Hw Attribute is set with NV.
2277 return EFI_INVALID_PARAMETER
;
2280 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2282 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2284 // Query is Volatile related.
2286 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2289 // Query is Non-Volatile related.
2291 VariableStoreHeader
= mNvVariableCache
;
2295 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2296 // with the storage size (excluding the storage header size).
2298 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2301 // Harware error record variable needs larger size.
2303 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2304 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2305 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2307 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2308 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2309 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2313 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2315 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2319 // Point to the starting address of the variables.
2321 Variable
= GetStartPointer (VariableStoreHeader
);
2324 // Now walk through the related variable store.
2326 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2327 NextVariable
= GetNextVariablePtr (Variable
);
2328 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2332 // We don't take the state of the variables in mind
2333 // when calculating RemainingVariableStorageSize,
2334 // since the space occupied by variables not marked with
2335 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2337 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2338 HwErrVariableTotalSize
+= VariableSize
;
2340 CommonVariableTotalSize
+= VariableSize
;
2344 // Only care about Variables with State VAR_ADDED, because
2345 // the space not marked as VAR_ADDED is reclaimable now.
2347 if (Variable
->State
== VAR_ADDED
) {
2348 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2349 HwErrVariableTotalSize
+= VariableSize
;
2351 CommonVariableTotalSize
+= VariableSize
;
2357 // Go to the next one.
2359 Variable
= NextVariable
;
2362 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2363 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2365 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2368 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2369 *MaximumVariableSize
= 0;
2370 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2371 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2374 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2380 This function reclaims variable storage if free size is below the threshold.
2389 UINTN CommonVariableSpace
;
2390 UINTN RemainingCommonVariableSpace
;
2391 UINTN RemainingHwErrVariableSpace
;
2393 Status
= EFI_SUCCESS
;
2395 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2397 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2399 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2401 // Check if the free area is blow a threshold.
2403 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2404 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2405 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2407 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2408 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2412 ASSERT_EFI_ERROR (Status
);
2418 Initializes variable write service after FVB was ready.
2420 @retval EFI_SUCCESS Function successfully executed.
2421 @retval Others Fail to initialize the variable service.
2425 VariableWriteServiceInitialize (
2430 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2433 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2434 VARIABLE_HEADER
*Variable
;
2437 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2438 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2441 // Check if the free area is really free.
2443 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
2444 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2447 // There must be something wrong in variable store, do reclaim operation.
2450 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2451 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2455 if (EFI_ERROR (Status
)) {
2464 // Flush the HOB variable to flash and invalidate HOB variable.
2466 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2468 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB
2470 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2471 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
2473 for ( Variable
= GetStartPointer (VariableStoreHeader
)
2474 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
2475 ; Variable
= GetNextVariablePtr (Variable
)
2477 ASSERT (Variable
->State
== VAR_ADDED
);
2478 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
2479 VariableData
= GetVariableDataPtr (Variable
);
2480 Status
= VariableServiceSetVariable (
2481 GetVariableNamePtr (Variable
),
2482 &Variable
->VendorGuid
,
2483 Variable
->Attributes
,
2487 ASSERT_EFI_ERROR (Status
);
2492 // Authenticated variable initialize.
2494 Status
= AutenticatedVariableServiceInitialize ();
2501 Initializes variable store area for non-volatile and volatile variable.
2503 @retval EFI_SUCCESS Function successfully executed.
2504 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2508 VariableCommonInitialize (
2513 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2514 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2515 VARIABLE_HEADER
*NextVariable
;
2516 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2517 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2518 UINT64 VariableStoreLength
;
2521 EFI_HOB_GUID_TYPE
*GuidHob
;
2524 // Allocate runtime memory for variable driver global structure.
2526 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2527 if (mVariableModuleGlobal
== NULL
) {
2528 return EFI_OUT_OF_RESOURCES
;
2531 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2534 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2535 // is stored with common variable in the same NV region. So the platform integrator should
2536 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2537 // PcdFlashNvStorageVariableSize.
2539 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2542 // Get HOB variable store.
2544 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
2545 if (GuidHob
!= NULL
) {
2546 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
2547 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
2548 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
2550 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
2555 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2557 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2558 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2559 if (VolatileVariableStore
== NULL
) {
2560 FreePool (mVariableModuleGlobal
);
2561 return EFI_OUT_OF_RESOURCES
;
2564 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2567 // Initialize Variable Specific Data.
2569 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2570 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2571 mVariableModuleGlobal
->FvbInstance
= NULL
;
2573 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiAuthenticatedVariableGuid
);
2574 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2575 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2576 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2577 VolatileVariableStore
->Reserved
= 0;
2578 VolatileVariableStore
->Reserved1
= 0;
2581 // Get non-volatile variable store.
2584 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2585 if (TempVariableStoreHeader
== 0) {
2586 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2588 VariableStoreBase
= TempVariableStoreHeader
+ \
2589 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2590 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2591 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2593 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2594 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2595 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2596 Status
= EFI_VOLUME_CORRUPTED
;
2597 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2600 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2603 // Parse non-volatile variable data and get last variable offset.
2605 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2606 while (IsValidVariableHeader (NextVariable
)) {
2607 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2608 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2609 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2611 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2614 NextVariable
= GetNextVariablePtr (NextVariable
);
2617 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2620 // Allocate runtime memory used for a memory copy of the FLASH region.
2621 // Keep the memory and the FLASH in sync as updates occur
2623 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2624 if (mNvVariableCache
== NULL
) {
2625 Status
= EFI_OUT_OF_RESOURCES
;
2628 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2629 Status
= EFI_SUCCESS
;
2632 if (EFI_ERROR (Status
)) {
2633 FreePool (mVariableModuleGlobal
);
2634 FreePool (VolatileVariableStore
);
2642 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2644 @param[in] Address The Flash address.
2645 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2646 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2650 GetFvbInfoByAddress (
2651 IN EFI_PHYSICAL_ADDRESS Address
,
2652 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2653 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2657 EFI_HANDLE
*HandleBuffer
;
2660 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2661 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2662 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2663 EFI_FVB_ATTRIBUTES_2 Attributes
;
2666 // Get all FVB handles.
2668 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2669 if (EFI_ERROR (Status
)) {
2670 return EFI_NOT_FOUND
;
2674 // Get the FVB to access variable store.
2677 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2678 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2679 if (EFI_ERROR (Status
)) {
2680 Status
= EFI_NOT_FOUND
;
2685 // Ensure this FVB protocol supported Write operation.
2687 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2688 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2693 // Compare the address and select the right one.
2695 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2696 if (EFI_ERROR (Status
)) {
2700 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2701 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
2702 if (FvbHandle
!= NULL
) {
2703 *FvbHandle
= HandleBuffer
[Index
];
2705 if (FvbProtocol
!= NULL
) {
2708 Status
= EFI_SUCCESS
;
2712 FreePool (HandleBuffer
);
2715 Status
= EFI_NOT_FOUND
;