]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/HiiDatabaseDxe/ConfigRouting.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / ConfigRouting.c
index f5e054d26577ab9bfa1dab1895fab96d26a242ba..71ea25bc19bfb17899ceef3a89464f5eb4d6f3e8 100644 (file)
@@ -1,14 +1,8 @@
 /** @file\r
 Implementation of interfaces function for EFI_HII_CONFIG_ROUTING_PROTOCOL.\r
 \r
-Copyright (c) 2007 - 2014, 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
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -44,7 +38,7 @@ CalculateConfigStringLen (
   //\r
   // The beginning of next <ConfigRequest>/<ConfigResp> should be "&GUID=".\r
   // Will meet '\0' if there is only one <ConfigRequest>/<ConfigResp>.\r
-  // \r
+  //\r
   TmpPtr = StrStr (String, L"&GUID=");\r
   if (TmpPtr == NULL) {\r
     return StrLen (String);\r
@@ -65,7 +59,7 @@ CalculateConfigStringLen (
 \r
   @retval EFI_NOT_FOUND          The device path is not invalid.\r
   @retval EFI_INVALID_PARAMETER  Any incoming parameter is invalid.\r
-  @retval EFI_OUT_OF_RESOURCES   Lake of resources to store neccesary structures.\r
+  @retval EFI_OUT_OF_RESOURCES   Lake of resources to store necessary structures.\r
   @retval EFI_SUCCESS            The device path is retrieved and translated to\r
                                  binary format.\r
 \r
@@ -117,7 +111,7 @@ GetDevicePath (
   if (((Length + 1) / 2) < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
     return EFI_NOT_FOUND;\r
   }\r
-  \r
+\r
   //\r
   // The data in <PathHdr> is encoded as hex UNICODE %02x bytes in the same order\r
   // as the device path resides in RAM memory.\r
@@ -127,7 +121,7 @@ GetDevicePath (
   if (DevicePathBuffer == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  \r
+\r
   //\r
   // Convert DevicePath\r
   //\r
@@ -141,7 +135,7 @@ GetDevicePath (
       DevicePathBuffer [Index/2] = (UINT8) ((DevicePathBuffer [Index/2] << 4) + DigitUint8);\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Validate DevicePath\r
   //\r
@@ -208,10 +202,10 @@ HiiToLower (
 \r
   @param  BufferLen              The length of the Buffer in bytes.\r
 \r
-  @param  Buffer                 Points to a buffer which will be converted to be the \r
+  @param  Buffer                 Points to a buffer which will be converted to be the\r
                                  content of the generated string.\r
 \r
-  @param  Flag                   If 1, the buffer contains data for the value of GUID or PATH stored in \r
+  @param  Flag                   If 1, the buffer contains data for the value of GUID or PATH stored in\r
                                  UINT8 *; if 2, the buffer contains unicode string for the value of NAME;\r
                                  if 3, the buffer contains other data.\r
 \r
@@ -244,7 +238,7 @@ GenerateSubStr (
     ASSERT (*SubStr != NULL);\r
     return;\r
   }\r
-  \r
+\r
   //\r
   // Header + Data + '&' + '\0'\r
   //\r
@@ -252,8 +246,7 @@ GenerateSubStr (
   Str    = AllocateZeroPool (Length * sizeof (CHAR16));\r
   ASSERT (Str != NULL);\r
 \r
-  StrCpy (Str, String);\r
-  Length = (BufferLen * 2 + 1) * sizeof (CHAR16);\r
+  StrCpyS (Str, Length, String);\r
 \r
   StringHeader = Str + StrLen (String);\r
   TemString    = (CHAR16 *) StringHeader;\r
@@ -265,7 +258,14 @@ GenerateSubStr (
     //\r
     TemBuffer = ((UINT8 *) Buffer);\r
     for (Index = 0; Index < BufferLen; Index ++, TemBuffer ++) {\r
-      TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemBuffer, 2);\r
+      UnicodeValueToStringS (\r
+        TemString,\r
+        sizeof (CHAR16) * (Length - StrnLenS (Str, Length)),\r
+        PREFIX_ZERO | RADIX_HEX,\r
+        *TemBuffer,\r
+        2\r
+        );\r
+      TemString += StrnLenS (TemString, Length - StrnLenS (Str, Length));\r
     }\r
     break;\r
   case 2:\r
@@ -278,7 +278,14 @@ GenerateSubStr (
     // Convert Unicode String to Config String, e.g. "ABCD" => "0041004200430044"\r
     //\r
     for (; *TemName != L'\0'; TemName++) {\r
-      TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemName, 4);\r
+      UnicodeValueToStringS (\r
+        TemString,\r
+        sizeof (CHAR16) * (Length - StrnLenS (Str, Length)),\r
+        PREFIX_ZERO | RADIX_HEX,\r
+        *TemName,\r
+        4\r
+        );\r
+      TemString += StrnLenS (TemString, Length - StrnLenS (Str, Length));\r
     }\r
     break;\r
   case 3:\r
@@ -287,7 +294,14 @@ GenerateSubStr (
     //\r
     TemBuffer = ((UINT8 *) Buffer) + BufferLen - 1;\r
     for (Index = 0; Index < BufferLen; Index ++, TemBuffer --) {\r
-      TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemBuffer, 2);\r
+      UnicodeValueToStringS (\r
+        TemString,\r
+        sizeof (CHAR16) * (Length - StrnLenS (Str, Length)),\r
+        PREFIX_ZERO | RADIX_HEX,\r
+        *TemBuffer,\r
+        2\r
+        );\r
+      TemString += StrnLenS (TemString, Length - StrnLenS (Str, Length));\r
     }\r
     break;\r
   default:\r
@@ -297,7 +311,7 @@ GenerateSubStr (
   //\r
   // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.\r
   //\r
-  StrCat (Str, L"&");  \r
+  StrCatS (Str, Length, L"&");\r
   HiiToLower (Str);\r
 \r
   *SubStr = Str;\r
@@ -332,7 +346,7 @@ OutputConfigBody (
   if (String == NULL || ConfigBody == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   //\r
   // The setting information should start OFFSET, not ALTCFG.\r
   //\r
@@ -392,6 +406,7 @@ AppendToMultiString (
 {\r
   UINTN AppendStringSize;\r
   UINTN MultiStringSize;\r
+  UINTN MaxLen;\r
 \r
   if (MultiString == NULL || *MultiString == NULL || AppendString == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -399,6 +414,7 @@ AppendToMultiString (
 \r
   AppendStringSize = StrSize (AppendString);\r
   MultiStringSize  = StrSize (*MultiString);\r
+  MaxLen = MAX_STRING_LENGTH / sizeof (CHAR16);\r
 \r
   //\r
   // Enlarge the buffer each time when length exceeds MAX_STRING_LENGTH.\r
@@ -410,12 +426,13 @@ AppendToMultiString (
                                   MultiStringSize + AppendStringSize,\r
                                   (VOID *) (*MultiString)\r
                                   );\r
+    MaxLen = (MultiStringSize + AppendStringSize) / sizeof (CHAR16);\r
     ASSERT (*MultiString != NULL);\r
   }\r
   //\r
   // Append the incoming string\r
   //\r
-  StrCat (*MultiString, AppendString);\r
+  StrCatS (*MultiString, MaxLen, AppendString);\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -434,7 +451,7 @@ AppendToMultiString (
                                  to free memory.\r
   @param  Len                    Length of the <Number>, in characters.\r
 \r
-  @retval EFI_OUT_OF_RESOURCES   Insufficient resources to store neccessary\r
+  @retval EFI_OUT_OF_RESOURCES   Insufficient resources to store necessary\r
                                  structures.\r
   @retval EFI_SUCCESS            Value of <Number> is outputted in Number\r
                                  successfully.\r
@@ -483,7 +500,7 @@ GetValueOfNumber (
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Exit;\r
   }\r
-  \r
+\r
   Length = *Len;\r
   ZeroMem (TemStr, sizeof (TemStr));\r
   for (Index = 0; Index < Length; Index ++) {\r
@@ -507,15 +524,490 @@ Exit:
   return Status;\r
 }\r
 \r
+/**\r
+ To find the BlockName in the string with same value.\r
+\r
+  @param  String                 Pointer to a Null-terminated Unicode string.\r
+  @param  BlockName              Pointer to a Null-terminated Unicode string to search for.\r
+  @param  Buffer                 Pointer to the value correspond to the BlockName.\r
+  @param  Found                  The Block whether has been found.\r
+  @param  BufferLen              The length of the buffer.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES   Insufficient resources to store necessary structures.\r
+  @retval EFI_SUCCESS            The function finishes successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+FindSameBlockElement(\r
+  IN  EFI_STRING   String,\r
+  IN  EFI_STRING   BlockName,\r
+  IN  UINT8        *Buffer,\r
+  OUT BOOLEAN      *Found,\r
+  IN  UINTN        BufferLen\r
+  )\r
+{\r
+  EFI_STRING   BlockPtr;\r
+  UINTN        Length;\r
+  UINT8        *TempBuffer;\r
+  EFI_STATUS   Status;\r
+\r
+  TempBuffer = NULL;\r
+  *Found = FALSE;\r
+  BlockPtr = StrStr (String, BlockName);\r
+\r
+  while (BlockPtr != NULL) {\r
+    BlockPtr += StrLen (BlockName);\r
+    Status = GetValueOfNumber (BlockPtr, &TempBuffer, &Length);\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+    ASSERT (TempBuffer != NULL);\r
+    if ((BufferLen == Length) && (0 == CompareMem (Buffer, TempBuffer, Length))) {\r
+      *Found = TRUE;\r
+      FreePool (TempBuffer);\r
+      TempBuffer = NULL;\r
+      return EFI_SUCCESS;\r
+    } else {\r
+      FreePool (TempBuffer);\r
+      TempBuffer = NULL;\r
+      BlockPtr = StrStr (BlockPtr + 1, BlockName);\r
+    }\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Compare the <AltResp> in ConfigAltResp and DefaultAltCfgResp, if the <AltResp>\r
+  in DefaultAltCfgResp but not in ConfigAltResp,add it to the ConfigAltResp.\r
+\r
+  @param  DefaultAltCfgResp      Pointer to a null-terminated Unicode string in\r
+                                 <MultiConfigAltResp> format. The default value\r
+                                 string may contain more than one ConfigAltResp\r
+                                 string for the different varstore buffer.\r
+  @param  ConfigAltResp          Pointer to a null-terminated Unicode string in\r
+                                 <ConfigAltResp> format.\r
+  @param  AltConfigHdr           Pointer to a Unicode string in <AltConfigHdr> format.\r
+  @param  ConfigAltRespChanged   Whether the ConfigAltResp has been changed.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES   Insufficient resources to store necessary structures.\r
+  @retval EFI_SUCCESS            The function finishes  successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+CompareBlockElementDefault (\r
+  IN      EFI_STRING  DefaultAltCfgResp,\r
+  IN OUT  EFI_STRING  *ConfigAltResp,\r
+  IN      EFI_STRING  AltConfigHdr,\r
+  IN OUT  BOOLEAN     *ConfigAltRespChanged\r
+)\r
+{\r
+  EFI_STATUS    Status;\r
+  EFI_STRING    BlockPtr;\r
+  EFI_STRING    BlockPtrStart;\r
+  EFI_STRING    StringPtr;\r
+  EFI_STRING    AppendString;\r
+  EFI_STRING    AltConfigHdrPtr;\r
+  UINT8         *TempBuffer;\r
+  UINTN         OffsetLength;\r
+  UINTN         AppendSize;\r
+  UINTN         TotalSize;\r
+  BOOLEAN       FoundOffset;\r
+\r
+  AppendString = NULL;\r
+  TempBuffer   = NULL;\r
+  //\r
+  // Make BlockPtr point to the first <BlockConfig> with AltConfigHdr in DefaultAltCfgResp.\r
+  //\r
+  AltConfigHdrPtr = StrStr (DefaultAltCfgResp, AltConfigHdr);\r
+  ASSERT (AltConfigHdrPtr != NULL);\r
+  BlockPtr = StrStr (AltConfigHdrPtr, L"&OFFSET=");\r
+  //\r
+  // Make StringPtr point to the AltConfigHdr in ConfigAltResp.\r
+  //\r
+  StringPtr = StrStr (*ConfigAltResp, AltConfigHdr);\r
+  ASSERT (StringPtr != NULL);\r
+\r
+  while (BlockPtr != NULL) {\r
+    //\r
+    // Find the "&OFFSET=<Number>" block and get the value of the Number with AltConfigHdr in DefaultAltCfgResp.\r
+    //\r
+    BlockPtrStart = BlockPtr;\r
+    BlockPtr += StrLen (L"&OFFSET=");\r
+    Status = GetValueOfNumber (BlockPtr, &TempBuffer, &OffsetLength);\r
+    if (EFI_ERROR (Status)) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto Exit;\r
+    }\r
+    //\r
+    // To find the same "&OFFSET=<Number>" block in ConfigAltResp.\r
+    //\r
+    Status = FindSameBlockElement (StringPtr, L"&OFFSET=", TempBuffer, &FoundOffset, OffsetLength);\r
+    if (TempBuffer != NULL) {\r
+      FreePool (TempBuffer);\r
+      TempBuffer = NULL;\r
+    }\r
+    if (EFI_ERROR (Status)) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto Exit;\r
+    }\r
+    if (!FoundOffset) {\r
+      //\r
+      // Don't find the same "&OFFSET=<Number>" block in ConfigAltResp.\r
+      // Calculate the size of <BlockConfig>.\r
+      // <BlockConfig>::='OFFSET='<Number>'&WIDTH='<Number>'&VALUE='<Number>.\r
+      //\r
+      BlockPtr = StrStr (BlockPtr + 1, L"&OFFSET=");\r
+      if (BlockPtr != NULL) {\r
+        AppendSize = (BlockPtr - BlockPtrStart) * sizeof (CHAR16);\r
+      } else {\r
+        AppendSize = StrSize (BlockPtrStart);\r
+      }\r
+      //\r
+      // Copy the <BlockConfig> to AppendString.\r
+      //\r
+      if (AppendString == NULL) {\r
+        AppendString = (EFI_STRING) AllocateZeroPool (AppendSize + sizeof (CHAR16));\r
+        StrnCatS (AppendString, AppendSize / sizeof (CHAR16) + 1, BlockPtrStart, AppendSize / sizeof (CHAR16));\r
+      } else {\r
+        TotalSize = StrSize (AppendString) + AppendSize + sizeof (CHAR16);\r
+        AppendString = (EFI_STRING) ReallocatePool (\r
+                                      StrSize (AppendString),\r
+                                      TotalSize,\r
+                                      AppendString\r
+                                      );\r
+        if (AppendString == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          goto Exit;\r
+        }\r
+        StrnCatS (AppendString, TotalSize / sizeof (CHAR16), BlockPtrStart, AppendSize / sizeof (CHAR16));\r
+      }\r
+    } else {\r
+      //\r
+      // To find next "&OFFSET=<Number>" block with AltConfigHdr in DefaultAltCfgResp.\r
+      //\r
+      BlockPtr = StrStr (BlockPtr + 1, L"&OFFSET=");\r
+    }\r
+  }\r
+\r
+  if (AppendString != NULL) {\r
+    //\r
+    // Reallocate ConfigAltResp to copy the AppendString.\r
+    //\r
+    TotalSize = StrSize (*ConfigAltResp) + StrSize (AppendString) + sizeof (CHAR16);\r
+    *ConfigAltResp = (EFI_STRING) ReallocatePool (\r
+                                    StrSize (*ConfigAltResp),\r
+                                    TotalSize,\r
+                                    *ConfigAltResp\r
+                                    );\r
+    if (*ConfigAltResp == NULL) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto Exit;\r
+    }\r
+    StrCatS (*ConfigAltResp, TotalSize / sizeof (CHAR16), AppendString);\r
+    *ConfigAltRespChanged = TRUE;\r
+  }\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+Exit:\r
+  if (AppendString != NULL) {\r
+    FreePool (AppendString);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Compare the <AltResp> in ConfigAltResp and DefaultAltCfgResp, if the <AltResp>\r
+  in DefaultAltCfgResp but not in ConfigAltResp,add it to the ConfigAltResp.\r
+\r
+  @param  DefaultAltCfgResp      Pointer to a null-terminated Unicode string in\r
+                                 <MultiConfigAltResp> format. The default value\r
+                                 string may contain more than one ConfigAltResp\r
+                                 string for the different varstore buffer.\r
+  @param  ConfigAltResp          Pointer to a null-terminated Unicode string in\r
+                                 <ConfigAltResp> format.\r
+  @param  AltConfigHdr           Pointer to a Unicode string in <AltConfigHdr> format.\r
+  @param  ConfigAltRespChanged   Whether the ConfigAltResp has been changed.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES   Insufficient resources to store necessary structures.\r
+  @retval EFI_SUCCESS            The function finishes  successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+CompareNameElementDefault (\r
+  IN     EFI_STRING  DefaultAltCfgResp,\r
+  IN OUT EFI_STRING  *ConfigAltResp,\r
+  IN     EFI_STRING  AltConfigHdr,\r
+  IN OUT BOOLEAN     *ConfigAltRespChanged\r
+)\r
+{\r
+  EFI_STATUS    Status;\r
+  EFI_STRING    NvConfigPtr;\r
+  EFI_STRING    NvConfigStart;\r
+  EFI_STRING    NvConfigValuePtr;\r
+  EFI_STRING    StringPtr;\r
+  EFI_STRING    NvConfigExist;\r
+  EFI_STRING    AppendString;\r
+  CHAR16        TempChar;\r
+  UINTN         AppendSize;\r
+  UINTN         TotalSize;\r
+\r
+  AppendString = NULL;\r
+  NvConfigExist = NULL;\r
+  //\r
+  // Make NvConfigPtr point to the first <NvConfig> with AltConfigHdr in DefaultAltCfgResp.\r
+  //\r
+  NvConfigPtr = StrStr (DefaultAltCfgResp, AltConfigHdr);\r
+  ASSERT (NvConfigPtr != NULL);\r
+  NvConfigPtr = StrStr (NvConfigPtr + StrLen(AltConfigHdr),L"&");\r
+  //\r
+  // Make StringPtr point to the first <NvConfig> with AltConfigHdr in ConfigAltResp.\r
+  //\r
+  StringPtr = StrStr (*ConfigAltResp, AltConfigHdr);\r
+  ASSERT (StringPtr != NULL);\r
+  StringPtr = StrStr (StringPtr + StrLen (AltConfigHdr), L"&");\r
+  ASSERT (StringPtr != NULL);\r
+\r
+  while (NvConfigPtr != NULL) {\r
+    //\r
+    // <NvConfig> ::= <Label>'='<String> | <Label>'='<Number>.\r
+    // Get the <Label> with AltConfigHdr in DefaultAltCfgResp.\r
+    //\r
+    NvConfigStart = NvConfigPtr;\r
+    NvConfigValuePtr = StrStr (NvConfigPtr + 1, L"=");\r
+    ASSERT (NvConfigValuePtr != NULL);\r
+    TempChar = *NvConfigValuePtr;\r
+    *NvConfigValuePtr = L'\0';\r
+    //\r
+    // Get the <Label> with AltConfigHdr in ConfigAltResp.\r
+    //\r
+    NvConfigExist = StrStr (StringPtr, NvConfigPtr);\r
+    if (NvConfigExist == NULL) {\r
+      //\r
+      // Don't find same <Label> in ConfigAltResp.\r
+      // Calculate the size of <NvConfig>.\r
+      //\r
+      *NvConfigValuePtr = TempChar;\r
+      NvConfigPtr = StrStr (NvConfigPtr + 1, L"&");\r
+      if (NvConfigPtr != NULL) {\r
+        AppendSize = (NvConfigPtr - NvConfigStart) * sizeof (CHAR16);\r
+      } else {\r
+        AppendSize = StrSize (NvConfigStart);\r
+      }\r
+      //\r
+      // Copy the <NvConfig> to AppendString.\r
+      //\r
+      if (AppendString == NULL) {\r
+        AppendString = (EFI_STRING) AllocateZeroPool (AppendSize + sizeof (CHAR16));\r
+        StrnCatS (AppendString, AppendSize / sizeof (CHAR16) + 1, NvConfigStart, AppendSize / sizeof (CHAR16));\r
+      } else {\r
+         TotalSize = StrSize (AppendString) + AppendSize + sizeof (CHAR16);\r
+         AppendString = (EFI_STRING) ReallocatePool (\r
+                                       StrSize (AppendString),\r
+                                       TotalSize,\r
+                                       AppendString\r
+                                       );\r
+        if (AppendString == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          goto Exit;\r
+        }\r
+        StrnCatS (AppendString, TotalSize / sizeof (CHAR16), NvConfigStart, AppendSize / sizeof (CHAR16));\r
+      }\r
+    } else {\r
+      //\r
+      // To find next <Label> in DefaultAltCfgResp.\r
+      //\r
+      *NvConfigValuePtr = TempChar;\r
+      NvConfigPtr = StrStr (NvConfigPtr + 1, L"&");\r
+    }\r
+  }\r
+  if (AppendString != NULL) {\r
+    //\r
+    // Reallocate ConfigAltResp to copy the AppendString.\r
+    //\r
+    TotalSize = StrSize (*ConfigAltResp) + StrSize (AppendString) + sizeof (CHAR16);\r
+    *ConfigAltResp = (EFI_STRING) ReallocatePool (\r
+                                    StrSize (*ConfigAltResp),\r
+                                    StrSize (*ConfigAltResp) + StrSize (AppendString) + sizeof (CHAR16),\r
+                                    *ConfigAltResp\r
+                                    );\r
+    if (*ConfigAltResp == NULL) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto Exit;\r
+    }\r
+    StrCatS (*ConfigAltResp, TotalSize / sizeof (CHAR16), AppendString);\r
+    *ConfigAltRespChanged = TRUE;\r
+  }\r
+  Status = EFI_SUCCESS;\r
+\r
+Exit:\r
+  if (AppendString != NULL) {\r
+    FreePool (AppendString);\r
+  }\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Compare the <AltResp> in AltCfgResp and DefaultAltCfgResp, if the <AltResp>\r
+  in DefaultAltCfgResp but not in AltCfgResp,add it to the AltCfgResp.\r
+\r
+  @param  AltCfgResp             Pointer to a null-terminated Unicode string in\r
+                                 <ConfigAltResp> format.\r
+  @param  DefaultAltCfgResp      Pointer to a null-terminated Unicode string in\r
+                                 <MultiConfigAltResp> format. The default value\r
+                                 string may contain more than one ConfigAltResp\r
+                                 string for the different varstore buffer.\r
+  @param  AltConfigHdr           Pointer to a Unicode string in <AltConfigHdr> format.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES   Insufficient resources to store necessary\r
+                                 structures.\r
+  @retval EFI_SUCCESS            The function finishes  successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+CompareAndMergeDefaultString (\r
+  IN OUT EFI_STRING  *AltCfgResp,\r
+  IN     EFI_STRING  DefaultAltCfgResp,\r
+  IN     EFI_STRING  AltConfigHdr\r
+  )\r
+{\r
+  EFI_STATUS     Status;\r
+  EFI_STRING     AltCfgRespBackup;\r
+  EFI_STRING     AltConfigHdrPtr;\r
+  EFI_STRING     AltConfigHdrPtrNext;\r
+  EFI_STRING     ConfigAltResp;\r
+  EFI_STRING     StringPtr;\r
+  EFI_STRING     StringPtrNext;\r
+  EFI_STRING     BlockPtr;\r
+  UINTN          ReallocateSize;\r
+  CHAR16         TempChar;\r
+  CHAR16         TempCharA;\r
+  BOOLEAN        ConfigAltRespChanged;\r
+\r
+  Status = EFI_OUT_OF_RESOURCES;\r
+  BlockPtr             = NULL;\r
+  AltConfigHdrPtrNext  = NULL;\r
+  StringPtrNext        = NULL;\r
+  ConfigAltResp        = NULL;\r
+  AltCfgRespBackup     = NULL;\r
+  TempChar             = L'\0';\r
+  TempCharA            = L'\0';\r
+  ConfigAltRespChanged = FALSE;\r
+\r
+  //\r
+  //To find the <AltResp> with AltConfigHdr in DefaultAltCfgResp, ignore other <AltResp> which follow it.\r
+  //\r
+  AltConfigHdrPtr = StrStr (DefaultAltCfgResp, AltConfigHdr);\r
+  ASSERT (AltConfigHdrPtr != NULL);\r
+  AltConfigHdrPtrNext = StrStr (AltConfigHdrPtr + 1, L"&GUID");\r
+  if (AltConfigHdrPtrNext != NULL) {\r
+    TempChar = *AltConfigHdrPtrNext;\r
+    *AltConfigHdrPtrNext = L'\0';\r
+  }\r
+  //\r
+  // To find the <AltResp> with AltConfigHdr in AltCfgResp, ignore other <AltResp> which follow it.\r
+  //\r
+  StringPtr = StrStr (*AltCfgResp, AltConfigHdr);\r
+  StringPtrNext = StrStr (StringPtr + 1, L"&GUID");\r
+  if (StringPtrNext != NULL) {\r
+    TempCharA = *StringPtrNext;\r
+    *StringPtrNext = L'\0';\r
+  }\r
+  //\r
+  // Copy the content of <ConfigAltResp> which contain current AltConfigHdr in AltCfgResp.\r
+  //\r
+  ConfigAltResp = AllocateCopyPool (StrSize (*AltCfgResp), *AltCfgResp);\r
+  if (ConfigAltResp == NULL) {\r
+    goto Exit;\r
+  }\r
+  //\r
+  // To find the <ConfigBody> with AltConfigHdr in DefaultAltCfgResp.\r
+  //\r
+  BlockPtr = StrStr (AltConfigHdrPtr, L"&OFFSET=");\r
+  if (BlockPtr != NULL) {\r
+    //\r
+    // <BlockConfig>::='OFFSET='<Number>'&WIDTH='<Number>'&VALUE='<Number> style.\r
+    // Call function CompareBlockElementDefault to compare the <BlockConfig> in DefaultAltCfgResp and ConfigAltResp.\r
+    // The ConfigAltResp which may contain the new <BlockConfig> get from DefaultAltCfgResp.\r
+    //\r
+    Status = CompareBlockElementDefault (DefaultAltCfgResp, &ConfigAltResp, AltConfigHdr, &ConfigAltRespChanged);\r
+    if (EFI_ERROR(Status)) {\r
+      goto Exit;\r
+    }\r
+  } else {\r
+    //\r
+    // <NvConfig> ::= <Label>'='<String> | <Label>'='<Number> style.\r
+    // Call function CompareNameElementDefault to compare the <NvConfig> in DefaultAltCfgResp and ConfigAltResp.\r
+    // The ConfigAltResp which may contain the new <NvConfig> get from DefaultAltCfgResp.\r
+    //\r
+    Status = CompareNameElementDefault (DefaultAltCfgResp, &ConfigAltResp, AltConfigHdr, &ConfigAltRespChanged);\r
+    if (EFI_ERROR(Status)) {\r
+      goto Exit;\r
+    }\r
+  }\r
+  //\r
+  // Restore the AltCfgResp.\r
+  //\r
+  if (StringPtrNext != NULL) {\r
+    *StringPtrNext = TempCharA;\r
+  }\r
+\r
+  //\r
+  // If the ConfigAltResp has no change,no need to update the content in AltCfgResp.\r
+  //\r
+  if (!ConfigAltRespChanged) {\r
+    Status = EFI_SUCCESS;\r
+    goto Exit;\r
+  }\r
+  //\r
+  // ConfigAltResp has been changed, need to update the content in AltCfgResp.\r
+  //\r
+  if (StringPtrNext != NULL) {\r
+    ReallocateSize = StrSize (ConfigAltResp) + StrSize (StringPtrNext) + sizeof (CHAR16);\r
+  } else {\r
+    ReallocateSize = StrSize (ConfigAltResp) + sizeof (CHAR16);\r
+  }\r
+\r
+  AltCfgRespBackup = (EFI_STRING) AllocateZeroPool (ReallocateSize);\r
+  if (AltCfgRespBackup == NULL) {\r
+    goto Exit;\r
+  }\r
+\r
+  StrCatS (AltCfgRespBackup, ReallocateSize / sizeof (CHAR16), ConfigAltResp);\r
+  if (StringPtrNext != NULL) {\r
+    StrCatS (AltCfgRespBackup, ReallocateSize / sizeof (CHAR16), StringPtrNext);\r
+  }\r
+\r
+  FreePool (*AltCfgResp);\r
+  *AltCfgResp = AltCfgRespBackup;\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+Exit:\r
+  if (ConfigAltResp != NULL) {\r
+    FreePool(ConfigAltResp);\r
+  }\r
+  //\r
+  // Restore the DefaultAltCfgResp.\r
+  //\r
+  if ( AltConfigHdrPtrNext != NULL) {\r
+    *AltConfigHdrPtrNext = TempChar;\r
+    AltConfigHdrPtrNext = NULL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
 /**\r
   This function merges DefaultAltCfgResp string into AltCfgResp string for\r
   the missing AltCfgId in AltCfgResq.\r
 \r
   @param  AltCfgResp             Pointer to a null-terminated Unicode string in\r
-                                 <ConfigAltResp> format. The default value string \r
-                                 will be merged into it. \r
+                                 <ConfigAltResp> format. The default value string\r
+                                 will be merged into it.\r
   @param  DefaultAltCfgResp      Pointer to a null-terminated Unicode string in\r
-                                 <MultiConfigAltResp> format. The default value \r
+                                 <MultiConfigAltResp> format. The default value\r
                                  string may contain more than one ConfigAltResp\r
                                  string for the different varstore buffer.\r
 \r
@@ -536,17 +1028,19 @@ MergeDefaultString (
   EFI_STRING   AltConfigHdr;\r
   UINTN        HeaderLength;\r
   UINTN        SizeAltCfgResp;\r
-  \r
+  UINTN        MaxLen;\r
+  UINTN        TotalSize;\r
+\r
   if (*AltCfgResp == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   //\r
-  // Get the requestr ConfigHdr\r
+  // Get the request ConfigHdr\r
   //\r
   SizeAltCfgResp  = 0;\r
   StringPtr       = *AltCfgResp;\r
-  \r
+\r
   //\r
   // Find <ConfigHdr> GUID=...&NAME=...&PATH=...\r
   //\r
@@ -572,26 +1066,27 @@ MergeDefaultString (
   // Construct AltConfigHdr string  "&<ConfigHdr>&ALTCFG=XXXX\0"\r
   //                                  |1| StrLen (ConfigHdr) | 8 | 4 | 1 |\r
   //\r
-  AltConfigHdr = AllocateZeroPool ((1 + HeaderLength + 8 + 4 + 1) * sizeof (CHAR16));\r
+  MaxLen = 1 + HeaderLength + 8 + 4 + 1;\r
+  AltConfigHdr = AllocateZeroPool (MaxLen * sizeof (CHAR16));\r
   if (AltConfigHdr == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  StrCpy (AltConfigHdr, L"&");\r
-  StrnCat (AltConfigHdr, *AltCfgResp, HeaderLength);\r
-  StrCat (AltConfigHdr, L"&ALTCFG=");\r
+  StrCpyS (AltConfigHdr, MaxLen, L"&");\r
+  StrnCatS (AltConfigHdr, MaxLen, *AltCfgResp, HeaderLength);\r
+  StrCatS (AltConfigHdr, MaxLen, L"&ALTCFG=");\r
   HeaderLength = StrLen (AltConfigHdr);\r
-  \r
+\r
   StringPtrDefault = StrStr (DefaultAltCfgResp, AltConfigHdr);\r
   while (StringPtrDefault != NULL) {\r
     //\r
     // Get AltCfg Name\r
     //\r
-    StrnCat (AltConfigHdr, StringPtrDefault + HeaderLength, 4);\r
-    StringPtr = StrStr (*AltCfgResp, AltConfigHdr); \r
-    \r
+    StrnCatS (AltConfigHdr, MaxLen, StringPtrDefault + HeaderLength, 4);\r
+    StringPtr = StrStr (*AltCfgResp, AltConfigHdr);\r
+\r
     //\r
     // Append the found default value string to the input AltCfgResp\r
-    // \r
+    //\r
     if (StringPtr == NULL) {\r
       StringPtrEnd   = StrStr (StringPtrDefault + 1, L"&GUID");\r
       SizeAltCfgResp = StrSize (*AltCfgResp);\r
@@ -599,43 +1094,52 @@ MergeDefaultString (
         //\r
         // No more default string is found.\r
         //\r
+        TotalSize = SizeAltCfgResp + StrSize (StringPtrDefault);\r
         *AltCfgResp    = (EFI_STRING) ReallocatePool (\r
                                      SizeAltCfgResp,\r
-                                     SizeAltCfgResp + StrSize (StringPtrDefault),\r
+                                     TotalSize,\r
                                      (VOID *) (*AltCfgResp)\r
                                      );\r
         if (*AltCfgResp == NULL) {\r
           FreePool (AltConfigHdr);\r
           return EFI_OUT_OF_RESOURCES;\r
         }\r
-        StrCat (*AltCfgResp, StringPtrDefault);\r
+        StrCatS (*AltCfgResp, TotalSize / sizeof (CHAR16), StringPtrDefault);\r
         break;\r
       } else {\r
         TempChar = *StringPtrEnd;\r
         *StringPtrEnd = L'\0';\r
+        TotalSize = SizeAltCfgResp + StrSize (StringPtrDefault);\r
         *AltCfgResp = (EFI_STRING) ReallocatePool (\r
                                      SizeAltCfgResp,\r
-                                     SizeAltCfgResp + StrSize (StringPtrDefault),\r
+                                     TotalSize,\r
                                      (VOID *) (*AltCfgResp)\r
                                      );\r
         if (*AltCfgResp == NULL) {\r
           FreePool (AltConfigHdr);\r
           return EFI_OUT_OF_RESOURCES;\r
         }\r
-        StrCat (*AltCfgResp, StringPtrDefault);\r
+        StrCatS (*AltCfgResp, TotalSize / sizeof (CHAR16), StringPtrDefault);\r
         *StringPtrEnd = TempChar;\r
       }\r
+    } else {\r
+      //\r
+      // The AltCfgResp contains <AltCfgResp>.\r
+      // If the <ConfigElement> in <AltCfgResp> in the DefaultAltCfgResp but not in the\r
+      // related <AltCfgResp> in AltCfgResp, merge it to AltCfgResp. else no need to merge.\r
+      //\r
+      CompareAndMergeDefaultString (AltCfgResp, DefaultAltCfgResp, AltConfigHdr);\r
     }\r
-    \r
+\r
     //\r
     // Find next AltCfg String\r
     //\r
     *(AltConfigHdr + HeaderLength) = L'\0';\r
     StringPtrDefault = StrStr (StringPtrDefault + 1, AltConfigHdr);\r
   }\r
-  \r
+\r
   FreePool (AltConfigHdr);\r
-  return EFI_SUCCESS;  \r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -652,9 +1156,9 @@ InsertDefaultValue (
   )\r
 {\r
   LIST_ENTRY             *Link;\r
-  IFR_DEFAULT_DATA       *DefaultValueArray; \r
+  IFR_DEFAULT_DATA       *DefaultValueArray;\r
   LIST_ENTRY             *DefaultLink;\r
\r
+\r
   DefaultLink   = &BlockData->DefaultValueEntry;\r
 \r
   for (Link = DefaultLink->ForwardLink; Link != DefaultLink; Link = Link->ForwardLink) {\r
@@ -662,8 +1166,9 @@ InsertDefaultValue (
     if (DefaultValueArray->DefaultId == DefaultValueData->DefaultId) {\r
       //\r
       // DEFAULT_VALUE_FROM_OPCODE has high priority, DEFAULT_VALUE_FROM_DEFAULT has low priority.\r
+      // When default types are DEFAULT_VALUE_FROM_OTHER_DEFAULT, the default value can be overrode.\r
       //\r
-      if (DefaultValueData->Type > DefaultValueArray->Type) {\r
+      if ((DefaultValueData->Type > DefaultValueArray->Type) || (DefaultValueData->Type == DefaultValueArray->Type && DefaultValueData->Type == DefaultValueFromOtherDefault)) {\r
         //\r
         // Update the default value array in BlockData.\r
         //\r
@@ -672,7 +1177,7 @@ InsertDefaultValue (
         DefaultValueArray->Cleaned = DefaultValueData->Cleaned;\r
       }\r
       return;\r
-    } \r
+    }\r
   }\r
 \r
   //\r
@@ -689,7 +1194,7 @@ InsertDefaultValue (
 \r
   @param  BlockLink      The list entry points to block array.\r
   @param  BlockData      The point to BlockData is added.\r
-  \r
+\r
 **/\r
 VOID\r
 InsertBlockData (\r
@@ -714,7 +1219,7 @@ InsertBlockData (
   for (Link = BlockLink->ForwardLink; Link != BlockLink; Link = Link->ForwardLink) {\r
     BlockArray = BASE_CR (Link, IFR_BLOCK_DATA, Entry);\r
     if (BlockArray->Offset == BlockSingleData->Offset) {\r
-      if (BlockArray->Width > BlockSingleData->Width) {\r
+      if ((BlockArray->Width > BlockSingleData->Width) || (BlockSingleData->IsBitVar && BlockArray->Width == BlockSingleData->Width)) {\r
         //\r
         // Insert this block data in the front of block array\r
         //\r
@@ -722,7 +1227,7 @@ InsertBlockData (
         return;\r
       }\r
 \r
-      if (BlockArray->Width == BlockSingleData->Width) {\r
+      if ((!BlockSingleData->IsBitVar) && BlockArray->Width == BlockSingleData->Width) {\r
         //\r
         // The same block array has been added.\r
         //\r
@@ -734,34 +1239,34 @@ InsertBlockData (
       }\r
     } else if (BlockArray->Offset > BlockSingleData->Offset) {\r
       //\r
-      // Insert new block data in the front of block array \r
+      // Insert new block data in the front of block array\r
       //\r
       InsertTailList (Link, &BlockSingleData->Entry);\r
       return;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Add new block data into the tail.\r
   //\r
-  InsertTailList (Link, &BlockSingleData->Entry); \r
+  InsertTailList (Link, &BlockSingleData->Entry);\r
 }\r
 \r
 /**\r
-  Retrieves a pointer to the a Null-terminated ASCII string containing the list \r
-  of languages that an HII handle in the HII Database supports.  The returned \r
+  Retrieves a pointer to the a Null-terminated ASCII string containing the list\r
+  of languages that an HII handle in the HII Database supports.  The returned\r
   string is allocated using AllocatePool().  The caller is responsible for freeing\r
   the returned string using FreePool().  The format of the returned string follows\r
   the language format assumed the HII Database.\r
-  \r
+\r
   If HiiHandle is NULL, then ASSERT().\r
 \r
   @param[in]  HiiHandle  A handle that was previously registered in the HII Database.\r
 \r
   @retval NULL   HiiHandle is not registered in the HII database\r
-  @retval NULL   There are not enough resources available to retrieve the suported \r
+  @retval NULL   There are not enough resources available to retrieve the supported\r
                  languages.\r
-  @retval NULL   The list of suported languages could not be retrieved.\r
+  @retval NULL   The list of supported languages could not be retrieved.\r
   @retval Other  A pointer to the Null-terminated ASCII string of supported languages.\r
 \r
 **/\r
@@ -784,8 +1289,8 @@ GetSupportedLanguages (
   Status = mPrivate.HiiString.GetLanguages (&mPrivate.HiiString, HiiHandle, &TempSupportedLanguages, &LanguageSize);\r
 \r
   //\r
-  // If GetLanguages() returns EFI_SUCCESS for a zero size, \r
-  // then there are no supported languages registered for HiiHandle.  If GetLanguages() \r
+  // If GetLanguages() returns EFI_SUCCESS for a zero size,\r
+  // then there are no supported languages registered for HiiHandle.  If GetLanguages()\r
   // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present\r
   // in the HII Database\r
   //\r
@@ -827,12 +1332,12 @@ GetSupportedLanguages (
 \r
 /**\r
   Retrieves a string from a string package.\r
-  \r
+\r
   If HiiHandle is NULL, then ASSERT().\r
   If StringId is 0, then ASSET.\r
 \r
   @param[in]  HiiHandle  A handle that was previously registered in the HII Database.\r
-  @param[in]  StringId   The identifier of the string to retrieved from the string \r
+  @param[in]  StringId   The identifier of the string to retrieved from the string\r
                          package associated with HiiHandle.\r
 \r
   @retval NULL   The string specified by StringId is not present in the string package.\r
@@ -859,7 +1364,7 @@ InternalGetString (
 \r
   //\r
   // Initialize all allocated buffers to NULL\r
-  // \r
+  //\r
   SupportedLanguages = NULL;\r
   PlatformLanguage   = NULL;\r
   BestLanguage       = NULL;\r
@@ -883,11 +1388,11 @@ InternalGetString (
   // Get the best matching language from SupportedLanguages\r
   //\r
   BestLanguage = GetBestLanguage (\r
-                   SupportedLanguages, \r
+                   SupportedLanguages,\r
                    FALSE,                                             // RFC 4646 mode\r
-                   Language,                                          // Highest priority \r
+                   Language,                                          // Highest priority\r
                    PlatformLanguage != NULL ? PlatformLanguage : "",  // Next highest priority\r
-                   SupportedLanguages,                                // Lowest priority \r
+                   SupportedLanguages,                                // Lowest priority\r
                    NULL\r
                    );\r
   if (BestLanguage == NULL) {\r
@@ -908,8 +1413,8 @@ InternalGetString (
                          NULL\r
                          );\r
   //\r
-  // If GetString() returns EFI_SUCCESS for a zero size, \r
-  // then there are no supported languages registered for HiiHandle.  If GetString() \r
+  // If GetString() returns EFI_SUCCESS for a zero size,\r
+  // then there are no supported languages registered for HiiHandle.  If GetString()\r
   // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present\r
   // in the HII Database\r
   //\r
@@ -968,12 +1473,12 @@ Error:
 /**\r
   This function checks VarOffset and VarWidth is in the block range.\r
 \r
-  @param  RequestBlockArray  The block array is to be checked. \r
+  @param  RequestBlockArray  The block array is to be checked.\r
   @param  VarOffset          Offset of var to the structure\r
   @param  VarWidth           Width of var.\r
   @param  IsNameValueType    Whether this varstore is name/value varstore or not.\r
   @param  HiiHandle          Hii handle for this hii package.\r
-  \r
+\r
   @retval TRUE   This Var is in the block range.\r
   @retval FALSE  This Var is not in the block range.\r
 **/\r
@@ -1051,18 +1556,18 @@ GetFormPackageData (
   // 0. Get Hii Form Package by HiiHandle\r
   //\r
   Status = ExportFormPackages (\r
-             &mPrivate, \r
-             DataBaseRecord->Handle, \r
-             DataBaseRecord->PackageList, \r
-             0, \r
-             Size, \r
+             &mPrivate,\r
+             DataBaseRecord->Handle,\r
+             DataBaseRecord->PackageList,\r
+             0,\r
+             Size,\r
              HiiFormPackage,\r
              &ResultSize\r
            );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
\r
+\r
   (*HiiFormPackage) = AllocatePool (ResultSize);\r
   if (*HiiFormPackage == NULL) {\r
     Status = EFI_OUT_OF_RESOURCES;\r
@@ -1075,18 +1580,18 @@ GetFormPackageData (
   Size   = ResultSize;\r
   ResultSize    = 0;\r
   Status = ExportFormPackages (\r
-             &mPrivate, \r
-             DataBaseRecord->Handle, \r
-             DataBaseRecord->PackageList, \r
+             &mPrivate,\r
+             DataBaseRecord->Handle,\r
+             DataBaseRecord->PackageList,\r
              0,\r
-             Size, \r
+             Size,\r
              *HiiFormPackage,\r
              &ResultSize\r
            );\r
   if (EFI_ERROR (Status)) {\r
     FreePool (*HiiFormPackage);\r
   }\r
-  \r
+\r
   *PackageSize = Size;\r
 \r
   return Status;\r
@@ -1101,7 +1606,7 @@ GetFormPackageData (
                                 the first found varstore will be as ConfigHdr.\r
   @param  IsEfiVarstore         Whether the request storage type is efi varstore type.\r
   @param  EfiVarStore           The efi varstore info which will return.\r
-**/                                \r
+**/\r
 EFI_STATUS\r
 GetVarStoreType (\r
   IN     HII_DATABASE_RECORD        *DataBaseRecord,\r
@@ -1115,15 +1620,16 @@ GetVarStoreType (
   UINTN                    PackageOffset;\r
   EFI_IFR_OP_HEADER        *IfrOpHdr;\r
   CHAR16                   *VarStoreName;\r
+  UINTN                    NameSize;\r
   EFI_STRING               GuidStr;\r
   EFI_STRING               NameStr;\r
   EFI_STRING               TempStr;\r
-  UINTN                    LengthString;  \r
+  UINTN                    LengthString;\r
   UINT8                    *HiiFormPackage;\r
   UINTN                    PackageSize;\r
   EFI_IFR_VARSTORE_EFI     *IfrEfiVarStore;\r
   EFI_HII_PACKAGE_HEADER   *PackageHeader;\r
-  \r
+\r
   HiiFormPackage = NULL;\r
   LengthString     = 0;\r
   Status           = EFI_SUCCESS;\r
@@ -1161,20 +1667,21 @@ GetVarStoreType (
     if (IfrOpHdr->OpCode == EFI_IFR_VARSTORE_EFI_OP ) {\r
       IfrEfiVarStore = (EFI_IFR_VARSTORE_EFI *) IfrOpHdr;\r
       //\r
-      // If the length is small than the structure, this is from old efi \r
-      // varstore definition. Old efi varstore get config directly from \r
+      // If the length is small than the structure, this is from old efi\r
+      // varstore definition. Old efi varstore get config directly from\r
       // GetVariable function.\r
       //\r
       if (IfrOpHdr->Length < sizeof (EFI_IFR_VARSTORE_EFI)) {\r
         continue;\r
       }\r
 \r
-      VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name) * sizeof (CHAR16));\r
+      NameSize = AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name);\r
+      VarStoreName = AllocateZeroPool (NameSize * sizeof (CHAR16));\r
       if (VarStoreName == NULL) {\r
         Status = EFI_OUT_OF_RESOURCES;\r
         goto Done;\r
       }\r
-      AsciiStrToUnicodeStr ((CHAR8 *) IfrEfiVarStore->Name, VarStoreName);\r
+      AsciiStrToUnicodeStrS ((CHAR8 *) IfrEfiVarStore->Name, VarStoreName, NameSize);\r
 \r
       GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *) &IfrEfiVarStore->Guid, 1, &GuidStr);\r
       GenerateSubStr (L"NAME=", StrLen (VarStoreName) * sizeof (CHAR16), (VOID *) VarStoreName, 2, &NameStr);\r
@@ -1188,8 +1695,8 @@ GetVarStoreType (
         Status = EFI_OUT_OF_RESOURCES;\r
         goto Done;\r
       }\r
-      StrCpy (TempStr, GuidStr);\r
-      StrCat (TempStr, NameStr);\r
+      StrCpyS (TempStr, LengthString, GuidStr);\r
+      StrCatS (TempStr, LengthString, NameStr);\r
       if (ConfigHdr == NULL || StrnCmp (ConfigHdr, TempStr, StrLen (TempStr)) == 0) {\r
         *EfiVarStore = (EFI_IFR_VARSTORE_EFI *) AllocateZeroPool (IfrOpHdr->Length);\r
         if (*EfiVarStore == NULL) {\r
@@ -1202,10 +1709,10 @@ GetVarStoreType (
         }\r
         *IsEfiVarstore = TRUE;\r
         CopyMem (*EfiVarStore, IfrEfiVarStore, IfrOpHdr->Length);\r
-      } \r
-        \r
+      }\r
+\r
       //\r
-      // Free alllocated temp string.\r
+      // Free allocated temp string.\r
       //\r
       FreePool (VarStoreName);\r
       FreePool (GuidStr);\r
@@ -1263,9 +1770,9 @@ GetElementsFromRequest (
   @param  Name              Varstore name.\r
   @param  ConfigHdr         Current configRequest info.\r
 \r
-  @retval  TRUE              This varstore is the requst one.\r
-  @retval  FALSE             This varstore is not the requst one.\r
-                                 \r
+  @retval  TRUE              This varstore is the request one.\r
+  @retval  FALSE             This varstore is not the request one.\r
+\r
 **/\r
 BOOLEAN\r
 IsThisVarstore (\r
@@ -1304,8 +1811,8 @@ IsThisVarstore (
     goto Done;\r
   }\r
 \r
-  StrCpy (TempStr, GuidStr);\r
-  StrCat (TempStr, NameStr);\r
+  StrCpyS (TempStr, LengthString, GuidStr);\r
+  StrCatS (TempStr, LengthString, NameStr);\r
 \r
   if (ConfigHdr == NULL || StrnCmp (ConfigHdr, TempStr, StrLen (TempStr)) == 0) {\r
     RetVal = TRUE;\r
@@ -1313,7 +1820,7 @@ IsThisVarstore (
 \r
 Done:\r
   if (GuidStr != NULL) {\r
-    FreePool (GuidStr); \r
+    FreePool (GuidStr);\r
   }\r
 \r
   if (NameStr != NULL) {\r
@@ -1333,9 +1840,9 @@ Done:
   @param  DataBaseRecord        The DataBaseRecord instance contains the found Hii handle and package.\r
   @param  ConfigHdr             Request string ConfigHdr. If it is NULL,\r
                                 the first found varstore will be as ConfigHdr.\r
-  @retval  TRUE                 This hii package is the reqeust one.\r
-  @retval  FALSE                This hii package is not the reqeust one.\r
-**/                                \r
+  @retval  TRUE                 This hii package is the request one.\r
+  @retval  FALSE                This hii package is not the request one.\r
+**/\r
 BOOLEAN\r
 IsThisPackageList (\r
   IN     HII_DATABASE_RECORD        *DataBaseRecord,\r
@@ -1347,6 +1854,7 @@ IsThisPackageList (
   UINTN                    PackageOffset;\r
   EFI_IFR_OP_HEADER        *IfrOpHdr;\r
   CHAR16                   *VarStoreName;\r
+  UINTN                    NameSize;\r
   UINT8                    *HiiFormPackage;\r
   UINTN                    PackageSize;\r
   EFI_IFR_VARSTORE_EFI     *IfrEfiVarStore;\r
@@ -1387,33 +1895,41 @@ IsThisPackageList (
     PackageOffset += IfrOpHdr->Length;\r
 \r
     switch (IfrOpHdr->OpCode) {\r
-    \r
+\r
     case EFI_IFR_VARSTORE_OP:\r
       IfrVarStore = (EFI_IFR_VARSTORE *) IfrOpHdr;\r
 \r
-      VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrVarStore->Name) * sizeof (CHAR16));\r
+      NameSize = AsciiStrSize ((CHAR8 *)IfrVarStore->Name);\r
+      VarStoreName = AllocateZeroPool (NameSize * sizeof (CHAR16));\r
       if (VarStoreName == NULL) {\r
         goto Done;\r
       }\r
-      AsciiStrToUnicodeStr ((CHAR8 *)IfrVarStore->Name, VarStoreName);\r
+      AsciiStrToUnicodeStrS ((CHAR8 *)IfrVarStore->Name, VarStoreName, NameSize);\r
 \r
       if (IsThisVarstore((VOID *)&IfrVarStore->Guid, VarStoreName, ConfigHdr)) {\r
         FindVarstore = TRUE;\r
         goto Done;\r
+      } else {\r
+        FreePool (VarStoreName);\r
+        VarStoreName = NULL;\r
       }\r
       break;\r
 \r
     case EFI_IFR_VARSTORE_EFI_OP:\r
       IfrEfiVarStore = (EFI_IFR_VARSTORE_EFI *) IfrOpHdr;\r
-      VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name) * sizeof (CHAR16));\r
+      NameSize = AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name);\r
+      VarStoreName = AllocateZeroPool (NameSize * sizeof (CHAR16));\r
       if (VarStoreName == NULL) {\r
         goto Done;\r
       }\r
-      AsciiStrToUnicodeStr ((CHAR8 *)IfrEfiVarStore->Name, VarStoreName);\r
+      AsciiStrToUnicodeStrS ((CHAR8 *)IfrEfiVarStore->Name, VarStoreName, NameSize);\r
 \r
       if (IsThisVarstore (&IfrEfiVarStore->Guid, VarStoreName, ConfigHdr)) {\r
         FindVarstore = TRUE;\r
         goto Done;\r
+      } else {\r
+        FreePool (VarStoreName);\r
+        VarStoreName = NULL;\r
       }\r
       break;\r
 \r
@@ -1425,7 +1941,7 @@ IsThisPackageList (
         goto Done;\r
       }\r
       break;\r
-      \r
+\r
     case EFI_IFR_FORM_OP:\r
     case EFI_IFR_FORM_MAP_OP:\r
       //\r
@@ -1454,14 +1970,16 @@ Done:
 \r
   @param  RequestBlockArray      The array includes all the request info or NULL.\r
   @param  HiiHandle              The hii handle for this form package.\r
-  @param  VarStorageData         The varstore data strucure.\r
+  @param  VarStorageData         The varstore data structure.\r
   @param  IfrOpHdr               Ifr opcode header for this opcode.\r
   @param  VarWidth               The buffer width for this opcode.\r
   @param  ReturnData             The data block added for this opcode.\r
+  @param  IsBitVar               Whether the the opcode refers to bit storage.\r
 \r
   @retval  EFI_SUCCESS           This opcode is required.\r
-  @retval  Others                This opcode is not required or error occur.\r
-                                 \r
+  @retval  EFI_NOT_FOUND         This opcode is not required.\r
+  @retval  Others                Contain some error.\r
+\r
 **/\r
 EFI_STATUS\r
 IsThisOpcodeRequired (\r
@@ -1470,16 +1988,22 @@ IsThisOpcodeRequired (
   IN OUT IFR_VARSTORAGE_DATA      *VarStorageData,\r
   IN     EFI_IFR_OP_HEADER        *IfrOpHdr,\r
   IN     UINT16                   VarWidth,\r
-  OUT    IFR_BLOCK_DATA           **ReturnData\r
+  OUT    IFR_BLOCK_DATA           **ReturnData,\r
+  IN     BOOLEAN                  IsBitVar\r
   )\r
 {\r
   IFR_BLOCK_DATA           *BlockData;\r
   UINT16                   VarOffset;\r
   EFI_STRING_ID            NameId;\r
   EFI_IFR_QUESTION_HEADER  *IfrQuestionHdr;\r
+  UINT16                   BitOffset;\r
+  UINT16                   BitWidth;\r
+  UINT16                   TotalBits;\r
 \r
   NameId    = 0;\r
   VarOffset = 0;\r
+  BitOffset = 0;\r
+  BitWidth = 0;\r
   IfrQuestionHdr = (EFI_IFR_QUESTION_HEADER  *)((CHAR8 *) IfrOpHdr + sizeof (EFI_IFR_OP_HEADER));\r
 \r
   if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {\r
@@ -1492,11 +2016,27 @@ IsThisOpcodeRequired (
       //\r
       // This question is not in the requested string. Skip it.\r
       //\r
-      return EFI_SUCCESS;\r
+      return EFI_NOT_FOUND;\r
     }\r
   } else {\r
-    VarOffset = IfrQuestionHdr->VarStoreInfo.VarOffset;\r
-    \r
+    //\r
+    // Get the byte offset/with and bit offset/width\r
+    //\r
+    if (IsBitVar) {\r
+      BitOffset = IfrQuestionHdr->VarStoreInfo.VarOffset;\r
+      BitWidth = VarWidth;\r
+      VarOffset = BitOffset / 8;\r
+      //\r
+      // Use current bit width and the bit width before current bit (with same byte offset) to calculate the byte width.\r
+      //\r
+      TotalBits = BitOffset % 8 + BitWidth;\r
+      VarWidth = (TotalBits % 8 == 0 ? TotalBits / 8: TotalBits / 8 + 1);\r
+    } else {\r
+      VarOffset = IfrQuestionHdr->VarStoreInfo.VarOffset;\r
+      BitWidth = VarWidth;\r
+      BitOffset = VarOffset * 8;\r
+    }\r
+\r
     //\r
     // Check whether this question is in requested block array.\r
     //\r
@@ -1504,11 +2044,11 @@ IsThisOpcodeRequired (
       //\r
       // This question is not in the requested string. Skip it.\r
       //\r
-      return EFI_SUCCESS;\r
+      return EFI_NOT_FOUND;\r
     }\r
 \r
     //\r
-    // Check this var question is in the var storage \r
+    // Check this var question is in the var storage\r
     //\r
     if (((VarOffset + VarWidth) > VarStorageData->Size)) {\r
       return EFI_INVALID_PARAMETER;\r
@@ -1530,6 +2070,9 @@ IsThisOpcodeRequired (
   BlockData->QuestionId = IfrQuestionHdr->QuestionId;\r
   BlockData->OpCode     = IfrOpHdr->OpCode;\r
   BlockData->Scope      = IfrOpHdr->Scope;\r
+  BlockData->IsBitVar   = IsBitVar;\r
+  BlockData->BitOffset  = BitOffset;\r
+  BlockData->BitWidth   = BitWidth;\r
   InitializeListHead (&BlockData->DefaultValueEntry);\r
   //\r
   // Add Block Data into VarStorageData BlockEntry\r
@@ -1546,7 +2089,7 @@ IsThisOpcodeRequired (
 \r
   @param  HiiHandle             Hii Handle for this hii package.\r
   @param  Package               Pointer to the form package data.\r
-  @param  PackageLength         Length of the pacakge.\r
+  @param  PackageLength         Length of the package.\r
   @param  ConfigHdr             Request string ConfigHdr. If it is NULL,\r
                                 the first found varstore will be as ConfigHdr.\r
   @param  RequestBlockArray     The block array is retrieved from the request string.\r
@@ -1554,8 +2097,8 @@ IsThisOpcodeRequired (
   @param  DefaultIdArray        Point to the got default id and default name array.\r
 \r
   @retval EFI_SUCCESS           The block array and the default value array are got.\r
-  @retval EFI_INVALID_PARAMETER The varstore defintion in the differnt form pacakges\r
-                                are conflicted. \r
+  @retval EFI_INVALID_PARAMETER The varstore definition in the different form packages\r
+                                are conflicted.\r
   @retval EFI_OUT_OF_RESOURCES  No enough memory.\r
 **/\r
 EFI_STATUS\r
@@ -1590,21 +2133,32 @@ ParseIfrData (
   IFR_DEFAULT_DATA         *DefaultDataPtr;\r
   IFR_BLOCK_DATA           *BlockData;\r
   CHAR16                   *VarStoreName;\r
+  UINTN                    NameSize;\r
   UINT16                   VarWidth;\r
   UINT16                   VarDefaultId;\r
   BOOLEAN                  FirstOneOfOption;\r
+  BOOLEAN                  FirstOrderedList;\r
   LIST_ENTRY               *LinkData;\r
   LIST_ENTRY               *LinkDefault;\r
   EFI_IFR_VARSTORE_NAME_VALUE *IfrNameValueVarStore;\r
   EFI_HII_PACKAGE_HEADER   *PackageHeader;\r
   EFI_VARSTORE_ID          VarStoreId;\r
+  UINT16                   SmallestDefaultId;\r
+  BOOLEAN                  SmallestIdFromFlag;\r
+  BOOLEAN                  FromOtherDefaultOpcode;\r
+  BOOLEAN                  QuestionReferBitField;\r
 \r
   Status           = EFI_SUCCESS;\r
   BlockData        = NULL;\r
   DefaultDataPtr   = NULL;\r
   FirstOneOfOption = FALSE;\r
   VarStoreId       = 0;\r
+  FirstOrderedList = FALSE;\r
+  VarStoreName     = NULL;\r
   ZeroMem (&DefaultData, sizeof (IFR_DEFAULT_DATA));\r
+  SmallestDefaultId = 0xFFFF;\r
+  FromOtherDefaultOpcode = FALSE;\r
+  QuestionReferBitField = FALSE;\r
 \r
   //\r
   // Go through the form package to parse OpCode one by one.\r
@@ -1645,12 +2199,13 @@ ParseIfrData (
 \r
       IfrVarStore = (EFI_IFR_VARSTORE *) IfrOpHdr;\r
 \r
-      VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrVarStore->Name) * sizeof (CHAR16));\r
+      NameSize = AsciiStrSize ((CHAR8 *)IfrVarStore->Name);\r
+      VarStoreName = AllocateZeroPool (NameSize * sizeof (CHAR16));\r
       if (VarStoreName == NULL) {\r
         Status = EFI_OUT_OF_RESOURCES;\r
         goto Done;\r
       }\r
-      AsciiStrToUnicodeStr ((CHAR8 *)IfrVarStore->Name, VarStoreName);\r
+      AsciiStrToUnicodeStrS ((CHAR8 *)IfrVarStore->Name, VarStoreName, NameSize);\r
 \r
       if (IsThisVarstore((VOID *)&IfrVarStore->Guid, VarStoreName, ConfigHdr)) {\r
         //\r
@@ -1661,6 +2216,9 @@ ParseIfrData (
         VarStorageData->Name       = VarStoreName;\r
         VarStorageData->Type       = EFI_HII_VARSTORE_BUFFER;\r
         VarStoreId                 = IfrVarStore->VarStoreId;\r
+      } else {\r
+        FreePool (VarStoreName);\r
+        VarStoreName = NULL;\r
       }\r
       break;\r
 \r
@@ -1675,20 +2233,21 @@ ParseIfrData (
       IfrEfiVarStore = (EFI_IFR_VARSTORE_EFI *) IfrOpHdr;\r
 \r
       //\r
-      // If the length is small than the structure, this is from old efi \r
-      // varstore definition. Old efi varstore get config directly from \r
+      // If the length is small than the structure, this is from old efi\r
+      // varstore definition. Old efi varstore get config directly from\r
       // GetVariable function.\r
-      //      \r
+      //\r
       if (IfrOpHdr->Length < sizeof (EFI_IFR_VARSTORE_EFI)) {\r
         break;\r
       }\r
 \r
-      VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name) * sizeof (CHAR16));\r
+      NameSize = AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name);\r
+      VarStoreName = AllocateZeroPool (NameSize * sizeof (CHAR16));\r
       if (VarStoreName == NULL) {\r
         Status = EFI_OUT_OF_RESOURCES;\r
         goto Done;\r
       }\r
-      AsciiStrToUnicodeStr ((CHAR8 *)IfrEfiVarStore->Name, VarStoreName);\r
+      AsciiStrToUnicodeStrS ((CHAR8 *)IfrEfiVarStore->Name, VarStoreName, NameSize);\r
 \r
       if (IsThisVarstore (&IfrEfiVarStore->Guid, VarStoreName, ConfigHdr)) {\r
         //\r
@@ -1699,6 +2258,9 @@ ParseIfrData (
         VarStorageData->Name       = VarStoreName;\r
         VarStorageData->Type       = EFI_HII_VARSTORE_EFI_VARIABLE_BUFFER;\r
         VarStoreId                 = IfrEfiVarStore->VarStoreId;\r
+      } else {\r
+        FreePool (VarStoreName);\r
+        VarStoreName = NULL;\r
       }\r
       break;\r
 \r
@@ -1749,7 +2311,7 @@ ParseIfrData (
 \r
     case EFI_IFR_REF_OP:\r
       //\r
-      // Ref question is not in IFR Form. This IFR form is not valid. \r
+      // Ref question is not in IFR Form. This IFR form is not valid.\r
       //\r
       if ( VarStoreId == 0) {\r
         Status = EFI_INVALID_PARAMETER;\r
@@ -1764,8 +2326,21 @@ ParseIfrData (
       }\r
       VarWidth  = (UINT16) (sizeof (EFI_HII_REF));\r
 \r
-      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);\r
+      //\r
+      // The BlockData may allocate by other opcode,need to clean.\r
+      //\r
+      if (BlockData != NULL){\r
+        BlockData = NULL;\r
+      }\r
+\r
+      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData, FALSE);\r
       if (EFI_ERROR (Status)) {\r
+        if (Status == EFI_NOT_FOUND){\r
+          //\r
+          //The opcode is not required,exit and parse other opcode.\r
+          //\r
+          break;\r
+        }\r
         goto Done;\r
       }\r
       break;\r
@@ -1777,7 +2352,7 @@ ParseIfrData (
       //\r
 \r
       //\r
-      // Numeric and OneOf question is not in IFR Form. This IFR form is not valid. \r
+      // Numeric and OneOf question is not in IFR Form. This IFR form is not valid.\r
       //\r
       if (VarStoreId == 0) {\r
         Status = EFI_INVALID_PARAMETER;\r
@@ -1790,19 +2365,35 @@ ParseIfrData (
       if (IfrOneOf->Question.VarStoreId != VarStoreId) {\r
         break;\r
       }\r
-      VarWidth  = (UINT16) (1 << (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE));\r
 \r
-      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);\r
+      if (QuestionReferBitField) {\r
+        VarWidth = IfrOneOf->Flags & EDKII_IFR_NUMERIC_SIZE_BIT;\r
+      } else {\r
+        VarWidth  = (UINT16) (1 << (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE));\r
+      }\r
+\r
+      //\r
+      // The BlockData may allocate by other opcode,need to clean.\r
+      //\r
+      if (BlockData != NULL){\r
+        BlockData = NULL;\r
+      }\r
+\r
+      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData, QuestionReferBitField);\r
       if (EFI_ERROR (Status)) {\r
+        if (Status == EFI_NOT_FOUND){\r
+          //\r
+          //The opcode is not required,exit and parse other opcode.\r
+          //\r
+          break;\r
+        }\r
         goto Done;\r
       }\r
 \r
-      if (BlockData == NULL) {\r
-        //\r
-        // BlockData == NULL means this opcode is not in the requst array.\r
-        //\r
-        break;\r
-      }\r
+      //\r
+      //when go to there,BlockData can't be NULLL.\r
+      //\r
+      ASSERT (BlockData != NULL);\r
 \r
       if (IfrOpHdr->OpCode == EFI_IFR_ONE_OF_OP) {\r
         //\r
@@ -1811,33 +2402,40 @@ ParseIfrData (
         FirstOneOfOption = TRUE;\r
       } else if (IfrOpHdr->OpCode == EFI_IFR_NUMERIC_OP) {\r
         //\r
-        // Numeric minimum value will be used as default value when no default is specified. \r
+        // Numeric minimum value will be used as default value when no default is specified.\r
         //\r
         DefaultData.Type        = DefaultValueFromDefault;\r
-        switch (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE) {\r
-        case EFI_IFR_NUMERIC_SIZE_1:\r
-          DefaultData.Value.u8 = IfrOneOf->data.u8.MinValue;\r
-          break;\r
+        if (QuestionReferBitField) {\r
+          //\r
+          // Since default value in bit field was stored as UINT32 type.\r
+          //\r
+          CopyMem (&DefaultData.Value.u32, &IfrOneOf->data.u32.MinValue, sizeof (UINT32));\r
+        } else {\r
+          switch (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE) {\r
+          case EFI_IFR_NUMERIC_SIZE_1:\r
+            DefaultData.Value.u8 = IfrOneOf->data.u8.MinValue;\r
+            break;\r
 \r
-        case EFI_IFR_NUMERIC_SIZE_2:\r
-          CopyMem (&DefaultData.Value.u16, &IfrOneOf->data.u16.MinValue, sizeof (UINT16));\r
-          break;\r
+          case EFI_IFR_NUMERIC_SIZE_2:\r
+           CopyMem (&DefaultData.Value.u16, &IfrOneOf->data.u16.MinValue, sizeof (UINT16));\r
+           break;\r
 \r
-        case EFI_IFR_NUMERIC_SIZE_4:\r
-          CopyMem (&DefaultData.Value.u32, &IfrOneOf->data.u32.MinValue, sizeof (UINT32));\r
-          break;\r
+          case EFI_IFR_NUMERIC_SIZE_4:\r
+            CopyMem (&DefaultData.Value.u32, &IfrOneOf->data.u32.MinValue, sizeof (UINT32));\r
+            break;\r
 \r
-        case EFI_IFR_NUMERIC_SIZE_8:\r
-          CopyMem (&DefaultData.Value.u64, &IfrOneOf->data.u64.MinValue, sizeof (UINT64));\r
-          break;\r
+          case EFI_IFR_NUMERIC_SIZE_8:\r
+            CopyMem (&DefaultData.Value.u64, &IfrOneOf->data.u64.MinValue, sizeof (UINT64));\r
+            break;\r
 \r
-        default:\r
-          Status = EFI_INVALID_PARAMETER;\r
-          goto Done;\r
+          default:\r
+            Status = EFI_INVALID_PARAMETER;\r
+            goto Done;\r
+         }\r
         }\r
         //\r
         // Set default value base on the DefaultId list get from IFR data.\r
-        //        \r
+        //\r
         for (LinkData = DefaultIdArray->Entry.ForwardLink; LinkData != &DefaultIdArray->Entry; LinkData = LinkData->ForwardLink) {\r
           DefaultDataPtr = BASE_CR (LinkData, IFR_DEFAULT_DATA, Entry);\r
           DefaultData.DefaultId   = DefaultDataPtr->DefaultId;\r
@@ -1850,11 +2448,11 @@ ParseIfrData (
       //\r
       // offset by question header\r
       // width by EFI_IFR_ORDERED_LIST MaxContainers * OneofOption Type\r
-      // no default value and default id, how to define its default value?\r
       //\r
 \r
+      FirstOrderedList = TRUE;\r
       //\r
-      // OrderedList question is not in IFR Form. This IFR form is not valid. \r
+      // OrderedList question is not in IFR Form. This IFR form is not valid.\r
       //\r
       if (VarStoreId == 0) {\r
         Status = EFI_INVALID_PARAMETER;\r
@@ -1869,8 +2467,22 @@ ParseIfrData (
         break;\r
       }\r
       VarWidth  = IfrOrderedList->MaxContainers;\r
-      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);\r
+\r
+      //\r
+      // The BlockData may allocate by other opcode,need to clean.\r
+      //\r
+      if (BlockData != NULL){\r
+        BlockData = NULL;\r
+      }\r
+\r
+      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData, FALSE);\r
       if (EFI_ERROR (Status)) {\r
+        if (Status == EFI_NOT_FOUND){\r
+          //\r
+          //The opcode is not required,exit and parse other opcode.\r
+          //\r
+          break;\r
+        }\r
         goto Done;\r
       }\r
       break;\r
@@ -1883,10 +2495,10 @@ ParseIfrData (
       // default id by CheckBox Flags if CheckBox flags (Default or Mau) is set, the default value is 1 to be set.\r
       // value by DefaultOption\r
       // default id by DeaultOption DefaultId can override CheckBox Flags and Default value.\r
-      // \r
+      //\r
 \r
       //\r
-      // CheckBox question is not in IFR Form. This IFR form is not valid. \r
+      // CheckBox question is not in IFR Form. This IFR form is not valid.\r
       //\r
       if (VarStoreId == 0) {\r
         Status = EFI_INVALID_PARAMETER;\r
@@ -1900,17 +2512,34 @@ ParseIfrData (
         break;\r
       }\r
       VarWidth  = (UINT16) sizeof (BOOLEAN);\r
-      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);\r
+\r
+      //\r
+      // The BlockData may allocate by other opcode,need to clean.\r
+      //\r
+      if (BlockData != NULL){\r
+        BlockData = NULL;\r
+      }\r
+\r
+      if (QuestionReferBitField) {\r
+        VarWidth = 1;\r
+      }\r
+      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData, QuestionReferBitField);\r
       if (EFI_ERROR (Status)) {\r
+        if (Status == EFI_NOT_FOUND){\r
+          //\r
+          //The opcode is not required,exit and parse other opcode.\r
+          //\r
+          break;\r
+        }\r
         goto Done;\r
       }\r
 \r
-      if (BlockData == NULL) {\r
-        //\r
-        // BlockData == NULL means this opcode is not in the requst array.\r
-        //\r
-        break;\r
-      }\r
+      //\r
+      //when go to there,BlockData can't be NULLL.\r
+      //\r
+      ASSERT (BlockData != NULL);\r
+\r
+      SmallestIdFromFlag = FALSE;\r
 \r
       //\r
       // Add default value for standard ID by CheckBox Flag\r
@@ -1922,21 +2551,24 @@ ParseIfrData (
       DefaultData.DefaultId   = VarDefaultId;\r
       if ((IfrCheckBox->Flags & EFI_IFR_CHECKBOX_DEFAULT) == EFI_IFR_CHECKBOX_DEFAULT) {\r
         //\r
-        // When flag is set, defautl value is TRUE.\r
+        // When flag is set, default value is TRUE.\r
         //\r
         DefaultData.Type    = DefaultValueFromFlag;\r
-        DefaultData.Value.b = TRUE;\r
-      } else {\r
-        //\r
-        // When flag is not set, defautl value is FASLE.\r
-        //\r
-        DefaultData.Type    = DefaultValueFromDefault;\r
-        DefaultData.Value.b = FALSE;\r
+        if (QuestionReferBitField) {\r
+          DefaultData.Value.u32 = TRUE;\r
+        } else {\r
+          DefaultData.Value.b = TRUE;\r
+        }\r
+        InsertDefaultValue (BlockData, &DefaultData);\r
+\r
+        if (SmallestDefaultId > EFI_HII_DEFAULT_CLASS_STANDARD) {\r
+          //\r
+          // Record the SmallestDefaultId and update the SmallestIdFromFlag.\r
+          //\r
+          SmallestDefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;\r
+          SmallestIdFromFlag = TRUE;\r
+        }\r
       }\r
-      //\r
-      // Add DefaultValue into current BlockData\r
-      //\r
-      InsertDefaultValue (BlockData, &DefaultData);\r
 \r
       //\r
       // Add default value for Manufacture ID by CheckBox Flag\r
@@ -1948,21 +2580,61 @@ ParseIfrData (
       DefaultData.DefaultId   = VarDefaultId;\r
       if ((IfrCheckBox->Flags & EFI_IFR_CHECKBOX_DEFAULT_MFG) == EFI_IFR_CHECKBOX_DEFAULT_MFG) {\r
         //\r
-        // When flag is set, defautl value is TRUE.\r
+        // When flag is set, default value is TRUE.\r
         //\r
         DefaultData.Type    = DefaultValueFromFlag;\r
-        DefaultData.Value.b = TRUE;\r
+        if (QuestionReferBitField) {\r
+          DefaultData.Value.u32 = TRUE;\r
+        } else {\r
+          DefaultData.Value.b = TRUE;\r
+        }\r
+        InsertDefaultValue (BlockData, &DefaultData);\r
+\r
+        if (SmallestDefaultId > EFI_HII_DEFAULT_CLASS_MANUFACTURING) {\r
+          //\r
+          // Record the SmallestDefaultId and update the SmallestIdFromFlag.\r
+          //\r
+          SmallestDefaultId = EFI_HII_DEFAULT_CLASS_MANUFACTURING;\r
+          SmallestIdFromFlag = TRUE;\r
+        }\r
+      }\r
+      if (SmallestIdFromFlag) {\r
+        //\r
+        // When smallest default Id is given by the  flag of CheckBox, set default value with TRUE for other default Id in the DefaultId list.\r
+        //\r
+        DefaultData.Type    = DefaultValueFromOtherDefault;\r
+        if (QuestionReferBitField) {\r
+          DefaultData.Value.u32 = TRUE;\r
+        } else {\r
+          DefaultData.Value.b = TRUE;\r
+        }\r
+        //\r
+        // Set default value for all the default id in the DefaultId list.\r
+        //\r
+        for (LinkData = DefaultIdArray->Entry.ForwardLink; LinkData != &DefaultIdArray->Entry; LinkData = LinkData->ForwardLink) {\r
+          DefaultDataPtr = BASE_CR (LinkData, IFR_DEFAULT_DATA, Entry);\r
+          DefaultData.DefaultId   = DefaultDataPtr->DefaultId;\r
+          InsertDefaultValue (BlockData, &DefaultData);\r
+        }\r
       } else {\r
         //\r
-        // When flag is not set, defautl value is FASLE.\r
+        // When flag is not set, default value is FASLE.\r
         //\r
         DefaultData.Type    = DefaultValueFromDefault;\r
-        DefaultData.Value.b = FALSE;\r
+        if (QuestionReferBitField) {\r
+          DefaultData.Value.u32 = FALSE;\r
+        } else {\r
+          DefaultData.Value.b = FALSE;\r
+        }\r
+        //\r
+        // Set default value for all the default id in the DefaultId list.\r
+        //\r
+        for (LinkData = DefaultIdArray->Entry.ForwardLink; LinkData != &DefaultIdArray->Entry; LinkData = LinkData->ForwardLink) {\r
+          DefaultDataPtr = BASE_CR (LinkData, IFR_DEFAULT_DATA, Entry);\r
+          DefaultData.DefaultId   = DefaultDataPtr->DefaultId;\r
+          InsertDefaultValue (BlockData, &DefaultData);\r
+        }\r
       }\r
-      //\r
-      // Add DefaultValue into current BlockData\r
-      //\r
-      InsertDefaultValue (BlockData, &DefaultData);\r
       break;\r
 \r
     case EFI_IFR_DATE_OP:\r
@@ -1973,7 +2645,7 @@ ParseIfrData (
       //\r
 \r
       //\r
-      // Date question is not in IFR Form. This IFR form is not valid. \r
+      // Date question is not in IFR Form. This IFR form is not valid.\r
       //\r
       if (VarStoreId == 0) {\r
         Status = EFI_INVALID_PARAMETER;\r
@@ -1987,9 +2659,22 @@ ParseIfrData (
         break;\r
       }\r
 \r
+      //\r
+      // The BlockData may allocate by other opcode,need to clean.\r
+      //\r
+      if (BlockData != NULL){\r
+        BlockData = NULL;\r
+      }\r
+\r
       VarWidth  = (UINT16) sizeof (EFI_HII_DATE);\r
-      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);\r
+      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData, FALSE);\r
       if (EFI_ERROR (Status)) {\r
+        if (Status == EFI_NOT_FOUND){\r
+          //\r
+          //The opcode is not required,exit and parse other opcode.\r
+          //\r
+          break;\r
+        }\r
         goto Done;\r
       }\r
       break;\r
@@ -2002,7 +2687,7 @@ ParseIfrData (
       //\r
 \r
       //\r
-      // Time question is not in IFR Form. This IFR form is not valid. \r
+      // Time question is not in IFR Form. This IFR form is not valid.\r
       //\r
       if (VarStoreId == 0) {\r
         Status = EFI_INVALID_PARAMETER;\r
@@ -2016,9 +2701,22 @@ ParseIfrData (
         break;\r
       }\r
 \r
+      //\r
+      // The BlockData may allocate by other opcode,need to clean.\r
+      //\r
+      if (BlockData != NULL){\r
+        BlockData = NULL;\r
+      }\r
+\r
       VarWidth  = (UINT16) sizeof (EFI_HII_TIME);\r
-      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);\r
+      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData, FALSE);\r
       if (EFI_ERROR (Status)) {\r
+        if (Status == EFI_NOT_FOUND){\r
+          //\r
+          //The opcode is not required,exit and parse other opcode.\r
+          //\r
+          break;\r
+        }\r
         goto Done;\r
       }\r
       break;\r
@@ -2031,7 +2729,7 @@ ParseIfrData (
       //\r
 \r
       //\r
-      // String question is not in IFR Form. This IFR form is not valid. \r
+      // String question is not in IFR Form. This IFR form is not valid.\r
       //\r
       if (VarStoreId == 0) {\r
         Status = EFI_INVALID_PARAMETER;\r
@@ -2045,16 +2743,24 @@ ParseIfrData (
         break;\r
       }\r
 \r
+      //\r
+      // The BlockData may allocate by other opcode,need to clean.\r
+      //\r
+      if (BlockData != NULL){\r
+        BlockData = NULL;\r
+      }\r
+\r
       VarWidth  = (UINT16) (IfrString->MaxSize * sizeof (UINT16));\r
-      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);\r
+      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData, FALSE);\r
       if (EFI_ERROR (Status)) {\r
+        if (Status == EFI_NOT_FOUND){\r
+          //\r
+          //The opcode is not required,exit and parse other opcode.\r
+          //\r
+          break;\r
+        }\r
         goto Done;\r
       }\r
-\r
-      //\r
-      // No default value for string.\r
-      //\r
-      BlockData = NULL;\r
       break;\r
 \r
     case EFI_IFR_PASSWORD_OP:\r
@@ -2065,7 +2771,7 @@ ParseIfrData (
       //\r
 \r
       //\r
-      // Password question is not in IFR Form. This IFR form is not valid. \r
+      // Password question is not in IFR Form. This IFR form is not valid.\r
       //\r
       if (VarStoreId == 0) {\r
         Status = EFI_INVALID_PARAMETER;\r
@@ -2079,9 +2785,22 @@ ParseIfrData (
         break;\r
       }\r
 \r
+      //\r
+      // The BlockData may allocate by other opcode,need to clean.\r
+      //\r
+      if (BlockData != NULL){\r
+        BlockData = NULL;\r
+      }\r
+\r
       VarWidth  = (UINT16) (IfrPassword->MaxSize * sizeof (UINT16));\r
-      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);\r
+      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData, FALSE);\r
       if (EFI_ERROR (Status)) {\r
+        if (Status == EFI_NOT_FOUND){\r
+          //\r
+          //The opcode is not required,exit and parse other opcode.\r
+          //\r
+          break;\r
+        }\r
         goto Done;\r
       }\r
 \r
@@ -2101,6 +2820,10 @@ ParseIfrData (
 \r
       IfrOneOfOption = (EFI_IFR_ONE_OF_OPTION *) IfrOpHdr;\r
       if (BlockData->OpCode == EFI_IFR_ORDERED_LIST_OP) {\r
+\r
+        if (!FirstOrderedList){\r
+          break;\r
+        }\r
         //\r
         // Get ordered list option data type.\r
         //\r
@@ -2143,7 +2866,7 @@ ParseIfrData (
           break;\r
         }\r
         //\r
-        // Check this var question is in the var storage \r
+        // Check this var question is in the var storage\r
         //\r
         if ((BlockData->Name == NULL) && ((BlockData->Offset + BlockData->Width) > VarStorageData->Size)) {\r
           Status = EFI_INVALID_PARAMETER;\r
@@ -2157,24 +2880,26 @@ ParseIfrData (
         // Add Block Data into VarStorageData BlockEntry\r
         //\r
         InsertBlockData (&VarStorageData->BlockEntry, &BlockData);\r
-        //\r
-        // No default data for OrderedList.\r
-        //\r
-        BlockData = NULL;\r
+\r
+        FirstOrderedList = FALSE;\r
+\r
         break;\r
       }\r
 \r
       //\r
       // 1. Set default value for OneOf option when flag field has default attribute.\r
+      //    And set the default value with the smallest default id for other default id in the DefaultId list.\r
       //\r
       if (((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT) == EFI_IFR_OPTION_DEFAULT) ||\r
           ((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT_MFG) == EFI_IFR_OPTION_DEFAULT_MFG)) {\r
         //\r
-        // This flag is used to specify whether this option is the first. Set it to FALSE for the following options. \r
-        // The first oneof option value will be used as default value when no default value is specified. \r
+        // This flag is used to specify whether this option is the first. Set it to FALSE for the following options.\r
+        // The first oneof option value will be used as default value when no default value is specified.\r
         //\r
         FirstOneOfOption = FALSE;\r
-        \r
+\r
+        SmallestIdFromFlag = FALSE;\r
+\r
         // Prepare new DefaultValue\r
         //\r
         DefaultData.Type     = DefaultValueFromFlag;\r
@@ -2182,28 +2907,57 @@ ParseIfrData (
         if ((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT) == EFI_IFR_OPTION_DEFAULT) {\r
           DefaultData.DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;\r
           InsertDefaultValue (BlockData, &DefaultData);\r
-        } \r
+          if (SmallestDefaultId > EFI_HII_DEFAULT_CLASS_STANDARD) {\r
+            //\r
+            // Record the SmallestDefaultId and update the SmallestIdFromFlag.\r
+            //\r
+            SmallestDefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;\r
+            SmallestIdFromFlag = TRUE;\r
+          }\r
+        }\r
         if ((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT_MFG) == EFI_IFR_OPTION_DEFAULT_MFG) {\r
           DefaultData.DefaultId = EFI_HII_DEFAULT_CLASS_MANUFACTURING;\r
           InsertDefaultValue (BlockData, &DefaultData);\r
+          if (SmallestDefaultId > EFI_HII_DEFAULT_CLASS_MANUFACTURING) {\r
+            //\r
+            // Record the SmallestDefaultId and update the SmallestIdFromFlag.\r
+            //\r
+            SmallestDefaultId = EFI_HII_DEFAULT_CLASS_MANUFACTURING;\r
+            SmallestIdFromFlag = TRUE;\r
+          }\r
+        }\r
+\r
+        if (SmallestIdFromFlag) {\r
+          //\r
+          // When smallest default Id is given by the flag of oneofOption, set this option value for other default Id in the DefaultId list.\r
+          //\r
+          DefaultData.Type = DefaultValueFromOtherDefault;\r
+          //\r
+          // Set default value for other default id in the DefaultId list.\r
+          //\r
+          for (LinkData = DefaultIdArray->Entry.ForwardLink; LinkData != &DefaultIdArray->Entry; LinkData = LinkData->ForwardLink) {\r
+            DefaultDataPtr = BASE_CR (LinkData, IFR_DEFAULT_DATA, Entry);\r
+            DefaultData.DefaultId   = DefaultDataPtr->DefaultId;\r
+            InsertDefaultValue (BlockData, &DefaultData);\r
+          }\r
         }\r
       }\r
 \r
       //\r
       // 2. Set as the default value when this is the first option.\r
-      // The first oneof option value will be used as default value when no default value is specified. \r
+      // The first oneof option value will be used as default value when no default value is specified.\r
       //\r
       if (FirstOneOfOption) {\r
-        // This flag is used to specify whether this option is the first. Set it to FALSE for the following options. \r
+        // This flag is used to specify whether this option is the first. Set it to FALSE for the following options.\r
         FirstOneOfOption = FALSE;\r
-        \r
+\r
         //\r
         // Prepare new DefaultValue\r
-        //        \r
+        //\r
         DefaultData.Type     = DefaultValueFromDefault;\r
         CopyMem (&DefaultData.Value, &IfrOneOfOption->Value, IfrOneOfOption->Header.Length - OFFSET_OF (EFI_IFR_ONE_OF_OPTION, Value));\r
         for (LinkData = DefaultIdArray->Entry.ForwardLink; LinkData != &DefaultIdArray->Entry; LinkData = LinkData->ForwardLink) {\r
-          DefaultDataPtr = BASE_CR (LinkData, IFR_DEFAULT_DATA, Entry); \r
+          DefaultDataPtr = BASE_CR (LinkData, IFR_DEFAULT_DATA, Entry);\r
           DefaultData.DefaultId   = DefaultDataPtr->DefaultId;\r
           InsertDefaultValue (BlockData, &DefaultData);\r
         }\r
@@ -2221,12 +2975,6 @@ ParseIfrData (
         break;\r
       }\r
 \r
-      if (BlockData->OpCode == EFI_IFR_ORDERED_LIST_OP) {\r
-        //\r
-        // OrderedList Opcode is no default value.\r
-        //\r
-        break;\r
-      }\r
       //\r
       // Get the DefaultId\r
       //\r
@@ -2237,7 +2985,11 @@ ParseIfrData (
       //\r
       DefaultData.Type        = DefaultValueFromOpcode;\r
       DefaultData.DefaultId   = VarDefaultId;\r
-      CopyMem (&DefaultData.Value, &IfrDefault->Value, IfrDefault->Header.Length - OFFSET_OF (EFI_IFR_DEFAULT, Value));\r
+      if (QuestionReferBitField) {\r
+        CopyMem (&DefaultData.Value.u32, &IfrDefault->Value.u32, sizeof (UINT32));\r
+      } else {\r
+        CopyMem (&DefaultData.Value, &IfrDefault->Value, IfrDefault->Header.Length - OFFSET_OF (EFI_IFR_DEFAULT, Value));\r
+      }\r
 \r
       // If the value field is expression, set the cleaned flag.\r
       if (IfrDefault->Type ==  EFI_IFR_TYPE_OTHER) {\r
@@ -2249,8 +3001,27 @@ ParseIfrData (
       InsertDefaultValue (BlockData, &DefaultData);\r
 \r
       //\r
-      // After insert the default value, reset the cleaned value for next \r
-      // time used. If not set here, need to set the value before everytime \r
+      // Set default value for other default id in the DefaultId list.\r
+      // when SmallestDefaultId == VarDefaultId means there are two defaults with same default Id.\r
+      // If the two defaults are both from default opcode, use the first default as the default value of other default Id.\r
+      // If one from flag and the other form default opcode, use the default opcode value as the default value of other default Id.\r
+      //\r
+      if ((SmallestDefaultId > VarDefaultId) || (SmallestDefaultId == VarDefaultId && !FromOtherDefaultOpcode)) {\r
+        FromOtherDefaultOpcode = TRUE;\r
+        SmallestDefaultId = VarDefaultId;\r
+        for (LinkData = DefaultIdArray->Entry.ForwardLink; LinkData != &DefaultIdArray->Entry; LinkData = LinkData->ForwardLink) {\r
+          DefaultDataPtr = BASE_CR (LinkData, IFR_DEFAULT_DATA, Entry);\r
+          if (DefaultDataPtr->DefaultId != DefaultData.DefaultId){\r
+            DefaultData.Type        = DefaultValueFromOtherDefault;\r
+            DefaultData.DefaultId   = DefaultDataPtr->DefaultId;\r
+            InsertDefaultValue (BlockData, &DefaultData);\r
+          }\r
+        }\r
+      }\r
+\r
+      //\r
+      // After insert the default value, reset the cleaned value for next\r
+      // time used. If not set here, need to set the value before every time.\r
       // use it.\r
       //\r
       DefaultData.Cleaned     = FALSE;\r
@@ -2260,17 +3031,29 @@ ParseIfrData (
       //\r
       // End Opcode is for Var question.\r
       //\r
+      QuestionReferBitField = FALSE;\r
       if (BlockData != NULL) {\r
         if (BlockData->Scope > 0) {\r
           BlockData->Scope--;\r
         }\r
         if (BlockData->Scope == 0) {\r
           BlockData = NULL;\r
+          //\r
+          // when finishing parsing a question, clean the SmallestDefaultId and GetDefaultFromDefaultOpcode.\r
+          //\r
+          SmallestDefaultId = 0xFFFF;\r
+          FromOtherDefaultOpcode = FALSE;\r
         }\r
       }\r
 \r
       break;\r
 \r
+    case EFI_IFR_GUID_OP:\r
+      if (CompareGuid ((EFI_GUID *)((UINT8 *)IfrOpHdr + sizeof (EFI_IFR_OP_HEADER)), &gEdkiiIfrBitVarstoreGuid)) {\r
+        QuestionReferBitField = TRUE;\r
+      }\r
+      break;\r
+\r
     default:\r
       if (BlockData != NULL) {\r
         if (BlockData->Scope > 0) {\r
@@ -2288,6 +3071,14 @@ ParseIfrData (
     PackageOffset += IfrOpHdr->Length;\r
   }\r
 \r
+  //\r
+  //if Status == EFI_NOT_FOUND, just means the opcode is not required,not contain any error,\r
+  //so set the Status to EFI_SUCCESS.\r
+  //\r
+  if (Status == EFI_NOT_FOUND){\r
+    Status = EFI_SUCCESS;\r
+  }\r
+\r
 Done:\r
   for (LinkData = VarStorageData->BlockEntry.ForwardLink; LinkData != &VarStorageData->BlockEntry; LinkData = LinkData->ForwardLink) {\r
     BlockData = BASE_CR (LinkData, IFR_BLOCK_DATA, Entry);\r
@@ -2329,6 +3120,8 @@ GetBlockElement (
   IFR_BLOCK_DATA       *NextBlockData;\r
   UINTN                Length;\r
 \r
+  TmpBuffer = NULL;\r
+\r
   //\r
   // Init RequestBlockArray\r
   //\r
@@ -2395,7 +3188,7 @@ GetBlockElement (
     if (*StringPtr != 0 && *StringPtr != L'&') {\r
       goto Done;\r
     }\r
-    \r
+\r
     //\r
     // Set Block Data\r
     //\r
@@ -2420,14 +3213,14 @@ GetBlockElement (
       if (EFI_ERROR (Status)) {\r
         goto Done;\r
       }\r
-\r
+      FreePool (TmpBuffer);\r
       StringPtr += Length;\r
       if (*StringPtr != 0 && *StringPtr != L'&') {\r
         goto Done;\r
       }\r
     }\r
     //\r
-    // If '\0', parsing is finished. \r
+    // If '\0', parsing is finished.\r
     //\r
     if (*StringPtr == 0) {\r
       break;\r
@@ -2544,7 +3337,7 @@ GetNameElement (
     if (HasValue) {\r
       //\r
       // If has value, skip the value.\r
-      //    \r
+      //\r
       StringPtr = NextTag + 1;\r
       *NextTag  = L'=';\r
       StringPtr = StrStr (StringPtr, L"&");\r
@@ -2609,14 +3402,14 @@ GenerateConfigRequest (
   // Append VarStorageData BlockEntry into *Request string\r
   // Now support only one varstore in a form package.\r
   //\r
-  \r
+\r
   //\r
   // Go through all VarStorageData Entry and get BlockEntry for each one for the multiple varstore in a single form package\r
   // Then construct them all to return MultiRequest string : ConfigHdr BlockConfig\r
   //\r
-  \r
+\r
   //\r
-  // Compute the length of the entire request starting with <ConfigHdr> and a \r
+  // Compute the length of the entire request starting with <ConfigHdr> and a\r
   // Null-terminator\r
   //\r
   DataExist = FALSE;\r
@@ -2629,7 +3422,7 @@ GenerateConfigRequest (
       //\r
       // Add <BlockName> length for each Name\r
       //\r
-      // <BlockName> ::= &Name1&Name2&... \r
+      // <BlockName> ::= &Name1&Name2&...\r
       //                 |1| StrLen(Name1)\r
       //\r
       Length = Length + (1 + StrLen (BlockData->Name));\r
@@ -2664,7 +3457,7 @@ GenerateConfigRequest (
   //\r
   // Start with <ConfigHdr>\r
   //\r
-  StrCpy (StringPtr, ConfigHdr);\r
+  StrCpyS (StringPtr, Length, ConfigHdr);\r
   StringPtr += StrLen (StringPtr);\r
 \r
   //\r
@@ -2687,10 +3480,10 @@ GenerateConfigRequest (
       // Append &OFFSET=XXXX&WIDTH=YYYY\0\r
       //\r
       UnicodeSPrint (\r
-        StringPtr, \r
-        (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16), \r
-        L"&OFFSET=%04X&WIDTH=%04X", \r
-        BlockData->Offset, \r
+        StringPtr,\r
+        (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16),\r
+        L"&OFFSET=%04X&WIDTH=%04X",\r
+        BlockData->Offset,\r
         BlockData->Width\r
       );\r
     }\r
@@ -2763,12 +3556,12 @@ GenerateHdr (
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Done;\r
   }\r
-  StrCpy (*ConfigHdr, GuidStr);\r
-  StrCat (*ConfigHdr, NameStr);\r
+  StrCpyS (*ConfigHdr, Length, GuidStr);\r
+  StrCatS (*ConfigHdr, Length, NameStr);\r
   if (VarStorageData->Name == NULL) {\r
-    StrCat (*ConfigHdr, L"&");\r
+    StrCatS (*ConfigHdr, Length, L"&");\r
   }\r
-  StrCat (*ConfigHdr, PathStr);\r
+  StrCatS (*ConfigHdr, Length, PathStr);\r
 \r
   //\r
   // Remove the last character L'&'\r
@@ -2791,57 +3584,217 @@ Done:
   return Status;\r
 }\r
 \r
+\r
 /**\r
-  Get Data buffer size based on data type.\r
+  Update the default value in the block data which is used as bit var store.\r
 \r
-  @param      ValueType             The input data type.\r
+  For example:\r
+  A question value saved in a bit fied: bitoffset = 1; bitwidth = 2;default value = 1.\r
+  And corresponding block data info: offset==0; width==1;currently the default value\r
+  is saved as 1.Actually the default value 1 need to be set to bit field 1, so the\r
+  default value of this block data shuold be:2.\r
+\r
+  typedef struct {\r
+    UINT8  Bit1 : 1; //\r
+    UINT8  Bit2 : 2; // Question saved in Bit2,so originalBlock info: offset = 0; width = 1;(byte level) defaul = 1.\r
+                     // (default value record for the bit field)\r
+    ......\r
+  }ExampleData;\r
+\r
+  After function UpdateDefaultValue,the Block info is: offset = 0; width = 1;(byte level) default = 2.\r
+                                                       (default value record for the Block)\r
+\r
+  UpdateDefaultValue function update default value of bit var block based on the bit field info in the block.\r
+\r
+  @param  BlockLink     The Link of the block data.\r
 \r
-  @retval     The data buffer size for the input type.\r
 **/\r
-UINT16\r
-GetStorageWidth (\r
-  IN UINT8       ValueType\r
-  )\r
+VOID\r
+UpdateDefaultValue (\r
+  IN LIST_ENTRY        *BlockLink\r
+)\r
 {\r
-  UINT16         StorageWidth;\r
+  LIST_ENTRY          *Link;\r
+  LIST_ENTRY          *ListEntry;\r
+  LIST_ENTRY          *LinkDefault;\r
+  IFR_BLOCK_DATA      *BlockData;\r
+  IFR_DEFAULT_DATA    *DefaultValueData;\r
+  UINTN               StartBit;\r
+  UINTN               EndBit;\r
+  UINT32              BitFieldDefaultValue;\r
 \r
-  switch (ValueType) {\r
-  case EFI_IFR_NUMERIC_SIZE_1:\r
-  case EFI_IFR_TYPE_BOOLEAN:\r
-    StorageWidth = (UINT16) sizeof (UINT8);\r
-    break;\r
+  for ( Link = BlockLink->ForwardLink; Link != BlockLink; Link = Link->ForwardLink) {\r
+    BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);\r
+    if (!BlockData ->IsBitVar) {\r
+      continue;\r
+    }\r
+    ListEntry  = &BlockData->DefaultValueEntry;\r
+    //\r
+    // Update the default value in the block data with all existing default id.\r
+    //\r
+    for (LinkDefault = ListEntry->ForwardLink; LinkDefault != ListEntry; LinkDefault = LinkDefault->ForwardLink) {\r
+      //\r
+      // Get the default data, and the value of the default data is for some field in the block.\r
+      // Note: Default value for bit field question is stored as UINT32.\r
+      //\r
+      DefaultValueData = BASE_CR (LinkDefault, IFR_DEFAULT_DATA, Entry);\r
+      BitFieldDefaultValue = DefaultValueData->Value.u32;\r
 \r
-  case EFI_IFR_NUMERIC_SIZE_2:\r
-    StorageWidth = (UINT16) sizeof (UINT16);\r
-    break;\r
+      StartBit = BlockData->BitOffset % 8;\r
+      EndBit = StartBit + BlockData->BitWidth - 1;\r
 \r
-  case EFI_IFR_NUMERIC_SIZE_4:\r
-    StorageWidth = (UINT16) sizeof (UINT32);\r
-    break;\r
+      //\r
+      // Set the bit field default value to related bit filed, then we will got the new default vaule for the block data.\r
+      //\r
+      DefaultValueData->Value.u32 = BitFieldWrite32 (0, StartBit, EndBit, BitFieldDefaultValue);\r
+    }\r
+  }\r
+}\r
 \r
-  case EFI_IFR_NUMERIC_SIZE_8:\r
-    StorageWidth = (UINT16) sizeof (UINT64);\r
-    break;\r
+/**\r
+Merge the default value in two block datas which have overlap region.\r
 \r
-  case EFI_IFR_TYPE_TIME:\r
-    StorageWidth = (UINT16) sizeof (EFI_IFR_TIME);\r
-    break;\r
+For bit fields, their related block data may have overlap region, such as:\r
 \r
-  case EFI_IFR_TYPE_DATE:\r
-    StorageWidth = (UINT16) sizeof (EFI_IFR_DATE);\r
-    break;\r
+typedef struct {\r
+  UINT16  Bit1 : 6;  // Question1 refer Bit1, Block1: offset = 0; width = 1;(byte level) default = 1\r
+  UINT16  Bit2 : 5;  // Question2 refer Bit2, Block2: offset = 0; width = 2;(byte level) default = 5\r
+                     // (default value record for the bit field)\r
+  ......\r
+}ExampleData;\r
 \r
-  default:\r
-    StorageWidth = 0;\r
-    break;\r
+After function UpdateDefaultValue:\r
+Block1: offset = 0; width = 1;(byte level) default = 1\r
+Block2: offset = 0; width = 2;(byte level) default = 320 (5 * (2 << 6))\r
+(default value record for block)\r
+\r
+After function MergeBlockDefaultValue:\r
+Block1: offset = 0; width = 1;(byte level) default = 65\r
+Block2: offset = 0; width = 2;(byte level) default = 321\r
+(Block1 and Block2 has overlap region, merge the overlap value to Block1 and Blcok2)\r
+\r
+Block1 and Block2 have overlap byte region, but currntly the default value of Block1 only contains\r
+value of Bit1 (low 6 bits),the default value of Block2 only contains the value of Bit2 (middle 5 bits).\r
+\r
+This fuction merge the default value of these two blocks, and make the default value of block1\r
+also contain the value of lower 2 bits of the Bit2. And make the default value of Block2 also\r
+contain the default value of Bit1.\r
+\r
+We can get the total value of the whole block that just cover these two blocks(in this case is:\r
+block: offset =0; width =2;) then the value of block2 is same as block, the value of block1 is\r
+the first byte value of block.\r
+\r
+@param  FirstBlock     Point to the block date whose default value need to be merged.\r
+@param  SecondBlock    Point to the block date whose default value need to be merged.\r
+\r
+**/\r
+VOID\r
+MergeBlockDefaultValue (\r
+  IN OUT IFR_BLOCK_DATA      *FirstBlock,\r
+  IN OUT IFR_BLOCK_DATA      *SecondBlock\r
+)\r
+{\r
+  LIST_ENTRY          *FirstListEntry;\r
+  LIST_ENTRY          *SecondListEntry;\r
+  LIST_ENTRY          *FirstDefaultLink;\r
+  LIST_ENTRY          *SecondDefaultLink;\r
+  IFR_DEFAULT_DATA    *FirstDefaultValueData;\r
+  IFR_DEFAULT_DATA    *SecondDefaultValueData;\r
+  UINT32              *FirstDefaultValue;\r
+  UINT32              *SecondDefaultValue;\r
+  UINT64              TotalValue;\r
+  UINT64              ShiftedValue;\r
+  UINT16              OffsetShift;\r
+\r
+  FirstListEntry = &FirstBlock->DefaultValueEntry;\r
+  for (FirstDefaultLink = FirstListEntry->ForwardLink; FirstDefaultLink != FirstListEntry; FirstDefaultLink = FirstDefaultLink->ForwardLink) {\r
+    FirstDefaultValueData = BASE_CR (FirstDefaultLink, IFR_DEFAULT_DATA, Entry);\r
+    SecondListEntry = &SecondBlock->DefaultValueEntry;\r
+    for (SecondDefaultLink = SecondListEntry->ForwardLink; SecondDefaultLink != SecondListEntry; SecondDefaultLink = SecondDefaultLink->ForwardLink) {\r
+      SecondDefaultValueData = BASE_CR (SecondDefaultLink, IFR_DEFAULT_DATA, Entry);\r
+      if (FirstDefaultValueData->DefaultId != SecondDefaultValueData->DefaultId) {\r
+        continue;\r
+      }\r
+      //\r
+      // Find default value with same default id in the two blocks.\r
+      // Note: Default value for bit field question is stored as UINT32 type.\r
+      //\r
+      FirstDefaultValue = &FirstDefaultValueData->Value.u32;\r
+      SecondDefaultValue = &SecondDefaultValueData->Value.u32;\r
+      //\r
+      // 1. Get the default value of the whole blcok that can just cover FirstBlock and SecondBlock.\r
+      // 2. Get the default value of FirstBlock and SecondBlock form the value of whole block based\r
+      //    on the offset and width of FirstBlock and SecondBlock.\r
+      //\r
+      if (FirstBlock->Offset > SecondBlock->Offset) {\r
+        OffsetShift = FirstBlock->Offset - SecondBlock->Offset;\r
+        ShiftedValue = LShiftU64 ((UINT64) (*FirstDefaultValue), OffsetShift * 8);\r
+        TotalValue = ShiftedValue | (UINT64) (*SecondDefaultValue);\r
+        *SecondDefaultValue = (UINT32) BitFieldRead64 (TotalValue, 0, SecondBlock->Width * 8 -1);\r
+        *FirstDefaultValue = (UINT32) BitFieldRead64 (TotalValue, OffsetShift * 8, OffsetShift * 8 + FirstBlock->Width *8 -1);\r
+      } else {\r
+        OffsetShift = SecondBlock->Offset -FirstBlock->Offset;\r
+        ShiftedValue = LShiftU64 ((UINT64) (*SecondDefaultValue), OffsetShift * 8);\r
+        TotalValue = ShiftedValue | (UINT64) (*FirstDefaultValue);\r
+        *FirstDefaultValue = (UINT32) BitFieldRead64 (TotalValue, 0, FirstBlock->Width * 8 -1);\r
+        *SecondDefaultValue = (UINT32) BitFieldRead64 (TotalValue, OffsetShift * 8, OffsetShift * 8 + SecondBlock->Width *8 -1);\r
+      }\r
+    }\r
   }\r
+}\r
+\r
+/**\r
+\r
+Update the default value in the block data which used as Bit VarStore\r
+\r
+@param  BlockLink     The Link of the block data.\r
+\r
+**/\r
+VOID\r
+UpdateBlockDataArray (\r
+  IN LIST_ENTRY        *BlockLink\r
+)\r
+{\r
+  LIST_ENTRY          *Link;\r
+  LIST_ENTRY          *TempLink;\r
+  IFR_BLOCK_DATA      *BlockData;\r
+  IFR_BLOCK_DATA      *NextBlockData;\r
+\r
+  //\r
+  // 1. Update default value in BitVar block data.\r
+  // Sine some block datas are used as BitVarStore, then the default value recored in the block\r
+  // is for related bit field in the block. so we need to set the default value to the related bit\r
+  // fields in the block data if the block data is used as bit varstore, then the default value of\r
+  // the block will be updated.\r
+  //\r
+  UpdateDefaultValue (BlockLink);\r
 \r
-  return StorageWidth;\r
+  //\r
+  // 2.Update default value for overlap BitVar blcok datas.\r
+  // For block datas have overlap region, we need to merge the default value in different blocks.\r
+  //\r
+  for (Link = BlockLink->ForwardLink; Link != BlockLink; Link = Link->ForwardLink) {\r
+    BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);\r
+    if (!BlockData ->IsBitVar) {\r
+      continue;\r
+    }\r
+    for (TempLink = Link->ForwardLink; TempLink != BlockLink; TempLink = TempLink->ForwardLink) {\r
+      NextBlockData = BASE_CR (TempLink, IFR_BLOCK_DATA, Entry);\r
+      if (!NextBlockData->IsBitVar || NextBlockData->Offset >= BlockData->Offset + BlockData->Width || BlockData->Offset >= NextBlockData->Offset + NextBlockData->Width) {\r
+        continue;\r
+      }\r
+      //\r
+      // Find two blocks are used as bit VarStore and have overlap region, so need to merge default value of these two blocks.\r
+      //\r
+      MergeBlockDefaultValue (BlockData, NextBlockData);\r
+    }\r
+  }\r
 }\r
 \r
 /**\r
   Generate ConfigAltResp string base on the varstore info.\r
 \r
+  @param      HiiHandle             Hii Handle for this hii package.\r
   @param      ConfigHdr             The config header for this varstore.\r
   @param      VarStorageData        The varstore info.\r
   @param      DefaultIdArray        The Default id array.\r
@@ -2852,6 +3805,7 @@ GetStorageWidth (
 **/\r
 EFI_STATUS\r
 GenerateAltConfigResp (\r
+  IN  EFI_HII_HANDLE               HiiHandle,\r
   IN  CHAR16                       *ConfigHdr,\r
   IN  IFR_VARSTORAGE_DATA          *VarStorageData,\r
   IN  IFR_DEFAULT_DATA             *DefaultIdArray,\r
@@ -2870,15 +3824,19 @@ GenerateAltConfigResp (
   IFR_DEFAULT_DATA      *DefaultValueData;\r
   UINTN                 Width;\r
   UINT8                 *TmpBuffer;\r
+  CHAR16                *DefaultString;\r
+  UINTN                 StrSize;\r
 \r
   BlockData     = NULL;\r
   DataExist     = FALSE;\r
-\r
+  DefaultString = NULL;\r
   //\r
   // Add length for <ConfigHdr> + '\0'\r
   //\r
   Length = StrLen (ConfigHdr) + 1;\r
 \r
+  UpdateBlockDataArray (&VarStorageData->BlockEntry);\r
+\r
   for (Link = DefaultIdArray->Entry.ForwardLink; Link != &DefaultIdArray->Entry; Link = Link->ForwardLink) {\r
     DefaultId = BASE_CR (Link, IFR_DEFAULT_DATA, Entry);\r
     //\r
@@ -2886,7 +3844,7 @@ GenerateAltConfigResp (
     //                |1| StrLen (ConfigHdr) | 8 | 4 |\r
     //\r
     Length += (1 + StrLen (ConfigHdr) + 8 + 4);\r
-    \r
+\r
     for (LinkData = VarStorageData->BlockEntry.ForwardLink; LinkData != &VarStorageData->BlockEntry; LinkData = LinkData->ForwardLink) {\r
       BlockData = BASE_CR (LinkData, IFR_BLOCK_DATA, Entry);\r
       ListEntry     = &BlockData->DefaultValueEntry;\r
@@ -2912,7 +3870,7 @@ GenerateAltConfigResp (
       }\r
     }\r
   }\r
-  \r
+\r
   //\r
   // No default value is found. The default string doesn't exist.\r
   //\r
@@ -2932,7 +3890,7 @@ GenerateAltConfigResp (
   //\r
   // Start with <ConfigHdr>\r
   //\r
-  StrCpy (StringPtr, ConfigHdr);\r
+  StrCpyS (StringPtr, Length, ConfigHdr);\r
   StringPtr += StrLen (StringPtr);\r
 \r
   for (Link = DefaultIdArray->Entry.ForwardLink; Link != &DefaultIdArray->Entry; Link = Link->ForwardLink) {\r
@@ -2942,10 +3900,10 @@ GenerateAltConfigResp (
     //                                |1| StrLen (ConfigHdr) | 8 | 4 |\r
     //\r
     UnicodeSPrint (\r
-      StringPtr, \r
-      (1 + StrLen (ConfigHdr) + 8 + 4 + 1) * sizeof (CHAR16), \r
-      L"&%s&ALTCFG=%04X", \r
-      ConfigHdr, \r
+      StringPtr,\r
+      (1 + StrLen (ConfigHdr) + 8 + 4 + 1) * sizeof (CHAR16),\r
+      L"&%s&ALTCFG=%04X",\r
+      ConfigHdr,\r
       DefaultId->DefaultId\r
       );\r
     StringPtr += StrLen (StringPtr);\r
@@ -2960,9 +3918,9 @@ GenerateAltConfigResp (
         }\r
         if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {\r
           UnicodeSPrint (\r
-            StringPtr, \r
-            (1 + StrLen (ConfigHdr) + 1) * sizeof (CHAR16), \r
-            L"&%s=", \r
+            StringPtr,\r
+            (1 + StrLen (ConfigHdr) + 1) * sizeof (CHAR16),\r
+            L"&%s=",\r
             BlockData->Name\r
             );\r
           StringPtr += StrLen (StringPtr);\r
@@ -2972,10 +3930,10 @@ GenerateAltConfigResp (
           // <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE'=<Number>\r
           //\r
           UnicodeSPrint (\r
-            StringPtr, \r
+            StringPtr,\r
             (8 + 4 + 7 + 4 + 7 + 1) * sizeof (CHAR16),\r
-            L"&OFFSET=%04X&WIDTH=%04X&VALUE=", \r
-            BlockData->Offset, \r
+            L"&OFFSET=%04X&WIDTH=%04X&VALUE=",\r
+            BlockData->Offset,\r
             BlockData->Width\r
             );\r
           StringPtr += StrLen (StringPtr);\r
@@ -2985,9 +3943,37 @@ GenerateAltConfigResp (
         // Convert Value to a hex string in "%x" format\r
         // NOTE: This is in the opposite byte that GUID and PATH use\r
         //\r
-        TmpBuffer = (UINT8 *) &(DefaultValueData->Value);\r
-        for (; Width > 0; Width--) {\r
-          StringPtr += UnicodeValueToString (StringPtr, PREFIX_ZERO | RADIX_HEX, TmpBuffer[Width - 1], 2);\r
+        if (BlockData->OpCode == EFI_IFR_STRING_OP){\r
+          DefaultString   = InternalGetString(HiiHandle, DefaultValueData->Value.string);\r
+          TmpBuffer = AllocateZeroPool (Width);\r
+          ASSERT (TmpBuffer != NULL);\r
+          if (DefaultString != NULL) {\r
+            StrSize = StrLen(DefaultString)* sizeof (CHAR16);\r
+            if (StrSize > Width) {\r
+              StrSize = Width;\r
+            }\r
+            CopyMem (TmpBuffer, (UINT8 *) DefaultString, StrSize);\r
+          }\r
+        } else {\r
+          TmpBuffer = (UINT8 *) &(DefaultValueData->Value);\r
+        }\r
+        for (; Width > 0 && (TmpBuffer != NULL); Width--) {\r
+          UnicodeValueToStringS (\r
+            StringPtr,\r
+            Length * sizeof (CHAR16) - ((UINTN)StringPtr - (UINTN)*DefaultAltCfgResp),\r
+            PREFIX_ZERO | RADIX_HEX,\r
+            TmpBuffer[Width - 1],\r
+            2\r
+            );\r
+          StringPtr += StrnLenS (StringPtr, Length - ((UINTN)StringPtr - (UINTN)*DefaultAltCfgResp) / sizeof (CHAR16));\r
+        }\r
+        if (DefaultString != NULL){\r
+          FreePool(DefaultString);\r
+          DefaultString = NULL;\r
+        }\r
+        if (BlockData->OpCode == EFI_IFR_STRING_OP && TmpBuffer != NULL) {\r
+          FreePool(TmpBuffer);\r
+          TmpBuffer  = NULL;\r
         }\r
       }\r
     }\r
@@ -2999,32 +3985,32 @@ GenerateAltConfigResp (
 }\r
 \r
 /**\r
-  This function gets the full request string and full default value string by \r
-  parsing IFR data in HII form packages. \r
-  \r
-  When Request points to NULL string, the request string and default value string \r
-  for each varstore in form package will return. \r
+  This function gets the full request string and full default value string by\r
+  parsing IFR data in HII form packages.\r
+\r
+  When Request points to NULL string, the request string and default value string\r
+  for each varstore in form package will return.\r
 \r
   @param  DataBaseRecord         The DataBaseRecord instance contains the found Hii handle and package.\r
   @param  DevicePath             Device Path which Hii Config Access Protocol is registered.\r
   @param  Request                Pointer to a null-terminated Unicode string in\r
                                  <ConfigRequest> format. When it doesn't contain\r
-                                 any RequestElement, it will be updated to return \r
+                                 any RequestElement, it will be updated to return\r
                                  the full RequestElement retrieved from IFR data.\r
                                  If it points to NULL, the request string for the first\r
                                  varstore in form package will be merged into a\r
-                                 <MultiConfigRequest> format string and return. \r
+                                 <MultiConfigRequest> format string and return.\r
   @param  AltCfgResp             Pointer to a null-terminated Unicode string in\r
                                  <ConfigAltResp> format. When the pointer is to NULL,\r
                                  the full default value string retrieved from IFR data\r
                                  will return. When the pinter is to a string, the\r
                                  full default value string retrieved from IFR data\r
                                  will be merged into the input string and return.\r
-                                 When Request points to NULL, the default value string \r
-                                 for each varstore in form package will be merged into \r
+                                 When Request points to NULL, the default value string\r
+                                 for each varstore in form package will be merged into\r
                                  a <MultiConfigAltResp> format string and return.\r
-  @param  PointerProgress        Optional parameter, it can be be NULL. \r
-                                 When it is not NULL, if Request is NULL, it returns NULL. \r
+  @param  PointerProgress        Optional parameter, it can be NULL.\r
+                                 When it is not NULL, if Request is NULL, it returns NULL.\r
                                  On return, points to a character in the Request\r
                                  string. Points to the string's null terminator if\r
                                  request was successful. Points to the most recent\r
@@ -3035,7 +4021,7 @@ GenerateAltConfigResp (
   @retval EFI_SUCCESS            The Results string is set to the full request string.\r
                                  And AltCfgResp contains all default value string.\r
   @retval EFI_OUT_OF_RESOURCES   Not enough memory for the return string.\r
-  @retval EFI_NOT_FOUND          The varstore (Guid and Name) in Request string \r
+  @retval EFI_NOT_FOUND          The varstore (Guid and Name) in Request string\r
                                  can't be found in Form package.\r
   @retval EFI_NOT_FOUND          HiiPackage can't be got on the input HiiHandle.\r
   @retval EFI_INVALID_PARAMETER  Request points to NULL.\r
@@ -3087,7 +4073,7 @@ GetFullStringFromHiiFormPackages (
   }\r
 \r
   //\r
-  // 1. Get the request block array by Request String when Request string containts the block array.\r
+  // 1. Get the request block array by Request String when Request string contains the block array.\r
   //\r
   StringPtr = NULL;\r
   if (*Request != NULL) {\r
@@ -3170,7 +4156,7 @@ GetFullStringFromHiiFormPackages (
   //\r
 \r
   //\r
-  // Parse the opcode in form pacakge to get the default setting.\r
+  // Parse the opcode in form package to get the default setting.\r
   //\r
   Status = ParseIfrData (DataBaseRecord->Handle,\r
                          HiiFormPackage,\r
@@ -3210,13 +4196,13 @@ GetFullStringFromHiiFormPackages (
   // Go through all VarStorageData Entry and get the DefaultId array for each one\r
   // Then construct them all to : ConfigHdr AltConfigHdr ConfigBody AltConfigHdr ConfigBody\r
   //\r
-  Status = GenerateAltConfigResp (ConfigHdr, VarStorageData, DefaultIdArray, &DefaultAltCfgResp);\r
+  Status = GenerateAltConfigResp (DataBaseRecord->Handle,ConfigHdr, VarStorageData, DefaultIdArray, &DefaultAltCfgResp);\r
   if (EFI_ERROR (Status)) {\r
     goto Done;\r
   }\r
 \r
   //\r
-  // 5. Merge string into the input AltCfgResp if the iput *AltCfgResp is not NULL.\r
+  // 5. Merge string into the input AltCfgResp if the input *AltCfgResp is not NULL.\r
   //\r
   if (*AltCfgResp != NULL && DefaultAltCfgResp != NULL) {\r
     Status = MergeDefaultString (AltCfgResp, DefaultAltCfgResp);\r
@@ -3262,6 +4248,10 @@ Done:
       }\r
       FreePool (BlockData);\r
     }\r
+    if (VarStorageData ->Name != NULL) {\r
+      FreePool (VarStorageData ->Name);\r
+      VarStorageData ->Name = NULL;\r
+    }\r
     FreePool (VarStorageData);\r
   }\r
 \r
@@ -3278,14 +4268,14 @@ Done:
   }\r
 \r
   //\r
-  // Free the allocated string \r
+  // Free the allocated string\r
   //\r
   if (ConfigHdr != NULL) {\r
     FreePool (ConfigHdr);\r
   }\r
 \r
   //\r
-  // Free Pacakge data\r
+  // Free Package data\r
   //\r
   if (HiiFormPackage != NULL) {\r
     FreePool (HiiFormPackage);\r
@@ -3305,13 +4295,13 @@ Done:
 }\r
 \r
 /**\r
-  This function gets the full request resp string by \r
+  This function gets the full request resp string by\r
   parsing IFR data in HII form packages.\r
 \r
   @param  This                   A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL\r
                                  instance.\r
-  @param  EfiVarStoreInfo        The efi varstore info which is save in the EFI \r
-                                 varstore data structure.                       \r
+  @param  EfiVarStoreInfo        The efi varstore info which is save in the EFI\r
+                                 varstore data structure.\r
   @param  Request                Pointer to a null-terminated Unicode string in\r
                                  <ConfigRequest> format.\r
   @param  RequestResp            Pointer to a null-terminated Unicode string in\r
@@ -3333,7 +4323,7 @@ Done:
 EFI_STATUS\r
 GetConfigRespFromEfiVarStore (\r
   IN  CONST EFI_HII_CONFIG_ROUTING_PROTOCOL  *This,\r
-  IN  EFI_IFR_VARSTORE_EFI                   *EfiVarStoreInfo,    \r
+  IN  EFI_IFR_VARSTORE_EFI                   *EfiVarStoreInfo,\r
   IN  EFI_STRING                             Request,\r
   OUT EFI_STRING                             *RequestResp,\r
   OUT EFI_STRING                             *AccessProgress\r
@@ -3341,6 +4331,7 @@ GetConfigRespFromEfiVarStore (
 {\r
   EFI_STATUS Status;\r
   EFI_STRING VarStoreName;\r
+  UINTN      NameSize;\r
   UINT8      *VarStore;\r
   UINTN      BufferSize;\r
 \r
@@ -3349,15 +4340,16 @@ GetConfigRespFromEfiVarStore (
   VarStore        = NULL;\r
   VarStoreName    = NULL;\r
   *AccessProgress = Request;\r
-  \r
-  VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)EfiVarStoreInfo->Name) * sizeof (CHAR16));\r
+\r
+  NameSize = AsciiStrSize ((CHAR8 *)EfiVarStoreInfo->Name);\r
+  VarStoreName = AllocateZeroPool (NameSize * sizeof (CHAR16));\r
   if (VarStoreName == NULL) {\r
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Done;\r
   }\r
-  AsciiStrToUnicodeStr ((CHAR8 *) EfiVarStoreInfo->Name, VarStoreName);\r
-   \r
-  \r
+  AsciiStrToUnicodeStrS ((CHAR8 *) EfiVarStoreInfo->Name, VarStoreName, NameSize);\r
+\r
+\r
   Status = gRT->GetVariable (VarStoreName, &EfiVarStoreInfo->Guid, NULL, &BufferSize, NULL);\r
   if (Status != EFI_BUFFER_TOO_SMALL) {\r
     goto Done;\r
@@ -3389,17 +4381,17 @@ Done:
 \r
 \r
 /**\r
-  This function route the full request resp string for efi varstore. \r
+  This function route the full request resp string for efi varstore.\r
 \r
   @param  This                   A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL\r
                                  instance.\r
-  @param  EfiVarStoreInfo        The efi varstore info which is save in the EFI \r
-                                 varstore data structure.      \r
+  @param  EfiVarStoreInfo        The efi varstore info which is save in the EFI\r
+                                 varstore data structure.\r
   @param  RequestResp            Pointer to a null-terminated Unicode string in\r
                                  <ConfigResp> format.\r
   @param  Result                 Pointer to a null-terminated Unicode string in\r
                                  <ConfigResp> format.\r
-                                 \r
+\r
   @retval EFI_SUCCESS            The Results string is set to the full request string.\r
                                  And AltCfgResp contains all default value string.\r
   @retval EFI_OUT_OF_RESOURCES   Not enough memory for the return string.\r
@@ -3409,13 +4401,14 @@ Done:
 EFI_STATUS\r
 RouteConfigRespForEfiVarStore (\r
   IN  CONST EFI_HII_CONFIG_ROUTING_PROTOCOL  *This,\r
-  IN  EFI_IFR_VARSTORE_EFI                   *EfiVarStoreInfo,  \r
+  IN  EFI_IFR_VARSTORE_EFI                   *EfiVarStoreInfo,\r
   IN  EFI_STRING                             RequestResp,\r
   OUT EFI_STRING                             *Result\r
   )\r
 {\r
   EFI_STATUS Status;\r
   EFI_STRING VarStoreName;\r
+  UINTN      NameSize;\r
   UINT8      *VarStore;\r
   UINTN      BufferSize;\r
   UINTN      BlockSize;\r
@@ -3424,16 +4417,19 @@ RouteConfigRespForEfiVarStore (
   BufferSize   = 0;\r
   VarStore     = NULL;\r
   VarStoreName = NULL;\r
+  *Result = RequestResp;\r
 \r
-  VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)EfiVarStoreInfo->Name) * sizeof (CHAR16));\r
+  NameSize = AsciiStrSize ((CHAR8 *)EfiVarStoreInfo->Name);\r
+  VarStoreName = AllocateZeroPool (NameSize * sizeof (CHAR16));\r
   if (VarStoreName == NULL) {\r
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Done;\r
   }\r
-  AsciiStrToUnicodeStr ((CHAR8 *) EfiVarStoreInfo->Name, VarStoreName);\r
-      \r
+  AsciiStrToUnicodeStrS ((CHAR8 *) EfiVarStoreInfo->Name, VarStoreName, NameSize);\r
+\r
   Status = gRT->GetVariable (VarStoreName, &EfiVarStoreInfo->Guid, NULL, &BufferSize, NULL);\r
   if (Status != EFI_BUFFER_TOO_SMALL) {\r
+    DEBUG ((DEBUG_ERROR, "The variable does not exist!"));\r
     goto Done;\r
   }\r
 \r
@@ -3452,6 +4448,7 @@ RouteConfigRespForEfiVarStore (
 \r
   Status = gRT->SetVariable (VarStoreName, &EfiVarStoreInfo->Guid, EfiVarStoreInfo->Attributes, BufferSize, VarStore);\r
   if (EFI_ERROR (Status)) {\r
+    *Result = RequestResp;\r
     goto Done;\r
   }\r
 \r
@@ -3470,7 +4467,7 @@ Done:
 /**\r
   Validate the config request elements.\r
 \r
-  @param  ConfigElements                A null-terminated Unicode string in <ConfigRequest> format, \r
+  @param  ConfigElements                A null-terminated Unicode string in <ConfigRequest> format,\r
                                         without configHdr field.\r
 \r
   @retval     CHAR16 *    THE first Name/value pair not correct.\r
@@ -3513,7 +4510,7 @@ OffsetWidthValidate (
 /**\r
   Validate the config request elements.\r
 \r
-  @param  ConfigElements                A null-terminated Unicode string in <ConfigRequest> format, \r
+  @param  ConfigElements                A null-terminated Unicode string in <ConfigRequest> format,\r
                                         without configHdr field.\r
 \r
   @retval     CHAR16 *    THE first Name/value pair not correct.\r
@@ -3538,7 +4535,7 @@ NameValueValidate (
     StringPtr += 1;\r
 \r
     StringPtr = StrStr (StringPtr, L"&");\r
-    \r
+\r
     if (StringPtr == NULL) {\r
       return NULL;\r
     }\r
@@ -3646,7 +4643,7 @@ ConfigRequestValidate (
   @retval EFI_INVALID_PARAMETER  Illegal syntax. Progress set to most recent &\r
                                  before the error or the beginning of the string.\r
   @retval EFI_INVALID_PARAMETER  The ExtractConfig function of the underlying HII\r
-                                 Configuration Access Protocol returned \r
+                                 Configuration Access Protocol returned\r
                                  EFI_INVALID_PARAMETER. Progress set to most recent\r
                                  & before the error or the beginning of the string.\r
 \r
@@ -3676,6 +4673,8 @@ HiiConfigRoutingExtractConfig (
   EFI_HII_CONFIG_ACCESS_PROTOCOL      *ConfigAccess;\r
   EFI_STRING                          AccessProgress;\r
   EFI_STRING                          AccessResults;\r
+  EFI_STRING                          AccessProgressBackup;\r
+  EFI_STRING                          AccessResultsBackup;\r
   EFI_STRING                          DefaultResults;\r
   BOOLEAN                             FirstElement;\r
   BOOLEAN                             IfrDataParsedFlag;\r
@@ -3683,6 +4682,9 @@ HiiConfigRoutingExtractConfig (
   EFI_IFR_VARSTORE_EFI                *EfiVarStoreInfo;\r
   EFI_STRING                          ErrorPtr;\r
   UINTN                               DevicePathSize;\r
+  UINTN                               ConigStringSize;\r
+  UINTN                               ConigStringSizeNewsize;\r
+  EFI_STRING                          ConfigStringPtr;\r
 \r
   if (This == NULL || Progress == NULL || Results == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -3701,6 +4703,8 @@ HiiConfigRoutingExtractConfig (
   Status         = EFI_SUCCESS;\r
   AccessResults  = NULL;\r
   AccessProgress = NULL;\r
+  AccessResultsBackup  = NULL;\r
+  AccessProgressBackup = NULL;\r
   DevicePath     = NULL;\r
   IfrDataParsedFlag = FALSE;\r
   IsEfiVarStore     = FALSE;\r
@@ -3776,7 +4780,7 @@ HiiConfigRoutingExtractConfig (
         }\r
       }\r
     }\r
-    \r
+\r
     //\r
     // Try to find driver handle by device path.\r
     //\r
@@ -3814,7 +4818,7 @@ HiiConfigRoutingExtractConfig (
     IfrDataParsedFlag = FALSE;\r
     if ((HiiHandle != NULL) && !GetElementsFromRequest(ConfigRequest)) {\r
       //\r
-      // Get the full request string from IFR when HiiPackage is registered to HiiHandle \r
+      // Get the full request string from IFR when HiiPackage is registered to HiiHandle\r
       //\r
       IfrDataParsedFlag = TRUE;\r
       Status = GetFullStringFromHiiFormPackages (Database, DevicePath, &ConfigRequest, &DefaultResults, &AccessProgress);\r
@@ -3843,13 +4847,69 @@ HiiConfigRoutingExtractConfig (
     if (EFI_ERROR (Status)) {\r
       goto Done;\r
     }\r
-    \r
+\r
     if (IsEfiVarStore) {\r
       //\r
       // Call the GetVariable function to extract settings.\r
       //\r
       Status = GetConfigRespFromEfiVarStore(This, EfiVarStoreInfo, ConfigRequest, &AccessResults, &AccessProgress);\r
       FreePool (EfiVarStoreInfo);\r
+      if (EFI_ERROR (Status)) {\r
+        //\r
+        // AccessProgress indicates the parsing progress on <ConfigRequest>.\r
+        // Map it to the progress on <MultiConfigRequest> then return it.\r
+        //\r
+        *Progress = StrStr (StringPtr, AccessProgress);\r
+        goto Done;\r
+      }\r
+\r
+      //\r
+      // For EfiVarstore, call corresponding ConfigAccess protocol to get the AltCfgResp from driver.\r
+      //\r
+      Status = gBS->HandleProtocol (\r
+                      DriverHandle,\r
+                      &gEfiHiiConfigAccessProtocolGuid,\r
+                      (VOID **) &ConfigAccess\r
+                      );\r
+      if (EFI_ERROR (Status)) {\r
+        //\r
+        // The driver has EfiVarStore, may not install ConfigAccess protocol.\r
+        // So ignore the error status in this case.\r
+        //\r
+        Status = EFI_SUCCESS;\r
+      } else {\r
+        Status = ConfigAccess->ExtractConfig (\r
+                                 ConfigAccess,\r
+                                 ConfigRequest,\r
+                                 &AccessProgressBackup,\r
+                                 &AccessResultsBackup\r
+                                 );\r
+        if (!EFI_ERROR(Status)) {\r
+          //\r
+          //Merge the AltCfgResp in AccessResultsBackup to AccessResults\r
+          //\r
+          if ((AccessResultsBackup != NULL) && (StrStr (AccessResultsBackup, L"&ALTCFG=") != NULL)) {\r
+            ConigStringSize = StrSize (AccessResults);\r
+            ConfigStringPtr = StrStr (AccessResultsBackup, L"&GUID=");\r
+            ConigStringSizeNewsize = StrSize (ConfigStringPtr) + ConigStringSize + sizeof (CHAR16);\r
+            AccessResults = (EFI_STRING) ReallocatePool (\r
+                                         ConigStringSize,\r
+                                         ConigStringSizeNewsize,\r
+                                         AccessResults);\r
+            StrCatS (AccessResults, ConigStringSizeNewsize / sizeof (CHAR16), ConfigStringPtr);\r
+          }\r
+        } else {\r
+          //\r
+          // In the ExtractConfig function of some driver may not support EfiVarStore,\r
+          // may return error status, just ignore the error status in this case.\r
+          //\r
+          Status = EFI_SUCCESS;\r
+        }\r
+        if (AccessResultsBackup != NULL) {\r
+          FreePool (AccessResultsBackup);\r
+          AccessResultsBackup = NULL;\r
+        }\r
+      }\r
     } else {\r
       //\r
       // Call corresponding ConfigAccess protocol to extract settings\r
@@ -3859,7 +4919,9 @@ HiiConfigRoutingExtractConfig (
                       &gEfiHiiConfigAccessProtocolGuid,\r
                       (VOID **) &ConfigAccess\r
                       );\r
-      ASSERT_EFI_ERROR (Status);\r
+      if (EFI_ERROR (Status)) {\r
+        goto Done;\r
+      }\r
 \r
       Status = ConfigAccess->ExtractConfig (\r
                                ConfigAccess,\r
@@ -3879,12 +4941,12 @@ HiiConfigRoutingExtractConfig (
 \r
     //\r
     // Attach this <ConfigAltResp> to a <MultiConfigAltResp>. There is a '&'\r
-    // which seperates the first <ConfigAltResp> and the following ones.\r
+    // which separates the first <ConfigAltResp> and the following ones.\r
     //\r
     ASSERT (*AccessProgress == 0);\r
 \r
     //\r
-    // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle \r
+    // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle\r
     //\r
     if (!IfrDataParsedFlag && HiiHandle != NULL) {\r
       Status = GetFullStringFromHiiFormPackages (Database, DevicePath, &ConfigRequest, &DefaultResults, NULL);\r
@@ -3900,13 +4962,13 @@ HiiConfigRoutingExtractConfig (
       FreePool (DefaultResults);\r
       DefaultResults = NULL;\r
     }\r
-    \r
+\r
 NextConfigString:\r
     if (!FirstElement) {\r
       Status = AppendToMultiString (Results, L"&");\r
       ASSERT_EFI_ERROR (Status);\r
     }\r
-    \r
+\r
     Status = AppendToMultiString (Results, AccessResults);\r
     ASSERT_EFI_ERROR (Status);\r
 \r
@@ -3934,22 +4996,22 @@ Done:
     FreePool (*Results);\r
     *Results = NULL;\r
   }\r
-  \r
+\r
   if (ConfigRequest != NULL) {\r
     FreePool (ConfigRequest);\r
   }\r
-  \r
+\r
   if (AccessResults != NULL) {\r
     FreePool (AccessResults);\r
   }\r
-  \r
+\r
   if (DefaultResults != NULL) {\r
     FreePool (DefaultResults);\r
   }\r
-  \r
+\r
   if (DevicePath != NULL) {\r
     FreePool (DevicePath);\r
-  }  \r
+  }\r
 \r
   return Status;\r
 }\r
@@ -3964,8 +5026,8 @@ Done:
                                  instance.\r
   @param  Results                Null-terminated Unicode string in\r
                                  <MultiConfigAltResp> format which has all values\r
-                                 filled in for the entirety of the current HII \r
-                                 database. String to be allocated by the  called \r
+                                 filled in for the entirety of the current HII\r
+                                 database. String to be allocated by the  called\r
                                  function. De-allocation is up to the caller.\r
 \r
   @retval EFI_SUCCESS            The Results string is filled with the values\r
@@ -4081,7 +5143,7 @@ HiiConfigRoutingExportConfig (
                              );\r
     if (EFI_ERROR (Status)) {\r
       //\r
-      // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle \r
+      // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle\r
       //\r
       if (HiiHandle != NULL && DevicePath != NULL) {\r
         IfrDataParsedFlag = TRUE;\r
@@ -4105,7 +5167,7 @@ HiiConfigRoutingExportConfig (
 \r
     if (!EFI_ERROR (Status)) {\r
       //\r
-      // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle \r
+      // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle\r
       //\r
       if (!IfrDataParsedFlag && HiiHandle != NULL && DevicePath != NULL) {\r
         StringPtr = StrStr (AccessResults, L"&GUID=");\r
@@ -4129,28 +5191,28 @@ HiiConfigRoutingExportConfig (
         FreePool (DefaultResults);\r
         DefaultResults = NULL;\r
       }\r
-      \r
+\r
       //\r
       // Attach this <ConfigAltResp> to a <MultiConfigAltResp>. There is a '&'\r
-      // which seperates the first <ConfigAltResp> and the following ones.      \r
+      // which separates the first <ConfigAltResp> and the following ones.\r
       //\r
       if (!FirstElement) {\r
         Status = AppendToMultiString (Results, L"&");\r
         ASSERT_EFI_ERROR (Status);\r
       }\r
-      \r
+\r
       Status = AppendToMultiString (Results, AccessResults);\r
       ASSERT_EFI_ERROR (Status);\r
 \r
       FirstElement = FALSE;\r
-      \r
+\r
       FreePool (AccessResults);\r
       AccessResults = NULL;\r
     }\r
   }\r
   FreePool (ConfigAccessHandles);\r
 \r
-  return EFI_SUCCESS;  \r
+  return EFI_SUCCESS;\r
 }\r
 \r
 \r
@@ -4318,7 +5380,7 @@ HiiConfigRoutingRouteConfig (
     if (IsEfiVarstore) {\r
       //\r
       // Call the SetVariable function to route settings.\r
-      //    \r
+      //\r
       Status = RouteConfigRespForEfiVarStore(This, EfiVarStoreInfo, ConfigResp, &AccessProgress);\r
       FreePool (EfiVarStoreInfo);\r
     } else {\r
@@ -4330,7 +5392,11 @@ HiiConfigRoutingRouteConfig (
                       &gEfiHiiConfigAccessProtocolGuid,\r
                       (VOID **)  &ConfigAccess\r
                       );\r
-      ASSERT_EFI_ERROR (Status);\r
+      if (EFI_ERROR (Status)) {\r
+        *Progress = StringPtr;\r
+        FreePool (ConfigResp);\r
+        return EFI_NOT_FOUND;\r
+      }\r
 \r
       Status = ConfigAccess->RouteConfig (\r
                                ConfigAccess,\r
@@ -4432,6 +5498,8 @@ HiiBlockToConfig (
   CHAR16                              *TemString;\r
   CHAR16                              TemChar;\r
 \r
+  TmpBuffer = NULL;\r
+\r
   if (This == NULL || Progress == NULL || Config == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
@@ -4584,11 +5652,18 @@ HiiBlockToConfig (
       Status = EFI_OUT_OF_RESOURCES;\r
       goto Exit;\r
     }\r
-    \r
+\r
     TemString = ValueStr;\r
     TemBuffer = Value + Width - 1;\r
     for (Index = 0; Index < Width; Index ++, TemBuffer --) {\r
-      TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemBuffer, 2);\r
+      UnicodeValueToStringS (\r
+        TemString,\r
+        Length  * sizeof (CHAR16) - ((UINTN)TemString - (UINTN)ValueStr),\r
+        PREFIX_ZERO | RADIX_HEX,\r
+        *TemBuffer,\r
+        2\r
+        );\r
+      TemString += StrnLenS (TemString, Length - ((UINTN)TemString - (UINTN)ValueStr) / sizeof (CHAR16));\r
     }\r
 \r
     FreePool (Value);\r
@@ -4608,8 +5683,8 @@ HiiBlockToConfig (
       *(ConfigElement + (StringPtr - TmpPtr)) = L'&';\r
     }\r
     *(ConfigElement + (StringPtr - TmpPtr) + 1) = 0;\r
-    StrCat (ConfigElement, L"VALUE=");\r
-    StrCat (ConfigElement, ValueStr);\r
+    StrCatS (ConfigElement, Length, L"VALUE=");\r
+    StrCatS (ConfigElement, Length, ValueStr);\r
 \r
     AppendToMultiString (Config, ConfigElement);\r
 \r
@@ -4634,7 +5709,7 @@ HiiBlockToConfig (
     Status = EFI_INVALID_PARAMETER;\r
     goto Exit;\r
   }\r
-  \r
+\r
   HiiToLower (*Config);\r
   *Progress = StringPtr;\r
   return EFI_SUCCESS;\r
@@ -4699,7 +5774,7 @@ Exit:
                                  value pair. Block is left updated and\r
                                  Progress points at the '&' preceding the first\r
                                  non-<BlockName>.\r
-  @retval EFI_BUFFER_TOO_SMALL   Block not large enough. Progress undefined. \r
+  @retval EFI_BUFFER_TOO_SMALL   Block not large enough. Progress undefined.\r
                                  BlockSize is updated with the required buffer size.\r
   @retval EFI_NOT_FOUND          Target for the specified routing data was not found.\r
                                  Progress points to the "G" in "GUID" of the errant\r
@@ -4728,6 +5803,8 @@ HiiConfigToBlock (
   UINTN                               BufferSize;\r
   UINTN                               MaxBlockSize;\r
 \r
+  TmpBuffer = NULL;\r
+\r
   if (This == NULL || BlockSize == NULL || Progress == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
@@ -4863,7 +5940,7 @@ HiiConfigToBlock (
       break;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // The input string is not ConfigResp format, return error.\r
   //\r
@@ -4998,10 +6075,10 @@ HiiGetAltCfg (
     &PathStr\r
     );\r
   if (AltCfgId != NULL) {\r
-    GenerateSubStr (L"ALTCFG=", sizeof (UINT16), (VOID *) AltCfgId, 3, &AltIdStr);  \r
+    GenerateSubStr (L"ALTCFG=", sizeof (UINT16), (VOID *) AltCfgId, 3, &AltIdStr);\r
   }\r
   if (Name != NULL) {\r
-    GenerateSubStr (L"NAME=", StrLen (Name) * sizeof (CHAR16), (VOID *) Name, 2, &NameStr);    \r
+    GenerateSubStr (L"NAME=", StrLen (Name) * sizeof (CHAR16), (VOID *) Name, 2, &NameStr);\r
   } else {\r
     GenerateSubStr (L"NAME=", 0, NULL, 2, &NameStr);\r
   }\r
@@ -5124,8 +6201,8 @@ Exit:
     if (*AltCfgResp == NULL) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
     } else {\r
-      StrnCpy (*AltCfgResp, HdrStart, HdrEnd - HdrStart);\r
-      StrCat (*AltCfgResp, Result);\r
+      StrnCpyS (*AltCfgResp, Length, HdrStart, HdrEnd - HdrStart);\r
+      StrCatS (*AltCfgResp, Length, Result);\r
       Status = EFI_SUCCESS;\r
     }\r
   }\r