]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/UefiHiiLib/HiiLib.c
MdeModulePkg/VarCheckHiiLib: Replace EFI_D_INFO with DEBUG_INFO
[mirror_edk2.git] / MdeModulePkg / Library / UefiHiiLib / HiiLib.c
index f48f42fcdb0050ec0410c27af77ff6f9554af89b..ce894c08b573b529313c5048c1dea3f9b23a7ebe 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   HII Library implementation that uses DXE protocols and services.\r
 \r
-  Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 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
@@ -377,6 +377,127 @@ HiiGetHiiHandles (
   }\r
 }\r
 \r
+/**\r
+  This function allows a caller to extract the form set opcode form the Hii Handle.\r
+  The returned buffer is allocated using AllocatePool().The caller is responsible \r
+  for freeing the allocated buffer using FreePool().\r
+\r
+  @param Handle            The HII handle.\r
+  @param Buffer            On return, points to a pointer which point to the buffer that contain the formset opcode.\r
+  @param BufferSize        On return, points to the length of the buffer.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES   No enough memory resource is allocated.\r
+  @retval EFI_NOT_FOUND          Can't find the package data for the input Handle.\r
+  @retval EFI_INVALID_PARAMETER  The input parameters are not correct.\r
+  @retval EFI_SUCCESS            Get the formset opcode from the hii handle successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+HiiGetFormSetFromHiiHandle(\r
+  IN  EFI_HII_HANDLE     Handle,\r
+  OUT EFI_IFR_FORM_SET   **Buffer,\r
+  OUT UINTN              *BufferSize\r
+  )\r
+{\r
+  EFI_STATUS                   Status;\r
+  UINTN                        PackageListSize;\r
+  UINTN                        TempSize;\r
+  EFI_HII_PACKAGE_LIST_HEADER  *HiiPackageList;\r
+  UINT8                        *Package;\r
+  UINT8                        *OpCodeData;\r
+  UINT8                        *FormSetBuffer;\r
+  UINT8                        *TempBuffer;\r
+  UINT32                       Offset;\r
+  UINT32                       Offset2;\r
+  UINT32                       PackageListLength;\r
+  EFI_HII_PACKAGE_HEADER       PackageHeader;\r
+\r
+  TempSize = 0;\r
+  FormSetBuffer = NULL;\r
+  TempBuffer    = NULL;\r
+\r
+  //\r
+  // Get HII PackageList\r
+  //\r
+  PackageListSize = 0;\r
+  HiiPackageList = NULL;\r
+  Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, Handle, &PackageListSize, HiiPackageList);\r
+  if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) {\r
+    return Status;\r
+  }\r
+\r
+  HiiPackageList = AllocatePool (PackageListSize);\r
+  if (HiiPackageList == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  Status = gHiiDatabase->ExportPackageLists (gHiiDatabase, Handle, &PackageListSize, HiiPackageList);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Get Form package from this HII package List\r
+  //\r
+  Status = EFI_NOT_FOUND;\r
+  Offset = sizeof (EFI_HII_PACKAGE_LIST_HEADER);\r
+  PackageListLength = ReadUnaligned32 (&HiiPackageList->PackageLength);\r
+\r
+  while (Offset < PackageListLength) {\r
+    Package = ((UINT8 *) HiiPackageList) + Offset;\r
+    CopyMem (&PackageHeader, Package, sizeof (EFI_HII_PACKAGE_HEADER));\r
+    Offset += PackageHeader.Length;\r
+\r
+    if (PackageHeader.Type != EFI_HII_PACKAGE_FORMS) {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // Search FormSet Opcode in this Form Package\r
+    //\r
+    Offset2 = sizeof (EFI_HII_PACKAGE_HEADER);\r
+    while (Offset2 < PackageHeader.Length) {\r
+      OpCodeData = Package + Offset2;\r
+      Offset2 += ((EFI_IFR_OP_HEADER *) OpCodeData)->Length;\r
+\r
+      if (((EFI_IFR_OP_HEADER *) OpCodeData)->OpCode != EFI_IFR_FORM_SET_OP) {\r
+        continue;\r
+      }\r
+\r
+      if (FormSetBuffer != NULL){\r
+        TempBuffer = AllocateCopyPool (TempSize + ((EFI_IFR_OP_HEADER *) OpCodeData)->Length, FormSetBuffer);\r
+        FreePool(FormSetBuffer);\r
+        FormSetBuffer = NULL;\r
+        if (TempBuffer == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          goto Done;\r
+        }\r
+        CopyMem (TempBuffer + TempSize,  OpCodeData, ((EFI_IFR_OP_HEADER *) OpCodeData)->Length);\r
+      } else {\r
+        TempBuffer = AllocateCopyPool (TempSize + ((EFI_IFR_OP_HEADER *) OpCodeData)->Length, OpCodeData);\r
+        if (TempBuffer == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          goto Done;\r
+        }\r
+      }\r
+      TempSize += ((EFI_IFR_OP_HEADER *) OpCodeData)->Length;\r
+      FormSetBuffer = TempBuffer;\r
+\r
+      Status = EFI_SUCCESS;\r
+      //\r
+      //One form package has one formset, exit current form package to search other form package in the packagelist.\r
+      //\r
+      break;\r
+    }\r
+  }\r
+Done:\r
+  FreePool (HiiPackageList);\r
+\r
+  *BufferSize = TempSize;\r
+  *Buffer = (EFI_IFR_FORM_SET *)FormSetBuffer;\r
+\r
+  return Status;\r
+}\r
+\r
 /**\r
   Converts all hex dtring characters in range ['A'..'F'] to ['a'..'f'] for \r
   hex digits that appear between a '=' and a '&' in a config string.\r
@@ -575,17 +696,17 @@ InternalHiiBrowserCallback (
 \r
   @param[in]  Guid          Pointer to an EFI_GUID that is the routing information\r
                             GUID.  Each of the 16 bytes in Guid is converted to \r
-                            a 2 Unicode character hexidecimal string.  This is \r
+                            a 2 Unicode character hexadecimal string.  This is\r
                             an optional parameter that may be NULL.\r
   @param[in]  Name          Pointer to a Null-terminated Unicode string that is \r
                             the routing information NAME.  This is an optional \r
                             parameter that may be NULL.  Each 16-bit Unicode \r
                             character in Name is converted to a 4 character Unicode \r
-                            hexidecimal string.                        \r
+                            hexadecimal string.\r
   @param[in]  DriverHandle  The driver handle which supports a Device Path Protocol\r
                             that is the routing information PATH.  Each byte of\r
                             the Device Path associated with DriverHandle is converted\r
-                            to a 2 Unicode character hexidecimal string.\r
+                            to a 2 Unicode character hexadecimal string.\r
 \r
   @retval NULL   DriverHandle does not support the Device Path Protocol.\r
   @retval Other  A pointer to the Null-terminate Unicode <ConfigHdr> string\r
@@ -606,6 +727,7 @@ HiiConstructConfigHdr (
   CHAR16                    *ReturnString;\r
   UINTN                     Index;\r
   UINT8                     *Buffer;\r
+  UINTN                     MaxLen;\r
 \r
   //\r
   // Compute the length of Name in Unicode characters.  \r
@@ -636,7 +758,8 @@ HiiConstructConfigHdr (
   // GUID=<HexCh>32&NAME=<Char>NameLength&PATH=<HexChar>DevicePathSize <Null>\r
   // | 5 | sizeof (EFI_GUID) * 2 | 6 | NameStrLen*4 | 6 | DevicePathSize * 2 | 1 |\r
   //\r
-  String = AllocateZeroPool ((5 + sizeof (EFI_GUID) * 2 + 6 + NameLength * 4 + 6 + DevicePathSize * 2 + 1) * sizeof (CHAR16));\r
+  MaxLen = 5 + sizeof (EFI_GUID) * 2 + 6 + NameLength * 4 + 6 + DevicePathSize * 2 + 1;\r
+  String = AllocateZeroPool (MaxLen * sizeof (CHAR16));\r
   if (String == NULL) {\r
     return NULL;\r
   }\r
@@ -644,7 +767,8 @@ HiiConstructConfigHdr (
   //\r
   // Start with L"GUID="\r
   //\r
-  ReturnString = StrCpy (String, L"GUID=");\r
+  StrCpyS (String, MaxLen, L"GUID=");\r
+  ReturnString = String;\r
   String += StrLen (String);\r
 \r
   if (Guid != NULL) {\r
@@ -652,14 +776,21 @@ HiiConstructConfigHdr (
     // Append Guid converted to <HexCh>32\r
     //\r
     for (Index = 0, Buffer = (UINT8 *)Guid; Index < sizeof (EFI_GUID); Index++) {\r
-      String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *(Buffer++), 2);\r
+      UnicodeValueToStringS (\r
+        String,\r
+        MaxLen * sizeof (CHAR16) - ((UINTN)String - (UINTN)ReturnString),\r
+        PREFIX_ZERO | RADIX_HEX,\r
+        *(Buffer++),\r
+        2\r
+        );\r
+      String += StrnLenS (String, MaxLen - ((UINTN)String - (UINTN)ReturnString) / sizeof (CHAR16));\r
     }\r
   }\r
   \r
   //\r
   // Append L"&NAME="\r
   //\r
-  StrCpy (String, L"&NAME=");\r
+  StrCatS (ReturnString, MaxLen, L"&NAME=");\r
   String += StrLen (String);\r
 \r
   if (Name != NULL) {\r
@@ -667,21 +798,35 @@ HiiConstructConfigHdr (
     // Append Name converted to <Char>NameLength\r
     //\r
     for (; *Name != L'\0'; Name++) {\r
-      String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *Name, 4);\r
+      UnicodeValueToStringS (\r
+        String,\r
+        sizeof (CHAR16) * MaxLen - ((UINTN)String - (UINTN)ReturnString),\r
+        PREFIX_ZERO | RADIX_HEX,\r
+        *Name,\r
+        4\r
+        );\r
+      String += StrnLenS (String, MaxLen - ((UINTN)String - (UINTN)ReturnString) / sizeof (CHAR16));\r
     }\r
   }\r
 \r
   //\r
   // Append L"&PATH="\r
   //\r
-  StrCpy (String, L"&PATH=");\r
+  StrCatS (ReturnString, MaxLen, L"&PATH=");\r
   String += StrLen (String);\r
 \r
   //\r
   // Append the device path associated with DriverHandle converted to <HexChar>DevicePathSize\r
   //\r
   for (Index = 0, Buffer = (UINT8 *)DevicePath; Index < DevicePathSize; Index++) {\r
-    String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *(Buffer++), 2);\r
+    UnicodeValueToStringS (\r
+      String,\r
+      sizeof (CHAR16) * MaxLen - ((UINTN)String - (UINTN)ReturnString),\r
+      PREFIX_ZERO | RADIX_HEX,\r
+      *(Buffer++),\r
+      2\r
+      );\r
+    String += StrnLenS (String, MaxLen - ((UINTN)String - (UINTN)ReturnString) / sizeof (CHAR16));\r
   }\r
 \r
   //\r
@@ -786,7 +931,7 @@ InternalHiiGetBufferFromString (
     StringPtr = (CHAR16 *) DataBuffer;\r
     ZeroMem (TemStr, sizeof (TemStr));\r
     for (Index = 0; Index < Length; Index += 4) {\r
-      StrnCpy (TemStr, ConfigHdr + Index, 4);\r
+      StrnCpyS (TemStr, sizeof (TemStr) / sizeof (CHAR16), ConfigHdr + Index, 4);\r
       StringPtr[Index/4] = (CHAR16) StrHexToUint64 (TemStr);\r
     }\r
     //\r
@@ -1006,7 +1151,7 @@ ValidateQuestionFromVfr (
   UINT64                       VarValue;\r
   EFI_IFR_TYPE_VALUE           TmpValue;\r
   EFI_STATUS                   Status;\r
-  EFI_HII_PACKAGE_HEADER       PacakgeHeader;\r
+  EFI_HII_PACKAGE_HEADER       PackageHeader;\r
   UINT32                       PackageOffset;\r
   UINT8                        *PackageData;\r
   UINTN                        IfrOffset;\r
@@ -1024,6 +1169,13 @@ ValidateQuestionFromVfr (
   UINTN                        Index;\r
   CHAR16                       *QuestionName;\r
   CHAR16                       *StringPtr;\r
+  UINT16                       BitOffset;\r
+  UINT16                       BitWidth;\r
+  UINT16                       TotalBits;\r
+  UINTN                        StartBit;\r
+  UINTN                        EndBit;\r
+  BOOLEAN                      QuestionReferBitField;\r
+  UINT32                       BufferValue;\r
 \r
   //\r
   // Initialize the local variables.\r
@@ -1037,21 +1189,24 @@ ValidateQuestionFromVfr (
   IfrEfiVarStore    = NULL;\r
   ZeroMem (&VarStoreData, sizeof (IFR_VARSTORAGE_DATA));\r
   ZeroMem (&VarBlockData, sizeof (VarBlockData));\r
+  BitOffset = 0;\r
+  BitWidth = 0;\r
+  QuestionReferBitField = FALSE;\r
 \r
   //\r
   // Check IFR value is in block data, then Validate Value\r
   //\r
   PackageOffset = sizeof (EFI_HII_PACKAGE_LIST_HEADER);\r
   while (PackageOffset < PackageListLength) {\r
-    CopyMem (&PacakgeHeader, (UINT8 *) HiiPackageList + PackageOffset, sizeof (PacakgeHeader));\r
+    CopyMem (&PackageHeader, (UINT8 *) HiiPackageList + PackageOffset, sizeof (PackageHeader));\r
 \r
     //\r
     // Parse IFR opcode from the form package.\r
     //\r
-    if (PacakgeHeader.Type == EFI_HII_PACKAGE_FORMS) {\r
-      IfrOffset   = sizeof (PacakgeHeader);\r
+    if (PackageHeader.Type == EFI_HII_PACKAGE_FORMS) {\r
+      IfrOffset   = sizeof (PackageHeader);\r
       PackageData = (UINT8 *) HiiPackageList + PackageOffset;\r
-      while (IfrOffset < PacakgeHeader.Length) {\r
+      while (IfrOffset < PackageHeader.Length) {\r
         IfrOpHdr = (EFI_IFR_OP_HEADER *) (PackageData + IfrOffset);\r
         //\r
         // Validate current setting to the value built in IFR opcode\r
@@ -1200,8 +1355,19 @@ ValidateQuestionFromVfr (
             //\r
             // Get Offset by Question header and Width by DataType Flags\r
             //\r
-            Offset = IfrOneOf->Question.VarStoreInfo.VarOffset;\r
-            Width  = (UINT16) (1 << (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE));\r
+            if (QuestionReferBitField) {\r
+              //\r
+              // Get the byte offset/width for bit field.\r
+              //\r
+              BitOffset = IfrOneOf->Question.VarStoreInfo.VarOffset;\r
+              BitWidth = IfrOneOf->Flags & EDKII_IFR_NUMERIC_SIZE_BIT;\r
+              Offset = BitOffset / 8;\r
+              TotalBits = BitOffset % 8 + BitWidth;\r
+              Width = (TotalBits % 8 == 0 ? TotalBits / 8: TotalBits / 8 + 1);\r
+            } else {\r
+              Offset = IfrOneOf->Question.VarStoreInfo.VarOffset;\r
+              Width  = (UINT16) (1 << (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE));\r
+            }\r
             //\r
             // Check whether this question is in current block array.\r
             //\r
@@ -1225,7 +1391,17 @@ ValidateQuestionFromVfr (
             // Get the current value for oneof opcode\r
             //\r
             VarValue = 0;\r
-            CopyMem (&VarValue, VarBuffer +  Offset, Width);\r
+            if (QuestionReferBitField) {\r
+              //\r
+              // Get the value in bit fields.\r
+              //\r
+              StartBit = BitOffset % 8;\r
+              EndBit = StartBit + BitWidth - 1;\r
+              CopyMem ((UINT8 *) &BufferValue, VarBuffer + Offset, Width);\r
+              VarValue = BitFieldRead32 (BufferValue, StartBit, EndBit);\r
+            } else {\r
+              CopyMem (&VarValue, VarBuffer +  Offset, Width);\r
+            }\r
           }\r
           //\r
           // Set Block Data, to be checked in the following Oneof option opcode.\r
@@ -1271,8 +1447,19 @@ ValidateQuestionFromVfr (
             //\r
             // Get Offset by Question header and Width by DataType Flags\r
             //\r
-            Offset = IfrNumeric->Question.VarStoreInfo.VarOffset;\r
-            Width  = (UINT16) (1 << (IfrNumeric->Flags & EFI_IFR_NUMERIC_SIZE));\r
+            if (QuestionReferBitField) {\r
+              //\r
+              // Get the byte offset/width for bit field.\r
+              //\r
+              BitOffset = IfrNumeric->Question.VarStoreInfo.VarOffset;\r
+              BitWidth = IfrNumeric->Flags & EDKII_IFR_NUMERIC_SIZE_BIT;\r
+              Offset = BitOffset / 8;\r
+              TotalBits = BitOffset % 8 + BitWidth;\r
+              Width  = (TotalBits % 8 == 0 ? TotalBits / 8: TotalBits / 8 + 1);\r
+            } else {\r
+              Offset = IfrNumeric->Question.VarStoreInfo.VarOffset;\r
+              Width  = (UINT16) (1 << (IfrNumeric->Flags & EFI_IFR_NUMERIC_SIZE));\r
+            }\r
             //\r
             // Check whether this question is in current block array.\r
             //\r
@@ -1296,43 +1483,110 @@ ValidateQuestionFromVfr (
             // Check the current value is in the numeric range.\r
             //\r
             VarValue = 0;\r
-            CopyMem (&VarValue, VarBuffer +  Offset, Width);\r
-          }\r
-          switch (IfrNumeric->Flags & EFI_IFR_NUMERIC_SIZE) {\r
-          case EFI_IFR_NUMERIC_SIZE_1:\r
-            if ((UINT8) VarValue < IfrNumeric->data.u8.MinValue || (UINT8) VarValue > IfrNumeric->data.u8.MaxValue) {\r
-              //\r
-              // Not in the valid range.\r
-              //\r
-              return EFI_INVALID_PARAMETER;\r
-            }\r
-            break;\r
-          case EFI_IFR_NUMERIC_SIZE_2:\r
-            if ((UINT16) VarValue < IfrNumeric->data.u16.MinValue || (UINT16) VarValue > IfrNumeric->data.u16.MaxValue) {\r
-              //\r
-              // Not in the valid range.\r
+            if (QuestionReferBitField) {\r
               //\r
-              return EFI_INVALID_PARAMETER;\r
-            }\r
-            break;\r
-          case EFI_IFR_NUMERIC_SIZE_4:\r
-            if ((UINT32) VarValue < IfrNumeric->data.u32.MinValue || (UINT32) VarValue > IfrNumeric->data.u32.MaxValue) {\r
-              //\r
-              // Not in the valid range.\r
+              // Get the value in the bit fields.\r
               //\r
-              return EFI_INVALID_PARAMETER;\r
+              StartBit = BitOffset % 8;\r
+              EndBit = StartBit + BitWidth - 1;\r
+              CopyMem ((UINT8 *) &BufferValue, VarBuffer + Offset, Width);\r
+              VarValue = BitFieldRead32 (BufferValue, StartBit, EndBit);\r
+            } else {\r
+              CopyMem (&VarValue, VarBuffer +  Offset, Width);\r
             }\r
-            break;\r
-          case EFI_IFR_NUMERIC_SIZE_8:\r
-            if ((UINT64) VarValue < IfrNumeric->data.u64.MinValue || (UINT64) VarValue > IfrNumeric->data.u64.MaxValue) {\r
-              //\r
-              // Not in the valid range.\r
-              //\r
-              return EFI_INVALID_PARAMETER;\r
+          }\r
+          if ( QuestionReferBitField) {\r
+             //\r
+             // Value in bit fields was stored as UINt32 type.\r
+             //\r
+             if ((IfrNumeric->Flags & EDKII_IFR_DISPLAY_BIT) == 0) {\r
+               if ((INT32) VarValue < (INT32) IfrNumeric->data.u32.MinValue || (INT32) VarValue > (INT32) IfrNumeric->data.u32.MaxValue) {\r
+                  //\r
+                  // Not in the valid range.\r
+                  //\r
+                  return EFI_INVALID_PARAMETER;\r
+                }\r
+             } else {\r
+               if (VarValue < IfrNumeric->data.u32.MinValue || VarValue > IfrNumeric->data.u32.MaxValue) {\r
+                  //\r
+                  // Not in the valid range.\r
+                  //\r
+                  return EFI_INVALID_PARAMETER;\r
+                }\r
+             }\r
+          } else {\r
+            if ((IfrNumeric->Flags & EFI_IFR_DISPLAY) == 0) {\r
+              switch (IfrNumeric->Flags & EFI_IFR_NUMERIC_SIZE) {\r
+              case EFI_IFR_NUMERIC_SIZE_1:\r
+                if ((INT8) VarValue < (INT8) IfrNumeric->data.u8.MinValue || (INT8) VarValue > (INT8) IfrNumeric->data.u8.MaxValue) {\r
+                  //\r
+                  // Not in the valid range.\r
+                  //\r
+                  return EFI_INVALID_PARAMETER;\r
+                }\r
+                break;\r
+              case EFI_IFR_NUMERIC_SIZE_2:\r
+                if ((INT16) VarValue < (INT16) IfrNumeric->data.u16.MinValue || (INT16) VarValue > (INT16) IfrNumeric->data.u16.MaxValue) {\r
+                  //\r
+                  // Not in the valid range.\r
+                  //\r
+                  return EFI_INVALID_PARAMETER;\r
+                }\r
+                break;\r
+              case EFI_IFR_NUMERIC_SIZE_4:\r
+                if ((INT32) VarValue < (INT32) IfrNumeric->data.u32.MinValue || (INT32) VarValue > (INT32) IfrNumeric->data.u32.MaxValue) {\r
+                  //\r
+                  // Not in the valid range.\r
+                  //\r
+                  return EFI_INVALID_PARAMETER;\r
+                }\r
+                break;\r
+              case EFI_IFR_NUMERIC_SIZE_8:\r
+                if ((INT64) VarValue < (INT64) IfrNumeric->data.u64.MinValue || (INT64) VarValue > (INT64) IfrNumeric->data.u64.MaxValue) {\r
+                  //\r
+                  // Not in the valid range.\r
+                  //\r
+                  return EFI_INVALID_PARAMETER;\r
+                }\r
+                break;\r
+              }\r
+            } else {\r
+              switch (IfrNumeric->Flags & EFI_IFR_NUMERIC_SIZE) {\r
+              case EFI_IFR_NUMERIC_SIZE_1:\r
+                if ((UINT8) VarValue < IfrNumeric->data.u8.MinValue || (UINT8) VarValue > IfrNumeric->data.u8.MaxValue) {\r
+                  //\r
+                  // Not in the valid range.\r
+                  //\r
+                  return EFI_INVALID_PARAMETER;\r
+                }\r
+                break;\r
+              case EFI_IFR_NUMERIC_SIZE_2:\r
+                if ((UINT16) VarValue < IfrNumeric->data.u16.MinValue || (UINT16) VarValue > IfrNumeric->data.u16.MaxValue) {\r
+                  //\r
+                  // Not in the valid range.\r
+                  //\r
+                  return EFI_INVALID_PARAMETER;\r
+                }\r
+                break;\r
+              case EFI_IFR_NUMERIC_SIZE_4:\r
+                if ((UINT32) VarValue < IfrNumeric->data.u32.MinValue || (UINT32) VarValue > IfrNumeric->data.u32.MaxValue) {\r
+                  //\r
+                  // Not in the valid range.\r
+                  //\r
+                  return EFI_INVALID_PARAMETER;\r
+                }\r
+                break;\r
+              case EFI_IFR_NUMERIC_SIZE_8:\r
+                if ((UINT64) VarValue < IfrNumeric->data.u64.MinValue || (UINT64) VarValue > IfrNumeric->data.u64.MaxValue) {\r
+                  //\r
+                  // Not in the valid range.\r
+                  //\r
+                  return EFI_INVALID_PARAMETER;\r
+                }\r
+                break;\r
+              }\r
             }\r
-            break;\r
           }\r
-\r
           break;\r
         case EFI_IFR_CHECKBOX_OP:\r
           //\r
@@ -1373,8 +1627,19 @@ ValidateQuestionFromVfr (
             //\r
             // Get Offset by Question header\r
             //\r
-            Offset = IfrCheckBox->Question.VarStoreInfo.VarOffset;\r
-            Width  = (UINT16) sizeof (BOOLEAN);\r
+           if (QuestionReferBitField) {\r
+              //\r
+              // Get the byte offset/width for bit field.\r
+              //\r
+              BitOffset = IfrCheckBox->Question.VarStoreInfo.VarOffset;\r
+              BitWidth = 1;\r
+              Offset = BitOffset / 8;\r
+              TotalBits = BitOffset % 8 + BitWidth;\r
+              Width = (TotalBits % 8 == 0 ? TotalBits / 8: TotalBits / 8 + 1);\r
+            } else {\r
+              Offset = IfrCheckBox->Question.VarStoreInfo.VarOffset;\r
+              Width  = (UINT16) sizeof (BOOLEAN);\r
+            }\r
             //\r
             // Check whether this question is in current block array.\r
             //\r
@@ -1397,7 +1662,17 @@ ValidateQuestionFromVfr (
             // Check the current value is in the numeric range.\r
             //\r
             VarValue = 0;\r
-            CopyMem (&VarValue, VarBuffer +  Offset, Width);\r
+            if (QuestionReferBitField) {\r
+              //\r
+              // Get the value in bit fields.\r
+              //\r
+              StartBit = BitOffset % 8;\r
+              EndBit = StartBit + BitWidth - 1;\r
+              CopyMem ((UINT8 *) &BufferValue, VarBuffer + Offset, Width);\r
+              VarValue = BitFieldRead32 (BufferValue, StartBit, EndBit);\r
+            } else {\r
+              CopyMem (&VarValue, VarBuffer +  Offset, Width);\r
+            }\r
           }\r
           //\r
           // Boolean type, only 1 and 0 is valid.\r
@@ -1426,7 +1701,7 @@ ValidateQuestionFromVfr (
             break;\r
           }\r
           //\r
-          // Get Width by OneOf Flags\r
+          // Get the Max size of the string.\r
           //\r
           Width  = (UINT16) (IfrString->MaxSize * sizeof (UINT16));\r
           if (NameValueType) {\r
@@ -1440,6 +1715,10 @@ ValidateQuestionFromVfr (
               //\r
               break;\r
             }\r
+            //\r
+            // Skip the VarName.\r
+            //\r
+            StringPtr += StrLen (QuestionName);\r
 \r
             //\r
             // Skip the "=".\r
@@ -1448,8 +1727,13 @@ ValidateQuestionFromVfr (
             \r
             //\r
             // Check current string length is less than maxsize\r
+            // e.g Config String: "0041004200430044", Unicode String: "ABCD". Unicode String length = Config String length / 4.\r
+            // Config string format in UEFI spec.\r
+            // <NvConfig> ::= <Label>'='<String>\r
+            // <String> ::= [<Char>]+\r
+            // <Char> ::= <HexCh>4\r
             //\r
-            if (StrSize (StringPtr) > Width) {\r
+            if (StrLen (StringPtr) / 4 > IfrString->MaxSize) {\r
               return EFI_INVALID_PARAMETER;\r
             }\r
           } else {\r
@@ -1479,7 +1763,7 @@ ValidateQuestionFromVfr (
             //\r
             // Check current string length is less than maxsize\r
             //\r
-            if (StrSize ((CHAR16 *) (VarBuffer + Offset)) > Width) {\r
+            if (StrLen ((CHAR16 *) (VarBuffer + Offset)) > IfrString->MaxSize) {\r
               return EFI_INVALID_PARAMETER;\r
             }\r
           }\r
@@ -1513,6 +1797,7 @@ ValidateQuestionFromVfr (
           }\r
           break;\r
         case EFI_IFR_END_OP:\r
+          QuestionReferBitField = FALSE;\r
           //\r
           // Decrease opcode scope for the validated opcode\r
           //\r
@@ -1527,6 +1812,11 @@ ValidateQuestionFromVfr (
             return EFI_INVALID_PARAMETER;\r
           }\r
           break;\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
         default:\r
           //\r
           // Increase Scope for the validated opcode\r
@@ -1550,7 +1840,7 @@ ValidateQuestionFromVfr (
     //\r
     // Go to next package.\r
     //\r
-    PackageOffset += PacakgeHeader.Length;\r
+    PackageOffset += PackageHeader.Length;\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -1693,7 +1983,7 @@ GetBlockDataInfo (
     //\r
     // Check whether VarBuffer is enough\r
     //\r
-    if ((UINTN) (Offset + Width) > MaxBufferSize) {\r
+    if ((UINT32)Offset + Width > MaxBufferSize) {\r
       DataBuffer = ReallocatePool (\r
                     MaxBufferSize,\r
                     Offset + Width + HII_LIB_DEFAULT_VARSTORE_SIZE,\r
@@ -1947,7 +2237,7 @@ GetElementsFromRequest (
   @param DefaultId  Specifies the type of defaults to retrieve only for setting default action.\r
   @param ActionType Action supports setting defaults and validate current setting.\r
   \r
-  @retval TURE    Action runs successfully.\r
+  @retval TRUE    Action runs successfully.\r
   @retval FALSE   Action is not valid or Action can't be executed successfully..\r
 **/\r
 BOOLEAN\r
@@ -1975,6 +2265,7 @@ InternalHiiIfrValueAction (
 \r
   EFI_HII_PACKAGE_LIST_HEADER  *HiiPackageList;\r
   UINTN                        PackageListLength;\r
+  UINTN                        MaxLen;\r
   EFI_DEVICE_PATH_PROTOCOL     *DevicePath;\r
   EFI_DEVICE_PATH_PROTOCOL     *TempDevicePath;\r
 \r
@@ -2019,8 +2310,9 @@ InternalHiiIfrValueAction (
   }\r
   \r
   StringPtr = ConfigAltResp;\r
-  \r
-  while (StringPtr != L'\0') {\r
+  ASSERT (StringPtr != NULL);\r
+\r
+  while (*StringPtr != L'\0') {\r
     //\r
     // 1. Find <ConfigHdr> GUID=...&NAME=...&PATH=...\r
     //\r
@@ -2230,14 +2522,15 @@ NextConfigAltResp:
     // Construct ConfigAltHdr string  "&<ConfigHdr>&ALTCFG=\0" \r
     //                               | 1 | StrLen (ConfigHdr) | 8 | 1 |\r
     //\r
-    ConfigAltHdr = AllocateZeroPool ((1 + StringPtr - StringHdr + 8 + 1) * sizeof (CHAR16));\r
+    MaxLen = 1 + StringPtr - StringHdr + 8 + 1;\r
+    ConfigAltHdr = AllocateZeroPool ( MaxLen * sizeof (CHAR16));\r
     if (ConfigAltHdr == NULL) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
       goto Done;\r
     }\r
-    StrCpy (ConfigAltHdr, L"&");\r
-    StrnCat (ConfigAltHdr, StringHdr, StringPtr - StringHdr);\r
-    StrCat (ConfigAltHdr, L"&ALTCFG=");\r
+    StrCpyS (ConfigAltHdr, MaxLen, L"&");\r
+    StrnCatS (ConfigAltHdr, MaxLen, StringHdr, StringPtr - StringHdr);\r
+    StrCatS (ConfigAltHdr, MaxLen, L"&ALTCFG=");\r
     \r
     //\r
     // Skip all AltResp (AltConfigHdr ConfigBody) for the same ConfigHdr\r
@@ -2339,7 +2632,7 @@ HiiValidateSettings (
                     entirety of the current HII database will be reset.\r
   @param DefaultId  Specifies the type of defaults to retrieve.\r
   \r
-  @retval TURE    The default value is set successfully.\r
+  @retval TRUE    The default value is set successfully.\r
   @retval FALSE   The default value can't be found and set.\r
 **/\r
 BOOLEAN\r
@@ -3284,7 +3577,8 @@ HiiCreateGotoExOpCode (
   @param[in]  OpCodeHandle          Handle to the buffer of opcodes.\r
   @param[in]  QuestionId            Question ID\r
   @param[in]  VarStoreId            Storage ID\r
-  @param[in]  VarOffset             Offset in Storage\r
+  @param[in]  VarOffset             Offset in Storage or String ID of the name (VarName)\r
+                                    for this name/value pair.\r
   @param[in]  Prompt                String ID for Prompt\r
   @param[in]  Help                  String ID for Help\r
   @param[in]  QuestionFlags         Flags in Question Header\r
@@ -3345,7 +3639,8 @@ HiiCreateCheckBoxOpCode (
   @param[in]  OpCodeHandle          Handle to the buffer of opcodes.\r
   @param[in]  QuestionId            Question ID\r
   @param[in]  VarStoreId            Storage ID\r
-  @param[in]  VarOffset             Offset in Storage\r
+  @param[in]  VarOffset             Offset in Storage or String ID of the name (VarName)\r
+                                    for this name/value pair.\r
   @param[in]  Prompt                String ID for Prompt\r
   @param[in]  Help                  String ID for Help\r
   @param[in]  QuestionFlags         Flags in Question Header\r
@@ -3446,7 +3741,8 @@ HiiCreateNumericOpCode (
   @param[in]  OpCodeHandle          Handle to the buffer of opcodes.\r
   @param[in]  QuestionId            Question ID\r
   @param[in]  VarStoreId            Storage ID\r
-  @param[in]  VarOffset             Offset in Storage\r
+  @param[in]  VarOffset             Offset in Storage or String ID of the name (VarName)\r
+                                    for this name/value pair.\r
   @param[in]  Prompt                String ID for Prompt\r
   @param[in]  Help                  String ID for Help\r
   @param[in]  QuestionFlags         Flags in Question Header\r
@@ -3513,7 +3809,8 @@ HiiCreateStringOpCode (
   @param[in]  OpCodeHandle          Handle to the buffer of opcodes.\r
   @param[in]  QuestionId            Question ID\r
   @param[in]  VarStoreId            Storage ID\r
-  @param[in]  VarOffset             Offset in Storage\r
+  @param[in]  VarOffset             Offset in Storage or String ID of the name (VarName)\r
+                                    for this name/value pair.\r
   @param[in]  Prompt                String ID for Prompt\r
   @param[in]  Help                  String ID for Help\r
   @param[in]  QuestionFlags         Flags in Question Header\r
@@ -3580,7 +3877,8 @@ HiiCreateOneOfOpCode (
   @param[in]  OpCodeHandle          Handle to the buffer of opcodes.\r
   @param[in]  QuestionId            Question ID\r
   @param[in]  VarStoreId            Storage ID\r
-  @param[in]  VarOffset             Offset in Storage\r
+  @param[in]  VarOffset             Offset in Storage or String ID of the name (VarName)\r
+                                    for this name/value pair.\r
   @param[in]  Prompt                String ID for Prompt\r
   @param[in]  Help                  String ID for Help\r
   @param[in]  QuestionFlags         Flags in Question Header\r
@@ -3682,7 +3980,8 @@ HiiCreateTextOpCode (
   @param[in]  QuestionId            Question ID\r
   @param[in]  VarStoreId            Storage ID, optional. If DateFlags is not\r
                                     QF_DATE_STORAGE_NORMAL, this parameter is ignored.\r
-  @param[in]  VarOffset             Offset in Storage, optional. If DateFlags is not\r
+  @param[in]  VarOffset             Offset in Storage or String ID of the name (VarName)\r
+                                    for this name/value pair, optional. If DateFlags is not\r
                                     QF_DATE_STORAGE_NORMAL, this parameter is ignored.\r
   @param[in]  Prompt                String ID for Prompt\r
   @param[in]  Help                  String ID for Help\r
@@ -3746,7 +4045,8 @@ HiiCreateDateOpCode (
   @param[in]  QuestionId            Question ID\r
   @param[in]  VarStoreId            Storage ID, optional. If TimeFlags is not\r
                                     QF_TIME_STORAGE_NORMAL, this parameter is ignored.\r
-  @param[in]  VarOffset             Offset in Storage, optional. If TimeFlags is not\r
+  @param[in]  VarOffset             Offset in Storage or String ID of the name (VarName)\r
+                                    for this name/value pair, optional. If TimeFlags is not\r
                                     QF_TIME_STORAGE_NORMAL, this parameter is ignored.\r
   @param[in]  Prompt                String ID for Prompt\r
   @param[in]  Help                  String ID for Help\r
@@ -4037,7 +4337,7 @@ HiiUpdateForm (
   UINTN                        BufferSize;\r
   UINT8                        *UpdateBufferPos;\r
   EFI_HII_PACKAGE_HEADER       *Package;\r
-  EFI_HII_PACKAGE_HEADER       *TempPacakge;\r
+  EFI_HII_PACKAGE_HEADER       *TempPackage;\r
   EFI_HII_PACKAGE_HEADER       PackageHeader;\r
   BOOLEAN                      Updated;\r
   HII_LIB_OPCODE_BUFFER        *OpCodeBufferStart;\r
@@ -4049,7 +4349,7 @@ HiiUpdateForm (
   ASSERT (HiiHandle != NULL);\r
   ASSERT (StartOpCodeHandle != NULL);\r
   UpdatePackageList = NULL;\r
-  TempPacakge       = NULL;\r
+  TempPackage       = NULL;\r
   HiiPackageList    = NULL;\r
   \r
   //\r
@@ -4095,8 +4395,8 @@ HiiUpdateForm (
   //\r
   // Allocate temp buffer to store the temp updated package buffer\r
   //\r
-  TempPacakge = AllocateZeroPool (BufferSize);\r
-  if (TempPacakge == NULL) {\r
+  TempPackage = AllocateZeroPool (BufferSize);\r
+  if (TempPackage == NULL) {\r
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Finish;\r
   }\r
@@ -4124,7 +4424,7 @@ HiiUpdateForm (
       //\r
       // Check this package is the matched package.\r
       //\r
-      Status = InternalHiiUpdateFormPackageData (FormSetGuid, FormId, Package, OpCodeBufferStart, OpCodeBufferEnd, TempPacakge);\r
+      Status = InternalHiiUpdateFormPackageData (FormSetGuid, FormId, Package, OpCodeBufferStart, OpCodeBufferEnd, TempPackage);\r
       //\r
       // The matched package is found. Its package buffer will be updated by the input new data.\r
       //\r
@@ -4136,7 +4436,7 @@ HiiUpdateForm (
         //\r
         // Add updated package buffer\r
         //\r
-        Package = TempPacakge;\r
+        Package = TempPackage;\r
       }\r
     }\r
 \r
@@ -4175,8 +4475,8 @@ Finish:
     FreePool (UpdatePackageList);\r
   }\r
   \r
-  if (TempPacakge != NULL) {\r
-    FreePool (TempPacakge);\r
+  if (TempPackage != NULL) {\r
+    FreePool (TempPackage);\r
   }\r
 \r
   return Status; \r