2 The common variable operation routines shared by DXE_RUNTIME variable
3 module and DXE_SMM variable module.
5 Caution: This module requires additional review when modified.
6 This driver will have external input - variable data. They may be input in SMM mode.
7 This external input must be validated carefully to avoid security issue like
8 buffer overflow, integer overflow.
10 VariableServiceGetNextVariableName () and VariableServiceQueryVariableInfo() are external API.
11 They need check input parameter.
13 VariableServiceGetVariable() and VariableServiceSetVariable() are external API
14 to receive datasize and data buffer. The size should be checked carefully.
16 VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,
17 integer overflow. It should also check attribute to avoid authentication bypass.
19 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
20 This program and the accompanying materials
21 are licensed and made available under the terms and conditions of the BSD License
22 which accompanies this distribution. The full text of the license may be found at
23 http://opensource.org/licenses/bsd-license.php
25 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
26 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
31 #include "AuthService.h"
33 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
36 /// Define a memory cache that improves the search performance for a variable.
38 VARIABLE_STORE_HEADER
*mNvVariableCache
= NULL
;
41 /// The memory entry used for variable statistics data.
43 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
47 Routine used to track statistical information about variable usage.
48 The data is stored in the EFI system table so it can be accessed later.
49 VariableInfo.efi can dump out the table. Only Boot Services variable
50 accesses are tracked by this code. The PcdVariableCollectStatistics
51 build flag controls if this feature is enabled.
53 A read that hits in the cache will have Read and Cache true for
54 the transaction. Data is allocated by this routine, but never
57 @param[in] VariableName Name of the Variable to track.
58 @param[in] VendorGuid Guid of the Variable to track.
59 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
60 @param[in] Read TRUE if GetVariable() was called.
61 @param[in] Write TRUE if SetVariable() was called.
62 @param[in] Delete TRUE if deleted via SetVariable().
63 @param[in] Cache TRUE for a cache hit.
68 IN CHAR16
*VariableName
,
69 IN EFI_GUID
*VendorGuid
,
77 VARIABLE_INFO_ENTRY
*Entry
;
79 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
82 // Don't collect statistics at runtime.
86 if (gVariableInfo
== NULL
) {
88 // On the first call allocate a entry and place a pointer to it in
89 // the EFI System Table.
91 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
92 ASSERT (gVariableInfo
!= NULL
);
94 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
95 gVariableInfo
->Name
= AllocatePool (StrSize (VariableName
));
96 ASSERT (gVariableInfo
->Name
!= NULL
);
97 StrCpy (gVariableInfo
->Name
, VariableName
);
98 gVariableInfo
->Volatile
= Volatile
;
102 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
103 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
104 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
112 Entry
->DeleteCount
++;
122 if (Entry
->Next
== NULL
) {
124 // If the entry is not in the table add it.
125 // Next iteration of the loop will fill in the data.
127 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
128 ASSERT (Entry
->Next
!= NULL
);
130 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
131 Entry
->Next
->Name
= AllocatePool (StrSize (VariableName
));
132 ASSERT (Entry
->Next
->Name
!= NULL
);
133 StrCpy (Entry
->Next
->Name
, VariableName
);
134 Entry
->Next
->Volatile
= Volatile
;
144 This code checks if variable header is valid or not.
146 @param Variable Pointer to the Variable Header.
148 @retval TRUE Variable header is valid.
149 @retval FALSE Variable header is not valid.
153 IsValidVariableHeader (
154 IN VARIABLE_HEADER
*Variable
157 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
167 This function writes data to the FWH at the correct LBA even if the LBAs
170 @param Global Pointer to VARAIBLE_GLOBAL structure.
171 @param Volatile Point out the Variable is Volatile or Non-Volatile.
172 @param SetByIndex TRUE if target pointer is given as index.
173 FALSE if target pointer is absolute.
174 @param Fvb Pointer to the writable FVB protocol.
175 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
177 @param DataSize Size of data to be written.
178 @param Buffer Pointer to the buffer from which data is written.
180 @retval EFI_INVALID_PARAMETER Parameters not valid.
181 @retval EFI_SUCCESS Variable store successfully updated.
185 UpdateVariableStore (
186 IN VARIABLE_GLOBAL
*Global
,
188 IN BOOLEAN SetByIndex
,
189 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
190 IN UINTN DataPtrIndex
,
195 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
203 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
204 VARIABLE_STORE_HEADER
*VolatileBase
;
205 EFI_PHYSICAL_ADDRESS FvVolHdr
;
206 EFI_PHYSICAL_ADDRESS DataPtr
;
210 DataPtr
= DataPtrIndex
;
213 // Check if the Data is Volatile.
217 return EFI_INVALID_PARAMETER
;
219 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
220 ASSERT_EFI_ERROR (Status
);
222 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
224 // Data Pointer should point to the actual Address where data is to be
228 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
231 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
232 return EFI_INVALID_PARAMETER
;
236 // Data Pointer should point to the actual Address where data is to be
239 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
241 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
244 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
245 return EFI_INVALID_PARAMETER
;
249 // If Volatile Variable just do a simple mem copy.
251 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
256 // If we are here we are dealing with Non-Volatile Variables.
258 LinearOffset
= (UINTN
) FwVolHeader
;
259 CurrWritePtr
= (UINTN
) DataPtr
;
260 CurrWriteSize
= DataSize
;
264 if (CurrWritePtr
< LinearOffset
) {
265 return EFI_INVALID_PARAMETER
;
268 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
269 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
271 // Check to see if the Variable Writes are spanning through multiple
274 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
275 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
276 Status
= Fvb
->Write (
279 (UINTN
) (CurrWritePtr
- LinearOffset
),
285 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
286 Status
= Fvb
->Write (
289 (UINTN
) (CurrWritePtr
- LinearOffset
),
293 if (EFI_ERROR (Status
)) {
297 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
298 CurrBuffer
= CurrBuffer
+ Size
;
299 CurrWriteSize
= CurrWriteSize
- Size
;
303 LinearOffset
+= PtrBlockMapEntry
->Length
;
314 This code gets the current status of Variable Store.
316 @param VarStoreHeader Pointer to the Variable Store Header.
318 @retval EfiRaw Variable store status is raw.
319 @retval EfiValid Variable store status is valid.
320 @retval EfiInvalid Variable store status is invalid.
323 VARIABLE_STORE_STATUS
324 GetVariableStoreStatus (
325 IN VARIABLE_STORE_HEADER
*VarStoreHeader
328 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) &&
329 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
330 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
334 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
335 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
336 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
337 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
338 VarStoreHeader
->Size
== 0xffffffff &&
339 VarStoreHeader
->Format
== 0xff &&
340 VarStoreHeader
->State
== 0xff
352 This code gets the size of name of variable.
354 @param Variable Pointer to the Variable Header.
356 @return UINTN Size of variable in bytes.
361 IN VARIABLE_HEADER
*Variable
364 if (Variable
->State
== (UINT8
) (-1) ||
365 Variable
->DataSize
== (UINT32
) (-1) ||
366 Variable
->NameSize
== (UINT32
) (-1) ||
367 Variable
->Attributes
== (UINT32
) (-1)) {
370 return (UINTN
) Variable
->NameSize
;
375 This code gets the size of variable data.
377 @param Variable Pointer to the Variable Header.
379 @return Size of variable in bytes.
384 IN VARIABLE_HEADER
*Variable
387 if (Variable
->State
== (UINT8
) (-1) ||
388 Variable
->DataSize
== (UINT32
) (-1) ||
389 Variable
->NameSize
== (UINT32
) (-1) ||
390 Variable
->Attributes
== (UINT32
) (-1)) {
393 return (UINTN
) Variable
->DataSize
;
398 This code gets the pointer to the variable name.
400 @param Variable Pointer to the Variable Header.
402 @return Pointer to Variable Name which is Unicode encoding.
407 IN VARIABLE_HEADER
*Variable
411 return (CHAR16
*) (Variable
+ 1);
416 This code gets the pointer to the variable data.
418 @param Variable Pointer to the Variable Header.
420 @return Pointer to Variable Data.
425 IN VARIABLE_HEADER
*Variable
431 // Be careful about pad size for alignment.
433 Value
= (UINTN
) GetVariableNamePtr (Variable
);
434 Value
+= NameSizeOfVariable (Variable
);
435 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
437 return (UINT8
*) Value
;
443 This code gets the pointer to the next variable header.
445 @param Variable Pointer to the Variable Header.
447 @return Pointer to next variable header.
452 IN VARIABLE_HEADER
*Variable
457 if (!IsValidVariableHeader (Variable
)) {
461 Value
= (UINTN
) GetVariableDataPtr (Variable
);
462 Value
+= DataSizeOfVariable (Variable
);
463 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
466 // Be careful about pad size for alignment.
468 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
473 Gets the pointer to the first variable header in given variable store area.
475 @param VarStoreHeader Pointer to the Variable Store Header.
477 @return Pointer to the first variable header.
482 IN VARIABLE_STORE_HEADER
*VarStoreHeader
486 // The end of variable store.
488 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
493 Gets the pointer to the end of the variable storage area.
495 This function gets pointer to the end of the variable storage
496 area, according to the input variable store header.
498 @param VarStoreHeader Pointer to the Variable Store Header.
500 @return Pointer to the end of the variable storage area.
505 IN VARIABLE_STORE_HEADER
*VarStoreHeader
509 // The end of variable store
511 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
517 Variable store garbage collection and reclaim operation.
519 @param VariableBase Base address of variable store.
520 @param LastVariableOffset Offset of last variable.
521 @param IsVolatile The variable store is volatile or not;
522 if it is non-volatile, need FTW.
523 @param UpdatingVariable Pointer to updating variable.
525 @return EFI_OUT_OF_RESOURCES
532 IN EFI_PHYSICAL_ADDRESS VariableBase
,
533 OUT UINTN
*LastVariableOffset
,
534 IN BOOLEAN IsVolatile
,
535 IN VARIABLE_HEADER
*UpdatingVariable
538 VARIABLE_HEADER
*Variable
;
539 VARIABLE_HEADER
*AddedVariable
;
540 VARIABLE_HEADER
*NextVariable
;
541 VARIABLE_HEADER
*NextAddedVariable
;
542 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
544 UINTN MaximumBufferSize
;
546 UINTN VariableNameSize
;
547 UINTN UpdatingVariableNameSize
;
554 CHAR16
*VariableNamePtr
;
555 CHAR16
*UpdatingVariableNamePtr
;
556 UINTN CommonVariableTotalSize
;
557 UINTN HwErrVariableTotalSize
;
559 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
561 CommonVariableTotalSize
= 0;
562 HwErrVariableTotalSize
= 0;
565 // Start Pointers for the variable.
567 Variable
= GetStartPointer (VariableStoreHeader
);
568 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
570 while (IsValidVariableHeader (Variable
)) {
571 NextVariable
= GetNextVariablePtr (Variable
);
572 if (Variable
->State
== VAR_ADDED
||
573 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
575 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
576 MaximumBufferSize
+= VariableSize
;
579 Variable
= NextVariable
;
583 // Reserve the 1 Bytes with Oxff to identify the
584 // end of the variable buffer.
586 MaximumBufferSize
+= 1;
587 ValidBuffer
= AllocatePool (MaximumBufferSize
);
588 if (ValidBuffer
== NULL
) {
589 return EFI_OUT_OF_RESOURCES
;
592 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
595 // Copy variable store header.
597 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
598 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
601 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
603 Variable
= GetStartPointer (VariableStoreHeader
);
604 while (IsValidVariableHeader (Variable
)) {
605 NextVariable
= GetNextVariablePtr (Variable
);
606 if (Variable
->State
== VAR_ADDED
) {
607 if (UpdatingVariable
!= NULL
) {
608 if (UpdatingVariable
== Variable
) {
609 Variable
= NextVariable
;
613 VariableNameSize
= NameSizeOfVariable(Variable
);
614 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
616 VariableNamePtr
= GetVariableNamePtr (Variable
);
617 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
618 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
619 VariableNameSize
== UpdatingVariableNameSize
&&
620 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
621 Variable
= NextVariable
;
625 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
626 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
627 CurrPtr
+= VariableSize
;
628 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
629 HwErrVariableTotalSize
+= VariableSize
;
630 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
631 CommonVariableTotalSize
+= VariableSize
;
634 Variable
= NextVariable
;
638 // Reinstall the variable being updated if it is not NULL.
640 if (UpdatingVariable
!= NULL
) {
641 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
642 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
643 CurrPtr
+= VariableSize
;
644 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
645 HwErrVariableTotalSize
+= VariableSize
;
646 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
647 CommonVariableTotalSize
+= VariableSize
;
652 // Reinstall all in delete transition variables.
654 Variable
= GetStartPointer (VariableStoreHeader
);
655 while (IsValidVariableHeader (Variable
)) {
656 NextVariable
= GetNextVariablePtr (Variable
);
657 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
660 // Buffer has cached all ADDED variable.
661 // Per IN_DELETED variable, we have to guarantee that
662 // no ADDED one in previous buffer.
666 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
667 while (IsValidVariableHeader (AddedVariable
)) {
668 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
669 NameSize
= NameSizeOfVariable (AddedVariable
);
670 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
671 NameSize
== NameSizeOfVariable (Variable
)
673 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
674 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
675 if (CompareMem (Point0
, Point1
, NameSizeOfVariable (AddedVariable
)) == 0) {
680 AddedVariable
= NextAddedVariable
;
684 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
686 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
687 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
688 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
689 CurrPtr
+= VariableSize
;
690 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
691 HwErrVariableTotalSize
+= VariableSize
;
692 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
693 CommonVariableTotalSize
+= VariableSize
;
698 Variable
= NextVariable
;
703 // If volatile variable store, just copy valid buffer.
705 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
706 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
707 Status
= EFI_SUCCESS
;
710 // If non-volatile variable store, perform FTW here.
712 Status
= FtwVariableSpace (
715 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
717 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
719 if (!EFI_ERROR (Status
)) {
720 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
722 mVariableModuleGlobal
->HwErrVariableTotalSize
= HwErrVariableTotalSize
;
723 mVariableModuleGlobal
->CommonVariableTotalSize
= CommonVariableTotalSize
;
726 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableBase
);
727 while (IsValidVariableHeader (NextVariable
)) {
728 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
729 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
730 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
731 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
732 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
735 NextVariable
= GetNextVariablePtr (NextVariable
);
737 *LastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableBase
;
740 FreePool (ValidBuffer
);
746 Find the variable in the specified variable store.
748 @param[in] VariableName Name of the variable to be found
749 @param[in] VendorGuid Vendor GUID to be found.
750 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
751 check at runtime when searching variable.
752 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.
754 @retval EFI_SUCCESS Variable found successfully
755 @retval EFI_NOT_FOUND Variable not found
759 IN CHAR16
*VariableName
,
760 IN EFI_GUID
*VendorGuid
,
761 IN BOOLEAN IgnoreRtCheck
,
762 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
765 VARIABLE_HEADER
*InDeletedVariable
;
769 // Find the variable by walk through HOB, volatile and non-volatile variable store.
771 InDeletedVariable
= NULL
;
773 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
774 ; (PtrTrack
->CurrPtr
< PtrTrack
->EndPtr
) && IsValidVariableHeader (PtrTrack
->CurrPtr
)
775 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
777 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
778 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
780 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
781 if (VariableName
[0] == 0) {
782 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
783 InDeletedVariable
= PtrTrack
->CurrPtr
;
788 if (CompareGuid (VendorGuid
, &PtrTrack
->CurrPtr
->VendorGuid
)) {
789 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
791 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
792 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
793 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
794 InDeletedVariable
= PtrTrack
->CurrPtr
;
805 PtrTrack
->CurrPtr
= InDeletedVariable
;
806 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
811 Finds variable in storage blocks of volatile and non-volatile storage areas.
813 This code finds variable in storage blocks of volatile and non-volatile storage areas.
814 If VariableName is an empty string, then we just return the first
815 qualified variable without comparing VariableName and VendorGuid.
816 If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
817 at runtime when searching existing variable, only VariableName and VendorGuid are compared.
818 Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
820 @param[in] VariableName Name of the variable to be found.
821 @param[in] VendorGuid Vendor GUID to be found.
822 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
823 including the range searched and the target position.
824 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
825 base of volatile variable storage area, base of
826 NV variable storage area, and a lock.
827 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
828 check at runtime when searching variable.
830 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
832 @retval EFI_SUCCESS Variable successfully found.
833 @retval EFI_NOT_FOUND Variable not found
838 IN CHAR16
*VariableName
,
839 IN EFI_GUID
*VendorGuid
,
840 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
841 IN VARIABLE_GLOBAL
*Global
,
842 IN BOOLEAN IgnoreRtCheck
846 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
847 VARIABLE_STORE_TYPE Type
;
849 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
850 return EFI_INVALID_PARAMETER
;
854 // 0: Volatile, 1: HOB, 2: Non-Volatile.
855 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
856 // make use of this mapping to implement search algorithm.
858 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->VolatileVariableBase
;
859 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) Global
->HobVariableBase
;
860 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
863 // Find the variable by walk through HOB, volatile and non-volatile variable store.
865 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
866 if (VariableStoreHeader
[Type
] == NULL
) {
870 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
871 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
872 PtrTrack
->Volatile
= (BOOLEAN
) (Type
== VariableStoreTypeVolatile
);
874 Status
= FindVariableEx (VariableName
, VendorGuid
, IgnoreRtCheck
, PtrTrack
);
875 if (!EFI_ERROR (Status
)) {
879 return EFI_NOT_FOUND
;
883 Get index from supported language codes according to language string.
885 This code is used to get corresponding index in supported language codes. It can handle
886 RFC4646 and ISO639 language tags.
887 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
888 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
891 SupportedLang = "engfraengfra"
893 Iso639Language = TRUE
894 The return value is "0".
896 SupportedLang = "en;fr;en-US;fr-FR"
898 Iso639Language = FALSE
899 The return value is "3".
901 @param SupportedLang Platform supported language codes.
902 @param Lang Configured language.
903 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
905 @retval The index of language in the language codes.
909 GetIndexFromSupportedLangCodes(
910 IN CHAR8
*SupportedLang
,
912 IN BOOLEAN Iso639Language
917 UINTN LanguageLength
;
919 if (Iso639Language
) {
920 CompareLength
= ISO_639_2_ENTRY_SIZE
;
921 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
922 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
924 // Successfully find the index of Lang string in SupportedLang string.
926 Index
= Index
/ CompareLength
;
934 // Compare RFC4646 language code
937 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
939 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
941 // Skip ';' characters in SupportedLang
943 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
945 // Determine the length of the next language code in SupportedLang
947 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
949 if ((CompareLength
== LanguageLength
) &&
950 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
952 // Successfully find the index of Lang string in SupportedLang string.
963 Get language string from supported language codes according to index.
965 This code is used to get corresponding language strings in supported language codes. It can handle
966 RFC4646 and ISO639 language tags.
967 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
968 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
971 SupportedLang = "engfraengfra"
973 Iso639Language = TRUE
974 The return value is "fra".
976 SupportedLang = "en;fr;en-US;fr-FR"
978 Iso639Language = FALSE
979 The return value is "fr".
981 @param SupportedLang Platform supported language codes.
982 @param Index The index in supported language codes.
983 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
985 @retval The language string in the language codes.
989 GetLangFromSupportedLangCodes (
990 IN CHAR8
*SupportedLang
,
992 IN BOOLEAN Iso639Language
1000 Supported
= SupportedLang
;
1001 if (Iso639Language
) {
1003 // According to the index of Lang string in SupportedLang string to get the language.
1004 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
1005 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1007 CompareLength
= ISO_639_2_ENTRY_SIZE
;
1008 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
1009 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
1014 // Take semicolon as delimitation, sequentially traverse supported language codes.
1016 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
1019 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
1021 // Have completed the traverse, but not find corrsponding string.
1022 // This case is not allowed to happen.
1027 if (SubIndex
== Index
) {
1029 // According to the index of Lang string in SupportedLang string to get the language.
1030 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1031 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1033 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
1034 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
1039 // Skip ';' characters in Supported
1041 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1047 Returns a pointer to an allocated buffer that contains the best matching language
1048 from a set of supported languages.
1050 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1051 code types may not be mixed in a single call to this function. This function
1052 supports a variable argument list that allows the caller to pass in a prioritized
1053 list of language codes to test against all the language codes in SupportedLanguages.
1055 If SupportedLanguages is NULL, then ASSERT().
1057 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1058 contains a set of language codes in the format
1059 specified by Iso639Language.
1060 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1061 in ISO 639-2 format. If FALSE, then all language
1062 codes are assumed to be in RFC 4646 language format
1063 @param[in] ... A variable argument list that contains pointers to
1064 Null-terminated ASCII strings that contain one or more
1065 language codes in the format specified by Iso639Language.
1066 The first language code from each of these language
1067 code lists is used to determine if it is an exact or
1068 close match to any of the language codes in
1069 SupportedLanguages. Close matches only apply to RFC 4646
1070 language codes, and the matching algorithm from RFC 4647
1071 is used to determine if a close match is present. If
1072 an exact or close match is found, then the matching
1073 language code from SupportedLanguages is returned. If
1074 no matches are found, then the next variable argument
1075 parameter is evaluated. The variable argument list
1076 is terminated by a NULL.
1078 @retval NULL The best matching language could not be found in SupportedLanguages.
1079 @retval NULL There are not enough resources available to return the best matching
1081 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1082 language in SupportedLanguages.
1087 VariableGetBestLanguage (
1088 IN CONST CHAR8
*SupportedLanguages
,
1089 IN BOOLEAN Iso639Language
,
1095 UINTN CompareLength
;
1096 UINTN LanguageLength
;
1097 CONST CHAR8
*Supported
;
1100 if (SupportedLanguages
== NULL
) {
1104 VA_START (Args
, Iso639Language
);
1105 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1107 // Default to ISO 639-2 mode
1110 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1113 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1115 if (!Iso639Language
) {
1116 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1120 // Trim back the length of Language used until it is empty
1122 while (LanguageLength
> 0) {
1124 // Loop through all language codes in SupportedLanguages
1126 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1128 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1130 if (!Iso639Language
) {
1132 // Skip ';' characters in Supported
1134 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1136 // Determine the length of the next language code in Supported
1138 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1140 // If Language is longer than the Supported, then skip to the next language
1142 if (LanguageLength
> CompareLength
) {
1147 // See if the first LanguageLength characters in Supported match Language
1149 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1152 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1153 Buffer
[CompareLength
] = '\0';
1154 return CopyMem (Buffer
, Supported
, CompareLength
);
1158 if (Iso639Language
) {
1160 // If ISO 639 mode, then each language can only be tested once
1165 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1167 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1174 // No matches were found
1180 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1182 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1184 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1185 and are read-only. Therefore, in variable driver, only store the original value for other use.
1187 @param[in] VariableName Name of variable.
1189 @param[in] Data Variable data.
1191 @param[in] DataSize Size of data. 0 means delete.
1195 AutoUpdateLangVariable (
1196 IN CHAR16
*VariableName
,
1202 CHAR8
*BestPlatformLang
;
1206 VARIABLE_POINTER_TRACK Variable
;
1207 BOOLEAN SetLanguageCodes
;
1210 // Don't do updates for delete operation
1212 if (DataSize
== 0) {
1216 SetLanguageCodes
= FALSE
;
1218 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1220 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1226 SetLanguageCodes
= TRUE
;
1229 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1230 // Therefore, in variable driver, only store the original value for other use.
1232 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1233 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1235 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1236 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1239 // PlatformLang holds a single language from PlatformLangCodes,
1240 // so the size of PlatformLangCodes is enough for the PlatformLang.
1242 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1243 FreePool (mVariableModuleGlobal
->PlatformLang
);
1245 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1246 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1248 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1250 // LangCodes is a volatile variable, so it can not be updated at runtime.
1256 SetLanguageCodes
= TRUE
;
1259 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1260 // Therefore, in variable driver, only store the original value for other use.
1262 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1263 FreePool (mVariableModuleGlobal
->LangCodes
);
1265 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1266 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1269 if (SetLanguageCodes
1270 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1271 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1273 // Update Lang if PlatformLang is already set
1274 // Update PlatformLang if Lang is already set
1276 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1277 if (!EFI_ERROR (Status
)) {
1281 VariableName
= L
"PlatformLang";
1282 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1283 DataSize
= Variable
.CurrPtr
->DataSize
;
1285 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1286 if (!EFI_ERROR (Status
)) {
1288 // Update PlatformLang
1290 VariableName
= L
"Lang";
1291 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1292 DataSize
= Variable
.CurrPtr
->DataSize
;
1295 // Neither PlatformLang nor Lang is set, directly return
1303 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1305 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1307 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1309 // Update Lang when PlatformLangCodes/LangCodes were set.
1311 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1313 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1315 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1316 if (BestPlatformLang
!= NULL
) {
1318 // Get the corresponding index in language codes.
1320 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1323 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1325 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1328 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1330 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1332 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1333 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, 0, 0, &Variable
, NULL
);
1335 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1337 ASSERT_EFI_ERROR(Status
);
1341 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1343 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1345 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1347 // When setting Lang, firstly get most matched language string from supported language codes.
1349 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1350 if (BestLang
!= NULL
) {
1352 // Get the corresponding index in language codes.
1354 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1357 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1359 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1362 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1364 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
1366 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1367 AsciiStrSize (BestPlatformLang
), Attributes
, 0, 0, &Variable
, NULL
);
1369 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1370 ASSERT_EFI_ERROR (Status
);
1377 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
1378 index of associated public key is needed.
1380 @param[in] VariableName Name of variable.
1381 @param[in] VendorGuid Guid of variable.
1382 @param[in] Data Variable data.
1383 @param[in] DataSize Size of data. 0 means delete.
1384 @param[in] Attributes Attributes of the variable.
1385 @param[in] KeyIndex Index of associated public key.
1386 @param[in] MonotonicCount Value of associated monotonic count.
1387 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1388 @param[in] TimeStamp Value of associated TimeStamp.
1390 @retval EFI_SUCCESS The update operation is success.
1391 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1396 IN CHAR16
*VariableName
,
1397 IN EFI_GUID
*VendorGuid
,
1400 IN UINT32 Attributes OPTIONAL
,
1401 IN UINT32 KeyIndex OPTIONAL
,
1402 IN UINT64 MonotonicCount OPTIONAL
,
1403 IN VARIABLE_POINTER_TRACK
*CacheVariable
,
1404 IN EFI_TIME
*TimeStamp OPTIONAL
1408 VARIABLE_HEADER
*NextVariable
;
1410 UINTN ScratchDataSize
;
1411 UINTN NonVolatileVarableStoreSize
;
1412 UINTN VarNameOffset
;
1413 UINTN VarDataOffset
;
1417 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1420 VARIABLE_POINTER_TRACK
*Variable
;
1421 VARIABLE_POINTER_TRACK NvVariable
;
1422 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1428 if (mVariableModuleGlobal
->FvbInstance
== NULL
) {
1430 // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
1432 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1434 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1436 return EFI_NOT_AVAILABLE_YET
;
1437 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1439 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1440 // The authenticated variable perhaps is not initialized, just return here.
1442 return EFI_NOT_AVAILABLE_YET
;
1446 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1447 Variable
= CacheVariable
;
1450 // Update/Delete existing NV variable.
1451 // CacheVariable points to the variable in the memory copy of Flash area
1452 // Now let Variable points to the same variable in Flash area.
1454 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1455 Variable
= &NvVariable
;
1456 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1457 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1458 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1459 Variable
->Volatile
= FALSE
;
1462 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1466 // Tricky part: Use scratch data area at the end of volatile variable store
1467 // as a temporary storage.
1469 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1470 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1471 ScratchDataSize
= ScratchSize
- sizeof (VARIABLE_HEADER
) - StrSize (VariableName
) - GET_PAD_SIZE (StrSize (VariableName
));
1473 if (Variable
->CurrPtr
!= NULL
) {
1475 // Update/Delete existing variable.
1479 // If AtRuntime and the variable is Volatile and Runtime Access,
1480 // the volatile is ReadOnly, and SetVariable should be aborted and
1481 // return EFI_WRITE_PROTECTED.
1483 if (Variable
->Volatile
) {
1484 Status
= EFI_WRITE_PROTECTED
;
1488 // Only variable that have NV attributes can be updated/deleted in Runtime.
1490 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1491 Status
= EFI_INVALID_PARAMETER
;
1496 // Only variable that have RT attributes can be updated/deleted in Runtime.
1498 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) {
1499 Status
= EFI_INVALID_PARAMETER
;
1505 // Setting a data variable with no access, or zero DataSize attributes
1506 // causes it to be deleted.
1507 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
1508 // not delete the variable.
1510 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0))|| ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
1511 State
= Variable
->CurrPtr
->State
;
1512 State
&= VAR_DELETED
;
1514 Status
= UpdateVariableStore (
1515 &mVariableModuleGlobal
->VariableGlobal
,
1519 (UINTN
) &Variable
->CurrPtr
->State
,
1523 if (!EFI_ERROR (Status
)) {
1524 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1525 if (!Variable
->Volatile
) {
1526 CacheVariable
->CurrPtr
->State
= State
;
1532 // If the variable is marked valid, and the same data has been passed in,
1533 // then return to the caller immediately.
1535 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1536 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0) &&
1537 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) &&
1538 (TimeStamp
== NULL
)) {
1540 // Variable content unchanged and no need to update timestamp, just return.
1542 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1543 Status
= EFI_SUCCESS
;
1545 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1546 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1549 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
1551 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
1553 // Cache the previous variable data into StorageArea.
1555 DataOffset
= sizeof (VARIABLE_HEADER
) + Variable
->CurrPtr
->NameSize
+ GET_PAD_SIZE (Variable
->CurrPtr
->NameSize
);
1556 CopyMem (mStorageArea
, (UINT8
*)((UINTN
) Variable
->CurrPtr
+ DataOffset
), Variable
->CurrPtr
->DataSize
);
1558 if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) ||
1559 (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0))) {
1561 // For variables with the GUID EFI_IMAGE_SECURITY_DATABASE_GUID (i.e. where the data
1562 // buffer is formatted as EFI_SIGNATURE_LIST), the driver shall not perform an append of
1563 // EFI_SIGNATURE_DATA values that are already part of the existing variable value.
1565 BufSize
= AppendSignatureList (mStorageArea
, Variable
->CurrPtr
->DataSize
, Data
, DataSize
);
1566 if (BufSize
== Variable
->CurrPtr
->DataSize
) {
1567 if ((TimeStamp
== NULL
) || CompareTimeStamp (TimeStamp
, &Variable
->CurrPtr
->TimeStamp
)) {
1569 // New EFI_SIGNATURE_DATA is not found and timestamp is not later
1570 // than current timestamp, return EFI_SUCCESS directly.
1572 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1573 Status
= EFI_SUCCESS
;
1579 // For other Variables, append the new data to the end of previous data.
1581 CopyMem ((UINT8
*)((UINTN
) mStorageArea
+ Variable
->CurrPtr
->DataSize
), Data
, DataSize
);
1582 BufSize
= Variable
->CurrPtr
->DataSize
+ DataSize
;
1585 RevBufSize
= MIN (PcdGet32 (PcdMaxVariableSize
), ScratchDataSize
);
1586 if (BufSize
> RevBufSize
) {
1588 // If variable size (previous + current) is bigger than reserved buffer in runtime,
1589 // return EFI_OUT_OF_RESOURCES.
1591 return EFI_OUT_OF_RESOURCES
;
1595 // Override Data and DataSize which are used for combined data area including previous and new data.
1597 Data
= mStorageArea
;
1602 // Mark the old variable as in delete transition.
1604 State
= Variable
->CurrPtr
->State
;
1605 State
&= VAR_IN_DELETED_TRANSITION
;
1607 Status
= UpdateVariableStore (
1608 &mVariableModuleGlobal
->VariableGlobal
,
1612 (UINTN
) &Variable
->CurrPtr
->State
,
1616 if (EFI_ERROR (Status
)) {
1619 if (!Variable
->Volatile
) {
1620 CacheVariable
->CurrPtr
->State
= State
;
1625 // Not found existing variable. Create a new variable.
1628 if ((DataSize
== 0) && ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0)) {
1629 Status
= EFI_SUCCESS
;
1634 // Make sure we are trying to create a new variable.
1635 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1637 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1638 Status
= EFI_NOT_FOUND
;
1643 // Only variable have NV|RT attribute can be created in Runtime.
1646 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1647 Status
= EFI_INVALID_PARAMETER
;
1653 // Function part - create a new variable and copy the data.
1654 // Both update a variable and create a variable will come here.
1656 SetMem (NextVariable
, ScratchSize
, 0xff);
1658 NextVariable
->StartId
= VARIABLE_DATA
;
1660 // NextVariable->State = VAR_ADDED;
1662 NextVariable
->Reserved
= 0;
1663 NextVariable
->PubKeyIndex
= KeyIndex
;
1664 NextVariable
->MonotonicCount
= MonotonicCount
;
1665 ZeroMem (&NextVariable
->TimeStamp
, sizeof (EFI_TIME
));
1667 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1668 (TimeStamp
!= NULL
)) {
1669 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
1670 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1673 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
1674 // when the new TimeStamp value is later than the current timestamp associated
1675 // with the variable, we need associate the new timestamp with the updated value.
1677 if (Variable
->CurrPtr
!= NULL
) {
1678 if (CompareTimeStamp (&Variable
->CurrPtr
->TimeStamp
, TimeStamp
)) {
1679 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1686 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
1687 // Attributes bitmask parameter of a GetVariable() call.
1689 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
1691 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1692 VarNameSize
= StrSize (VariableName
);
1694 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1698 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1700 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1704 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1706 // There will be pad bytes after Data, the NextVariable->NameSize and
1707 // NextVariable->DataSize should not include pad size so that variable
1708 // service can get actual size in GetVariable.
1710 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1711 NextVariable
->DataSize
= (UINT32
)DataSize
;
1714 // The actual size of the variable that stores in storage should
1715 // include pad size.
1717 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1718 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1720 // Create a nonvolatile variable.
1723 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1724 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1725 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1726 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1727 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1729 Status
= EFI_OUT_OF_RESOURCES
;
1733 // Perform garbage collection & reclaim operation.
1735 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1736 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
->CurrPtr
);
1737 if (EFI_ERROR (Status
)) {
1741 // If still no enough space, return out of resources.
1743 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1744 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1745 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1746 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1747 Status
= EFI_OUT_OF_RESOURCES
;
1754 // 1. Write variable header
1755 // 2. Set variable state to header valid
1756 // 3. Write variable data
1757 // 4. Set variable state to valid
1762 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1763 Status
= UpdateVariableStore (
1764 &mVariableModuleGlobal
->VariableGlobal
,
1768 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1769 sizeof (VARIABLE_HEADER
),
1770 (UINT8
*) NextVariable
1773 if (EFI_ERROR (Status
)) {
1780 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1781 Status
= UpdateVariableStore (
1782 &mVariableModuleGlobal
->VariableGlobal
,
1786 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1788 &NextVariable
->State
1791 if (EFI_ERROR (Status
)) {
1797 Status
= UpdateVariableStore (
1798 &mVariableModuleGlobal
->VariableGlobal
,
1802 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1803 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1804 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1807 if (EFI_ERROR (Status
)) {
1813 NextVariable
->State
= VAR_ADDED
;
1814 Status
= UpdateVariableStore (
1815 &mVariableModuleGlobal
->VariableGlobal
,
1819 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1821 &NextVariable
->State
1824 if (EFI_ERROR (Status
)) {
1828 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1830 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1831 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1833 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1836 // update the memory copy of Flash region.
1838 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1841 // Create a volatile variable.
1845 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1846 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1848 // Perform garbage collection & reclaim operation.
1850 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1851 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
->CurrPtr
);
1852 if (EFI_ERROR (Status
)) {
1856 // If still no enough space, return out of resources.
1858 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1859 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1861 Status
= EFI_OUT_OF_RESOURCES
;
1867 NextVariable
->State
= VAR_ADDED
;
1868 Status
= UpdateVariableStore (
1869 &mVariableModuleGlobal
->VariableGlobal
,
1873 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1875 (UINT8
*) NextVariable
1878 if (EFI_ERROR (Status
)) {
1882 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1886 // Mark the old variable as deleted.
1888 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1889 State
= Variable
->CurrPtr
->State
;
1890 State
&= VAR_DELETED
;
1892 Status
= UpdateVariableStore (
1893 &mVariableModuleGlobal
->VariableGlobal
,
1897 (UINTN
) &Variable
->CurrPtr
->State
,
1901 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1902 CacheVariable
->CurrPtr
->State
= State
;
1906 if (!EFI_ERROR (Status
)) {
1907 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1915 Check if a Unicode character is a hexadecimal character.
1917 This function checks if a Unicode character is a
1918 hexadecimal character. The valid hexadecimal character is
1919 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
1922 @param Char The character to check against.
1924 @retval TRUE If the Char is a hexadecmial character.
1925 @retval FALSE If the Char is not a hexadecmial character.
1930 IsHexaDecimalDigitCharacter (
1934 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
1939 This code checks if variable is hardware error record variable or not.
1941 According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid
1942 and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.
1944 @param VariableName Pointer to variable name.
1945 @param VendorGuid Variable Vendor Guid.
1947 @retval TRUE Variable is hardware error record variable.
1948 @retval FALSE Variable is not hardware error record variable.
1953 IsHwErrRecVariable (
1954 IN CHAR16
*VariableName
,
1955 IN EFI_GUID
*VendorGuid
1958 if (!CompareGuid (VendorGuid
, &gEfiHardwareErrorVariableGuid
) ||
1959 (StrLen (VariableName
) != StrLen (L
"HwErrRec####")) ||
1960 (StrnCmp(VariableName
, L
"HwErrRec", StrLen (L
"HwErrRec")) != 0) ||
1961 !IsHexaDecimalDigitCharacter (VariableName
[0x8]) ||
1962 !IsHexaDecimalDigitCharacter (VariableName
[0x9]) ||
1963 !IsHexaDecimalDigitCharacter (VariableName
[0xA]) ||
1964 !IsHexaDecimalDigitCharacter (VariableName
[0xB])) {
1972 This code checks if variable should be treated as read-only variable.
1974 @param[in] VariableName Name of the Variable.
1975 @param[in] VendorGuid GUID of the Variable.
1977 @retval TRUE This variable is read-only variable.
1978 @retval FALSE This variable is NOT read-only variable.
1982 IsReadOnlyVariable (
1983 IN CHAR16
*VariableName
,
1984 IN EFI_GUID
*VendorGuid
1987 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)) {
1988 if ((StrCmp (VariableName
, EFI_SETUP_MODE_NAME
) == 0) ||
1989 (StrCmp (VariableName
, EFI_SIGNATURE_SUPPORT_NAME
) == 0) ||
1990 (StrCmp (VariableName
, EFI_SECURE_BOOT_MODE_NAME
) == 0)) {
2000 This code finds variable in storage blocks (Volatile or Non-Volatile).
2002 Caution: This function may receive untrusted input.
2003 This function may be invoked in SMM mode, and datasize is external input.
2004 This function will do basic validation, before parse the data.
2006 @param VariableName Name of Variable to be found.
2007 @param VendorGuid Variable vendor GUID.
2008 @param Attributes Attribute value of the variable found.
2009 @param DataSize Size of Data found. If size is less than the
2010 data, this value contains the required size.
2011 @param Data Data pointer.
2013 @return EFI_INVALID_PARAMETER Invalid parameter.
2014 @return EFI_SUCCESS Find the specified variable.
2015 @return EFI_NOT_FOUND Not found.
2016 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2021 VariableServiceGetVariable (
2022 IN CHAR16
*VariableName
,
2023 IN EFI_GUID
*VendorGuid
,
2024 OUT UINT32
*Attributes OPTIONAL
,
2025 IN OUT UINTN
*DataSize
,
2030 VARIABLE_POINTER_TRACK Variable
;
2033 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
2034 return EFI_INVALID_PARAMETER
;
2037 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2039 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2040 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2047 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
2048 ASSERT (VarDataSize
!= 0);
2050 if (*DataSize
>= VarDataSize
) {
2052 Status
= EFI_INVALID_PARAMETER
;
2056 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
2057 if (Attributes
!= NULL
) {
2058 *Attributes
= Variable
.CurrPtr
->Attributes
;
2061 *DataSize
= VarDataSize
;
2062 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
2064 Status
= EFI_SUCCESS
;
2067 *DataSize
= VarDataSize
;
2068 Status
= EFI_BUFFER_TOO_SMALL
;
2073 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2081 This code Finds the Next available variable.
2083 Caution: This function may receive untrusted input.
2084 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2086 @param VariableNameSize Size of the variable name.
2087 @param VariableName Pointer to variable name.
2088 @param VendorGuid Variable Vendor Guid.
2090 @return EFI_INVALID_PARAMETER Invalid parameter.
2091 @return EFI_SUCCESS Find the specified variable.
2092 @return EFI_NOT_FOUND Not found.
2093 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
2098 VariableServiceGetNextVariableName (
2099 IN OUT UINTN
*VariableNameSize
,
2100 IN OUT CHAR16
*VariableName
,
2101 IN OUT EFI_GUID
*VendorGuid
2104 VARIABLE_STORE_TYPE Type
;
2105 VARIABLE_POINTER_TRACK Variable
;
2106 VARIABLE_POINTER_TRACK VariableInHob
;
2109 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
2111 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
2112 return EFI_INVALID_PARAMETER
;
2115 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2117 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
2118 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
2122 if (VariableName
[0] != 0) {
2124 // If variable name is not NULL, get next variable.
2126 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2130 // 0: Volatile, 1: HOB, 2: Non-Volatile.
2131 // The index and attributes mapping must be kept in this order as FindVariable
2132 // makes use of this mapping to implement search algorithm.
2134 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
2135 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2136 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
2140 // Switch from Volatile to HOB, to Non-Volatile.
2142 while ((Variable
.CurrPtr
>= Variable
.EndPtr
) ||
2143 (Variable
.CurrPtr
== NULL
) ||
2144 !IsValidVariableHeader (Variable
.CurrPtr
)
2147 // Find current storage index
2149 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
2150 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
2154 ASSERT (Type
< VariableStoreTypeMax
);
2156 // Switch to next storage
2158 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
2159 if (VariableStoreHeader
[Type
] != NULL
) {
2164 // Capture the case that
2165 // 1. current storage is the last one, or
2166 // 2. no further storage
2168 if (Type
== VariableStoreTypeMax
) {
2169 Status
= EFI_NOT_FOUND
;
2172 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
2173 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
2174 Variable
.CurrPtr
= Variable
.StartPtr
;
2178 // Variable is found
2180 if (Variable
.CurrPtr
->State
== VAR_ADDED
) {
2181 if ((AtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
2184 // Don't return NV variable when HOB overrides it
2186 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
2187 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
2189 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2190 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
2191 Status
= FindVariableEx (
2192 GetVariableNamePtr (Variable
.CurrPtr
),
2193 &Variable
.CurrPtr
->VendorGuid
,
2197 if (!EFI_ERROR (Status
)) {
2198 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2203 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
2204 ASSERT (VarNameSize
!= 0);
2206 if (VarNameSize
<= *VariableNameSize
) {
2207 CopyMem (VariableName
, GetVariableNamePtr (Variable
.CurrPtr
), VarNameSize
);
2208 CopyMem (VendorGuid
, &Variable
.CurrPtr
->VendorGuid
, sizeof (EFI_GUID
));
2209 Status
= EFI_SUCCESS
;
2211 Status
= EFI_BUFFER_TOO_SMALL
;
2214 *VariableNameSize
= VarNameSize
;
2219 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2223 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2229 This code sets variable in storage blocks (Volatile or Non-Volatile).
2231 Caution: This function may receive untrusted input.
2232 This function may be invoked in SMM mode, and datasize and data are external input.
2233 This function will do basic validation, before parse the data.
2234 This function will parse the authentication carefully to avoid security issues, like
2235 buffer overflow, integer overflow.
2236 This function will check attribute carefully to avoid authentication bypass.
2238 @param VariableName Name of Variable to be found.
2239 @param VendorGuid Variable vendor GUID.
2240 @param Attributes Attribute value of the variable found
2241 @param DataSize Size of Data found. If size is less than the
2242 data, this value contains the required size.
2243 @param Data Data pointer.
2245 @return EFI_INVALID_PARAMETER Invalid parameter.
2246 @return EFI_SUCCESS Set successfully.
2247 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2248 @return EFI_NOT_FOUND Not found.
2249 @return EFI_WRITE_PROTECTED Variable is read-only.
2254 VariableServiceSetVariable (
2255 IN CHAR16
*VariableName
,
2256 IN EFI_GUID
*VendorGuid
,
2257 IN UINT32 Attributes
,
2262 VARIABLE_POINTER_TRACK Variable
;
2264 VARIABLE_HEADER
*NextVariable
;
2265 EFI_PHYSICAL_ADDRESS Point
;
2269 // Check input parameters.
2271 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2272 return EFI_INVALID_PARAMETER
;
2275 if (IsReadOnlyVariable (VariableName
, VendorGuid
)) {
2276 return EFI_WRITE_PROTECTED
;
2279 if (DataSize
!= 0 && Data
== NULL
) {
2280 return EFI_INVALID_PARAMETER
;
2284 // Check for reserverd bit in variable attribute.
2286 if ((Attributes
& (~EFI_VARIABLE_ATTRIBUTES_MASK
)) != 0) {
2287 return EFI_INVALID_PARAMETER
;
2291 // Make sure if runtime bit is set, boot service bit is set also.
2293 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2294 return EFI_INVALID_PARAMETER
;
2298 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
2299 // cannot be set both.
2301 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
2302 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
2303 return EFI_INVALID_PARAMETER
;
2306 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2307 if (DataSize
< AUTHINFO_SIZE
) {
2309 // Try to write Authenticated Variable without AuthInfo.
2311 return EFI_SECURITY_VIOLATION
;
2313 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2314 } else if ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) {
2316 // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.
2318 if (DataSize
< OFFSET_OF_AUTHINFO2_CERT_DATA
||
2319 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
> DataSize
- (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
)) ||
2320 ((EFI_VARIABLE_AUTHENTICATION_2
*) Data
)->AuthInfo
.Hdr
.dwLength
< OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) {
2321 return EFI_SECURITY_VIOLATION
;
2323 PayloadSize
= DataSize
- AUTHINFO2_SIZE (Data
);
2325 PayloadSize
= DataSize
;
2329 // The size of the VariableName, including the Unicode Null in bytes plus
2330 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2331 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2333 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2334 if ((PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
2335 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
2336 return EFI_INVALID_PARAMETER
;
2338 if (!IsHwErrRecVariable(VariableName
, VendorGuid
)) {
2339 return EFI_INVALID_PARAMETER
;
2343 // The size of the VariableName, including the Unicode Null in bytes plus
2344 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2346 if ((PayloadSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2347 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxVariableSize
))) {
2348 return EFI_INVALID_PARAMETER
;
2354 // HwErrRecSupport Global Variable identifies the level of hardware error record persistence
2355 // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS.
2357 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, L
"HwErrRecSupport") == 0)) {
2358 return EFI_WRITE_PROTECTED
;
2362 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2365 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2367 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2368 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2370 // Parse non-volatile variable data and get last variable offset.
2372 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2373 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2374 && IsValidVariableHeader (NextVariable
)) {
2375 NextVariable
= GetNextVariablePtr (NextVariable
);
2377 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2381 // Check whether the input variable is already existed.
2383 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, TRUE
);
2384 if (!EFI_ERROR (Status
)) {
2385 if (((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) && AtRuntime ()) {
2386 return EFI_WRITE_PROTECTED
;
2391 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2393 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2395 // Process PK, KEK, Sigdb seperately.
2397 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
2398 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, TRUE
);
2399 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
2400 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2401 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) &&
2402 ((StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE
) == 0) || (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE1
) == 0))) {
2403 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2404 if (EFI_ERROR (Status
)) {
2405 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2408 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2411 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2412 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2419 This code returns information about the EFI variables.
2421 Caution: This function may receive untrusted input.
2422 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
2424 @param Attributes Attributes bitmask to specify the type of variables
2425 on which to return information.
2426 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2427 for the EFI variables associated with the attributes specified.
2428 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2429 for EFI variables associated with the attributes specified.
2430 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2431 associated with the attributes specified.
2433 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2434 @return EFI_SUCCESS Query successfully.
2435 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2440 VariableServiceQueryVariableInfo (
2441 IN UINT32 Attributes
,
2442 OUT UINT64
*MaximumVariableStorageSize
,
2443 OUT UINT64
*RemainingVariableStorageSize
,
2444 OUT UINT64
*MaximumVariableSize
2447 VARIABLE_HEADER
*Variable
;
2448 VARIABLE_HEADER
*NextVariable
;
2449 UINT64 VariableSize
;
2450 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2451 UINT64 CommonVariableTotalSize
;
2452 UINT64 HwErrVariableTotalSize
;
2454 CommonVariableTotalSize
= 0;
2455 HwErrVariableTotalSize
= 0;
2457 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2458 return EFI_INVALID_PARAMETER
;
2461 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2463 // Make sure the Attributes combination is supported by the platform.
2465 return EFI_UNSUPPORTED
;
2466 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2468 // Make sure if runtime bit is set, boot service bit is set also.
2470 return EFI_INVALID_PARAMETER
;
2471 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2473 // Make sure RT Attribute is set if we are in Runtime phase.
2475 return EFI_INVALID_PARAMETER
;
2476 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2478 // Make sure Hw Attribute is set with NV.
2480 return EFI_INVALID_PARAMETER
;
2483 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2485 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2487 // Query is Volatile related.
2489 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2492 // Query is Non-Volatile related.
2494 VariableStoreHeader
= mNvVariableCache
;
2498 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2499 // with the storage size (excluding the storage header size).
2501 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2504 // Harware error record variable needs larger size.
2506 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2507 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2508 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2510 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2511 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2512 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2516 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2518 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2522 // Point to the starting address of the variables.
2524 Variable
= GetStartPointer (VariableStoreHeader
);
2527 // Now walk through the related variable store.
2529 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2530 NextVariable
= GetNextVariablePtr (Variable
);
2531 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2535 // We don't take the state of the variables in mind
2536 // when calculating RemainingVariableStorageSize,
2537 // since the space occupied by variables not marked with
2538 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2540 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2541 HwErrVariableTotalSize
+= VariableSize
;
2543 CommonVariableTotalSize
+= VariableSize
;
2547 // Only care about Variables with State VAR_ADDED, because
2548 // the space not marked as VAR_ADDED is reclaimable now.
2550 if (Variable
->State
== VAR_ADDED
) {
2551 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2552 HwErrVariableTotalSize
+= VariableSize
;
2554 CommonVariableTotalSize
+= VariableSize
;
2560 // Go to the next one.
2562 Variable
= NextVariable
;
2565 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2566 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2568 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2571 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2572 *MaximumVariableSize
= 0;
2573 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2574 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2577 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2583 This function reclaims variable storage if free size is below the threshold.
2585 Caution: This function may be invoked at SMM mode.
2586 Care must be taken to make sure not security issue.
2595 UINTN CommonVariableSpace
;
2596 UINTN RemainingCommonVariableSpace
;
2597 UINTN RemainingHwErrVariableSpace
;
2599 Status
= EFI_SUCCESS
;
2601 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2603 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2605 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2607 // Check if the free area is blow a threshold.
2609 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2610 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2611 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2613 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2614 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2618 ASSERT_EFI_ERROR (Status
);
2624 Initializes variable write service after FVB was ready.
2626 @retval EFI_SUCCESS Function successfully executed.
2627 @retval Others Fail to initialize the variable service.
2631 VariableWriteServiceInitialize (
2636 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2639 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2640 VARIABLE_HEADER
*Variable
;
2643 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2644 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2647 // Check if the free area is really free.
2649 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreHeader
->Size
; Index
++) {
2650 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2653 // There must be something wrong in variable store, do reclaim operation.
2656 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2657 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2661 if (EFI_ERROR (Status
)) {
2670 // Flush the HOB variable to flash and invalidate HOB variable.
2672 if (mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
!= 0) {
2674 // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB
2676 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
2677 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= 0;
2679 for ( Variable
= GetStartPointer (VariableStoreHeader
)
2680 ; (Variable
< GetEndPointer (VariableStoreHeader
) && IsValidVariableHeader (Variable
))
2681 ; Variable
= GetNextVariablePtr (Variable
)
2683 ASSERT (Variable
->State
== VAR_ADDED
);
2684 ASSERT ((Variable
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0);
2685 VariableData
= GetVariableDataPtr (Variable
);
2686 Status
= VariableServiceSetVariable (
2687 GetVariableNamePtr (Variable
),
2688 &Variable
->VendorGuid
,
2689 Variable
->Attributes
,
2693 ASSERT_EFI_ERROR (Status
);
2698 // Authenticated variable initialize.
2700 Status
= AutenticatedVariableServiceInitialize ();
2707 Initializes variable store area for non-volatile and volatile variable.
2709 @retval EFI_SUCCESS Function successfully executed.
2710 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2714 VariableCommonInitialize (
2719 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2720 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2721 VARIABLE_HEADER
*NextVariable
;
2722 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2723 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2724 UINT64 VariableStoreLength
;
2727 EFI_HOB_GUID_TYPE
*GuidHob
;
2730 // Allocate runtime memory for variable driver global structure.
2732 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2733 if (mVariableModuleGlobal
== NULL
) {
2734 return EFI_OUT_OF_RESOURCES
;
2737 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2740 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2741 // is stored with common variable in the same NV region. So the platform integrator should
2742 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2743 // PcdFlashNvStorageVariableSize.
2745 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2748 // Get HOB variable store.
2750 GuidHob
= GetFirstGuidHob (&gEfiAuthenticatedVariableGuid
);
2751 if (GuidHob
!= NULL
) {
2752 VariableStoreHeader
= GET_GUID_HOB_DATA (GuidHob
);
2753 if (GetVariableStoreStatus (VariableStoreHeader
) == EfiValid
) {
2754 mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VariableStoreHeader
;
2756 DEBUG ((EFI_D_ERROR
, "HOB Variable Store header is corrupted!\n"));
2761 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2763 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2764 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2765 if (VolatileVariableStore
== NULL
) {
2766 FreePool (mVariableModuleGlobal
);
2767 return EFI_OUT_OF_RESOURCES
;
2770 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2773 // Initialize Variable Specific Data.
2775 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2776 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2777 mVariableModuleGlobal
->FvbInstance
= NULL
;
2779 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiAuthenticatedVariableGuid
);
2780 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2781 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2782 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2783 VolatileVariableStore
->Reserved
= 0;
2784 VolatileVariableStore
->Reserved1
= 0;
2787 // Get non-volatile variable store.
2790 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2791 if (TempVariableStoreHeader
== 0) {
2792 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2796 // Check if the Firmware Volume is not corrupted
2798 if ((((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->Signature
!= EFI_FVH_SIGNATURE
) ||
2799 (!CompareGuid (&gEfiSystemNvDataFvGuid
, &((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
))->FileSystemGuid
))) {
2800 Status
= EFI_VOLUME_CORRUPTED
;
2801 DEBUG ((EFI_D_ERROR
, "Firmware Volume for Variable Store is corrupted\n"));
2805 VariableStoreBase
= TempVariableStoreHeader
+ \
2806 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2807 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2808 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2810 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2811 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2812 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2813 Status
= EFI_VOLUME_CORRUPTED
;
2814 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2817 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2820 // Parse non-volatile variable data and get last variable offset.
2822 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2823 while (IsValidVariableHeader (NextVariable
)) {
2824 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2825 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2826 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2828 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2831 NextVariable
= GetNextVariablePtr (NextVariable
);
2834 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2837 // Allocate runtime memory used for a memory copy of the FLASH region.
2838 // Keep the memory and the FLASH in sync as updates occur
2840 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2841 if (mNvVariableCache
== NULL
) {
2842 Status
= EFI_OUT_OF_RESOURCES
;
2845 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2846 Status
= EFI_SUCCESS
;
2849 if (EFI_ERROR (Status
)) {
2850 FreePool (mVariableModuleGlobal
);
2851 FreePool (VolatileVariableStore
);
2859 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2861 @param[in] Address The Flash address.
2862 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2863 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2867 GetFvbInfoByAddress (
2868 IN EFI_PHYSICAL_ADDRESS Address
,
2869 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2870 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2874 EFI_HANDLE
*HandleBuffer
;
2877 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2878 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2879 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2880 EFI_FVB_ATTRIBUTES_2 Attributes
;
2883 // Get all FVB handles.
2885 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2886 if (EFI_ERROR (Status
)) {
2887 return EFI_NOT_FOUND
;
2891 // Get the FVB to access variable store.
2894 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2895 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2896 if (EFI_ERROR (Status
)) {
2897 Status
= EFI_NOT_FOUND
;
2902 // Ensure this FVB protocol supported Write operation.
2904 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2905 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2910 // Compare the address and select the right one.
2912 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2913 if (EFI_ERROR (Status
)) {
2917 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2918 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
2919 if (FvbHandle
!= NULL
) {
2920 *FvbHandle
= HandleBuffer
[Index
];
2922 if (FvbProtocol
!= NULL
) {
2925 Status
= EFI_SUCCESS
;
2929 FreePool (HandleBuffer
);
2932 Status
= EFI_NOT_FOUND
;