]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Console/GraphicsConsoleDxe/GraphicsConsole.c
MdeModulePkg: Fix parameter verification for SetAttribute
[mirror_edk2.git] / MdeModulePkg / Universal / Console / GraphicsConsoleDxe / GraphicsConsole.c
index 13c5d699912b4d930ae6c4f3328b8b1db336fdd7..037fb5adaa22fe1199593c57e00eac366b49e335 100644 (file)
@@ -1,50 +1,23 @@
-/**@file\r
+/** @file\r
   This is the main routine for initializing the Graphics Console support routines.\r
-Remaining Tasks\r
-  Add all standard Glyphs from EFI 1.02 Specification\r
-  Implement optimal automatic Mode creation algorithm\r
-  Solve palette issues for mixed graphics and text\r
-  When does this protocol reset the palette?\r
-    \r
-Copyright (c) 2006 - 2007 Intel Corporation. <BR>\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
 \r
-**/\r
+Copyright (c) 2006 - 2014, 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
+http://opensource.org/licenses/bsd-license.php\r
 \r
-#include "GraphicsConsole.h"\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
-STATIC\r
-EFI_STATUS\r
-GetTextColors (\r
-  IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
-  OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL    *Foreground,\r
-  OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL    *Background\r
-  );\r
-\r
-STATIC\r
-EFI_STATUS\r
-DrawUnicodeWeightAtCursorN (\r
-  IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
-  IN  CHAR16                           *UnicodeWeight,\r
-  IN  UINTN                            Count\r
-  );\r
+**/\r
 \r
-STATIC\r
-EFI_STATUS\r
-EraseCursor (\r
-  IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This\r
-  );\r
+#include "GraphicsConsole.h"\r
 \r
 //\r
-// Globals\r
+// Graphics Console Device Private Data template\r
 //\r
-GRAPHICS_CONSOLE_DEV        mGraphicsConsoleDevTemplate = {\r
+GRAPHICS_CONSOLE_DEV    mGraphicsConsoleDevTemplate = {\r
   GRAPHICS_CONSOLE_DEV_SIGNATURE,\r
   (EFI_GRAPHICS_OUTPUT_PROTOCOL *) NULL,\r
   (EFI_UGA_DRAW_PROTOCOL *) NULL,\r
@@ -62,53 +35,64 @@ GRAPHICS_CONSOLE_DEV        mGraphicsConsoleDevTemplate = {
   },\r
   {\r
     0,\r
-    0,\r
+    -1,\r
     EFI_TEXT_ATTR(EFI_LIGHTGRAY, EFI_BLACK),\r
     0,\r
     0,\r
     TRUE\r
   },\r
-  {\r
-    { 80, 25, 0, 0, 0, 0 },  // Mode 0\r
-    { 80, 50, 0, 0, 0, 0 },  // Mode 1 \r
-    {  0,  0, 0, 0, 0, 0 }   // Mode 2\r
-  },\r
-  (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) NULL,\r
-  (EFI_HII_HANDLE) 0\r
+  (GRAPHICS_CONSOLE_MODE_DATA *) NULL,\r
+  (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) NULL\r
+};\r
+\r
+GRAPHICS_CONSOLE_MODE_DATA mGraphicsConsoleModeData[] = {\r
+  {100, 31},\r
+  //\r
+  // New modes can be added here.\r
+  // The last entry is specific for full screen mode.\r
+  //\r
+  {0, 0}\r
 };\r
 \r
-EFI_HII_PROTOCOL            *mHii;\r
+EFI_HII_DATABASE_PROTOCOL   *mHiiDatabase;\r
+EFI_HII_FONT_PROTOCOL       *mHiiFont;\r
+EFI_HII_HANDLE              mHiiHandle;\r
+VOID                        *mHiiRegistration;\r
+\r
+EFI_GUID             mFontPackageListGuid = {0xf5f219d3, 0x7006, 0x4648, {0xac, 0x8d, 0xd6, 0x1d, 0xfb, 0x7b, 0xc6, 0xad}};\r
 \r
-static CHAR16               mCrLfString[3] = { CHAR_CARRIAGE_RETURN, CHAR_LINEFEED, CHAR_NULL };\r
+CHAR16               mCrLfString[3] = { CHAR_CARRIAGE_RETURN, CHAR_LINEFEED, CHAR_NULL };\r
 \r
-static EFI_GRAPHICS_OUTPUT_BLT_PIXEL        mEfiColors[16] = {\r
+EFI_GRAPHICS_OUTPUT_BLT_PIXEL        mGraphicsEfiColors[16] = {\r
   //\r
-  // B     G     R\r
+  // B    G    R   reserved\r
   //\r
   {0x00, 0x00, 0x00, 0x00},  // BLACK\r
-  {0x98, 0x00, 0x00, 0x00},  // BLUE\r
-  {0x00, 0x98, 0x00, 0x00},  // GREEN\r
-  {0x98, 0x98, 0x00, 0x00},  // CYAN\r
-  {0x00, 0x00, 0x98, 0x00},  // RED\r
+  {0x98, 0x00, 0x00, 0x00},  // LIGHTBLUE\r
+  {0x00, 0x98, 0x00, 0x00},  // LIGHGREEN\r
+  {0x98, 0x98, 0x00, 0x00},  // LIGHCYAN\r
+  {0x00, 0x00, 0x98, 0x00},  // LIGHRED\r
   {0x98, 0x00, 0x98, 0x00},  // MAGENTA\r
   {0x00, 0x98, 0x98, 0x00},  // BROWN\r
   {0x98, 0x98, 0x98, 0x00},  // LIGHTGRAY\r
   {0x30, 0x30, 0x30, 0x00},  // DARKGRAY - BRIGHT BLACK\r
-  {0xff, 0x00, 0x00, 0x00},  // LIGHTBLUE - ?\r
-  {0x00, 0xff, 0x00, 0x00},  // LIGHTGREEN - ?\r
-  {0xff, 0xff, 0x00, 0x00},  // LIGHTCYAN\r
-  {0x00, 0x00, 0xff, 0x00},  // LIGHTRED\r
-  {0xff, 0x00, 0xff, 0x00},  // LIGHTMAGENTA\r
-  {0x00, 0xff, 0xff, 0x00},  // LIGHTBROWN\r
-  {0xff, 0xff, 0xff, 0x00}  // WHITE\r
+  {0xff, 0x00, 0x00, 0x00},  // BLUE\r
+  {0x00, 0xff, 0x00, 0x00},  // LIME\r
+  {0xff, 0xff, 0x00, 0x00},  // CYAN\r
+  {0x00, 0x00, 0xff, 0x00},  // RED\r
+  {0xff, 0x00, 0xff, 0x00},  // FUCHSIA\r
+  {0x00, 0xff, 0xff, 0x00},  // YELLOW\r
+  {0xff, 0xff, 0xff, 0x00}   // WHITE\r
 };\r
 \r
-static EFI_NARROW_GLYPH     mCursorGlyph = {\r
+EFI_NARROW_GLYPH     mCursorGlyph = {\r
   0x0000,\r
   0x00,\r
   { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF }\r
 };\r
 \r
+CHAR16       SpaceStr[] = { NARROW_CHAR, ' ', 0 };\r
+\r
 EFI_DRIVER_BINDING_PROTOCOL gGraphicsConsoleDriverBinding = {\r
   GraphicsConsoleControllerDriverSupported,\r
   GraphicsConsoleControllerDriverStart,\r
@@ -118,6 +102,22 @@ EFI_DRIVER_BINDING_PROTOCOL gGraphicsConsoleDriverBinding = {
   NULL\r
 };\r
 \r
+/**\r
+  Test to see if Graphics Console could be supported on the Controller.\r
+\r
+  Graphics Console could be supported if Graphics Output Protocol or UGA Draw\r
+  Protocol exists on the Controller. (UGA Draw Protocol could be skipped\r
+  if PcdUgaConsumeSupport is set to FALSE.)\r
+\r
+  @param  This                Protocol instance pointer.\r
+  @param  Controller          Handle of device to test.\r
+  @param  RemainingDevicePath Optional parameter use to pick a specific child\r
+                              device to start.\r
+\r
+  @retval EFI_SUCCESS         This driver supports this device.\r
+  @retval other               This driver does not support this device.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GraphicsConsoleControllerDriverSupported (\r
@@ -126,12 +126,13 @@ GraphicsConsoleControllerDriverSupported (
   IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath\r
   )\r
 {\r
-  EFI_STATUS                Status;\r
+  EFI_STATUS                   Status;\r
   EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;\r
-  EFI_UGA_DRAW_PROTOCOL     *UgaDraw;\r
-  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
+  EFI_UGA_DRAW_PROTOCOL        *UgaDraw;\r
+  EFI_DEVICE_PATH_PROTOCOL     *DevicePath;\r
 \r
-  UgaDraw = NULL;\r
+  GraphicsOutput = NULL;\r
+  UgaDraw        = NULL;\r
   //\r
   // Open the IO Abstraction(s) needed to perform the supported test\r
   //\r
@@ -143,9 +144,8 @@ GraphicsConsoleControllerDriverSupported (
                   Controller,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
                   );\r
-  \r
-  if (EFI_ERROR (Status)) {\r
-    GraphicsOutput = NULL;\r
+\r
+  if (EFI_ERROR (Status) && FeaturePcdGet (PcdUgaConsumeSupport)) {\r
     //\r
     // Open Graphics Output Protocol failed, try to open UGA Draw Protocol\r
     //\r
@@ -157,9 +157,9 @@ GraphicsConsoleControllerDriverSupported (
                     Controller,\r
                     EFI_OPEN_PROTOCOL_BY_DRIVER\r
                     );\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
+  }\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
   }\r
 \r
   //\r
@@ -185,6 +185,7 @@ GraphicsConsoleControllerDriverSupported (
   } else {\r
     goto Error;\r
   }\r
+\r
   //\r
   // Does Hii Exist?  If not, we aren't ready to run\r
   //\r
@@ -201,7 +202,7 @@ Error:
           This->DriverBindingHandle,\r
           Controller\r
           );\r
-  } else {\r
+  } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {\r
     gBS->CloseProtocol (\r
           Controller,\r
           &gEfiUgaDrawProtocolGuid,\r
@@ -212,47 +213,182 @@ Error:
   return Status;\r
 }\r
 \r
+/**\r
+  Initialize all the text modes which the graphics console supports.\r
+\r
+  It returns information for available text modes that the graphics can support.\r
+\r
+  @param[in]  HorizontalResolution     The size of video screen in pixels in the X dimension.\r
+  @param[in]  VerticalResolution       The size of video screen in pixels in the Y dimension.\r
+  @param[in]  GopModeNumber            The graphics mode number which graphis console is based on.\r
+  @param[out] TextModeCount            The total number of text modes that graphics console supports.\r
+  @param[out] TextModeData             The buffer to the text modes column and row information.\r
+                                       Caller is responsible to free it when it's non-NULL.\r
+\r
+  @retval EFI_SUCCESS                  The supporting mode information is returned.\r
+  @retval EFI_INVALID_PARAMETER        The parameters are invalid.\r
+\r
+**/\r
 EFI_STATUS\r
-EFIAPI\r
-GraphicsConsoleControllerDriverStart (\r
-  IN EFI_DRIVER_BINDING_PROTOCOL    *This,\r
-  IN EFI_HANDLE                     Controller,\r
-  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath\r
+InitializeGraphicsConsoleTextMode (\r
+  IN UINT32                        HorizontalResolution,\r
+  IN UINT32                        VerticalResolution,\r
+  IN UINT32                        GopModeNumber,\r
+  OUT UINTN                        *TextModeCount,\r
+  OUT GRAPHICS_CONSOLE_MODE_DATA   **TextModeData\r
   )\r
-/*++\r
+{\r
+  UINTN                       Index;\r
+  UINTN                       Count;\r
+  GRAPHICS_CONSOLE_MODE_DATA  *ModeBuffer;\r
+  GRAPHICS_CONSOLE_MODE_DATA  *NewModeBuffer;\r
+  UINTN                       ValidCount;\r
+  UINTN                       ValidIndex;\r
+  UINTN                       MaxColumns;\r
+  UINTN                       MaxRows;  \r
+  \r
+  if ((TextModeCount == NULL) || (TextModeData == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Count = sizeof (mGraphicsConsoleModeData) / sizeof (GRAPHICS_CONSOLE_MODE_DATA);\r
+\r
+  //\r
+  // Compute the maximum number of text Rows and Columns that this current graphics mode can support.\r
+  // To make graphics console work well, MaxColumns and MaxRows should not be zero.\r
+  //\r
+  MaxColumns = HorizontalResolution / EFI_GLYPH_WIDTH;\r
+  MaxRows    = VerticalResolution / EFI_GLYPH_HEIGHT;\r
+\r
+  //\r
+  // According to UEFI spec, all output devices support at least 80x25 text mode.\r
+  //\r
+  ASSERT ((MaxColumns >= 80) && (MaxRows >= 25));\r
+\r
+  //\r
+  // Add full screen mode to the last entry.\r
+  //\r
+  mGraphicsConsoleModeData[Count - 1].Columns = MaxColumns;\r
+  mGraphicsConsoleModeData[Count - 1].Rows    = MaxRows;\r
+\r
+  //\r
+  // Get defined mode buffer pointer.\r
+  //\r
+  ModeBuffer = mGraphicsConsoleModeData;\r
 \r
-  Routine Description:\r
+  //\r
+  // Here we make sure that the final mode exposed does not include the duplicated modes,\r
+  // and does not include the invalid modes which exceed the max column and row.\r
+  // Reserve 2 modes for 80x25, 80x50 of graphics console.\r
+  //\r
+  NewModeBuffer = AllocateZeroPool (sizeof (GRAPHICS_CONSOLE_MODE_DATA) * (Count + 2));\r
+  ASSERT (NewModeBuffer != NULL);\r
+\r
+  //\r
+  // Mode 0 and mode 1 is for 80x25, 80x50 according to UEFI spec.\r
+  //\r
+  ValidCount = 0;  \r
 \r
-    Start the controller.\r
+  NewModeBuffer[ValidCount].Columns       = 80;\r
+  NewModeBuffer[ValidCount].Rows          = 25;\r
+  NewModeBuffer[ValidCount].GopWidth      = HorizontalResolution;\r
+  NewModeBuffer[ValidCount].GopHeight     = VerticalResolution;\r
+  NewModeBuffer[ValidCount].GopModeNumber = GopModeNumber;\r
+  NewModeBuffer[ValidCount].DeltaX        = (HorizontalResolution - (NewModeBuffer[ValidCount].Columns * EFI_GLYPH_WIDTH)) >> 1;\r
+  NewModeBuffer[ValidCount].DeltaY        = (VerticalResolution - (NewModeBuffer[ValidCount].Rows * EFI_GLYPH_HEIGHT)) >> 1;      \r
+  ValidCount++;\r
 \r
-  Arguments:\r
+  if ((MaxColumns >= 80) && (MaxRows >= 50)) {\r
+    NewModeBuffer[ValidCount].Columns = 80;\r
+    NewModeBuffer[ValidCount].Rows    = 50;\r
+    NewModeBuffer[ValidCount].DeltaX  = (HorizontalResolution - (80 * EFI_GLYPH_WIDTH)) >> 1;\r
+    NewModeBuffer[ValidCount].DeltaY  = (VerticalResolution - (50 * EFI_GLYPH_HEIGHT)) >> 1;    \r
+  }\r
+  NewModeBuffer[ValidCount].GopWidth      = HorizontalResolution;\r
+  NewModeBuffer[ValidCount].GopHeight     = VerticalResolution;\r
+  NewModeBuffer[ValidCount].GopModeNumber = GopModeNumber;\r
+  ValidCount++;\r
+  \r
+  //\r
+  // Start from mode 2 to put the valid mode other than 80x25 and 80x50 in the output mode buffer.\r
+  //\r
+  for (Index = 0; Index < Count; Index++) {\r
+    if ((ModeBuffer[Index].Columns == 0) || (ModeBuffer[Index].Rows == 0) ||\r
+        (ModeBuffer[Index].Columns > MaxColumns) || (ModeBuffer[Index].Rows > MaxRows)) {\r
+      //\r
+      // Skip the pre-defined mode which is invalid or exceeds the max column and row.\r
+      //\r
+      continue;\r
+    }\r
+    for (ValidIndex = 0; ValidIndex < ValidCount; ValidIndex++) {\r
+      if ((ModeBuffer[Index].Columns == NewModeBuffer[ValidIndex].Columns) &&\r
+          (ModeBuffer[Index].Rows == NewModeBuffer[ValidIndex].Rows)) {\r
+        //\r
+        // Skip the duplicated mode.\r
+        //\r
+        break;\r
+      }\r
+    }\r
+    if (ValidIndex == ValidCount) {\r
+      NewModeBuffer[ValidCount].Columns       = ModeBuffer[Index].Columns;\r
+      NewModeBuffer[ValidCount].Rows          = ModeBuffer[Index].Rows;\r
+      NewModeBuffer[ValidCount].GopWidth      = HorizontalResolution;\r
+      NewModeBuffer[ValidCount].GopHeight     = VerticalResolution;\r
+      NewModeBuffer[ValidCount].GopModeNumber = GopModeNumber;\r
+      NewModeBuffer[ValidCount].DeltaX        = (HorizontalResolution - (NewModeBuffer[ValidCount].Columns * EFI_GLYPH_WIDTH)) >> 1;\r
+      NewModeBuffer[ValidCount].DeltaY        = (VerticalResolution - (NewModeBuffer[ValidCount].Rows * EFI_GLYPH_HEIGHT)) >> 1;\r
+      ValidCount++;\r
+    }\r
+  }\r
\r
+  DEBUG_CODE (\r
+    for (Index = 0; Index < ValidCount; Index++) {\r
+      DEBUG ((EFI_D_INFO, "Graphics - Mode %d, Column = %d, Row = %d\n", \r
+                           Index, NewModeBuffer[Index].Columns, NewModeBuffer[Index].Rows));  \r
+    }\r
+  );\r
+  \r
+  //\r
+  // Return valid mode count and mode information buffer.\r
+  //\r
+  *TextModeCount = ValidCount;\r
+  *TextModeData  = NewModeBuffer;\r
+  return EFI_SUCCESS;\r
+}\r
 \r
-    This                - A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
-    Controller          - The handle of the controller to start.\r
-    RemainingDevicePath - A pointer to the remaining portion of a devcie path.\r
+/**\r
+  Start this driver on Controller by opening Graphics Output protocol or\r
+  UGA Draw protocol, and installing Simple Text Out protocol on Controller.\r
+  (UGA Draw protocol could be skipped if PcdUgaConsumeSupport is set to FALSE.)\r
 \r
-  Returns:\r
+  @param  This                 Protocol instance pointer.\r
+  @param  Controller           Handle of device to bind driver to\r
+  @param  RemainingDevicePath  Optional parameter use to pick a specific child\r
+                               device to start.\r
 \r
-    EFI_SUCCESS          - Return successfully.\r
-    EFI_OUT_OF_RESOURCES - Out of resources.\r
+  @retval EFI_SUCCESS          This driver is added to Controller.\r
+  @retval other                This driver does not support this device.\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GraphicsConsoleControllerDriverStart (\r
+  IN EFI_DRIVER_BINDING_PROTOCOL    *This,\r
+  IN EFI_HANDLE                     Controller,\r
+  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath\r
+  )\r
 {\r
-  EFI_STATUS            Status;\r
-  GRAPHICS_CONSOLE_DEV  *Private;\r
-  EFI_HII_PACKAGES      *Package;\r
-  EFI_HII_FONT_PACK     *FontPack;\r
-  UINTN                 NarrowFontSize;\r
-  UINT32                HorizontalResolution;\r
-  UINT32                VerticalResolution;\r
-  UINT32                ColorDepth;\r
-  UINT32                RefreshRate;\r
-  UINTN                 MaxMode;\r
-  UINTN                 Columns;\r
-  UINTN                 Rows;\r
-  UINT8                 *Location;\r
+  EFI_STATUS                           Status;\r
+  GRAPHICS_CONSOLE_DEV                 *Private;\r
+  UINT32                               HorizontalResolution;\r
+  UINT32                               VerticalResolution;\r
+  UINT32                               ColorDepth;\r
+  UINT32                               RefreshRate;\r
+  UINT32                               ModeIndex;\r
+  UINTN                                MaxMode;\r
   UINT32                               ModeNumber;\r
-  UINTN                                SizeOfInfo;\r
+  EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE    *Mode;\r
+  UINTN                                SizeOfInfo;  \r
   EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;\r
   \r
   ModeNumber = 0;\r
@@ -278,9 +414,8 @@ GraphicsConsoleControllerDriverStart (
                   Controller,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
                   );\r
-  if (EFI_ERROR(Status)) {\r
-    Private->GraphicsOutput = NULL;\r
 \r
+  if (EFI_ERROR(Status) && FeaturePcdGet (PcdUgaConsumeSupport)) {\r
     Status = gBS->OpenProtocol (\r
                     Controller,\r
                     &gEfiUgaDrawProtocolGuid,\r
@@ -289,86 +424,95 @@ GraphicsConsoleControllerDriverStart (
                     Controller,\r
                     EFI_OPEN_PROTOCOL_BY_DRIVER\r
                     );\r
-    if (EFI_ERROR (Status)) {\r
-      goto Error;\r
-    }\r
   }\r
 \r
-  //\r
-  // Get the HII protocol. If Supported() succeeds, do we really\r
-  // need to get HII protocol again?\r
-  //\r
-  Status = EfiLocateHiiProtocol ();\r
   if (EFI_ERROR (Status)) {\r
     goto Error;\r
   }\r
 \r
-  NarrowFontSize  = ReturnNarrowFontSize ();\r
-\r
-  FontPack        = AllocateZeroPool (sizeof (EFI_HII_FONT_PACK) + NarrowFontSize);\r
-  ASSERT (FontPack);\r
-\r
-  FontPack->Header.Length         = (UINT32) (sizeof (EFI_HII_FONT_PACK) + NarrowFontSize);\r
-  FontPack->Header.Type           = EFI_HII_FONT;\r
-  FontPack->NumberOfNarrowGlyphs  = (UINT16) (NarrowFontSize / sizeof (EFI_NARROW_GLYPH));\r
-\r
-  Location                        = (UINT8 *) (&FontPack->NumberOfWideGlyphs + sizeof (UINT8));\r
-  CopyMem (Location, UsStdNarrowGlyphData, NarrowFontSize);\r
-\r
-  //\r
-  // Register our Fonts into the global database\r
-  //\r
-  Package = PreparePackages (1, NULL, FontPack);\r
-  mHii->NewPack (mHii, Package, &(Private->HiiHandle));\r
-  FreePool (Package);\r
-\r
-  //\r
-  // Free the font database\r
-  //\r
-  FreePool (FontPack);\r
-\r
-  //\r
-  // If the current mode information can not be retrieved, then attemp to set the default mode\r
-  // of 800x600, 32 bit colot, 60 Hz refresh.\r
-  //\r
-  HorizontalResolution  = 800;\r
-  VerticalResolution    = 600;\r
+  HorizontalResolution  = PcdGet32 (PcdVideoHorizontalResolution);\r
+  VerticalResolution    = PcdGet32 (PcdVideoVerticalResolution);\r
 \r
   if (Private->GraphicsOutput != NULL) {\r
     //\r
-    // The console is build on top of Graphics Output Protocol, find the mode number for 800x600\r
+    // The console is build on top of Graphics Output Protocol, find the mode number\r
+    // for the user-defined mode; if there are multiple video devices,\r
+    // graphic console driver will set all the video devices to the same mode.\r
     //\r
-    for (ModeNumber = 0; ModeNumber < Private->GraphicsOutput->Mode->MaxMode; ModeNumber++) {\r
-      Status = Private->GraphicsOutput->QueryMode (\r
-                         Private->GraphicsOutput,\r
-                         ModeNumber,\r
-                         &SizeOfInfo,\r
-                         &Info\r
-                         );\r
-      if (!EFI_ERROR (Status)) {\r
-        if ((Info->HorizontalResolution == 800) &&\r
-            (Info->VerticalResolution == 600)) {\r
-          Status = Private->GraphicsOutput->SetMode (Private->GraphicsOutput, ModeNumber);\r
-          if (!EFI_ERROR (Status)) {\r
-            FreePool (Info);\r
-            break;\r
+    if ((HorizontalResolution == 0x0) || (VerticalResolution == 0x0)) {\r
+      //\r
+      // Find the highest resolution which GOP supports.\r
+      //    \r
+      MaxMode = Private->GraphicsOutput->Mode->MaxMode;\r
+      \r
+      for (ModeIndex = 0; ModeIndex < MaxMode; ModeIndex++) {\r
+        Status = Private->GraphicsOutput->QueryMode (\r
+                           Private->GraphicsOutput,\r
+                           ModeIndex,\r
+                           &SizeOfInfo,\r
+                           &Info\r
+                           );\r
+        if (!EFI_ERROR (Status)) {\r
+          if ((Info->HorizontalResolution > HorizontalResolution) ||\r
+              ((Info->HorizontalResolution == HorizontalResolution) && (Info->VerticalResolution > VerticalResolution))) {\r
+            HorizontalResolution = Info->HorizontalResolution;\r
+            VerticalResolution   = Info->VerticalResolution;\r
+            ModeNumber           = ModeIndex;\r
           }\r
+          FreePool (Info);\r
+        }\r
+      }\r
+      if ((HorizontalResolution == 0x0) || (VerticalResolution == 0x0)) {\r
+        Status = EFI_UNSUPPORTED;\r
+        goto Error;\r
+      }\r
+    } else {\r
+      //\r
+      // Use user-defined resolution\r
+      //\r
+      Status = CheckModeSupported (\r
+                   Private->GraphicsOutput,\r
+                   HorizontalResolution,\r
+                   VerticalResolution,\r
+                   &ModeNumber\r
+                   );\r
+      if (EFI_ERROR (Status)) {\r
+        //\r
+        // if not supporting current mode, try 800x600 which is required by UEFI/EFI spec\r
+        //\r
+        Status = CheckModeSupported (\r
+                     Private->GraphicsOutput,\r
+                     800,\r
+                     600,\r
+                     &ModeNumber\r
+                     );\r
+        Mode = Private->GraphicsOutput->Mode;\r
+        if (EFI_ERROR (Status) && Mode->MaxMode != 0) {\r
+          //\r
+          // Set default mode failed or device don't support default mode, then get the current mode information\r
+          //\r
+          HorizontalResolution = Mode->Info->HorizontalResolution;\r
+          VerticalResolution = Mode->Info->VerticalResolution;\r
+          ModeNumber = Mode->Mode;\r
         }\r
-        FreePool (Info);\r
       }\r
     }\r
-\r
-    if (EFI_ERROR (Status) || (ModeNumber == Private->GraphicsOutput->Mode->MaxMode)) {\r
+    if (ModeNumber != Private->GraphicsOutput->Mode->Mode) {\r
       //\r
-      // Set default mode failed or device don't support default mode, then get the current mode information\r
+      // Current graphics mode is not set or is not set to the mode which we has found,\r
+      // set the new graphic mode.\r
       //\r
-      HorizontalResolution = Private->GraphicsOutput->Mode->Info->HorizontalResolution;\r
-      VerticalResolution = Private->GraphicsOutput->Mode->Info->VerticalResolution;\r
-      ModeNumber = Private->GraphicsOutput->Mode->Mode;\r
+      Status = Private->GraphicsOutput->SetMode (Private->GraphicsOutput, ModeNumber);\r
+      if (EFI_ERROR (Status)) {\r
+        //\r
+        // The mode set operation failed\r
+        //\r
+        goto Error;\r
+      }\r
     }\r
-  } else {\r
+  } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {\r
     //\r
-    // The console is build on top of UGA Draw Protocol\r
+    // At first try to set user-defined resolution\r
     //\r
     ColorDepth            = 32;\r
     RefreshRate           = 60;\r
@@ -381,95 +525,61 @@ GraphicsConsoleControllerDriverStart (
                                 );\r
     if (EFI_ERROR (Status)) {\r
       //\r
-      // Get the current mode information from the UGA Draw Protocol\r
+      // Try to set 800*600 which is required by UEFI/EFI spec\r
       //\r
-      Status = Private->UgaDraw->GetMode (\r
+      Status = Private->UgaDraw->SetMode (\r
                                   Private->UgaDraw,\r
-                                  &HorizontalResolution,\r
-                                  &VerticalResolution,\r
-                                  &ColorDepth,\r
-                                  &RefreshRate\r
+                                  800,\r
+                                  600,\r
+                                  ColorDepth,\r
+                                  RefreshRate\r
                                   );\r
       if (EFI_ERROR (Status)) {\r
-        goto Error;\r
+        Status = Private->UgaDraw->GetMode (\r
+                                    Private->UgaDraw,\r
+                                    &HorizontalResolution,\r
+                                    &VerticalResolution,\r
+                                    &ColorDepth,\r
+                                    &RefreshRate\r
+                                    );\r
+        if (EFI_ERROR (Status)) {\r
+          goto Error;\r
+        }\r
       }\r
     }\r
   }\r
 \r
-  //\r
-  // Compute the maximum number of text Rows and Columns that this current graphics mode can support\r
-  //\r
-  Columns = HorizontalResolution / GLYPH_WIDTH;\r
-  Rows    = VerticalResolution / GLYPH_HEIGHT;\r
+  DEBUG ((EFI_D_INFO, "GraphicsConsole video resolution %d x %d\n", HorizontalResolution, VerticalResolution));\r
 \r
   //\r
-  // See if the mode is too small to support the required 80x25 text mode\r
+  // Initialize the mode which GraphicsConsole supports.\r
   //\r
-  if (Columns < 80 || Rows < 25) {\r
-    goto Error;\r
-  }\r
-  //\r
-  // Add Mode #0 that must be 80x25\r
-  //\r
-  MaxMode = 0;\r
-  Private->ModeData[MaxMode].GopWidth   = HorizontalResolution;\r
-  Private->ModeData[MaxMode].GopHeight  = VerticalResolution;\r
-  Private->ModeData[MaxMode].GopModeNumber = ModeNumber;\r
-  Private->ModeData[MaxMode].DeltaX     = (HorizontalResolution - (80 * GLYPH_WIDTH)) >> 1;\r
-  Private->ModeData[MaxMode].DeltaY     = (VerticalResolution - (25 * GLYPH_HEIGHT)) >> 1;\r
-  MaxMode++;\r
+  Status = InitializeGraphicsConsoleTextMode (\r
+             HorizontalResolution,\r
+             VerticalResolution,\r
+             ModeNumber,\r
+             &MaxMode,\r
+             &Private->ModeData\r
+             );\r
 \r
-  //\r
-  // If it is possible to support Mode #1 - 80x50, than add it as an active mode\r
-  //\r
-  if (Rows >= 50) {\r
-    Private->ModeData[MaxMode].GopWidth   = HorizontalResolution;\r
-    Private->ModeData[MaxMode].GopHeight  = VerticalResolution;\r
-    Private->ModeData[MaxMode].GopModeNumber = ModeNumber;\r
-    Private->ModeData[MaxMode].DeltaX     = (HorizontalResolution - (80 * GLYPH_WIDTH)) >> 1;\r
-    Private->ModeData[MaxMode].DeltaY     = (VerticalResolution - (50 * GLYPH_HEIGHT)) >> 1;\r
-    MaxMode++;\r
+  if (EFI_ERROR (Status)) {\r
+    goto Error;\r
   }\r
-  //\r
-  // If the graphics mode is 800x600, than add a text mode that uses the entire display\r
-  //\r
-  if (HorizontalResolution == 800 && VerticalResolution == 600) {\r
 \r
-    if (MaxMode < 2) {\r
-      Private->ModeData[MaxMode].Columns    = 0;\r
-      Private->ModeData[MaxMode].Rows       = 0;\r
-      Private->ModeData[MaxMode].GopWidth   = 800;\r
-      Private->ModeData[MaxMode].GopHeight  = 600;\r
-      Private->ModeData[MaxMode].GopModeNumber = ModeNumber;\r
-      Private->ModeData[MaxMode].DeltaX     = 0;\r
-      Private->ModeData[MaxMode].DeltaY     = 0;\r
-      MaxMode++;\r
-    }\r
-\r
-    Private->ModeData[MaxMode].Columns    = 800 / GLYPH_WIDTH;\r
-    Private->ModeData[MaxMode].Rows       = 600 / GLYPH_HEIGHT;\r
-    Private->ModeData[MaxMode].GopWidth   = 800;\r
-    Private->ModeData[MaxMode].GopHeight  = 600;\r
-    Private->ModeData[MaxMode].GopModeNumber = ModeNumber;\r
-    Private->ModeData[MaxMode].DeltaX     = (800 % GLYPH_WIDTH) >> 1;\r
-    Private->ModeData[MaxMode].DeltaY     = (600 % GLYPH_HEIGHT) >> 1;\r
-    MaxMode++;\r
-  }\r
   //\r
   // Update the maximum number of modes\r
   //\r
   Private->SimpleTextOutputMode.MaxMode = (INT32) MaxMode;\r
 \r
-  //\r
-  // Determine the number of text modes that this protocol can support\r
-  //\r
-  Status = GraphicsConsoleConOutSetMode (&Private->SimpleTextOutput, 0);\r
-  if (EFI_ERROR (Status)) {\r
-    goto Error;\r
-  }\r
-\r
   DEBUG_CODE_BEGIN ();\r
-    GraphicsConsoleConOutOutputString (&Private->SimpleTextOutput, (CHAR16 *)L"Graphics Console Started\n\r");\r
+    Status = GraphicsConsoleConOutSetMode (&Private->SimpleTextOutput, 0);\r
+    if (EFI_ERROR (Status)) {\r
+      goto Error;\r
+    }\r
+    Status = GraphicsConsoleConOutOutputString (&Private->SimpleTextOutput, (CHAR16 *)L"Graphics Console Started\n\r");\r
+    if (EFI_ERROR (Status)) {\r
+      goto Error;\r
+    }  \r
   DEBUG_CODE_END ();\r
 \r
   //\r
@@ -485,38 +595,59 @@ GraphicsConsoleControllerDriverStart (
 Error:\r
   if (EFI_ERROR (Status)) {\r
     //\r
-    // Close the GOP or UGA IO Protocol\r
+    // Close the GOP and UGA Draw Protocol\r
     //\r
     if (Private->GraphicsOutput != NULL) {\r
       gBS->CloseProtocol (\r
-            Controller,\r
-            &gEfiGraphicsOutputProtocolGuid,\r
-            This->DriverBindingHandle,\r
-            Controller\r
-            );\r
-    } else {\r
+             Controller,\r
+             &gEfiGraphicsOutputProtocolGuid,\r
+             This->DriverBindingHandle,\r
+             Controller\r
+             );\r
+    } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {\r
       gBS->CloseProtocol (\r
-            Controller,\r
-            &gEfiUgaDrawProtocolGuid,\r
-            This->DriverBindingHandle,\r
-            Controller\r
-            );\r
+             Controller,\r
+             &gEfiUgaDrawProtocolGuid,\r
+             This->DriverBindingHandle,\r
+             Controller\r
+             );\r
+    }\r
+\r
+    if (Private->LineBuffer != NULL) {\r
+      FreePool (Private->LineBuffer);\r
+    }\r
+\r
+    if (Private->ModeData != NULL) {\r
+      FreePool (Private->ModeData);\r
     }\r
 \r
     //\r
     // Free private data\r
     //\r
-    if (Private != NULL) {\r
-      if (Private->LineBuffer != NULL) {\r
-        FreePool (Private->LineBuffer);\r
-      }\r
-      FreePool (Private);\r
-    }\r
+    FreePool (Private);\r
   }\r
 \r
   return Status;\r
 }\r
 \r
+/**\r
+  Stop this driver on Controller by removing Simple Text Out protocol\r
+  and closing the Graphics Output Protocol or UGA Draw protocol on Controller.\r
+  (UGA Draw protocol could be skipped if PcdUgaConsumeSupport is set to FALSE.)\r
+\r
+\r
+  @param  This              Protocol instance pointer.\r
+  @param  Controller        Handle of device to stop driver on\r
+  @param  NumberOfChildren  Number of Handles in ChildHandleBuffer. If number of\r
+                            children is zero stop the entire bus driver.\r
+  @param  ChildHandleBuffer List of Child Handles to Stop.\r
+\r
+  @retval EFI_SUCCESS       This driver is removed Controller.\r
+  @retval EFI_NOT_STARTED   Simple Text Out protocol could not be found the\r
+                            Controller.\r
+  @retval other             This driver was not removed from this device.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GraphicsConsoleControllerDriverStop (\r
@@ -561,7 +692,7 @@ GraphicsConsoleControllerDriverStop (
             This->DriverBindingHandle,\r
             Controller\r
             );\r
-    } else {\r
+    } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {\r
       gBS->CloseProtocol (\r
             Controller,\r
             &gEfiUgaDrawProtocolGuid,\r
@@ -570,139 +701,189 @@ GraphicsConsoleControllerDriverStop (
             );\r
     }\r
 \r
-    //\r
-    // Remove the font pack\r
-    //\r
-    mHii->RemovePack (mHii, Private->HiiHandle);\r
+    if (Private->LineBuffer != NULL) {\r
+      FreePool (Private->LineBuffer);\r
+    }\r
+\r
+    if (Private->ModeData != NULL) {\r
+      FreePool (Private->ModeData);\r
+    }\r
 \r
     //\r
     // Free our instance data\r
     //\r
-    if (Private != NULL) {\r
-      FreePool (Private->LineBuffer);\r
-      FreePool (Private);\r
-    }\r
+    FreePool (Private);\r
   }\r
 \r
   return Status;\r
 }\r
 \r
+/**\r
+  Check if the current specific mode supported the user defined resolution\r
+  for the Graphics Console device based on Graphics Output Protocol.\r
+\r
+  If yes, set the graphic devcice's current mode to this specific mode.\r
+\r
+  @param  GraphicsOutput        Graphics Output Protocol instance pointer.\r
+  @param  HorizontalResolution  User defined horizontal resolution\r
+  @param  VerticalResolution    User defined vertical resolution.\r
+  @param  CurrentModeNumber     Current specific mode to be check.\r
+\r
+  @retval EFI_SUCCESS       The mode is supported.\r
+  @retval EFI_UNSUPPORTED   The specific mode is out of range of graphics\r
+                            device supported.\r
+  @retval other             The specific mode does not support user defined\r
+                            resolution or failed to set the current mode to the\r
+                            specific mode on graphics device.\r
+\r
+**/\r
 EFI_STATUS\r
-EfiLocateHiiProtocol (\r
-  VOID\r
+CheckModeSupported (\r
+  EFI_GRAPHICS_OUTPUT_PROTOCOL  *GraphicsOutput,\r
+  IN  UINT32                    HorizontalResolution,\r
+  IN  UINT32                    VerticalResolution,\r
+  OUT UINT32                    *CurrentModeNumber\r
   )\r
-/*++\r
+{\r
+  UINT32     ModeNumber;\r
+  EFI_STATUS Status;\r
+  UINTN      SizeOfInfo;\r
+  EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;\r
+  UINT32     MaxMode;\r
+\r
+  Status  = EFI_SUCCESS;\r
+  MaxMode = GraphicsOutput->Mode->MaxMode;\r
+\r
+  for (ModeNumber = 0; ModeNumber < MaxMode; ModeNumber++) {\r
+    Status = GraphicsOutput->QueryMode (\r
+                       GraphicsOutput,\r
+                       ModeNumber,\r
+                       &SizeOfInfo,\r
+                       &Info\r
+                       );\r
+    if (!EFI_ERROR (Status)) {\r
+      if ((Info->HorizontalResolution == HorizontalResolution) &&\r
+          (Info->VerticalResolution == VerticalResolution)) {\r
+        if ((GraphicsOutput->Mode->Info->HorizontalResolution == HorizontalResolution) &&\r
+            (GraphicsOutput->Mode->Info->VerticalResolution == VerticalResolution)) {\r
+          //\r
+          // If video device has been set to this mode, we do not need to SetMode again\r
+          //\r
+          FreePool (Info);\r
+          break;\r
+        } else {\r
+          Status = GraphicsOutput->SetMode (GraphicsOutput, ModeNumber);\r
+          if (!EFI_ERROR (Status)) {\r
+            FreePool (Info);\r
+            break;\r
+          }\r
+        }\r
+      }\r
+      FreePool (Info);\r
+    }\r
+  }\r
+\r
+  if (ModeNumber == GraphicsOutput->Mode->MaxMode) {\r
+    Status = EFI_UNSUPPORTED;\r
+  }\r
+\r
+  *CurrentModeNumber = ModeNumber;\r
+  return Status;\r
+}\r
 \r
-  Routine Description:\r
-    Find if the HII protocol is available. If yes, locate the HII protocol\r
 \r
-  Arguments:\r
+/**\r
+  Locate HII Database protocol and HII Font protocol.\r
 \r
-  Returns:\r
+  @retval  EFI_SUCCESS     HII Database protocol and HII Font protocol\r
+                           are located successfully.\r
+  @return  other           Failed to locate HII Database protocol or\r
+                           HII Font protocol.\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+EfiLocateHiiProtocol (\r
+  VOID\r
+  )\r
 {\r
-  EFI_HANDLE  Handle;\r
-  UINTN       Size;\r
   EFI_STATUS  Status;\r
 \r
-  //\r
-  // There should only be one - so buffer size is this\r
-  //\r
-  Size = sizeof (EFI_HANDLE);\r
-\r
-  Status = gBS->LocateHandle (\r
-                  ByProtocol,\r
-                  &gEfiHiiProtocolGuid,\r
-                  NULL,\r
-                  &Size,\r
-                  &Handle\r
-                  );\r
-\r
+  Status = gBS->LocateProtocol (&gEfiHiiDatabaseProtocolGuid, NULL, (VOID **) &mHiiDatabase);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
-  Status = gBS->HandleProtocol (\r
-                  Handle,\r
-                  &gEfiHiiProtocolGuid,\r
-                  (VOID **)&mHii\r
-                  );\r
-\r
+  Status = gBS->LocateProtocol (&gEfiHiiFontProtocolGuid, NULL, (VOID **) &mHiiFont);\r
   return Status;\r
 }\r
+\r
 //\r
 // Body of the STO functions\r
 //\r
+\r
+/**\r
+  Reset the text output device hardware and optionally run diagnostics.\r
+\r
+  Implements SIMPLE_TEXT_OUTPUT.Reset().\r
+  If ExtendeVerification is TRUE, then perform dependent Graphics Console\r
+  device reset, and set display mode to mode 0.\r
+  If ExtendedVerification is FALSE, only set display mode to mode 0.\r
+\r
+  @param  This                  Protocol instance pointer.\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 text output device was reset.\r
+  @retval EFI_DEVICE_ERROR     The text output device is not functioning correctly and\r
+                               could not be reset.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GraphicsConsoleConOutReset (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
   IN  BOOLEAN                          ExtendedVerification\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements SIMPLE_TEXT_OUTPUT.Reset().\r
-    If ExtendeVerification is TRUE, then perform dependent Graphics Console \r
-    device reset, 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 Graphics Console is not functioning correctly \r
-                \r
---*/\r
 {\r
-  This->SetAttribute (This, EFI_TEXT_ATTR (This->Mode->Attribute & 0x0F, EFI_BACKGROUND_BLACK));\r
-  return This->SetMode (This, 0);\r
+  EFI_STATUS    Status;\r
+  Status = This->SetMode (This, 0);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  Status = This->SetAttribute (This, EFI_TEXT_ATTR (This->Mode->Attribute & 0x0F, EFI_BACKGROUND_BLACK));\r
+  return Status;\r
 }\r
 \r
+\r
+/**\r
+  Write a Unicode string to the output device.\r
+\r
+  Implements SIMPLE_TEXT_OUTPUT.OutputString().\r
+  The Unicode string will be converted to Glyphs and will be\r
+  sent to the Graphics Console.\r
+\r
+  @param  This                    Protocol instance pointer.\r
+  @param  WString                 The NULL-terminated Unicode string to be displayed\r
+                                  on the output device(s). All output devices must\r
+                                  also support the Unicode drawing defined in this file.\r
+\r
+  @retval EFI_SUCCESS             The string was output to the device.\r
+  @retval EFI_DEVICE_ERROR        The device reported an error while attempting to output\r
+                                  the text.\r
+  @retval EFI_UNSUPPORTED         The output device's mode is not currently in a\r
+                                  defined text mode.\r
+  @retval EFI_WARN_UNKNOWN_GLYPH  This warning code indicates that some of the\r
+                                  characters in the Unicode string could not be\r
+                                  rendered and were skipped.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GraphicsConsoleConOutOutputString (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
   IN  CHAR16                           *WString\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements SIMPLE_TEXT_OUTPUT.OutputString().\r
-    The Unicode string will be converted to Glyphs and will be \r
-    sent to the Graphics Console.\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 Graphics Console.\r
-        \r
-  Returns:\r
-  \r
-    EFI_SUCCESS\r
-       The string is output successfully.   \r
-    \r
-    EFI_DEVICE_ERROR\r
-      The Graphics Console failed 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
---*/\r
 {\r
   GRAPHICS_CONSOLE_DEV  *Private;\r
   EFI_GRAPHICS_OUTPUT_PROTOCOL   *GraphicsOutput;\r
@@ -723,10 +904,16 @@ GraphicsConsoleConOutOutputString (
   UINTN                 Index;\r
   INT32                 OriginAttribute;\r
   EFI_TPL               OldTpl;\r
-  CHAR16                         SpaceStr[] = { NARROW_CHAR, ' ', 0 };\r
 \r
-  Status = EFI_SUCCESS;\r
+  if (This->Mode->Mode == -1) {\r
+    //\r
+    // If current mode is not valid, return error.\r
+    //\r
+    return EFI_UNSUPPORTED;\r
+  }\r
 \r
+  Status = EFI_SUCCESS;\r
+  \r
   OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
   //\r
   // Current mode\r
@@ -738,10 +925,10 @@ GraphicsConsoleConOutOutputString (
 \r
   MaxColumn = Private->ModeData[Mode].Columns;\r
   MaxRow    = Private->ModeData[Mode].Rows;\r
-  DeltaX    = Private->ModeData[Mode].DeltaX;\r
-  DeltaY    = Private->ModeData[Mode].DeltaY;\r
-  Width     = MaxColumn * GLYPH_WIDTH;\r
-  Height    = (MaxRow - 1) * GLYPH_HEIGHT;\r
+  DeltaX    = (UINTN) Private->ModeData[Mode].DeltaX;\r
+  DeltaY    = (UINTN) Private->ModeData[Mode].DeltaY;\r
+  Width     = MaxColumn * EFI_GLYPH_WIDTH;\r
+  Height    = (MaxRow - 1) * EFI_GLYPH_HEIGHT;\r
   Delta     = Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);\r
 \r
   //\r
@@ -749,7 +936,7 @@ GraphicsConsoleConOutOutputString (
   //\r
   GetTextColors (This, &Foreground, &Background);\r
 \r
-  EraseCursor (This);\r
+  FlushCursor (This);\r
 \r
   Warning = FALSE;\r
 \r
@@ -758,7 +945,7 @@ GraphicsConsoleConOutOutputString (
   //\r
   OriginAttribute = This->Mode->Attribute;\r
 \r
-  while (*WString) {\r
+  while (*WString != L'\0') {\r
 \r
     if (*WString == CHAR_BACKSPACE) {\r
       //\r
@@ -769,7 +956,7 @@ GraphicsConsoleConOutOutputString (
         This->Mode->CursorRow--;\r
         This->Mode->CursorColumn = (INT32) (MaxColumn - 1);\r
         This->OutputString (This, SpaceStr);\r
-        EraseCursor (This);\r
+        FlushCursor (This);\r
         This->Mode->CursorRow--;\r
         This->Mode->CursorColumn = (INT32) (MaxColumn - 1);\r
       } else if (This->Mode->CursorColumn > 0) {\r
@@ -779,7 +966,7 @@ GraphicsConsoleConOutOutputString (
         //\r
         This->Mode->CursorColumn--;\r
         This->OutputString (This, SpaceStr);\r
-        EraseCursor (This);\r
+        FlushCursor (This);\r
         This->Mode->CursorColumn--;\r
       }\r
 \r
@@ -801,7 +988,7 @@ GraphicsConsoleConOutOutputString (
                     NULL,\r
                     EfiBltVideoToVideo,\r
                     DeltaX,\r
-                    DeltaY + GLYPH_HEIGHT,\r
+                    DeltaY + EFI_GLYPH_HEIGHT,\r
                     DeltaX,\r
                     DeltaY,\r
                     Width,\r
@@ -821,10 +1008,10 @@ GraphicsConsoleConOutOutputString (
                     DeltaX,\r
                     DeltaY + Height,\r
                     Width,\r
-                    GLYPH_HEIGHT,\r
+                    EFI_GLYPH_HEIGHT,\r
                     Delta\r
                     );\r
-        } else {\r
+        } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {\r
           //\r
           // Scroll Screen Up One Row\r
           //\r
@@ -833,7 +1020,7 @@ GraphicsConsoleConOutOutputString (
                     NULL,\r
                     EfiUgaVideoToVideo,\r
                     DeltaX,\r
-                    DeltaY + GLYPH_HEIGHT,\r
+                    DeltaY + EFI_GLYPH_HEIGHT,\r
                     DeltaX,\r
                     DeltaY,\r
                     Width,\r
@@ -853,7 +1040,7 @@ GraphicsConsoleConOutOutputString (
                     DeltaX,\r
                     DeltaY + Height,\r
                     Width,\r
-                    GLYPH_HEIGHT,\r
+                    EFI_GLYPH_HEIGHT,\r
                     Delta\r
                     );\r
         }\r
@@ -895,33 +1082,18 @@ GraphicsConsoleConOutOutputString (
       // Count is used to determine how many characters are used regardless of their attributes\r
       //\r
       for (Count = 0, Index = 0; (This->Mode->CursorColumn + Index) < MaxColumn; Count++, Index++) {\r
-        if (WString[Count] == CHAR_NULL) {\r
-          break;\r
-        }\r
-\r
-        if (WString[Count] == CHAR_BACKSPACE) {\r
-          break;\r
-        }\r
-\r
-        if (WString[Count] == CHAR_LINEFEED) {\r
-          break;\r
-        }\r
-\r
-        if (WString[Count] == CHAR_CARRIAGE_RETURN) {\r
-          break;\r
-        }\r
-\r
-        if (WString[Count] == WIDE_CHAR) {\r
-          break;\r
-        }\r
-\r
-        if (WString[Count] == NARROW_CHAR) {\r
+        if (WString[Count] == CHAR_NULL ||\r
+            WString[Count] == CHAR_BACKSPACE ||\r
+            WString[Count] == CHAR_LINEFEED ||\r
+            WString[Count] == CHAR_CARRIAGE_RETURN ||\r
+            WString[Count] == WIDE_CHAR ||\r
+            WString[Count] == NARROW_CHAR) {\r
           break;\r
         }\r
         //\r
         // Is the wide attribute on?\r
         //\r
-        if (This->Mode->Attribute & EFI_WIDE_ATTRIBUTE) {\r
+        if ((This->Mode->Attribute & EFI_WIDE_ATTRIBUTE) != 0) {\r
           //\r
           // If wide, add one more width unit than normal since we are going to increment at the end of the for loop\r
           //\r
@@ -953,16 +1125,16 @@ GraphicsConsoleConOutOutputString (
       }\r
 \r
       if (This->Mode->CursorColumn >= (INT32) MaxColumn) {\r
-        EraseCursor (This);\r
+        FlushCursor (This);\r
         This->OutputString (This, mCrLfString);\r
-        EraseCursor (This);\r
+        FlushCursor (This);\r
       }\r
     }\r
   }\r
 \r
   This->Mode->Attribute = OriginAttribute;\r
 \r
-  EraseCursor (This);\r
+  FlushCursor (This);\r
 \r
   if (Warning) {\r
     Status = EFI_WARN_UNKNOWN_GLYPH;\r
@@ -973,56 +1145,53 @@ GraphicsConsoleConOutOutputString (
 \r
 }\r
 \r
+/**\r
+  Verifies that all characters in a Unicode string can be output to the\r
+  target device.\r
+\r
+  Implements SIMPLE_TEXT_OUTPUT.TestString().\r
+  If one of the characters in the *Wstring is neither valid valid Unicode\r
+  drawing characters, not ASCII code, then this function will return\r
+  EFI_UNSUPPORTED\r
+\r
+  @param  This    Protocol instance pointer.\r
+  @param  WString The NULL-terminated Unicode string to be examined for the output\r
+                  device(s).\r
+\r
+  @retval EFI_SUCCESS      The device(s) are capable of rendering the output string.\r
+  @retval EFI_UNSUPPORTED  Some of the characters in the Unicode string cannot be\r
+                           rendered by one or more of the output devices mapped\r
+                           by the EFI handle.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GraphicsConsoleConOutTestString (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
   IN  CHAR16                           *WString\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements SIMPLE_TEXT_OUTPUT.TestString().\r
-    If one of the characters in the *Wstring is\r
-    neither valid 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 Graphics Console 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
   EFI_STATUS            Status;\r
-  UINT16                GlyphWidth;\r
-  UINT32                GlyphStatus;\r
   UINT16                Count;\r
-  GLYPH_UNION           *Glyph;\r
-\r
-  GlyphStatus = 0;\r
-  Count       = 0;\r
-\r
-  while (WString[Count]) {\r
-    Status = mHii->GetGlyph (\r
-                    mHii,\r
-                    WString,\r
-                    &Count,\r
-                    (UINT8 **) &Glyph,\r
-                    &GlyphWidth,\r
-                    &GlyphStatus\r
-                    );\r
+\r
+  EFI_IMAGE_OUTPUT      *Blt;\r
+\r
+  Blt   = NULL;\r
+  Count = 0;\r
+\r
+  while (WString[Count] != 0) {\r
+    Status = mHiiFont->GetGlyph (\r
+                         mHiiFont,\r
+                         WString[Count],\r
+                         NULL,\r
+                         &Blt,\r
+                         NULL\r
+                         );\r
+    if (Blt != NULL) {\r
+      FreePool (Blt);\r
+      Blt = NULL;\r
+    }\r
+    Count++;\r
 \r
     if (EFI_ERROR (Status)) {\r
       return EFI_UNSUPPORTED;\r
@@ -1032,6 +1201,24 @@ GraphicsConsoleConOutTestString (
   return EFI_SUCCESS;\r
 }\r
 \r
+\r
+/**\r
+  Returns information for an available text mode that the output device(s)\r
+  supports\r
+\r
+  Implements SIMPLE_TEXT_OUTPUT.QueryMode().\r
+  It returnes information for an available text mode that the Graphics Console supports.\r
+  In this driver,we only support text mode 80x25, which is defined as mode 0.\r
+\r
+  @param  This                  Protocol instance pointer.\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
 GraphicsConsoleConOutQueryMode (\r
@@ -1040,35 +1227,6 @@ GraphicsConsoleConOutQueryMode (
   OUT UINTN                            *Columns,\r
   OUT UINTN                            *Rows\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements SIMPLE_TEXT_OUTPUT.QueryMode().\r
-    It returnes information for an available text mode\r
-    that the Graphics Console 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 - Indicates the calling context.\r
-    \r
-    ModeNumber - The mode number to return information on.\r
-        \r
-    Columns - The returned columns of the requested mode.\r
-        \r
-    Rows - 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
---*/\r
 {\r
   GRAPHICS_CONSOLE_DEV  *Private;\r
   EFI_STATUS            Status;\r
@@ -1080,13 +1238,13 @@ GraphicsConsoleConOutQueryMode (
 \r
   OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
   Status = EFI_SUCCESS;\r
-  \r
+\r
   Private   = GRAPHICS_CONSOLE_CON_OUT_DEV_FROM_THIS (This);\r
 \r
   *Columns  = Private->ModeData[ModeNumber].Columns;\r
   *Rows     = Private->ModeData[ModeNumber].Rows;\r
 \r
-  if (*Columns <= 0 && *Rows <= 0) {\r
+  if (*Columns <= 0 || *Rows <= 0) {\r
     Status = EFI_UNSUPPORTED;\r
     goto Done;\r
 \r
@@ -1097,37 +1255,28 @@ Done:
   return Status;\r
 }\r
 \r
+\r
+/**\r
+  Sets the output device(s) to a specified mode.\r
+\r
+  Implements SIMPLE_TEXT_OUTPUT.SetMode().\r
+  Set the Graphics Console to a specified mode. In this driver, we only support mode 0.\r
+\r
+  @param  This                  Protocol instance pointer.\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 because of\r
+                                Graphics Console device error.\r
+  @retval EFI_UNSUPPORTED       The text mode number is not valid.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GraphicsConsoleConOutSetMode (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
   IN  UINTN                            ModeNumber\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements SIMPLE_TEXT_OUTPUT.SetMode().\r
-    Set the Graphics Console to a specified mode.\r
-    In this driver, we only support mode 0.        \r
-  \r
-  Arguments:\r
-  \r
-    This - Indicates the calling context.\r
-    \r
-    ModeNumber - 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 Graphics Console device error.\r
-    \r
-    EFI_UNSUPPORTED\r
-      The text mode number is not valid.       \r
-                \r
---*/\r
 {\r
   EFI_STATUS                      Status;\r
   GRAPHICS_CONSOLE_DEV            *Private;\r
@@ -1146,12 +1295,6 @@ GraphicsConsoleConOutSetMode (
   Private   = GRAPHICS_CONSOLE_CON_OUT_DEV_FROM_THIS (This);\r
   GraphicsOutput = Private->GraphicsOutput;\r
   UgaDraw   = Private->UgaDraw;\r
-  ModeData  = &(Private->ModeData[ModeNumber]);\r
-\r
-  if (ModeData->Columns <= 0 && ModeData->Rows <= 0) {\r
-    Status = EFI_UNSUPPORTED;\r
-    goto Done;\r
-  }\r
 \r
   //\r
   // Make sure the requested mode number is supported\r
@@ -1160,49 +1303,52 @@ GraphicsConsoleConOutSetMode (
     Status = EFI_UNSUPPORTED;\r
     goto Done;\r
   }\r
+  \r
+  ModeData  = &(Private->ModeData[ModeNumber]);\r
 \r
   if (ModeData->Columns <= 0 && ModeData->Rows <= 0) {\r
     Status = EFI_UNSUPPORTED;\r
     goto Done;\r
   }\r
-  //\r
-  // Attempt to allocate a line buffer for the requested mode number\r
-  //\r
-  NewLineBuffer = AllocatePool (sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) * ModeData->Columns * GLYPH_WIDTH * GLYPH_HEIGHT);\r
 \r
-  if (NewLineBuffer == NULL) {\r
-    //\r
-    // The new line buffer could not be allocated, so return an error.\r
-    // No changes to the state of the current console have been made, so the current console is still valid\r
-    //\r
-    Status = EFI_OUT_OF_RESOURCES;\r
-    goto Done;\r
-  }\r
   //\r
   // If the mode has been set at least one other time, then LineBuffer will not be NULL\r
   //\r
   if (Private->LineBuffer != NULL) {\r
-    //\r
-    // Clear the current text window on the current graphics console\r
-    //\r
-    This->ClearScreen (This);\r
-\r
     //\r
     // If the new mode is the same as the old mode, then just return EFI_SUCCESS\r
     //\r
     if ((INT32) ModeNumber == This->Mode->Mode) {\r
-      FreePool (NewLineBuffer);\r
+      //\r
+      // Clear the current text window on the current graphics console\r
+      //\r
+      This->ClearScreen (This);\r
       Status = EFI_SUCCESS;\r
       goto Done;\r
     }\r
     //\r
-    // Otherwise, the size of the text console and/or the UGA mode will be changed,\r
-    // so turn off the cursor, and free the LineBuffer for the current mode\r
+    // Otherwise, the size of the text console and/or the GOP/UGA mode will be changed,\r
+    // so erase the cursor, and free the LineBuffer for the current mode\r
     //\r
-    This->EnableCursor (This, FALSE);\r
+    FlushCursor (This);\r
 \r
     FreePool (Private->LineBuffer);\r
   }\r
+\r
+  //\r
+  // Attempt to allocate a line buffer for the requested mode number\r
+  //\r
+  NewLineBuffer = AllocatePool (sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) * ModeData->Columns * EFI_GLYPH_WIDTH * EFI_GLYPH_HEIGHT);\r
+\r
+  if (NewLineBuffer == NULL) {\r
+    //\r
+    // The new line buffer could not be allocated, so return an error.\r
+    // No changes to the state of the current console have been made, so the current console is still valid\r
+    //\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Done;\r
+  }\r
+\r
   //\r
   // Assign the current line buffer to the newly allocated line buffer\r
   //\r
@@ -1211,7 +1357,7 @@ GraphicsConsoleConOutSetMode (
   if (GraphicsOutput != NULL) {\r
     if (ModeData->GopModeNumber != GraphicsOutput->Mode->Mode) {\r
       //\r
-      // Either no graphics mode is currently set, or it is set to the wrong resolution, so set the new grapghics mode\r
+      // Either no graphics mode is currently set, or it is set to the wrong resolution, so set the new graphics mode\r
       //\r
       Status = GraphicsOutput->SetMode (GraphicsOutput, ModeData->GopModeNumber);\r
       if (EFI_ERROR (Status)) {\r
@@ -1226,7 +1372,7 @@ GraphicsConsoleConOutSetMode (
       //\r
       Status = GraphicsOutput->Blt (\r
                           GraphicsOutput,\r
-                          &mEfiColors[0],\r
+                          &mGraphicsEfiColors[0],\r
                           EfiBltVideoFill,\r
                           0,\r
                           0,\r
@@ -1237,7 +1383,7 @@ GraphicsConsoleConOutSetMode (
                           0\r
                           );\r
     }\r
-  } else {\r
+  } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {\r
     //\r
     // Get the current UGA Draw mode information\r
     //\r
@@ -1250,7 +1396,7 @@ GraphicsConsoleConOutSetMode (
                         );\r
     if (EFI_ERROR (Status) || HorizontalResolution != ModeData->GopWidth || VerticalResolution != ModeData->GopHeight) {\r
       //\r
-      // Either no graphics mode is currently set, or it is set to the wrong resolution, so set the new grapghics mode\r
+      // Either no graphics mode is currently set, or it is set to the wrong resolution, so set the new graphics mode\r
       //\r
       Status = UgaDraw->SetMode (\r
                           UgaDraw,\r
@@ -1271,7 +1417,7 @@ GraphicsConsoleConOutSetMode (
       //\r
       Status = UgaDraw->Blt (\r
                           UgaDraw,\r
-                          (EFI_UGA_PIXEL *) (UINTN) &mEfiColors[0],\r
+                          (EFI_UGA_PIXEL *) (UINTN) &mGraphicsEfiColors[0],\r
                           EfiUgaVideoFill,\r
                           0,\r
                           0,\r
@@ -1290,10 +1436,12 @@ GraphicsConsoleConOutSetMode (
   This->Mode->Mode = (INT32) ModeNumber;\r
 \r
   //\r
-  // Move the text cursor to the upper left hand corner of the displat and enable it\r
+  // Move the text cursor to the upper left hand corner of the display and flush it\r
   //\r
-  This->SetCursorPosition (This, 0, 0);\r
-  This->EnableCursor (This, TRUE);\r
+  This->Mode->CursorColumn  = 0;\r
+  This->Mode->CursorRow     = 0;\r
+\r
+  FlushCursor (This);  \r
 \r
   Status = EFI_SUCCESS;\r
 \r
@@ -1302,40 +1450,40 @@ Done:
   return Status;\r
 }\r
 \r
+\r
+/**\r
+  Sets the background and foreground colors for the OutputString () and\r
+  ClearScreen () functions.\r
+\r
+  Implements SIMPLE_TEXT_OUTPUT.SetAttribute().\r
+\r
+  @param  This                  Protocol instance pointer.\r
+  @param  Attribute             The attribute to set. Bits 0..3 are the foreground\r
+                                color, and bits 4..6 are the background color.\r
+                                All other bits 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 Graphics Console port error.\r
+  @retval EFI_UNSUPPORTED       The attribute requested is not defined.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GraphicsConsoleConOutSetAttribute (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
   IN  UINTN                            Attribute\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements SIMPLE_TEXT_OUTPUT.SetAttribute().       \r
-  \r
-  Arguments:\r
-  \r
-    This - Indicates the calling context.\r
-    \r
-    Attrubute - 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 Graphics Console port error.\r
-          \r
-    EFI_UNSUPPORTED\r
-      The attribute requested is not defined by EFI spec.   \r
-                \r
---*/\r
 {\r
   EFI_TPL               OldTpl;\r
-  \r
-  if ((Attribute | 0xFF) != 0xFF) {\r
+\r
+  if ((Attribute | 0x7F) != 0x7F) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  if (This->Mode->Mode == -1) {\r
+    //\r
+    // If current mode is not valid, return error.\r
+    //\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
@@ -1345,46 +1493,36 @@ GraphicsConsoleConOutSetAttribute (
 \r
   OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
 \r
-  EraseCursor (This);\r
+  FlushCursor (This);\r
 \r
   This->Mode->Attribute = (INT32) Attribute;\r
 \r
-  EraseCursor (This);\r
+  FlushCursor (This);\r
 \r
   gBS->RestoreTPL (OldTpl);\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
+\r
+/**\r
+  Clears the output device(s) display to the currently selected background\r
+  color.\r
+\r
+  Implements SIMPLE_TEXT_OUTPUT.ClearScreen().\r
+\r
+  @param  This                  Protocol instance pointer.\r
+\r
+  @retval  EFI_SUCCESS      The operation completed successfully.\r
+  @retval  EFI_DEVICE_ERROR The device had an error and could not complete the request.\r
+  @retval  EFI_UNSUPPORTED  The output device is not in a valid text mode.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GraphicsConsoleConOutClearScreen (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements SIMPLE_TEXT_OUTPUT.ClearScreen().\r
-    It clears the Graphics Console's display to the \r
-    currently selected background color.\r
-        \r
-  \r
-  Arguments:\r
-  \r
-    This - Indicates the calling context.\r
-\r
-  Returns:\r
-  \r
-    EFI_SUCCESS\r
-      The operation completed successfully.\r
-       \r
-    EFI_DEVICE_ERROR\r
-      The Graphics Console cannot be cleared due to Graphics Console device error.        \r
-    \r
-    EFI_UNSUPPORTED\r
-      The Graphics Console is not in a valid text mode.       \r
-                \r
---*/\r
 {\r
   EFI_STATUS                    Status;\r
   GRAPHICS_CONSOLE_DEV          *Private;\r
@@ -1394,6 +1532,13 @@ GraphicsConsoleConOutClearScreen (
   EFI_GRAPHICS_OUTPUT_BLT_PIXEL Foreground;\r
   EFI_GRAPHICS_OUTPUT_BLT_PIXEL Background;\r
   EFI_TPL                       OldTpl;\r
+  \r
+  if (This->Mode->Mode == -1) {\r
+    //\r
+    // If current mode is not valid, return error.\r
+    //\r
+    return EFI_UNSUPPORTED;\r
+  }\r
 \r
   OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
 \r
@@ -1416,7 +1561,7 @@ GraphicsConsoleConOutClearScreen (
                         ModeData->GopHeight,\r
                         0\r
                         );\r
-  } else {\r
+  } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {\r
     Status = UgaDraw->Blt (\r
                         UgaDraw,\r
                         (EFI_UGA_PIXEL *) (UINTN) &Background,\r
@@ -1429,18 +1574,40 @@ GraphicsConsoleConOutClearScreen (
                         ModeData->GopHeight,\r
                         0\r
                         );\r
+  } else {\r
+    Status = EFI_UNSUPPORTED;\r
   }\r
 \r
   This->Mode->CursorColumn  = 0;\r
   This->Mode->CursorRow     = 0;\r
 \r
-  EraseCursor (This);\r
+  FlushCursor (This);\r
 \r
   gBS->RestoreTPL (OldTpl);\r
 \r
   return Status;\r
 }\r
 \r
+\r
+/**\r
+  Sets the current coordinates of the cursor position.\r
+\r
+  Implements SIMPLE_TEXT_OUTPUT.SetCursorPosition().\r
+\r
+  @param  This        Protocol instance pointer.\r
+  @param  Column      The position to set the cursor to. Must be greater than or\r
+                      equal to zero and less than the number of columns and rows\r
+                      by QueryMode ().\r
+  @param  Row         The position to set the cursor to. Must be greater than or\r
+                      equal to zero and less than the number of columns and rows\r
+                      by QueryMode ().\r
+\r
+  @retval EFI_SUCCESS      The operation completed successfully.\r
+  @retval EFI_DEVICE_ERROR The device had an error and could not complete the request.\r
+  @retval EFI_UNSUPPORTED  The output device is not in a valid text mode, or the\r
+                           cursor position is invalid for the current mode.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GraphicsConsoleConOutSetCursorPosition (\r
@@ -1448,38 +1615,19 @@ GraphicsConsoleConOutSetCursorPosition (
   IN  UINTN                            Column,\r
   IN  UINTN                            Row\r
   )\r
-/*++\r
-  Routine Description:\r
-  \r
-    Implements SIMPLE_TEXT_OUTPUT.SetCursorPosition().          \r
-  \r
-  Arguments:\r
-  \r
-    This - Indicates the calling context.\r
-        \r
-    Column - The row to set cursor to.\r
-        \r
-    Row - 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 Graphics Console device error.        \r
-    \r
-    EFI_UNSUPPORTED\r
-      The Graphics Console is not in a valid text mode, or the cursor position\r
-      is invalid for current mode.     \r
-                \r
---*/\r
 {\r
   GRAPHICS_CONSOLE_DEV        *Private;\r
   GRAPHICS_CONSOLE_MODE_DATA  *ModeData;\r
   EFI_STATUS                  Status;\r
   EFI_TPL                     OldTpl;\r
 \r
+  if (This->Mode->Mode == -1) {\r
+    //\r
+    // If current mode is not valid, return error.\r
+    //\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   Status = EFI_SUCCESS;\r
 \r
   OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
@@ -1492,17 +1640,17 @@ GraphicsConsoleConOutSetCursorPosition (
     goto Done;\r
   }\r
 \r
-  if (((INT32) Column == This->Mode->CursorColumn) && ((INT32) Row == This->Mode->CursorRow)) {\r
+  if ((This->Mode->CursorColumn == (INT32) Column) && (This->Mode->CursorRow == (INT32) Row)) {\r
     Status = EFI_SUCCESS;\r
     goto Done;\r
   }\r
 \r
-  EraseCursor (This);\r
+  FlushCursor (This);\r
 \r
   This->Mode->CursorColumn  = (INT32) Column;\r
   This->Mode->CursorRow     = (INT32) Row;\r
 \r
-  EraseCursor (This);\r
+  FlushCursor (This);\r
 \r
 Done:\r
   gBS->RestoreTPL (OldTpl);\r
@@ -1510,50 +1658,59 @@ Done:
   return Status;\r
 }\r
 \r
+\r
+/**\r
+  Makes the cursor visible or invisible.\r
+\r
+  Implements SIMPLE_TEXT_OUTPUT.EnableCursor().\r
+\r
+  @param  This                  Protocol instance pointer.\r
+  @param  Visible               If TRUE, the cursor is set to be visible, If FALSE,\r
+                                the cursor is set to be invisible.\r
+\r
+  @retval EFI_SUCCESS           The operation completed successfully.\r
+  @retval EFI_UNSUPPORTED       The output device's mode is not currently in a\r
+                                defined text mode.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GraphicsConsoleConOutEnableCursor (\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 - Indicates the calling context.\r
-        \r
-    Visible - 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 Graphics Console does not support a hidden cursor.   \r
-                \r
---*/\r
 {\r
   EFI_TPL               OldTpl;\r
-  \r
+\r
+  if (This->Mode->Mode == -1) {\r
+    //\r
+    // If current mode is not valid, return error.\r
+    //\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
-    \r
-  EraseCursor (This);\r
+\r
+  FlushCursor (This);\r
 \r
   This->Mode->CursorVisible = Visible;\r
 \r
-  EraseCursor (This);\r
+  FlushCursor (This);\r
 \r
   gBS->RestoreTPL (OldTpl);\r
   return EFI_SUCCESS;\r
 }\r
 \r
-STATIC\r
+/**\r
+  Gets Graphics Console devcie's foreground color and background color.\r
+\r
+  @param  This                  Protocol instance pointer.\r
+  @param  Foreground            Returned text foreground color.\r
+  @param  Background            Returned text background color.\r
+\r
+  @retval EFI_SUCCESS           It returned always.\r
+\r
+**/\r
 EFI_STATUS\r
 GetTextColors (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
@@ -1565,13 +1722,25 @@ GetTextColors (
 \r
   Attribute   = This->Mode->Attribute & 0x7F;\r
 \r
-  *Foreground = mEfiColors[Attribute & 0x0f];\r
-  *Background = mEfiColors[Attribute >> 4];\r
+  *Foreground = mGraphicsEfiColors[Attribute & 0x0f];\r
+  *Background = mGraphicsEfiColors[Attribute >> 4];\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-STATIC\r
+/**\r
+  Draw Unicode string on the Graphics Console device's screen.\r
+\r
+  @param  This                  Protocol instance pointer.\r
+  @param  UnicodeWeight         One Unicode string to be displayed.\r
+  @param  Count                 The count of Unicode string.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES  If no memory resource to use.\r
+  @retval EFI_UNSUPPORTED       If no Graphics Output protocol and UGA Draw\r
+                                protocol exist.\r
+  @retval EFI_SUCCESS           Drawing Unicode string implemented successfully.\r
+\r
+**/\r
 EFI_STATUS\r
 DrawUnicodeWeightAtCursorN (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,\r
@@ -1579,156 +1748,166 @@ DrawUnicodeWeightAtCursorN (
   IN  UINTN                            Count\r
   )\r
 {\r
-  GRAPHICS_CONSOLE_DEV  *Private;\r
-  EFI_STATUS            Status;\r
-  EFI_STATUS            ReturnStatus;\r
-  GLYPH_UNION           *Glyph;\r
-  GLYPH_UNION           GlyphData;\r
-  INTN                  GlyphX;\r
-  INTN                  GlyphY;\r
-  EFI_GRAPHICS_OUTPUT_PROTOCOL  *GraphicsOutput;\r
-  EFI_UGA_DRAW_PROTOCOL *UgaDraw;\r
-  EFI_GRAPHICS_OUTPUT_BLT_PIXEL Foreground;\r
-  EFI_GRAPHICS_OUTPUT_BLT_PIXEL Background;\r
-  UINTN                 Index;\r
-  UINTN                 ArrayIndex;\r
-  UINTN                 Counts;\r
-  UINT16                GlyphWidth;\r
-  UINT32                GlyphStatus;\r
+  EFI_STATUS                        Status;\r
+  GRAPHICS_CONSOLE_DEV              *Private;\r
+  EFI_IMAGE_OUTPUT                  *Blt;\r
+  EFI_STRING                        String;\r
+  EFI_FONT_DISPLAY_INFO             *FontInfo;\r
+  EFI_UGA_DRAW_PROTOCOL             *UgaDraw;\r
+  EFI_HII_ROW_INFO                  *RowInfoArray;\r
+  UINTN                             RowInfoArraySize;\r
+\r
+  Private = GRAPHICS_CONSOLE_CON_OUT_DEV_FROM_THIS (This);\r
+  Blt = (EFI_IMAGE_OUTPUT *) AllocateZeroPool (sizeof (EFI_IMAGE_OUTPUT));\r
+  if (Blt == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
 \r
-  Private       = GRAPHICS_CONSOLE_CON_OUT_DEV_FROM_THIS (This);\r
+  Blt->Width        = (UINT16) (Private->ModeData[This->Mode->Mode].GopWidth);\r
+  Blt->Height       = (UINT16) (Private->ModeData[This->Mode->Mode].GopHeight);\r
 \r
-  ReturnStatus  = EFI_SUCCESS;\r
-  GlyphStatus   = 0;\r
-  GlyphWidth    = 0x08;\r
+  String = AllocateCopyPool ((Count + 1) * sizeof (CHAR16), UnicodeWeight);\r
+  if (String == NULL) {\r
+    FreePool (Blt);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  // Set the end character\r
+  //\r
+  *(String + Count) = L'\0';\r
 \r
-  GetTextColors (This, &Foreground, &Background);\r
+  FontInfo = (EFI_FONT_DISPLAY_INFO *) AllocateZeroPool (sizeof (EFI_FONT_DISPLAY_INFO));\r
+  if (FontInfo == NULL) {\r
+    FreePool (Blt);\r
+    FreePool (String);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  // Get current foreground and background colors.\r
+  //\r
+  GetTextColors (This, &FontInfo->ForegroundColor, &FontInfo->BackgroundColor);\r
 \r
-  Index       = 0;\r
-  ArrayIndex  = 0;\r
-  while (Index < Count) {\r
-    if (This->Mode->Attribute & EFI_WIDE_ATTRIBUTE) {\r
-      GlyphStatus = WIDE_CHAR;\r
-    } else {\r
-      GlyphStatus = NARROW_CHAR;\r
-    }\r
+  if (Private->GraphicsOutput != NULL) {\r
+    //\r
+    // If Graphics Output protocol exists, using HII Font protocol to draw.\r
+    //\r
+    Blt->Image.Screen = Private->GraphicsOutput;\r
+\r
+    Status = mHiiFont->StringToImage (\r
+                         mHiiFont,\r
+                         EFI_HII_IGNORE_IF_NO_GLYPH | EFI_HII_DIRECT_TO_SCREEN | EFI_HII_IGNORE_LINE_BREAK,\r
+                         String,\r
+                         FontInfo,\r
+                         &Blt,\r
+                         This->Mode->CursorColumn * EFI_GLYPH_WIDTH + Private->ModeData[This->Mode->Mode].DeltaX,\r
+                         This->Mode->CursorRow * EFI_GLYPH_HEIGHT + Private->ModeData[This->Mode->Mode].DeltaY,\r
+                         NULL,\r
+                         NULL,\r
+                         NULL\r
+                         );\r
 \r
-    Status = mHii->GetGlyph (\r
-                    mHii,\r
-                    UnicodeWeight,\r
-                    (UINT16 *) &Index,\r
-                    (UINT8 **) &Glyph,\r
-                    &GlyphWidth,\r
-                    &GlyphStatus\r
-                    );\r
-    if (EFI_ERROR (Status)) {\r
-      ReturnStatus = Status;\r
-    }\r
+  } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {\r
+    //\r
+    // If Graphics Output protocol cannot be found and PcdUgaConsumeSupport enabled,\r
+    // using UGA Draw protocol to draw.\r
+    //\r
+    ASSERT (Private->UgaDraw!= NULL);\r
 \r
-    Counts = 0;\r
+    UgaDraw = Private->UgaDraw;\r
+\r
+    Blt->Image.Bitmap = AllocateZeroPool (Blt->Width * Blt->Height * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));\r
+    if (Blt->Image.Bitmap == NULL) {\r
+      FreePool (Blt);\r
+      FreePool (String);\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
 \r
-    CopyMem (&GlyphData, Glyph, sizeof (GLYPH_UNION));\r
+    RowInfoArray = NULL;\r
+    //\r
+    //  StringToImage only support blt'ing image to device using GOP protocol. If GOP is not supported in this platform,\r
+    //  we ask StringToImage to print the string to blt buffer, then blt to device using UgaDraw.\r
+    //\r
+    Status = mHiiFont->StringToImage (\r
+                          mHiiFont,\r
+                          EFI_HII_IGNORE_IF_NO_GLYPH | EFI_HII_IGNORE_LINE_BREAK,\r
+                          String,\r
+                          FontInfo,\r
+                          &Blt,\r
+                          This->Mode->CursorColumn * EFI_GLYPH_WIDTH + Private->ModeData[This->Mode->Mode].DeltaX,\r
+                          This->Mode->CursorRow * EFI_GLYPH_HEIGHT + Private->ModeData[This->Mode->Mode].DeltaY,\r
+                          &RowInfoArray,\r
+                          &RowInfoArraySize,\r
+                          NULL\r
+                          );\r
 \r
-    do {\r
+    if (!EFI_ERROR (Status)) {\r
       //\r
-      // We are creating the second half of the wide character's BLT buffer\r
+      // Line breaks are handled by caller of DrawUnicodeWeightAtCursorN, so the updated parameter RowInfoArraySize by StringToImage will\r
+      // always be 1 or 0 (if there is no valid Unicode Char can be printed). ASSERT here to make sure.\r
       //\r
-      if (GlyphWidth == 0x10 && Counts == 1) {\r
-        CopyMem (&GlyphData.NarrowGlyph.GlyphCol1, &Glyph->WideGlyph.GlyphCol2, sizeof (Glyph->WideGlyph.GlyphCol2));\r
-      }\r
+      ASSERT (RowInfoArraySize <= 1);\r
 \r
-      Counts++;\r
-\r
-      if (GlyphWidth == 0x10) {\r
-        mHii->GlyphToBlt (\r
-                mHii,\r
-                (UINT8 *) &GlyphData,\r
-                Foreground,\r
-                Background,\r
-                Count * 2,\r
-                GLYPH_WIDTH,\r
-                GLYPH_HEIGHT,\r
-                &Private->LineBuffer[ArrayIndex * GLYPH_WIDTH]\r
-                );\r
-      } else {\r
-        mHii->GlyphToBlt (\r
-                mHii,\r
-                (UINT8 *) &GlyphData,\r
-                Foreground,\r
-                Background,\r
-                Count,\r
-                GLYPH_WIDTH,\r
-                GLYPH_HEIGHT,\r
-                &Private->LineBuffer[ArrayIndex * GLYPH_WIDTH]\r
-                );\r
-      }\r
-\r
-      ArrayIndex++;\r
+      Status = UgaDraw->Blt (\r
+                          UgaDraw,\r
+                          (EFI_UGA_PIXEL *) Blt->Image.Bitmap,\r
+                          EfiUgaBltBufferToVideo,\r
+                          This->Mode->CursorColumn * EFI_GLYPH_WIDTH  + Private->ModeData[This->Mode->Mode].DeltaX,\r
+                          (This->Mode->CursorRow) * EFI_GLYPH_HEIGHT + Private->ModeData[This->Mode->Mode].DeltaY,\r
+                          This->Mode->CursorColumn * EFI_GLYPH_WIDTH  + Private->ModeData[This->Mode->Mode].DeltaX,\r
+                          (This->Mode->CursorRow) * EFI_GLYPH_HEIGHT + Private->ModeData[This->Mode->Mode].DeltaY,\r
+                          RowInfoArray[0].LineWidth,\r
+                          RowInfoArray[0].LineHeight,\r
+                          Blt->Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)\r
+                          );\r
+    }\r
 \r
-    } while (Counts < 2 && GlyphWidth == 0x10);\r
+    FreePool (RowInfoArray);\r
+    FreePool (Blt->Image.Bitmap);\r
+  } else {\r
+    Status = EFI_UNSUPPORTED;\r
+  }\r
 \r
+  if (Blt != NULL) {\r
+    FreePool (Blt);\r
   }\r
-  //\r
-  // If we are printing Wide characters, treat the BLT as if it is twice as many characters\r
-  //\r
-  if (GlyphWidth == 0x10) {\r
-    Count = Count * 2;\r
+  if (String != NULL) {\r
+    FreePool (String);\r
   }\r
-  //\r
-  // Blt a character to the screen\r
-  //\r
-  GlyphX  = This->Mode->CursorColumn * GLYPH_WIDTH;\r
-  GlyphY  = This->Mode->CursorRow * GLYPH_HEIGHT;\r
-  GraphicsOutput = Private->GraphicsOutput;\r
-  UgaDraw = Private->UgaDraw;\r
-  if (GraphicsOutput != NULL) {\r
-    GraphicsOutput->Blt (\r
-              GraphicsOutput,\r
-              Private->LineBuffer,\r
-              EfiBltBufferToVideo,\r
-              0,\r
-              0,\r
-              GlyphX + Private->ModeData[This->Mode->Mode].DeltaX,\r
-              GlyphY + Private->ModeData[This->Mode->Mode].DeltaY,\r
-              GLYPH_WIDTH * Count,\r
-              GLYPH_HEIGHT,\r
-              GLYPH_WIDTH * Count * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)\r
-              );\r
-  } else {\r
-    UgaDraw->Blt (\r
-              UgaDraw,\r
-              (EFI_UGA_PIXEL *) (UINTN) Private->LineBuffer,\r
-              EfiUgaBltBufferToVideo,\r
-              0,\r
-              0,\r
-              GlyphX + Private->ModeData[This->Mode->Mode].DeltaX,\r
-              GlyphY + Private->ModeData[This->Mode->Mode].DeltaY,\r
-              GLYPH_WIDTH * Count,\r
-              GLYPH_HEIGHT,\r
-              GLYPH_WIDTH * Count * sizeof (EFI_UGA_PIXEL)\r
-              );\r
+  if (FontInfo != NULL) {\r
+    FreePool (FontInfo);\r
   }\r
-\r
-  return ReturnStatus;\r
+  return Status;\r
 }\r
 \r
-STATIC\r
+/**\r
+  Flush the cursor on the screen.\r
+  \r
+  If CursorVisible is FALSE, nothing to do and return directly.\r
+  If CursorVisible is TRUE, \r
+     i) If the cursor shows on screen, it will be erased.\r
+    ii) If the cursor does not show on screen, it will be shown. \r
+\r
+  @param  This                  Protocol instance pointer.\r
+\r
+  @retval EFI_SUCCESS           The cursor is erased successfully.\r
+\r
+**/\r
 EFI_STATUS\r
-EraseCursor (\r
+FlushCursor (\r
   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This\r
   )\r
 {\r
-  GRAPHICS_CONSOLE_DEV        *Private;\r
-  EFI_SIMPLE_TEXT_OUTPUT_MODE *CurrentMode;\r
-  INTN                        GlyphX;\r
-  INTN                        GlyphY;\r
+  GRAPHICS_CONSOLE_DEV                *Private;\r
+  EFI_SIMPLE_TEXT_OUTPUT_MODE         *CurrentMode;\r
+  INTN                                GlyphX;\r
+  INTN                                GlyphY;\r
   EFI_GRAPHICS_OUTPUT_PROTOCOL        *GraphicsOutput;\r
-  EFI_UGA_DRAW_PROTOCOL       *UgaDraw;\r
+  EFI_UGA_DRAW_PROTOCOL               *UgaDraw;\r
   EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION Foreground;\r
   EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION Background;\r
-  EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION BltChar[GLYPH_HEIGHT][GLYPH_WIDTH];\r
-  UINTN                       X;\r
-  UINTN                       Y;\r
+  EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION BltChar[EFI_GLYPH_HEIGHT][EFI_GLYPH_WIDTH];\r
+  UINTN                               PosX;\r
+  UINTN                               PosY;\r
 \r
   CurrentMode = This->Mode;\r
 \r
@@ -1741,13 +1920,13 @@ EraseCursor (
   UgaDraw = Private->UgaDraw;\r
 \r
   //\r
-  // BUGBUG - we need to think about what to do with wide and narrow character deletions.\r
+  // In this driver, only narrow character was supported.\r
   //\r
   //\r
   // Blt a character to the screen\r
   //\r
-  GlyphX  = (CurrentMode->CursorColumn * GLYPH_WIDTH) + Private->ModeData[CurrentMode->Mode].DeltaX;\r
-  GlyphY  = (CurrentMode->CursorRow * GLYPH_HEIGHT) + Private->ModeData[CurrentMode->Mode].DeltaY;\r
+  GlyphX  = (CurrentMode->CursorColumn * EFI_GLYPH_WIDTH) + Private->ModeData[CurrentMode->Mode].DeltaX;\r
+  GlyphY  = (CurrentMode->CursorRow * EFI_GLYPH_HEIGHT) + Private->ModeData[CurrentMode->Mode].DeltaY;\r
   if (GraphicsOutput != NULL) {\r
     GraphicsOutput->Blt (\r
               GraphicsOutput,\r
@@ -1757,11 +1936,11 @@ EraseCursor (
               GlyphY,\r
               0,\r
               0,\r
-              GLYPH_WIDTH,\r
-              GLYPH_HEIGHT,\r
-              GLYPH_WIDTH * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)\r
+              EFI_GLYPH_WIDTH,\r
+              EFI_GLYPH_HEIGHT,\r
+              EFI_GLYPH_WIDTH * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)\r
               );\r
-  } else {\r
+  } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {\r
     UgaDraw->Blt (\r
               UgaDraw,\r
               (EFI_UGA_PIXEL *) (UINTN) BltChar,\r
@@ -1770,9 +1949,9 @@ EraseCursor (
               GlyphY,\r
               0,\r
               0,\r
-              GLYPH_WIDTH,\r
-              GLYPH_HEIGHT,\r
-              GLYPH_WIDTH * sizeof (EFI_UGA_PIXEL)\r
+              EFI_GLYPH_WIDTH,\r
+              EFI_GLYPH_HEIGHT,\r
+              EFI_GLYPH_WIDTH * sizeof (EFI_UGA_PIXEL)\r
               );\r
   }\r
 \r
@@ -1781,10 +1960,10 @@ EraseCursor (
   //\r
   // Convert Monochrome bitmap of the Glyph to BltBuffer structure\r
   //\r
-  for (Y = 0; Y < GLYPH_HEIGHT; Y++) {\r
-    for (X = 0; X < GLYPH_WIDTH; X++) {\r
-      if ((mCursorGlyph.GlyphCol1[Y] & (1 << X)) != 0) {\r
-        BltChar[Y][GLYPH_WIDTH - X - 1].Raw ^= Foreground.Raw;\r
+  for (PosY = 0; PosY < EFI_GLYPH_HEIGHT; PosY++) {\r
+    for (PosX = 0; PosX < EFI_GLYPH_WIDTH; PosX++) {\r
+      if ((mCursorGlyph.GlyphCol1[PosY] & (BIT0 << PosX)) != 0) {\r
+        BltChar[PosY][EFI_GLYPH_WIDTH - PosX - 1].Raw ^= Foreground.Raw;\r
       }\r
     }\r
   }\r
@@ -1798,11 +1977,11 @@ EraseCursor (
               0,\r
               GlyphX,\r
               GlyphY,\r
-              GLYPH_WIDTH,\r
-              GLYPH_HEIGHT,\r
-              GLYPH_WIDTH * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)\r
+              EFI_GLYPH_WIDTH,\r
+              EFI_GLYPH_HEIGHT,\r
+              EFI_GLYPH_WIDTH * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)\r
               );\r
-  } else {\r
+  } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {\r
     UgaDraw->Blt (\r
               UgaDraw,\r
               (EFI_UGA_PIXEL *) (UINTN) BltChar,\r
@@ -1811,23 +1990,100 @@ EraseCursor (
               0,\r
               GlyphX,\r
               GlyphY,\r
-              GLYPH_WIDTH,\r
-              GLYPH_HEIGHT,\r
-              GLYPH_WIDTH * sizeof (EFI_UGA_PIXEL)\r
+              EFI_GLYPH_WIDTH,\r
+              EFI_GLYPH_HEIGHT,\r
+              EFI_GLYPH_WIDTH * sizeof (EFI_UGA_PIXEL)\r
               );\r
   }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  HII Database Protocol notification event handler.\r
+\r
+  Register font package when HII Database Protocol has been installed.\r
+\r
+  @param[in] Event    Event whose notification function is being invoked.\r
+  @param[in] Context  Pointer to the notification function's context.\r
+**/\r
+VOID\r
+EFIAPI\r
+RegisterFontPackage (\r
+  IN  EFI_EVENT       Event,\r
+  IN  VOID            *Context\r
+  )\r
+{\r
+  EFI_STATUS                           Status;\r
+  EFI_HII_SIMPLE_FONT_PACKAGE_HDR      *SimplifiedFont;\r
+  UINT32                               PackageLength;\r
+  UINT8                                *Package;\r
+  UINT8                                *Location;\r
+  EFI_HII_DATABASE_PROTOCOL            *HiiDatabase;\r
+\r
+  //\r
+  // Locate HII Database Protocol\r
+  //\r
+  Status = gBS->LocateProtocol (\r
+                  &gEfiHiiDatabaseProtocolGuid,\r
+                  NULL,\r
+                  (VOID **) &HiiDatabase\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // Add 4 bytes to the header for entire length for HiiAddPackages use only.\r
+  //\r
+  //    +--------------------------------+ <-- Package\r
+  //    |                                |\r
+  //    |    PackageLength(4 bytes)      |\r
+  //    |                                |\r
+  //    |--------------------------------| <-- SimplifiedFont\r
+  //    |                                |\r
+  //    |EFI_HII_SIMPLE_FONT_PACKAGE_HDR |\r
+  //    |                                |\r
+  //    |--------------------------------| <-- Location\r
+  //    |                                |\r
+  //    |     gUsStdNarrowGlyphData      |\r
+  //    |                                |\r
+  //    +--------------------------------+\r
+\r
+  PackageLength   = sizeof (EFI_HII_SIMPLE_FONT_PACKAGE_HDR) + mNarrowFontSize + 4;\r
+  Package = AllocateZeroPool (PackageLength);\r
+  ASSERT (Package != NULL);\r
+\r
+  WriteUnaligned32((UINT32 *) Package,PackageLength);\r
+  SimplifiedFont = (EFI_HII_SIMPLE_FONT_PACKAGE_HDR *) (Package + 4);\r
+  SimplifiedFont->Header.Length        = (UINT32) (PackageLength - 4);\r
+  SimplifiedFont->Header.Type          = EFI_HII_PACKAGE_SIMPLE_FONTS;\r
+  SimplifiedFont->NumberOfNarrowGlyphs = (UINT16) (mNarrowFontSize / sizeof (EFI_NARROW_GLYPH));\r
+\r
+  Location = (UINT8 *) (&SimplifiedFont->NumberOfWideGlyphs + 1);\r
+  CopyMem (Location, gUsStdNarrowGlyphData, mNarrowFontSize);\r
+\r
+  //\r
+  // Add this simplified font package to a package list then install it.\r
+  //\r
+  mHiiHandle = HiiAddPackages (\r
+                 &mFontPackageListGuid,\r
+                 NULL,\r
+                 Package,\r
+                 NULL\r
+                 );\r
+  ASSERT (mHiiHandle != NULL);\r
+  FreePool (Package);\r
+}\r
+\r
 /**\r
   The user Entry Point for module GraphicsConsole. The user code starts with this function.\r
 \r
-  @param[in] ImageHandle    The firmware allocated handle for the EFI image.  \r
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.\r
   @param[in] SystemTable    A pointer to the EFI System Table.\r
-  \r
-  @retval EFI_SUCCESS       The entry point is executed successfully.\r
-  @retval other             Some error occurs when executing this entry point.\r
+\r
+  @retval  EFI_SUCCESS       The entry point is executed successfully.\r
+  @return  other             Some error occurs when executing this entry point.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1839,21 +2095,31 @@ InitializeGraphicsConsole (
 {\r
   EFI_STATUS              Status;\r
 \r
+  //\r
+  // Register notify function on HII Database Protocol to add font package.\r
+  //\r
+  EfiCreateProtocolNotifyEvent (\r
+    &gEfiHiiDatabaseProtocolGuid,\r
+    TPL_CALLBACK,\r
+    RegisterFontPackage,\r
+    NULL,\r
+    &mHiiRegistration\r
+    );\r
+\r
   //\r
   // Install driver model protocol(s).\r
   //\r
-  Status = EfiLibInstallAllDriverProtocols (\r
+  Status = EfiLibInstallDriverBindingComponentName2 (\r
              ImageHandle,\r
              SystemTable,\r
              &gGraphicsConsoleDriverBinding,\r
              ImageHandle,\r
              &gGraphicsConsoleComponentName,\r
-             NULL,\r
-             NULL\r
+             &gGraphicsConsoleComponentName2\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-\r
   return Status;\r
 }\r
 \r
+\r