]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/GraphicsLib/Graphics.c
Remove SafeFreePool from MemoryAllocationLib as this API's name is misleading. Its...
[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 if (ImageData != NULL) {
471 FreePool (ImageData);
472 }
473 continue;
474 }
475 } else {
476 //
477 // Get the specified image from FV.
478 //
479 Status = GetGraphicsBitMapFromFVEx (ImageHandle, LogoFile, (VOID **) &ImageData, &ImageSize);
480 if (EFI_ERROR (Status)) {
481 return EFI_UNSUPPORTED;
482 }
483
484 CoordinateX = 0;
485 CoordinateY = 0;
486 Attribute = EfiBadgingDisplayAttributeCenter;
487 }
488
489 Blt = NULL;
490 Status = ConvertBmpToGopBlt (
491 ImageData,
492 ImageSize,
493 (VOID **) &Blt,
494 &BltSize,
495 &Height,
496 &Width
497 );
498 if (EFI_ERROR (Status)) {
499 if (ImageData != NULL) {
500 FreePool (ImageData);
501 }
502 if (Badging == NULL) {
503 return Status;
504 } else {
505 continue;
506 }
507 }
508
509 //
510 // Caculate the display position according to Attribute.
511 //
512 switch (Attribute) {
513 case EfiBadgingDisplayAttributeLeftTop:
514 DestX = CoordinateX;
515 DestY = CoordinateY;
516 break;
517
518 case EfiBadgingDisplayAttributeCenterTop:
519 DestX = (SizeOfX - Width) / 2;
520 DestY = CoordinateY;
521 break;
522
523 case EfiBadgingDisplayAttributeRightTop:
524 DestX = (SizeOfX - Width - CoordinateX);
525 DestY = CoordinateY;;
526 break;
527
528 case EfiBadgingDisplayAttributeCenterRight:
529 DestX = (SizeOfX - Width - CoordinateX);
530 DestY = (SizeOfY - Height) / 2;
531 break;
532
533 case EfiBadgingDisplayAttributeRightBottom:
534 DestX = (SizeOfX - Width - CoordinateX);
535 DestY = (SizeOfY - Height - CoordinateY);
536 break;
537
538 case EfiBadgingDisplayAttributeCenterBottom:
539 DestX = (SizeOfX - Width) / 2;
540 DestY = (SizeOfY - Height - CoordinateY);
541 break;
542
543 case EfiBadgingDisplayAttributeLeftBottom:
544 DestX = CoordinateX;
545 DestY = (SizeOfY - Height - CoordinateY);
546 break;
547
548 case EfiBadgingDisplayAttributeCenterLeft:
549 DestX = CoordinateX;
550 DestY = (SizeOfY - Height) / 2;
551 break;
552
553 case EfiBadgingDisplayAttributeCenter:
554 DestX = (SizeOfX - Width) / 2;
555 DestY = (SizeOfY - Height) / 2;
556 break;
557
558 default:
559 DestX = CoordinateX;
560 DestY = CoordinateY;
561 break;
562 }
563
564 if ((DestX >= 0) && (DestY >= 0)) {
565 if (GraphicsOutput != NULL) {
566 Status = GraphicsOutput->Blt (
567 GraphicsOutput,
568 Blt,
569 EfiBltBufferToVideo,
570 0,
571 0,
572 (UINTN) DestX,
573 (UINTN) DestY,
574 Width,
575 Height,
576 Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
577 );
578 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
579 Status = UgaDraw->Blt (
580 UgaDraw,
581 (EFI_UGA_PIXEL *) Blt,
582 EfiUgaBltBufferToVideo,
583 0,
584 0,
585 (UINTN) DestX,
586 (UINTN) DestY,
587 Width,
588 Height,
589 Width * sizeof (EFI_UGA_PIXEL)
590 );
591 } else {
592 Status = EFI_UNSUPPORTED;
593 }
594 }
595
596 if (ImageData != NULL) {
597 FreePool (ImageData);
598 }
599 if (Blt != NULL) {
600 FreePool (Blt);
601 }
602
603 if (Badging == NULL) {
604 break;
605 }
606 }
607
608 return Status;
609 }
610
611 /**
612 Use Console Control to turn on UGA based Simple Text Out consoles. The UGA
613 Simple Text Out screens will now be synced up with all non UGA output devices
614
615 @retval EFI_SUCCESS UGA devices are back in text mode and synced up.
616 @retval EFI_UNSUPPORTED Logo not found
617
618 **/
619 EFI_STATUS
620 EFIAPI
621 DisableQuietBoot (
622 VOID
623 )
624 {
625 EFI_STATUS Status;
626 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl;
627
628 Status = gBS->LocateProtocol (&gEfiConsoleControlProtocolGuid, NULL, (VOID **) &ConsoleControl);
629 if (EFI_ERROR (Status)) {
630 return EFI_UNSUPPORTED;
631 }
632
633 //
634 // Set console control to text mode.
635 //
636 return ConsoleControl->SetMode (ConsoleControl, EfiConsoleControlScreenText);
637 }
638
639 /**
640 Internal display string worker function.
641
642 @param GraphicsOutput Graphics output protocol interface.
643 @param UgaDraw UGA draw protocol interface.
644 @param Sto Simple text out protocol interface.
645 @param X X coordinate to start printing.
646 @param Y Y coordinate to start printing.
647 @param Foreground Foreground color.
648 @param Background Background color.
649 @param fmt Format string.
650 @param args Print arguments.
651
652 @return Number of Characters printed. Zero means no any character
653 displayed successfully.
654
655 **/
656 UINTN
657 Print (
658 IN EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput,
659 IN EFI_UGA_DRAW_PROTOCOL *UgaDraw,
660 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *Sto,
661 IN UINTN X,
662 IN UINTN Y,
663 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Foreground,
664 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Background,
665 IN CHAR16 *fmt,
666 IN VA_LIST args
667 )
668 {
669 VOID *Buffer;
670 EFI_STATUS Status;
671 UINTN Index;
672 CHAR16 *UnicodeWeight;
673 UINT32 HorizontalResolution;
674 UINT32 VerticalResolution;
675 UINT32 ColorDepth;
676 UINT32 RefreshRate;
677 UINTN BufferLen;
678 UINTN LineBufferLen;
679 EFI_HII_FONT_PROTOCOL *HiiFont;
680 EFI_IMAGE_OUTPUT *Blt;
681 EFI_FONT_DISPLAY_INFO *FontInfo;
682 EFI_HII_ROW_INFO *RowInfoArray;
683 UINTN RowInfoArraySize;
684 UINTN PrintNum;
685
686 //
687 // For now, allocate an arbitrarily long buffer
688 //
689 Buffer = AllocateZeroPool (0x10000);
690 if (Buffer == NULL) {
691 return 0;
692 }
693
694 HorizontalResolution = 0;
695 VerticalResolution = 0;
696 Blt = NULL;
697 FontInfo = NULL;
698 PrintNum = 0;
699
700 if (GraphicsOutput != NULL) {
701 HorizontalResolution = GraphicsOutput->Mode->Info->HorizontalResolution;
702 VerticalResolution = GraphicsOutput->Mode->Info->VerticalResolution;
703 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
704 UgaDraw->GetMode (UgaDraw, &HorizontalResolution, &VerticalResolution, &ColorDepth, &RefreshRate);
705 } else {
706 Status = EFI_UNSUPPORTED;
707 goto Error;
708 }
709
710 ASSERT ((HorizontalResolution != 0) && (VerticalResolution !=0));
711
712 Status = gBS->LocateProtocol (&gEfiHiiFontProtocolGuid, NULL, (VOID **) &HiiFont);
713 if (EFI_ERROR (Status)) {
714 goto Error;
715 }
716
717 PrintNum = UnicodeVSPrint (Buffer, 0x10000, fmt, args);
718
719 UnicodeWeight = (CHAR16 *) Buffer;
720
721 for (Index = 0; UnicodeWeight[Index] != 0; Index++) {
722 if (UnicodeWeight[Index] == CHAR_BACKSPACE ||
723 UnicodeWeight[Index] == CHAR_LINEFEED ||
724 UnicodeWeight[Index] == CHAR_CARRIAGE_RETURN) {
725 UnicodeWeight[Index] = 0;
726 }
727 }
728
729 BufferLen = StrLen (Buffer);
730
731 LineBufferLen = sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) * HorizontalResolution * EFI_GLYPH_HEIGHT;
732 if (EFI_GLYPH_WIDTH * EFI_GLYPH_HEIGHT * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL) * BufferLen > LineBufferLen) {
733 Status = EFI_INVALID_PARAMETER;
734 goto Error;
735 }
736
737 Blt = (EFI_IMAGE_OUTPUT *) AllocateZeroPool (sizeof (EFI_IMAGE_OUTPUT));
738 if (Blt == NULL) {
739 Status = EFI_OUT_OF_RESOURCES;
740 goto Error;
741 }
742
743 Blt->Width = (UINT16) (HorizontalResolution);
744 Blt->Height = (UINT16) (VerticalResolution);
745
746 FontInfo = (EFI_FONT_DISPLAY_INFO *) AllocateZeroPool (sizeof (EFI_FONT_DISPLAY_INFO));
747 if (FontInfo == NULL) {
748 Status = EFI_OUT_OF_RESOURCES;
749 goto Error;
750 }
751 if (Foreground != NULL) {
752 CopyMem (&FontInfo->ForegroundColor, Foreground, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
753 } else {
754 CopyMem (
755 &FontInfo->ForegroundColor,
756 &mEfiColors[Sto->Mode->Attribute & 0x0f],
757 sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
758 );
759 }
760 if (Background != NULL) {
761 CopyMem (&FontInfo->BackgroundColor, Background, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
762 } else {
763 CopyMem (
764 &FontInfo->BackgroundColor,
765 &mEfiColors[Sto->Mode->Attribute >> 4],
766 sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
767 );
768 }
769
770 if (GraphicsOutput != NULL) {
771 Blt->Image.Screen = GraphicsOutput;
772
773 Status = HiiFont->StringToImage (
774 HiiFont,
775 EFI_HII_IGNORE_IF_NO_GLYPH | EFI_HII_DIRECT_TO_SCREEN,
776 Buffer,
777 FontInfo,
778 &Blt,
779 X,
780 Y,
781 NULL,
782 NULL,
783 NULL
784 );
785
786 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
787 ASSERT (UgaDraw!= NULL);
788
789 Blt->Image.Bitmap = AllocateZeroPool (Blt->Width * Blt->Height * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
790 if (Blt->Image.Bitmap == NULL) {
791 FreePool (Blt);
792 FreePool (Buffer);
793 return EFI_OUT_OF_RESOURCES;
794 }
795
796 RowInfoArray = NULL;
797 //
798 // StringToImage only support blt'ing image to device using GOP protocol. If GOP is not supported in this platform,
799 // we ask StringToImage to print the string to blt buffer, then blt to device using UgaDraw.
800 //
801 Status = HiiFont->StringToImage (
802 HiiFont,
803 EFI_HII_IGNORE_IF_NO_GLYPH,
804 Buffer,
805 FontInfo,
806 &Blt,
807 X,
808 Y,
809 &RowInfoArray,
810 &RowInfoArraySize,
811 NULL
812 );
813
814 if (!EFI_ERROR (Status)) {
815 //
816 // Line breaks are handled by caller of DrawUnicodeWeightAtCursorN, so the updated parameter RowInfoArraySize by StringToImage will
817 // always be 1 or 0 (if there is no valid Unicode Char can be printed). ASSERT here to make sure.
818 //
819 ASSERT (RowInfoArraySize <= 1);
820
821 Status = UgaDraw->Blt (
822 UgaDraw,
823 (EFI_UGA_PIXEL *) Blt->Image.Bitmap,
824 EfiUgaBltBufferToVideo,
825 X,
826 Y,
827 X,
828 Y,
829 RowInfoArray[0].LineWidth,
830 RowInfoArray[0].LineHeight,
831 Blt->Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
832 );
833 }
834
835 if (RowInfoArray != NULL) {
836 FreePool (RowInfoArray);
837 }
838 if (Blt->Image.Bitmap != NULL) {
839 FreePool (Blt->Image.Bitmap);
840 }
841 } else {
842 Status = EFI_UNSUPPORTED;
843 }
844
845 Error:
846 if (Blt != NULL) {
847 FreePool (Blt);
848 }
849 if (FontInfo != NULL) {
850 FreePool (FontInfo);
851 }
852 FreePool (Buffer);
853
854 if (EFI_ERROR (Status)) {
855 return PrintNum;
856 } else {
857 return 0;
858 }
859 }
860
861 /**
862 Print Unicode string to graphics screen at the given X,Y coordinates of the graphics screen.
863 see definition of Print to find rules for constructing Fmt.
864
865 @param X Row to start printing at.
866 @param Y Column to start printing at.
867 @param ForeGround Foreground color.
868 @param BackGround background color.
869 @param Fmt Print format sting. See definition of Print.
870 @param ... Argumnet stream defined by Fmt string.
871
872 @return Number of Characters printed. Zero means no any character
873 displayed successfully.
874
875 **/
876 UINTN
877 EFIAPI
878 PrintXY (
879 IN UINTN X,
880 IN UINTN Y,
881 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *ForeGround, OPTIONAL
882 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BackGround, OPTIONAL
883 IN CHAR16 *Fmt,
884 ...
885 )
886 {
887 EFI_HANDLE Handle;
888 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
889 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
890 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *Sto;
891 EFI_STATUS Status;
892 VA_LIST Args;
893
894 VA_START (Args, Fmt);
895
896 Handle = gST->ConsoleOutHandle;
897
898 Status = gBS->HandleProtocol (
899 Handle,
900 &gEfiGraphicsOutputProtocolGuid,
901 (VOID **) &GraphicsOutput
902 );
903
904 UgaDraw = NULL;
905 if (EFI_ERROR (Status) && FeaturePcdGet (PcdUgaConsumeSupport)) {
906 //
907 // If no GOP available, try to open UGA Draw protocol if supported.
908 //
909 GraphicsOutput = NULL;
910
911 Status = gBS->HandleProtocol (
912 Handle,
913 &gEfiUgaDrawProtocolGuid,
914 (VOID **) &UgaDraw
915 );
916 }
917 if (EFI_ERROR (Status)) {
918 return 0;
919 }
920
921 Status = gBS->HandleProtocol (
922 Handle,
923 &gEfiSimpleTextOutProtocolGuid,
924 (VOID **) &Sto
925 );
926
927 if (EFI_ERROR (Status)) {
928 return 0;
929 }
930
931 return Print (GraphicsOutput, UgaDraw, Sto, X, Y, ForeGround, BackGround, Fmt, Args);
932 }
933