]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c
MdeModulePkg/HiiDatabase: Handle questions with Bit VarStore
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / ConfigKeywordHandler.c
index 5e7aca96816e4d8fce42dcbe30b86f4f84c19090..1b48c1cebe6e281ac0ab349ccf418a158d646d50 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 Implementation of interfaces function for EFI_CONFIG_KEYWORD_HANDLER_PROTOCOL.\r
 \r
-Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2015 - 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
@@ -28,7 +28,7 @@ extern HII_DATABASE_PRIVATE_DATA mPrivate;
   @param  NextString             string follow the possible PathHdr string.\r
 \r
   @retval EFI_INVALID_PARAMETER  The device path is not valid or the 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 binary format.\r
                                  The Input string not include PathHdr section.\r
 \r
@@ -178,6 +178,7 @@ ExtractNameSpace (
   )\r
 {\r
   CHAR16    *TmpPtr;\r
+  UINTN     NameSpaceSize;\r
 \r
   ASSERT (NameSpace != NULL);\r
 \r
@@ -218,11 +219,12 @@ ExtractNameSpace (
   // Input NameSpace is unicode string. The language in String package is ascii string.\r
   // Here will convert the unicode string to ascii and save it.\r
   //\r
-  *NameSpace = AllocatePool (StrLen (String) + 1);\r
+  NameSpaceSize = StrLen (String) + 1;\r
+  *NameSpace = AllocatePool (NameSpaceSize);\r
   if (*NameSpace == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  UnicodeStrToAsciiStr (String, *NameSpace);\r
+  UnicodeStrToAsciiStrS (String, *NameSpace, NameSpaceSize);\r
 \r
   if (TmpPtr != NULL) {\r
     *TmpPtr = L'&'; \r
@@ -238,10 +240,10 @@ ExtractNameSpace (
 \r
   @param  String                 KeywordRequestformat string.\r
   @param  Keyword                return the extract keyword string.\r
-  @param  NextString             return the next string follow this keyword sectin.\r
+  @param  NextString             return the next string follow this keyword section.\r
 \r
   @retval EFI_SUCCESS            Success to get the keyword string.\r
-  @retval EFI_INVALID_PARAMETER  Parsr the input string return error.\r
+  @retval EFI_INVALID_PARAMETER  Parse the input string return error.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -304,10 +306,10 @@ ExtractKeyword (
 \r
   @param  String                 KeywordRequestformat string.\r
   @param  Value                  return the extract value string.\r
-  @param  NextString             return the next string follow this keyword sectin.\r
+  @param  NextString             return the next string follow this keyword section.\r
 \r
   @retval EFI_SUCCESS            Success to get the keyword string.\r
-  @retval EFI_INVALID_PARAMETER  Parsr the input string return error.\r
+  @retval EFI_INVALID_PARAMETER  Parse the input string return error.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -359,10 +361,10 @@ ExtractValue (
 \r
   @param  String                 KeywordRequestformat string.\r
   @param  FilterFlags            return the filter condition.\r
-  @param  NextString             return the next string follow this keyword sectin.\r
+  @param  NextString             return the next string follow this keyword section.\r
 \r
   @retval EFI_SUCCESS            Success to get the keyword string.\r
-  @retval EFI_INVALID_PARAMETER  Parsr the input string return error.\r
+  @retval EFI_INVALID_PARAMETER  Parse the input string return error.\r
 \r
 **/\r
 BOOLEAN\r
@@ -475,7 +477,7 @@ ExtractFilter (
         String = KeywordPtr;\r
       } else {\r
         //\r
-        // Only has paltform defined filter section, just skip it.\r
+        // Only has platform defined filter section, just skip it.\r
         //\r
         String += StrLen (String);\r
       }\r
@@ -518,9 +520,9 @@ ExtractReadOnlyFromOpCode (
 \r
   This is a internal function.\r
 \r
-  @param  OpCodeData             The questin binary ifr data.\r
+  @param  OpCodeData             The question binary ifr data.\r
   @param  KeywordRequest         KeywordRequestformat string.\r
-  @param  NextString             return the next string follow this keyword sectin.\r
+  @param  NextString             return the next string follow this keyword section.\r
   @param  ReadOnly               Return whether this question is read only.\r
 \r
   @retval KEYWORD_HANDLER_NO_ERROR                     Success validate.\r
@@ -709,7 +711,7 @@ GetRecordFromDevicePath (
   @param  BufferSize             Length of the buffer.\r
   @param  StringDest             Buffer to store the string text. \r
 \r
-  @retval EFI_SUCCESS            The string text was outputed successfully.\r
+  @retval EFI_SUCCESS            The string text was outputted successfully.\r
   @retval EFI_OUT_OF_RESOURCES   Out of resource.\r
 \r
 **/\r
@@ -779,6 +781,7 @@ GetStringIdFromString (
   UINTN                                StringSize;\r
   CHAR16                               *String;\r
   CHAR8                                *AsciiKeywordValue;\r
+  UINTN                                KeywordValueSize;\r
   EFI_STATUS                           Status;\r
 \r
   ASSERT (StringPackage != NULL && KeywordValue != NULL && StringId != NULL);\r
@@ -794,11 +797,12 @@ GetStringIdFromString (
   //\r
   // Make a ascii keyword value for later use.\r
   //\r
-  AsciiKeywordValue = AllocatePool (StrLen (KeywordValue) + 1);\r
+  KeywordValueSize = StrLen (KeywordValue) + 1;\r
+  AsciiKeywordValue = AllocatePool (KeywordValueSize);\r
   if (AsciiKeywordValue == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  UnicodeStrToAsciiStr(KeywordValue, AsciiKeywordValue);\r
+  UnicodeStrToAsciiStrS (KeywordValue, AsciiKeywordValue, KeywordValueSize);\r
 \r
   while (*BlockHdr != EFI_HII_SIBT_END) {\r
     switch (*BlockHdr) {\r
@@ -871,7 +875,7 @@ GetStringIdFromString (
       if (EFI_ERROR (Status)) {\r
         goto Done;\r
       }\r
-      \r
+      ASSERT (String != NULL);\r
       if (StrCmp(KeywordValue, String) == 0) {\r
         *StringId = CurrentStringId;\r
         goto Done;\r
@@ -891,7 +895,7 @@ GetStringIdFromString (
       if (EFI_ERROR (Status)) {\r
         goto Done;\r
       }\r
-      \r
+      ASSERT (String != NULL);\r
       if (StrCmp(KeywordValue, String) == 0) {\r
         *StringId = CurrentStringId;\r
         goto Done;\r
@@ -910,7 +914,7 @@ GetStringIdFromString (
         if (EFI_ERROR (Status)) {\r
           goto Done;\r
         }\r
-\r
+        ASSERT (String != NULL);\r
         BlockSize += StringSize;\r
         if (StrCmp(KeywordValue, String) == 0) {\r
           *StringId = CurrentStringId;\r
@@ -935,7 +939,7 @@ GetStringIdFromString (
         if (EFI_ERROR (Status)) {\r
           goto Done;\r
         }\r
-\r
+        ASSERT (String != NULL);\r
         BlockSize += StringSize;\r
         if (StrCmp(KeywordValue, String) == 0) {\r
           *StringId = CurrentStringId;\r
@@ -1065,11 +1069,12 @@ GetNextStringId (
       StringTextPtr = BlockHdr + Offset;\r
       \r
       if (FindString) {\r
-        *KeywordValue = AllocatePool (AsciiStrSize ((CHAR8 *) StringTextPtr) * sizeof (CHAR16));\r
+        StringSize = AsciiStrSize ((CHAR8 *) StringTextPtr);\r
+        *KeywordValue = AllocatePool (StringSize * sizeof (CHAR16));\r
         if (*KeywordValue == NULL) {\r
           return 0;\r
         }\r
-        AsciiStrToUnicodeStr ((CHAR8 *) StringTextPtr, *KeywordValue);\r
+        AsciiStrToUnicodeStrS ((CHAR8 *) StringTextPtr, *KeywordValue, StringSize);\r
         return CurrentStringId;\r
       } else if (CurrentStringId == StringId) {\r
         FindString = TRUE;\r
@@ -1084,11 +1089,12 @@ GetNextStringId (
       StringTextPtr = BlockHdr + Offset;\r
       \r
       if (FindString) {\r
-        *KeywordValue = AllocatePool (AsciiStrSize ((CHAR8 *) StringTextPtr) * sizeof (CHAR16));\r
+        StringSize = AsciiStrSize ((CHAR8 *) StringTextPtr);\r
+        *KeywordValue = AllocatePool (StringSize * sizeof (CHAR16));\r
         if (*KeywordValue == NULL) {\r
           return 0;\r
         }\r
-        AsciiStrToUnicodeStr ((CHAR8 *) StringTextPtr, *KeywordValue);\r
+        AsciiStrToUnicodeStrS ((CHAR8 *) StringTextPtr, *KeywordValue, StringSize);\r
         return CurrentStringId;\r
       } else if (CurrentStringId == StringId) {\r
         FindString = TRUE;\r
@@ -1105,11 +1111,12 @@ GetNextStringId (
 \r
       for (Index = 0; Index < StringCount; Index++) {\r
         if (FindString) {\r
-          *KeywordValue = AllocatePool (AsciiStrSize ((CHAR8 *) StringTextPtr) * sizeof (CHAR16));\r
+          StringSize = AsciiStrSize ((CHAR8 *) StringTextPtr);\r
+          *KeywordValue = AllocatePool (StringSize * sizeof (CHAR16));\r
           if (*KeywordValue == NULL) {\r
             return 0;\r
           }\r
-          AsciiStrToUnicodeStr ((CHAR8 *) StringTextPtr, *KeywordValue);\r
+          AsciiStrToUnicodeStrS ((CHAR8 *) StringTextPtr, *KeywordValue, StringSize);\r
           return CurrentStringId;\r
         } else if (CurrentStringId == StringId) {\r
           FindString = TRUE;\r
@@ -1132,11 +1139,12 @@ GetNextStringId (
 \r
       for (Index = 0; Index < StringCount; Index++) {\r
         if (FindString) {\r
-          *KeywordValue = AllocatePool (AsciiStrSize ((CHAR8 *) StringTextPtr) * sizeof (CHAR16));\r
+          StringSize = AsciiStrSize ((CHAR8 *) StringTextPtr);\r
+          *KeywordValue = AllocatePool (StringSize * sizeof (CHAR16));\r
           if (*KeywordValue == NULL) {\r
             return 0;\r
           }\r
-          AsciiStrToUnicodeStr ((CHAR8 *) StringTextPtr, *KeywordValue);\r
+          AsciiStrToUnicodeStrS ((CHAR8 *) StringTextPtr, *KeywordValue, StringSize);\r
           return CurrentStringId;\r
         } else if (CurrentStringId == StringId) {\r
           FindString = TRUE;\r
@@ -1302,7 +1310,7 @@ GetNextStringId (
   @param  KeywordValue                   Keyword value.\r
   @param  StringId                       String Id for this keyword.\r
 \r
-  @retval KEYWORD_HANDLER_NO_ERROR                     Get String id succes.\r
+  @retval KEYWORD_HANDLER_NO_ERROR                     Get String id successfully.\r
   @retval KEYWORD_HANDLER_KEYWORD_NOT_FOUND            Not found the string id in the string package.\r
   @retval KEYWORD_HANDLER_NAMESPACE_ID_NOT_FOUND       Not found the string package for this namespace.\r
   @retval KEYWORD_HANDLER_UNDEFINED_PROCESSING_ERROR   Out of resource error.\r
@@ -1601,7 +1609,7 @@ GetWidth (
 }\r
 \r
 /**\r
-  Converts all hex dtring characters in range ['A'..'F'] to ['a'..'f'] for \r
+  Converts all hex string characters in range ['A'..'F'] to ['a'..'f'] for\r
   hex digits that appear between a '=' and a '&' in a config string.\r
 \r
   If ConfigString is NULL, then ASSERT().\r
@@ -1649,7 +1657,7 @@ InternalLowerConfigString (
   @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
@@ -1670,6 +1678,7 @@ ConstructConfigHdr (
   UINT8                     *Buffer;\r
   CHAR16                    *Name;\r
   CHAR8                     *AsciiName;\r
+  UINTN                     NameSize;\r
   EFI_GUID                  *Guid;\r
   UINTN                     MaxLen;\r
 \r
@@ -1699,9 +1708,10 @@ ConstructConfigHdr (
   }\r
 \r
   if (AsciiName != NULL) {\r
-    Name = AllocateZeroPool (AsciiStrSize (AsciiName) * 2);\r
+    NameSize = AsciiStrSize (AsciiName);\r
+    Name = AllocateZeroPool (NameSize * sizeof (CHAR16));\r
     ASSERT (Name != NULL);\r
-    AsciiStrToUnicodeStr(AsciiName, Name);\r
+    AsciiStrToUnicodeStrS (AsciiName, Name, NameSize);\r
   } else {\r
     Name = NULL;\r
   }\r
@@ -1753,7 +1763,14 @@ ConstructConfigHdr (
     // 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
@@ -1768,7 +1785,14 @@ ConstructConfigHdr (
     // 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
+        MaxLen * sizeof (CHAR16) - ((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
@@ -1782,7 +1806,14 @@ ConstructConfigHdr (
   // 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
+      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
@@ -2346,7 +2377,7 @@ GetStringIdFromDatabase (
 }\r
 \r
 /**\r
-  Genereate the KeywordResp String.\r
+  Generate the KeywordResp String.\r
 \r
   <KeywordResp> ::= <NameSpaceId><PathHdr>'&'<Keyword>'&VALUE='<Number>['&READONLY']\r
 \r
@@ -2375,6 +2406,7 @@ GenerateKeywordResp (
   CHAR16    *RespStr;\r
   CHAR16    *PathHdr;\r
   CHAR16    *UnicodeNameSpace;\r
+  UINTN     NameSpaceLength;\r
 \r
   ASSERT ((NameSpace != NULL) && (DevicePath != NULL) && (KeywordData != NULL) && (ValueStr != NULL) && (KeywordResp != NULL));\r
 \r
@@ -2385,12 +2417,13 @@ GenerateKeywordResp (
   // 1.1 NameSpaceId size.\r
   // 'NAMESPACE='<String>\r
   //\r
-  RespStrLen = 10 + AsciiStrLen (NameSpace);\r
-  UnicodeNameSpace = AllocatePool ((AsciiStrLen (NameSpace) + 1) * sizeof (CHAR16));\r
+  NameSpaceLength = AsciiStrLen (NameSpace);\r
+  RespStrLen = 10 + NameSpaceLength;\r
+  UnicodeNameSpace = AllocatePool ((NameSpaceLength + 1) * sizeof (CHAR16));\r
   if (UnicodeNameSpace == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  AsciiStrToUnicodeStr(NameSpace, UnicodeNameSpace);\r
+  AsciiStrToUnicodeStrS (NameSpace, UnicodeNameSpace, NameSpaceLength + 1);\r
 \r
   //\r
   // 1.2 PathHdr size.\r
@@ -2407,7 +2440,7 @@ GenerateKeywordResp (
   RespStrLen += StrLen (PathHdr);\r
 \r
   //\r
-  // 1.3 Keyword setion.\r
+  // 1.3 Keyword section.\r
   // 'KEYWORD='<String>[':'<DecCh>(1/4)]\r
   //\r
   RespStrLen += 8 + StrLen (KeywordData);\r
@@ -2697,7 +2730,7 @@ Error:
   }\r
 \r
   //\r
-  // return the already get MultiKeywordString even error occured.\r
+  // return the already get MultiKeywordString even error occurred.\r
   //\r
   if (MultiKeywordResp == NULL) {\r
     Status = EFI_NOT_FOUND;\r
@@ -2895,11 +2928,11 @@ EfiConfigKeywordHandlerSetData (
     StringPtr = NextStringPtr;\r
 \r
     //\r
-    // 5. Find ReadOnly filter.\r
+    // 5. Find READONLY tag.\r
     //\r
-    if ((StringPtr != NULL) && StrnCmp (StringPtr, L"&ReadOnly", StrLen (L"&ReadOnly")) == 0) {\r
+    if ((StringPtr != NULL) && StrnCmp (StringPtr, L"&READONLY", StrLen (L"&READONLY")) == 0) {\r
       ReadOnly = TRUE;\r
-      StringPtr += StrLen (L"&ReadOnly");\r
+      StringPtr += StrLen (L"&READONLY");\r
     } else {\r
       ReadOnly = FALSE;\r
     }\r
@@ -2925,9 +2958,18 @@ EfiConfigKeywordHandlerSetData (
     // 8. Check the readonly flag.\r
     //\r
     if (ExtractReadOnlyFromOpCode (OpCode) != ReadOnly) {\r
+      //\r
+      // Extracting readonly flag form opcode and extracting "READONLY" tag form KeywordString should have the same results.\r
+      // If not, the input KeywordString must be incorrect, return the error status to caller.\r
+      //\r
+      *ProgressErr = KEYWORD_HANDLER_INCOMPATIBLE_VALUE_DETECTED;\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto Done;\r
+    }\r
+    if (ReadOnly) {\r
       *ProgressErr = KEYWORD_HANDLER_ACCESS_NOT_PERMITTED;\r
       Status = EFI_ACCESS_DENIED;\r
-      goto Done;      \r
+      goto Done;\r
     }\r
     \r
     //\r
@@ -3050,7 +3092,7 @@ Done:
   @retval EFI_SUCCESS             The specified action was completed successfully.\r
   \r
   @retval EFI_INVALID_PARAMETER   One or more of the following are TRUE:\r
-                                  1.Progress, ProgressErr, or Resuts is NULL.\r
+                                  1.Progress, ProgressErr, or Results is NULL.\r
                                   2.Parsing of the KeywordString resulted in an error. See\r
                                     Progress and ProgressErr for more data.\r
   \r