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