]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/HiiDatabaseDxe/ConfigRouting.c
MdeModulePkg HiiDataBase: Fix the potential NULL pointer reference
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / ConfigRouting.c
index 9f036a54e19572bc19fb416bc011751c7b9bc774..646864f4dfc1bc7a88019562408d011f5f9e2893 100644 (file)
@@ -1,7 +1,7 @@
 /** @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
+Copyright (c) 2007 - 2017, 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
@@ -65,7 +65,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
@@ -264,7 +264,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 +284,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 +300,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
@@ -436,7 +457,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
@@ -518,7 +539,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 +595,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 +736,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 +866,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
@@ -1021,7 +1042,7 @@ MergeDefaultString (
   }\r
   \r
   //\r
-  // Get the requestr ConfigHdr\r
+  // Get the request ConfigHdr\r
   //\r
   SizeAltCfgResp  = 0;\r
   StringPtr       = *AltCfgResp;\r
@@ -1204,7 +1225,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 +1233,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
@@ -1249,9 +1270,9 @@ InsertBlockData (
   @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
@@ -1697,7 +1718,7 @@ GetVarStoreType (
       } \r
         \r
       //\r
-      // Free alllocated temp string.\r
+      // Free allocated temp string.\r
       //\r
       FreePool (VarStoreName);\r
       FreePool (GuidStr);\r
@@ -1755,8 +1776,8 @@ 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
+  @retval  TRUE              This varstore is the request one.\r
+  @retval  FALSE             This varstore is not the request one.\r
                                  \r
 **/\r
 BOOLEAN\r
@@ -1825,8 +1846,8 @@ 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
+  @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
@@ -1955,10 +1976,11 @@ 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
@@ -1972,16 +1994,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,7 +2025,23 @@ IsThisOpcodeRequired (
       return EFI_NOT_FOUND;\r
     }\r
   } else {\r
-    VarOffset = IfrQuestionHdr->VarStoreInfo.VarOffset;\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
@@ -2032,6 +2076,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
@@ -2048,7 +2095,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
@@ -2056,7 +2103,7 @@ 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
+  @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
@@ -2103,8 +2150,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 +2164,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
@@ -2290,7 +2339,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
@@ -2322,7 +2371,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 +2385,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
@@ -2357,26 +2411,33 @@ ParseIfrData (
         // 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
@@ -2420,7 +2481,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
@@ -2465,7 +2526,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
@@ -2493,10 +2557,14 @@ 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
+        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
@@ -2518,10 +2586,14 @@ 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
@@ -2534,10 +2606,14 @@ ParseIfrData (
       }\r
       if (SmallestIdFromFlag) {\r
         //\r
-        // When smallest default Id is given by the  flag of CheckBox, set defaut value with TRUE for other default Id in the DefaultId list.\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
-        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
@@ -2548,10 +2624,14 @@ ParseIfrData (
         }\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
@@ -2593,7 +2673,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
@@ -2635,7 +2715,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
@@ -2677,7 +2757,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
@@ -2719,7 +2799,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
@@ -2911,7 +2991,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
@@ -2942,8 +3026,8 @@ ParseIfrData (
       }\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
+      // 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
@@ -2953,6 +3037,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 +3054,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
@@ -3547,6 +3638,212 @@ GetStorageWidth (
   return StorageWidth;\r
 }\r
 \r
+/**\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
+  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
+**/\r
+VOID\r
+UpdateDefaultValue (\r
+  IN LIST_ENTRY        *BlockLink\r
+)\r
+{\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
+  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
+      StartBit = BlockData->BitOffset % 8;\r
+      EndBit = StartBit + BlockData->BitWidth - 1;\r
+\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
+/**\r
+Merge the default value in two block datas which have overlap region.\r
+\r
+For bit fields, their related block data may have overlap region, such as:\r
+\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
+After function UpdateDefaultValue:\r
+Block1: offset = 0; width = 1;(byte level) default = 1\r
+Block2: offset = 0; width = 2;(byte level) default = 320 (5 * (2 << 6))\r
+(default value record for block)\r
+\r
+After function MergeBlockDefaultValue:\r
+Block1: offset = 0; width = 1;(byte level) default = 65\r
+Block2: offset = 0; width = 2;(byte level) default = 321\r
+(Block1 and Block2 has overlap region, merge the overlap value to Block1 and Blcok2)\r
+\r
+Block1 and Block2 have overlap byte region, but currntly the default value of Block1 only contains\r
+value of Bit1 (low 6 bits),the default value of Block2 only contains the value of Bit2 (middle 5 bits).\r
+\r
+This fuction merge the default value of these two blocks, and make the default value of block1\r
+also contain the value of lower 2 bits of the Bit2. And make the default value of Block2 also\r
+contain the default value of Bit1.\r
+\r
+We can get the total value of the whole block that just cover these two blocks(in this case is:\r
+block: offset =0; width =2;) then the value of block2 is same as block, the value of block1 is\r
+the first byte value of block.\r
+\r
+@param  FirstBlock     Point to the block date whose default value need to be merged.\r
+@param  SecondBlock    Point to the block date whose default value need to be merged.\r
+\r
+**/\r
+VOID\r
+MergeBlockDefaultValue (\r
+  IN OUT IFR_BLOCK_DATA      *FirstBlock,\r
+  IN OUT IFR_BLOCK_DATA      *SecondBlock\r
+)\r
+{\r
+  LIST_ENTRY          *FirstListEntry;\r
+  LIST_ENTRY          *SecondListEntry;\r
+  LIST_ENTRY          *FirstDefaultLink;\r
+  LIST_ENTRY          *SecondDefaultLink;\r
+  IFR_DEFAULT_DATA    *FirstDefaultValueData;\r
+  IFR_DEFAULT_DATA    *SecondDefaultValueData;\r
+  UINT32              *FirstDefaultValue;\r
+  UINT32              *SecondDefaultValue;\r
+  UINT64              TotalValue;\r
+  UINT64              ShiftedValue;\r
+  UINT16              OffsetShift;\r
+\r
+  FirstListEntry = &FirstBlock->DefaultValueEntry;\r
+  for (FirstDefaultLink = FirstListEntry->ForwardLink; FirstDefaultLink != FirstListEntry; FirstDefaultLink = FirstDefaultLink->ForwardLink) {\r
+    FirstDefaultValueData = BASE_CR (FirstDefaultLink, IFR_DEFAULT_DATA, Entry);\r
+    SecondListEntry = &SecondBlock->DefaultValueEntry;\r
+    for (SecondDefaultLink = SecondListEntry->ForwardLink; SecondDefaultLink != SecondListEntry; SecondDefaultLink = SecondDefaultLink->ForwardLink) {\r
+      SecondDefaultValueData = BASE_CR (SecondDefaultLink, IFR_DEFAULT_DATA, Entry);\r
+      if (FirstDefaultValueData->DefaultId != SecondDefaultValueData->DefaultId) {\r
+        continue;\r
+      }\r
+      //\r
+      // Find default value with same default id in the two blocks.\r
+      // Note: Default value for bit field question is stored as UINT32 type.\r
+      //\r
+      FirstDefaultValue = &FirstDefaultValueData->Value.u32;\r
+      SecondDefaultValue = &SecondDefaultValueData->Value.u32;\r
+      //\r
+      // 1. Get the default value of the whole blcok that can just cover FirstBlock and SecondBlock.\r
+      // 2. Get the default value of FirstBlock and SecondBlock form the value of whole block based\r
+      //    on the offset and width of FirstBlock and SecondBlock.\r
+      //\r
+      if (FirstBlock->Offset > SecondBlock->Offset) {\r
+        OffsetShift = FirstBlock->Offset - SecondBlock->Offset;\r
+        ShiftedValue = LShiftU64 ((UINT64) (*FirstDefaultValue), OffsetShift * 8);\r
+        TotalValue = ShiftedValue | (UINT64) (*SecondDefaultValue);\r
+        *SecondDefaultValue = (UINT32) BitFieldRead64 (TotalValue, 0, SecondBlock->Width * 8 -1);\r
+        *FirstDefaultValue = (UINT32) BitFieldRead64 (TotalValue, OffsetShift * 8, OffsetShift * 8 + FirstBlock->Width *8 -1);\r
+      } else {\r
+        OffsetShift = SecondBlock->Offset -FirstBlock->Offset;\r
+        ShiftedValue = LShiftU64 ((UINT64) (*SecondDefaultValue), OffsetShift * 8);\r
+        TotalValue = ShiftedValue | (UINT64) (*FirstDefaultValue);\r
+        *FirstDefaultValue = (UINT32) BitFieldRead64 (TotalValue, 0, FirstBlock->Width * 8 -1);\r
+        *SecondDefaultValue = (UINT32) BitFieldRead64 (TotalValue, OffsetShift * 8, OffsetShift * 8 + SecondBlock->Width *8 -1);\r
+      }\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+\r
+Update the default value in the block data which used as Bit VarStore\r
+\r
+@param  BlockLink     The Link of the block data.\r
+\r
+**/\r
+VOID\r
+UpdateBlockDataArray (\r
+  IN LIST_ENTRY        *BlockLink\r
+)\r
+{\r
+  LIST_ENTRY          *Link;\r
+  LIST_ENTRY          *TempLink;\r
+  IFR_BLOCK_DATA      *BlockData;\r
+  IFR_BLOCK_DATA      *NextBlockData;\r
+\r
+  //\r
+  // 1. Update default value in BitVar block data.\r
+  // Sine some block datas are used as BitVarStore, then the default value recored in the block\r
+  // is for related bit field in the block. so we need to set the default value to the related bit\r
+  // fields in the block data if the block data is used as bit varstore, then the default value of\r
+  // the block will be updated.\r
+  //\r
+  UpdateDefaultValue (BlockLink);\r
+\r
+  //\r
+  // 2.Update default value for overlap BitVar blcok datas.\r
+  // For block datas have overlap region, we need to merge the default value in different blocks.\r
+  //\r
+  for (Link = BlockLink->ForwardLink; Link != BlockLink; Link = Link->ForwardLink) {\r
+    BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);\r
+    if (!BlockData ->IsBitVar) {\r
+      continue;\r
+    }\r
+    for (TempLink = Link->ForwardLink; TempLink != BlockLink; TempLink = TempLink->ForwardLink) {\r
+      NextBlockData = BASE_CR (TempLink, IFR_BLOCK_DATA, Entry);\r
+      if (!NextBlockData->IsBitVar || NextBlockData->Offset >= BlockData->Offset + BlockData->Width || BlockData->Offset >= NextBlockData->Offset + NextBlockData->Width) {\r
+        continue;\r
+      }\r
+      //\r
+      // Find two blocks are used as bit VarStore and have overlap region, so need to merge default value of these two blocks.\r
+      //\r
+      MergeBlockDefaultValue (BlockData, NextBlockData);\r
+    }\r
+  }\r
+}\r
+\r
 /**\r
   Generate ConfigAltResp string base on the varstore info.\r
 \r
@@ -3581,6 +3878,7 @@ GenerateAltConfigResp (
   UINTN                 Width;\r
   UINT8                 *TmpBuffer;\r
   CHAR16                *DefaultString;\r
+  UINTN                 StrSize;\r
 \r
   BlockData     = NULL;\r
   DataExist     = FALSE;\r
@@ -3590,6 +3888,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
@@ -3698,17 +3998,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
@@ -3743,7 +4062,7 @@ GenerateAltConfigResp (
                                  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
+  @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
@@ -3807,7 +4126,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
@@ -3890,7 +4209,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
@@ -3936,7 +4255,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
@@ -4009,7 +4328,7 @@ Done:
   }\r
 \r
   //\r
-  // Free Pacakge data\r
+  // Free Package data\r
   //\r
   if (HiiFormPackage != NULL) {\r
     FreePool (HiiFormPackage);\r
@@ -4675,7 +4994,7 @@ 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
@@ -4928,7 +5247,7 @@ HiiConfigRoutingExportConfig (
       \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
@@ -5390,7 +5709,14 @@ HiiBlockToConfig (
     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