]> 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 b5a9f33e1407b870f01aff2a2c51f62b14b6f434..9625f4d518d968206315716dc4a5a4a08da51bad 100644 (file)
@@ -1,41 +1,28 @@
-/*++\r
+/** @file\r
+  Implementation for EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL protocol.\r
 \r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. 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
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \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
 \r
-Module Name:\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-    TerminalConOut.c\r
-    \r
-Abstract: \r
-    \r
-\r
-Revision History\r
---*/\r
-\r
-//\r
-// Include common header file for this module.\r
-//\r
-#include "CommonHeader.h"\r
+**/\r
 \r
 #include "Terminal.h"\r
 \r
-#include "FrameworkDxe.h"\r
-\r
 //\r
 // This list is used to define the valid extend chars.\r
 // It also provides a mapping from Unicode to PCANSI or\r
 // ASCII. The ASCII mapping we just made up.\r
 //\r
 //\r
-STATIC UNICODE_TO_CHAR  UnicodeToPcAnsiOrAscii[] = {\r
-  { BOXDRAW_HORIZONTAL,                 0xc4, L'-' }, \r
+UNICODE_TO_CHAR  UnicodeToPcAnsiOrAscii[] = {\r
+  { BOXDRAW_HORIZONTAL,                 0xc4, L'-' },\r
   { BOXDRAW_VERTICAL,                   0xb3, L'|' },\r
   { BOXDRAW_DOWN_RIGHT,                 0xda, L'/' },\r
   { BOXDRAW_DOWN_LEFT,                  0xbf, L'\\' },\r
@@ -84,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
@@ -96,40 +83,35 @@ 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
 //\r
+\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
+\r
+  @param  This                  Indicates the calling context.\r
+  @param  ExtendedVerification  Indicates that the driver may perform a more\r
+                                exhaustive verification operation of the device\r
+                                during reset.\r
+\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
 EFIAPI\r
 TerminalConOutReset (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
   IN  BOOLEAN                          ExtendedVerification\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset().\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
-  \r
-  Arguments:\r
-  \r
-    This - Indicates the calling context.\r
-    \r
-    ExtendedVerification - Indicates that the driver may perform a more exhaustive\r
-                           verification operation of the device during reset.\r
-        \r
-  Returns:\r
-  \r
-    EFI_SUCCESS\r
-       The reset operation succeeds.   \r
-    \r
-    EFI_DEVICE_ERROR\r
-      The terminal is not functioning correctly or the serial port reset fails.\r
-                \r
---*/\r
 {\r
   EFI_STATUS    Status;\r
   TERMINAL_DEV  *TerminalDevice;\r
@@ -145,7 +127,7 @@ TerminalConOutReset (
     //\r
     REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
       EFI_PROGRESS_CODE,\r
-      EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_PC_RESET,\r
+      (EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_PC_RESET),\r
       TerminalDevice->DevicePath\r
       );\r
 \r
@@ -156,7 +138,7 @@ TerminalConOutReset (
       //\r
       REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
         EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
-        EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_EC_CONTROLLER_ERROR,\r
+        (EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_EC_CONTROLLER_ERROR),\r
         TerminalDevice->DevicePath\r
         );\r
 \r
@@ -164,49 +146,37 @@ TerminalConOutReset (
     }\r
   }\r
 \r
-  This->SetAttribute (This, EFI_TEXT_ATTR (This->Mode->Attribute & 0x0F, EFI_BACKGROUND_BLACK));\r
+  This->SetAttribute (This, EFI_TEXT_ATTR (This->Mode->Attribute & 0x0F, EFI_BLACK));\r
 \r
   Status = This->SetMode (This, 0);\r
 \r
   return Status;\r
 }\r
 \r
+\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
+  @param  This                    Indicates the calling context.\r
+  @param  WString                 The Null-terminated Unicode string to be displayed\r
+                                  on the terminal screen.\r
+\r
+  @retval EFI_SUCCESS             The string is output successfully.\r
+  @retval EFI_DEVICE_ERROR        The serial port fails to send the string out.\r
+  @retval EFI_WARN_UNKNOWN_GLYPH  Indicates that some of the characters in the Unicode string could not\r
+                                  be rendered and are skipped.\r
+  @retval EFI_UNSUPPORTED         If current display mode is out of range.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 TerminalConOutOutputString (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
   IN  CHAR16                           *WString\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString().\r
-    The Unicode string will be converted to terminal expressible data stream\r
-    and send to terminal via serial port.\r
-    \r
-  \r
-  Arguments:\r
-  \r
-    This - Indicates the calling context.\r
-    \r
-    WString - The Null-terminated Unicode string to be displayed on \r
-              the terminal screen.\r
-        \r
-  Returns:\r
-  \r
-    EFI_SUCCESS\r
-       The string is output successfully.   \r
-    \r
-    EFI_DEVICE_ERROR\r
-      The serial port fails to send the string out.\r
-      \r
-    EFI_WARN_UNKNOWN_GLYPH\r
-      Indicates that some of the characters in the Unicode string could not \r
-      be rendered and are skipped.          \r
-      \r
-    EFI_UNSUPPORTED\r
-                \r
---*/\r
 {\r
   TERMINAL_DEV                *TerminalDevice;\r
   EFI_SIMPLE_TEXT_OUTPUT_MODE *Mode;\r
@@ -226,6 +196,7 @@ TerminalConOutOutputString (
 \r
   ValidBytes  = 0;\r
   Warning     = FALSE;\r
+  AsciiChar   = 0;\r
 \r
   //\r
   //  get Terminal device data structure pointer.\r
@@ -233,11 +204,11 @@ TerminalConOutOutputString (
   TerminalDevice = TERMINAL_CON_OUT_DEV_FROM_THIS (This);\r
 \r
   //\r
-  //  get current display mode\r
-  //  Terminal driver only support mode 0\r
+  //  Get current display mode\r
   //\r
   Mode = This->Mode;\r
-  if (Mode->Mode != 0) {\r
+\r
+  if (Mode->Mode >= Mode->MaxMode) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
@@ -252,9 +223,10 @@ TerminalConOutOutputString (
 \r
     switch (TerminalDevice->TerminalType) {\r
 \r
-    case PcAnsiType:\r
-    case VT100Type:\r
-    case VT100PlusType:\r
+    case PCANSITYPE:\r
+    case VT100TYPE:\r
+    case VT100PLUSTYPE:\r
+    case TTYTERMTYPE:\r
 \r
       if (!TerminalIsValidTextGraphics (*WString, &GraphicChar, &AsciiChar)) {\r
         //\r
@@ -280,7 +252,7 @@ TerminalConOutOutputString (
 \r
       }\r
 \r
-      if (TerminalDevice->TerminalType != PcAnsiType) {\r
+      if (TerminalDevice->TerminalType != PCANSITYPE) {\r
         GraphicChar = AsciiChar;\r
       }\r
 \r
@@ -298,7 +270,7 @@ TerminalConOutOutputString (
 \r
       break;\r
 \r
-    case VTUTF8Type:\r
+    case VTUTF8TYPE:\r
       UnicodeToUtf8 (*WString, &Utf8Char, &ValidBytes);\r
       Length = ValidBytes;\r
       Status = TerminalDevice->SerialIo->Write (\r
@@ -344,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
@@ -360,44 +356,35 @@ TerminalConOutOutputString (
 OutputError:\r
   REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
     EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
-    EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_EC_OUTPUT_ERROR,\r
+    (EFI_PERIPHERAL_REMOTE_CONSOLE | EFI_P_EC_OUTPUT_ERROR),\r
     TerminalDevice->DevicePath\r
     );\r
 \r
   return EFI_DEVICE_ERROR;\r
 }\r
 \r
+\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
+  EFI_UNSUPPORTED.\r
+\r
+  @param  This              Indicates the calling context.\r
+  @param  WString           The Null-terminated Unicode string to be tested.\r
+\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
 EFIAPI\r
 TerminalConOutTestString (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
   IN  CHAR16                           *WString\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.TestString().\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
-    EFI_UNSUPPORTED.\r
-        \r
-  \r
-  Arguments:\r
-  \r
-    This - Indicates the calling context.\r
-    \r
-    WString - The Null-terminated Unicode string to be tested.\r
-        \r
-  Returns:\r
-  \r
-    EFI_SUCCESS\r
-       The terminal is capable of rendering the output string. \r
-    \r
-    EFI_UNSUPPORTED\r
-      Some of the characters in the Unicode string cannot be rendered.      \r
-                \r
---*/\r
 {\r
   TERMINAL_DEV  *TerminalDevice;\r
   EFI_STATUS    Status;\r
@@ -409,13 +396,14 @@ TerminalConOutTestString (
 \r
   switch (TerminalDevice->TerminalType) {\r
 \r
-  case PcAnsiType:\r
-  case VT100Type:\r
-  case VT100PlusType:\r
+  case PCANSITYPE:\r
+  case VT100TYPE:\r
+  case VT100PLUSTYPE:\r
+  case TTYTERMTYPE:\r
     Status = AnsiTestString (TerminalDevice, WString);\r
     break;\r
 \r
-  case VTUTF8Type:\r
+  case VTUTF8TYPE:\r
     Status = VTUTF8TestString (TerminalDevice, WString);\r
     break;\r
 \r
@@ -427,6 +415,22 @@ TerminalConOutTestString (
   return Status;\r
 }\r
 \r
+\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
+\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
+  @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
 EFIAPI\r
 TerminalConOutQueryMode (\r
@@ -435,90 +439,45 @@ TerminalConOutQueryMode (
   OUT UINTN                            *Columns,\r
   OUT UINTN                            *Rows\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.QueryMode().\r
-    It returns information for an available text mode\r
-    that the terminal supports.\r
-    In this driver, we only support text mode 80x25, which is\r
-    defined as mode 0.\r
-        \r
-  \r
-  Arguments:\r
-  \r
-    *This\r
-        Indicates the calling context.\r
-    \r
-    ModeNumber\r
-        The mode number to return information on.\r
-        \r
-    Columns\r
-        The returned columns of the requested mode.\r
-        \r
-    Rows\r
-        The returned rows of the requested mode.                \r
-        \r
-  Returns:\r
-  \r
-    EFI_SUCCESS\r
-      The requested mode information is returned. \r
-    \r
-    EFI_UNSUPPORTED\r
-      The mode number is not valid.   \r
-      \r
-    EFI_DEVICE_ERROR\r
-                \r
---*/\r
 {\r
-  if (This->Mode->MaxMode > 1) {\r
-    return EFI_DEVICE_ERROR;\r
+  TERMINAL_DEV  *TerminalDevice;\r
+\r
+  if (ModeNumber >= (UINTN) This->Mode->MaxMode) {\r
+    return EFI_UNSUPPORTED;\r
   }\r
 \r
-  if (ModeNumber == 0) {\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
-    *Columns  = MODE0_COLUMN_COUNT;\r
-    *Rows     = MODE0_ROW_COUNT;\r
+  return EFI_SUCCESS;\r
+}\r
 \r
-    return EFI_SUCCESS;\r
-  }\r
 \r
-  return EFI_UNSUPPORTED;\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
+  @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
+  @retval EFI_UNSUPPORTED   The text mode number is not valid.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 TerminalConOutSetMode (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
   IN  UINTN                            ModeNumber\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements EFI_SIMPLE_TEXT_OUT.SetMode().\r
-    Set the terminal to a specified display mode.\r
-    In this driver, we only support mode 0.        \r
-  \r
-  Arguments:\r
-  \r
-    This\r
-        Indicates the calling context.\r
-    \r
-    ModeNumber\r
-        The text mode to set.\r
-        \r
-  Returns:\r
-  \r
-    EFI_SUCCESS\r
-       The requested text mode is set.\r
-       \r
-    EFI_DEVICE_ERROR\r
-      The requested text mode cannot be set because of serial device error.\r
-    \r
-    EFI_UNSUPPORTED\r
-      The text mode number is not valid.       \r
-                \r
---*/\r
 {\r
   EFI_STATUS    Status;\r
   TERMINAL_DEV  *TerminalDevice;\r
@@ -528,11 +487,14 @@ TerminalConOutSetMode (
   //\r
   TerminalDevice = TERMINAL_CON_OUT_DEV_FROM_THIS (This);\r
 \r
-  if (ModeNumber != 0) {\r
+  if (ModeNumber >= (UINTN) This->Mode->MaxMode) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  This->Mode->Mode = 0;\r
+  //\r
+  // Set the current mode\r
+  //\r
+  This->Mode->Mode = (INT32) ModeNumber;\r
 \r
   This->ClearScreen (This);\r
 \r
@@ -544,7 +506,7 @@ TerminalConOutSetMode (
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
-  This->Mode->Mode  = 0;\r
+  This->Mode->Mode  = (INT32) ModeNumber;\r
 \r
   Status            = This->ClearScreen (This);\r
   if (EFI_ERROR (Status)) {\r
@@ -555,38 +517,25 @@ TerminalConOutSetMode (
 \r
 }\r
 \r
+\r
+/**\r
+  Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute().\r
+\r
+  @param This        Indicates the calling context.\r
+  @param Attribute   The attribute to set. Only bit0..6 are valid, all other bits\r
+                     are undefined and must be zero.\r
+\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
 EFIAPI\r
 TerminalConOutSetAttribute (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
   IN  UINTN                            Attribute\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute().       \r
-  \r
-  Arguments:\r
-  \r
-    This\r
-        Indicates the calling context.\r
-    \r
-    Attribute\r
-        The attribute to set. Only bit0..6 are valid, all other bits\r
-        are undefined and must be zero.\r
-        \r
-  Returns:\r
-  \r
-    EFI_SUCCESS\r
-      The requested attribute is set. \r
-       \r
-    EFI_DEVICE_ERROR\r
-      The requested attribute cannot be set due to serial port error.\r
-          \r
-    EFI_UNSUPPORTED\r
-      The attribute requested is not defined by EFI spec.   \r
-                \r
---*/\r
 {\r
   UINT8         ForegroundControl;\r
   UINT8         BackgroundControl;\r
@@ -610,6 +559,15 @@ TerminalConOutSetAttribute (
   if ((Attribute | 0x7f) != 0x7f) {\r
     return EFI_UNSUPPORTED;\r
   }\r
+\r
+  //\r
+  // Skip outputting the command string for the same attribute\r
+  // It improves the terminal performance significantly\r
+  //\r
+  if (This->Mode->Attribute == (INT32) Attribute) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
   //\r
   //  convert Attribute value to terminal emulator\r
   //  understandable foreground color\r
@@ -732,36 +690,24 @@ TerminalConOutSetAttribute (
 \r
 }\r
 \r
+\r
+/**\r
+  Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.ClearScreen().\r
+  It clears the ANSI terminal's display to the\r
+  currently selected background color.\r
+\r
+  @param This     Indicates the calling context.\r
+\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
 EFIAPI\r
 TerminalConOutClearScreen (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.ClearScreen().\r
-    It clears the ANSI terminal's display to the \r
-    currently selected background color.\r
-        \r
-  \r
-  Arguments:\r
-  \r
-    This\r
-        Indicates the calling context.\r
-\r
-  Returns:\r
-  \r
-    EFI_SUCCESS\r
-      The operation completed successfully.\r
-       \r
-    EFI_DEVICE_ERROR\r
-      The terminal screen cannot be cleared due to serial port error.        \r
-    \r
-    EFI_UNSUPPORTED\r
-      The terminal is not in a valid display mode.       \r
-                \r
---*/\r
 {\r
   EFI_STATUS    Status;\r
   TERMINAL_DEV  *TerminalDevice;\r
@@ -784,6 +730,20 @@ TerminalConOutClearScreen (
   return Status;\r
 }\r
 \r
+\r
+/**\r
+  Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetCursorPosition().\r
+\r
+  @param This      Indicates the calling context.\r
+  @param Column    The row to set cursor to.\r
+  @param Row       The column to set cursor to.\r
+\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
 EFI_STATUS\r
 EFIAPI\r
 TerminalConOutSetCursorPosition (\r
@@ -791,41 +751,13 @@ TerminalConOutSetCursorPosition (
   IN  UINTN                            Column,\r
   IN  UINTN                            Row\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetCursorPosition().          \r
-  \r
-  Arguments:\r
-  \r
-    This\r
-        Indicates the calling context.\r
-        \r
-    Column\r
-        The row to set cursor to.\r
-        \r
-    Row\r
-        The column to set cursor to.                \r
-\r
-  Returns:\r
-  \r
-    EFI_SUCCESS\r
-      The operation completed successfully.\r
-       \r
-    EFI_DEVICE_ERROR\r
-      The request fails due to serial port error.        \r
-    \r
-    EFI_UNSUPPORTED\r
-      The terminal is not in a valid text mode, or the cursor position\r
-      is invalid for current mode.     \r
-                \r
---*/\r
 {\r
   EFI_SIMPLE_TEXT_OUTPUT_MODE *Mode;\r
   UINTN                       MaxColumn;\r
   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
@@ -853,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
@@ -875,36 +830,26 @@ TerminalConOutSetCursorPosition (
   return EFI_SUCCESS;\r
 }\r
 \r
+\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
+  @retval EFI_SUCCESS      The request is valid.\r
+  @retval EFI_UNSUPPORTED  The terminal does not support cursor hidden.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 TerminalConOutEnableCursor (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
   IN  BOOLEAN                          Visible\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements SIMPLE_TEXT_OUTPUT.EnableCursor().\r
-    In this driver, the cursor cannot be hidden.        \r
-  \r
-  Arguments:\r
-  \r
-    This\r
-        Indicates the calling context.\r
-        \r
-    Visible\r
-        If TRUE, the cursor is set to be visible,\r
-        If FALSE, the cursor is set to be invisible.        \r
-\r
-  Returns:\r
-  \r
-    EFI_SUCCESS\r
-      The request is valid.\r
-       \r
-    EFI_UNSUPPORTED\r
-      The terminal does not support cursor hidden.   \r
-                \r
---*/\r
 {\r
   if (!Visible) {\r
     return EFI_UNSUPPORTED;\r
@@ -913,31 +858,25 @@ TerminalConOutEnableCursor (
   return EFI_SUCCESS;\r
 }\r
 \r
+\r
+/**\r
+  Detects if a Unicode char is for Box Drawing text graphics.\r
+\r
+  @param  Graphic      Unicode char to test.\r
+  @param  PcAnsi       Optional pointer to return PCANSI equivalent of\r
+                       Graphic.\r
+  @param  Ascii        Optional pointer to return ASCII equivalent of\r
+                       Graphic.\r
+\r
+  @retval TRUE         If Graphic is a supported Unicode Box Drawing character.\r
+\r
+**/\r
 BOOLEAN\r
 TerminalIsValidTextGraphics (\r
   IN  CHAR16  Graphic,\r
   OUT CHAR8   *PcAnsi, OPTIONAL\r
   OUT CHAR8   *Ascii OPTIONAL\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-    Detects if a Unicode char is for Box Drawing text graphics.\r
-\r
-Arguments:\r
-\r
-    Graphic  - Unicode char to test.\r
-\r
-    PcAnsi  - Optional pointer to return PCANSI equivalent of Graphic.\r
-\r
-    Ascii   - Optional pointer to return ASCII equivalent of Graphic.\r
-\r
-Returns:\r
-\r
-    TRUE if Graphic is a supported Unicode Box Drawing character.\r
-\r
---*/\r
 {\r
   UNICODE_TO_CHAR *Table;\r
 \r
@@ -966,6 +905,15 @@ Returns:
   return FALSE;\r
 }\r
 \r
+/**\r
+  Detects if a valid ASCII char.\r
+\r
+  @param  Ascii        An ASCII character.\r
+                       \r
+  @retval TRUE         If it is a valid ASCII character.\r
+  @retval FALSE        If it is not a valid ASCII character.\r
+\r
+**/\r
 BOOLEAN\r
 TerminalIsValidAscii (\r
   IN  CHAR16  Ascii\r
@@ -981,6 +929,15 @@ TerminalIsValidAscii (
   return FALSE;\r
 }\r
 \r
+/**\r
+  Detects if a valid EFI control character.\r
+\r
+  @param  CharC        An input EFI Control character.\r
+                       \r
+  @retval TRUE         If it is a valid EFI control character.\r
+  @retval FALSE        If it is not a valid EFI control character.\r
+\r
+**/\r
 BOOLEAN\r
 TerminalIsValidEfiCntlChar (\r
   IN  CHAR16  CharC\r