]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Console/TerminalDxe/TerminalConOut.c
MdeModulePkg/TerminalDxe: Fix IA32 VS2015x86 build break
[mirror_edk2.git] / MdeModulePkg / Universal / Console / TerminalDxe / TerminalConOut.c
index c7c94fe0296b026749d125d397cd9e0383d1dd40..9625f4d518d968206315716dc4a5a4a08da51bad 100644 (file)
@@ -1,8 +1,9 @@
 /** @file\r
   Implementation for EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL protocol.\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (C) 2016 Silicon Graphics, Inc. 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
 http://opensource.org/licenses/bsd-license.php\r
@@ -20,7 +21,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 // ASCII. The ASCII mapping we just made up.\r
 //\r
 //\r
-STATIC UNICODE_TO_CHAR  UnicodeToPcAnsiOrAscii[] = {\r
+UNICODE_TO_CHAR  UnicodeToPcAnsiOrAscii[] = {\r
   { BOXDRAW_HORIZONTAL,                 0xc4, L'-' },\r
   { BOXDRAW_VERTICAL,                   0xb3, L'|' },\r
   { BOXDRAW_DOWN_RIGHT,                 0xda, L'/' },\r
@@ -70,10 +71,10 @@ STATIC UNICODE_TO_CHAR  UnicodeToPcAnsiOrAscii[] = {
   { GEOMETRICSHAPE_DOWN_TRIANGLE,       0x1f, L'v' },\r
   { GEOMETRICSHAPE_LEFT_TRIANGLE,       0x11, L'<' },\r
 \r
-  {  ARROW_LEFT,                         0x3c, L'<' },\r
-  {  ARROW_UP,                           0x18, L'^' },\r
-  {  ARROW_RIGHT,                        0x3e, L'>' },\r
-  {  ARROW_DOWN,                         0x19, L'v' },\r
+  { ARROW_LEFT,                         0x3c, L'<' },\r
+  { ARROW_UP,                           0x18, L'^' },\r
+  { ARROW_RIGHT,                        0x3e, L'>' },\r
+  { ARROW_DOWN,                         0x19, L'v' },\r
 \r
   { 0x0000,                             0x00, L'\0' }\r
 };\r
@@ -82,6 +83,8 @@ CHAR16 mSetModeString[]            = { ESC, '[', '=', '3', 'h', 0 };
 CHAR16 mSetAttributeString[]       = { ESC, '[', '0', 'm', ESC, '[', '4', '0', 'm', ESC, '[', '4', '0', 'm', 0 };\r
 CHAR16 mClearScreenString[]        = { ESC, '[', '2', 'J', 0 };\r
 CHAR16 mSetCursorPositionString[]  = { ESC, '[', '0', '0', ';', '0', '0', 'H', 0 };\r
+CHAR16 mCursorForwardString[]      = { ESC, '[', '0', '0', 'C', 0 };\r
+CHAR16 mCursorBackwardString[]     = { ESC, '[', '0', '0', 'D', 0 };\r
 \r
 //\r
 // Body of the ConOut functions\r
@@ -89,6 +92,7 @@ CHAR16 mSetCursorPositionString[]  = { ESC, '[', '0', '0', ';', '0', '0', 'H', 0
 \r
 /**\r
   Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset().\r
+\r
   If ExtendeVerification is TRUE, then perform dependent serial device reset,\r
   and set display mode to mode 0.\r
   If ExtendedVerification is FALSE, only set display mode to mode 0.\r
@@ -98,8 +102,8 @@ CHAR16 mSetCursorPositionString[]  = { ESC, '[', '0', '0', ';', '0', '0', 'H', 0
                                 exhaustive verification operation of the device\r
                                 during reset.\r
 \r
-  @return EFI_SUCCESS           The reset operation succeeds.\r
-  @return EFI_DEVICE_ERROR      The terminal is not functioning correctly or the serial port reset fails.\r
+  @retval EFI_SUCCESS           The reset operation succeeds.\r
+  @retval EFI_DEVICE_ERROR      The terminal is not functioning correctly or the serial port reset fails.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -123,7 +127,7 @@ TerminalConOutReset (
     //\r
     REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
       EFI_PROGRESS_CODE,\r
-      PcdGet32 (PcdStatusCodeValueRemoteConsoleReset),\r
+      (EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_PC_RESET),\r
       TerminalDevice->DevicePath\r
       );\r
 \r
@@ -134,7 +138,7 @@ TerminalConOutReset (
       //\r
       REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
         EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
-        PcdGet32 (PcdStatusCodeValueRemoteConsoleError),\r
+        (EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_EC_CONTROLLER_ERROR),\r
         TerminalDevice->DevicePath\r
         );\r
 \r
@@ -152,6 +156,7 @@ TerminalConOutReset (
 \r
 /**\r
   Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString().\r
+\r
   The Unicode string will be converted to terminal expressible data stream\r
   and send to terminal via serial port.\r
 \r
@@ -191,6 +196,7 @@ TerminalConOutOutputString (
 \r
   ValidBytes  = 0;\r
   Warning     = FALSE;\r
+  AsciiChar   = 0;\r
 \r
   //\r
   //  get Terminal device data structure pointer.\r
@@ -202,7 +208,7 @@ TerminalConOutOutputString (
   //\r
   Mode = This->Mode;\r
 \r
-  if (Mode->Mode > 2) {\r
+  if (Mode->Mode >= Mode->MaxMode) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
@@ -220,6 +226,7 @@ TerminalConOutOutputString (
     case PCANSITYPE:\r
     case VT100TYPE:\r
     case VT100PLUSTYPE:\r
+    case TTYTERMTYPE:\r
 \r
       if (!TerminalIsValidTextGraphics (*WString, &GraphicChar, &AsciiChar)) {\r
         //\r
@@ -309,6 +316,30 @@ TerminalConOutOutputString (
           Mode->CursorRow++;\r
         }\r
 \r
+        if (TerminalDevice->TerminalType == TTYTERMTYPE &&\r
+            !TerminalDevice->OutputEscChar) {\r
+          //\r
+          // We've written the last character on the line.  The\r
+          // terminal doesn't actually wrap its cursor until we print\r
+          // the next character, but the driver thinks it has wrapped\r
+          // already.  Print CR LF to synchronize the terminal with\r
+          // the driver, but only if we're not in the middle of\r
+          // printing an escape sequence.\r
+          //\r
+          CHAR8 CrLfStr[] = {'\r', '\n'};\r
+\r
+          Length = sizeof(CrLfStr);\r
+\r
+          Status = TerminalDevice->SerialIo->Write (\r
+                                                TerminalDevice->SerialIo,\r
+                                                &Length,\r
+                                                CrLfStr\r
+                                                );\r
+\r
+          if (EFI_ERROR (Status)) {\r
+            goto OutputError;\r
+          }\r
+        }\r
       }\r
       break;\r
 \r
@@ -325,7 +356,7 @@ TerminalConOutOutputString (
 OutputError:\r
   REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
     EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
-    PcdGet32 (PcdStatusCodeValueRemoteConsoleOutputError),\r
+    (EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_EC_OUTPUT_ERROR),\r
     TerminalDevice->DevicePath\r
     );\r
 \r
@@ -335,6 +366,7 @@ OutputError:
 \r
 /**\r
   Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.TestString().\r
+\r
   If one of the characters in the *Wstring is\r
   neither valid Unicode drawing characters,\r
   not ASCII code, then this function will return\r
@@ -343,8 +375,8 @@ OutputError:
   @param  This              Indicates the calling context.\r
   @param  WString           The Null-terminated Unicode string to be tested.\r
 \r
-  @return EFI_SUCCESS       The terminal is capable of rendering the output string.\r
-  @return EFI_UNSUPPORTED   Some of the characters in the Unicode string cannot be rendered.\r
+  @retval EFI_SUCCESS       The terminal is capable of rendering the output string.\r
+  @retval EFI_UNSUPPORTED   Some of the characters in the Unicode string cannot be rendered.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -367,6 +399,7 @@ TerminalConOutTestString (
   case PCANSITYPE:\r
   case VT100TYPE:\r
   case VT100PLUSTYPE:\r
+  case TTYTERMTYPE:\r
     Status = AnsiTestString (TerminalDevice, WString);\r
     break;\r
 \r
@@ -385,19 +418,17 @@ TerminalConOutTestString (
 \r
 /**\r
   Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.QueryMode().\r
+\r
   It returns information for an available text mode\r
   that the terminal supports.\r
-  In this driver, we support text mode 80x25 (mode 0),\r
-  80x50 (mode 1), 100x31 (mode 2).\r
 \r
   @param This        Indicates the calling context.\r
   @param ModeNumber  The mode number to return information on.\r
   @param Columns     The returned columns of the requested mode.\r
   @param Rows        The returned rows of the requested mode.\r
 \r
-  @return EFI_SUCCESS       The requested mode information is returned.\r
-  @return EFI_UNSUPPORTED   The mode number is not valid.\r
-  @return EFI_DEVICE_ERROR\r
+  @retval EFI_SUCCESS       The requested mode information is returned.\r
+  @retval EFI_UNSUPPORTED   The mode number is not valid.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -409,40 +440,36 @@ TerminalConOutQueryMode (
   OUT UINTN                            *Rows\r
   )\r
 {\r
-  if (This->Mode->MaxMode > 3) {\r
-    return EFI_DEVICE_ERROR;\r
-  }\r
+  TERMINAL_DEV  *TerminalDevice;\r
 \r
-  if (ModeNumber == 0) {\r
-    *Columns  = MODE0_COLUMN_COUNT;\r
-    *Rows     = MODE0_ROW_COUNT;\r
-    return EFI_SUCCESS;\r
-  } else if (ModeNumber == 1) {\r
-    *Columns  = MODE1_COLUMN_COUNT;\r
-    *Rows     = MODE1_ROW_COUNT;\r
-    return EFI_SUCCESS;\r
-  } else if (ModeNumber == 2) {\r
-    *Columns  = MODE2_COLUMN_COUNT;\r
-    *Rows     = MODE2_ROW_COUNT;\r
-    return EFI_SUCCESS;\r
+  if (ModeNumber >= (UINTN) This->Mode->MaxMode) {\r
+    return EFI_UNSUPPORTED;\r
   }\r
 \r
-  return EFI_UNSUPPORTED;\r
+  //\r
+  // Get Terminal device data structure pointer.\r
+  //\r
+  TerminalDevice = TERMINAL_CON_OUT_DEV_FROM_THIS (This);  \r
+  *Columns = TerminalDevice->TerminalConsoleModeData[ModeNumber].Columns;\r
+  *Rows    = TerminalDevice->TerminalConsoleModeData[ModeNumber].Rows;\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 \r
 /**\r
   Implements EFI_SIMPLE_TEXT_OUT.SetMode().\r
+\r
   Set the terminal to a specified display mode.\r
   In this driver, we only support mode 0.\r
 \r
   @param This          Indicates the calling context.\r
   @param ModeNumber    The text mode to set.\r
 \r
-  @return EFI_SUCCESS       The requested text mode is set.\r
-  @return EFI_DEVICE_ERROR  The requested text mode cannot be set \r
+  @retval EFI_SUCCESS       The requested text mode is set.\r
+  @retval EFI_DEVICE_ERROR  The requested text mode cannot be set \r
                             because of serial device error.\r
-  @return EFI_UNSUPPORTED   The text mode number is not valid.\r
+  @retval EFI_UNSUPPORTED   The text mode number is not valid.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -460,7 +487,7 @@ TerminalConOutSetMode (
   //\r
   TerminalDevice = TERMINAL_CON_OUT_DEV_FROM_THIS (This);\r
 \r
-  if (ModeNumber > 2) {\r
+  if (ModeNumber >= (UINTN) This->Mode->MaxMode) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
@@ -498,9 +525,9 @@ TerminalConOutSetMode (
   @param Attribute   The attribute to set. Only bit0..6 are valid, all other bits\r
                      are undefined and must be zero.\r
 \r
-  @return EFI_SUCCESS        The requested attribute is set.\r
-  @return EFI_DEVICE_ERROR   The requested attribute cannot be set due to serial port error.\r
-  @return EFI_UNSUPPORTED    The attribute requested is not defined by EFI spec.\r
+  @retval EFI_SUCCESS        The requested attribute is set.\r
+  @retval EFI_DEVICE_ERROR   The requested attribute cannot be set due to serial port error.\r
+  @retval EFI_UNSUPPORTED    The attribute requested is not defined by EFI spec.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -535,7 +562,7 @@ TerminalConOutSetAttribute (
 \r
   //\r
   // Skip outputting the command string for the same attribute\r
-  // It improves the terminal performance siginificantly\r
+  // It improves the terminal performance significantly\r
   //\r
   if (This->Mode->Attribute == (INT32) Attribute) {\r
     return EFI_SUCCESS;\r
@@ -671,9 +698,9 @@ TerminalConOutSetAttribute (
 \r
   @param This     Indicates the calling context.\r
 \r
-  @return EFI_SUCCESS       The operation completed successfully.\r
-  @return EFI_DEVICE_ERROR  The terminal screen cannot be cleared due to serial port error.\r
-  @return EFI_UNSUPPORTED   The terminal is not in a valid display mode.\r
+  @retval EFI_SUCCESS       The operation completed successfully.\r
+  @retval EFI_DEVICE_ERROR  The terminal screen cannot be cleared due to serial port error.\r
+  @retval EFI_UNSUPPORTED   The terminal is not in a valid display mode.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -711,9 +738,9 @@ TerminalConOutClearScreen (
   @param Column    The row to set cursor to.\r
   @param Row       The column to set cursor to.\r
 \r
-  @return EFI_SUCCESS       The operation completed successfully.\r
-  @return EFI_DEVICE_ERROR  The request fails due to serial port error.\r
-  @return EFI_UNSUPPORTED   The terminal is not in a valid text mode, or the cursor position\r
+  @retval EFI_SUCCESS       The operation completed successfully.\r
+  @retval EFI_DEVICE_ERROR  The request fails due to serial port error.\r
+  @retval EFI_UNSUPPORTED   The terminal is not in a valid text mode, or the cursor position\r
                             is invalid for current mode.\r
 \r
 **/\r
@@ -730,6 +757,7 @@ TerminalConOutSetCursorPosition (
   UINTN                       MaxRow;\r
   EFI_STATUS                  Status;\r
   TERMINAL_DEV                *TerminalDevice;\r
+  CHAR16                      *String;\r
 \r
   TerminalDevice = TERMINAL_CON_OUT_DEV_FROM_THIS (This);\r
 \r
@@ -757,13 +785,36 @@ TerminalConOutSetCursorPosition (
   //\r
   // control sequence to move the cursor\r
   //\r
-  mSetCursorPositionString[ROW_OFFSET + 0]    = (CHAR16) ('0' + ((Row + 1) / 10));\r
-  mSetCursorPositionString[ROW_OFFSET + 1]    = (CHAR16) ('0' + ((Row + 1) % 10));\r
-  mSetCursorPositionString[COLUMN_OFFSET + 0] = (CHAR16) ('0' + ((Column + 1) / 10));\r
-  mSetCursorPositionString[COLUMN_OFFSET + 1] = (CHAR16) ('0' + ((Column + 1) % 10));\r
+  // Optimize cursor motion control sequences for TtyTerm.  Move\r
+  // within the current line if possible, and don't output anyting if\r
+  // it isn't necessary.\r
+  //\r
+  if (TerminalDevice->TerminalType == TTYTERMTYPE &&\r
+      (UINTN)Mode->CursorRow == Row) {\r
+    if ((UINTN)Mode->CursorColumn > Column) {\r
+      mCursorBackwardString[FW_BACK_OFFSET + 0] = (CHAR16) ('0' + ((Mode->CursorColumn - Column) / 10));\r
+      mCursorBackwardString[FW_BACK_OFFSET + 1] = (CHAR16) ('0' + ((Mode->CursorColumn - Column) % 10));\r
+      String = mCursorBackwardString;\r
+    }\r
+    else if (Column > (UINTN)Mode->CursorColumn) {\r
+      mCursorForwardString[FW_BACK_OFFSET + 0] = (CHAR16) ('0' + ((Column - Mode->CursorColumn) / 10));\r
+      mCursorForwardString[FW_BACK_OFFSET + 1] = (CHAR16) ('0' + ((Column - Mode->CursorColumn) % 10));\r
+      String = mCursorForwardString;\r
+    }\r
+    else {\r
+      String = L"";  // No cursor motion necessary\r
+    }\r
+  }\r
+  else {\r
+    mSetCursorPositionString[ROW_OFFSET + 0]    = (CHAR16) ('0' + ((Row + 1) / 10));\r
+    mSetCursorPositionString[ROW_OFFSET + 1]    = (CHAR16) ('0' + ((Row + 1) % 10));\r
+    mSetCursorPositionString[COLUMN_OFFSET + 0] = (CHAR16) ('0' + ((Column + 1) / 10));\r
+    mSetCursorPositionString[COLUMN_OFFSET + 1] = (CHAR16) ('0' + ((Column + 1) % 10));\r
+    String = mSetCursorPositionString;\r
+  }\r
 \r
   TerminalDevice->OutputEscChar               = TRUE;\r
-  Status = This->OutputString (This, mSetCursorPositionString);\r
+  Status = This->OutputString (This, String);\r
   TerminalDevice->OutputEscChar = FALSE;\r
 \r
   if (EFI_ERROR (Status)) {\r
@@ -782,14 +833,15 @@ TerminalConOutSetCursorPosition (
 \r
 /**\r
   Implements SIMPLE_TEXT_OUTPUT.EnableCursor().\r
+\r
   In this driver, the cursor cannot be hidden.\r
 \r
   @param This      Indicates the calling context.\r
   @param Visible   If TRUE, the cursor is set to be visible,\r
                    If FALSE, the cursor is set to be invisible.\r
 \r
-  @return EFI_SUCCESS      The request is valid.\r
-  @return EFI_UNSUPPORTED  The terminal does not support cursor hidden.\r
+  @retval EFI_SUCCESS      The request is valid.\r
+  @retval EFI_UNSUPPORTED  The terminal does not support cursor hidden.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -816,7 +868,7 @@ TerminalConOutEnableCursor (
   @param  Ascii        Optional pointer to return ASCII equivalent of\r
                        Graphic.\r
 \r
-  @return TRUE         If Graphic is a supported Unicode Box Drawing character.\r
+  @retval TRUE         If Graphic is a supported Unicode Box Drawing character.\r
 \r
 **/\r
 BOOLEAN\r