]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StdLib/LibC/Uefi/Devices/Console/daConsole.c
StdLib: Some deployed versions of the Simple Text Input Protocol randomly return...
[mirror_edk2.git] / StdLib / LibC / Uefi / Devices / Console / daConsole.c
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