1 /** @file
2   Mde UEFI library API implementation.
3   Print to StdErr or ConOut defined in EFI_SYSTEM_TABLE
4 
5   Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>
6   This program and the accompanying materials
7   are licensed and made available under the terms and conditions of the BSD License
8   which accompanies this distribution.  The full text of the license may be found at
9   http://opensource.org/licenses/bsd-license.php.
10 
11   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 #include "UefiLibInternal.h"
17 
18 GLOBAL_REMOVE_IF_UNREFERENCED EFI_GRAPHICS_OUTPUT_BLT_PIXEL mEfiColors[16] = {
19   { 0x00, 0x00, 0x00, 0x00 },
20   { 0x98, 0x00, 0x00, 0x00 },
21   { 0x00, 0x98, 0x00, 0x00 },
22   { 0x98, 0x98, 0x00, 0x00 },
23   { 0x00, 0x00, 0x98, 0x00 },
24   { 0x98, 0x00, 0x98, 0x00 },
25   { 0x00, 0x98, 0x98, 0x00 },
26   { 0x98, 0x98, 0x98, 0x00 },
27   { 0x10, 0x10, 0x10, 0x00 },
28   { 0xff, 0x10, 0x10, 0x00 },
29   { 0x10, 0xff, 0x10, 0x00 },
30   { 0xff, 0xff, 0x10, 0x00 },
31   { 0x10, 0x10, 0xff, 0x00 },
32   { 0xf0, 0x10, 0xff, 0x00 },
33   { 0x10, 0xff, 0xff, 0x00 },
34   { 0xff, 0xff, 0xff, 0x00 }
35 };
36 
37 /**
38   Internal function which prints a formatted Unicode string to the console output device
39   specified by Console
40 
41   This function prints a formatted Unicode string to the console output device
42   specified by Console and returns the number of Unicode characters that printed
43   to it.  If the length of the formatted Unicode string is greater than PcdUefiLibMaxPrintBufferSize,
44   then only the first PcdUefiLibMaxPrintBufferSize characters are sent to Console.
45   If Format is NULL, then ASSERT().
46   If Format is not aligned on a 16-bit boundary, then ASSERT().
47 
48   @param Format   A Null-terminated Unicode format string.
49   @param Console  The output console.
50   @param Marker   A VA_LIST marker for the variable argument list.
51 
52   @return The number of Unicode characters in the produced
53           output buffer, not including the Null-terminator.
54 **/
55 UINTN
InternalPrint(IN CONST CHAR16 * Format,IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * Console,IN VA_LIST Marker)56 InternalPrint (
57   IN  CONST CHAR16                     *Format,
58   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *Console,
59   IN  VA_LIST                          Marker
60   )
61 {
62   EFI_STATUS  Status;
63   UINTN       Return;
64   CHAR16      *Buffer;
65   UINTN       BufferSize;
66 
67   ASSERT (Format != NULL);
68   ASSERT (((UINTN) Format & BIT0) == 0);
69   ASSERT (Console != NULL);
70 
71   BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);
72 
73   Buffer = (CHAR16 *) AllocatePool(BufferSize);
74   ASSERT (Buffer != NULL);
75 
76   Return = UnicodeVSPrint (Buffer, BufferSize, Format, Marker);
77 
78   if (Console != NULL && Return > 0) {
79     //
80     // To be extra safe make sure Console has been initialized
81     //
82     Status = Console->OutputString (Console, Buffer);
83     if (EFI_ERROR (Status)) {
84       Return = 0;
85     }
86   }
87 
88   FreePool (Buffer);
89 
90   return Return;
91 }
92 
93 /**
94   Prints a formatted Unicode string to the console output device specified by
95   ConOut defined in the EFI_SYSTEM_TABLE.
96 
97   This function prints a formatted Unicode string to the console output device
98   specified by ConOut in EFI_SYSTEM_TABLE and returns the number of Unicode
99   characters that printed to ConOut.  If the length of the formatted Unicode
100   string is greater than PcdUefiLibMaxPrintBufferSize, then only the first
101   PcdUefiLibMaxPrintBufferSize characters are sent to ConOut.
102   If Format is NULL, then ASSERT().
103   If Format is not aligned on a 16-bit boundary, then ASSERT().
104   If gST->ConOut is NULL, then ASSERT().
105 
106   @param Format   A Null-terminated Unicode format string.
107   @param ...      A Variable argument list whose contents are accessed based
108                   on the format string specified by Format.
109 
110   @return The number of Unicode characters printed to ConOut.
111 
112 **/
113 UINTN
114 EFIAPI
Print(IN CONST CHAR16 * Format,...)115 Print (
116   IN CONST CHAR16  *Format,
117   ...
118   )
119 {
120   VA_LIST Marker;
121   UINTN   Return;
122 
123   VA_START (Marker, Format);
124 
125   Return = InternalPrint (Format, gST->ConOut, Marker);
126 
127   VA_END (Marker);
128 
129   return Return;
130 }
131 
132 /**
133   Prints a formatted Unicode string to the console output device specified by
134   StdErr defined in the EFI_SYSTEM_TABLE.
135 
136   This function prints a formatted Unicode string to the console output device
137   specified by StdErr in EFI_SYSTEM_TABLE and returns the number of Unicode
138   characters that printed to StdErr.  If the length of the formatted Unicode
139   string is greater than PcdUefiLibMaxPrintBufferSize, then only the first
140   PcdUefiLibMaxPrintBufferSize characters are sent to StdErr.
141   If Format is NULL, then ASSERT().
142   If Format is not aligned on a 16-bit boundary, then ASSERT().
143   If gST->StdErr is NULL, then ASSERT().
144 
145   @param Format   A Null-terminated Unicode format string.
146   @param ...      Variable argument list whose contents are accessed based
147                   on the format string specified by Format.
148 
149   @return The number of Unicode characters printed to StdErr.
150 
151 **/
152 UINTN
153 EFIAPI
ErrorPrint(IN CONST CHAR16 * Format,...)154 ErrorPrint (
155   IN CONST CHAR16  *Format,
156   ...
157   )
158 {
159   VA_LIST Marker;
160   UINTN   Return;
161 
162   VA_START (Marker, Format);
163 
164   Return = InternalPrint( Format, gST->StdErr, Marker);
165 
166   VA_END (Marker);
167 
168   return Return;
169 }
170 
171 
172 /**
173   Internal function which prints a formatted ASCII string to the console output device
174   specified by Console
175 
176   This function prints a formatted ASCII string to the console output device
177   specified by Console and returns the number of ASCII characters that printed
178   to it.  If the length of the formatted ASCII string is greater than PcdUefiLibMaxPrintBufferSize,
179   then only the first PcdUefiLibMaxPrintBufferSize characters are sent to Console.
180 
181   If Format is NULL, then ASSERT().
182 
183   @param Format   A Null-terminated ASCII format string.
184   @param Console  The output console.
185   @param Marker   VA_LIST marker for the variable argument list.
186 
187   @return The number of Unicode characters in the produced
188           output buffer not including the Null-terminator.
189 
190 **/
191 UINTN
AsciiInternalPrint(IN CONST CHAR8 * Format,IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * Console,IN VA_LIST Marker)192 AsciiInternalPrint (
193   IN  CONST CHAR8                      *Format,
194   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *Console,
195   IN  VA_LIST                          Marker
196   )
197 {
198   EFI_STATUS  Status;
199   UINTN       Return;
200   CHAR16      *Buffer;
201   UINTN       BufferSize;
202 
203   ASSERT (Format != NULL);
204   ASSERT (Console != NULL);
205 
206   BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);
207 
208   Buffer = (CHAR16 *) AllocatePool(BufferSize);
209   ASSERT (Buffer != NULL);
210 
211   Return = UnicodeVSPrintAsciiFormat (Buffer, BufferSize, Format, Marker);
212 
213   if (Console != NULL) {
214     //
215     // To be extra safe make sure Console has been initialized
216     //
217     Status = Console->OutputString (Console, Buffer);
218     if (EFI_ERROR (Status)) {
219       Return = 0;
220     }
221   }
222 
223   FreePool (Buffer);
224 
225   return Return;
226 }
227 
228 /**
229   Prints a formatted ASCII string to the console output device specified by
230   ConOut defined in the EFI_SYSTEM_TABLE.
231 
232   This function prints a formatted ASCII string to the console output device
233   specified by ConOut in EFI_SYSTEM_TABLE and returns the number of ASCII
234   characters that printed to ConOut.  If the length of the formatted ASCII
235   string is greater than PcdUefiLibMaxPrintBufferSize, then only the first
236   PcdUefiLibMaxPrintBufferSize characters are sent to ConOut.
237   If Format is NULL, then ASSERT().
238   If gST->ConOut is NULL, then ASSERT().
239 
240   @param Format   A Null-terminated ASCII format string.
241   @param ...      Variable argument list whose contents are accessed based
242                   on the format string specified by Format.
243 
244   @return The number of ASCII characters printed to ConOut.
245 
246 **/
247 UINTN
248 EFIAPI
AsciiPrint(IN CONST CHAR8 * Format,...)249 AsciiPrint (
250   IN CONST CHAR8  *Format,
251   ...
252   )
253 {
254   VA_LIST Marker;
255   UINTN   Return;
256   ASSERT (Format != NULL);
257 
258   VA_START (Marker, Format);
259 
260   Return = AsciiInternalPrint( Format, gST->ConOut, Marker);
261 
262   VA_END (Marker);
263 
264   return Return;
265 }
266 
267 /**
268   Prints a formatted ASCII string to the console output device specified by
269   StdErr defined in the EFI_SYSTEM_TABLE.
270 
271   This function prints a formatted ASCII string to the console output device
272   specified by StdErr in EFI_SYSTEM_TABLE and returns the number of ASCII
273   characters that printed to StdErr.  If the length of the formatted ASCII
274   string is greater than PcdUefiLibMaxPrintBufferSize, then only the first
275   PcdUefiLibMaxPrintBufferSize characters are sent to StdErr.
276   If Format is NULL, then ASSERT().
277   If gST->StdErr is NULL, then ASSERT().
278 
279   @param Format   A Null-terminated ASCII format string.
280   @param ...      Variable argument list whose contents are accessed based
281                   on the format string specified by Format.
282 
283   @return The number of ASCII characters printed to ConErr.
284 
285 **/
286 UINTN
287 EFIAPI
AsciiErrorPrint(IN CONST CHAR8 * Format,...)288 AsciiErrorPrint (
289   IN CONST CHAR8  *Format,
290   ...
291   )
292 {
293   VA_LIST Marker;
294   UINTN   Return;
295 
296   ASSERT (Format != NULL);
297 
298   VA_START (Marker, Format);
299 
300   Return = AsciiInternalPrint( Format, gST->StdErr, Marker);
301 
302   VA_END (Marker);
303 
304   return Return;
305 }
306 
307 /**
308   Internal function to print a formatted Unicode string to a graphics console device specified by
309   ConsoleOutputHandle defined in the EFI_SYSTEM_TABLE at the given (X,Y) coordinates.
310 
311   This function prints a formatted Unicode string to the graphics console device
312   specified by ConsoleOutputHandle in EFI_SYSTEM_TABLE and returns the number of
313   Unicode characters printed. The EFI_HII_FONT_PROTOCOL is used to convert the
314   string to a bitmap using the glyphs registered with the
315   HII database.  No wrapping is performed, so any portions of the string the fall
316   outside the active display region will not be displayed.
317 
318   If a graphics console device is not associated with the ConsoleOutputHandle
319   defined in the EFI_SYSTEM_TABLE then no string is printed, and 0 is returned.
320   If the EFI_HII_FONT_PROTOCOL is not present in the handle database, then no
321   string is printed, and 0 is returned.
322 
323   @param  PointX       An X coordinate to print the string.
324   @param  PointY       A Y coordinate to print the string.
325   @param  Foreground   The foreground color of the string being printed.  This is
326                        an optional parameter that may be NULL.  If it is NULL,
327                        then the foreground color of the current ConOut device
328                        in the EFI_SYSTEM_TABLE is used.
329   @param  Background   The background color of the string being printed.  This is
330                        an optional parameter that may be NULL.  If it is NULL,
331                        then the background color of the current ConOut device
332                        in the EFI_SYSTEM_TABLE is used.
333   @param  Buffer       A Null-terminated Unicode formatted string.
334   @param  PrintNum     The number of Unicode formatted string to be printed.
335 
336   @return  The number of Unicode Characters printed. Zero means no any character
337            displayed successfully.
338 
339 **/
340 UINTN
InternalPrintGraphic(IN UINTN PointX,IN UINTN PointY,IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL * Foreground,IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL * Background,IN CHAR16 * Buffer,IN UINTN PrintNum)341 InternalPrintGraphic (
342   IN UINTN                            PointX,
343   IN UINTN                            PointY,
344   IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL    *Foreground,
345   IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL    *Background,
346   IN CHAR16                           *Buffer,
347   IN UINTN                            PrintNum
348   )
349 {
350   EFI_STATUS                          Status;
351   UINT32                              HorizontalResolution;
352   UINT32                              VerticalResolution;
353   UINT32                              ColorDepth;
354   UINT32                              RefreshRate;
355   EFI_HII_FONT_PROTOCOL               *HiiFont;
356   EFI_IMAGE_OUTPUT                    *Blt;
357   EFI_FONT_DISPLAY_INFO               FontInfo;
358   EFI_HII_ROW_INFO                    *RowInfoArray;
359   UINTN                               RowInfoArraySize;
360   EFI_GRAPHICS_OUTPUT_PROTOCOL        *GraphicsOutput;
361   EFI_UGA_DRAW_PROTOCOL               *UgaDraw;
362   EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL     *Sto;
363   EFI_HANDLE                          ConsoleHandle;
364   UINTN                               Width;
365   UINTN                               Height;
366   UINTN                               Delta;
367 
368   HorizontalResolution  = 0;
369   VerticalResolution    = 0;
370   Blt                   = NULL;
371   RowInfoArray          = NULL;
372 
373   ConsoleHandle = gST->ConsoleOutHandle;
374 
375   ASSERT( ConsoleHandle != NULL);
376 
377   Status = gBS->HandleProtocol (
378                   ConsoleHandle,
379                   &gEfiGraphicsOutputProtocolGuid,
380                   (VOID **) &GraphicsOutput
381                   );
382 
383   UgaDraw = NULL;
384   if (EFI_ERROR (Status) && FeaturePcdGet (PcdUgaConsumeSupport)) {
385     //
386     // If no GOP available, try to open UGA Draw protocol if supported.
387     //
388     GraphicsOutput = NULL;
389 
390     Status = gBS->HandleProtocol (
391                     ConsoleHandle,
392                     &gEfiUgaDrawProtocolGuid,
393                     (VOID **) &UgaDraw
394                     );
395   }
396   if (EFI_ERROR (Status)) {
397     goto Error;
398   }
399 
400   Status = gBS->HandleProtocol (
401                   ConsoleHandle,
402                   &gEfiSimpleTextOutProtocolGuid,
403                   (VOID **) &Sto
404                   );
405 
406   if (EFI_ERROR (Status)) {
407     goto Error;
408   }
409 
410   if (GraphicsOutput != NULL) {
411     HorizontalResolution = GraphicsOutput->Mode->Info->HorizontalResolution;
412     VerticalResolution = GraphicsOutput->Mode->Info->VerticalResolution;
413   } else if (UgaDraw != NULL && FeaturePcdGet (PcdUgaConsumeSupport)) {
414     UgaDraw->GetMode (UgaDraw, &HorizontalResolution, &VerticalResolution, &ColorDepth, &RefreshRate);
415   } else {
416     goto Error;
417   }
418 
419   ASSERT ((HorizontalResolution != 0) && (VerticalResolution !=0));
420 
421   Status = gBS->LocateProtocol (&gEfiHiiFontProtocolGuid, NULL, (VOID **) &HiiFont);
422   if (EFI_ERROR (Status)) {
423     goto Error;
424   }
425 
426   Blt = (EFI_IMAGE_OUTPUT *) AllocateZeroPool (sizeof (EFI_IMAGE_OUTPUT));
427   ASSERT (Blt != NULL);
428 
429   Blt->Width        = (UINT16) (HorizontalResolution);
430   Blt->Height       = (UINT16) (VerticalResolution);
431 
432   ZeroMem (&FontInfo, sizeof (EFI_FONT_DISPLAY_INFO));
433 
434   if (Foreground != NULL) {
435     CopyMem (&FontInfo.ForegroundColor, Foreground, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
436   } else {
437     CopyMem (
438       &FontInfo.ForegroundColor,
439       &mEfiColors[Sto->Mode->Attribute & 0x0f],
440       sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
441       );
442   }
443   if (Background != NULL) {
444     CopyMem (&FontInfo.BackgroundColor, Background, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
445   } else {
446     CopyMem (
447       &FontInfo.BackgroundColor,
448       &mEfiColors[Sto->Mode->Attribute >> 4],
449       sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
450       );
451   }
452 
453   if (GraphicsOutput != NULL) {
454     Blt->Image.Screen = GraphicsOutput;
455 
456     Status = HiiFont->StringToImage (
457                          HiiFont,
458                          EFI_HII_IGNORE_IF_NO_GLYPH | EFI_HII_OUT_FLAG_CLIP |
459                          EFI_HII_OUT_FLAG_CLIP_CLEAN_X | EFI_HII_OUT_FLAG_CLIP_CLEAN_Y |
460                          EFI_HII_IGNORE_LINE_BREAK | EFI_HII_DIRECT_TO_SCREEN,
461                          Buffer,
462                          &FontInfo,
463                          &Blt,
464                          PointX,
465                          PointY,
466                          &RowInfoArray,
467                          &RowInfoArraySize,
468                          NULL
469                          );
470     if (EFI_ERROR (Status)) {
471       goto Error;
472     }
473 
474   } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
475     ASSERT (UgaDraw!= NULL);
476 
477     Blt->Image.Bitmap = AllocateZeroPool (Blt->Width * Blt->Height * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
478     ASSERT (Blt->Image.Bitmap != NULL);
479 
480     //
481     //  StringToImage only support blt'ing image to device using GOP protocol. If GOP is not supported in this platform,
482     //  we ask StringToImage to print the string to blt buffer, then blt to device using UgaDraw.
483     //
484     Status = HiiFont->StringToImage (
485                          HiiFont,
486                          EFI_HII_IGNORE_IF_NO_GLYPH | EFI_HII_OUT_FLAG_CLIP |
487                          EFI_HII_OUT_FLAG_CLIP_CLEAN_X | EFI_HII_OUT_FLAG_CLIP_CLEAN_Y |
488                          EFI_HII_IGNORE_LINE_BREAK,
489                          Buffer,
490                          &FontInfo,
491                          &Blt,
492                          PointX,
493                          PointY,
494                          &RowInfoArray,
495                          &RowInfoArraySize,
496                          NULL
497                          );
498 
499     if (!EFI_ERROR (Status)) {
500       ASSERT (RowInfoArray != NULL);
501       //
502       // Explicit Line break characters are ignored, so the updated parameter RowInfoArraySize by StringToImage will
503       // always be 1 or 0 (if there is no valid Unicode Char can be printed). ASSERT here to make sure.
504       //
505       ASSERT (RowInfoArraySize <= 1);
506 
507       if (RowInfoArraySize != 0) {
508         Width  = RowInfoArray[0].LineWidth;
509         Height = RowInfoArray[0].LineHeight;
510         Delta  = Blt->Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
511       } else {
512         Width  = 0;
513         Height = 0;
514         Delta  = 0;
515       }
516       Status = UgaDraw->Blt (
517                           UgaDraw,
518                           (EFI_UGA_PIXEL *) Blt->Image.Bitmap,
519                           EfiUgaBltBufferToVideo,
520                           PointX,
521                           PointY,
522                           PointX,
523                           PointY,
524                           Width,
525                           Height,
526                           Delta
527                           );
528     } else {
529       goto Error;
530     }
531     FreePool (Blt->Image.Bitmap);
532   } else {
533     goto Error;
534   }
535   //
536   // Calculate the number of actual printed characters
537   //
538   if (RowInfoArraySize != 0) {
539     PrintNum = RowInfoArray[0].EndIndex - RowInfoArray[0].StartIndex + 1;
540   } else {
541     PrintNum = 0;
542   }
543 
544   FreePool (RowInfoArray);
545   FreePool (Blt);
546   return PrintNum;
547 
548 Error:
549   if (Blt != NULL) {
550     FreePool (Blt);
551   }
552   return 0;
553 }
554 
555 /**
556   Prints a formatted Unicode string to a graphics console device specified by
557   ConsoleOutputHandle defined in the EFI_SYSTEM_TABLE at the given (X,Y) coordinates.
558 
559   This function prints a formatted Unicode string to the graphics console device
560   specified by ConsoleOutputHandle in EFI_SYSTEM_TABLE and returns the number of
561   Unicode characters displayed, not including partial characters that may be clipped
562   by the right edge of the display.  If the length of the formatted Unicode string is
563   greater than PcdUefiLibMaxPrintBufferSize, then at most the first
564   PcdUefiLibMaxPrintBufferSize characters are printed.The EFI_HII_FONT_PROTOCOL
565   StringToImage() service is used to convert the string to a bitmap using the glyphs
566   registered with the HII database. No wrapping is performed, so any portions of the
567   string the fall outside the active display region will not be displayed. Please see
568   Section 27.2.6 of the UEFI Specification for a description of the supported string
569   format including the set of control codes supported by the StringToImage() service.
570 
571   If a graphics console device is not associated with the ConsoleOutputHandle
572   defined in the EFI_SYSTEM_TABLE then no string is printed, and 0 is returned.
573   If the EFI_HII_FONT_PROTOCOL is not present in the handle database, then no
574   string is printed, and 0 is returned.
575   If Format is NULL, then ASSERT().
576   If Format is not aligned on a 16-bit boundary, then ASSERT().
577   If gST->ConsoleOutputHandle is NULL, then ASSERT().
578 
579   @param  PointX       An X coordinate to print the string.
580   @param  PointY       A Y coordinate to print the string.
581   @param  ForeGround   The foreground color of the string being printed.  This is
582                        an optional parameter that may be NULL.  If it is NULL,
583                        then the foreground color of the current ConOut device
584                        in the EFI_SYSTEM_TABLE is used.
585   @param  BackGround   The background color of the string being printed.  This is
586                        an optional parameter that may be NULL.  If it is NULL,
587                        then the background color of the current ConOut device
588                        in the EFI_SYSTEM_TABLE is used.
589   @param  Format       A Null-terminated Unicode format string.  See Print Library
590                        for the supported format string syntax.
591   @param  ...          A Variable argument list whose contents are accessed based on
592                        the format string specified by Format.
593 
594   @return  The number of Unicode characters printed.
595 
596 **/
597 UINTN
598 EFIAPI
PrintXY(IN UINTN PointX,IN UINTN PointY,IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL * ForeGround,OPTIONAL IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL * BackGround,OPTIONAL IN CONST CHAR16 * Format,...)599 PrintXY (
600   IN UINTN                            PointX,
601   IN UINTN                            PointY,
602   IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL    *ForeGround, OPTIONAL
603   IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL    *BackGround, OPTIONAL
604   IN CONST CHAR16                     *Format,
605   ...
606   )
607 {
608   VA_LIST                             Marker;
609   CHAR16                              *Buffer;
610   UINTN                               BufferSize;
611   UINTN                               PrintNum;
612   UINTN                               ReturnNum;
613 
614   ASSERT (Format != NULL);
615   ASSERT (((UINTN) Format & BIT0) == 0);
616 
617   VA_START (Marker, Format);
618 
619   BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);
620 
621   Buffer = (CHAR16 *) AllocatePool (BufferSize);
622   ASSERT (Buffer != NULL);
623 
624   PrintNum = UnicodeVSPrint (Buffer, BufferSize, Format, Marker);
625 
626   VA_END (Marker);
627 
628   ReturnNum = InternalPrintGraphic (PointX, PointY, ForeGround, BackGround, Buffer, PrintNum);
629 
630   FreePool (Buffer);
631 
632   return ReturnNum;
633 }
634 
635 /**
636   Prints a formatted ASCII string to a graphics console device specified by
637   ConsoleOutputHandle defined in the EFI_SYSTEM_TABLE at the given (X,Y) coordinates.
638 
639   This function prints a formatted ASCII string to the graphics console device
640   specified by ConsoleOutputHandle in EFI_SYSTEM_TABLE and returns the number of
641   ASCII characters displayed, not including partial characters that may be clipped
642   by the right edge of the display.  If the length of the formatted ASCII string is
643   greater than PcdUefiLibMaxPrintBufferSize, then at most the first
644   PcdUefiLibMaxPrintBufferSize characters are printed.The EFI_HII_FONT_PROTOCOL
645   StringToImage() service is used to convert the string to a bitmap using the glyphs
646   registered with the HII database. No wrapping is performed, so any portions of the
647   string the fall outside the active display region will not be displayed. Please see
648   Section 27.2.6 of the UEFI Specification for a description of the supported string
649   format including the set of control codes supported by the StringToImage() service.
650 
651   If a graphics console device is not associated with the ConsoleOutputHandle
652   defined in the EFI_SYSTEM_TABLE then no string is printed, and 0 is returned.
653   If the EFI_HII_FONT_PROTOCOL is not present in the handle database, then no
654   string is printed, and 0 is returned.
655   If Format is NULL, then ASSERT().
656   If gST->ConsoleOutputHandle is NULL, then ASSERT().
657 
658   @param  PointX       An X coordinate to print the string.
659   @param  PointY       A Y coordinate to print the string.
660   @param  ForeGround   The foreground color of the string being printed.  This is
661                        an optional parameter that may be NULL.  If it is NULL,
662                        then the foreground color of the current ConOut device
663                        in the EFI_SYSTEM_TABLE is used.
664   @param  BackGround   The background color of the string being printed.  This is
665                        an optional parameter that may be NULL.  If it is NULL,
666                        then the background color of the current ConOut device
667                        in the EFI_SYSTEM_TABLE is used.
668   @param  Format       A Null-terminated ASCII format string.  See Print Library
669                        for the supported format string syntax.
670   @param  ...          Variable argument list whose contents are accessed based on
671                        the format string specified by Format.
672 
673   @return  The number of ASCII characters printed.
674 
675 **/
676 UINTN
677 EFIAPI
AsciiPrintXY(IN UINTN PointX,IN UINTN PointY,IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL * ForeGround,OPTIONAL IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL * BackGround,OPTIONAL IN CONST CHAR8 * Format,...)678 AsciiPrintXY (
679   IN UINTN                            PointX,
680   IN UINTN                            PointY,
681   IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL    *ForeGround, OPTIONAL
682   IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL    *BackGround, OPTIONAL
683   IN CONST CHAR8                      *Format,
684   ...
685   )
686 {
687   VA_LIST                             Marker;
688   CHAR16                              *Buffer;
689   UINTN                               BufferSize;
690   UINTN                               PrintNum;
691   UINTN                               ReturnNum;
692 
693   ASSERT (Format != NULL);
694 
695   VA_START (Marker, Format);
696 
697   BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);
698 
699   Buffer = (CHAR16 *) AllocatePool (BufferSize);
700   ASSERT (Buffer != NULL);
701 
702   PrintNum = UnicodeSPrintAsciiFormat (Buffer, BufferSize, Format, Marker);
703 
704   VA_END (Marker);
705 
706   ReturnNum = InternalPrintGraphic (PointX, PointY, ForeGround, BackGround, Buffer, PrintNum);
707 
708   FreePool (Buffer);
709 
710   return ReturnNum;
711 }
712 
713 /**
714   Appends a formatted Unicode string to a Null-terminated Unicode string
715 
716   This function appends a formatted Unicode string to the Null-terminated
717   Unicode string specified by String.   String is optional and may be NULL.
718   Storage for the formatted Unicode string returned is allocated using
719   AllocatePool().  The pointer to the appended string is returned.  The caller
720   is responsible for freeing the returned string.
721 
722   If String is not NULL and not aligned on a 16-bit boundary, then ASSERT().
723   If FormatString is NULL, then ASSERT().
724   If FormatString is not aligned on a 16-bit boundary, then ASSERT().
725 
726   @param[in] String         A Null-terminated Unicode string.
727   @param[in] FormatString   A Null-terminated Unicode format string.
728   @param[in]  Marker        VA_LIST marker for the variable argument list.
729 
730   @retval NULL    There was not enough available memory.
731   @return         Null-terminated Unicode string is that is the formatted
732                   string appended to String.
733 **/
734 CHAR16*
735 EFIAPI
CatVSPrint(IN CHAR16 * String,OPTIONAL IN CONST CHAR16 * FormatString,IN VA_LIST Marker)736 CatVSPrint (
737   IN  CHAR16  *String, OPTIONAL
738   IN  CONST CHAR16  *FormatString,
739   IN  VA_LIST       Marker
740   )
741 {
742   UINTN   CharactersRequired;
743   UINTN   SizeRequired;
744   CHAR16  *BufferToReturn;
745   VA_LIST ExtraMarker;
746 
747   VA_COPY (ExtraMarker, Marker);
748   CharactersRequired = SPrintLength(FormatString, ExtraMarker);
749   VA_END (ExtraMarker);
750 
751   if (String != NULL) {
752     SizeRequired = StrSize(String) + (CharactersRequired * sizeof(CHAR16));
753   } else {
754     SizeRequired = sizeof(CHAR16) + (CharactersRequired * sizeof(CHAR16));
755   }
756 
757   BufferToReturn = AllocatePool(SizeRequired);
758 
759   if (BufferToReturn == NULL) {
760     return NULL;
761   } else {
762     BufferToReturn[0] = L'\0';
763   }
764 
765   if (String != NULL) {
766     StrCpyS(BufferToReturn, SizeRequired / sizeof(CHAR16), String);
767   }
768 
769   UnicodeVSPrint(BufferToReturn + StrLen(BufferToReturn), (CharactersRequired+1) * sizeof(CHAR16), FormatString, Marker);
770 
771   ASSERT(StrSize(BufferToReturn)==SizeRequired);
772 
773   return (BufferToReturn);
774 }
775 
776 /**
777   Appends a formatted Unicode string to a Null-terminated Unicode string
778 
779   This function appends a formatted Unicode string to the Null-terminated
780   Unicode string specified by String.   String is optional and may be NULL.
781   Storage for the formatted Unicode string returned is allocated using
782   AllocatePool().  The pointer to the appended string is returned.  The caller
783   is responsible for freeing the returned string.
784 
785   If String is not NULL and not aligned on a 16-bit boundary, then ASSERT().
786   If FormatString is NULL, then ASSERT().
787   If FormatString is not aligned on a 16-bit boundary, then ASSERT().
788 
789   @param[in] String         A Null-terminated Unicode string.
790   @param[in] FormatString   A Null-terminated Unicode format string.
791   @param[in] ...            The variable argument list whose contents are
792                             accessed based on the format string specified by
793                             FormatString.
794 
795   @retval NULL    There was not enough available memory.
796   @return         Null-terminated Unicode string is that is the formatted
797                   string appended to String.
798 **/
799 CHAR16 *
800 EFIAPI
CatSPrint(IN CHAR16 * String,OPTIONAL IN CONST CHAR16 * FormatString,...)801 CatSPrint (
802   IN  CHAR16  *String, OPTIONAL
803   IN  CONST CHAR16  *FormatString,
804   ...
805   )
806 {
807   VA_LIST   Marker;
808   CHAR16    *NewString;
809 
810   VA_START (Marker, FormatString);
811   NewString = CatVSPrint(String, FormatString, Marker);
812   VA_END (Marker);
813   return NewString;
814 }
815 
816