]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Console/GraphicsConsoleDxe/GraphicsConsole.c
Check comments, format and fix some typo.
[mirror_edk2.git] / MdeModulePkg / Universal / Console / GraphicsConsoleDxe / GraphicsConsole.c
index 13486a0462255b6d03cf86429658df6d211118e4..2fe6dc02dae7578fd8aebe115c5ad272bd27025d 100644 (file)
@@ -1,10 +1,5 @@
 /** @file\r
   This is the main routine for initializing the Graphics Console support routines.\r
-Remaining Tasks\r
-  Add all standard Glyphs from UEFI 2.0 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 - 2008 Intel Corporation. <BR>\r
 All rights reserved. This program and the accompanying materials\r
@@ -19,6 +14,90 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "GraphicsConsole.h"\r
 \r
+//\r
+// Graphics Console Device Private Data template\r
+//\r
+GRAPHICS_CONSOLE_DEV    mGraphicsConsoleDevTemplate = {\r
+  GRAPHICS_CONSOLE_DEV_SIGNATURE,\r
+  (EFI_GRAPHICS_OUTPUT_PROTOCOL *) NULL,\r
+  (EFI_UGA_DRAW_PROTOCOL *) NULL,\r
+  {\r
+    GraphicsConsoleConOutReset,\r
+    GraphicsConsoleConOutOutputString,\r
+    GraphicsConsoleConOutTestString,\r
+    GraphicsConsoleConOutQueryMode,\r
+    GraphicsConsoleConOutSetMode,\r
+    GraphicsConsoleConOutSetAttribute,\r
+    GraphicsConsoleConOutClearScreen,\r
+    GraphicsConsoleConOutSetCursorPosition,\r
+    GraphicsConsoleConOutEnableCursor,\r
+    (EFI_SIMPLE_TEXT_OUTPUT_MODE *) NULL\r
+  },\r
+  {\r
+    0,\r
+    0,\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
+    { 100,31, 0, 0, 0, 0 },  // Mode 2\r
+    {  0,  0, 0, 0, 0, 0 }   // Mode 3\r
+  },\r
+  (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) NULL,\r
+  (EFI_HII_HANDLE ) 0\r
+};\r
+\r
+EFI_HII_DATABASE_PROTOCOL   *mHiiDatabase;\r
+EFI_HII_FONT_PROTOCOL       *mHiiFont;\r
+BOOLEAN                     mFirstAccessFlag = TRUE;\r
+\r
+EFI_GUID             mFontPackageListGuid = {0xf5f219d3, 0x7006, 0x4648, {0xac, 0x8d, 0xd6, 0x1d, 0xfb, 0x7b, 0xc6, 0xad}};\r
+\r
+CHAR16               mCrLfString[3] = { CHAR_CARRIAGE_RETURN, CHAR_LINEFEED, CHAR_NULL };\r
+\r
+EFI_GRAPHICS_OUTPUT_BLT_PIXEL        mEfiColors[16] = {\r
+  //\r
+  // B    G    R   reserved\r
+  //\r
+  {0x00, 0x00, 0x00, 0x00},  // BLACK\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},  // 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
+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
+  GraphicsConsoleControllerDriverStop,\r
+  0xa,\r
+  NULL,\r
+  NULL\r
+};\r
+\r
 /**\r
   Gets Graphics Console devcie's foreground color and background color.\r
 \r
@@ -37,7 +116,7 @@ GetTextColors (
   );\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
@@ -71,18 +150,18 @@ EraseCursor (
 \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
+  If yes, set the graphic device'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       This driver is removed ControllerHandle.\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
+                            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
@@ -96,105 +175,21 @@ CheckModeSupported (
   OUT UINT32  *CurrentModeNumber\r
   );\r
 \r
-//\r
-// Globals\r
-//\r
-GRAPHICS_CONSOLE_DEV        mGraphicsConsoleDevTemplate = {\r
-  GRAPHICS_CONSOLE_DEV_SIGNATURE,\r
-  (EFI_GRAPHICS_OUTPUT_PROTOCOL *) NULL,\r
-  (EFI_UGA_DRAW_PROTOCOL *) NULL,\r
-  {\r
-    GraphicsConsoleConOutReset,\r
-    GraphicsConsoleConOutOutputString,\r
-    GraphicsConsoleConOutTestString,\r
-    GraphicsConsoleConOutQueryMode,\r
-    GraphicsConsoleConOutSetMode,\r
-    GraphicsConsoleConOutSetAttribute,\r
-    GraphicsConsoleConOutClearScreen,\r
-    GraphicsConsoleConOutSetCursorPosition,\r
-    GraphicsConsoleConOutEnableCursor,\r
-    (EFI_SIMPLE_TEXT_OUTPUT_MODE *) NULL\r
-  },\r
-  {\r
-    0,\r
-    0,\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
-    { 100,31, 0, 0, 0, 0 },  // Mode 2\r
-    {  0,  0, 0, 0, 0, 0 }   // Mode 3\r
-  },\r
-  (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) NULL,\r
-  (EFI_HII_HANDLE ) 0\r
-};\r
-\r
-EFI_HII_DATABASE_PROTOCOL   *mHiiDatabase;\r
-EFI_HII_FONT_PROTOCOL       *mHiiFont;\r
-BOOLEAN                     mFirstAccessFlag = TRUE;\r
-\r
-STATIC 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
-\r
-STATIC EFI_GRAPHICS_OUTPUT_BLT_PIXEL        mEfiColors[16] = {\r
-  //\r
-  // B     G     R\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, 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
-};\r
-\r
-STATIC 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
-\r
-EFI_DRIVER_BINDING_PROTOCOL gGraphicsConsoleDriverBinding = {\r
-  GraphicsConsoleControllerDriverSupported,\r
-  GraphicsConsoleControllerDriverStart,\r
-  GraphicsConsoleControllerDriverStop,\r
-  0xa,\r
-  NULL,\r
-  NULL\r
-};\r
-\r
 \r
 /**\r
-  Test to see if Graphics Console could be supported on the ControllerHandle.\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 ControllerHandle. (UGA Draw Protocol could be shipped\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  ControllerHandle    Handle of device to test.\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
+  @retval EFI_SUCCESS         This driver supports this device.\r
+  @retval other               This driver does not support this device.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -294,17 +289,17 @@ Error:
 \r
 \r
 /**\r
-  Start this driver on ControllerHandle by opening Graphics Output protocol or \r
-  UGA Draw protocol, and installing Simple Text Out protocol on ControllerHandle.\r
-  (UGA Draw protocol could be shkipped if PcdUgaConsumeSupport is set to FALSE.)\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
   @param  This                 Protocol instance pointer.\r
-  @param  ControllerHandle     Handle of device to bind driver to\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
-  @retval EFI_SUCCESS          This driver is added to ControllerHandle\r
-  @retval other                This driver does not support this device\r
+  @retval EFI_SUCCESS          This driver is added to Controller.\r
+  @retval other                This driver does not support this device.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -317,7 +312,7 @@ GraphicsConsoleControllerDriverStart (
 {\r
   EFI_STATUS                           Status;\r
   GRAPHICS_CONSOLE_DEV                 *Private;\r
-  UINT                               NarrowFontSize;\r
+  UINT32                               NarrowFontSize;\r
   UINT32                               HorizontalResolution;\r
   UINT32                               VerticalResolution;\r
   UINT32                               ColorDepth;\r
@@ -327,11 +322,11 @@ GraphicsConsoleControllerDriverStart (
   UINTN                                Rows;\r
   UINT32                               ModeNumber;\r
   EFI_HII_SIMPLE_FONT_PACKAGE_HDR      *SimplifiedFont;\r
-  UINT                               PackageLength;\r
+  UINT32                               PackageLength;\r
   EFI_HII_PACKAGE_LIST_HEADER          *PackageList;\r
   UINT8                                *Package;\r
   UINT8                                *Location;\r
-\r
+  EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE    *Mode;\r
   ModeNumber = 0;\r
 \r
   //\r
@@ -378,19 +373,33 @@ GraphicsConsoleControllerDriverStart (
     // 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
+    //    +--------------------------------+ <-- 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) + 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
+    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) (NarrowFontSize / sizeof (EFI_NARROW_GLYPH));\r
 \r
     Location = (UINT8 *) (&SimplifiedFont->NumberOfWideGlyphs + 1);\r
-    CopyMem (Location, UsStdNarrowGlyphData, NarrowFontSize);\r
+    CopyMem (Location, gUsStdNarrowGlyphData, NarrowFontSize);\r
 \r
     //\r
     // Add this simplified font package to a package list then install it.\r
@@ -398,14 +407,14 @@ GraphicsConsoleControllerDriverStart (
     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
+    FreePool (PackageList);\r
+    FreePool (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
@@ -439,14 +448,16 @@ GraphicsConsoleControllerDriverStart (
                    &ModeNumber\r
                    );\r
     }\r
-\r
-    if (EFI_ERROR (Status) || (ModeNumber == Private->GraphicsOutput->Mode->MaxMode)) {\r
+    \r
+    Mode = Private->GraphicsOutput->Mode;\r
+     \r
+    if (EFI_ERROR (Status) || (Mode->MaxMode)) {\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
@@ -509,22 +520,22 @@ GraphicsConsoleControllerDriverStart (
   // 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].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
+  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].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
+    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
 \r
@@ -532,13 +543,13 @@ GraphicsConsoleControllerDriverStart (
   // If it is not to support Mode #1 - 80x50, then skip it\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].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
+    Private->ModeData[MaxMode].DeltaX        = 0;\r
+    Private->ModeData[MaxMode].DeltaY        = 0;\r
     MaxMode++;\r
   }\r
 \r
@@ -546,11 +557,11 @@ GraphicsConsoleControllerDriverStart (
   // 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].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
+    Private->ModeData[MaxMode].DeltaX        = (HorizontalResolution - (100 * EFI_GLYPH_WIDTH)) >> 1;\r
+    Private->ModeData[MaxMode].DeltaY        = (VerticalResolution - (31 * EFI_GLYPH_HEIGHT)) >> 1;\r
     MaxMode++;\r
   }\r
 \r
@@ -558,13 +569,13 @@ GraphicsConsoleControllerDriverStart (
   // Add Mode #3 that uses the entire display for user-defined mode\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].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
+    Private->ModeData[MaxMode].DeltaX        = (HorizontalResolution % EFI_GLYPH_WIDTH) >> 1;\r
+    Private->ModeData[MaxMode].DeltaY        = (VerticalResolution % EFI_GLYPH_HEIGHT) >> 1;\r
     MaxMode++;\r
   }\r
 \r
@@ -598,7 +609,7 @@ 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
@@ -631,20 +642,20 @@ Error:
 }\r
 \r
 /**\r
-  Stop this driver on ControllerHandle by removing Simple Text Out protocol \r
-  and closing the Graphics Output Protocol or UGA Draw protocol on ControllerHandle.\r
-  (UGA Draw protocol could be shkipped if PcdUgaConsumeSupport is set to FALSE.)\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  ControllerHandle  Handle of device to stop driver on\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 ControllerHandle.\r
+  @retval EFI_SUCCESS       This driver is removed Controller.\r
   @retval EFI_NOT_STARTED   Simple Text Out protocol could not be found the \r
-                            ControllerHandle.\r
+                            Controller.\r
   @retval other             This driver was not removed from this device.\r
 \r
 **/\r
@@ -723,7 +734,7 @@ 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
@@ -732,9 +743,9 @@ GraphicsConsoleControllerDriverStop (
   @param  VerticalResolution    User defined vertical resolution.\r
   @param  CurrentModeNumber     Current specific mode to be check.\r
 \r
-  @retval EFI_SUCCESS       This driver is removed ControllerHandle.\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
+                            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
@@ -752,10 +763,12 @@ CheckModeSupported (
   EFI_STATUS Status;\r
   UINTN      SizeOfInfo;\r
   EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;\r
+  UINT32     MaxMode;\r
 \r
-  Status = EFI_SUCCESS;\r
-\r
-  for (ModeNumber = 0; ModeNumber < GraphicsOutput->Mode->MaxMode; ModeNumber++) {\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
@@ -767,11 +780,11 @@ CheckModeSupported (
           (Info->VerticalResolution == VerticalResolution)) {\r
         Status = GraphicsOutput->SetMode (GraphicsOutput, ModeNumber);\r
         if (!EFI_ERROR (Status)) {\r
-          gBS->FreePool (Info);\r
+          FreePool (Info);\r
           break;\r
         }\r
       }\r
-      gBS->FreePool (Info);\r
+      FreePool (Info);\r
     }\r
   }\r
 \r
@@ -842,7 +855,7 @@ EfiLocateHiiProtocol (
 //\r
 \r
 /**\r
-  Reset the text output device hardware and optionaly run diagnostics.\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
@@ -1090,27 +1103,12 @@ 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
@@ -1210,8 +1208,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
@@ -1381,7 +1381,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
@@ -1420,7 +1420,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
@@ -1460,7 +1460,7 @@ 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 enable it\r
   //\r
   This->SetCursorPosition (This, 0, 0);\r
 \r
@@ -1640,7 +1640,7 @@ 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
@@ -1720,7 +1720,7 @@ GetTextColors (
 }\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
@@ -1759,20 +1759,29 @@ 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
-  *(String + Count) = 0;\r
+  //\r
+  // Set the end character\r
+  //\r
+  *(String + Count) = L'\0';\r
 \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
+  // Get current foreground and background colors.\r
+  //\r
   GetTextColors (This, &FontInfo->ForegroundColor, &FontInfo->BackgroundColor);\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
@@ -1789,14 +1798,18 @@ DrawUnicodeWeightAtCursorN (
                          );\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
     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
-      SafeFreePool (Blt);\r
-      SafeFreePool (String);\r
+      FreePool (Blt);\r
+      FreePool (String);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
@@ -1839,15 +1852,21 @@ 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
@@ -1864,17 +1883,17 @@ EraseCursor (
   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
@@ -1929,7 +1948,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
@@ -1972,8 +1991,8 @@ EraseCursor (
   @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
@@ -1998,7 +2017,6 @@ InitializeGraphicsConsole (
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-\r
   return Status;\r
 }\r
 \r