]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellParametersProtocol.c
ShellPkg: Standardized HP Copyright Message String
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellParametersProtocol.c
index b90a2c32bbce2d076b713e0375a5db2f35b3a1f6..00b413e90b4d136f64125c3bce9cb1489a0256d3 100644 (file)
@@ -2,7 +2,9 @@
   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 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (C) 2014, Red Hat, Inc.\r
+  (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>\r
+  Copyright (c) 2009 - 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
 \r
 **/\r
 \r
-#include "ShellParametersProtocol.h"\r
-#include "ConsoleWrappers.h"\r
+#include "Shell.h"\r
 \r
 /**\r
-  return the next parameter from a command line string;\r
+  Return the next parameter's end from a command line string.\r
+\r
+  @param[in] String        the string to parse\r
+**/\r
+CONST CHAR16*\r
+EFIAPI\r
+FindEndOfParameter(\r
+  IN CONST CHAR16 *String\r
+  )\r
+{\r
+  CONST CHAR16 *First;\r
+  CONST CHAR16 *CloseQuote;\r
+\r
+  First = FindFirstCharacter(String, L" \"", L'^');\r
+\r
+  //\r
+  // nothing, all one parameter remaining\r
+  //\r
+  if (*First == CHAR_NULL) {\r
+    return (First);\r
+  }\r
+\r
+  //\r
+  // If space before a quote (or neither found, i.e. both CHAR_NULL),\r
+  // then that's the end.\r
+  //\r
+  if (*First == L' ') {\r
+    return (First);\r
+  }\r
+\r
+  CloseQuote = FindFirstCharacter (First+1, L"\"", L'^');\r
+\r
+  //\r
+  // We did not find a terminator...\r
+  //\r
+  if (*CloseQuote == CHAR_NULL) {\r
+    return (NULL);\r
+  }\r
+\r
+  return (FindEndOfParameter (CloseQuote+1));\r
+}\r
+\r
+/**\r
+  Return the next parameter from a command line string.\r
 \r
   This function moves the next parameter from Walker into TempParameter and moves\r
   Walker up past that parameter for recursive calling.  When the final parameter\r
   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
+  This will also remove all remaining ^ characters after processing.\r
+\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
+  @param[in]      Length        buffer size of TempParameter.\r
 \r
+  @return   EFI_INALID_PARAMETER  A required parameter was NULL or pointed to a NULL or empty string.\r
+  @return   EFI_NOT_FOUND         A closing " could not be found on the specified string\r
 **/\r
-VOID\r
+EFI_STATUS\r
 EFIAPI\r
 GetNextParameter(\r
-  CHAR16 **Walker,\r
-  CHAR16 **TempParameter\r
+  IN OUT CHAR16   **Walker,\r
+  IN OUT CHAR16   **TempParameter,\r
+  IN CONST UINTN  Length\r
   )\r
 {\r
-  CHAR16 *NextDelim;\r
-  CHAR16 *TempLoc;\r
+  CONST CHAR16 *NextDelim;\r
+\r
+  if (Walker           == NULL\r
+    ||*Walker          == NULL\r
+    ||TempParameter    == NULL\r
+    ||*TempParameter   == NULL\r
+    ){\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
 \r
-  ASSERT(Walker           != NULL);\r
-  ASSERT(*Walker          != NULL);\r
-  ASSERT(TempParameter    != NULL);\r
-  ASSERT(*TempParameter   != NULL);\r
 \r
   //\r
   // make sure we dont have any leading spaces\r
@@ -57,84 +110,60 @@ GetNextParameter(
   // make sure we still have some params now...\r
   //\r
   if (StrLen(*Walker) == 0) {\r
-    ASSERT((*Walker)[0] == CHAR_NULL);\r
-    *Walker = NULL;\r
-    return;\r
+DEBUG_CODE_BEGIN();\r
+    *Walker        = NULL;\r
+DEBUG_CODE_END();\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+\r
+  NextDelim = FindEndOfParameter(*Walker);\r
+\r
+  if (NextDelim == NULL){\r
+DEBUG_CODE_BEGIN();\r
+    *Walker        = NULL;\r
+DEBUG_CODE_END();\r
+    return (EFI_NOT_FOUND);\r
   }\r
 \r
+  StrnCpy(*TempParameter, (*Walker), NextDelim - *Walker);\r
+\r
   //\r
-  // we have a quoted parameter\r
-  // could be the last parameter, but SHOULD have a trailing quote\r
+  // Add a CHAR_NULL if we didnt get one via the copy\r
   //\r
-  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
-        TempLoc++;\r
-      } else if (*TempLoc == L'^' && *(TempLoc+1) == L'\"') {\r
-        TempLoc++;\r
-      } else if (*TempLoc == L'\"') {\r
-        NextDelim = TempLoc;\r
-        break;\r
-      }\r
-    }\r
+  if (*NextDelim != CHAR_NULL) {\r
+    (*TempParameter)[NextDelim - *Walker] = CHAR_NULL;\r
+  }\r
+\r
+  //\r
+  // Update Walker for the next iteration through the function\r
+  //\r
+  *Walker = (CHAR16*)NextDelim;\r
+\r
+  //\r
+  // Remove any non-escaped quotes in the string\r
+  // Remove any remaining escape characters in the string\r
+  //\r
+  for (NextDelim = FindFirstCharacter(*TempParameter, L"\"^", CHAR_NULL) \r
+    ; *NextDelim != CHAR_NULL \r
+    ; NextDelim = FindFirstCharacter(NextDelim, L"\"^", CHAR_NULL)\r
+    ) {\r
+    if (*NextDelim == L'^') {\r
 \r
-    if (NextDelim - ((*Walker)+1) == 0) {\r
-      //\r
-      // found ""\r
-      //\r
-      StrCpy(*TempParameter, L"");\r
-      *Walker = NextDelim + 1;\r
-    } else if (NextDelim != NULL) {\r
-      StrnCpy(*TempParameter, (*Walker)+1, NextDelim - ((*Walker)+1));\r
-      *Walker = NextDelim + 1;\r
-    } else {\r
       //\r
-      // last one... someone forgot the training quote!\r
+      // eliminate the escape ^\r
       //\r
-      StrCpy(*TempParameter, *Walker);\r
-      *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
-        CopyMem(TempLoc, TempLoc+1, StrSize(TempLoc) - sizeof(TempLoc[0]));\r
-      }\r
-    }\r
-  } else {\r
-    //\r
-    // we have a regular parameter (no quote) OR\r
-    // we have the final parameter (no trailing space)\r
-    //\r
-    NextDelim = StrStr((*Walker), L" ");\r
-    if (NextDelim != NULL) {\r
-      StrnCpy(*TempParameter, *Walker, NextDelim - (*Walker));\r
-      (*TempParameter)[NextDelim - (*Walker)] = CHAR_NULL;\r
-      *Walker = NextDelim+1;\r
-    } else {\r
+      CopyMem ((CHAR16*)NextDelim, NextDelim + 1, StrSize (NextDelim + 1));\r
+      NextDelim++;\r
+    } else if (*NextDelim == L'\"') {\r
+\r
       //\r
-      // last one.\r
+      // eliminate the unescaped quote\r
       //\r
-      StrCpy(*TempParameter, *Walker);\r
-      *Walker = NULL;\r
-    }\r
-    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
-      (*TempParameter)[StrLen(*TempParameter)-1] = CHAR_NULL;\r
-    }\r
-    while ((*TempParameter)[0] == L' ') {\r
-      CopyMem(*TempParameter, (*TempParameter)+1, StrSize(*TempParameter) - sizeof((*TempParameter)[0]));\r
+      CopyMem ((CHAR16*)NextDelim, NextDelim + 1, StrSize (NextDelim + 1));\r
     }\r
   }\r
-  return;\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -144,9 +173,12 @@ GetNextParameter(
   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
+  All special character processing (alias, environment variable, redirection, \r
+  etc... must be complete before calling this API.\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
 \r
   @return EFI_SUCCESS           the operation was sucessful\r
   @return EFI_OUT_OF_RESOURCES  a memory allocation failed.\r
@@ -183,22 +215,19 @@ ParseCommandLineToArgs(
   for ( Count = 0\r
       , Walker = (CHAR16*)CommandLine\r
       ; Walker != NULL && *Walker != CHAR_NULL\r
-      ; GetNextParameter(&Walker, &TempParameter)\r
-      , Count++\r
-     );\r
-\r
-/*  Count = 0;\r
-  Walker = (CHAR16*)CommandLine;\r
-  while(Walker != NULL) {\r
-    GetNextParameter(&Walker, &TempParameter);\r
-    Count++;\r
+      ; Count++\r
+      ) {\r
+    if (EFI_ERROR(GetNextParameter(&Walker, &TempParameter, Size))) {\r
+      break;\r
+    }\r
   }\r
-*/\r
+\r
   //\r
   // lets allocate the pointer array\r
   //\r
   (*Argv) = AllocateZeroPool((Count)*sizeof(CHAR16*));\r
   if (*Argv == NULL) {\r
+    SHELL_FREE_NON_NULL(TempParameter);\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
 \r
@@ -206,14 +235,21 @@ ParseCommandLineToArgs(
   Walker = (CHAR16*)CommandLine;\r
   while(Walker != NULL && *Walker != CHAR_NULL) {\r
     SetMem16(TempParameter, Size, CHAR_NULL);\r
-    GetNextParameter(&Walker, &TempParameter);\r
-    NewParam = AllocateZeroPool(StrSize(TempParameter));\r
-    ASSERT(NewParam != NULL);\r
-    StrCpy(NewParam, TempParameter);\r
+    if (EFI_ERROR(GetNextParameter(&Walker, &TempParameter, Size))) {\r
+      SHELL_FREE_NON_NULL(TempParameter);\r
+      return (EFI_INVALID_PARAMETER);\r
+    }\r
+\r
+    NewParam = AllocateCopyPool(StrSize(TempParameter), TempParameter);\r
+    if (NewParam == NULL){\r
+      SHELL_FREE_NON_NULL(TempParameter);\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
     ((CHAR16**)(*Argv))[(*Argc)] = NewParam;\r
     (*Argc)++;\r
   }\r
   ASSERT(Count >= (*Argc));\r
+  SHELL_FREE_NON_NULL(TempParameter);\r
   return (EFI_SUCCESS);\r
 }\r
 \r
@@ -222,9 +258,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
@@ -296,10 +332,10 @@ CreatePopulateInstallShellParametersProtocol (
   //\r
   // Build the full command line\r
   //\r
-  Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, &FullCommandLine);\r
+  Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, FullCommandLine);\r
   if (Status == EFI_BUFFER_TOO_SMALL) {\r
     FullCommandLine = AllocateZeroPool(Size + LoadedImage->LoadOptionsSize);\r
-    Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, &FullCommandLine);\r
+    Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, FullCommandLine);\r
   }\r
   if (Status == EFI_NOT_FOUND) {\r
     //\r
@@ -319,9 +355,7 @@ CreatePopulateInstallShellParametersProtocol (
     FullCommandLine = AllocateZeroPool(Size);\r
   }\r
   if (FullCommandLine != NULL) {\r
-    if (LoadedImage->LoadOptionsSize != 0){\r
-      StrCpy(FullCommandLine, LoadedImage->LoadOptions);\r
-    }\r
+    CopyMem (FullCommandLine, LoadedImage->LoadOptions, LoadedImage->LoadOptionsSize);\r
     //\r
     // Populate Argc and Argv\r
     //\r
@@ -456,7 +490,7 @@ IsUnicodeFile(
 \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
+  @param[in, out] TheString  A pointer to the string to update.\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -477,6 +511,158 @@ StripQuotes (
   }\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
+  Fix a string to only have the file name, removing starting at the first space of whatever is quoted.\r
+\r
+  @param[in]  FileName    The filename to start with.\r
+\r
+  @retval NULL  FileName was invalid.\r
+  @return       The modified FileName.\r
+**/\r
+CHAR16*\r
+EFIAPI\r
+FixFileName (\r
+  IN CHAR16 *FileName\r
+  )\r
+{\r
+  CHAR16  *Copy;\r
+  CHAR16  *TempLocation;\r
+\r
+  if (FileName == NULL) {\r
+    return (NULL);\r
+  }\r
+\r
+  if (FileName[0] == L'\"') {\r
+    Copy = FileName+1;\r
+    if ((TempLocation = StrStr(Copy , L"\"")) != NULL) {\r
+      TempLocation[0] = CHAR_NULL;\r
+    }    \r
+  } else {\r
+    Copy = FileName;\r
+    while(Copy[0] == L' ') {\r
+      Copy++;\r
+    }\r
+    if ((TempLocation = StrStr(Copy , L" ")) != NULL) {\r
+      TempLocation[0] = CHAR_NULL;\r
+    }    \r
+  }\r
+\r
+  if (Copy[0] == CHAR_NULL) {\r
+    return (NULL);\r
+  }\r
+\r
+  return (Copy);\r
+}\r
+\r
+/**\r
+  Fix a string to only have the environment variable name, removing starting at the first space of whatever is quoted and removing the leading and trailing %.\r
+\r
+  @param[in]  FileName    The filename to start with.\r
+\r
+  @retval NULL  FileName was invalid.\r
+  @return       The modified FileName.\r
+**/\r
+CHAR16*\r
+EFIAPI\r
+FixVarName (\r
+  IN CHAR16 *FileName\r
+  )\r
+{\r
+  CHAR16  *Copy;\r
+  CHAR16  *TempLocation;\r
+\r
+  Copy = FileName;\r
+\r
+  if (FileName[0] == L'%') {\r
+    Copy = FileName+1;\r
+    if ((TempLocation = StrStr(Copy , L"%")) != NULL) {\r
+      TempLocation[0] = CHAR_NULL;\r
+    }    \r
+  }\r
+\r
+  return (FixFileName(Copy));\r
+}\r
+\r
+/**\r
+  Remove the unicode file tag from the begining of the file buffer since that will not be\r
+  used by StdIn.\r
+  \r
+  @param[in]  Handle    Pointer to the handle of the file to be processed.\r
+  \r
+  @retval EFI_SUCCESS   The unicode file tag has been moved successfully.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RemoveFileTag(\r
+  IN SHELL_FILE_HANDLE *Handle\r
+  )\r
+{\r
+  UINTN             CharSize;\r
+  CHAR16            CharBuffer;\r
+\r
+  CharSize    = sizeof(CHAR16);\r
+  CharBuffer  = 0;\r
+  gEfiShellProtocol->ReadFile(*Handle, &CharSize, &CharBuffer);\r
+  if (CharBuffer != gUnicodeFileTag) {\r
+    gEfiShellProtocol->SetFilePosition(*Handle, 0);\r
+  }\r
+  return (EFI_SUCCESS);\r
+}\r
+\r
+/**\r
+  Write the unicode file tag to the specified file.\r
+\r
+  It is the caller's responsibility to ensure that\r
+  ShellInfoObject.NewEfiShellProtocol has been initialized before calling this\r
+  function.\r
+\r
+  @param[in] FileHandle  The file to write the unicode file tag to.\r
+\r
+  @return  Status code from ShellInfoObject.NewEfiShellProtocol->WriteFile.\r
+**/\r
+EFI_STATUS\r
+WriteFileTag (\r
+  IN SHELL_FILE_HANDLE FileHandle\r
+  )\r
+{\r
+  CHAR16     FileTag;\r
+  UINTN      Size;\r
+  EFI_STATUS Status;\r
+\r
+  FileTag = gUnicodeFileTag;\r
+  Size = sizeof FileTag;\r
+  Status = ShellInfoObject.NewEfiShellProtocol->WriteFile (FileHandle, &Size,\r
+                                                  &FileTag);\r
+  ASSERT (EFI_ERROR (Status) || Size == sizeof FileTag);\r
+  return Status;\r
+}\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
@@ -484,12 +670,12 @@ StripQuotes (
 \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[out] SystemTableInfo           Pointer to old system table information.\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
@@ -522,7 +708,6 @@ UpdateStdInStdOutStdErr(
   BOOLEAN           OutAppend;\r
   BOOLEAN           ErrAppend;\r
   UINTN             Size;\r
-  CHAR16            TagBuffer[2];\r
   SPLIT_LIST        *Split;\r
   CHAR16            *FirstLocation;\r
 \r
@@ -548,8 +733,8 @@ UpdateStdInStdOutStdErr(
   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
+  SystemTableInfo->ErrOut         = gST->StdErr;\r
+  SystemTableInfo->ErrOutHandle   = gST->StandardErrorHandle;\r
   *OldStdIn                       = ShellParameters->StdIn;\r
   *OldStdOut                      = ShellParameters->StdOut;\r
   *OldStdErr                      = ShellParameters->StdErr;\r
@@ -559,6 +744,9 @@ UpdateStdInStdOutStdErr(
   }\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
@@ -821,6 +1009,11 @@ UpdateStdInStdOutStdErr(
     }\r
   }\r
 \r
+  //\r
+  // re-populate the string to support any filenames that were in quotes.\r
+  //\r
+  StrnCpy(CommandLineCopy, NewCommandLine, StrLen(NewCommandLine));\r
+\r
   if (FirstLocation != CommandLineCopy + StrLen(CommandLineCopy)\r
     && ((UINTN)(FirstLocation - CommandLineCopy) < StrLen(NewCommandLine))\r
     ){\r
@@ -828,23 +1021,36 @@ UpdateStdInStdOutStdErr(
   }\r
 \r
   if (!EFI_ERROR(Status)) {\r
-    if (StdErrFileName != NULL && (CommandLineWalker = StrStr(StdErrFileName, L" ")) != NULL) {\r
-      CommandLineWalker[0] = CHAR_NULL;\r
+\r
+    if (StdErrFileName != NULL) {\r
+      if ((StdErrFileName    = FixFileName(StdErrFileName)) == NULL) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
-    if (StdOutFileName != NULL && (CommandLineWalker = StrStr(StdOutFileName, L" ")) != NULL) {\r
-      CommandLineWalker[0] = CHAR_NULL;\r
+    if (StdOutFileName != NULL) {\r
+      if ((StdOutFileName    = FixFileName(StdOutFileName)) == NULL) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
-    if (StdInFileName  != NULL && (CommandLineWalker = StrStr(StdInFileName , L" ")) != NULL) {\r
-      CommandLineWalker[0] = CHAR_NULL;\r
+    if (StdInFileName  != NULL) {\r
+      if ((StdInFileName     = FixFileName(StdInFileName)) == NULL) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
-    if (StdErrVarName  != NULL && (CommandLineWalker = StrStr(StdErrVarName , L" ")) != NULL) {\r
-      CommandLineWalker[0] = CHAR_NULL;\r
+    if (StdErrVarName  != NULL) {\r
+      if ((StdErrVarName     = FixVarName(StdErrVarName)) == NULL) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
-    if (StdOutVarName  != NULL && (CommandLineWalker = StrStr(StdOutVarName , L" ")) != NULL) {\r
-      CommandLineWalker[0] = CHAR_NULL;\r
+    if (StdOutVarName  != NULL) {\r
+      if ((StdOutVarName     = FixVarName(StdOutVarName)) == NULL) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
-    if (StdInVarName   != NULL && (CommandLineWalker = StrStr(StdInVarName  , L" ")) != NULL) {\r
-      CommandLineWalker[0] = CHAR_NULL;\r
+    if (StdInVarName   != NULL) {\r
+      if ((StdInVarName      = FixVarName(StdInVarName)) == NULL) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
 \r
     //\r
@@ -893,6 +1099,9 @@ UpdateStdInStdOutStdErr(
       ||(StdErrFileName != NULL && !ErrUnicode && ErrAppend && (!EFI_ERROR(ShellFileExists(StdErrFileName)) && !EFI_ERROR(IsUnicodeFile(StdErrFileName))))\r
       ){\r
       Status = EFI_INVALID_PARAMETER;\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
@@ -910,13 +1119,7 @@ UpdateStdInStdOutStdErr(
         }\r
         Status = ShellOpenFileByName(StdErrFileName, &TempHandle, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE,0);\r
         if (!ErrAppend && ErrUnicode && !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
+          Status = WriteFileTag (TempHandle);\r
         }\r
         if (!ErrUnicode && !EFI_ERROR(Status)) {\r
           TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
@@ -924,7 +1127,7 @@ UpdateStdInStdOutStdErr(
         }\r
         if (!EFI_ERROR(Status)) {\r
           ShellParameters->StdErr = TempHandle;\r
-          gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle);\r
+          gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle, gST->StdErr);\r
         }\r
       }\r
 \r
@@ -945,20 +1148,20 @@ UpdateStdInStdOutStdErr(
           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
+            Status = WriteFileTag (TempHandle);\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
+              // When appending to a new unicode file, write the file tag.\r
+              // Otherwise (ie. when appending to a new ASCII file, or an\r
+              // existent file with any encoding), just seek to the end.\r
+              //\r
+              Status = (FileSize == 0 && OutUnicode) ?\r
+                         WriteFileTag (TempHandle) :\r
+                         ShellInfoObject.NewEfiShellProtocol->SetFilePosition (\r
+                                                                TempHandle,\r
+                                                                FileSize);\r
             }\r
           }\r
           if (!OutUnicode && !EFI_ERROR(Status)) {\r
@@ -967,7 +1170,7 @@ UpdateStdInStdOutStdErr(
           }\r
           if (!EFI_ERROR(Status)) {\r
             ShellParameters->StdOut = TempHandle;\r
-            gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle);\r
+            gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle, gST->ConOut);\r
           }\r
         }\r
       }\r
@@ -985,7 +1188,7 @@ UpdateStdInStdOutStdErr(
         TempHandle = CreateFileInterfaceEnv(StdOutVarName);\r
         ASSERT(TempHandle != NULL);\r
         ShellParameters->StdOut = TempHandle;\r
-        gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle);\r
+        gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle, gST->ConOut);\r
       }\r
 \r
       //\r
@@ -1001,7 +1204,7 @@ UpdateStdInStdOutStdErr(
         TempHandle = CreateFileInterfaceEnv(StdErrVarName);\r
         ASSERT(TempHandle != NULL);\r
         ShellParameters->StdErr = TempHandle;\r
-        gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle);\r
+        gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle, gST->StdErr);\r
       }\r
 \r
       //\r
@@ -1009,16 +1212,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
-          gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle);\r
+          if (!InUnicode) {\r
+            TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
+          }\r
+          Size = 0;\r
+          if (TempHandle == NULL || ((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
@@ -1031,7 +1237,15 @@ UpdateStdInStdOutStdErr(
           &TempHandle,\r
           EFI_FILE_MODE_READ,\r
           0);\r
-        if (!InUnicode && !EFI_ERROR(Status)) {\r
+        if (InUnicode) {\r
+          //\r
+          // Chop off the 0xFEFF if it's there...\r
+          //\r
+          RemoveFileTag(&TempHandle);\r
+        } else if (!EFI_ERROR(Status)) {\r
+          //\r
+          // Create the ASCII->Unicode conversion layer\r
+          //\r
           TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
         }\r
         if (!EFI_ERROR(Status)) {\r
@@ -1043,9 +1257,18 @@ UpdateStdInStdOutStdErr(
   }\r
   FreePool(CommandLineCopy);\r
 \r
+  CalculateEfiHdrCrc(&gST->Hdr);\r
+\r
   if (gST->ConIn == NULL ||gST->ConOut == NULL) {\r
-    return (EFI_OUT_OF_RESOURCES);\r
+    Status = EFI_OUT_OF_RESOURCES;\r
   }\r
+\r
+  if (Status == EFI_NOT_FOUND) {\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_REDUNDA_REDIR), ShellInfoObject.HiiHandle);\r
+  } else if (EFI_ERROR(Status)) {\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_REDIR), ShellInfoObject.HiiHandle);\r
+  }\r
+\r
   return (Status);\r
 }\r
 \r
@@ -1053,11 +1276,11 @@ 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[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
+  @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
@@ -1110,12 +1333,14 @@ RestoreStdInStdOutStdErr (
     gST->ConOut               = SystemTableInfo->ConOut;\r
     gST->ConsoleOutHandle     = SystemTableInfo->ConOutHandle;\r
   }\r
-  if (gST->StdErr != SystemTableInfo->ConErr) {\r
+  if (gST->StdErr != SystemTableInfo->ErrOut) {\r
     CloseSimpleTextOutOnFile(gST->StdErr);\r
-    gST->StdErr               = SystemTableInfo->ConErr;\r
-    gST->StandardErrorHandle  = SystemTableInfo->ConErrHandle;\r
+    gST->StdErr               = SystemTableInfo->ErrOut;\r
+    gST->StandardErrorHandle  = SystemTableInfo->ErrOutHandle;\r
   }\r
 \r
+  CalculateEfiHdrCrc(&gST->Hdr);\r
+\r
   return (EFI_SUCCESS);\r
 }\r
 /**\r
@@ -1125,10 +1350,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
@@ -1159,9 +1384,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