2 Mde UEFI library API implemention.
3 Print to StdErr or ConOut defined in EFI_SYSTEM_TABLE
5 Copyright (c) 2007, Intel Corporation<BR>
6 All rights reserved. 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
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.
16 #include "UefiLibInternal.h"
18 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 }
38 Internal function which prints a formatted Unicode string to the console output device
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().
48 @param Format Null-terminated Unicode format string.
49 @param Console The output console.
50 @param Marker VA_LIST marker for the variable argument list.
52 @return The number of Unicode characters in the produced
53 output buffer not including the Null-terminator.
57 IN CONST CHAR16
*Format
,
58 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*Console
,
66 ASSERT (Format
!= NULL
);
67 ASSERT (((UINTN
) Format
& BIT0
) == 0);
69 BufferSize
= (PcdGet32 (PcdUefiLibMaxPrintBufferSize
) + 1) * sizeof (CHAR16
);
71 Buffer
= (CHAR16
*) AllocatePool(BufferSize
);
72 ASSERT (Buffer
!= NULL
);
74 Return
= UnicodeVSPrint (Buffer
, BufferSize
, Format
, Marker
);
76 if (Console
!= NULL
&& Return
> 0) {
78 // To be extra safe make sure Console has been initialized
80 Console
->OutputString (Console
, Buffer
);
89 Prints a formatted Unicode string to the console output device specified by
90 ConOut defined in the EFI_SYSTEM_TABLE.
92 This function prints a formatted Unicode string to the console output device
93 specified by ConOut in EFI_SYSTEM_TABLE and returns the number of Unicode
94 characters that printed to ConOut. If the length of the formatted Unicode
95 string is greater than PcdUefiLibMaxPrintBufferSize, then only the first
96 PcdUefiLibMaxPrintBufferSize characters are sent to ConOut.
97 If Format is NULL, then ASSERT().
98 If Format is not aligned on a 16-bit boundary, then ASSERT().
100 @param Format Null-terminated Unicode format string.
101 @param ... Variable argument list whose contents are accessed based
102 on the format string specified by Format.
104 @return The number of Unicode characters in the produced
105 output buffer not including the Null-terminator.
111 IN CONST CHAR16
*Format
,
118 VA_START (Marker
, Format
);
120 Return
= InternalPrint (Format
, gST
->ConOut
, Marker
);
128 Prints a formatted Unicode string to the console output device specified by
129 StdErr defined in the EFI_SYSTEM_TABLE.
131 This function prints a formatted Unicode string to the console output device
132 specified by StdErr in EFI_SYSTEM_TABLE and returns the number of Unicode
133 characters that printed to StdErr. If the length of the formatted Unicode
134 string is greater than PcdUefiLibMaxPrintBufferSize, then only the first
135 PcdUefiLibMaxPrintBufferSize characters are sent to StdErr.
136 If Format is NULL, then ASSERT().
137 If Format is not aligned on a 16-bit boundary, then ASSERT().
139 @param Format Null-terminated Unicode format string.
140 @param ... Variable argument list whose contents are accessed based
141 on the format string specified by Format.
143 @return The number of Unicode characters in the produced
144 output buffer not including the Null-terminator.
149 IN CONST CHAR16
*Format
,
156 VA_START (Marker
, Format
);
158 Return
= InternalPrint( Format
, gST
->StdErr
, Marker
);
167 Internal function which prints a formatted ASCII string to the console output device
170 This function prints a formatted ASCII string to the console output device
171 specified by Console and returns the number of ASCII characters that printed
172 to it. If the length of the formatted ASCII string is greater than PcdUefiLibMaxPrintBufferSize,
173 then only the first PcdUefiLibMaxPrintBufferSize characters are sent to Console.
175 If Format is NULL, then ASSERT().
177 @param Format Null-terminated ASCII format string.
178 @param Console The output console.
179 @param Marker VA_LIST marker for the variable argument list.
181 @return The number of Unicode characters in the produced
182 output buffer not including the Null-terminator.
187 IN CONST CHAR8
*Format
,
188 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*Console
,
196 ASSERT (Format
!= NULL
);
198 BufferSize
= (PcdGet32 (PcdUefiLibMaxPrintBufferSize
) + 1) * sizeof (CHAR16
);
200 Buffer
= (CHAR16
*) AllocatePool(BufferSize
);
201 ASSERT (Buffer
!= NULL
);
203 Return
= UnicodeVSPrintAsciiFormat (Buffer
, BufferSize
, Format
, Marker
);
205 if (Console
!= NULL
) {
207 // To be extra safe make sure Console has been initialized
209 Console
->OutputString (Console
, Buffer
);
218 Prints a formatted ASCII string to the console output device specified by
219 ConOut defined in the EFI_SYSTEM_TABLE.
221 This function prints a formatted ASCII string to the console output device
222 specified by ConOut in EFI_SYSTEM_TABLE and returns the number of ASCII
223 characters that printed to ConOut. If the length of the formatted ASCII
224 string is greater than PcdUefiLibMaxPrintBufferSize, then only the first
225 PcdUefiLibMaxPrintBufferSize characters are sent to ConOut.
226 If Format is NULL, then ASSERT().
228 @param Format Null-terminated ASCII format string.
229 @param ... Variable argument list whose contents are accessed based
230 on the format string specified by Format.
232 @return The number of Ascii characters in the produced
233 output buffer not including the Null-terminator.
239 IN CONST CHAR8
*Format
,
245 ASSERT (Format
!= NULL
);
247 VA_START (Marker
, Format
);
249 Return
= AsciiInternalPrint( Format
, gST
->ConOut
, Marker
);
257 Prints a formatted ASCII string to the console output device specified by
258 StdErr defined in the EFI_SYSTEM_TABLE.
260 This function prints a formatted ASCII string to the console output device
261 specified by StdErr in EFI_SYSTEM_TABLE and returns the number of ASCII
262 characters that printed to StdErr. If the length of the formatted ASCII
263 string is greater than PcdUefiLibMaxPrintBufferSize, then only the first
264 PcdUefiLibMaxPrintBufferSize characters are sent to StdErr.
265 If Format is NULL, then ASSERT().
267 @param Format Null-terminated ASCII format string.
268 @param ... Variable argument list whose contents are accessed based
269 on the format string specified by Format.
271 @return The number of Ascii characters in the produced output
272 buffer not including the Null-terminator.
278 IN CONST CHAR8
*Format
,
285 ASSERT (Format
!= NULL
);
287 VA_START (Marker
, Format
);
289 Return
= AsciiInternalPrint( Format
, gST
->StdErr
, Marker
);
297 Internal function to print a formatted Unicode string to a graphics console device specified by
298 ConsoleOutputHandle defined in the EFI_SYSTEM_TABLE at the given (X,Y) coordinates.
300 This function prints a formatted Unicode string to the graphics console device
301 specified by ConsoleOutputHandle in EFI_SYSTEM_TABLE and returns the number of
302 Unicode characters printed. The EFI_HII_FONT_PROTOCOL is used to convert the
303 string to a bitmap using the glyphs registered with the
304 HII database. No wrapping is performed, so any portions of the string the fall
305 outside the active display region will not be displayed.
307 If a graphics console device is not associated with the ConsoleOutputHandle
308 defined in the EFI_SYSTEM_TABLE then no string is printed, and 0 is returned.
309 If the EFI_HII_FONT_PROTOCOL is not present in the handle database, then no
310 string is printed, and 0 is returned.
312 @param X X coordinate to print the string.
313 @param Y Y coordinate to print the string.
314 @param ForeGround The forground color of the string being printed. This is
315 an optional parameter that may be NULL. If it is NULL,
316 then the foreground color of the current ConOut device
317 in the EFI_SYSTEM_TABLE is used.
318 @param BackGround The background color of the string being printed. This is
319 an optional parameter that may be NULL. If it is NULL,
320 then the background color of the current ConOut device
321 in the EFI_SYSTEM_TABLE is used.
322 @param Buffer Null-terminated Unicode formatted string.
323 @param PrintNum The number of Unicode formatted string to be printed.
325 @return Number of Unicode Characters printed. Zero means no any character
326 displayed successfully.
330 InternalPrintGraphic (
333 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*Foreground
,
334 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*Background
,
341 CHAR16
*UnicodeWeight
;
342 UINT32 HorizontalResolution
;
343 UINT32 VerticalResolution
;
347 EFI_HII_FONT_PROTOCOL
*HiiFont
;
348 EFI_IMAGE_OUTPUT
*Blt
;
349 EFI_FONT_DISPLAY_INFO FontInfo
;
350 EFI_HII_ROW_INFO
*RowInfoArray
;
351 UINTN RowInfoArraySize
;
352 EFI_GRAPHICS_OUTPUT_PROTOCOL
*GraphicsOutput
;
353 EFI_UGA_DRAW_PROTOCOL
*UgaDraw
;
354 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*Sto
;
355 EFI_HANDLE ConsoleHandle
;
357 HorizontalResolution
= 0;
358 VerticalResolution
= 0;
361 ConsoleHandle
= gST
->ConsoleOutHandle
;
363 Status
= gBS
->HandleProtocol (
365 &gEfiGraphicsOutputProtocolGuid
,
366 (VOID
**) &GraphicsOutput
370 if (EFI_ERROR (Status
) && FeaturePcdGet (PcdUgaConsumeSupport
)) {
372 // If no GOP available, try to open UGA Draw protocol if supported.
374 GraphicsOutput
= NULL
;
376 Status
= gBS
->HandleProtocol (
378 &gEfiUgaDrawProtocolGuid
,
382 if (EFI_ERROR (Status
)) {
386 Status
= gBS
->HandleProtocol (
388 &gEfiSimpleTextOutProtocolGuid
,
392 if (EFI_ERROR (Status
)) {
396 if (GraphicsOutput
!= NULL
) {
397 HorizontalResolution
= GraphicsOutput
->Mode
->Info
->HorizontalResolution
;
398 VerticalResolution
= GraphicsOutput
->Mode
->Info
->VerticalResolution
;
399 } else if (FeaturePcdGet (PcdUgaConsumeSupport
)) {
400 UgaDraw
->GetMode (UgaDraw
, &HorizontalResolution
, &VerticalResolution
, &ColorDepth
, &RefreshRate
);
402 Status
= EFI_UNSUPPORTED
;
406 ASSERT ((HorizontalResolution
!= 0) && (VerticalResolution
!=0));
408 Status
= gBS
->LocateProtocol (&gEfiHiiFontProtocolGuid
, NULL
, (VOID
**) &HiiFont
);
409 if (EFI_ERROR (Status
)) {
413 UnicodeWeight
= Buffer
;
415 for (Index
= 0; UnicodeWeight
[Index
] != 0; Index
++) {
416 if (UnicodeWeight
[Index
] == CHAR_BACKSPACE
||
417 UnicodeWeight
[Index
] == CHAR_LINEFEED
||
418 UnicodeWeight
[Index
] == CHAR_CARRIAGE_RETURN
) {
419 UnicodeWeight
[Index
] = 0;
423 LineBufferLen
= sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
) * HorizontalResolution
* EFI_GLYPH_HEIGHT
;
424 if (EFI_GLYPH_WIDTH
* EFI_GLYPH_HEIGHT
* sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL
) * PrintNum
> LineBufferLen
) {
425 Status
= EFI_INVALID_PARAMETER
;
429 Blt
= (EFI_IMAGE_OUTPUT
*) AllocateZeroPool (sizeof (EFI_IMAGE_OUTPUT
));
430 ASSERT (Blt
!= NULL
);
432 Blt
->Width
= (UINT16
) (HorizontalResolution
);
433 Blt
->Height
= (UINT16
) (VerticalResolution
);
435 ZeroMem (&FontInfo
, sizeof (EFI_FONT_DISPLAY_INFO
));
437 if (Foreground
!= NULL
) {
438 CopyMem (&FontInfo
.ForegroundColor
, Foreground
, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
));
441 &FontInfo
.ForegroundColor
,
442 &mEfiColors
[Sto
->Mode
->Attribute
& 0x0f],
443 sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
)
446 if (Background
!= NULL
) {
447 CopyMem (&FontInfo
.BackgroundColor
, Background
, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
));
450 &FontInfo
.BackgroundColor
,
451 &mEfiColors
[Sto
->Mode
->Attribute
>> 4],
452 sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
)
456 if (GraphicsOutput
!= NULL
) {
457 Blt
->Image
.Screen
= GraphicsOutput
;
459 Status
= HiiFont
->StringToImage (
461 EFI_HII_IGNORE_IF_NO_GLYPH
| EFI_HII_DIRECT_TO_SCREEN
,
472 } else if (FeaturePcdGet (PcdUgaConsumeSupport
)) {
473 ASSERT (UgaDraw
!= NULL
);
475 Blt
->Image
.Bitmap
= AllocateZeroPool (Blt
->Width
* Blt
->Height
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
));
476 ASSERT (Blt
->Image
.Bitmap
!= NULL
);
480 // StringToImage only support blt'ing image to device using GOP protocol. If GOP is not supported in this platform,
481 // we ask StringToImage to print the string to blt buffer, then blt to device using UgaDraw.
483 Status
= HiiFont
->StringToImage (
485 EFI_HII_IGNORE_IF_NO_GLYPH
,
496 if (!EFI_ERROR (Status
)) {
497 ASSERT (RowInfoArray
!= NULL
);
499 // Line breaks are handled by caller of DrawUnicodeWeightAtCursorN, so the updated parameter RowInfoArraySize by StringToImage will
500 // always be 1 or 0 (if there is no valid Unicode Char can be printed). ASSERT here to make sure.
502 ASSERT (RowInfoArraySize
<= 1);
504 Status
= UgaDraw
->Blt (
506 (EFI_UGA_PIXEL
*) Blt
->Image
.Bitmap
,
507 EfiUgaBltBufferToVideo
,
512 RowInfoArray
[0].LineWidth
,
513 RowInfoArray
[0].LineHeight
,
514 Blt
->Width
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
)
518 FreePool (RowInfoArray
);
519 FreePool (Blt
->Image
.Bitmap
);
522 Status
= EFI_UNSUPPORTED
;
528 if (EFI_ERROR (Status
)) {
536 Prints a formatted Unicode string to a graphics console device specified by
537 ConsoleOutputHandle defined in the EFI_SYSTEM_TABLE at the given (X,Y) coordinates.
539 This function prints a formatted Unicode string to the graphics console device
540 specified by ConsoleOutputHandle in EFI_SYSTEM_TABLE and returns the number of
541 Unicode characters printed. If the length of the formatted Unicode string is
542 greater than PcdUefiLibMaxPrintBufferSize, then only the first
543 PcdUefiLibMaxPrintBufferSize characters are printed. The EFI_HII_FONT_PROTOCOL
544 is used to convert the string to a bitmap using the glyphs registered with the
545 HII database. No wrapping is performed, so any portions of the string the fall
546 outside the active display region will not be displayed.
548 If a graphics console device is not associated with the ConsoleOutputHandle
549 defined in the EFI_SYSTEM_TABLE then no string is printed, and 0 is returned.
550 If the EFI_HII_FONT_PROTOCOL is not present in the handle database, then no
551 string is printed, and 0 is returned.
552 If Format is NULL, then ASSERT().
553 If Format is not aligned on a 16-bit boundary, then ASSERT().
555 @param X X coordinate to print the string.
556 @param Y Y coordinate to print the string.
557 @param ForeGround The forground color of the string being printed. This is
558 an optional parameter that may be NULL. If it is NULL,
559 then the foreground color of the current ConOut device
560 in the EFI_SYSTEM_TABLE is used.
561 @param BackGround The background color of the string being printed. This is
562 an optional parameter that may be NULL. If it is NULL,
563 then the background color of the current ConOut device
564 in the EFI_SYSTEM_TABLE is used.
565 @param Format Null-terminated Unicode format string. See Print Library
566 for the supported format string syntax.
567 @param ... Variable argument list whose contents are accessed based on
568 the format string specified by Format.
570 @return The number of characters printed.
578 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*ForeGround
, OPTIONAL
579 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BackGround
, OPTIONAL
580 IN CONST CHAR16
*Format
,
590 ASSERT (Format
!= NULL
);
591 ASSERT (((UINTN
) Format
& BIT0
) == 0);
593 VA_START (Marker
, Format
);
595 BufferSize
= (PcdGet32 (PcdUefiLibMaxPrintBufferSize
) + 1) * sizeof (CHAR16
);
597 Buffer
= (CHAR16
*) AllocatePool (BufferSize
);
598 ASSERT (Buffer
!= NULL
);
600 PrintNum
= UnicodeVSPrint (Buffer
, BufferSize
, Format
, Marker
);
602 ReturnNum
= InternalPrintGraphic (X
, Y
, ForeGround
, BackGround
, Buffer
, PrintNum
);
610 Prints a formatted ASCII string to a graphics console device specified by
611 ConsoleOutputHandle defined in the EFI_SYSTEM_TABLE at the given (X,Y) coordinates.
613 This function prints a formatted ASCII string to the graphics console device
614 specified by ConsoleOutputHandle in EFI_SYSTEM_TABLE and returns the number of
615 Unicode characters printed. If the length of the formatted ASCII string is
616 greater than PcdUefiLibMaxPrintBufferSize, then only the first
617 PcdUefiLibMaxPrintBufferSize characters are printed. The EFI_HII_FONT_PROTOCOL
618 is used to convert the string to a bitmap using the glyphs registered with the
619 HII database. No wrapping is performed, so any portions of the string the fall
620 outside the active display region will not be displayed.
622 If a graphics console device is not associated with the ConsoleOutputHandle
623 defined in the EFI_SYSTEM_TABLE then no string is printed, and 0 is returned.
624 If the EFI_HII_FONT_PROTOCOL is not present in the handle database, then no
625 string is printed, and 0 is returned.
626 If Format is NULL, then ASSERT().
628 @param X X coordinate to print the string.
629 @param Y Y coordinate to print the string.
630 @param ForeGround The forground color of the string being printed. This is
631 an optional parameter that may be NULL. If it is NULL,
632 then the foreground color of the current ConOut device
633 in the EFI_SYSTEM_TABLE is used.
634 @param BackGround The background color of the string being printed. This is
635 an optional parameter that may be NULL. If it is NULL,
636 then the background color of the current ConOut device
637 in the EFI_SYSTEM_TABLE is used.
638 @param Format Null-terminated ASCII format string. See Print Library
639 for the supported format string syntax.
640 @param ... Variable argument list whose contents are accessed based on
641 the format string specified by Format.
643 @return The number of characters printed.
651 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*ForeGround
, OPTIONAL
652 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BackGround
, OPTIONAL
653 IN CONST CHAR8
*Format
,
663 ASSERT (Format
!= NULL
);
665 VA_START (Marker
, Format
);
667 BufferSize
= (PcdGet32 (PcdUefiLibMaxPrintBufferSize
) + 1) * sizeof (CHAR16
);
669 Buffer
= (CHAR16
*) AllocatePool (BufferSize
);
670 ASSERT (Buffer
!= NULL
);
672 PrintNum
= UnicodeSPrintAsciiFormat (Buffer
, BufferSize
, Format
, Marker
);
674 ReturnNum
= InternalPrintGraphic (X
, Y
, ForeGround
, BackGround
, Buffer
, PrintNum
);