]> git.proxmox.com Git - mirror_edk2.git/commitdiff
StdLib: Some deployed versions of the Simple Text Input Protocol randomly return...
authorDaryl McDaniel <daryl.mcdaniel@intel.com>
Tue, 28 Oct 2014 19:20:48 +0000 (19:20 +0000)
committerdarylm503 <darylm503@Edk2>
Tue, 28 Oct 2014 19:20:48 +0000 (19:20 +0000)
StdLibPrivateInternalFiles/Include/Device/Console.h: Change UnGetKey, in the ConInstance structure, from an EFI_INPUT_KEY structure to a CHAR16 variable.

Include/sys/termios.h: Add CHAR_SUB and CHAR_ESC for translation of '^Z' and the Escape Scan Code into the EOF and ESC characters, respectively.

LibC/Uefi/Devices/Console/daConsole.c:  Add da_ConRawRead() function to simplify the read logic. Discard NUL characters from the input stream.  In Blocking mode, retry until a non-NUL character is received.  In NonBlocking mode, a NUL causes an EAGAIN error to be returned.  Translate the Escape Scan Code into an ESC character.  If Scan Codes are ignored, retry if in Blocking mode else return an EAGAIN error.  UnGetKey becomes a single wide character instead of a structure.
    Change da_Poll() to use da_ConRawRead().

LibC/Uefi/InteractiveIO/IIOutilities.c:  BUG fix.  Return the processed input character instead of the raw character.  Allows EOF propagation.

LibC/Uefi/InteractiveIO/CanonRead.c:  Enable EOF propagation.

LibC/Uefi/InteractiveIO/IIOechoCtrl.h:  Use symbols defined in termios.h instead of hard-coded constant numbers.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Daryl McDaniel <daryl.mcdaniel@intel.com>
Reviewed-by: Jaben Carsey <Jaben.carsey@intel.com>
Reviewed-by: Erik Bjorge <erik.c.bjorge@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16254 6f19259b-4bc3-4df7-8a09-765794883524

StdLib/Include/sys/termios.h
StdLib/LibC/Uefi/Devices/Console/daConsole.c
StdLib/LibC/Uefi/InteractiveIO/CanonRead.c
StdLib/LibC/Uefi/InteractiveIO/IIOechoCtrl.h
StdLib/LibC/Uefi/InteractiveIO/IIOutilities.c
StdLibPrivateInternalFiles/Include/Device/Console.h

index e144d521f58b97838abc4a375092c339a209bc23..671a5c06c067ebda7b903bae0840748be4d1608d 100644 (file)
@@ -2,7 +2,7 @@
     Macros and declarations for terminal oriented ioctls and\r
     I/O discipline.\r
 \r
-    Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
+    Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>\r
     This program and the accompanying materials are licensed and made available under\r
     the terms and conditions of the BSD License that accompanies this distribution.\r
     The full text of the license may be found at\r
@@ -421,6 +421,8 @@ typedef enum {
 } TtyFunKey;\r
 \r
 // Non-UEFI character definitions\r
-#define CHAR_EOT    0x0004        /* End of Text (EOT) character */\r
+#define CHAR_EOT    0x0004        /* End of Text (EOT) character -- Unix End-of-File character */\r
+#define CHAR_SUB    0x001a        /* MSDOS End-of-File character */\r
+#define CHAR_ESC    0x001b        /* Escape (ESC) character */\r
 \r
 #endif /* !_SYS_TERMIOS_H_ */\r
index 927ec944eaeafa5d444322d5bc3cd72c82089209..1f40177d474db3f730d220851320c7ebfef2d6a7 100644 (file)
@@ -10,7 +10,7 @@
   The devices status as a wide device is indicatd by _S_IWTTY being set in\r
   f_iflags.\r
 \r
-  Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available under\r
   the terms and conditions of the BSD License that accompanies this distribution.\r
   The full text of the license may be found at\r
@@ -24,6 +24,7 @@
 #include  <Library/BaseLib.h>\r
 #include  <Library/MemoryAllocationLib.h>\r
 #include  <Library/UefiBootServicesTableLib.h>\r
+#include  <Library/DebugLib.h>\r
 #include  <Protocol/SimpleTextIn.h>\r
 #include  <Protocol/SimpleTextOut.h>\r
 \r
@@ -35,6 +36,7 @@
 #include  <stdarg.h>\r
 #include  <sys/fcntl.h>\r
 #include  <unistd.h>\r
+#include  <sys/termios.h>\r
 #include  <kfile.h>\r
 #include  <Device/Device.h>\r
 #include  <Device/IIO.h>\r
@@ -238,7 +240,6 @@ da_ConWrite(
 \r
   // Depending on status, update BufferSize and return\r
   if(!RETURN_ERROR(Status)) {\r
-    //BufferSize = NumChar;\r
     NumChar = BufferSize;\r
     Stream->NumWritten += NumChar;\r
   }\r
@@ -246,13 +247,85 @@ da_ConWrite(
   return NumChar;\r
 }\r
 \r
+/** Read a wide character from the console input device.\r
+\r
+    Returns NUL or a translated input character.\r
+\r
+    @param[in]      filp          Pointer to file descriptor for this file.\r
+    @param[out]     Buffer        Buffer in which to place the read character.\r
+\r
+    @retval    EFI_DEVICE_ERROR   A hardware error has occurred.\r
+    @retval    EFI_NOT_READY      No data is available.  Try again later.\r
+    @retval    EFI_SUCCESS        One wide character has been placed in Character\r
+                                    - 0x0000  NUL, ignore this\r
+                                    - Otherwise, should be a good wide character in Character\r
+**/\r
+static\r
+EFI_STATUS\r
+da_ConRawRead (\r
+  IN OUT  struct __filedes   *filp,\r
+     OUT  wchar_t            *Character\r
+)\r
+{\r
+  EFI_SIMPLE_TEXT_INPUT_PROTOCOL   *Proto;\r
+  ConInstance                      *Stream;\r
+  cIIO                             *Self;\r
+  EFI_STATUS                        Status;\r
+  EFI_INPUT_KEY                     Key = {0,0};\r
+  wchar_t                           RetChar;\r
+\r
+  Self    = (cIIO *)filp->devdata;\r
+  Stream  = BASE_CR(filp->f_ops, ConInstance, Abstraction);\r
+  Proto   = (EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)Stream->Dev;\r
+\r
+  if(Stream->UnGetKey == CHAR_NULL) {\r
+    Status = Proto->ReadKeyStroke(Proto, &Key);\r
+  }\r
+  else {\r
+    Status  = EFI_SUCCESS;\r
+    // Use the data in the Un-get buffer\r
+    // Guaranteed that ScanCode and UnicodeChar are not both NUL\r
+    Key.ScanCode        = SCAN_NULL;\r
+    Key.UnicodeChar     = Stream->UnGetKey;\r
+    Stream->UnGetKey    = CHAR_NULL;\r
+  }\r
+  if(Status == EFI_SUCCESS) {\r
+    // Translate the Escape Scan Code to an ESC character\r
+    if (Key.ScanCode != 0) {\r
+      if (Key.ScanCode == SCAN_ESC) {\r
+        RetChar = CHAR_ESC;\r
+      }\r
+      else if((Self->Termio.c_iflag & IGNSPEC) != 0) {\r
+        // If we are ignoring special characters, return a NUL\r
+        RetChar = 0;\r
+      }\r
+      else {\r
+        // Must be a control, function, or other non-printable key.\r
+        // Map it into the Platform portion of the Unicode private use area\r
+        RetChar = TtyFunKeyMax - Key.ScanCode;\r
+      }\r
+    }\r
+    else {\r
+      RetChar = Key.UnicodeChar;\r
+    }\r
+    *Character = RetChar;\r
+  }\r
+  else {\r
+    *Character = 0;\r
+  }\r
+  return Status;\r
+}\r
+\r
 /** Read a wide character from the console input device.\r
 \r
   NOTE: The UEFI Console is a wide device, _S_IWTTY, so characters returned\r
         by da_ConRead are WIDE characters.  It is the responsibility of the\r
         higher-level function(s) to perform any necessary conversions.\r
 \r
-    @param[in,out]  BufferSize  Number of characters in Buffer.\r
+    A NUL character, 0x0000, is never returned.  In the event that such a character\r
+    is encountered, the read is either retried or -1 is returned with errno set\r
+    to EAGAIN.\r
+\r
     @param[in]      filp          Pointer to file descriptor for this file.\r
     @param[in]      offset        Ignored.\r
     @param[in]      BufferSize    Buffer size, in bytes.\r
@@ -274,79 +347,63 @@ da_ConRead(
 {\r
   EFI_SIMPLE_TEXT_INPUT_PROTOCOL   *Proto;\r
   ConInstance                      *Stream;\r
-  cIIO                              *Self;\r
-  EFI_INPUT_KEY                     Key = {0,0};\r
-  EFI_STATUS                        Status = RETURN_SUCCESS;\r
+  //cIIO                              *Self;\r
+  EFI_STATUS                        Status;\r
   UINTN                             Edex;\r
   ssize_t                           NumRead;\r
-  int                               Flags;\r
-  wchar_t                           RetChar;   // Default to No Data\r
+  BOOLEAN                           BlockingMode;\r
+  wchar_t                           RetChar;\r
 \r
   NumRead = -1;\r
   if(BufferSize < sizeof(wchar_t)) {\r
     errno = EINVAL;     // Buffer is too small to hold one character\r
   }\r
   else {\r
-    Self = (cIIO *)filp->devdata;\r
-  Stream = BASE_CR(filp->f_ops, ConInstance, Abstraction);\r
-  Proto = (EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)Stream->Dev;\r
-    Flags = filp->Oflags;\r
-    if((Stream->UnGetKey.UnicodeChar == CHAR_NULL) && (Stream->UnGetKey.ScanCode == SCAN_NULL)) {\r
-      // No data pending in the Un-get buffer.  Get a char from the hardware.\r
-      if((Flags & O_NONBLOCK) == 0) {\r
+    Stream = BASE_CR(filp->f_ops, ConInstance, Abstraction);\r
+    Proto = (EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)Stream->Dev;\r
+    BlockingMode = ((filp->Oflags & O_NONBLOCK) == 0);\r
+\r
+    do {\r
+      Status = EFI_SUCCESS;\r
+      if(BlockingMode) {\r
         // Read a byte in Blocking mode\r
-      Status = gBS->WaitForEvent( 1, &Proto->WaitForKey, &Edex);\r
-        EFIerrno = Status;\r
-        if(Status != EFI_SUCCESS) {\r
-          errno = EINVAL;\r
-      }\r
-        else {\r
-      Status = Proto->ReadKeyStroke(Proto, &Key);\r
-          if(Status == EFI_SUCCESS) {\r
-            NumRead = 1;   // Indicate that Key holds the data\r
-          }\r
-          else {\r
-            errno = EIO;\r
-          }\r
-        }\r
-      }\r
-      else {\r
-        // Read a byte in Non-Blocking mode\r
-      Status = Proto->ReadKeyStroke(Proto, &Key);\r
-        EFIerrno = Status;\r
-        if(Status == EFI_SUCCESS) {\r
-          // Got a keystroke.\r
-          NumRead = 1;   // Indicate that Key holds the data\r
-        }\r
-        else if(Status == EFI_NOT_READY) {\r
-          // Keystroke data is not available\r
-          errno = EAGAIN;\r
-        }\r
-        else {\r
-          // Hardware error\r
-          errno = EIO;\r
-        }\r
+        Status = gBS->WaitForEvent( 1, &Proto->WaitForKey, &Edex);\r
       }\r
+\r
+      /*  WaitForEvent should not be able to fail since\r
+            NumberOfEvents is set to constant 1 so is never 0\r
+            Event is set by the Simple Text Input protocol so should never be EVT_NOTIFY_SIGNAL\r
+            Current TPL should be TPL_APPLICATION.\r
+          ASSERT so that we catch any problems during development.\r
+      */\r
+      ASSERT(Status == EFI_SUCCESS);\r
+\r
+      Status = da_ConRawRead (filp, &RetChar);\r
+    } while ( BlockingMode &&\r
+             (RetChar == 0) &&\r
+             (Status != EFI_DEVICE_ERROR));\r
+\r
+    EFIerrno = Status;\r
+    if(Status == EFI_SUCCESS) {\r
+      // Got a keystroke.\r
+      NumRead = 1;   // Indicate that Key holds the data\r
+    }\r
+    else if(Status == EFI_NOT_READY) {\r
+      // Keystroke data is not available\r
+      errno = EAGAIN;\r
     }\r
     else {\r
-      // Use the data in the Un-get buffer\r
-      Key.ScanCode          = Stream->UnGetKey.ScanCode;\r
-      Key.UnicodeChar       = Stream->UnGetKey.UnicodeChar;\r
-      Stream->UnGetKey.ScanCode     = SCAN_NULL;\r
-      Stream->UnGetKey.UnicodeChar  = CHAR_NULL;\r
-      NumRead = 1;   // Indicate that Key holds the data\r
+      // Hardware error\r
+      errno = EIO;\r
     }\r
-    // If we have data, prepare it for return.\r
-    if(NumRead == 1) {\r
-      RetChar = Key.UnicodeChar;\r
-      if((RetChar == 0) && ((Self->Termio.c_iflag & IGNSPEC) == 0)) {\r
-        // Must be a control, function, or other non-printable key.\r
-        // Map it into the Platform portion of the Unicode private use area\r
-        RetChar = (Key.ScanCode == 0) ? 0 : 0xF900U - Key.ScanCode;\r
-      }\r
+    if (RetChar == 0) {\r
+      NumRead = -1;\r
+      errno = EAGAIN;\r
+    }\r
+    else {\r
       *((wchar_t *)Buffer) = RetChar;\r
-      }\r
     }\r
+  }\r
   return NumRead;\r
 }\r
 \r
@@ -542,24 +599,23 @@ da_ConPoll(
     return POLLNVAL;    // Looks like a bad filp pointer\r
   }\r
   if(Stream->InstanceNum == 0) {\r
-    // Only input is supported for this device\r
+    // STDIN: Only input is supported for this device\r
     Proto = (EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)Stream->Dev;\r
-    if((Stream->UnGetKey.UnicodeChar == CHAR_NULL) && (Stream->UnGetKey.ScanCode == SCAN_NULL)) {\r
-      Status = Proto->ReadKeyStroke(Proto, &Stream->UnGetKey);\r
-      if(Status == RETURN_SUCCESS) {\r
-        RdyMask = POLLIN;\r
-        if(Stream->UnGetKey.UnicodeChar != CHAR_NULL) {\r
-          RdyMask |= POLLRDNORM;\r
-        }\r
-      }\r
-      else {\r
-        Stream->UnGetKey.ScanCode     = SCAN_NULL;\r
-        Stream->UnGetKey.UnicodeChar  = CHAR_NULL;\r
+    Status = da_ConRawRead (filp, &Stream->UnGetKey);\r
+    if(Status == RETURN_SUCCESS) {\r
+      RdyMask = POLLIN;\r
+      if ((Stream->UnGetKey <  TtyFunKeyMin)   ||\r
+          (Stream->UnGetKey >= TtyFunKeyMax))\r
+      {\r
+        RdyMask |= POLLRDNORM;\r
       }\r
     }\r
+    else {\r
+      Stream->UnGetKey  = CHAR_NULL;\r
+    }\r
   }\r
   else if(Stream->InstanceNum < NUM_SPECIAL) {  // Not 0, is it 1 or 2?\r
-    // Only output is supported for this device\r
+    // (STDOUT || STDERR): Only output is supported for this device\r
     RdyMask = POLLOUT;\r
   }\r
   else {\r
@@ -638,8 +694,7 @@ __Cons_construct(
 \r
     Stream->NumRead     = 0;\r
     Stream->NumWritten  = 0;\r
-    Stream->UnGetKey.ScanCode     = SCAN_NULL;\r
-    Stream->UnGetKey.UnicodeChar  = CHAR_NULL;\r
+    Stream->UnGetKey    = CHAR_NULL;\r
 \r
     if(Stream->Dev == NULL) {\r
       continue;                 // No device for this stream.\r
index db6af6e4bfa60ff3956b57a53b4be0ad786414e1..8c8e076d80b6f6be7efe719ac53a6d2cd6038bf4 100644 (file)
@@ -3,7 +3,7 @@
 \r
   The functions assume that isatty() is TRUE at the time they are called.\r
 \r
-  Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available\r
   under the terms and conditions of the BSD License which accompanies this\r
   distribution.  The full text of the license may be found at\r
@@ -87,6 +87,10 @@ IIO_CanonRead (
   //  Input and process characters until BufferSize is exhausted.\r
   do {\r
     InChar = IIO_GetInChar(filp, FirstRead);\r
+    if (InChar == WEOF) {\r
+      NumRead = 0;\r
+      break;\r
+    }\r
     FirstRead = FALSE;\r
     Activate  = TRUE;\r
     if(InChar == CHAR_CARRIAGE_RETURN) {\r
@@ -128,6 +132,8 @@ IIO_CanonRead (
     }\r
     else if(CCEQ(Termio->c_cc[VEOF], InChar)) {\r
       InChar = WEOF;\r
+      NumRead = 0;\r
+      EchoIsOK = FALSE;   // Buffer, but don't echo this character\r
     }\r
     else if(CCEQ(Termio->c_cc[VEOL], InChar)) {\r
       EchoIsOK = FALSE;   // Buffer, but don't echo this character\r
index 69d040af01f9be956e4dc7875f22c4bf2075b888..068a7203ef4828b3c9bb7ba768507a52fc2a60ef 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Constants and declarations for the Echo function.\r
 \r
-  Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available\r
   under the terms and conditions of the BSD License which accompanies this\r
   distribution.  The full text of the license may be found at\r
@@ -18,10 +18,10 @@ __BEGIN_DECLS
 \r
 /* These constants are assigned values within the Unicode Private Use range.\r
    The value of IIO_ECHO_MIN must be adjusted to ensure that IIO_ECHO_MAX\r
-   never exceeds the value of 0xF900.\r
+   never exceeds the value of (TtyFunKeyMin - 1).\r
 */\r
 typedef enum {\r
-  IIO_ECHO_MIN      = (TtyFunKeyMin - 3),\r
+  IIO_ECHO_MIN      = (TtySpecKeyMin),\r
   IIO_ECHO_DISCARD  = IIO_ECHO_MIN,       // Ignore this character completely\r
   IIO_ECHO_ERASE,                         // Erase previous character\r
   IIO_ECHO_KILL,                          // Kill the entire line\r
index 1c978eae709a4fc88d167976637646327d6ebdf4..0e80ff0eaf7538547ae7269aa6e077e92f273221 100644 (file)
@@ -3,7 +3,7 @@
 \r
   The functions assume that isatty() is TRUE at the time they are called.\r
 \r
-  Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available\r
   under the terms and conditions of the BSD License which accompanies this\r
   distribution.  The full text of the license may be found at\r
@@ -104,7 +104,7 @@ IIO_GetInChar (
   else {\r
     RetVal = (wint_t)InChar;\r
   }\r
-  return InChar;\r
+  return RetVal;\r
 }\r
 \r
 /** Get the current cursor position.\r
index 9b4eb52d76e17a80f2aeb763896f75ad4fcee635..dce2630218a9453a1e4d35db5e889ed3ddb2304c 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Declarations and macros for the console abstraction.\r
 \r
-  Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available\r
   under the terms and conditions of the BSD License which accompanies this\r
   distribution.  The full text of the license may be found at\r
@@ -32,8 +32,8 @@ typedef struct {
   UINTN                       Reserved_1;   // Ensure that next member starts on an 8-byte boundary\r
   UINT64                      NumRead;      ///< Number of characters Read.\r
   UINT64                      NumWritten;   ///< Number of characters Written.\r
-  EFI_INPUT_KEY               UnGetKey;     ///< One-key pushback, for poll().\r
   __mbstate_t                 CharState;    ///< Character state for the byte stream passing through this device\r
+  CHAR16                      UnGetKey;     ///< One-key pushback, for poll().\r
 } ConInstance;\r
 \r
 __BEGIN_DECLS\r