]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Variable/EmuRuntimeDxe/EmuVariable.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / EmuRuntimeDxe / EmuVariable.c
index 622fe530b3057485e15969e097087ff3897b528e..1bcf931b96a670ab1fc66cd66f9d88d68f363e7f 100644 (file)
@@ -3,8 +3,8 @@
   Emulation Variable services operate on the runtime volatile memory.\r
   The nonvolatile variable space doesn't exist.\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<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
 http://opensource.org/licenses/bsd-license.php\r
@@ -29,7 +29,7 @@ VARIABLE_INFO_ENTRY *gVariableInfo = NULL;
 #define ISO_639_2_ENTRY_SIZE    3\r
 \r
 /**\r
-  Update the variable region with Variable information. These are the same \r
+  Update the variable region with Variable information. These are the same\r
   arguments as the EFI Variable services.\r
 \r
   @param[in] VariableName       Name of variable\r
@@ -40,7 +40,7 @@ VARIABLE_INFO_ENTRY *gVariableInfo = NULL;
 \r
   @param[in] DataSize           Size of data. 0 means delete\r
 \r
-  @param[in] Attributes              Attribues of the variable\r
+  @param[in] Attributes         Attribues of the variable\r
 \r
   @param[in] Variable           The variable information which is used to keep track of variable usage.\r
 \r
@@ -186,7 +186,7 @@ GetNextPotentialVariablePtr (
   //\r
   // Be careful about pad size for alignment\r
   //\r
-  VarHeader = (VARIABLE_HEADER *) (GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));\r
+  VarHeader = (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));\r
 \r
   return VarHeader;\r
 }\r
@@ -268,17 +268,17 @@ GetEndPointer (
   //\r
   // The end of variable store\r
   //\r
-  return (VARIABLE_HEADER *) ((UINTN) VolHeader + VolHeader->Size);\r
+  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VolHeader + VolHeader->Size);\r
 }\r
 \r
 /**\r
-  Routine used to track statistical information about variable usage. \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
+  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
+  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
+  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
@@ -320,15 +320,15 @@ UpdateVariableInfo (
       ASSERT (gVariableInfo != NULL);\r
 \r
       CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
-      gVariableInfo->Name = AllocatePool (StrSize (VariableName));\r
+      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));\r
       ASSERT (gVariableInfo->Name != NULL);\r
-      StrCpy (gVariableInfo->Name, VariableName);\r
+      StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);\r
       gVariableInfo->Volatile = Volatile;\r
 \r
       gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\r
     }\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
@@ -358,9 +358,9 @@ UpdateVariableInfo (
         ASSERT (Entry->Next != NULL);\r
 \r
         CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
-        Entry->Next->Name = AllocatePool (StrSize (VariableName));\r
+        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));\r
         ASSERT (Entry->Next->Name != NULL);\r
-        StrCpy (Entry->Next->Name, VariableName);\r
+        StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);\r
         Entry->Next->Volatile = Volatile;\r
       }\r
 \r
@@ -395,21 +395,18 @@ UpdateVariableInfo (
 \r
 **/\r
 UINTN\r
-EFIAPI\r
 GetIndexFromSupportedLangCodes(\r
   IN  CHAR8            *SupportedLang,\r
   IN  CHAR8            *Lang,\r
   IN  BOOLEAN          Iso639Language\r
-  ) \r
+  )\r
 {\r
   UINTN    Index;\r
-  UINT32   CompareLength;\r
-  CHAR8    *Supported;\r
+  UINT   CompareLength;\r
+  UINTN    LanguageLength;\r
 \r
-  Index = 0;\r
-  Supported = SupportedLang;\r
   if (Iso639Language) {\r
-    CompareLength = 3;\r
+    CompareLength = ISO_639_2_ENTRY_SIZE;\r
     for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
       if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
         //\r
@@ -425,20 +422,26 @@ GetIndexFromSupportedLangCodes(
     //\r
     // Compare RFC4646 language code\r
     //\r
-    while (*Supported != '\0') {\r
+    Index = 0;\r
+    for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r
+\r
+    for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r
       //\r
-      // take semicolon as delimitation, sequentially traverse supported language codes.\r
+      // Skip ';' characters in SupportedLang\r
       //\r
-      for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
-        Supported++;\r
-      }\r
-      if (AsciiStrnCmp (Lang, Supported - CompareLength, CompareLength) == 0) {\r
+      for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r
+      //\r
+      // Determine the length of the next language code in SupportedLang\r
+      //\r
+      for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r
+\r
+      if ((CompareLength == LanguageLength) &&\r
+          (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r
         //\r
         // Successfully find the index of Lang string in SupportedLang string.\r
         //\r
         return Index;\r
       }\r
-      Index++;\r
     }\r
     ASSERT (FALSE);\r
     return 0;\r
@@ -472,7 +475,6 @@ GetIndexFromSupportedLangCodes(
 \r
 **/\r
 CHAR8 *\r
-EFIAPI\r
 GetLangFromSupportedLangCodes (\r
   IN  CHAR8            *SupportedLang,\r
   IN  UINTN            Index,\r
@@ -480,7 +482,7 @@ GetLangFromSupportedLangCodes (
 )\r
 {\r
   UINTN    SubIndex;\r
-  UINT32   CompareLength;\r
+  UINT   CompareLength;\r
   CHAR8    *Supported;\r
 \r
   SubIndex  = 0;\r
@@ -491,10 +493,10 @@ GetLangFromSupportedLangCodes (
     // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
     // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
     //\r
-    CompareLength = 3;\r
-    SetMem (mVariableModuleGlobal->Lang, sizeof(mVariableModuleGlobal->Lang), 0);\r
+    CompareLength = ISO_639_2_ENTRY_SIZE;\r
+    mVariableModuleGlobal->Lang[CompareLength] = '\0';\r
     return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
-      \r
+\r
   } else {\r
     while (TRUE) {\r
       //\r
@@ -517,12 +519,148 @@ GetLangFromSupportedLangCodes (
         // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
         // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
         //\r
-        SetMem (mVariableModuleGlobal->PlatformLang, sizeof (mVariableModuleGlobal->PlatformLang), 0);\r
+        mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';\r
         return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r
       }\r
-      SubIndex++;                       \r
+      SubIndex++;\r
+\r
+      //\r
+      // Skip ';' characters in Supported\r
+      //\r
+      for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Returns a pointer to an allocated buffer that contains the best matching language\r
+  from a set of supported languages.\r
+\r
+  This function supports both ISO 639-2 and RFC 4646 language codes, but language\r
+  code types may not be mixed in a single call to this function. This function\r
+  supports a variable argument list that allows the caller to pass in a prioritized\r
+  list of language codes to test against all the language codes in SupportedLanguages.\r
+\r
+  If SupportedLanguages is NULL, then ASSERT().\r
+\r
+  @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 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
+                                  language codes in the format specified by Iso639Language.\r
+                                  The first language code from each of these language\r
+                                  code lists is used to determine if it is an exact or\r
+                                  close match to any of the language codes in\r
+                                  SupportedLanguages.  Close matches only apply to RFC 4646\r
+                                  language codes, and the matching algorithm from RFC 4647\r
+                                  is used to determine if a close match is present.  If\r
+                                  an exact or close match is found, then the matching\r
+                                  language code from SupportedLanguages is returned.  If\r
+                                  no matches are found, then the next variable argument\r
+                                  parameter is evaluated.  The variable argument list\r
+                                  is terminated by a NULL.\r
+\r
+  @retval NULL   The best matching language could not be found in SupportedLanguages.\r
+  @retval NULL   There are not enough resources available to return the best matching\r
+                 language.\r
+  @retval Other  A pointer to a Null-terminated ASCII string that is the best matching\r
+                 language in SupportedLanguages.\r
+\r
+**/\r
+CHAR8 *\r
+EFIAPI\r
+VariableGetBestLanguage (\r
+  IN CONST CHAR8  *SupportedLanguages,\r
+  IN UINTN        Iso639Language,\r
+  ...\r
+  )\r
+{\r
+  VA_LIST      Args;\r
+  CHAR8        *Language;\r
+  UINTN        CompareLength;\r
+  UINTN        LanguageLength;\r
+  CONST CHAR8  *Supported;\r
+  CHAR8        *Buffer;\r
+\r
+  ASSERT (SupportedLanguages != NULL);\r
+\r
+  VA_START (Args, Iso639Language);\r
+  while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
+    //\r
+    // Default to ISO 639-2 mode\r
+    //\r
+    CompareLength  = 3;\r
+    LanguageLength = MIN (3, AsciiStrLen (Language));\r
+\r
+    //\r
+    // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
+    //\r
+    if (Iso639Language == 0) {\r
+      for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
+    }\r
+\r
+    //\r
+    // Trim back the length of Language used until it is empty\r
+    //\r
+    while (LanguageLength > 0) {\r
+      //\r
+      // Loop through all language codes in SupportedLanguages\r
+      //\r
+      for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
+        //\r
+        // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
+        //\r
+        if (Iso639Language == 0) {\r
+          //\r
+          // Skip ';' characters in Supported\r
+          //\r
+          for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
+          //\r
+          // Determine the length of the next language code in Supported\r
+          //\r
+          for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
+          //\r
+          // If Language is longer than the Supported, then skip to the next language\r
+          //\r
+          if (LanguageLength > CompareLength) {\r
+            continue;\r
+          }\r
+        }\r
+        //\r
+        // See if the first LanguageLength characters in Supported match Language\r
+        //\r
+        if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
+          VA_END (Args);\r
+\r
+          Buffer = (Iso639Language != 0) ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r
+          Buffer[CompareLength] = '\0';\r
+          return CopyMem (Buffer, Supported, CompareLength);\r
+        }\r
+      }\r
+\r
+      if (Iso639Language != 0) {\r
+        //\r
+        // If ISO 639 mode, then each language can only be tested once\r
+        //\r
+        LanguageLength = 0;\r
+      } else {\r
+        //\r
+        // If RFC 4646 mode, then trim Language from the right to the next '-' character\r
+        //\r
+        for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
+      }\r
     }\r
   }\r
+  VA_END (Args);\r
+\r
+  //\r
+  // No matches were found\r
+  //\r
+  return NULL;\r
 }\r
 \r
 /**\r
@@ -539,105 +677,190 @@ GetLangFromSupportedLangCodes (
 \r
   @param[in] DataSize           Size of data. 0 means delete\r
 \r
-  @retval EFI_SUCCESS  auto update operation is successful.\r
-\r
 **/\r
-EFI_STATUS\r
-EFIAPI\r
+VOID\r
 AutoUpdateLangVariable(\r
   IN  CHAR16             *VariableName,\r
   IN  VOID               *Data,\r
   IN  UINTN              DataSize\r
   )\r
 {\r
-  EFI_STATUS     Status;\r
-  CHAR8          *BestPlatformLang;\r
-  CHAR8          *BestLang;\r
-  UINTN          Index;\r
-  UINT32         Attributes;\r
+  EFI_STATUS             Status;\r
+  CHAR8                  *BestPlatformLang;\r
+  CHAR8                  *BestLang;\r
+  UINTN                  Index;\r
+  UINT32                 Attributes;\r
   VARIABLE_POINTER_TRACK Variable;\r
+  BOOLEAN                SetLanguageCodes;\r
 \r
   //\r
-  // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
+  // Don't do updates for delete operation\r
   //\r
-  Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
+  if (DataSize == 0) {\r
+    return;\r
+  }\r
+\r
+  SetLanguageCodes = FALSE;\r
 \r
   if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {\r
+    //\r
+    // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
+    //\r
+    if (EfiAtRuntime ()) {\r
+      return;\r
+    }\r
+\r
+    SetLanguageCodes = TRUE;\r
+\r
     //\r
     // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
     // Therefore, in variable driver, only store the original value for other use.\r
     //\r
-    AsciiStrnCpy (mVariableModuleGlobal->PlatformLangCodes, Data, DataSize);\r
-  } else if (StrCmp (VariableName, L"LangCodes") == 0) {\r
+    if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r
+      FreePool (mVariableModuleGlobal->PlatformLangCodes);\r
+    }\r
+    mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
+    ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r
+\r
     //\r
-    // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
-    // Therefore, in variable driver, only store the original value for other use.\r
+    // PlatformLang holds a single language from PlatformLangCodes,\r
+    // so the size of PlatformLangCodes is enough for the PlatformLang.\r
     //\r
-    AsciiStrnCpy (mVariableModuleGlobal->LangCodes, Data, DataSize);\r
-  } else if ((StrCmp (VariableName, L"PlatformLang") == 0) && (DataSize != 0)) {\r
-    ASSERT (AsciiStrLen (mVariableModuleGlobal->PlatformLangCodes) != 0);\r
+    if (mVariableModuleGlobal->PlatformLang != NULL) {\r
+      FreePool (mVariableModuleGlobal->PlatformLang);\r
+    }\r
+    mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
+    ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r
 \r
+  } else if (StrCmp (VariableName, L"LangCodes") == 0) {\r
     //\r
-    // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
+    // LangCodes is a volatile variable, so it can not be updated at runtime.\r
     //\r
-    BestPlatformLang = GetBestLanguage(mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
+    if (EfiAtRuntime ()) {\r
+      return;\r
+    }\r
+\r
+    SetLanguageCodes = TRUE;\r
 \r
     //\r
-    // Get the corresponding index in language codes.\r
+    // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
+    // Therefore, in variable driver, only store the original value for other use.\r
     //\r
-    Index = GetIndexFromSupportedLangCodes(mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
+    if (mVariableModuleGlobal->LangCodes != NULL) {\r
+      FreePool (mVariableModuleGlobal->LangCodes);\r
+    }\r
+    mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
+    ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r
+  }\r
 \r
+  if (SetLanguageCodes\r
+      && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r
+      && (mVariableModuleGlobal->LangCodes != NULL)) {\r
     //\r
-    // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
+    // Update Lang if PlatformLang is already set\r
+    // Update PlatformLang if Lang is already set\r
     //\r
-    BestLang = GetLangFromSupportedLangCodes(mVariableModuleGlobal->LangCodes, Index, TRUE);\r
+    Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *) mVariableModuleGlobal);\r
+    if (!EFI_ERROR (Status)) {\r
+      //\r
+      // Update Lang\r
+      //\r
+      VariableName = L"PlatformLang";\r
+      Data         = GetVariableDataPtr (Variable.CurrPtr);\r
+      DataSize     = Variable.CurrPtr->DataSize;\r
+    } else {\r
+      Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *) mVariableModuleGlobal);\r
+      if (!EFI_ERROR (Status)) {\r
+        //\r
+        // Update PlatformLang\r
+        //\r
+        VariableName = L"Lang";\r
+        Data         = GetVariableDataPtr (Variable.CurrPtr);\r
+        DataSize     = Variable.CurrPtr->DataSize;\r
+      } else {\r
+        //\r
+        // Neither PlatformLang nor Lang is set, directly return\r
+        //\r
+        return;\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
+  //\r
+  Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
 \r
+  if (StrCmp (VariableName, L"PlatformLang") == 0) {\r
     //\r
-    // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
+    // Update Lang when PlatformLangCodes/LangCodes were set.\r
     //\r
-    FindVariable(L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);\r
+    if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
+      //\r
+      // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
+      //\r
+      BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
+      if (BestPlatformLang != NULL) {\r
+        //\r
+        // Get the corresponding index in language codes.\r
+        //\r
+        Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
 \r
-    Status = UpdateVariable(L"Lang", &gEfiGlobalVariableGuid, \r
-                    BestLang, ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r
+        //\r
+        // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
+        //\r
+        BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r
 \r
-    DEBUG((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r
+        //\r
+        // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
+        //\r
+        FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);\r
 \r
-    ASSERT_EFI_ERROR(Status);\r
+        Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang, ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r
 \r
-  } else if ((StrCmp (VariableName, L"Lang") == 0) && (DataSize != 0)) {\r
-    ASSERT (AsciiStrLen (mVariableModuleGlobal->LangCodes) != 0);\r
+        DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r
 \r
-    //\r
-    // When setting Lang, firstly get most matched language string from supported language codes.\r
-    //\r
-    BestLang = GetBestLanguage(mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
+        ASSERT_EFI_ERROR(Status);\r
+      }\r
+    }\r
 \r
+  } else if (StrCmp (VariableName, L"Lang") == 0) {\r
     //\r
-    // Get the corresponding index in language codes.\r
+    // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
     //\r
-    Index = GetIndexFromSupportedLangCodes(mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
+    if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
+      //\r
+      // When setting Lang, firstly get most matched language string from supported language codes.\r
+      //\r
+      BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
+      if (BestLang != NULL) {\r
+        //\r
+        // Get the corresponding index in language codes.\r
+        //\r
+        Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
 \r
-    //\r
-    // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
-    //\r
-    BestPlatformLang = GetLangFromSupportedLangCodes(mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
+        //\r
+        // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
+        //\r
+        BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
 \r
-    //\r
-    // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
-    //\r
-    FindVariable(L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);\r
+        //\r
+        // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
+        //\r
+        FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);\r
 \r
-    Status = UpdateVariable(L"PlatformLang", &gEfiGlobalVariableGuid, \r
-                    BestPlatformLang, AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r
+        Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang,\r
+                                 AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r
 \r
-    DEBUG((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r
-    ASSERT_EFI_ERROR(Status);\r
+        DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r
+        ASSERT_EFI_ERROR (Status);\r
+      }\r
+    }\r
   }\r
-  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  Update the variable region with Variable information. These are the same \r
+  Update the variable region with Variable information. These are the same\r
   arguments as the EFI Variable services.\r
 \r
   @param[in] VariableName       Name of variable\r
@@ -684,10 +907,10 @@ UpdateVariable (
     // Update/Delete existing variable\r
     //\r
 \r
-    if (EfiAtRuntime ()) {        \r
+    if (EfiAtRuntime ()) {\r
       //\r
-      // If EfiAtRuntime and the variable is Volatile and Runtime Access,  \r
-      // the volatile is ReadOnly, and SetVariable should be aborted and \r
+      // If EfiAtRuntime and the variable is Volatile and Runtime Access,\r
+      // the volatile is ReadOnly, and SetVariable should be aborted and\r
       // return EFI_WRITE_PROTECTED.\r
       //\r
       if (Variable->Volatile) {\r
@@ -729,21 +952,21 @@ UpdateVariable (
       //\r
       Variable->CurrPtr->State &= VAR_IN_DELETED_TRANSITION;\r
     }\r
-    \r
+\r
   } else {\r
     //\r
     // No found existing variable, Create a new variable\r
-    //  \r
-    \r
+    //\r
+\r
     //\r
     // Make sure we are trying to create a new variable.\r
-    // Setting a data variable with no access, or zero DataSize attributes means to delete it.    \r
+    // Setting a data variable with no access, or zero DataSize attributes means to delete it.\r
     //\r
     if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
       Status = EFI_NOT_FOUND;\r
       goto Done;\r
     }\r
-        \r
+\r
     //\r
     // Only variable have NV|RT attribute can be created in Runtime\r
     //\r
@@ -751,14 +974,14 @@ UpdateVariable (
         (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
       Status = EFI_INVALID_PARAMETER;\r
       goto Done;\r
-    }         \r
+    }\r
   }\r
-  \r
+\r
   //\r
   // Function part - create a new variable and copy the data.\r
   // Both update a variable and create a variable will come here.\r
   //\r
-  \r
+\r
   VarNameOffset = sizeof (VARIABLE_HEADER);\r
   VarNameSize   = StrSize (VariableName);\r
   VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
@@ -766,25 +989,25 @@ UpdateVariable (
 \r
   if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
     NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(Global->NonVolatileVariableBase))->Size;\r
-    if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
-      && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > FixedPcdGet32(PcdHwErrStorageSize)))\r
-      || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
-      && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - FixedPcdGet32(PcdHwErrStorageSize)))) {\r
+    if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
+      && ((HEADER_ALIGN (VarSize) + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
+      || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)\r
+      && ((HEADER_ALIGN (VarSize) + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
       goto Done;\r
     }\r
 \r
     NextVariable  = (VARIABLE_HEADER *) (UINT8 *) (mVariableModuleGlobal->NonVolatileLastVariableOffset\r
                       + (UINTN) Global->NonVolatileVariableBase);\r
-    mVariableModuleGlobal->NonVolatileLastVariableOffset += VarSize;\r
+    mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
 \r
     if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
-      mVariableModuleGlobal->HwErrVariableTotalSize += VarSize;\r
+      mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
     } else {\r
-      mVariableModuleGlobal->CommonVariableTotalSize += VarSize;\r
+      mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
     }\r
   } else {\r
-    if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
+    if ((UINT32) (HEADER_ALIGN (VarSize) + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
           ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size\r
           ) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
@@ -793,7 +1016,7 @@ UpdateVariable (
 \r
     NextVariable    = (VARIABLE_HEADER *) (UINT8 *) (mVariableModuleGlobal->VolatileLastVariableOffset\r
                         + (UINTN) Global->VolatileVariableBase);\r
-    mVariableModuleGlobal->VolatileLastVariableOffset += VarSize;\r
+    mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
   }\r
 \r
   NextVariable->StartId     = VARIABLE_DATA;\r
@@ -880,8 +1103,8 @@ FindVariable (
   // Start Pointers for the variable.\r
   // Actual Data Pointer where data can be written.\r
   //\r
-  Variable[0] = (VARIABLE_HEADER *) (VariableStoreHeader[0] + 1);\r
-  Variable[1] = (VARIABLE_HEADER *) (VariableStoreHeader[1] + 1);\r
+  Variable[0] = (VARIABLE_HEADER *) HEADER_ALIGN (VariableStoreHeader[0] + 1);\r
+  Variable[1] = (VARIABLE_HEADER *) HEADER_ALIGN (VariableStoreHeader[1] + 1);\r
 \r
   if (VariableName[0] != 0 && VendorGuid == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -890,7 +1113,7 @@ FindVariable (
   // Find the variable by walk through non-volatile and volatile variable store\r
   //\r
   for (Index = 0; Index < 2; Index++) {\r
-    PtrTrack->StartPtr  = (VARIABLE_HEADER *) (VariableStoreHeader[Index] + 1);\r
+    PtrTrack->StartPtr  = (VARIABLE_HEADER *) HEADER_ALIGN (VariableStoreHeader[Index] + 1);\r
     PtrTrack->EndPtr    = GetEndPointer (VariableStoreHeader[Index]);\r
 \r
     while ((Variable[Index] < GetEndPointer (VariableStoreHeader[Index])) && (Variable[Index] != NULL)) {\r
@@ -921,21 +1144,21 @@ FindVariable (
 \r
 /**\r
   This code finds variable in storage blocks (Volatile or Non-Volatile).\r
-  \r
+\r
   @param  VariableName           A Null-terminated Unicode string that is the name of\r
                                  the vendor's variable.\r
   @param  VendorGuid             A unique identifier for the vendor.\r
-  @param  Attributes             If not NULL, a pointer to the memory location to return the \r
+  @param  Attributes             If not NULL, a pointer to the memory location to return the\r
                                  attributes bitmask for the variable.\r
   @param  DataSize               Size of Data found. If size is less than the\r
                                  data, this value contains the required size.\r
-  @param  Data                   On input, the size in bytes of the return Data buffer.  \r
+  @param  Data                   On input, the size in bytes of the return Data buffer.\r
                                  On output, the size of data returned in Data.\r
   @param  Global                 Pointer to VARIABLE_GLOBAL structure\r
 \r
-  @retval EFI_SUCCESS            The function completed successfully. \r
+  @retval EFI_SUCCESS            The function completed successfully.\r
   @retval EFI_NOT_FOUND          The variable was not found.\r
-  @retval EFI_BUFFER_TOO_SMALL   DataSize is too small for the result.  DataSize has \r
+  @retval EFI_BUFFER_TOO_SMALL   DataSize is too small for the result.  DataSize has\r
                                  been updated with the size needed to complete the request.\r
   @retval EFI_INVALID_PARAMETER  VariableName or VendorGuid or DataSize is NULL.\r
 \r
@@ -954,11 +1177,16 @@ EmuGetVariable (
   VARIABLE_POINTER_TRACK  Variable;\r
   UINTN                   VarDataSize;\r
   EFI_STATUS              Status;\r
+  UINT8                   *VariableDataPtr;\r
 \r
   if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (VariableName[0] == 0) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
   AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
 \r
   //\r
@@ -978,8 +1206,10 @@ EmuGetVariable (
       Status = EFI_INVALID_PARAMETER;\r
       goto Done;\r
     }\r
+    VariableDataPtr = GetVariableDataPtr (Variable.CurrPtr);\r
+    ASSERT (VariableDataPtr != NULL);\r
 \r
-    CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
+    CopyMem (Data, VariableDataPtr, VarDataSize);\r
     if (Attributes != NULL) {\r
       *Attributes = Variable.CurrPtr->Attributes;\r
     }\r
@@ -1003,18 +1233,23 @@ Done:
 \r
   This code Finds the Next available variable.\r
 \r
-  @param  VariableNameSize       Size of the variable.\r
+  @param  VariableNameSize       The size of the VariableName buffer. The size must be large enough to fit input\r
+                                 string supplied in VariableName buffer.\r
   @param  VariableName           On input, supplies the last VariableName that was returned by GetNextVariableName().\r
                                  On output, returns the Null-terminated Unicode string of the current variable.\r
   @param  VendorGuid             On input, supplies the last VendorGuid that was returned by GetNextVariableName().\r
-                                 On output, returns the VendorGuid of the current variable.  \r
+                                 On output, returns the VendorGuid of the current variable.\r
   @param  Global                 Pointer to VARIABLE_GLOBAL structure.\r
 \r
-  @retval EFI_SUCCESS            The function completed successfully. \r
+  @retval EFI_SUCCESS            The function completed successfully.\r
   @retval EFI_NOT_FOUND          The next variable was not found.\r
-  @retval EFI_BUFFER_TOO_SMALL   VariableNameSize is too small for the result. \r
+  @retval EFI_BUFFER_TOO_SMALL   The VariableNameSize is too small for the result.\r
                                  VariableNameSize has been updated with the size needed to complete the request.\r
   @retval EFI_INVALID_PARAMETER  VariableNameSize or VariableName or 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
+  @retval EFI_INVALID_PARAMETER  Null-terminator is not found in the first VariableNameSize bytes of\r
+                                 the input VariableName buffer.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1029,16 +1264,42 @@ EmuGetNextVariableName (
   VARIABLE_POINTER_TRACK  Variable;\r
   UINTN                   VarNameSize;\r
   EFI_STATUS              Status;\r
+  UINTN                   MaxLen;\r
 \r
   if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // Calculate the possible maximum length of name string, including the Null terminator.\r
+  //\r
+  MaxLen = *VariableNameSize / sizeof (CHAR16);\r
+  if ((MaxLen == 0) || (StrnLenS (VariableName, MaxLen) == MaxLen)) {\r
+    //\r
+    // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,\r
+    // follow spec to return EFI_INVALID_PARAMETER.\r
+    //\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
 \r
   Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
 \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
@@ -1056,7 +1317,7 @@ EmuGetNextVariableName (
     if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r
       Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));\r
       if (Variable.Volatile) {\r
-        Variable.StartPtr = (VARIABLE_HEADER *) ((UINTN) (Global->VolatileVariableBase + sizeof (VARIABLE_STORE_HEADER)));\r
+        Variable.StartPtr = (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) (Global->VolatileVariableBase + sizeof (VARIABLE_STORE_HEADER)));\r
         Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase));\r
       } else {\r
         Status = EFI_NOT_FOUND;\r
@@ -1107,9 +1368,9 @@ Done:
   This code sets variable in storage blocks (Volatile or Non-Volatile).\r
 \r
   @param  VariableName           A Null-terminated Unicode string that is the name of the vendor's\r
-                                 variable.  Each VariableName is unique for each \r
-                                 VendorGuid.  VariableName must contain 1 or more \r
-                                 Unicode characters.  If VariableName is an empty Unicode \r
+                                 variable.  Each VariableName is unique for each\r
+                                 VendorGuid.  VariableName must contain 1 or more\r
+                                 Unicode characters.  If VariableName is an empty Unicode\r
                                  string, then EFI_INVALID_PARAMETER is returned.\r
   @param  VendorGuid             A unique identifier for the vendor\r
   @param  Attributes             Attributes bitmask to set for the variable\r
@@ -1120,10 +1381,10 @@ Done:
   @param  VolatileOffset         The offset of last volatile variable\r
   @param  NonVolatileOffset      The offset of last non-volatile variable\r
 \r
-  @retval EFI_SUCCESS            The firmware has successfully stored the variable and its data as \r
+  @retval EFI_SUCCESS            The firmware has successfully stored the variable and its data as\r
                                  defined by the Attributes.\r
-  @retval EFI_INVALID_PARAMETER  An invalid combination of attribute bits was supplied, or the \r
-                                 DataSize exceeds the maximum allowed, or VariableName is an empty \r
+  @retval EFI_INVALID_PARAMETER  An invalid combination of attribute bits was supplied, or the\r
+                                 DataSize exceeds the maximum allowed, or VariableName is an empty\r
                                  Unicode string, or VendorGuid is NULL.\r
   @retval EFI_OUT_OF_RESOURCES   Not enough storage is available to hold the variable and its data.\r
   @retval EFI_DEVICE_ERROR       The variable could not be saved due to a hardware failure.\r
@@ -1152,26 +1413,41 @@ EmuSetVariable (
   //\r
   if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
-  }  \r
+  }\r
 \r
   if (DataSize != 0 && Data == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // Not support authenticated variable write yet.\r
+  //\r
+  if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   //\r
   //  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
   }\r
+\r
+\r
+  if ((UINTN)(~0) - DataSize < StrSize(VariableName)){\r
+    //\r
+    // Prevent whole variable size overflow\r
+    //\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   //\r
   //  The size of the VariableName, including the Unicode Null in bytes plus\r
-  //  the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxHardwareErrorVariableSize)\r
-  //  bytes for HwErrRec, and FixedPcdGet32(PcdMaxVariableSize) bytes for the others.\r
+  //  the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
+  //  bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
   //\r
   if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-    if ((DataSize > FixedPcdGet32(PcdMaxHardwareErrorVariableSize)) ||                                                       \r
-        (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > FixedPcdGet32(PcdMaxHardwareErrorVariableSize))) {\r
+    if (StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER)) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
     //\r
@@ -1183,12 +1459,11 @@ EmuSetVariable (
   } else {\r
   //\r
   //  The size of the VariableName, including the Unicode Null in bytes plus\r
-  //  the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxVariableSize) bytes.\r
+  //  the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r
   //\r
-    if ((DataSize > FixedPcdGet32(PcdMaxVariableSize)) ||\r
-        (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > FixedPcdGet32(PcdMaxVariableSize))) {\r
+    if (StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER)) {\r
       return EFI_INVALID_PARAMETER;\r
-    }  \r
+    }\r
   }\r
 \r
   AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
@@ -1196,7 +1471,7 @@ EmuSetVariable (
   //\r
   // Check whether the input variable is already existed\r
   //\r
-  \r
+\r
   Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
 \r
   //\r
@@ -1216,18 +1491,18 @@ EmuSetVariable (
 \r
   @param  Attributes                   Attributes bitmask to specify the type of variables\r
                                        on which to return information.\r
-  @param  MaximumVariableStorageSize   On output the maximum size of the storage space available for \r
-                                       the EFI variables associated with the attributes specified.  \r
-  @param  RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI \r
+  @param  MaximumVariableStorageSize   On output the maximum size of the storage space available for\r
+                                       the EFI variables associated with the attributes specified.\r
+  @param  RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI\r
                                        variables associated with the attributes specified.\r
-  @param  MaximumVariableSize          Returns the maximum size of an individual EFI variable \r
+  @param  MaximumVariableSize          Returns the maximum size of an individual EFI variable\r
                                        associated with the attributes specified.\r
   @param  Global                       Pointer to VARIABLE_GLOBAL structure.\r
 \r
   @retval EFI_SUCCESS                  Valid answer returned.\r
   @retval EFI_INVALID_PARAMETER        An invalid combination of attribute bits was supplied\r
-  @retval EFI_UNSUPPORTED              The attribute is not supported on this platform, and the \r
-                                       MaximumVariableStorageSize, RemainingVariableStorageSize, \r
+  @retval EFI_UNSUPPORTED              The attribute is not supported on this platform, and the\r
+                                       MaximumVariableStorageSize, RemainingVariableStorageSize,\r
                                        MaximumVariableSize are undefined.\r
 \r
 **/\r
@@ -1254,12 +1529,12 @@ EmuQueryVariableInfo (
   if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
     //\r
     // Make sure the Attributes combination is supported by the platform.\r
     //\r
-    return EFI_UNSUPPORTED;  \r
+    return EFI_UNSUPPORTED;\r
   } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
     //\r
     // Make sure if runtime bit is set, boot service bit is set also.\r
@@ -1275,6 +1550,11 @@ EmuQueryVariableInfo (
     // Make sure Hw Attribute is set with NV.\r
     //\r
     return EFI_INVALID_PARAMETER;\r
+  } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
+    //\r
+    // Not support authentiated variable write yet.\r
+    //\r
+    return EFI_UNSUPPORTED;\r
   }\r
 \r
   AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
@@ -1301,24 +1581,24 @@ EmuQueryVariableInfo (
   // Harware error record variable needs larger size.\r
   //\r
   if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-    *MaximumVariableStorageSize = FixedPcdGet32(PcdHwErrStorageSize);\r
-    *MaximumVariableSize = FixedPcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
+    *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
+    *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
   } else {\r
     if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
-      ASSERT (FixedPcdGet32(PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
-      *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - FixedPcdGet32(PcdHwErrStorageSize);\r
+      ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
+      *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);\r
     }\r
 \r
     //\r
-    // Let *MaximumVariableSize be FixedPcdGet32(PcdMaxVariableSize) with the exception of the variable header size.\r
+    // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r
     //\r
-    *MaximumVariableSize = FixedPcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
+    *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
   }\r
 \r
   //\r
   // Point to the starting address of the variables.\r
   //\r
-  Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
+  Variable = (VARIABLE_HEADER *) HEADER_ALIGN (VariableStoreHeader + 1);\r
 \r
   //\r
   // Now walk through the related variable store.\r
@@ -1330,7 +1610,7 @@ EmuQueryVariableInfo (
     }\r
     VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
 \r
-    if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
+    if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
       HwErrVariableTotalSize += VariableSize;\r
     } else {\r
       CommonVariableTotalSize += VariableSize;\r
@@ -1353,7 +1633,7 @@ EmuQueryVariableInfo (
   } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
     *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
   }\r
-  \r
+\r
   ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
   return EFI_SUCCESS;\r
 }\r
@@ -1371,10 +1651,15 @@ InitializeVariableStore (
   IN  BOOLEAN               VolatileStore\r
   )\r
 {\r
+  EFI_STATUS            Status;\r
   VARIABLE_STORE_HEADER *VariableStore;\r
   BOOLEAN               FullyInitializeStore;\r
   EFI_PHYSICAL_ADDRESS  *VariableBase;\r
   UINTN                 *LastVariableOffset;\r
+  VARIABLE_STORE_HEADER *VariableStoreHeader;\r
+  VARIABLE_HEADER       *Variable;\r
+  VOID                  *VariableData;\r
+  EFI_HOB_GUID_TYPE     *GuidHob;\r
 \r
   FullyInitializeStore = TRUE;\r
 \r
@@ -1389,18 +1674,16 @@ InitializeVariableStore (
   //\r
   // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
   // is stored with common variable in the same NV region. So the platform integrator should\r
-  // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of \r
+  // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of\r
   // PcdVariableStoreSize.\r
   //\r
-  ASSERT (FixedPcdGet32(PcdHwErrStorageSize) <= FixedPcdGet32(PcdVariableStoreSize));\r
+  ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdVariableStoreSize));\r
 \r
   //\r
   // Allocate memory for variable store.\r
   //\r
   if (VolatileStore || (PcdGet64 (PcdEmuVariableNvStoreReserved) == 0)) {\r
-    VariableStore = (VARIABLE_STORE_HEADER *) AllocateRuntimePool (\r
-                                                FixedPcdGet32(PcdVariableStoreSize)\r
-                                                );\r
+    VariableStore = (VARIABLE_STORE_HEADER *) AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize));\r
   } else {\r
     //\r
     // A memory location has been reserved for the NV variable store.  Certain\r
@@ -1411,7 +1694,7 @@ InitializeVariableStore (
       (VARIABLE_STORE_HEADER *)(VOID*)(UINTN)\r
         PcdGet64 (PcdEmuVariableNvStoreReserved);\r
     if (\r
-         (VariableStore->Size == FixedPcdGet32(PcdVariableStoreSize)) &&\r
+         (VariableStore->Size == PcdGet32 (PcdVariableStoreSize)) &&\r
          (VariableStore->Format == VARIABLE_STORE_FORMATTED) &&\r
          (VariableStore->State == VARIABLE_STORE_HEALTHY)\r
        ) {\r
@@ -1429,7 +1712,7 @@ InitializeVariableStore (
   }\r
 \r
   if (FullyInitializeStore) {\r
-    SetMem (VariableStore, FixedPcdGet32(PcdVariableStoreSize), 0xff);\r
+    SetMem (VariableStore, PcdGet32 (PcdVariableStoreSize), 0xff);\r
   }\r
 \r
   //\r
@@ -1439,12 +1722,50 @@ InitializeVariableStore (
   InitializeLocationForLastVariableOffset (VariableStore, LastVariableOffset);\r
 \r
   CopyGuid (&VariableStore->Signature, &gEfiVariableGuid);\r
-  VariableStore->Size       = FixedPcdGet32(PcdVariableStoreSize);\r
+  VariableStore->Size       = PcdGet32 (PcdVariableStoreSize);\r
   VariableStore->Format     = VARIABLE_STORE_FORMATTED;\r
   VariableStore->State      = VARIABLE_STORE_HEALTHY;\r
   VariableStore->Reserved   = 0;\r
   VariableStore->Reserved1  = 0;\r
 \r
+  if (!VolatileStore) {\r
+    //\r
+    // Get HOB variable store.\r
+    //\r
+    GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r
+    if (GuidHob != NULL) {\r
+      VariableStoreHeader = (VARIABLE_STORE_HEADER *) GET_GUID_HOB_DATA (GuidHob);\r
+      if (CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&\r
+          (VariableStoreHeader->Format == VARIABLE_STORE_FORMATTED) &&\r
+          (VariableStoreHeader->State == VARIABLE_STORE_HEALTHY)\r
+         ) {\r
+        DEBUG ((EFI_D_INFO, "HOB Variable Store appears to be valid.\n"));\r
+        //\r
+        // Flush the HOB variable to Emulation Variable storage.\r
+        //\r
+        for ( Variable = (VARIABLE_HEADER *) HEADER_ALIGN (VariableStoreHeader + 1)\r
+            ; (Variable < GetEndPointer (VariableStoreHeader) && (Variable != NULL))\r
+            ; Variable = GetNextVariablePtr (Variable)\r
+            ) {\r
+          ASSERT (Variable->State == VAR_ADDED);\r
+          ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
+          VariableData = GetVariableDataPtr (Variable);\r
+          Status = EmuSetVariable (\r
+                     GET_VARIABLE_NAME_PTR (Variable),\r
+                     &Variable->VendorGuid,\r
+                     Variable->Attributes,\r
+                     Variable->DataSize,\r
+                     VariableData,\r
+                     &mVariableModuleGlobal->VariableGlobal[Physical],\r
+                     &mVariableModuleGlobal->VolatileLastVariableOffset,\r
+                     &mVariableModuleGlobal->NonVolatileLastVariableOffset\r
+                     );\r
+          ASSERT_EFI_ERROR (Status);\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r