]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellParametersProtocol.c
ShellPkg: Add checking for memory allocation and pointer returns from functions.
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellParametersProtocol.c
index 914853fdbf991da0f2435bff377652039a8167ce..55cf3c279eb74401d6d62ed64b3095094f80a42d 100644 (file)
@@ -2,7 +2,7 @@
   Member functions of EFI_SHELL_PARAMETERS_PROTOCOL and functions for creation,\r
   manipulation, and initialization of EFI_SHELL_PARAMETERS_PROTOCOL.\r
 \r
-  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2011, 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
@@ -14,6 +14,7 @@
 **/\r
 \r
 #include "ShellParametersProtocol.h"\r
+#include "ConsoleWrappers.h"\r
 \r
 /**\r
   return the next parameter from a command line string;\r
@@ -25,9 +26,9 @@
   Temp Parameter must be large enough to hold the parameter before calling this\r
   function.\r
 \r
-  @param[in,out] Walker        pointer to string of command line.  Adjusted to\r
+  @param[in, out] Walker        pointer to string of command line.  Adjusted to\r
                                 reminaing command line on return\r
-  @param[in,out] TempParameter pointer to string of command line item extracted.\r
+  @param[in, out] TempParameter pointer to string of command line item extracted.\r
 \r
 **/\r
 VOID\r
@@ -68,14 +69,8 @@ GetNextParameter(
   if ((*Walker)[0] == L'\"') {\r
     NextDelim = NULL;\r
     for (TempLoc = *Walker + 1 ; TempLoc != NULL && *TempLoc != CHAR_NULL ; TempLoc++) {\r
-      if (*TempLoc == L'^' && *(TempLoc+1) == L'^') {\r
+      if (*TempLoc == L'^' && *(TempLoc+1) == L'\"') {\r
         TempLoc++;\r
-      } else if (*TempLoc == L'^' && *(TempLoc+1) == L'\"') {\r
-        TempLoc++;\r
-      } else if (*TempLoc == L'^' && *(TempLoc+1) == L'|') {\r
-        TempLoc++;\r
-      } else if (*TempLoc == L'^') {\r
-        *TempLoc = L' ';\r
       } else if (*TempLoc == L'\"') {\r
         NextDelim = TempLoc;\r
         break;\r
@@ -99,10 +94,7 @@ GetNextParameter(
       *Walker = NULL;\r
     }\r
     for (TempLoc = *TempParameter ; TempLoc != NULL && *TempLoc != CHAR_NULL ; TempLoc++) {\r
-      if ((*TempLoc == L'^' && *(TempLoc+1) == L'^')\r
-       || (*TempLoc == L'^' && *(TempLoc+1) == L'|')\r
-       || (*TempLoc == L'^' && *(TempLoc+1) == L'\"')\r
-      ){\r
+      if (*TempLoc == L'^' && *(TempLoc+1) == L'\"') {\r
         CopyMem(TempLoc, TempLoc+1, StrSize(TempLoc) - sizeof(TempLoc[0]));\r
       }\r
     }\r
@@ -126,8 +118,6 @@ GetNextParameter(
     for (NextDelim = *TempParameter ; NextDelim != NULL && *NextDelim != CHAR_NULL ; NextDelim++) {\r
       if (*NextDelim == L'^' && *(NextDelim+1) == L'^') {\r
         CopyMem(NextDelim, NextDelim+1, StrSize(NextDelim) - sizeof(NextDelim[0]));\r
-      } else if (*NextDelim == L'^') {\r
-        *NextDelim = L' ';\r
       }\r
     }\r
     while ((*TempParameter)[StrLen(*TempParameter)-1] == L' ') {\r
@@ -141,15 +131,15 @@ GetNextParameter(
 }\r
 \r
 /**\r
-  function to populate Argc and Argv.\r
+  Function to populate Argc and Argv.\r
 \r
   This function parses the CommandLine and divides it into standard C style Argc/Argv\r
   parameters for inclusion in EFI_SHELL_PARAMETERS_PROTOCOL.  this supports space\r
   delimited and quote surrounded parameter definition.\r
 \r
-  @param[in] CommandLine        String of command line to parse\r
-  @param[in,out] Argv           pointer to array of strings; one for each parameter\r
-  @param[in,out] Argc           pointer to number of strings in Argv array\r
+  @param[in] CommandLine         String of command line to parse\r
+  @param[in, out] Argv           pointer to array of strings; one for each parameter\r
+  @param[in, out] Argc           pointer to number of strings in Argv array\r
 \r
   @return EFI_SUCCESS           the operation was sucessful\r
   @return EFI_OUT_OF_RESOURCES  a memory allocation failed.\r
@@ -225,9 +215,9 @@ ParseCommandLineToArgs(
   installs it on our handle and if there is an existing version of the protocol\r
   that one is cached for removal later.\r
 \r
-  @param[in,out] NewShellParameters on a successful return, a pointer to pointer\r
+  @param[in, out] NewShellParameters on a successful return, a pointer to pointer\r
                                      to the newly installed interface.\r
-  @param[in,out] RootShellInstance  on a successful return, pointer to boolean.\r
+  @param[in, out] RootShellInstance  on a successful return, pointer to boolean.\r
                                      TRUE if this is the root shell instance.\r
 \r
   @retval EFI_SUCCESS               the operation completed successfully.\r
@@ -280,7 +270,9 @@ CreatePopulateInstallShellParametersProtocol (
   // Allocate the new structure\r
   //\r
   *NewShellParameters = AllocateZeroPool(sizeof(EFI_SHELL_PARAMETERS_PROTOCOL));\r
-  ASSERT(NewShellParameters != NULL);\r
+  if ((*NewShellParameters) == NULL) {\r
+    return (EFI_OUT_OF_RESOURCES);\r
+  }\r
 \r
   //\r
   // get loaded image protocol\r
@@ -307,19 +299,22 @@ CreatePopulateInstallShellParametersProtocol (
     // no parameters via environment... ok\r
     //\r
   } else {\r
-    ASSERT_EFI_ERROR(Status);\r
+    if (EFI_ERROR(Status)) {\r
+      return (Status);\r
+    }\r
   }\r
   if (Size == 0 && LoadedImage->LoadOptionsSize != 0) {\r
+    ASSERT(FullCommandLine == NULL);\r
     //\r
     // Now we need to include a NULL terminator in the size.\r
     //\r
     Size = LoadedImage->LoadOptionsSize + sizeof(FullCommandLine[0]);\r
     FullCommandLine = AllocateZeroPool(Size);\r
   }\r
-  if (LoadedImage->LoadOptionsSize != 0){\r
-    StrCpy(FullCommandLine, LoadedImage->LoadOptions);\r
-  }\r
   if (FullCommandLine != NULL) {\r
+    if (LoadedImage->LoadOptionsSize != 0){\r
+      StrCpy(FullCommandLine, LoadedImage->LoadOptions);\r
+    }\r
     //\r
     // Populate Argc and Argv\r
     //\r
@@ -413,18 +408,106 @@ CleanUpShellParametersProtocol (
   return (Status);\r
 }\r
 \r
+/**\r
+  Determin if a file name represents a unicode file.\r
+\r
+  @param[in] FileName     Pointer to the filename to open.\r
+\r
+  @retval EFI_SUCCESS     The file is a unicode file.\r
+  @return An error upon failure.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+IsUnicodeFile(\r
+  IN CONST CHAR16 *FileName\r
+  )\r
+{\r
+  SHELL_FILE_HANDLE Handle;\r
+  EFI_STATUS        Status;\r
+  UINT64            OriginalFilePosition;\r
+  UINTN             CharSize;\r
+  CHAR16            CharBuffer;\r
+\r
+  Status = gEfiShellProtocol->OpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ);\r
+  if (EFI_ERROR(Status)) {\r
+    return (Status);\r
+  }\r
+  gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);\r
+  gEfiShellProtocol->SetFilePosition(Handle, 0);\r
+  CharSize = sizeof(CHAR16);\r
+  Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);\r
+  if (EFI_ERROR(Status) || CharBuffer != gUnicodeFileTag) {\r
+    Status = EFI_BUFFER_TOO_SMALL;\r
+  }\r
+  gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r
+  gEfiShellProtocol->CloseFile(Handle);\r
+  return (Status);  \r
+}\r
+\r
+/**\r
+  Strips out quotes sections of a string.\r
+\r
+  All of the characters between quotes is replaced with spaces.\r
+\r
+  @param[in, out] TheString  A pointer to the string to update.\r
+**/\r
+VOID\r
+EFIAPI\r
+StripQuotes (\r
+  IN OUT CHAR16 *TheString\r
+  )\r
+{\r
+  BOOLEAN RemoveNow;\r
+\r
+  for (RemoveNow = FALSE ; TheString != NULL && *TheString != CHAR_NULL ; TheString++) {\r
+    if (*TheString == L'^' && *(TheString + 1) == L'\"') {\r
+      TheString++;\r
+    } else if (*TheString == L'\"') {\r
+      RemoveNow = (BOOLEAN)!RemoveNow;\r
+    } else if (RemoveNow) {\r
+      *TheString = L' ';\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Calcualte the 32-bit CRC in a EFI table using the service provided by the\r
+  gRuntime service.\r
+\r
+  @param  Hdr                    Pointer to an EFI standard header\r
+\r
+**/\r
+VOID\r
+CalculateEfiHdrCrc (\r
+  IN  OUT EFI_TABLE_HEADER    *Hdr\r
+  )\r
+{\r
+  UINT32 Crc;\r
+\r
+  Hdr->CRC32 = 0;\r
+\r
+  //\r
+  // If gBS->CalculateCrce32 () == CoreEfiNotAvailableYet () then\r
+  //  Crc will come back as zero if we set it to zero here\r
+  //\r
+  Crc = 0;\r
+  gBS->CalculateCrc32 ((UINT8 *)Hdr, Hdr->HeaderSize, &Crc);\r
+  Hdr->CRC32 = Crc;\r
+}\r
+\r
 /**\r
   Funcion will replace the current StdIn and StdOut in the ShellParameters protocol\r
   structure by parsing NewCommandLine.  The current values are returned to the\r
   user.\r
 \r
-  If OldStdIn or OldStdOut is NULL then that value is not returned.\r
+  This will also update the system table.\r
 \r
-  @param[in,out] ShellParameters        Pointer to parameter structure to modify.\r
-  @param[in] NewCommandLine             The new command line to parse and use.\r
-  @param[out] OldStdIn                  Pointer to old StdIn.\r
-  @param[out] OldStdOut                 Pointer to old StdOut.\r
-  @param[out] OldStdErr                 Pointer to old StdErr.\r
+  @param[in, out] ShellParameters        Pointer to parameter structure to modify.\r
+  @param[in] NewCommandLine              The new command line to parse and use.\r
+  @param[out] OldStdIn                   Pointer to old StdIn.\r
+  @param[out] OldStdOut                  Pointer to old StdOut.\r
+  @param[out] OldStdErr                  Pointer to old StdErr.\r
+  @param[out] SystemTableInfo            Pointer to old system table information.\r
 \r
   @retval   EFI_SUCCESS                 Operation was sucessful, Argv and Argc are valid.\r
   @retval   EFI_OUT_OF_RESOURCES        A memory allocation failed.\r
@@ -433,10 +516,11 @@ EFI_STATUS
 EFIAPI\r
 UpdateStdInStdOutStdErr(\r
   IN OUT EFI_SHELL_PARAMETERS_PROTOCOL  *ShellParameters,\r
-  IN CONST CHAR16                       *NewCommandLine,\r
+  IN CHAR16                             *NewCommandLine,\r
   OUT SHELL_FILE_HANDLE                 *OldStdIn,\r
   OUT SHELL_FILE_HANDLE                 *OldStdOut,\r
-  OUT SHELL_FILE_HANDLE                 *OldStdErr\r
+  OUT SHELL_FILE_HANDLE                 *OldStdErr,\r
+  OUT SYSTEM_TABLE_INFO                 *SystemTableInfo\r
   )\r
 {\r
   CHAR16            *CommandLineCopy;\r
@@ -458,8 +542,8 @@ UpdateStdInStdOutStdErr(
   UINTN             Size;\r
   CHAR16            TagBuffer[2];\r
   SPLIT_LIST        *Split;\r
+  CHAR16            *FirstLocation;\r
 \r
-  ASSERT(ShellParameters != NULL);\r
   OutUnicode      = TRUE;\r
   InUnicode       = TRUE;\r
   ErrUnicode      = TRUE;\r
@@ -472,24 +556,35 @@ UpdateStdInStdOutStdErr(
   ErrAppend       = FALSE;\r
   OutAppend       = FALSE;\r
   CommandLineCopy = NULL;\r
+  FirstLocation   = NULL;\r
 \r
-  if (OldStdIn != NULL) {\r
-    *OldStdIn = ShellParameters->StdIn;\r
-  }\r
-  if (OldStdOut != NULL) {\r
-    *OldStdOut = ShellParameters->StdOut;\r
-  }\r
-  if (OldStdErr != NULL) {\r
-    *OldStdErr = ShellParameters->StdErr;\r
+  if (ShellParameters == NULL || SystemTableInfo == NULL || OldStdIn == NULL || OldStdOut == NULL || OldStdErr == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
+  SystemTableInfo->ConIn          = gST->ConIn;\r
+  SystemTableInfo->ConInHandle    = gST->ConsoleInHandle;\r
+  SystemTableInfo->ConOut         = gST->ConOut;\r
+  SystemTableInfo->ConOutHandle   = gST->ConsoleOutHandle;\r
+  SystemTableInfo->ConErr         = gST->StdErr;\r
+  SystemTableInfo->ConErrHandle   = gST->StandardErrorHandle;\r
+  *OldStdIn                       = ShellParameters->StdIn;\r
+  *OldStdOut                      = ShellParameters->StdOut;\r
+  *OldStdErr                      = ShellParameters->StdErr;\r
+\r
   if (NewCommandLine == NULL) {\r
     return (EFI_SUCCESS);\r
   }\r
 \r
   CommandLineCopy = StrnCatGrow(&CommandLineCopy, NULL, NewCommandLine, 0);\r
+  if (CommandLineCopy == NULL) {\r
+    return (EFI_OUT_OF_RESOURCES);\r
+  }\r
   Status          = EFI_SUCCESS;\r
   Split           = NULL;\r
+  FirstLocation   = CommandLineCopy + StrLen(CommandLineCopy);\r
+\r
+  StripQuotes(CommandLineCopy);\r
 \r
   if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {\r
     Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);\r
@@ -502,41 +597,78 @@ UpdateStdInStdOutStdErr(
   }\r
 \r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>>v ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 12, L' ');\r
     StdErrVarName   = CommandLineWalker += 6;\r
     ErrAppend       = TRUE;\r
+    if (StrStr(CommandLineWalker, L" 2>>v ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   }\r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>>v ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 12, L' ');\r
     StdOutVarName   = CommandLineWalker += 6;\r
     OutAppend       = TRUE;\r
+    if (StrStr(CommandLineWalker, L" 1>>v ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   } else if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >>v ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 10, L' ');\r
     StdOutVarName   = CommandLineWalker += 5;\r
     OutAppend       = TRUE;\r
+    if (StrStr(CommandLineWalker, L" >>v ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   } else if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >v ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 8, L' ');\r
     StdOutVarName   = CommandLineWalker += 4;\r
     OutAppend       = FALSE;\r
+    if (StrStr(CommandLineWalker, L" >v ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   }\r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>>a ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 12, L' ');\r
     StdOutFileName  = CommandLineWalker += 6;\r
     OutAppend       = TRUE;\r
     OutUnicode      = FALSE;\r
+    if (StrStr(CommandLineWalker, L" 1>>a ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   }\r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>> ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 10, L' ');\r
     if (StdOutFileName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
       StdOutFileName  = CommandLineWalker += 5;\r
       OutAppend       = TRUE;\r
     }\r
+    if (StrStr(CommandLineWalker, L" 1>> ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   } \r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >> ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 8, L' ');\r
     if (StdOutFileName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
       StdOutFileName  = CommandLineWalker += 4;\r
       OutAppend       = TRUE;\r
     }\r
+    if (StrStr(CommandLineWalker, L" >> ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   }\r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >>a ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 10, L' ');\r
     if (StdOutFileName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
@@ -544,8 +676,13 @@ UpdateStdInStdOutStdErr(
       OutAppend       = TRUE;\r
       OutUnicode      = FALSE;\r
     }\r
+    if (StrStr(CommandLineWalker, L" >>a ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   } \r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>a ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 10, L' ');\r
     if (StdOutFileName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
@@ -553,8 +690,13 @@ UpdateStdInStdOutStdErr(
       OutAppend       = FALSE;\r
       OutUnicode      = FALSE;\r
     }\r
+    if (StrStr(CommandLineWalker, L" 1>a ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   } \r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >a ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 8, L' ');\r
     if (StdOutFileName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
@@ -562,33 +704,53 @@ UpdateStdInStdOutStdErr(
       OutAppend       = FALSE;\r
       OutUnicode      = FALSE;\r
     }\r
+    if (StrStr(CommandLineWalker, L" >a ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   }\r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>> ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 10, L' ');\r
     if (StdErrFileName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
       StdErrFileName  = CommandLineWalker += 5;\r
       ErrAppend       = TRUE;\r
     }\r
+    if (StrStr(CommandLineWalker, L" 2>> ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   }\r
 \r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>v ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 10, L' ');\r
     if (StdErrVarName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
       StdErrVarName   = CommandLineWalker += 5;\r
       ErrAppend       = FALSE;\r
     }\r
+    if (StrStr(CommandLineWalker, L" 2>v ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   }\r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>v ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 10, L' ');\r
     if (StdOutVarName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
       StdOutVarName   = CommandLineWalker += 5;\r
       OutAppend       = FALSE;\r
     }\r
+    if (StrStr(CommandLineWalker, L" 1>v ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   }\r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>a ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 10, L' ');\r
     if (StdErrFileName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
@@ -596,59 +758,96 @@ UpdateStdInStdOutStdErr(
       ErrAppend       = FALSE;\r
       ErrUnicode      = FALSE;\r
     }\r
+    if (StrStr(CommandLineWalker, L" 2>a ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   }\r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2> ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 8, L' ');\r
     if (StdErrFileName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
       StdErrFileName  = CommandLineWalker += 4;\r
       ErrAppend       = FALSE;\r
     }\r
+    if (StrStr(CommandLineWalker, L" 2> ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   }\r
 \r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1> ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 8, L' ');\r
     if (StdOutFileName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
       StdOutFileName  = CommandLineWalker += 4;\r
       OutAppend       = FALSE;\r
     }\r
+    if (StrStr(CommandLineWalker, L" 1> ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   }\r
 \r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" > ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 6, L' ');\r
     if (StdOutFileName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
       StdOutFileName  = CommandLineWalker += 3;\r
       OutAppend       = FALSE;\r
     }\r
+    if (StrStr(CommandLineWalker, L" > ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
   }\r
 \r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" < ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 6, L' ');\r
     if (StdInFileName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
       StdInFileName  = CommandLineWalker += 3;\r
-      OutAppend       = FALSE;\r
+    }\r
+    if (StrStr(CommandLineWalker, L" < ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
     }\r
   }\r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" <a ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 8, L' ');\r
     if (StdInFileName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
-      StdInFileName  = CommandLineWalker += 4;\r
-      OutAppend       = FALSE;\r
+      StdInFileName   = CommandLineWalker += 4;\r
+      InUnicode       = FALSE;\r
+    }\r
+    if (StrStr(CommandLineWalker, L" <a ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
     }\r
   }\r
   if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" <v ")) != NULL) {\r
+    FirstLocation = MIN(CommandLineWalker, FirstLocation);\r
+    SetMem16(CommandLineWalker, 8, L' ');\r
     if (StdInVarName != NULL) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else {\r
       StdInVarName  = CommandLineWalker += 4;\r
-      OutAppend       = FALSE;\r
+    }\r
+    if (StrStr(CommandLineWalker, L" <v ") != NULL) {\r
+      Status = EFI_NOT_FOUND;\r
     }\r
   }\r
 \r
+  if (FirstLocation != CommandLineCopy + StrLen(CommandLineCopy)\r
+    && ((UINTN)(FirstLocation - CommandLineCopy) < StrLen(NewCommandLine))\r
+    ){\r
+    *(NewCommandLine + (UINTN)(FirstLocation - CommandLineCopy)) = CHAR_NULL;\r
+  }\r
+\r
   if (!EFI_ERROR(Status)) {\r
     if (StdErrFileName != NULL && (CommandLineWalker = StrStr(StdErrFileName, L" ")) != NULL) {\r
       CommandLineWalker[0] = CHAR_NULL;\r
@@ -672,22 +871,53 @@ UpdateStdInStdOutStdErr(
     //\r
     // Verify not the same and not duplicating something from a split\r
     //\r
-    if ((StdErrFileName != NULL && StdOutFileName!= NULL && StringNoCaseCompare(&StdErrFileName, &StdOutFileName) == 0)\r
+    if (\r
+      //\r
+      // Check that no 2 filenames are the same\r
+      //\r
+      (StdErrFileName != NULL && StdOutFileName!= NULL && StringNoCaseCompare(&StdErrFileName, &StdOutFileName) == 0)\r
       ||(StdErrFileName != NULL && StdInFileName != NULL && StringNoCaseCompare(&StdErrFileName, &StdInFileName ) == 0)\r
       ||(StdOutFileName != NULL && StdInFileName != NULL && StringNoCaseCompare(&StdOutFileName, &StdInFileName ) == 0)\r
+      //\r
+      // Check that no 2 variable names are the same\r
+      //\r
       ||(StdErrVarName  != NULL && StdInVarName  != NULL && StringNoCaseCompare(&StdErrVarName , &StdInVarName  ) == 0)\r
       ||(StdOutVarName  != NULL && StdInVarName != NULL && StringNoCaseCompare(&StdOutVarName , &StdInVarName  ) == 0)\r
       ||(StdErrVarName  != NULL && StdOutVarName != NULL && StringNoCaseCompare(&StdErrVarName , &StdOutVarName ) == 0)\r
+      //\r
+      // When a split (using | operator) is in place some are not allowed\r
+      //\r
       ||(Split != NULL && Split->SplitStdIn  != NULL && (StdInVarName  != NULL || StdInFileName  != NULL))\r
       ||(Split != NULL && Split->SplitStdOut != NULL && (StdOutVarName != NULL || StdOutFileName != NULL))\r
+      //\r
+      // Check that nothing is trying to be output to 2 locations.\r
+      //\r
       ||(StdErrFileName != NULL && StdErrVarName != NULL)\r
       ||(StdOutFileName != NULL && StdOutVarName != NULL)\r
       ||(StdInFileName  != NULL && StdInVarName  != NULL)\r
+      //\r
+      // Check for no volatile environment variables\r
+      //\r
       ||(StdErrVarName  != NULL && !IsVolatileEnv(StdErrVarName))\r
       ||(StdOutVarName  != NULL && !IsVolatileEnv(StdOutVarName))\r
+      //\r
+      // Cant redirect during a reconnect operation.\r
+      //\r
+      ||(StrStr(NewCommandLine, L"connect -r") != NULL \r
+         && (StdOutVarName != NULL || StdOutFileName != NULL || StdErrFileName != NULL || StdErrVarName != NULL))\r
+      //\r
+      // Check that filetypes (Unicode/Ascii) do not change during an append\r
+      //\r
+      ||(StdOutFileName != NULL && OutUnicode && OutAppend && (!EFI_ERROR(ShellFileExists(StdOutFileName)) && EFI_ERROR(IsUnicodeFile(StdOutFileName))))\r
+      ||(StdErrFileName != NULL && ErrUnicode && ErrAppend && (!EFI_ERROR(ShellFileExists(StdErrFileName)) && EFI_ERROR(IsUnicodeFile(StdErrFileName))))\r
+      ||(StdOutFileName != NULL && !OutUnicode && OutAppend && (!EFI_ERROR(ShellFileExists(StdOutFileName)) && !EFI_ERROR(IsUnicodeFile(StdOutFileName))))\r
+      ||(StdErrFileName != NULL && !ErrUnicode && ErrAppend && (!EFI_ERROR(ShellFileExists(StdErrFileName)) && !EFI_ERROR(IsUnicodeFile(StdErrFileName))))\r
       ){\r
       Status = EFI_INVALID_PARAMETER;\r
-    } else {\r
+      ShellParameters->StdIn  = *OldStdIn;\r
+      ShellParameters->StdOut = *OldStdOut;\r
+      ShellParameters->StdErr = *OldStdErr;\r
+    } else if (!EFI_ERROR(Status)){\r
       //\r
       // Open the Std<Whatever> and we should not have conflicts here...\r
       //\r
@@ -703,13 +933,12 @@ UpdateStdInStdOutStdErr(
           ShellInfoObject.NewEfiShellProtocol->DeleteFileByName(StdErrFileName);\r
         }\r
         Status = ShellOpenFileByName(StdErrFileName, &TempHandle, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE,0);\r
-        ASSERT(TempHandle != NULL);\r
         if (!ErrAppend && ErrUnicode && !EFI_ERROR(Status)) {\r
           //\r
-          // Write out the UnicodeFileTag\r
+          // Write out the gUnicodeFileTag\r
           //\r
           Size = sizeof(CHAR16);\r
-          TagBuffer[0] = UnicodeFileTag;\r
+          TagBuffer[0] = gUnicodeFileTag;\r
           TagBuffer[1] = CHAR_NULL;\r
           ShellInfoObject.NewEfiShellProtocol->WriteFile(TempHandle, &Size, TagBuffer);\r
         }\r
@@ -719,6 +948,7 @@ UpdateStdInStdOutStdErr(
         }\r
         if (!EFI_ERROR(Status)) {\r
           ShellParameters->StdErr = TempHandle;\r
+          gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle);\r
         }\r
       }\r
 \r
@@ -733,31 +963,37 @@ UpdateStdInStdOutStdErr(
           ShellInfoObject.NewEfiShellProtocol->DeleteFileByName(StdOutFileName);\r
         }\r
         Status = ShellOpenFileByName(StdOutFileName, &TempHandle, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE,0);\r
-        ASSERT(TempHandle != NULL);\r
-        if (!OutAppend && OutUnicode && !EFI_ERROR(Status)) {\r
-          //\r
-          // Write out the UnicodeFileTag\r
-          //\r
-          Size = sizeof(CHAR16);\r
-          TagBuffer[0] = UnicodeFileTag;\r
-          TagBuffer[1] = CHAR_NULL;\r
-          ShellInfoObject.NewEfiShellProtocol->WriteFile(TempHandle, &Size, TagBuffer);\r
-        } else if (OutAppend) {\r
-          //\r
-          // Move to end of file\r
-          //\r
-          Status = ShellInfoObject.NewEfiShellProtocol->GetFileSize(TempHandle, &FileSize);\r
+        if (TempHandle == NULL) {\r
+          Status = EFI_INVALID_PARAMETER;\r
+        } else {\r
+          if (StrStr(StdOutFileName, L"NUL")==StdOutFileName) {\r
+            //no-op\r
+          } else if (!OutAppend && OutUnicode && !EFI_ERROR(Status)) {\r
+            //\r
+            // Write out the gUnicodeFileTag\r
+            //\r
+            Size = sizeof(CHAR16);\r
+            TagBuffer[0] = gUnicodeFileTag;\r
+            TagBuffer[1] = CHAR_NULL;\r
+            ShellInfoObject.NewEfiShellProtocol->WriteFile(TempHandle, &Size, TagBuffer);\r
+          } else if (OutAppend) {\r
+            //\r
+            // Move to end of file\r
+            //\r
+            Status = ShellInfoObject.NewEfiShellProtocol->GetFileSize(TempHandle, &FileSize);\r
+            if (!EFI_ERROR(Status)) {\r
+              Status = ShellInfoObject.NewEfiShellProtocol->SetFilePosition(TempHandle, FileSize);\r
+            }\r
+          }\r
+          if (!OutUnicode && !EFI_ERROR(Status)) {\r
+            TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
+            ASSERT(TempHandle != NULL);\r
+          }\r
           if (!EFI_ERROR(Status)) {\r
-            Status = ShellInfoObject.NewEfiShellProtocol->SetFilePosition(TempHandle, FileSize);\r
+            ShellParameters->StdOut = TempHandle;\r
+            gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle);\r
           }\r
         }\r
-        if (!OutUnicode && !EFI_ERROR(Status)) {\r
-          TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
-          ASSERT(TempHandle != NULL);\r
-        }\r
-        if (!EFI_ERROR(Status)) {\r
-          ShellParameters->StdOut = TempHandle;\r
-        }\r
       }\r
 \r
       //\r
@@ -772,11 +1008,8 @@ UpdateStdInStdOutStdErr(
         }\r
         TempHandle = CreateFileInterfaceEnv(StdOutVarName);\r
         ASSERT(TempHandle != NULL);\r
-        if (!OutUnicode) {\r
-          TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
-          ASSERT(TempHandle != NULL);\r
-        }\r
         ShellParameters->StdOut = TempHandle;\r
+        gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle);\r
       }\r
 \r
       //\r
@@ -791,11 +1024,8 @@ UpdateStdInStdOutStdErr(
         }\r
         TempHandle = CreateFileInterfaceEnv(StdErrVarName);\r
         ASSERT(TempHandle != NULL);\r
-        if (!ErrUnicode) {\r
-          TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
-          ASSERT(TempHandle != NULL);\r
-        }\r
         ShellParameters->StdErr = TempHandle;\r
+        gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle);\r
       }\r
 \r
       //\r
@@ -803,15 +1033,19 @@ UpdateStdInStdOutStdErr(
       //\r
       if (!EFI_ERROR(Status) && StdInVarName != NULL) {\r
         TempHandle = CreateFileInterfaceEnv(StdInVarName);\r
-        if (!InUnicode) {\r
-          TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
-        }\r
-        Size = 0;\r
-        ASSERT(TempHandle != NULL);\r
-        if (((EFI_FILE_PROTOCOL*)TempHandle)->Read(TempHandle, &Size, NULL) != EFI_BUFFER_TOO_SMALL) {\r
-          Status = EFI_INVALID_PARAMETER;\r
+        if (TempHandle == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
         } else {\r
-          ShellParameters->StdIn = TempHandle;\r
+          if (!InUnicode) {\r
+            TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
+          }\r
+          Size = 0;\r
+          if (((EFI_FILE_PROTOCOL*)TempHandle)->Read(TempHandle, &Size, NULL) != EFI_BUFFER_TOO_SMALL) {\r
+            Status = EFI_INVALID_PARAMETER;\r
+          } else {\r
+            ShellParameters->StdIn = TempHandle;\r
+            gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle);\r
+          }\r
         }\r
       }\r
 \r
@@ -829,11 +1063,18 @@ UpdateStdInStdOutStdErr(
         }\r
         if (!EFI_ERROR(Status)) {\r
           ShellParameters->StdIn = TempHandle;\r
+          gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle);\r
         }\r
       }\r
     }\r
   }\r
   FreePool(CommandLineCopy);\r
+\r
+  CalculateEfiHdrCrc(&gST->Hdr);\r
+\r
+  if (gST->ConIn == NULL ||gST->ConOut == NULL) {\r
+    return (EFI_OUT_OF_RESOURCES);\r
+  }\r
   return (Status);\r
 }\r
 \r
@@ -841,38 +1082,71 @@ UpdateStdInStdOutStdErr(
   Funcion will replace the current StdIn and StdOut in the ShellParameters protocol\r
   structure with StdIn and StdOut.  The current values are de-allocated.\r
 \r
-  @param[in,out] ShellParameters       pointer to parameter structure to modify\r
-  @param[out] OldStdIn                 Pointer to old StdIn.\r
-  @param[out] OldStdOut                Pointer to old StdOut.\r
-  @param[out] OldStdErr                Pointer to old StdErr.\r
+  @param[in, out] ShellParameters      Pointer to parameter structure to modify.\r
+  @param[in] OldStdIn                  Pointer to old StdIn.\r
+  @param[in] OldStdOut                 Pointer to old StdOut.\r
+  @param[in] OldStdErr                 Pointer to old StdErr.\r
+  @param[in] SystemTableInfo           Pointer to old system table information.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 RestoreStdInStdOutStdErr (\r
   IN OUT EFI_SHELL_PARAMETERS_PROTOCOL  *ShellParameters,\r
-  OUT SHELL_FILE_HANDLE                 *OldStdIn OPTIONAL,\r
-  OUT SHELL_FILE_HANDLE                 *OldStdOut OPTIONAL,\r
-  OUT SHELL_FILE_HANDLE                 *OldStdErr OPTIONAL\r
+  IN  SHELL_FILE_HANDLE                 *OldStdIn,\r
+  IN  SHELL_FILE_HANDLE                 *OldStdOut,\r
+  IN  SHELL_FILE_HANDLE                 *OldStdErr,\r
+  IN  SYSTEM_TABLE_INFO                 *SystemTableInfo\r
   )\r
 {\r
   SPLIT_LIST        *Split;\r
+\r
+  if (ShellParameters == NULL \r
+    ||OldStdIn        == NULL\r
+    ||OldStdOut       == NULL\r
+    ||OldStdErr       == NULL\r
+    ||SystemTableInfo == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
   if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {\r
     Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);\r
   } else {\r
     Split = NULL;\r
   }\r
-  if (OldStdIn  != NULL && ShellParameters->StdIn  != *OldStdIn) {\r
+  if (ShellParameters->StdIn  != *OldStdIn) {\r
     if ((Split != NULL && Split->SplitStdIn != ShellParameters->StdIn) || Split == NULL) {\r
       gEfiShellProtocol->CloseFile(ShellParameters->StdIn);\r
     }\r
-    ShellParameters->StdIn = OldStdIn==NULL?NULL:*OldStdIn;\r
+    ShellParameters->StdIn = *OldStdIn;\r
   }\r
-  if (OldStdOut != NULL && ShellParameters->StdOut != *OldStdOut) {\r
+  if (ShellParameters->StdOut != *OldStdOut) {\r
     if ((Split != NULL && Split->SplitStdOut != ShellParameters->StdOut) || Split == NULL) {\r
       gEfiShellProtocol->CloseFile(ShellParameters->StdOut);\r
     }\r
-    ShellParameters->StdOut = OldStdOut==NULL?NULL:*OldStdOut;\r
+    ShellParameters->StdOut = *OldStdOut;\r
+  }\r
+  if (ShellParameters->StdErr != *OldStdErr) {\r
+    gEfiShellProtocol->CloseFile(ShellParameters->StdErr);\r
+    ShellParameters->StdErr = *OldStdErr;\r
+  }\r
+\r
+  if (gST->ConIn != SystemTableInfo->ConIn) {\r
+    CloseSimpleTextInOnFile(gST->ConIn);\r
+    gST->ConIn                = SystemTableInfo->ConIn;\r
+    gST->ConsoleInHandle      = SystemTableInfo->ConInHandle;\r
   }\r
+  if (gST->ConOut != SystemTableInfo->ConOut) {\r
+    CloseSimpleTextOutOnFile(gST->ConOut);\r
+    gST->ConOut               = SystemTableInfo->ConOut;\r
+    gST->ConsoleOutHandle     = SystemTableInfo->ConOutHandle;\r
+  }\r
+  if (gST->StdErr != SystemTableInfo->ConErr) {\r
+    CloseSimpleTextOutOnFile(gST->StdErr);\r
+    gST->StdErr               = SystemTableInfo->ConErr;\r
+    gST->StandardErrorHandle  = SystemTableInfo->ConErrHandle;\r
+  }\r
+\r
+  CalculateEfiHdrCrc(&gST->Hdr);\r
+\r
   return (EFI_SUCCESS);\r
 }\r
 /**\r
@@ -882,10 +1156,10 @@ RestoreStdInStdOutStdErr (
 \r
   If OldArgv or OldArgc is NULL then that value is not returned.\r
 \r
-  @param[in,out] ShellParameters        Pointer to parameter structure to modify.\r
-  @param[in] NewCommandLine             The new command line to parse and use.\r
-  @param[out] OldArgv                   Pointer to old list of parameters.\r
-  @param[out] OldArgc                   Pointer to old number of items in Argv list.\r
+  @param[in, out] ShellParameters        Pointer to parameter structure to modify.\r
+  @param[in] NewCommandLine              The new command line to parse and use.\r
+  @param[out] OldArgv                    Pointer to old list of parameters.\r
+  @param[out] OldArgc                    Pointer to old number of items in Argv list.\r
 \r
   @retval   EFI_SUCCESS                 Operation was sucessful, Argv and Argc are valid.\r
   @retval   EFI_OUT_OF_RESOURCES        A memory allocation failed.\r
@@ -916,9 +1190,9 @@ UpdateArgcArgv(
   structure with Argv and Argc.  The current values are de-allocated and the\r
   OldArgv must not be deallocated by the caller.\r
 \r
-  @param[in,out] ShellParameters       pointer to parameter structure to modify\r
-  @param[in] OldArgv                   pointer to old list of parameters\r
-  @param[in] OldArgc                   pointer to old number of items in Argv list\r
+  @param[in, out] ShellParameters       pointer to parameter structure to modify\r
+  @param[in] OldArgv                    pointer to old list of parameters\r
+  @param[in] OldArgc                    pointer to old number of items in Argv list\r
 **/\r
 VOID\r
 EFIAPI\r