]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellParametersProtocol.c
Fix CRLF format
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellParametersProtocol.c
index 02606d615bedf86fb9947640e96b6a7881d31136..ab435a126313da06da9eaecfe3e8c478e3617430 100644 (file)
@@ -2,7 +2,8 @@
   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) 2013 Hewlett-Packard Development Company, L.P.\r
+  Copyright (c) 2009 - 2014, 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
@@ -13,8 +14,7 @@
 \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
@@ -192,6 +192,7 @@ ParseCommandLineToArgs(
   //\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
@@ -207,6 +208,7 @@ ParseCommandLineToArgs(
     (*Argc)++;\r
   }\r
   ASSERT(Count >= (*Argc));\r
+  SHELL_FREE_NON_NULL(TempParameter);\r
   return (EFI_SUCCESS);\r
 }\r
 \r
@@ -289,10 +291,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
@@ -312,9 +314,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
@@ -470,6 +470,125 @@ 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
+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
   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
@@ -541,8 +660,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
@@ -552,6 +671,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
@@ -814,6 +936,11 @@ UpdateStdInStdOutStdErr(
     }\r
   }\r
 \r
+  //\r
+  // re-populate the string to support any filenames that were in quotes.\r
+  //\r
+  StrCpy(CommandLineCopy, NewCommandLine);\r
+\r
   if (FirstLocation != CommandLineCopy + StrLen(CommandLineCopy)\r
     && ((UINTN)(FirstLocation - CommandLineCopy) < StrLen(NewCommandLine))\r
     ){\r
@@ -821,23 +948,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
@@ -920,7 +1060,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
@@ -963,7 +1103,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
@@ -981,7 +1121,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
@@ -997,7 +1137,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
@@ -1005,16 +1145,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
@@ -1027,7 +1170,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
@@ -1039,9 +1190,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
@@ -1106,12 +1266,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