]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/HiiDatabaseDxe/ConfigRouting.c
MdeModulePkg/HiiDatabaseDxe:remove dead code block
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / ConfigRouting.c
index a704734b309bd831e83e7d28189637c4401a0200..17a914208c6d68db8b5debe6af1ae0d2017ec372 100644 (file)
@@ -1,14 +1,8 @@
 /** @file\r
 Implementation of interfaces function for EFI_HII_CONFIG_ROUTING_PROTOCOL.\r
 \r
-Copyright (c) 2007 - 2016, 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
@@ -264,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
@@ -277,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
@@ -286,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
@@ -296,7 +311,7 @@ GenerateSubStr (
   //\r
   // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.\r
   //\r
-  StrCatS (Str, Length, L"&");  \r
+  StrCatS (Str, Length, L"&");\r
   HiiToLower (Str);\r
 \r
   *SubStr = Str;\r
@@ -331,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
@@ -436,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
@@ -485,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
@@ -518,7 +533,7 @@ Exit:
   @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 neccessary structures.\r
+  @retval EFI_OUT_OF_RESOURCES   Insufficient resources to store necessary structures.\r
   @retval EFI_SUCCESS            The function finishes successfully.\r
 \r
 **/\r
@@ -574,7 +589,7 @@ FindSameBlockElement(
   @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 neccessary structures.\r
+  @retval EFI_OUT_OF_RESOURCES   Insufficient resources to store necessary structures.\r
   @retval EFI_SUCCESS            The function finishes  successfully.\r
 \r
 **/\r
@@ -715,7 +730,7 @@ Exit:
   @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 neccessary structures.\r
+  @retval EFI_OUT_OF_RESOURCES   Insufficient resources to store necessary structures.\r
   @retval EFI_SUCCESS            The function finishes  successfully.\r
 \r
 **/\r
@@ -845,7 +860,7 @@ Exit:
                                  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 neccessary\r
+  @retval EFI_OUT_OF_RESOURCES   Insufficient resources to store necessary\r
                                  structures.\r
   @retval EFI_SUCCESS            The function finishes  successfully.\r
 \r
@@ -894,6 +909,7 @@ CompareAndMergeDefaultString (
   // To find the <AltResp> with AltConfigHdr in AltCfgResp, ignore other <AltResp> which follow it.\r
   //\r
   StringPtr = StrStr (*AltCfgResp, AltConfigHdr);\r
+  ASSERT (StringPtr != NULL);\r
   StringPtrNext = StrStr (StringPtr + 1, L"&GUID");\r
   if (StringPtrNext != NULL) {\r
     TempCharA = *StringPtrNext;\r
@@ -989,10 +1005,10 @@ Exit:
   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
@@ -1015,17 +1031,17 @@ MergeDefaultString (
   UINTN        SizeAltCfgResp;\r
   UINTN        MaxLen;\r
   UINTN        TotalSize;\r
-  \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
@@ -1060,18 +1076,18 @@ MergeDefaultString (
   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
     StrnCatS (AltConfigHdr, MaxLen, StringPtrDefault + HeaderLength, 4);\r
-    StringPtr = StrStr (*AltCfgResp, AltConfigHdr); \r
-    \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
@@ -1115,16 +1131,16 @@ MergeDefaultString (
       //\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
@@ -1141,9 +1157,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
@@ -1151,8 +1167,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
@@ -1161,7 +1178,7 @@ InsertDefaultValue (
         DefaultValueArray->Cleaned = DefaultValueData->Cleaned;\r
       }\r
       return;\r
-    } \r
+    }\r
   }\r
 \r
   //\r
@@ -1178,7 +1195,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
@@ -1203,7 +1220,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
@@ -1211,7 +1228,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
@@ -1223,34 +1240,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
@@ -1273,8 +1290,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
@@ -1316,12 +1333,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
@@ -1348,7 +1365,7 @@ InternalGetString (
 \r
   //\r
   // Initialize all allocated buffers to NULL\r
-  // \r
+  //\r
   SupportedLanguages = NULL;\r
   PlatformLanguage   = NULL;\r
   BestLanguage       = NULL;\r
@@ -1372,11 +1389,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
@@ -1397,8 +1414,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
@@ -1457,12 +1474,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
@@ -1540,18 +1557,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
@@ -1564,18 +1581,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
@@ -1590,7 +1607,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
@@ -1604,15 +1621,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
@@ -1650,20 +1668,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
@@ -1691,10 +1710,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
@@ -1752,9 +1771,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
@@ -1802,7 +1821,7 @@ IsThisVarstore (
 \r
 Done:\r
   if (GuidStr != NULL) {\r
-    FreePool (GuidStr); \r
+    FreePool (GuidStr);\r
   }\r
 \r
   if (NameStr != NULL) {\r
@@ -1822,9 +1841,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
@@ -1836,6 +1855,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
@@ -1876,15 +1896,16 @@ 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
@@ -1897,11 +1918,12 @@ IsThisPackageList (
 \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
@@ -1920,7 +1942,7 @@ IsThisPackageList (
         goto Done;\r
       }\r
       break;\r
-      \r
+\r
     case EFI_IFR_FORM_OP:\r
     case EFI_IFR_FORM_MAP_OP:\r
       //\r
@@ -1949,15 +1971,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  EFI_NOT_FOUND         This opcode is not required.\r
   @retval  Others                Contain some error.\r
-                                 \r
+\r
 **/\r
 EFI_STATUS\r
 IsThisOpcodeRequired (\r
@@ -1966,16 +1989,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
@@ -1991,8 +2020,24 @@ IsThisOpcodeRequired (
       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
@@ -2004,7 +2049,7 @@ IsThisOpcodeRequired (
     }\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
@@ -2026,6 +2071,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
@@ -2042,7 +2090,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
@@ -2050,8 +2098,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
@@ -2086,6 +2134,7 @@ 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
@@ -2095,6 +2144,10 @@ ParseIfrData (
   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
@@ -2104,6 +2157,9 @@ ParseIfrData (
   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
@@ -2144,12 +2200,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
@@ -2177,20 +2234,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
@@ -2254,7 +2312,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
@@ -2276,7 +2334,7 @@ ParseIfrData (
         BlockData = NULL;\r
       }\r
 \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
@@ -2295,7 +2353,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
@@ -2308,7 +2366,12 @@ ParseIfrData (
       if (IfrOneOf->Question.VarStoreId != VarStoreId) {\r
         break;\r
       }\r
-      VarWidth  = (UINT16) (1 << (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE));\r
+\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
@@ -2317,7 +2380,7 @@ ParseIfrData (
         BlockData = NULL;\r
       }\r
 \r
-      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);\r
+      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData, QuestionReferBitField);\r
       if (EFI_ERROR (Status)) {\r
         if (Status == EFI_NOT_FOUND){\r
           //\r
@@ -2340,33 +2403,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
@@ -2383,7 +2453,7 @@ ParseIfrData (
 \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
@@ -2406,7 +2476,7 @@ ParseIfrData (
         BlockData = NULL;\r
       }\r
 \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
@@ -2426,10 +2496,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
@@ -2451,7 +2521,10 @@ ParseIfrData (
         BlockData = NULL;\r
       }\r
 \r
-      Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);\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
@@ -2467,6 +2540,8 @@ ParseIfrData (
       //\r
       ASSERT (BlockData != NULL);\r
 \r
+      SmallestIdFromFlag = FALSE;\r
+\r
       //\r
       // Add default value for standard ID by CheckBox Flag\r
       //\r
@@ -2477,21 +2552,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
@@ -2503,21 +2581,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
@@ -2528,7 +2646,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
@@ -2550,7 +2668,7 @@ ParseIfrData (
       }\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
@@ -2570,7 +2688,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
@@ -2592,7 +2710,7 @@ ParseIfrData (
       }\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
@@ -2612,7 +2730,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
@@ -2634,7 +2752,7 @@ ParseIfrData (
       }\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
@@ -2654,7 +2772,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
@@ -2676,7 +2794,7 @@ ParseIfrData (
       }\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
@@ -2749,13 +2867,10 @@ 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
-          if (BlockData->Name != NULL) {\r
-            FreePool (BlockData->Name);\r
-          }\r
           FreePool (BlockData);\r
           goto Done;\r
         }\r
@@ -2771,15 +2886,18 @@ ParseIfrData (
 \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
@@ -2787,28 +2905,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
@@ -2836,7 +2983,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
@@ -2848,8 +2999,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
@@ -2859,17 +3029,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
@@ -3004,7 +3186,7 @@ GetBlockElement (
     if (*StringPtr != 0 && *StringPtr != L'&') {\r
       goto Done;\r
     }\r
-    \r
+\r
     //\r
     // Set Block Data\r
     //\r
@@ -3036,7 +3218,7 @@ GetBlockElement (
       }\r
     }\r
     //\r
-    // If '\0', parsing is finished. \r
+    // If '\0', parsing is finished.\r
     //\r
     if (*StringPtr == 0) {\r
       break;\r
@@ -3153,7 +3335,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
@@ -3218,14 +3400,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
@@ -3238,7 +3420,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
@@ -3296,10 +3478,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
@@ -3400,52 +3582,211 @@ 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
+  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
-  @param      ValueType             The input data type.\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
-  return StorageWidth;\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
+  //\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
@@ -3482,6 +3823,7 @@ GenerateAltConfigResp (
   UINTN                 Width;\r
   UINT8                 *TmpBuffer;\r
   CHAR16                *DefaultString;\r
+  UINTN                 StrSize;\r
 \r
   BlockData     = NULL;\r
   DataExist     = FALSE;\r
@@ -3491,6 +3833,8 @@ GenerateAltConfigResp (
   //\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
@@ -3498,7 +3842,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
@@ -3524,7 +3868,7 @@ GenerateAltConfigResp (
       }\r
     }\r
   }\r
-  \r
+\r
   //\r
   // No default value is found. The default string doesn't exist.\r
   //\r
@@ -3554,10 +3898,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
@@ -3572,9 +3916,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
@@ -3584,10 +3928,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
@@ -3599,17 +3943,36 @@ GenerateAltConfigResp (
         //\r
         if (BlockData->OpCode == EFI_IFR_STRING_OP){\r
           DefaultString   = InternalGetString(HiiHandle, DefaultValueData->Value.string);\r
-          TmpBuffer = (UINT8 *) DefaultString;\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
-          StringPtr += UnicodeValueToString (StringPtr, PREFIX_ZERO | RADIX_HEX, TmpBuffer[Width - 1], 2);\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
   }\r
@@ -3620,32 +3983,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
@@ -3656,7 +4019,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
@@ -3708,7 +4071,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
@@ -3791,7 +4154,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
@@ -3837,7 +4200,7 @@ GetFullStringFromHiiFormPackages (
   }\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
@@ -3903,14 +4266,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
@@ -3930,13 +4293,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
@@ -3958,7 +4321,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
@@ -3966,6 +4329,7 @@ GetConfigRespFromEfiVarStore (
 {\r
   EFI_STATUS Status;\r
   EFI_STRING VarStoreName;\r
+  UINTN      NameSize;\r
   UINT8      *VarStore;\r
   UINTN      BufferSize;\r
 \r
@@ -3974,15 +4338,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
@@ -4014,17 +4379,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
@@ -4034,13 +4399,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
@@ -4049,16 +4415,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
@@ -4077,6 +4446,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
@@ -4095,7 +4465,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
@@ -4138,7 +4508,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
@@ -4163,7 +4533,7 @@ NameValueValidate (
     StringPtr += 1;\r
 \r
     StringPtr = StrStr (StringPtr, L"&");\r
-    \r
+\r
     if (StringPtr == NULL) {\r
       return NULL;\r
     }\r
@@ -4271,7 +4641,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
@@ -4408,7 +4778,7 @@ HiiConfigRoutingExtractConfig (
         }\r
       }\r
     }\r
-    \r
+\r
     //\r
     // Try to find driver handle by device path.\r
     //\r
@@ -4446,7 +4816,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
@@ -4475,7 +4845,7 @@ HiiConfigRoutingExtractConfig (
     if (EFI_ERROR (Status)) {\r
       goto Done;\r
     }\r
-    \r
+\r
     if (IsEfiVarStore) {\r
       //\r
       // Call the GetVariable function to extract settings.\r
@@ -4569,12 +4939,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
@@ -4590,13 +4960,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
@@ -4624,22 +4994,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
@@ -4654,8 +5024,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
@@ -4771,7 +5141,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
@@ -4795,7 +5165,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
@@ -4819,28 +5189,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
@@ -5008,7 +5378,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
@@ -5124,7 +5494,6 @@ HiiBlockToConfig (
   UINTN                               Index;\r
   UINT8                               *TemBuffer;\r
   CHAR16                              *TemString;\r
-  CHAR16                              TemChar;\r
 \r
   TmpBuffer = NULL;\r
 \r
@@ -5191,10 +5560,13 @@ HiiBlockToConfig (
   //\r
   // Copy <ConfigHdr> and an additional '&' to <ConfigResp>\r
   //\r
-  TemChar = *StringPtr;\r
-  *StringPtr = '\0';\r
-  AppendToMultiString(Config, ConfigRequest);\r
-  *StringPtr = TemChar;\r
+  TemString = AllocateCopyPool (sizeof (CHAR16) * (StringPtr - ConfigRequest + 1), ConfigRequest);\r
+  if (TemString == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  TemString[StringPtr - ConfigRequest] = '\0';\r
+  AppendToMultiString(Config, TemString);\r
+  FreePool (TemString);\r
 \r
   //\r
   // Parse each <RequestElement> if exists\r
@@ -5280,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
@@ -5330,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
@@ -5395,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
@@ -5561,7 +5940,7 @@ HiiConfigToBlock (
       break;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // The input string is not ConfigResp format, return error.\r
   //\r
@@ -5696,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