**/\r
\r
#include "Variable.h"\r
+#include "VariableParsing.h"\r
\r
VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;\r
\r
\r
AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut;\r
\r
-/**\r
- Routine used to track statistical information about variable usage.\r
- The data is stored in the EFI system table so it can be accessed later.\r
- VariableInfo.efi can dump out the table. Only Boot Services variable\r
- accesses are tracked by this code. The PcdVariableCollectStatistics\r
- build flag controls if this feature is enabled.\r
-\r
- A read that hits in the cache will have Read and Cache true for\r
- the transaction. Data is allocated by this routine, but never\r
- freed.\r
-\r
- @param[in] VariableName Name of the Variable to track.\r
- @param[in] VendorGuid Guid of the Variable to track.\r
- @param[in] Volatile TRUE if volatile FALSE if non-volatile.\r
- @param[in] Read TRUE if GetVariable() was called.\r
- @param[in] Write TRUE if SetVariable() was called.\r
- @param[in] Delete TRUE if deleted via SetVariable().\r
- @param[in] Cache TRUE for a cache hit.\r
-\r
-**/\r
-VOID\r
-UpdateVariableInfo (\r
- IN CHAR16 *VariableName,\r
- IN EFI_GUID *VendorGuid,\r
- IN BOOLEAN Volatile,\r
- IN BOOLEAN Read,\r
- IN BOOLEAN Write,\r
- IN BOOLEAN Delete,\r
- IN BOOLEAN Cache\r
- )\r
-{\r
- VARIABLE_INFO_ENTRY *Entry;\r
-\r
- if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
-\r
- if (AtRuntime ()) {\r
- // Don't collect statistics at runtime.\r
- return;\r
- }\r
-\r
- if (gVariableInfo == NULL) {\r
- //\r
- // On the first call allocate a entry and place a pointer to it in\r
- // the EFI System Table.\r
- //\r
- gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
- ASSERT (gVariableInfo != NULL);\r
-\r
- CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
- gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));\r
- ASSERT (gVariableInfo->Name != NULL);\r
- StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);\r
- gVariableInfo->Volatile = Volatile;\r
- }\r
-\r
-\r
- for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
- if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
- if (StrCmp (VariableName, Entry->Name) == 0) {\r
- if (Read) {\r
- Entry->ReadCount++;\r
- }\r
- if (Write) {\r
- Entry->WriteCount++;\r
- }\r
- if (Delete) {\r
- Entry->DeleteCount++;\r
- }\r
- if (Cache) {\r
- Entry->CacheCount++;\r
- }\r
-\r
- return;\r
- }\r
- }\r
-\r
- if (Entry->Next == NULL) {\r
- //\r
- // If the entry is not in the table add it.\r
- // Next iteration of the loop will fill in the data.\r
- //\r
- Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
- ASSERT (Entry->Next != NULL);\r
-\r
- CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
- Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));\r
- ASSERT (Entry->Next->Name != NULL);\r
- StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);\r
- Entry->Next->Volatile = Volatile;\r
- }\r
-\r
- }\r
- }\r
-}\r
-\r
-\r
-/**\r
-\r
- This code checks if variable header is valid or not.\r
-\r
- @param Variable Pointer to the Variable Header.\r
- @param VariableStoreEnd Pointer to the Variable Store End.\r
-\r
- @retval TRUE Variable header is valid.\r
- @retval FALSE Variable header is not valid.\r
-\r
-**/\r
-BOOLEAN\r
-IsValidVariableHeader (\r
- IN VARIABLE_HEADER *Variable,\r
- IN VARIABLE_HEADER *VariableStoreEnd\r
- )\r
-{\r
- if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId != VARIABLE_DATA)) {\r
- //\r
- // Variable is NULL or has reached the end of variable store,\r
- // or the StartId is not correct.\r
- //\r
- return FALSE;\r
- }\r
-\r
- return TRUE;\r
-}\r
-\r
-\r
/**\r
\r
This function writes data to the FWH at the correct LBA even if the LBAs\r
return EFI_SUCCESS;\r
}\r
\r
-\r
-/**\r
-\r
- This code gets the current status of Variable Store.\r
-\r
- @param VarStoreHeader Pointer to the Variable Store Header.\r
-\r
- @retval EfiRaw Variable store status is raw.\r
- @retval EfiValid Variable store status is valid.\r
- @retval EfiInvalid Variable store status is invalid.\r
-\r
-**/\r
-VARIABLE_STORE_STATUS\r
-GetVariableStoreStatus (\r
- IN VARIABLE_STORE_HEADER *VarStoreHeader\r
- )\r
-{\r
- if ((CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) ||\r
- CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&\r
- VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
- VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
- ) {\r
-\r
- return EfiValid;\r
- } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r
- ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r
- ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r
- ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r
- VarStoreHeader->Size == 0xffffffff &&\r
- VarStoreHeader->Format == 0xff &&\r
- VarStoreHeader->State == 0xff\r
- ) {\r
-\r
- return EfiRaw;\r
- } else {\r
- return EfiInvalid;\r
- }\r
-}\r
-\r
-/**\r
- This code gets the size of variable header.\r
-\r
- @return Size of variable header in bytes in type UINTN.\r
-\r
-**/\r
-UINTN\r
-GetVariableHeaderSize (\r
- VOID\r
- )\r
-{\r
- UINTN Value;\r
-\r
- if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
- Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
- } else {\r
- Value = sizeof (VARIABLE_HEADER);\r
- }\r
-\r
- return Value;\r
-}\r
-\r
-/**\r
-\r
- This code gets the size of name of variable.\r
-\r
- @param Variable Pointer to the Variable Header.\r
-\r
- @return UINTN Size of variable in bytes.\r
-\r
-**/\r
-UINTN\r
-NameSizeOfVariable (\r
- IN VARIABLE_HEADER *Variable\r
- )\r
-{\r
- AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
-\r
- AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
- if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
- if (AuthVariable->State == (UINT8) (-1) ||\r
- AuthVariable->DataSize == (UINT32) (-1) ||\r
- AuthVariable->NameSize == (UINT32) (-1) ||\r
- AuthVariable->Attributes == (UINT32) (-1)) {\r
- return 0;\r
- }\r
- return (UINTN) AuthVariable->NameSize;\r
- } else {\r
- if (Variable->State == (UINT8) (-1) ||\r
- Variable->DataSize == (UINT32) (-1) ||\r
- Variable->NameSize == (UINT32) (-1) ||\r
- Variable->Attributes == (UINT32) (-1)) {\r
- return 0;\r
- }\r
- return (UINTN) Variable->NameSize;\r
- }\r
-}\r
-\r
-/**\r
- This code sets the size of name of variable.\r
-\r
- @param[in] Variable Pointer to the Variable Header.\r
- @param[in] NameSize Name size to set.\r
-\r
-**/\r
-VOID\r
-SetNameSizeOfVariable (\r
- IN VARIABLE_HEADER *Variable,\r
- IN UINTN NameSize\r
- )\r
-{\r
- AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
-\r
- AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
- if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
- AuthVariable->NameSize = (UINT32) NameSize;\r
- } else {\r
- Variable->NameSize = (UINT32) NameSize;\r
- }\r
-}\r
-\r
-/**\r
-\r
- This code gets the size of variable data.\r
-\r
- @param Variable Pointer to the Variable Header.\r
-\r
- @return Size of variable in bytes.\r
-\r
-**/\r
-UINTN\r
-DataSizeOfVariable (\r
- IN VARIABLE_HEADER *Variable\r
- )\r
-{\r
- AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
-\r
- AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
- if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
- if (AuthVariable->State == (UINT8) (-1) ||\r
- AuthVariable->DataSize == (UINT32) (-1) ||\r
- AuthVariable->NameSize == (UINT32) (-1) ||\r
- AuthVariable->Attributes == (UINT32) (-1)) {\r
- return 0;\r
- }\r
- return (UINTN) AuthVariable->DataSize;\r
- } else {\r
- if (Variable->State == (UINT8) (-1) ||\r
- Variable->DataSize == (UINT32) (-1) ||\r
- Variable->NameSize == (UINT32) (-1) ||\r
- Variable->Attributes == (UINT32) (-1)) {\r
- return 0;\r
- }\r
- return (UINTN) Variable->DataSize;\r
- }\r
-}\r
-\r
-/**\r
- This code sets the size of variable data.\r
-\r
- @param[in] Variable Pointer to the Variable Header.\r
- @param[in] DataSize Data size to set.\r
-\r
-**/\r
-VOID\r
-SetDataSizeOfVariable (\r
- IN VARIABLE_HEADER *Variable,\r
- IN UINTN DataSize\r
- )\r
-{\r
- AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
-\r
- AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
- if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
- AuthVariable->DataSize = (UINT32) DataSize;\r
- } else {\r
- Variable->DataSize = (UINT32) DataSize;\r
- }\r
-}\r
-\r
-/**\r
-\r
- This code gets the pointer to the variable name.\r
-\r
- @param Variable Pointer to the Variable Header.\r
-\r
- @return Pointer to Variable Name which is Unicode encoding.\r
-\r
-**/\r
-CHAR16 *\r
-GetVariableNamePtr (\r
- IN VARIABLE_HEADER *Variable\r
- )\r
-{\r
- return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());\r
-}\r
-\r
-/**\r
- This code gets the pointer to the variable guid.\r
-\r
- @param Variable Pointer to the Variable Header.\r
-\r
- @return A EFI_GUID* pointer to Vendor Guid.\r
-\r
-**/\r
-EFI_GUID *\r
-GetVendorGuidPtr (\r
- IN VARIABLE_HEADER *Variable\r
- )\r
-{\r
- AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
-\r
- AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
- if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
- return &AuthVariable->VendorGuid;\r
- } else {\r
- return &Variable->VendorGuid;\r
- }\r
-}\r
-\r
-/**\r
-\r
- This code gets the pointer to the variable data.\r
-\r
- @param Variable Pointer to the Variable Header.\r
-\r
- @return Pointer to Variable Data.\r
-\r
-**/\r
-UINT8 *\r
-GetVariableDataPtr (\r
- IN VARIABLE_HEADER *Variable\r
- )\r
-{\r
- UINTN Value;\r
-\r
- //\r
- // Be careful about pad size for alignment.\r
- //\r
- Value = (UINTN) GetVariableNamePtr (Variable);\r
- Value += NameSizeOfVariable (Variable);\r
- Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r
-\r
- return (UINT8 *) Value;\r
-}\r
-\r
-/**\r
- This code gets the variable data offset related to variable header.\r
-\r
- @param Variable Pointer to the Variable Header.\r
-\r
- @return Variable Data offset.\r
-\r
-**/\r
-UINTN\r
-GetVariableDataOffset (\r
- IN VARIABLE_HEADER *Variable\r
- )\r
-{\r
- UINTN Value;\r
-\r
- //\r
- // Be careful about pad size for alignment\r
- //\r
- Value = GetVariableHeaderSize ();\r
- Value += NameSizeOfVariable (Variable);\r
- Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r
-\r
- return Value;\r
-}\r
-\r
-/**\r
-\r
- This code gets the pointer to the next variable header.\r
-\r
- @param Variable Pointer to the Variable Header.\r
-\r
- @return Pointer to next variable header.\r
-\r
-**/\r
-VARIABLE_HEADER *\r
-GetNextVariablePtr (\r
- IN VARIABLE_HEADER *Variable\r
- )\r
-{\r
- UINTN Value;\r
-\r
- Value = (UINTN) GetVariableDataPtr (Variable);\r
- Value += DataSizeOfVariable (Variable);\r
- Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r
-\r
- //\r
- // Be careful about pad size for alignment.\r
- //\r
- return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r
-}\r
-\r
-/**\r
-\r
- Gets the pointer to the first variable header in given variable store area.\r
-\r
- @param VarStoreHeader Pointer to the Variable Store Header.\r
-\r
- @return Pointer to the first variable header.\r
-\r
-**/\r
-VARIABLE_HEADER *\r
-GetStartPointer (\r
- IN VARIABLE_STORE_HEADER *VarStoreHeader\r
- )\r
-{\r
- //\r
- // The start of variable store.\r
- //\r
- return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r
-}\r
-\r
-/**\r
-\r
- Gets the pointer to the end of the variable storage area.\r
-\r
- This function gets pointer to the end of the variable storage\r
- area, according to the input variable store header.\r
-\r
- @param VarStoreHeader Pointer to the Variable Store Header.\r
-\r
- @return Pointer to the end of the variable storage area.\r
-\r
-**/\r
-VARIABLE_HEADER *\r
-GetEndPointer (\r
- IN VARIABLE_STORE_HEADER *VarStoreHeader\r
- )\r
-{\r
- //\r
- // The end of variable store\r
- //\r
- return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
-}\r
-\r
/**\r
Record variable error flag.\r
\r
return Status;\r
}\r
\r
-/**\r
- Find the variable in the specified variable store.\r
-\r
- @param[in] VariableName Name of the variable to be found\r
- @param[in] VendorGuid Vendor GUID to be found.\r
- @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
- check at runtime when searching variable.\r
- @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.\r
-\r
- @retval EFI_SUCCESS Variable found successfully\r
- @retval EFI_NOT_FOUND Variable not found\r
-**/\r
-EFI_STATUS\r
-FindVariableEx (\r
- IN CHAR16 *VariableName,\r
- IN EFI_GUID *VendorGuid,\r
- IN BOOLEAN IgnoreRtCheck,\r
- IN OUT VARIABLE_POINTER_TRACK *PtrTrack\r
- )\r
-{\r
- VARIABLE_HEADER *InDeletedVariable;\r
- VOID *Point;\r
-\r
- PtrTrack->InDeletedTransitionPtr = NULL;\r
-\r
- //\r
- // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
- //\r
- InDeletedVariable = NULL;\r
-\r
- for ( PtrTrack->CurrPtr = PtrTrack->StartPtr\r
- ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)\r
- ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)\r
- ) {\r
- if (PtrTrack->CurrPtr->State == VAR_ADDED ||\r
- PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
- ) {\r
- if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
- if (VariableName[0] == 0) {\r
- if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
- InDeletedVariable = PtrTrack->CurrPtr;\r
- } else {\r
- PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
- return EFI_SUCCESS;\r
- }\r
- } else {\r
- if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr))) {\r
- Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);\r
-\r
- ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);\r
- if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {\r
- if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
- InDeletedVariable = PtrTrack->CurrPtr;\r
- } else {\r
- PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
- return EFI_SUCCESS;\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
-\r
- PtrTrack->CurrPtr = InDeletedVariable;\r
- return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
-}\r
-\r
-\r
/**\r
Finds variable in storage blocks of volatile and non-volatile storage areas.\r
\r
}\r
}\r
\r
-/**\r
- Compare two EFI_TIME data.\r
-\r
-\r
- @param FirstTime A pointer to the first EFI_TIME data.\r
- @param SecondTime A pointer to the second EFI_TIME data.\r
-\r
- @retval TRUE The FirstTime is not later than the SecondTime.\r
- @retval FALSE The FirstTime is later than the SecondTime.\r
-\r
-**/\r
-BOOLEAN\r
-VariableCompareTimeStampInternal (\r
- IN EFI_TIME *FirstTime,\r
- IN EFI_TIME *SecondTime\r
- )\r
-{\r
- if (FirstTime->Year != SecondTime->Year) {\r
- return (BOOLEAN) (FirstTime->Year < SecondTime->Year);\r
- } else if (FirstTime->Month != SecondTime->Month) {\r
- return (BOOLEAN) (FirstTime->Month < SecondTime->Month);\r
- } else if (FirstTime->Day != SecondTime->Day) {\r
- return (BOOLEAN) (FirstTime->Day < SecondTime->Day);\r
- } else if (FirstTime->Hour != SecondTime->Hour) {\r
- return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);\r
- } else if (FirstTime->Minute != SecondTime->Minute) {\r
- return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);\r
- }\r
-\r
- return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);\r
-}\r
-\r
/**\r
Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,\r
index of associated public key is needed.\r
return Status;\r
}\r
\r
-/**\r
- This code Finds the Next available variable.\r
-\r
- Caution: This function may receive untrusted input.\r
- This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
-\r
- @param[in] VariableName Pointer to variable name.\r
- @param[in] VendorGuid Variable Vendor Guid.\r
- @param[out] VariablePtr Pointer to variable header address.\r
-\r
- @retval EFI_SUCCESS The function completed successfully.\r
- @retval EFI_NOT_FOUND The next variable was not found.\r
- @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.\r
- @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and\r
- GUID of an existing variable.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceGetNextVariableInternal (\r
- IN CHAR16 *VariableName,\r
- IN EFI_GUID *VendorGuid,\r
- OUT VARIABLE_HEADER **VariablePtr\r
- )\r
-{\r
- VARIABLE_STORE_TYPE Type;\r
- VARIABLE_POINTER_TRACK Variable;\r
- VARIABLE_POINTER_TRACK VariableInHob;\r
- VARIABLE_POINTER_TRACK VariablePtrTrack;\r
- EFI_STATUS Status;\r
- VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
-\r
- Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
- if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
- //\r
- // For VariableName is an empty string, FindVariable() will try to find and return\r
- // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)\r
- // as no any variable is found, still go to return the error (EFI_NOT_FOUND).\r
- //\r
- if (VariableName[0] != 0) {\r
- //\r
- // For VariableName is not an empty string, and FindVariable() returns error as\r
- // VariableName and VendorGuid are not a name and GUID of an existing variable,\r
- // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.\r
- //\r
- Status = EFI_INVALID_PARAMETER;\r
- }\r
- goto Done;\r
- }\r
-\r
- if (VariableName[0] != 0) {\r
- //\r
- // If variable name is not NULL, get next variable.\r
- //\r
- Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
- }\r
-\r
- //\r
- // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
- // The index and attributes mapping must be kept in this order as FindVariable\r
- // makes use of this mapping to implement search algorithm.\r
- //\r
- VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
- VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
- VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
-\r
- while (TRUE) {\r
- //\r
- // Switch from Volatile to HOB, to Non-Volatile.\r
- //\r
- while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {\r
- //\r
- // Find current storage index\r
- //\r
- for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
- if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
- break;\r
- }\r
- }\r
- ASSERT (Type < VariableStoreTypeMax);\r
- //\r
- // Switch to next storage\r
- //\r
- for (Type++; Type < VariableStoreTypeMax; Type++) {\r
- if (VariableStoreHeader[Type] != NULL) {\r
- break;\r
- }\r
- }\r
- //\r
- // Capture the case that\r
- // 1. current storage is the last one, or\r
- // 2. no further storage\r
- //\r
- if (Type == VariableStoreTypeMax) {\r
- Status = EFI_NOT_FOUND;\r
- goto Done;\r
- }\r
- Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
- Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
- Variable.CurrPtr = Variable.StartPtr;\r
- }\r
-\r
- //\r
- // Variable is found\r
- //\r
- if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
- if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
- if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
- //\r
- // If it is a IN_DELETED_TRANSITION variable,\r
- // and there is also a same ADDED one at the same time,\r
- // don't return it.\r
- //\r
- VariablePtrTrack.StartPtr = Variable.StartPtr;\r
- VariablePtrTrack.EndPtr = Variable.EndPtr;\r
- Status = FindVariableEx (\r
- GetVariableNamePtr (Variable.CurrPtr),\r
- GetVendorGuidPtr (Variable.CurrPtr),\r
- FALSE,\r
- &VariablePtrTrack\r
- );\r
- if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r
- Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
- continue;\r
- }\r
- }\r
-\r
- //\r
- // Don't return NV variable when HOB overrides it\r
- //\r
- if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
- (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
- ) {\r
- VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
- VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
- Status = FindVariableEx (\r
- GetVariableNamePtr (Variable.CurrPtr),\r
- GetVendorGuidPtr (Variable.CurrPtr),\r
- FALSE,\r
- &VariableInHob\r
- );\r
- if (!EFI_ERROR (Status)) {\r
- Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
- continue;\r
- }\r
- }\r
-\r
- *VariablePtr = Variable.CurrPtr;\r
- Status = EFI_SUCCESS;\r
- goto Done;\r
- }\r
- }\r
-\r
- Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
- }\r
-\r
-Done:\r
- return Status;\r
-}\r
-\r
/**\r
\r
This code Finds the Next available variable.\r
IN BOOLEAN IgnoreRtCheck\r
);\r
\r
-/**\r
-\r
- Gets the pointer to the end of the variable storage area.\r
-\r
- This function gets pointer to the end of the variable storage\r
- area, according to the input variable store header.\r
-\r
- @param VarStoreHeader Pointer to the Variable Store Header.\r
-\r
- @return Pointer to the end of the variable storage area.\r
-\r
-**/\r
-VARIABLE_HEADER *\r
-GetEndPointer (\r
- IN VARIABLE_STORE_HEADER *VarStoreHeader\r
- );\r
-\r
-/**\r
- This code gets the size of variable header.\r
-\r
- @return Size of variable header in bytes in type UINTN.\r
-\r
-**/\r
-UINTN\r
-GetVariableHeaderSize (\r
- VOID\r
- );\r
-\r
-/**\r
-\r
- This code gets the pointer to the variable name.\r
-\r
- @param Variable Pointer to the Variable Header.\r
-\r
- @return Pointer to Variable Name which is Unicode encoding.\r
-\r
-**/\r
-CHAR16 *\r
-GetVariableNamePtr (\r
- IN VARIABLE_HEADER *Variable\r
- );\r
-\r
-/**\r
- This code gets the pointer to the variable guid.\r
-\r
- @param Variable Pointer to the Variable Header.\r
-\r
- @return A EFI_GUID* pointer to Vendor Guid.\r
-\r
-**/\r
-EFI_GUID *\r
-GetVendorGuidPtr (\r
- IN VARIABLE_HEADER *Variable\r
- );\r
-\r
-/**\r
-\r
- This code gets the pointer to the variable data.\r
-\r
- @param Variable Pointer to the Variable Header.\r
-\r
- @return Pointer to Variable Data.\r
-\r
-**/\r
-UINT8 *\r
-GetVariableDataPtr (\r
- IN VARIABLE_HEADER *Variable\r
- );\r
-\r
-/**\r
-\r
- This code gets the size of variable data.\r
-\r
- @param Variable Pointer to the Variable Header.\r
-\r
- @return Size of variable in bytes.\r
-\r
-**/\r
-UINTN\r
-DataSizeOfVariable (\r
- IN VARIABLE_HEADER *Variable\r
- );\r
-\r
/**\r
This function is to check if the remaining variable space is enough to set\r
all Variables from argument list successfully. The purpose of the check\r
VOID\r
);\r
\r
-/**\r
- Get non-volatile maximum variable size.\r
-\r
- @return Non-volatile maximum variable size.\r
-\r
-**/\r
-UINTN\r
-GetNonVolatileMaxVariableSize (\r
- VOID\r
- );\r
-\r
/**\r
Get maximum variable size, covering both non-volatile and volatile variables.\r
\r
OUT VOID *Data OPTIONAL\r
);\r
\r
-/**\r
- This code Finds the Next available variable.\r
-\r
- Caution: This function may receive untrusted input.\r
- This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
-\r
- @param[in] VariableName Pointer to variable name.\r
- @param[in] VendorGuid Variable Vendor Guid.\r
- @param[out] VariablePtr Pointer to variable header address.\r
-\r
- @retval EFI_SUCCESS The function completed successfully.\r
- @retval EFI_NOT_FOUND The next variable was not found.\r
- @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.\r
- @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and\r
- GUID of an existing variable.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceGetNextVariableInternal (\r
- IN CHAR16 *VariableName,\r
- IN EFI_GUID *VendorGuid,\r
- OUT VARIABLE_HEADER **VariablePtr\r
- );\r
-\r
/**\r
\r
This code Finds the Next available variable.\r
/** @file\r
Provides variable driver extended services.\r
\r
-Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>\r
SPDX-License-Identifier: BSD-2-Clause-Patent\r
\r
**/\r
\r
#include "Variable.h"\r
+#include "VariableParsing.h"\r
\r
/**\r
Finds variable in storage blocks of volatile and non-volatile storage areas.\r
--- /dev/null
+/** @file\r
+ Functions in this module are associated with variable parsing operations and\r
+ are intended to be usable across variable driver source files.\r
+\r
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "VariableParsing.h"\r
+\r
+/**\r
+\r
+ This code checks if variable header is valid or not.\r
+\r
+ @param[in] Variable Pointer to the Variable Header.\r
+ @param[in] VariableStoreEnd Pointer to the Variable Store End.\r
+\r
+ @retval TRUE Variable header is valid.\r
+ @retval FALSE Variable header is not valid.\r
+\r
+**/\r
+BOOLEAN\r
+IsValidVariableHeader (\r
+ IN VARIABLE_HEADER *Variable,\r
+ IN VARIABLE_HEADER *VariableStoreEnd\r
+ )\r
+{\r
+ if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId != VARIABLE_DATA)) {\r
+ //\r
+ // Variable is NULL or has reached the end of variable store,\r
+ // or the StartId is not correct.\r
+ //\r
+ return FALSE;\r
+ }\r
+\r
+ return TRUE;\r
+}\r
+\r
+/**\r
+\r
+ This code gets the current status of Variable Store.\r
+\r
+ @param[in] VarStoreHeader Pointer to the Variable Store Header.\r
+\r
+ @retval EfiRaw Variable store status is raw.\r
+ @retval EfiValid Variable store status is valid.\r
+ @retval EfiInvalid Variable store status is invalid.\r
+\r
+**/\r
+VARIABLE_STORE_STATUS\r
+GetVariableStoreStatus (\r
+ IN VARIABLE_STORE_HEADER *VarStoreHeader\r
+ )\r
+{\r
+ if ((CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) ||\r
+ CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&\r
+ VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
+ VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
+ ) {\r
+\r
+ return EfiValid;\r
+ } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r
+ ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r
+ ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r
+ ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r
+ VarStoreHeader->Size == 0xffffffff &&\r
+ VarStoreHeader->Format == 0xff &&\r
+ VarStoreHeader->State == 0xff\r
+ ) {\r
+\r
+ return EfiRaw;\r
+ } else {\r
+ return EfiInvalid;\r
+ }\r
+}\r
+\r
+/**\r
+ This code gets the size of variable header.\r
+\r
+ @return Size of variable header in bytes in type UINTN.\r
+\r
+**/\r
+UINTN\r
+GetVariableHeaderSize (\r
+ VOID\r
+ )\r
+{\r
+ UINTN Value;\r
+\r
+ if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
+ Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
+ } else {\r
+ Value = sizeof (VARIABLE_HEADER);\r
+ }\r
+\r
+ return Value;\r
+}\r
+\r
+/**\r
+\r
+ This code gets the size of name of variable.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return UINTN Size of variable in bytes.\r
+\r
+**/\r
+UINTN\r
+NameSizeOfVariable (\r
+ IN VARIABLE_HEADER *Variable\r
+ )\r
+{\r
+ AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
+\r
+ AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
+ if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
+ if (AuthVariable->State == (UINT8) (-1) ||\r
+ AuthVariable->DataSize == (UINT32) (-1) ||\r
+ AuthVariable->NameSize == (UINT32) (-1) ||\r
+ AuthVariable->Attributes == (UINT32) (-1)) {\r
+ return 0;\r
+ }\r
+ return (UINTN) AuthVariable->NameSize;\r
+ } else {\r
+ if (Variable->State == (UINT8) (-1) ||\r
+ Variable->DataSize == (UINT32) (-1) ||\r
+ Variable->NameSize == (UINT32) (-1) ||\r
+ Variable->Attributes == (UINT32) (-1)) {\r
+ return 0;\r
+ }\r
+ return (UINTN) Variable->NameSize;\r
+ }\r
+}\r
+\r
+/**\r
+ This code sets the size of name of variable.\r
+\r
+ @param[in] Variable Pointer to the Variable Header.\r
+ @param[in] NameSize Name size to set.\r
+\r
+**/\r
+VOID\r
+SetNameSizeOfVariable (\r
+ IN VARIABLE_HEADER *Variable,\r
+ IN UINTN NameSize\r
+ )\r
+{\r
+ AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
+\r
+ AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
+ if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
+ AuthVariable->NameSize = (UINT32) NameSize;\r
+ } else {\r
+ Variable->NameSize = (UINT32) NameSize;\r
+ }\r
+}\r
+\r
+/**\r
+\r
+ This code gets the size of variable data.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return Size of variable in bytes.\r
+\r
+**/\r
+UINTN\r
+DataSizeOfVariable (\r
+ IN VARIABLE_HEADER *Variable\r
+ )\r
+{\r
+ AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
+\r
+ AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
+ if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
+ if (AuthVariable->State == (UINT8) (-1) ||\r
+ AuthVariable->DataSize == (UINT32) (-1) ||\r
+ AuthVariable->NameSize == (UINT32) (-1) ||\r
+ AuthVariable->Attributes == (UINT32) (-1)) {\r
+ return 0;\r
+ }\r
+ return (UINTN) AuthVariable->DataSize;\r
+ } else {\r
+ if (Variable->State == (UINT8) (-1) ||\r
+ Variable->DataSize == (UINT32) (-1) ||\r
+ Variable->NameSize == (UINT32) (-1) ||\r
+ Variable->Attributes == (UINT32) (-1)) {\r
+ return 0;\r
+ }\r
+ return (UINTN) Variable->DataSize;\r
+ }\r
+}\r
+\r
+/**\r
+ This code sets the size of variable data.\r
+\r
+ @param[in] Variable Pointer to the Variable Header.\r
+ @param[in] DataSize Data size to set.\r
+\r
+**/\r
+VOID\r
+SetDataSizeOfVariable (\r
+ IN VARIABLE_HEADER *Variable,\r
+ IN UINTN DataSize\r
+ )\r
+{\r
+ AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
+\r
+ AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
+ if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
+ AuthVariable->DataSize = (UINT32) DataSize;\r
+ } else {\r
+ Variable->DataSize = (UINT32) DataSize;\r
+ }\r
+}\r
+\r
+/**\r
+\r
+ This code gets the pointer to the variable name.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return Pointer to Variable Name which is Unicode encoding.\r
+\r
+**/\r
+CHAR16 *\r
+GetVariableNamePtr (\r
+ IN VARIABLE_HEADER *Variable\r
+ )\r
+{\r
+ return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());\r
+}\r
+\r
+/**\r
+ This code gets the pointer to the variable guid.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return A EFI_GUID* pointer to Vendor Guid.\r
+\r
+**/\r
+EFI_GUID *\r
+GetVendorGuidPtr (\r
+ IN VARIABLE_HEADER *Variable\r
+ )\r
+{\r
+ AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r
+\r
+ AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r
+ if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
+ return &AuthVariable->VendorGuid;\r
+ } else {\r
+ return &Variable->VendorGuid;\r
+ }\r
+}\r
+\r
+/**\r
+\r
+ This code gets the pointer to the variable data.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return Pointer to Variable Data.\r
+\r
+**/\r
+UINT8 *\r
+GetVariableDataPtr (\r
+ IN VARIABLE_HEADER *Variable\r
+ )\r
+{\r
+ UINTN Value;\r
+\r
+ //\r
+ // Be careful about pad size for alignment.\r
+ //\r
+ Value = (UINTN) GetVariableNamePtr (Variable);\r
+ Value += NameSizeOfVariable (Variable);\r
+ Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r
+\r
+ return (UINT8 *) Value;\r
+}\r
+\r
+/**\r
+ This code gets the variable data offset related to variable header.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return Variable Data offset.\r
+\r
+**/\r
+UINTN\r
+GetVariableDataOffset (\r
+ IN VARIABLE_HEADER *Variable\r
+ )\r
+{\r
+ UINTN Value;\r
+\r
+ //\r
+ // Be careful about pad size for alignment\r
+ //\r
+ Value = GetVariableHeaderSize ();\r
+ Value += NameSizeOfVariable (Variable);\r
+ Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r
+\r
+ return Value;\r
+}\r
+\r
+/**\r
+\r
+ This code gets the pointer to the next variable header.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return Pointer to next variable header.\r
+\r
+**/\r
+VARIABLE_HEADER *\r
+GetNextVariablePtr (\r
+ IN VARIABLE_HEADER *Variable\r
+ )\r
+{\r
+ UINTN Value;\r
+\r
+ Value = (UINTN) GetVariableDataPtr (Variable);\r
+ Value += DataSizeOfVariable (Variable);\r
+ Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r
+\r
+ //\r
+ // Be careful about pad size for alignment.\r
+ //\r
+ return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r
+}\r
+\r
+/**\r
+\r
+ Gets the pointer to the first variable header in given variable store area.\r
+\r
+ @param[in] VarStoreHeader Pointer to the Variable Store Header.\r
+\r
+ @return Pointer to the first variable header.\r
+\r
+**/\r
+VARIABLE_HEADER *\r
+GetStartPointer (\r
+ IN VARIABLE_STORE_HEADER *VarStoreHeader\r
+ )\r
+{\r
+ //\r
+ // The start of variable store.\r
+ //\r
+ return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r
+}\r
+\r
+/**\r
+\r
+ Gets the pointer to the end of the variable storage area.\r
+\r
+ This function gets pointer to the end of the variable storage\r
+ area, according to the input variable store header.\r
+\r
+ @param[in] VarStoreHeader Pointer to the Variable Store Header.\r
+\r
+ @return Pointer to the end of the variable storage area.\r
+\r
+**/\r
+VARIABLE_HEADER *\r
+GetEndPointer (\r
+ IN VARIABLE_STORE_HEADER *VarStoreHeader\r
+ )\r
+{\r
+ //\r
+ // The end of variable store\r
+ //\r
+ return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
+}\r
+\r
+/**\r
+ Compare two EFI_TIME data.\r
+\r
+\r
+ @param[in] FirstTime A pointer to the first EFI_TIME data.\r
+ @param[in] SecondTime A pointer to the second EFI_TIME data.\r
+\r
+ @retval TRUE The FirstTime is not later than the SecondTime.\r
+ @retval FALSE The FirstTime is later than the SecondTime.\r
+\r
+**/\r
+BOOLEAN\r
+VariableCompareTimeStampInternal (\r
+ IN EFI_TIME *FirstTime,\r
+ IN EFI_TIME *SecondTime\r
+ )\r
+{\r
+ if (FirstTime->Year != SecondTime->Year) {\r
+ return (BOOLEAN) (FirstTime->Year < SecondTime->Year);\r
+ } else if (FirstTime->Month != SecondTime->Month) {\r
+ return (BOOLEAN) (FirstTime->Month < SecondTime->Month);\r
+ } else if (FirstTime->Day != SecondTime->Day) {\r
+ return (BOOLEAN) (FirstTime->Day < SecondTime->Day);\r
+ } else if (FirstTime->Hour != SecondTime->Hour) {\r
+ return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);\r
+ } else if (FirstTime->Minute != SecondTime->Minute) {\r
+ return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);\r
+ }\r
+\r
+ return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);\r
+}\r
+\r
+/**\r
+ Find the variable in the specified variable store.\r
+\r
+ @param[in] VariableName Name of the variable to be found\r
+ @param[in] VendorGuid Vendor GUID to be found.\r
+ @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
+ check at runtime when searching variable.\r
+ @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.\r
+\r
+ @retval EFI_SUCCESS Variable found successfully\r
+ @retval EFI_NOT_FOUND Variable not found\r
+**/\r
+EFI_STATUS\r
+FindVariableEx (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ IN BOOLEAN IgnoreRtCheck,\r
+ IN OUT VARIABLE_POINTER_TRACK *PtrTrack\r
+ )\r
+{\r
+ VARIABLE_HEADER *InDeletedVariable;\r
+ VOID *Point;\r
+\r
+ PtrTrack->InDeletedTransitionPtr = NULL;\r
+\r
+ //\r
+ // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
+ //\r
+ InDeletedVariable = NULL;\r
+\r
+ for ( PtrTrack->CurrPtr = PtrTrack->StartPtr\r
+ ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)\r
+ ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)\r
+ ) {\r
+ if (PtrTrack->CurrPtr->State == VAR_ADDED ||\r
+ PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
+ ) {\r
+ if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
+ if (VariableName[0] == 0) {\r
+ if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
+ InDeletedVariable = PtrTrack->CurrPtr;\r
+ } else {\r
+ PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
+ return EFI_SUCCESS;\r
+ }\r
+ } else {\r
+ if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr))) {\r
+ Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);\r
+\r
+ ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);\r
+ if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {\r
+ if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
+ InDeletedVariable = PtrTrack->CurrPtr;\r
+ } else {\r
+ PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ PtrTrack->CurrPtr = InDeletedVariable;\r
+ return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ This code Finds the Next available variable.\r
+\r
+ Caution: This function may receive untrusted input.\r
+ This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
+\r
+ @param[in] VariableName Pointer to variable name.\r
+ @param[in] VendorGuid Variable Vendor Guid.\r
+ @param[out] VariablePtr Pointer to variable header address.\r
+\r
+ @retval EFI_SUCCESS The function completed successfully.\r
+ @retval EFI_NOT_FOUND The next variable was not found.\r
+ @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.\r
+ @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and\r
+ GUID of an existing variable.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VariableServiceGetNextVariableInternal (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ OUT VARIABLE_HEADER **VariablePtr\r
+ )\r
+{\r
+ VARIABLE_STORE_TYPE Type;\r
+ VARIABLE_POINTER_TRACK Variable;\r
+ VARIABLE_POINTER_TRACK VariableInHob;\r
+ VARIABLE_POINTER_TRACK VariablePtrTrack;\r
+ EFI_STATUS Status;\r
+ VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r
+\r
+ Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
+ if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
+ //\r
+ // For VariableName is an empty string, FindVariable() will try to find and return\r
+ // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)\r
+ // as no any variable is found, still go to return the error (EFI_NOT_FOUND).\r
+ //\r
+ if (VariableName[0] != 0) {\r
+ //\r
+ // For VariableName is not an empty string, and FindVariable() returns error as\r
+ // VariableName and VendorGuid are not a name and GUID of an existing variable,\r
+ // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.\r
+ //\r
+ Status = EFI_INVALID_PARAMETER;\r
+ }\r
+ goto Done;\r
+ }\r
+\r
+ if (VariableName[0] != 0) {\r
+ //\r
+ // If variable name is not NULL, get next variable.\r
+ //\r
+ Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
+ }\r
+\r
+ //\r
+ // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
+ // The index and attributes mapping must be kept in this order as FindVariable\r
+ // makes use of this mapping to implement search algorithm.\r
+ //\r
+ VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
+ VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
+ VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r
+\r
+ while (TRUE) {\r
+ //\r
+ // Switch from Volatile to HOB, to Non-Volatile.\r
+ //\r
+ while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {\r
+ //\r
+ // Find current storage index\r
+ //\r
+ for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
+ if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
+ break;\r
+ }\r
+ }\r
+ ASSERT (Type < VariableStoreTypeMax);\r
+ //\r
+ // Switch to next storage\r
+ //\r
+ for (Type++; Type < VariableStoreTypeMax; Type++) {\r
+ if (VariableStoreHeader[Type] != NULL) {\r
+ break;\r
+ }\r
+ }\r
+ //\r
+ // Capture the case that\r
+ // 1. current storage is the last one, or\r
+ // 2. no further storage\r
+ //\r
+ if (Type == VariableStoreTypeMax) {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+ Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
+ Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r
+ Variable.CurrPtr = Variable.StartPtr;\r
+ }\r
+\r
+ //\r
+ // Variable is found\r
+ //\r
+ if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
+ if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
+ if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
+ //\r
+ // If it is a IN_DELETED_TRANSITION variable,\r
+ // and there is also a same ADDED one at the same time,\r
+ // don't return it.\r
+ //\r
+ VariablePtrTrack.StartPtr = Variable.StartPtr;\r
+ VariablePtrTrack.EndPtr = Variable.EndPtr;\r
+ Status = FindVariableEx (\r
+ GetVariableNamePtr (Variable.CurrPtr),\r
+ GetVendorGuidPtr (Variable.CurrPtr),\r
+ FALSE,\r
+ &VariablePtrTrack\r
+ );\r
+ if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r
+ Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
+ continue;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Don't return NV variable when HOB overrides it\r
+ //\r
+ if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
+ (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
+ ) {\r
+ VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
+ VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
+ Status = FindVariableEx (\r
+ GetVariableNamePtr (Variable.CurrPtr),\r
+ GetVendorGuidPtr (Variable.CurrPtr),\r
+ FALSE,\r
+ &VariableInHob\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
+ continue;\r
+ }\r
+ }\r
+\r
+ *VariablePtr = Variable.CurrPtr;\r
+ Status = EFI_SUCCESS;\r
+ goto Done;\r
+ }\r
+ }\r
+\r
+ Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
+ }\r
+\r
+Done:\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Routine used to track statistical information about variable usage.\r
+ The data is stored in the EFI system table so it can be accessed later.\r
+ VariableInfo.efi can dump out the table. Only Boot Services variable\r
+ accesses are tracked by this code. The PcdVariableCollectStatistics\r
+ build flag controls if this feature is enabled.\r
+\r
+ A read that hits in the cache will have Read and Cache true for\r
+ the transaction. Data is allocated by this routine, but never\r
+ freed.\r
+\r
+ @param[in] VariableName Name of the Variable to track.\r
+ @param[in] VendorGuid Guid of the Variable to track.\r
+ @param[in] Volatile TRUE if volatile FALSE if non-volatile.\r
+ @param[in] Read TRUE if GetVariable() was called.\r
+ @param[in] Write TRUE if SetVariable() was called.\r
+ @param[in] Delete TRUE if deleted via SetVariable().\r
+ @param[in] Cache TRUE for a cache hit.\r
+\r
+**/\r
+VOID\r
+UpdateVariableInfo (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ IN BOOLEAN Volatile,\r
+ IN BOOLEAN Read,\r
+ IN BOOLEAN Write,\r
+ IN BOOLEAN Delete,\r
+ IN BOOLEAN Cache\r
+ )\r
+{\r
+ VARIABLE_INFO_ENTRY *Entry;\r
+\r
+ if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
+\r
+ if (AtRuntime ()) {\r
+ // Don't collect statistics at runtime.\r
+ return;\r
+ }\r
+\r
+ if (gVariableInfo == NULL) {\r
+ //\r
+ // On the first call allocate a entry and place a pointer to it in\r
+ // the EFI System Table.\r
+ //\r
+ gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
+ ASSERT (gVariableInfo != NULL);\r
+\r
+ CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
+ gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));\r
+ ASSERT (gVariableInfo->Name != NULL);\r
+ StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);\r
+ gVariableInfo->Volatile = Volatile;\r
+ }\r
+\r
+\r
+ for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
+ if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
+ if (StrCmp (VariableName, Entry->Name) == 0) {\r
+ if (Read) {\r
+ Entry->ReadCount++;\r
+ }\r
+ if (Write) {\r
+ Entry->WriteCount++;\r
+ }\r
+ if (Delete) {\r
+ Entry->DeleteCount++;\r
+ }\r
+ if (Cache) {\r
+ Entry->CacheCount++;\r
+ }\r
+\r
+ return;\r
+ }\r
+ }\r
+\r
+ if (Entry->Next == NULL) {\r
+ //\r
+ // If the entry is not in the table add it.\r
+ // Next iteration of the loop will fill in the data.\r
+ //\r
+ Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
+ ASSERT (Entry->Next != NULL);\r
+\r
+ CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
+ Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));\r
+ ASSERT (Entry->Next->Name != NULL);\r
+ StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);\r
+ Entry->Next->Volatile = Volatile;\r
+ }\r
+\r
+ }\r
+ }\r
+}\r
--- /dev/null
+/** @file\r
+ Functions in this module are associated with variable parsing operations and\r
+ are intended to be usable across variable driver source files.\r
+\r
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#ifndef _VARIABLE_PARSING_H_\r
+#define _VARIABLE_PARSING_H_\r
+\r
+#include <Guid/ImageAuthentication.h>\r
+#include "Variable.h"\r
+\r
+/**\r
+\r
+ This code checks if variable header is valid or not.\r
+\r
+ @param[in] Variable Pointer to the Variable Header.\r
+ @param[in] VariableStoreEnd Pointer to the Variable Store End.\r
+\r
+ @retval TRUE Variable header is valid.\r
+ @retval FALSE Variable header is not valid.\r
+\r
+**/\r
+BOOLEAN\r
+IsValidVariableHeader (\r
+ IN VARIABLE_HEADER *Variable,\r
+ IN VARIABLE_HEADER *VariableStoreEnd\r
+ );\r
+\r
+/**\r
+\r
+ This code gets the current status of Variable Store.\r
+\r
+ @param[in] VarStoreHeader Pointer to the Variable Store Header.\r
+\r
+ @retval EfiRaw Variable store status is raw.\r
+ @retval EfiValid Variable store status is valid.\r
+ @retval EfiInvalid Variable store status is invalid.\r
+\r
+**/\r
+VARIABLE_STORE_STATUS\r
+GetVariableStoreStatus (\r
+ IN VARIABLE_STORE_HEADER *VarStoreHeader\r
+ );\r
+\r
+/**\r
+ This code gets the size of variable header.\r
+\r
+ @return Size of variable header in bytes in type UINTN.\r
+\r
+**/\r
+UINTN\r
+GetVariableHeaderSize (\r
+ VOID\r
+ );\r
+\r
+/**\r
+\r
+ This code gets the size of name of variable.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return UINTN Size of variable in bytes.\r
+\r
+**/\r
+UINTN\r
+NameSizeOfVariable (\r
+ IN VARIABLE_HEADER *Variable\r
+ );\r
+\r
+/**\r
+ This code sets the size of name of variable.\r
+\r
+ @param[in] Variable Pointer to the Variable Header.\r
+ @param[in] NameSize Name size to set.\r
+\r
+**/\r
+VOID\r
+SetNameSizeOfVariable (\r
+ IN VARIABLE_HEADER *Variable,\r
+ IN UINTN NameSize\r
+ );\r
+\r
+/**\r
+\r
+ This code gets the size of variable data.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return Size of variable in bytes.\r
+\r
+**/\r
+UINTN\r
+DataSizeOfVariable (\r
+ IN VARIABLE_HEADER *Variable\r
+ );\r
+\r
+/**\r
+ This code sets the size of variable data.\r
+\r
+ @param[in] Variable Pointer to the Variable Header.\r
+ @param[in] DataSize Data size to set.\r
+\r
+**/\r
+VOID\r
+SetDataSizeOfVariable (\r
+ IN VARIABLE_HEADER *Variable,\r
+ IN UINTN DataSize\r
+ );\r
+\r
+/**\r
+\r
+ This code gets the pointer to the variable name.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return Pointer to Variable Name which is Unicode encoding.\r
+\r
+**/\r
+CHAR16 *\r
+GetVariableNamePtr (\r
+ IN VARIABLE_HEADER *Variable\r
+ );\r
+\r
+/**\r
+ This code gets the pointer to the variable guid.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return A EFI_GUID* pointer to Vendor Guid.\r
+\r
+**/\r
+EFI_GUID *\r
+GetVendorGuidPtr (\r
+ IN VARIABLE_HEADER *Variable\r
+ );\r
+\r
+/**\r
+\r
+ This code gets the pointer to the variable data.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return Pointer to Variable Data.\r
+\r
+**/\r
+UINT8 *\r
+GetVariableDataPtr (\r
+ IN VARIABLE_HEADER *Variable\r
+ );\r
+\r
+/**\r
+ This code gets the variable data offset related to variable header.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return Variable Data offset.\r
+\r
+**/\r
+UINTN\r
+GetVariableDataOffset (\r
+ IN VARIABLE_HEADER *Variable\r
+ );\r
+\r
+/**\r
+\r
+ This code gets the pointer to the next variable header.\r
+\r
+ @param Variable Pointer to the Variable Header.\r
+\r
+ @return Pointer to next variable header.\r
+\r
+**/\r
+VARIABLE_HEADER *\r
+GetNextVariablePtr (\r
+ IN VARIABLE_HEADER *Variable\r
+ );\r
+\r
+/**\r
+\r
+ Gets the pointer to the first variable header in given variable store area.\r
+\r
+ @param[in] VarStoreHeader Pointer to the Variable Store Header.\r
+\r
+ @return Pointer to the first variable header.\r
+\r
+**/\r
+VARIABLE_HEADER *\r
+GetStartPointer (\r
+ IN VARIABLE_STORE_HEADER *VarStoreHeader\r
+ );\r
+\r
+/**\r
+\r
+ Gets the pointer to the end of the variable storage area.\r
+\r
+ This function gets pointer to the end of the variable storage\r
+ area, according to the input variable store header.\r
+\r
+ @param[in] VarStoreHeader Pointer to the Variable Store Header.\r
+\r
+ @return Pointer to the end of the variable storage area.\r
+\r
+**/\r
+VARIABLE_HEADER *\r
+GetEndPointer (\r
+ IN VARIABLE_STORE_HEADER *VarStoreHeader\r
+ );\r
+\r
+/**\r
+ Compare two EFI_TIME data.\r
+\r
+\r
+ @param[in] FirstTime A pointer to the first EFI_TIME data.\r
+ @param[in] SecondTime A pointer to the second EFI_TIME data.\r
+\r
+ @retval TRUE The FirstTime is not later than the SecondTime.\r
+ @retval FALSE The FirstTime is later than the SecondTime.\r
+\r
+**/\r
+BOOLEAN\r
+VariableCompareTimeStampInternal (\r
+ IN EFI_TIME *FirstTime,\r
+ IN EFI_TIME *SecondTime\r
+ );\r
+\r
+/**\r
+ Find the variable in the specified variable store.\r
+\r
+ @param[in] VariableName Name of the variable to be found\r
+ @param[in] VendorGuid Vendor GUID to be found.\r
+ @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
+ check at runtime when searching variable.\r
+ @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.\r
+\r
+ @retval EFI_SUCCESS Variable found successfully\r
+ @retval EFI_NOT_FOUND Variable not found\r
+**/\r
+EFI_STATUS\r
+FindVariableEx (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ IN BOOLEAN IgnoreRtCheck,\r
+ IN OUT VARIABLE_POINTER_TRACK *PtrTrack\r
+ );\r
+\r
+/**\r
+ This code Finds the Next available variable.\r
+\r
+ Caution: This function may receive untrusted input.\r
+ This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
+\r
+ @param[in] VariableName Pointer to variable name.\r
+ @param[in] VendorGuid Variable Vendor Guid.\r
+ @param[out] VariablePtr Pointer to variable header address.\r
+\r
+ @retval EFI_SUCCESS The function completed successfully.\r
+ @retval EFI_NOT_FOUND The next variable was not found.\r
+ @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.\r
+ @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and\r
+ GUID of an existing variable.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VariableServiceGetNextVariableInternal (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ OUT VARIABLE_HEADER **VariablePtr\r
+ );\r
+\r
+/**\r
+ Routine used to track statistical information about variable usage.\r
+ The data is stored in the EFI system table so it can be accessed later.\r
+ VariableInfo.efi can dump out the table. Only Boot Services variable\r
+ accesses are tracked by this code. The PcdVariableCollectStatistics\r
+ build flag controls if this feature is enabled.\r
+\r
+ A read that hits in the cache will have Read and Cache true for\r
+ the transaction. Data is allocated by this routine, but never\r
+ freed.\r
+\r
+ @param[in] VariableName Name of the Variable to track.\r
+ @param[in] VendorGuid Guid of the Variable to track.\r
+ @param[in] Volatile TRUE if volatile FALSE if non-volatile.\r
+ @param[in] Read TRUE if GetVariable() was called.\r
+ @param[in] Write TRUE if SetVariable() was called.\r
+ @param[in] Delete TRUE if deleted via SetVariable().\r
+ @param[in] Cache TRUE for a cache hit.\r
+\r
+**/\r
+VOID\r
+UpdateVariableInfo (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ IN BOOLEAN Volatile,\r
+ IN BOOLEAN Read,\r
+ IN BOOLEAN Write,\r
+ IN BOOLEAN Delete,\r
+ IN BOOLEAN Cache\r
+ );\r
+\r
+#endif\r
Variable.c\r
VariableDxe.c\r
Variable.h\r
+ VariableParsing.c\r
+ VariableParsing.h\r
PrivilegePolymorphic.h\r
Measurement.c\r
TcgMorLockDxe.c\r
\r
#include <Guid/SmmVariableCommon.h>\r
#include "Variable.h"\r
+#include "VariableParsing.h"\r
\r
BOOLEAN mAtRuntime = FALSE;\r
UINT8 *mVariableBufferPayload = NULL;\r
Variable.c\r
VariableTraditionalMm.c\r
VariableSmm.c\r
+ VariableParsing.c\r
+ VariableParsing.h\r
VarCheck.c\r
Variable.h\r
PrivilegePolymorphic.h\r
Variable.c\r
VariableSmm.c\r
VariableStandaloneMm.c\r
+ VariableParsing.c\r
+ VariableParsing.h\r
VarCheck.c\r
Variable.h\r
PrivilegePolymorphic.h\r