]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPlatformPkg/Bds/BdsHelper.c
ArmPlatformPkg: Print arguments for EFI Application
[mirror_edk2.git] / ArmPlatformPkg / Bds / BdsHelper.c
index 459ebc39fcbdd5afc266d1675336d0d885138aa2..152061d0f717e369148277e972dc332f2d82d233 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
 *\r
-*  Copyright (c) 2011-2012, ARM Limited. All rights reserved.\r
+*  Copyright (c) 2011 - 2014, ARM Limited. All rights reserved.\r
 *  \r
 *  This program and the accompanying materials                          \r
 *  are licensed and made available under the terms and conditions of the BSD License         \r
@@ -26,9 +26,16 @@ EditHIInputStr (
   EFI_INPUT_KEY   Key;\r
   EFI_STATUS      Status;\r
 \r
+  // The command line must be at least one character long\r
+  ASSERT (MaxCmdLine > 0);\r
+\r
+  // Ensure the last character of the buffer is the NULL character\r
+  CmdLine[MaxCmdLine - 1] = '\0';\r
+\r
   Print (CmdLine);\r
 \r
-  for (CmdLineIndex = StrLen (CmdLine); CmdLineIndex < MaxCmdLine; ) {\r
+  // To prevent a buffer overflow, we only allow to enter (MaxCmdLine-1) characters\r
+  for (CmdLineIndex = StrLen (CmdLine); CmdLineIndex < MaxCmdLine; ) {
     Status = gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &WaitIndex);\r
     ASSERT_EFI_ERROR (Status);\r
 \r
@@ -45,7 +52,7 @@ EditHIInputStr (
 \r
     if ((Char == CHAR_LINEFEED) || (Char == CHAR_CARRIAGE_RETURN) || (Char == 0x7f)) {\r
       CmdLine[CmdLineIndex] = '\0';\r
-      Print (L"\n\r");\r
+      Print (L"\r\n");\r
 \r
       return EFI_SUCCESS;\r
     } else if ((Key.UnicodeChar == L'\b') || (Key.ScanCode == SCAN_LEFT) || (Key.ScanCode == SCAN_DELETE)){\r
@@ -55,7 +62,7 @@ EditHIInputStr (
       }\r
     } else if ((Key.ScanCode == SCAN_ESC) || (Char == 0x1B) || (Char == 0x0)) {\r
       return EFI_INVALID_PARAMETER;\r
-    } else {\r
+    } else if (CmdLineIndex < (MaxCmdLine-1)) {
       CmdLine[CmdLineIndex++] = Key.UnicodeChar;\r
       Print (L"%c", Key.UnicodeChar);\r
     }\r
@@ -180,7 +187,7 @@ GetHIInputBoolean (
 \r
   while(1) {\r
     Print (L"[y/n] ");\r
-    Status = GetHIInputStr (CmdBoolean, 2);\r
+    Status = GetHIInputStr (CmdBoolean, 2);
     if (EFI_ERROR(Status)) {\r
       return Status;\r
     } else if ((CmdBoolean[0] == L'y') || (CmdBoolean[0] == L'Y')) {\r
@@ -198,7 +205,8 @@ HasFilePathEfiExtension (
   IN CHAR16* FilePath\r
   )\r
 {\r
-  return (StrCmp (FilePath + (StrSize(FilePath)/sizeof(CHAR16)) - 5, L".efi") == 0);\r
+  return (StrCmp (FilePath + (StrSize (FilePath) / sizeof (CHAR16)) - 5, L".EFI") == 0) ||\r
+         (StrCmp (FilePath + (StrSize (FilePath) / sizeof (CHAR16)) - 5, L".efi") == 0);\r
 }\r
 \r
 // Return the last non end-type Device Path Node from a Device Path\r
@@ -315,3 +323,94 @@ GetAlignedDevicePath (
   }\r
 }\r
 \r
+BOOLEAN\r
+IsUnicodeString (\r
+  IN VOID* String\r
+  )\r
+{\r
+  // We do not support NULL pointer\r
+  ASSERT (String != NULL);\r
+\r
+  if (*(CHAR16*)String < 0x100) {\r
+    //Note: We could get issue if the string is an empty Ascii string...\r
+    return TRUE;\r
+  } else {\r
+    return FALSE;\r
+  }\r
+}\r
+\r
+/*\r
+ * Try to detect if the given string is an ASCII or Unicode string\r
+ *\r
+ * There are actually few limitation to this function but it is mainly to give\r
+ * a user friendly output.\r
+ *\r
+ * Some limitations:\r
+ *   - it only supports unicode string that use ASCII character (< 0x100)\r
+ *   - single character ASCII strings are interpreted as Unicode string\r
+ *   - string cannot be longer than 2 x BOOT_DEVICE_OPTION_MAX (600 bytes)\r
+ *\r
+ * @param String    Buffer that might contain a Unicode or Ascii string\r
+ * @param IsUnicode If not NULL this boolean value returns if the string is an\r
+ *                  ASCII or Unicode string.\r
+ */\r
+BOOLEAN\r
+IsPrintableString (\r
+  IN  VOID*    String,\r
+  OUT BOOLEAN *IsUnicode\r
+  )\r
+{\r
+  BOOLEAN UnicodeDetected;\r
+  BOOLEAN IsPrintable;\r
+  UINTN Index;\r
+  CHAR16 Character;\r
+\r
+  // We do not support NULL pointer\r
+  ASSERT (String != NULL);\r
+\r
+  // Test empty string\r
+  if (*(CHAR16*)String == L'\0') {\r
+    if (IsUnicode) {\r
+      *IsUnicode = TRUE;\r
+    }\r
+    return TRUE;\r
+  } else if (*(CHAR16*)String == '\0') {\r
+    if (IsUnicode) {\r
+      *IsUnicode = FALSE;\r
+    }\r
+    return TRUE;\r
+  }\r
+\r
+  // Limitation: if the string is an ASCII single character string. This comparison\r
+  // will assume it is a Unicode string.\r
+  if (*(CHAR16*)String < 0x100) {\r
+    UnicodeDetected = TRUE;\r
+  } else {\r
+    UnicodeDetected = FALSE;\r
+  }\r
+\r
+  IsPrintable = FALSE;\r
+  for (Index = 0; Index < BOOT_DEVICE_OPTION_MAX * 2; Index++) {\r
+    if (UnicodeDetected) {\r
+      Character = ((CHAR16*)String)[Index];\r
+    } else {\r
+      Character = ((CHAR8*)String)[Index];\r
+    }\r
+\r
+    if (Character == '\0') {\r
+      // End of the string\r
+      IsPrintable = TRUE;\r
+      break;\r
+    } else if ((Character < 0x20) || (Character > 0x7f)) {\r
+      // We only support the range of printable ASCII character\r
+      IsPrintable = FALSE;\r
+      break;\r
+    }\r
+  }\r
+\r
+  if (IsPrintable && IsUnicode) {\r
+    *IsUnicode = UnicodeDetected;\r
+  }\r
+\r
+  return IsPrintable;\r
+}\r