]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsConsole.c
fe6d436a561530ae3bce967e3c7da385e625fdf1
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / GenericBdsLib / BdsConsole.c
1 /** @file
2 BDS Lib functions which contain all the code to connect console device
3
4 Copyright (c) 2004 - 2012, 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 "InternalBdsLib.h"
16 #include <IndustryStandard/Bmp.h>
17
18
19 /**
20 Check if we need to save the EFI variable with "ConVarName" as name
21 as NV type
22 If ConVarName is NULL, then ASSERT().
23
24 @param ConVarName The name of the EFI variable.
25
26 @retval TRUE Set the EFI variable as NV type.
27 @retval FALSE EFI variable as NV type can be set NonNV.
28 **/
29 BOOLEAN
30 IsNvNeed (
31 IN CHAR16 *ConVarName
32 )
33 {
34 CHAR16 *Ptr;
35
36 ASSERT (ConVarName != NULL);
37
38 Ptr = ConVarName;
39
40 //
41 // If the variable includes "Dev" at last, we consider
42 // it does not support NV attribute.
43 //
44 while (*Ptr != L'\0') {
45 Ptr++;
46 }
47
48 if (((INTN)((UINTN)Ptr - (UINTN)ConVarName) / sizeof (CHAR16)) <= 3) {
49 return TRUE;
50 }
51
52 if ((*(Ptr - 3) == 'D') && (*(Ptr - 2) == 'e') && (*(Ptr - 1) == 'v')) {
53 return FALSE;
54 } else {
55 return TRUE;
56 }
57 }
58
59 /**
60 Fill console handle in System Table if there are no valid console handle in.
61
62 Firstly, check the validation of console handle in System Table. If it is invalid,
63 update it by the first console device handle from EFI console variable.
64
65 @param VarName The name of the EFI console variable.
66 @param ConsoleGuid Specified Console protocol GUID.
67 @param ConsoleHandle On IN, console handle in System Table to be checked.
68 On OUT, new console hanlde in system table.
69 @param ProtocolInterface On IN, console protocol on console handle in System Table to be checked.
70 On OUT, new console protocol on new console hanlde in system table.
71
72 @retval TRUE System Table has been updated.
73 @retval FALSE System Table hasn't been updated.
74
75 **/
76 BOOLEAN
77 UpdateSystemTableConsole (
78 IN CHAR16 *VarName,
79 IN EFI_GUID *ConsoleGuid,
80 IN OUT EFI_HANDLE *ConsoleHandle,
81 IN OUT VOID **ProtocolInterface
82 )
83 {
84 EFI_STATUS Status;
85 UINTN DevicePathSize;
86 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;
87 EFI_DEVICE_PATH_PROTOCOL *VarConsole;
88 EFI_DEVICE_PATH_PROTOCOL *Instance;
89 VOID *Interface;
90 EFI_HANDLE NewHandle;
91 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *TextOut;
92
93 ASSERT (VarName != NULL);
94 ASSERT (ConsoleHandle != NULL);
95 ASSERT (ConsoleGuid != NULL);
96 ASSERT (ProtocolInterface != NULL);
97
98 if (*ConsoleHandle != NULL) {
99 Status = gBS->HandleProtocol (
100 *ConsoleHandle,
101 ConsoleGuid,
102 &Interface
103 );
104 if (Status == EFI_SUCCESS && Interface == *ProtocolInterface) {
105 //
106 // If ConsoleHandle is valid and console protocol on this handle also
107 // also matched, just return.
108 //
109 return FALSE;
110 }
111 }
112
113 //
114 // Get all possible consoles device path from EFI variable
115 //
116 VarConsole = BdsLibGetVariableAndSize (
117 VarName,
118 &gEfiGlobalVariableGuid,
119 &DevicePathSize
120 );
121 if (VarConsole == NULL) {
122 //
123 // If there is no any console device, just return.
124 //
125 return FALSE;
126 }
127
128 FullDevicePath = VarConsole;
129
130 do {
131 //
132 // Check every instance of the console variable
133 //
134 Instance = GetNextDevicePathInstance (&VarConsole, &DevicePathSize);
135 if (Instance == NULL) {
136 FreePool (FullDevicePath);
137 ASSERT (FALSE);
138 }
139
140 //
141 // Find console device handle by device path instance
142 //
143 Status = gBS->LocateDevicePath (
144 ConsoleGuid,
145 &Instance,
146 &NewHandle
147 );
148 if (!EFI_ERROR (Status)) {
149 //
150 // Get the console protocol on this console device handle
151 //
152 Status = gBS->HandleProtocol (
153 NewHandle,
154 ConsoleGuid,
155 &Interface
156 );
157 if (!EFI_ERROR (Status)) {
158 //
159 // Update new console handle in System Table.
160 //
161 *ConsoleHandle = NewHandle;
162 *ProtocolInterface = Interface;
163 if (CompareGuid (ConsoleGuid, &gEfiSimpleTextOutProtocolGuid)) {
164 //
165 // If it is console out device, set console mode 80x25 if current mode is invalid.
166 //
167 TextOut = (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *) Interface;
168 if (TextOut->Mode->Mode == -1) {
169 TextOut->SetMode (TextOut, 0);
170 }
171 }
172 return TRUE;
173 }
174 }
175
176 } while (Instance != NULL);
177
178 //
179 // No any available console devcie found.
180 //
181 return FALSE;
182 }
183
184 /**
185 Connect the console device base on the variable ConVarName, if
186 device path of the ConVarName is multi-instance device path and
187 anyone of the instances is connected success, this function will
188 return success.
189 Dispatch service is called basing on input when the handle associate
190 with one device path node can not be created successfully. Since in
191 some cases we assume driver dependency does not exist and do not
192 need to call this service.
193
194 @param ConVarName Console related variable name, ConIn, ConOut,
195 ErrOut.
196 @param NeedDispatch Whether requires dispatch service during connection
197
198 @retval EFI_NOT_FOUND There is not any console devices connected
199 success
200 @retval EFI_SUCCESS Success connect any one instance of the console
201 device path base on the variable ConVarName.
202
203 **/
204 EFI_STATUS
205 ConnectConsoleVariableInternal (
206 IN CHAR16 *ConVarName,
207 IN BOOLEAN NeedDispatch
208 )
209 {
210 EFI_STATUS Status;
211 EFI_DEVICE_PATH_PROTOCOL *StartDevicePath;
212 UINTN VariableSize;
213 EFI_DEVICE_PATH_PROTOCOL *Instance;
214 EFI_DEVICE_PATH_PROTOCOL *Next;
215 EFI_DEVICE_PATH_PROTOCOL *CopyOfDevicePath;
216 UINTN Size;
217 BOOLEAN DeviceExist;
218
219 Status = EFI_SUCCESS;
220 DeviceExist = FALSE;
221
222 //
223 // Check if the console variable exist
224 //
225 StartDevicePath = BdsLibGetVariableAndSize (
226 ConVarName,
227 &gEfiGlobalVariableGuid,
228 &VariableSize
229 );
230 if (StartDevicePath == NULL) {
231 return EFI_UNSUPPORTED;
232 }
233
234 CopyOfDevicePath = StartDevicePath;
235 do {
236 //
237 // Check every instance of the console variable
238 //
239 Instance = GetNextDevicePathInstance (&CopyOfDevicePath, &Size);
240 if (Instance == NULL) {
241 FreePool (StartDevicePath);
242 return EFI_UNSUPPORTED;
243 }
244
245 Next = Instance;
246 while (!IsDevicePathEndType (Next)) {
247 Next = NextDevicePathNode (Next);
248 }
249
250 SetDevicePathEndNode (Next);
251 //
252 // Connect the USB console
253 // USB console device path is a short-form device path that
254 // starts with the first element being a USB WWID
255 // or a USB Class device path
256 //
257 if ((DevicePathType (Instance) == MESSAGING_DEVICE_PATH) &&
258 ((DevicePathSubType (Instance) == MSG_USB_CLASS_DP)
259 || (DevicePathSubType (Instance) == MSG_USB_WWID_DP)
260 )) {
261 Status = BdsLibConnectUsbDevByShortFormDP (0xFF, Instance);
262 if (!EFI_ERROR (Status)) {
263 DeviceExist = TRUE;
264 }
265 } else {
266 //
267 // Connect the instance device path
268 //
269 Status = ConnectDevicePathInternal (Instance, NeedDispatch);
270
271 if (EFI_ERROR (Status)) {
272 //
273 // Delete the instance from the console varialbe
274 //
275 BdsLibUpdateConsoleVariable (ConVarName, NULL, Instance);
276 } else {
277 DeviceExist = TRUE;
278 }
279 }
280 FreePool(Instance);
281 } while (CopyOfDevicePath != NULL);
282
283 FreePool (StartDevicePath);
284
285 if (!DeviceExist) {
286 return EFI_NOT_FOUND;
287 }
288
289 return EFI_SUCCESS;
290 }
291
292 /**
293 This function update console variable based on ConVarName, it can
294 add or remove one specific console device path from the variable
295
296 @param ConVarName Console related variable name, ConIn, ConOut,
297 ErrOut.
298 @param CustomizedConDevicePath The console device path which will be added to
299 the console variable ConVarName, this parameter
300 can not be multi-instance.
301 @param ExclusiveDevicePath The console device path which will be removed
302 from the console variable ConVarName, this
303 parameter can not be multi-instance.
304
305 @retval EFI_UNSUPPORTED The added device path is same to the removed one.
306 @retval EFI_SUCCESS Success add or remove the device path from the
307 console variable.
308
309 **/
310 EFI_STATUS
311 EFIAPI
312 BdsLibUpdateConsoleVariable (
313 IN CHAR16 *ConVarName,
314 IN EFI_DEVICE_PATH_PROTOCOL *CustomizedConDevicePath,
315 IN EFI_DEVICE_PATH_PROTOCOL *ExclusiveDevicePath
316 )
317 {
318 EFI_STATUS Status;
319 EFI_DEVICE_PATH_PROTOCOL *VarConsole;
320 UINTN DevicePathSize;
321 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
322 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;
323 UINT32 Attributes;
324
325 VarConsole = NULL;
326 DevicePathSize = 0;
327
328 //
329 // Notes: check the device path point, here should check
330 // with compare memory
331 //
332 if (CustomizedConDevicePath == ExclusiveDevicePath) {
333 return EFI_UNSUPPORTED;
334 }
335 //
336 // Delete the ExclusiveDevicePath from current default console
337 //
338 VarConsole = BdsLibGetVariableAndSize (
339 ConVarName,
340 &gEfiGlobalVariableGuid,
341 &DevicePathSize
342 );
343
344 //
345 // Initialize NewDevicePath
346 //
347 NewDevicePath = VarConsole;
348
349 //
350 // If ExclusiveDevicePath is even the part of the instance in VarConsole, delete it.
351 // In the end, NewDevicePath is the final device path.
352 //
353 if (ExclusiveDevicePath != NULL && VarConsole != NULL) {
354 NewDevicePath = BdsLibDelPartMatchInstance (VarConsole, ExclusiveDevicePath);
355 }
356 //
357 // Try to append customized device path to NewDevicePath.
358 //
359 if (CustomizedConDevicePath != NULL) {
360 if (!BdsLibMatchDevicePaths (NewDevicePath, CustomizedConDevicePath)) {
361 //
362 // Check if there is part of CustomizedConDevicePath in NewDevicePath, delete it.
363 //
364 NewDevicePath = BdsLibDelPartMatchInstance (NewDevicePath, CustomizedConDevicePath);
365 //
366 // In the first check, the default console variable will be _ModuleEntryPoint,
367 // just append current customized device path
368 //
369 TempNewDevicePath = NewDevicePath;
370 NewDevicePath = AppendDevicePathInstance (NewDevicePath, CustomizedConDevicePath);
371 if (TempNewDevicePath != NULL) {
372 FreePool(TempNewDevicePath);
373 }
374 }
375 }
376
377 //
378 // The attribute for ConInDev, ConOutDev and ErrOutDev does not include NV.
379 //
380 if (IsNvNeed(ConVarName)) {
381 //
382 // ConVarName has NV attribute.
383 //
384 Attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE;
385 } else {
386 //
387 // ConVarName does not have NV attribute.
388 //
389 Attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
390 }
391
392 //
393 // Finally, Update the variable of the default console by NewDevicePath
394 //
395 DevicePathSize = GetDevicePathSize (NewDevicePath);
396 Status = gRT->SetVariable (
397 ConVarName,
398 &gEfiGlobalVariableGuid,
399 Attributes,
400 DevicePathSize,
401 NewDevicePath
402 );
403 if ((DevicePathSize == 0) && (Status == EFI_NOT_FOUND)) {
404 Status = EFI_SUCCESS;
405 }
406 ASSERT_EFI_ERROR (Status);
407
408 if (VarConsole == NewDevicePath) {
409 if (VarConsole != NULL) {
410 FreePool(VarConsole);
411 }
412 } else {
413 if (VarConsole != NULL) {
414 FreePool(VarConsole);
415 }
416 if (NewDevicePath != NULL) {
417 FreePool(NewDevicePath);
418 }
419 }
420
421 return Status;
422
423 }
424
425
426 /**
427 Connect the console device base on the variable ConVarName, if
428 device path of the ConVarName is multi-instance device path and
429 anyone of the instances is connected success, then this function
430 will return success.
431 If the handle associate with one device path node can not
432 be created successfully, then still give chance to do the dispatch,
433 which load the missing drivers if possible..
434
435 @param ConVarName Console related variable name, ConIn, ConOut,
436 ErrOut.
437
438 @retval EFI_NOT_FOUND There is not any console devices connected
439 success
440 @retval EFI_SUCCESS Success connect any one instance of the console
441 device path base on the variable ConVarName.
442
443 **/
444 EFI_STATUS
445 EFIAPI
446 BdsLibConnectConsoleVariable (
447 IN CHAR16 *ConVarName
448 )
449 {
450 return ConnectConsoleVariableInternal(ConVarName, TRUE);
451 }
452
453 /**
454 Connect the console device base on the variable ConVarName, if
455 device path of the ConVarName is multi-instance device path and
456 anyone of the instances is connected success, then this function
457 will return success.
458 Dispatch service is not called when the handle associate with one
459 device path node can not be created successfully. Here no driver
460 dependency is assumed exist, so need not to call this service.
461
462
463 @param ConVarName Console related variable name, ConIn, ConOut,
464 ErrOut.
465
466 @retval EFI_NOT_FOUND There is not any console devices connected
467 success
468 @retval EFI_SUCCESS Success connect any one instance of the console
469 device path base on the variable ConVarName.
470
471 **/
472 EFI_STATUS
473 EFIAPI
474 BdsLibConnectConsoleVariableWithOutDispatch (
475 IN CHAR16 *ConVarName
476 )
477 {
478 return ConnectConsoleVariableInternal(ConVarName, FALSE);
479 }
480
481 /**
482 This function will search every simpletext device in current system,
483 and make every simpletext device as pertantial console device.
484
485 **/
486 VOID
487 EFIAPI
488 BdsLibConnectAllConsoles (
489 VOID
490 )
491 {
492 UINTN Index;
493 EFI_DEVICE_PATH_PROTOCOL *ConDevicePath;
494 UINTN HandleCount;
495 EFI_HANDLE *HandleBuffer;
496
497 Index = 0;
498 HandleCount = 0;
499 HandleBuffer = NULL;
500 ConDevicePath = NULL;
501
502 //
503 // Update all the console variables
504 //
505 gBS->LocateHandleBuffer (
506 ByProtocol,
507 &gEfiSimpleTextInProtocolGuid,
508 NULL,
509 &HandleCount,
510 &HandleBuffer
511 );
512
513 for (Index = 0; Index < HandleCount; Index++) {
514 gBS->HandleProtocol (
515 HandleBuffer[Index],
516 &gEfiDevicePathProtocolGuid,
517 (VOID **) &ConDevicePath
518 );
519 BdsLibUpdateConsoleVariable (L"ConIn", ConDevicePath, NULL);
520 }
521
522 if (HandleBuffer != NULL) {
523 FreePool(HandleBuffer);
524 HandleBuffer = NULL;
525 }
526
527 gBS->LocateHandleBuffer (
528 ByProtocol,
529 &gEfiSimpleTextOutProtocolGuid,
530 NULL,
531 &HandleCount,
532 &HandleBuffer
533 );
534 for (Index = 0; Index < HandleCount; Index++) {
535 gBS->HandleProtocol (
536 HandleBuffer[Index],
537 &gEfiDevicePathProtocolGuid,
538 (VOID **) &ConDevicePath
539 );
540 BdsLibUpdateConsoleVariable (L"ConOut", ConDevicePath, NULL);
541 BdsLibUpdateConsoleVariable (L"ErrOut", ConDevicePath, NULL);
542 }
543
544 if (HandleBuffer != NULL) {
545 FreePool(HandleBuffer);
546 }
547
548 //
549 // Connect all console variables
550 //
551 BdsLibConnectAllDefaultConsoles ();
552
553 }
554
555 /**
556 This function will connect console device base on the console
557 device variable ConIn, ConOut and ErrOut.
558
559 @retval EFI_SUCCESS At least one of the ConIn and ConOut device have
560 been connected success.
561 @retval EFI_STATUS Return the status of BdsLibConnectConsoleVariable ().
562
563 **/
564 EFI_STATUS
565 EFIAPI
566 BdsLibConnectAllDefaultConsoles (
567 VOID
568 )
569 {
570 EFI_STATUS Status;
571 BOOLEAN SystemTableUpdated;
572
573 //
574 // Connect all default console variables
575 //
576
577 //
578 // It seems impossible not to have any ConOut device on platform,
579 // so we check the status here.
580 //
581 Status = BdsLibConnectConsoleVariable (L"ConOut");
582 if (EFI_ERROR (Status)) {
583 return Status;
584 }
585
586 //
587 // Insert the performance probe for Console Out
588 //
589 PERF_START (NULL, "ConOut", "BDS", 1);
590 PERF_END (NULL, "ConOut", "BDS", 0);
591
592 //
593 // Because possibly the platform is legacy free, in such case,
594 // ConIn devices (Serial Port and PS2 Keyboard ) does not exist,
595 // so we need not check the status.
596 //
597 BdsLibConnectConsoleVariable (L"ConIn");
598
599 //
600 // The _ModuleEntryPoint err out var is legal.
601 //
602 BdsLibConnectConsoleVariable (L"ErrOut");
603
604 SystemTableUpdated = FALSE;
605 //
606 // Fill console handles in System Table if no console device assignd.
607 //
608 if (UpdateSystemTableConsole (L"ConIn", &gEfiSimpleTextInProtocolGuid, &gST->ConsoleInHandle, (VOID **) &gST->ConIn)) {
609 SystemTableUpdated = TRUE;
610 }
611 if (UpdateSystemTableConsole (L"ConOut", &gEfiSimpleTextOutProtocolGuid, &gST->ConsoleOutHandle, (VOID **) &gST->ConOut)) {
612 SystemTableUpdated = TRUE;
613 }
614 if (UpdateSystemTableConsole (L"ErrOut", &gEfiSimpleTextOutProtocolGuid, &gST->StandardErrorHandle, (VOID **) &gST->StdErr)) {
615 SystemTableUpdated = TRUE;
616 }
617
618 if (SystemTableUpdated) {
619 //
620 // Update the CRC32 in the EFI System Table header
621 //
622 gST->Hdr.CRC32 = 0;
623 gBS->CalculateCrc32 (
624 (UINT8 *) &gST->Hdr,
625 gST->Hdr.HeaderSize,
626 &gST->Hdr.CRC32
627 );
628 }
629
630 return EFI_SUCCESS;
631
632 }
633
634 /**
635 This function will connect console device except ConIn base on the console
636 device variable ConOut and ErrOut.
637
638 @retval EFI_SUCCESS At least one of the ConOut device have
639 been connected success.
640 @retval EFI_STATUS Return the status of BdsLibConnectConsoleVariable ().
641
642 **/
643 EFI_STATUS
644 EFIAPI
645 BdsLibConnectAllDefaultConsolesWithOutConIn (
646 VOID
647 )
648 {
649 EFI_STATUS Status;
650 BOOLEAN SystemTableUpdated;
651
652 //
653 // Connect all default console variables except ConIn
654 //
655
656 //
657 // It seems impossible not to have any ConOut device on platform,
658 // so we check the status here.
659 //
660 Status = BdsLibConnectConsoleVariable (L"ConOut");
661 if (EFI_ERROR (Status)) {
662 return Status;
663 }
664
665 //
666 // Insert the performance probe for Console Out
667 //
668 PERF_START (NULL, "ConOut", "BDS", 1);
669 PERF_END (NULL, "ConOut", "BDS", 0);
670
671 //
672 // The _ModuleEntryPoint err out var is legal.
673 //
674 BdsLibConnectConsoleVariable (L"ErrOut");
675
676 SystemTableUpdated = FALSE;
677 //
678 // Fill console handles in System Table if no console device assignd.
679 //
680 if (UpdateSystemTableConsole (L"ConOut", &gEfiSimpleTextOutProtocolGuid, &gST->ConsoleOutHandle, (VOID **) &gST->ConOut)) {
681 SystemTableUpdated = TRUE;
682 }
683 if (UpdateSystemTableConsole (L"ErrOut", &gEfiSimpleTextOutProtocolGuid, &gST->StandardErrorHandle, (VOID **) &gST->StdErr)) {
684 SystemTableUpdated = TRUE;
685 }
686
687 if (SystemTableUpdated) {
688 //
689 // Update the CRC32 in the EFI System Table header
690 //
691 gST->Hdr.CRC32 = 0;
692 gBS->CalculateCrc32 (
693 (UINT8 *) &gST->Hdr,
694 gST->Hdr.HeaderSize,
695 &gST->Hdr.CRC32
696 );
697 }
698
699 return EFI_SUCCESS;
700
701 }
702
703 /**
704 Convert a *.BMP graphics image to a GOP blt buffer. If a NULL Blt buffer
705 is passed in a GopBlt buffer will be allocated by this routine. If a GopBlt
706 buffer is passed in it will be used if it is big enough.
707
708 @param BmpImage Pointer to BMP file
709 @param BmpImageSize Number of bytes in BmpImage
710 @param GopBlt Buffer containing GOP version of BmpImage.
711 @param GopBltSize Size of GopBlt in bytes.
712 @param PixelHeight Height of GopBlt/BmpImage in pixels
713 @param PixelWidth Width of GopBlt/BmpImage in pixels
714
715 @retval EFI_SUCCESS GopBlt and GopBltSize are returned.
716 @retval EFI_UNSUPPORTED BmpImage is not a valid *.BMP image
717 @retval EFI_BUFFER_TOO_SMALL The passed in GopBlt buffer is not big enough.
718 GopBltSize will contain the required size.
719 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate.
720
721 **/
722 EFI_STATUS
723 ConvertBmpToGopBlt (
724 IN VOID *BmpImage,
725 IN UINTN BmpImageSize,
726 IN OUT VOID **GopBlt,
727 IN OUT UINTN *GopBltSize,
728 OUT UINTN *PixelHeight,
729 OUT UINTN *PixelWidth
730 )
731 {
732 UINT8 *Image;
733 UINT8 *ImageHeader;
734 BMP_IMAGE_HEADER *BmpHeader;
735 BMP_COLOR_MAP *BmpColorMap;
736 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer;
737 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;
738 UINT64 BltBufferSize;
739 UINTN Index;
740 UINTN Height;
741 UINTN Width;
742 UINTN ImageIndex;
743 UINT32 DataSizePerLine;
744 BOOLEAN IsAllocated;
745 UINT32 ColorMapNum;
746
747 if (sizeof (BMP_IMAGE_HEADER) > BmpImageSize) {
748 return EFI_INVALID_PARAMETER;
749 }
750
751 BmpHeader = (BMP_IMAGE_HEADER *) BmpImage;
752
753 if (BmpHeader->CharB != 'B' || BmpHeader->CharM != 'M') {
754 return EFI_UNSUPPORTED;
755 }
756
757 //
758 // Doesn't support compress.
759 //
760 if (BmpHeader->CompressionType != 0) {
761 return EFI_UNSUPPORTED;
762 }
763
764 //
765 // Only support BITMAPINFOHEADER format.
766 // BITMAPFILEHEADER + BITMAPINFOHEADER = BMP_IMAGE_HEADER
767 //
768 if (BmpHeader->HeaderSize != sizeof (BMP_IMAGE_HEADER) - OFFSET_OF(BMP_IMAGE_HEADER, HeaderSize)) {
769 return EFI_UNSUPPORTED;
770 }
771
772 //
773 // The data size in each line must be 4 byte alignment.
774 //
775 DataSizePerLine = ((BmpHeader->PixelWidth * BmpHeader->BitPerPixel + 31) >> 3) & (~0x3);
776 BltBufferSize = MultU64x32 (DataSizePerLine, BmpHeader->PixelHeight);
777 if (BltBufferSize > (UINT32) ~0) {
778 return EFI_INVALID_PARAMETER;
779 }
780
781 if ((BmpHeader->Size != BmpImageSize) ||
782 (BmpHeader->Size < BmpHeader->ImageOffset) ||
783 (BmpHeader->Size - BmpHeader->ImageOffset != BmpHeader->PixelHeight * DataSizePerLine)) {
784 return EFI_INVALID_PARAMETER;
785 }
786
787 //
788 // Calculate Color Map offset in the image.
789 //
790 Image = BmpImage;
791 BmpColorMap = (BMP_COLOR_MAP *) (Image + sizeof (BMP_IMAGE_HEADER));
792 if (BmpHeader->ImageOffset < sizeof (BMP_IMAGE_HEADER)) {
793 return EFI_INVALID_PARAMETER;
794 }
795
796 if (BmpHeader->ImageOffset > sizeof (BMP_IMAGE_HEADER)) {
797 switch (BmpHeader->BitPerPixel) {
798 case 1:
799 ColorMapNum = 2;
800 break;
801 case 4:
802 ColorMapNum = 16;
803 break;
804 case 8:
805 ColorMapNum = 256;
806 break;
807 default:
808 ColorMapNum = 0;
809 break;
810 }
811 if (BmpHeader->ImageOffset - sizeof (BMP_IMAGE_HEADER) != sizeof (BMP_COLOR_MAP) * ColorMapNum) {
812 return EFI_INVALID_PARAMETER;
813 }
814 }
815
816 //
817 // Calculate graphics image data address in the image
818 //
819 Image = ((UINT8 *) BmpImage) + BmpHeader->ImageOffset;
820 ImageHeader = Image;
821
822 //
823 // Calculate the BltBuffer needed size.
824 //
825 BltBufferSize = MultU64x32 ((UINT64) BmpHeader->PixelWidth, BmpHeader->PixelHeight);
826 //
827 // Ensure the BltBufferSize * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) doesn't overflow
828 //
829 if (BltBufferSize > DivU64x32 ((UINTN) ~0, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL))) {
830 return EFI_UNSUPPORTED;
831 }
832 BltBufferSize = MultU64x32 (BltBufferSize, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
833
834 IsAllocated = FALSE;
835 if (*GopBlt == NULL) {
836 //
837 // GopBlt is not allocated by caller.
838 //
839 *GopBltSize = (UINTN) BltBufferSize;
840 *GopBlt = AllocatePool (*GopBltSize);
841 IsAllocated = TRUE;
842 if (*GopBlt == NULL) {
843 return EFI_OUT_OF_RESOURCES;
844 }
845 } else {
846 //
847 // GopBlt has been allocated by caller.
848 //
849 if (*GopBltSize < (UINTN) BltBufferSize) {
850 *GopBltSize = (UINTN) BltBufferSize;
851 return EFI_BUFFER_TOO_SMALL;
852 }
853 }
854
855 *PixelWidth = BmpHeader->PixelWidth;
856 *PixelHeight = BmpHeader->PixelHeight;
857
858 //
859 // Convert image from BMP to Blt buffer format
860 //
861 BltBuffer = *GopBlt;
862 for (Height = 0; Height < BmpHeader->PixelHeight; Height++) {
863 Blt = &BltBuffer[(BmpHeader->PixelHeight - Height - 1) * BmpHeader->PixelWidth];
864 for (Width = 0; Width < BmpHeader->PixelWidth; Width++, Image++, Blt++) {
865 switch (BmpHeader->BitPerPixel) {
866 case 1:
867 //
868 // Convert 1-bit (2 colors) BMP to 24-bit color
869 //
870 for (Index = 0; Index < 8 && Width < BmpHeader->PixelWidth; Index++) {
871 Blt->Red = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Red;
872 Blt->Green = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Green;
873 Blt->Blue = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Blue;
874 Blt++;
875 Width++;
876 }
877
878 Blt--;
879 Width--;
880 break;
881
882 case 4:
883 //
884 // Convert 4-bit (16 colors) BMP Palette to 24-bit color
885 //
886 Index = (*Image) >> 4;
887 Blt->Red = BmpColorMap[Index].Red;
888 Blt->Green = BmpColorMap[Index].Green;
889 Blt->Blue = BmpColorMap[Index].Blue;
890 if (Width < (BmpHeader->PixelWidth - 1)) {
891 Blt++;
892 Width++;
893 Index = (*Image) & 0x0f;
894 Blt->Red = BmpColorMap[Index].Red;
895 Blt->Green = BmpColorMap[Index].Green;
896 Blt->Blue = BmpColorMap[Index].Blue;
897 }
898 break;
899
900 case 8:
901 //
902 // Convert 8-bit (256 colors) BMP Palette to 24-bit color
903 //
904 Blt->Red = BmpColorMap[*Image].Red;
905 Blt->Green = BmpColorMap[*Image].Green;
906 Blt->Blue = BmpColorMap[*Image].Blue;
907 break;
908
909 case 24:
910 //
911 // It is 24-bit BMP.
912 //
913 Blt->Blue = *Image++;
914 Blt->Green = *Image++;
915 Blt->Red = *Image;
916 break;
917
918 default:
919 //
920 // Other bit format BMP is not supported.
921 //
922 if (IsAllocated) {
923 FreePool (*GopBlt);
924 *GopBlt = NULL;
925 }
926 return EFI_UNSUPPORTED;
927 break;
928 };
929
930 }
931
932 ImageIndex = (UINTN) (Image - ImageHeader);
933 if ((ImageIndex % 4) != 0) {
934 //
935 // Bmp Image starts each row on a 32-bit boundary!
936 //
937 Image = Image + (4 - (ImageIndex % 4));
938 }
939 }
940
941 return EFI_SUCCESS;
942 }
943
944 /**
945 Use SystemTable Conout to stop video based Simple Text Out consoles from going
946 to the video device. Put up LogoFile on every video device that is a console.
947
948 @param[in] LogoFile File name of logo to display on the center of the screen.
949
950 @retval EFI_SUCCESS ConsoleControl has been flipped to graphics and logo displayed.
951 @retval EFI_UNSUPPORTED Logo not found
952
953 **/
954 EFI_STATUS
955 EFIAPI
956 EnableQuietBoot (
957 IN EFI_GUID *LogoFile
958 )
959 {
960 EFI_STATUS Status;
961 EFI_OEM_BADGING_PROTOCOL *Badging;
962 UINT32 SizeOfX;
963 UINT32 SizeOfY;
964 INTN DestX;
965 INTN DestY;
966 UINT8 *ImageData;
967 UINTN ImageSize;
968 UINTN BltSize;
969 UINT32 Instance;
970 EFI_BADGING_FORMAT Format;
971 EFI_BADGING_DISPLAY_ATTRIBUTE Attribute;
972 UINTN CoordinateX;
973 UINTN CoordinateY;
974 UINTN Height;
975 UINTN Width;
976 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;
977 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
978 UINT32 ColorDepth;
979 UINT32 RefreshRate;
980 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
981 EFI_BOOT_LOGO_PROTOCOL *BootLogo;
982 UINTN NumberOfLogos;
983 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *LogoBlt;
984 UINTN LogoDestX;
985 UINTN LogoDestY;
986 UINTN LogoHeight;
987 UINTN LogoWidth;
988 UINTN NewDestX;
989 UINTN NewDestY;
990 UINTN NewHeight;
991 UINTN NewWidth;
992 UINT64 BufferSize;
993
994 UgaDraw = NULL;
995 //
996 // Try to open GOP first
997 //
998 Status = gBS->HandleProtocol (gST->ConsoleOutHandle, &gEfiGraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput);
999 if (EFI_ERROR (Status) && FeaturePcdGet (PcdUgaConsumeSupport)) {
1000 GraphicsOutput = NULL;
1001 //
1002 // Open GOP failed, try to open UGA
1003 //
1004 Status = gBS->HandleProtocol (gST->ConsoleOutHandle, &gEfiUgaDrawProtocolGuid, (VOID **) &UgaDraw);
1005 }
1006 if (EFI_ERROR (Status)) {
1007 return EFI_UNSUPPORTED;
1008 }
1009
1010 //
1011 // Try to open Boot Logo Protocol.
1012 //
1013 BootLogo = NULL;
1014 gBS->LocateProtocol (&gEfiBootLogoProtocolGuid, NULL, (VOID **) &BootLogo);
1015
1016 //
1017 // Erase Cursor from screen
1018 //
1019 gST->ConOut->EnableCursor (gST->ConOut, FALSE);
1020
1021 Badging = NULL;
1022 Status = gBS->LocateProtocol (&gEfiOEMBadgingProtocolGuid, NULL, (VOID **) &Badging);
1023
1024 if (GraphicsOutput != NULL) {
1025 SizeOfX = GraphicsOutput->Mode->Info->HorizontalResolution;
1026 SizeOfY = GraphicsOutput->Mode->Info->VerticalResolution;
1027
1028 } else if (UgaDraw != NULL && FeaturePcdGet (PcdUgaConsumeSupport)) {
1029 Status = UgaDraw->GetMode (UgaDraw, &SizeOfX, &SizeOfY, &ColorDepth, &RefreshRate);
1030 if (EFI_ERROR (Status)) {
1031 return EFI_UNSUPPORTED;
1032 }
1033 } else {
1034 return EFI_UNSUPPORTED;
1035 }
1036
1037 Blt = NULL;
1038 NumberOfLogos = 0;
1039 LogoDestX = 0;
1040 LogoDestY = 0;
1041 LogoHeight = 0;
1042 LogoWidth = 0;
1043 NewDestX = 0;
1044 NewDestY = 0;
1045 NewHeight = 0;
1046 NewWidth = 0;
1047 Instance = 0;
1048 while (1) {
1049 ImageData = NULL;
1050 ImageSize = 0;
1051
1052 if (Badging != NULL) {
1053 //
1054 // Get image from OEMBadging protocol.
1055 //
1056 Status = Badging->GetImage (
1057 Badging,
1058 &Instance,
1059 &Format,
1060 &ImageData,
1061 &ImageSize,
1062 &Attribute,
1063 &CoordinateX,
1064 &CoordinateY
1065 );
1066 if (EFI_ERROR (Status)) {
1067 goto Done;
1068 }
1069
1070 //
1071 // Currently only support BMP format.
1072 //
1073 if (Format != EfiBadgingFormatBMP) {
1074 if (ImageData != NULL) {
1075 FreePool (ImageData);
1076 }
1077 continue;
1078 }
1079 } else {
1080 //
1081 // Get the specified image from FV.
1082 //
1083 Status = GetSectionFromAnyFv (LogoFile, EFI_SECTION_RAW, 0, (VOID **) &ImageData, &ImageSize);
1084 if (EFI_ERROR (Status)) {
1085 return EFI_UNSUPPORTED;
1086 }
1087
1088 CoordinateX = 0;
1089 CoordinateY = 0;
1090 if (!FeaturePcdGet(PcdBootlogoOnlyEnable)) {
1091 Attribute = EfiBadgingDisplayAttributeCenter;
1092 } else {
1093 Attribute = EfiBadgingDisplayAttributeCustomized;
1094 }
1095 }
1096
1097 if (Blt != NULL) {
1098 FreePool (Blt);
1099 }
1100 Blt = NULL;
1101 Status = ConvertBmpToGopBlt (
1102 ImageData,
1103 ImageSize,
1104 (VOID **) &Blt,
1105 &BltSize,
1106 &Height,
1107 &Width
1108 );
1109 if (EFI_ERROR (Status)) {
1110 FreePool (ImageData);
1111
1112 if (Badging == NULL) {
1113 return Status;
1114 } else {
1115 continue;
1116 }
1117 }
1118
1119 //
1120 // Calculate the display position according to Attribute.
1121 //
1122 switch (Attribute) {
1123 case EfiBadgingDisplayAttributeLeftTop:
1124 DestX = CoordinateX;
1125 DestY = CoordinateY;
1126 break;
1127
1128 case EfiBadgingDisplayAttributeCenterTop:
1129 DestX = (SizeOfX - Width) / 2;
1130 DestY = CoordinateY;
1131 break;
1132
1133 case EfiBadgingDisplayAttributeRightTop:
1134 DestX = (SizeOfX - Width - CoordinateX);
1135 DestY = CoordinateY;;
1136 break;
1137
1138 case EfiBadgingDisplayAttributeCenterRight:
1139 DestX = (SizeOfX - Width - CoordinateX);
1140 DestY = (SizeOfY - Height) / 2;
1141 break;
1142
1143 case EfiBadgingDisplayAttributeRightBottom:
1144 DestX = (SizeOfX - Width - CoordinateX);
1145 DestY = (SizeOfY - Height - CoordinateY);
1146 break;
1147
1148 case EfiBadgingDisplayAttributeCenterBottom:
1149 DestX = (SizeOfX - Width) / 2;
1150 DestY = (SizeOfY - Height - CoordinateY);
1151 break;
1152
1153 case EfiBadgingDisplayAttributeLeftBottom:
1154 DestX = CoordinateX;
1155 DestY = (SizeOfY - Height - CoordinateY);
1156 break;
1157
1158 case EfiBadgingDisplayAttributeCenterLeft:
1159 DestX = CoordinateX;
1160 DestY = (SizeOfY - Height) / 2;
1161 break;
1162
1163 case EfiBadgingDisplayAttributeCenter:
1164 DestX = (SizeOfX - Width) / 2;
1165 DestY = (SizeOfY - Height) / 2;
1166 break;
1167
1168 case EfiBadgingDisplayAttributeCustomized:
1169 DestX = (SizeOfX - Width) / 2;
1170 DestY = ((SizeOfY * 382) / 1000) - Height / 2;
1171 break;
1172
1173 default:
1174 DestX = CoordinateX;
1175 DestY = CoordinateY;
1176 break;
1177 }
1178
1179 if ((DestX >= 0) && (DestY >= 0)) {
1180 if (GraphicsOutput != NULL) {
1181 Status = GraphicsOutput->Blt (
1182 GraphicsOutput,
1183 Blt,
1184 EfiBltBufferToVideo,
1185 0,
1186 0,
1187 (UINTN) DestX,
1188 (UINTN) DestY,
1189 Width,
1190 Height,
1191 Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
1192 );
1193 } else if (UgaDraw != NULL && FeaturePcdGet (PcdUgaConsumeSupport)) {
1194 Status = UgaDraw->Blt (
1195 UgaDraw,
1196 (EFI_UGA_PIXEL *) Blt,
1197 EfiUgaBltBufferToVideo,
1198 0,
1199 0,
1200 (UINTN) DestX,
1201 (UINTN) DestY,
1202 Width,
1203 Height,
1204 Width * sizeof (EFI_UGA_PIXEL)
1205 );
1206 } else {
1207 Status = EFI_UNSUPPORTED;
1208 }
1209
1210 //
1211 // Report displayed Logo information.
1212 //
1213 if (!EFI_ERROR (Status)) {
1214 NumberOfLogos++;
1215
1216 if (LogoWidth == 0) {
1217 //
1218 // The first Logo.
1219 //
1220 LogoDestX = (UINTN) DestX;
1221 LogoDestY = (UINTN) DestY;
1222 LogoWidth = Width;
1223 LogoHeight = Height;
1224 } else {
1225 //
1226 // Merge new logo with old one.
1227 //
1228 NewDestX = MIN ((UINTN) DestX, LogoDestX);
1229 NewDestY = MIN ((UINTN) DestY, LogoDestY);
1230 NewWidth = MAX ((UINTN) DestX + Width, LogoDestX + LogoWidth) - NewDestX;
1231 NewHeight = MAX ((UINTN) DestY + Height, LogoDestY + LogoHeight) - NewDestY;
1232
1233 LogoDestX = NewDestX;
1234 LogoDestY = NewDestY;
1235 LogoWidth = NewWidth;
1236 LogoHeight = NewHeight;
1237 }
1238 }
1239 }
1240
1241 FreePool (ImageData);
1242
1243 if (Badging == NULL) {
1244 break;
1245 }
1246 }
1247
1248 Done:
1249 if (BootLogo == NULL || NumberOfLogos == 0) {
1250 //
1251 // No logo displayed.
1252 //
1253 if (Blt != NULL) {
1254 FreePool (Blt);
1255 }
1256
1257 return Status;
1258 }
1259
1260 //
1261 // Advertise displayed Logo information.
1262 //
1263 if (NumberOfLogos == 1) {
1264 //
1265 // Only one logo displayed, use its Blt buffer directly for BootLogo protocol.
1266 //
1267 LogoBlt = Blt;
1268 Status = EFI_SUCCESS;
1269 } else {
1270 //
1271 // More than one Logo displayed, get merged BltBuffer using VideoToBuffer operation.
1272 //
1273 if (Blt != NULL) {
1274 FreePool (Blt);
1275 }
1276
1277 //
1278 // Ensure the LogoHeight * LogoWidth doesn't overflow
1279 //
1280 if (LogoHeight > DivU64x64Remainder ((UINTN) ~0, LogoWidth, NULL)) {
1281 return EFI_UNSUPPORTED;
1282 }
1283 BufferSize = MultU64x64 (LogoWidth, LogoHeight);
1284
1285 //
1286 // Ensure the BufferSize * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) doesn't overflow
1287 //
1288 if (BufferSize > DivU64x32 ((UINTN) ~0, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL))) {
1289 return EFI_UNSUPPORTED;
1290 }
1291
1292 LogoBlt = AllocateZeroPool ((UINTN)BufferSize * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
1293 if (LogoBlt == NULL) {
1294 return EFI_OUT_OF_RESOURCES;
1295 }
1296
1297 if (GraphicsOutput != NULL) {
1298 Status = GraphicsOutput->Blt (
1299 GraphicsOutput,
1300 LogoBlt,
1301 EfiBltVideoToBltBuffer,
1302 LogoDestX,
1303 LogoDestY,
1304 0,
1305 0,
1306 LogoWidth,
1307 LogoHeight,
1308 LogoWidth * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
1309 );
1310 } else if (UgaDraw != NULL && FeaturePcdGet (PcdUgaConsumeSupport)) {
1311 Status = UgaDraw->Blt (
1312 UgaDraw,
1313 (EFI_UGA_PIXEL *) LogoBlt,
1314 EfiUgaVideoToBltBuffer,
1315 LogoDestX,
1316 LogoDestY,
1317 0,
1318 0,
1319 LogoWidth,
1320 LogoHeight,
1321 LogoWidth * sizeof (EFI_UGA_PIXEL)
1322 );
1323 } else {
1324 Status = EFI_UNSUPPORTED;
1325 }
1326 }
1327
1328 if (!EFI_ERROR (Status)) {
1329 BootLogo->SetBootLogo (BootLogo, LogoBlt, LogoDestX, LogoDestY, LogoWidth, LogoHeight);
1330 }
1331 FreePool (LogoBlt);
1332
1333 return Status;
1334 }
1335
1336 /**
1337 Use SystemTable Conout to turn on video based Simple Text Out consoles. The
1338 Simple Text Out screens will now be synced up with all non video output devices
1339
1340 @retval EFI_SUCCESS UGA devices are back in text mode and synced up.
1341
1342 **/
1343 EFI_STATUS
1344 EFIAPI
1345 DisableQuietBoot (
1346 VOID
1347 )
1348 {
1349
1350 //
1351 // Enable Cursor on Screen
1352 //
1353 gST->ConOut->EnableCursor (gST->ConOut, TRUE);
1354 return EFI_SUCCESS;
1355 }
1356