]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
MdeModulePkg/ResetSystemRuntimeDxe: Add more info in debug message
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Variable.c
index 71a6fd20936401d45c616df0e56facd42b1e2c84..42b0bfda570ddd91a945910cafab49517bab5a0a 100644 (file)
@@ -16,8 +16,8 @@
   VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,\r
   integer overflow. It should also check attribute to avoid authentication bypass.\r
 \r
-Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
-(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2015-2018 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -97,57 +97,6 @@ AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn = {
 \r
 AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut;\r
 \r
-/**\r
-\r
-  SecureBoot Hook for auth variable update.\r
-\r
-  @param[in] VariableName                 Name of Variable to be found.\r
-  @param[in] VendorGuid                   Variable vendor GUID.\r
-**/\r
-VOID\r
-EFIAPI\r
-SecureBootHook (\r
-  IN CHAR16                                 *VariableName,\r
-  IN EFI_GUID                               *VendorGuid\r
-  );\r
-\r
-/**\r
-  Initialization for MOR Lock Control.\r
-\r
-  @retval EFI_SUCEESS     MorLock initialization success.\r
-  @return Others          Some error occurs.\r
-**/\r
-EFI_STATUS\r
-MorLockInit (\r
-  VOID\r
-  );\r
-\r
-/**\r
-  This service is an MOR/MorLock checker handler for the SetVariable().\r
-\r
-  @param  VariableName the name of the vendor's variable, as a\r
-                       Null-Terminated Unicode String\r
-  @param  VendorGuid   Unify identifier for vendor.\r
-  @param  Attributes   Point to memory location to return the attributes of variable. If the point\r
-                       is NULL, the parameter would be ignored.\r
-  @param  DataSize     The size in bytes of Data-Buffer.\r
-  @param  Data         Point to the content of the variable.\r
-\r
-  @retval  EFI_SUCCESS            The MOR/MorLock check pass, and Variable driver can store the variable data.\r
-  @retval  EFI_INVALID_PARAMETER  The MOR/MorLock data or data size or attributes is not allowed for MOR variable.\r
-  @retval  EFI_ACCESS_DENIED      The MOR/MorLock is locked.\r
-  @retval  EFI_ALREADY_STARTED    The MorLock variable is handled inside this function.\r
-                                  Variable driver can just return EFI_SUCCESS.\r
-**/\r
-EFI_STATUS\r
-SetVariableCheckHandlerMor (\r
-  IN CHAR16     *VariableName,\r
-  IN EFI_GUID   *VendorGuid,\r
-  IN UINT32     Attributes,\r
-  IN UINTN      DataSize,\r
-  IN VOID       *Data\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
@@ -289,6 +238,8 @@ IsValidVariableHeader (
   @param Buffer                  Pointer to the buffer from which data is written.\r
 \r
   @retval EFI_INVALID_PARAMETER  Parameters not valid.\r
+  @retval EFI_UNSUPPORTED        Fvb is a NULL for Non-Volatile variable update.\r
+  @retval EFI_OUT_OF_RESOURCES   The remaining size is not enough.\r
   @retval EFI_SUCCESS            Variable store successfully updated.\r
 \r
 **/\r
@@ -325,7 +276,7 @@ UpdateVariableStore (
   //\r
   if (!Volatile) {\r
     if (Fvb == NULL) {\r
-      return EFI_INVALID_PARAMETER;\r
+      return EFI_UNSUPPORTED;\r
     }\r
     Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr);\r
     ASSERT_EFI_ERROR (Status);\r
@@ -339,8 +290,8 @@ UpdateVariableStore (
       DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
     }\r
 \r
-    if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r
-      return EFI_INVALID_PARAMETER;\r
+    if ((DataPtr + DataSize) > ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r
+      return EFI_OUT_OF_RESOURCES;\r
     }\r
   } else {\r
     //\r
@@ -352,8 +303,8 @@ UpdateVariableStore (
       DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
     }\r
 \r
-    if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
-      return EFI_INVALID_PARAMETER;\r
+    if ((DataPtr + DataSize) > ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
+      return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
     //\r
@@ -1582,8 +1533,8 @@ GetLangFromSupportedLangCodes (
   @param[in]  SupportedLanguages  A pointer to a Null-terminated ASCII string that\r
                                   contains a set of language codes in the format\r
                                   specified by Iso639Language.\r
-  @param[in]  Iso639Language      If TRUE, then all language codes are assumed to be\r
-                                  in ISO 639-2 format.  If FALSE, then all language\r
+  @param[in]  Iso639Language      If not zero, then all language codes are assumed to be\r
+                                  in ISO 639-2 format.  If zero, then all language\r
                                   codes are assumed to be in RFC 4646 language format\r
   @param[in]  ...                 A variable argument list that contains pointers to\r
                                   Null-terminated ASCII strings that contain one or more\r
@@ -1611,7 +1562,7 @@ CHAR8 *
 EFIAPI\r
 VariableGetBestLanguage (\r
   IN CONST CHAR8  *SupportedLanguages,\r
-  IN BOOLEAN      Iso639Language,\r
+  IN UINTN        Iso639Language,\r
   ...\r
   )\r
 {\r
@@ -1637,7 +1588,7 @@ VariableGetBestLanguage (
     //\r
     // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
     //\r
-    if (!Iso639Language) {\r
+    if (Iso639Language == 0) {\r
       for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
     }\r
 \r
@@ -1652,7 +1603,7 @@ VariableGetBestLanguage (
         //\r
         // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
         //\r
-        if (!Iso639Language) {\r
+        if (Iso639Language == 0) {\r
           //\r
           // Skip ';' characters in Supported\r
           //\r
@@ -1674,13 +1625,13 @@ VariableGetBestLanguage (
         if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
           VA_END (Args);\r
 \r
-          Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r
+          Buffer = (Iso639Language != 0) ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r
           Buffer[CompareLength] = '\0';\r
           return CopyMem (Buffer, Supported, CompareLength);\r
         }\r
       }\r
 \r
-      if (Iso639Language) {\r
+      if (Iso639Language != 0) {\r
         //\r
         // If ISO 639 mode, then each language can only be tested once\r
         //\r
@@ -1816,6 +1767,7 @@ CheckRemainingSpaceForConsistencyInternal (
       //\r
       // No enough space for Variable[Index].\r
       //\r
+      VA_END (Args);\r
       return FALSE;\r
     }\r
     //\r
@@ -2395,12 +2347,14 @@ UpdateVariable (
         CopyMem (BufferForMerge, (UINT8 *) ((UINTN) CacheVariable->CurrPtr + DataOffset), DataSizeOfVariable (CacheVariable->CurrPtr));\r
 \r
         //\r
-        // Set Max Common/Auth Variable Data Size as default MaxDataSize.\r
+        // Set Max Auth/Non-Volatile/Volatile Variable Data Size as default MaxDataSize.\r
         //\r
         if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
           MaxDataSize = mVariableModuleGlobal->MaxAuthVariableSize - DataOffset;\r
-        } else {\r
+        } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
           MaxDataSize = mVariableModuleGlobal->MaxVariableSize - DataOffset;\r
+        } else {\r
+          MaxDataSize = mVariableModuleGlobal->MaxVolatileVariableSize - DataOffset;\r
         }\r
 \r
         //\r
@@ -3184,8 +3138,11 @@ VariableServiceSetVariable (
 \r
   //\r
   // Check for reserverd bit in variable attribute.\r
+  // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated but we still allow\r
+  // the delete operation of common authenticated variable at user physical presence.\r
+  // So leave EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute check to AuthVariableLib\r
   //\r
-  if ((Attributes & (~EFI_VARIABLE_ATTRIBUTES_MASK)) != 0) {\r
+  if ((Attributes & (~(EFI_VARIABLE_ATTRIBUTES_MASK | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS))) != 0) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -3193,7 +3150,11 @@ VariableServiceSetVariable (
   //  Make sure if runtime bit is set, boot service bit is set also.\r
   //\r
   if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
-    return EFI_INVALID_PARAMETER;\r
+    if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
+      return EFI_UNSUPPORTED;\r
+    } else {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
   } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
     if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r
       //\r
@@ -3216,15 +3177,16 @@ VariableServiceSetVariable (
   //\r
   if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
      && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {\r
-    return EFI_INVALID_PARAMETER;\r
+    return EFI_UNSUPPORTED;\r
   }\r
 \r
   if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r
-    if (DataSize < AUTHINFO_SIZE) {\r
-      //\r
-      // Try to write Authenticated Variable without AuthInfo.\r
-      //\r
-      return EFI_SECURITY_VIOLATION;\r
+    //\r
+    //  If DataSize == AUTHINFO_SIZE and then PayloadSize is 0.\r
+    //  Maybe it's the delete operation of common authenticated variable at user physical presence.\r
+    //\r
+    if (DataSize != AUTHINFO_SIZE) {\r
+      return EFI_UNSUPPORTED;\r
     }\r
     PayloadSize = DataSize - AUTHINFO_SIZE;\r
   } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {\r
@@ -3260,16 +3222,20 @@ VariableServiceSetVariable (
   } else {\r
     //\r
     //  The size of the VariableName, including the Unicode Null in bytes plus\r
-    //  the DataSize is limited to maximum size of Max(Auth)VariableSize bytes.\r
+    //  the DataSize is limited to maximum size of Max(Auth|Volatile)VariableSize bytes.\r
     //\r
     if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
       if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ()) {\r
         return EFI_INVALID_PARAMETER;\r
       }\r
-    } else {\r
+    } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
       if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ()) {\r
         return EFI_INVALID_PARAMETER;\r
       }\r
+    } else {\r
+      if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVolatileVariableSize - GetVariableHeaderSize ()) {\r
+        return EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
   }\r
 \r
@@ -3441,12 +3407,14 @@ VariableServiceQueryVariableInfoInternal (
     }\r
 \r
     //\r
-    // Let *MaximumVariableSize be Max(Auth)VariableSize with the exception of the variable header size.\r
+    // Let *MaximumVariableSize be Max(Auth|Volatile)VariableSize with the exception of the variable header size.\r
     //\r
     if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
       *MaximumVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();\r
-    } else {\r
+    } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
       *MaximumVariableSize = mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ();\r
+    } else {\r
+      *MaximumVariableSize = mVariableModuleGlobal->MaxVolatileVariableSize - GetVariableHeaderSize ();\r
     }\r
   }\r
 \r
@@ -3570,6 +3538,13 @@ VariableServiceQueryVariableInfo (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
+    //\r
+    //  Deprecated attribute, make this check as highest priority.\r
+    //\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   if ((Attributes & EFI_VARIABLE_ATTRIBUTES_MASK) == 0) {\r
     //\r
     // Make sure the Attributes combination is supported by the platform.\r
@@ -3692,6 +3667,30 @@ GetNonVolatileMaxVariableSize (
   }\r
 }\r
 \r
+/**\r
+  Get maximum variable size, covering both non-volatile and volatile variables.\r
+\r
+  @return Maximum variable size.\r
+\r
+**/\r
+UINTN\r
+GetMaxVariableSize (\r
+  VOID\r
+  )\r
+{\r
+  UINTN MaxVariableSize;\r
+\r
+  MaxVariableSize = GetNonVolatileMaxVariableSize();\r
+  //\r
+  // The condition below fails implicitly if PcdMaxVolatileVariableSize equals\r
+  // the default zero value.\r
+  //\r
+  if (MaxVariableSize < PcdGet32 (PcdMaxVolatileVariableSize)) {\r
+    MaxVariableSize = PcdGet32 (PcdMaxVolatileVariableSize);\r
+  }\r
+  return MaxVariableSize;\r
+}\r
+\r
 /**\r
   Init non-volatile variable store.\r
 \r
@@ -4071,6 +4070,186 @@ VariableWriteServiceInitialize (
   return Status;\r
 }\r
 \r
+/**\r
+  Convert normal variable storage to the allocated auth variable storage.\r
+\r
+  @param[in]  NormalVarStorage  Pointer to the normal variable storage header\r
+\r
+  @retval the allocated auth variable storage\r
+**/\r
+VOID *\r
+ConvertNormalVarStorageToAuthVarStorage (\r
+  VARIABLE_STORE_HEADER *NormalVarStorage\r
+  )\r
+{\r
+  VARIABLE_HEADER *StartPtr;\r
+  UINT8           *NextPtr;\r
+  VARIABLE_HEADER *EndPtr;\r
+  UINTN           AuthVarStroageSize;\r
+  AUTHENTICATED_VARIABLE_HEADER *AuthStartPtr;\r
+  VARIABLE_STORE_HEADER         *AuthVarStorage;\r
+\r
+  AuthVarStroageSize  = sizeof (VARIABLE_STORE_HEADER);\r
+  //\r
+  // Set AuthFormat as FALSE for normal variable storage\r
+  //\r
+  mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;\r
+\r
+  //\r
+  // Calculate Auth Variable Storage Size\r
+  //\r
+  StartPtr = GetStartPointer (NormalVarStorage);\r
+  EndPtr   = GetEndPointer (NormalVarStorage);\r
+  while (StartPtr < EndPtr) {\r
+    if (StartPtr->State == VAR_ADDED) {\r
+      AuthVarStroageSize = HEADER_ALIGN (AuthVarStroageSize);\r
+      AuthVarStroageSize += sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
+      AuthVarStroageSize += StartPtr->NameSize + GET_PAD_SIZE (StartPtr->NameSize);\r
+      AuthVarStroageSize += StartPtr->DataSize + GET_PAD_SIZE (StartPtr->DataSize);\r
+    }\r
+    StartPtr  = GetNextVariablePtr (StartPtr);\r
+  }\r
+\r
+  //\r
+  // Allocate Runtime memory for Auth Variable Storage\r
+  //\r
+  AuthVarStorage = AllocateRuntimeZeroPool (AuthVarStroageSize);\r
+  ASSERT (AuthVarStorage != NULL);\r
+  if (AuthVarStorage == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  //\r
+  // Copy Variable from Normal storage to Auth storage\r
+  //\r
+  StartPtr = GetStartPointer (NormalVarStorage);\r
+  EndPtr   = GetEndPointer (NormalVarStorage);\r
+  AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) GetStartPointer (AuthVarStorage);\r
+  while (StartPtr < EndPtr) {\r
+    if (StartPtr->State == VAR_ADDED) {\r
+      AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) HEADER_ALIGN (AuthStartPtr);\r
+      //\r
+      // Copy Variable Header\r
+      //\r
+      AuthStartPtr->StartId     = StartPtr->StartId;\r
+      AuthStartPtr->State       = StartPtr->State;\r
+      AuthStartPtr->Attributes  = StartPtr->Attributes;\r
+      AuthStartPtr->NameSize    = StartPtr->NameSize;\r
+      AuthStartPtr->DataSize    = StartPtr->DataSize;\r
+      CopyGuid (&AuthStartPtr->VendorGuid, &StartPtr->VendorGuid);\r
+      //\r
+      // Copy Variable Name\r
+      //\r
+      NextPtr = (UINT8 *) (AuthStartPtr + 1);\r
+      CopyMem (NextPtr, GetVariableNamePtr (StartPtr), AuthStartPtr->NameSize);\r
+      //\r
+      // Copy Variable Data\r
+      //\r
+      NextPtr = NextPtr + AuthStartPtr->NameSize + GET_PAD_SIZE (AuthStartPtr->NameSize);\r
+      CopyMem (NextPtr, GetVariableDataPtr (StartPtr), AuthStartPtr->DataSize);\r
+      //\r
+      // Go to next variable\r
+      //\r
+      AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) (NextPtr + AuthStartPtr->DataSize + GET_PAD_SIZE (AuthStartPtr->DataSize));\r
+    }\r
+    StartPtr = GetNextVariablePtr (StartPtr);\r
+  }\r
+  //\r
+  // Update Auth Storage Header\r
+  //\r
+  AuthVarStorage->Format = NormalVarStorage->Format;\r
+  AuthVarStorage->State  = NormalVarStorage->State;\r
+  AuthVarStorage->Size = (UINT32)((UINTN)AuthStartPtr - (UINTN)AuthVarStorage);\r
+  CopyGuid (&AuthVarStorage->Signature, &gEfiAuthenticatedVariableGuid);\r
+  ASSERT (AuthVarStorage->Size <= AuthVarStroageSize);\r
+\r
+  //\r
+  // Restore AuthFormat\r
+  //\r
+  mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;\r
+  return AuthVarStorage;\r
+}\r
+\r
+/**\r
+  Get HOB variable store.\r
+\r
+  @param[out] VariableGuid      NV variable store signature.\r
+\r
+  @retval EFI_SUCCESS           Function successfully executed.\r
+  @retval EFI_OUT_OF_RESOURCES  Fail to allocate enough memory resource.\r
+\r
+**/\r
+EFI_STATUS\r
+GetHobVariableStore (\r
+  IN EFI_GUID                   *VariableGuid\r
+  )\r
+{\r
+  VARIABLE_STORE_HEADER         *VariableStoreHeader;\r
+  UINT64                        VariableStoreLength;\r
+  EFI_HOB_GUID_TYPE             *GuidHob;\r
+  BOOLEAN                       NeedConvertNormalToAuth;\r
+\r
+  //\r
+  // Make sure there is no more than one Variable HOB.\r
+  //\r
+  DEBUG_CODE (\r
+    GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);\r
+    if (GuidHob != NULL) {\r
+      if ((GetNextGuidHob (&gEfiAuthenticatedVariableGuid, GET_NEXT_HOB (GuidHob)) != NULL)) {\r
+        DEBUG ((DEBUG_ERROR, "ERROR: Found two Auth Variable HOBs\n"));\r
+        ASSERT (FALSE);\r
+      } else if (GetFirstGuidHob (&gEfiVariableGuid) != NULL) {\r
+        DEBUG ((DEBUG_ERROR, "ERROR: Found one Auth + one Normal Variable HOBs\n"));\r
+        ASSERT (FALSE);\r
+      }\r
+    } else {\r
+      GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r
+      if (GuidHob != NULL) {\r
+        if ((GetNextGuidHob (&gEfiVariableGuid, GET_NEXT_HOB (GuidHob)) != NULL)) {\r
+          DEBUG ((DEBUG_ERROR, "ERROR: Found two Normal Variable HOBs\n"));\r
+          ASSERT (FALSE);\r
+        }\r
+      }\r
+    }\r
+  );\r
+\r
+  //\r
+  // Combinations supported:\r
+  // 1. Normal NV variable store +\r
+  //    Normal HOB variable store\r
+  // 2. Auth NV variable store +\r
+  //    Auth HOB variable store\r
+  // 3. Auth NV variable store +\r
+  //    Normal HOB variable store (code will convert it to Auth Format)\r
+  //\r
+  NeedConvertNormalToAuth = FALSE;\r
+  GuidHob = GetFirstGuidHob (VariableGuid);\r
+  if (GuidHob == NULL && VariableGuid == &gEfiAuthenticatedVariableGuid) {\r
+    //\r
+    // Try getting it from normal variable HOB\r
+    //\r
+    GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r
+    NeedConvertNormalToAuth = TRUE;\r
+  }\r
+  if (GuidHob != NULL) {\r
+    VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
+    VariableStoreLength = GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE);\r
+    if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
+      if (!NeedConvertNormalToAuth) {\r
+        mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r
+      } else {\r
+        mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) ConvertNormalVarStorageToAuthVarStorage ((VOID *) VariableStoreHeader);\r
+      }\r
+      if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+    } else {\r
+      DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
 \r
 /**\r
   Initializes variable store area for non-volatile and volatile variable.\r
@@ -4086,10 +4265,7 @@ VariableCommonInitialize (
 {\r
   EFI_STATUS                      Status;\r
   VARIABLE_STORE_HEADER           *VolatileVariableStore;\r
-  VARIABLE_STORE_HEADER           *VariableStoreHeader;\r
-  UINT64                          VariableStoreLength;\r
   UINTN                           ScratchSize;\r
-  EFI_HOB_GUID_TYPE               *GuidHob;\r
   EFI_GUID                        *VariableGuid;\r
   EFI_FIRMWARE_VOLUME_HEADER      *NvFvHeader;\r
 \r
@@ -4133,26 +4309,21 @@ VariableCommonInitialize (
   //\r
   // Get HOB variable store.\r
   //\r
-  GuidHob = GetFirstGuidHob (VariableGuid);\r
-  if (GuidHob != NULL) {\r
-    VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
-    VariableStoreLength = GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE);\r
-    if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
-      mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r
-      if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r
-        FreePool (NvFvHeader);\r
-        FreePool (mVariableModuleGlobal);\r
-        return EFI_OUT_OF_RESOURCES;\r
-      }\r
-    } else {\r
-      DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
-    }\r
+  Status = GetHobVariableStore (VariableGuid);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (NvFvHeader);\r
+    FreePool (mVariableModuleGlobal);\r
+    return Status;\r
   }\r
 \r
+  mVariableModuleGlobal->MaxVolatileVariableSize = ((PcdGet32 (PcdMaxVolatileVariableSize) != 0) ?\r
+                                                    PcdGet32 (PcdMaxVolatileVariableSize) :\r
+                                                    mVariableModuleGlobal->MaxVariableSize\r
+                                                    );\r
   //\r
   // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
   //\r
-  ScratchSize = GetNonVolatileMaxVariableSize ();\r
+  ScratchSize = GetMaxVariableSize ();\r
   mVariableModuleGlobal->ScratchBufferSize = ScratchSize;\r
   VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
   if (VolatileVariableStore == NULL) {\r