2 Library supports diplaying graphical splash screen,
3 locking of keyboard input and printing character on
4 screen. These basic graphics operations are based on UEFI HII,
5 Graphics Output protocol or UGA Draw protocol.
7 Copyright (c) 2006 - 2008, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Protocol/SimpleTextOut.h>
22 #include <Protocol/OEMBadging.h>
23 #include <Protocol/ConsoleControl.h>
24 #include <Protocol/GraphicsOutput.h>
25 #include <Protocol/UgaDraw.h>
26 #include <Protocol/HiiFont.h>
27 #include <Protocol/HiiImage.h>
31 #include <Library/GraphicsLib.h>
32 #include <Library/PrintLib.h>
33 #include <Library/BaseLib.h>
34 #include <Library/MemoryAllocationLib.h>
35 #include <Library/UefiBootServicesTableLib.h>
36 #include <Library/DebugLib.h>
37 #include <Library/BaseMemoryLib.h>
38 #include <Library/DxeServicesLib.h>
39 #include <Library/PcdLib.h>
42 Convert a *.BMP graphics image to a GOP blt buffer. If a NULL Blt buffer
43 is passed in a GopBlt buffer will be allocated by this routine. If a GopBlt
44 buffer is passed in it will be used if it is big enough.
46 @param BmpImage Pointer to BMP file
47 @param BmpImageSize Number of bytes in BmpImage
48 @param GopBlt Buffer containing GOP version of BmpImage.
49 @param GopBltSize Size of GopBlt in bytes.
50 @param PixelHeight Height of GopBlt/BmpImage in pixels
51 @param PixelWidth Width of GopBlt/BmpImage in pixels
53 @retval EFI_SUCCESS GopBlt and GopBltSize are returned.
54 @retval EFI_UNSUPPORTED BmpImage is not a valid *.BMP image
55 @retval EFI_BUFFER_TOO_SMALL The passed in GopBlt buffer is not big enough.
56 GopBltSize will contain the required size.
57 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate.
64 IN UINTN BmpImageSize
,
66 IN OUT UINTN
*GopBltSize
,
67 OUT UINTN
*PixelHeight
,
73 BMP_IMAGE_HEADER
*BmpHeader
;
74 BMP_COLOR_MAP
*BmpColorMap
;
75 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
;
76 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*Blt
;
84 BmpHeader
= (BMP_IMAGE_HEADER
*) BmpImage
;
86 if (BmpHeader
->CharB
!= 'B' || BmpHeader
->CharM
!= 'M') {
87 return EFI_UNSUPPORTED
;
91 // Doesn't support compress.
93 if (BmpHeader
->CompressionType
!= 0) {
94 return EFI_UNSUPPORTED
;
98 // Calculate Color Map offset in the image.
101 BmpColorMap
= (BMP_COLOR_MAP
*) (Image
+ sizeof (BMP_IMAGE_HEADER
));
104 // Calculate graphics image data address in the image
106 Image
= ((UINT8
*) BmpImage
) + BmpHeader
->ImageOffset
;
110 // Calculate the BltBuffer needed size.
112 BltBufferSize
= BmpHeader
->PixelWidth
* BmpHeader
->PixelHeight
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
);
114 if (*GopBlt
== NULL
) {
116 // GopBlt is not allocated by caller.
118 *GopBltSize
= BltBufferSize
;
119 *GopBlt
= AllocatePool (*GopBltSize
);
121 if (*GopBlt
== NULL
) {
122 return EFI_OUT_OF_RESOURCES
;
126 // GopBlt has been allocated by caller.
128 if (*GopBltSize
< BltBufferSize
) {
129 *GopBltSize
= BltBufferSize
;
130 return EFI_BUFFER_TOO_SMALL
;
134 *PixelWidth
= BmpHeader
->PixelWidth
;
135 *PixelHeight
= BmpHeader
->PixelHeight
;
138 // Convert image from BMP to Blt buffer format
141 for (Height
= 0; Height
< BmpHeader
->PixelHeight
; Height
++) {
142 Blt
= &BltBuffer
[(BmpHeader
->PixelHeight
- Height
- 1) * BmpHeader
->PixelWidth
];
143 for (Width
= 0; Width
< BmpHeader
->PixelWidth
; Width
++, Image
++, Blt
++) {
144 switch (BmpHeader
->BitPerPixel
) {
147 // Convert 1-bit (2 colors) BMP to 24-bit color
149 for (Index
= 0; Index
< 8 && Width
< BmpHeader
->PixelWidth
; Index
++) {
150 Blt
->Red
= BmpColorMap
[((*Image
) >> (7 - Index
)) & 0x1].Red
;
151 Blt
->Green
= BmpColorMap
[((*Image
) >> (7 - Index
)) & 0x1].Green
;
152 Blt
->Blue
= BmpColorMap
[((*Image
) >> (7 - Index
)) & 0x1].Blue
;
163 // Convert 4-bit (16 colors) BMP Palette to 24-bit color
165 Index
= (*Image
) >> 4;
166 Blt
->Red
= BmpColorMap
[Index
].Red
;
167 Blt
->Green
= BmpColorMap
[Index
].Green
;
168 Blt
->Blue
= BmpColorMap
[Index
].Blue
;
169 if (Width
< (BmpHeader
->PixelWidth
- 1)) {
172 Index
= (*Image
) & 0x0f;
173 Blt
->Red
= BmpColorMap
[Index
].Red
;
174 Blt
->Green
= BmpColorMap
[Index
].Green
;
175 Blt
->Blue
= BmpColorMap
[Index
].Blue
;
181 // Convert 8-bit (256 colors) BMP Palette to 24-bit color
183 Blt
->Red
= BmpColorMap
[*Image
].Red
;
184 Blt
->Green
= BmpColorMap
[*Image
].Green
;
185 Blt
->Blue
= BmpColorMap
[*Image
].Blue
;
192 Blt
->Blue
= *Image
++;
193 Blt
->Green
= *Image
++;
199 // Other bit format BMP is not supported.
205 return EFI_UNSUPPORTED
;
211 ImageIndex
= (UINTN
) (Image
- ImageHeader
);
212 if ((ImageIndex
% 4) != 0) {
214 // Bmp Image starts each row on a 32-bit boundary!
216 Image
= Image
+ (4 - (ImageIndex
% 4));
225 Use Console Control Protocol to lock the Console In Spliter virtual handle.
226 This is the ConInHandle and ConIn handle in the EFI system table. All key
227 presses will be ignored until the Password is typed in. The only way to
228 disable the password is to type it in to a ConIn device.
230 @param Password Password used to lock ConIn device.
232 @retval EFI_SUCCESS lock the Console In Spliter virtual handle successfully.
233 @retval EFI_UNSUPPORTED Password not found.
243 EFI_CONSOLE_CONTROL_PROTOCOL
*ConsoleControl
;
245 Status
= gBS
->LocateProtocol (&gEfiConsoleControlProtocolGuid
, NULL
, (VOID
**) &ConsoleControl
);
246 if (EFI_ERROR (Status
)) {
247 return EFI_UNSUPPORTED
;
250 Status
= ConsoleControl
->LockStdIn (ConsoleControl
, Password
);
256 Use Console Control to turn off UGA based Simple Text Out consoles from going
257 to the UGA device. Put up LogoFile on every UGA device that is a console.
259 @param LogoFile File name of logo to display on the center of the screen.
261 @retval EFI_SUCCESS ConsoleControl has been flipped to graphics and logo displayed.
262 @retval EFI_UNSUPPORTED Logo not found.
268 IN EFI_GUID
*LogoFile
271 return EnableQuietBootEx (LogoFile
, NULL
);
275 Use Console Control to turn off UGA based Simple Text Out consoles from going
276 to the UGA device. Put up LogoFile on every UGA device that is a console
278 @param LogoFile File name of logo to display on the center of the screen.
279 @param ImageHandle The driver image handle of the caller. The parameter is used to
280 optimize the loading of the logo file so that the FV from which
281 the driver image is loaded will be tried first.
283 @retval EFI_SUCCESS ConsoleControl has been flipped to graphics and logo displayed.
284 @retval EFI_UNSUPPORTED Logo not found.
290 IN EFI_GUID
*LogoFile
,
291 IN EFI_HANDLE ImageHandle
295 EFI_CONSOLE_CONTROL_PROTOCOL
*ConsoleControl
;
296 EFI_OEM_BADGING_PROTOCOL
*Badging
;
305 EFI_BADGING_FORMAT Format
;
306 EFI_BADGING_DISPLAY_ATTRIBUTE Attribute
;
311 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*Blt
;
312 EFI_UGA_DRAW_PROTOCOL
*UgaDraw
;
315 EFI_GRAPHICS_OUTPUT_PROTOCOL
*GraphicsOutput
;
317 Status
= gBS
->LocateProtocol (&gEfiConsoleControlProtocolGuid
, NULL
, (VOID
**) &ConsoleControl
);
318 if (EFI_ERROR (Status
)) {
319 return EFI_UNSUPPORTED
;
324 // Try to open GOP first
326 Status
= gBS
->HandleProtocol (gST
->ConsoleOutHandle
, &gEfiGraphicsOutputProtocolGuid
, (VOID
**) &GraphicsOutput
);
327 if (EFI_ERROR (Status
) && FeaturePcdGet (PcdUgaConsumeSupport
)) {
328 GraphicsOutput
= NULL
;
330 // Open GOP failed, try to open UGA
332 Status
= gBS
->HandleProtocol (gST
->ConsoleOutHandle
, &gEfiUgaDrawProtocolGuid
, (VOID
**) &UgaDraw
);
334 if (EFI_ERROR (Status
)) {
335 return EFI_UNSUPPORTED
;
339 Status
= gBS
->LocateProtocol (&gEfiOEMBadgingProtocolGuid
, NULL
, (VOID
**) &Badging
);
342 // Set console control to graphics mode.
344 Status
= ConsoleControl
->SetMode (ConsoleControl
, EfiConsoleControlScreenGraphics
);
345 if (EFI_ERROR (Status
)) {
346 return EFI_UNSUPPORTED
;
349 if (GraphicsOutput
!= NULL
) {
350 SizeOfX
= GraphicsOutput
->Mode
->Info
->HorizontalResolution
;
351 SizeOfY
= GraphicsOutput
->Mode
->Info
->VerticalResolution
;
352 } else if (FeaturePcdGet (PcdUgaConsumeSupport
)) {
353 Status
= UgaDraw
->GetMode (UgaDraw
, &SizeOfX
, &SizeOfY
, &ColorDepth
, &RefreshRate
);
354 if (EFI_ERROR (Status
)) {
355 return EFI_UNSUPPORTED
;
358 return EFI_UNSUPPORTED
;
366 if (Badging
!= NULL
) {
368 // Get image from OEMBadging protocol.
370 Status
= Badging
->GetImage (
380 if (EFI_ERROR (Status
)) {
385 // Currently only support BMP format.
387 if (Format
!= EfiBadgingFormatBMP
) {
388 if (ImageData
!= NULL
) {
389 FreePool (ImageData
);
395 // Get the specified image from FV.
397 Status
= GetSectionFromAnyFv (LogoFile
, EFI_SECTION_RAW
, 0, (VOID
**) &ImageData
, &ImageSize
);
398 if (EFI_ERROR (Status
)) {
399 return EFI_UNSUPPORTED
;
404 Attribute
= EfiBadgingDisplayAttributeCenter
;
408 Status
= ConvertBmpToGopBlt (
416 if (EFI_ERROR (Status
)) {
417 if (ImageData
!= NULL
) {
418 FreePool (ImageData
);
420 if (Badging
== NULL
) {
428 // Caculate the display position according to Attribute.
431 case EfiBadgingDisplayAttributeLeftTop
:
436 case EfiBadgingDisplayAttributeCenterTop
:
437 DestX
= (SizeOfX
- Width
) / 2;
441 case EfiBadgingDisplayAttributeRightTop
:
442 DestX
= (SizeOfX
- Width
- CoordinateX
);
443 DestY
= CoordinateY
;;
446 case EfiBadgingDisplayAttributeCenterRight
:
447 DestX
= (SizeOfX
- Width
- CoordinateX
);
448 DestY
= (SizeOfY
- Height
) / 2;
451 case EfiBadgingDisplayAttributeRightBottom
:
452 DestX
= (SizeOfX
- Width
- CoordinateX
);
453 DestY
= (SizeOfY
- Height
- CoordinateY
);
456 case EfiBadgingDisplayAttributeCenterBottom
:
457 DestX
= (SizeOfX
- Width
) / 2;
458 DestY
= (SizeOfY
- Height
- CoordinateY
);
461 case EfiBadgingDisplayAttributeLeftBottom
:
463 DestY
= (SizeOfY
- Height
- CoordinateY
);
466 case EfiBadgingDisplayAttributeCenterLeft
:
468 DestY
= (SizeOfY
- Height
) / 2;
471 case EfiBadgingDisplayAttributeCenter
:
472 DestX
= (SizeOfX
- Width
) / 2;
473 DestY
= (SizeOfY
- Height
) / 2;
482 if ((DestX
>= 0) && (DestY
>= 0)) {
483 if (GraphicsOutput
!= NULL
) {
484 Status
= GraphicsOutput
->Blt (
494 Width
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
)
496 } else if (FeaturePcdGet (PcdUgaConsumeSupport
)) {
497 Status
= UgaDraw
->Blt (
499 (EFI_UGA_PIXEL
*) Blt
,
500 EfiUgaBltBufferToVideo
,
507 Width
* sizeof (EFI_UGA_PIXEL
)
510 Status
= EFI_UNSUPPORTED
;
514 if (ImageData
!= NULL
) {
515 FreePool (ImageData
);
521 if (Badging
== NULL
) {
530 Use Console Control to turn on UGA based Simple Text Out consoles. The UGA
531 Simple Text Out screens will now be synced up with all non UGA output devices
533 @retval EFI_SUCCESS UGA devices are back in text mode and synced up.
534 @retval EFI_UNSUPPORTED Logo not found
544 EFI_CONSOLE_CONTROL_PROTOCOL
*ConsoleControl
;
546 Status
= gBS
->LocateProtocol (&gEfiConsoleControlProtocolGuid
, NULL
, (VOID
**) &ConsoleControl
);
547 if (EFI_ERROR (Status
)) {
548 return EFI_UNSUPPORTED
;
552 // Set console control to text mode.
554 return ConsoleControl
->SetMode (ConsoleControl
, EfiConsoleControlScreenText
);