]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/GraphicsLib/Graphics.c
Update supported module type of PeiPalLib according to the latest MDE library spec.
[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/DxeServicesLib.h>
39 #include <Library/PcdLib.h>
40
41 /**
42 Convert a *.BMP graphics image to a GOP blt buffer. If a NULL Blt buffer
43 is passed in a GopBlt buffer will be allocated by this routine. If a GopBlt
44 buffer is passed in it will be used if it is big enough.
45
46 @param BmpImage Pointer to BMP file
47 @param BmpImageSize Number of bytes in BmpImage
48 @param GopBlt Buffer containing GOP version of BmpImage.
49 @param GopBltSize Size of GopBlt in bytes.
50 @param PixelHeight Height of GopBlt/BmpImage in pixels
51 @param PixelWidth Width of GopBlt/BmpImage in pixels
52
53 @retval EFI_SUCCESS GopBlt and GopBltSize are returned.
54 @retval EFI_UNSUPPORTED BmpImage is not a valid *.BMP image
55 @retval EFI_BUFFER_TOO_SMALL The passed in GopBlt buffer is not big enough.
56 GopBltSize will contain the required size.
57 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate.
58
59 **/
60 EFI_STATUS
61 EFIAPI
62 ConvertBmpToGopBlt (
63 IN VOID *BmpImage,
64 IN UINTN BmpImageSize,
65 IN OUT VOID **GopBlt,
66 IN OUT UINTN *GopBltSize,
67 OUT UINTN *PixelHeight,
68 OUT UINTN *PixelWidth
69 )
70 {
71 UINT8 *Image;
72 UINT8 *ImageHeader;
73 BMP_IMAGE_HEADER *BmpHeader;
74 BMP_COLOR_MAP *BmpColorMap;
75 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer;
76 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;
77 UINTN BltBufferSize;
78 UINTN Index;
79 UINTN Height;
80 UINTN Width;
81 UINTN ImageIndex;
82 BOOLEAN IsAllocated;
83
84 BmpHeader = (BMP_IMAGE_HEADER *) BmpImage;
85
86 if (BmpHeader->CharB != 'B' || BmpHeader->CharM != 'M') {
87 return EFI_UNSUPPORTED;
88 }
89
90 //
91 // Doesn't support compress.
92 //
93 if (BmpHeader->CompressionType != 0) {
94 return EFI_UNSUPPORTED;
95 }
96
97 //
98 // Calculate Color Map offset in the image.
99 //
100 Image = BmpImage;
101 BmpColorMap = (BMP_COLOR_MAP *) (Image + sizeof (BMP_IMAGE_HEADER));
102
103 //
104 // Calculate graphics image data address in the image
105 //
106 Image = ((UINT8 *) BmpImage) + BmpHeader->ImageOffset;
107 ImageHeader = Image;
108
109 //
110 // Calculate the BltBuffer needed size.
111 //
112 BltBufferSize = BmpHeader->PixelWidth * BmpHeader->PixelHeight * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
113 IsAllocated = FALSE;
114 if (*GopBlt == NULL) {
115 //
116 // GopBlt is not allocated by caller.
117 //
118 *GopBltSize = BltBufferSize;
119 *GopBlt = AllocatePool (*GopBltSize);
120 IsAllocated = TRUE;
121 if (*GopBlt == NULL) {
122 return EFI_OUT_OF_RESOURCES;
123 }
124 } else {
125 //
126 // GopBlt has been allocated by caller.
127 //
128 if (*GopBltSize < BltBufferSize) {
129 *GopBltSize = BltBufferSize;
130 return EFI_BUFFER_TOO_SMALL;
131 }
132 }
133
134 *PixelWidth = BmpHeader->PixelWidth;
135 *PixelHeight = BmpHeader->PixelHeight;
136
137 //
138 // Convert image from BMP to Blt buffer format
139 //
140 BltBuffer = *GopBlt;
141 for (Height = 0; Height < BmpHeader->PixelHeight; Height++) {
142 Blt = &BltBuffer[(BmpHeader->PixelHeight - Height - 1) * BmpHeader->PixelWidth];
143 for (Width = 0; Width < BmpHeader->PixelWidth; Width++, Image++, Blt++) {
144 switch (BmpHeader->BitPerPixel) {
145 case 1:
146 //
147 // Convert 1-bit (2 colors) BMP to 24-bit color
148 //
149 for (Index = 0; Index < 8 && Width < BmpHeader->PixelWidth; Index++) {
150 Blt->Red = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Red;
151 Blt->Green = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Green;
152 Blt->Blue = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Blue;
153 Blt++;
154 Width++;
155 }
156
157 Blt --;
158 Width --;
159 break;
160
161 case 4:
162 //
163 // Convert 4-bit (16 colors) BMP Palette to 24-bit color
164 //
165 Index = (*Image) >> 4;
166 Blt->Red = BmpColorMap[Index].Red;
167 Blt->Green = BmpColorMap[Index].Green;
168 Blt->Blue = BmpColorMap[Index].Blue;
169 if (Width < (BmpHeader->PixelWidth - 1)) {
170 Blt++;
171 Width++;
172 Index = (*Image) & 0x0f;
173 Blt->Red = BmpColorMap[Index].Red;
174 Blt->Green = BmpColorMap[Index].Green;
175 Blt->Blue = BmpColorMap[Index].Blue;
176 }
177 break;
178
179 case 8:
180 //
181 // Convert 8-bit (256 colors) BMP Palette to 24-bit color
182 //
183 Blt->Red = BmpColorMap[*Image].Red;
184 Blt->Green = BmpColorMap[*Image].Green;
185 Blt->Blue = BmpColorMap[*Image].Blue;
186 break;
187
188 case 24:
189 //
190 // It is 24-bit BMP.
191 //
192 Blt->Blue = *Image++;
193 Blt->Green = *Image++;
194 Blt->Red = *Image;
195 break;
196
197 default:
198 //
199 // Other bit format BMP is not supported.
200 //
201 if (IsAllocated) {
202 FreePool (*GopBlt);
203 *GopBlt = NULL;
204 }
205 return EFI_UNSUPPORTED;
206 break;
207 };
208
209 }
210
211 ImageIndex = (UINTN) (Image - ImageHeader);
212 if ((ImageIndex % 4) != 0) {
213 //
214 // Bmp Image starts each row on a 32-bit boundary!
215 //
216 Image = Image + (4 - (ImageIndex % 4));
217 }
218 }
219
220 return EFI_SUCCESS;
221 }
222
223
224 /**
225 Use Console Control Protocol to lock the Console In Spliter virtual handle.
226 This is the ConInHandle and ConIn handle in the EFI system table. All key
227 presses will be ignored until the Password is typed in. The only way to
228 disable the password is to type it in to a ConIn device.
229
230 @param Password Password used to lock ConIn device.
231
232 @retval EFI_SUCCESS lock the Console In Spliter virtual handle successfully.
233 @retval EFI_UNSUPPORTED Password not found.
234
235 **/
236 EFI_STATUS
237 EFIAPI
238 LockKeyboards (
239 IN CHAR16 *Password
240 )
241 {
242 EFI_STATUS Status;
243 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl;
244
245 Status = gBS->LocateProtocol (&gEfiConsoleControlProtocolGuid, NULL, (VOID **) &ConsoleControl);
246 if (EFI_ERROR (Status)) {
247 return EFI_UNSUPPORTED;
248 }
249
250 Status = ConsoleControl->LockStdIn (ConsoleControl, Password);
251 return Status;
252 }
253
254
255 /**
256 Use Console Control to turn off UGA based Simple Text Out consoles from going
257 to the UGA device. Put up LogoFile on every UGA device that is a console.
258
259 @param LogoFile File name of logo to display on the center of the screen.
260
261 @retval EFI_SUCCESS ConsoleControl has been flipped to graphics and logo displayed.
262 @retval EFI_UNSUPPORTED Logo not found.
263
264 **/
265 EFI_STATUS
266 EFIAPI
267 EnableQuietBoot (
268 IN EFI_GUID *LogoFile
269 )
270 {
271 return EnableQuietBootEx (LogoFile, NULL);
272 }
273
274 /**
275 Use Console Control to turn off UGA based Simple Text Out consoles from going
276 to the UGA device. Put up LogoFile on every UGA device that is a console
277
278 @param LogoFile File name of logo to display on the center of the screen.
279 @param ImageHandle The driver image handle of the caller. The parameter is used to
280 optimize the loading of the logo file so that the FV from which
281 the driver image is loaded will be tried first.
282
283 @retval EFI_SUCCESS ConsoleControl has been flipped to graphics and logo displayed.
284 @retval EFI_UNSUPPORTED Logo not found.
285
286 **/
287 EFI_STATUS
288 EFIAPI
289 EnableQuietBootEx (
290 IN EFI_GUID *LogoFile,
291 IN EFI_HANDLE ImageHandle
292 )
293 {
294 EFI_STATUS Status;
295 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl;
296 EFI_OEM_BADGING_PROTOCOL *Badging;
297 UINT32 SizeOfX;
298 UINT32 SizeOfY;
299 INTN DestX;
300 INTN DestY;
301 UINT8 *ImageData;
302 UINTN ImageSize;
303 UINTN BltSize;
304 UINT32 Instance;
305 EFI_BADGING_FORMAT Format;
306 EFI_BADGING_DISPLAY_ATTRIBUTE Attribute;
307 UINTN CoordinateX;
308 UINTN CoordinateY;
309 UINTN Height;
310 UINTN Width;
311 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;
312 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
313 UINT32 ColorDepth;
314 UINT32 RefreshRate;
315 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
316
317 Status = gBS->LocateProtocol (&gEfiConsoleControlProtocolGuid, NULL, (VOID **) &ConsoleControl);
318 if (EFI_ERROR (Status)) {
319 return EFI_UNSUPPORTED;
320 }
321
322 UgaDraw = NULL;
323 //
324 // Try to open GOP first
325 //
326 Status = gBS->HandleProtocol (gST->ConsoleOutHandle, &gEfiGraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput);
327 if (EFI_ERROR (Status) && FeaturePcdGet (PcdUgaConsumeSupport)) {
328 GraphicsOutput = NULL;
329 //
330 // Open GOP failed, try to open UGA
331 //
332 Status = gBS->HandleProtocol (gST->ConsoleOutHandle, &gEfiUgaDrawProtocolGuid, (VOID **) &UgaDraw);
333 }
334 if (EFI_ERROR (Status)) {
335 return EFI_UNSUPPORTED;
336 }
337
338 Badging = NULL;
339 Status = gBS->LocateProtocol (&gEfiOEMBadgingProtocolGuid, NULL, (VOID **) &Badging);
340
341 //
342 // Set console control to graphics mode.
343 //
344 Status = ConsoleControl->SetMode (ConsoleControl, EfiConsoleControlScreenGraphics);
345 if (EFI_ERROR (Status)) {
346 return EFI_UNSUPPORTED;
347 }
348
349 if (GraphicsOutput != NULL) {
350 SizeOfX = GraphicsOutput->Mode->Info->HorizontalResolution;
351 SizeOfY = GraphicsOutput->Mode->Info->VerticalResolution;
352 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
353 Status = UgaDraw->GetMode (UgaDraw, &SizeOfX, &SizeOfY, &ColorDepth, &RefreshRate);
354 if (EFI_ERROR (Status)) {
355 return EFI_UNSUPPORTED;
356 }
357 } else {
358 return EFI_UNSUPPORTED;
359 }
360
361 Instance = 0;
362 while (1) {
363 ImageData = NULL;
364 ImageSize = 0;
365
366 if (Badging != NULL) {
367 //
368 // Get image from OEMBadging protocol.
369 //
370 Status = Badging->GetImage (
371 Badging,
372 &Instance,
373 &Format,
374 &ImageData,
375 &ImageSize,
376 &Attribute,
377 &CoordinateX,
378 &CoordinateY
379 );
380 if (EFI_ERROR (Status)) {
381 return Status;
382 }
383
384 //
385 // Currently only support BMP format.
386 //
387 if (Format != EfiBadgingFormatBMP) {
388 if (ImageData != NULL) {
389 FreePool (ImageData);
390 }
391 continue;
392 }
393 } else {
394 //
395 // Get the specified image from FV.
396 //
397 Status = GetSectionFromAnyFv (LogoFile, EFI_SECTION_RAW, 0, (VOID **) &ImageData, &ImageSize);
398 if (EFI_ERROR (Status)) {
399 return EFI_UNSUPPORTED;
400 }
401
402 CoordinateX = 0;
403 CoordinateY = 0;
404 Attribute = EfiBadgingDisplayAttributeCenter;
405 }
406
407 Blt = NULL;
408 Status = ConvertBmpToGopBlt (
409 ImageData,
410 ImageSize,
411 (VOID **) &Blt,
412 &BltSize,
413 &Height,
414 &Width
415 );
416 if (EFI_ERROR (Status)) {
417 if (ImageData != NULL) {
418 FreePool (ImageData);
419 }
420 if (Badging == NULL) {
421 return Status;
422 } else {
423 continue;
424 }
425 }
426
427 //
428 // Caculate the display position according to Attribute.
429 //
430 switch (Attribute) {
431 case EfiBadgingDisplayAttributeLeftTop:
432 DestX = CoordinateX;
433 DestY = CoordinateY;
434 break;
435
436 case EfiBadgingDisplayAttributeCenterTop:
437 DestX = (SizeOfX - Width) / 2;
438 DestY = CoordinateY;
439 break;
440
441 case EfiBadgingDisplayAttributeRightTop:
442 DestX = (SizeOfX - Width - CoordinateX);
443 DestY = CoordinateY;;
444 break;
445
446 case EfiBadgingDisplayAttributeCenterRight:
447 DestX = (SizeOfX - Width - CoordinateX);
448 DestY = (SizeOfY - Height) / 2;
449 break;
450
451 case EfiBadgingDisplayAttributeRightBottom:
452 DestX = (SizeOfX - Width - CoordinateX);
453 DestY = (SizeOfY - Height - CoordinateY);
454 break;
455
456 case EfiBadgingDisplayAttributeCenterBottom:
457 DestX = (SizeOfX - Width) / 2;
458 DestY = (SizeOfY - Height - CoordinateY);
459 break;
460
461 case EfiBadgingDisplayAttributeLeftBottom:
462 DestX = CoordinateX;
463 DestY = (SizeOfY - Height - CoordinateY);
464 break;
465
466 case EfiBadgingDisplayAttributeCenterLeft:
467 DestX = CoordinateX;
468 DestY = (SizeOfY - Height) / 2;
469 break;
470
471 case EfiBadgingDisplayAttributeCenter:
472 DestX = (SizeOfX - Width) / 2;
473 DestY = (SizeOfY - Height) / 2;
474 break;
475
476 default:
477 DestX = CoordinateX;
478 DestY = CoordinateY;
479 break;
480 }
481
482 if ((DestX >= 0) && (DestY >= 0)) {
483 if (GraphicsOutput != NULL) {
484 Status = GraphicsOutput->Blt (
485 GraphicsOutput,
486 Blt,
487 EfiBltBufferToVideo,
488 0,
489 0,
490 (UINTN) DestX,
491 (UINTN) DestY,
492 Width,
493 Height,
494 Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
495 );
496 } else if (FeaturePcdGet (PcdUgaConsumeSupport)) {
497 Status = UgaDraw->Blt (
498 UgaDraw,
499 (EFI_UGA_PIXEL *) Blt,
500 EfiUgaBltBufferToVideo,
501 0,
502 0,
503 (UINTN) DestX,
504 (UINTN) DestY,
505 Width,
506 Height,
507 Width * sizeof (EFI_UGA_PIXEL)
508 );
509 } else {
510 Status = EFI_UNSUPPORTED;
511 }
512 }
513
514 if (ImageData != NULL) {
515 FreePool (ImageData);
516 }
517 if (Blt != NULL) {
518 FreePool (Blt);
519 }
520
521 if (Badging == NULL) {
522 break;
523 }
524 }
525
526 return Status;
527 }
528
529 /**
530 Use Console Control to turn on UGA based Simple Text Out consoles. The UGA
531 Simple Text Out screens will now be synced up with all non UGA output devices
532
533 @retval EFI_SUCCESS UGA devices are back in text mode and synced up.
534 @retval EFI_UNSUPPORTED Logo not found
535
536 **/
537 EFI_STATUS
538 EFIAPI
539 DisableQuietBoot (
540 VOID
541 )
542 {
543 EFI_STATUS Status;
544 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl;
545
546 Status = gBS->LocateProtocol (&gEfiConsoleControlProtocolGuid, NULL, (VOID **) &ConsoleControl);
547 if (EFI_ERROR (Status)) {
548 return EFI_UNSUPPORTED;
549 }
550
551 //
552 // Set console control to text mode.
553 //
554 return ConsoleControl->SetMode (ConsoleControl, EfiConsoleControlScreenText);
555 }
556