]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c
MdeModulePkg: Change use of EFI_D_* to DEBUG_*
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / ConfigKeywordHandler.c
index e88a0c45975fb9a3352d3069008d164902f48f29..3dfa8bf41f55bd9a2b80e3d85378c6968cdcb9d6 100644 (file)
@@ -1,14 +1,8 @@
 /** @file\r
 Implementation of interfaces function for EFI_CONFIG_KEYWORD_HANDLER_PROTOCOL.\r
 \r
-Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -28,7 +22,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
@@ -109,7 +103,7 @@ ExtractDevicePath (
   if (((Length + 1) / 2) < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   //\r
   // The data in <PathHdr> is encoded as hex UNICODE %02x bytes in the same order\r
   // as the device path resides in RAM memory.\r
@@ -119,7 +113,7 @@ ExtractDevicePath (
   if (DevicePathBuffer == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  \r
+\r
   //\r
   // Convert DevicePath\r
   //\r
@@ -133,7 +127,7 @@ ExtractDevicePath (
       DevicePathBuffer [Index/2] = (UINT8) ((DevicePathBuffer [Index/2] << 4) + DigitUint8);\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Validate DevicePath\r
   //\r
@@ -178,6 +172,7 @@ ExtractNameSpace (
   )\r
 {\r
   CHAR16    *TmpPtr;\r
+  UINTN     NameSpaceSize;\r
 \r
   ASSERT (NameSpace != NULL);\r
 \r
@@ -208,7 +203,7 @@ ExtractNameSpace (
 \r
   TmpPtr = StrStr (String, L"&");\r
   if (TmpPtr != NULL) {\r
-    *TmpPtr = 0; \r
+    *TmpPtr = 0;\r
   }\r
   if (NextString != NULL) {\r
     *NextString = String + StrLen (String);\r
@@ -218,14 +213,15 @@ 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
+    *TmpPtr = L'&';\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -238,10 +234,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
@@ -254,7 +250,7 @@ ExtractKeyword (
   EFI_STRING  TmpPtr;\r
 \r
   ASSERT ((Keyword != NULL) && (NextString != NULL));\r
-  \r
+\r
   TmpPtr = NULL;\r
 \r
   //\r
@@ -278,10 +274,10 @@ ExtractKeyword (
   }\r
 \r
   String += StrLen (L"KEYWORD=");\r
-  \r
+\r
   TmpPtr = StrStr (String, L"&");\r
   if (TmpPtr != NULL) {\r
-    *TmpPtr = 0; \r
+    *TmpPtr = 0;\r
   }\r
   *NextString = String + StrLen (String);\r
 \r
@@ -293,7 +289,7 @@ ExtractKeyword (
   if (TmpPtr != NULL) {\r
     *TmpPtr = L'&';\r
   }\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -304,10 +300,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
@@ -333,10 +329,10 @@ ExtractValue (
   }\r
 \r
   String += StrLen (L"VALUE=");\r
-  \r
+\r
   TmpPtr = StrStr (String, L"&");\r
   if (TmpPtr != NULL) {\r
-    *TmpPtr = 0; \r
+    *TmpPtr = 0;\r
   }\r
   *NextString = String + StrLen (String);\r
 \r
@@ -348,7 +344,7 @@ ExtractValue (
   if (TmpPtr != NULL) {\r
     *TmpPtr = L'&';\r
   }\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -359,10 +355,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
@@ -374,7 +370,7 @@ ExtractFilter (
 {\r
   CHAR16      *PathPtr;\r
   CHAR16      *KeywordPtr;\r
-  BOOLEAN     RetVal; \r
+  BOOLEAN     RetVal;\r
 \r
   ASSERT ((FilterFlags != NULL) && (NextString != NULL));\r
 \r
@@ -385,7 +381,7 @@ ExtractFilter (
     *NextString = NULL;\r
     return FALSE;\r
   }\r
-  \r
+\r
   *FilterFlags = 0;\r
   RetVal = TRUE;\r
 \r
@@ -475,7 +471,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 +514,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
@@ -556,7 +552,7 @@ ValidateFilter (
 \r
   //\r
   // Get ReadOnly flag from Question.\r
-  // \r
+  //\r
   *ReadOnly = ExtractReadOnlyFromOpCode(OpCodeData);\r
 \r
   while (ExtractFilter (StringPtr, &FilterFlags, &NextFilter)) {\r
@@ -651,7 +647,7 @@ ValidateFilter (
     //\r
     StringPtr = NextFilter;\r
   }\r
-  \r
+\r
 Done:\r
   //\r
   // The current filter which is processing.\r
@@ -700,16 +696,16 @@ GetRecordFromDevicePath (
 }\r
 \r
 /**\r
-  Calculate the size of StringSrc and output it. Also copy string text from src \r
+  Calculate the size of StringSrc and output it. Also copy string text from src\r
   to dest.\r
 \r
   This is a internal function.\r
 \r
   @param  StringSrc              Points to current null-terminated string.\r
   @param  BufferSize             Length of the buffer.\r
-  @param  StringDest             Buffer to store the string text. \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
@@ -736,7 +732,7 @@ GetUnicodeStringTextAndSize (
   if (*StringDest == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  \r
+\r
   CopyMem (*StringDest, StringSrc, StringSize);\r
 \r
   *BufferSize = StringSize;\r
@@ -779,6 +775,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 +791,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 +869,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 +889,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 +908,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 +933,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
@@ -1063,13 +1061,14 @@ GetNextStringId (
     case EFI_HII_SIBT_STRING_SCSU:\r
       Offset = sizeof (EFI_HII_STRING_BLOCK);\r
       StringTextPtr = BlockHdr + Offset;\r
-      \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
@@ -1082,13 +1081,14 @@ GetNextStringId (
     case EFI_HII_SIBT_STRING_SCSU_FONT:\r
       Offset = sizeof (EFI_HII_SIBT_STRING_SCSU_FONT_BLOCK) - sizeof (UINT8);\r
       StringTextPtr = BlockHdr + Offset;\r
-      \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 +1105,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 +1133,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
@@ -1197,7 +1199,7 @@ GetNextStringId (
       BlockSize += Offset;\r
       CopyMem (&StringCount, BlockHdr + sizeof (EFI_HII_STRING_BLOCK), sizeof (UINT16));\r
       for (Index = 0; Index < StringCount; Index++) {\r
-        GetUnicodeStringTextAndSize (StringTextPtr, &StringSize, &String);        \r
+        GetUnicodeStringTextAndSize (StringTextPtr, &StringSize, &String);\r
 \r
         if (FindString) {\r
           *KeywordValue = String;\r
@@ -1302,7 +1304,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
@@ -1338,10 +1340,9 @@ GetStringIdFromRecord (
     StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE);\r
 \r
     if (AsciiStrnCmp(Name, StringPackage->StringPkgHdr->Language, AsciiStrLen (Name)) == 0) {\r
-      Status = GetStringIdFromString (StringPackage, KeywordValue, StringId); \r
+      Status = GetStringIdFromString (StringPackage, KeywordValue, StringId);\r
       if (EFI_ERROR (Status)) {\r
-        RetVal = KEYWORD_HANDLER_KEYWORD_NOT_FOUND;\r
-        continue;\r
+        return KEYWORD_HANDLER_KEYWORD_NOT_FOUND;\r
       } else {\r
         if (*NameSpace == NULL) {\r
           *NameSpace = AllocateCopyPool (AsciiStrSize (StringPackage->StringPkgHdr->Language), StringPackage->StringPkgHdr->Language);\r
@@ -1423,10 +1424,10 @@ IsStorageOpCode (
   @retval  the opcode for the question.\r
 \r
 **/\r
-UINT8 * \r
+UINT8 *\r
 FindQuestionFromStringId (\r
   IN HII_IFR_PACKAGE_INSTANCE      *FormPackage,\r
-  IN EFI_STRING_ID                 KeywordStrId \r
+  IN EFI_STRING_ID                 KeywordStrId\r
   )\r
 {\r
   UINT8                        *OpCodeData;\r
@@ -1523,7 +1524,7 @@ FindStorageFromVarId (
   @retval  the width info for one question.\r
 \r
 **/\r
-UINT16 \r
+UINT16\r
 GetWidth (\r
   IN UINT8        *OpCodeData\r
   )\r
@@ -1541,16 +1542,16 @@ GetWidth (
     switch (((EFI_IFR_ONE_OF *) OpCodeData)->Flags & EFI_IFR_NUMERIC_SIZE) {\r
     case EFI_IFR_NUMERIC_SIZE_1:\r
       return (UINT16) sizeof (UINT8);\r
-    \r
+\r
     case EFI_IFR_NUMERIC_SIZE_2:\r
       return  (UINT16) sizeof (UINT16);\r
-    \r
+\r
     case EFI_IFR_NUMERIC_SIZE_4:\r
       return (UINT16) sizeof (UINT32);\r
-    \r
+\r
     case EFI_IFR_NUMERIC_SIZE_8:\r
       return (UINT16) sizeof (UINT64);\r
-    \r
+\r
     default:\r
       ASSERT (FALSE);\r
       return 0;\r
@@ -1565,16 +1566,16 @@ GetWidth (
     switch (((EFI_IFR_ONE_OF_OPTION *) NextOpCodeData)->Type) {\r
     case EFI_IFR_TYPE_NUM_SIZE_8:\r
       return (UINT16) sizeof (UINT8) * ((EFI_IFR_ORDERED_LIST *) OpCodeData)->MaxContainers;\r
-    \r
+\r
     case EFI_IFR_TYPE_NUM_SIZE_16:\r
       return (UINT16) sizeof (UINT16) * ((EFI_IFR_ORDERED_LIST *) OpCodeData)->MaxContainers ;\r
-    \r
+\r
     case EFI_IFR_TYPE_NUM_SIZE_32:\r
       return (UINT16) sizeof (UINT32) * ((EFI_IFR_ORDERED_LIST *) OpCodeData)->MaxContainers;\r
-    \r
+\r
     case EFI_IFR_TYPE_NUM_SIZE_64:\r
       return (UINT16) sizeof (UINT64) * ((EFI_IFR_ORDERED_LIST *) OpCodeData)->MaxContainers;\r
-    \r
+\r
     default:\r
       ASSERT (FALSE);\r
       return 0;\r
@@ -1582,8 +1583,10 @@ GetWidth (
 \r
   case EFI_IFR_CHECKBOX_OP:\r
     return (UINT16) sizeof (BOOLEAN);\r
-    \r
+\r
   case EFI_IFR_PASSWORD_OP:\r
+    return (UINT16)((UINTN) ((EFI_IFR_PASSWORD *) OpCodeData)->MaxSize * sizeof (CHAR16));\r
+\r
   case EFI_IFR_STRING_OP:\r
     return (UINT16)((UINTN) ((EFI_IFR_STRING *) OpCodeData)->MaxSize * sizeof (CHAR16));\r
 \r
@@ -1600,7 +1603,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
@@ -1639,16 +1642,16 @@ InternalLowerConfigString (
 \r
 /**\r
   Allocates and returns a Null-terminated Unicode <ConfigHdr> string.\r
-  \r
+\r
   The format of a <ConfigHdr> is as follows:\r
 \r
     GUID=<HexCh>32&NAME=<Char>NameLength&PATH=<HexChar>DevicePathSize<Null>\r
 \r
-  @param[in]  OpCodeData    The opcode for the storage.                  \r
+  @param[in]  OpCodeData    The opcode for the storage.\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
@@ -1669,6 +1672,7 @@ ConstructConfigHdr (
   UINT8                     *Buffer;\r
   CHAR16                    *Name;\r
   CHAR8                     *AsciiName;\r
+  UINTN                     NameSize;\r
   EFI_GUID                  *Guid;\r
   UINTN                     MaxLen;\r
 \r
@@ -1679,17 +1683,17 @@ ConstructConfigHdr (
     Guid      = (EFI_GUID *)(UINTN *)&((EFI_IFR_VARSTORE *) OpCodeData)->Guid;\r
     AsciiName = (CHAR8 *) ((EFI_IFR_VARSTORE *) OpCodeData)->Name;\r
     break;\r
-  \r
+\r
   case EFI_IFR_VARSTORE_NAME_VALUE_OP:\r
     Guid      = (EFI_GUID *)(UINTN *)&((EFI_IFR_VARSTORE_NAME_VALUE *) OpCodeData)->Guid;\r
     AsciiName = NULL;\r
     break;\r
-  \r
+\r
   case EFI_IFR_VARSTORE_EFI_OP:\r
     Guid      = (EFI_GUID *)(UINTN *)&((EFI_IFR_VARSTORE_EFI *) OpCodeData)->Guid;\r
     AsciiName = (CHAR8 *) ((EFI_IFR_VARSTORE_EFI *) OpCodeData)->Name;\r
     break;\r
-  \r
+\r
   default:\r
     ASSERT (FALSE);\r
     Guid      = NULL;\r
@@ -1698,15 +1702,16 @@ 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
 \r
   //\r
-  // Compute the length of Name in Unicode characters.  \r
+  // Compute the length of Name in Unicode characters.\r
   // If Name is NULL, then the length is 0.\r
   //\r
   NameLength = 0;\r
@@ -1752,14 +1757,21 @@ 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
+\r
   //\r
   // Append L"&NAME="\r
   //\r
-  StrCpyS (String, MaxLen, L"&NAME=");\r
+  StrCatS (ReturnString, MaxLen, L"&NAME=");\r
   String += StrLen (String);\r
 \r
   if (Name != NULL) {\r
@@ -1767,21 +1779,35 @@ 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
   //\r
   // Append L"&PATH="\r
   //\r
-  StrCpyS (String, MaxLen, 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
+      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
@@ -1809,12 +1835,12 @@ EFI_STRING
 ConstructRequestElement (\r
   IN CHAR16      *Name,\r
   IN UINT16      Offset,\r
-  IN UINT16      Width  \r
+  IN UINT16      Width\r
   )\r
 {\r
   CHAR16    *StringPtr;\r
   UINTN     Length;\r
-  \r
+\r
   if (Name != NULL) {\r
     //\r
     // Add <BlockName> length for each Name\r
@@ -1854,10 +1880,10 @@ ConstructRequestElement (
     // Append OFFSET=XXXX&WIDTH=YYYY\0\r
     //\r
     UnicodeSPrint (\r
-      StringPtr, \r
-      (7 + 4 + 7 + 4 + 1) * sizeof (CHAR16), \r
-      L"OFFSET=%04X&WIDTH=%04X", \r
-      Offset, \r
+      StringPtr,\r
+      (7 + 4 + 7 + 4 + 1) * sizeof (CHAR16),\r
+      L"OFFSET=%04X&WIDTH=%04X",\r
+      Offset,\r
       Width\r
     );\r
   }\r
@@ -1890,7 +1916,7 @@ GetNameFromId (
 \r
   Name = NULL;\r
   BestLanguage = NULL;\r
-  PlatformLanguage = NULL; \r
+  PlatformLanguage = NULL;\r
   SupportedLanguages = NULL;\r
 \r
   GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);\r
@@ -1900,10 +1926,10 @@ GetNameFromId (
   // Get the best matching language from SupportedLanguages\r
   //\r
   BestLanguage = GetBestLanguage (\r
-                   SupportedLanguages, \r
+                   SupportedLanguages,\r
                    FALSE,                                             // RFC 4646 mode\r
                    PlatformLanguage != NULL ? PlatformLanguage : "",  // Highest priority\r
-                   SupportedLanguages,                                // Lowest priority \r
+                   SupportedLanguages,                                // Lowest priority\r
                    NULL\r
                    );\r
   if (BestLanguage == NULL) {\r
@@ -1981,7 +2007,7 @@ ExtractConfigRequest (
   IN  EFI_STRING_ID         KeywordStrId,\r
   OUT UINT8                 **OpCodeData,\r
   OUT EFI_STRING            *ConfigRequest\r
-  ) \r
+  )\r
 {\r
   LIST_ENTRY                          *Link;\r
   HII_DATABASE_PACKAGE_LIST_INSTANCE  *PackageListNode;\r
@@ -2020,8 +2046,8 @@ ExtractConfigRequest (
       // Header->VarStoreId == 0 means no storage for this question.\r
       //\r
       ASSERT (Header->VarStoreId != 0);\r
-      DEBUG ((EFI_D_INFO, "Varstore Id: 0x%x\n", Header->VarStoreId));\r
-      \r
+      DEBUG ((DEBUG_INFO, "Varstore Id: 0x%x\n", Header->VarStoreId));\r
+\r
       Storage = FindStorageFromVarId (FormPackage, Header->VarStoreId);\r
       ASSERT (Storage != NULL);\r
 \r
@@ -2045,14 +2071,10 @@ ExtractConfigRequest (
       StringPtr = *ConfigRequest;\r
 \r
       StrCpyS (StringPtr, MaxLen, ConfigHdr);\r
-      StringPtr += StrLen (StringPtr);\r
 \r
-      *StringPtr = L'&';\r
-      StringPtr++;\r
+      StrCatS (StringPtr, MaxLen, L"&");\r
 \r
-      StrCpyS (StringPtr, MaxLen, RequestElement);\r
-      StringPtr += StrLen (StringPtr);\r
-      *StringPtr = L'\0';\r
+      StrCatS (StringPtr, MaxLen, RequestElement);\r
 \r
       FreePool (ConfigHdr);\r
       FreePool (RequestElement);\r
@@ -2088,7 +2110,7 @@ ExtractConfigResp (
   IN  EFI_STRING            ValueElement,\r
   OUT UINT8                 **OpCodeData,\r
   OUT EFI_STRING            *ConfigResp\r
-  ) \r
+  )\r
 {\r
   LIST_ENTRY                          *Link;\r
   HII_DATABASE_PACKAGE_LIST_INSTANCE  *PackageListNode;\r
@@ -2127,8 +2149,8 @@ ExtractConfigResp (
       // Header->VarStoreId == 0 means no storage for this question.\r
       //\r
       ASSERT (Header->VarStoreId != 0);\r
-      DEBUG ((EFI_D_INFO, "Varstore Id: 0x%x\n", Header->VarStoreId));\r
-      \r
+      DEBUG ((DEBUG_INFO, "Varstore Id: 0x%x\n", Header->VarStoreId));\r
+\r
       Storage = FindStorageFromVarId (FormPackage, Header->VarStoreId);\r
       ASSERT (Storage != NULL);\r
 \r
@@ -2153,23 +2175,17 @@ ExtractConfigResp (
       StringPtr = *ConfigResp;\r
 \r
       StrCpyS (StringPtr, MaxLen, ConfigHdr);\r
-      StringPtr += StrLen (StringPtr);\r
 \r
-      *StringPtr = L'&';\r
-      StringPtr++;\r
+      StrCatS (StringPtr, MaxLen, L"&");\r
+\r
+\r
+      StrCatS (StringPtr, MaxLen, RequestElement);\r
 \r
-      StrCpyS (StringPtr, MaxLen, RequestElement);\r
-      StringPtr += StrLen (StringPtr);\r
-      \r
-      *StringPtr = L'&';\r
-      StringPtr++;\r
+      StrCatS (StringPtr, MaxLen, L"&");\r
 \r
-      StrCpyS (StringPtr, MaxLen, L"VALUE=");\r
-      StringPtr += StrLen (StringPtr);\r
+      StrCatS (StringPtr, MaxLen, L"VALUE=");\r
 \r
-      StrCpyS (StringPtr, MaxLen, ValueElement);\r
-      StringPtr += StrLen (StringPtr);\r
-      *StringPtr = L'\0';\r
+      StrCatS (StringPtr, MaxLen, ValueElement);\r
 \r
       FreePool (ConfigHdr);\r
       FreePool (RequestElement);\r
@@ -2319,7 +2335,7 @@ GetStringIdFromDatabase (
       *ProgressErr = GetStringIdFromRecord (Record, NameSpace, KeywordData, KeywordStringId);\r
       if (*ProgressErr == KEYWORD_HANDLER_NO_ERROR) {\r
         *DataBaseRecord = Record;\r
-        \r
+\r
         if ((DevicePathPkg = Record->PackageList->DevicePathPkg) != NULL) {\r
           DestDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) (DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER));\r
           DevicePathSize = GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) DestDevicePath);\r
@@ -2339,7 +2355,7 @@ GetStringIdFromDatabase (
         return EFI_OUT_OF_RESOURCES;\r
       } else if (*ProgressErr == KEYWORD_HANDLER_KEYWORD_NOT_FOUND) {\r
         FindNameSpace = TRUE;\r
-      }  \r
+      }\r
     }\r
 \r
     //\r
@@ -2355,7 +2371,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
@@ -2372,7 +2388,7 @@ GetStringIdFromDatabase (
 **/\r
 EFI_STATUS\r
 GenerateKeywordResp (\r
-  IN  CHAR8                          *NameSpace, \r
+  IN  CHAR8                          *NameSpace,\r
   IN  EFI_DEVICE_PATH_PROTOCOL       *DevicePath,\r
   IN  EFI_STRING                     KeywordData,\r
   IN  EFI_STRING                     ValueStr,\r
@@ -2384,6 +2400,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
@@ -2394,12 +2411,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
@@ -2416,7 +2434,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
@@ -2453,50 +2471,40 @@ GenerateKeywordResp (
   // 2.1 Copy NameSpaceId section.\r
   //\r
   StrCpyS (RespStr, RespStrLen, L"NAMESPACE=");\r
-  RespStr += StrLen (RespStr);\r
-  StrCpyS (RespStr, RespStrLen, UnicodeNameSpace);\r
-  RespStr += StrLen (RespStr);\r
+\r
+  StrCatS (RespStr, RespStrLen, UnicodeNameSpace);\r
 \r
   //\r
   // 2.2 Copy PathHdr section.\r
   //\r
-  StrCpyS (RespStr, RespStrLen, PathHdr);\r
-  RespStr += StrLen (RespStr);\r
+  StrCatS (RespStr, RespStrLen, PathHdr);\r
 \r
   //\r
   // 2.3 Copy Keyword section.\r
   //\r
-  StrCpyS (RespStr, RespStrLen, L"KEYWORD=");\r
-  RespStr += StrLen (RespStr);\r
-  StrCpyS (RespStr, RespStrLen, KeywordData);\r
-  RespStr += StrLen (RespStr);\r
+  StrCatS (RespStr, RespStrLen, L"KEYWORD=");\r
+\r
+  StrCatS (RespStr, RespStrLen, KeywordData);\r
 \r
   //\r
   // 2.4 Copy the Value section.\r
   //\r
-  StrCpyS (RespStr, RespStrLen, ValueStr);\r
-  RespStr += StrLen (RespStr);\r
+  StrCatS (RespStr, RespStrLen, ValueStr);\r
 \r
   //\r
   // 2.5 Copy ReadOnly section if exist.\r
   //\r
   if (ReadOnly) {\r
-    StrCpyS (RespStr, RespStrLen, L"&READONLY");\r
-    RespStr += StrLen (RespStr);\r
+    StrCatS (RespStr, RespStrLen, L"&READONLY");\r
   }\r
 \r
-  //\r
-  // 2.6 Add the end.\r
-  //\r
-  *RespStr = L'\0';\r
-\r
   if (UnicodeNameSpace != NULL) {\r
     FreePool (UnicodeNameSpace);\r
   }\r
   if (PathHdr != NULL) {\r
     FreePool (PathHdr);\r
   }\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -2529,33 +2537,34 @@ MergeToMultiKeywordResp (
 \r
   MultiKeywordRespLen = (StrLen (*MultiKeywordResp) + 1 + StrLen (*KeywordResp) + 1) * sizeof (CHAR16);\r
 \r
-  StringPtr = AllocateCopyPool (MultiKeywordRespLen, *MultiKeywordResp);\r
+  StringPtr = ReallocatePool (\r
+                StrSize (*MultiKeywordResp),\r
+                MultiKeywordRespLen,\r
+                *MultiKeywordResp\r
+                );\r
   if (StringPtr == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  \r
-  FreePool (*MultiKeywordResp);\r
-  *MultiKeywordResp = StringPtr;\r
 \r
-  StringPtr += StrLen (StringPtr);\r
+  *MultiKeywordResp = StringPtr;\r
 \r
-  *StringPtr = L'&';\r
-  StringPtr++;\r
+  StrCatS (StringPtr, MultiKeywordRespLen / sizeof (CHAR16), L"&");\r
 \r
-  StrCpyS (StringPtr, MultiKeywordRespLen / sizeof (CHAR16), *KeywordResp);\r
+  StrCatS (StringPtr, MultiKeywordRespLen / sizeof (CHAR16), *KeywordResp);\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  Enumerate all keyword in the system. \r
-  \r
-  If error occur when parse one keyword, just skip it and parse the next one. \r
+  Enumerate all keyword in the system.\r
+\r
+  If error occur when parse one keyword, just skip it and parse the next one.\r
 \r
   This is a internal function.\r
 \r
   @param  NameSpace                      The namespace used to search the string.\r
   @param  MultiResp                      Return the MultiKeywordResp string for the system.\r
+  @param  ProgressErr                    Return the error status.\r
 \r
   @retval EFI_OUT_OF_RESOURCES           The memory can't be allocated.\r
   @retval EFI_SUCCESS                    Generate the MultiKeywordResp string.\r
@@ -2565,7 +2574,8 @@ MergeToMultiKeywordResp (
 EFI_STATUS\r
 EnumerateAllKeywords (\r
   IN  CHAR8             *NameSpace,\r
-  OUT EFI_STRING        *MultiResp\r
+  OUT EFI_STRING        *MultiResp,\r
+  OUT UINT32            *ProgressErr\r
   )\r
 {\r
   LIST_ENTRY                          *Link;\r
@@ -2585,6 +2595,7 @@ EnumerateAllKeywords (
   CHAR16                              *MultiKeywordResp;\r
   CHAR16                              *KeywordData;\r
   BOOLEAN                             ReadOnly;\r
+  BOOLEAN                             FindKeywordPackages;\r
 \r
   DataBaseRecord   = NULL;\r
   Status           = EFI_SUCCESS;\r
@@ -2594,6 +2605,7 @@ EnumerateAllKeywords (
   ConfigRequest    = NULL;\r
   ValueElement     = NULL;\r
   KeywordResp      = NULL;\r
+  FindKeywordPackages = FALSE;\r
 \r
   if (NameSpace == NULL) {\r
     NameSpace = UEFI_CONFIG_LANG;\r
@@ -2616,6 +2628,7 @@ EnumerateAllKeywords (
       // Check whether has keyword string package.\r
       //\r
       if (AsciiStrnCmp(NameSpace, StringPackage->StringPkgHdr->Language, AsciiStrLen (NameSpace)) == 0) {\r
+        FindKeywordPackages = TRUE;\r
         //\r
         // Keep the NameSpace string.\r
         //\r
@@ -2630,7 +2643,7 @@ EnumerateAllKeywords (
         // Any valid string start from 2. so here initial it to 1.\r
         //\r
         NextStringId = 1;\r
-        \r
+\r
         //\r
         // Enumerate all valid stringid in the package.\r
         //\r
@@ -2645,7 +2658,7 @@ EnumerateAllKeywords (
             //\r
             goto Error;\r
           }\r
-          \r
+\r
           //\r
           // 3.4 Extract Value for the input keyword.\r
           //\r
@@ -2679,7 +2692,7 @@ EnumerateAllKeywords (
             //\r
             goto Done;\r
           }\r
-          \r
+\r
           //\r
           // 6. Merge to the MultiKeywordResp string.\r
           //\r
@@ -2702,7 +2715,7 @@ Error:
           if (KeywordResp != NULL) {\r
             FreePool (KeywordResp);\r
             KeywordResp = NULL;\r
-          } \r
+          }\r
         }\r
 \r
         if (LocalNameSpace != NULL) {\r
@@ -2710,19 +2723,24 @@ Error:
           LocalNameSpace = NULL;\r
         }\r
       }\r
-    } \r
+    }\r
   }\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
+    if (!FindKeywordPackages) {\r
+      *ProgressErr = KEYWORD_HANDLER_NAMESPACE_ID_NOT_FOUND;\r
+    } else {\r
+      *ProgressErr = KEYWORD_HANDLER_KEYWORD_NOT_FOUND;\r
+    }\r
   } else {\r
     Status = EFI_SUCCESS;\r
   }\r
   *MultiResp = MultiKeywordResp;\r
-  \r
+\r
 Done:\r
   if (LocalNameSpace != NULL) {\r
     FreePool (LocalNameSpace);\r
@@ -2742,18 +2760,18 @@ Done:
   This function accepts a <MultiKeywordResp> formatted string, finds the associated\r
   keyword owners, creates a <MultiConfigResp> string from it and forwards it to the\r
   EFI_HII_ROUTING_PROTOCOL.RouteConfig function.\r
-  \r
-  If there is an issue in resolving the contents of the KeywordString, then the \r
-  function returns an error and also sets the Progress and ProgressErr with the \r
+\r
+  If there is an issue in resolving the contents of the KeywordString, then the\r
+  function returns an error and also sets the Progress and ProgressErr with the\r
   appropriate information about where the issue occurred and additional data about\r
-  the nature of the issue. \r
-  \r
+  the nature of the issue.\r
+\r
   In the case when KeywordString containing multiple keywords, when an EFI_NOT_FOUND\r
   error is generated during processing the second or later keyword element, the system\r
-  storage associated with earlier keywords is not modified. All elements of the \r
+  storage associated with earlier keywords is not modified. All elements of the\r
   KeywordString must successfully pass all tests for format and access prior to making\r
   any modifications to storage.\r
-  \r
+\r
   In the case when EFI_DEVICE_ERROR is returned from the processing of a KeywordString\r
   containing multiple keywords, the state of storage associated with earlier keywords\r
   is undefined.\r
@@ -2761,17 +2779,18 @@ Done:
 \r
   @param This             Pointer to the EFI_KEYWORD_HANDLER _PROTOCOL instance.\r
 \r
-  @param KeywordString    A null-terminated string in <MultiKeywordResp> format. \r
+  @param KeywordString    A null-terminated string in <MultiKeywordResp> format.\r
 \r
-  @param Progress         On return, points to a character in the KeywordString. \r
-                          Points to the string's NULL terminator if the request \r
-                          was successful. Points to the most recent '&' before \r
-                          the first failing string element if the request was \r
-                          not successful.\r
+  @param Progress         On return, points to a character in the KeywordString.\r
+                          Points to the string's NULL terminator if the request\r
+                          was successful. Points to the most recent '&' before\r
+                          the first failing name / value pair (or the beginning\r
+                          of the string if the failure is in the first name / value\r
+                          pair) if the request was not successful.\r
 \r
   @param ProgressErr      If during the processing of the KeywordString there was\r
-                          a failure, this parameter gives additional information \r
-                          about the possible source of the problem. The various \r
+                          a failure, this parameter gives additional information\r
+                          about the possible source of the problem. The various\r
                           errors are defined in "Related Definitions" below.\r
 \r
 \r
@@ -2779,16 +2798,16 @@ Done:
 \r
   @retval EFI_INVALID_PARAMETER   One or more of the following are TRUE:\r
                                   1. KeywordString is NULL.\r
-                                  2. Parsing of the KeywordString resulted in an \r
+                                  2. Parsing of the KeywordString resulted in an\r
                                      error. See Progress and ProgressErr for more data.\r
 \r
-  @retval EFI_NOT_FOUND           An element of the KeywordString was not found. \r
+  @retval EFI_NOT_FOUND           An element of the KeywordString was not found.\r
                                   See ProgressErr for more data.\r
 \r
-  @retval EFI_OUT_OF_RESOURCES    Required system resources could not be allocated.  \r
+  @retval EFI_OUT_OF_RESOURCES    Required system resources could not be allocated.\r
                                   See ProgressErr for more data.\r
-                                  \r
-  @retval EFI_ACCESS_DENIED       The action violated system policy. See ProgressErr \r
+\r
+  @retval EFI_ACCESS_DENIED       The action violated system policy. See ProgressErr\r
                                   for more data.\r
 \r
   @retval EFI_DEVICE_ERROR        An unexpected system error occurred. See ProgressErr\r
@@ -2796,7 +2815,7 @@ Done:
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI \r
+EFIAPI\r
 EfiConfigKeywordHandlerSetData (\r
   IN EFI_CONFIG_KEYWORD_HANDLER_PROTOCOL *This,\r
   IN CONST EFI_STRING                    KeywordString,\r
@@ -2820,6 +2839,7 @@ EfiConfigKeywordHandlerSetData (
   BOOLEAN                             ReadOnly;\r
   EFI_STRING                          InternalProgress;\r
   CHAR16                              *TempString;\r
+  CHAR16                              *KeywordStartPos;\r
 \r
   if (This == NULL || Progress == NULL || ProgressErr == NULL || KeywordString == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -2834,6 +2854,7 @@ EfiConfigKeywordHandlerSetData (
   KeywordData     = NULL;\r
   ValueElement    = NULL;\r
   ConfigResp      = NULL;\r
+  KeywordStartPos = NULL;\r
   KeywordStringId = 0;\r
 \r
   //\r
@@ -2857,7 +2878,7 @@ EfiConfigKeywordHandlerSetData (
     // 1.1 Check whether the input namespace is valid.\r
     //\r
     if (AsciiStrnCmp(NameSpace, UEFI_CONFIG_LANG, AsciiStrLen (UEFI_CONFIG_LANG)) != 0) {\r
-      *ProgressErr = KEYWORD_HANDLER_UNDEFINED_PROCESSING_ERROR;\r
+      *ProgressErr = KEYWORD_HANDLER_MALFORMED_STRING;\r
       Status = EFI_INVALID_PARAMETER;\r
       goto Done;\r
     }\r
@@ -2877,6 +2898,7 @@ EfiConfigKeywordHandlerSetData (
     //\r
     // 3. Extract keyword from the KeywordRequest string.\r
     //\r
+    KeywordStartPos = StringPtr;\r
     Status = ExtractKeyword(StringPtr, &KeywordData, &NextStringPtr);\r
     if (EFI_ERROR (Status)) {\r
       //\r
@@ -2903,11 +2925,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
@@ -2933,11 +2955,20 @@ 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
+      goto Done;\r
     }\r
-    \r
+    if (ReadOnly) {\r
+      *ProgressErr = KEYWORD_HANDLER_ACCESS_NOT_PERMITTED;\r
+      Status = EFI_ACCESS_DENIED;\r
+      goto Done;\r
+    }\r
+\r
     //\r
     // 9. Merge to the MultiKeywordResp string.\r
     //\r
@@ -2954,13 +2985,14 @@ EfiConfigKeywordHandlerSetData (
     FreePool (KeywordData);\r
     FreePool (ValueElement);\r
     NameSpace = NULL;\r
-    DevicePath = NULL; \r
+    DevicePath = NULL;\r
     KeywordData = NULL;\r
     ValueElement = NULL;\r
     if (ConfigResp != NULL) {\r
       FreePool (ConfigResp);\r
       ConfigResp = NULL;\r
     }\r
+    KeywordStartPos = NULL;\r
   }\r
 \r
   //\r
@@ -2975,10 +3007,16 @@ EfiConfigKeywordHandlerSetData (
     Status = EFI_DEVICE_ERROR;\r
     goto Done;\r
   }\r
-  \r
+\r
   *ProgressErr = KEYWORD_HANDLER_NO_ERROR;\r
 \r
 Done:\r
+  if (KeywordStartPos != NULL) {\r
+    *Progress = KeywordString + (KeywordStartPos - TempString);\r
+  } else {\r
+    *Progress = KeywordString + (StringPtr - TempString);\r
+  }\r
+\r
   ASSERT (TempString != NULL);\r
   FreePool (TempString);\r
   if (NameSpace != NULL) {\r
@@ -2998,62 +3036,63 @@ Done:
   }\r
   if (MultiConfigResp != NULL && MultiConfigResp != ConfigResp) {\r
     FreePool (MultiConfigResp);\r
-  }  \r
-  *Progress = StringPtr;\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
 /**\r
 \r
-  This function accepts a <MultiKeywordRequest> formatted string, finds the underlying \r
+  This function accepts a <MultiKeywordRequest> formatted string, finds the underlying\r
   keyword owners, creates a <MultiConfigRequest> string from it and forwards it to the\r
   EFI_HII_ROUTING_PROTOCOL.ExtractConfig function.\r
-  \r
+\r
   If there is an issue in resolving the contents of the KeywordString, then the function\r
   returns an EFI_INVALID_PARAMETER and also set the Progress and ProgressErr with the\r
   appropriate information about where the issue occurred and additional data about the\r
   nature of the issue.\r
-  \r
+\r
   In the case when KeywordString is NULL, or contains multiple keywords, or when\r
   EFI_NOT_FOUND is generated while processing the keyword elements, the Results string\r
-  contains values returned for all keywords processed prior to the keyword generating the \r
+  contains values returned for all keywords processed prior to the keyword generating the\r
   error but no values for the keyword with error or any following keywords.\r
 \r
-  \r
+\r
   @param This           Pointer to the EFI_KEYWORD_HANDLER _PROTOCOL instance.\r
-  \r
+\r
   @param NameSpaceId    A null-terminated string containing the platform configuration\r
                         language to search through in the system. If a NULL is passed\r
                         in, then it is assumed that any platform configuration language\r
                         with the prefix of "x-UEFI-" are searched.\r
-                        \r
+\r
   @param KeywordString  A null-terminated string in <MultiKeywordRequest> format. If a\r
-                        NULL is passed in the KeywordString field, all of the known \r
-                        keywords in the system for the NameSpaceId specified are \r
+                        NULL is passed in the KeywordString field, all of the known\r
+                        keywords in the system for the NameSpaceId specified are\r
                         returned in the Results field.\r
-  \r
+\r
   @param Progress       On return, points to a character in the KeywordString. Points\r
-                        to the string's NULL terminator if the request was successful. \r
-                        Points to the most recent '&' before the first failing string\r
-                        element if the request was not successful.\r
-                        \r
+                        to the string's NULL terminator if the request was successful.\r
+                        Points to the most recent '&' before the first failing name / value\r
+                        pair (or the beginning of the string if the failure is in the first\r
+                        name / value pair) if the request was not successful.\r
+\r
   @param ProgressErr    If during the processing of the KeywordString there was a\r
-                        failure, this parameter gives additional information about the \r
+                        failure, this parameter gives additional information about the\r
                         possible source of the problem. See the definitions in SetData()\r
                         for valid value definitions.\r
-  \r
+\r
   @param Results        A null-terminated string in <MultiKeywordResp> format is returned\r
-                        which has all the values filled in for the keywords in the \r
+                        which has all the values filled in for the keywords in the\r
                         KeywordString. This is a callee-allocated field, and must be freed\r
-                        by the caller after being used. \r
+                        by the caller after being used.\r
 \r
   @retval EFI_SUCCESS             The specified action was completed successfully.\r
-  \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
+\r
 \r
   @retval EFI_NOT_FOUND           An element of the KeywordString was not found. See\r
                                   ProgressErr for more data.\r
@@ -3063,7 +3102,7 @@ Done:
 \r
   @retval EFI_OUT_OF_RESOURCES    Required system resources could not be allocated.  See\r
                                   ProgressErr for more data.\r
-                                  \r
+\r
   @retval EFI_ACCESS_DENIED       The action violated system policy.  See ProgressErr for\r
                                   more data.\r
 \r
@@ -3072,12 +3111,12 @@ Done:
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI \r
+EFIAPI\r
 EfiConfigKeywordHandlerGetData (\r
   IN EFI_CONFIG_KEYWORD_HANDLER_PROTOCOL  *This,\r
   IN CONST EFI_STRING                     NameSpaceId, OPTIONAL\r
   IN CONST EFI_STRING                     KeywordString, OPTIONAL\r
-  OUT EFI_STRING                          *Progress, \r
+  OUT EFI_STRING                          *Progress,\r
   OUT UINT32                              *ProgressErr,\r
   OUT EFI_STRING                          *Results\r
   )\r
@@ -3087,7 +3126,7 @@ EfiConfigKeywordHandlerGetData (
   EFI_DEVICE_PATH_PROTOCOL            *DevicePath;\r
   HII_DATABASE_RECORD                 *DataBaseRecord;\r
   CHAR16                              *StringPtr;\r
-  CHAR16                              *NextStringPtr;  \r
+  CHAR16                              *NextStringPtr;\r
   CHAR16                              *KeywordData;\r
   EFI_STRING_ID                       KeywordStringId;\r
   UINT8                               *OpCode;\r
@@ -3131,7 +3170,7 @@ EfiConfigKeywordHandlerGetData (
     TempString = NULL;\r
   }\r
   if (EFI_ERROR (Status)) {\r
-    *ProgressErr = KEYWORD_HANDLER_NAMESPACE_ID_NOT_FOUND;\r
+    *ProgressErr = KEYWORD_HANDLER_MALFORMED_STRING;\r
     return Status;\r
   }\r
   //\r
@@ -3139,11 +3178,11 @@ EfiConfigKeywordHandlerGetData (
   //\r
   if (NameSpace != NULL){\r
     if (AsciiStrnCmp(NameSpace, UEFI_CONFIG_LANG, AsciiStrLen (UEFI_CONFIG_LANG)) != 0) {\r
-      *ProgressErr = KEYWORD_HANDLER_UNDEFINED_PROCESSING_ERROR;\r
+      *ProgressErr = KEYWORD_HANDLER_MALFORMED_STRING;\r
       return EFI_INVALID_PARAMETER;\r
     }\r
   }\r
-  \r
+\r
   if (KeywordString != NULL) {\r
     //\r
     // Use temp string to avoid changing input string buffer.\r
@@ -3163,7 +3202,7 @@ EfiConfigKeywordHandlerGetData (
       }\r
       StringPtr = NextStringPtr;\r
 \r
-     \r
+\r
       //\r
       // 3. Process Keyword section from the input keywordRequest string.\r
       //\r
@@ -3195,7 +3234,7 @@ EfiConfigKeywordHandlerGetData (
       if (EFI_ERROR (Status)) {\r
         goto Done;\r
       }\r
-      \r
+\r
       //\r
       // 3.4 Extract Value for the input keyword.\r
       //\r
@@ -3248,7 +3287,7 @@ EfiConfigKeywordHandlerGetData (
       FreePool (KeywordData);\r
       FreePool (ValueElement);\r
       FreePool (ConfigRequest);\r
-      DevicePath = NULL; \r
+      DevicePath = NULL;\r
       KeywordData = NULL;\r
       ValueElement = NULL;\r
       ConfigRequest = NULL;\r
@@ -3261,7 +3300,7 @@ EfiConfigKeywordHandlerGetData (
     //\r
     // Enumerate all keyword in the system.\r
     //\r
-    Status = EnumerateAllKeywords(NameSpace, &MultiKeywordResp);\r
+    Status = EnumerateAllKeywords(NameSpace, &MultiKeywordResp, ProgressErr);\r
     if (EFI_ERROR (Status)) {\r
       goto Done;\r
     }\r
@@ -3271,6 +3310,8 @@ EfiConfigKeywordHandlerGetData (
   *ProgressErr = KEYWORD_HANDLER_NO_ERROR;\r
 \r
 Done:\r
+  *Progress = KeywordString + (StringPtr - TempString);\r
+\r
   if (TempString != NULL) {\r
     FreePool (TempString);\r
   }\r
@@ -3283,6 +3324,6 @@ Done:
   if (KeywordData != NULL) {\r
     FreePool (KeywordData);\r
   }\r
-  *Progress = StringPtr;\r
+\r
   return Status;\r
 }\r