]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/HiiDatabaseDxe/ConfigRouting.c
MdeModulePkg: Change use of EFI_D_* to DEBUG_*
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / ConfigRouting.c
index a97e28b8dd6090a49a8f888a677794a4f8cbfe59..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
@@ -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
@@ -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
@@ -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 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
@@ -1162,7 +1178,7 @@ InsertDefaultValue (
         DefaultValueArray->Cleaned = DefaultValueData->Cleaned;\r
       }\r
       return;\r
-    } \r
+    }\r
   }\r
 \r
   //\r
@@ -1179,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
@@ -1204,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
@@ -1212,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
@@ -1224,26 +1240,26 @@ 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
@@ -1274,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
@@ -1317,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
@@ -1349,7 +1365,7 @@ InternalGetString (
 \r
   //\r
   // Initialize all allocated buffers to NULL\r
-  // \r
+  //\r
   SupportedLanguages = NULL;\r
   PlatformLanguage   = NULL;\r
   BestLanguage       = NULL;\r
@@ -1373,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
@@ -1398,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
@@ -1458,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
@@ -1541,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
@@ -1565,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
@@ -1591,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
@@ -1609,12 +1625,12 @@ GetVarStoreType (
   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
@@ -1652,8 +1668,8 @@ 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
@@ -1694,8 +1710,8 @@ GetVarStoreType (
         }\r
         *IsEfiVarstore = TRUE;\r
         CopyMem (*EfiVarStore, IfrEfiVarStore, IfrOpHdr->Length);\r
-      } \r
-        \r
+      }\r
+\r
       //\r
       // Free allocated temp string.\r
       //\r
@@ -1757,7 +1773,7 @@ GetElementsFromRequest (
 \r
   @retval  TRUE              This varstore is the request one.\r
   @retval  FALSE             This varstore is not the request one.\r
-                                 \r
+\r
 **/\r
 BOOLEAN\r
 IsThisVarstore (\r
@@ -1805,7 +1821,7 @@ IsThisVarstore (
 \r
 Done:\r
   if (GuidStr != NULL) {\r
-    FreePool (GuidStr); \r
+    FreePool (GuidStr);\r
   }\r
 \r
   if (NameStr != NULL) {\r
@@ -1827,7 +1843,7 @@ Done:
                                 the first found varstore will be as ConfigHdr.\r
   @retval  TRUE                 This hii package is the request one.\r
   @retval  FALSE                This hii package is not the request one.\r
-**/                                \r
+**/\r
 BOOLEAN\r
 IsThisPackageList (\r
   IN     HII_DATABASE_RECORD        *DataBaseRecord,\r
@@ -1880,7 +1896,7 @@ 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
@@ -1926,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
@@ -1959,11 +1975,12 @@ Done:
   @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
@@ -1972,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
@@ -1997,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
@@ -2010,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
@@ -2032,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
@@ -2057,7 +2099,7 @@ IsThisOpcodeRequired (
 \r
   @retval EFI_SUCCESS           The block array and the default value array are got.\r
   @retval EFI_INVALID_PARAMETER The varstore definition in the different form packages\r
-                                are conflicted. \r
+                                are conflicted.\r
   @retval EFI_OUT_OF_RESOURCES  No enough memory.\r
 **/\r
 EFI_STATUS\r
@@ -2103,8 +2145,9 @@ ParseIfrData (
   EFI_HII_PACKAGE_HEADER   *PackageHeader;\r
   EFI_VARSTORE_ID          VarStoreId;\r
   UINT16                   SmallestDefaultId;\r
-  UINT16                   SmallestIdFromFlag;\r
+  BOOLEAN                  SmallestIdFromFlag;\r
   BOOLEAN                  FromOtherDefaultOpcode;\r
+  BOOLEAN                  QuestionReferBitField;\r
 \r
   Status           = EFI_SUCCESS;\r
   BlockData        = NULL;\r
@@ -2116,6 +2159,7 @@ ParseIfrData (
   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
@@ -2190,10 +2234,10 @@ 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
@@ -2268,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
@@ -2290,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
@@ -2309,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
@@ -2322,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
@@ -2331,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
@@ -2354,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
@@ -2397,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
@@ -2420,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
@@ -2440,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
@@ -2465,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
@@ -2496,7 +2555,11 @@ ParseIfrData (
         // 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_STANDARD) {\r
@@ -2521,7 +2584,11 @@ ParseIfrData (
         // 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
@@ -2537,7 +2604,11 @@ ParseIfrData (
         // 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
-        DefaultData.Value.b = TRUE;\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
@@ -2551,7 +2622,11 @@ ParseIfrData (
         // 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
@@ -2571,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
@@ -2593,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
@@ -2613,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
@@ -2635,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
@@ -2655,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
@@ -2677,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
@@ -2697,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
@@ -2719,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
@@ -2792,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
@@ -2819,13 +2891,13 @@ ParseIfrData (
       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
         SmallestIdFromFlag = FALSE;\r
-        \r
+\r
         // Prepare new DefaultValue\r
         //\r
         DefaultData.Type     = DefaultValueFromFlag;\r
@@ -2871,19 +2943,19 @@ ParseIfrData (
 \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
@@ -2911,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
@@ -2953,6 +3029,7 @@ 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
@@ -2969,6 +3046,12 @@ ParseIfrData (
 \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
@@ -3103,7 +3186,7 @@ GetBlockElement (
     if (*StringPtr != 0 && *StringPtr != L'&') {\r
       goto Done;\r
     }\r
-    \r
+\r
     //\r
     // Set Block Data\r
     //\r
@@ -3135,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
@@ -3252,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
@@ -3317,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
@@ -3337,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
@@ -3395,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
@@ -3499,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
-  @param      ValueType             The input data type.\r
+  typedef struct {\r
+    UINT8  Bit1 : 1; //\r
+    UINT8  Bit2 : 2; // Question saved in Bit2,so originalBlock info: offset = 0; width = 1;(byte level) defaul = 1.\r
+                     // (default value record for the bit field)\r
+    ......\r
+  }ExampleData;\r
+\r
+  After function UpdateDefaultValue,the Block info is: offset = 0; width = 1;(byte level) default = 2.\r
+                                                       (default value record for the Block)\r
+\r
+  UpdateDefaultValue function update default value of bit var block based on the bit field info in the block.\r
+\r
+  @param  BlockLink     The Link of the block data.\r
 \r
-  @retval     The data buffer size for the input type.\r
 **/\r
-UINT16\r
-GetStorageWidth (\r
-  IN UINT8       ValueType\r
-  )\r
+VOID\r
+UpdateDefaultValue (\r
+  IN LIST_ENTRY        *BlockLink\r
+)\r
 {\r
-  UINT16         StorageWidth;\r
+  LIST_ENTRY          *Link;\r
+  LIST_ENTRY          *ListEntry;\r
+  LIST_ENTRY          *LinkDefault;\r
+  IFR_BLOCK_DATA      *BlockData;\r
+  IFR_DEFAULT_DATA    *DefaultValueData;\r
+  UINTN               StartBit;\r
+  UINTN               EndBit;\r
+  UINT32              BitFieldDefaultValue;\r
 \r
-  switch (ValueType) {\r
-  case EFI_IFR_NUMERIC_SIZE_1:\r
-  case EFI_IFR_TYPE_BOOLEAN:\r
-    StorageWidth = (UINT16) sizeof (UINT8);\r
-    break;\r
+  for ( Link = BlockLink->ForwardLink; Link != BlockLink; Link = Link->ForwardLink) {\r
+    BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);\r
+    if (!BlockData ->IsBitVar) {\r
+      continue;\r
+    }\r
+    ListEntry  = &BlockData->DefaultValueEntry;\r
+    //\r
+    // Update the default value in the block data with all existing default id.\r
+    //\r
+    for (LinkDefault = ListEntry->ForwardLink; LinkDefault != ListEntry; LinkDefault = LinkDefault->ForwardLink) {\r
+      //\r
+      // Get the default data, and the value of the default data is for some field in the block.\r
+      // Note: Default value for bit field question is stored as UINT32.\r
+      //\r
+      DefaultValueData = BASE_CR (LinkDefault, IFR_DEFAULT_DATA, Entry);\r
+      BitFieldDefaultValue = DefaultValueData->Value.u32;\r
 \r
-  case EFI_IFR_NUMERIC_SIZE_2:\r
-    StorageWidth = (UINT16) sizeof (UINT16);\r
-    break;\r
+      StartBit = BlockData->BitOffset % 8;\r
+      EndBit = StartBit + BlockData->BitWidth - 1;\r
 \r
-  case EFI_IFR_NUMERIC_SIZE_4:\r
-    StorageWidth = (UINT16) sizeof (UINT32);\r
-    break;\r
+      //\r
+      // Set the bit field default value to related bit filed, then we will got the new default vaule for the block data.\r
+      //\r
+      DefaultValueData->Value.u32 = BitFieldWrite32 (0, StartBit, EndBit, BitFieldDefaultValue);\r
+    }\r
+  }\r
+}\r
 \r
-  case EFI_IFR_NUMERIC_SIZE_8:\r
-    StorageWidth = (UINT16) sizeof (UINT64);\r
-    break;\r
+/**\r
+Merge the default value in two block datas which have overlap region.\r
 \r
-  case EFI_IFR_TYPE_TIME:\r
-    StorageWidth = (UINT16) sizeof (EFI_IFR_TIME);\r
-    break;\r
+For bit fields, their related block data may have overlap region, such as:\r
 \r
-  case EFI_IFR_TYPE_DATE:\r
-    StorageWidth = (UINT16) sizeof (EFI_IFR_DATE);\r
-    break;\r
+typedef struct {\r
+  UINT16  Bit1 : 6;  // Question1 refer Bit1, Block1: offset = 0; width = 1;(byte level) default = 1\r
+  UINT16  Bit2 : 5;  // Question2 refer Bit2, Block2: offset = 0; width = 2;(byte level) default = 5\r
+                     // (default value record for the bit field)\r
+  ......\r
+}ExampleData;\r
 \r
-  default:\r
-    StorageWidth = 0;\r
-    break;\r
+After function UpdateDefaultValue:\r
+Block1: offset = 0; width = 1;(byte level) default = 1\r
+Block2: offset = 0; width = 2;(byte level) default = 320 (5 * (2 << 6))\r
+(default value record for block)\r
+\r
+After function MergeBlockDefaultValue:\r
+Block1: offset = 0; width = 1;(byte level) default = 65\r
+Block2: offset = 0; width = 2;(byte level) default = 321\r
+(Block1 and Block2 has overlap region, merge the overlap value to Block1 and Blcok2)\r
+\r
+Block1 and Block2 have overlap byte region, but currntly the default value of Block1 only contains\r
+value of Bit1 (low 6 bits),the default value of Block2 only contains the value of Bit2 (middle 5 bits).\r
+\r
+This fuction merge the default value of these two blocks, and make the default value of block1\r
+also contain the value of lower 2 bits of the Bit2. And make the default value of Block2 also\r
+contain the default value of Bit1.\r
+\r
+We can get the total value of the whole block that just cover these two blocks(in this case is:\r
+block: offset =0; width =2;) then the value of block2 is same as block, the value of block1 is\r
+the first byte value of block.\r
+\r
+@param  FirstBlock     Point to the block date whose default value need to be merged.\r
+@param  SecondBlock    Point to the block date whose default value need to be merged.\r
+\r
+**/\r
+VOID\r
+MergeBlockDefaultValue (\r
+  IN OUT IFR_BLOCK_DATA      *FirstBlock,\r
+  IN OUT IFR_BLOCK_DATA      *SecondBlock\r
+)\r
+{\r
+  LIST_ENTRY          *FirstListEntry;\r
+  LIST_ENTRY          *SecondListEntry;\r
+  LIST_ENTRY          *FirstDefaultLink;\r
+  LIST_ENTRY          *SecondDefaultLink;\r
+  IFR_DEFAULT_DATA    *FirstDefaultValueData;\r
+  IFR_DEFAULT_DATA    *SecondDefaultValueData;\r
+  UINT32              *FirstDefaultValue;\r
+  UINT32              *SecondDefaultValue;\r
+  UINT64              TotalValue;\r
+  UINT64              ShiftedValue;\r
+  UINT16              OffsetShift;\r
+\r
+  FirstListEntry = &FirstBlock->DefaultValueEntry;\r
+  for (FirstDefaultLink = FirstListEntry->ForwardLink; FirstDefaultLink != FirstListEntry; FirstDefaultLink = FirstDefaultLink->ForwardLink) {\r
+    FirstDefaultValueData = BASE_CR (FirstDefaultLink, IFR_DEFAULT_DATA, Entry);\r
+    SecondListEntry = &SecondBlock->DefaultValueEntry;\r
+    for (SecondDefaultLink = SecondListEntry->ForwardLink; SecondDefaultLink != SecondListEntry; SecondDefaultLink = SecondDefaultLink->ForwardLink) {\r
+      SecondDefaultValueData = BASE_CR (SecondDefaultLink, IFR_DEFAULT_DATA, Entry);\r
+      if (FirstDefaultValueData->DefaultId != SecondDefaultValueData->DefaultId) {\r
+        continue;\r
+      }\r
+      //\r
+      // Find default value with same default id in the two blocks.\r
+      // Note: Default value for bit field question is stored as UINT32 type.\r
+      //\r
+      FirstDefaultValue = &FirstDefaultValueData->Value.u32;\r
+      SecondDefaultValue = &SecondDefaultValueData->Value.u32;\r
+      //\r
+      // 1. Get the default value of the whole blcok that can just cover FirstBlock and SecondBlock.\r
+      // 2. Get the default value of FirstBlock and SecondBlock form the value of whole block based\r
+      //    on the offset and width of FirstBlock and SecondBlock.\r
+      //\r
+      if (FirstBlock->Offset > SecondBlock->Offset) {\r
+        OffsetShift = FirstBlock->Offset - SecondBlock->Offset;\r
+        ShiftedValue = LShiftU64 ((UINT64) (*FirstDefaultValue), OffsetShift * 8);\r
+        TotalValue = ShiftedValue | (UINT64) (*SecondDefaultValue);\r
+        *SecondDefaultValue = (UINT32) BitFieldRead64 (TotalValue, 0, SecondBlock->Width * 8 -1);\r
+        *FirstDefaultValue = (UINT32) BitFieldRead64 (TotalValue, OffsetShift * 8, OffsetShift * 8 + FirstBlock->Width *8 -1);\r
+      } else {\r
+        OffsetShift = SecondBlock->Offset -FirstBlock->Offset;\r
+        ShiftedValue = LShiftU64 ((UINT64) (*SecondDefaultValue), OffsetShift * 8);\r
+        TotalValue = ShiftedValue | (UINT64) (*FirstDefaultValue);\r
+        *FirstDefaultValue = (UINT32) BitFieldRead64 (TotalValue, 0, FirstBlock->Width * 8 -1);\r
+        *SecondDefaultValue = (UINT32) BitFieldRead64 (TotalValue, OffsetShift * 8, OffsetShift * 8 + SecondBlock->Width *8 -1);\r
+      }\r
+    }\r
   }\r
+}\r
+\r
+/**\r
+\r
+Update the default value in the block data which used as Bit VarStore\r
+\r
+@param  BlockLink     The Link of the block data.\r
+\r
+**/\r
+VOID\r
+UpdateBlockDataArray (\r
+  IN LIST_ENTRY        *BlockLink\r
+)\r
+{\r
+  LIST_ENTRY          *Link;\r
+  LIST_ENTRY          *TempLink;\r
+  IFR_BLOCK_DATA      *BlockData;\r
+  IFR_BLOCK_DATA      *NextBlockData;\r
 \r
-  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
@@ -3581,6 +3823,7 @@ GenerateAltConfigResp (
   UINTN                 Width;\r
   UINT8                 *TmpBuffer;\r
   CHAR16                *DefaultString;\r
+  UINTN                 StrSize;\r
 \r
   BlockData     = NULL;\r
   DataExist     = FALSE;\r
@@ -3590,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
@@ -3597,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
@@ -3623,7 +3868,7 @@ GenerateAltConfigResp (
       }\r
     }\r
   }\r
-  \r
+\r
   //\r
   // No default value is found. The default string doesn't exist.\r
   //\r
@@ -3653,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
@@ -3671,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
@@ -3683,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
@@ -3698,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
@@ -3719,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 NULL.\r
-                                 When it is not NULL, if Request is NULL, it returns 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
@@ -3755,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
@@ -4002,7 +4266,7 @@ Done:
   }\r
 \r
   //\r
-  // Free the allocated string \r
+  // Free the allocated string\r
   //\r
   if (ConfigHdr != NULL) {\r
     FreePool (ConfigHdr);\r
@@ -4029,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
@@ -4057,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
@@ -4082,8 +4346,8 @@ GetConfigRespFromEfiVarStore (
     goto Done;\r
   }\r
   AsciiStrToUnicodeStrS ((CHAR8 *) EfiVarStoreInfo->Name, VarStoreName, NameSize);\r
-   \r
-  \r
+\r
+\r
   Status = gRT->GetVariable (VarStoreName, &EfiVarStoreInfo->Guid, NULL, &BufferSize, NULL);\r
   if (Status != EFI_BUFFER_TOO_SMALL) {\r
     goto Done;\r
@@ -4115,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
@@ -4135,7 +4399,7 @@ 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
@@ -4160,7 +4424,7 @@ RouteConfigRespForEfiVarStore (
     goto Done;\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
     DEBUG ((DEBUG_ERROR, "The variable does not exist!"));\r
@@ -4201,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
@@ -4244,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
@@ -4269,7 +4533,7 @@ NameValueValidate (
     StringPtr += 1;\r
 \r
     StringPtr = StrStr (StringPtr, L"&");\r
-    \r
+\r
     if (StringPtr == NULL) {\r
       return NULL;\r
     }\r
@@ -4377,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
@@ -4514,7 +4778,7 @@ HiiConfigRoutingExtractConfig (
         }\r
       }\r
     }\r
-    \r
+\r
     //\r
     // Try to find driver handle by device path.\r
     //\r
@@ -4552,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
@@ -4581,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
@@ -4680,7 +4944,7 @@ HiiConfigRoutingExtractConfig (
     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
@@ -4696,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
@@ -4730,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
@@ -4760,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
@@ -4877,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
@@ -4901,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
@@ -4925,7 +5189,7 @@ HiiConfigRoutingExportConfig (
         FreePool (DefaultResults);\r
         DefaultResults = NULL;\r
       }\r
-      \r
+\r
       //\r
       // Attach this <ConfigAltResp> to a <MultiConfigAltResp>. There is a '&'\r
       // which separates the first <ConfigAltResp> and the following ones.\r
@@ -4934,19 +5198,19 @@ HiiConfigRoutingExportConfig (
         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
@@ -5114,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
@@ -5230,7 +5494,6 @@ HiiBlockToConfig (
   UINTN                               Index;\r
   UINT8                               *TemBuffer;\r
   CHAR16                              *TemString;\r
-  CHAR16                              TemChar;\r
 \r
   TmpBuffer = NULL;\r
 \r
@@ -5297,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
@@ -5386,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
@@ -5436,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
@@ -5501,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
@@ -5667,7 +5940,7 @@ HiiConfigToBlock (
       break;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // The input string is not ConfigResp format, return error.\r
   //\r
@@ -5802,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