2 Functions in this module are associated with variable parsing operations and
3 are intended to be usable across variable driver source files.
5 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "VariableParsing.h"
14 This code checks if variable header is valid or not.
16 @param[in] Variable Pointer to the Variable Header.
17 @param[in] VariableStoreEnd Pointer to the Variable Store End.
19 @retval TRUE Variable header is valid.
20 @retval FALSE Variable header is not valid.
24 IsValidVariableHeader (
25 IN VARIABLE_HEADER
*Variable
,
26 IN VARIABLE_HEADER
*VariableStoreEnd
29 if ((Variable
== NULL
) || (Variable
>= VariableStoreEnd
) || (Variable
->StartId
!= VARIABLE_DATA
)) {
31 // Variable is NULL or has reached the end of variable store,
32 // or the StartId is not correct.
42 This code gets the current status of Variable Store.
44 @param[in] VarStoreHeader Pointer to the Variable Store Header.
46 @retval EfiRaw Variable store status is raw.
47 @retval EfiValid Variable store status is valid.
48 @retval EfiInvalid Variable store status is invalid.
52 GetVariableStoreStatus (
53 IN VARIABLE_STORE_HEADER
*VarStoreHeader
56 if ((CompareGuid (&VarStoreHeader
->Signature
, &gEfiAuthenticatedVariableGuid
) ||
57 CompareGuid (&VarStoreHeader
->Signature
, &gEfiVariableGuid
)) &&
58 VarStoreHeader
->Format
== VARIABLE_STORE_FORMATTED
&&
59 VarStoreHeader
->State
== VARIABLE_STORE_HEALTHY
63 } else if (((UINT32
*)(&VarStoreHeader
->Signature
))[0] == 0xffffffff &&
64 ((UINT32
*)(&VarStoreHeader
->Signature
))[1] == 0xffffffff &&
65 ((UINT32
*)(&VarStoreHeader
->Signature
))[2] == 0xffffffff &&
66 ((UINT32
*)(&VarStoreHeader
->Signature
))[3] == 0xffffffff &&
67 VarStoreHeader
->Size
== 0xffffffff &&
68 VarStoreHeader
->Format
== 0xff &&
69 VarStoreHeader
->State
== 0xff
79 This code gets the size of variable header.
81 @return Size of variable header in bytes in type UINTN.
85 GetVariableHeaderSize (
91 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
92 Value
= sizeof (AUTHENTICATED_VARIABLE_HEADER
);
94 Value
= sizeof (VARIABLE_HEADER
);
102 This code gets the size of name of variable.
104 @param Variable Pointer to the Variable Header.
106 @return UINTN Size of variable in bytes.
111 IN VARIABLE_HEADER
*Variable
114 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
116 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
117 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
118 if (AuthVariable
->State
== (UINT8
) (-1) ||
119 AuthVariable
->DataSize
== (UINT32
) (-1) ||
120 AuthVariable
->NameSize
== (UINT32
) (-1) ||
121 AuthVariable
->Attributes
== (UINT32
) (-1)) {
124 return (UINTN
) AuthVariable
->NameSize
;
126 if (Variable
->State
== (UINT8
) (-1) ||
127 Variable
->DataSize
== (UINT32
) (-1) ||
128 Variable
->NameSize
== (UINT32
) (-1) ||
129 Variable
->Attributes
== (UINT32
) (-1)) {
132 return (UINTN
) Variable
->NameSize
;
137 This code sets the size of name of variable.
139 @param[in] Variable Pointer to the Variable Header.
140 @param[in] NameSize Name size to set.
144 SetNameSizeOfVariable (
145 IN VARIABLE_HEADER
*Variable
,
149 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
151 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
152 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
153 AuthVariable
->NameSize
= (UINT32
) NameSize
;
155 Variable
->NameSize
= (UINT32
) NameSize
;
161 This code gets the size of variable data.
163 @param Variable Pointer to the Variable Header.
165 @return Size of variable in bytes.
170 IN VARIABLE_HEADER
*Variable
173 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
175 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
176 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
177 if (AuthVariable
->State
== (UINT8
) (-1) ||
178 AuthVariable
->DataSize
== (UINT32
) (-1) ||
179 AuthVariable
->NameSize
== (UINT32
) (-1) ||
180 AuthVariable
->Attributes
== (UINT32
) (-1)) {
183 return (UINTN
) AuthVariable
->DataSize
;
185 if (Variable
->State
== (UINT8
) (-1) ||
186 Variable
->DataSize
== (UINT32
) (-1) ||
187 Variable
->NameSize
== (UINT32
) (-1) ||
188 Variable
->Attributes
== (UINT32
) (-1)) {
191 return (UINTN
) Variable
->DataSize
;
196 This code sets the size of variable data.
198 @param[in] Variable Pointer to the Variable Header.
199 @param[in] DataSize Data size to set.
203 SetDataSizeOfVariable (
204 IN VARIABLE_HEADER
*Variable
,
208 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
210 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
211 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
212 AuthVariable
->DataSize
= (UINT32
) DataSize
;
214 Variable
->DataSize
= (UINT32
) DataSize
;
220 This code gets the pointer to the variable name.
222 @param Variable Pointer to the Variable Header.
224 @return Pointer to Variable Name which is Unicode encoding.
229 IN VARIABLE_HEADER
*Variable
232 return (CHAR16
*) ((UINTN
) Variable
+ GetVariableHeaderSize ());
236 This code gets the pointer to the variable guid.
238 @param Variable Pointer to the Variable Header.
240 @return A EFI_GUID* pointer to Vendor Guid.
245 IN VARIABLE_HEADER
*Variable
248 AUTHENTICATED_VARIABLE_HEADER
*AuthVariable
;
250 AuthVariable
= (AUTHENTICATED_VARIABLE_HEADER
*) Variable
;
251 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
252 return &AuthVariable
->VendorGuid
;
254 return &Variable
->VendorGuid
;
260 This code gets the pointer to the variable data.
262 @param Variable Pointer to the Variable Header.
264 @return Pointer to Variable Data.
269 IN VARIABLE_HEADER
*Variable
275 // Be careful about pad size for alignment.
277 Value
= (UINTN
) GetVariableNamePtr (Variable
);
278 Value
+= NameSizeOfVariable (Variable
);
279 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
281 return (UINT8
*) Value
;
285 This code gets the variable data offset related to variable header.
287 @param Variable Pointer to the Variable Header.
289 @return Variable Data offset.
293 GetVariableDataOffset (
294 IN VARIABLE_HEADER
*Variable
300 // Be careful about pad size for alignment
302 Value
= GetVariableHeaderSize ();
303 Value
+= NameSizeOfVariable (Variable
);
304 Value
+= GET_PAD_SIZE (NameSizeOfVariable (Variable
));
311 This code gets the pointer to the next variable header.
313 @param Variable Pointer to the Variable Header.
315 @return Pointer to next variable header.
320 IN VARIABLE_HEADER
*Variable
325 Value
= (UINTN
) GetVariableDataPtr (Variable
);
326 Value
+= DataSizeOfVariable (Variable
);
327 Value
+= GET_PAD_SIZE (DataSizeOfVariable (Variable
));
330 // Be careful about pad size for alignment.
332 return (VARIABLE_HEADER
*) HEADER_ALIGN (Value
);
337 Gets the pointer to the first variable header in given variable store area.
339 @param[in] VarStoreHeader Pointer to the Variable Store Header.
341 @return Pointer to the first variable header.
346 IN VARIABLE_STORE_HEADER
*VarStoreHeader
350 // The start of variable store.
352 return (VARIABLE_HEADER
*) HEADER_ALIGN (VarStoreHeader
+ 1);
357 Gets the pointer to the end of the variable storage area.
359 This function gets pointer to the end of the variable storage
360 area, according to the input variable store header.
362 @param[in] VarStoreHeader Pointer to the Variable Store Header.
364 @return Pointer to the end of the variable storage area.
369 IN VARIABLE_STORE_HEADER
*VarStoreHeader
373 // The end of variable store
375 return (VARIABLE_HEADER
*) HEADER_ALIGN ((UINTN
) VarStoreHeader
+ VarStoreHeader
->Size
);
379 Compare two EFI_TIME data.
382 @param[in] FirstTime A pointer to the first EFI_TIME data.
383 @param[in] SecondTime A pointer to the second EFI_TIME data.
385 @retval TRUE The FirstTime is not later than the SecondTime.
386 @retval FALSE The FirstTime is later than the SecondTime.
390 VariableCompareTimeStampInternal (
391 IN EFI_TIME
*FirstTime
,
392 IN EFI_TIME
*SecondTime
395 if (FirstTime
->Year
!= SecondTime
->Year
) {
396 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
397 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
398 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
399 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
400 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
401 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
402 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
403 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
404 return (BOOLEAN
) (FirstTime
->Minute
< SecondTime
->Minute
);
407 return (BOOLEAN
) (FirstTime
->Second
<= SecondTime
->Second
);
411 Find the variable in the specified variable store.
413 @param[in] VariableName Name of the variable to be found
414 @param[in] VendorGuid Vendor GUID to be found.
415 @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
416 check at runtime when searching variable.
417 @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.
419 @retval EFI_SUCCESS Variable found successfully
420 @retval EFI_NOT_FOUND Variable not found
424 IN CHAR16
*VariableName
,
425 IN EFI_GUID
*VendorGuid
,
426 IN BOOLEAN IgnoreRtCheck
,
427 IN OUT VARIABLE_POINTER_TRACK
*PtrTrack
430 VARIABLE_HEADER
*InDeletedVariable
;
433 PtrTrack
->InDeletedTransitionPtr
= NULL
;
436 // Find the variable by walk through HOB, volatile and non-volatile variable store.
438 InDeletedVariable
= NULL
;
440 for ( PtrTrack
->CurrPtr
= PtrTrack
->StartPtr
441 ; IsValidVariableHeader (PtrTrack
->CurrPtr
, PtrTrack
->EndPtr
)
442 ; PtrTrack
->CurrPtr
= GetNextVariablePtr (PtrTrack
->CurrPtr
)
444 if (PtrTrack
->CurrPtr
->State
== VAR_ADDED
||
445 PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)
447 if (IgnoreRtCheck
|| !AtRuntime () || ((PtrTrack
->CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
448 if (VariableName
[0] == 0) {
449 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
450 InDeletedVariable
= PtrTrack
->CurrPtr
;
452 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
456 if (CompareGuid (VendorGuid
, GetVendorGuidPtr (PtrTrack
->CurrPtr
))) {
457 Point
= (VOID
*) GetVariableNamePtr (PtrTrack
->CurrPtr
);
459 ASSERT (NameSizeOfVariable (PtrTrack
->CurrPtr
) != 0);
460 if (CompareMem (VariableName
, Point
, NameSizeOfVariable (PtrTrack
->CurrPtr
)) == 0) {
461 if (PtrTrack
->CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
462 InDeletedVariable
= PtrTrack
->CurrPtr
;
464 PtrTrack
->InDeletedTransitionPtr
= InDeletedVariable
;
474 PtrTrack
->CurrPtr
= InDeletedVariable
;
475 return (PtrTrack
->CurrPtr
== NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
;
479 This code Finds the Next available variable.
481 Caution: This function may receive untrusted input.
482 This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
484 @param[in] VariableName Pointer to variable name.
485 @param[in] VendorGuid Variable Vendor Guid.
486 @param[out] VariablePtr Pointer to variable header address.
488 @retval EFI_SUCCESS The function completed successfully.
489 @retval EFI_NOT_FOUND The next variable was not found.
490 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.
491 @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
492 GUID of an existing variable.
497 VariableServiceGetNextVariableInternal (
498 IN CHAR16
*VariableName
,
499 IN EFI_GUID
*VendorGuid
,
500 OUT VARIABLE_HEADER
**VariablePtr
503 VARIABLE_STORE_TYPE Type
;
504 VARIABLE_POINTER_TRACK Variable
;
505 VARIABLE_POINTER_TRACK VariableInHob
;
506 VARIABLE_POINTER_TRACK VariablePtrTrack
;
508 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
510 Status
= FindVariable (VariableName
, VendorGuid
, &Variable
, &mVariableModuleGlobal
->VariableGlobal
, FALSE
);
511 if (Variable
.CurrPtr
== NULL
|| EFI_ERROR (Status
)) {
513 // For VariableName is an empty string, FindVariable() will try to find and return
514 // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)
515 // as no any variable is found, still go to return the error (EFI_NOT_FOUND).
517 if (VariableName
[0] != 0) {
519 // For VariableName is not an empty string, and FindVariable() returns error as
520 // VariableName and VendorGuid are not a name and GUID of an existing variable,
521 // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.
523 Status
= EFI_INVALID_PARAMETER
;
528 if (VariableName
[0] != 0) {
530 // If variable name is not NULL, get next variable.
532 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
536 // 0: Volatile, 1: HOB, 2: Non-Volatile.
537 // The index and attributes mapping must be kept in this order as FindVariable
538 // makes use of this mapping to implement search algorithm.
540 VariableStoreHeader
[VariableStoreTypeVolatile
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
;
541 VariableStoreHeader
[VariableStoreTypeHob
] = (VARIABLE_STORE_HEADER
*) (UINTN
) mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
;
542 VariableStoreHeader
[VariableStoreTypeNv
] = mNvVariableCache
;
546 // Switch from Volatile to HOB, to Non-Volatile.
548 while (!IsValidVariableHeader (Variable
.CurrPtr
, Variable
.EndPtr
)) {
550 // Find current storage index
552 for (Type
= (VARIABLE_STORE_TYPE
) 0; Type
< VariableStoreTypeMax
; Type
++) {
553 if ((VariableStoreHeader
[Type
] != NULL
) && (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[Type
]))) {
557 ASSERT (Type
< VariableStoreTypeMax
);
559 // Switch to next storage
561 for (Type
++; Type
< VariableStoreTypeMax
; Type
++) {
562 if (VariableStoreHeader
[Type
] != NULL
) {
567 // Capture the case that
568 // 1. current storage is the last one, or
569 // 2. no further storage
571 if (Type
== VariableStoreTypeMax
) {
572 Status
= EFI_NOT_FOUND
;
575 Variable
.StartPtr
= GetStartPointer (VariableStoreHeader
[Type
]);
576 Variable
.EndPtr
= GetEndPointer (VariableStoreHeader
[Type
]);
577 Variable
.CurrPtr
= Variable
.StartPtr
;
583 if (Variable
.CurrPtr
->State
== VAR_ADDED
|| Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
584 if (!AtRuntime () || ((Variable
.CurrPtr
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
) != 0)) {
585 if (Variable
.CurrPtr
->State
== (VAR_IN_DELETED_TRANSITION
& VAR_ADDED
)) {
587 // If it is a IN_DELETED_TRANSITION variable,
588 // and there is also a same ADDED one at the same time,
591 VariablePtrTrack
.StartPtr
= Variable
.StartPtr
;
592 VariablePtrTrack
.EndPtr
= Variable
.EndPtr
;
593 Status
= FindVariableEx (
594 GetVariableNamePtr (Variable
.CurrPtr
),
595 GetVendorGuidPtr (Variable
.CurrPtr
),
599 if (!EFI_ERROR (Status
) && VariablePtrTrack
.CurrPtr
->State
== VAR_ADDED
) {
600 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
606 // Don't return NV variable when HOB overrides it
608 if ((VariableStoreHeader
[VariableStoreTypeHob
] != NULL
) && (VariableStoreHeader
[VariableStoreTypeNv
] != NULL
) &&
609 (Variable
.StartPtr
== GetStartPointer (VariableStoreHeader
[VariableStoreTypeNv
]))
611 VariableInHob
.StartPtr
= GetStartPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
612 VariableInHob
.EndPtr
= GetEndPointer (VariableStoreHeader
[VariableStoreTypeHob
]);
613 Status
= FindVariableEx (
614 GetVariableNamePtr (Variable
.CurrPtr
),
615 GetVendorGuidPtr (Variable
.CurrPtr
),
619 if (!EFI_ERROR (Status
)) {
620 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
625 *VariablePtr
= Variable
.CurrPtr
;
626 Status
= EFI_SUCCESS
;
631 Variable
.CurrPtr
= GetNextVariablePtr (Variable
.CurrPtr
);
639 Routine used to track statistical information about variable usage.
640 The data is stored in the EFI system table so it can be accessed later.
641 VariableInfo.efi can dump out the table. Only Boot Services variable
642 accesses are tracked by this code. The PcdVariableCollectStatistics
643 build flag controls if this feature is enabled.
645 A read that hits in the cache will have Read and Cache true for
646 the transaction. Data is allocated by this routine, but never
649 @param[in] VariableName Name of the Variable to track.
650 @param[in] VendorGuid Guid of the Variable to track.
651 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
652 @param[in] Read TRUE if GetVariable() was called.
653 @param[in] Write TRUE if SetVariable() was called.
654 @param[in] Delete TRUE if deleted via SetVariable().
655 @param[in] Cache TRUE for a cache hit.
660 IN CHAR16
*VariableName
,
661 IN EFI_GUID
*VendorGuid
,
669 VARIABLE_INFO_ENTRY
*Entry
;
671 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
674 // Don't collect statistics at runtime.
678 if (gVariableInfo
== NULL
) {
680 // On the first call allocate a entry and place a pointer to it in
681 // the EFI System Table.
683 gVariableInfo
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
684 ASSERT (gVariableInfo
!= NULL
);
686 CopyGuid (&gVariableInfo
->VendorGuid
, VendorGuid
);
687 gVariableInfo
->Name
= AllocateZeroPool (StrSize (VariableName
));
688 ASSERT (gVariableInfo
->Name
!= NULL
);
689 StrCpyS (gVariableInfo
->Name
, StrSize(VariableName
)/sizeof(CHAR16
), VariableName
);
690 gVariableInfo
->Volatile
= Volatile
;
694 for (Entry
= gVariableInfo
; Entry
!= NULL
; Entry
= Entry
->Next
) {
695 if (CompareGuid (VendorGuid
, &Entry
->VendorGuid
)) {
696 if (StrCmp (VariableName
, Entry
->Name
) == 0) {
704 Entry
->DeleteCount
++;
714 if (Entry
->Next
== NULL
) {
716 // If the entry is not in the table add it.
717 // Next iteration of the loop will fill in the data.
719 Entry
->Next
= AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY
));
720 ASSERT (Entry
->Next
!= NULL
);
722 CopyGuid (&Entry
->Next
->VendorGuid
, VendorGuid
);
723 Entry
->Next
->Name
= AllocateZeroPool (StrSize (VariableName
));
724 ASSERT (Entry
->Next
->Name
!= NULL
);
725 StrCpyS (Entry
->Next
->Name
, StrSize(VariableName
)/sizeof(CHAR16
), VariableName
);
726 Entry
->Next
->Volatile
= Volatile
;