2 The common variable operation routines shared by DXE_RINTIME variable
3 module and DXE_SMM variable module.
5 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "AuthService.h"
19 VARIABLE_MODULE_GLOBAL
*mVariableModuleGlobal
;
22 /// Define a memory cache that improves the search performance for a variable.
24 VARIABLE_STORE_HEADER
*mNvVariableCache
= NULL
;
27 /// The memory entry used for variable statistics data.
29 VARIABLE_INFO_ENTRY
*gVariableInfo
= NULL
;
33 Routine used to track statistical information about variable usage.
34 The data is stored in the EFI system table so it can be accessed later.
35 VariableInfo.efi can dump out the table. Only Boot Services variable
36 accesses are tracked by this code. The PcdVariableCollectStatistics
37 build flag controls if this feature is enabled.
39 A read that hits in the cache will have Read and Cache true for
40 the transaction. Data is allocated by this routine, but never
43 @param[in] VariableName Name of the Variable to track.
44 @param[in] VendorGuid Guid of the Variable to track.
45 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
46 @param[in] Read TRUE if GetVariable() was called.
47 @param[in] Write TRUE if SetVariable() was called.
48 @param[in] Delete TRUE if deleted via SetVariable().
49 @param[in] Cache TRUE for a cache hit.
54 IN CHAR16
*VariableName
,
55 IN EFI_GUID
*VendorGuid
,
63 VARIABLE_INFO_ENTRY
*Entry
;
65 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
68 // Don't collect statistics at runtime.
72 if (gVariableInfo
== NULL
) {
74 // On the first call allocate a entry and place a pointer to it in
75 // the EFI System Table.
77 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
78 ASSERT (gVariableInfo
!= NULL
);
80 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
81 gVariableInfo
->Name
= AllocatePool (StrSize (VariableName
));
82 ASSERT (gVariableInfo
->Name
!= NULL
);
83 StrCpy (gVariableInfo
->Name
, VariableName
);
84 gVariableInfo
->Volatile
= Volatile
;
88 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
89 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
90 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
108 if (Entry
->Next
== NULL
) {
110 // If the entry is not in the table add it.
111 // Next iteration of the loop will fill in the data.
113 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
114 ASSERT (Entry
->Next
!= NULL
);
116 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
117 Entry
->Next
->Name
= AllocatePool (StrSize (VariableName
));
118 ASSERT (Entry
->Next
->Name
!= NULL
);
119 StrCpy (Entry
->Next
->Name
, VariableName
);
120 Entry
->Next
->Volatile
= Volatile
;
130 This code checks if variable header is valid or not.
132 @param Variable Pointer to the Variable Header.
134 @retval TRUE Variable header is valid.
135 @retval FALSE Variable header is not valid.
139 IsValidVariableHeader (
140 IN VARIABLE_HEADER
*Variable
143 if (Variable
== NULL
|| Variable
->StartId
!= VARIABLE_DATA
) {
153 This function writes data to the FWH at the correct LBA even if the LBAs
156 @param Global Pointer to VARAIBLE_GLOBAL structure.
157 @param Volatile Point out the Variable is Volatile or Non-Volatile.
158 @param SetByIndex TRUE if target pointer is given as index.
159 FALSE if target pointer is absolute.
160 @param Fvb Pointer to the writable FVB protocol.
161 @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
163 @param DataSize Size of data to be written.
164 @param Buffer Pointer to the buffer from which data is written.
166 @retval EFI_INVALID_PARAMETER Parameters not valid.
167 @retval EFI_SUCCESS Variable store successfully updated.
171 UpdateVariableStore (
172 IN VARIABLE_GLOBAL
*Global
,
174 IN BOOLEAN SetByIndex
,
175 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
176 IN UINTN DataPtrIndex
,
181 EFI_FV_BLOCK_MAP_ENTRY
*PtrBlockMapEntry
;
189 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
190 VARIABLE_STORE_HEADER
*VolatileBase
;
191 EFI_PHYSICAL_ADDRESS FvVolHdr
;
192 EFI_PHYSICAL_ADDRESS DataPtr
;
196 DataPtr
= DataPtrIndex
;
199 // Check if the Data is Volatile.
203 return EFI_INVALID_PARAMETER
;
205 Status
= Fvb
->GetPhysicalAddress(Fvb
, &FvVolHdr
);
206 ASSERT_EFI_ERROR (Status
);
208 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvVolHdr
);
210 // Data Pointer should point to the actual Address where data is to be
214 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
217 if ((DataPtr
+ DataSize
) >= ((EFI_PHYSICAL_ADDRESS
) (UINTN
) ((UINT8
*) FwVolHeader
+ FwVolHeader
->FvLength
))) {
218 return EFI_INVALID_PARAMETER
;
222 // Data Pointer should point to the actual Address where data is to be
225 VolatileBase
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
227 DataPtr
+= mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
230 if ((DataPtr
+ DataSize
) >= ((UINTN
) ((UINT8
*) VolatileBase
+ VolatileBase
->Size
))) {
231 return EFI_INVALID_PARAMETER
;
235 // If Volatile Variable just do a simple mem copy.
237 CopyMem ((UINT8
*)(UINTN
)DataPtr
, Buffer
, DataSize
);
242 // If we are here we are dealing with Non-Volatile Variables.
244 LinearOffset
= (UINTN
) FwVolHeader
;
245 CurrWritePtr
= (UINTN
) DataPtr
;
246 CurrWriteSize
= DataSize
;
250 if (CurrWritePtr
< LinearOffset
) {
251 return EFI_INVALID_PARAMETER
;
254 for (PtrBlockMapEntry
= FwVolHeader
->BlockMap
; PtrBlockMapEntry
->NumBlocks
!= 0; PtrBlockMapEntry
++) {
255 for (BlockIndex2
= 0; BlockIndex2
< PtrBlockMapEntry
->NumBlocks
; BlockIndex2
++) {
257 // Check to see if the Variable Writes are spanning through multiple
260 if ((CurrWritePtr
>= LinearOffset
) && (CurrWritePtr
< LinearOffset
+ PtrBlockMapEntry
->Length
)) {
261 if ((CurrWritePtr
+ CurrWriteSize
) <= (LinearOffset
+ PtrBlockMapEntry
->Length
)) {
262 Status
= Fvb
->Write (
265 (UINTN
) (CurrWritePtr
- LinearOffset
),
271 Size
= (UINT32
) (LinearOffset
+ PtrBlockMapEntry
->Length
- CurrWritePtr
);
272 Status
= Fvb
->Write (
275 (UINTN
) (CurrWritePtr
- LinearOffset
),
279 if (EFI_ERROR (Status
)) {
283 CurrWritePtr
= LinearOffset
+ PtrBlockMapEntry
->Length
;
284 CurrBuffer
= CurrBuffer
+ Size
;
285 CurrWriteSize
= CurrWriteSize
- Size
;
289 LinearOffset
+= PtrBlockMapEntry
->Length
;
300 This code gets the current status of Variable Store.
302 @param VarStoreHeader Pointer to the Variable Store Header.
304 @retval EfiRaw Variable store status is raw.
305 @retval EfiValid Variable store status is valid.
306 @retval EfiInvalid Variable store status is invalid.
309 VARIABLE_STORE_STATUS
310 GetVariableStoreStatus (
311 IN VARIABLE_STORE_HEADER
*VarStoreHeader
314 if (CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) &&
315 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
316 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
320 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
321 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
322 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
323 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
324 VarStoreHeader
->Size
== 0xffffffff &&
325 VarStoreHeader
->Format
== 0xff &&
326 VarStoreHeader
->State
== 0xff
338 This code gets the size of name of variable.
340 @param Variable Pointer to the Variable Header.
342 @return UINTN Size of variable in bytes.
347 IN VARIABLE_HEADER
*Variable
350 if (Variable
->State
== (UINT8
) (-1) ||
351 Variable
->DataSize
== (UINT32
) (-1) ||
352 Variable
->NameSize
== (UINT32
) (-1) ||
353 Variable
->Attributes
== (UINT32
) (-1)) {
356 return (UINTN
) Variable
->NameSize
;
361 This code gets the size of variable data.
363 @param Variable Pointer to the Variable Header.
365 @return Size of variable in bytes.
370 IN VARIABLE_HEADER
*Variable
373 if (Variable
->State
== (UINT8
) (-1) ||
374 Variable
->DataSize
== (UINT32
) (-1) ||
375 Variable
->NameSize
== (UINT32
) (-1) ||
376 Variable
->Attributes
== (UINT32
) (-1)) {
379 return (UINTN
) Variable
->DataSize
;
384 This code gets the pointer to the variable name.
386 @param Variable Pointer to the Variable Header.
388 @return Pointer to Variable Name which is Unicode encoding.
393 IN VARIABLE_HEADER
*Variable
397 return (CHAR16
*) (Variable
+ 1);
402 This code gets the pointer to the variable data.
404 @param Variable Pointer to the Variable Header.
406 @return Pointer to Variable Data.
411 IN VARIABLE_HEADER
*Variable
417 // Be careful about pad size for alignment.
419 Value
= (UINTN
) GetVariableNamePtr (Variable
);
420 Value
+= NameSizeOfVariable (Variable
);
421 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
423 return (UINT8
*) Value
;
429 This code gets the pointer to the next variable header.
431 @param Variable Pointer to the Variable Header.
433 @return Pointer to next variable header.
438 IN VARIABLE_HEADER
*Variable
443 if (!IsValidVariableHeader (Variable
)) {
447 Value
= (UINTN
) GetVariableDataPtr (Variable
);
448 Value
+= DataSizeOfVariable (Variable
);
449 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
452 // Be careful about pad size for alignment.
454 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
459 Gets the pointer to the first variable header in given variable store area.
461 @param VarStoreHeader Pointer to the Variable Store Header.
463 @return Pointer to the first variable header.
468 IN VARIABLE_STORE_HEADER
*VarStoreHeader
472 // The end of variable store.
474 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
479 Gets the pointer to the end of the variable storage area.
481 This function gets pointer to the end of the variable storage
482 area, according to the input variable store header.
484 @param VarStoreHeader Pointer to the Variable Store Header.
486 @return Pointer to the end of the variable storage area.
491 IN VARIABLE_STORE_HEADER
*VarStoreHeader
495 // The end of variable store
497 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
503 Variable store garbage collection and reclaim operation.
505 @param VariableBase Base address of variable store.
506 @param LastVariableOffset Offset of last variable.
507 @param IsVolatile The variable store is volatile or not;
508 if it is non-volatile, need FTW.
509 @param UpdatingVariable Pointer to updating variable.
511 @return EFI_OUT_OF_RESOURCES
518 IN EFI_PHYSICAL_ADDRESS VariableBase
,
519 OUT UINTN
*LastVariableOffset
,
520 IN BOOLEAN IsVolatile
,
521 IN VARIABLE_HEADER
*UpdatingVariable
524 VARIABLE_HEADER
*Variable
;
525 VARIABLE_HEADER
*AddedVariable
;
526 VARIABLE_HEADER
*NextVariable
;
527 VARIABLE_HEADER
*NextAddedVariable
;
528 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
530 UINTN MaximumBufferSize
;
532 UINTN VariableNameSize
;
533 UINTN UpdatingVariableNameSize
;
540 CHAR16
*VariableNamePtr
;
541 CHAR16
*UpdatingVariableNamePtr
;
543 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) VariableBase
);
545 // Recalculate the total size of Common/HwErr type variables in non-volatile area.
548 mVariableModuleGlobal
->CommonVariableTotalSize
= 0;
549 mVariableModuleGlobal
->HwErrVariableTotalSize
= 0;
553 // Start Pointers for the variable.
555 Variable
= GetStartPointer (VariableStoreHeader
);
556 MaximumBufferSize
= sizeof (VARIABLE_STORE_HEADER
);
558 while (IsValidVariableHeader (Variable
)) {
559 NextVariable
= GetNextVariablePtr (Variable
);
560 if (Variable
->State
== VAR_ADDED
||
561 Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
563 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
564 MaximumBufferSize
+= VariableSize
;
567 Variable
= NextVariable
;
571 // Reserve the 1 Bytes with Oxff to identify the
572 // end of the variable buffer.
574 MaximumBufferSize
+= 1;
575 ValidBuffer
= AllocatePool (MaximumBufferSize
);
576 if (ValidBuffer
== NULL
) {
577 return EFI_OUT_OF_RESOURCES
;
580 SetMem (ValidBuffer
, MaximumBufferSize
, 0xff);
583 // Copy variable store header.
585 CopyMem (ValidBuffer
, VariableStoreHeader
, sizeof (VARIABLE_STORE_HEADER
));
586 CurrPtr
= (UINT8
*) GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
589 // Reinstall all ADDED variables as long as they are not identical to Updating Variable.
591 Variable
= GetStartPointer (VariableStoreHeader
);
592 while (IsValidVariableHeader (Variable
)) {
593 NextVariable
= GetNextVariablePtr (Variable
);
594 if (Variable
->State
== VAR_ADDED
) {
595 if (UpdatingVariable
!= NULL
) {
596 if (UpdatingVariable
== Variable
) {
597 Variable
= NextVariable
;
601 VariableNameSize
= NameSizeOfVariable(Variable
);
602 UpdatingVariableNameSize
= NameSizeOfVariable(UpdatingVariable
);
604 VariableNamePtr
= GetVariableNamePtr (Variable
);
605 UpdatingVariableNamePtr
= GetVariableNamePtr (UpdatingVariable
);
606 if (CompareGuid (&Variable
->VendorGuid
, &UpdatingVariable
->VendorGuid
) &&
607 VariableNameSize
== UpdatingVariableNameSize
&&
608 CompareMem (VariableNamePtr
, UpdatingVariableNamePtr
, VariableNameSize
) == 0 ) {
609 Variable
= NextVariable
;
613 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
614 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
615 CurrPtr
+= VariableSize
;
616 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
617 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
618 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
619 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
622 Variable
= NextVariable
;
626 // Reinstall the variable being updated if it is not NULL.
628 if (UpdatingVariable
!= NULL
) {
629 VariableSize
= (UINTN
)(GetNextVariablePtr (UpdatingVariable
)) - (UINTN
)UpdatingVariable
;
630 CopyMem (CurrPtr
, (UINT8
*) UpdatingVariable
, VariableSize
);
631 CurrPtr
+= VariableSize
;
632 if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
633 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
634 } else if ((!IsVolatile
) && ((UpdatingVariable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
635 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
640 // Reinstall all in delete transition variables.
642 Variable
= GetStartPointer (VariableStoreHeader
);
643 while (IsValidVariableHeader (Variable
)) {
644 NextVariable
= GetNextVariablePtr (Variable
);
645 if (Variable
!= UpdatingVariable
&& Variable
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
648 // Buffer has cached all ADDED variable.
649 // Per IN_DELETED variable, we have to guarantee that
650 // no ADDED one in previous buffer.
654 AddedVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) ValidBuffer
);
655 while (IsValidVariableHeader (AddedVariable
)) {
656 NextAddedVariable
= GetNextVariablePtr (AddedVariable
);
657 NameSize
= NameSizeOfVariable (AddedVariable
);
658 if (CompareGuid (&AddedVariable
->VendorGuid
, &Variable
->VendorGuid
) &&
659 NameSize
== NameSizeOfVariable (Variable
)
661 Point0
= (VOID
*) GetVariableNamePtr (AddedVariable
);
662 Point1
= (VOID
*) GetVariableNamePtr (Variable
);
663 if (CompareMem (Point0
, Point1
, NameSizeOfVariable (AddedVariable
)) == 0) {
668 AddedVariable
= NextAddedVariable
;
672 // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
674 VariableSize
= (UINTN
) NextVariable
- (UINTN
) Variable
;
675 CopyMem (CurrPtr
, (UINT8
*) Variable
, VariableSize
);
676 ((VARIABLE_HEADER
*) CurrPtr
)->State
= VAR_ADDED
;
677 CurrPtr
+= VariableSize
;
678 if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
679 mVariableModuleGlobal
->HwErrVariableTotalSize
+= VariableSize
;
680 } else if ((!IsVolatile
) && ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
681 mVariableModuleGlobal
->CommonVariableTotalSize
+= VariableSize
;
686 Variable
= NextVariable
;
691 // If volatile variable store, just copy valid buffer.
693 SetMem ((UINT8
*) (UINTN
) VariableBase
, VariableStoreHeader
->Size
, 0xff);
694 CopyMem ((UINT8
*) (UINTN
) VariableBase
, ValidBuffer
, (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
));
695 Status
= EFI_SUCCESS
;
698 // If non-volatile variable store, perform FTW here.
700 Status
= FtwVariableSpace (
703 (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
)
705 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableBase
, VariableStoreHeader
->Size
);
707 if (!EFI_ERROR (Status
)) {
708 *LastVariableOffset
= (UINTN
) (CurrPtr
- (UINT8
*) ValidBuffer
);
710 *LastVariableOffset
= 0;
713 FreePool (ValidBuffer
);
720 Finds variable in storage blocks of volatile and non-volatile storage areas.
722 This code finds variable in storage blocks of volatile and non-volatile storage areas.
723 If VariableName is an empty string, then we just return the first
724 qualified variable without comparing VariableName and VendorGuid.
725 Otherwise, VariableName and VendorGuid are compared.
727 @param VariableName Name of the variable to be found.
728 @param VendorGuid Vendor GUID to be found.
729 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
730 including the range searched and the target position.
731 @param Global Pointer to VARIABLE_GLOBAL structure, including
732 base of volatile variable storage area, base of
733 NV variable storage area, and a lock.
735 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
737 @retval EFI_SUCCESS Variable successfully found.
738 @retval EFI_NOT_FOUND Variable not found
743 IN CHAR16
*VariableName
,
744 IN EFI_GUID
*VendorGuid
,
745 OUT VARIABLE_POINTER_TRACK
*PtrTrack
,
746 IN VARIABLE_GLOBAL
*Global
749 VARIABLE_HEADER
*Variable
[2];
750 VARIABLE_HEADER
*InDeletedVariable
;
751 VARIABLE_STORE_HEADER
*VariableStoreHeader
[2];
752 UINTN InDeletedStorageIndex
;
757 // 0: Volatile, 1: Non-Volatile.
758 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
759 // make use of this mapping to implement search algorithm.
761 VariableStoreHeader
[0] = (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
762 VariableStoreHeader
[1] = mNvVariableCache
;
765 // Start Pointers for the variable.
766 // Actual Data Pointer where data can be written.
768 Variable
[0] = GetStartPointer (VariableStoreHeader
[0]);
769 Variable
[1] = GetStartPointer (VariableStoreHeader
[1]);
771 if (VariableName
[0] != 0 && VendorGuid
== NULL
) {
772 return EFI_INVALID_PARAMETER
;
776 // Find the variable by walk through volatile and then non-volatile variable store.
778 InDeletedVariable
= NULL
;
779 InDeletedStorageIndex
= 0;
780 for (Index
= 0; Index
< 2; Index
++) {
781 while ((Variable
[Index
] < GetEndPointer (VariableStoreHeader
[Index
])) && IsValidVariableHeader (Variable
[Index
])) {
782 if (Variable
[Index
]->State
== VAR_ADDED
||
783 Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
785 if (!AtRuntime () || ((Variable
[Index
]->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
786 if (VariableName
[0] == 0) {
787 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
788 InDeletedVariable
= Variable
[Index
];
789 InDeletedStorageIndex
= Index
;
791 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
792 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
793 PtrTrack
->CurrPtr
= Variable
[Index
];
794 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
799 if (CompareGuid (VendorGuid
, &Variable
[Index
]->VendorGuid
)) {
800 Point
= (VOID
*) GetVariableNamePtr (Variable
[Index
]);
802 ASSERT (NameSizeOfVariable (Variable
[Index
]) != 0);
803 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (Variable
[Index
])) == 0) {
804 if (Variable
[Index
]->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
805 InDeletedVariable
= Variable
[Index
];
806 InDeletedStorageIndex
= Index
;
808 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[Index
]);
809 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[Index
]);
810 PtrTrack
->CurrPtr
= Variable
[Index
];
811 PtrTrack
->Volatile
= (BOOLEAN
)(Index
== 0);
821 Variable
[Index
] = GetNextVariablePtr (Variable
[Index
]);
823 if (InDeletedVariable
!= NULL
) {
824 PtrTrack
->StartPtr
= GetStartPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
825 PtrTrack
->EndPtr
= GetEndPointer (VariableStoreHeader
[InDeletedStorageIndex
]);
826 PtrTrack
->CurrPtr
= InDeletedVariable
;
827 PtrTrack
->Volatile
= (BOOLEAN
)(InDeletedStorageIndex
== 0);
831 PtrTrack
->CurrPtr
= NULL
;
832 return EFI_NOT_FOUND
;
836 Get index from supported language codes according to language string.
838 This code is used to get corresponding index in supported language codes. It can handle
839 RFC4646 and ISO639 language tags.
840 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
841 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
844 SupportedLang = "engfraengfra"
846 Iso639Language = TRUE
847 The return value is "0".
849 SupportedLang = "en;fr;en-US;fr-FR"
851 Iso639Language = FALSE
852 The return value is "3".
854 @param SupportedLang Platform supported language codes.
855 @param Lang Configured language.
856 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
858 @retval The index of language in the language codes.
862 GetIndexFromSupportedLangCodes(
863 IN CHAR8
*SupportedLang
,
865 IN BOOLEAN Iso639Language
870 UINTN LanguageLength
;
872 if (Iso639Language
) {
873 CompareLength
= ISO_639_2_ENTRY_SIZE
;
874 for (Index
= 0; Index
< AsciiStrLen (SupportedLang
); Index
+= CompareLength
) {
875 if (AsciiStrnCmp (Lang
, SupportedLang
+ Index
, CompareLength
) == 0) {
877 // Successfully find the index of Lang string in SupportedLang string.
879 Index
= Index
/ CompareLength
;
887 // Compare RFC4646 language code
890 for (LanguageLength
= 0; Lang
[LanguageLength
] != '\0'; LanguageLength
++);
892 for (Index
= 0; *SupportedLang
!= '\0'; Index
++, SupportedLang
+= CompareLength
) {
894 // Skip ';' characters in SupportedLang
896 for (; *SupportedLang
!= '\0' && *SupportedLang
== ';'; SupportedLang
++);
898 // Determine the length of the next language code in SupportedLang
900 for (CompareLength
= 0; SupportedLang
[CompareLength
] != '\0' && SupportedLang
[CompareLength
] != ';'; CompareLength
++);
902 if ((CompareLength
== LanguageLength
) &&
903 (AsciiStrnCmp (Lang
, SupportedLang
, CompareLength
) == 0)) {
905 // Successfully find the index of Lang string in SupportedLang string.
916 Get language string from supported language codes according to index.
918 This code is used to get corresponding language strings in supported language codes. It can handle
919 RFC4646 and ISO639 language tags.
920 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
921 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
924 SupportedLang = "engfraengfra"
926 Iso639Language = TRUE
927 The return value is "fra".
929 SupportedLang = "en;fr;en-US;fr-FR"
931 Iso639Language = FALSE
932 The return value is "fr".
934 @param SupportedLang Platform supported language codes.
935 @param Index The index in supported language codes.
936 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
938 @retval The language string in the language codes.
942 GetLangFromSupportedLangCodes (
943 IN CHAR8
*SupportedLang
,
945 IN BOOLEAN Iso639Language
953 Supported
= SupportedLang
;
954 if (Iso639Language
) {
956 // According to the index of Lang string in SupportedLang string to get the language.
957 // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.
958 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
960 CompareLength
= ISO_639_2_ENTRY_SIZE
;
961 mVariableModuleGlobal
->Lang
[CompareLength
] = '\0';
962 return CopyMem (mVariableModuleGlobal
->Lang
, SupportedLang
+ Index
* CompareLength
, CompareLength
);
967 // Take semicolon as delimitation, sequentially traverse supported language codes.
969 for (CompareLength
= 0; *Supported
!= ';' && *Supported
!= '\0'; CompareLength
++) {
972 if ((*Supported
== '\0') && (SubIndex
!= Index
)) {
974 // Have completed the traverse, but not find corrsponding string.
975 // This case is not allowed to happen.
980 if (SubIndex
== Index
) {
982 // According to the index of Lang string in SupportedLang string to get the language.
983 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
984 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
986 mVariableModuleGlobal
->PlatformLang
[CompareLength
] = '\0';
987 return CopyMem (mVariableModuleGlobal
->PlatformLang
, Supported
- CompareLength
, CompareLength
);
992 // Skip ';' characters in Supported
994 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1000 Returns a pointer to an allocated buffer that contains the best matching language
1001 from a set of supported languages.
1003 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1004 code types may not be mixed in a single call to this function. This function
1005 supports a variable argument list that allows the caller to pass in a prioritized
1006 list of language codes to test against all the language codes in SupportedLanguages.
1008 If SupportedLanguages is NULL, then ASSERT().
1010 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1011 contains a set of language codes in the format
1012 specified by Iso639Language.
1013 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1014 in ISO 639-2 format. If FALSE, then all language
1015 codes are assumed to be in RFC 4646 language format
1016 @param[in] ... A variable argument list that contains pointers to
1017 Null-terminated ASCII strings that contain one or more
1018 language codes in the format specified by Iso639Language.
1019 The first language code from each of these language
1020 code lists is used to determine if it is an exact or
1021 close match to any of the language codes in
1022 SupportedLanguages. Close matches only apply to RFC 4646
1023 language codes, and the matching algorithm from RFC 4647
1024 is used to determine if a close match is present. If
1025 an exact or close match is found, then the matching
1026 language code from SupportedLanguages is returned. If
1027 no matches are found, then the next variable argument
1028 parameter is evaluated. The variable argument list
1029 is terminated by a NULL.
1031 @retval NULL The best matching language could not be found in SupportedLanguages.
1032 @retval NULL There are not enough resources available to return the best matching
1034 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1035 language in SupportedLanguages.
1040 VariableGetBestLanguage (
1041 IN CONST CHAR8
*SupportedLanguages
,
1042 IN BOOLEAN Iso639Language
,
1048 UINTN CompareLength
;
1049 UINTN LanguageLength
;
1050 CONST CHAR8
*Supported
;
1053 if (SupportedLanguages
== NULL
) {
1057 VA_START (Args
, Iso639Language
);
1058 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1060 // Default to ISO 639-2 mode
1063 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1066 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1068 if (!Iso639Language
) {
1069 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1073 // Trim back the length of Language used until it is empty
1075 while (LanguageLength
> 0) {
1077 // Loop through all language codes in SupportedLanguages
1079 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1081 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1083 if (!Iso639Language
) {
1085 // Skip ';' characters in Supported
1087 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1089 // Determine the length of the next language code in Supported
1091 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1093 // If Language is longer than the Supported, then skip to the next language
1095 if (LanguageLength
> CompareLength
) {
1100 // See if the first LanguageLength characters in Supported match Language
1102 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1105 Buffer
= Iso639Language
? mVariableModuleGlobal
->Lang
: mVariableModuleGlobal
->PlatformLang
;
1106 Buffer
[CompareLength
] = '\0';
1107 return CopyMem (Buffer
, Supported
, CompareLength
);
1111 if (Iso639Language
) {
1113 // If ISO 639 mode, then each language can only be tested once
1118 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1120 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1127 // No matches were found
1133 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1135 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1137 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1138 and are read-only. Therefore, in variable driver, only store the original value for other use.
1140 @param[in] VariableName Name of variable.
1142 @param[in] Data Variable data.
1144 @param[in] DataSize Size of data. 0 means delete.
1148 AutoUpdateLangVariable(
1149 IN CHAR16
*VariableName
,
1155 CHAR8
*BestPlatformLang
;
1159 VARIABLE_POINTER_TRACK Variable
;
1160 BOOLEAN SetLanguageCodes
;
1163 // Don't do updates for delete operation
1165 if (DataSize
== 0) {
1169 SetLanguageCodes
= FALSE
;
1171 if (StrCmp (VariableName
, L
"PlatformLangCodes") == 0) {
1173 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1179 SetLanguageCodes
= TRUE
;
1182 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1183 // Therefore, in variable driver, only store the original value for other use.
1185 if (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) {
1186 FreePool (mVariableModuleGlobal
->PlatformLangCodes
);
1188 mVariableModuleGlobal
->PlatformLangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1189 ASSERT (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
);
1192 // PlatformLang holds a single language from PlatformLangCodes,
1193 // so the size of PlatformLangCodes is enough for the PlatformLang.
1195 if (mVariableModuleGlobal
->PlatformLang
!= NULL
) {
1196 FreePool (mVariableModuleGlobal
->PlatformLang
);
1198 mVariableModuleGlobal
->PlatformLang
= AllocateRuntimePool (DataSize
);
1199 ASSERT (mVariableModuleGlobal
->PlatformLang
!= NULL
);
1201 } else if (StrCmp (VariableName
, L
"LangCodes") == 0) {
1203 // LangCodes is a volatile variable, so it can not be updated at runtime.
1209 SetLanguageCodes
= TRUE
;
1212 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1213 // Therefore, in variable driver, only store the original value for other use.
1215 if (mVariableModuleGlobal
->LangCodes
!= NULL
) {
1216 FreePool (mVariableModuleGlobal
->LangCodes
);
1218 mVariableModuleGlobal
->LangCodes
= AllocateRuntimeCopyPool (DataSize
, Data
);
1219 ASSERT (mVariableModuleGlobal
->LangCodes
!= NULL
);
1222 if (SetLanguageCodes
1223 && (mVariableModuleGlobal
->PlatformLangCodes
!= NULL
)
1224 && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1226 // Update Lang if PlatformLang is already set
1227 // Update PlatformLang if Lang is already set
1229 Status
= FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*) mVariableModuleGlobal
);
1230 if (!EFI_ERROR (Status
)) {
1234 VariableName
= L
"PlatformLang";
1235 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1236 DataSize
= Variable
.CurrPtr
->DataSize
;
1238 Status
= FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*) mVariableModuleGlobal
);
1239 if (!EFI_ERROR (Status
)) {
1241 // Update PlatformLang
1243 VariableName
= L
"Lang";
1244 Data
= GetVariableDataPtr (Variable
.CurrPtr
);
1245 DataSize
= Variable
.CurrPtr
->DataSize
;
1248 // Neither PlatformLang nor Lang is set, directly return
1256 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1258 Attributes
= EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1260 if (StrCmp (VariableName
, L
"PlatformLang") == 0) {
1262 // Update Lang when PlatformLangCodes/LangCodes were set.
1264 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1266 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1268 BestPlatformLang
= VariableGetBestLanguage (mVariableModuleGlobal
->PlatformLangCodes
, FALSE
, Data
, NULL
);
1269 if (BestPlatformLang
!= NULL
) {
1271 // Get the corresponding index in language codes.
1273 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, BestPlatformLang
, FALSE
);
1276 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1278 BestLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, Index
, TRUE
);
1281 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1283 FindVariable (L
"Lang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
);
1285 Status
= UpdateVariable (L
"Lang", &gEfiGlobalVariableGuid
, BestLang
,
1286 ISO_639_2_ENTRY_SIZE
+ 1, Attributes
, 0, 0, &Variable
, NULL
);
1288 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang
, BestLang
));
1290 ASSERT_EFI_ERROR(Status
);
1294 } else if (StrCmp (VariableName
, L
"Lang") == 0) {
1296 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1298 if ((mVariableModuleGlobal
->PlatformLangCodes
!= NULL
) && (mVariableModuleGlobal
->LangCodes
!= NULL
)) {
1300 // When setting Lang, firstly get most matched language string from supported language codes.
1302 BestLang
= VariableGetBestLanguage (mVariableModuleGlobal
->LangCodes
, TRUE
, Data
, NULL
);
1303 if (BestLang
!= NULL
) {
1305 // Get the corresponding index in language codes.
1307 Index
= GetIndexFromSupportedLangCodes (mVariableModuleGlobal
->LangCodes
, BestLang
, TRUE
);
1310 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1312 BestPlatformLang
= GetLangFromSupportedLangCodes (mVariableModuleGlobal
->PlatformLangCodes
, Index
, FALSE
);
1315 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1317 FindVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, &Variable
, (VARIABLE_GLOBAL
*)mVariableModuleGlobal
);
1319 Status
= UpdateVariable (L
"PlatformLang", &gEfiGlobalVariableGuid
, BestPlatformLang
,
1320 AsciiStrSize (BestPlatformLang
), Attributes
, 0, 0, &Variable
, NULL
);
1322 DEBUG ((EFI_D_INFO
, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang
, BestPlatformLang
));
1323 ASSERT_EFI_ERROR (Status
);
1330 Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
1331 index of associated public key is needed.
1333 @param[in] VariableName Name of variable.
1334 @param[in] VendorGuid Guid of variable.
1335 @param[in] Data Variable data.
1336 @param[in] DataSize Size of data. 0 means delete.
1337 @param[in] Attributes Attributes of the variable.
1338 @param[in] KeyIndex Index of associated public key.
1339 @param[in] MonotonicCount Value of associated monotonic count.
1340 @param[in] CacheVariable The variable information which is used to keep track of variable usage.
1341 @param[in] TimeStamp Value of associated TimeStamp.
1343 @retval EFI_SUCCESS The update operation is success.
1344 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1349 IN CHAR16
*VariableName
,
1350 IN EFI_GUID
*VendorGuid
,
1353 IN UINT32 Attributes OPTIONAL
,
1354 IN UINT32 KeyIndex OPTIONAL
,
1355 IN UINT64 MonotonicCount OPTIONAL
,
1356 IN VARIABLE_POINTER_TRACK
*CacheVariable
,
1357 IN EFI_TIME
*TimeStamp OPTIONAL
1361 VARIABLE_HEADER
*NextVariable
;
1363 UINTN ScratchDataSize
;
1364 UINTN NonVolatileVarableStoreSize
;
1365 UINTN VarNameOffset
;
1366 UINTN VarDataOffset
;
1370 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
1373 VARIABLE_POINTER_TRACK
*Variable
;
1374 VARIABLE_POINTER_TRACK NvVariable
;
1375 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
1381 if (mVariableModuleGlobal
->FvbInstance
== NULL
) {
1383 // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.
1385 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1387 // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1389 return EFI_NOT_AVAILABLE_YET
;
1390 } else if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
1392 // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL
1393 // The authenticated variable perhaps is not initialized, just return here.
1395 return EFI_NOT_AVAILABLE_YET
;
1399 if ((CacheVariable
->CurrPtr
== NULL
) || CacheVariable
->Volatile
) {
1400 Variable
= CacheVariable
;
1403 // Update/Delete existing NV variable.
1404 // CacheVariable points to the variable in the memory copy of Flash area
1405 // Now let Variable points to the same variable in Flash area.
1407 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1408 Variable
= &NvVariable
;
1409 Variable
->StartPtr
= GetStartPointer (VariableStoreHeader
);
1410 Variable
->EndPtr
= GetEndPointer (VariableStoreHeader
);
1411 Variable
->CurrPtr
= (VARIABLE_HEADER
*)((UINTN
)Variable
->StartPtr
+ ((UINTN
)CacheVariable
->CurrPtr
- (UINTN
)CacheVariable
->StartPtr
));
1412 Variable
->Volatile
= FALSE
;
1415 Fvb
= mVariableModuleGlobal
->FvbInstance
;
1419 // Tricky part: Use scratch data area at the end of volatile variable store
1420 // as a temporary storage.
1422 NextVariable
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
));
1423 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
1424 ScratchDataSize
= ScratchSize
- sizeof (VARIABLE_HEADER
) - StrSize (VariableName
) - GET_PAD_SIZE (StrSize (VariableName
));
1426 if (Variable
->CurrPtr
!= NULL
) {
1428 // Update/Delete existing variable.
1432 // If AtRuntime and the variable is Volatile and Runtime Access,
1433 // the volatile is ReadOnly, and SetVariable should be aborted and
1434 // return EFI_WRITE_PROTECTED.
1436 if (Variable
->Volatile
) {
1437 Status
= EFI_WRITE_PROTECTED
;
1441 // Only variable that have NV attributes can be updated/deleted in Runtime.
1443 if ((Variable
->CurrPtr
->Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
1444 Status
= EFI_INVALID_PARAMETER
;
1450 // Setting a data variable with no access, or zero DataSize attributes
1451 // causes it to be deleted.
1452 // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will
1453 // not delete the variable.
1455 if ((((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) && (DataSize
== 0))|| ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0)) {
1456 State
= Variable
->CurrPtr
->State
;
1457 State
&= VAR_DELETED
;
1459 Status
= UpdateVariableStore (
1460 &mVariableModuleGlobal
->VariableGlobal
,
1464 (UINTN
) &Variable
->CurrPtr
->State
,
1468 if (!EFI_ERROR (Status
)) {
1469 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, FALSE
, TRUE
, FALSE
);
1470 if (!Variable
->Volatile
) {
1471 CacheVariable
->CurrPtr
->State
= State
;
1477 // If the variable is marked valid, and the same data has been passed in,
1478 // then return to the caller immediately.
1480 if (DataSizeOfVariable (Variable
->CurrPtr
) == DataSize
&&
1481 (CompareMem (Data
, GetVariableDataPtr (Variable
->CurrPtr
), DataSize
) == 0) &&
1482 ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0)) {
1484 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
->Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1485 Status
= EFI_SUCCESS
;
1487 } else if ((Variable
->CurrPtr
->State
== VAR_ADDED
) ||
1488 (Variable
->CurrPtr
->State
== (VAR_ADDED
& VAR_IN_DELETED_TRANSITION
))) {
1491 // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable
1493 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
1495 BufSize
= Variable
->CurrPtr
->DataSize
+ DataSize
;
1496 RevBufSize
= MIN (PcdGet32 (PcdMaxAppendVariableSize
), ScratchDataSize
);
1498 if (BufSize
> RevBufSize
) {
1500 // If variable size (previous + current) is bigger than reserved buffer in runtime,
1501 // return EFI_OUT_OF_RESOURCES.
1503 return EFI_OUT_OF_RESOURCES
;
1506 SetMem (mStorageArea
, PcdGet32 (PcdMaxAppendVariableSize
), 0xff);
1508 // Cache the previous variable data into StorageArea.
1510 DataOffset
= sizeof (VARIABLE_HEADER
) + Variable
->CurrPtr
->NameSize
+ GET_PAD_SIZE (Variable
->CurrPtr
->NameSize
);
1511 CopyMem (mStorageArea
, (UINT8
*)((UINTN
)Variable
->CurrPtr
+ DataOffset
), Variable
->CurrPtr
->DataSize
);
1514 // Append the new data to the end of previous data.
1516 CopyMem ((UINT8
*)((UINTN
)mStorageArea
+ Variable
->CurrPtr
->DataSize
), Data
, DataSize
);
1519 // Override Data and DataSize which are used for combined data area including previous and new data.
1521 Data
= mStorageArea
;
1526 // Mark the old variable as in delete transition.
1528 State
= Variable
->CurrPtr
->State
;
1529 State
&= VAR_IN_DELETED_TRANSITION
;
1531 Status
= UpdateVariableStore (
1532 &mVariableModuleGlobal
->VariableGlobal
,
1536 (UINTN
) &Variable
->CurrPtr
->State
,
1540 if (EFI_ERROR (Status
)) {
1543 if (!Variable
->Volatile
) {
1544 CacheVariable
->CurrPtr
->State
= State
;
1549 // Not found existing variable. Create a new variable.
1553 // EFI_VARIABLE_APPEND_WRITE attribute only set for existing variable
1555 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) != 0) {
1556 Status
= EFI_INVALID_PARAMETER
;
1561 // Make sure we are trying to create a new variable.
1562 // Setting a data variable with zero DataSize or no access attributes means to delete it.
1564 if (DataSize
== 0 || (Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == 0) {
1565 Status
= EFI_NOT_FOUND
;
1570 // Only variable have NV|RT attribute can be created in Runtime.
1573 (((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0) || ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0))) {
1574 Status
= EFI_INVALID_PARAMETER
;
1580 // Function part - create a new variable and copy the data.
1581 // Both update a variable and create a variable will come here.
1583 SetMem (NextVariable
, ScratchSize
, 0xff);
1585 NextVariable
->StartId
= VARIABLE_DATA
;
1587 // NextVariable->State = VAR_ADDED;
1589 NextVariable
->Reserved
= 0;
1590 NextVariable
->PubKeyIndex
= KeyIndex
;
1591 NextVariable
->MonotonicCount
= MonotonicCount
;
1592 SetMem (&NextVariable
->TimeStamp
, sizeof (EFI_TIME
), 0);
1594 if (((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) &&
1595 TimeStamp
!= NULL
) {
1596 if ((Attributes
& EFI_VARIABLE_APPEND_WRITE
) == 0) {
1597 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1600 // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only
1601 // when the new TimeStamp value is later than the current timestamp associated
1602 // with the variable, we need associate the new timestamp with the updated value.
1604 if (CompareTimeStamp (&Variable
->CurrPtr
->TimeStamp
, TimeStamp
)) {
1605 CopyMem (&NextVariable
->TimeStamp
, TimeStamp
, sizeof (EFI_TIME
));
1611 // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned
1612 // Attributes bitmask parameter of a GetVariable() call.
1614 NextVariable
->Attributes
= Attributes
& (~EFI_VARIABLE_APPEND_WRITE
);
1616 VarNameOffset
= sizeof (VARIABLE_HEADER
);
1617 VarNameSize
= StrSize (VariableName
);
1619 (UINT8
*) ((UINTN
) NextVariable
+ VarNameOffset
),
1623 VarDataOffset
= VarNameOffset
+ VarNameSize
+ GET_PAD_SIZE (VarNameSize
);
1625 (UINT8
*) ((UINTN
) NextVariable
+ VarDataOffset
),
1629 CopyMem (&NextVariable
->VendorGuid
, VendorGuid
, sizeof (EFI_GUID
));
1631 // There will be pad bytes after Data, the NextVariable->NameSize and
1632 // NextVariable->DataSize should not include pad size so that variable
1633 // service can get actual size in GetVariable.
1635 NextVariable
->NameSize
= (UINT32
)VarNameSize
;
1636 NextVariable
->DataSize
= (UINT32
)DataSize
;
1639 // The actual size of the variable that stores in storage should
1640 // include pad size.
1642 VarSize
= VarDataOffset
+ DataSize
+ GET_PAD_SIZE (DataSize
);
1643 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
1645 // Create a nonvolatile variable.
1648 NonVolatileVarableStoreSize
= ((VARIABLE_STORE_HEADER
*)(UINTN
)(mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
))->Size
;
1649 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1650 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1651 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1652 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1654 Status
= EFI_OUT_OF_RESOURCES
;
1658 // Perform garbage collection & reclaim operation.
1660 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
1661 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
, FALSE
, Variable
->CurrPtr
);
1662 if (EFI_ERROR (Status
)) {
1666 // If still no enough space, return out of resources.
1668 if ((((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0)
1669 && ((VarSize
+ mVariableModuleGlobal
->HwErrVariableTotalSize
) > PcdGet32 (PcdHwErrStorageSize
)))
1670 || (((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == 0)
1671 && ((VarSize
+ mVariableModuleGlobal
->CommonVariableTotalSize
) > NonVolatileVarableStoreSize
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
)))) {
1672 Status
= EFI_OUT_OF_RESOURCES
;
1679 // 1. Write variable header
1680 // 2. Set variable state to header valid
1681 // 3. Write variable data
1682 // 4. Set variable state to valid
1687 CacheOffset
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
;
1688 Status
= UpdateVariableStore (
1689 &mVariableModuleGlobal
->VariableGlobal
,
1693 mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
1694 sizeof (VARIABLE_HEADER
),
1695 (UINT8
*) NextVariable
1698 if (EFI_ERROR (Status
)) {
1705 NextVariable
->State
= VAR_HEADER_VALID_ONLY
;
1706 Status
= UpdateVariableStore (
1707 &mVariableModuleGlobal
->VariableGlobal
,
1711 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1713 &NextVariable
->State
1716 if (EFI_ERROR (Status
)) {
1722 Status
= UpdateVariableStore (
1723 &mVariableModuleGlobal
->VariableGlobal
,
1727 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ sizeof (VARIABLE_HEADER
),
1728 (UINT32
) VarSize
- sizeof (VARIABLE_HEADER
),
1729 (UINT8
*) NextVariable
+ sizeof (VARIABLE_HEADER
)
1732 if (EFI_ERROR (Status
)) {
1738 NextVariable
->State
= VAR_ADDED
;
1739 Status
= UpdateVariableStore (
1740 &mVariableModuleGlobal
->VariableGlobal
,
1744 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+ OFFSET_OF (VARIABLE_HEADER
, State
),
1746 &NextVariable
->State
1749 if (EFI_ERROR (Status
)) {
1753 mVariableModuleGlobal
->NonVolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1755 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) != 0) {
1756 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1758 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VarSize
);
1761 // update the memory copy of Flash region.
1763 CopyMem ((UINT8
*)mNvVariableCache
+ CacheOffset
, (UINT8
*)NextVariable
, VarSize
);
1766 // Create a volatile variable.
1770 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1771 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
) {
1773 // Perform garbage collection & reclaim operation.
1775 Status
= Reclaim (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
,
1776 &mVariableModuleGlobal
->VolatileLastVariableOffset
, TRUE
, Variable
->CurrPtr
);
1777 if (EFI_ERROR (Status
)) {
1781 // If still no enough space, return out of resources.
1783 if ((UINT32
) (VarSize
+ mVariableModuleGlobal
->VolatileLastVariableOffset
) >
1784 ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
)))->Size
1786 Status
= EFI_OUT_OF_RESOURCES
;
1792 NextVariable
->State
= VAR_ADDED
;
1793 Status
= UpdateVariableStore (
1794 &mVariableModuleGlobal
->VariableGlobal
,
1798 mVariableModuleGlobal
->VolatileLastVariableOffset
,
1800 (UINT8
*) NextVariable
1803 if (EFI_ERROR (Status
)) {
1807 mVariableModuleGlobal
->VolatileLastVariableOffset
+= HEADER_ALIGN (VarSize
);
1811 // Mark the old variable as deleted.
1813 if (!Reclaimed
&& !EFI_ERROR (Status
) && Variable
->CurrPtr
!= NULL
) {
1814 State
= Variable
->CurrPtr
->State
;
1815 State
&= VAR_DELETED
;
1817 Status
= UpdateVariableStore (
1818 &mVariableModuleGlobal
->VariableGlobal
,
1822 (UINTN
) &Variable
->CurrPtr
->State
,
1826 if (!EFI_ERROR (Status
) && !Variable
->Volatile
) {
1827 CacheVariable
->CurrPtr
->State
= State
;
1831 if (!EFI_ERROR (Status
)) {
1832 UpdateVariableInfo (VariableName
, VendorGuid
, Volatile
, FALSE
, TRUE
, FALSE
, FALSE
);
1841 This code finds variable in storage blocks (Volatile or Non-Volatile).
1843 @param VariableName Name of Variable to be found.
1844 @param VendorGuid Variable vendor GUID.
1845 @param Attributes Attribute value of the variable found.
1846 @param DataSize Size of Data found. If size is less than the
1847 data, this value contains the required size.
1848 @param Data Data pointer.
1850 @return EFI_INVALID_PARAMETER Invalid parameter.
1851 @return EFI_SUCCESS Find the specified variable.
1852 @return EFI_NOT_FOUND Not found.
1853 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1858 VariableServiceGetVariable (
1859 IN CHAR16
*VariableName
,
1860 IN EFI_GUID
*VendorGuid
,
1861 OUT UINT32
*Attributes OPTIONAL
,
1862 IN OUT UINTN
*DataSize
,
1867 VARIABLE_POINTER_TRACK Variable
;
1870 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
1871 return EFI_INVALID_PARAMETER
;
1874 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1876 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1877 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1884 VarDataSize
= DataSizeOfVariable (Variable
.CurrPtr
);
1885 ASSERT (VarDataSize
!= 0);
1887 if (*DataSize
>= VarDataSize
) {
1889 Status
= EFI_INVALID_PARAMETER
;
1893 CopyMem (Data
, GetVariableDataPtr (Variable
.CurrPtr
), VarDataSize
);
1894 if (Attributes
!= NULL
) {
1895 *Attributes
= Variable
.CurrPtr
->Attributes
;
1898 *DataSize
= VarDataSize
;
1899 UpdateVariableInfo (VariableName
, VendorGuid
, Variable
.Volatile
, TRUE
, FALSE
, FALSE
, FALSE
);
1901 Status
= EFI_SUCCESS
;
1904 *DataSize
= VarDataSize
;
1905 Status
= EFI_BUFFER_TOO_SMALL
;
1910 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1918 This code Finds the Next available variable.
1920 @param VariableNameSize Size of the variable name.
1921 @param VariableName Pointer to variable name.
1922 @param VendorGuid Variable Vendor Guid.
1924 @return EFI_INVALID_PARAMETER Invalid parameter.
1925 @return EFI_SUCCESS Find the specified variable.
1926 @return EFI_NOT_FOUND Not found.
1927 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1932 VariableServiceGetNextVariableName (
1933 IN OUT UINTN
*VariableNameSize
,
1934 IN OUT CHAR16
*VariableName
,
1935 IN OUT EFI_GUID
*VendorGuid
1938 VARIABLE_POINTER_TRACK Variable
;
1942 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1943 return EFI_INVALID_PARAMETER
;
1946 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
1948 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
1949 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
1953 if (VariableName
[0] != 0) {
1955 // If variable name is not NULL, get next variable.
1957 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
1962 // If both volatile and non-volatile variable store are parsed,
1963 // return not found.
1965 if (Variable
.CurrPtr
>= Variable
.EndPtr
|| Variable
.CurrPtr
== NULL
) {
1966 Variable
.Volatile
= (BOOLEAN
) (Variable
.Volatile
^ ((BOOLEAN
) 0x1));
1967 if (!Variable
.Volatile
) {
1968 Variable
.StartPtr
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
1969 Variable
.EndPtr
= GetEndPointer ((VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
));
1971 Status
= EFI_NOT_FOUND
;
1975 Variable
.CurrPtr
= Variable
.StartPtr
;
1976 if (!IsValidVariableHeader (Variable
.CurrPtr
)) {
1981 // Variable is found
1983 if (IsValidVariableHeader (Variable
.CurrPtr
) && Variable
.CurrPtr
->State
== VAR_ADDED
) {
1984 if ((AtRuntime () && ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) == 0) {
1985 VarNameSize
= NameSizeOfVariable (Variable
.CurrPtr
);
1986 ASSERT (VarNameSize
!= 0);
1988 if (VarNameSize
<= *VariableNameSize
) {
1991 GetVariableNamePtr (Variable
.CurrPtr
),
1996 &Variable
.CurrPtr
->VendorGuid
,
1999 Status
= EFI_SUCCESS
;
2001 Status
= EFI_BUFFER_TOO_SMALL
;
2004 *VariableNameSize
= VarNameSize
;
2009 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
2013 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2019 This code sets variable in storage blocks (Volatile or Non-Volatile).
2021 @param VariableName Name of Variable to be found.
2022 @param VendorGuid Variable vendor GUID.
2023 @param Attributes Attribute value of the variable found
2024 @param DataSize Size of Data found. If size is less than the
2025 data, this value contains the required size.
2026 @param Data Data pointer.
2028 @return EFI_INVALID_PARAMETER Invalid parameter.
2029 @return EFI_SUCCESS Set successfully.
2030 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
2031 @return EFI_NOT_FOUND Not found.
2032 @return EFI_WRITE_PROTECTED Variable is read-only.
2037 VariableServiceSetVariable (
2038 IN CHAR16
*VariableName
,
2039 IN EFI_GUID
*VendorGuid
,
2040 IN UINT32 Attributes
,
2045 VARIABLE_POINTER_TRACK Variable
;
2047 VARIABLE_HEADER
*NextVariable
;
2048 EFI_PHYSICAL_ADDRESS Point
;
2052 // Check input parameters.
2054 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
2055 return EFI_INVALID_PARAMETER
;
2058 if (DataSize
!= 0 && Data
== NULL
) {
2059 return EFI_INVALID_PARAMETER
;
2063 // Make sure if runtime bit is set, boot service bit is set also.
2065 if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2066 return EFI_INVALID_PARAMETER
;
2070 // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute
2071 // cannot be set both.
2073 if (((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) \
2074 && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)) {
2075 return EFI_INVALID_PARAMETER
;
2078 if ((Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) {
2079 if (DataSize
< AUTHINFO_SIZE
) {
2081 // Try to write Authencated Variable without AuthInfo.
2083 return EFI_SECURITY_VIOLATION
;
2085 PayloadSize
= DataSize
- AUTHINFO_SIZE
;
2087 PayloadSize
= DataSize
;
2090 // The size of the VariableName, including the Unicode Null in bytes plus
2091 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
2092 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
2094 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2095 if ((PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
)) ||
2096 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxHardwareErrorVariableSize
))) {
2097 return EFI_INVALID_PARAMETER
;
2100 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX".
2102 if (StrnCmp(VariableName
, L
"HwErrRec", StrLen(L
"HwErrRec")) != 0) {
2103 return EFI_INVALID_PARAMETER
;
2107 // The size of the VariableName, including the Unicode Null in bytes plus
2108 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
2110 if ((PayloadSize
> PcdGet32 (PcdMaxVariableSize
)) ||
2111 (sizeof (VARIABLE_HEADER
) + StrSize (VariableName
) + PayloadSize
> PcdGet32 (PcdMaxVariableSize
))) {
2112 return EFI_INVALID_PARAMETER
;
2116 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2119 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.
2121 if (1 < InterlockedIncrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
)) {
2122 Point
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2124 // Parse non-volatile variable data and get last variable offset.
2126 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
);
2127 while ((NextVariable
< GetEndPointer ((VARIABLE_STORE_HEADER
*) (UINTN
) Point
))
2128 && IsValidVariableHeader (NextVariable
)) {
2129 NextVariable
= GetNextVariablePtr (NextVariable
);
2131 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) Point
;
2135 // Check whether the input variable is already existed.
2137 FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
);
2140 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
2142 AutoUpdateLangVariable (VariableName
, Data
, DataSize
);
2144 // Process PK, KEK, Sigdb seperately.
2146 if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0)){
2147 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, TRUE
);
2148 } else if (CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
) && (StrCmp (VariableName
, EFI_KEY_EXCHANGE_KEY_NAME
) == 0)) {
2149 Status
= ProcessVarWithPk (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
, FALSE
);
2150 } else if (CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
) && ((Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) == 0)) {
2151 Status
= ProcessVarWithKek (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2153 Status
= ProcessVariable (VariableName
, VendorGuid
, Data
, DataSize
, &Variable
, Attributes
);
2156 InterlockedDecrement (&mVariableModuleGlobal
->VariableGlobal
.ReentrantState
);
2157 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2164 This code returns information about the EFI variables.
2166 @param Attributes Attributes bitmask to specify the type of variables
2167 on which to return information.
2168 @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
2169 for the EFI variables associated with the attributes specified.
2170 @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
2171 for EFI variables associated with the attributes specified.
2172 @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
2173 associated with the attributes specified.
2175 @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2176 @return EFI_SUCCESS Query successfully.
2177 @return EFI_UNSUPPORTED The attribute is not supported on this platform.
2182 VariableServiceQueryVariableInfo (
2183 IN UINT32 Attributes
,
2184 OUT UINT64
*MaximumVariableStorageSize
,
2185 OUT UINT64
*RemainingVariableStorageSize
,
2186 OUT UINT64
*MaximumVariableSize
2189 VARIABLE_HEADER
*Variable
;
2190 VARIABLE_HEADER
*NextVariable
;
2191 UINT64 VariableSize
;
2192 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2193 UINT64 CommonVariableTotalSize
;
2194 UINT64 HwErrVariableTotalSize
;
2196 CommonVariableTotalSize
= 0;
2197 HwErrVariableTotalSize
= 0;
2199 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
2200 return EFI_INVALID_PARAMETER
;
2203 if((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == 0) {
2205 // Make sure the Attributes combination is supported by the platform.
2207 return EFI_UNSUPPORTED
;
2208 } else if ((Attributes
& (EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
)) == EFI_VARIABLE_RUNTIME_ACCESS
) {
2210 // Make sure if runtime bit is set, boot service bit is set also.
2212 return EFI_INVALID_PARAMETER
;
2213 } else if (AtRuntime () && ((Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) == 0)) {
2215 // Make sure RT Attribute is set if we are in Runtime phase.
2217 return EFI_INVALID_PARAMETER
;
2218 } else if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2220 // Make sure Hw Attribute is set with NV.
2222 return EFI_INVALID_PARAMETER
;
2225 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2227 if((Attributes
& EFI_VARIABLE_NON_VOLATILE
) == 0) {
2229 // Query is Volatile related.
2231 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*) ((UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
2234 // Query is Non-Volatile related.
2236 VariableStoreHeader
= mNvVariableCache
;
2240 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2241 // with the storage size (excluding the storage header size).
2243 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
);
2246 // Harware error record variable needs larger size.
2248 if ((Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2249 *MaximumVariableStorageSize
= PcdGet32 (PcdHwErrStorageSize
);
2250 *MaximumVariableSize
= PcdGet32 (PcdMaxHardwareErrorVariableSize
) - sizeof (VARIABLE_HEADER
);
2252 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
2253 ASSERT (PcdGet32 (PcdHwErrStorageSize
) < VariableStoreHeader
->Size
);
2254 *MaximumVariableStorageSize
= VariableStoreHeader
->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32 (PcdHwErrStorageSize
);
2258 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
2260 *MaximumVariableSize
= PcdGet32 (PcdMaxVariableSize
) - sizeof (VARIABLE_HEADER
);
2264 // Point to the starting address of the variables.
2266 Variable
= GetStartPointer (VariableStoreHeader
);
2269 // Now walk through the related variable store.
2271 while ((Variable
< GetEndPointer (VariableStoreHeader
)) && IsValidVariableHeader (Variable
)) {
2272 NextVariable
= GetNextVariablePtr (Variable
);
2273 VariableSize
= (UINT64
) (UINTN
) NextVariable
- (UINT64
) (UINTN
) Variable
;
2277 // We don't take the state of the variables in mind
2278 // when calculating RemainingVariableStorageSize,
2279 // since the space occupied by variables not marked with
2280 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2282 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2283 HwErrVariableTotalSize
+= VariableSize
;
2285 CommonVariableTotalSize
+= VariableSize
;
2289 // Only care about Variables with State VAR_ADDED, because
2290 // the space not marked as VAR_ADDED is reclaimable now.
2292 if (Variable
->State
== VAR_ADDED
) {
2293 if ((Variable
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
) {
2294 HwErrVariableTotalSize
+= VariableSize
;
2296 CommonVariableTotalSize
+= VariableSize
;
2302 // Go to the next one.
2304 Variable
= NextVariable
;
2307 if ((Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
) == EFI_VARIABLE_HARDWARE_ERROR_RECORD
){
2308 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- HwErrVariableTotalSize
;
2310 *RemainingVariableStorageSize
= *MaximumVariableStorageSize
- CommonVariableTotalSize
;
2313 if (*RemainingVariableStorageSize
< sizeof (VARIABLE_HEADER
)) {
2314 *MaximumVariableSize
= 0;
2315 } else if ((*RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
)) < *MaximumVariableSize
) {
2316 *MaximumVariableSize
= *RemainingVariableStorageSize
- sizeof (VARIABLE_HEADER
);
2319 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
);
2325 This function reclaims variable storage if free size is below the threshold.
2334 UINTN CommonVariableSpace
;
2335 UINTN RemainingCommonVariableSpace
;
2336 UINTN RemainingHwErrVariableSpace
;
2338 Status
= EFI_SUCCESS
;
2340 CommonVariableSpace
= ((VARIABLE_STORE_HEADER
*) ((UINTN
) (mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
)))->Size
- sizeof (VARIABLE_STORE_HEADER
) - PcdGet32(PcdHwErrStorageSize
); //Allowable max size of common variable storage space
2342 RemainingCommonVariableSpace
= CommonVariableSpace
- mVariableModuleGlobal
->CommonVariableTotalSize
;
2344 RemainingHwErrVariableSpace
= PcdGet32 (PcdHwErrStorageSize
) - mVariableModuleGlobal
->HwErrVariableTotalSize
;
2346 // Check if the free area is blow a threshold.
2348 if ((RemainingCommonVariableSpace
< PcdGet32 (PcdMaxVariableSize
))
2349 || ((PcdGet32 (PcdHwErrStorageSize
) != 0) &&
2350 (RemainingHwErrVariableSpace
< PcdGet32 (PcdMaxHardwareErrorVariableSize
)))){
2352 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2353 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2357 ASSERT_EFI_ERROR (Status
);
2363 Initializes variable write service after FVB was ready.
2365 @retval EFI_SUCCESS Function successfully executed.
2366 @retval Others Fail to initialize the variable service.
2370 VariableWriteServiceInitialize (
2375 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2378 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2379 UINT64 VariableStoreLength
;
2381 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
2382 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2383 VariableStoreLength
= VariableStoreHeader
->Size
;
2386 // Check if the free area is really free.
2388 for (Index
= mVariableModuleGlobal
->NonVolatileLastVariableOffset
; Index
< VariableStoreLength
; Index
++) {
2389 Data
= ((UINT8
*) mNvVariableCache
)[Index
];
2392 // There must be something wrong in variable store, do reclaim operation.
2395 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
,
2396 &mVariableModuleGlobal
->NonVolatileLastVariableOffset
,
2400 if (EFI_ERROR (Status
)) {
2408 // Authenticated variable initialize.
2410 Status
= AutenticatedVariableServiceInitialize ();
2417 Initializes variable store area for non-volatile and volatile variable.
2419 @retval EFI_SUCCESS Function successfully executed.
2420 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2424 VariableCommonInitialize (
2429 VARIABLE_STORE_HEADER
*VolatileVariableStore
;
2430 VARIABLE_STORE_HEADER
*VariableStoreHeader
;
2431 VARIABLE_HEADER
*NextVariable
;
2432 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader
;
2433 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
2434 UINT64 VariableStoreLength
;
2439 // Allocate runtime memory for variable driver global structure.
2441 mVariableModuleGlobal
= AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL
));
2442 if (mVariableModuleGlobal
== NULL
) {
2443 return EFI_OUT_OF_RESOURCES
;
2446 InitializeLock (&mVariableModuleGlobal
->VariableGlobal
.VariableServicesLock
, TPL_NOTIFY
);
2449 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2450 // is stored with common variable in the same NV region. So the platform integrator should
2451 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
2452 // PcdFlashNvStorageVariableSize.
2454 ASSERT (PcdGet32 (PcdHwErrStorageSize
) <= PcdGet32 (PcdFlashNvStorageVariableSize
));
2457 // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
2459 ScratchSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
2460 VolatileVariableStore
= AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize
) + ScratchSize
);
2461 if (VolatileVariableStore
== NULL
) {
2462 FreePool (mVariableModuleGlobal
);
2463 return EFI_OUT_OF_RESOURCES
;
2466 SetMem (VolatileVariableStore
, PcdGet32 (PcdVariableStoreSize
) + ScratchSize
, 0xff);
2469 // Initialize Variable Specific Data.
2471 mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VolatileVariableStore
;
2472 mVariableModuleGlobal
->VolatileLastVariableOffset
= (UINTN
) GetStartPointer (VolatileVariableStore
) - (UINTN
) VolatileVariableStore
;
2473 mVariableModuleGlobal
->FvbInstance
= NULL
;
2475 CopyGuid (&VolatileVariableStore
->Signature
, &gEfiAuthenticatedVariableGuid
);
2476 VolatileVariableStore
->Size
= PcdGet32 (PcdVariableStoreSize
);
2477 VolatileVariableStore
->Format
= VARIABLE_STORE_FORMATTED
;
2478 VolatileVariableStore
->State
= VARIABLE_STORE_HEALTHY
;
2479 VolatileVariableStore
->Reserved
= 0;
2480 VolatileVariableStore
->Reserved1
= 0;
2483 // Get non-volatile varaible store.
2486 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
2487 if (TempVariableStoreHeader
== 0) {
2488 TempVariableStoreHeader
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
2490 VariableStoreBase
= TempVariableStoreHeader
+ \
2491 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2492 VariableStoreLength
= (UINT64
) PcdGet32 (PcdFlashNvStorageVariableSize
) - \
2493 (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(TempVariableStoreHeader
)) -> HeaderLength
);
2495 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
2496 VariableStoreHeader
= (VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
;
2497 if (GetVariableStoreStatus (VariableStoreHeader
) != EfiValid
) {
2498 Status
= EFI_VOLUME_CORRUPTED
;
2499 DEBUG((EFI_D_INFO
, "Variable Store header is corrupted\n"));
2502 ASSERT(VariableStoreHeader
->Size
== VariableStoreLength
);
2505 // Parse non-volatile variable data and get last variable offset.
2507 NextVariable
= GetStartPointer ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
);
2508 while (IsValidVariableHeader (NextVariable
)) {
2509 VariableSize
= NextVariable
->NameSize
+ NextVariable
->DataSize
+ sizeof (VARIABLE_HEADER
);
2510 if ((NextVariable
->Attributes
& (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) == (EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_HARDWARE_ERROR_RECORD
)) {
2511 mVariableModuleGlobal
->HwErrVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2513 mVariableModuleGlobal
->CommonVariableTotalSize
+= HEADER_ALIGN (VariableSize
);
2516 NextVariable
= GetNextVariablePtr (NextVariable
);
2519 mVariableModuleGlobal
->NonVolatileLastVariableOffset
= (UINTN
) NextVariable
- (UINTN
) VariableStoreBase
;
2522 // Allocate runtime memory used for a memory copy of the FLASH region.
2523 // Keep the memory and the FLASH in sync as updates occur
2525 mNvVariableCache
= AllocateRuntimeZeroPool ((UINTN
)VariableStoreLength
);
2526 if (mNvVariableCache
== NULL
) {
2527 Status
= EFI_OUT_OF_RESOURCES
;
2530 CopyMem (mNvVariableCache
, (CHAR8
*)(UINTN
)VariableStoreBase
, (UINTN
)VariableStoreLength
);
2531 Status
= EFI_SUCCESS
;
2534 if (EFI_ERROR (Status
)) {
2535 FreePool (mVariableModuleGlobal
);
2536 FreePool (VolatileVariableStore
);
2544 Get the proper fvb handle and/or fvb protocol by the given Flash address.
2546 @param[in] Address The Flash address.
2547 @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
2548 @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
2552 GetFvbInfoByAddress (
2553 IN EFI_PHYSICAL_ADDRESS Address
,
2554 OUT EFI_HANDLE
*FvbHandle OPTIONAL
,
2555 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvbProtocol OPTIONAL
2559 EFI_HANDLE
*HandleBuffer
;
2562 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
2563 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
2564 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
2565 EFI_FVB_ATTRIBUTES_2 Attributes
;
2568 // Get all FVB handles.
2570 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
2571 if (EFI_ERROR (Status
)) {
2572 return EFI_NOT_FOUND
;
2576 // Get the FVB to access variable store.
2579 for (Index
= 0; Index
< HandleCount
; Index
+= 1, Status
= EFI_NOT_FOUND
, Fvb
= NULL
) {
2580 Status
= GetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
2581 if (EFI_ERROR (Status
)) {
2582 Status
= EFI_NOT_FOUND
;
2587 // Ensure this FVB protocol supported Write operation.
2589 Status
= Fvb
->GetAttributes (Fvb
, &Attributes
);
2590 if (EFI_ERROR (Status
) || ((Attributes
& EFI_FVB2_WRITE_STATUS
) == 0)) {
2595 // Compare the address and select the right one.
2597 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
2598 if (EFI_ERROR (Status
)) {
2602 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) FvbBaseAddress
);
2603 if ((Address
>= FvbBaseAddress
) && (Address
< (FvbBaseAddress
+ FwVolHeader
->FvLength
))) {
2604 if (FvbHandle
!= NULL
) {
2605 *FvbHandle
= HandleBuffer
[Index
];
2607 if (FvbProtocol
!= NULL
) {
2610 Status
= EFI_SUCCESS
;
2614 FreePool (HandleBuffer
);
2617 Status
= EFI_NOT_FOUND
;