]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ConsoleWrappers.c
ShellPkg: Fix Shell treats every .EFI file as an executable application.
[mirror_edk2.git] / ShellPkg / Application / Shell / ConsoleWrappers.c
index 561a666473dbcd8cf80dc93b9db0f64a4198febf..46af141eba99c9de3827f44a1f0689ee7327b417 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Function definitions for shell simple text in and out on top of file handles.\r
 \r
-  Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>\r
+  Copyright (c) 2010 - 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 <Uefi.h>\r
-#include <ShellBase.h>\r
-\r
-#include "ConsoleWrappers.h"\r
 #include "Shell.h"\r
 \r
+extern BOOLEAN AsciiRedirection;\r
+\r
 typedef struct {\r
   EFI_SIMPLE_TEXT_INPUT_PROTOCOL  SimpleTextIn;\r
   SHELL_FILE_HANDLE               FileHandle;\r
   EFI_HANDLE                      TheHandle;\r
+  UINT64                          RemainingBytesOfInputFile;\r
 } SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL;\r
 \r
 typedef struct {\r
   EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL SimpleTextOut;\r
   SHELL_FILE_HANDLE               FileHandle;\r
   EFI_HANDLE                      TheHandle;\r
+  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *OriginalSimpleTextOut;\r
 } SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL;\r
 \r
 /**\r
@@ -45,19 +46,7 @@ ConInWaitForKey (
   IN  VOID            *Context\r
   )\r
 {\r
-  UINT64 Position;\r
-  UINT64 Size;\r
-  //\r
-  // Someone is waiting on the keystroke event, if there's\r
-  // a key pending, signal the event\r
-  //\r
-  // Context is the pointer to EFI_SIMPLE_TEXT_INPUT_PROTOCOL\r
-  //\r
-  ShellInfoObject.NewEfiShellProtocol->GetFilePosition(((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)Context)->FileHandle, &Position);\r
-  ShellInfoObject.NewEfiShellProtocol->GetFileSize    (((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)Context)->FileHandle, &Size    );\r
-  if (Position < Size) {\r
-    gBS->SignalEvent (Event);\r
-  }\r
+  gBS->SignalEvent (Event);\r
 }\r
 \r
 /**\r
@@ -94,10 +83,38 @@ FileBasedSimpleTextInReadKeyStroke(
   )\r
 {\r
   UINTN Size;\r
-  Size = sizeof(CHAR16);\r
+  UINTN CharSize;\r
+\r
+  //\r
+  // Verify the parameters\r
+  //\r
   if (Key == NULL || This == NULL) {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
+\r
+  //\r
+  // Check if we have any characters left in the stream.\r
+  //\r
+  if (((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile == 0) {\r
+    return (EFI_NOT_READY);\r
+  }\r
+\r
+  Size = sizeof(CHAR16);\r
+\r
+  if(!AsciiRedirection) {\r
+    CharSize = sizeof(CHAR16);\r
+  } else {\r
+    CharSize = sizeof(CHAR8);\r
+  } \r
+  //\r
+  // Decrement the amount of free space by Size or set to zero (for odd length files)\r
+  //\r
+  if (((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile > CharSize) {\r
+    ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile -= CharSize;\r
+  } else {\r
+    ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile = 0;\r
+  }\r
+\r
   Key->ScanCode = 0;\r
   return (ShellInfoObject.NewEfiShellProtocol->ReadFile(\r
     ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->FileHandle,\r
@@ -124,6 +141,8 @@ CreateSimpleTextInOnFile(
 {\r
   SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL  *ProtocolToReturn;\r
   EFI_STATUS                            Status;\r
+  UINT64                                CurrentPosition;\r
+  UINT64                                FileSize;\r
 \r
   if (HandleLocation == NULL || FileHandleToUse == NULL) {\r
     return (NULL);\r
@@ -133,7 +152,15 @@ CreateSimpleTextInOnFile(
   if (ProtocolToReturn == NULL) {\r
     return (NULL);\r
   }\r
-  ProtocolToReturn->FileHandle                  = FileHandleToUse;\r
+\r
+  ShellGetFileSize    (FileHandleToUse, &FileSize);\r
+  ShellGetFilePosition(FileHandleToUse, &CurrentPosition);\r
+\r
+  //\r
+  // Initialize the protocol members\r
+  //\r
+  ProtocolToReturn->RemainingBytesOfInputFile  = FileSize - CurrentPosition;\r
+  ProtocolToReturn->FileHandle                 = FileHandleToUse;\r
   ProtocolToReturn->SimpleTextIn.Reset         = FileBasedSimpleTextInReset;\r
   ProtocolToReturn->SimpleTextIn.ReadKeyStroke = FileBasedSimpleTextInReadKeyStroke;\r
   \r
@@ -257,7 +284,16 @@ FileBasedSimpleTextOutQueryMode (
   OUT UINTN                           *Rows\r
   )\r
 {\r
-  return (EFI_UNSUPPORTED);\r
+  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *PassThruProtocol;\r
+  \r
+  PassThruProtocol = ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)This)->OriginalSimpleTextOut;\r
+  \r
+  // Pass the QueryMode call thru to the original SimpleTextOutProtocol\r
+  return (PassThruProtocol->QueryMode(\r
+    PassThruProtocol,\r
+    ModeNumber,\r
+    Columns,\r
+    Rows));\r
 }\r
 \r
 /**\r
@@ -390,8 +426,9 @@ FileBasedSimpleTextOutOutputString (
   Function to create a EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL on top of a \r
   SHELL_FILE_HANDLE to support redirecting output from a file.\r
 \r
-  @param[in]  FileHandleToUse The pointer to the SHELL_FILE_HANDLE to use.\r
-  @param[in]  HandleLocation  The pointer of a location to copy handle with protocol to.\r
+  @param[in]  FileHandleToUse  The pointer to the SHELL_FILE_HANDLE to use.\r
+  @param[in]  HandleLocation   The pointer of a location to copy handle with protocol to.\r
+  @param[in]  OriginalProtocol The pointer to the original output protocol for pass thru of functions.\r
 \r
   @retval NULL                There was insufficient memory available.\r
   @return                     A pointer to the allocated protocol structure;\r
@@ -399,8 +436,9 @@ FileBasedSimpleTextOutOutputString (
 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*\r
 EFIAPI\r
 CreateSimpleTextOutOnFile(\r
-  IN SHELL_FILE_HANDLE  FileHandleToUse,\r
-  IN EFI_HANDLE         *HandleLocation\r
+  IN SHELL_FILE_HANDLE               FileHandleToUse,\r
+  IN EFI_HANDLE                      *HandleLocation,\r
+  IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *OriginalProtocol\r
   )\r
 {\r
   SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ProtocolToReturn;\r
@@ -415,6 +453,7 @@ CreateSimpleTextOutOnFile(
     return (NULL);\r
   }\r
   ProtocolToReturn->FileHandle                      = FileHandleToUse;\r
+  ProtocolToReturn->OriginalSimpleTextOut           = OriginalProtocol;\r
   ProtocolToReturn->SimpleTextOut.Reset             = FileBasedSimpleTextOutReset;\r
   ProtocolToReturn->SimpleTextOut.TestString        = FileBasedSimpleTextOutTestString;\r
   ProtocolToReturn->SimpleTextOut.QueryMode         = FileBasedSimpleTextOutQueryMode;\r
@@ -429,12 +468,12 @@ CreateSimpleTextOutOnFile(
     FreePool(ProtocolToReturn);\r
     return (NULL);\r
   }\r
-  ProtocolToReturn->SimpleTextOut.Mode->MaxMode       = 0;\r
-  ProtocolToReturn->SimpleTextOut.Mode->Mode          = 0;\r
-  ProtocolToReturn->SimpleTextOut.Mode->Attribute     = 0;\r
-  ProtocolToReturn->SimpleTextOut.Mode->CursorColumn  = 0;\r
-  ProtocolToReturn->SimpleTextOut.Mode->CursorRow     = 0;\r
-  ProtocolToReturn->SimpleTextOut.Mode->CursorVisible = FALSE;\r
+  ProtocolToReturn->SimpleTextOut.Mode->MaxMode       = OriginalProtocol->Mode->MaxMode;\r
+  ProtocolToReturn->SimpleTextOut.Mode->Mode          = OriginalProtocol->Mode->Mode;\r
+  ProtocolToReturn->SimpleTextOut.Mode->Attribute     = OriginalProtocol->Mode->Attribute;\r
+  ProtocolToReturn->SimpleTextOut.Mode->CursorColumn  = OriginalProtocol->Mode->CursorColumn;\r
+  ProtocolToReturn->SimpleTextOut.Mode->CursorRow     = OriginalProtocol->Mode->CursorRow;\r
+  ProtocolToReturn->SimpleTextOut.Mode->CursorVisible = OriginalProtocol->Mode->CursorVisible;\r
 \r
   Status = gBS->InstallProtocolInterface(\r
     &(ProtocolToReturn->TheHandle), \r
@@ -445,7 +484,8 @@ CreateSimpleTextOutOnFile(
     *HandleLocation = ProtocolToReturn->TheHandle;\r
     return ((EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*)ProtocolToReturn);\r
   } else {\r
-    FreePool(ProtocolToReturn);\r
+    SHELL_FREE_NON_NULL(ProtocolToReturn->SimpleTextOut.Mode);\r
+    SHELL_FREE_NON_NULL(ProtocolToReturn);\r
     return (NULL);\r
   }\r
 }\r
@@ -472,6 +512,7 @@ CloseSimpleTextOutOnFile(
     ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*)SimpleTextOut)->TheHandle, \r
     &gEfiSimpleTextOutProtocolGuid, \r
     &(((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*)SimpleTextOut)->SimpleTextOut));\r
+  FreePool(SimpleTextOut->Mode);\r
   FreePool(SimpleTextOut);\r
   return (Status);\r
 }\r