]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/Dxe/Graphics/Graphics.c
Add in the 1st version of ECP.
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / Dxe / Graphics / Graphics.c
1 /*++
2
3 Copyright (c) 2004 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 Graphics.c
15
16 Abstract:
17
18 Support for Basic Graphics operations.
19
20 BugBug: Currently *.BMP files are supported. This will be replaced
21 when Tiano graphics format is supported.
22
23 --*/
24
25 #include "Tiano.h"
26 #include "EfiDriverLib.h"
27 #include "GraphicsLib.h"
28
29 EFI_STATUS
30 GetGraphicsBitMapFromFV (
31 IN EFI_GUID *FileNameGuid,
32 OUT VOID **Image,
33 OUT UINTN *ImageSize
34 )
35 /*++
36
37 Routine Description:
38
39 Return the graphics image file named FileNameGuid into Image and return it's
40 size in ImageSize. All Firmware Volumes (FV) in the system are searched for the
41 file name.
42
43 Arguments:
44
45 FileNameGuid - File Name of graphics file in the FV(s).
46
47 Image - Pointer to pointer to return graphics image. If NULL, a
48 buffer will be allocated.
49
50 ImageSize - Size of the graphics Image in bytes. Zero if no image found.
51
52
53 Returns:
54
55 EFI_SUCCESS - Image and ImageSize are valid.
56 EFI_BUFFER_TOO_SMALL - Image not big enough. ImageSize has required size
57 EFI_NOT_FOUND - FileNameGuid not found
58
59 --*/
60 {
61 return GetGraphicsBitMapFromFVEx (NULL, FileNameGuid, Image, ImageSize);
62 }
63
64 EFI_STATUS
65 GetGraphicsBitMapFromFVEx (
66 IN EFI_HANDLE ImageHandle,
67 IN EFI_GUID *FileNameGuid,
68 OUT VOID **Image,
69 OUT UINTN *ImageSize
70 )
71 /*++
72
73 Routine Description:
74
75 Return the graphics image file named FileNameGuid into Image and return it's
76 size in ImageSize. All Firmware Volumes (FV) in the system are searched for the
77 file name.
78
79 Arguments:
80
81 ImageHandle - The driver image handle of the caller. The parameter is used to
82 optimize the loading of the image file so that the FV from which
83 the driver image is loaded will be tried first.
84
85 FileNameGuid - File Name of graphics file in the FV(s).
86
87 Image - Pointer to pointer to return graphics image. If NULL, a
88 buffer will be allocated.
89
90 ImageSize - Size of the graphics Image in bytes. Zero if no image found.
91
92
93 Returns:
94
95 EFI_SUCCESS - Image and ImageSize are valid.
96 EFI_BUFFER_TOO_SMALL - Image not big enough. ImageSize has required size
97 EFI_NOT_FOUND - FileNameGuid not found
98
99 --*/
100 {
101 return GetImageEx (
102 ImageHandle,
103 &gEfiDefaultBmpLogoGuid,
104 EFI_SECTION_RAW,
105 Image,
106 ImageSize,
107 FALSE
108 );
109 }
110
111
112 EFI_STATUS
113 ConvertBmpToGopBlt (
114 IN VOID *BmpImage,
115 IN UINTN BmpImageSize,
116 IN OUT VOID **GopBlt,
117 IN OUT UINTN *GopBltSize,
118 OUT UINTN *PixelHeight,
119 OUT UINTN *PixelWidth
120 )
121 /*++
122
123 Routine Description:
124
125 Convert a *.BMP graphics image to a GOP/UGA blt buffer. If a NULL Blt buffer
126 is passed in a GopBlt buffer will be allocated by this routine. If a GopBlt
127 buffer is passed in it will be used if it is big enough.
128
129 Arguments:
130
131 BmpImage - Pointer to BMP file
132
133 BmpImageSize - Number of bytes in BmpImage
134
135 GopBlt - Buffer containing GOP version of BmpImage.
136
137 GopBltSize - Size of GopBlt in bytes.
138
139 PixelHeight - Height of GopBlt/BmpImage in pixels
140
141 PixelWidth - Width of GopBlt/BmpImage in pixels
142
143
144 Returns:
145
146 EFI_SUCCESS - GopBlt and GopBltSize are returned.
147 EFI_UNSUPPORTED - BmpImage is not a valid *.BMP image
148 EFI_BUFFER_TOO_SMALL - The passed in GopBlt buffer is not big enough.
149 GopBltSize will contain the required size.
150 EFI_OUT_OF_RESOURCES - No enough buffer to allocate
151
152 --*/
153 {
154 UINT8 *Image;
155 UINT8 *ImageHeader;
156 BMP_IMAGE_HEADER *BmpHeader;
157 BMP_COLOR_MAP *BmpColorMap;
158 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer;
159 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;
160 UINTN BltBufferSize;
161 UINTN Index;
162 UINTN Height;
163 UINTN Width;
164 UINTN ImageIndex;
165 BOOLEAN IsAllocated;
166
167 BmpHeader = (BMP_IMAGE_HEADER *) BmpImage;
168 if (BmpHeader->CharB != 'B' || BmpHeader->CharM != 'M') {
169 return EFI_UNSUPPORTED;
170 }
171
172 if (BmpHeader->CompressionType != 0) {
173 return EFI_UNSUPPORTED;
174 }
175
176 //
177 // Calculate Color Map offset in the image.
178 //
179 Image = BmpImage;
180 BmpColorMap = (BMP_COLOR_MAP *) (Image + sizeof (BMP_IMAGE_HEADER));
181
182 //
183 // Calculate graphics image data address in the image
184 //
185 Image = ((UINT8 *) BmpImage) + BmpHeader->ImageOffset;
186 ImageHeader = Image;
187
188 BltBufferSize = BmpHeader->PixelWidth * BmpHeader->PixelHeight * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
189 IsAllocated = FALSE;
190 if (*GopBlt == NULL) {
191 *GopBltSize = BltBufferSize;
192 *GopBlt = EfiLibAllocatePool (*GopBltSize);
193 IsAllocated = TRUE;
194 if (*GopBlt == NULL) {
195 return EFI_OUT_OF_RESOURCES;
196 }
197 } else {
198 if (*GopBltSize < BltBufferSize) {
199 *GopBltSize = BltBufferSize;
200 return EFI_BUFFER_TOO_SMALL;
201 }
202 }
203
204 *PixelWidth = BmpHeader->PixelWidth;
205 *PixelHeight = BmpHeader->PixelHeight;
206
207 //
208 // Convert image from BMP to Blt buffer format
209 //
210 BltBuffer = *GopBlt;
211 for (Height = 0; Height < BmpHeader->PixelHeight; Height++) {
212 Blt = &BltBuffer[(BmpHeader->PixelHeight - Height - 1) * BmpHeader->PixelWidth];
213 for (Width = 0; Width < BmpHeader->PixelWidth; Width++, Image++, Blt++) {
214 switch (BmpHeader->BitPerPixel) {
215 case 1:
216 //
217 // Convert 1bit BMP to 24-bit color
218 //
219 for (Index = 0; Index < 8 && Width < BmpHeader->PixelWidth; Index++) {
220 Blt->Red = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Red;
221 Blt->Green = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Green;
222 Blt->Blue = BmpColorMap[((*Image) >> (7 - Index)) & 0x1].Blue;
223 Blt++;
224 Width++;
225 }
226
227 Blt --;
228 Width --;
229 break;
230
231 case 4:
232 //
233 // Convert BMP Palette to 24-bit color
234 //
235 Index = (*Image) >> 4;
236 Blt->Red = BmpColorMap[Index].Red;
237 Blt->Green = BmpColorMap[Index].Green;
238 Blt->Blue = BmpColorMap[Index].Blue;
239 if (Width < (BmpHeader->PixelWidth - 1)) {
240 Blt++;
241 Width++;
242 Index = (*Image) & 0x0f;
243 Blt->Red = BmpColorMap[Index].Red;
244 Blt->Green = BmpColorMap[Index].Green;
245 Blt->Blue = BmpColorMap[Index].Blue;
246 }
247 break;
248
249 case 8:
250 //
251 // Convert BMP Palette to 24-bit color
252 //
253 Blt->Red = BmpColorMap[*Image].Red;
254 Blt->Green = BmpColorMap[*Image].Green;
255 Blt->Blue = BmpColorMap[*Image].Blue;
256 break;
257
258 case 24:
259 Blt->Blue = *Image++;
260 Blt->Green = *Image++;
261 Blt->Red = *Image;
262 break;
263
264 default:
265 if (IsAllocated) {
266 gBS->FreePool (*GopBlt);
267 *GopBlt = NULL;
268 }
269 return EFI_UNSUPPORTED;
270 break;
271 };
272
273 }
274
275 ImageIndex = (UINTN) (Image - ImageHeader);
276 if ((ImageIndex % 4) != 0) {
277 //
278 // Bmp Image starts each row on a 32-bit boundary!
279 //
280 Image = Image + (4 - (ImageIndex % 4));
281 }
282 }
283
284 return EFI_SUCCESS;
285 }
286
287
288 EFI_STATUS
289 LockKeyboards (
290 IN CHAR16 *Password
291 )
292 /*++
293
294 Routine Description:
295 Use Console Control Protocol to lock the Console In Spliter virtual handle.
296 This is the ConInHandle and ConIn handle in the EFI system table. All key
297 presses will be ignored until the Password is typed in. The only way to
298 disable the password is to type it in to a ConIn device.
299
300 Arguments:
301 Password - Password used to lock ConIn device
302
303
304 Returns:
305
306 EFI_SUCCESS - ConsoleControl has been flipped to graphics and logo
307 displayed.
308 EFI_UNSUPPORTED - Logo not found
309
310 --*/
311 {
312 EFI_STATUS Status;
313 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl;
314
315 Status = gBS->LocateProtocol (&gEfiConsoleControlProtocolGuid, NULL, (VOID**)&ConsoleControl);
316 if (EFI_ERROR (Status)) {
317 return EFI_UNSUPPORTED;
318 }
319
320 Status = ConsoleControl->LockStdIn (ConsoleControl, Password);
321 return Status;
322 }
323
324 EFI_STATUS
325 EnableQuietBoot (
326 IN EFI_GUID *LogoFile
327 )
328 /*++
329
330 Routine Description:
331
332 Use Console Control to turn off UGA based Simple Text Out consoles from going
333 to the UGA device. Put up LogoFile on every UGA device that is a console
334
335 Arguments:
336
337 LogoFile - File name of logo to display on the center of the screen.
338
339
340 Returns:
341
342 EFI_SUCCESS - ConsoleControl has been flipped to graphics and logo
343 displayed.
344 EFI_UNSUPPORTED - Logo not found
345
346 --*/
347 {
348 return EnableQuietBootEx (LogoFile, NULL);
349 }
350
351 EFI_STATUS
352 EnableQuietBootEx (
353 IN EFI_GUID *LogoFile,
354 IN EFI_HANDLE ImageHandle
355 )
356 /*++
357
358 Routine Description:
359
360 Use Console Control to turn off GOP/UGA based Simple Text Out consoles from going
361 to the GOP/UGA device. Put up LogoFile on every GOP/UGA device that is a console
362
363 Arguments:
364
365 LogoFile - File name of logo to display on the center of the screen.
366 ImageHandle - The driver image handle of the caller. The parameter is used to
367 optimize the loading of the logo file so that the FV from which
368 the driver image is loaded will be tried first.
369
370
371 Returns:
372
373 EFI_SUCCESS - ConsoleControl has been flipped to graphics and logo
374 displayed.
375 EFI_UNSUPPORTED - Logo not found
376
377 --*/
378 {
379 EFI_STATUS Status;
380 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl;
381 EFI_OEM_BADGING_PROTOCOL *Badging;
382 UINT32 SizeOfX;
383 UINT32 SizeOfY;
384 INTN DestX;
385 INTN DestY;
386 UINT8 *ImageData;
387 UINTN ImageSize;
388 UINTN BltSize;
389 UINT32 Instance;
390 EFI_BADGING_FORMAT Format;
391 EFI_BADGING_DISPLAY_ATTRIBUTE Attribute;
392 UINTN CoordinateX;
393 UINTN CoordinateY;
394 UINTN Height;
395 UINTN Width;
396 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;
397 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
398 UINT32 ColorDepth;
399 UINT32 RefreshRate;
400 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
401
402 Status = gBS->LocateProtocol (&gEfiConsoleControlProtocolGuid, NULL, (VOID**)&ConsoleControl);
403 if (EFI_ERROR (Status)) {
404 return EFI_UNSUPPORTED;
405 }
406
407 UgaDraw = NULL;
408 //
409 // Try to open GOP first
410 //
411 Status = gBS->HandleProtocol (gST->ConsoleOutHandle, &gEfiGraphicsOutputProtocolGuid, (VOID**)&GraphicsOutput);
412 if (EFI_ERROR (Status)) {
413 GraphicsOutput = NULL;
414 //
415 // Open GOP failed, try to open UGA
416 //
417 Status = gBS->HandleProtocol (gST->ConsoleOutHandle, &gEfiUgaDrawProtocolGuid, (VOID**)&UgaDraw);
418 if (EFI_ERROR (Status)) {
419 return EFI_UNSUPPORTED;
420 }
421 }
422
423 Badging = NULL;
424 Status = gBS->LocateProtocol (&gEfiOEMBadgingProtocolGuid, NULL, (VOID**)&Badging);
425
426 ConsoleControl->SetMode (ConsoleControl, EfiConsoleControlScreenGraphics);
427
428 if (GraphicsOutput != NULL) {
429 SizeOfX = GraphicsOutput->Mode->Info->HorizontalResolution;
430 SizeOfY = GraphicsOutput->Mode->Info->VerticalResolution;
431 } else {
432 Status = UgaDraw->GetMode (UgaDraw, &SizeOfX, &SizeOfY, &ColorDepth, &RefreshRate);
433 if (EFI_ERROR (Status)) {
434 return EFI_UNSUPPORTED;
435 }
436 }
437
438 Instance = 0;
439 while (1) {
440 ImageData = NULL;
441 ImageSize = 0;
442
443 if (Badging != NULL) {
444 Status = Badging->GetImage (
445 Badging,
446 &Instance,
447 &Format,
448 &ImageData,
449 &ImageSize,
450 &Attribute,
451 &CoordinateX,
452 &CoordinateY
453 );
454 if (EFI_ERROR (Status)) {
455 return Status;
456 }
457
458 //
459 // Currently only support BMP format
460 //
461 if (Format != EfiBadgingFormatBMP) {
462 gBS->FreePool (ImageData);
463 continue;
464 }
465 } else {
466 Status = GetGraphicsBitMapFromFVEx (ImageHandle, LogoFile, &ImageData, &ImageSize);
467 if (EFI_ERROR (Status)) {
468 return EFI_UNSUPPORTED;
469 }
470
471 CoordinateX = 0;
472 CoordinateY = 0;
473 Attribute = EfiBadgingDisplayAttributeCenter;
474 }
475
476 Blt = NULL;
477 Status = ConvertBmpToGopBlt (
478 ImageData,
479 ImageSize,
480 (VOID**)&Blt,
481 &BltSize,
482 &Height,
483 &Width
484 );
485 if (EFI_ERROR (Status)) {
486 gBS->FreePool (ImageData);
487 if (Badging == NULL) {
488 return Status;
489 } else {
490 continue;
491 }
492 }
493
494 switch (Attribute) {
495 case EfiBadgingDisplayAttributeLeftTop:
496 DestX = CoordinateX;
497 DestY = CoordinateY;
498 break;
499
500 case EfiBadgingDisplayAttributeCenterTop:
501 DestX = (SizeOfX - Width) / 2;
502 DestY = CoordinateY;
503 break;
504
505 case EfiBadgingDisplayAttributeRightTop:
506 DestX = (SizeOfX - Width - CoordinateX);
507 DestY = CoordinateY;;
508 break;
509
510 case EfiBadgingDisplayAttributeCenterRight:
511 DestX = (SizeOfX - Width - CoordinateX);
512 DestY = (SizeOfY - Height) / 2;
513 break;
514
515 case EfiBadgingDisplayAttributeRightBottom:
516 DestX = (SizeOfX - Width - CoordinateX);
517 DestY = (SizeOfY - Height - CoordinateY);
518 break;
519
520 case EfiBadgingDisplayAttributeCenterBottom:
521 DestX = (SizeOfX - Width) / 2;
522 DestY = (SizeOfY - Height - CoordinateY);
523 break;
524
525 case EfiBadgingDisplayAttributeLeftBottom:
526 DestX = CoordinateX;
527 DestY = (SizeOfY - Height - CoordinateY);
528 break;
529
530 case EfiBadgingDisplayAttributeCenterLeft:
531 DestX = CoordinateX;
532 DestY = (SizeOfY - Height) / 2;
533 break;
534
535 case EfiBadgingDisplayAttributeCenter:
536 DestX = (SizeOfX - Width) / 2;
537 DestY = (SizeOfY - Height) / 2;
538 break;
539
540 default:
541 DestX = CoordinateX;
542 DestY = CoordinateY;
543 break;
544 }
545
546 if ((DestX >= 0) && (DestY >= 0)) {
547 if (GraphicsOutput != NULL) {
548 Status = GraphicsOutput->Blt (
549 GraphicsOutput,
550 Blt,
551 EfiBltBufferToVideo,
552 0,
553 0,
554 (UINTN) DestX,
555 (UINTN) DestY,
556 Width,
557 Height,
558 Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
559 );
560 } else {
561 Status = UgaDraw->Blt (
562 UgaDraw,
563 (EFI_UGA_PIXEL *) Blt,
564 EfiUgaBltBufferToVideo,
565 0,
566 0,
567 (UINTN) DestX,
568 (UINTN) DestY,
569 Width,
570 Height,
571 Width * sizeof (EFI_UGA_PIXEL)
572 );
573 }
574 }
575
576 gBS->FreePool (ImageData);
577 gBS->FreePool (Blt);
578
579 if (Badging == NULL) {
580 break;
581 }
582 }
583
584 return Status;
585 }
586
587
588 EFI_STATUS
589 DisableQuietBoot (
590 VOID
591 )
592 /*++
593
594 Routine Description:
595
596 Use Console Control to turn on GOP/UGA based Simple Text Out consoles. The GOP/UGA
597 Simple Text Out screens will now be synced up with all non GOP/UGA output devices
598
599 Arguments:
600
601 NONE
602
603 Returns:
604
605 EFI_SUCCESS - GOP/UGA devices are back in text mode and synced up.
606 EFI_UNSUPPORTED - Logo not found
607
608 --*/
609 {
610 EFI_STATUS Status;
611 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl;
612
613 Status = gBS->LocateProtocol (&gEfiConsoleControlProtocolGuid, NULL, (VOID**)&ConsoleControl);
614 if (EFI_ERROR (Status)) {
615 return EFI_UNSUPPORTED;
616 }
617
618 return ConsoleControl->SetMode (ConsoleControl, EfiConsoleControlScreenText);
619 }