]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Csm/BiosThunk/VideoDxe/BiosVideo.c
Take the highest horizontal resolution as highest video resolution.
[mirror_edk2.git] / IntelFrameworkModulePkg / Csm / BiosThunk / VideoDxe / BiosVideo.c
1 /** @file
2 ConsoleOut Routines that speak VGA.
3
4 Copyright (c) 2007 - 2013, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include "BiosVideo.h"
18
19 //
20 // EFI Driver Binding Protocol Instance
21 //
22 EFI_DRIVER_BINDING_PROTOCOL gBiosVideoDriverBinding = {
23 BiosVideoDriverBindingSupported,
24 BiosVideoDriverBindingStart,
25 BiosVideoDriverBindingStop,
26 0x3,
27 NULL,
28 NULL
29 };
30
31 //
32 // Global lookup tables for VGA graphics modes
33 //
34 UINT8 mVgaLeftMaskTable[] = { 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
35
36 UINT8 mVgaRightMaskTable[] = { 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
37
38 UINT8 mVgaBitMaskTable[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
39
40 //
41 // Save controller attributes during first start
42 //
43 UINT64 mOriginalPciAttributes;
44 BOOLEAN mPciAttributesSaved = FALSE;
45
46 EFI_GRAPHICS_OUTPUT_BLT_PIXEL mVgaColorToGraphicsOutputColor[] = {
47 { 0x00, 0x00, 0x00, 0x00 },
48 { 0x98, 0x00, 0x00, 0x00 },
49 { 0x00, 0x98, 0x00, 0x00 },
50 { 0x98, 0x98, 0x00, 0x00 },
51 { 0x00, 0x00, 0x98, 0x00 },
52 { 0x98, 0x00, 0x98, 0x00 },
53 { 0x00, 0x98, 0x98, 0x00 },
54 { 0x98, 0x98, 0x98, 0x00 },
55 { 0x10, 0x10, 0x10, 0x00 },
56 { 0xff, 0x10, 0x10, 0x00 },
57 { 0x10, 0xff, 0x10, 0x00 },
58 { 0xff, 0xff, 0x10, 0x00 },
59 { 0x10, 0x10, 0xff, 0x00 },
60 { 0xf0, 0x10, 0xff, 0x00 },
61 { 0x10, 0xff, 0xff, 0x00 },
62 { 0xff, 0xff, 0xff, 0x00 }
63 };
64
65 //
66 // Standard timing defined by VESA EDID
67 //
68 VESA_BIOS_EXTENSIONS_EDID_TIMING mEstablishedEdidTiming[] = {
69 //
70 // Established Timing I
71 //
72 {800, 600, 60},
73 {800, 600, 56},
74 {640, 480, 75},
75 {640, 480, 72},
76 {640, 480, 67},
77 {640, 480, 60},
78 {720, 400, 88},
79 {720, 400, 70},
80 //
81 // Established Timing II
82 //
83 {1280, 1024, 75},
84 {1024, 768, 75},
85 {1024, 768, 70},
86 {1024, 768, 60},
87 {1024, 768, 87},
88 {832, 624, 75},
89 {800, 600, 75},
90 {800, 600, 72},
91 //
92 // Established Timing III
93 //
94 {1152, 870, 75}
95 };
96
97 /**
98 Supported.
99
100 @param This Pointer to driver binding protocol
101 @param Controller Controller handle to connect
102 @param RemainingDevicePath A pointer to the remaining portion of a device
103 path
104
105 @retval EFI_STATUS EFI_SUCCESS:This controller can be managed by this
106 driver, Otherwise, this controller cannot be
107 managed by this driver
108
109 **/
110 EFI_STATUS
111 EFIAPI
112 BiosVideoDriverBindingSupported (
113 IN EFI_DRIVER_BINDING_PROTOCOL *This,
114 IN EFI_HANDLE Controller,
115 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
116 )
117 {
118 EFI_STATUS Status;
119 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
120 EFI_PCI_IO_PROTOCOL *PciIo;
121 PCI_TYPE00 Pci;
122 EFI_DEV_PATH *Node;
123
124 //
125 // See if the Legacy BIOS Protocol is available
126 //
127 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);
128 if (EFI_ERROR (Status)) {
129 return Status;
130 }
131
132 //
133 // Open the IO Abstraction(s) needed to perform the supported test
134 //
135 Status = gBS->OpenProtocol (
136 Controller,
137 &gEfiPciIoProtocolGuid,
138 (VOID **) &PciIo,
139 This->DriverBindingHandle,
140 Controller,
141 EFI_OPEN_PROTOCOL_BY_DRIVER
142 );
143 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
144 return Status;
145 }
146
147 if (Status == EFI_ALREADY_STARTED) {
148 //
149 // If VgaMiniPort protocol is installed, EFI_ALREADY_STARTED indicates failure,
150 // because VgaMiniPort protocol is installed on controller handle directly.
151 //
152 Status = gBS->OpenProtocol (
153 Controller,
154 &gEfiVgaMiniPortProtocolGuid,
155 NULL,
156 NULL,
157 NULL,
158 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
159 );
160 if (!EFI_ERROR (Status)) {
161 return EFI_ALREADY_STARTED;
162 }
163 }
164 //
165 // See if this is a PCI Graphics Controller by looking at the Command register and
166 // Class Code Register
167 //
168 Status = PciIo->Pci.Read (
169 PciIo,
170 EfiPciIoWidthUint32,
171 0,
172 sizeof (Pci) / sizeof (UINT32),
173 &Pci
174 );
175 if (EFI_ERROR (Status)) {
176 Status = EFI_UNSUPPORTED;
177 goto Done;
178 }
179
180 Status = EFI_UNSUPPORTED;
181 if (Pci.Hdr.ClassCode[2] == 0x03 || (Pci.Hdr.ClassCode[2] == 0x00 && Pci.Hdr.ClassCode[1] == 0x01)) {
182
183 Status = EFI_SUCCESS;
184 //
185 // If this is a graphics controller,
186 // go further check RemainingDevicePath validation
187 //
188 if (RemainingDevicePath != NULL) {
189 Node = (EFI_DEV_PATH *) RemainingDevicePath;
190 //
191 // Check if RemainingDevicePath is the End of Device Path Node,
192 // if yes, return EFI_SUCCESS
193 //
194 if (!IsDevicePathEnd (Node)) {
195 //
196 // If RemainingDevicePath isn't the End of Device Path Node,
197 // check its validation
198 //
199 if (Node->DevPath.Type != ACPI_DEVICE_PATH ||
200 Node->DevPath.SubType != ACPI_ADR_DP ||
201 DevicePathNodeLength(&Node->DevPath) < sizeof(ACPI_ADR_DEVICE_PATH)) {
202 Status = EFI_UNSUPPORTED;
203 }
204 }
205 }
206 }
207
208 Done:
209 gBS->CloseProtocol (
210 Controller,
211 &gEfiPciIoProtocolGuid,
212 This->DriverBindingHandle,
213 Controller
214 );
215
216 return Status;
217 }
218
219
220 /**
221 Install Graphics Output Protocol onto VGA device handles.
222
223 @param This Pointer to driver binding protocol
224 @param Controller Controller handle to connect
225 @param RemainingDevicePath A pointer to the remaining portion of a device
226 path
227
228 @return EFI_STATUS
229
230 **/
231 EFI_STATUS
232 EFIAPI
233 BiosVideoDriverBindingStart (
234 IN EFI_DRIVER_BINDING_PROTOCOL *This,
235 IN EFI_HANDLE Controller,
236 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
237 )
238 {
239 EFI_STATUS Status;
240 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
241 EFI_PCI_IO_PROTOCOL *PciIo;
242 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
243 UINTN Flags;
244 UINT64 Supports;
245
246 //
247 // Initialize local variables
248 //
249 PciIo = NULL;
250 ParentDevicePath = NULL;
251
252 //
253 //
254 // See if the Legacy BIOS Protocol is available
255 //
256 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);
257 if (EFI_ERROR (Status)) {
258 return Status;
259 }
260
261 //
262 // Prepare for status code
263 //
264 Status = gBS->HandleProtocol (
265 Controller,
266 &gEfiDevicePathProtocolGuid,
267 (VOID **) &ParentDevicePath
268 );
269 if (EFI_ERROR (Status)) {
270 return Status;
271 }
272
273 //
274 // Open the IO Abstraction(s) needed
275 //
276 Status = gBS->OpenProtocol (
277 Controller,
278 &gEfiPciIoProtocolGuid,
279 (VOID **) &PciIo,
280 This->DriverBindingHandle,
281 Controller,
282 EFI_OPEN_PROTOCOL_BY_DRIVER
283 );
284 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
285 return Status;
286 }
287
288 //
289 // Save original PCI attributes
290 //
291 if (!mPciAttributesSaved) {
292 Status = PciIo->Attributes (
293 PciIo,
294 EfiPciIoAttributeOperationGet,
295 0,
296 &mOriginalPciAttributes
297 );
298
299 if (EFI_ERROR (Status)) {
300 goto Done;
301 }
302 mPciAttributesSaved = TRUE;
303 }
304
305 //
306 // Get supported PCI attributes
307 //
308 Status = PciIo->Attributes (
309 PciIo,
310 EfiPciIoAttributeOperationSupported,
311 0,
312 &Supports
313 );
314 if (EFI_ERROR (Status)) {
315 goto Done;
316 }
317
318 Supports &= (EFI_PCI_IO_ATTRIBUTE_VGA_IO | EFI_PCI_IO_ATTRIBUTE_VGA_IO_16);
319 if (Supports == 0 || Supports == (EFI_PCI_IO_ATTRIBUTE_VGA_IO | EFI_PCI_IO_ATTRIBUTE_VGA_IO_16)) {
320 Status = EFI_UNSUPPORTED;
321 goto Done;
322 }
323
324 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
325 EFI_PROGRESS_CODE,
326 EFI_PERIPHERAL_LOCAL_CONSOLE | EFI_P_PC_ENABLE,
327 ParentDevicePath
328 );
329 //
330 // Enable the device and make sure VGA cycles are being forwarded to this VGA device
331 //
332 Status = PciIo->Attributes (
333 PciIo,
334 EfiPciIoAttributeOperationEnable,
335 EFI_PCI_DEVICE_ENABLE | EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY | Supports,
336 NULL
337 );
338 if (EFI_ERROR (Status)) {
339 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
340 EFI_ERROR_CODE | EFI_ERROR_MINOR,
341 EFI_PERIPHERAL_LOCAL_CONSOLE | EFI_P_EC_RESOURCE_CONFLICT,
342 ParentDevicePath
343 );
344 goto Done;
345 }
346 //
347 // Check to see if there is a legacy option ROM image associated with this PCI device
348 //
349 Status = LegacyBios->CheckPciRom (
350 LegacyBios,
351 Controller,
352 NULL,
353 NULL,
354 &Flags
355 );
356 if (EFI_ERROR (Status)) {
357 goto Done;
358 }
359 //
360 // Post the legacy option ROM if it is available.
361 //
362 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
363 EFI_PROGRESS_CODE,
364 EFI_P_PC_RESET,
365 ParentDevicePath
366 );
367 Status = LegacyBios->InstallPciRom (
368 LegacyBios,
369 Controller,
370 NULL,
371 &Flags,
372 NULL,
373 NULL,
374 NULL,
375 NULL
376 );
377 if (EFI_ERROR (Status)) {
378 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
379 EFI_ERROR_CODE | EFI_ERROR_MINOR,
380 EFI_PERIPHERAL_LOCAL_CONSOLE | EFI_P_EC_CONTROLLER_ERROR,
381 ParentDevicePath
382 );
383 goto Done;
384 }
385
386 if (RemainingDevicePath != NULL) {
387 if (IsDevicePathEnd (RemainingDevicePath) &&
388 (FeaturePcdGet (PcdBiosVideoCheckVbeEnable) || FeaturePcdGet (PcdBiosVideoCheckVgaEnable))) {
389 //
390 // If RemainingDevicePath is the End of Device Path Node,
391 // don't create any child device and return EFI_SUCESS
392 Status = EFI_SUCCESS;
393 goto Done;
394 }
395 }
396
397 //
398 // Create child handle and install GraphicsOutputProtocol on it
399 //
400 Status = BiosVideoChildHandleInstall (
401 This,
402 Controller,
403 PciIo,
404 LegacyBios,
405 ParentDevicePath,
406 RemainingDevicePath
407 );
408
409 Done:
410 if ((EFI_ERROR (Status)) && (Status != EFI_ALREADY_STARTED)) {
411 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
412 EFI_PROGRESS_CODE,
413 EFI_PERIPHERAL_LOCAL_CONSOLE | EFI_P_PC_DISABLE,
414 ParentDevicePath
415 );
416
417 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
418 EFI_PROGRESS_CODE,
419 EFI_PERIPHERAL_LOCAL_CONSOLE | EFI_P_EC_NOT_DETECTED,
420 ParentDevicePath
421 );
422 if (!HasChildHandle (Controller)) {
423 if (mPciAttributesSaved) {
424 //
425 // Restore original PCI attributes
426 //
427 PciIo->Attributes (
428 PciIo,
429 EfiPciIoAttributeOperationSet,
430 mOriginalPciAttributes,
431 NULL
432 );
433 }
434 }
435 //
436 // Release PCI I/O Protocols on the controller handle.
437 //
438 gBS->CloseProtocol (
439 Controller,
440 &gEfiPciIoProtocolGuid,
441 This->DriverBindingHandle,
442 Controller
443 );
444 }
445
446 return Status;
447 }
448
449
450 /**
451 Stop.
452
453 @param This Pointer to driver binding protocol
454 @param Controller Controller handle to connect
455 @param NumberOfChildren Number of children handle created by this driver
456 @param ChildHandleBuffer Buffer containing child handle created
457
458 @retval EFI_SUCCESS Driver disconnected successfully from controller
459 @retval EFI_UNSUPPORTED Cannot find BIOS_VIDEO_DEV structure
460
461 **/
462 EFI_STATUS
463 EFIAPI
464 BiosVideoDriverBindingStop (
465 IN EFI_DRIVER_BINDING_PROTOCOL *This,
466 IN EFI_HANDLE Controller,
467 IN UINTN NumberOfChildren,
468 IN EFI_HANDLE *ChildHandleBuffer
469 )
470 {
471 EFI_STATUS Status;
472 BOOLEAN AllChildrenStopped;
473 UINTN Index;
474 EFI_PCI_IO_PROTOCOL *PciIo;
475
476 AllChildrenStopped = TRUE;
477
478 if (NumberOfChildren == 0) {
479 //
480 // Close PCI I/O protocol on the controller handle
481 //
482 gBS->CloseProtocol (
483 Controller,
484 &gEfiPciIoProtocolGuid,
485 This->DriverBindingHandle,
486 Controller
487 );
488
489 return EFI_SUCCESS;
490 }
491
492 for (Index = 0; Index < NumberOfChildren; Index++) {
493 Status = BiosVideoChildHandleUninstall (This, Controller, ChildHandleBuffer[Index]);
494
495 if (EFI_ERROR (Status)) {
496 AllChildrenStopped = FALSE;
497 }
498 }
499
500 if (!AllChildrenStopped) {
501 return EFI_DEVICE_ERROR;
502 }
503
504 if (!HasChildHandle (Controller)) {
505 if (mPciAttributesSaved) {
506 Status = gBS->HandleProtocol (
507 Controller,
508 &gEfiPciIoProtocolGuid,
509 (VOID **) &PciIo
510 );
511 ASSERT_EFI_ERROR (Status);
512
513 //
514 // Restore original PCI attributes
515 //
516 Status = PciIo->Attributes (
517 PciIo,
518 EfiPciIoAttributeOperationSet,
519 mOriginalPciAttributes,
520 NULL
521 );
522 ASSERT_EFI_ERROR (Status);
523 }
524 }
525
526
527 return EFI_SUCCESS;
528 }
529
530
531 /**
532 Install child handles if the Handle supports MBR format.
533
534 @param This Calling context.
535 @param ParentHandle Parent Handle
536 @param ParentPciIo Parent PciIo interface
537 @param ParentLegacyBios Parent LegacyBios interface
538 @param ParentDevicePath Parent Device Path
539 @param RemainingDevicePath Remaining Device Path
540
541 @retval EFI_SUCCESS If a child handle was added
542 @retval other A child handle was not added
543
544 **/
545 EFI_STATUS
546 BiosVideoChildHandleInstall (
547 IN EFI_DRIVER_BINDING_PROTOCOL *This,
548 IN EFI_HANDLE ParentHandle,
549 IN EFI_PCI_IO_PROTOCOL *ParentPciIo,
550 IN EFI_LEGACY_BIOS_PROTOCOL *ParentLegacyBios,
551 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,
552 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
553 )
554 {
555 EFI_STATUS Status;
556 BIOS_VIDEO_DEV *BiosVideoPrivate;
557 PCI_TYPE00 Pci;
558 ACPI_ADR_DEVICE_PATH AcpiDeviceNode;
559 BOOLEAN ProtocolInstalled;
560
561 //
562 // Allocate the private device structure for video device
563 //
564 BiosVideoPrivate = (BIOS_VIDEO_DEV *) AllocateZeroPool (
565 sizeof (BIOS_VIDEO_DEV)
566 );
567 if (NULL == BiosVideoPrivate) {
568 Status = EFI_OUT_OF_RESOURCES;
569 goto Done;
570 }
571
572 //
573 // See if this is a VGA compatible controller or not
574 //
575 Status = ParentPciIo->Pci.Read (
576 ParentPciIo,
577 EfiPciIoWidthUint32,
578 0,
579 sizeof (Pci) / sizeof (UINT32),
580 &Pci
581 );
582 if (EFI_ERROR (Status)) {
583 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
584 EFI_ERROR_CODE | EFI_ERROR_MINOR,
585 EFI_PERIPHERAL_LOCAL_CONSOLE | EFI_P_EC_CONTROLLER_ERROR,
586 ParentDevicePath
587 );
588 goto Done;
589 }
590 BiosVideoPrivate->VgaCompatible = FALSE;
591 if (Pci.Hdr.ClassCode[2] == 0x00 && Pci.Hdr.ClassCode[1] == 0x01) {
592 BiosVideoPrivate->VgaCompatible = TRUE;
593 }
594
595 if (Pci.Hdr.ClassCode[2] == 0x03 && Pci.Hdr.ClassCode[1] == 0x00 && Pci.Hdr.ClassCode[0] == 0x00) {
596 BiosVideoPrivate->VgaCompatible = TRUE;
597 }
598
599 if (PcdGetBool (PcdBiosVideoSetTextVgaModeEnable)) {
600 //
601 // Create EXIT_BOOT_SERIVES Event
602 //
603 Status = gBS->CreateEventEx (
604 EVT_NOTIFY_SIGNAL,
605 TPL_NOTIFY,
606 BiosVideoNotifyExitBootServices,
607 BiosVideoPrivate,
608 &gEfiEventExitBootServicesGuid,
609 &BiosVideoPrivate->ExitBootServicesEvent
610 );
611 if (EFI_ERROR (Status)) {
612 goto Done;
613 }
614 }
615
616 //
617 // Initialize the child private structure
618 //
619 BiosVideoPrivate->Signature = BIOS_VIDEO_DEV_SIGNATURE;
620
621 //
622 // Fill in Graphics Output specific mode structures
623 //
624 BiosVideoPrivate->HardwareNeedsStarting = TRUE;
625 BiosVideoPrivate->ModeData = NULL;
626 BiosVideoPrivate->LineBuffer = NULL;
627 BiosVideoPrivate->VgaFrameBuffer = NULL;
628 BiosVideoPrivate->VbeFrameBuffer = NULL;
629
630 //
631 // Fill in the Graphics Output Protocol
632 //
633 BiosVideoPrivate->GraphicsOutput.QueryMode = BiosVideoGraphicsOutputQueryMode;
634 BiosVideoPrivate->GraphicsOutput.SetMode = BiosVideoGraphicsOutputSetMode;
635
636
637 //
638 // Allocate buffer for Graphics Output Protocol mode information
639 //
640 BiosVideoPrivate->GraphicsOutput.Mode = (EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *) AllocatePool (
641 sizeof (EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE)
642 );
643 if (NULL == BiosVideoPrivate->GraphicsOutput.Mode) {
644 Status = EFI_OUT_OF_RESOURCES;
645 goto Done;
646 }
647
648 BiosVideoPrivate->GraphicsOutput.Mode->Info = (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *) AllocatePool (
649 sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION)
650 );
651 if (NULL == BiosVideoPrivate->GraphicsOutput.Mode->Info) {
652 Status = EFI_OUT_OF_RESOURCES;
653 goto Done;
654 }
655
656 //
657 // Assume that Graphics Output Protocol will be produced until proven otherwise
658 //
659 BiosVideoPrivate->ProduceGraphicsOutput = TRUE;
660
661 //
662 // Set Gop Device Path, here RemainingDevicePath will not be one End of Device Path Node.
663 //
664 if ((RemainingDevicePath == NULL) || (!IsDevicePathEnd (RemainingDevicePath))) {
665 if (RemainingDevicePath == NULL) {
666 ZeroMem (&AcpiDeviceNode, sizeof (ACPI_ADR_DEVICE_PATH));
667 AcpiDeviceNode.Header.Type = ACPI_DEVICE_PATH;
668 AcpiDeviceNode.Header.SubType = ACPI_ADR_DP;
669 AcpiDeviceNode.ADR = ACPI_DISPLAY_ADR (1, 0, 0, 1, 0, ACPI_ADR_DISPLAY_TYPE_VGA, 0, 0);
670 SetDevicePathNodeLength (&AcpiDeviceNode.Header, sizeof (ACPI_ADR_DEVICE_PATH));
671
672 BiosVideoPrivate->GopDevicePath = AppendDevicePathNode (
673 ParentDevicePath,
674 (EFI_DEVICE_PATH_PROTOCOL *) &AcpiDeviceNode
675 );
676 } else {
677 BiosVideoPrivate->GopDevicePath = AppendDevicePathNode (ParentDevicePath, RemainingDevicePath);
678 }
679
680 //
681 // Creat child handle and device path protocol firstly
682 //
683 BiosVideoPrivate->Handle = NULL;
684 Status = gBS->InstallMultipleProtocolInterfaces (
685 &BiosVideoPrivate->Handle,
686 &gEfiDevicePathProtocolGuid,
687 BiosVideoPrivate->GopDevicePath,
688 NULL
689 );
690 if (EFI_ERROR (Status)) {
691 goto Done;
692 }
693 }
694
695 //
696 // Fill in the VGA Mini Port Protocol fields
697 //
698 BiosVideoPrivate->VgaMiniPort.SetMode = BiosVideoVgaMiniPortSetMode;
699 BiosVideoPrivate->VgaMiniPort.VgaMemoryOffset = 0xb8000;
700 BiosVideoPrivate->VgaMiniPort.CrtcAddressRegisterOffset = 0x3d4;
701 BiosVideoPrivate->VgaMiniPort.CrtcDataRegisterOffset = 0x3d5;
702 BiosVideoPrivate->VgaMiniPort.VgaMemoryBar = EFI_PCI_IO_PASS_THROUGH_BAR;
703 BiosVideoPrivate->VgaMiniPort.CrtcAddressRegisterBar = EFI_PCI_IO_PASS_THROUGH_BAR;
704 BiosVideoPrivate->VgaMiniPort.CrtcDataRegisterBar = EFI_PCI_IO_PASS_THROUGH_BAR;
705
706 //
707 // Child handle need to consume the Legacy Bios protocol
708 //
709 BiosVideoPrivate->LegacyBios = ParentLegacyBios;
710
711 //
712 // When check for VBE, PCI I/O protocol is needed, so use parent's protocol interface temporally
713 //
714 BiosVideoPrivate->PciIo = ParentPciIo;
715
716 //
717 // Check for VESA BIOS Extensions for modes that are compatible with Graphics Output
718 //
719 if (FeaturePcdGet (PcdBiosVideoCheckVbeEnable)) {
720 Status = BiosVideoCheckForVbe (BiosVideoPrivate);
721 DEBUG ((EFI_D_INFO, "BiosVideoCheckForVbe - %r\n", Status));
722 } else {
723 Status = EFI_UNSUPPORTED;
724 }
725 if (EFI_ERROR (Status)) {
726 //
727 // The VESA BIOS Extensions are not compatible with Graphics Output, so check for support
728 // for the standard 640x480 16 color VGA mode
729 //
730 DEBUG ((EFI_D_INFO, "VgaCompatible - %x\n", BiosVideoPrivate->VgaCompatible));
731 if (BiosVideoPrivate->VgaCompatible) {
732 if (FeaturePcdGet (PcdBiosVideoCheckVgaEnable)) {
733 Status = BiosVideoCheckForVga (BiosVideoPrivate);
734 DEBUG ((EFI_D_INFO, "BiosVideoCheckForVga - %r\n", Status));
735 } else {
736 Status = EFI_UNSUPPORTED;
737 }
738 }
739
740 if (EFI_ERROR (Status)) {
741 //
742 // Free GOP mode structure if it is not freed before
743 // VgaMiniPort does not need this structure any more
744 //
745 if (BiosVideoPrivate->GraphicsOutput.Mode != NULL) {
746 if (BiosVideoPrivate->GraphicsOutput.Mode->Info != NULL) {
747 FreePool (BiosVideoPrivate->GraphicsOutput.Mode->Info);
748 BiosVideoPrivate->GraphicsOutput.Mode->Info = NULL;
749 }
750 FreePool (BiosVideoPrivate->GraphicsOutput.Mode);
751 BiosVideoPrivate->GraphicsOutput.Mode = NULL;
752 }
753
754 //
755 // Neither VBE nor the standard 640x480 16 color VGA mode are supported, so do
756 // not produce the Graphics Output protocol. Instead, produce the VGA MiniPort Protocol.
757 //
758 BiosVideoPrivate->ProduceGraphicsOutput = FALSE;
759
760 //
761 // INT services are available, so on the 80x25 and 80x50 text mode are supported
762 //
763 BiosVideoPrivate->VgaMiniPort.MaxMode = 2;
764 }
765 }
766
767 ProtocolInstalled = FALSE;
768
769 if (BiosVideoPrivate->ProduceGraphicsOutput) {
770 //
771 // Creat child handle and install Graphics Output Protocol,EDID Discovered/Active Protocol
772 //
773 Status = gBS->InstallMultipleProtocolInterfaces (
774 &BiosVideoPrivate->Handle,
775 &gEfiGraphicsOutputProtocolGuid,
776 &BiosVideoPrivate->GraphicsOutput,
777 &gEfiEdidDiscoveredProtocolGuid,
778 &BiosVideoPrivate->EdidDiscovered,
779 &gEfiEdidActiveProtocolGuid,
780 &BiosVideoPrivate->EdidActive,
781 NULL
782 );
783
784 if (!EFI_ERROR (Status)) {
785 //
786 // Open the Parent Handle for the child
787 //
788 Status = gBS->OpenProtocol (
789 ParentHandle,
790 &gEfiPciIoProtocolGuid,
791 (VOID **) &BiosVideoPrivate->PciIo,
792 This->DriverBindingHandle,
793 BiosVideoPrivate->Handle,
794 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
795 );
796 if (EFI_ERROR (Status)) {
797 goto Done;
798 }
799 ProtocolInstalled = TRUE;
800 }
801 }
802
803 if (!ProtocolInstalled) {
804 //
805 // Install VGA Mini Port Protocol
806 //
807 Status = gBS->InstallMultipleProtocolInterfaces (
808 &ParentHandle,
809 &gEfiVgaMiniPortProtocolGuid,
810 &BiosVideoPrivate->VgaMiniPort,
811 NULL
812 );
813 }
814
815 Done:
816 if (EFI_ERROR (Status)) {
817 if ((BiosVideoPrivate != NULL) && (BiosVideoPrivate->ExitBootServicesEvent != NULL)) {
818 gBS->CloseEvent (BiosVideoPrivate->ExitBootServicesEvent);
819 }
820 //
821 // Free private data structure
822 //
823 BiosVideoDeviceReleaseResource (BiosVideoPrivate);
824 }
825
826 return Status;
827 }
828
829
830 /**
831 Deregister an video child handle and free resources.
832
833 @param This Protocol instance pointer.
834 @param Controller Video controller handle
835 @param Handle Video child handle
836
837 @return EFI_STATUS
838
839 **/
840 EFI_STATUS
841 BiosVideoChildHandleUninstall (
842 EFI_DRIVER_BINDING_PROTOCOL *This,
843 EFI_HANDLE Controller,
844 EFI_HANDLE Handle
845 )
846 {
847 EFI_STATUS Status;
848 EFI_IA32_REGISTER_SET Regs;
849 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
850 EFI_VGA_MINI_PORT_PROTOCOL *VgaMiniPort;
851 BIOS_VIDEO_DEV *BiosVideoPrivate;
852 EFI_PCI_IO_PROTOCOL *PciIo;
853
854 BiosVideoPrivate = NULL;
855 GraphicsOutput = NULL;
856 PciIo = NULL;
857 Status = EFI_UNSUPPORTED;
858
859 Status = gBS->OpenProtocol (
860 Handle,
861 &gEfiGraphicsOutputProtocolGuid,
862 (VOID **) &GraphicsOutput,
863 This->DriverBindingHandle,
864 Handle,
865 EFI_OPEN_PROTOCOL_GET_PROTOCOL
866 );
867 if (!EFI_ERROR (Status)) {
868 BiosVideoPrivate = BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (GraphicsOutput);
869 }
870
871 if (EFI_ERROR (Status)) {
872 Status = gBS->OpenProtocol (
873 Handle,
874 &gEfiVgaMiniPortProtocolGuid,
875 (VOID **) &VgaMiniPort,
876 This->DriverBindingHandle,
877 Handle,
878 EFI_OPEN_PROTOCOL_GET_PROTOCOL
879 );
880 if (!EFI_ERROR (Status)) {
881 BiosVideoPrivate = BIOS_VIDEO_DEV_FROM_VGA_MINI_PORT_THIS (VgaMiniPort);
882 }
883 }
884
885 if (BiosVideoPrivate == NULL) {
886 return EFI_UNSUPPORTED;
887 }
888
889 //
890 // Set the 80x25 Text VGA Mode
891 //
892 Regs.H.AH = 0x00;
893 Regs.H.AL = 0x03;
894 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
895
896 Regs.H.AH = 0x11;
897 Regs.H.AL = 0x14;
898 Regs.H.BL = 0;
899 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
900
901 //
902 // Close PCI I/O protocol that opened by child handle
903 //
904 Status = gBS->CloseProtocol (
905 Controller,
906 &gEfiPciIoProtocolGuid,
907 This->DriverBindingHandle,
908 Handle
909 );
910
911 //
912 // Uninstall protocols on child handle
913 //
914 if (BiosVideoPrivate->ProduceGraphicsOutput) {
915 Status = gBS->UninstallMultipleProtocolInterfaces (
916 BiosVideoPrivate->Handle,
917 &gEfiDevicePathProtocolGuid,
918 BiosVideoPrivate->GopDevicePath,
919 &gEfiGraphicsOutputProtocolGuid,
920 &BiosVideoPrivate->GraphicsOutput,
921 &gEfiEdidDiscoveredProtocolGuid,
922 &BiosVideoPrivate->EdidDiscovered,
923 &gEfiEdidActiveProtocolGuid,
924 &BiosVideoPrivate->EdidActive,
925 NULL
926 );
927 }
928 if (!BiosVideoPrivate->ProduceGraphicsOutput) {
929 Status = gBS->UninstallMultipleProtocolInterfaces (
930 Controller,
931 &gEfiVgaMiniPortProtocolGuid,
932 &BiosVideoPrivate->VgaMiniPort,
933 NULL
934 );
935 }
936
937 if (EFI_ERROR (Status)) {
938 gBS->OpenProtocol (
939 Controller,
940 &gEfiPciIoProtocolGuid,
941 (VOID **) &PciIo,
942 This->DriverBindingHandle,
943 Handle,
944 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
945 );
946 return Status;
947 }
948
949 if (PcdGetBool (PcdBiosVideoSetTextVgaModeEnable)) {
950 //
951 // Close EXIT_BOOT_SERIVES Event
952 //
953 gBS->CloseEvent (BiosVideoPrivate->ExitBootServicesEvent);
954 }
955
956 //
957 // Release all allocated resources
958 //
959 BiosVideoDeviceReleaseResource (BiosVideoPrivate);
960
961 return EFI_SUCCESS;
962 }
963
964
965 /**
966 Release resource for biso video instance.
967
968 @param BiosVideoPrivate Video child device private data structure
969
970 **/
971 VOID
972 BiosVideoDeviceReleaseResource (
973 BIOS_VIDEO_DEV *BiosVideoPrivate
974 )
975 {
976 if (BiosVideoPrivate == NULL) {
977 return ;
978 }
979
980 //
981 // Release all the resourses occupied by the BIOS_VIDEO_DEV
982 //
983
984 //
985 // Free VGA Frame Buffer
986 //
987 if (BiosVideoPrivate->VgaFrameBuffer != NULL) {
988 FreePool (BiosVideoPrivate->VgaFrameBuffer);
989 }
990 //
991 // Free VBE Frame Buffer
992 //
993 if (BiosVideoPrivate->VbeFrameBuffer != NULL) {
994 FreePool (BiosVideoPrivate->VbeFrameBuffer);
995 }
996 //
997 // Free line buffer
998 //
999 if (BiosVideoPrivate->LineBuffer != NULL) {
1000 FreePool (BiosVideoPrivate->LineBuffer);
1001 }
1002 //
1003 // Free mode data
1004 //
1005 if (BiosVideoPrivate->ModeData != NULL) {
1006 FreePool (BiosVideoPrivate->ModeData);
1007 }
1008 //
1009 // Free memory allocated below 1MB
1010 //
1011 if (BiosVideoPrivate->PagesBelow1MB != 0) {
1012 gBS->FreePages (BiosVideoPrivate->PagesBelow1MB, BiosVideoPrivate->NumberOfPagesBelow1MB);
1013 }
1014
1015 if (BiosVideoPrivate->VbeSaveRestorePages != 0) {
1016 gBS->FreePages (BiosVideoPrivate->VbeSaveRestoreBuffer, BiosVideoPrivate->VbeSaveRestorePages);
1017 }
1018
1019 //
1020 // Free graphics output protocol occupied resource
1021 //
1022 if (BiosVideoPrivate->GraphicsOutput.Mode != NULL) {
1023 if (BiosVideoPrivate->GraphicsOutput.Mode->Info != NULL) {
1024 FreePool (BiosVideoPrivate->GraphicsOutput.Mode->Info);
1025 BiosVideoPrivate->GraphicsOutput.Mode->Info = NULL;
1026 }
1027 FreePool (BiosVideoPrivate->GraphicsOutput.Mode);
1028 BiosVideoPrivate->GraphicsOutput.Mode = NULL;
1029 }
1030 //
1031 // Free EDID discovered protocol occupied resource
1032 //
1033 if (BiosVideoPrivate->EdidDiscovered.Edid != NULL) {
1034 FreePool (BiosVideoPrivate->EdidDiscovered.Edid);
1035 }
1036 //
1037 // Free EDID active protocol occupied resource
1038 //
1039 if (BiosVideoPrivate->EdidActive.Edid != NULL) {
1040 FreePool (BiosVideoPrivate->EdidActive.Edid);
1041 }
1042
1043 if (BiosVideoPrivate->GopDevicePath!= NULL) {
1044 FreePool (BiosVideoPrivate->GopDevicePath);
1045 }
1046
1047 FreePool (BiosVideoPrivate);
1048
1049 return ;
1050 }
1051
1052
1053 /**
1054 Generate a search key for a specified timing data.
1055
1056 @param EdidTiming Pointer to EDID timing
1057
1058 @return The 32 bit unique key for search.
1059
1060 **/
1061 UINT32
1062 CalculateEdidKey (
1063 VESA_BIOS_EXTENSIONS_EDID_TIMING *EdidTiming
1064 )
1065 {
1066 UINT32 Key;
1067
1068 //
1069 // Be sure no conflicts for all standard timing defined by VESA.
1070 //
1071 Key = (EdidTiming->HorizontalResolution * 2) + EdidTiming->VerticalResolution;
1072 return Key;
1073 }
1074
1075
1076 /**
1077 Parse the Established Timing and Standard Timing in EDID data block.
1078
1079 @param EdidBuffer Pointer to EDID data block
1080 @param ValidEdidTiming Valid EDID timing information
1081
1082 @retval TRUE The EDID data is valid.
1083 @retval FALSE The EDID data is invalid.
1084
1085 **/
1086 BOOLEAN
1087 ParseEdidData (
1088 UINT8 *EdidBuffer,
1089 VESA_BIOS_EXTENSIONS_VALID_EDID_TIMING *ValidEdidTiming
1090 )
1091 {
1092 UINT8 CheckSum;
1093 UINT32 Index;
1094 UINT32 ValidNumber;
1095 UINT32 TimingBits;
1096 UINT8 *BufferIndex;
1097 UINT16 HorizontalResolution;
1098 UINT16 VerticalResolution;
1099 UINT8 AspectRatio;
1100 UINT8 RefreshRate;
1101 VESA_BIOS_EXTENSIONS_EDID_TIMING TempTiming;
1102 VESA_BIOS_EXTENSIONS_EDID_DATA_BLOCK *EdidDataBlock;
1103
1104 EdidDataBlock = (VESA_BIOS_EXTENSIONS_EDID_DATA_BLOCK *) EdidBuffer;
1105
1106 //
1107 // Check the checksum of EDID data
1108 //
1109 CheckSum = 0;
1110 for (Index = 0; Index < VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE; Index ++) {
1111 CheckSum = (UINT8) (CheckSum + EdidBuffer[Index]);
1112 }
1113 if (CheckSum != 0) {
1114 return FALSE;
1115 }
1116
1117 ValidNumber = 0;
1118 gBS->SetMem (ValidEdidTiming, sizeof (VESA_BIOS_EXTENSIONS_VALID_EDID_TIMING), 0);
1119
1120 if ((EdidDataBlock->EstablishedTimings[0] != 0) ||
1121 (EdidDataBlock->EstablishedTimings[1] != 0) ||
1122 (EdidDataBlock->EstablishedTimings[2] != 0)
1123 ) {
1124 //
1125 // Established timing data
1126 //
1127 TimingBits = EdidDataBlock->EstablishedTimings[0] |
1128 (EdidDataBlock->EstablishedTimings[1] << 8) |
1129 ((EdidDataBlock->EstablishedTimings[2] & 0x80) << 9) ;
1130 for (Index = 0; Index < VESA_BIOS_EXTENSIONS_EDID_ESTABLISHED_TIMING_MAX_NUMBER; Index ++) {
1131 if ((TimingBits & 0x1) != 0) {
1132 DEBUG ((EFI_D_INFO, "Established Timing: %d x %d\n",
1133 mEstablishedEdidTiming[Index].HorizontalResolution, mEstablishedEdidTiming[Index].VerticalResolution));
1134 ValidEdidTiming->Key[ValidNumber] = CalculateEdidKey (&mEstablishedEdidTiming[Index]);
1135 ValidNumber ++;
1136 }
1137 TimingBits = TimingBits >> 1;
1138 }
1139 }
1140
1141 //
1142 // Parse the standard timing data
1143 //
1144 BufferIndex = &EdidDataBlock->StandardTimingIdentification[0];
1145 for (Index = 0; Index < 8; Index ++) {
1146 //
1147 // Check if this is a valid Standard Timing entry
1148 // VESA documents unused fields should be set to 01h
1149 //
1150 if ((BufferIndex[0] != 0x1) && (BufferIndex[1] != 0x1)){
1151 //
1152 // A valid Standard Timing
1153 //
1154 HorizontalResolution = (UINT16) (BufferIndex[0] * 8 + 248);
1155 AspectRatio = (UINT8) (BufferIndex[1] >> 6);
1156 switch (AspectRatio) {
1157 case 0:
1158 VerticalResolution = (UINT16) (HorizontalResolution / 16 * 10);
1159 break;
1160 case 1:
1161 VerticalResolution = (UINT16) (HorizontalResolution / 4 * 3);
1162 break;
1163 case 2:
1164 VerticalResolution = (UINT16) (HorizontalResolution / 5 * 4);
1165 break;
1166 case 3:
1167 VerticalResolution = (UINT16) (HorizontalResolution / 16 * 9);
1168 break;
1169 default:
1170 VerticalResolution = (UINT16) (HorizontalResolution / 4 * 3);
1171 break;
1172 }
1173 RefreshRate = (UINT8) ((BufferIndex[1] & 0x1f) + 60);
1174 DEBUG ((EFI_D_INFO, "Standard Timing: %d x %d\n", HorizontalResolution, VerticalResolution));
1175 TempTiming.HorizontalResolution = HorizontalResolution;
1176 TempTiming.VerticalResolution = VerticalResolution;
1177 TempTiming.RefreshRate = RefreshRate;
1178 ValidEdidTiming->Key[ValidNumber] = CalculateEdidKey (&TempTiming);
1179 ValidNumber ++;
1180 }
1181 BufferIndex += 2;
1182 }
1183
1184 //
1185 // Parse the Detailed Timing data
1186 //
1187 BufferIndex = &EdidDataBlock->DetailedTimingDescriptions[0];
1188 for (Index = 0; Index < 4; Index ++, BufferIndex += VESA_BIOS_EXTENSIONS_DETAILED_TIMING_EACH_DESCRIPTOR_SIZE) {
1189 if ((BufferIndex[0] == 0x0) && (BufferIndex[1] == 0x0)) {
1190 //
1191 // Check if this is a valid Detailed Timing Descriptor
1192 // If first 2 bytes are zero, it is monitor descriptor other than detailed timing descriptor
1193 //
1194 continue;
1195 }
1196 //
1197 // Calculate Horizontal and Vertical resolution
1198 //
1199 TempTiming.HorizontalResolution = ((UINT16)(BufferIndex[4] & 0xF0) << 4) | (BufferIndex[2]);
1200 TempTiming.VerticalResolution = ((UINT16)(BufferIndex[7] & 0xF0) << 4) | (BufferIndex[5]);
1201 DEBUG ((EFI_D_INFO, "Detailed Timing %d: %d x %d\n",
1202 Index, TempTiming.HorizontalResolution, TempTiming.VerticalResolution));
1203 ValidEdidTiming->Key[ValidNumber] = CalculateEdidKey (&TempTiming);
1204 ValidNumber ++;
1205 }
1206
1207 ValidEdidTiming->ValidNumber = ValidNumber;
1208 return TRUE;
1209 }
1210
1211
1212 /**
1213 Search a specified Timing in all the valid EDID timings.
1214
1215 @param ValidEdidTiming All valid EDID timing information.
1216 @param EdidTiming The Timing to search for.
1217
1218 @retval TRUE Found.
1219 @retval FALSE Not found.
1220
1221 **/
1222 BOOLEAN
1223 SearchEdidTiming (
1224 VESA_BIOS_EXTENSIONS_VALID_EDID_TIMING *ValidEdidTiming,
1225 VESA_BIOS_EXTENSIONS_EDID_TIMING *EdidTiming
1226 )
1227 {
1228 UINT32 Index;
1229 UINT32 Key;
1230
1231 Key = CalculateEdidKey (EdidTiming);
1232
1233 for (Index = 0; Index < ValidEdidTiming->ValidNumber; Index ++) {
1234 if (Key == ValidEdidTiming->Key[Index]) {
1235 return TRUE;
1236 }
1237 }
1238
1239 return FALSE;
1240 }
1241
1242 /**
1243 Check if all video child handles have been uninstalled.
1244
1245 @param Controller Video controller handle
1246
1247 @return TRUE Child handles exist.
1248 @return FALSE All video child handles have been uninstalled.
1249
1250 **/
1251 BOOLEAN
1252 HasChildHandle (
1253 IN EFI_HANDLE Controller
1254 )
1255 {
1256 UINTN Index;
1257 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;
1258 UINTN EntryCount;
1259 BOOLEAN HasChild;
1260 EFI_STATUS Status;
1261
1262 EntryCount = 0;
1263 HasChild = FALSE;
1264 Status = gBS->OpenProtocolInformation (
1265 Controller,
1266 &gEfiPciIoProtocolGuid,
1267 &OpenInfoBuffer,
1268 &EntryCount
1269 );
1270 for (Index = 0; Index < EntryCount; Index++) {
1271 if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {
1272 HasChild = TRUE;
1273 }
1274 }
1275
1276 return HasChild;
1277 }
1278
1279 /**
1280 Check for VBE device.
1281
1282 @param BiosVideoPrivate Pointer to BIOS_VIDEO_DEV structure
1283
1284 @retval EFI_SUCCESS VBE device found
1285
1286 **/
1287 EFI_STATUS
1288 BiosVideoCheckForVbe (
1289 IN OUT BIOS_VIDEO_DEV *BiosVideoPrivate
1290 )
1291 {
1292 EFI_STATUS Status;
1293 EFI_IA32_REGISTER_SET Regs;
1294 UINT16 *ModeNumberPtr;
1295 UINT16 VbeModeNumber;
1296 BOOLEAN ModeFound;
1297 BOOLEAN EdidFound;
1298 BIOS_VIDEO_MODE_DATA *ModeBuffer;
1299 BIOS_VIDEO_MODE_DATA *CurrentModeData;
1300 UINTN PreferMode;
1301 UINTN ModeNumber;
1302 VESA_BIOS_EXTENSIONS_EDID_TIMING Timing;
1303 VESA_BIOS_EXTENSIONS_VALID_EDID_TIMING ValidEdidTiming;
1304 EFI_EDID_OVERRIDE_PROTOCOL *EdidOverride;
1305 UINT32 EdidAttributes;
1306 BOOLEAN EdidOverrideFound;
1307 UINTN EdidOverrideDataSize;
1308 UINT8 *EdidOverrideDataBlock;
1309 UINTN EdidActiveDataSize;
1310 UINT8 *EdidActiveDataBlock;
1311 UINT32 HighestHorizontalResolution;
1312 UINT32 HighestVerticalResolution;
1313 UINTN HighestResolutionMode;
1314
1315 EdidFound = TRUE;
1316 EdidOverrideFound = FALSE;
1317 EdidOverrideDataBlock = NULL;
1318 EdidActiveDataSize = 0;
1319 EdidActiveDataBlock = NULL;
1320 HighestHorizontalResolution = 0;
1321 HighestVerticalResolution = 0;
1322 HighestResolutionMode = 0;
1323
1324 //
1325 // Allocate buffer under 1MB for VBE data structures
1326 //
1327 BiosVideoPrivate->NumberOfPagesBelow1MB = EFI_SIZE_TO_PAGES (
1328 sizeof (VESA_BIOS_EXTENSIONS_INFORMATION_BLOCK) +
1329 sizeof (VESA_BIOS_EXTENSIONS_MODE_INFORMATION_BLOCK) +
1330 sizeof (VESA_BIOS_EXTENSIONS_EDID_DATA_BLOCK) +
1331 sizeof (VESA_BIOS_EXTENSIONS_CRTC_INFORMATION_BLOCK)
1332 );
1333
1334 BiosVideoPrivate->PagesBelow1MB = 0x00100000 - 1;
1335
1336 Status = gBS->AllocatePages (
1337 AllocateMaxAddress,
1338 EfiBootServicesData,
1339 BiosVideoPrivate->NumberOfPagesBelow1MB,
1340 &BiosVideoPrivate->PagesBelow1MB
1341 );
1342 if (EFI_ERROR (Status)) {
1343 return Status;
1344 }
1345
1346 ZeroMem (&ValidEdidTiming, sizeof (VESA_BIOS_EXTENSIONS_VALID_EDID_TIMING));
1347
1348 //
1349 // Fill in the VBE related data structures
1350 //
1351 BiosVideoPrivate->VbeInformationBlock = (VESA_BIOS_EXTENSIONS_INFORMATION_BLOCK *) (UINTN) (BiosVideoPrivate->PagesBelow1MB);
1352 BiosVideoPrivate->VbeModeInformationBlock = (VESA_BIOS_EXTENSIONS_MODE_INFORMATION_BLOCK *) (BiosVideoPrivate->VbeInformationBlock + 1);
1353 BiosVideoPrivate->VbeEdidDataBlock = (VESA_BIOS_EXTENSIONS_EDID_DATA_BLOCK *) (BiosVideoPrivate->VbeModeInformationBlock + 1);
1354 BiosVideoPrivate->VbeCrtcInformationBlock = (VESA_BIOS_EXTENSIONS_CRTC_INFORMATION_BLOCK *) (BiosVideoPrivate->VbeEdidDataBlock + 1);
1355 BiosVideoPrivate->VbeSaveRestorePages = 0;
1356 BiosVideoPrivate->VbeSaveRestoreBuffer = 0;
1357
1358 //
1359 // Test to see if the Video Adapter is compliant with VBE 3.0
1360 //
1361 gBS->SetMem (&Regs, sizeof (Regs), 0);
1362 Regs.X.AX = VESA_BIOS_EXTENSIONS_RETURN_CONTROLLER_INFORMATION;
1363 gBS->SetMem (BiosVideoPrivate->VbeInformationBlock, sizeof (VESA_BIOS_EXTENSIONS_INFORMATION_BLOCK), 0);
1364 BiosVideoPrivate->VbeInformationBlock->VESASignature = VESA_BIOS_EXTENSIONS_VBE2_SIGNATURE;
1365 Regs.X.ES = EFI_SEGMENT ((UINTN) BiosVideoPrivate->VbeInformationBlock);
1366 Regs.X.DI = EFI_OFFSET ((UINTN) BiosVideoPrivate->VbeInformationBlock);
1367
1368 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
1369
1370 Status = EFI_DEVICE_ERROR;
1371
1372 //
1373 // See if the VESA call succeeded
1374 //
1375 if (Regs.X.AX != VESA_BIOS_EXTENSIONS_STATUS_SUCCESS) {
1376 return Status;
1377 }
1378 //
1379 // Check for 'VESA' signature
1380 //
1381 if (BiosVideoPrivate->VbeInformationBlock->VESASignature != VESA_BIOS_EXTENSIONS_VESA_SIGNATURE) {
1382 return Status;
1383 }
1384 //
1385 // Check to see if this is VBE 2.0 or higher
1386 //
1387 if (BiosVideoPrivate->VbeInformationBlock->VESAVersion < VESA_BIOS_EXTENSIONS_VERSION_2_0) {
1388 return Status;
1389 }
1390
1391 EdidFound = FALSE;
1392 EdidAttributes = 0xff;
1393 EdidOverrideDataSize = 0;
1394
1395 //
1396 // Find EDID Override protocol firstly, this protocol is installed by platform if needed.
1397 //
1398 Status = gBS->LocateProtocol (
1399 &gEfiEdidOverrideProtocolGuid,
1400 NULL,
1401 (VOID **) &EdidOverride
1402 );
1403 if (!EFI_ERROR (Status)) {
1404 //
1405 // Allocate double size of VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE to avoid overflow
1406 //
1407 EdidOverrideDataBlock = AllocatePool (VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE * 2);
1408 if (NULL == EdidOverrideDataBlock) {
1409 Status = EFI_OUT_OF_RESOURCES;
1410 goto Done;
1411 }
1412
1413 Status = EdidOverride->GetEdid (
1414 EdidOverride,
1415 BiosVideoPrivate->Handle,
1416 &EdidAttributes,
1417 &EdidOverrideDataSize,
1418 (UINT8 **) &EdidOverrideDataBlock
1419 );
1420 if (!EFI_ERROR (Status) &&
1421 EdidAttributes == 0 &&
1422 EdidOverrideDataSize != 0) {
1423 //
1424 // Succeeded to get EDID Override Data
1425 //
1426 EdidOverrideFound = TRUE;
1427 }
1428 }
1429
1430 if (!EdidOverrideFound || EdidAttributes == EFI_EDID_OVERRIDE_DONT_OVERRIDE) {
1431 //
1432 // If EDID Override data doesn't exist or EFI_EDID_OVERRIDE_DONT_OVERRIDE returned,
1433 // read EDID information through INT10 call
1434 //
1435
1436 gBS->SetMem (&Regs, sizeof (Regs), 0);
1437 Regs.X.AX = VESA_BIOS_EXTENSIONS_EDID;
1438 Regs.X.BX = 1;
1439 Regs.X.CX = 0;
1440 Regs.X.DX = 0;
1441 Regs.X.ES = EFI_SEGMENT ((UINTN) BiosVideoPrivate->VbeEdidDataBlock);
1442 Regs.X.DI = EFI_OFFSET ((UINTN) BiosVideoPrivate->VbeEdidDataBlock);
1443
1444 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
1445 //
1446 // See if the VESA call succeeded
1447 //
1448 if (Regs.X.AX == VESA_BIOS_EXTENSIONS_STATUS_SUCCESS) {
1449 //
1450 // Set EDID Discovered Data
1451 //
1452 BiosVideoPrivate->EdidDiscovered.SizeOfEdid = VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE;
1453 BiosVideoPrivate->EdidDiscovered.Edid = (UINT8 *) AllocateCopyPool (
1454 VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE,
1455 BiosVideoPrivate->VbeEdidDataBlock
1456 );
1457
1458 if (NULL == BiosVideoPrivate->EdidDiscovered.Edid) {
1459 Status = EFI_OUT_OF_RESOURCES;
1460 goto Done;
1461 }
1462
1463 EdidFound = TRUE;
1464 }
1465 }
1466
1467 if (EdidFound) {
1468 EdidActiveDataSize = VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE;
1469 EdidActiveDataBlock = BiosVideoPrivate->EdidDiscovered.Edid;
1470 } else if (EdidOverrideFound) {
1471 EdidActiveDataSize = EdidOverrideDataSize;
1472 EdidActiveDataBlock = EdidOverrideDataBlock;
1473 EdidFound = TRUE;
1474 }
1475
1476 if (EdidFound) {
1477 //
1478 // Parse EDID data structure to retrieve modes supported by monitor
1479 //
1480 if (ParseEdidData ((UINT8 *) EdidActiveDataBlock, &ValidEdidTiming)) {
1481 //
1482 // Copy EDID Override Data to EDID Active Data
1483 //
1484 BiosVideoPrivate->EdidActive.SizeOfEdid = (UINT32) EdidActiveDataSize;
1485 BiosVideoPrivate->EdidActive.Edid = (UINT8 *) AllocateCopyPool (
1486 EdidActiveDataSize,
1487 EdidActiveDataBlock
1488 );
1489 if (NULL == BiosVideoPrivate->EdidActive.Edid) {
1490 Status = EFI_OUT_OF_RESOURCES;
1491 goto Done;
1492 }
1493 }
1494 } else {
1495 BiosVideoPrivate->EdidActive.SizeOfEdid = 0;
1496 BiosVideoPrivate->EdidActive.Edid = NULL;
1497 EdidFound = FALSE;
1498 }
1499
1500 //
1501 // Walk through the mode list to see if there is at least one mode the is compatible with the EDID mode
1502 //
1503 ModeNumberPtr = (UINT16 *)
1504 (
1505 (((UINTN) BiosVideoPrivate->VbeInformationBlock->VideoModePtr & 0xffff0000) >> 12) |
1506 ((UINTN) BiosVideoPrivate->VbeInformationBlock->VideoModePtr & 0x0000ffff)
1507 );
1508
1509 PreferMode = 0;
1510 ModeNumber = 0;
1511
1512 //
1513 // ModeNumberPtr may be not 16-byte aligned, so ReadUnaligned16 is used to access the buffer pointed by ModeNumberPtr.
1514 //
1515 for (VbeModeNumber = ReadUnaligned16 (ModeNumberPtr);
1516 VbeModeNumber != VESA_BIOS_EXTENSIONS_END_OF_MODE_LIST;
1517 VbeModeNumber = ReadUnaligned16 (++ModeNumberPtr)) {
1518 //
1519 // Make sure this is a mode number defined by the VESA VBE specification. If it isn'tm then skip this mode number.
1520 //
1521 if ((VbeModeNumber & VESA_BIOS_EXTENSIONS_MODE_NUMBER_VESA) == 0) {
1522 continue;
1523 }
1524 //
1525 // Get the information about the mode
1526 //
1527 gBS->SetMem (&Regs, sizeof (Regs), 0);
1528 Regs.X.AX = VESA_BIOS_EXTENSIONS_RETURN_MODE_INFORMATION;
1529 Regs.X.CX = VbeModeNumber;
1530 gBS->SetMem (BiosVideoPrivate->VbeModeInformationBlock, sizeof (VESA_BIOS_EXTENSIONS_MODE_INFORMATION_BLOCK), 0);
1531 Regs.X.ES = EFI_SEGMENT ((UINTN) BiosVideoPrivate->VbeModeInformationBlock);
1532 Regs.X.DI = EFI_OFFSET ((UINTN) BiosVideoPrivate->VbeModeInformationBlock);
1533
1534 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
1535
1536 //
1537 // See if the call succeeded. If it didn't, then try the next mode.
1538 //
1539 if (Regs.X.AX != VESA_BIOS_EXTENSIONS_STATUS_SUCCESS) {
1540 continue;
1541 }
1542 //
1543 // See if the mode supports color. If it doesn't then try the next mode.
1544 //
1545 if ((BiosVideoPrivate->VbeModeInformationBlock->ModeAttributes & VESA_BIOS_EXTENSIONS_MODE_ATTRIBUTE_COLOR) == 0) {
1546 continue;
1547 }
1548 //
1549 // See if the mode supports graphics. If it doesn't then try the next mode.
1550 //
1551 if ((BiosVideoPrivate->VbeModeInformationBlock->ModeAttributes & VESA_BIOS_EXTENSIONS_MODE_ATTRIBUTE_GRAPHICS) == 0) {
1552 continue;
1553 }
1554 //
1555 // See if the mode supports a linear frame buffer. If it doesn't then try the next mode.
1556 //
1557 if ((BiosVideoPrivate->VbeModeInformationBlock->ModeAttributes & VESA_BIOS_EXTENSIONS_MODE_ATTRIBUTE_LINEAR_FRAME_BUFFER) == 0) {
1558 continue;
1559 }
1560 //
1561 // See if the mode supports 32 bit color. If it doesn't then try the next mode.
1562 // 32 bit mode can be implemented by 24 Bits Per Pixels. Also make sure the
1563 // number of bits per pixel is a multiple of 8 or more than 32 bits per pixel
1564 //
1565 if (BiosVideoPrivate->VbeModeInformationBlock->BitsPerPixel < 24) {
1566 continue;
1567 }
1568
1569 if (BiosVideoPrivate->VbeModeInformationBlock->BitsPerPixel > 32) {
1570 continue;
1571 }
1572
1573 if ((BiosVideoPrivate->VbeModeInformationBlock->BitsPerPixel % 8) != 0) {
1574 continue;
1575 }
1576 //
1577 // See if the physical base pointer for the linear mode is valid. If it isn't then try the next mode.
1578 //
1579 if (BiosVideoPrivate->VbeModeInformationBlock->PhysBasePtr == 0) {
1580 continue;
1581 }
1582
1583 DEBUG ((EFI_D_INFO, "Video Controller Mode 0x%x: %d x %d\n",
1584 VbeModeNumber, BiosVideoPrivate->VbeModeInformationBlock->XResolution, BiosVideoPrivate->VbeModeInformationBlock->YResolution));
1585
1586 if (EdidFound && (ValidEdidTiming.ValidNumber > 0)) {
1587 //
1588 // EDID exist, check whether this mode match with any mode in EDID
1589 //
1590 Timing.HorizontalResolution = BiosVideoPrivate->VbeModeInformationBlock->XResolution;
1591 Timing.VerticalResolution = BiosVideoPrivate->VbeModeInformationBlock->YResolution;
1592 if (!SearchEdidTiming (&ValidEdidTiming, &Timing)) {
1593 continue;
1594 }
1595 }
1596
1597 //
1598 // Select a reasonable mode to be set for current display mode
1599 //
1600 ModeFound = FALSE;
1601
1602 if (BiosVideoPrivate->VbeModeInformationBlock->XResolution == 1024 &&
1603 BiosVideoPrivate->VbeModeInformationBlock->YResolution == 768
1604 ) {
1605 ModeFound = TRUE;
1606 }
1607 if (BiosVideoPrivate->VbeModeInformationBlock->XResolution == 800 &&
1608 BiosVideoPrivate->VbeModeInformationBlock->YResolution == 600
1609 ) {
1610 ModeFound = TRUE;
1611 PreferMode = ModeNumber;
1612 }
1613 if (BiosVideoPrivate->VbeModeInformationBlock->XResolution == 640 &&
1614 BiosVideoPrivate->VbeModeInformationBlock->YResolution == 480
1615 ) {
1616 ModeFound = TRUE;
1617 }
1618
1619 if ((!EdidFound) && (!ModeFound)) {
1620 //
1621 // When no EDID exist, only select three possible resolutions, i.e. 1024x768, 800x600, 640x480
1622 //
1623 continue;
1624 }
1625
1626 //
1627 // Record the highest resolution mode to set later
1628 //
1629 if ((BiosVideoPrivate->VbeModeInformationBlock->XResolution > HighestHorizontalResolution) ||
1630 ((BiosVideoPrivate->VbeModeInformationBlock->XResolution == HighestHorizontalResolution) &&
1631 (BiosVideoPrivate->VbeModeInformationBlock->YResolution > HighestVerticalResolution))) {
1632 HighestHorizontalResolution = BiosVideoPrivate->VbeModeInformationBlock->XResolution;
1633 HighestVerticalResolution = BiosVideoPrivate->VbeModeInformationBlock->YResolution;
1634 HighestResolutionMode = ModeNumber;
1635 }
1636
1637 //
1638 // Add mode to the list of available modes
1639 //
1640 ModeNumber ++;
1641 ModeBuffer = (BIOS_VIDEO_MODE_DATA *) AllocatePool (
1642 ModeNumber * sizeof (BIOS_VIDEO_MODE_DATA)
1643 );
1644 if (NULL == ModeBuffer) {
1645 Status = EFI_OUT_OF_RESOURCES;
1646 goto Done;
1647 }
1648
1649 if (ModeNumber > 1) {
1650 CopyMem (
1651 ModeBuffer,
1652 BiosVideoPrivate->ModeData,
1653 (ModeNumber - 1) * sizeof (BIOS_VIDEO_MODE_DATA)
1654 );
1655 }
1656
1657 if (BiosVideoPrivate->ModeData != NULL) {
1658 FreePool (BiosVideoPrivate->ModeData);
1659 }
1660
1661 CurrentModeData = &ModeBuffer[ModeNumber - 1];
1662 CurrentModeData->VbeModeNumber = VbeModeNumber;
1663 if (BiosVideoPrivate->VbeInformationBlock->VESAVersion >= VESA_BIOS_EXTENSIONS_VERSION_3_0) {
1664 CurrentModeData->BytesPerScanLine = BiosVideoPrivate->VbeModeInformationBlock->LinBytesPerScanLine;
1665 CurrentModeData->Red.Position = BiosVideoPrivate->VbeModeInformationBlock->LinRedFieldPosition;
1666 CurrentModeData->Red.Mask = (UINT8) ((1 << BiosVideoPrivate->VbeModeInformationBlock->LinRedMaskSize) - 1);
1667 CurrentModeData->Blue.Position = BiosVideoPrivate->VbeModeInformationBlock->LinBlueFieldPosition;
1668 CurrentModeData->Blue.Mask = (UINT8) ((1 << BiosVideoPrivate->VbeModeInformationBlock->LinBlueMaskSize) - 1);
1669 CurrentModeData->Green.Position = BiosVideoPrivate->VbeModeInformationBlock->LinGreenFieldPosition;
1670 CurrentModeData->Green.Mask = (UINT8) ((1 << BiosVideoPrivate->VbeModeInformationBlock->LinGreenMaskSize) - 1);
1671 CurrentModeData->Reserved.Position = BiosVideoPrivate->VbeModeInformationBlock->LinRsvdFieldPosition;
1672 CurrentModeData->Reserved.Mask = (UINT8) ((1 << BiosVideoPrivate->VbeModeInformationBlock->LinRsvdMaskSize) - 1);
1673 } else {
1674 CurrentModeData->BytesPerScanLine = BiosVideoPrivate->VbeModeInformationBlock->BytesPerScanLine;
1675 CurrentModeData->Red.Position = BiosVideoPrivate->VbeModeInformationBlock->RedFieldPosition;
1676 CurrentModeData->Red.Mask = (UINT8) ((1 << BiosVideoPrivate->VbeModeInformationBlock->RedMaskSize) - 1);
1677 CurrentModeData->Blue.Position = BiosVideoPrivate->VbeModeInformationBlock->BlueFieldPosition;
1678 CurrentModeData->Blue.Mask = (UINT8) ((1 << BiosVideoPrivate->VbeModeInformationBlock->BlueMaskSize) - 1);
1679 CurrentModeData->Green.Position = BiosVideoPrivate->VbeModeInformationBlock->GreenFieldPosition;
1680 CurrentModeData->Green.Mask = (UINT8) ((1 << BiosVideoPrivate->VbeModeInformationBlock->GreenMaskSize) - 1);
1681 CurrentModeData->Reserved.Position = BiosVideoPrivate->VbeModeInformationBlock->RsvdFieldPosition;
1682 CurrentModeData->Reserved.Mask = (UINT8) ((1 << BiosVideoPrivate->VbeModeInformationBlock->RsvdMaskSize) - 1);
1683 }
1684
1685 CurrentModeData->PixelFormat = PixelBitMask;
1686 if ((BiosVideoPrivate->VbeModeInformationBlock->BitsPerPixel == 32) &&
1687 (CurrentModeData->Red.Mask == 0xff) && (CurrentModeData->Green.Mask == 0xff) && (CurrentModeData->Blue.Mask == 0xff)) {
1688 if ((CurrentModeData->Red.Position == 0) && (CurrentModeData->Green.Position == 8) && (CurrentModeData->Blue.Position == 16)) {
1689 CurrentModeData->PixelFormat = PixelRedGreenBlueReserved8BitPerColor;
1690 } else if ((CurrentModeData->Blue.Position == 0) && (CurrentModeData->Green.Position == 8) && (CurrentModeData->Red.Position == 16)) {
1691 CurrentModeData->PixelFormat = PixelBlueGreenRedReserved8BitPerColor;
1692 }
1693 }
1694
1695 CurrentModeData->PixelBitMask.RedMask = ((UINT32) CurrentModeData->Red.Mask) << CurrentModeData->Red.Position;
1696 CurrentModeData->PixelBitMask.GreenMask = ((UINT32) CurrentModeData->Green.Mask) << CurrentModeData->Green.Position;
1697 CurrentModeData->PixelBitMask.BlueMask = ((UINT32) CurrentModeData->Blue.Mask) << CurrentModeData->Blue.Position;
1698 CurrentModeData->PixelBitMask.ReservedMask = ((UINT32) CurrentModeData->Reserved.Mask) << CurrentModeData->Reserved.Position;
1699
1700 CurrentModeData->LinearFrameBuffer = (VOID *) (UINTN)BiosVideoPrivate->VbeModeInformationBlock->PhysBasePtr;
1701 CurrentModeData->HorizontalResolution = BiosVideoPrivate->VbeModeInformationBlock->XResolution;
1702 CurrentModeData->VerticalResolution = BiosVideoPrivate->VbeModeInformationBlock->YResolution;
1703
1704 CurrentModeData->BitsPerPixel = BiosVideoPrivate->VbeModeInformationBlock->BitsPerPixel;
1705 CurrentModeData->FrameBufferSize = CurrentModeData->BytesPerScanLine * CurrentModeData->VerticalResolution;
1706 //
1707 // Make sure the FrameBufferSize does not exceed the max available frame buffer size reported by VEB.
1708 //
1709 ASSERT (CurrentModeData->FrameBufferSize <= (UINTN)(BiosVideoPrivate->VbeInformationBlock->TotalMemory * 64 * 1024));
1710
1711 BiosVideoPrivate->ModeData = ModeBuffer;
1712 }
1713 //
1714 // Check to see if we found any modes that are compatible with GRAPHICS OUTPUT
1715 //
1716 if (ModeNumber == 0) {
1717 Status = EFI_DEVICE_ERROR;
1718 goto Done;
1719 }
1720
1721 //
1722 // Assign Gop's Blt function
1723 //
1724 BiosVideoPrivate->GraphicsOutput.Blt = BiosVideoGraphicsOutputVbeBlt;
1725
1726 BiosVideoPrivate->GraphicsOutput.Mode->MaxMode = (UINT32) ModeNumber;
1727 //
1728 // Current mode is unknow till now, set it to an invalid mode.
1729 //
1730 BiosVideoPrivate->GraphicsOutput.Mode->Mode = GRAPHICS_OUTPUT_INVALIDE_MODE_NUMBER;
1731
1732 //
1733 // Find the best mode to initialize
1734 //
1735 if ((PcdGet32 (PcdVideoHorizontalResolution) == 0x0) || (PcdGet32 (PcdVideoVerticalResolution) == 0x0)) {
1736 DEBUG_CODE (
1737 BIOS_VIDEO_MODE_DATA *ModeData;
1738 ModeData = &BiosVideoPrivate->ModeData[HighestResolutionMode];
1739 DEBUG ((EFI_D_INFO, "BiosVideo set highest resolution %d x %d\n",
1740 ModeData->HorizontalResolution, ModeData->VerticalResolution));
1741 );
1742 PreferMode = HighestResolutionMode;
1743 }
1744 Status = BiosVideoGraphicsOutputSetMode (&BiosVideoPrivate->GraphicsOutput, (UINT32) PreferMode);
1745 if (EFI_ERROR (Status)) {
1746 for (PreferMode = 0; PreferMode < ModeNumber; PreferMode ++) {
1747 Status = BiosVideoGraphicsOutputSetMode (
1748 &BiosVideoPrivate->GraphicsOutput,
1749 (UINT32) PreferMode
1750 );
1751 if (!EFI_ERROR (Status)) {
1752 break;
1753 }
1754 }
1755 if (PreferMode == ModeNumber) {
1756 //
1757 // None mode is set successfully.
1758 //
1759 goto Done;
1760 }
1761 }
1762
1763 Done:
1764 //
1765 // If there was an error, then free the mode structure
1766 //
1767 if (EFI_ERROR (Status)) {
1768 if (BiosVideoPrivate->ModeData != NULL) {
1769 FreePool (BiosVideoPrivate->ModeData);
1770 BiosVideoPrivate->ModeData = NULL;
1771 BiosVideoPrivate->MaxMode = 0;
1772 }
1773 if (EdidOverrideDataBlock != NULL) {
1774 FreePool (EdidOverrideDataBlock);
1775 }
1776 }
1777
1778 return Status;
1779 }
1780
1781
1782 /**
1783 Check for VGA device.
1784
1785 @param BiosVideoPrivate Pointer to BIOS_VIDEO_DEV structure
1786
1787 @retval EFI_SUCCESS Standard VGA device found
1788
1789 **/
1790 EFI_STATUS
1791 BiosVideoCheckForVga (
1792 IN OUT BIOS_VIDEO_DEV *BiosVideoPrivate
1793 )
1794 {
1795 EFI_STATUS Status;
1796 BIOS_VIDEO_MODE_DATA *ModeBuffer;
1797
1798 Status = EFI_UNSUPPORTED;
1799
1800 //
1801 // Assign Gop's Blt function
1802 //
1803 BiosVideoPrivate->GraphicsOutput.Blt = BiosVideoGraphicsOutputVgaBlt;
1804
1805 //
1806 // Add mode to the list of available modes
1807 // caller should guarantee that Mode has been allocated.
1808 //
1809 ASSERT (BiosVideoPrivate->GraphicsOutput.Mode != NULL);
1810 BiosVideoPrivate->GraphicsOutput.Mode->MaxMode = 1;
1811
1812 ModeBuffer = (BIOS_VIDEO_MODE_DATA *) AllocatePool (
1813 sizeof (BIOS_VIDEO_MODE_DATA)
1814 );
1815 if (NULL == ModeBuffer) {
1816 Status = EFI_OUT_OF_RESOURCES;
1817 goto Done;
1818 }
1819
1820 ModeBuffer->VbeModeNumber = 0x0012;
1821 ModeBuffer->BytesPerScanLine = 640;
1822 ModeBuffer->LinearFrameBuffer = (VOID *) (UINTN) (0xa0000);
1823 ModeBuffer->HorizontalResolution = 640;
1824 ModeBuffer->VerticalResolution = 480;
1825 ModeBuffer->PixelFormat = PixelBltOnly;
1826 ModeBuffer->BitsPerPixel = 8;
1827 ModeBuffer->ColorDepth = 32;
1828 ModeBuffer->RefreshRate = 60;
1829
1830 BiosVideoPrivate->ModeData = ModeBuffer;
1831
1832 //
1833 // Test to see if the Video Adapter support the 640x480 16 color mode
1834 //
1835 BiosVideoPrivate->GraphicsOutput.Mode->Mode = GRAPHICS_OUTPUT_INVALIDE_MODE_NUMBER;
1836 Status = BiosVideoGraphicsOutputSetMode (&BiosVideoPrivate->GraphicsOutput, 0);
1837
1838 Done:
1839 //
1840 // If there was an error, then free the mode structure
1841 //
1842 if (EFI_ERROR (Status)) {
1843 if (BiosVideoPrivate->ModeData != NULL) {
1844 FreePool (BiosVideoPrivate->ModeData);
1845 BiosVideoPrivate->ModeData = NULL;
1846 }
1847 if (BiosVideoPrivate->GraphicsOutput.Mode != NULL) {
1848 if (BiosVideoPrivate->GraphicsOutput.Mode->Info != NULL) {
1849 FreePool (BiosVideoPrivate->GraphicsOutput.Mode->Info);
1850 BiosVideoPrivate->GraphicsOutput.Mode->Info = NULL;
1851 }
1852 FreePool (BiosVideoPrivate->GraphicsOutput.Mode);
1853 BiosVideoPrivate->GraphicsOutput.Mode = NULL;
1854 }
1855 }
1856 return Status;
1857 }
1858
1859 //
1860 // Graphics Output Protocol Member Functions for VESA BIOS Extensions
1861 //
1862
1863 /**
1864 Graphics Output protocol interface to get video mode.
1865
1866 @param This Protocol instance pointer.
1867 @param ModeNumber The mode number to return information on.
1868 @param SizeOfInfo A pointer to the size, in bytes, of the Info
1869 buffer.
1870 @param Info Caller allocated buffer that returns information
1871 about ModeNumber.
1872
1873 @retval EFI_SUCCESS Mode information returned.
1874 @retval EFI_DEVICE_ERROR A hardware error occurred trying to retrieve the
1875 video mode.
1876 @retval EFI_NOT_STARTED Video display is not initialized. Call SetMode ()
1877 @retval EFI_INVALID_PARAMETER One of the input args was NULL.
1878
1879 **/
1880 EFI_STATUS
1881 EFIAPI
1882 BiosVideoGraphicsOutputQueryMode (
1883 IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
1884 IN UINT32 ModeNumber,
1885 OUT UINTN *SizeOfInfo,
1886 OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION **Info
1887 )
1888 {
1889 BIOS_VIDEO_DEV *BiosVideoPrivate;
1890 BIOS_VIDEO_MODE_DATA *ModeData;
1891
1892 BiosVideoPrivate = BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (This);
1893
1894 if (BiosVideoPrivate->HardwareNeedsStarting) {
1895 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1896 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1897 EFI_PERIPHERAL_LOCAL_CONSOLE | EFI_P_EC_OUTPUT_ERROR,
1898 BiosVideoPrivate->GopDevicePath
1899 );
1900 return EFI_NOT_STARTED;
1901 }
1902
1903 if (This == NULL || Info == NULL || SizeOfInfo == NULL || ModeNumber >= This->Mode->MaxMode) {
1904 return EFI_INVALID_PARAMETER;
1905 }
1906
1907 *Info = (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *) AllocatePool (
1908 sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION)
1909 );
1910 if (NULL == *Info) {
1911 return EFI_OUT_OF_RESOURCES;
1912 }
1913
1914 *SizeOfInfo = sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION);
1915
1916 ModeData = &BiosVideoPrivate->ModeData[ModeNumber];
1917 (*Info)->Version = 0;
1918 (*Info)->HorizontalResolution = ModeData->HorizontalResolution;
1919 (*Info)->VerticalResolution = ModeData->VerticalResolution;
1920 (*Info)->PixelFormat = ModeData->PixelFormat;
1921 CopyMem (&((*Info)->PixelInformation), &(ModeData->PixelBitMask), sizeof(ModeData->PixelBitMask));
1922
1923 (*Info)->PixelsPerScanLine = (ModeData->BytesPerScanLine * 8) / ModeData->BitsPerPixel;
1924
1925 return EFI_SUCCESS;
1926 }
1927
1928 /**
1929 Worker function to set video mode.
1930
1931 @param BiosVideoPrivate Instance of BIOS_VIDEO_DEV.
1932 @param ModeData The mode data to be set.
1933 @param DevicePath Pointer to Device Path Protocol.
1934
1935 @retval EFI_SUCCESS Graphics mode was changed.
1936 @retval EFI_DEVICE_ERROR The device had an error and could not complete the
1937 request.
1938 @retval EFI_UNSUPPORTED ModeNumber is not supported by this device.
1939
1940 **/
1941 EFI_STATUS
1942 BiosVideoSetModeWorker (
1943 IN BIOS_VIDEO_DEV *BiosVideoPrivate,
1944 IN BIOS_VIDEO_MODE_DATA *ModeData,
1945 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
1946 )
1947 {
1948 EFI_STATUS Status;
1949 EFI_IA32_REGISTER_SET Regs;
1950
1951 if (BiosVideoPrivate->LineBuffer != NULL) {
1952 FreePool (BiosVideoPrivate->LineBuffer);
1953 }
1954
1955 if (BiosVideoPrivate->VgaFrameBuffer != NULL) {
1956 FreePool (BiosVideoPrivate->VgaFrameBuffer);
1957 }
1958
1959 if (BiosVideoPrivate->VbeFrameBuffer != NULL) {
1960 FreePool (BiosVideoPrivate->VbeFrameBuffer);
1961 }
1962
1963 BiosVideoPrivate->LineBuffer = (UINT8 *) AllocatePool (
1964 ModeData->BytesPerScanLine
1965 );
1966 if (NULL == BiosVideoPrivate->LineBuffer) {
1967 return EFI_OUT_OF_RESOURCES;
1968 }
1969 //
1970 // Clear all registers
1971 //
1972 ZeroMem (&Regs, sizeof (Regs));
1973
1974 if (ModeData->VbeModeNumber < 0x100) {
1975 //
1976 // Allocate a working buffer for BLT operations to the VGA frame buffer
1977 //
1978 BiosVideoPrivate->VgaFrameBuffer = (UINT8 *) AllocatePool (4 * 480 * 80);
1979 if (NULL == BiosVideoPrivate->VgaFrameBuffer) {
1980 return EFI_OUT_OF_RESOURCES;
1981 }
1982 //
1983 // Set VGA Mode
1984 //
1985 Regs.X.AX = ModeData->VbeModeNumber;
1986 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
1987
1988 } else {
1989 //
1990 // Allocate a working buffer for BLT operations to the VBE frame buffer
1991 //
1992 BiosVideoPrivate->VbeFrameBuffer =
1993 (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) AllocatePool (
1994 ModeData->BytesPerScanLine * ModeData->VerticalResolution
1995 );
1996 if (NULL == BiosVideoPrivate->VbeFrameBuffer) {
1997 return EFI_OUT_OF_RESOURCES;
1998 }
1999 //
2000 // Set VBE mode
2001 //
2002 Regs.X.AX = VESA_BIOS_EXTENSIONS_SET_MODE;
2003 Regs.X.BX = (UINT16) (ModeData->VbeModeNumber | VESA_BIOS_EXTENSIONS_MODE_NUMBER_LINEAR_FRAME_BUFFER);
2004 ZeroMem (BiosVideoPrivate->VbeCrtcInformationBlock, sizeof (VESA_BIOS_EXTENSIONS_CRTC_INFORMATION_BLOCK));
2005 Regs.X.ES = EFI_SEGMENT ((UINTN) BiosVideoPrivate->VbeCrtcInformationBlock);
2006 Regs.X.DI = EFI_OFFSET ((UINTN) BiosVideoPrivate->VbeCrtcInformationBlock);
2007 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
2008
2009 //
2010 // Check to see if the call succeeded
2011 //
2012 if (Regs.X.AX != VESA_BIOS_EXTENSIONS_STATUS_SUCCESS) {
2013 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
2014 EFI_ERROR_CODE | EFI_ERROR_MINOR,
2015 EFI_PERIPHERAL_LOCAL_CONSOLE | EFI_P_EC_OUTPUT_ERROR,
2016 DevicePath
2017 );
2018 return EFI_DEVICE_ERROR;
2019 }
2020 //
2021 // Initialize the state of the VbeFrameBuffer
2022 //
2023 Status = BiosVideoPrivate->PciIo->Mem.Read (
2024 BiosVideoPrivate->PciIo,
2025 EfiPciIoWidthUint32,
2026 EFI_PCI_IO_PASS_THROUGH_BAR,
2027 (UINT64) (UINTN) ModeData->LinearFrameBuffer,
2028 (ModeData->BytesPerScanLine * ModeData->VerticalResolution) >> 2,
2029 BiosVideoPrivate->VbeFrameBuffer
2030 );
2031 if (EFI_ERROR (Status)) {
2032 return Status;
2033 }
2034 }
2035
2036 return EFI_SUCCESS;
2037 }
2038
2039 /**
2040 Graphics Output protocol interface to set video mode.
2041
2042 @param This Protocol instance pointer.
2043 @param ModeNumber The mode number to be set.
2044
2045 @retval EFI_SUCCESS Graphics mode was changed.
2046 @retval EFI_DEVICE_ERROR The device had an error and could not complete the
2047 request.
2048 @retval EFI_UNSUPPORTED ModeNumber is not supported by this device.
2049
2050 **/
2051 EFI_STATUS
2052 EFIAPI
2053 BiosVideoGraphicsOutputSetMode (
2054 IN EFI_GRAPHICS_OUTPUT_PROTOCOL * This,
2055 IN UINT32 ModeNumber
2056 )
2057 {
2058 EFI_STATUS Status;
2059 BIOS_VIDEO_DEV *BiosVideoPrivate;
2060 BIOS_VIDEO_MODE_DATA *ModeData;
2061 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Background;
2062
2063 if (This == NULL) {
2064 return EFI_INVALID_PARAMETER;
2065 }
2066
2067 BiosVideoPrivate = BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (This);
2068
2069 ModeData = &BiosVideoPrivate->ModeData[ModeNumber];
2070
2071 if (ModeNumber >= This->Mode->MaxMode) {
2072 return EFI_UNSUPPORTED;
2073 }
2074
2075 if (ModeNumber == This->Mode->Mode) {
2076 //
2077 // Clear screen to black
2078 //
2079 ZeroMem (&Background, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
2080 BiosVideoGraphicsOutputVbeBlt (
2081 This,
2082 &Background,
2083 EfiBltVideoFill,
2084 0,
2085 0,
2086 0,
2087 0,
2088 ModeData->HorizontalResolution,
2089 ModeData->VerticalResolution,
2090 0
2091 );
2092 return EFI_SUCCESS;
2093 }
2094
2095 Status = BiosVideoSetModeWorker (BiosVideoPrivate, ModeData, BiosVideoPrivate->GopDevicePath);
2096 if (EFI_ERROR (Status)) {
2097 return Status;
2098 }
2099
2100 This->Mode->Mode = ModeNumber;
2101 This->Mode->Info->Version = 0;
2102 This->Mode->Info->HorizontalResolution = ModeData->HorizontalResolution;
2103 This->Mode->Info->VerticalResolution = ModeData->VerticalResolution;
2104 This->Mode->Info->PixelFormat = ModeData->PixelFormat;
2105 CopyMem (&(This->Mode->Info->PixelInformation), &(ModeData->PixelBitMask), sizeof (ModeData->PixelBitMask));
2106 This->Mode->Info->PixelsPerScanLine = (ModeData->BytesPerScanLine * 8) / ModeData->BitsPerPixel;
2107 This->Mode->SizeOfInfo = sizeof(EFI_GRAPHICS_OUTPUT_MODE_INFORMATION);
2108 This->Mode->FrameBufferSize = ModeData->FrameBufferSize;
2109 This->Mode->FrameBufferBase = (EFI_PHYSICAL_ADDRESS) (UINTN) ModeData->LinearFrameBuffer;
2110
2111 BiosVideoPrivate->HardwareNeedsStarting = FALSE;
2112
2113 return EFI_SUCCESS;
2114 }
2115
2116 /**
2117 Update physical frame buffer, copy 4 bytes block, then copy remaining bytes.
2118
2119 @param PciIo The pointer of EFI_PCI_IO_PROTOCOL
2120 @param VbeBuffer The data to transfer to screen
2121 @param MemAddress Physical frame buffer base address
2122 @param DestinationX The X coordinate of the destination for BltOperation
2123 @param DestinationY The Y coordinate of the destination for BltOperation
2124 @param TotalBytes The total bytes of copy
2125 @param VbePixelWidth Bytes per pixel
2126 @param BytesPerScanLine Bytes per scan line
2127
2128 **/
2129 VOID
2130 CopyVideoBuffer (
2131 IN EFI_PCI_IO_PROTOCOL *PciIo,
2132 IN UINT8 *VbeBuffer,
2133 IN VOID *MemAddress,
2134 IN UINTN DestinationX,
2135 IN UINTN DestinationY,
2136 IN UINTN TotalBytes,
2137 IN UINT32 VbePixelWidth,
2138 IN UINTN BytesPerScanLine
2139 )
2140 {
2141 UINTN FrameBufferAddr;
2142 UINTN CopyBlockNum;
2143 UINTN RemainingBytes;
2144 UINTN UnalignedBytes;
2145 EFI_STATUS Status;
2146
2147 FrameBufferAddr = (UINTN) MemAddress + (DestinationY * BytesPerScanLine) + DestinationX * VbePixelWidth;
2148
2149 //
2150 // If TotalBytes is less than 4 bytes, only start byte copy.
2151 //
2152 if (TotalBytes < 4) {
2153 Status = PciIo->Mem.Write (
2154 PciIo,
2155 EfiPciIoWidthUint8,
2156 EFI_PCI_IO_PASS_THROUGH_BAR,
2157 (UINT64) FrameBufferAddr,
2158 TotalBytes,
2159 VbeBuffer
2160 );
2161 ASSERT_EFI_ERROR (Status);
2162 return;
2163 }
2164
2165 //
2166 // If VbeBuffer is not 4-byte aligned, start byte copy.
2167 //
2168 UnalignedBytes = (4 - ((UINTN) VbeBuffer & 0x3)) & 0x3;
2169
2170 if (UnalignedBytes != 0) {
2171 Status = PciIo->Mem.Write (
2172 PciIo,
2173 EfiPciIoWidthUint8,
2174 EFI_PCI_IO_PASS_THROUGH_BAR,
2175 (UINT64) FrameBufferAddr,
2176 UnalignedBytes,
2177 VbeBuffer
2178 );
2179 ASSERT_EFI_ERROR (Status);
2180 FrameBufferAddr += UnalignedBytes;
2181 VbeBuffer += UnalignedBytes;
2182 }
2183
2184 //
2185 // Calculate 4-byte block count and remaining bytes.
2186 //
2187 CopyBlockNum = (TotalBytes - UnalignedBytes) >> 2;
2188 RemainingBytes = (TotalBytes - UnalignedBytes) & 3;
2189
2190 //
2191 // Copy 4-byte block and remaining bytes to physical frame buffer.
2192 //
2193 if (CopyBlockNum != 0) {
2194 Status = PciIo->Mem.Write (
2195 PciIo,
2196 EfiPciIoWidthUint32,
2197 EFI_PCI_IO_PASS_THROUGH_BAR,
2198 (UINT64) FrameBufferAddr,
2199 CopyBlockNum,
2200 VbeBuffer
2201 );
2202 ASSERT_EFI_ERROR (Status);
2203 }
2204
2205 if (RemainingBytes != 0) {
2206 FrameBufferAddr += (CopyBlockNum << 2);
2207 VbeBuffer += (CopyBlockNum << 2);
2208 Status = PciIo->Mem.Write (
2209 PciIo,
2210 EfiPciIoWidthUint8,
2211 EFI_PCI_IO_PASS_THROUGH_BAR,
2212 (UINT64) FrameBufferAddr,
2213 RemainingBytes,
2214 VbeBuffer
2215 );
2216 ASSERT_EFI_ERROR (Status);
2217 }
2218 }
2219
2220 /**
2221 Worker function to block transfer for VBE device.
2222
2223 @param BiosVideoPrivate Instance of BIOS_VIDEO_DEV
2224 @param BltBuffer The data to transfer to screen
2225 @param BltOperation The operation to perform
2226 @param SourceX The X coordinate of the source for BltOperation
2227 @param SourceY The Y coordinate of the source for BltOperation
2228 @param DestinationX The X coordinate of the destination for
2229 BltOperation
2230 @param DestinationY The Y coordinate of the destination for
2231 BltOperation
2232 @param Width The width of a rectangle in the blt rectangle in
2233 pixels
2234 @param Height The height of a rectangle in the blt rectangle in
2235 pixels
2236 @param Delta Not used for EfiBltVideoFill and
2237 EfiBltVideoToVideo operation. If a Delta of 0 is
2238 used, the entire BltBuffer will be operated on. If
2239 a subrectangle of the BltBuffer is used, then
2240 Delta represents the number of bytes in a row of
2241 the BltBuffer.
2242 @param Mode Mode data.
2243
2244 @retval EFI_INVALID_PARAMETER Invalid parameter passed in
2245 @retval EFI_SUCCESS Blt operation success
2246
2247 **/
2248 EFI_STATUS
2249 BiosVideoVbeBltWorker (
2250 IN BIOS_VIDEO_DEV *BiosVideoPrivate,
2251 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer, OPTIONAL
2252 IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation,
2253 IN UINTN SourceX,
2254 IN UINTN SourceY,
2255 IN UINTN DestinationX,
2256 IN UINTN DestinationY,
2257 IN UINTN Width,
2258 IN UINTN Height,
2259 IN UINTN Delta,
2260 IN BIOS_VIDEO_MODE_DATA *Mode
2261 )
2262 {
2263 EFI_PCI_IO_PROTOCOL *PciIo;
2264 EFI_TPL OriginalTPL;
2265 UINTN DstY;
2266 UINTN SrcY;
2267 UINTN DstX;
2268 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;
2269 VOID *MemAddress;
2270 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *VbeFrameBuffer;
2271 UINTN BytesPerScanLine;
2272 UINTN Index;
2273 UINT8 *VbeBuffer;
2274 UINT8 *VbeBuffer1;
2275 UINT8 *BltUint8;
2276 UINT32 VbePixelWidth;
2277 UINT32 Pixel;
2278 UINTN TotalBytes;
2279
2280 PciIo = BiosVideoPrivate->PciIo;
2281
2282 VbeFrameBuffer = BiosVideoPrivate->VbeFrameBuffer;
2283 MemAddress = Mode->LinearFrameBuffer;
2284 BytesPerScanLine = Mode->BytesPerScanLine;
2285 VbePixelWidth = Mode->BitsPerPixel / 8;
2286 BltUint8 = (UINT8 *) BltBuffer;
2287 TotalBytes = Width * VbePixelWidth;
2288
2289 if (((UINTN) BltOperation) >= EfiGraphicsOutputBltOperationMax) {
2290 return EFI_INVALID_PARAMETER;
2291 }
2292
2293 if (Width == 0 || Height == 0) {
2294 return EFI_INVALID_PARAMETER;
2295 }
2296 //
2297 // We need to fill the Virtual Screen buffer with the blt data.
2298 // The virtual screen is upside down, as the first row is the bootom row of
2299 // the image.
2300 //
2301 if (BltOperation == EfiBltVideoToBltBuffer) {
2302 //
2303 // Video to BltBuffer: Source is Video, destination is BltBuffer
2304 //
2305 if (SourceY + Height > Mode->VerticalResolution) {
2306 return EFI_INVALID_PARAMETER;
2307 }
2308
2309 if (SourceX + Width > Mode->HorizontalResolution) {
2310 return EFI_INVALID_PARAMETER;
2311 }
2312 } else {
2313 //
2314 // BltBuffer to Video: Source is BltBuffer, destination is Video
2315 //
2316 if (DestinationY + Height > Mode->VerticalResolution) {
2317 return EFI_INVALID_PARAMETER;
2318 }
2319
2320 if (DestinationX + Width > Mode->HorizontalResolution) {
2321 return EFI_INVALID_PARAMETER;
2322 }
2323 }
2324 //
2325 // If Delta is zero, then the entire BltBuffer is being used, so Delta
2326 // is the number of bytes in each row of BltBuffer. Since BltBuffer is Width pixels size,
2327 // the number of bytes in each row can be computed.
2328 //
2329 if (Delta == 0) {
2330 Delta = Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
2331 }
2332 //
2333 // We have to raise to TPL Notify, so we make an atomic write the frame buffer.
2334 // We would not want a timer based event (Cursor, ...) to come in while we are
2335 // doing this operation.
2336 //
2337 OriginalTPL = gBS->RaiseTPL (TPL_NOTIFY);
2338
2339 switch (BltOperation) {
2340 case EfiBltVideoToBltBuffer:
2341 for (SrcY = SourceY, DstY = DestinationY; DstY < (Height + DestinationY); SrcY++, DstY++) {
2342 Blt = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) (BltUint8 + DstY * Delta + DestinationX * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
2343 //
2344 // Shuffle the packed bytes in the hardware buffer to match EFI_GRAPHICS_OUTPUT_BLT_PIXEL
2345 //
2346 VbeBuffer = ((UINT8 *) VbeFrameBuffer + (SrcY * BytesPerScanLine + SourceX * VbePixelWidth));
2347 for (DstX = DestinationX; DstX < (Width + DestinationX); DstX++) {
2348 Pixel = VbeBuffer[0] | VbeBuffer[1] << 8 | VbeBuffer[2] << 16 | VbeBuffer[3] << 24;
2349 Blt->Red = (UINT8) ((Pixel >> Mode->Red.Position) & Mode->Red.Mask);
2350 Blt->Blue = (UINT8) ((Pixel >> Mode->Blue.Position) & Mode->Blue.Mask);
2351 Blt->Green = (UINT8) ((Pixel >> Mode->Green.Position) & Mode->Green.Mask);
2352 Blt->Reserved = 0;
2353 Blt++;
2354 VbeBuffer += VbePixelWidth;
2355 }
2356
2357 }
2358 break;
2359
2360 case EfiBltVideoToVideo:
2361 for (Index = 0; Index < Height; Index++) {
2362 if (DestinationY <= SourceY) {
2363 SrcY = SourceY + Index;
2364 DstY = DestinationY + Index;
2365 } else {
2366 SrcY = SourceY + Height - Index - 1;
2367 DstY = DestinationY + Height - Index - 1;
2368 }
2369
2370 VbeBuffer = ((UINT8 *) VbeFrameBuffer + DstY * BytesPerScanLine + DestinationX * VbePixelWidth);
2371 VbeBuffer1 = ((UINT8 *) VbeFrameBuffer + SrcY * BytesPerScanLine + SourceX * VbePixelWidth);
2372
2373 gBS->CopyMem (
2374 VbeBuffer,
2375 VbeBuffer1,
2376 TotalBytes
2377 );
2378
2379 //
2380 // Update physical frame buffer.
2381 //
2382 CopyVideoBuffer (
2383 PciIo,
2384 VbeBuffer,
2385 MemAddress,
2386 DestinationX,
2387 DstY,
2388 TotalBytes,
2389 VbePixelWidth,
2390 BytesPerScanLine
2391 );
2392 }
2393 break;
2394
2395 case EfiBltVideoFill:
2396 VbeBuffer = (UINT8 *) ((UINTN) VbeFrameBuffer + (DestinationY * BytesPerScanLine) + DestinationX * VbePixelWidth);
2397 Blt = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) BltUint8;
2398 //
2399 // Shuffle the RGB fields in EFI_GRAPHICS_OUTPUT_BLT_PIXEL to match the hardware buffer
2400 //
2401 Pixel = ((Blt->Red & Mode->Red.Mask) << Mode->Red.Position) |
2402 (
2403 (Blt->Green & Mode->Green.Mask) <<
2404 Mode->Green.Position
2405 ) |
2406 ((Blt->Blue & Mode->Blue.Mask) << Mode->Blue.Position);
2407
2408 for (Index = 0; Index < Width; Index++) {
2409 gBS->CopyMem (
2410 VbeBuffer,
2411 &Pixel,
2412 VbePixelWidth
2413 );
2414 VbeBuffer += VbePixelWidth;
2415 }
2416
2417 VbeBuffer = (UINT8 *) ((UINTN) VbeFrameBuffer + (DestinationY * BytesPerScanLine) + DestinationX * VbePixelWidth);
2418 for (DstY = DestinationY + 1; DstY < (Height + DestinationY); DstY++) {
2419 gBS->CopyMem (
2420 (VOID *) ((UINTN) VbeFrameBuffer + (DstY * BytesPerScanLine) + DestinationX * VbePixelWidth),
2421 VbeBuffer,
2422 TotalBytes
2423 );
2424 }
2425
2426 for (DstY = DestinationY; DstY < (Height + DestinationY); DstY++) {
2427 //
2428 // Update physical frame buffer.
2429 //
2430 CopyVideoBuffer (
2431 PciIo,
2432 VbeBuffer,
2433 MemAddress,
2434 DestinationX,
2435 DstY,
2436 TotalBytes,
2437 VbePixelWidth,
2438 BytesPerScanLine
2439 );
2440 }
2441 break;
2442
2443 case EfiBltBufferToVideo:
2444 for (SrcY = SourceY, DstY = DestinationY; SrcY < (Height + SourceY); SrcY++, DstY++) {
2445 Blt = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) (BltUint8 + (SrcY * Delta) + (SourceX) * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
2446 VbeBuffer = ((UINT8 *) VbeFrameBuffer + (DstY * BytesPerScanLine + DestinationX * VbePixelWidth));
2447 for (DstX = DestinationX; DstX < (Width + DestinationX); DstX++) {
2448 //
2449 // Shuffle the RGB fields in EFI_GRAPHICS_OUTPUT_BLT_PIXEL to match the hardware buffer
2450 //
2451 Pixel = ((Blt->Red & Mode->Red.Mask) << Mode->Red.Position) |
2452 ((Blt->Green & Mode->Green.Mask) << Mode->Green.Position) |
2453 ((Blt->Blue & Mode->Blue.Mask) << Mode->Blue.Position);
2454 gBS->CopyMem (
2455 VbeBuffer,
2456 &Pixel,
2457 VbePixelWidth
2458 );
2459 Blt++;
2460 VbeBuffer += VbePixelWidth;
2461 }
2462
2463 VbeBuffer = ((UINT8 *) VbeFrameBuffer + (DstY * BytesPerScanLine + DestinationX * VbePixelWidth));
2464
2465 //
2466 // Update physical frame buffer.
2467 //
2468 CopyVideoBuffer (
2469 PciIo,
2470 VbeBuffer,
2471 MemAddress,
2472 DestinationX,
2473 DstY,
2474 TotalBytes,
2475 VbePixelWidth,
2476 BytesPerScanLine
2477 );
2478 }
2479 break;
2480
2481 default: ;
2482 }
2483
2484 gBS->RestoreTPL (OriginalTPL);
2485
2486 return EFI_SUCCESS;
2487 }
2488
2489 /**
2490 Graphics Output protocol instance to block transfer for VBE device.
2491
2492 @param This Pointer to Graphics Output protocol instance
2493 @param BltBuffer The data to transfer to screen
2494 @param BltOperation The operation to perform
2495 @param SourceX The X coordinate of the source for BltOperation
2496 @param SourceY The Y coordinate of the source for BltOperation
2497 @param DestinationX The X coordinate of the destination for
2498 BltOperation
2499 @param DestinationY The Y coordinate of the destination for
2500 BltOperation
2501 @param Width The width of a rectangle in the blt rectangle in
2502 pixels
2503 @param Height The height of a rectangle in the blt rectangle in
2504 pixels
2505 @param Delta Not used for EfiBltVideoFill and
2506 EfiBltVideoToVideo operation. If a Delta of 0 is
2507 used, the entire BltBuffer will be operated on. If
2508 a subrectangle of the BltBuffer is used, then
2509 Delta represents the number of bytes in a row of
2510 the BltBuffer.
2511
2512 @retval EFI_INVALID_PARAMETER Invalid parameter passed in
2513 @retval EFI_SUCCESS Blt operation success
2514
2515 **/
2516 EFI_STATUS
2517 EFIAPI
2518 BiosVideoGraphicsOutputVbeBlt (
2519 IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
2520 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer, OPTIONAL
2521 IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation,
2522 IN UINTN SourceX,
2523 IN UINTN SourceY,
2524 IN UINTN DestinationX,
2525 IN UINTN DestinationY,
2526 IN UINTN Width,
2527 IN UINTN Height,
2528 IN UINTN Delta
2529 )
2530 {
2531 BIOS_VIDEO_DEV *BiosVideoPrivate;
2532 BIOS_VIDEO_MODE_DATA *Mode;
2533
2534 if (This == NULL) {
2535 return EFI_INVALID_PARAMETER;
2536 }
2537
2538 BiosVideoPrivate = BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (This);
2539 Mode = &BiosVideoPrivate->ModeData[This->Mode->Mode];
2540
2541 return BiosVideoVbeBltWorker (
2542 BiosVideoPrivate,
2543 BltBuffer,
2544 BltOperation,
2545 SourceX,
2546 SourceY,
2547 DestinationX,
2548 DestinationY,
2549 Width,
2550 Height,
2551 Delta,
2552 Mode
2553 );
2554 }
2555
2556 /**
2557 Write graphics controller registers.
2558
2559 @param PciIo Pointer to PciIo protocol instance of the
2560 controller
2561 @param Address Register address
2562 @param Data Data to be written to register
2563
2564 @return None
2565
2566 **/
2567 VOID
2568 WriteGraphicsController (
2569 IN EFI_PCI_IO_PROTOCOL *PciIo,
2570 IN UINTN Address,
2571 IN UINTN Data
2572 )
2573 {
2574 Address = Address | (Data << 8);
2575 PciIo->Io.Write (
2576 PciIo,
2577 EfiPciIoWidthUint16,
2578 EFI_PCI_IO_PASS_THROUGH_BAR,
2579 VGA_GRAPHICS_CONTROLLER_ADDRESS_REGISTER,
2580 1,
2581 &Address
2582 );
2583 }
2584
2585
2586 /**
2587 Read the four bit plane of VGA frame buffer.
2588
2589 @param PciIo Pointer to PciIo protocol instance of the
2590 controller
2591 @param HardwareBuffer Hardware VGA frame buffer address
2592 @param MemoryBuffer Memory buffer address
2593 @param WidthInBytes Number of bytes in a line to read
2594 @param Height Height of the area to read
2595
2596 @return None
2597
2598 **/
2599 VOID
2600 VgaReadBitPlanes (
2601 EFI_PCI_IO_PROTOCOL *PciIo,
2602 UINT8 *HardwareBuffer,
2603 UINT8 *MemoryBuffer,
2604 UINTN WidthInBytes,
2605 UINTN Height
2606 )
2607 {
2608 UINTN BitPlane;
2609 UINTN Rows;
2610 UINTN FrameBufferOffset;
2611 UINT8 *Source;
2612 UINT8 *Destination;
2613
2614 //
2615 // Program the Mode Register Write mode 0, Read mode 0
2616 //
2617 WriteGraphicsController (
2618 PciIo,
2619 VGA_GRAPHICS_CONTROLLER_MODE_REGISTER,
2620 VGA_GRAPHICS_CONTROLLER_READ_MODE_0 | VGA_GRAPHICS_CONTROLLER_WRITE_MODE_0
2621 );
2622
2623 for (BitPlane = 0, FrameBufferOffset = 0;
2624 BitPlane < VGA_NUMBER_OF_BIT_PLANES;
2625 BitPlane++, FrameBufferOffset += VGA_BYTES_PER_BIT_PLANE
2626 ) {
2627 //
2628 // Program the Read Map Select Register to select the correct bit plane
2629 //
2630 WriteGraphicsController (
2631 PciIo,
2632 VGA_GRAPHICS_CONTROLLER_READ_MAP_SELECT_REGISTER,
2633 BitPlane
2634 );
2635
2636 Source = HardwareBuffer;
2637 Destination = MemoryBuffer + FrameBufferOffset;
2638
2639 for (Rows = 0; Rows < Height; Rows++, Source += VGA_BYTES_PER_SCAN_LINE, Destination += VGA_BYTES_PER_SCAN_LINE) {
2640 PciIo->Mem.Read (
2641 PciIo,
2642 EfiPciIoWidthUint8,
2643 EFI_PCI_IO_PASS_THROUGH_BAR,
2644 (UINT64) (UINTN) Source,
2645 WidthInBytes,
2646 (VOID *) Destination
2647 );
2648 }
2649 }
2650 }
2651
2652
2653 /**
2654 Internal routine to convert VGA color to Grahpics Output color.
2655
2656 @param MemoryBuffer Buffer containing VGA color
2657 @param CoordinateX The X coordinate of pixel on screen
2658 @param CoordinateY The Y coordinate of pixel on screen
2659 @param BltBuffer Buffer to contain converted Grahpics Output color
2660
2661 @return None
2662
2663 **/
2664 VOID
2665 VgaConvertToGraphicsOutputColor (
2666 UINT8 *MemoryBuffer,
2667 UINTN CoordinateX,
2668 UINTN CoordinateY,
2669 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer
2670 )
2671 {
2672 UINTN Mask;
2673 UINTN Bit;
2674 UINTN Color;
2675
2676 MemoryBuffer += ((CoordinateY << 6) + (CoordinateY << 4) + (CoordinateX >> 3));
2677 Mask = mVgaBitMaskTable[CoordinateX & 0x07];
2678 for (Bit = 0x01, Color = 0; Bit < 0x10; Bit <<= 1, MemoryBuffer += VGA_BYTES_PER_BIT_PLANE) {
2679 if ((*MemoryBuffer & Mask) != 0) {
2680 Color |= Bit;
2681 }
2682 }
2683
2684 *BltBuffer = mVgaColorToGraphicsOutputColor[Color];
2685 }
2686
2687 /**
2688 Internal routine to convert Grahpics Output color to VGA color.
2689
2690 @param BltBuffer buffer containing Grahpics Output color
2691
2692 @return Converted VGA color
2693
2694 **/
2695 UINT8
2696 VgaConvertColor (
2697 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer
2698 )
2699 {
2700 UINT8 Color;
2701
2702 Color = (UINT8) ((BltBuffer->Blue >> 7) | ((BltBuffer->Green >> 6) & 0x02) | ((BltBuffer->Red >> 5) & 0x04));
2703 if ((BltBuffer->Red + BltBuffer->Green + BltBuffer->Blue) > 0x180) {
2704 Color |= 0x08;
2705 }
2706
2707 return Color;
2708 }
2709
2710
2711 /**
2712 Grahpics Output protocol instance to block transfer for VGA device.
2713
2714 @param This Pointer to Grahpics Output protocol instance
2715 @param BltBuffer The data to transfer to screen
2716 @param BltOperation The operation to perform
2717 @param SourceX The X coordinate of the source for BltOperation
2718 @param SourceY The Y coordinate of the source for BltOperation
2719 @param DestinationX The X coordinate of the destination for
2720 BltOperation
2721 @param DestinationY The Y coordinate of the destination for
2722 BltOperation
2723 @param Width The width of a rectangle in the blt rectangle in
2724 pixels
2725 @param Height The height of a rectangle in the blt rectangle in
2726 pixels
2727 @param Delta Not used for EfiBltVideoFill and
2728 EfiBltVideoToVideo operation. If a Delta of 0 is
2729 used, the entire BltBuffer will be operated on. If
2730 a subrectangle of the BltBuffer is used, then
2731 Delta represents the number of bytes in a row of
2732 the BltBuffer.
2733
2734 @retval EFI_INVALID_PARAMETER Invalid parameter passed in
2735 @retval EFI_SUCCESS Blt operation success
2736
2737 **/
2738 EFI_STATUS
2739 EFIAPI
2740 BiosVideoGraphicsOutputVgaBlt (
2741 IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
2742 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer, OPTIONAL
2743 IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation,
2744 IN UINTN SourceX,
2745 IN UINTN SourceY,
2746 IN UINTN DestinationX,
2747 IN UINTN DestinationY,
2748 IN UINTN Width,
2749 IN UINTN Height,
2750 IN UINTN Delta
2751 )
2752 {
2753 BIOS_VIDEO_DEV *BiosVideoPrivate;
2754 EFI_TPL OriginalTPL;
2755 UINT8 *MemAddress;
2756 UINTN BytesPerScanLine;
2757 UINTN Bit;
2758 UINTN Index;
2759 UINTN Index1;
2760 UINTN StartAddress;
2761 UINTN Bytes;
2762 UINTN Offset;
2763 UINT8 LeftMask;
2764 UINT8 RightMask;
2765 UINTN Address;
2766 UINTN AddressFix;
2767 UINT8 *Address1;
2768 UINT8 *SourceAddress;
2769 UINT8 *DestinationAddress;
2770 EFI_PCI_IO_PROTOCOL *PciIo;
2771 UINT8 Data;
2772 UINT8 PixelColor;
2773 UINT8 *VgaFrameBuffer;
2774 UINTN SourceOffset;
2775 UINTN SourceWidth;
2776 UINTN Rows;
2777 UINTN Columns;
2778 UINTN CoordinateX;
2779 UINTN CoordinateY;
2780 UINTN CurrentMode;
2781
2782 if (This == NULL || ((UINTN) BltOperation) >= EfiGraphicsOutputBltOperationMax) {
2783 return EFI_INVALID_PARAMETER;
2784 }
2785
2786 BiosVideoPrivate = BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (This);
2787
2788 CurrentMode = This->Mode->Mode;
2789 PciIo = BiosVideoPrivate->PciIo;
2790 MemAddress = BiosVideoPrivate->ModeData[CurrentMode].LinearFrameBuffer;
2791 BytesPerScanLine = BiosVideoPrivate->ModeData[CurrentMode].BytesPerScanLine >> 3;
2792 VgaFrameBuffer = BiosVideoPrivate->VgaFrameBuffer;
2793
2794
2795 if (Width == 0 || Height == 0) {
2796 return EFI_INVALID_PARAMETER;
2797 }
2798 //
2799 // We need to fill the Virtual Screen buffer with the blt data.
2800 // The virtual screen is upside down, as the first row is the bootom row of
2801 // the image.
2802 //
2803 if (BltOperation == EfiBltVideoToBltBuffer) {
2804 //
2805 // Video to BltBuffer: Source is Video, destination is BltBuffer
2806 //
2807 if (SourceY + Height > BiosVideoPrivate->ModeData[CurrentMode].VerticalResolution) {
2808 return EFI_INVALID_PARAMETER;
2809 }
2810
2811 if (SourceX + Width > BiosVideoPrivate->ModeData[CurrentMode].HorizontalResolution) {
2812 return EFI_INVALID_PARAMETER;
2813 }
2814 } else {
2815 //
2816 // BltBuffer to Video: Source is BltBuffer, destination is Video
2817 //
2818 if (DestinationY + Height > BiosVideoPrivate->ModeData[CurrentMode].VerticalResolution) {
2819 return EFI_INVALID_PARAMETER;
2820 }
2821
2822 if (DestinationX + Width > BiosVideoPrivate->ModeData[CurrentMode].HorizontalResolution) {
2823 return EFI_INVALID_PARAMETER;
2824 }
2825 }
2826 //
2827 // If Delta is zero, then the entire BltBuffer is being used, so Delta
2828 // is the number of bytes in each row of BltBuffer. Since BltBuffer is Width pixels size,
2829 // the number of bytes in each row can be computed.
2830 //
2831 if (Delta == 0) {
2832 Delta = Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
2833 }
2834 //
2835 // We have to raise to TPL Notify, so we make an atomic write the frame buffer.
2836 // We would not want a timer based event (Cursor, ...) to come in while we are
2837 // doing this operation.
2838 //
2839 OriginalTPL = gBS->RaiseTPL (TPL_NOTIFY);
2840
2841 //
2842 // Compute some values we need for VGA
2843 //
2844 switch (BltOperation) {
2845 case EfiBltVideoToBltBuffer:
2846
2847 SourceOffset = (SourceY << 6) + (SourceY << 4) + (SourceX >> 3);
2848 SourceWidth = ((SourceX + Width - 1) >> 3) - (SourceX >> 3) + 1;
2849
2850 //
2851 // Read all the pixels in the 4 bit planes into a memory buffer that looks like the VGA buffer
2852 //
2853 VgaReadBitPlanes (
2854 PciIo,
2855 MemAddress + SourceOffset,
2856 VgaFrameBuffer + SourceOffset,
2857 SourceWidth,
2858 Height
2859 );
2860
2861 //
2862 // Convert VGA Bit Planes to a Graphics Output 32-bit color value
2863 //
2864 BltBuffer += (DestinationY * (Delta >> 2) + DestinationX);
2865 for (Rows = 0, CoordinateY = SourceY; Rows < Height; Rows++, CoordinateY++, BltBuffer += (Delta >> 2)) {
2866 for (Columns = 0, CoordinateX = SourceX; Columns < Width; Columns++, CoordinateX++, BltBuffer++) {
2867 VgaConvertToGraphicsOutputColor (VgaFrameBuffer, CoordinateX, CoordinateY, BltBuffer);
2868 }
2869
2870 BltBuffer -= Width;
2871 }
2872
2873 break;
2874
2875 case EfiBltVideoToVideo:
2876 //
2877 // Check for an aligned Video to Video operation
2878 //
2879 if ((SourceX & 0x07) == 0x00 && (DestinationX & 0x07) == 0x00 && (Width & 0x07) == 0x00) {
2880 //
2881 // Program the Mode Register Write mode 1, Read mode 0
2882 //
2883 WriteGraphicsController (
2884 PciIo,
2885 VGA_GRAPHICS_CONTROLLER_MODE_REGISTER,
2886 VGA_GRAPHICS_CONTROLLER_READ_MODE_0 | VGA_GRAPHICS_CONTROLLER_WRITE_MODE_1
2887 );
2888
2889 SourceAddress = (UINT8 *) (MemAddress + (SourceY << 6) + (SourceY << 4) + (SourceX >> 3));
2890 DestinationAddress = (UINT8 *) (MemAddress + (DestinationY << 6) + (DestinationY << 4) + (DestinationX >> 3));
2891 Bytes = Width >> 3;
2892 for (Index = 0, Offset = 0; Index < Height; Index++, Offset += BytesPerScanLine) {
2893 PciIo->CopyMem (
2894 PciIo,
2895 EfiPciIoWidthUint8,
2896 EFI_PCI_IO_PASS_THROUGH_BAR,
2897 (UINT64) (UINTN) (DestinationAddress + Offset),
2898 EFI_PCI_IO_PASS_THROUGH_BAR,
2899 (UINT64) (UINTN) (SourceAddress + Offset),
2900 Bytes
2901 );
2902 }
2903 } else {
2904 SourceOffset = (SourceY << 6) + (SourceY << 4) + (SourceX >> 3);
2905 SourceWidth = ((SourceX + Width - 1) >> 3) - (SourceX >> 3) + 1;
2906
2907 //
2908 // Read all the pixels in the 4 bit planes into a memory buffer that looks like the VGA buffer
2909 //
2910 VgaReadBitPlanes (
2911 PciIo,
2912 MemAddress + SourceOffset,
2913 VgaFrameBuffer + SourceOffset,
2914 SourceWidth,
2915 Height
2916 );
2917 }
2918
2919 break;
2920
2921 case EfiBltVideoFill:
2922 StartAddress = (UINTN) (MemAddress + (DestinationY << 6) + (DestinationY << 4) + (DestinationX >> 3));
2923 Bytes = ((DestinationX + Width - 1) >> 3) - (DestinationX >> 3);
2924 LeftMask = mVgaLeftMaskTable[DestinationX & 0x07];
2925 RightMask = mVgaRightMaskTable[(DestinationX + Width - 1) & 0x07];
2926 if (Bytes == 0) {
2927 LeftMask = (UINT8) (LeftMask & RightMask);
2928 RightMask = 0;
2929 }
2930
2931 if (LeftMask == 0xff) {
2932 StartAddress--;
2933 Bytes++;
2934 LeftMask = 0;
2935 }
2936
2937 if (RightMask == 0xff) {
2938 Bytes++;
2939 RightMask = 0;
2940 }
2941
2942 PixelColor = VgaConvertColor (BltBuffer);
2943
2944 //
2945 // Program the Mode Register Write mode 2, Read mode 0
2946 //
2947 WriteGraphicsController (
2948 PciIo,
2949 VGA_GRAPHICS_CONTROLLER_MODE_REGISTER,
2950 VGA_GRAPHICS_CONTROLLER_READ_MODE_0 | VGA_GRAPHICS_CONTROLLER_WRITE_MODE_2
2951 );
2952
2953 //
2954 // Program the Data Rotate/Function Select Register to replace
2955 //
2956 WriteGraphicsController (
2957 PciIo,
2958 VGA_GRAPHICS_CONTROLLER_DATA_ROTATE_REGISTER,
2959 VGA_GRAPHICS_CONTROLLER_FUNCTION_REPLACE
2960 );
2961
2962 if (LeftMask != 0) {
2963 //
2964 // Program the BitMask register with the Left column mask
2965 //
2966 WriteGraphicsController (
2967 PciIo,
2968 VGA_GRAPHICS_CONTROLLER_BIT_MASK_REGISTER,
2969 LeftMask
2970 );
2971
2972 for (Index = 0, Address = StartAddress; Index < Height; Index++, Address += BytesPerScanLine) {
2973 //
2974 // Read data from the bit planes into the latches
2975 //
2976 PciIo->Mem.Read (
2977 PciIo,
2978 EfiPciIoWidthUint8,
2979 EFI_PCI_IO_PASS_THROUGH_BAR,
2980 (UINT64) (UINTN) Address,
2981 1,
2982 &Data
2983 );
2984 //
2985 // Write the lower 4 bits of PixelColor to the bit planes in the pixels enabled by BitMask
2986 //
2987 PciIo->Mem.Write (
2988 PciIo,
2989 EfiPciIoWidthUint8,
2990 EFI_PCI_IO_PASS_THROUGH_BAR,
2991 (UINT64) (UINTN) Address,
2992 1,
2993 &PixelColor
2994 );
2995 }
2996 }
2997
2998 if (Bytes > 1) {
2999 //
3000 // Program the BitMask register with the middle column mask of 0xff
3001 //
3002 WriteGraphicsController (
3003 PciIo,
3004 VGA_GRAPHICS_CONTROLLER_BIT_MASK_REGISTER,
3005 0xff
3006 );
3007
3008 for (Index = 0, Address = StartAddress + 1; Index < Height; Index++, Address += BytesPerScanLine) {
3009 PciIo->Mem.Write (
3010 PciIo,
3011 EfiPciIoWidthFillUint8,
3012 EFI_PCI_IO_PASS_THROUGH_BAR,
3013 (UINT64) (UINTN) Address,
3014 Bytes - 1,
3015 &PixelColor
3016 );
3017 }
3018 }
3019
3020 if (RightMask != 0) {
3021 //
3022 // Program the BitMask register with the Right column mask
3023 //
3024 WriteGraphicsController (
3025 PciIo,
3026 VGA_GRAPHICS_CONTROLLER_BIT_MASK_REGISTER,
3027 RightMask
3028 );
3029
3030 for (Index = 0, Address = StartAddress + Bytes; Index < Height; Index++, Address += BytesPerScanLine) {
3031 //
3032 // Read data from the bit planes into the latches
3033 //
3034 PciIo->Mem.Read (
3035 PciIo,
3036 EfiPciIoWidthUint8,
3037 EFI_PCI_IO_PASS_THROUGH_BAR,
3038 (UINT64) (UINTN) Address,
3039 1,
3040 &Data
3041 );
3042 //
3043 // Write the lower 4 bits of PixelColor to the bit planes in the pixels enabled by BitMask
3044 //
3045 PciIo->Mem.Write (
3046 PciIo,
3047 EfiPciIoWidthUint8,
3048 EFI_PCI_IO_PASS_THROUGH_BAR,
3049 (UINT64) (UINTN) Address,
3050 1,
3051 &PixelColor
3052 );
3053 }
3054 }
3055 break;
3056
3057 case EfiBltBufferToVideo:
3058 StartAddress = (UINTN) (MemAddress + (DestinationY << 6) + (DestinationY << 4) + (DestinationX >> 3));
3059 LeftMask = mVgaBitMaskTable[DestinationX & 0x07];
3060
3061 //
3062 // Program the Mode Register Write mode 2, Read mode 0
3063 //
3064 WriteGraphicsController (
3065 PciIo,
3066 VGA_GRAPHICS_CONTROLLER_MODE_REGISTER,
3067 VGA_GRAPHICS_CONTROLLER_READ_MODE_0 | VGA_GRAPHICS_CONTROLLER_WRITE_MODE_2
3068 );
3069
3070 //
3071 // Program the Data Rotate/Function Select Register to replace
3072 //
3073 WriteGraphicsController (
3074 PciIo,
3075 VGA_GRAPHICS_CONTROLLER_DATA_ROTATE_REGISTER,
3076 VGA_GRAPHICS_CONTROLLER_FUNCTION_REPLACE
3077 );
3078
3079 for (Index = 0, Address = StartAddress; Index < Height; Index++, Address += BytesPerScanLine) {
3080 for (Index1 = 0; Index1 < Width; Index1++) {
3081 BiosVideoPrivate->LineBuffer[Index1] = VgaConvertColor (&BltBuffer[(SourceY + Index) * (Delta >> 2) + SourceX + Index1]);
3082 }
3083 AddressFix = Address;
3084
3085 for (Bit = 0; Bit < 8; Bit++) {
3086 //
3087 // Program the BitMask register with the Left column mask
3088 //
3089 WriteGraphicsController (
3090 PciIo,
3091 VGA_GRAPHICS_CONTROLLER_BIT_MASK_REGISTER,
3092 LeftMask
3093 );
3094
3095 for (Index1 = Bit, Address1 = (UINT8 *) AddressFix; Index1 < Width; Index1 += 8, Address1++) {
3096 //
3097 // Read data from the bit planes into the latches
3098 //
3099 PciIo->Mem.Read (
3100 PciIo,
3101 EfiPciIoWidthUint8,
3102 EFI_PCI_IO_PASS_THROUGH_BAR,
3103 (UINT64) (UINTN) Address1,
3104 1,
3105 &Data
3106 );
3107
3108 PciIo->Mem.Write (
3109 PciIo,
3110 EfiPciIoWidthUint8,
3111 EFI_PCI_IO_PASS_THROUGH_BAR,
3112 (UINT64) (UINTN) Address1,
3113 1,
3114 &BiosVideoPrivate->LineBuffer[Index1]
3115 );
3116 }
3117
3118 LeftMask = (UINT8) (LeftMask >> 1);
3119 if (LeftMask == 0) {
3120 LeftMask = 0x80;
3121 AddressFix++;
3122 }
3123 }
3124 }
3125
3126 break;
3127
3128 default: ;
3129 }
3130
3131 gBS->RestoreTPL (OriginalTPL);
3132
3133 return EFI_SUCCESS;
3134 }
3135
3136 //
3137 // VGA Mini Port Protocol Functions
3138 //
3139
3140 /**
3141 VgaMiniPort protocol interface to set mode.
3142
3143 @param This Pointer to VgaMiniPort protocol instance
3144 @param ModeNumber The index of the mode
3145
3146 @retval EFI_UNSUPPORTED The requested mode is not supported
3147 @retval EFI_SUCCESS The requested mode is set successfully
3148
3149 **/
3150 EFI_STATUS
3151 EFIAPI
3152 BiosVideoVgaMiniPortSetMode (
3153 IN EFI_VGA_MINI_PORT_PROTOCOL *This,
3154 IN UINTN ModeNumber
3155 )
3156 {
3157 BIOS_VIDEO_DEV *BiosVideoPrivate;
3158 EFI_IA32_REGISTER_SET Regs;
3159
3160 if (This == NULL) {
3161 return EFI_INVALID_PARAMETER;
3162 }
3163
3164 //
3165 // Make sure the ModeNumber is a valid value
3166 //
3167 if (ModeNumber >= This->MaxMode) {
3168 return EFI_UNSUPPORTED;
3169 }
3170 //
3171 // Get the device structure for this device
3172 //
3173 BiosVideoPrivate = BIOS_VIDEO_DEV_FROM_VGA_MINI_PORT_THIS (This);
3174
3175 switch (ModeNumber) {
3176 case 0:
3177 //
3178 // Set the 80x25 Text VGA Mode
3179 //
3180 Regs.H.AH = 0x00;
3181 Regs.H.AL = 0x83;
3182 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
3183
3184 Regs.H.AH = 0x11;
3185 Regs.H.AL = 0x14;
3186 Regs.H.BL = 0;
3187 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
3188 break;
3189
3190 case 1:
3191 //
3192 // Set the 80x50 Text VGA Mode
3193 //
3194 Regs.H.AH = 0x00;
3195 Regs.H.AL = 0x83;
3196 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
3197 Regs.H.AH = 0x11;
3198 Regs.H.AL = 0x12;
3199 Regs.H.BL = 0;
3200 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
3201 break;
3202
3203 default:
3204 return EFI_UNSUPPORTED;
3205 }
3206
3207 return EFI_SUCCESS;
3208 }
3209
3210 /**
3211 Event handler for Exit Boot Service.
3212
3213 @param Event The event that be siganlled when exiting boot service.
3214 @param Context Pointer to instance of BIOS_VIDEO_DEV.
3215
3216 **/
3217 VOID
3218 EFIAPI
3219 BiosVideoNotifyExitBootServices (
3220 IN EFI_EVENT Event,
3221 IN VOID *Context
3222 )
3223 {
3224 BIOS_VIDEO_DEV *BiosVideoPrivate;
3225 EFI_IA32_REGISTER_SET Regs;
3226
3227 BiosVideoPrivate = (BIOS_VIDEO_DEV *)Context;
3228
3229 //
3230 // Set the 80x25 Text VGA Mode
3231 //
3232 Regs.H.AH = 0x00;
3233 Regs.H.AL = 0x03;
3234 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
3235
3236 Regs.H.AH = 0x00;
3237 Regs.H.AL = 0x83;
3238 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
3239
3240 Regs.H.AH = 0x11;
3241 Regs.H.AL = 0x04;
3242 Regs.H.BL = 0;
3243 BiosVideoPrivate->LegacyBios->Int86 (BiosVideoPrivate->LegacyBios, 0x10, &Regs);
3244 }
3245
3246 /**
3247 The user Entry Point for module UefiBiosVideo. The user code starts with this function.
3248
3249 @param[in] ImageHandle The firmware allocated handle for the EFI image.
3250 @param[in] SystemTable A pointer to the EFI System Table.
3251
3252 @retval EFI_SUCCESS The entry point is executed successfully.
3253 @retval other Some error occurs when executing this entry point.
3254
3255 **/
3256 EFI_STATUS
3257 EFIAPI
3258 BiosVideoEntryPoint(
3259 IN EFI_HANDLE ImageHandle,
3260 IN EFI_SYSTEM_TABLE *SystemTable
3261 )
3262 {
3263 EFI_STATUS Status;
3264
3265 //
3266 // Install driver model protocol(s).
3267 //
3268 Status = EfiLibInstallDriverBindingComponentName2 (
3269 ImageHandle,
3270 SystemTable,
3271 &gBiosVideoDriverBinding,
3272 ImageHandle,
3273 &gBiosVideoComponentName,
3274 &gBiosVideoComponentName2
3275 );
3276 ASSERT_EFI_ERROR (Status);
3277
3278 //
3279 // Install Legacy BIOS GUID to mark this driver as a BIOS Thunk Driver
3280 //
3281 return gBS->InstallMultipleProtocolInterfaces (
3282 &ImageHandle,
3283 &gEfiLegacyBiosGuid,
3284 NULL,
3285 NULL
3286 );
3287 }
3288