]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: pre-verify split commands
authorJaben Carsey <jaben.carsey@intel.com>
Fri, 20 Dec 2013 01:13:36 +0000 (01:13 +0000)
committerjcarsey <jcarsey@6f19259b-4bc3-4df7-8a09-765794883524>
Fri, 20 Dec 2013 01:13:36 +0000 (01:13 +0000)
This makes sure that all parts of commands split via pipe operation are valid before starting.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jaben Carsey <jaben.carsey@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15011 6f19259b-4bc3-4df7-8a09-765794883524

ShellPkg/Application/Shell/Shell.c
ShellPkg/Application/Shell/Shell.h

index efb718d05a1b7e96a197a3afd55f5cda098f457a..7e4c891d316cdc49fb0d0ce7ec48440ed6100002 100644 (file)
@@ -1596,6 +1596,96 @@ GetOperationType(
   return (UNKNOWN_INVALID);\r
 }\r
 \r
+EFI_STATUS \r
+EFIAPI\r
+IsValidSplit(\r
+  IN CONST CHAR16 *CmdLine\r
+  )\r
+{\r
+  CHAR16        *Temp;\r
+  CHAR16        *FirstParameter;\r
+  CHAR16        *TempWalker;\r
+  EFI_STATUS    Status;\r
+\r
+  Temp           = NULL;\r
+\r
+  Temp = StrnCatGrow(&Temp, NULL, CmdLine, 0);\r
+  if (Temp == NULL) {\r
+    return (EFI_OUT_OF_RESOURCES);\r
+  }\r
+\r
+  FirstParameter = StrStr(Temp, L"|");\r
+  if (FirstParameter != NULL) {\r
+    *FirstParameter = CHAR_NULL;\r
+  }\r
+\r
+  FirstParameter = NULL;\r
+\r
+  //\r
+  // Process the command line\r
+  //\r
+  Status = ProcessCommandLineToFinal(&Temp);\r
+\r
+  if (!EFI_ERROR(Status)) {\r
+    FirstParameter = AllocateZeroPool(StrSize(CmdLine));\r
+    if (FirstParameter == NULL) {\r
+      SHELL_FREE_NON_NULL(Temp);\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
+    TempWalker = (CHAR16*)Temp;\r
+    GetNextParameter(&TempWalker, &FirstParameter);\r
+\r
+    if (GetOperationType(FirstParameter) == UNKNOWN_INVALID) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
+      SetLastError(SHELL_NOT_FOUND);\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
+  }\r
+\r
+  SHELL_FREE_NON_NULL(Temp);\r
+  SHELL_FREE_NON_NULL(FirstParameter);\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Determine if a command line contains with a split contains only valid commands\r
+\r
+  @param[in] CmdLine      The command line to parse.\r
+\r
+  @retval EFI_SUCCESS     CmdLine has only valid commands, application, or has no split.\r
+  @retval EFI_ABORTED     CmdLine has at least one invalid command or application.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VerifySplit(\r
+  IN CONST CHAR16 *CmdLine\r
+  )\r
+{\r
+  CONST CHAR16  *TempSpot;\r
+  EFI_STATUS    Status;\r
+\r
+  //\r
+  // Verify up to the pipe or end character\r
+  //\r
+  Status = IsValidSplit(CmdLine);\r
+  if (EFI_ERROR(Status)) {\r
+    return (Status);\r
+  }\r
+\r
+  //\r
+  // If this was the only item, then get out\r
+  //\r
+  if (!ContainsSplit(CmdLine)) {\r
+    return (EFI_SUCCESS);\r
+  }\r
+\r
+  //\r
+  // recurse to verify the next item\r
+  //\r
+  TempSpot = FindSplit(CmdLine)+1;\r
+  return (VerifySplit(TempSpot));\r
+}\r
+\r
 /**\r
   Process a split based operation.\r
 \r
@@ -1613,6 +1703,11 @@ ProcessNewSplitCommandLine(
   SPLIT_LIST                *Split;\r
   EFI_STATUS                Status;\r
 \r
+  Status = VerifySplit(CmdLine);\r
+  if (EFI_ERROR(Status)) {\r
+    return (Status);\r
+  }\r
+\r
   Split = NULL;\r
 \r
   //\r
@@ -1728,7 +1823,7 @@ DoHelpUpdate(
 EFI_STATUS\r
 EFIAPI\r
 SetLastError(\r
-  IN CONST UINT64   ErrorCode\r
+  IN CONST SHELL_STATUS   ErrorCode\r
   )\r
 {\r
   CHAR16 LeString[19];\r
@@ -1943,7 +2038,7 @@ RunCommandOrFile(
       //\r
       if (!EFI_ERROR(ShellIsDirectory(CommandWithPath))) {\r
         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
-        SetLastError(EFI_NOT_FOUND);\r
+        SetLastError(SHELL_NOT_FOUND);\r
       }\r
       switch (Type) {\r
         case   SCRIPT_FILE_NAME:\r
@@ -2129,7 +2224,7 @@ RunCommand(
       // Whatever was typed, it was invalid.\r
       //\r
       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
-      SetLastError(EFI_NOT_FOUND);\r
+      SetLastError(SHELL_NOT_FOUND);\r
       break;\r
   }\r
  \r
index 4943fd19cbcb2b90fea35feb028cf2f60702ed33..9e5338244b60077798348a24db5c5ce205447960 100644 (file)
@@ -132,6 +132,32 @@ typedef enum {
   UNKNOWN_INVALID\r
 } SHELL_OPERATION_TYPES;\r
 \r
+/**\r
+  Converts the command line to it's post-processed form.  this replaces variables and alias' per UEFI Shell spec.\r
+\r
+  @param[in,out] CmdLine        pointer to the command line to update\r
+\r
+  @retval EFI_SUCCESS           The operation was successful\r
+  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+  @return                       some other error occured\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ProcessCommandLineToFinal(\r
+  IN OUT CHAR16 **CmdLine\r
+  );\r
+\r
+/**\r
+  Function to update the shell variable "lasterror"\r
+\r
+  @param[in] ErrorCode      the error code to put into lasterror\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SetLastError(\r
+  IN CONST SHELL_STATUS   ErrorCode\r
+  );\r
+\r
 /**\r
   Sets all the alias' that were registered with the ShellCommandLib library.\r
 \r