]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/PlatformBootManagerLib/QuietBoot.c
972050fa9cc73a9603d2f7011ec8762d9a0c3b05
[mirror_edk2.git] / OvmfPkg / Library / PlatformBootManagerLib / QuietBoot.c
1 /** @file
2 Platform BDS function for quiet boot support.
3
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
9
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.
12
13 **/
14
15 #include "BdsPlatform.h"
16
17 /**
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.
21
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
28
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.
34
35 **/
36 STATIC
37 EFI_STATUS
38 ConvertBmpToGopBlt (
39 IN VOID *BmpImage,
40 IN UINTN BmpImageSize,
41 IN OUT VOID **GopBlt,
42 IN OUT UINTN *GopBltSize,
43 OUT UINTN *PixelHeight,
44 OUT UINTN *PixelWidth
45 )
46 {
47 UINT8 *Image;
48 UINT8 *ImageHeader;
49 BMP_IMAGE_HEADER *BmpHeader;
50 BMP_COLOR_MAP *BmpColorMap;
51 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer;
52 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;
53 UINT64 BltBufferSize;
54 UINTN Index;
55 UINTN Height;
56 UINTN Width;
57 UINTN ImageIndex;
58 UINT32 DataSizePerLine;
59 BOOLEAN IsAllocated;
60 UINT32 ColorMapNum;
61
62 if (sizeof (BMP_IMAGE_HEADER) > BmpImageSize) {
63 return EFI_INVALID_PARAMETER;
64 }
65
66 BmpHeader = (BMP_IMAGE_HEADER *) BmpImage;
67
68 if (BmpHeader->CharB != 'B' || BmpHeader->CharM != 'M') {
69 return EFI_UNSUPPORTED;
70 }
71
72 //
73 // Doesn't support compress.
74 //
75 if (BmpHeader->CompressionType != 0) {
76 return EFI_UNSUPPORTED;
77 }
78
79 //
80 // Only support BITMAPINFOHEADER format.
81 // BITMAPFILEHEADER + BITMAPINFOHEADER = BMP_IMAGE_HEADER
82 //
83 if (BmpHeader->HeaderSize != sizeof (BMP_IMAGE_HEADER) - OFFSET_OF(BMP_IMAGE_HEADER, HeaderSize)) {
84 return EFI_UNSUPPORTED;
85 }
86
87 //
88 // The data size in each line must be 4 byte alignment.
89 //
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;
94 }
95
96 if ((BmpHeader->Size != BmpImageSize) ||
97 (BmpHeader->Size < BmpHeader->ImageOffset) ||
98 (BmpHeader->Size - BmpHeader->ImageOffset != BmpHeader->PixelHeight * DataSizePerLine)) {
99 return EFI_INVALID_PARAMETER;
100 }
101
102 //
103 // Calculate Color Map offset in the image.
104 //
105 Image = BmpImage;
106 BmpColorMap = (BMP_COLOR_MAP *) (Image + sizeof (BMP_IMAGE_HEADER));
107 if (BmpHeader->ImageOffset < sizeof (BMP_IMAGE_HEADER)) {
108 return EFI_INVALID_PARAMETER;
109 }
110
111 if (BmpHeader->ImageOffset > sizeof (BMP_IMAGE_HEADER)) {
112 switch (BmpHeader->BitPerPixel) {
113 case 1:
114 ColorMapNum = 2;
115 break;
116 case 4:
117 ColorMapNum = 16;
118 break;
119 case 8:
120 ColorMapNum = 256;
121 break;
122 default:
123 ColorMapNum = 0;
124 break;
125 }
126 //
127 // BMP file may has padding data between the bmp header section and the bmp data section.
128 //
129 if (BmpHeader->ImageOffset - sizeof (BMP_IMAGE_HEADER) < sizeof (BMP_COLOR_MAP) * ColorMapNum) {
130 return EFI_INVALID_PARAMETER;
131 }
132 }
133
134 //
135 // Calculate graphics image data address in the image
136 //
137 Image = ((UINT8 *) BmpImage) + BmpHeader->ImageOffset;
138 ImageHeader = Image;
139
140 //
141 // Calculate the BltBuffer needed size.
142 //
143 BltBufferSize = MultU64x32 ((UINT64) BmpHeader->PixelWidth, BmpHeader->PixelHeight);
144 //
145 // Ensure the BltBufferSize * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) doesn't overflow
146 //
147 if (BltBufferSize > DivU64x32 ((UINTN) ~0, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL))) {
148 return EFI_UNSUPPORTED;
149 }
150 BltBufferSize = MultU64x32 (BltBufferSize, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
151
152 IsAllocated = FALSE;
153 if (*GopBlt == NULL) {
154 //
155 // GopBlt is not allocated by caller.
156 //
157 *GopBltSize = (UINTN) BltBufferSize;
158 *GopBlt = AllocatePool (*GopBltSize);
159 IsAllocated = TRUE;
160 if (*GopBlt == NULL) {
161 return EFI_OUT_OF_RESOURCES;
162 }
163 } else {
164 //
165 // GopBlt has been allocated by caller.
166 //
167 if (*GopBltSize < (UINTN) BltBufferSize) {
168 *GopBltSize = (UINTN) BltBufferSize;
169 return EFI_BUFFER_TOO_SMALL;
170 }
171 }
172
173 *PixelWidth = BmpHeader->PixelWidth;
174 *PixelHeight = BmpHeader->PixelHeight;
175
176 //
177 // Convert image from BMP to Blt buffer format
178 //
179 BltBuffer = *GopBlt;
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) {
184 case 1:
185 //
186 // Convert 1-bit (2 colors) BMP to 24-bit color
187 //
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;
192 Blt++;
193 Width++;
194 }
195
196 Blt--;
197 Width--;
198 break;
199
200 case 4:
201 //
202 // Convert 4-bit (16 colors) BMP Palette to 24-bit color
203 //
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)) {
209 Blt++;
210 Width++;
211 Index = (*Image) & 0x0f;
212 Blt->Red = BmpColorMap[Index].Red;
213 Blt->Green = BmpColorMap[Index].Green;
214 Blt->Blue = BmpColorMap[Index].Blue;
215 }
216 break;
217
218 case 8:
219 //
220 // Convert 8-bit (256 colors) BMP Palette to 24-bit color
221 //
222 Blt->Red = BmpColorMap[*Image].Red;
223 Blt->Green = BmpColorMap[*Image].Green;
224 Blt->Blue = BmpColorMap[*Image].Blue;
225 break;
226
227 case 24:
228 //
229 // It is 24-bit BMP.
230 //
231 Blt->Blue = *Image++;
232 Blt->Green = *Image++;
233 Blt->Red = *Image;
234 break;
235
236 default:
237 //
238 // Other bit format BMP is not supported.
239 //
240 if (IsAllocated) {
241 FreePool (*GopBlt);
242 *GopBlt = NULL;
243 }
244 return EFI_UNSUPPORTED;
245 };
246
247 }
248
249 ImageIndex = (UINTN) (Image - ImageHeader);
250 if ((ImageIndex % 4) != 0) {
251 //
252 // Bmp Image starts each row on a 32-bit boundary!
253 //
254 Image = Image + (4 - (ImageIndex % 4));
255 }
256 }
257
258 return EFI_SUCCESS;
259 }
260
261 /**
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.
264
265 @param[in] LogoFile File name of logo to display on the center of the screen.
266
267 @retval EFI_SUCCESS ConsoleControl has been flipped to graphics and logo displayed.
268 @retval EFI_UNSUPPORTED Logo not found
269
270 **/
271 EFI_STATUS
272 EnableQuietBoot (
273 IN EFI_GUID *LogoFile
274 )
275 {
276 EFI_STATUS Status;
277 EFI_OEM_BADGING_PROTOCOL *Badging;
278 UINT32 SizeOfX;
279 UINT32 SizeOfY;
280 INTN DestX;
281 INTN DestY;
282 UINT8 *ImageData;
283 UINTN ImageSize;
284 UINTN BltSize;
285 UINT32 Instance;
286 EFI_BADGING_FORMAT Format;
287 EFI_BADGING_DISPLAY_ATTRIBUTE Attribute;
288 UINTN CoordinateX;
289 UINTN CoordinateY;
290 UINTN Height;
291 UINTN Width;
292 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;
293 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
294 UINT32 ColorDepth;
295 UINT32 RefreshRate;
296 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
297 EFI_BOOT_LOGO_PROTOCOL *BootLogo;
298 UINTN NumberOfLogos;
299 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *LogoBlt;
300 UINTN LogoDestX;
301 UINTN LogoDestY;
302 UINTN LogoHeight;
303 UINTN LogoWidth;
304 UINTN NewDestX;
305 UINTN NewDestY;
306 UINTN NewHeight;
307 UINTN NewWidth;
308 UINT64 BufferSize;
309
310 UgaDraw = NULL;
311 //
312 // Try to open GOP first
313 //
314 Status = gBS->HandleProtocol (gST->ConsoleOutHandle, &gEfiGraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput);
315 if (EFI_ERROR (Status) && FeaturePcdGet (PcdUgaConsumeSupport)) {
316 GraphicsOutput = NULL;
317 //
318 // Open GOP failed, try to open UGA
319 //
320 Status = gBS->HandleProtocol (gST->ConsoleOutHandle, &gEfiUgaDrawProtocolGuid, (VOID **) &UgaDraw);
321 }
322 if (EFI_ERROR (Status)) {
323 return EFI_UNSUPPORTED;
324 }
325
326 //
327 // Try to open Boot Logo Protocol.
328 //
329 BootLogo = NULL;
330 gBS->LocateProtocol (&gEfiBootLogoProtocolGuid, NULL, (VOID **) &BootLogo);
331
332 //
333 // Erase Cursor from screen
334 //
335 gST->ConOut->EnableCursor (gST->ConOut, FALSE);
336
337 Badging = NULL;
338 Status = gBS->LocateProtocol (&gEfiOEMBadgingProtocolGuid, NULL, (VOID **) &Badging);
339
340 if (GraphicsOutput != NULL) {
341 SizeOfX = GraphicsOutput->Mode->Info->HorizontalResolution;
342 SizeOfY = GraphicsOutput->Mode->Info->VerticalResolution;
343
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;
348 }
349 } else {
350 return EFI_UNSUPPORTED;
351 }
352
353 Blt = NULL;
354 NumberOfLogos = 0;
355 LogoDestX = 0;
356 LogoDestY = 0;
357 LogoHeight = 0;
358 LogoWidth = 0;
359 NewDestX = 0;
360 NewDestY = 0;
361 NewHeight = 0;
362 NewWidth = 0;
363 Instance = 0;
364 Height = 0;
365 Width = 0;
366 while (1) {
367 ImageData = NULL;
368 ImageSize = 0;
369
370 if (Badging != NULL) {
371 //
372 // Get image from OEMBadging protocol.
373 //
374 Status = Badging->GetImage (
375 Badging,
376 &Instance,
377 &Format,
378 &ImageData,
379 &ImageSize,
380 &Attribute,
381 &CoordinateX,
382 &CoordinateY
383 );
384 if (EFI_ERROR (Status)) {
385 goto Done;
386 }
387
388 //
389 // Currently only support BMP format.
390 //
391 if (Format != EfiBadgingFormatBMP) {
392 if (ImageData != NULL) {
393 FreePool (ImageData);
394 }
395 continue;
396 }
397 } else {
398 //
399 // Get the specified image from FV.
400 //
401 Status = GetSectionFromAnyFv (LogoFile, EFI_SECTION_RAW, 0, (VOID **) &ImageData, &ImageSize);
402 if (EFI_ERROR (Status)) {
403 return EFI_UNSUPPORTED;
404 }
405
406 CoordinateX = 0;
407 CoordinateY = 0;
408 if (!FeaturePcdGet(PcdBootlogoOnlyEnable)) {
409 Attribute = EfiBadgingDisplayAttributeCenter;
410 } else {
411 Attribute = EfiBadgingDisplayAttributeCustomized;
412 }
413 }
414
415 if (Blt != NULL) {
416 FreePool (Blt);
417 }
418 Blt = NULL;
419 Status = ConvertBmpToGopBlt (
420 ImageData,
421 ImageSize,
422 (VOID **) &Blt,
423 &BltSize,
424 &Height,
425 &Width
426 );
427 if (EFI_ERROR (Status)) {
428 FreePool (ImageData);
429
430 if (Badging == NULL) {
431 return Status;
432 } else {
433 continue;
434 }
435 }
436
437 //
438 // Calculate the display position according to Attribute.
439 //
440 switch (Attribute) {
441 case EfiBadgingDisplayAttributeLeftTop:
442 DestX = CoordinateX;
443 DestY = CoordinateY;
444 break;
445
446 case EfiBadgingDisplayAttributeCenterTop:
447 DestX = (SizeOfX - Width) / 2;
448 DestY = CoordinateY;
449 break;
450
451 case EfiBadgingDisplayAttributeRightTop:
452 DestX = (SizeOfX - Width - CoordinateX);
453 DestY = CoordinateY;;
454 break;
455
456 case EfiBadgingDisplayAttributeCenterRight:
457 DestX = (SizeOfX - Width - CoordinateX);
458 DestY = (SizeOfY - Height) / 2;
459 break;
460
461 case EfiBadgingDisplayAttributeRightBottom:
462 DestX = (SizeOfX - Width - CoordinateX);
463 DestY = (SizeOfY - Height - CoordinateY);
464 break;
465
466 case EfiBadgingDisplayAttributeCenterBottom:
467 DestX = (SizeOfX - Width) / 2;
468 DestY = (SizeOfY - Height - CoordinateY);
469 break;
470
471 case EfiBadgingDisplayAttributeLeftBottom:
472 DestX = CoordinateX;
473 DestY = (SizeOfY - Height - CoordinateY);
474 break;
475
476 case EfiBadgingDisplayAttributeCenterLeft:
477 DestX = CoordinateX;
478 DestY = (SizeOfY - Height) / 2;
479 break;
480
481 case EfiBadgingDisplayAttributeCenter:
482 DestX = (SizeOfX - Width) / 2;
483 DestY = (SizeOfY - Height) / 2;
484 break;
485
486 case EfiBadgingDisplayAttributeCustomized:
487 DestX = (SizeOfX - Width) / 2;
488 DestY = ((SizeOfY * 382) / 1000) - Height / 2;
489 break;
490
491 default:
492 DestX = CoordinateX;
493 DestY = CoordinateY;
494 break;
495 }
496
497 if ((DestX >= 0) && (DestY >= 0)) {
498 if (GraphicsOutput != NULL) {
499 Status = GraphicsOutput->Blt (
500 GraphicsOutput,
501 Blt,
502 EfiBltBufferToVideo,
503 0,
504 0,
505 (UINTN) DestX,
506 (UINTN) DestY,
507 Width,
508 Height,
509 Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
510 );
511 } else if (UgaDraw != NULL && FeaturePcdGet (PcdUgaConsumeSupport)) {
512 Status = UgaDraw->Blt (
513 UgaDraw,
514 (EFI_UGA_PIXEL *) Blt,
515 EfiUgaBltBufferToVideo,
516 0,
517 0,
518 (UINTN) DestX,
519 (UINTN) DestY,
520 Width,
521 Height,
522 Width * sizeof (EFI_UGA_PIXEL)
523 );
524 } else {
525 Status = EFI_UNSUPPORTED;
526 }
527
528 //
529 // Report displayed Logo information.
530 //
531 if (!EFI_ERROR (Status)) {
532 NumberOfLogos++;
533
534 if (LogoWidth == 0) {
535 //
536 // The first Logo.
537 //
538 LogoDestX = (UINTN) DestX;
539 LogoDestY = (UINTN) DestY;
540 LogoWidth = Width;
541 LogoHeight = Height;
542 } else {
543 //
544 // Merge new logo with old one.
545 //
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;
550
551 LogoDestX = NewDestX;
552 LogoDestY = NewDestY;
553 LogoWidth = NewWidth;
554 LogoHeight = NewHeight;
555 }
556 }
557 }
558
559 FreePool (ImageData);
560
561 if (Badging == NULL) {
562 break;
563 }
564 }
565
566 Done:
567 if (BootLogo == NULL || NumberOfLogos == 0) {
568 //
569 // No logo displayed.
570 //
571 if (Blt != NULL) {
572 FreePool (Blt);
573 }
574
575 return Status;
576 }
577
578 //
579 // Advertise displayed Logo information.
580 //
581 if (NumberOfLogos == 1) {
582 //
583 // Only one logo displayed, use its Blt buffer directly for BootLogo protocol.
584 //
585 LogoBlt = Blt;
586 Status = EFI_SUCCESS;
587 } else {
588 //
589 // More than one Logo displayed, get merged BltBuffer using VideoToBuffer operation.
590 //
591 if (Blt != NULL) {
592 FreePool (Blt);
593 }
594
595 //
596 // Ensure the LogoHeight * LogoWidth doesn't overflow
597 //
598 if (LogoHeight > DivU64x64Remainder ((UINTN) ~0, LogoWidth, NULL)) {
599 return EFI_UNSUPPORTED;
600 }
601 BufferSize = MultU64x64 (LogoWidth, LogoHeight);
602
603 //
604 // Ensure the BufferSize * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) doesn't overflow
605 //
606 if (BufferSize > DivU64x32 ((UINTN) ~0, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL))) {
607 return EFI_UNSUPPORTED;
608 }
609
610 LogoBlt = AllocateZeroPool ((UINTN)BufferSize * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
611 if (LogoBlt == NULL) {
612 return EFI_OUT_OF_RESOURCES;
613 }
614
615 if (GraphicsOutput != NULL) {
616 Status = GraphicsOutput->Blt (
617 GraphicsOutput,
618 LogoBlt,
619 EfiBltVideoToBltBuffer,
620 LogoDestX,
621 LogoDestY,
622 0,
623 0,
624 LogoWidth,
625 LogoHeight,
626 LogoWidth * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
627 );
628 } else if (UgaDraw != NULL && FeaturePcdGet (PcdUgaConsumeSupport)) {
629 Status = UgaDraw->Blt (
630 UgaDraw,
631 (EFI_UGA_PIXEL *) LogoBlt,
632 EfiUgaVideoToBltBuffer,
633 LogoDestX,
634 LogoDestY,
635 0,
636 0,
637 LogoWidth,
638 LogoHeight,
639 LogoWidth * sizeof (EFI_UGA_PIXEL)
640 );
641 } else {
642 Status = EFI_UNSUPPORTED;
643 }
644 }
645
646 if (!EFI_ERROR (Status)) {
647 BootLogo->SetBootLogo (BootLogo, LogoBlt, LogoDestX, LogoDestY, LogoWidth, LogoHeight);
648 }
649 FreePool (LogoBlt);
650
651 return Status;
652 }
653
654 /**
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
657
658 @retval EFI_SUCCESS UGA devices are back in text mode and synced up.
659
660 **/
661 EFI_STATUS
662 DisableQuietBoot (
663 VOID
664 )
665 {
666
667 //
668 // Enable Cursor on Screen
669 //
670 gST->ConOut->EnableCursor (gST->ConOut, TRUE);
671 return EFI_SUCCESS;
672 }
673