]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/Shell.c
Add initializing code for local variable 'CalleeExitStatus' and 'ExitStatus' in ...
[mirror_edk2.git] / ShellPkg / Application / Shell / Shell.c
index fe722e955448bd0c60d6901da4bf3f9d350e85fc..18055d5a85adb57f0cd9ca31c5f925abe70ca9e3 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   This is THE shell (application)\r
 \r
-  Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
   Copyright (c) 2013, Hewlett-Packard Development Company, L.P.\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
@@ -71,9 +71,9 @@ STATIC CONST CHAR16 mExecutableExtensions[] = L".NSH;.EFI";
 STATIC CONST CHAR16 mStartupScript[]        = L"startup.nsh";\r
 \r
 /**\r
-  Cleans off leading and trailing spaces and tabs\r
+  Cleans off leading and trailing spaces and tabs.\r
 \r
-  @param[in] String pointer to the string to trim them off\r
+  @param[in] String pointer to the string to trim them off.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -91,9 +91,9 @@ TrimSpaces(
   }\r
 \r
   //\r
-  // Remove any spaces at the end of the (*String).\r
+  // Remove any spaces and tabs at the end of the (*String).\r
   //\r
-  while ((*String)[StrLen((*String))-1] == L' ') {\r
+  while ((StrLen (*String) > 0) && (((*String)[StrLen((*String))-1] == L' ') || ((*String)[StrLen((*String))-1] == L'\t'))) {\r
     (*String)[StrLen((*String))-1] = CHAR_NULL;\r
   }\r
 \r
@@ -107,8 +107,7 @@ TrimSpaces(
 \r
   @retval                 A pointer to the | character in CmdLine or NULL if not present.\r
 **/\r
-CONST\r
-CHAR16*\r
+CONST CHAR16*\r
 EFIAPI\r
 FindSplit(\r
   IN CONST CHAR16 *CmdLine\r
@@ -245,6 +244,9 @@ UefiMain (
   UINTN                           Size;\r
   EFI_HANDLE                      ConInHandle;\r
   EFI_SIMPLE_TEXT_INPUT_PROTOCOL  *OldConIn;\r
+  UINTN                           ExitDataSize;\r
+  CHAR16                          *ExitData;\r
+  SHELL_STATUS                    ExitStatus;\r
 \r
   if (PcdGet8(PcdShellSupportLevel) > 3) {\r
     return (EFI_UNSUPPORTED);\r
@@ -298,6 +300,12 @@ UefiMain (
   // install our console logger.  This will keep a log of the output for back-browsing\r
   //\r
   Status = ConsoleLoggerInstall(ShellInfoObject.LogScreenCount, &ShellInfoObject.ConsoleInfo);\r
+  if(EFI_ERROR (Status)) {\r
+    ExitStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r
+  } else {\r
+    ExitStatus = SHELL_SUCCESS;\r
+  }\r
+       \r
   if (!EFI_ERROR(Status)) {\r
     //\r
     // Enable the cursor to be visible\r
@@ -407,7 +415,7 @@ UefiMain (
     // Display the mapping\r
     //\r
     if (PcdGet8(PcdShellSupportLevel) >= 2 && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap) {\r
-      Status = RunCommand(L"map");\r
+      Status = RunCommand(L"map", NULL);\r
       ASSERT_EFI_ERROR(Status);\r
     }\r
 \r
@@ -473,7 +481,11 @@ UefiMain (
         //\r
         // process the startup script or launch the called app.\r
         //\r
-        Status = DoStartupScript(ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath);\r
+        Status = DoStartupScript(\r
+                  ShellInfoObject.ImageDevPath,\r
+                  ShellInfoObject.FileDevPath,\r
+                  &ExitStatus\r
+                  );\r
       }\r
 \r
       if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit && !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r
@@ -506,6 +518,7 @@ UefiMain (
           //\r
           Status = DoShellPrompt();\r
         } while (!ShellCommandGetExit());\r
+        ExitStatus = (SHELL_STATUS) ShellCommandGetExitCode();\r
       }\r
       if (OldConIn != NULL && ConInHandle != NULL) {\r
         CloseSimpleTextInOnFile (gST->ConIn);\r
@@ -576,10 +589,29 @@ UefiMain (
     DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;);\r
   }\r
 \r
-  if (ShellCommandGetExit()) {\r
-    return ((EFI_STATUS)ShellCommandGetExitCode());\r
+  // If the command exited with an error, we pass this error out in the ExitData\r
+  // so that it can be retrieved by the EfiShellExecute function (which may\r
+  // start the shell with gBS->StartImage)\r
+  if (ExitStatus != SHELL_SUCCESS) {\r
+    // Allocate a buffer for exit data to pass to gBS->Exit().\r
+    // This buffer will contain the empty string immediately followed by\r
+    // the shell's exit status. (The empty string is required by the UEFI spec)\r
+    ExitDataSize = (sizeof (CHAR16) + sizeof (SHELL_STATUS));\r
+    ExitData = AllocatePool (ExitDataSize);\r
+    if (ExitData == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    ExitData[0] = '\0';\r
+    // Use CopyMem to avoid alignment faults\r
+    CopyMem ((ExitData + 1), &ExitStatus, sizeof (ExitStatus));\r
+\r
+    gBS->Exit (ImageHandle, EFI_ABORTED, ExitDataSize, ExitData);\r
+  } else {\r
+    return EFI_SUCCESS;\r
   }\r
-  return (Status);\r
+\r
+  ASSERT (FALSE);\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -873,13 +905,16 @@ ProcessCommandLine(
   @param ImagePath              the path to the image for shell.  first place to look for the startup script\r
   @param FilePath               the path to the file for shell.  second place to look for the startup script.\r
 \r
+  @param[out] ExitStatus      The exit code of the script. Ignored if NULL.\r
+\r
   @retval EFI_SUCCESS           the variable is initialized.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 DoStartupScript(\r
-  EFI_DEVICE_PATH_PROTOCOL *ImagePath,\r
-  EFI_DEVICE_PATH_PROTOCOL *FilePath\r
+  IN  EFI_DEVICE_PATH_PROTOCOL *ImagePath,\r
+  IN  EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
+  OUT SHELL_STATUS             *ExitStatus\r
   )\r
 {\r
   EFI_STATUS                    Status;\r
@@ -914,7 +949,7 @@ DoStartupScript(
       StrCat(FileStringPath, L" ");\r
       StrCat(FileStringPath, ShellInfoObject.ShellInitSettings.FileOptions);\r
     }\r
-    Status = RunCommand(FileStringPath);\r
+    Status = RunCommand(FileStringPath, ExitStatus);\r
     FreePool(FileStringPath);\r
     return (Status);\r
 \r
@@ -991,7 +1026,13 @@ DoStartupScript(
   // If we got a file, run it\r
   //\r
   if (!EFI_ERROR(Status) && FileHandle != NULL) {\r
-    Status = RunScriptFileHandle (FileHandle, mStartupScript);\r
+    Status = RunScriptFile (\r
+              mStartupScript,\r
+              FileHandle,\r
+              L"",\r
+              ShellInfoObject.NewShellParametersProtocol,\r
+              ExitStatus\r
+              );\r
     ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle);\r
   } else {\r
     FileStringPath = ShellFindFilePath(mStartupScript);\r
@@ -1002,7 +1043,13 @@ DoStartupScript(
       Status = EFI_SUCCESS;\r
       ASSERT(FileHandle == NULL);\r
     } else {\r
-      Status = RunScriptFile(FileStringPath);\r
+      Status = RunScriptFile(\r
+                FileStringPath,\r
+                NULL,\r
+                L"",\r
+                ShellInfoObject.NewShellParametersProtocol,\r
+                ExitStatus\r
+                );\r
       FreePool(FileStringPath);\r
     }\r
   }\r
@@ -1067,8 +1114,8 @@ DoShellPrompt (
   //\r
   if (!EFI_ERROR (Status)) {\r
     CmdLine[BufferSize / sizeof (CHAR16)] = CHAR_NULL;\r
-    Status = RunCommand(CmdLine);\r
-  }\r
+    Status = RunCommand(CmdLine, NULL);\r
+    }\r
 \r
   //\r
   // Done with this command\r
@@ -1327,6 +1374,9 @@ ShellConvertVariables (
   @param[in] StdIn          The pointer to the Standard input.\r
   @param[in] StdOut         The pointer to the Standard output.\r
 \r
+  @param[out] ExitStatus      The exit code of the last command in the pipeline.\r
+                              Ignored if NULL.\r
+\r
   @retval EFI_SUCCESS       The split command is executed successfully.\r
   @retval other             Some error occurs when executing the split command.\r
 **/\r
@@ -1335,7 +1385,8 @@ EFIAPI
 RunSplitCommand(\r
   IN CONST CHAR16             *CmdLine,\r
   IN       SHELL_FILE_HANDLE  *StdIn,\r
-  IN       SHELL_FILE_HANDLE  *StdOut\r
+  IN       SHELL_FILE_HANDLE  *StdOut,\r
+  OUT      SHELL_STATUS       *ExitStatus\r
   )\r
 {\r
   EFI_STATUS        Status;\r
@@ -1389,7 +1440,7 @@ RunSplitCommand(
   ASSERT(Split->SplitStdOut != NULL);\r
   InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link);\r
 \r
-  Status = RunCommand(OurCommandLine);\r
+  Status = RunCommand(OurCommandLine, NULL);\r
 \r
   //\r
   // move the output from the first to the in to the second.\r
@@ -1404,7 +1455,7 @@ RunSplitCommand(
   ShellInfoObject.NewEfiShellProtocol->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn), 0);\r
 \r
   if (!EFI_ERROR(Status)) {\r
-    Status = RunCommand(NextCommandLine);\r
+    Status = RunCommand(NextCommandLine, ExitStatus);\r
   }\r
 \r
   //\r
@@ -1432,13 +1483,12 @@ RunSplitCommand(
 \r
 /**\r
   Take the original command line, substitute any variables, free \r
-  the original string, return the modified copy\r
+  the original string, return the modified copy.\r
 \r
-  @param[in,out] CmdLine  pointer to the command line to update\r
-  @param[out]CmdName      upon successful return the name of the command to be run\r
+  @param[in] CmdLine  pointer to the command line to update.\r
 \r
-  @retval EFI_SUCCESS           the function was successful\r
-  @retval EFI_OUT_OF_RESOURCES  a memory allocation failed\r
+  @retval EFI_SUCCESS           the function was successful.\r
+  @retval EFI_OUT_OF_RESOURCES  a memory allocation failed.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1458,13 +1508,12 @@ ShellSubstituteVariables(
 \r
 /**\r
   Take the original command line, substitute any alias in the first group of space delimited characters, free \r
-  the original string, return the modified copy\r
+  the original string, return the modified copy.\r
 \r
-  @param[in] CmdLine  pointer to the command line to update\r
-  @param[out]CmdName  upon successful return the name of the command to be run\r
+  @param[in] CmdLine  pointer to the command line to update.\r
 \r
-  @retval EFI_SUCCESS           the function was successful\r
-  @retval EFI_OUT_OF_RESOURCES  a memory allocation failed\r
+  @retval EFI_SUCCESS           the function was successful.\r
+  @retval EFI_OUT_OF_RESOURCES  a memory allocation failed.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1530,13 +1579,13 @@ ShellSubstituteAliases(
 /**\r
   Takes the Argv[0] part of the command line and determine the meaning of it.\r
 \r
-  @param[in] CmdLine  pointer to the command line to update\r
+  @param[in] CmdName  pointer to the command line to update.\r
   \r
-  @retval INTERNAL_COMMAND    The name is an internal command\r
-  @retval FILE_SYS_CHANGE     the name is a file system change\r
-  @retval SCRIPT_FILE_NAME    the name is a NSH script file\r
-  @retval UNKNOWN_INVALID     the name is unknown\r
-  @retval EFI_APPLICATION     the name is an application (.EFI)\r
+  @retval Internal_Command    The name is an internal command.\r
+  @retval File_Sys_Change     the name is a file system change.\r
+  @retval Script_File_Name    the name is a NSH script file.\r
+  @retval Unknown_Invalid     the name is unknown.\r
+  @retval Efi_Application     the name is an application (.EFI).\r
 **/\r
 SHELL_OPERATION_TYPES\r
 EFIAPI\r
@@ -1553,7 +1602,7 @@ GetOperationType(
   // test for an internal command.\r
   //\r
   if (ShellCommandIsCommandOnList(CmdName)) {\r
-    return (INTERNAL_COMMAND);\r
+    return (Internal_Command);\r
   }\r
 \r
   //\r
@@ -1561,9 +1610,9 @@ GetOperationType(
   //\r
   if (CmdName[(StrLen(CmdName)-1)] == L':') {\r
     if (StrStr(CmdName, L" ") != NULL) {\r
-      return (UNKNOWN_INVALID);\r
+      return (Unknown_Invalid);\r
     }\r
-    return (FILE_SYS_CHANGE);\r
+    return (File_Sys_Change);\r
   }\r
 \r
   //\r
@@ -1578,7 +1627,7 @@ GetOperationType(
       TempLocation2 = mScriptExtension;\r
       if (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0) {\r
         SHELL_FREE_NON_NULL(FileWithPath);\r
-        return (SCRIPT_FILE_NAME);\r
+        return (Script_File_Name);\r
       }\r
     }\r
 \r
@@ -1586,20 +1635,121 @@ GetOperationType(
     // Was a file, but not a script.  we treat this as an application.\r
     //\r
     SHELL_FREE_NON_NULL(FileWithPath);\r
-    return (EFI_APPLICATION);\r
+    return (Efi_Application);\r
   }\r
   \r
   SHELL_FREE_NON_NULL(FileWithPath);\r
   //\r
   // No clue what this is... return invalid flag...\r
   //\r
-  return (UNKNOWN_INVALID);\r
+  return (Unknown_Invalid);\r
+}\r
+\r
+/**\r
+  Determine if the first item in a command line is valid.\r
+\r
+  @param[in] CmdLine            The command line to parse.\r
+\r
+  @retval EFI_SUCCESS           The item is valid.\r
+  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+  @retval EFI_NOT_FOUND         The operation type is unknown or invalid.\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
-  @param[in] CmdLine    pointer to the command line to process\r
+  @param[in] CmdLine      Pointer to the command line to process\r
+  @param[out] ExitStatus  The exit status of the command. Ignored if NULL.\r
+                          Invalid if this function returns an error.\r
 \r
   @retval EFI_SUCCESS   The operation was successful\r
   @return               an error occured.\r
@@ -1607,12 +1757,18 @@ GetOperationType(
 EFI_STATUS\r
 EFIAPI\r
 ProcessNewSplitCommandLine(\r
-  IN CONST CHAR16 *CmdLine\r
+  IN CONST CHAR16       *CmdLine,\r
+  OUT      SHELL_STATUS *ExitStatus\r
   )\r
 {\r
   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
@@ -1623,9 +1779,14 @@ ProcessNewSplitCommandLine(
   }\r
 \r
   if (Split == NULL) {\r
-    Status = RunSplitCommand(CmdLine, NULL, NULL);\r
+    Status = RunSplitCommand(CmdLine, NULL, NULL, ExitStatus);\r
   } else {\r
-    Status = RunSplitCommand(CmdLine, Split->SplitStdIn, Split->SplitStdOut);\r
+    Status = RunSplitCommand(\r
+              CmdLine,\r
+              Split->SplitStdIn,\r
+              Split->SplitStdOut,\r
+              ExitStatus\r
+              );\r
   }\r
   if (EFI_ERROR(Status)) {\r
     ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, CmdLine);\r
@@ -1634,11 +1795,11 @@ ProcessNewSplitCommandLine(
 }\r
 \r
 /**\r
-  Handle a request to change the current file system\r
+  Handle a request to change the current file system.\r
 \r
-  @param[in] CmdLine  The passed in command line\r
+  @param[in] CmdLine  The passed in command line.\r
 \r
-  @retval EFI_SUCCESS The operation was successful\r
+  @retval EFI_SUCCESS The operation was successful.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1721,14 +1882,14 @@ DoHelpUpdate(
 }\r
 \r
 /**\r
-  Function to update the shell variable "lasterror"\r
+  Function to update the shell variable "lasterror".\r
 \r
-  @param[in] ErrorCode      the error code to put into lasterror\r
+  @param[in] ErrorCode      the error code to put into lasterror.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 SetLastError(\r
-  IN CONST UINT64   ErrorCode\r
+  IN CONST SHELL_STATUS   ErrorCode\r
   )\r
 {\r
   CHAR16 LeString[19];\r
@@ -1754,7 +1915,7 @@ SetLastError(
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-ProcessCommandLineAliasVariable(\r
+ProcessCommandLineToFinal(\r
   IN OUT CHAR16 **CmdLine\r
   )\r
 {\r
@@ -1800,6 +1961,8 @@ ProcessCommandLineAliasVariable(
   @param[in] FirstParameter   the first parameter on the command line\r
   @param[in] ParamProtocol    the shell parameters protocol pointer\r
 \r
+  @param[out] ExitStatus      The exit code of the command. Ignored if NULL.\r
+\r
   @retval EFI_SUCCESS     The command was completed.\r
   @retval EFI_ABORTED     The command's operation was aborted.\r
 **/\r
@@ -1808,7 +1971,8 @@ EFIAPI
 RunInternalCommand(\r
   IN CONST CHAR16                   *CmdLine,\r
   IN       CHAR16                   *FirstParameter,\r
-  IN EFI_SHELL_PARAMETERS_PROTOCOL  *ParamProtocol\r
+  IN EFI_SHELL_PARAMETERS_PROTOCOL  *ParamProtocol,\r
+  OUT      SHELL_STATUS             *ExitStatus OPTIONAL\r
 )\r
 {\r
   EFI_STATUS                Status;\r
@@ -1835,13 +1999,27 @@ RunInternalCommand(
       if (LastError) {\r
         SetLastError(CommandReturnedStatus);\r
       }\r
+      if (ExitStatus != NULL) {\r
+        *ExitStatus = CommandReturnedStatus;\r
+      }\r
 \r
       //\r
       // Pass thru the exitcode from the app.\r
       //\r
       if (ShellCommandGetExit()) {\r
+        //\r
+        // An Exit was requested ("exit" command), pass its value up.\r
+        //\r
         Status = CommandReturnedStatus;\r
-      } else if (CommandReturnedStatus != 0 && IsScriptOnlyCommand(FirstParameter)) {\r
+      } else if (CommandReturnedStatus != SHELL_SUCCESS && IsScriptOnlyCommand(FirstParameter)) {\r
+        //\r
+        // Always abort when a script only command fails for any reason\r
+        //\r
+        Status = EFI_ABORTED;\r
+      } else if (ShellCommandGetCurrentScriptFile() != NULL && CommandReturnedStatus == SHELL_ABORTED) {\r
+        //\r
+        // Abort when in a script and a command aborted\r
+        //\r
         Status = EFI_ABORTED;\r
       }\r
     }\r
@@ -1853,11 +2031,17 @@ RunInternalCommand(
   //\r
   RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r
 \r
-  if (ShellCommandGetCurrentScriptFile() != NULL &&  !IsScriptOnlyCommand(FirstParameter)) {\r
-    //\r
-    // if this is NOT a scipt only command return success so the script won't quit.\r
-    // prevent killing the script - this is the only place where we know the actual command name (after alias and variable replacement...)\r
-    //\r
+  //\r
+  // If a script is running and the command is not a scipt only command, then\r
+  // change return value to success so the script won't halt (unless aborted).\r
+  //\r
+  // Script only commands have to be able halt the script since the script will\r
+  // not operate if they are failing.\r
+  //\r
+  if ( ShellCommandGetCurrentScriptFile() != NULL\r
+    && !IsScriptOnlyCommand(FirstParameter)\r
+    && Status != EFI_ABORTED\r
+    ) {\r
     Status = EFI_SUCCESS;\r
   }\r
 \r
@@ -1872,6 +2056,9 @@ RunInternalCommand(
   @param[in] FirstParameter   the first parameter on the command line\r
   @param[in] ParamProtocol    the shell parameters protocol pointer\r
 \r
+  @param[out] ExitStatus      The exit code of the command or file.\r
+                              Ignored if NULL.\r
+\r
   @retval EFI_SUCCESS     The command was completed.\r
   @retval EFI_ABORTED     The command's operation was aborted.\r
 **/\r
@@ -1881,24 +2068,31 @@ RunCommandOrFile(
   IN       SHELL_OPERATION_TYPES    Type,\r
   IN CONST CHAR16                   *CmdLine,\r
   IN       CHAR16                   *FirstParameter,\r
-  IN EFI_SHELL_PARAMETERS_PROTOCOL  *ParamProtocol\r
+  IN EFI_SHELL_PARAMETERS_PROTOCOL  *ParamProtocol,\r
+  OUT      SHELL_STATUS             *ExitStatus\r
 )\r
 {\r
   EFI_STATUS                Status;\r
-  EFI_STATUS                StatusCode;\r
   CHAR16                    *CommandWithPath;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevPath;\r
+  SHELL_STATUS              CalleeExitStatus;\r
 \r
   Status            = EFI_SUCCESS;\r
   CommandWithPath   = NULL;\r
   DevPath           = NULL;\r
+  CalleeExitStatus  = SHELL_INVALID_PARAMETER;\r
 \r
   switch (Type) {\r
-    case   INTERNAL_COMMAND:\r
-      Status = RunInternalCommand(CmdLine, FirstParameter, ParamProtocol);\r
+    case   Internal_Command:\r
+      Status = RunInternalCommand(\r
+                CmdLine,\r
+                FirstParameter,\r
+                ParamProtocol,\r
+                &CalleeExitStatus\r
+                );\r
       break;\r
-    case   SCRIPT_FILE_NAME:\r
-    case   EFI_APPLICATION:\r
+    case   Script_File_Name:\r
+    case   Efi_Application:\r
       //\r
       // Process a fully qualified path\r
       //\r
@@ -1926,13 +2120,19 @@ 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
-          Status = RunScriptFile (CommandWithPath);\r
+        case   Script_File_Name:\r
+          Status = RunScriptFile (\r
+                    CommandWithPath,\r
+                    NULL,\r
+                    CmdLine,\r
+                    ParamProtocol,\r
+                    &CalleeExitStatus\r
+                    );\r
           break;\r
-        case   EFI_APPLICATION:\r
+        case   Efi_Application:\r
           //\r
           // Get the device path of the application image\r
           //\r
@@ -1950,22 +2150,44 @@ RunCommandOrFile(
             DevPath,\r
             CmdLine,\r
             NULL,\r
-            &StatusCode\r
+            NULL,\r
+            NULL\r
            );\r
 \r
           SHELL_FREE_NON_NULL(DevPath);\r
 \r
+          if(EFI_ERROR (Status)) {\r
+            CalleeExitStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r
+          } else {\r
+            CalleeExitStatus = SHELL_SUCCESS;\r
+          }\r
+\r
           //\r
           // Update last error status.\r
           //\r
-          SetLastError(StatusCode);\r
+          // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS\r
+          SetLastError(CalleeExitStatus);\r
+          break;\r
+        default:\r
+          //\r
+          // Do nothing.\r
+          //\r
           break;\r
       }\r
       break;\r
+    default:\r
+      //\r
+      // Do nothing.\r
+      //\r
+      break;\r
   }\r
 \r
   SHELL_FREE_NON_NULL(CommandWithPath);\r
 \r
+  if (ExitStatus != NULL) {\r
+    *ExitStatus = CalleeExitStatus;\r
+  }\r
+\r
   return (Status);\r
 }\r
 \r
@@ -1977,16 +2199,20 @@ RunCommandOrFile(
   @param[in] FirstParameter   the first parameter on the command line.\r
   @param[in] ParamProtocol    the shell parameters protocol pointer\r
 \r
+  @param[out] ExitStatus      The exit code of the command or file.\r
+                              Ignored if NULL.\r
+\r
   @retval EFI_SUCCESS     The command was completed.\r
   @retval EFI_ABORTED     The command's operation was aborted.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 SetupAndRunCommandOrFile(\r
-  IN SHELL_OPERATION_TYPES          Type,\r
-  IN CHAR16                         *CmdLine,\r
-  IN CHAR16                         *FirstParameter,\r
-  IN EFI_SHELL_PARAMETERS_PROTOCOL  *ParamProtocol\r
+  IN   SHELL_OPERATION_TYPES          Type,\r
+  IN   CHAR16                         *CmdLine,\r
+  IN   CHAR16                         *FirstParameter,\r
+  IN   EFI_SHELL_PARAMETERS_PROTOCOL  *ParamProtocol,\r
+  OUT  SHELL_STATUS                   *ExitStatus\r
 )\r
 {\r
   EFI_STATUS                Status;\r
@@ -2005,7 +2231,13 @@ SetupAndRunCommandOrFile(
   // Now run the command, script, or application\r
   //\r
   if (!EFI_ERROR(Status)) {\r
-    Status = RunCommandOrFile(Type, CmdLine, FirstParameter, ParamProtocol);\r
+    Status = RunCommandOrFile(\r
+              Type,\r
+              CmdLine,\r
+              FirstParameter,\r
+              ParamProtocol,\r
+              ExitStatus\r
+              );\r
   }\r
 \r
   //\r
@@ -2030,6 +2262,7 @@ SetupAndRunCommandOrFile(
   command or dispatch an external application.\r
 \r
   @param[in] CmdLine      The command line to parse.\r
+  @param[out] ExitStatus  The exit code of the command. Ignored if NULL.\r
 \r
   @retval EFI_SUCCESS     The command was completed.\r
   @retval EFI_ABORTED     The command's operation was aborted.\r
@@ -2037,7 +2270,8 @@ SetupAndRunCommandOrFile(
 EFI_STATUS\r
 EFIAPI\r
 RunCommand(\r
-  IN CONST CHAR16   *CmdLine\r
+  IN CONST CHAR16         *CmdLine,\r
+  OUT      SHELL_STATUS   *ExitStatus\r
   )\r
 {\r
   EFI_STATUS                Status;\r
@@ -2069,7 +2303,7 @@ RunCommand(
     return (EFI_SUCCESS);\r
   }\r
 \r
-  Status = ProcessCommandLineAliasVariable(&CleanOriginal);\r
+  Status = ProcessCommandLineToFinal(&CleanOriginal);\r
   if (EFI_ERROR(Status)) {\r
     SHELL_FREE_NON_NULL(CleanOriginal);\r
     return (Status);\r
@@ -2079,7 +2313,7 @@ RunCommand(
   // We dont do normal processing with a split command line (output from one command input to another)\r
   //\r
   if (ContainsSplit(CleanOriginal)) {\r
-    Status = ProcessNewSplitCommandLine(CleanOriginal);\r
+    Status = ProcessNewSplitCommandLine(CleanOriginal, ExitStatus);\r
     SHELL_FREE_NON_NULL(CleanOriginal);\r
     return (Status);\r
   } \r
@@ -2099,20 +2333,26 @@ RunCommand(
   // Depending on the first parameter we change the behavior\r
   //\r
   switch (Type = GetOperationType(FirstParameter)) {\r
-    case   FILE_SYS_CHANGE:\r
+    case   File_Sys_Change:\r
       Status = ChangeMappedDrive(CleanOriginal);\r
       break;\r
-    case   INTERNAL_COMMAND:\r
-    case   SCRIPT_FILE_NAME:\r
-    case   EFI_APPLICATION:\r
-      Status = SetupAndRunCommandOrFile(Type, CleanOriginal, FirstParameter, ShellInfoObject.NewShellParametersProtocol);\r
+    case   Internal_Command:\r
+    case   Script_File_Name:\r
+    case   Efi_Application:\r
+      Status = SetupAndRunCommandOrFile(\r
+                Type,\r
+                CleanOriginal,\r
+                FirstParameter,\r
+                ShellInfoObject.NewShellParametersProtocol,\r
+                ExitStatus\r
+                );\r
       break;\r
     default:\r
       //\r
       // 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
@@ -2160,13 +2400,16 @@ IsValidCommandName(
   @param[in] Handle             The handle to the already opened file.\r
   @param[in] Name               The name of the script file.\r
 \r
+  @param[out] ExitStatus      The exit code of the script. Ignored if NULL.\r
+\r
   @retval EFI_SUCCESS           the script completed sucessfully\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 RunScriptFileHandle (\r
-  IN SHELL_FILE_HANDLE  Handle,\r
-  IN CONST CHAR16       *Name\r
+  IN  SHELL_FILE_HANDLE  Handle,\r
+  IN  CONST CHAR16       *Name,\r
+  OUT SHELL_STATUS       *ExitStatus\r
   )\r
 {\r
   EFI_STATUS          Status;\r
@@ -2182,8 +2425,11 @@ RunScriptFileHandle (
   CONST CHAR16        *CurDir;\r
   UINTN               LineCount;\r
   CHAR16              LeString[50];\r
+  SHELL_STATUS        CalleeExitStatus;\r
 \r
   ASSERT(!ShellCommandGetScriptExit());\r
+  \r
+  CalleeExitStatus = SHELL_SUCCESS;\r
 \r
   PreScriptEchoState = ShellCommandGetEchoState();\r
 \r
@@ -2234,11 +2480,13 @@ RunScriptFileHandle (
   while(!ShellFileHandleEof(Handle)) {\r
     CommandLine = ShellFileHandleReturnLine(Handle, &Ascii);\r
     LineCount++;\r
-    if (CommandLine == NULL || StrLen(CommandLine) == 0) {\r
+    if (CommandLine == NULL || StrLen(CommandLine) == 0 || CommandLine[0] == '#') {\r
+      SHELL_FREE_NON_NULL(CommandLine);\r
       continue;\r
     }\r
     NewScriptFile->CurrentCommand = AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST));\r
     if (NewScriptFile->CurrentCommand == NULL) {\r
+      SHELL_FREE_NON_NULL(CommandLine);\r
       DeleteScriptFileStruct(NewScriptFile);\r
       return (EFI_OUT_OF_RESOURCES);\r
     }\r
@@ -2365,7 +2613,7 @@ RunScriptFileHandle (
             //\r
             PreCommandEchoState = ShellCommandGetEchoState();\r
             ShellCommandSetEchoState(FALSE);\r
-            Status = RunCommand(CommandLine3+1);\r
+            Status = RunCommand(CommandLine3+1, NULL);\r
 \r
             //\r
             // If command was "@echo -off" or "@echo -on" then don't restore echo state\r
@@ -2387,7 +2635,7 @@ RunScriptFileHandle (
               }\r
               ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2);\r
             }\r
-            Status = RunCommand(CommandLine3);\r
+            Status = RunCommand(CommandLine3, NULL);\r
           }\r
         }\r
 \r
@@ -2395,7 +2643,8 @@ RunScriptFileHandle (
           //\r
           // ShellCommandGetExitCode() always returns a UINT64\r
           //\r
-          UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellCommandGetExitCode());\r
+          CalleeExitStatus = (SHELL_STATUS) ShellCommandGetExitCode();\r
+          UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", CalleeExitStatus);\r
           DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r
           InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r
 \r
@@ -2407,9 +2656,11 @@ RunScriptFileHandle (
           break;\r
         }\r
         if (EFI_ERROR(Status)) {\r
+          CalleeExitStatus = (SHELL_STATUS) Status;\r
           break;\r
         }\r
         if (ShellCommandGetExit()) {\r
+          CalleeExitStatus = (SHELL_STATUS) ShellCommandGetExitCode();\r
           break;\r
         }\r
       }\r
@@ -2441,6 +2692,11 @@ RunScriptFileHandle (
   if (ShellCommandGetCurrentScriptFile()==NULL) {\r
     ShellCommandSetEchoState(PreScriptEchoState);\r
   }\r
+\r
+  if (ExitStatus != NULL) {\r
+    *ExitStatus = CalleeExitStatus;\r
+  }\r
+\r
   return (EFI_SUCCESS);\r
 }\r
 \r
@@ -2448,30 +2704,65 @@ RunScriptFileHandle (
   Function to process a NSH script file.\r
 \r
   @param[in] ScriptPath         Pointer to the script file name (including file system path).\r
+  @param[in] Handle             the handle of the script file already opened.\r
+  @param[in] CmdLine            the command line to run.\r
+  @param[in] ParamProtocol      the shell parameters protocol pointer\r
+\r
+  @param[out] ExitStatus      The exit code of the script. Ignored if NULL.\r
 \r
   @retval EFI_SUCCESS           the script completed sucessfully\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 RunScriptFile (\r
-  IN CONST CHAR16 *ScriptPath\r
+  IN  CONST CHAR16                   *ScriptPath,\r
+  IN  SHELL_FILE_HANDLE              Handle OPTIONAL,\r
+  IN  CONST CHAR16                   *CmdLine,\r
+  IN  EFI_SHELL_PARAMETERS_PROTOCOL  *ParamProtocol,\r
+  OUT SHELL_STATUS                   *ExitStatus\r
   )\r
 {\r
   EFI_STATUS          Status;\r
   SHELL_FILE_HANDLE   FileHandle;\r
+  UINTN                     Argc;\r
+  CHAR16                    **Argv;\r
 \r
   if (ShellIsFile(ScriptPath) != EFI_SUCCESS) {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
-  Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r
-  if (EFI_ERROR(Status)) {\r
-    return (Status);\r
-  }\r
+  //\r
+  // get the argc and argv updated for scripts\r
+  //\r
+  Status = UpdateArgcArgv(ParamProtocol, CmdLine, &Argv, &Argc);\r
+  if (!EFI_ERROR(Status)) {\r
 \r
-  Status = RunScriptFileHandle(FileHandle, ScriptPath);\r
+    if (Handle == NULL) {\r
+      //\r
+      // open the file\r
+      //\r
+      Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r
+      if (!EFI_ERROR(Status)) {\r
+        //\r
+        // run it\r
+        //\r
+        Status = RunScriptFileHandle(FileHandle, ScriptPath, ExitStatus);\r
 \r
-  ShellCloseFile(&FileHandle);\r
+        //\r
+        // now close the file\r
+        //\r
+        ShellCloseFile(&FileHandle);\r
+      }\r
+    } else {\r
+      Status = RunScriptFileHandle(Handle, ScriptPath, ExitStatus);\r
+    }\r
+  }\r
+\r
+  //\r
+  // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.\r
+  // This is safe even if the update API failed.  In this case, it may be a no-op.\r
+  //\r
+  RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r
 \r
   return (Status);\r
 }\r