3 The common variable operation routines shared by DXE_RUNTIME variable
4 module and DXE_SMM variable module.
6 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
22 /// Define a memory cache that improves the search performance for a variable.
24 VARIABLE_STORE_HEADER
*mNvVariableCache
= NULL
;
27 /// The memory entry used for variable statistics data.
29 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
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.
202 ASSERT (Fvb
!= NULL
);
203 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
204 ASSERT_EFI_ERROR (Status
);
206 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
208 // Data Pointer should point to the actual Address where data is to be
212 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
215 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
216 return EFI_INVALID_PARAMETER
;
220 // Data Pointer should point to the actual Address where data is to be
223 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
225 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
228 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
229 return EFI_INVALID_PARAMETER
;
233 // If Volatile Variable just do a simple mem copy.
235 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
240 // If we are here we are dealing with Non-Volatile Variables.
242 LinearOffset
= (UINTN
) FwVolHeader
;
243 CurrWritePtr
= (UINTN
) DataPtr
;
244 CurrWriteSize
= DataSize
;
248 if (CurrWritePtr
< LinearOffset
) {
249 return EFI_INVALID_PARAMETER
;
252 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
253 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
255 // Check to see if the Variable Writes are spanning through multiple
258 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
259 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
260 Status
= Fvb
->Write (
263 (UINTN
) (CurrWritePtr
- LinearOffset
),
269 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
270 Status
= Fvb
->Write (
273 (UINTN
) (CurrWritePtr
- LinearOffset
),
277 if (EFI_ERROR (Status
)) {
281 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
282 CurrBuffer
= CurrBuffer
+ Size
;
283 CurrWriteSize
= CurrWriteSize
- Size
;
287 LinearOffset
+= PtrBlockMapEntry
->Length
;
298 This code gets the current status of Variable Store.
300 @param VarStoreHeader Pointer to the Variable Store Header.
302 @retval EfiRaw Variable store status is raw.
303 @retval EfiValid Variable store status is valid.
304 @retval EfiInvalid Variable store status is invalid.
307 VARIABLE_STORE_STATUS
308 GetVariableStoreStatus (
309 IN VARIABLE_STORE_HEADER
*VarStoreHeader
312 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
) &&
313 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
314 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
318 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
319 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
320 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
321 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
322 VarStoreHeader
->Size
== 0xffffffff &&
323 VarStoreHeader
->Format
== 0xff &&
324 VarStoreHeader
->State
== 0xff
336 This code gets the size of name of variable.
338 @param Variable Pointer to the Variable Header.
340 @return UINTN Size of variable in bytes.
345 IN VARIABLE_HEADER
*Variable
348 if (Variable
->State
== (UINT8
) (-1) ||
349 Variable
->DataSize
== (UINT32
) (-1) ||
350 Variable
->NameSize
== (UINT32
) (-1) ||
351 Variable
->Attributes
== (UINT32
) (-1)) {
354 return (UINTN
) Variable
->NameSize
;
359 This code gets the size of variable data.
361 @param Variable Pointer to the Variable Header.
363 @return Size of variable in bytes.
368 IN VARIABLE_HEADER
*Variable
371 if (Variable
->State
== (UINT8
) (-1) ||
372 Variable
->DataSize
== (UINT32
) (-1) ||
373 Variable
->NameSize
== (UINT32
) (-1) ||
374 Variable
->Attributes
== (UINT32
) (-1)) {
377 return (UINTN
) Variable
->DataSize
;
382 This code gets the pointer to the variable name.
384 @param Variable Pointer to the Variable Header.
386 @return Pointer to Variable Name which is Unicode encoding.
391 IN VARIABLE_HEADER
*Variable
395 return (CHAR16
*) (Variable
+ 1);
400 This code gets the pointer to the variable data.
402 @param Variable Pointer to the Variable Header.
404 @return Pointer to Variable Data.
409 IN VARIABLE_HEADER
*Variable
415 // Be careful about pad size for alignment.
417 Value
= (UINTN
) GetVariableNamePtr (Variable
);
418 Value
+= NameSizeOfVariable (Variable
);
419 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
421 return (UINT8
*) Value
;
427 This code gets the pointer to the next variable header.
429 @param Variable Pointer to the Variable Header.
431 @return Pointer to next variable header.
436 IN VARIABLE_HEADER
*Variable
441 if (!IsValidVariableHeader (Variable
)) {
445 Value
= (UINTN
) GetVariableDataPtr (Variable
);
446 Value
+= DataSizeOfVariable (Variable
);
447 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
450 // Be careful about pad size for alignment.
452 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
457 Gets the pointer to the first variable header in given variable store area.
459 @param VarStoreHeader Pointer to the Variable Store Header.
461 @return Pointer to the first variable header.
466 IN VARIABLE_STORE_HEADER
*VarStoreHeader
470 // The end of variable store.
472 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
477 Gets the pointer to the end of the variable storage area.
479 This function gets pointer to the end of the variable storage
480 area, according to the input variable store header.
482 @param VarStoreHeader Pointer to the Variable Store Header.
484 @return Pointer to the end of the variable storage area.
489 IN VARIABLE_STORE_HEADER
*VarStoreHeader
493 // The end of variable store
495 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
501 Variable store garbage collection and reclaim operation.
503 @param VariableBase Base address of variable store.
504 @param LastVariableOffset Offset of last variable.
505 @param IsVolatile The variable store is volatile or not;
506 if it is non-volatile, need FTW.
507 @param UpdatingVariable Pointer to updating variable.
509 @return EFI_OUT_OF_RESOURCES
516 IN EFI_PHYSICAL_ADDRESS VariableBase
,
517 OUT UINTN
*LastVariableOffset
,
518 IN BOOLEAN IsVolatile
,
519 IN VARIABLE_HEADER
*UpdatingVariable
522 VARIABLE_HEADER
*Variable
;
523 VARIABLE_HEADER
*AddedVariable
;
524 VARIABLE_HEADER
*NextVariable
;
525 VARIABLE_HEADER
*NextAddedVariable
;
526 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
528 UINTN MaximumBufferSize
;
530 UINTN VariableNameSize
;
531 UINTN UpdatingVariableNameSize
;
538 CHAR16
*VariableNamePtr
;
539 CHAR16
*UpdatingVariableNamePtr
;
540 UINTN CommonVariableTotalSize
;
541 UINTN HwErrVariableTotalSize
;
543 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
545 CommonVariableTotalSize
= 0;
546 HwErrVariableTotalSize
= 0;
549 // Start Pointers for the variable.
551 Variable
= GetStartPointer (VariableStoreHeader
);
552 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
554 while (IsValidVariableHeader (Variable
)) {
555 NextVariable
= GetNextVariablePtr (Variable
);
556 if (Variable
->State
== VAR_ADDED
||
557 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
559 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
560 MaximumBufferSize
+= VariableSize
;
563 Variable
= NextVariable
;
567 // Reserve the 1 Bytes with Oxff to identify the
568 // end of the variable buffer.
570 MaximumBufferSize
+= 1;
571 ValidBuffer
= AllocatePool (MaximumBufferSize
);
572 if (ValidBuffer
== NULL
) {
573 return EFI_OUT_OF_RESOURCES
;
576 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
579 // Copy variable store header.
581 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
582 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
585 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
587 Variable
= GetStartPointer (VariableStoreHeader
);
588 while (IsValidVariableHeader (Variable
)) {
589 NextVariable
= GetNextVariablePtr (Variable
);
590 if (Variable
->State
== VAR_ADDED
) {
591 if (UpdatingVariable
!= NULL
) {
592 if (UpdatingVariable
== Variable
) {
593 Variable
= NextVariable
;
597 VariableNameSize
= NameSizeOfVariable(Variable
);
598 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
600 VariableNamePtr
= GetVariableNamePtr (Variable
);
601 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
602 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
603 VariableNameSize
== UpdatingVariableNameSize
&&
604 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
605 Variable
= NextVariable
;
609 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
610 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
611 CurrPtr
+= VariableSize
;
612 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
613 HwErrVariableTotalSize
+= VariableSize
;
614 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
615 CommonVariableTotalSize
+= VariableSize
;
618 Variable
= NextVariable
;
622 // Reinstall the variable being updated if it is not NULL.
624 if (UpdatingVariable
!= NULL
) {
625 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
626 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
627 CurrPtr
+= VariableSize
;
628 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
629 HwErrVariableTotalSize
+= VariableSize
;
630 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
631 CommonVariableTotalSize
+= VariableSize
;
636 // Reinstall all in delete transition variables.
638 Variable
= GetStartPointer (VariableStoreHeader
);
639 while (IsValidVariableHeader (Variable
)) {
640 NextVariable
= GetNextVariablePtr (Variable
);
641 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
644 // Buffer has cached all ADDED variable.
645 // Per IN_DELETED variable, we have to guarantee that
646 // no ADDED one in previous buffer.
650 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
651 while (IsValidVariableHeader (AddedVariable
)) {
652 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
653 NameSize
= NameSizeOfVariable (AddedVariable
);
654 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
655 NameSize
== NameSizeOfVariable (Variable
)
657 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
658 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
659 if (CompareMem (Point0
, Point1
, NameSizeOfVariable (AddedVariable
)) == 0) {
664 AddedVariable
= NextAddedVariable
;
668 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
670 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
671 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
672 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
673 CurrPtr
+= VariableSize
;
674 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
675 HwErrVariableTotalSize
+= VariableSize
;
676 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
677 CommonVariableTotalSize
+= VariableSize
;
682 Variable
= NextVariable
;
687 // If volatile variable store, just copy valid buffer.
689 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
690 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
691 Status
= EFI_SUCCESS
;
694 // If non-volatile variable store, perform FTW here.
696 Status
= FtwVariableSpace (
699 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
701 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
703 if (!EFI_ERROR (Status
)) {
704 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
706 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
707 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
710 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
711 while (IsValidVariableHeader (NextVariable
)) {
712 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
713 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
714 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
715 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
716 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
719 NextVariable
= GetNextVariablePtr (NextVariable
);
721 *LastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableBase
;
724 FreePool (ValidBuffer
);
730 Find the variable in the specified variable store.
732 @param VariableName Name of the variable to be found
733 @param VendorGuid Vendor GUID to be found.
734 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
735 check at runtime when searching variable.
736 @param PtrTrack Variable Track Pointer structure that contains Variable Information.
738 @retval EFI_SUCCESS Variable found successfully
739 @retval EFI_NOT_FOUND Variable not found
743 IN CHAR16
*VariableName
,
744 IN EFI_GUID
*VendorGuid
,
745 IN BOOLEAN IgnoreRtCheck
,
746 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
749 VARIABLE_HEADER
*InDeletedVariable
;
753 // Find the variable by walk through HOB, volatile and non-volatile variable store.
755 InDeletedVariable
= NULL
;
757 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
758 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
759 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
761 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
762 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
764 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
765 if (VariableName
[0] == 0) {
766 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
767 InDeletedVariable
= PtrTrack
->CurrPtr
;
772 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
773 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
775 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
776 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
777 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
778 InDeletedVariable
= PtrTrack
->CurrPtr
;
789 PtrTrack
->CurrPtr
= InDeletedVariable
;
790 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
795 Finds variable in storage blocks of volatile and non-volatile storage areas.
797 This code finds variable in storage blocks of volatile and non-volatile storage areas.
798 If VariableName is an empty string, then we just return the first
799 qualified variable without comparing VariableName and VendorGuid.
800 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
801 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
802 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
804 @param VariableName Name of the variable to be found.
805 @param VendorGuid Vendor GUID to be found.
806 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
807 including the range searched and the target position.
808 @param Global Pointer to VARIABLE_GLOBAL structure, including
809 base of volatile variable storage area, base of
810 NV variable storage area, and a lock.
811 @param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
812 check at runtime when searching variable.
814 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
816 @retval EFI_SUCCESS Variable successfully found.
817 @retval EFI_NOT_FOUND Variable not found
822 IN CHAR16
*VariableName
,
823 IN EFI_GUID
*VendorGuid
,
824 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
825 IN VARIABLE_GLOBAL
*Global
,
826 IN BOOLEAN IgnoreRtCheck
830 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
831 VARIABLE_STORE_TYPE Type
;
833 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
834 return EFI_INVALID_PARAMETER
;
838 // 0: Volatile, 1: HOB, 2: Non-Volatile.
839 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
840 // make use of this mapping to implement search algorithm.
842 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
843 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
844 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
847 // Find the variable by walk through HOB, volatile and non-volatile variable store.
849 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
850 if (VariableStoreHeader
[Type
] == NULL
) {
854 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
855 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
856 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
858 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
859 if (!EFI_ERROR (Status
)) {
863 return EFI_NOT_FOUND
;
867 Get index from supported language codes according to language string.
869 This code is used to get corresponding index in supported language codes. It can handle
870 RFC4646 and ISO639 language tags.
871 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
872 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
875 SupportedLang = "engfraengfra"
877 Iso639Language = TRUE
878 The return value is "0".
880 SupportedLang = "en;fr;en-US;fr-FR"
882 Iso639Language = FALSE
883 The return value is "3".
885 @param SupportedLang Platform supported language codes.
886 @param Lang Configured language.
887 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
889 @retval The index of language in the language codes.
893 GetIndexFromSupportedLangCodes(
894 IN CHAR8
*SupportedLang
,
896 IN BOOLEAN Iso639Language
901 UINTN LanguageLength
;
903 if (Iso639Language
) {
904 CompareLength
= ISO_639_2_ENTRY_SIZE
;
905 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
906 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
908 // Successfully find the index of Lang string in SupportedLang string.
910 Index
= Index
/ CompareLength
;
918 // Compare RFC4646 language code
921 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
923 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
925 // Skip ';' characters in SupportedLang
927 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
929 // Determine the length of the next language code in SupportedLang
931 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
933 if ((CompareLength
== LanguageLength
) &&
934 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
936 // Successfully find the index of Lang string in SupportedLang string.
947 Get language string from supported language codes according to index.
949 This code is used to get corresponding language strings in supported language codes. It can handle
950 RFC4646 and ISO639 language tags.
951 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
952 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
955 SupportedLang = "engfraengfra"
957 Iso639Language = TRUE
958 The return value is "fra".
960 SupportedLang = "en;fr;en-US;fr-FR"
962 Iso639Language = FALSE
963 The return value is "fr".
965 @param SupportedLang Platform supported language codes.
966 @param Index The index in supported language codes.
967 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
969 @retval The language string in the language codes.
973 GetLangFromSupportedLangCodes (
974 IN CHAR8
*SupportedLang
,
976 IN BOOLEAN Iso639Language
984 Supported
= SupportedLang
;
985 if (Iso639Language
) {
987 // According to the index of Lang string in SupportedLang string to get the language.
988 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
989 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
991 CompareLength
= ISO_639_2_ENTRY_SIZE
;
992 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
993 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
998 // Take semicolon as delimitation, sequentially traverse supported language codes.
1000 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1003 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1005 // Have completed the traverse, but not find corrsponding string.
1006 // This case is not allowed to happen.
1011 if (SubIndex
== Index
) {
1013 // According to the index of Lang string in SupportedLang string to get the language.
1014 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1015 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1017 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1018 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1023 // Skip ';' characters in Supported
1025 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1031 Returns a pointer to an allocated buffer that contains the best matching language
1032 from a set of supported languages.
1034 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1035 code types may not be mixed in a single call to this function. This function
1036 supports a variable argument list that allows the caller to pass in a prioritized
1037 list of language codes to test against all the language codes in SupportedLanguages.
1039 If SupportedLanguages is NULL, then ASSERT().
1041 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1042 contains a set of language codes in the format
1043 specified by Iso639Language.
1044 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1045 in ISO 639-2 format. If FALSE, then all language
1046 codes are assumed to be in RFC 4646 language format
1047 @param[in] ... A variable argument list that contains pointers to
1048 Null-terminated ASCII strings that contain one or more
1049 language codes in the format specified by Iso639Language.
1050 The first language code from each of these language
1051 code lists is used to determine if it is an exact or
1052 close match to any of the language codes in
1053 SupportedLanguages. Close matches only apply to RFC 4646
1054 language codes, and the matching algorithm from RFC 4647
1055 is used to determine if a close match is present. If
1056 an exact or close match is found, then the matching
1057 language code from SupportedLanguages is returned. If
1058 no matches are found, then the next variable argument
1059 parameter is evaluated. The variable argument list
1060 is terminated by a NULL.
1062 @retval NULL The best matching language could not be found in SupportedLanguages.
1063 @retval NULL There are not enough resources available to return the best matching
1065 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1066 language in SupportedLanguages.
1071 VariableGetBestLanguage (
1072 IN CONST CHAR8
*SupportedLanguages
,
1073 IN BOOLEAN Iso639Language
,
1079 UINTN CompareLength
;
1080 UINTN LanguageLength
;
1081 CONST CHAR8
*Supported
;
1084 ASSERT (SupportedLanguages
!= NULL
);
1086 VA_START (Args
, Iso639Language
);
1087 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1089 // Default to ISO 639-2 mode
1092 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1095 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1097 if (!Iso639Language
) {
1098 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1102 // Trim back the length of Language used until it is empty
1104 while (LanguageLength
> 0) {
1106 // Loop through all language codes in SupportedLanguages
1108 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1110 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1112 if (!Iso639Language
) {
1114 // Skip ';' characters in Supported
1116 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1118 // Determine the length of the next language code in Supported
1120 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1122 // If Language is longer than the Supported, then skip to the next language
1124 if (LanguageLength
> CompareLength
) {
1129 // See if the first LanguageLength characters in Supported match Language
1131 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1134 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1135 Buffer
[CompareLength
] = '\0';
1136 return CopyMem (Buffer
, Supported
, CompareLength
);
1140 if (Iso639Language
) {
1142 // If ISO 639 mode, then each language can only be tested once
1147 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1149 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1156 // No matches were found
1162 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1164 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1166 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1167 and are read-only. Therefore, in variable driver, only store the original value for other use.
1169 @param[in] VariableName Name of variable.
1171 @param[in] Data Variable data.
1173 @param[in] DataSize Size of data. 0 means delete.
1177 AutoUpdateLangVariable (
1178 IN CHAR16
*VariableName
,
1184 CHAR8
*BestPlatformLang
;
1188 VARIABLE_POINTER_TRACK Variable
;
1189 BOOLEAN SetLanguageCodes
;
1192 // Don't do updates for delete operation
1194 if (DataSize
== 0) {
1198 SetLanguageCodes
= FALSE
;
1200 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1202 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1208 SetLanguageCodes
= TRUE
;
1211 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1212 // Therefore, in variable driver, only store the original value for other use.
1214 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1215 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1217 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1218 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1221 // PlatformLang holds a single language from PlatformLangCodes,
1222 // so the size of PlatformLangCodes is enough for the PlatformLang.
1224 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1225 FreePool (mVariableModuleGlobal
->PlatformLang
);
1227 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1228 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1230 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1232 // LangCodes is a volatile variable, so it can not be updated at runtime.
1238 SetLanguageCodes
= TRUE
;
1241 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1242 // Therefore, in variable driver, only store the original value for other use.
1244 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1245 FreePool (mVariableModuleGlobal
->LangCodes
);
1247 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1248 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1251 if (SetLanguageCodes
1252 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1253 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1255 // Update Lang if PlatformLang is already set
1256 // Update PlatformLang if Lang is already set
1258 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1259 if (!EFI_ERROR (Status
)) {
1263 VariableName
= L
"PlatformLang";
1264 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1265 DataSize
= Variable
.CurrPtr
->DataSize
;
1267 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1268 if (!EFI_ERROR (Status
)) {
1270 // Update PlatformLang
1272 VariableName
= L
"Lang";
1273 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1274 DataSize
= Variable
.CurrPtr
->DataSize
;
1277 // Neither PlatformLang nor Lang is set, directly return
1285 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1287 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1289 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1291 // Update Lang when PlatformLangCodes/LangCodes were set.
1293 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1295 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1297 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1298 if (BestPlatformLang
!= NULL
) {
1300 // Get the corresponding index in language codes.
1302 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1305 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1307 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1310 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1312 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
, FALSE
);
1314 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1315 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, &Variable
);
1317 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1319 ASSERT_EFI_ERROR(Status
);
1323 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1325 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1327 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1329 // When setting Lang, firstly get most matched language string from supported language codes.
1331 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1332 if (BestLang
!= NULL
) {
1334 // Get the corresponding index in language codes.
1336 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1339 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1341 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1344 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1346 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1348 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1349 AsciiStrSize (BestPlatformLang
), Attributes
, &Variable
);
1351 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1352 ASSERT_EFI_ERROR (Status
);
1359 Update the variable region with Variable information. These are the same
1360 arguments as the EFI Variable services.
1362 @param[in] VariableName Name of variable.
1363 @param[in] VendorGuid Guid of variable.
1364 @param[in] Data Variable data.
1365 @param[in] DataSize Size of data. 0 means delete.
1366 @param[in] Attributes Attribues of the variable.
1367 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1369 @retval EFI_SUCCESS The update operation is success.
1370 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1375 IN CHAR16
*VariableName
,
1376 IN EFI_GUID
*VendorGuid
,
1379 IN UINT32 Attributes OPTIONAL
,
1380 IN VARIABLE_POINTER_TRACK
*CacheVariable
1384 VARIABLE_HEADER
*NextVariable
;
1386 UINTN NonVolatileVarableStoreSize
;
1387 UINTN VarNameOffset
;
1388 UINTN VarDataOffset
;
1392 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1395 VARIABLE_POINTER_TRACK
*Variable
;
1396 VARIABLE_POINTER_TRACK NvVariable
;
1397 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1400 if ((mVariableModuleGlobal
->FvbInstance
== NULL
) && ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0)) {
1402 // The FVB protocol is not ready. Trying to update NV variable prior to the installation
1403 // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
1405 return EFI_NOT_AVAILABLE_YET
;
1408 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1409 Variable
= CacheVariable
;
1412 // Update/Delete existing NV variable.
1413 // CacheVariable points to the variable in the memory copy of Flash area
1414 // Now let Variable points to the same variable in Flash area.
1416 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1417 Variable
= &NvVariable
;
1418 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1419 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1420 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1421 Variable
->Volatile
= FALSE
;
1424 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1427 if (Variable
->CurrPtr
!= NULL
) {
1429 // Update/Delete existing variable.
1433 // If AtRuntime and the variable is Volatile and Runtime Access,
1434 // the volatile is ReadOnly, and SetVariable should be aborted and
1435 // return EFI_WRITE_PROTECTED.
1437 if (Variable
->Volatile
) {
1438 Status
= EFI_WRITE_PROTECTED
;
1442 // Only variable that have NV|RT attributes can be updated/deleted in Runtime.
1444 if (((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0)) {
1445 Status
= EFI_INVALID_PARAMETER
;
1451 // Setting a data variable with no access, or zero DataSize attributes
1452 // causes it to be deleted.
1454 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1455 State
= Variable
->CurrPtr
->State
;
1456 State
&= VAR_DELETED
;
1458 Status
= UpdateVariableStore (
1459 &mVariableModuleGlobal
->VariableGlobal
,
1463 (UINTN
) &Variable
->CurrPtr
->State
,
1467 if (!EFI_ERROR (Status
)) {
1468 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1469 if (!Variable
->Volatile
) {
1470 CacheVariable
->CurrPtr
->State
= State
;
1476 // If the variable is marked valid, and the same data has been passed in,
1477 // then return to the caller immediately.
1479 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1480 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0)) {
1482 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1483 Status
= EFI_SUCCESS
;
1485 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1486 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1489 // Mark the old variable as in delete transition.
1491 State
= Variable
->CurrPtr
->State
;
1492 State
&= VAR_IN_DELETED_TRANSITION
;
1494 Status
= UpdateVariableStore (
1495 &mVariableModuleGlobal
->VariableGlobal
,
1499 (UINTN
) &Variable
->CurrPtr
->State
,
1503 if (EFI_ERROR (Status
)) {
1506 if (!Variable
->Volatile
) {
1507 CacheVariable
->CurrPtr
->State
= State
;
1512 // Not found existing variable. Create a new variable.
1516 // Make sure we are trying to create a new variable.
1517 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1519 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1520 Status
= EFI_NOT_FOUND
;
1525 // Only variable have NV|RT attribute can be created in Runtime.
1528 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1529 Status
= EFI_INVALID_PARAMETER
;
1535 // Function part - create a new variable and copy the data.
1536 // Both update a variable and create a variable will come here.
1539 // Tricky part: Use scratch data area at the end of volatile variable store
1540 // as a temporary storage.
1542 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1543 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1545 SetMem (NextVariable
, ScratchSize
, 0xff);
1547 NextVariable
->StartId
= VARIABLE_DATA
;
1548 NextVariable
->Attributes
= Attributes
;
1550 // NextVariable->State = VAR_ADDED;
1552 NextVariable
->Reserved
= 0;
1553 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1554 VarNameSize
= StrSize (VariableName
);
1556 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1560 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1562 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1566 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1568 // There will be pad bytes after Data, the NextVariable->NameSize and
1569 // NextVariable->DataSize should not include pad size so that variable
1570 // service can get actual size in GetVariable.
1572 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1573 NextVariable
->DataSize
= (UINT32
)DataSize
;
1576 // The actual size of the variable that stores in storage should
1577 // include pad size.
1579 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1580 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1582 // Create a nonvolatile variable.
1585 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1586 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1587 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1588 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1589 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1591 Status
= EFI_OUT_OF_RESOURCES
;
1595 // Perform garbage collection & reclaim operation.
1597 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1598 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
->CurrPtr
);
1599 if (EFI_ERROR (Status
)) {
1603 // If still no enough space, return out of resources.
1605 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1606 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1607 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1608 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1609 Status
= EFI_OUT_OF_RESOURCES
;
1616 // 1. Write variable header
1617 // 2. Set variable state to header valid
1618 // 3. Write variable data
1619 // 4. Set variable state to valid
1624 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1625 Status
= UpdateVariableStore (
1626 &mVariableModuleGlobal
->VariableGlobal
,
1630 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1631 sizeof (VARIABLE_HEADER
),
1632 (UINT8
*) NextVariable
1635 if (EFI_ERROR (Status
)) {
1642 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1643 Status
= UpdateVariableStore (
1644 &mVariableModuleGlobal
->VariableGlobal
,
1648 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1650 &NextVariable
->State
1653 if (EFI_ERROR (Status
)) {
1659 Status
= UpdateVariableStore (
1660 &mVariableModuleGlobal
->VariableGlobal
,
1664 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1665 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1666 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1669 if (EFI_ERROR (Status
)) {
1675 NextVariable
->State
= VAR_ADDED
;
1676 Status
= UpdateVariableStore (
1677 &mVariableModuleGlobal
->VariableGlobal
,
1681 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1683 &NextVariable
->State
1686 if (EFI_ERROR (Status
)) {
1690 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1692 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1693 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1695 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1698 // update the memory copy of Flash region.
1700 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1703 // Create a volatile variable.
1707 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1708 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1710 // Perform garbage collection & reclaim operation.
1712 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1713 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
->CurrPtr
);
1714 if (EFI_ERROR (Status
)) {
1718 // If still no enough space, return out of resources.
1720 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1721 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1723 Status
= EFI_OUT_OF_RESOURCES
;
1729 NextVariable
->State
= VAR_ADDED
;
1730 Status
= UpdateVariableStore (
1731 &mVariableModuleGlobal
->VariableGlobal
,
1735 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1737 (UINT8
*) NextVariable
1740 if (EFI_ERROR (Status
)) {
1744 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1748 // Mark the old variable as deleted.
1750 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1751 State
= Variable
->CurrPtr
->State
;
1752 State
&= VAR_DELETED
;
1754 Status
= UpdateVariableStore (
1755 &mVariableModuleGlobal
->VariableGlobal
,
1759 (UINTN
) &Variable
->CurrPtr
->State
,
1763 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1764 CacheVariable
->CurrPtr
->State
= State
;
1768 if (!EFI_ERROR (Status
)) {
1769 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1777 Check if a Unicode character is a hexadecimal character.
1779 This function checks if a Unicode character is a
1780 hexadecimal character. The valid hexadecimal character is
1781 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
1784 @param Char The character to check against.
1786 @retval TRUE If the Char is a hexadecmial character.
1787 @retval FALSE If the Char is not a hexadecmial character.
1792 IsHexaDecimalDigitCharacter (
1796 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
1801 This code checks if variable is hardware error record variable or not.
1803 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
1804 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
1806 @param VariableName Pointer to variable name.
1807 @param VendorGuid Variable Vendor Guid.
1809 @retval TRUE Variable is hardware error record variable.
1810 @retval FALSE Variable is not hardware error record variable.
1815 IsHwErrRecVariable (
1816 IN CHAR16
*VariableName
,
1817 IN EFI_GUID
*VendorGuid
1820 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
1821 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
1822 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
1823 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
1824 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
1825 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
1826 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
1835 This code finds variable in storage blocks (Volatile or Non-Volatile).
1837 @param VariableName Name of Variable to be found.
1838 @param VendorGuid Variable vendor GUID.
1839 @param Attributes Attribute value of the variable found.
1840 @param DataSize Size of Data found. If size is less than the
1841 data, this value contains the required size.
1842 @param Data Data pointer.
1844 @return EFI_INVALID_PARAMETER Invalid parameter.
1845 @return EFI_SUCCESS Find the specified variable.
1846 @return EFI_NOT_FOUND Not found.
1847 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1852 VariableServiceGetVariable (
1853 IN CHAR16
*VariableName
,
1854 IN EFI_GUID
*VendorGuid
,
1855 OUT UINT32
*Attributes OPTIONAL
,
1856 IN OUT UINTN
*DataSize
,
1861 VARIABLE_POINTER_TRACK Variable
;
1864 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1865 return EFI_INVALID_PARAMETER
;
1868 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1870 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1871 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1878 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1879 ASSERT (VarDataSize
!= 0);
1881 if (*DataSize
>= VarDataSize
) {
1883 Status
= EFI_INVALID_PARAMETER
;
1887 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1888 if (Attributes
!= NULL
) {
1889 *Attributes
= Variable
.CurrPtr
->Attributes
;
1892 *DataSize
= VarDataSize
;
1893 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1895 Status
= EFI_SUCCESS
;
1898 *DataSize
= VarDataSize
;
1899 Status
= EFI_BUFFER_TOO_SMALL
;
1904 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1912 This code Finds the Next available variable.
1914 @param VariableNameSize Size of the variable name.
1915 @param VariableName Pointer to variable name.
1916 @param VendorGuid Variable Vendor Guid.
1918 @return EFI_INVALID_PARAMETER Invalid parameter.
1919 @return EFI_SUCCESS Find the specified variable.
1920 @return EFI_NOT_FOUND Not found.
1921 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1926 VariableServiceGetNextVariableName (
1927 IN OUT UINTN
*VariableNameSize
,
1928 IN OUT CHAR16
*VariableName
,
1929 IN OUT EFI_GUID
*VendorGuid
1932 VARIABLE_STORE_TYPE Type
;
1933 VARIABLE_POINTER_TRACK Variable
;
1934 VARIABLE_POINTER_TRACK VariableInHob
;
1937 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
1939 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1940 return EFI_INVALID_PARAMETER
;
1943 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1945 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1946 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1950 if (VariableName
[0] != 0) {
1952 // If variable name is not NULL, get next variable.
1954 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1958 // 0: Volatile, 1: HOB, 2: Non-Volatile.
1959 // The index and attributes mapping must be kept in this order as FindVariable
1960 // makes use of this mapping to implement search algorithm.
1962 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
1963 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
1964 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
1968 // Switch from Volatile to HOB, to Non-Volatile.
1970 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
1971 (Variable
.CurrPtr
== NULL
) ||
1972 !IsValidVariableHeader (Variable
.CurrPtr
)
1975 // Find current storage index
1977 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
1978 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
1982 ASSERT (Type
< VariableStoreTypeMax
);
1984 // Switch to next storage
1986 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
1987 if (VariableStoreHeader
[Type
] != NULL
) {
1992 // Capture the case that
1993 // 1. current storage is the last one, or
1994 // 2. no further storage
1996 if (Type
== VariableStoreTypeMax
) {
1997 Status
= EFI_NOT_FOUND
;
2000 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2001 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2002 Variable
.CurrPtr
= Variable
.StartPtr
;
2006 // Variable is found
2008 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
2009 if ((AtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
2012 // Don't return NV variable when HOB overrides it
2014 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2015 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2017 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2018 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2019 Status
= FindVariableEx (
2020 GetVariableNamePtr (Variable
.CurrPtr
),
2021 &Variable
.CurrPtr
->VendorGuid
,
2025 if (!EFI_ERROR (Status
)) {
2026 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2031 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2032 ASSERT (VarNameSize
!= 0);
2034 if (VarNameSize
<= *VariableNameSize
) {
2035 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2036 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2037 Status
= EFI_SUCCESS
;
2039 Status
= EFI_BUFFER_TOO_SMALL
;
2042 *VariableNameSize
= VarNameSize
;
2047 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2051 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2057 This code sets variable in storage blocks (Volatile or Non-Volatile).
2059 @param VariableName Name of Variable to be found.
2060 @param VendorGuid Variable vendor GUID.
2061 @param Attributes Attribute value of the variable found
2062 @param DataSize Size of Data found. If size is less than the
2063 data, this value contains the required size.
2064 @param Data Data pointer.
2066 @return EFI_INVALID_PARAMETER Invalid parameter.
2067 @return EFI_SUCCESS Set successfully.
2068 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2069 @return EFI_NOT_FOUND Not found.
2070 @return EFI_WRITE_PROTECTED Variable is read-only.
2075 VariableServiceSetVariable (
2076 IN CHAR16
*VariableName
,
2077 IN EFI_GUID
*VendorGuid
,
2078 IN UINT32 Attributes
,
2083 VARIABLE_POINTER_TRACK Variable
;
2085 VARIABLE_HEADER
*NextVariable
;
2086 EFI_PHYSICAL_ADDRESS Point
;
2089 // Check input parameters.
2091 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2092 return EFI_INVALID_PARAMETER
;
2095 if (DataSize
!= 0 && Data
== NULL
) {
2096 return EFI_INVALID_PARAMETER
;
2100 // Not support authenticated variable write yet.
2102 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2103 return EFI_INVALID_PARAMETER
;
2107 // Make sure if runtime bit is set, boot service bit is set also.
2109 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2110 return EFI_INVALID_PARAMETER
;
2114 // The size of the VariableName, including the Unicode Null in bytes plus
2115 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2116 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2118 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2119 if ((DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
2120 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
2121 return EFI_INVALID_PARAMETER
;
2123 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2124 return EFI_INVALID_PARAMETER
;
2128 // The size of the VariableName, including the Unicode Null in bytes plus
2129 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2131 if ((DataSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2132 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + DataSize
> PcdGet32 (PcdMaxVariableSize
))) {
2133 return EFI_INVALID_PARAMETER
;
2139 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence
2140 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.
2142 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, L
"HwErrRecSupport") == 0)) {
2143 return EFI_WRITE_PROTECTED
;
2147 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2150 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2152 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2153 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2155 // Parse non-volatile variable data and get last variable offset.
2157 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2158 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2159 && IsValidVariableHeader (NextVariable
)) {
2160 NextVariable
= GetNextVariablePtr (NextVariable
);
2162 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2166 // Check whether the input variable is already existed.
2168 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2169 if (!EFI_ERROR (Status
)) {
2170 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2171 return EFI_WRITE_PROTECTED
;
2176 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2178 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2180 Status
= UpdateVariable (VariableName
, VendorGuid
, Data
, DataSize
, Attributes
, &Variable
);
2182 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2183 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2190 This code returns information about the EFI variables.
2192 @param Attributes Attributes bitmask to specify the type of variables
2193 on which to return information.
2194 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2195 for the EFI variables associated with the attributes specified.
2196 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2197 for EFI variables associated with the attributes specified.
2198 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2199 associated with the attributes specified.
2201 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2202 @return EFI_SUCCESS Query successfully.
2203 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2208 VariableServiceQueryVariableInfo (
2209 IN UINT32 Attributes
,
2210 OUT UINT64
*MaximumVariableStorageSize
,
2211 OUT UINT64
*RemainingVariableStorageSize
,
2212 OUT UINT64
*MaximumVariableSize
2215 VARIABLE_HEADER
*Variable
;
2216 VARIABLE_HEADER
*NextVariable
;
2217 UINT64 VariableSize
;
2218 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2219 UINT64 CommonVariableTotalSize
;
2220 UINT64 HwErrVariableTotalSize
;
2222 CommonVariableTotalSize
= 0;
2223 HwErrVariableTotalSize
= 0;
2225 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2226 return EFI_INVALID_PARAMETER
;
2229 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2231 // Make sure the Attributes combination is supported by the platform.
2233 return EFI_UNSUPPORTED
;
2234 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2236 // Make sure if runtime bit is set, boot service bit is set also.
2238 return EFI_INVALID_PARAMETER
;
2239 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2241 // Make sure RT Attribute is set if we are in Runtime phase.
2243 return EFI_INVALID_PARAMETER
;
2244 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2246 // Make sure Hw Attribute is set with NV.
2248 return EFI_INVALID_PARAMETER
;
2249 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
2251 // Not support authentiated variable write yet.
2253 return EFI_UNSUPPORTED
;
2256 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2258 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2260 // Query is Volatile related.
2262 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2265 // Query is Non-Volatile related.
2267 VariableStoreHeader
= mNvVariableCache
;
2271 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2272 // with the storage size (excluding the storage header size).
2274 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2277 // Harware error record variable needs larger size.
2279 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2280 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2281 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2283 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2284 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2285 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2289 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2291 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2295 // Point to the starting address of the variables.
2297 Variable
= GetStartPointer (VariableStoreHeader
);
2300 // Now walk through the related variable store.
2302 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2303 NextVariable
= GetNextVariablePtr (Variable
);
2304 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2308 // We don't take the state of the variables in mind
2309 // when calculating RemainingVariableStorageSize,
2310 // since the space occupied by variables not marked with
2311 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2313 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2314 HwErrVariableTotalSize
+= VariableSize
;
2316 CommonVariableTotalSize
+= VariableSize
;
2320 // Only care about Variables with State VAR_ADDED, because
2321 // the space not marked as VAR_ADDED is reclaimable now.
2323 if (Variable
->State
== VAR_ADDED
) {
2324 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2325 HwErrVariableTotalSize
+= VariableSize
;
2327 CommonVariableTotalSize
+= VariableSize
;
2333 // Go to the next one.
2335 Variable
= NextVariable
;
2338 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2339 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2341 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2344 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2345 *MaximumVariableSize
= 0;
2346 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2347 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2350 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2356 This function reclaims variable storage if free size is below the threshold.
2365 UINTN CommonVariableSpace
;
2366 UINTN RemainingCommonVariableSpace
;
2367 UINTN RemainingHwErrVariableSpace
;
2369 Status
= EFI_SUCCESS
;
2371 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2373 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2375 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2377 // Check if the free area is blow a threshold.
2379 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2380 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2381 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2383 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2384 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2388 ASSERT_EFI_ERROR (Status
);
2394 Initializes variable write service after FVB was ready.
2396 @retval EFI_SUCCESS Function successfully executed.
2397 @retval Others Fail to initialize the variable service.
2401 VariableWriteServiceInitialize (
2406 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2409 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2410 VARIABLE_HEADER
*Variable
;
2413 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2414 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2417 // Check if the free area is really free.
2419 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
2420 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2423 // There must be something wrong in variable store, do reclaim operation.
2426 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2427 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2431 if (EFI_ERROR (Status
)) {
2439 // Flush the HOB variable to flash and invalidate HOB variable.
2441 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2443 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB
2445 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2446 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
2448 for ( Variable
= GetStartPointer (VariableStoreHeader
)
2449 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
2450 ; Variable
= GetNextVariablePtr (Variable
)
2452 ASSERT (Variable
->State
== VAR_ADDED
);
2453 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
2454 VariableData
= GetVariableDataPtr (Variable
);
2455 Status
= VariableServiceSetVariable (
2456 GetVariableNamePtr (Variable
),
2457 &Variable
->VendorGuid
,
2458 Variable
->Attributes
,
2462 ASSERT_EFI_ERROR (Status
);
2470 Initializes variable store area for non-volatile and volatile variable.
2472 @retval EFI_SUCCESS Function successfully executed.
2473 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2477 VariableCommonInitialize (
2482 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2483 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2484 VARIABLE_HEADER
*NextVariable
;
2485 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2486 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2487 UINT64 VariableStoreLength
;
2490 EFI_HOB_GUID_TYPE
*GuidHob
;
2493 // Allocate runtime memory for variable driver global structure.
2495 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2496 if (mVariableModuleGlobal
== NULL
) {
2497 return EFI_OUT_OF_RESOURCES
;
2500 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2503 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2504 // is stored with common variable in the same NV region. So the platform integrator should
2505 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2506 // PcdFlashNvStorageVariableSize.
2508 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2511 // Get HOB variable store.
2513 GuidHob
= GetFirstGuidHob (&gEfiVariableGuid
);
2514 if (GuidHob
!= NULL
) {
2515 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
2516 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
2517 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
2519 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
2524 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2526 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2527 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2528 if (VolatileVariableStore
== NULL
) {
2529 FreePool (mVariableModuleGlobal
);
2530 return EFI_OUT_OF_RESOURCES
;
2533 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2536 // Initialize Variable Specific Data.
2538 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2539 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2540 mVariableModuleGlobal
->FvbInstance
= NULL
;
2542 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiVariableGuid
);
2543 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2544 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2545 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2546 VolatileVariableStore
->Reserved
= 0;
2547 VolatileVariableStore
->Reserved1
= 0;
2550 // Get non-volatile variable store.
2553 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2554 if (TempVariableStoreHeader
== 0) {
2555 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2559 // Check if the Firmware Volume is not corrupted
2561 if ((((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->Signature
!= EFI_FVH_SIGNATURE
) ||
2562 (!CompareGuid (&gEfiSystemNvDataFvGuid
, &((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->FileSystemGuid
))) {
2563 Status
= EFI_VOLUME_CORRUPTED
;
2564 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
2568 VariableStoreBase
= TempVariableStoreHeader
+ \
2569 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2570 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2571 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2573 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2574 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2575 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2576 Status
= EFI_VOLUME_CORRUPTED
;
2577 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2580 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2583 // Parse non-volatile variable data and get last variable offset.
2585 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2586 while (IsValidVariableHeader (NextVariable
)) {
2587 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2588 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2589 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2591 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2594 NextVariable
= GetNextVariablePtr (NextVariable
);
2597 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2600 // Allocate runtime memory used for a memory copy of the FLASH region.
2601 // Keep the memory and the FLASH in sync as updates occur
2603 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2604 if (mNvVariableCache
== NULL
) {
2605 Status
= EFI_OUT_OF_RESOURCES
;
2608 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2609 Status
= EFI_SUCCESS
;
2612 if (EFI_ERROR (Status
)) {
2613 FreePool (mVariableModuleGlobal
);
2614 FreePool (VolatileVariableStore
);
2622 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2624 @param[in] Address The Flash address.
2625 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2626 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2630 GetFvbInfoByAddress (
2631 IN EFI_PHYSICAL_ADDRESS Address
,
2632 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2633 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2637 EFI_HANDLE
*HandleBuffer
;
2640 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2641 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2642 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2643 EFI_FVB_ATTRIBUTES_2 Attributes
;
2646 // Get all FVB handles.
2648 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2649 if (EFI_ERROR (Status
)) {
2650 return EFI_NOT_FOUND
;
2654 // Get the FVB to access variable store.
2657 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2658 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2659 if (EFI_ERROR (Status
)) {
2660 Status
= EFI_NOT_FOUND
;
2665 // Ensure this FVB protocol supported Write operation.
2667 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2668 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2673 // Compare the address and select the right one.
2675 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2676 if (EFI_ERROR (Status
)) {
2680 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2681 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
2682 if (FvbHandle
!= NULL
) {
2683 *FvbHandle
= HandleBuffer
[Index
];
2685 if (FvbProtocol
!= NULL
) {
2688 Status
= EFI_SUCCESS
;
2692 FreePool (HandleBuffer
);
2695 Status
= EFI_NOT_FOUND
;