]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/GraphicsLib/Graphics.c
Remove the unnecessary include, such as PiDxe.h, PiPei.h, Base.h and Uefi.h in header...
[mirror_edk2.git] / MdeModulePkg / Library / GraphicsLib / Graphics.c
1 /** @file
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.
6
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
12
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.
15
16 **/
17
18
19 #include <PiDxe.h>
20
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>
28
29 #include <Guid/Bmp.h>
30
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/DxePiLib.h>
39 #include <Library/PcdLib.h>
40
41 STATIC EFI_GRAPHICS_OUTPUT_BLT_PIXEL mEfiColors[16] = {
42 { 0x00, 0x00, 0x00, 0x00 },
43 { 0x98, 0x00, 0x00, 0x00 },
44 { 0x00, 0x98, 0x00, 0x00 },
45 { 0x98, 0x98, 0x00, 0x00 },
46 { 0x00, 0x00, 0x98, 0x00 },
47 { 0x98, 0x00, 0x98, 0x00 },
48 { 0x00, 0x98, 0x98, 0x00 },
49 { 0x98, 0x98, 0x98, 0x00 },
50 { 0x10, 0x10, 0x10, 0x00 },
51 { 0xff, 0x10, 0x10, 0x00 },
52 { 0x10, 0xff, 0x10, 0x00 },
53 { 0xff, 0xff, 0x10, 0x00 },
54 { 0x10, 0x10, 0xff, 0x00 },
55 { 0xf0, 0x10, 0xff, 0x00 },
56 { 0x10, 0xff, 0xff, 0x00 },
57 { 0xff, 0xff, 0xff, 0x00 }
58 };
59
60
61 /**
62 Return the graphics image file named FileNameGuid into Image and return it's
63 size in ImageSize. All Firmware Volumes (FV) in the system are searched for the
64 file name.
65
66 @param FileNameGuid File Name of graphics file in the FV(s).
67 @param Image Pointer to pointer to return graphics image. If NULL, a
68 buffer will be allocated.
69 @param ImageSize Size of the graphics Image in bytes. Zero if no image found.
70
71 @retval EFI_SUCCESS Image and ImageSize are valid.
72 @retval EFI_BUFFER_TOO_SMALL Image not big enough. ImageSize has required size
73 @retval EFI_NOT_FOUND FileNameGuid not found
74
75 **/
76 EFI_STATUS
77 EFIAPI
78 GetGraphicsBitMapFromFV (
79 IN EFI_GUID *FileNameGuid,
80 OUT VOID **Image,
81 OUT UINTN *ImageSize
82 )
83 {
84 return GetGraphicsBitMapFromFVEx (NULL, FileNameGuid, Image, ImageSize);
85 }
86
87 /**
88 Return the graphics image file named FileNameGuid into Image and return it's
89 size in ImageSize. All Firmware Volumes (FV) in the system are searched for the
90 file name.
91
92 @param ImageHandle The driver image handle of the caller. The parameter is used to
93 optimize the loading of the image file so that the FV from which
94 the driver image is loaded will be tried first.
95 @param FileNameGuid File Name of graphics file in the FV(s).
96 @param Image Pointer to pointer to return graphics image. If NULL, a
97 buffer will be allocated.
98 @param ImageSize Size of the graphics Image in bytes. Zero if no image found.
99
100 @retval EFI_SUCCESS Image and ImageSize are valid.
101 @retval EFI_BUFFER_TOO_SMALL Image not big enough. ImageSize has required size
102 @retval EFI_NOT_FOUND FileNameGuid not found
103
104 **/
105 EFI_STATUS
106 EFIAPI
107 GetGraphicsBitMapFromFVEx (
108 IN EFI_HANDLE ImageHandle,
109 IN EFI_GUID *FileNameGuid,
110 OUT VOID **Image,
111 OUT UINTN *ImageSize
112 )
113 {
114 return PiLibGetSectionFromAnyFv (
115 FileNameGuid,
116 EFI_SECTION_RAW,
117 0,
118 Image,
119 ImageSize
120 );
121 }
122
123 /**
124 Convert a *.BMP graphics image to a GOP blt buffer. If a NULL Blt buffer
125 is passed in a GopBlt buffer will be allocated by this routine. If a GopBlt
126 buffer is passed in it will be used if it is big enough.
127
128 @param BmpImage Pointer to BMP file
129 @param BmpImageSize Number of bytes in BmpImage
130 @param GopBlt Buffer containing GOP version of BmpImage.
131 @param GopBltSize Size of GopBlt in bytes.
132 @param PixelHeight Height of GopBlt/BmpImage in pixels
133 @param PixelWidth Width of GopBlt/BmpImage in pixels
134
135 @retval EFI_SUCCESS GopBlt and GopBltSize are returned.
136 @retval EFI_UNSUPPORTED BmpImage is not a valid *.BMP image
137 @retval EFI_BUFFER_TOO_SMALL The passed in GopBlt buffer is not big enough.
138 GopBltSize will contain the required size.
139 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate.
140
141 **/
142 EFI_STATUS
143 EFIAPI
144 ConvertBmpToGopBlt (
145 IN VOID *BmpImage,
146 IN UINTN BmpImageSize,
147 IN OUT VOID **GopBlt,
148 IN OUT UINTN *GopBltSize,
149 OUT UINTN *PixelHeight,
150 OUT UINTN *PixelWidth
151 )
152 {
153 UINT8 *Image;
154 UINT8 *ImageHeader;
155 BMP_IMAGE_HEADER *BmpHeader;
156 BMP_COLOR_MAP *BmpColorMap;
157 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer;
158 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;
159 UINTN BltBufferSize;
160 UINTN Index;
161 UINTN Height;
162 UINTN Width;
163 UINTN ImageIndex;
164 BOOLEAN IsAllocated;
165
166 BmpHeader = (BMP_IMAGE_HEADER *) BmpImage;
167
168 if (BmpHeader->CharB != 'B' || BmpHeader->CharM != 'M') {
169 return EFI_UNSUPPORTED;
170 }
171
172 //
173 // Doesn't support compress.
174 //
175 if (BmpHeader->CompressionType != 0) {
176 return EFI_UNSUPPORTED;
177 }
178
179 //
180 // Calculate Color Map offset in the image.
181 //
182 Image = BmpImage;
183 BmpColorMap = (BMP_COLOR_MAP *) (Image + sizeof (BMP_IMAGE_HEADER));
184
185 //
186 // Calculate graphics image data address in the image
187 //
188 Image = ((UINT8 *) BmpImage) + BmpHeader->ImageOffset;
189 ImageHeader = Image;
190
191 //
192 // Calculate the BltBuffer needed size.
193 //
194 BltBufferSize = BmpHeader->PixelWidth * BmpHeader->PixelHeight * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
195 IsAllocated = FALSE;
196 if (*GopBlt == NULL) {
197 //
198 // GopBlt is not allocated by caller.
199 //
200 *GopBltSize = BltBufferSize;
201 *GopBlt = AllocatePool (*GopBltSize);
202 IsAllocated = TRUE;
203 if (*GopBlt == NULL) {
204 return EFI_OUT_OF_RESOURCES;
205 }
206 } else {
207 //
208 // GopBlt has been allocated by caller.
209 //
210 if (*GopBltSize < BltBufferSize) {
211 *GopBltSize = BltBufferSize;
212 return EFI_BUFFER_TOO_SMALL;
213 }
214 }
215
216 *PixelWidth = BmpHeader->PixelWidth;
217 *PixelHeight = BmpHeader->PixelHeight;
218
219 //
220 // Convert image from BMP to Blt buffer format
221 //
222 BltBuffer = *GopBlt;
223 for (Height = 0; Height < BmpHeader->PixelHeight; Height++) {
224 Blt = &BltBuffer[(BmpHeader->PixelHeight - Height - 1) * BmpHeader->PixelWidth];
225 for (Width = 0; Width < BmpHeader->PixelWidth; Width++, Image++, Blt++) {
226 switch (BmpHeader->BitPerPixel) {
227 case 1:
228 //
229 // Convert 1-bit (2 colors) BMP to 24-bit color
230 //
231 for (Index = 0; Index < 8 && Width < BmpHeader->PixelWidth; Index++) {
232 Blt->Red = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Red;
233 Blt->Green = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Green;
234 Blt->Blue = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Blue;
235 Blt++;
236 Width++;
237 }
238
239 Blt --;
240 Width --;
241 break;
242
243 case 4:
244 //
245 // Convert 4-bit (16 colors) BMP Palette to 24-bit color
246 //
247 Index = (*Image) >> 4;
248 Blt->Red = BmpColorMap[Index].Red;
249 Blt->Green = BmpColorMap[Index].Green;
250 Blt->Blue = BmpColorMap[Index].Blue;
251 if (Width < (BmpHeader->PixelWidth - 1)) {
252 Blt++;
253 Width++;
254 Index = (*Image) & 0x0f;
255 Blt->Red = BmpColorMap[Index].Red;
256 Blt->Green = BmpColorMap[Index].Green;
257 Blt->Blue = BmpColorMap[Index].Blue;
258 }
259 break;
260
261 case 8:
262 //
263 // Convert 8-bit (256 colors) BMP Palette to 24-bit color
264 //
265 Blt->Red = BmpColorMap[*Image].Red;
266 Blt->Green = BmpColorMap[*Image].Green;
267 Blt->Blue = BmpColorMap[*Image].Blue;
268 break;
269
270 case 24:
271 //
272 // It is 24-bit BMP.
273 //
274 Blt->Blue = *Image++;
275 Blt->Green = *Image++;
276 Blt->Red = *Image;
277 break;
278
279 default:
280 //
281 // Other bit format BMP is not supported.
282 //
283 if (IsAllocated) {
284 FreePool (*GopBlt);
285 *GopBlt = NULL;
286 }
287 return EFI_UNSUPPORTED;
288 break;
289 };
290
291 }
292
293 ImageIndex = (UINTN) (Image - ImageHeader);
294 if ((ImageIndex % 4) != 0) {
295 //
296 // Bmp Image starts each row on a 32-bit boundary!
297 //
298 Image = Image + (4 - (ImageIndex % 4));
299 }
300 }
301
302 return EFI_SUCCESS;
303 }
304
305
306 /**
307 Use Console Control Protocol to lock the Console In Spliter virtual handle.
308 This is the ConInHandle and ConIn handle in the EFI system table. All key
309 presses will be ignored until the Password is typed in. The only way to
310 disable the password is to type it in to a ConIn device.
311
312 @param Password Password used to lock ConIn device.
313
314 @retval EFI_SUCCESS lock the Console In Spliter virtual handle successfully.
315 @retval EFI_UNSUPPORTED Password not found.
316
317 **/
318 EFI_STATUS
319 EFIAPI
320 LockKeyboards (
321 IN CHAR16 *Password
322 )
323 {
324 EFI_STATUS Status;
325 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl;
326
327 Status = gBS->LocateProtocol (&gEfiConsoleControlProtocolGuid, NULL, (VOID **) &ConsoleControl);
328 if (EFI_ERROR (Status)) {
329 return EFI_UNSUPPORTED;
330 }
331
332 Status = ConsoleControl->LockStdIn (ConsoleControl, Password);
333 return Status;
334 }
335
336
337 /**
338 Use Console Control to turn off UGA based Simple Text Out consoles from going
339 to the UGA device. Put up LogoFile on every UGA device that is a console.
340
341 @param LogoFile File name of logo to display on the center of the screen.
342
343 @retval EFI_SUCCESS ConsoleControl has been flipped to graphics and logo displayed.
344 @retval EFI_UNSUPPORTED Logo not found.
345
346 **/
347 EFI_STATUS
348 EFIAPI
349 EnableQuietBoot (
350 IN EFI_GUID *LogoFile
351 )
352 {
353 return EnableQuietBootEx (LogoFile, NULL);
354 }
355
356 /**
357 Use Console Control to turn off UGA based Simple Text Out consoles from going
358 to the UGA device. Put up LogoFile on every UGA device that is a console
359
360 @param LogoFile File name of logo to display on the center of the screen.
361 @param ImageHandle The driver image handle of the caller. The parameter is used to
362 optimize the loading of the logo file so that the FV from which
363 the driver image is loaded will be tried first.
364
365 @retval EFI_SUCCESS ConsoleControl has been flipped to graphics and logo displayed.
366 @retval EFI_UNSUPPORTED Logo not found.
367
368 **/
369 EFI_STATUS
370 EFIAPI
371 EnableQuietBootEx (
372 IN EFI_GUID *LogoFile,
373 IN EFI_HANDLE ImageHandle
374 )
375 {
376 EFI_STATUS Status;
377 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl;
378 EFI_OEM_BADGING_PROTOCOL *Badging;
379 UINT32 SizeOfX;
380 UINT32 SizeOfY;
381 INTN DestX;
382 INTN DestY;
383 UINT8 *ImageData;
384 UINTN ImageSize;
385 UINTN BltSize;
386 UINT32 Instance;
387 EFI_BADGING_FORMAT Format;
388 EFI_BADGING_DISPLAY_ATTRIBUTE Attribute;
389 UINTN CoordinateX;
390 UINTN CoordinateY;
391 UINTN Height;
392 UINTN Width;
393 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;
394 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
395 UINT32 ColorDepth;
396 UINT32 RefreshRate;
397 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
398
399 Status = gBS->LocateProtocol (&gEfiConsoleControlProtocolGuid, NULL, (VOID **) &ConsoleControl);
400 if (EFI_ERROR (Status)) {
401 return EFI_UNSUPPORTED;
402 }
403
404 UgaDraw = NULL;
405 //
406 // Try to open GOP first
407 //
408 Status = gBS->HandleProtocol (gST->ConsoleOutHandle, &gEfiGraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput);
409 if (EFI_ERROR (Status) && FeaturePcdGet (PcdUgaConsumeSupport)) {
410 GraphicsOutput = NULL;
411 //
412 // Open GOP failed, try to open UGA
413 //
414 Status = gBS->HandleProtocol (gST->ConsoleOutHandle, &gEfiUgaDrawProtocolGuid, (VOID **) &UgaDraw);
415 }
416 if (EFI_ERROR (Status)) {
417 return EFI_UNSUPPORTED;
418 }
419
420 Badging = NULL;
421 Status = gBS->LocateProtocol (&gEfiOEMBadgingProtocolGuid, NULL, (VOID **) &Badging);
422
423 //
424 // Set console control to graphics mode.
425 //
426 Status = ConsoleControl->SetMode (ConsoleControl, EfiConsoleControlScreenGraphics);
427 if (EFI_ERROR (Status)) {
428 return EFI_UNSUPPORTED;
429 }
430
431 if (GraphicsOutput != NULL) {
432 SizeOfX = GraphicsOutput->Mode->Info->HorizontalResolution;
433 SizeOfY = GraphicsOutput->Mode->Info->VerticalResolution;
434 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
435 Status = UgaDraw->GetMode (UgaDraw, &SizeOfX, &SizeOfY, &ColorDepth, &RefreshRate);
436 if (EFI_ERROR (Status)) {
437 return EFI_UNSUPPORTED;
438 }
439 } else {
440 return EFI_UNSUPPORTED;
441 }
442
443 Instance = 0;
444 while (1) {
445 ImageData = NULL;
446 ImageSize = 0;
447
448 if (Badging != NULL) {
449 //
450 // Get image from OEMBadging protocol.
451 //
452 Status = Badging->GetImage (
453 Badging,
454 &Instance,
455 &Format,
456 &ImageData,
457 &ImageSize,
458 &Attribute,
459 &CoordinateX,
460 &CoordinateY
461 );
462 if (EFI_ERROR (Status)) {
463 return Status;
464 }
465
466 //
467 // Currently only support BMP format.
468 //
469 if (Format != EfiBadgingFormatBMP) {
470 SafeFreePool (ImageData);
471 continue;
472 }
473 } else {
474 //
475 // Get the specified image from FV.
476 //
477 Status = GetGraphicsBitMapFromFVEx (ImageHandle, LogoFile, (VOID **) &ImageData, &ImageSize);
478 if (EFI_ERROR (Status)) {
479 return EFI_UNSUPPORTED;
480 }
481
482 CoordinateX = 0;
483 CoordinateY = 0;
484 Attribute = EfiBadgingDisplayAttributeCenter;
485 }
486
487 Blt = NULL;
488 Status = ConvertBmpToGopBlt (
489 ImageData,
490 ImageSize,
491 (VOID **) &Blt,
492 &BltSize,
493 &Height,
494 &Width
495 );
496 if (EFI_ERROR (Status)) {
497 SafeFreePool (ImageData);
498 if (Badging == NULL) {
499 return Status;
500 } else {
501 continue;
502 }
503 }
504
505 //
506 // Caculate the display position according to Attribute.
507 //
508 switch (Attribute) {
509 case EfiBadgingDisplayAttributeLeftTop:
510 DestX = CoordinateX;
511 DestY = CoordinateY;
512 break;
513
514 case EfiBadgingDisplayAttributeCenterTop:
515 DestX = (SizeOfX - Width) / 2;
516 DestY = CoordinateY;
517 break;
518
519 case EfiBadgingDisplayAttributeRightTop:
520 DestX = (SizeOfX - Width - CoordinateX);
521 DestY = CoordinateY;;
522 break;
523
524 case EfiBadgingDisplayAttributeCenterRight:
525 DestX = (SizeOfX - Width - CoordinateX);
526 DestY = (SizeOfY - Height) / 2;
527 break;
528
529 case EfiBadgingDisplayAttributeRightBottom:
530 DestX = (SizeOfX - Width - CoordinateX);
531 DestY = (SizeOfY - Height - CoordinateY);
532 break;
533
534 case EfiBadgingDisplayAttributeCenterBottom:
535 DestX = (SizeOfX - Width) / 2;
536 DestY = (SizeOfY - Height - CoordinateY);
537 break;
538
539 case EfiBadgingDisplayAttributeLeftBottom:
540 DestX = CoordinateX;
541 DestY = (SizeOfY - Height - CoordinateY);
542 break;
543
544 case EfiBadgingDisplayAttributeCenterLeft:
545 DestX = CoordinateX;
546 DestY = (SizeOfY - Height) / 2;
547 break;
548
549 case EfiBadgingDisplayAttributeCenter:
550 DestX = (SizeOfX - Width) / 2;
551 DestY = (SizeOfY - Height) / 2;
552 break;
553
554 default:
555 DestX = CoordinateX;
556 DestY = CoordinateY;
557 break;
558 }
559
560 if ((DestX >= 0) && (DestY >= 0)) {
561 if (GraphicsOutput != NULL) {
562 Status = GraphicsOutput->Blt (
563 GraphicsOutput,
564 Blt,
565 EfiBltBufferToVideo,
566 0,
567 0,
568 (UINTN) DestX,
569 (UINTN) DestY,
570 Width,
571 Height,
572 Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
573 );
574 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
575 Status = UgaDraw->Blt (
576 UgaDraw,
577 (EFI_UGA_PIXEL *) Blt,
578 EfiUgaBltBufferToVideo,
579 0,
580 0,
581 (UINTN) DestX,
582 (UINTN) DestY,
583 Width,
584 Height,
585 Width * sizeof (EFI_UGA_PIXEL)
586 );
587 } else {
588 Status = EFI_UNSUPPORTED;
589 }
590 }
591
592 SafeFreePool (ImageData);
593 SafeFreePool (Blt);
594
595 if (Badging == NULL) {
596 break;
597 }
598 }
599
600 return Status;
601 }
602
603 /**
604 Use Console Control to turn on UGA based Simple Text Out consoles. The UGA
605 Simple Text Out screens will now be synced up with all non UGA output devices
606
607 @retval EFI_SUCCESS UGA devices are back in text mode and synced up.
608 @retval EFI_UNSUPPORTED Logo not found
609
610 **/
611 EFI_STATUS
612 EFIAPI
613 DisableQuietBoot (
614 VOID
615 )
616 {
617 EFI_STATUS Status;
618 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl;
619
620 Status = gBS->LocateProtocol (&gEfiConsoleControlProtocolGuid, NULL, (VOID **) &ConsoleControl);
621 if (EFI_ERROR (Status)) {
622 return EFI_UNSUPPORTED;
623 }
624
625 //
626 // Set console control to text mode.
627 //
628 return ConsoleControl->SetMode (ConsoleControl, EfiConsoleControlScreenText);
629 }
630
631 /**
632 Internal display string worker function.
633
634 @param GraphicsOutput Graphics output protocol interface.
635 @param UgaDraw UGA draw protocol interface.
636 @param Sto Simple text out protocol interface.
637 @param X X coordinate to start printing.
638 @param Y Y coordinate to start printing.
639 @param Foreground Foreground color.
640 @param Background Background color.
641 @param fmt Format string.
642 @param args Print arguments.
643
644 @return Number of Characters printed. Zero means no any character
645 displayed successfully.
646
647 **/
648 UINTN
649 Print (
650 IN EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput,
651 IN EFI_UGA_DRAW_PROTOCOL *UgaDraw,
652 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *Sto,
653 IN UINTN X,
654 IN UINTN Y,
655 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Foreground,
656 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Background,
657 IN CHAR16 *fmt,
658 IN VA_LIST args
659 )
660 {
661 VOID *Buffer;
662 EFI_STATUS Status;
663 UINTN Index;
664 CHAR16 *UnicodeWeight;
665 UINT32 HorizontalResolution;
666 UINT32 VerticalResolution;
667 UINT32 ColorDepth;
668 UINT32 RefreshRate;
669 UINTN BufferLen;
670 UINTN LineBufferLen;
671 EFI_HII_FONT_PROTOCOL *HiiFont;
672 EFI_IMAGE_OUTPUT *Blt;
673 EFI_FONT_DISPLAY_INFO *FontInfo;
674 EFI_HII_ROW_INFO *RowInfoArray;
675 UINTN RowInfoArraySize;
676 UINTN PrintNum;
677
678 //
679 // For now, allocate an arbitrarily long buffer
680 //
681 Buffer = AllocateZeroPool (0x10000);
682 if (Buffer == NULL) {
683 return 0;
684 }
685
686 HorizontalResolution = 0;
687 VerticalResolution = 0;
688 Blt = NULL;
689 FontInfo = NULL;
690 PrintNum = 0;
691
692 if (GraphicsOutput != NULL) {
693 HorizontalResolution = GraphicsOutput->Mode->Info->HorizontalResolution;
694 VerticalResolution = GraphicsOutput->Mode->Info->VerticalResolution;
695 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
696 UgaDraw->GetMode (UgaDraw, &HorizontalResolution, &VerticalResolution, &ColorDepth, &RefreshRate);
697 } else {
698 Status = EFI_UNSUPPORTED;
699 goto Error;
700 }
701
702 ASSERT ((HorizontalResolution != 0) && (VerticalResolution !=0));
703
704 Status = gBS->LocateProtocol (&gEfiHiiFontProtocolGuid, NULL, (VOID **) &HiiFont);
705 if (EFI_ERROR (Status)) {
706 goto Error;
707 }
708
709 PrintNum = UnicodeVSPrint (Buffer, 0x10000, fmt, args);
710
711 UnicodeWeight = (CHAR16 *) Buffer;
712
713 for (Index = 0; UnicodeWeight[Index] != 0; Index++) {
714 if (UnicodeWeight[Index] == CHAR_BACKSPACE ||
715 UnicodeWeight[Index] == CHAR_LINEFEED ||
716 UnicodeWeight[Index] == CHAR_CARRIAGE_RETURN) {
717 UnicodeWeight[Index] = 0;
718 }
719 }
720
721 BufferLen = StrLen (Buffer);
722
723 LineBufferLen = sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) * HorizontalResolution * EFI_GLYPH_HEIGHT;
724 if (EFI_GLYPH_WIDTH * EFI_GLYPH_HEIGHT * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL) * BufferLen > LineBufferLen) {
725 Status = EFI_INVALID_PARAMETER;
726 goto Error;
727 }
728
729 Blt = (EFI_IMAGE_OUTPUT *) AllocateZeroPool (sizeof (EFI_IMAGE_OUTPUT));
730 if (Blt == NULL) {
731 Status = EFI_OUT_OF_RESOURCES;
732 goto Error;
733 }
734
735 Blt->Width = (UINT16) (HorizontalResolution);
736 Blt->Height = (UINT16) (VerticalResolution);
737
738 FontInfo = (EFI_FONT_DISPLAY_INFO *) AllocateZeroPool (sizeof (EFI_FONT_DISPLAY_INFO));
739 if (FontInfo == NULL) {
740 Status = EFI_OUT_OF_RESOURCES;
741 goto Error;
742 }
743 if (Foreground != NULL) {
744 CopyMem (&FontInfo->ForegroundColor, Foreground, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
745 } else {
746 CopyMem (
747 &FontInfo->ForegroundColor,
748 &mEfiColors[Sto->Mode->Attribute & 0x0f],
749 sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
750 );
751 }
752 if (Background != NULL) {
753 CopyMem (&FontInfo->BackgroundColor, Background, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
754 } else {
755 CopyMem (
756 &FontInfo->BackgroundColor,
757 &mEfiColors[Sto->Mode->Attribute >> 4],
758 sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
759 );
760 }
761
762 if (GraphicsOutput != NULL) {
763 Blt->Image.Screen = GraphicsOutput;
764
765 Status = HiiFont->StringToImage (
766 HiiFont,
767 EFI_HII_IGNORE_IF_NO_GLYPH | EFI_HII_DIRECT_TO_SCREEN,
768 Buffer,
769 FontInfo,
770 &Blt,
771 X,
772 Y,
773 NULL,
774 NULL,
775 NULL
776 );
777
778 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
779 ASSERT (UgaDraw!= NULL);
780
781 Blt->Image.Bitmap = AllocateZeroPool (Blt->Width * Blt->Height * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
782 if (Blt->Image.Bitmap == NULL) {
783 SafeFreePool (Blt);
784 SafeFreePool (Buffer);
785 return EFI_OUT_OF_RESOURCES;
786 }
787
788 RowInfoArray = NULL;
789 //
790 // StringToImage only support blt'ing image to device using GOP protocol. If GOP is not supported in this platform,
791 // we ask StringToImage to print the string to blt buffer, then blt to device using UgaDraw.
792 //
793 Status = HiiFont->StringToImage (
794 HiiFont,
795 EFI_HII_IGNORE_IF_NO_GLYPH,
796 Buffer,
797 FontInfo,
798 &Blt,
799 X,
800 Y,
801 &RowInfoArray,
802 &RowInfoArraySize,
803 NULL
804 );
805
806 if (!EFI_ERROR (Status)) {
807 //
808 // Line breaks are handled by caller of DrawUnicodeWeightAtCursorN, so the updated parameter RowInfoArraySize by StringToImage will
809 // always be 1 or 0 (if there is no valid Unicode Char can be printed). ASSERT here to make sure.
810 //
811 ASSERT (RowInfoArraySize <= 1);
812
813 Status = UgaDraw->Blt (
814 UgaDraw,
815 (EFI_UGA_PIXEL *) Blt->Image.Bitmap,
816 EfiUgaBltBufferToVideo,
817 X,
818 Y,
819 X,
820 Y,
821 RowInfoArray[0].LineWidth,
822 RowInfoArray[0].LineHeight,
823 Blt->Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
824 );
825 }
826
827 SafeFreePool (RowInfoArray);
828 SafeFreePool (Blt->Image.Bitmap);
829 } else {
830 Status = EFI_UNSUPPORTED;
831 }
832
833 Error:
834 SafeFreePool (Blt);
835 SafeFreePool (FontInfo);
836 FreePool (Buffer);
837
838 if (EFI_ERROR (Status)) {
839 return PrintNum;
840 } else {
841 return 0;
842 }
843 }
844
845 /**
846 Print Unicode string to graphics screen at the given X,Y coordinates of the graphics screen.
847 see definition of Print to find rules for constructing Fmt.
848
849 @param X Row to start printing at.
850 @param Y Column to start printing at.
851 @param ForeGround Foreground color.
852 @param BackGround background color.
853 @param Fmt Print format sting. See definition of Print.
854 @param ... Argumnet stream defined by Fmt string.
855
856 @return Number of Characters printed. Zero means no any character
857 displayed successfully.
858
859 **/
860 UINTN
861 EFIAPI
862 PrintXY (
863 IN UINTN X,
864 IN UINTN Y,
865 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *ForeGround, OPTIONAL
866 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BackGround, OPTIONAL
867 IN CHAR16 *Fmt,
868 ...
869 )
870 {
871 EFI_HANDLE Handle;
872 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
873 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
874 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *Sto;
875 EFI_STATUS Status;
876 VA_LIST Args;
877
878 VA_START (Args, Fmt);
879
880 Handle = gST->ConsoleOutHandle;
881
882 Status = gBS->HandleProtocol (
883 Handle,
884 &gEfiGraphicsOutputProtocolGuid,
885 (VOID **) &GraphicsOutput
886 );
887
888 UgaDraw = NULL;
889 if (EFI_ERROR (Status) && FeaturePcdGet (PcdUgaConsumeSupport)) {
890 //
891 // If no GOP available, try to open UGA Draw protocol if supported.
892 //
893 GraphicsOutput = NULL;
894
895 Status = gBS->HandleProtocol (
896 Handle,
897 &gEfiUgaDrawProtocolGuid,
898 (VOID **) &UgaDraw
899 );
900 }
901 if (EFI_ERROR (Status)) {
902 return 0;
903 }
904
905 Status = gBS->HandleProtocol (
906 Handle,
907 &gEfiSimpleTextOutProtocolGuid,
908 (VOID **) &Sto
909 );
910
911 if (EFI_ERROR (Status)) {
912 return 0;
913 }
914
915 return Print (GraphicsOutput, UgaDraw, Sto, X, Y, ForeGround, BackGround, Fmt, Args);
916 }
917