]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/HiiDatabaseDxe/Font.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / Font.c
index 4d2fcc081b5cf4ee5a0c5f6d536b24cba5b48b93..ca63df168c9477e2a7e3cfa784981be0cbf98131 100644 (file)
@@ -2,14 +2,8 @@
 Implementation for EFI_HII_FONT_PROTOCOL.\r
 \r
 \r
-Copyright (c) 2007 - 2011, 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
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -275,7 +269,7 @@ GetGlyphBuffer (
   @param  RowWidth       The width of the text on the line, in pixels.\r
   @param  RowHeight      The height of the line, in pixels.\r
   @param  Transparent    If TRUE, the Background color is ignored and all\r
-                         "off" pixels in the character's drawn wil use the\r
+                         "off" pixels in the character's drawn will use the\r
                          pixel value from BltBuffer.\r
   @param  Origin         On input, points to the origin of the to be\r
                          displayed character, on output, points to the\r
@@ -304,14 +298,14 @@ NarrowGlyphToBlt (
 \r
   Height = EFI_GLYPH_HEIGHT;\r
   Width  = EFI_GLYPH_WIDTH;\r
-  \r
+\r
   //\r
   // Move position to the left-top corner of char.\r
   //\r
   Buffer = *Origin - EFI_GLYPH_HEIGHT * ImageWidth;\r
 \r
   //\r
-  // Char may be partially displayed when CLIP_X or CLIP_Y is not set. \r
+  // Char may be partially displayed when CLIP_X or CLIP_Y is not set.\r
   //\r
   if (RowHeight < Height) {\r
     Height = (UINT8) RowHeight;\r
@@ -351,7 +345,7 @@ NarrowGlyphToBlt (
   @param  RowWidth                The width of the text on the line, in pixels.\r
   @param  RowHeight               The height of the line, in pixels.\r
   @param  Transparent             If TRUE, the Background color is ignored and all\r
-                                  "off" pixels in the character's drawn wil use the\r
+                                  "off" pixels in the character's drawn will use the\r
                                   pixel value from BltBuffer.\r
   @param  Cell                    Points to EFI_HII_GLYPH_INFO structure.\r
   @param  Attributes              The attribute of incoming glyph in GlyphBuffer.\r
@@ -411,7 +405,7 @@ GlyphToBlt (
   // The glyph's upper left hand corner pixel is the most significant bit of the\r
   // first bitmap byte.\r
   //\r
-  for (Ypos = 0; Ypos < Cell->Height && ((UINTN) (Ypos + YposOffset) < RowHeight); Ypos++) {\r
+  for (Ypos = 0; Ypos < Cell->Height && (((UINT32) Ypos + YposOffset) < RowHeight); Ypos++) {\r
     OffsetY = BITMAP_LEN_1_BIT (Cell->Width, Ypos);\r
 \r
     //\r
@@ -419,7 +413,7 @@ GlyphToBlt (
     //\r
     for (Xpos = 0; Xpos < Cell->Width / 8; Xpos++) {\r
       Data  = *(GlyphBuffer + OffsetY + Xpos);\r
-      for (Index = 0; Index < 8 && ((UINTN) (Xpos * 8 + Index + Cell->OffsetX) < RowWidth); Index++) {\r
+      for (Index = 0; Index < 8 && (((UINT32) Xpos * 8 + Index + Cell->OffsetX) < RowWidth); Index++) {\r
         if ((Data & (1 << (8 - Index - 1))) != 0) {\r
           BltBuffer[Ypos * ImageWidth + Xpos * 8 + Index] = Foreground;\r
         } else {\r
@@ -435,7 +429,7 @@ GlyphToBlt (
       // There are some padding bits in this byte. Ignore them.\r
       //\r
       Data  = *(GlyphBuffer + OffsetY + Xpos);\r
-      for (Index = 0; Index < Cell->Width % 8 && ((UINTN) (Xpos * 8 + Index + Cell->OffsetX) < RowWidth); Index++) {\r
+      for (Index = 0; Index < Cell->Width % 8 && (((UINT32) Xpos * 8 + Index + Cell->OffsetX) < RowWidth); Index++) {\r
         if ((Data & (1 << (8 - Index - 1))) != 0) {\r
           BltBuffer[Ypos * ImageWidth + Xpos * 8 + Index] = Foreground;\r
         } else {\r
@@ -467,7 +461,7 @@ GlyphToBlt (
   @param  RowWidth                The width of the text on the line, in pixels.\r
   @param  RowHeight               The height of the line, in pixels.\r
   @param  Transparent             If TRUE, the Background color is ignored and all\r
-                                  "off" pixels in the character's drawn wil use the\r
+                                  "off" pixels in the character's drawn will use the\r
                                   pixel value from BltBuffer.\r
   @param  Cell                    Points to EFI_HII_GLYPH_INFO structure.\r
   @param  Attributes              The attribute of incoming glyph in GlyphBuffer.\r
@@ -593,7 +587,7 @@ GlyphToImage (
   @param  InputCell               Buffer which stores cell information of the\r
                                   encoded bitmap.\r
   @param  GlyphBuffer             Output the corresponding bitmap data of the found\r
-                                  block. It is the caller's responsiblity to free\r
+                                  block. It is the caller's responsibility to free\r
                                   this buffer.\r
   @param  Cell                    Output cell information of the encoded bitmap.\r
   @param  GlyphBufferLen          If not NULL, output the length of GlyphBuffer.\r
@@ -647,7 +641,7 @@ WriteOutputParam (
   @param  CharValue               Unicode character value, which identifies a glyph\r
                                   block.\r
   @param  GlyphBuffer             Output the corresponding bitmap data of the found\r
-                                  block. It is the caller's responsiblity to free\r
+                                  block. It is the caller's responsibility to free\r
                                   this buffer.\r
   @param  Cell                    Output cell information of the encoded bitmap.\r
   @param  GlyphBufferLen          If not NULL, output the length of GlyphBuffer.\r
@@ -685,7 +679,7 @@ FindGlyphBlock (
   ASSERT (FontPackage->Signature == HII_FONT_PACKAGE_SIGNATURE);\r
   BaseLine  = 0;\r
   MinOffsetY = 0;\r
-  \r
+\r
   if (CharValue == (CHAR16) (-1)) {\r
     //\r
     // Collect the cell information specified in font package fixed header.\r
@@ -704,10 +698,6 @@ FindGlyphBlock (
       (UINT8 *) FontPackage->FontPkgHdr + 3 * sizeof (UINT32),\r
       sizeof (EFI_HII_GLYPH_INFO)\r
       );\r
-    BaseLine = (UINT16) (LocalCell.Height + LocalCell.OffsetY);\r
-    if (MinOffsetY > LocalCell.OffsetY) {\r
-      MinOffsetY = LocalCell.OffsetY;\r
-    }\r
   }\r
 \r
   BlockPtr    = FontPackage->GlyphBlock;\r
@@ -757,12 +747,12 @@ FindGlyphBlock (
       break;\r
 \r
     case EFI_HII_GIBT_EXT1:\r
-      BlockPtr += *(BlockPtr + sizeof (EFI_HII_GLYPH_BLOCK) + sizeof (UINT8));\r
+      BlockPtr += *(UINT8*)((UINTN)BlockPtr + sizeof (EFI_HII_GLYPH_BLOCK) + sizeof (UINT8));\r
       break;\r
     case EFI_HII_GIBT_EXT2:\r
       CopyMem (\r
         &Length16,\r
-        BlockPtr + sizeof (EFI_HII_GLYPH_BLOCK) + sizeof (UINT8),\r
+        (UINT8*)((UINTN)BlockPtr + sizeof (EFI_HII_GLYPH_BLOCK) + sizeof (UINT8)),\r
         sizeof (UINT16)\r
         );\r
       BlockPtr += Length16;\r
@@ -770,7 +760,7 @@ FindGlyphBlock (
     case EFI_HII_GIBT_EXT4:\r
       CopyMem (\r
         &Length32,\r
-        BlockPtr + sizeof (EFI_HII_GLYPH_BLOCK) + sizeof (UINT8),\r
+        (UINT8*)((UINTN)BlockPtr + sizeof (EFI_HII_GLYPH_BLOCK) + sizeof (UINT8)),\r
         sizeof (UINT32)\r
         );\r
       BlockPtr += Length32;\r
@@ -793,7 +783,7 @@ FindGlyphBlock (
       BufferLen = BITMAP_LEN_1_BIT (LocalCell.Width, LocalCell.Height);\r
       if (CharCurrent == CharValue) {\r
         return WriteOutputParam (\r
-                 BlockPtr + sizeof (EFI_HII_GIBT_GLYPH_BLOCK) - sizeof (UINT8),\r
+                 (UINT8*)((UINTN)BlockPtr + sizeof (EFI_HII_GIBT_GLYPH_BLOCK) - sizeof (UINT8)),\r
                  BufferLen,\r
                  &LocalCell,\r
                  GlyphBuffer,\r
@@ -843,6 +833,14 @@ FindGlyphBlock (
       if (EFI_ERROR (Status)) {\r
         return Status;\r
       }\r
+      if (CharValue == (CHAR16) (-1)) {\r
+        if (BaseLine < DefaultCell.Height + DefaultCell.OffsetY) {\r
+          BaseLine = (UINT16) (DefaultCell.Height + DefaultCell.OffsetY);\r
+        }\r
+        if (MinOffsetY > DefaultCell.OffsetY) {\r
+          MinOffsetY = DefaultCell.OffsetY;\r
+        }\r
+      }\r
       BufferLen = BITMAP_LEN_1_BIT (DefaultCell.Width, DefaultCell.Height);\r
 \r
       if (CharCurrent == CharValue) {\r
@@ -865,6 +863,14 @@ FindGlyphBlock (
       if (EFI_ERROR (Status)) {\r
         return Status;\r
       }\r
+      if (CharValue == (CHAR16) (-1)) {\r
+        if (BaseLine < DefaultCell.Height + DefaultCell.OffsetY) {\r
+          BaseLine = (UINT16) (DefaultCell.Height + DefaultCell.OffsetY);\r
+        }\r
+        if (MinOffsetY > DefaultCell.OffsetY) {\r
+          MinOffsetY = DefaultCell.OffsetY;\r
+        }\r
+      }\r
       BufferLen = BITMAP_LEN_1_BIT (DefaultCell.Width, DefaultCell.Height);\r
       BlockPtr += sizeof (EFI_HII_GIBT_GLYPHS_DEFAULT_BLOCK) - sizeof (UINT8);\r
       for (Index = 0; Index < Length16; Index++) {\r
@@ -933,16 +939,18 @@ SaveFontName (
   )\r
 {\r
   UINTN         FontInfoLen;\r
+  UINTN         NameSize;\r
 \r
   ASSERT (FontName != NULL && FontInfo != NULL);\r
 \r
-  FontInfoLen = sizeof (EFI_FONT_INFO) - sizeof (CHAR16) + StrSize (FontName);\r
+  NameSize = StrSize (FontName);\r
+  FontInfoLen = sizeof (EFI_FONT_INFO) - sizeof (CHAR16) + NameSize;\r
   *FontInfo = (EFI_FONT_INFO *) AllocateZeroPool (FontInfoLen);\r
   if (*FontInfo == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  StrCpy ((*FontInfo)->FontName, FontName);\r
+  StrCpyS ((*FontInfo)->FontName, NameSize / sizeof (CHAR16), FontName);\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -971,6 +979,7 @@ GetSystemFont (
 {\r
   EFI_FONT_DISPLAY_INFO              *Info;\r
   UINTN                              InfoSize;\r
+  UINTN                              NameSize;\r
 \r
   if (Private == NULL || Private->Signature != HII_DATABASE_PRIVATE_DATA_SIGNATURE) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -982,7 +991,8 @@ GetSystemFont (
   //\r
   // The standard font always has the name "sysdefault".\r
   //\r
-  InfoSize = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (CHAR16) + StrSize (L"sysdefault");\r
+  NameSize = StrSize (L"sysdefault");\r
+  InfoSize = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (CHAR16) + NameSize;\r
   Info = (EFI_FONT_DISPLAY_INFO *) AllocateZeroPool (InfoSize);\r
   if (Info == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -993,7 +1003,7 @@ GetSystemFont (
   Info->FontInfoMask       = EFI_FONT_INFO_SYS_FONT | EFI_FONT_INFO_SYS_SIZE | EFI_FONT_INFO_SYS_STYLE;\r
   Info->FontInfo.FontStyle = 0;\r
   Info->FontInfo.FontSize  = EFI_GLYPH_HEIGHT;\r
-  StrCpy (Info->FontInfo.FontName, L"sysdefault");\r
+  StrCpyS (Info->FontInfo.FontName, NameSize / sizeof (CHAR16), L"sysdefault");\r
 \r
   *FontInfo = Info;\r
   if (FontInfoSize != NULL) {\r
@@ -1040,8 +1050,12 @@ IsSystemFontInfo (
     return TRUE;\r
   }\r
 \r
+  SystemDefault = NULL;\r
+  DefaultLen    = 0;\r
+\r
   Status = GetSystemFont (Private, &SystemDefault, &DefaultLen);\r
   ASSERT_EFI_ERROR (Status);\r
+  ASSERT ((SystemDefault != NULL) && (DefaultLen != 0));\r
 \r
   //\r
   // Record the system default info.\r
@@ -1079,8 +1093,8 @@ IsSystemFontInfo (
   }\r
   if ((StringInfo->FontInfoMask & EFI_FONT_INFO_SYS_FORE_COLOR) == 0) {\r
     if (CompareMem (\r
-          &StringInfo->ForegroundColor, \r
-          &SystemDefault->ForegroundColor, \r
+          &StringInfo->ForegroundColor,\r
+          &SystemDefault->ForegroundColor,\r
           sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)\r
           ) != 0) {\r
       goto Exit;\r
@@ -1088,8 +1102,8 @@ IsSystemFontInfo (
   }\r
   if ((StringInfo->FontInfoMask & EFI_FONT_INFO_SYS_BACK_COLOR) == 0) {\r
     if (CompareMem (\r
-          &StringInfo->BackgroundColor, \r
-          &SystemDefault->BackgroundColor, \r
+          &StringInfo->BackgroundColor,\r
+          &SystemDefault->BackgroundColor,\r
           sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)\r
           ) != 0) {\r
       goto Exit;\r
@@ -1123,7 +1137,7 @@ Exit:
   @param  FontHandle              On entry, Points to the font handle returned by a\r
                                   previous  call to GetFontInfo() or NULL to start\r
                                   with the first font.\r
-  @param  GlobalFontInfo          If not NULL, output the corresponding globa font\r
+  @param  GlobalFontInfo          If not NULL, output the corresponding global font\r
                                   info.\r
 \r
   @retval TRUE                    Existed\r
@@ -1152,7 +1166,7 @@ IsFontInfoExisted (
   ASSERT (FontInfo != NULL);\r
 \r
   //\r
-  // Matched flag represents an exactly match; VagueMatched1 repensents a RESIZE\r
+  // Matched flag represents an exactly match; VagueMatched1 represents a RESIZE\r
   // or RESTYLE match; VagueMatched2 represents a RESIZE | RESTYLE match.\r
   //\r
   Matched           = FALSE;\r
@@ -1228,7 +1242,7 @@ IsFontInfoExisted (
         }\r
         break;\r
       //\r
-      // If EFI_FONT_INFO_RESIZE is specified, then the sytem may attempt to\r
+      // If EFI_FONT_INFO_RESIZE is specified, then the system may attempt to\r
       // stretch or shrink a font to meet the size requested.\r
       //\r
       case EFI_FONT_INFO_ANY_FONT | EFI_FONT_INFO_RESIZE:\r
@@ -1440,6 +1454,7 @@ IsLineBreak (
     //\r
     // Mandatory line break characters, which force a line-break\r
     //\r
+    case 0x000A:\r
     case 0x000C:\r
     case 0x000D:\r
     case 0x2028:\r
@@ -1604,6 +1619,7 @@ HiiStringToImage (
   UINTN                               StrLength;\r
   EFI_GRAPHICS_OUTPUT_BLT_PIXEL       *RowBufferPtr;\r
   HII_GLOBAL_FONT_INFO                *GlobalFont;\r
+  UINT32                              PreInitBkgnd;\r
 \r
   //\r
   // Check incoming parameters.\r
@@ -1687,6 +1703,7 @@ HiiStringToImage (
   SysFontFlag   = IsSystemFontInfo (Private, (EFI_FONT_DISPLAY_INFO *) StringInfo, &SystemDefault, NULL);\r
 \r
   if (SysFontFlag) {\r
+    ASSERT (SystemDefault != NULL);\r
     FontInfo   = NULL;\r
     Height     = SystemDefault->FontInfo.FontSize;\r
     BaseLine   = SystemDefault->FontInfo.FontSize;\r
@@ -1722,15 +1739,15 @@ HiiStringToImage (
       goto Exit;\r
     }\r
   }\r
-  \r
+\r
   //\r
-  // Use the maxinum height of font as the base line.\r
-  // And, use the maxinum height as line height.\r
+  // Use the maximum height of font as the base line.\r
+  // And, use the maximum height as line height.\r
   //\r
   LineHeight     = Height;\r
   LastLineHeight = Height;\r
   BaseLineOffset = Height - BaseLine;\r
-  \r
+\r
   //\r
   // Parse the string to be displayed to drop some ignored characters.\r
   //\r
@@ -1760,7 +1777,7 @@ HiiStringToImage (
   }\r
   //\r
   // If EFI_HII_IGNORE_IF_NO_GLYPH is set, then characters which have no glyphs\r
-  // are not drawn. Otherwise they are replaced wth Unicode character 0xFFFD.\r
+  // are not drawn. Otherwise they are replaced with Unicode character 0xFFFD.\r
   //\r
   StringIn2  = AllocateZeroPool (StrSize (StringPtr));\r
   if (StringIn2 == NULL) {\r
@@ -1776,7 +1793,7 @@ HiiStringToImage (
       Index++;\r
       continue;\r
     }\r
-    \r
+\r
     Status = GetGlyphBuffer (Private, *StringPtr, FontInfo, &GlyphBuf[Index], &Cell[Index], &Attributes[Index]);\r
     if (Status == EFI_NOT_FOUND) {\r
       if ((Flags & EFI_HII_IGNORE_IF_NO_GLYPH) == EFI_HII_IGNORE_IF_NO_GLYPH) {\r
@@ -1819,7 +1836,13 @@ HiiStringToImage (
   //\r
   Image     = *Blt;\r
   BufferPtr = Image->Image.Bitmap + Image->Width * BltY + BltX;\r
-  ASSERT (Image->Height >= BltY);\r
+  if (Image->Height < BltY) {\r
+    //\r
+    // the top edge of the image should be in Image resolution scope.\r
+    //\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto Exit;\r
+  }\r
   MaxRowNum = (UINT16) ((Image->Height - BltY) / Height);\r
   if ((Image->Height - BltY) % Height != 0) {\r
     LastLineHeight = (Image->Height - BltY) % Height;\r
@@ -1898,7 +1921,7 @@ HiiStringToImage (
     // If this character is the last character of a row, we need not\r
     // draw its (AdvanceX - Width - OffsetX) for next character.\r
     //\r
-    LineWidth -= (UINTN) (Cell[Index].AdvanceX - Cell[Index].Width - Cell[Index].OffsetX);\r
+    LineWidth -= (Cell[Index].AdvanceX - Cell[Index].Width - Cell[Index].OffsetX);\r
 \r
     //\r
     // Clip the right-most character if cannot fit when EFI_HII_OUT_FLAG_CLEAN_X is set.\r
@@ -1921,8 +1944,8 @@ HiiStringToImage (
         //\r
         // Don't draw the last char on this row. And, don't draw the second last char (AdvanceX - Width - OffsetX).\r
         //\r
-        LineWidth -= (UINTN) (Cell[Index].Width + Cell[Index].OffsetX);\r
-        LineWidth -= (UINTN) (Cell[Index - 1].AdvanceX - Cell[Index - 1].Width - Cell[Index - 1].OffsetX);\r
+        LineWidth -= (Cell[Index].Width + Cell[Index].OffsetX);\r
+        LineWidth -= (Cell[Index - 1].AdvanceX - Cell[Index - 1].Width - Cell[Index - 1].OffsetX);\r
         RowInfo[RowIndex].EndIndex       = Index - 1;\r
         RowInfo[RowIndex].LineWidth      = LineWidth;\r
         RowInfo[RowIndex].LineHeight     = LineHeight;\r
@@ -1942,8 +1965,8 @@ HiiStringToImage (
     // opportunity prior to a character whose right-most extent would exceed Width.\r
     // Search the right-most line-break opportunity here.\r
     //\r
-    if ((Flags & EFI_HII_OUT_FLAG_WRAP) == EFI_HII_OUT_FLAG_WRAP && \r
-        (RowInfo[RowIndex].LineWidth + BltX > Image->Width || StringPtr[NextIndex] != 0) && \r
+    if ((Flags & EFI_HII_OUT_FLAG_WRAP) == EFI_HII_OUT_FLAG_WRAP &&\r
+        (RowInfo[RowIndex].LineWidth + BltX > Image->Width || StringPtr[NextIndex] != 0) &&\r
         !LineBreak) {\r
       if ((Flags & EFI_HII_IGNORE_LINE_BREAK) == 0) {\r
         LineWidth = RowInfo[RowIndex].LineWidth;\r
@@ -1979,7 +2002,7 @@ HiiStringToImage (
           if (Index1 == RowInfo[RowIndex].StartIndex) {\r
             LineWidth = 0;\r
           } else {\r
-            LineWidth -= (UINTN) (Cell[Index1 - 1].AdvanceX - Cell[Index1 - 1].Width - Cell[Index1 - 1].OffsetX);\r
+            LineWidth -= (Cell[Index1 - 1].AdvanceX - Cell[Index1 - 1].Width - Cell[Index1 - 1].OffsetX);\r
           }\r
           RowInfo[RowIndex].LineWidth = LineWidth;\r
         }\r
@@ -1996,8 +2019,8 @@ HiiStringToImage (
             //\r
             // Don't draw the last char on this row. And, don't draw the second last char (AdvanceX - Width - OffsetX).\r
             //\r
-            LineWidth -= (UINTN) (Cell[Index1].Width + Cell[Index1].OffsetX);\r
-            LineWidth -= (UINTN) (Cell[Index1 - 1].AdvanceX - Cell[Index1 - 1].Width - Cell[Index1 - 1].OffsetX);\r
+            LineWidth -= (Cell[Index1].Width + Cell[Index1].OffsetX);\r
+            LineWidth -= (Cell[Index1 - 1].AdvanceX - Cell[Index1 - 1].Width - Cell[Index1 - 1].OffsetX);\r
             RowInfo[RowIndex].EndIndex       = Index1 - 1;\r
             RowInfo[RowIndex].LineWidth      = LineWidth;\r
           } else {\r
@@ -2011,7 +2034,7 @@ HiiStringToImage (
         }\r
       }\r
     }\r
-    \r
+\r
     //\r
     // LineWidth can't exceed Image width.\r
     //\r
@@ -2027,12 +2050,17 @@ HiiStringToImage (
     if ((Flags & EFI_HII_DIRECT_TO_SCREEN) == EFI_HII_DIRECT_TO_SCREEN) {\r
       BltBuffer = NULL;\r
       if (RowInfo[RowIndex].LineWidth != 0) {\r
-        BltBuffer = AllocateZeroPool (RowInfo[RowIndex].LineWidth * RowInfo[RowIndex].LineHeight * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));\r
+        BltBuffer = AllocatePool (RowInfo[RowIndex].LineWidth * RowInfo[RowIndex].LineHeight * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));\r
         if (BltBuffer == NULL) {\r
           Status = EFI_OUT_OF_RESOURCES;\r
           goto Exit;\r
         }\r
         //\r
+        // Initialize the background color.\r
+        //\r
+        PreInitBkgnd = Background.Blue | Background.Green << 8 | Background.Red << 16;\r
+        SetMem32 (BltBuffer,RowInfo[RowIndex].LineWidth * RowInfo[RowIndex].LineHeight * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL),PreInitBkgnd);\r
+        //\r
         // Set BufferPtr to Origin by adding baseline to the starting position.\r
         //\r
         BufferPtr = BltBuffer + BaseLine * RowInfo[RowIndex].LineWidth;\r
@@ -2040,7 +2068,7 @@ HiiStringToImage (
       for (Index1 = RowInfo[RowIndex].StartIndex; Index1 <= RowInfo[RowIndex].EndIndex; Index1++) {\r
         if (RowInfo[RowIndex].LineWidth > 0 && RowInfo[RowIndex].LineWidth > LineOffset) {\r
           //\r
-          // Only BLT these character which have corrsponding glyph in font basebase.\r
+          // Only BLT these character which have corresponding glyph in font database.\r
           //\r
           GlyphToImage (\r
             GlyphBuf[Index1],\r
@@ -2057,7 +2085,7 @@ HiiStringToImage (
           );\r
         }\r
         if (ColumnInfoArray != NULL) {\r
-          if ((GlyphBuf[Index1] == NULL && Cell[Index1].AdvanceX == 0) \r
+          if ((GlyphBuf[Index1] == NULL && Cell[Index1].AdvanceX == 0)\r
               || RowInfo[RowIndex].LineWidth == 0) {\r
             *ColumnInfoArray = (UINTN) ~0;\r
           } else {\r
@@ -2085,12 +2113,12 @@ HiiStringToImage (
           FreePool (BltBuffer);\r
           goto Exit;\r
         }\r
-  \r
+\r
         FreePool (BltBuffer);\r
       }\r
     } else {\r
       //\r
-      // Save the starting position for calculate the starting postition of next row. \r
+      // Save the starting position for calculate the starting position of next row.\r
       //\r
       RowBufferPtr = BufferPtr;\r
       //\r
@@ -2100,7 +2128,7 @@ HiiStringToImage (
       for (Index1 = RowInfo[RowIndex].StartIndex; Index1 <= RowInfo[RowIndex].EndIndex; Index1++) {\r
         if (RowInfo[RowIndex].LineWidth > 0 && RowInfo[RowIndex].LineWidth > LineOffset) {\r
           //\r
-          // Only BLT these character which have corrsponding glyph in font basebase.\r
+          // Only BLT these character which have corresponding glyph in font database.\r
           //\r
           GlyphToImage (\r
             GlyphBuf[Index1],\r
@@ -2117,7 +2145,7 @@ HiiStringToImage (
           );\r
         }\r
         if (ColumnInfoArray != NULL) {\r
-          if ((GlyphBuf[Index1] == NULL && Cell[Index1].AdvanceX == 0) \r
+          if ((GlyphBuf[Index1] == NULL && Cell[Index1].AdvanceX == 0)\r
               || RowInfo[RowIndex].LineWidth == 0) {\r
             *ColumnInfoArray = (UINTN) ~0;\r
           } else {\r
@@ -2140,9 +2168,8 @@ HiiStringToImage (
 \r
 NextLine:\r
     //\r
-    // Recalculate the start point of X/Y axis to draw multi-lines with the order of top-to-down\r
+    // Recalculate the start point of Y axis to draw multi-lines with the order of top-to-down\r
     //\r
-    BltX = 0;\r
     BltY += RowInfo[RowIndex].LineHeight;\r
 \r
     RowIndex++;\r
@@ -2266,13 +2293,13 @@ Exit:
                                   when character display is normalized that some\r
                                   character cells overlap.\r
 \r
-  @retval EFI_SUCCESS             The string was successfully rendered.\r
-  @retval EFI_OUT_OF_RESOURCES    Unable to allocate an output buffer for\r
-                                  RowInfoArray or Blt.\r
+  @retval EFI_SUCCESS            The string was successfully rendered.\r
+  @retval EFI_OUT_OF_RESOURCES   Unable to allocate an output buffer for\r
+                                 RowInfoArray or Blt.\r
   @retval EFI_INVALID_PARAMETER  The Blt or PackageList was NULL.\r
   @retval EFI_INVALID_PARAMETER  Flags were invalid combination.\r
-  @retval EFI_NOT_FOUND         The specified PackageList is not in the Database or the stringid is not \r
-                          in the specified PackageList. \r
+  @retval EFI_NOT_FOUND          The specified PackageList is not in the Database or the string id is not\r
+                                 in the specified PackageList.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -2298,6 +2325,7 @@ HiiStringIdToImage (
   EFI_STRING                          String;\r
   UINTN                               StringSize;\r
   UINTN                               FontLen;\r
+  UINTN                               NameSize;\r
   EFI_FONT_INFO                       *StringFontInfo;\r
   EFI_FONT_DISPLAY_INFO               *NewStringInfo;\r
   CHAR8                               TempSupportedLanguages;\r
@@ -2358,11 +2386,11 @@ HiiStringIdToImage (
   if (EFI_ERROR (Status)) {\r
     goto Exit;\r
   }\r
\r
+\r
   if (Language == NULL) {\r
     Language = "";\r
   }\r
-  CurrentLanguage = GetEfiGlobalVariable (L"PlatformLang");\r
+  GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&CurrentLanguage, NULL);\r
   BestLanguage = GetBestLanguage (\r
                    SupportedLanguages,\r
                    FALSE,\r
@@ -2375,7 +2403,7 @@ HiiStringIdToImage (
     Status = EFI_NOT_FOUND;\r
     goto Exit;\r
   }\r
-    \r
+\r
   StringSize = MAX_STRING_LENGTH;\r
   String = (EFI_STRING) AllocateZeroPool (StringSize);\r
   if (String == NULL) {\r
@@ -2413,32 +2441,33 @@ HiiStringIdToImage (
   if (EFI_ERROR (Status)) {\r
     goto Exit;\r
   }\r
-    \r
+\r
   //\r
   // When StringInfo specifies that string will be output in the system default font and color,\r
-  // use particular stringfontinfo described in string package instead if exists. \r
+  // use particular stringfontinfo described in string package instead if exists.\r
   // StringFontInfo equals NULL means system default font attaches with the string block.\r
   //\r
   if (StringFontInfo != NULL && IsSystemFontInfo (Private, (EFI_FONT_DISPLAY_INFO *) StringInfo, NULL, NULL)) {\r
-    FontLen = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (CHAR16) + StrSize (StringFontInfo->FontName);\r
+    NameSize = StrSize (StringFontInfo->FontName);\r
+    FontLen = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (CHAR16) + NameSize;\r
     NewStringInfo = AllocateZeroPool (FontLen);\r
-    if (NewStringInfo == NULL) {      \r
+    if (NewStringInfo == NULL) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
       goto Exit;\r
     }\r
     NewStringInfo->FontInfoMask       = EFI_FONT_INFO_SYS_FORE_COLOR | EFI_FONT_INFO_SYS_BACK_COLOR;\r
     NewStringInfo->FontInfo.FontStyle = StringFontInfo->FontStyle;\r
-    NewStringInfo->FontInfo.FontSize  = StringFontInfo->FontSize;    \r
-    StrCpy (NewStringInfo->FontInfo.FontName, StringFontInfo->FontName);\r
-  \r
+    NewStringInfo->FontInfo.FontSize  = StringFontInfo->FontSize;\r
+    StrCpyS (NewStringInfo->FontInfo.FontName, NameSize / sizeof (CHAR16), StringFontInfo->FontName);\r
+\r
     Status = HiiStringToImage (\r
-               This, \r
-               Flags, \r
-               String, \r
-               NewStringInfo, \r
-               Blt, \r
-               BltX, \r
-               BltY, \r
+               This,\r
+               Flags,\r
+               String,\r
+               NewStringInfo,\r
+               Blt,\r
+               BltX,\r
+               BltY,\r
                RowInfoArray,\r
                RowInfoArraySize,\r
                ColumnInfoArray\r
@@ -2574,6 +2603,7 @@ HiiGetGlyph (
     Foreground = StringInfoOut->ForegroundColor;\r
     Background = StringInfoOut->BackgroundColor;\r
   } else {\r
+    ASSERT (SystemDefault != NULL);\r
     Foreground = SystemDefault->ForegroundColor;\r
     Background = SystemDefault->BackgroundColor;\r
   }\r
@@ -2604,7 +2634,7 @@ HiiGetGlyph (
     //\r
     BaseLine  = (UINT16) (Cell.Height + Cell.OffsetY);\r
     //\r
-    // Set BltBuffer to the position of Origin. \r
+    // Set BltBuffer to the position of Origin.\r
     //\r
     BltBuffer = Image->Image.Bitmap + (Cell.Height + Cell.OffsetY) * Image->Width - Cell.OffsetX;\r
     GlyphToImage (\r
@@ -2673,14 +2703,15 @@ Exit:
                                   with the  first font. On return, points to the\r
                                   returned font handle or points to NULL if there\r
                                   are no more matching fonts.\r
-  @param  StringInfoIn            Upon entry, points to the font to return\r
-                                  information about. \r
-                                  If NULL, then the information about the system default \r
-                                  font will be returned.\r
-  @param  StringInfoOut           Upon return, contains the matching font's\r
-                                  information.  If NULL, then no information is\r
-                                  returned. It's caller's responsibility to free\r
-                                  this buffer.\r
+  @param  StringInfoIn            Upon entry, points to the font to return information\r
+                                  about. If NULL, then the information about the system\r
+                                  default font will be returned.\r
+  @param  StringInfoOut           Upon return, contains the matching font's information.\r
+                                  If NULL, then no information is returned. This buffer\r
+                                  is allocated with a call to the Boot Service AllocatePool().\r
+                                  It is the caller's responsibility to call the Boot\r
+                                  Service FreePool() when the caller no longer requires\r
+                                  the contents of StringInfoOut.\r
   @param  String                  Points to the string which will be tested to\r
                                   determine  if all characters are available. If\r
                                   NULL, then any font  is acceptable.\r
@@ -2716,6 +2747,7 @@ HiiGetFontInfo (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  StringInfoOutLen = 0;\r
   FontInfo        = NULL;\r
   SystemDefault   = NULL;\r
   LocalFontHandle = NULL;\r
@@ -2762,7 +2794,7 @@ HiiGetFontInfo (
       goto Exit;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // StringInfoIn must not be NULL if it is not system default font info.\r
   //\r
@@ -2770,15 +2802,15 @@ HiiGetFontInfo (
   //\r
   // Check the font information mask to make sure it is valid.\r
   //\r
-  if (((StringInfoIn->FontInfoMask & (EFI_FONT_INFO_SYS_FONT  | EFI_FONT_INFO_ANY_FONT))  == \r
+  if (((StringInfoIn->FontInfoMask & (EFI_FONT_INFO_SYS_FONT  | EFI_FONT_INFO_ANY_FONT))  ==\r
        (EFI_FONT_INFO_SYS_FONT | EFI_FONT_INFO_ANY_FONT))   ||\r
-      ((StringInfoIn->FontInfoMask & (EFI_FONT_INFO_SYS_SIZE  | EFI_FONT_INFO_ANY_SIZE))  == \r
+      ((StringInfoIn->FontInfoMask & (EFI_FONT_INFO_SYS_SIZE  | EFI_FONT_INFO_ANY_SIZE))  ==\r
        (EFI_FONT_INFO_SYS_SIZE | EFI_FONT_INFO_ANY_SIZE))   ||\r
-      ((StringInfoIn->FontInfoMask & (EFI_FONT_INFO_SYS_STYLE | EFI_FONT_INFO_ANY_STYLE)) == \r
+      ((StringInfoIn->FontInfoMask & (EFI_FONT_INFO_SYS_STYLE | EFI_FONT_INFO_ANY_STYLE)) ==\r
        (EFI_FONT_INFO_SYS_STYLE | EFI_FONT_INFO_ANY_STYLE)) ||\r
-      ((StringInfoIn->FontInfoMask & (EFI_FONT_INFO_RESIZE    | EFI_FONT_INFO_ANY_SIZE))  == \r
-       (EFI_FONT_INFO_RESIZE | EFI_FONT_INFO_ANY_SIZE))     ||           \r
-      ((StringInfoIn->FontInfoMask & (EFI_FONT_INFO_RESTYLE   | EFI_FONT_INFO_ANY_STYLE)) == \r
+      ((StringInfoIn->FontInfoMask & (EFI_FONT_INFO_RESIZE    | EFI_FONT_INFO_ANY_SIZE))  ==\r
+       (EFI_FONT_INFO_RESIZE | EFI_FONT_INFO_ANY_SIZE))     ||\r
+      ((StringInfoIn->FontInfoMask & (EFI_FONT_INFO_RESTYLE   | EFI_FONT_INFO_ANY_STYLE)) ==\r
        (EFI_FONT_INFO_RESTYLE | EFI_FONT_INFO_ANY_STYLE))) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
@@ -2800,7 +2832,7 @@ HiiGetFontInfo (
 \r
   if ((StringInfoIn->FontInfoMask & EFI_FONT_INFO_SYS_SIZE) == EFI_FONT_INFO_SYS_SIZE) {\r
     InfoOut.FontInfo.FontSize = SystemDefault->FontInfo.FontSize;\r
-  } \r
+  }\r
   if ((StringInfoIn->FontInfoMask & EFI_FONT_INFO_SYS_STYLE) == EFI_FONT_INFO_SYS_STYLE) {\r
     InfoOut.FontInfo.FontStyle = SystemDefault->FontInfo.FontStyle;\r
   }\r
@@ -2810,7 +2842,7 @@ HiiGetFontInfo (
   if ((StringInfoIn->FontInfoMask & EFI_FONT_INFO_SYS_BACK_COLOR) == EFI_FONT_INFO_SYS_BACK_COLOR) {\r
     InfoOut.BackgroundColor = SystemDefault->BackgroundColor;\r
   }\r
-  \r
+\r
   ASSERT (FontInfo != NULL);\r
   FontInfo->FontSize  = InfoOut.FontInfo.FontSize;\r
   FontInfo->FontStyle = InfoOut.FontInfo.FontStyle;\r
@@ -2818,7 +2850,7 @@ HiiGetFontInfo (
   if (IsFontInfoExisted (Private, FontInfo, &InfoOut.FontInfoMask, LocalFontHandle, &GlobalFont)) {\r
     //\r
     // Test to guarantee all characters are available in the found font.\r
-    //    \r
+    //\r
     if (String != NULL) {\r
       StringIn = String;\r
       while (*StringIn != 0) {\r
@@ -2835,21 +2867,21 @@ HiiGetFontInfo (
     //\r
     if (StringInfoOut != NULL) {\r
       StringInfoOutLen = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (EFI_FONT_INFO) + GlobalFont->FontInfoSize;\r
-      *StringInfoOut   = (EFI_FONT_DISPLAY_INFO *) AllocateZeroPool (StringInfoOutLen);      \r
+      *StringInfoOut   = (EFI_FONT_DISPLAY_INFO *) AllocateZeroPool (StringInfoOutLen);\r
       if (*StringInfoOut == NULL) {\r
         Status = EFI_OUT_OF_RESOURCES;\r
         LocalFontHandle = NULL;\r
         goto Exit;\r
       }\r
-      \r
+\r
       CopyMem (*StringInfoOut, &InfoOut, sizeof (EFI_FONT_DISPLAY_INFO));\r
       CopyMem (&(*StringInfoOut)->FontInfo, GlobalFont->FontInfo, GlobalFont->FontInfoSize);\r
     }\r
-    \r
-    LocalFontHandle = GlobalFont->Entry.ForwardLink;    \r
+\r
+    LocalFontHandle = GlobalFont->Entry.ForwardLink;\r
     Status = EFI_SUCCESS;\r
     goto Exit;\r
-  }  \r
+  }\r
 \r
   Status = EFI_NOT_FOUND;\r
 \r