]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/Variable: Consolidate common parsing functions
authorMichael Kubacki <michael.a.kubacki@intel.com>
Tue, 24 Sep 2019 00:32:07 +0000 (17:32 -0700)
committerMichael Kubacki <michael.a.kubacki@intel.com>
Wed, 6 Nov 2019 05:55:53 +0000 (21:55 -0800)
This change moves the following functions into a dedicated file
so they may be used in other variable files as needed. These are
commonly needed for basic variable data structure parsing
operations. The functions are grouped together in VariableParsing.c
to support cohesiveness for these operations in the file.
Furthermore, it reduces the overall size of the common Variable.c
file.

 * DataSizeOfVariable ()
 * FindVariableEx ()
 * GetEndPointer ()
 * GetNextVariablePtr ()
 * GetStartPointer ()
 * GetVariableDataOffset ()
 * GetVariableDataPtr ()
 * GetVariableHeaderSize ()
 * GetVariableNamePtr ()
 * GetVariableStoreStatus ()
 * GetVendorGuidPtr ()
 * IsValidVariableHeader ()
 * NameSizeOfVariable ()
 * SetDataSizeOfVariable ()
 * SetNameSizeOfVariable ()
 * UpdateVariableInfo ()
 * VariableCompareTimeStampInternal ()
 * VariableServiceGetNextVariableInternal ()

Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
Reviewed-by: Jian J Wang <jian.j.wang@intel.com>
Reviewed-by: Hao A Wu <hao.a.wu@intel.com>
MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c [new file with mode: 0644]
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h [new file with mode: 0644]
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf

index f32c9c2808a7dd79408b4cc81ba275a4c9f028fc..76536308e6acaa9686e65b690d196dc24868fd0d 100644 (file)
@@ -23,6 +23,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 **/\r
 \r
 #include "Variable.h"\r
+#include "VariableParsing.h"\r
 \r
 VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;\r
 \r
@@ -92,131 +93,6 @@ AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn = {
 \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
@@ -376,345 +252,6 @@ UpdateVariableStore (
   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
@@ -1228,75 +765,6 @@ Done:
   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
@@ -2078,38 +1546,6 @@ AutoUpdateLangVariable (
   }\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
@@ -2885,166 +2321,6 @@ Done:
   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
index 9eac43759fd1b5a7d3cba9e762a1066737c796fb..fb574b2e328525a6799e347cd4d1d9aff35b9fa7 100644 (file)
@@ -179,89 +179,6 @@ FindVariable (
   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
@@ -450,17 +367,6 @@ ReclaimForOS(
   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
@@ -546,31 +452,6 @@ VariableServiceGetVariable (
   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
index cb6fcebe2d90d1557b76f944a672fd9c03e547f8..dc78f68fa9e05e9580ab043a0f58d866c3428666 100644 (file)
@@ -1,12 +1,13 @@
 /** @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
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
new file mode 100644 (file)
index 0000000..5698a1a
--- /dev/null
@@ -0,0 +1,731 @@
+/** @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
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
new file mode 100644 (file)
index 0000000..b0d7f76
--- /dev/null
@@ -0,0 +1,306 @@
+/** @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
index 641376c9c5192847d60fac6ef4df3c7499921c89..c35e5fe7878cc54aa47e7462f19885d15a1da0a4 100644 (file)
@@ -36,6 +36,8 @@
   Variable.c\r
   VariableDxe.c\r
   Variable.h\r
+  VariableParsing.c\r
+  VariableParsing.h\r
   PrivilegePolymorphic.h\r
   Measurement.c\r
   TcgMorLockDxe.c\r
index ec463d063ea4e1d5e0eb2bb0db5fdf3d43ea3873..ce409f22a3efd3213a1d2b6cf3df1bbe765da56c 100644 (file)
@@ -30,6 +30,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 \r
 #include <Guid/SmmVariableCommon.h>\r
 #include "Variable.h"\r
+#include "VariableParsing.h"\r
 \r
 BOOLEAN                                              mAtRuntime              = FALSE;\r
 UINT8                                                *mVariableBufferPayload = NULL;\r
index 0a160d269d4236ec7ccb0d9c17ee5ce46519e6b2..626738b9c77c96c77ce6a34528b8655c5e0030d1 100644 (file)
@@ -45,6 +45,8 @@
   Variable.c\r
   VariableTraditionalMm.c\r
   VariableSmm.c\r
+  VariableParsing.c\r
+  VariableParsing.h\r
   VarCheck.c\r
   Variable.h\r
   PrivilegePolymorphic.h\r
index 21bc81163b87ed69ee45c072853c0c55b9a8dfad..ff714b193a7a4a36a45c817c34868d44088e8f38 100644 (file)
@@ -45,6 +45,8 @@
   Variable.c\r
   VariableSmm.c\r
   VariableStandaloneMm.c\r
+  VariableParsing.c\r
+  VariableParsing.h\r
   VarCheck.c\r
   Variable.h\r
   PrivilegePolymorphic.h\r