]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Console/GraphicsConsoleDxe/GraphicsConsole.c
MdeModulePkg: TerminalDxe driver code clean up
[mirror_edk2.git] / MdeModulePkg / Universal / Console / GraphicsConsoleDxe / GraphicsConsole.c
index b4ed7b91fdddf9785574b432b72d032741c790b7..bb279f985027e1cdc5c37b485b11ea604cce86da 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   This is the main routine for initializing the Graphics Console support routines.\r
 \r
-Copyright (c) 2006 - 2008 Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+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
@@ -14,87 +14,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "GraphicsConsole.h"\r
 \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
-  OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL    *Foreground,\r
-  OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL    *Background\r
-  );\r
-\r
-/**\r
-  Draw Unicode string on the Graphice 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
-  IN  CHAR16                           *UnicodeWeight,\r
-  IN  UINTN                            Count\r
-  );\r
-\r
-/**\r
-  Erase the cursor on the screen.\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
-  IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This\r
-  );\r
-\r
-/**\r
-  Check if the current specific mode supported the user defined resolution\r
-  for the Graphice Console devcie 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
-                            devcie 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
-CheckModeSupported (\r
-  EFI_GRAPHICS_OUTPUT_PROTOCOL  *GraphicsOutput,\r
-  IN  UINT32  HorizontalResolution,\r
-  IN  UINT32  VerticalResolution,\r
-  OUT UINT32  *CurrentModeNumber\r
-  );\r
-\r
 //\r
-// Graphics Console Devcie Private Data template\r
+// Graphics Console Device Private Data template\r
 //\r
-STATIC 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
@@ -119,52 +42,53 @@ STATIC GRAPHICS_CONSOLE_DEV    mGraphicsConsoleDevTemplate = {
     TRUE\r
   },\r
   {\r
-    { 80, 25, 0, 0, 0, 0 },  // Mode 0\r
-    { 80, 50, 0, 0, 0, 0 },  // Mode 1\r
-    { 100,31, 0, 0, 0, 0 },  // Mode 2\r
-    {  0,  0, 0, 0, 0, 0 }   // Mode 3\r
+    { 80, 25, 0, 0, 0, 0, 0 },  // Mode 0\r
+    { 80, 50, 0, 0, 0, 0, 0 },  // Mode 1\r
+    { 100,31, 0, 0, 0, 0, 0 },  // Mode 2\r
+    {  0,  0, 0, 0, 0, 0, 0 },  // Mode 3\r
+    {  0,  0, 0, 0, 0, 0, 0 }   // Mode 4\r
   },\r
-  (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) NULL,\r
-  (EFI_HII_HANDLE ) 0\r
+  (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) NULL\r
 };\r
 \r
 EFI_HII_DATABASE_PROTOCOL   *mHiiDatabase;\r
 EFI_HII_FONT_PROTOCOL       *mHiiFont;\r
-BOOLEAN                     mFirstAccessFlag = TRUE;\r
+EFI_HII_HANDLE              mHiiHandle;\r
+EFI_EVENT                   mHiiRegistration;\r
 \r
-STATIC EFI_GUID             mFontPackageListGuid = {0xf5f219d3, 0x7006, 0x4648, 0xac, 0x8d, 0xd6, 0x1d, 0xfb, 0x7b, 0xc6, 0xad};\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
-STATIC CHAR16       SpaceStr[] = { NARROW_CHAR, ' ', 0 };\r
+CHAR16       SpaceStr[] = { NARROW_CHAR, ' ', 0 };\r
 \r
 EFI_DRIVER_BINDING_PROTOCOL gGraphicsConsoleDriverBinding = {\r
   GraphicsConsoleControllerDriverSupported,\r
@@ -175,7 +99,6 @@ EFI_DRIVER_BINDING_PROTOCOL gGraphicsConsoleDriverBinding = {
   NULL\r
 };\r
 \r
-\r
 /**\r
   Test to see if Graphics Console could be supported on the Controller.\r
 \r
@@ -289,10 +212,10 @@ Error:
 \r
 \r
 /**\r
-  Start this driver on Controller by opening Graphics Output protocol or \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 shkipped if PcdUgaConsumeSupport is set to FALSE.)\r
-  \r
+  (UGA Draw protocol could be skipped if PcdUgaConsumeSupport is set to FALSE.)\r
+\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
@@ -312,21 +235,17 @@ GraphicsConsoleControllerDriverStart (
 {\r
   EFI_STATUS                           Status;\r
   GRAPHICS_CONSOLE_DEV                 *Private;\r
-  UINTN                                NarrowFontSize;\r
   UINT32                               HorizontalResolution;\r
   UINT32                               VerticalResolution;\r
   UINT32                               ColorDepth;\r
   UINT32                               RefreshRate;\r
+  UINTN                                ModeIndex;\r
   UINTN                                MaxMode;\r
   UINTN                                Columns;\r
   UINTN                                Rows;\r
   UINT32                               ModeNumber;\r
-  EFI_HII_SIMPLE_FONT_PACKAGE_HDR      *SimplifiedFont;\r
-  UINTN                                PackageLength;\r
-  EFI_HII_PACKAGE_LIST_HEADER          *PackageList;\r
-  UINT8                                *Package;\r
-  UINT8                                *Location;\r
-\r
+  EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE    *Mode;\r
+  GRAPHICS_CONSOLE_MODE_DATA           *ModeData;\r
   ModeNumber = 0;\r
 \r
   //\r
@@ -366,41 +285,9 @@ GraphicsConsoleControllerDriverStart (
     goto Error;\r
   }\r
 \r
-  NarrowFontSize  = ReturnNarrowFontSize ();\r
-\r
-  if (mFirstAccessFlag) {\r
-    //\r
-    // Add 4 bytes to the header for entire length for HiiLibPreparePackageList use only.\r
-    // Looks ugly. Might be updated when font tool is ready.\r
-    //\r
-    PackageLength   = sizeof (EFI_HII_SIMPLE_FONT_PACKAGE_HDR) + NarrowFontSize + 4;\r
-    Package = AllocateZeroPool (PackageLength);\r
-    if (Package == NULL) {\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }\r
-    CopyMem (Package, &PackageLength, 4);\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) (NarrowFontSize / sizeof (EFI_NARROW_GLYPH));\r
-\r
-    Location = (UINT8 *) (&SimplifiedFont->NumberOfWideGlyphs + 1);\r
-    CopyMem (Location, gUsStdNarrowGlyphData, NarrowFontSize);\r
-\r
-    //\r
-    // Add this simplified font package to a package list then install it.\r
-    //\r
-    PackageList = HiiLibPreparePackageList (1, &mFontPackageListGuid, Package);\r
-    Status = mHiiDatabase->NewPackageList (mHiiDatabase, PackageList, NULL, &(Private->HiiHandle));\r
-    ASSERT_EFI_ERROR (Status);\r
-    SafeFreePool (PackageList);\r
-    SafeFreePool (Package);\r
-\r
-    mFirstAccessFlag = FALSE;\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
+  // If the current mode information can not be retrieved, then attempt to set the default mode\r
+  // of 800x600, 32 bit color, 60 Hz refresh.\r
   //\r
   HorizontalResolution  = 800;\r
   VerticalResolution    = 600;\r
@@ -435,13 +322,15 @@ GraphicsConsoleControllerDriverStart (
                    );\r
     }\r
 \r
-    if (EFI_ERROR (Status) || (ModeNumber == Private->GraphicsOutput->Mode->MaxMode)) {\r
+    Mode = Private->GraphicsOutput->Mode;\r
+\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 = Private->GraphicsOutput->Mode->Info->HorizontalResolution;\r
-      VerticalResolution = Private->GraphicsOutput->Mode->Info->VerticalResolution;\r
-      ModeNumber = Private->GraphicsOutput->Mode->Mode;\r
+      HorizontalResolution = Mode->Info->HorizontalResolution;\r
+      VerticalResolution = Mode->Info->VerticalResolution;\r
+      ModeNumber = Mode->Mode;\r
     }\r
   } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {\r
     //\r
@@ -489,78 +378,63 @@ GraphicsConsoleControllerDriverStart (
   }\r
 \r
   //\r
-  // Compute the maximum number of text Rows and Columns that this current graphics mode can support\r
+  // Add Mode #3 that uses the entire display for user-defined mode\r
   //\r
-  Columns = HorizontalResolution / EFI_GLYPH_WIDTH;\r
-  Rows    = VerticalResolution / EFI_GLYPH_HEIGHT;\r
+  Private->ModeData[3].Columns = HorizontalResolution / EFI_GLYPH_WIDTH;\r
+  Private->ModeData[3].Rows    = VerticalResolution / EFI_GLYPH_HEIGHT;\r
 \r
   //\r
-  // See if the mode is too small to support the required 80x25 text mode\r
+  // Add Mode #4 that uses the PCD values\r
   //\r
-  if (Columns < 80 || Rows < 25) {\r
-    goto Error;\r
-  }\r
+  Private->ModeData[4].Columns = (UINTN) PcdGet32 (PcdConOutColumn);\r
+  Private->ModeData[4].Rows    = (UINTN) PcdGet32 (PcdConOutRow);\r
+\r
   //\r
-  // Add Mode #0 that must be 80x25\r
+  // Compute the maximum number of text Rows and Columns that this current graphics mode can support\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 * EFI_GLYPH_WIDTH)) >> 1;\r
-  Private->ModeData[MaxMode].DeltaY        = (VerticalResolution - (25 * EFI_GLYPH_HEIGHT)) >> 1;\r
-  MaxMode++;\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 * EFI_GLYPH_WIDTH)) >> 1;\r
-    Private->ModeData[MaxMode].DeltaY        = (VerticalResolution - (50 * EFI_GLYPH_HEIGHT)) >> 1;\r
-    MaxMode++;\r
-  }\r
+  Columns = HorizontalResolution / EFI_GLYPH_WIDTH;\r
+  Rows    = VerticalResolution / EFI_GLYPH_HEIGHT;\r
 \r
   //\r
-  // If it is not to support Mode #1 - 80x50, then skip it\r
+  // Here we make sure that mode 0 is valid\r
   //\r
-  if (MaxMode < 2) {\r
-    Private->ModeData[MaxMode].Columns       = 0;\r
-    Private->ModeData[MaxMode].Rows          = 0;\r
-    Private->ModeData[MaxMode].GopWidth      = HorizontalResolution;\r
-    Private->ModeData[MaxMode].GopHeight     = VerticalResolution;\r
-    Private->ModeData[MaxMode].GopModeNumber = ModeNumber;\r
-    Private->ModeData[MaxMode].DeltaX        = 0;\r
-    Private->ModeData[MaxMode].DeltaY        = 0;\r
-    MaxMode++;\r
+  if (Columns < Private->ModeData[0].Columns ||\r
+      Rows < Private->ModeData[0].Rows) {\r
+    //\r
+    // 80x25 cannot be supported.\r
+    //\r
+    // Fallback to using the PcdConOutColumn and PcdConOutRow\r
+    // for mode 0.  If the PCDs are also to large, then mode 0\r
+    // will be shrunk to fit as needed.\r
+    //\r
+    Private->ModeData[0].Columns = MIN (Private->ModeData[4].Columns, Columns);\r
+    Private->ModeData[0].Rows    = MIN (Private->ModeData[4].Rows, Rows);\r
   }\r
 \r
-  //\r
-  // Add Mode #2 that must be 100x31 (graphic mode >= 800x600)\r
-  //\r
-  if (Columns >= 100 && Rows >= 31) {\r
-    Private->ModeData[MaxMode].GopWidth      = HorizontalResolution;\r
-    Private->ModeData[MaxMode].GopHeight     = VerticalResolution;\r
-    Private->ModeData[MaxMode].GopModeNumber = ModeNumber;\r
-    Private->ModeData[MaxMode].DeltaX        = (HorizontalResolution - (100 * EFI_GLYPH_WIDTH)) >> 1;\r
-    Private->ModeData[MaxMode].DeltaY        = (VerticalResolution - (31 * EFI_GLYPH_HEIGHT)) >> 1;\r
-    MaxMode++;\r
+  MaxMode = 0;\r
+  for (ModeIndex = 0; ModeIndex < GRAPHICS_MAX_MODE; ModeIndex++) {\r
+    ModeData = &Private->ModeData[ModeIndex];\r
+    ModeData->GopWidth      = HorizontalResolution;\r
+    ModeData->GopHeight     = VerticalResolution;\r
+    ModeData->GopModeNumber = ModeNumber;\r
+    if (Columns >= ModeData->Columns &&\r
+        Rows >= ModeData->Rows) {\r
+      ModeData->DeltaX        = (HorizontalResolution - (ModeData->Columns * EFI_GLYPH_WIDTH)) >> 1;\r
+      ModeData->DeltaY        = (VerticalResolution - (ModeData->Rows * EFI_GLYPH_HEIGHT)) >> 1;\r
+      MaxMode = ModeIndex + 1;\r
+    } else {\r
+      ModeData->Columns       = 0;\r
+      ModeData->Rows          = 0;\r
+      ModeData->DeltaX        = 0;\r
+      ModeData->DeltaY        = 0;\r
+    }\r
   }\r
 \r
   //\r
-  // Add Mode #3 that uses the entire display for user-defined mode\r
+  // See if the resolution was too small to support any text modes\r
   //\r
-  if (HorizontalResolution > 800 && VerticalResolution > 600) {\r
-    Private->ModeData[MaxMode].Columns       = HorizontalResolution/EFI_GLYPH_WIDTH;\r
-    Private->ModeData[MaxMode].Rows          = VerticalResolution/EFI_GLYPH_HEIGHT;\r
-    Private->ModeData[MaxMode].GopWidth      = HorizontalResolution;\r
-    Private->ModeData[MaxMode].GopHeight     = VerticalResolution;\r
-    Private->ModeData[MaxMode].GopModeNumber = ModeNumber;\r
-    Private->ModeData[MaxMode].DeltaX        = (HorizontalResolution % EFI_GLYPH_WIDTH) >> 1;\r
-    Private->ModeData[MaxMode].DeltaY        = (VerticalResolution % EFI_GLYPH_HEIGHT) >> 1;\r
-    MaxMode++;\r
+  if (MaxMode == 0) {\r
+    goto Error;\r
   }\r
 \r
   //\r
@@ -597,39 +471,38 @@ Error:
     //\r
     if (Private->GraphicsOutput != NULL) {\r
       gBS->CloseProtocol (\r
-            Controller,\r
-            &gEfiGraphicsOutputProtocolGuid,\r
-            This->DriverBindingHandle,\r
-            Controller\r
-            );\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
     //\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
+  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 shkipped if PcdUgaConsumeSupport is set to FALSE.)\r
-  \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
@@ -638,7 +511,7 @@ Error:
   @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
+  @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
@@ -696,21 +569,14 @@ GraphicsConsoleControllerDriverStop (
             );\r
     }\r
 \r
-    //\r
-    // Remove the font pack\r
-    //\r
-    if (Private->HiiHandle != NULL) {\r
-      HiiLibRemovePackages (Private->HiiHandle);\r
-      mFirstAccessFlag = TRUE;\r
+    if (Private->LineBuffer != NULL) {\r
+      FreePool (Private->LineBuffer);\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
@@ -718,20 +584,20 @@ GraphicsConsoleControllerDriverStop (
 \r
 /**\r
   Check if the current specific mode supported the user defined resolution\r
-  for the Graphice Console devcie based on Graphics Output Protocol.\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
+\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
-                            devcie supported.\r
-  @retval other             The specific mode does not support user defined \r
-                            resolution or failed to set the current mode to the \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
@@ -747,10 +613,12 @@ CheckModeSupported (
   EFI_STATUS Status;\r
   UINTN      SizeOfInfo;\r
   EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;\r
+  UINT32     MaxMode;\r
 \r
-  Status = EFI_SUCCESS;\r
+  Status  = EFI_SUCCESS;\r
+  MaxMode = GraphicsOutput->Mode->MaxMode;\r
 \r
-  for (ModeNumber = 0; ModeNumber < GraphicsOutput->Mode->MaxMode; ModeNumber++) {\r
+  for (ModeNumber = 0; ModeNumber < MaxMode; ModeNumber++) {\r
     Status = GraphicsOutput->QueryMode (\r
                        GraphicsOutput,\r
                        ModeNumber,\r
@@ -760,13 +628,21 @@ CheckModeSupported (
     if (!EFI_ERROR (Status)) {\r
       if ((Info->HorizontalResolution == HorizontalResolution) &&\r
           (Info->VerticalResolution == VerticalResolution)) {\r
-        Status = GraphicsOutput->SetMode (GraphicsOutput, ModeNumber);\r
-        if (!EFI_ERROR (Status)) {\r
-          gBS->FreePool (Info);\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
           break;\r
+        } else {\r
+          Status = GraphicsOutput->SetMode (GraphicsOutput, ModeNumber);\r
+          if (!EFI_ERROR (Status)) {\r
+            FreePool (Info);\r
+            break;\r
+          }\r
         }\r
       }\r
-      gBS->FreePool (Info);\r
+      FreePool (Info);\r
     }\r
   }\r
 \r
@@ -782,9 +658,9 @@ CheckModeSupported (
 /**\r
   Locate HII Database protocol and HII Font protocol.\r
 \r
-  @retval  EFI_SUCCESS     HII Database protocol and HII Font protocol \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
+  @return  other           Failed to locate HII Database protocol or\r
                            HII Font protocol.\r
 \r
 **/\r
@@ -837,8 +713,8 @@ EfiLocateHiiProtocol (
 //\r
 \r
 /**\r
-  Reset the text output device hardware and optionaly run diagnostics.\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
@@ -869,7 +745,7 @@ GraphicsConsoleConOutReset (
 /**\r
   Write a Unicode string to the output device.\r
 \r
-  Implements SIMPLE_TEXT_OUTPUT.OutputString(). \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
@@ -928,8 +804,8 @@ 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
+  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
@@ -939,7 +815,7 @@ GraphicsConsoleConOutOutputString (
   //\r
   GetTextColors (This, &Foreground, &Background);\r
 \r
-  EraseCursor (This);\r
+  FlushCursor (This);\r
 \r
   Warning = FALSE;\r
 \r
@@ -959,7 +835,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
@@ -969,7 +845,7 @@ GraphicsConsoleConOutOutputString (
         //\r
         This->Mode->CursorColumn--;\r
         This->OutputString (This, SpaceStr);\r
-        EraseCursor (This);\r
+        FlushCursor (This);\r
         This->Mode->CursorColumn--;\r
       }\r
 \r
@@ -1085,33 +961,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
@@ -1143,16 +1004,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
@@ -1164,10 +1025,10 @@ GraphicsConsoleConOutOutputString (
 }\r
 \r
 /**\r
-  Verifies that all characters in a Unicode string can be output to the \r
+  Verifies that all characters in a Unicode string can be output to the\r
   target device.\r
 \r
-  Implements SIMPLE_TEXT_OUTPUT.QueryMode().\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
@@ -1205,8 +1066,10 @@ GraphicsConsoleConOutTestString (
                          &Blt,\r
                          NULL\r
                          );\r
-    SafeFreePool (Blt);\r
-    Blt = NULL;\r
+    if (Blt != NULL) {\r
+      FreePool (Blt);\r
+      Blt = NULL;\r
+    }\r
     Count++;\r
 \r
     if (EFI_ERROR (Status)) {\r
@@ -1274,7 +1137,7 @@ Done:
 \r
 /**\r
   Sets the output device(s) to a specified mode.\r
-  \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
@@ -1282,7 +1145,7 @@ Done:
   @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
+  @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
@@ -1313,11 +1176,6 @@ GraphicsConsoleConOutSetMode (
   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
   //\r
@@ -1364,7 +1222,7 @@ GraphicsConsoleConOutSetMode (
     // 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
-    EraseCursor (This);\r
+    FlushCursor (This);\r
 \r
     FreePool (Private->LineBuffer);\r
   }\r
@@ -1376,7 +1234,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
@@ -1391,7 +1249,7 @@ GraphicsConsoleConOutSetMode (
       //\r
       Status = GraphicsOutput->Blt (\r
                           GraphicsOutput,\r
-                          &mEfiColors[0],\r
+                          &mGraphicsEfiColors[0],\r
                           EfiBltVideoFill,\r
                           0,\r
                           0,\r
@@ -1415,7 +1273,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
@@ -1436,7 +1294,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
@@ -1455,9 +1313,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->Mode->CursorColumn  = 0;\r
+  This->Mode->CursorRow     = 0;\r
+\r
+  FlushCursor (This);  \r
 \r
   Status = EFI_SUCCESS;\r
 \r
@@ -1475,7 +1336,7 @@ Done:
 \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
+                                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
@@ -1502,11 +1363,11 @@ 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
@@ -1515,7 +1376,7 @@ GraphicsConsoleConOutSetAttribute (
 \r
 \r
 /**\r
-  Clears the output device(s) display to the currently selected background \r
+  Clears the output device(s) display to the currently selected background\r
   color.\r
 \r
   Implements SIMPLE_TEXT_OUTPUT.ClearScreen().\r
@@ -1583,7 +1444,7 @@ GraphicsConsoleConOutClearScreen (
   This->Mode->CursorColumn  = 0;\r
   This->Mode->CursorRow     = 0;\r
 \r
-  EraseCursor (This);\r
+  FlushCursor (This);\r
 \r
   gBS->RestoreTPL (OldTpl);\r
 \r
@@ -1640,12 +1501,12 @@ GraphicsConsoleConOutSetCursorPosition (
     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
@@ -1677,11 +1538,11 @@ GraphicsConsoleConOutEnableCursor (
 \r
   OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
 \r
-  EraseCursor (This);\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
@@ -1708,14 +1569,14 @@ 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
 /**\r
-  Draw Unicode string on the Graphice Console device's screen.\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
@@ -1754,7 +1615,7 @@ DrawUnicodeWeightAtCursorN (
 \r
   String = AllocateCopyPool ((Count + 1) * sizeof (CHAR16), UnicodeWeight);\r
   if (String == NULL) {\r
-    SafeFreePool (Blt);\r
+    FreePool (Blt);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
   //\r
@@ -1764,8 +1625,8 @@ DrawUnicodeWeightAtCursorN (
 \r
   FontInfo = (EFI_FONT_DISPLAY_INFO *) AllocateZeroPool (sizeof (EFI_FONT_DISPLAY_INFO));\r
   if (FontInfo == NULL) {\r
-    SafeFreePool (Blt);\r
-    SafeFreePool (String);\r
+    FreePool (Blt);\r
+    FreePool (String);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
   //\r
@@ -1775,13 +1636,13 @@ DrawUnicodeWeightAtCursorN (
 \r
   if (Private->GraphicsOutput != NULL) {\r
     //\r
-    // If Graphcis Output protocol exists, using HII Font protocol to draw. \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,\r
+                         EFI_HII_IGNORE_IF_NO_GLYPH | EFI_HII_DIRECT_TO_SCREEN | EFI_HII_IGNORE_LINE_BREAK,\r
                          String,\r
                          FontInfo,\r
                          &Blt,\r
@@ -1794,7 +1655,7 @@ DrawUnicodeWeightAtCursorN (
 \r
   } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {\r
     //\r
-    // If Graphics Output protocol cannot be found and PcdUgaConsumeSupport enabled, \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
@@ -1803,8 +1664,8 @@ DrawUnicodeWeightAtCursorN (
 \r
     Blt->Image.Bitmap = AllocateZeroPool (Blt->Width * Blt->Height * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));\r
     if (Blt->Image.Bitmap == NULL) {\r
-      SafeFreePool (Blt);\r
-      SafeFreePool (String);\r
+      FreePool (Blt);\r
+      FreePool (String);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
@@ -1815,7 +1676,7 @@ DrawUnicodeWeightAtCursorN (
     //\r
     Status = mHiiFont->StringToImage (\r
                           mHiiFont,\r
-                          EFI_HII_IGNORE_IF_NO_GLYPH,\r
+                          EFI_HII_IGNORE_IF_NO_GLYPH | EFI_HII_IGNORE_LINE_BREAK,\r
                           String,\r
                           FontInfo,\r
                           &Blt,\r
@@ -1847,20 +1708,31 @@ DrawUnicodeWeightAtCursorN (
                           );\r
     }\r
 \r
-    SafeFreePool (RowInfoArray);\r
-    SafeFreePool (Blt->Image.Bitmap);\r
+    FreePool (RowInfoArray);\r
+    FreePool (Blt->Image.Bitmap);\r
   } else {\r
     Status = EFI_UNSUPPORTED;\r
   }\r
 \r
-  SafeFreePool (Blt);\r
-  SafeFreePool (String);\r
-  SafeFreePool (FontInfo);\r
+  if (Blt != NULL) {\r
+    FreePool (Blt);\r
+  }\r
+  if (String != NULL) {\r
+    FreePool (String);\r
+  }\r
+  if (FontInfo != NULL) {\r
+    FreePool (FontInfo);\r
+  }\r
   return Status;\r
 }\r
 \r
 /**\r
-  Erase the cursor on the screen.\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
@@ -1868,21 +1740,21 @@ DrawUnicodeWeightAtCursorN (
 \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[EFI_GLYPH_HEIGHT][EFI_GLYPH_WIDTH];\r
-  UINTN                       PosX;\r
-  UINTN                       PosY;\r
+  UINTN                               PosX;\r
+  UINTN                               PosY;\r
 \r
   CurrentMode = This->Mode;\r
 \r
@@ -1937,7 +1809,7 @@ EraseCursor (
   //\r
   for (PosY = 0; PosY < EFI_GLYPH_HEIGHT; PosY++) {\r
     for (PosX = 0; PosX < EFI_GLYPH_WIDTH; PosX++) {\r
-      if ((mCursorGlyph.GlyphCol1[PosY] & (1 << PosX)) != 0) {\r
+      if ((mCursorGlyph.GlyphCol1[PosY] & (BIT0 << PosX)) != 0) {\r
         BltChar[PosY][EFI_GLYPH_WIDTH - PosX - 1].Raw ^= Foreground.Raw;\r
       }\r
     }\r
@@ -1974,14 +1846,89 @@ EraseCursor (
   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
+  ASSERT_EFI_ERROR (Status);\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] 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
+  @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
@@ -1993,6 +1940,17 @@ 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
@@ -2006,7 +1964,6 @@ InitializeGraphicsConsole (
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-\r
   return Status;\r
 }\r
 \r