2 Platform BDS function for quiet boot support.
4 Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "BdsPlatform.h"
18 Convert a *.BMP graphics image to a GOP blt buffer. If a NULL Blt buffer
19 is passed in a GopBlt buffer will be allocated by this routine. If a GopBlt
20 buffer is passed in it will be used if it is big enough.
22 @param BmpImage Pointer to BMP file
23 @param BmpImageSize Number of bytes in BmpImage
24 @param GopBlt Buffer containing GOP version of BmpImage.
25 @param GopBltSize Size of GopBlt in bytes.
26 @param PixelHeight Height of GopBlt/BmpImage in pixels
27 @param PixelWidth Width of GopBlt/BmpImage in pixels
29 @retval EFI_SUCCESS GopBlt and GopBltSize are returned.
30 @retval EFI_UNSUPPORTED BmpImage is not a valid *.BMP image
31 @retval EFI_BUFFER_TOO_SMALL The passed in GopBlt buffer is not big enough.
32 GopBltSize will contain the required size.
33 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate.
40 IN UINTN BmpImageSize
,
42 IN OUT UINTN
*GopBltSize
,
43 OUT UINTN
*PixelHeight
,
49 BMP_IMAGE_HEADER
*BmpHeader
;
50 BMP_COLOR_MAP
*BmpColorMap
;
51 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
;
52 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*Blt
;
58 UINT32 DataSizePerLine
;
62 if (sizeof (BMP_IMAGE_HEADER
) > BmpImageSize
) {
63 return EFI_INVALID_PARAMETER
;
66 BmpHeader
= (BMP_IMAGE_HEADER
*) BmpImage
;
68 if (BmpHeader
->CharB
!= 'B' || BmpHeader
->CharM
!= 'M') {
69 return EFI_UNSUPPORTED
;
73 // Doesn't support compress.
75 if (BmpHeader
->CompressionType
!= 0) {
76 return EFI_UNSUPPORTED
;
80 // Only support BITMAPINFOHEADER format.
81 // BITMAPFILEHEADER + BITMAPINFOHEADER = BMP_IMAGE_HEADER
83 if (BmpHeader
->HeaderSize
!= sizeof (BMP_IMAGE_HEADER
) - OFFSET_OF(BMP_IMAGE_HEADER
, HeaderSize
)) {
84 return EFI_UNSUPPORTED
;
88 // The data size in each line must be 4 byte alignment.
90 DataSizePerLine
= ((BmpHeader
->PixelWidth
* BmpHeader
->BitPerPixel
+ 31) >> 3) & (~0x3);
91 BltBufferSize
= MultU64x32 (DataSizePerLine
, BmpHeader
->PixelHeight
);
92 if (BltBufferSize
> (UINT32
) ~0) {
93 return EFI_INVALID_PARAMETER
;
96 if ((BmpHeader
->Size
!= BmpImageSize
) ||
97 (BmpHeader
->Size
< BmpHeader
->ImageOffset
) ||
98 (BmpHeader
->Size
- BmpHeader
->ImageOffset
!= BmpHeader
->PixelHeight
* DataSizePerLine
)) {
99 return EFI_INVALID_PARAMETER
;
103 // Calculate Color Map offset in the image.
106 BmpColorMap
= (BMP_COLOR_MAP
*) (Image
+ sizeof (BMP_IMAGE_HEADER
));
107 if (BmpHeader
->ImageOffset
< sizeof (BMP_IMAGE_HEADER
)) {
108 return EFI_INVALID_PARAMETER
;
111 if (BmpHeader
->ImageOffset
> sizeof (BMP_IMAGE_HEADER
)) {
112 switch (BmpHeader
->BitPerPixel
) {
127 // BMP file may has padding data between the bmp header section and the bmp data section.
129 if (BmpHeader
->ImageOffset
- sizeof (BMP_IMAGE_HEADER
) < sizeof (BMP_COLOR_MAP
) * ColorMapNum
) {
130 return EFI_INVALID_PARAMETER
;
135 // Calculate graphics image data address in the image
137 Image
= ((UINT8
*) BmpImage
) + BmpHeader
->ImageOffset
;
141 // Calculate the BltBuffer needed size.
143 BltBufferSize
= MultU64x32 ((UINT64
) BmpHeader
->PixelWidth
, BmpHeader
->PixelHeight
);
145 // Ensure the BltBufferSize * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) doesn't overflow
147 if (BltBufferSize
> DivU64x32 ((UINTN
) ~0, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
))) {
148 return EFI_UNSUPPORTED
;
150 BltBufferSize
= MultU64x32 (BltBufferSize
, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
));
153 if (*GopBlt
== NULL
) {
155 // GopBlt is not allocated by caller.
157 *GopBltSize
= (UINTN
) BltBufferSize
;
158 *GopBlt
= AllocatePool (*GopBltSize
);
160 if (*GopBlt
== NULL
) {
161 return EFI_OUT_OF_RESOURCES
;
165 // GopBlt has been allocated by caller.
167 if (*GopBltSize
< (UINTN
) BltBufferSize
) {
168 *GopBltSize
= (UINTN
) BltBufferSize
;
169 return EFI_BUFFER_TOO_SMALL
;
173 *PixelWidth
= BmpHeader
->PixelWidth
;
174 *PixelHeight
= BmpHeader
->PixelHeight
;
177 // Convert image from BMP to Blt buffer format
180 for (Height
= 0; Height
< BmpHeader
->PixelHeight
; Height
++) {
181 Blt
= &BltBuffer
[(BmpHeader
->PixelHeight
- Height
- 1) * BmpHeader
->PixelWidth
];
182 for (Width
= 0; Width
< BmpHeader
->PixelWidth
; Width
++, Image
++, Blt
++) {
183 switch (BmpHeader
->BitPerPixel
) {
186 // Convert 1-bit (2 colors) BMP to 24-bit color
188 for (Index
= 0; Index
< 8 && Width
< BmpHeader
->PixelWidth
; Index
++) {
189 Blt
->Red
= BmpColorMap
[((*Image
) >> (7 - Index
)) & 0x1].Red
;
190 Blt
->Green
= BmpColorMap
[((*Image
) >> (7 - Index
)) & 0x1].Green
;
191 Blt
->Blue
= BmpColorMap
[((*Image
) >> (7 - Index
)) & 0x1].Blue
;
202 // Convert 4-bit (16 colors) BMP Palette to 24-bit color
204 Index
= (*Image
) >> 4;
205 Blt
->Red
= BmpColorMap
[Index
].Red
;
206 Blt
->Green
= BmpColorMap
[Index
].Green
;
207 Blt
->Blue
= BmpColorMap
[Index
].Blue
;
208 if (Width
< (BmpHeader
->PixelWidth
- 1)) {
211 Index
= (*Image
) & 0x0f;
212 Blt
->Red
= BmpColorMap
[Index
].Red
;
213 Blt
->Green
= BmpColorMap
[Index
].Green
;
214 Blt
->Blue
= BmpColorMap
[Index
].Blue
;
220 // Convert 8-bit (256 colors) BMP Palette to 24-bit color
222 Blt
->Red
= BmpColorMap
[*Image
].Red
;
223 Blt
->Green
= BmpColorMap
[*Image
].Green
;
224 Blt
->Blue
= BmpColorMap
[*Image
].Blue
;
231 Blt
->Blue
= *Image
++;
232 Blt
->Green
= *Image
++;
238 // Other bit format BMP is not supported.
244 return EFI_UNSUPPORTED
;
249 ImageIndex
= (UINTN
) (Image
- ImageHeader
);
250 if ((ImageIndex
% 4) != 0) {
252 // Bmp Image starts each row on a 32-bit boundary!
254 Image
= Image
+ (4 - (ImageIndex
% 4));
262 Use SystemTable Conout to stop video based Simple Text Out consoles from going
263 to the video device. Put up LogoFile on every video device that is a console.
265 @param[in] LogoFile File name of logo to display on the center of the screen.
267 @retval EFI_SUCCESS ConsoleControl has been flipped to graphics and logo displayed.
268 @retval EFI_UNSUPPORTED Logo not found
273 IN EFI_GUID
*LogoFile
277 EFI_OEM_BADGING_PROTOCOL
*Badging
;
286 EFI_BADGING_FORMAT Format
;
287 EFI_BADGING_DISPLAY_ATTRIBUTE Attribute
;
292 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*Blt
;
293 EFI_UGA_DRAW_PROTOCOL
*UgaDraw
;
296 EFI_GRAPHICS_OUTPUT_PROTOCOL
*GraphicsOutput
;
297 EFI_BOOT_LOGO_PROTOCOL
*BootLogo
;
299 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*LogoBlt
;
312 // Try to open GOP first
314 Status
= gBS
->HandleProtocol (gST
->ConsoleOutHandle
, &gEfiGraphicsOutputProtocolGuid
, (VOID
**) &GraphicsOutput
);
315 if (EFI_ERROR (Status
) && FeaturePcdGet (PcdUgaConsumeSupport
)) {
316 GraphicsOutput
= NULL
;
318 // Open GOP failed, try to open UGA
320 Status
= gBS
->HandleProtocol (gST
->ConsoleOutHandle
, &gEfiUgaDrawProtocolGuid
, (VOID
**) &UgaDraw
);
322 if (EFI_ERROR (Status
)) {
323 return EFI_UNSUPPORTED
;
327 // Try to open Boot Logo Protocol.
330 gBS
->LocateProtocol (&gEfiBootLogoProtocolGuid
, NULL
, (VOID
**) &BootLogo
);
333 // Erase Cursor from screen
335 gST
->ConOut
->EnableCursor (gST
->ConOut
, FALSE
);
338 Status
= gBS
->LocateProtocol (&gEfiOEMBadgingProtocolGuid
, NULL
, (VOID
**) &Badging
);
340 if (GraphicsOutput
!= NULL
) {
341 SizeOfX
= GraphicsOutput
->Mode
->Info
->HorizontalResolution
;
342 SizeOfY
= GraphicsOutput
->Mode
->Info
->VerticalResolution
;
344 } else if (UgaDraw
!= NULL
&& FeaturePcdGet (PcdUgaConsumeSupport
)) {
345 Status
= UgaDraw
->GetMode (UgaDraw
, &SizeOfX
, &SizeOfY
, &ColorDepth
, &RefreshRate
);
346 if (EFI_ERROR (Status
)) {
347 return EFI_UNSUPPORTED
;
350 return EFI_UNSUPPORTED
;
370 if (Badging
!= NULL
) {
372 // Get image from OEMBadging protocol.
374 Status
= Badging
->GetImage (
384 if (EFI_ERROR (Status
)) {
389 // Currently only support BMP format.
391 if (Format
!= EfiBadgingFormatBMP
) {
392 if (ImageData
!= NULL
) {
393 FreePool (ImageData
);
399 // Get the specified image from FV.
401 Status
= GetSectionFromAnyFv (LogoFile
, EFI_SECTION_RAW
, 0, (VOID
**) &ImageData
, &ImageSize
);
402 if (EFI_ERROR (Status
)) {
403 return EFI_UNSUPPORTED
;
408 if (!FeaturePcdGet(PcdBootlogoOnlyEnable
)) {
409 Attribute
= EfiBadgingDisplayAttributeCenter
;
411 Attribute
= EfiBadgingDisplayAttributeCustomized
;
419 Status
= ConvertBmpToGopBlt (
427 if (EFI_ERROR (Status
)) {
428 FreePool (ImageData
);
430 if (Badging
== NULL
) {
438 // Calculate the display position according to Attribute.
441 case EfiBadgingDisplayAttributeLeftTop
:
446 case EfiBadgingDisplayAttributeCenterTop
:
447 DestX
= (SizeOfX
- Width
) / 2;
451 case EfiBadgingDisplayAttributeRightTop
:
452 DestX
= (SizeOfX
- Width
- CoordinateX
);
453 DestY
= CoordinateY
;;
456 case EfiBadgingDisplayAttributeCenterRight
:
457 DestX
= (SizeOfX
- Width
- CoordinateX
);
458 DestY
= (SizeOfY
- Height
) / 2;
461 case EfiBadgingDisplayAttributeRightBottom
:
462 DestX
= (SizeOfX
- Width
- CoordinateX
);
463 DestY
= (SizeOfY
- Height
- CoordinateY
);
466 case EfiBadgingDisplayAttributeCenterBottom
:
467 DestX
= (SizeOfX
- Width
) / 2;
468 DestY
= (SizeOfY
- Height
- CoordinateY
);
471 case EfiBadgingDisplayAttributeLeftBottom
:
473 DestY
= (SizeOfY
- Height
- CoordinateY
);
476 case EfiBadgingDisplayAttributeCenterLeft
:
478 DestY
= (SizeOfY
- Height
) / 2;
481 case EfiBadgingDisplayAttributeCenter
:
482 DestX
= (SizeOfX
- Width
) / 2;
483 DestY
= (SizeOfY
- Height
) / 2;
486 case EfiBadgingDisplayAttributeCustomized
:
487 DestX
= (SizeOfX
- Width
) / 2;
488 DestY
= ((SizeOfY
* 382) / 1000) - Height
/ 2;
497 if ((DestX
>= 0) && (DestY
>= 0)) {
498 if (GraphicsOutput
!= NULL
) {
499 Status
= GraphicsOutput
->Blt (
509 Width
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
)
511 } else if (UgaDraw
!= NULL
&& FeaturePcdGet (PcdUgaConsumeSupport
)) {
512 Status
= UgaDraw
->Blt (
514 (EFI_UGA_PIXEL
*) Blt
,
515 EfiUgaBltBufferToVideo
,
522 Width
* sizeof (EFI_UGA_PIXEL
)
525 Status
= EFI_UNSUPPORTED
;
529 // Report displayed Logo information.
531 if (!EFI_ERROR (Status
)) {
534 if (LogoWidth
== 0) {
538 LogoDestX
= (UINTN
) DestX
;
539 LogoDestY
= (UINTN
) DestY
;
544 // Merge new logo with old one.
546 NewDestX
= MIN ((UINTN
) DestX
, LogoDestX
);
547 NewDestY
= MIN ((UINTN
) DestY
, LogoDestY
);
548 NewWidth
= MAX ((UINTN
) DestX
+ Width
, LogoDestX
+ LogoWidth
) - NewDestX
;
549 NewHeight
= MAX ((UINTN
) DestY
+ Height
, LogoDestY
+ LogoHeight
) - NewDestY
;
551 LogoDestX
= NewDestX
;
552 LogoDestY
= NewDestY
;
553 LogoWidth
= NewWidth
;
554 LogoHeight
= NewHeight
;
559 FreePool (ImageData
);
561 if (Badging
== NULL
) {
567 if (BootLogo
== NULL
|| NumberOfLogos
== 0) {
569 // No logo displayed.
579 // Advertise displayed Logo information.
581 if (NumberOfLogos
== 1) {
583 // Only one logo displayed, use its Blt buffer directly for BootLogo protocol.
586 Status
= EFI_SUCCESS
;
589 // More than one Logo displayed, get merged BltBuffer using VideoToBuffer operation.
596 // Ensure the LogoHeight * LogoWidth doesn't overflow
598 if (LogoHeight
> DivU64x64Remainder ((UINTN
) ~0, LogoWidth
, NULL
)) {
599 return EFI_UNSUPPORTED
;
601 BufferSize
= MultU64x64 (LogoWidth
, LogoHeight
);
604 // Ensure the BufferSize * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) doesn't overflow
606 if (BufferSize
> DivU64x32 ((UINTN
) ~0, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
))) {
607 return EFI_UNSUPPORTED
;
610 LogoBlt
= AllocateZeroPool ((UINTN
)BufferSize
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
));
611 if (LogoBlt
== NULL
) {
612 return EFI_OUT_OF_RESOURCES
;
615 if (GraphicsOutput
!= NULL
) {
616 Status
= GraphicsOutput
->Blt (
619 EfiBltVideoToBltBuffer
,
626 LogoWidth
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
)
628 } else if (UgaDraw
!= NULL
&& FeaturePcdGet (PcdUgaConsumeSupport
)) {
629 Status
= UgaDraw
->Blt (
631 (EFI_UGA_PIXEL
*) LogoBlt
,
632 EfiUgaVideoToBltBuffer
,
639 LogoWidth
* sizeof (EFI_UGA_PIXEL
)
642 Status
= EFI_UNSUPPORTED
;
646 if (!EFI_ERROR (Status
)) {
647 BootLogo
->SetBootLogo (BootLogo
, LogoBlt
, LogoDestX
, LogoDestY
, LogoWidth
, LogoHeight
);
655 Use SystemTable Conout to turn on video based Simple Text Out consoles. The
656 Simple Text Out screens will now be synced up with all non video output devices
658 @retval EFI_SUCCESS UGA devices are back in text mode and synced up.
668 // Enable Cursor on Screen
670 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);