2 ConsoleOut Routines that speak VGA.
4 Copyright (c) 2007 - 2012, Intel Corporation. All rights reserved.<BR>
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
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.
17 #include "BiosVideo.h"
20 // EFI Driver Binding Protocol Instance
22 EFI_DRIVER_BINDING_PROTOCOL gBiosVideoDriverBinding
= {
23 BiosVideoDriverBindingSupported
,
24 BiosVideoDriverBindingStart
,
25 BiosVideoDriverBindingStop
,
32 // Global lookup tables for VGA graphics modes
34 UINT8 mVgaLeftMaskTable
[] = { 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
36 UINT8 mVgaRightMaskTable
[] = { 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
38 UINT8 mVgaBitMaskTable
[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
41 // Save controller attributes during first start
43 UINT64 mOriginalPciAttributes
;
44 BOOLEAN mPciAttributesSaved
= FALSE
;
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 }
66 // Standard timing defined by VESA EDID
68 VESA_BIOS_EXTENSIONS_EDID_TIMING mEstablishedEdidTiming
[] = {
70 // Established Timing I
81 // Established Timing II
92 // Established Timing III
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
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
112 BiosVideoDriverBindingSupported (
113 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
114 IN EFI_HANDLE Controller
,
115 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
119 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
120 EFI_PCI_IO_PROTOCOL
*PciIo
;
125 // See if the Legacy BIOS Protocol is available
127 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
128 if (EFI_ERROR (Status
)) {
133 // Open the IO Abstraction(s) needed to perform the supported test
135 Status
= gBS
->OpenProtocol (
137 &gEfiPciIoProtocolGuid
,
139 This
->DriverBindingHandle
,
141 EFI_OPEN_PROTOCOL_BY_DRIVER
143 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
147 if (Status
== EFI_ALREADY_STARTED
) {
149 // If VgaMiniPort protocol is installed, EFI_ALREADY_STARTED indicates failure,
150 // because VgaMiniPort protocol is installed on controller handle directly.
152 Status
= gBS
->OpenProtocol (
154 &gEfiVgaMiniPortProtocolGuid
,
158 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
160 if (!EFI_ERROR (Status
)) {
161 return EFI_ALREADY_STARTED
;
165 // See if this is a PCI Graphics Controller by looking at the Command register and
166 // Class Code Register
168 Status
= PciIo
->Pci
.Read (
172 sizeof (Pci
) / sizeof (UINT32
),
175 if (EFI_ERROR (Status
)) {
176 Status
= EFI_UNSUPPORTED
;
180 Status
= EFI_UNSUPPORTED
;
181 if (Pci
.Hdr
.ClassCode
[2] == 0x03 || (Pci
.Hdr
.ClassCode
[2] == 0x00 && Pci
.Hdr
.ClassCode
[1] == 0x01)) {
183 Status
= EFI_SUCCESS
;
185 // If this is a graphics controller,
186 // go further check RemainingDevicePath validation
188 if (RemainingDevicePath
!= NULL
) {
189 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
191 // Check if RemainingDevicePath is the End of Device Path Node,
192 // if yes, return EFI_SUCCESS
194 if (!IsDevicePathEnd (Node
)) {
196 // If RemainingDevicePath isn't the End of Device Path Node,
197 // check its validation
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
;
211 &gEfiPciIoProtocolGuid
,
212 This
->DriverBindingHandle
,
221 Install Graphics Output Protocol onto VGA device handles.
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
233 BiosVideoDriverBindingStart (
234 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
235 IN EFI_HANDLE Controller
,
236 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
240 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
241 EFI_PCI_IO_PROTOCOL
*PciIo
;
242 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
247 // Initialize local variables
250 ParentDevicePath
= NULL
;
254 // See if the Legacy BIOS Protocol is available
256 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
257 if (EFI_ERROR (Status
)) {
262 // Prepare for status code
264 Status
= gBS
->HandleProtocol (
266 &gEfiDevicePathProtocolGuid
,
267 (VOID
**) &ParentDevicePath
269 if (EFI_ERROR (Status
)) {
274 // Open the IO Abstraction(s) needed
276 Status
= gBS
->OpenProtocol (
278 &gEfiPciIoProtocolGuid
,
280 This
->DriverBindingHandle
,
282 EFI_OPEN_PROTOCOL_BY_DRIVER
284 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
289 // Save original PCI attributes
291 if (!mPciAttributesSaved
) {
292 Status
= PciIo
->Attributes (
294 EfiPciIoAttributeOperationGet
,
296 &mOriginalPciAttributes
299 if (EFI_ERROR (Status
)) {
302 mPciAttributesSaved
= TRUE
;
306 // Get supported PCI attributes
308 Status
= PciIo
->Attributes (
310 EfiPciIoAttributeOperationSupported
,
314 if (EFI_ERROR (Status
)) {
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
;
324 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
326 EFI_PERIPHERAL_LOCAL_CONSOLE
| EFI_P_PC_ENABLE
,
330 // Enable the device and make sure VGA cycles are being forwarded to this VGA device
332 Status
= PciIo
->Attributes (
334 EfiPciIoAttributeOperationEnable
,
335 EFI_PCI_DEVICE_ENABLE
| EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY
| Supports
,
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
,
347 // Check to see if there is a legacy option ROM image associated with this PCI device
349 Status
= LegacyBios
->CheckPciRom (
356 if (EFI_ERROR (Status
)) {
360 // Post the legacy option ROM if it is available.
362 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
367 Status
= LegacyBios
->InstallPciRom (
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
,
386 if (RemainingDevicePath
!= NULL
) {
387 if (IsDevicePathEnd (RemainingDevicePath
) &&
388 (FeaturePcdGet (PcdBiosVideoCheckVbeEnable
) || FeaturePcdGet (PcdBiosVideoCheckVgaEnable
))) {
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
;
398 // Create child handle and install GraphicsOutputProtocol on it
400 Status
= BiosVideoChildHandleInstall (
410 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
411 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
413 EFI_PERIPHERAL_LOCAL_CONSOLE
| EFI_P_PC_DISABLE
,
417 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
419 EFI_PERIPHERAL_LOCAL_CONSOLE
| EFI_P_EC_NOT_DETECTED
,
422 if (!HasChildHandle (Controller
)) {
423 if (mPciAttributesSaved
) {
425 // Restore original PCI attributes
429 EfiPciIoAttributeOperationSet
,
430 mOriginalPciAttributes
,
436 // Release PCI I/O Protocols on the controller handle.
440 &gEfiPciIoProtocolGuid
,
441 This
->DriverBindingHandle
,
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
458 @retval EFI_SUCCESS Driver disconnected successfully from controller
459 @retval EFI_UNSUPPORTED Cannot find BIOS_VIDEO_DEV structure
464 BiosVideoDriverBindingStop (
465 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
466 IN EFI_HANDLE Controller
,
467 IN UINTN NumberOfChildren
,
468 IN EFI_HANDLE
*ChildHandleBuffer
472 BOOLEAN AllChildrenStopped
;
474 EFI_PCI_IO_PROTOCOL
*PciIo
;
476 AllChildrenStopped
= TRUE
;
478 if (NumberOfChildren
== 0) {
480 // Close PCI I/O protocol on the controller handle
484 &gEfiPciIoProtocolGuid
,
485 This
->DriverBindingHandle
,
492 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
493 Status
= BiosVideoChildHandleUninstall (This
, Controller
, ChildHandleBuffer
[Index
]);
495 if (EFI_ERROR (Status
)) {
496 AllChildrenStopped
= FALSE
;
500 if (!AllChildrenStopped
) {
501 return EFI_DEVICE_ERROR
;
504 if (!HasChildHandle (Controller
)) {
505 if (mPciAttributesSaved
) {
506 Status
= gBS
->HandleProtocol (
508 &gEfiPciIoProtocolGuid
,
511 ASSERT_EFI_ERROR (Status
);
514 // Restore original PCI attributes
516 Status
= PciIo
->Attributes (
518 EfiPciIoAttributeOperationSet
,
519 mOriginalPciAttributes
,
522 ASSERT_EFI_ERROR (Status
);
532 Install child handles if the Handle supports MBR format.
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
541 @retval EFI_SUCCESS If a child handle was added
542 @retval other A child handle was not added
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
556 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
558 ACPI_ADR_DEVICE_PATH AcpiDeviceNode
;
559 BOOLEAN ProtocolInstalled
;
562 // Allocate the private device structure for video device
564 BiosVideoPrivate
= (BIOS_VIDEO_DEV
*) AllocateZeroPool (
565 sizeof (BIOS_VIDEO_DEV
)
567 if (NULL
== BiosVideoPrivate
) {
568 Status
= EFI_OUT_OF_RESOURCES
;
573 // See if this is a VGA compatible controller or not
575 Status
= ParentPciIo
->Pci
.Read (
579 sizeof (Pci
) / sizeof (UINT32
),
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
,
590 BiosVideoPrivate
->VgaCompatible
= FALSE
;
591 if (Pci
.Hdr
.ClassCode
[2] == 0x00 && Pci
.Hdr
.ClassCode
[1] == 0x01) {
592 BiosVideoPrivate
->VgaCompatible
= TRUE
;
595 if (Pci
.Hdr
.ClassCode
[2] == 0x03 && Pci
.Hdr
.ClassCode
[1] == 0x00 && Pci
.Hdr
.ClassCode
[0] == 0x00) {
596 BiosVideoPrivate
->VgaCompatible
= TRUE
;
599 if (PcdGetBool (PcdBiosVideoSetTextVgaModeEnable
)) {
601 // Create EXIT_BOOT_SERIVES Event
603 Status
= gBS
->CreateEventEx (
606 BiosVideoNotifyExitBootServices
,
608 &gEfiEventExitBootServicesGuid
,
609 &BiosVideoPrivate
->ExitBootServicesEvent
611 if (EFI_ERROR (Status
)) {
617 // Initialize the child private structure
619 BiosVideoPrivate
->Signature
= BIOS_VIDEO_DEV_SIGNATURE
;
622 // Fill in Graphics Output specific mode structures
624 BiosVideoPrivate
->HardwareNeedsStarting
= TRUE
;
625 BiosVideoPrivate
->ModeData
= NULL
;
626 BiosVideoPrivate
->LineBuffer
= NULL
;
627 BiosVideoPrivate
->VgaFrameBuffer
= NULL
;
628 BiosVideoPrivate
->VbeFrameBuffer
= NULL
;
631 // Fill in the Graphics Output Protocol
633 BiosVideoPrivate
->GraphicsOutput
.QueryMode
= BiosVideoGraphicsOutputQueryMode
;
634 BiosVideoPrivate
->GraphicsOutput
.SetMode
= BiosVideoGraphicsOutputSetMode
;
638 // Allocate buffer for Graphics Output Protocol mode information
640 BiosVideoPrivate
->GraphicsOutput
.Mode
= (EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE
*) AllocatePool (
641 sizeof (EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE
)
643 if (NULL
== BiosVideoPrivate
->GraphicsOutput
.Mode
) {
644 Status
= EFI_OUT_OF_RESOURCES
;
648 BiosVideoPrivate
->GraphicsOutput
.Mode
->Info
= (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*) AllocatePool (
649 sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
)
651 if (NULL
== BiosVideoPrivate
->GraphicsOutput
.Mode
->Info
) {
652 Status
= EFI_OUT_OF_RESOURCES
;
657 // Assume that Graphics Output Protocol will be produced until proven otherwise
659 BiosVideoPrivate
->ProduceGraphicsOutput
= TRUE
;
662 // Set Gop Device Path, here RemainingDevicePath will not be one End of Device Path Node.
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
));
672 BiosVideoPrivate
->GopDevicePath
= AppendDevicePathNode (
674 (EFI_DEVICE_PATH_PROTOCOL
*) &AcpiDeviceNode
677 BiosVideoPrivate
->GopDevicePath
= AppendDevicePathNode (ParentDevicePath
, RemainingDevicePath
);
681 // Creat child handle and device path protocol firstly
683 BiosVideoPrivate
->Handle
= NULL
;
684 Status
= gBS
->InstallMultipleProtocolInterfaces (
685 &BiosVideoPrivate
->Handle
,
686 &gEfiDevicePathProtocolGuid
,
687 BiosVideoPrivate
->GopDevicePath
,
690 if (EFI_ERROR (Status
)) {
696 // Fill in the VGA Mini Port Protocol fields
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
;
707 // Child handle need to consume the Legacy Bios protocol
709 BiosVideoPrivate
->LegacyBios
= ParentLegacyBios
;
712 // When check for VBE, PCI I/O protocol is needed, so use parent's protocol interface temporally
714 BiosVideoPrivate
->PciIo
= ParentPciIo
;
717 // Check for VESA BIOS Extensions for modes that are compatible with Graphics Output
719 if (FeaturePcdGet (PcdBiosVideoCheckVbeEnable
)) {
720 Status
= BiosVideoCheckForVbe (BiosVideoPrivate
);
721 DEBUG ((EFI_D_INFO
, "BiosVideoCheckForVbe - %r\n", Status
));
723 Status
= EFI_UNSUPPORTED
;
725 if (EFI_ERROR (Status
)) {
727 // The VESA BIOS Extensions are not compatible with Graphics Output, so check for support
728 // for the standard 640x480 16 color VGA mode
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
));
736 Status
= EFI_UNSUPPORTED
;
740 if (EFI_ERROR (Status
)) {
742 // Free GOP mode structure if it is not freed before
743 // VgaMiniPort does not need this structure any more
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
;
750 FreePool (BiosVideoPrivate
->GraphicsOutput
.Mode
);
751 BiosVideoPrivate
->GraphicsOutput
.Mode
= NULL
;
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.
758 BiosVideoPrivate
->ProduceGraphicsOutput
= FALSE
;
761 // INT services are available, so on the 80x25 and 80x50 text mode are supported
763 BiosVideoPrivate
->VgaMiniPort
.MaxMode
= 2;
767 ProtocolInstalled
= FALSE
;
769 if (BiosVideoPrivate
->ProduceGraphicsOutput
) {
771 // Creat child handle and install Graphics Output Protocol,EDID Discovered/Active Protocol
773 Status
= gBS
->InstallMultipleProtocolInterfaces (
774 &BiosVideoPrivate
->Handle
,
775 &gEfiGraphicsOutputProtocolGuid
,
776 &BiosVideoPrivate
->GraphicsOutput
,
777 &gEfiEdidDiscoveredProtocolGuid
,
778 &BiosVideoPrivate
->EdidDiscovered
,
779 &gEfiEdidActiveProtocolGuid
,
780 &BiosVideoPrivate
->EdidActive
,
784 if (!EFI_ERROR (Status
)) {
786 // Open the Parent Handle for the child
788 Status
= gBS
->OpenProtocol (
790 &gEfiPciIoProtocolGuid
,
791 (VOID
**) &BiosVideoPrivate
->PciIo
,
792 This
->DriverBindingHandle
,
793 BiosVideoPrivate
->Handle
,
794 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
796 if (EFI_ERROR (Status
)) {
799 ProtocolInstalled
= TRUE
;
803 if (!ProtocolInstalled
) {
805 // Install VGA Mini Port Protocol
807 Status
= gBS
->InstallMultipleProtocolInterfaces (
809 &gEfiVgaMiniPortProtocolGuid
,
810 &BiosVideoPrivate
->VgaMiniPort
,
816 if (EFI_ERROR (Status
)) {
817 if ((BiosVideoPrivate
!= NULL
) && (BiosVideoPrivate
->ExitBootServicesEvent
!= NULL
)) {
818 gBS
->CloseEvent (BiosVideoPrivate
->ExitBootServicesEvent
);
821 // Free private data structure
823 BiosVideoDeviceReleaseResource (BiosVideoPrivate
);
831 Deregister an video child handle and free resources.
833 @param This Protocol instance pointer.
834 @param Controller Video controller handle
835 @param Handle Video child handle
841 BiosVideoChildHandleUninstall (
842 EFI_DRIVER_BINDING_PROTOCOL
*This
,
843 EFI_HANDLE Controller
,
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
;
854 BiosVideoPrivate
= NULL
;
855 GraphicsOutput
= NULL
;
857 Status
= EFI_UNSUPPORTED
;
859 Status
= gBS
->OpenProtocol (
861 &gEfiGraphicsOutputProtocolGuid
,
862 (VOID
**) &GraphicsOutput
,
863 This
->DriverBindingHandle
,
865 EFI_OPEN_PROTOCOL_GET_PROTOCOL
867 if (!EFI_ERROR (Status
)) {
868 BiosVideoPrivate
= BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (GraphicsOutput
);
871 if (EFI_ERROR (Status
)) {
872 Status
= gBS
->OpenProtocol (
874 &gEfiVgaMiniPortProtocolGuid
,
875 (VOID
**) &VgaMiniPort
,
876 This
->DriverBindingHandle
,
878 EFI_OPEN_PROTOCOL_GET_PROTOCOL
880 if (!EFI_ERROR (Status
)) {
881 BiosVideoPrivate
= BIOS_VIDEO_DEV_FROM_VGA_MINI_PORT_THIS (VgaMiniPort
);
885 if (BiosVideoPrivate
== NULL
) {
886 return EFI_UNSUPPORTED
;
890 // Set the 80x25 Text VGA Mode
894 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
899 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
902 // Close PCI I/O protocol that opened by child handle
904 Status
= gBS
->CloseProtocol (
906 &gEfiPciIoProtocolGuid
,
907 This
->DriverBindingHandle
,
912 // Uninstall protocols on child handle
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
,
928 if (!BiosVideoPrivate
->ProduceGraphicsOutput
) {
929 Status
= gBS
->UninstallMultipleProtocolInterfaces (
931 &gEfiVgaMiniPortProtocolGuid
,
932 &BiosVideoPrivate
->VgaMiniPort
,
937 if (EFI_ERROR (Status
)) {
940 &gEfiPciIoProtocolGuid
,
942 This
->DriverBindingHandle
,
944 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
949 if (PcdGetBool (PcdBiosVideoSetTextVgaModeEnable
)) {
951 // Close EXIT_BOOT_SERIVES Event
953 gBS
->CloseEvent (BiosVideoPrivate
->ExitBootServicesEvent
);
957 // Release all allocated resources
959 BiosVideoDeviceReleaseResource (BiosVideoPrivate
);
966 Release resource for biso video instance.
968 @param BiosVideoPrivate Video child device private data structure
972 BiosVideoDeviceReleaseResource (
973 BIOS_VIDEO_DEV
*BiosVideoPrivate
976 if (BiosVideoPrivate
== NULL
) {
981 // Release all the resourses occupied by the BIOS_VIDEO_DEV
985 // Free VGA Frame Buffer
987 if (BiosVideoPrivate
->VgaFrameBuffer
!= NULL
) {
988 FreePool (BiosVideoPrivate
->VgaFrameBuffer
);
991 // Free VBE Frame Buffer
993 if (BiosVideoPrivate
->VbeFrameBuffer
!= NULL
) {
994 FreePool (BiosVideoPrivate
->VbeFrameBuffer
);
999 if (BiosVideoPrivate
->LineBuffer
!= NULL
) {
1000 FreePool (BiosVideoPrivate
->LineBuffer
);
1005 if (BiosVideoPrivate
->ModeData
!= NULL
) {
1006 FreePool (BiosVideoPrivate
->ModeData
);
1009 // Free memory allocated below 1MB
1011 if (BiosVideoPrivate
->PagesBelow1MB
!= 0) {
1012 gBS
->FreePages (BiosVideoPrivate
->PagesBelow1MB
, BiosVideoPrivate
->NumberOfPagesBelow1MB
);
1015 if (BiosVideoPrivate
->VbeSaveRestorePages
!= 0) {
1016 gBS
->FreePages (BiosVideoPrivate
->VbeSaveRestoreBuffer
, BiosVideoPrivate
->VbeSaveRestorePages
);
1020 // Free graphics output protocol occupied resource
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
;
1027 FreePool (BiosVideoPrivate
->GraphicsOutput
.Mode
);
1028 BiosVideoPrivate
->GraphicsOutput
.Mode
= NULL
;
1031 // Free EDID discovered protocol occupied resource
1033 if (BiosVideoPrivate
->EdidDiscovered
.Edid
!= NULL
) {
1034 FreePool (BiosVideoPrivate
->EdidDiscovered
.Edid
);
1037 // Free EDID active protocol occupied resource
1039 if (BiosVideoPrivate
->EdidActive
.Edid
!= NULL
) {
1040 FreePool (BiosVideoPrivate
->EdidActive
.Edid
);
1043 if (BiosVideoPrivate
->GopDevicePath
!= NULL
) {
1044 FreePool (BiosVideoPrivate
->GopDevicePath
);
1047 FreePool (BiosVideoPrivate
);
1054 Generate a search key for a specified timing data.
1056 @param EdidTiming Pointer to EDID timing
1058 @return The 32 bit unique key for search.
1063 VESA_BIOS_EXTENSIONS_EDID_TIMING
*EdidTiming
1069 // Be sure no conflicts for all standard timing defined by VESA.
1071 Key
= (EdidTiming
->HorizontalResolution
* 2) + EdidTiming
->VerticalResolution
;
1077 Parse the Established Timing and Standard Timing in EDID data block.
1079 @param EdidBuffer Pointer to EDID data block
1080 @param ValidEdidTiming Valid EDID timing information
1082 @retval TRUE The EDID data is valid.
1083 @retval FALSE The EDID data is invalid.
1089 VESA_BIOS_EXTENSIONS_VALID_EDID_TIMING
*ValidEdidTiming
1097 UINT16 HorizontalResolution
;
1098 UINT16 VerticalResolution
;
1101 VESA_BIOS_EXTENSIONS_EDID_TIMING TempTiming
;
1102 VESA_BIOS_EXTENSIONS_EDID_DATA_BLOCK
*EdidDataBlock
;
1104 EdidDataBlock
= (VESA_BIOS_EXTENSIONS_EDID_DATA_BLOCK
*) EdidBuffer
;
1107 // Check the checksum of EDID data
1110 for (Index
= 0; Index
< VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE
; Index
++) {
1111 CheckSum
= (UINT8
) (CheckSum
+ EdidBuffer
[Index
]);
1113 if (CheckSum
!= 0) {
1118 gBS
->SetMem (ValidEdidTiming
, sizeof (VESA_BIOS_EXTENSIONS_VALID_EDID_TIMING
), 0);
1120 if ((EdidDataBlock
->EstablishedTimings
[0] != 0) ||
1121 (EdidDataBlock
->EstablishedTimings
[1] != 0) ||
1122 (EdidDataBlock
->EstablishedTimings
[2] != 0)
1125 // Established timing data
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 ValidEdidTiming
->Key
[ValidNumber
] = CalculateEdidKey (&mEstablishedEdidTiming
[Index
]);
1135 TimingBits
= TimingBits
>> 1;
1139 // If no Established timing data, read the standard timing data
1141 BufferIndex
= &EdidDataBlock
->StandardTimingIdentification
[0];
1142 for (Index
= 0; Index
< 8; Index
++) {
1143 if ((BufferIndex
[0] != 0x1) && (BufferIndex
[1] != 0x1)){
1145 // A valid Standard Timing
1147 HorizontalResolution
= (UINT16
) (BufferIndex
[0] * 8 + 248);
1148 AspectRatio
= (UINT8
) (BufferIndex
[1] >> 6);
1149 switch (AspectRatio
) {
1151 VerticalResolution
= (UINT16
) (HorizontalResolution
/ 16 * 10);
1154 VerticalResolution
= (UINT16
) (HorizontalResolution
/ 4 * 3);
1157 VerticalResolution
= (UINT16
) (HorizontalResolution
/ 5 * 4);
1160 VerticalResolution
= (UINT16
) (HorizontalResolution
/ 16 * 9);
1163 VerticalResolution
= (UINT16
) (HorizontalResolution
/ 4 * 3);
1166 RefreshRate
= (UINT8
) ((BufferIndex
[1] & 0x1f) + 60);
1167 TempTiming
.HorizontalResolution
= HorizontalResolution
;
1168 TempTiming
.VerticalResolution
= VerticalResolution
;
1169 TempTiming
.RefreshRate
= RefreshRate
;
1170 ValidEdidTiming
->Key
[ValidNumber
] = CalculateEdidKey (&TempTiming
);
1177 ValidEdidTiming
->ValidNumber
= ValidNumber
;
1183 Search a specified Timing in all the valid EDID timings.
1185 @param ValidEdidTiming All valid EDID timing information.
1186 @param EdidTiming The Timing to search for.
1189 @retval FALSE Not found.
1194 VESA_BIOS_EXTENSIONS_VALID_EDID_TIMING
*ValidEdidTiming
,
1195 VESA_BIOS_EXTENSIONS_EDID_TIMING
*EdidTiming
1201 Key
= CalculateEdidKey (EdidTiming
);
1203 for (Index
= 0; Index
< ValidEdidTiming
->ValidNumber
; Index
++) {
1204 if (Key
== ValidEdidTiming
->Key
[Index
]) {
1213 Check if all video child handles have been uninstalled.
1215 @param Controller Video controller handle
1217 @return TRUE Child handles exist.
1218 @return FALSE All video child handles have been uninstalled.
1223 IN EFI_HANDLE Controller
1227 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
1234 Status
= gBS
->OpenProtocolInformation (
1236 &gEfiPciIoProtocolGuid
,
1240 for (Index
= 0; Index
< EntryCount
; Index
++) {
1241 if ((OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
1250 Check for VBE device.
1252 @param BiosVideoPrivate Pointer to BIOS_VIDEO_DEV structure
1254 @retval EFI_SUCCESS VBE device found
1258 BiosVideoCheckForVbe (
1259 IN OUT BIOS_VIDEO_DEV
*BiosVideoPrivate
1263 EFI_IA32_REGISTER_SET Regs
;
1264 UINT16
*ModeNumberPtr
;
1267 BIOS_VIDEO_MODE_DATA
*ModeBuffer
;
1268 BIOS_VIDEO_MODE_DATA
*CurrentModeData
;
1271 VESA_BIOS_EXTENSIONS_EDID_TIMING Timing
;
1272 VESA_BIOS_EXTENSIONS_VALID_EDID_TIMING ValidEdidTiming
;
1273 EFI_EDID_OVERRIDE_PROTOCOL
*EdidOverride
;
1274 UINT32 EdidAttributes
;
1275 BOOLEAN EdidOverrideFound
;
1276 UINTN EdidOverrideDataSize
;
1277 UINT8
*EdidOverrideDataBlock
;
1278 UINTN EdidActiveDataSize
;
1279 UINT8
*EdidActiveDataBlock
;
1280 UINT32 HighestHorizontalResolution
;
1281 UINT32 HighestVerticalResolution
;
1282 UINTN HighestResolutionMode
;
1285 EdidOverrideFound
= FALSE
;
1286 EdidOverrideDataBlock
= NULL
;
1287 EdidActiveDataSize
= 0;
1288 EdidActiveDataBlock
= NULL
;
1289 HighestHorizontalResolution
= 0;
1290 HighestVerticalResolution
= 0;
1291 HighestResolutionMode
= 0;
1294 // Allocate buffer under 1MB for VBE data structures
1296 BiosVideoPrivate
->NumberOfPagesBelow1MB
= EFI_SIZE_TO_PAGES (
1297 sizeof (VESA_BIOS_EXTENSIONS_INFORMATION_BLOCK
) +
1298 sizeof (VESA_BIOS_EXTENSIONS_MODE_INFORMATION_BLOCK
) +
1299 sizeof (VESA_BIOS_EXTENSIONS_EDID_DATA_BLOCK
) +
1300 sizeof (VESA_BIOS_EXTENSIONS_CRTC_INFORMATION_BLOCK
)
1303 BiosVideoPrivate
->PagesBelow1MB
= 0x00100000 - 1;
1305 Status
= gBS
->AllocatePages (
1307 EfiBootServicesData
,
1308 BiosVideoPrivate
->NumberOfPagesBelow1MB
,
1309 &BiosVideoPrivate
->PagesBelow1MB
1311 if (EFI_ERROR (Status
)) {
1315 ZeroMem (&ValidEdidTiming
, sizeof (VESA_BIOS_EXTENSIONS_VALID_EDID_TIMING
));
1318 // Fill in the VBE related data structures
1320 BiosVideoPrivate
->VbeInformationBlock
= (VESA_BIOS_EXTENSIONS_INFORMATION_BLOCK
*) (UINTN
) (BiosVideoPrivate
->PagesBelow1MB
);
1321 BiosVideoPrivate
->VbeModeInformationBlock
= (VESA_BIOS_EXTENSIONS_MODE_INFORMATION_BLOCK
*) (BiosVideoPrivate
->VbeInformationBlock
+ 1);
1322 BiosVideoPrivate
->VbeEdidDataBlock
= (VESA_BIOS_EXTENSIONS_EDID_DATA_BLOCK
*) (BiosVideoPrivate
->VbeModeInformationBlock
+ 1);
1323 BiosVideoPrivate
->VbeCrtcInformationBlock
= (VESA_BIOS_EXTENSIONS_CRTC_INFORMATION_BLOCK
*) (BiosVideoPrivate
->VbeEdidDataBlock
+ 1);
1324 BiosVideoPrivate
->VbeSaveRestorePages
= 0;
1325 BiosVideoPrivate
->VbeSaveRestoreBuffer
= 0;
1328 // Test to see if the Video Adapter is compliant with VBE 3.0
1330 gBS
->SetMem (&Regs
, sizeof (Regs
), 0);
1331 Regs
.X
.AX
= VESA_BIOS_EXTENSIONS_RETURN_CONTROLLER_INFORMATION
;
1332 gBS
->SetMem (BiosVideoPrivate
->VbeInformationBlock
, sizeof (VESA_BIOS_EXTENSIONS_INFORMATION_BLOCK
), 0);
1333 BiosVideoPrivate
->VbeInformationBlock
->VESASignature
= VESA_BIOS_EXTENSIONS_VBE2_SIGNATURE
;
1334 Regs
.X
.ES
= EFI_SEGMENT ((UINTN
) BiosVideoPrivate
->VbeInformationBlock
);
1335 Regs
.X
.DI
= EFI_OFFSET ((UINTN
) BiosVideoPrivate
->VbeInformationBlock
);
1337 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
1339 Status
= EFI_DEVICE_ERROR
;
1342 // See if the VESA call succeeded
1344 if (Regs
.X
.AX
!= VESA_BIOS_EXTENSIONS_STATUS_SUCCESS
) {
1348 // Check for 'VESA' signature
1350 if (BiosVideoPrivate
->VbeInformationBlock
->VESASignature
!= VESA_BIOS_EXTENSIONS_VESA_SIGNATURE
) {
1354 // Check to see if this is VBE 2.0 or higher
1356 if (BiosVideoPrivate
->VbeInformationBlock
->VESAVersion
< VESA_BIOS_EXTENSIONS_VERSION_2_0
) {
1361 EdidAttributes
= 0xff;
1362 EdidOverrideDataSize
= 0;
1365 // Find EDID Override protocol firstly, this protocol is installed by platform if needed.
1367 Status
= gBS
->LocateProtocol (
1368 &gEfiEdidOverrideProtocolGuid
,
1370 (VOID
**) &EdidOverride
1372 if (!EFI_ERROR (Status
)) {
1374 // Allocate double size of VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE to avoid overflow
1376 EdidOverrideDataBlock
= AllocatePool (sizeof (VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE
* 2));
1377 if (NULL
== EdidOverrideDataBlock
) {
1378 Status
= EFI_OUT_OF_RESOURCES
;
1382 Status
= EdidOverride
->GetEdid (
1384 BiosVideoPrivate
->Handle
,
1386 &EdidOverrideDataSize
,
1387 (UINT8
**) &EdidOverrideDataBlock
1389 if (!EFI_ERROR (Status
) &&
1390 EdidAttributes
== 0 &&
1391 EdidOverrideDataSize
!= 0) {
1393 // Succeeded to get EDID Override Data
1395 EdidOverrideFound
= TRUE
;
1399 if (!EdidOverrideFound
|| EdidAttributes
== EFI_EDID_OVERRIDE_DONT_OVERRIDE
) {
1401 // If EDID Override data doesn't exist or EFI_EDID_OVERRIDE_DONT_OVERRIDE returned,
1402 // read EDID information through INT10 call
1405 gBS
->SetMem (&Regs
, sizeof (Regs
), 0);
1406 Regs
.X
.AX
= VESA_BIOS_EXTENSIONS_EDID
;
1410 Regs
.X
.ES
= EFI_SEGMENT ((UINTN
) BiosVideoPrivate
->VbeEdidDataBlock
);
1411 Regs
.X
.DI
= EFI_OFFSET ((UINTN
) BiosVideoPrivate
->VbeEdidDataBlock
);
1413 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
1415 // See if the VESA call succeeded
1417 if (Regs
.X
.AX
== VESA_BIOS_EXTENSIONS_STATUS_SUCCESS
) {
1419 // Set EDID Discovered Data
1421 BiosVideoPrivate
->EdidDiscovered
.SizeOfEdid
= VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE
;
1422 BiosVideoPrivate
->EdidDiscovered
.Edid
= (UINT8
*) AllocateCopyPool (
1423 VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE
,
1424 BiosVideoPrivate
->VbeEdidDataBlock
1427 if (NULL
== BiosVideoPrivate
->EdidDiscovered
.Edid
) {
1428 Status
= EFI_OUT_OF_RESOURCES
;
1437 EdidActiveDataSize
= VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE
;
1438 EdidActiveDataBlock
= BiosVideoPrivate
->EdidDiscovered
.Edid
;
1439 } else if (EdidOverrideFound
) {
1440 EdidActiveDataSize
= EdidOverrideDataSize
;
1441 EdidActiveDataBlock
= EdidOverrideDataBlock
;
1447 // Parse EDID data structure to retrieve modes supported by monitor
1449 if (ParseEdidData ((UINT8
*) EdidActiveDataBlock
, &ValidEdidTiming
)) {
1451 // Copy EDID Override Data to EDID Active Data
1453 BiosVideoPrivate
->EdidActive
.SizeOfEdid
= (UINT32
) EdidActiveDataSize
;
1454 BiosVideoPrivate
->EdidActive
.Edid
= (UINT8
*) AllocateCopyPool (
1458 if (NULL
== BiosVideoPrivate
->EdidActive
.Edid
) {
1459 Status
= EFI_OUT_OF_RESOURCES
;
1464 BiosVideoPrivate
->EdidActive
.SizeOfEdid
= 0;
1465 BiosVideoPrivate
->EdidActive
.Edid
= NULL
;
1470 // Walk through the mode list to see if there is at least one mode the is compatible with the EDID mode
1472 ModeNumberPtr
= (UINT16
*)
1474 (((UINTN
) BiosVideoPrivate
->VbeInformationBlock
->VideoModePtr
& 0xffff0000) >> 12) |
1475 ((UINTN
) BiosVideoPrivate
->VbeInformationBlock
->VideoModePtr
& 0x0000ffff)
1481 for (; *ModeNumberPtr
!= VESA_BIOS_EXTENSIONS_END_OF_MODE_LIST
; ModeNumberPtr
++) {
1483 // Make sure this is a mode number defined by the VESA VBE specification. If it isn'tm then skip this mode number.
1485 if ((*ModeNumberPtr
& VESA_BIOS_EXTENSIONS_MODE_NUMBER_VESA
) == 0) {
1489 // Get the information about the mode
1491 gBS
->SetMem (&Regs
, sizeof (Regs
), 0);
1492 Regs
.X
.AX
= VESA_BIOS_EXTENSIONS_RETURN_MODE_INFORMATION
;
1493 Regs
.X
.CX
= *ModeNumberPtr
;
1494 gBS
->SetMem (BiosVideoPrivate
->VbeModeInformationBlock
, sizeof (VESA_BIOS_EXTENSIONS_MODE_INFORMATION_BLOCK
), 0);
1495 Regs
.X
.ES
= EFI_SEGMENT ((UINTN
) BiosVideoPrivate
->VbeModeInformationBlock
);
1496 Regs
.X
.DI
= EFI_OFFSET ((UINTN
) BiosVideoPrivate
->VbeModeInformationBlock
);
1498 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
1501 // See if the call succeeded. If it didn't, then try the next mode.
1503 if (Regs
.X
.AX
!= VESA_BIOS_EXTENSIONS_STATUS_SUCCESS
) {
1507 // See if the mode supports color. If it doesn't then try the next mode.
1509 if ((BiosVideoPrivate
->VbeModeInformationBlock
->ModeAttributes
& VESA_BIOS_EXTENSIONS_MODE_ATTRIBUTE_COLOR
) == 0) {
1513 // See if the mode supports graphics. If it doesn't then try the next mode.
1515 if ((BiosVideoPrivate
->VbeModeInformationBlock
->ModeAttributes
& VESA_BIOS_EXTENSIONS_MODE_ATTRIBUTE_GRAPHICS
) == 0) {
1519 // See if the mode supports a linear frame buffer. If it doesn't then try the next mode.
1521 if ((BiosVideoPrivate
->VbeModeInformationBlock
->ModeAttributes
& VESA_BIOS_EXTENSIONS_MODE_ATTRIBUTE_LINEAR_FRAME_BUFFER
) == 0) {
1525 // See if the mode supports 32 bit color. If it doesn't then try the next mode.
1526 // 32 bit mode can be implemented by 24 Bits Per Pixels. Also make sure the
1527 // number of bits per pixel is a multiple of 8 or more than 32 bits per pixel
1529 if (BiosVideoPrivate
->VbeModeInformationBlock
->BitsPerPixel
< 24) {
1533 if (BiosVideoPrivate
->VbeModeInformationBlock
->BitsPerPixel
> 32) {
1537 if ((BiosVideoPrivate
->VbeModeInformationBlock
->BitsPerPixel
% 8) != 0) {
1541 // See if the physical base pointer for the linear mode is valid. If it isn't then try the next mode.
1543 if (BiosVideoPrivate
->VbeModeInformationBlock
->PhysBasePtr
== 0) {
1547 if (EdidFound
&& (ValidEdidTiming
.ValidNumber
> 0)) {
1549 // EDID exist, check whether this mode match with any mode in EDID
1551 Timing
.HorizontalResolution
= BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
;
1552 Timing
.VerticalResolution
= BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
;
1553 if (!SearchEdidTiming (&ValidEdidTiming
, &Timing
)) {
1559 // Select a reasonable mode to be set for current display mode
1563 if (BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
== 1024 &&
1564 BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
== 768
1568 if (BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
== 800 &&
1569 BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
== 600
1572 PreferMode
= ModeNumber
;
1574 if (BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
== 640 &&
1575 BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
== 480
1580 if ((!EdidFound
) && (!ModeFound
)) {
1582 // When no EDID exist, only select three possible resolutions, i.e. 1024x768, 800x600, 640x480
1588 // Record the highest resolution mode to set later
1590 if ((BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
>= HighestHorizontalResolution
) &&
1591 (BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
>= HighestVerticalResolution
)) {
1592 HighestHorizontalResolution
= BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
;
1593 HighestVerticalResolution
= BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
;
1594 HighestResolutionMode
= ModeNumber
;
1598 // Add mode to the list of available modes
1601 ModeBuffer
= (BIOS_VIDEO_MODE_DATA
*) AllocatePool (
1602 ModeNumber
* sizeof (BIOS_VIDEO_MODE_DATA
)
1604 if (NULL
== ModeBuffer
) {
1605 Status
= EFI_OUT_OF_RESOURCES
;
1609 if (ModeNumber
> 1) {
1612 BiosVideoPrivate
->ModeData
,
1613 (ModeNumber
- 1) * sizeof (BIOS_VIDEO_MODE_DATA
)
1617 if (BiosVideoPrivate
->ModeData
!= NULL
) {
1618 FreePool (BiosVideoPrivate
->ModeData
);
1621 CurrentModeData
= &ModeBuffer
[ModeNumber
- 1];
1622 CurrentModeData
->VbeModeNumber
= *ModeNumberPtr
;
1623 if (BiosVideoPrivate
->VbeInformationBlock
->VESAVersion
>= VESA_BIOS_EXTENSIONS_VERSION_3_0
) {
1624 CurrentModeData
->BytesPerScanLine
= BiosVideoPrivate
->VbeModeInformationBlock
->LinBytesPerScanLine
;
1625 CurrentModeData
->Red
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->LinRedFieldPosition
;
1626 CurrentModeData
->Red
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->LinRedMaskSize
) - 1);
1627 CurrentModeData
->Blue
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->LinBlueFieldPosition
;
1628 CurrentModeData
->Blue
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->LinBlueMaskSize
) - 1);
1629 CurrentModeData
->Green
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->LinGreenFieldPosition
;
1630 CurrentModeData
->Green
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->LinGreenMaskSize
) - 1);
1631 CurrentModeData
->Reserved
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->LinRsvdFieldPosition
;
1632 CurrentModeData
->Reserved
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->LinRsvdMaskSize
) - 1);
1634 CurrentModeData
->BytesPerScanLine
= BiosVideoPrivate
->VbeModeInformationBlock
->BytesPerScanLine
;
1635 CurrentModeData
->Red
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->RedFieldPosition
;
1636 CurrentModeData
->Red
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->RedMaskSize
) - 1);
1637 CurrentModeData
->Blue
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->BlueFieldPosition
;
1638 CurrentModeData
->Blue
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->BlueMaskSize
) - 1);
1639 CurrentModeData
->Green
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->GreenFieldPosition
;
1640 CurrentModeData
->Green
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->GreenMaskSize
) - 1);
1641 CurrentModeData
->Reserved
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->RsvdFieldPosition
;
1642 CurrentModeData
->Reserved
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->RsvdMaskSize
) - 1);
1645 CurrentModeData
->PixelFormat
= PixelBitMask
;
1646 if ((BiosVideoPrivate
->VbeModeInformationBlock
->BitsPerPixel
== 32) &&
1647 (CurrentModeData
->Red
.Mask
== 0xff) && (CurrentModeData
->Green
.Mask
== 0xff) && (CurrentModeData
->Blue
.Mask
== 0xff)) {
1648 if ((CurrentModeData
->Red
.Position
== 0) && (CurrentModeData
->Green
.Position
== 8) && (CurrentModeData
->Blue
.Position
== 16)) {
1649 CurrentModeData
->PixelFormat
= PixelRedGreenBlueReserved8BitPerColor
;
1650 } else if ((CurrentModeData
->Blue
.Position
== 0) && (CurrentModeData
->Green
.Position
== 8) && (CurrentModeData
->Red
.Position
== 16)) {
1651 CurrentModeData
->PixelFormat
= PixelBlueGreenRedReserved8BitPerColor
;
1655 CurrentModeData
->PixelBitMask
.RedMask
= ((UINT32
) CurrentModeData
->Red
.Mask
) << CurrentModeData
->Red
.Position
;
1656 CurrentModeData
->PixelBitMask
.GreenMask
= ((UINT32
) CurrentModeData
->Green
.Mask
) << CurrentModeData
->Green
.Position
;
1657 CurrentModeData
->PixelBitMask
.BlueMask
= ((UINT32
) CurrentModeData
->Blue
.Mask
) << CurrentModeData
->Blue
.Position
;
1658 CurrentModeData
->PixelBitMask
.ReservedMask
= ((UINT32
) CurrentModeData
->Reserved
.Mask
) << CurrentModeData
->Reserved
.Position
;
1659 CurrentModeData
->FrameBufferSize
= BiosVideoPrivate
->VbeInformationBlock
->TotalMemory
* 64 * 1024;
1661 CurrentModeData
->LinearFrameBuffer
= (VOID
*) (UINTN
)BiosVideoPrivate
->VbeModeInformationBlock
->PhysBasePtr
;
1662 CurrentModeData
->HorizontalResolution
= BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
;
1663 CurrentModeData
->VerticalResolution
= BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
;
1665 CurrentModeData
->BitsPerPixel
= BiosVideoPrivate
->VbeModeInformationBlock
->BitsPerPixel
;
1667 BiosVideoPrivate
->ModeData
= ModeBuffer
;
1670 // Check to see if we found any modes that are compatible with GRAPHICS OUTPUT
1672 if (ModeNumber
== 0) {
1673 Status
= EFI_DEVICE_ERROR
;
1678 // Assign Gop's Blt function
1680 BiosVideoPrivate
->GraphicsOutput
.Blt
= BiosVideoGraphicsOutputVbeBlt
;
1682 BiosVideoPrivate
->GraphicsOutput
.Mode
->MaxMode
= (UINT32
) ModeNumber
;
1684 // Current mode is unknow till now, set it to an invalid mode.
1686 BiosVideoPrivate
->GraphicsOutput
.Mode
->Mode
= GRAPHICS_OUTPUT_INVALIDE_MODE_NUMBER
;
1689 // Find the best mode to initialize
1691 if ((PcdGet32 (PcdVideoHorizontalResolution
) == 0x0) || (PcdGet32 (PcdVideoVerticalResolution
) == 0x0)) {
1693 BIOS_VIDEO_MODE_DATA
*ModeData
;
1694 ModeData
= &BiosVideoPrivate
->ModeData
[HighestResolutionMode
];
1695 DEBUG ((EFI_D_INFO
, "BiosVideo set highest resolution %d x %d\n",
1696 ModeData
->HorizontalResolution
, ModeData
->VerticalResolution
));
1698 PreferMode
= HighestResolutionMode
;
1700 Status
= BiosVideoGraphicsOutputSetMode (&BiosVideoPrivate
->GraphicsOutput
, (UINT32
) PreferMode
);
1701 if (EFI_ERROR (Status
)) {
1702 for (PreferMode
= 0; PreferMode
< ModeNumber
; PreferMode
++) {
1703 Status
= BiosVideoGraphicsOutputSetMode (
1704 &BiosVideoPrivate
->GraphicsOutput
,
1707 if (!EFI_ERROR (Status
)) {
1711 if (PreferMode
== ModeNumber
) {
1713 // None mode is set successfully.
1721 // If there was an error, then free the mode structure
1723 if (EFI_ERROR (Status
)) {
1724 if (BiosVideoPrivate
->ModeData
!= NULL
) {
1725 FreePool (BiosVideoPrivate
->ModeData
);
1726 BiosVideoPrivate
->ModeData
= NULL
;
1727 BiosVideoPrivate
->MaxMode
= 0;
1729 if (EdidOverrideDataBlock
!= NULL
) {
1730 FreePool (EdidOverrideDataBlock
);
1739 Check for VGA device.
1741 @param BiosVideoPrivate Pointer to BIOS_VIDEO_DEV structure
1743 @retval EFI_SUCCESS Standard VGA device found
1747 BiosVideoCheckForVga (
1748 IN OUT BIOS_VIDEO_DEV
*BiosVideoPrivate
1752 BIOS_VIDEO_MODE_DATA
*ModeBuffer
;
1754 Status
= EFI_UNSUPPORTED
;
1757 // Assign Gop's Blt function
1759 BiosVideoPrivate
->GraphicsOutput
.Blt
= BiosVideoGraphicsOutputVgaBlt
;
1762 // Add mode to the list of available modes
1763 // caller should guarantee that Mode has been allocated.
1765 ASSERT (BiosVideoPrivate
->GraphicsOutput
.Mode
!= NULL
);
1766 BiosVideoPrivate
->GraphicsOutput
.Mode
->MaxMode
= 1;
1768 ModeBuffer
= (BIOS_VIDEO_MODE_DATA
*) AllocatePool (
1769 sizeof (BIOS_VIDEO_MODE_DATA
)
1771 if (NULL
== ModeBuffer
) {
1772 Status
= EFI_OUT_OF_RESOURCES
;
1776 ModeBuffer
->VbeModeNumber
= 0x0012;
1777 ModeBuffer
->BytesPerScanLine
= 640;
1778 ModeBuffer
->LinearFrameBuffer
= (VOID
*) (UINTN
) (0xa0000);
1779 ModeBuffer
->HorizontalResolution
= 640;
1780 ModeBuffer
->VerticalResolution
= 480;
1781 ModeBuffer
->PixelFormat
= PixelBltOnly
;
1782 ModeBuffer
->BitsPerPixel
= 8;
1783 ModeBuffer
->ColorDepth
= 32;
1784 ModeBuffer
->RefreshRate
= 60;
1786 BiosVideoPrivate
->ModeData
= ModeBuffer
;
1789 // Test to see if the Video Adapter support the 640x480 16 color mode
1791 BiosVideoPrivate
->GraphicsOutput
.Mode
->Mode
= GRAPHICS_OUTPUT_INVALIDE_MODE_NUMBER
;
1792 Status
= BiosVideoGraphicsOutputSetMode (&BiosVideoPrivate
->GraphicsOutput
, 0);
1796 // If there was an error, then free the mode structure
1798 if (EFI_ERROR (Status
)) {
1799 if (BiosVideoPrivate
->ModeData
!= NULL
) {
1800 FreePool (BiosVideoPrivate
->ModeData
);
1801 BiosVideoPrivate
->ModeData
= NULL
;
1803 if (BiosVideoPrivate
->GraphicsOutput
.Mode
!= NULL
) {
1804 if (BiosVideoPrivate
->GraphicsOutput
.Mode
->Info
!= NULL
) {
1805 FreePool (BiosVideoPrivate
->GraphicsOutput
.Mode
->Info
);
1806 BiosVideoPrivate
->GraphicsOutput
.Mode
->Info
= NULL
;
1808 FreePool (BiosVideoPrivate
->GraphicsOutput
.Mode
);
1809 BiosVideoPrivate
->GraphicsOutput
.Mode
= NULL
;
1816 // Graphics Output Protocol Member Functions for VESA BIOS Extensions
1820 Graphics Output protocol interface to get video mode.
1822 @param This Protocol instance pointer.
1823 @param ModeNumber The mode number to return information on.
1824 @param SizeOfInfo A pointer to the size, in bytes, of the Info
1826 @param Info Caller allocated buffer that returns information
1829 @retval EFI_SUCCESS Mode information returned.
1830 @retval EFI_DEVICE_ERROR A hardware error occurred trying to retrieve the
1832 @retval EFI_NOT_STARTED Video display is not initialized. Call SetMode ()
1833 @retval EFI_INVALID_PARAMETER One of the input args was NULL.
1838 BiosVideoGraphicsOutputQueryMode (
1839 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*This
,
1840 IN UINT32 ModeNumber
,
1841 OUT UINTN
*SizeOfInfo
,
1842 OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
**Info
1845 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
1846 BIOS_VIDEO_MODE_DATA
*ModeData
;
1848 BiosVideoPrivate
= BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (This
);
1850 if (BiosVideoPrivate
->HardwareNeedsStarting
) {
1851 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1852 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1853 EFI_PERIPHERAL_LOCAL_CONSOLE
| EFI_P_EC_OUTPUT_ERROR
,
1854 BiosVideoPrivate
->GopDevicePath
1856 return EFI_NOT_STARTED
;
1859 if (This
== NULL
|| Info
== NULL
|| SizeOfInfo
== NULL
|| ModeNumber
>= This
->Mode
->MaxMode
) {
1860 return EFI_INVALID_PARAMETER
;
1863 *Info
= (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*) AllocatePool (
1864 sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
)
1866 if (NULL
== *Info
) {
1867 return EFI_OUT_OF_RESOURCES
;
1870 *SizeOfInfo
= sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
);
1872 ModeData
= &BiosVideoPrivate
->ModeData
[ModeNumber
];
1873 (*Info
)->Version
= 0;
1874 (*Info
)->HorizontalResolution
= ModeData
->HorizontalResolution
;
1875 (*Info
)->VerticalResolution
= ModeData
->VerticalResolution
;
1876 (*Info
)->PixelFormat
= ModeData
->PixelFormat
;
1877 CopyMem (&((*Info
)->PixelInformation
), &(ModeData
->PixelBitMask
), sizeof(ModeData
->PixelBitMask
));
1879 (*Info
)->PixelsPerScanLine
= (ModeData
->BytesPerScanLine
* 8) / ModeData
->BitsPerPixel
;
1885 Worker function to set video mode.
1887 @param BiosVideoPrivate Instance of BIOS_VIDEO_DEV.
1888 @param ModeData The mode data to be set.
1889 @param DevicePath Pointer to Device Path Protocol.
1891 @retval EFI_SUCCESS Graphics mode was changed.
1892 @retval EFI_DEVICE_ERROR The device had an error and could not complete the
1894 @retval EFI_UNSUPPORTED ModeNumber is not supported by this device.
1898 BiosVideoSetModeWorker (
1899 IN BIOS_VIDEO_DEV
*BiosVideoPrivate
,
1900 IN BIOS_VIDEO_MODE_DATA
*ModeData
,
1901 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1905 EFI_IA32_REGISTER_SET Regs
;
1907 if (BiosVideoPrivate
->LineBuffer
!= NULL
) {
1908 FreePool (BiosVideoPrivate
->LineBuffer
);
1911 if (BiosVideoPrivate
->VgaFrameBuffer
!= NULL
) {
1912 FreePool (BiosVideoPrivate
->VgaFrameBuffer
);
1915 if (BiosVideoPrivate
->VbeFrameBuffer
!= NULL
) {
1916 FreePool (BiosVideoPrivate
->VbeFrameBuffer
);
1919 BiosVideoPrivate
->LineBuffer
= (UINT8
*) AllocatePool (
1920 ModeData
->BytesPerScanLine
1922 if (NULL
== BiosVideoPrivate
->LineBuffer
) {
1923 return EFI_OUT_OF_RESOURCES
;
1926 // Clear all registers
1928 ZeroMem (&Regs
, sizeof (Regs
));
1930 if (ModeData
->VbeModeNumber
< 0x100) {
1932 // Allocate a working buffer for BLT operations to the VGA frame buffer
1934 BiosVideoPrivate
->VgaFrameBuffer
= (UINT8
*) AllocatePool (4 * 480 * 80);
1935 if (NULL
== BiosVideoPrivate
->VgaFrameBuffer
) {
1936 return EFI_OUT_OF_RESOURCES
;
1941 Regs
.X
.AX
= ModeData
->VbeModeNumber
;
1942 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
1946 // Allocate a working buffer for BLT operations to the VBE frame buffer
1948 BiosVideoPrivate
->VbeFrameBuffer
=
1949 (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*) AllocatePool (
1950 ModeData
->BytesPerScanLine
* ModeData
->VerticalResolution
1952 if (NULL
== BiosVideoPrivate
->VbeFrameBuffer
) {
1953 return EFI_OUT_OF_RESOURCES
;
1958 Regs
.X
.AX
= VESA_BIOS_EXTENSIONS_SET_MODE
;
1959 Regs
.X
.BX
= (UINT16
) (ModeData
->VbeModeNumber
| VESA_BIOS_EXTENSIONS_MODE_NUMBER_LINEAR_FRAME_BUFFER
);
1960 ZeroMem (BiosVideoPrivate
->VbeCrtcInformationBlock
, sizeof (VESA_BIOS_EXTENSIONS_CRTC_INFORMATION_BLOCK
));
1961 Regs
.X
.ES
= EFI_SEGMENT ((UINTN
) BiosVideoPrivate
->VbeCrtcInformationBlock
);
1962 Regs
.X
.DI
= EFI_OFFSET ((UINTN
) BiosVideoPrivate
->VbeCrtcInformationBlock
);
1963 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
1966 // Check to see if the call succeeded
1968 if (Regs
.X
.AX
!= VESA_BIOS_EXTENSIONS_STATUS_SUCCESS
) {
1969 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1970 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1971 EFI_PERIPHERAL_LOCAL_CONSOLE
| EFI_P_EC_OUTPUT_ERROR
,
1974 return EFI_DEVICE_ERROR
;
1977 // Initialize the state of the VbeFrameBuffer
1979 Status
= BiosVideoPrivate
->PciIo
->Mem
.Read (
1980 BiosVideoPrivate
->PciIo
,
1981 EfiPciIoWidthUint32
,
1982 EFI_PCI_IO_PASS_THROUGH_BAR
,
1983 (UINT64
) (UINTN
) ModeData
->LinearFrameBuffer
,
1984 (ModeData
->BytesPerScanLine
* ModeData
->VerticalResolution
) >> 2,
1985 BiosVideoPrivate
->VbeFrameBuffer
1987 if (EFI_ERROR (Status
)) {
1996 Graphics Output protocol interface to set video mode.
1998 @param This Protocol instance pointer.
1999 @param ModeNumber The mode number to be set.
2001 @retval EFI_SUCCESS Graphics mode was changed.
2002 @retval EFI_DEVICE_ERROR The device had an error and could not complete the
2004 @retval EFI_UNSUPPORTED ModeNumber is not supported by this device.
2009 BiosVideoGraphicsOutputSetMode (
2010 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
* This
,
2011 IN UINT32 ModeNumber
2015 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
2016 BIOS_VIDEO_MODE_DATA
*ModeData
;
2017 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Background
;
2020 return EFI_INVALID_PARAMETER
;
2023 BiosVideoPrivate
= BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (This
);
2025 ModeData
= &BiosVideoPrivate
->ModeData
[ModeNumber
];
2027 if (ModeNumber
>= This
->Mode
->MaxMode
) {
2028 return EFI_UNSUPPORTED
;
2031 if (ModeNumber
== This
->Mode
->Mode
) {
2033 // Clear screen to black
2035 ZeroMem (&Background
, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
));
2036 BiosVideoGraphicsOutputVbeBlt (
2044 ModeData
->HorizontalResolution
,
2045 ModeData
->VerticalResolution
,
2051 Status
= BiosVideoSetModeWorker (BiosVideoPrivate
, ModeData
, BiosVideoPrivate
->GopDevicePath
);
2052 if (EFI_ERROR (Status
)) {
2056 This
->Mode
->Mode
= ModeNumber
;
2057 This
->Mode
->Info
->Version
= 0;
2058 This
->Mode
->Info
->HorizontalResolution
= ModeData
->HorizontalResolution
;
2059 This
->Mode
->Info
->VerticalResolution
= ModeData
->VerticalResolution
;
2060 This
->Mode
->Info
->PixelFormat
= ModeData
->PixelFormat
;
2061 CopyMem (&(This
->Mode
->Info
->PixelInformation
), &(ModeData
->PixelBitMask
), sizeof (ModeData
->PixelBitMask
));
2062 This
->Mode
->Info
->PixelsPerScanLine
= (ModeData
->BytesPerScanLine
* 8) / ModeData
->BitsPerPixel
;
2063 This
->Mode
->SizeOfInfo
= sizeof(EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
);
2066 // Frame BufferSize remain unchanged
2068 This
->Mode
->FrameBufferBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) ModeData
->LinearFrameBuffer
;
2069 This
->Mode
->FrameBufferSize
= ModeData
->FrameBufferSize
;
2071 BiosVideoPrivate
->HardwareNeedsStarting
= FALSE
;
2077 Update physical frame buffer, copy 4 bytes block, then copy remaining bytes.
2079 @param PciIo The pointer of EFI_PCI_IO_PROTOCOL
2080 @param VbeBuffer The data to transfer to screen
2081 @param MemAddress Physical frame buffer base address
2082 @param DestinationX The X coordinate of the destination for BltOperation
2083 @param DestinationY The Y coordinate of the destination for BltOperation
2084 @param TotalBytes The total bytes of copy
2085 @param VbePixelWidth Bytes per pixel
2086 @param BytesPerScanLine Bytes per scan line
2091 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
2092 IN UINT8
*VbeBuffer
,
2093 IN VOID
*MemAddress
,
2094 IN UINTN DestinationX
,
2095 IN UINTN DestinationY
,
2096 IN UINTN TotalBytes
,
2097 IN UINT32 VbePixelWidth
,
2098 IN UINTN BytesPerScanLine
2101 UINTN FrameBufferAddr
;
2103 UINTN RemainingBytes
;
2104 UINTN UnalignedBytes
;
2107 FrameBufferAddr
= (UINTN
) MemAddress
+ (DestinationY
* BytesPerScanLine
) + DestinationX
* VbePixelWidth
;
2110 // If TotalBytes is less than 4 bytes, only start byte copy.
2112 if (TotalBytes
< 4) {
2113 Status
= PciIo
->Mem
.Write (
2116 EFI_PCI_IO_PASS_THROUGH_BAR
,
2117 (UINT64
) FrameBufferAddr
,
2121 ASSERT_EFI_ERROR (Status
);
2126 // If VbeBuffer is not 4-byte aligned, start byte copy.
2128 UnalignedBytes
= (4 - ((UINTN
) VbeBuffer
& 0x3)) & 0x3;
2130 if (UnalignedBytes
!= 0) {
2131 Status
= PciIo
->Mem
.Write (
2134 EFI_PCI_IO_PASS_THROUGH_BAR
,
2135 (UINT64
) FrameBufferAddr
,
2139 ASSERT_EFI_ERROR (Status
);
2140 FrameBufferAddr
+= UnalignedBytes
;
2141 VbeBuffer
+= UnalignedBytes
;
2145 // Calculate 4-byte block count and remaining bytes.
2147 CopyBlockNum
= (TotalBytes
- UnalignedBytes
) >> 2;
2148 RemainingBytes
= (TotalBytes
- UnalignedBytes
) & 3;
2151 // Copy 4-byte block and remaining bytes to physical frame buffer.
2153 if (CopyBlockNum
!= 0) {
2154 Status
= PciIo
->Mem
.Write (
2156 EfiPciIoWidthUint32
,
2157 EFI_PCI_IO_PASS_THROUGH_BAR
,
2158 (UINT64
) FrameBufferAddr
,
2162 ASSERT_EFI_ERROR (Status
);
2165 if (RemainingBytes
!= 0) {
2166 FrameBufferAddr
+= (CopyBlockNum
<< 2);
2167 VbeBuffer
+= (CopyBlockNum
<< 2);
2168 Status
= PciIo
->Mem
.Write (
2171 EFI_PCI_IO_PASS_THROUGH_BAR
,
2172 (UINT64
) FrameBufferAddr
,
2176 ASSERT_EFI_ERROR (Status
);
2181 Worker function to block transfer for VBE device.
2183 @param BiosVideoPrivate Instance of BIOS_VIDEO_DEV
2184 @param BltBuffer The data to transfer to screen
2185 @param BltOperation The operation to perform
2186 @param SourceX The X coordinate of the source for BltOperation
2187 @param SourceY The Y coordinate of the source for BltOperation
2188 @param DestinationX The X coordinate of the destination for
2190 @param DestinationY The Y coordinate of the destination for
2192 @param Width The width of a rectangle in the blt rectangle in
2194 @param Height The height of a rectangle in the blt rectangle in
2196 @param Delta Not used for EfiBltVideoFill and
2197 EfiBltVideoToVideo operation. If a Delta of 0 is
2198 used, the entire BltBuffer will be operated on. If
2199 a subrectangle of the BltBuffer is used, then
2200 Delta represents the number of bytes in a row of
2202 @param Mode Mode data.
2204 @retval EFI_INVALID_PARAMETER Invalid parameter passed in
2205 @retval EFI_SUCCESS Blt operation success
2209 BiosVideoVbeBltWorker (
2210 IN BIOS_VIDEO_DEV
*BiosVideoPrivate
,
2211 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
, OPTIONAL
2212 IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation
,
2215 IN UINTN DestinationX
,
2216 IN UINTN DestinationY
,
2220 IN BIOS_VIDEO_MODE_DATA
*Mode
2223 EFI_PCI_IO_PROTOCOL
*PciIo
;
2224 EFI_TPL OriginalTPL
;
2228 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*Blt
;
2230 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*VbeFrameBuffer
;
2231 UINTN BytesPerScanLine
;
2236 UINT32 VbePixelWidth
;
2240 PciIo
= BiosVideoPrivate
->PciIo
;
2242 VbeFrameBuffer
= BiosVideoPrivate
->VbeFrameBuffer
;
2243 MemAddress
= Mode
->LinearFrameBuffer
;
2244 BytesPerScanLine
= Mode
->BytesPerScanLine
;
2245 VbePixelWidth
= Mode
->BitsPerPixel
/ 8;
2246 BltUint8
= (UINT8
*) BltBuffer
;
2247 TotalBytes
= Width
* VbePixelWidth
;
2249 if (((UINTN
) BltOperation
) >= EfiGraphicsOutputBltOperationMax
) {
2250 return EFI_INVALID_PARAMETER
;
2253 if (Width
== 0 || Height
== 0) {
2254 return EFI_INVALID_PARAMETER
;
2257 // We need to fill the Virtual Screen buffer with the blt data.
2258 // The virtual screen is upside down, as the first row is the bootom row of
2261 if (BltOperation
== EfiBltVideoToBltBuffer
) {
2263 // Video to BltBuffer: Source is Video, destination is BltBuffer
2265 if (SourceY
+ Height
> Mode
->VerticalResolution
) {
2266 return EFI_INVALID_PARAMETER
;
2269 if (SourceX
+ Width
> Mode
->HorizontalResolution
) {
2270 return EFI_INVALID_PARAMETER
;
2274 // BltBuffer to Video: Source is BltBuffer, destination is Video
2276 if (DestinationY
+ Height
> Mode
->VerticalResolution
) {
2277 return EFI_INVALID_PARAMETER
;
2280 if (DestinationX
+ Width
> Mode
->HorizontalResolution
) {
2281 return EFI_INVALID_PARAMETER
;
2285 // If Delta is zero, then the entire BltBuffer is being used, so Delta
2286 // is the number of bytes in each row of BltBuffer. Since BltBuffer is Width pixels size,
2287 // the number of bytes in each row can be computed.
2290 Delta
= Width
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
);
2293 // We have to raise to TPL Notify, so we make an atomic write the frame buffer.
2294 // We would not want a timer based event (Cursor, ...) to come in while we are
2295 // doing this operation.
2297 OriginalTPL
= gBS
->RaiseTPL (TPL_NOTIFY
);
2299 switch (BltOperation
) {
2300 case EfiBltVideoToBltBuffer
:
2301 for (SrcY
= SourceY
, DstY
= DestinationY
; DstY
< (Height
+ DestinationY
); SrcY
++, DstY
++) {
2302 Blt
= (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*) (BltUint8
+ DstY
* Delta
+ DestinationX
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
));
2304 // Shuffle the packed bytes in the hardware buffer to match EFI_GRAPHICS_OUTPUT_BLT_PIXEL
2306 VbeBuffer
= ((UINT8
*) VbeFrameBuffer
+ (SrcY
* BytesPerScanLine
+ SourceX
* VbePixelWidth
));
2307 for (DstX
= DestinationX
; DstX
< (Width
+ DestinationX
); DstX
++) {
2308 Pixel
= VbeBuffer
[0] | VbeBuffer
[1] << 8 | VbeBuffer
[2] << 16 | VbeBuffer
[3] << 24;
2309 Blt
->Red
= (UINT8
) ((Pixel
>> Mode
->Red
.Position
) & Mode
->Red
.Mask
);
2310 Blt
->Blue
= (UINT8
) ((Pixel
>> Mode
->Blue
.Position
) & Mode
->Blue
.Mask
);
2311 Blt
->Green
= (UINT8
) ((Pixel
>> Mode
->Green
.Position
) & Mode
->Green
.Mask
);
2314 VbeBuffer
+= VbePixelWidth
;
2320 case EfiBltVideoToVideo
:
2321 for (Index
= 0; Index
< Height
; Index
++) {
2322 if (DestinationY
<= SourceY
) {
2323 SrcY
= SourceY
+ Index
;
2324 DstY
= DestinationY
+ Index
;
2326 SrcY
= SourceY
+ Height
- Index
- 1;
2327 DstY
= DestinationY
+ Height
- Index
- 1;
2330 VbeBuffer
= ((UINT8
*) VbeFrameBuffer
+ DstY
* BytesPerScanLine
+ DestinationX
* VbePixelWidth
);
2331 VbeBuffer1
= ((UINT8
*) VbeFrameBuffer
+ SrcY
* BytesPerScanLine
+ SourceX
* VbePixelWidth
);
2340 // Update physical frame buffer.
2355 case EfiBltVideoFill
:
2356 VbeBuffer
= (UINT8
*) ((UINTN
) VbeFrameBuffer
+ (DestinationY
* BytesPerScanLine
) + DestinationX
* VbePixelWidth
);
2357 Blt
= (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*) BltUint8
;
2359 // Shuffle the RGB fields in EFI_GRAPHICS_OUTPUT_BLT_PIXEL to match the hardware buffer
2361 Pixel
= ((Blt
->Red
& Mode
->Red
.Mask
) << Mode
->Red
.Position
) |
2363 (Blt
->Green
& Mode
->Green
.Mask
) <<
2364 Mode
->Green
.Position
2366 ((Blt
->Blue
& Mode
->Blue
.Mask
) << Mode
->Blue
.Position
);
2368 for (Index
= 0; Index
< Width
; Index
++) {
2374 VbeBuffer
+= VbePixelWidth
;
2377 VbeBuffer
= (UINT8
*) ((UINTN
) VbeFrameBuffer
+ (DestinationY
* BytesPerScanLine
) + DestinationX
* VbePixelWidth
);
2378 for (DstY
= DestinationY
+ 1; DstY
< (Height
+ DestinationY
); DstY
++) {
2380 (VOID
*) ((UINTN
) VbeFrameBuffer
+ (DstY
* BytesPerScanLine
) + DestinationX
* VbePixelWidth
),
2386 for (DstY
= DestinationY
; DstY
< (Height
+ DestinationY
); DstY
++) {
2388 // Update physical frame buffer.
2403 case EfiBltBufferToVideo
:
2404 for (SrcY
= SourceY
, DstY
= DestinationY
; SrcY
< (Height
+ SourceY
); SrcY
++, DstY
++) {
2405 Blt
= (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*) (BltUint8
+ (SrcY
* Delta
) + (SourceX
) * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
));
2406 VbeBuffer
= ((UINT8
*) VbeFrameBuffer
+ (DstY
* BytesPerScanLine
+ DestinationX
* VbePixelWidth
));
2407 for (DstX
= DestinationX
; DstX
< (Width
+ DestinationX
); DstX
++) {
2409 // Shuffle the RGB fields in EFI_GRAPHICS_OUTPUT_BLT_PIXEL to match the hardware buffer
2411 Pixel
= ((Blt
->Red
& Mode
->Red
.Mask
) << Mode
->Red
.Position
) |
2412 ((Blt
->Green
& Mode
->Green
.Mask
) << Mode
->Green
.Position
) |
2413 ((Blt
->Blue
& Mode
->Blue
.Mask
) << Mode
->Blue
.Position
);
2420 VbeBuffer
+= VbePixelWidth
;
2423 VbeBuffer
= ((UINT8
*) VbeFrameBuffer
+ (DstY
* BytesPerScanLine
+ DestinationX
* VbePixelWidth
));
2426 // Update physical frame buffer.
2444 gBS
->RestoreTPL (OriginalTPL
);
2450 Graphics Output protocol instance to block transfer for VBE device.
2452 @param This Pointer to Graphics Output protocol instance
2453 @param BltBuffer The data to transfer to screen
2454 @param BltOperation The operation to perform
2455 @param SourceX The X coordinate of the source for BltOperation
2456 @param SourceY The Y coordinate of the source for BltOperation
2457 @param DestinationX The X coordinate of the destination for
2459 @param DestinationY The Y coordinate of the destination for
2461 @param Width The width of a rectangle in the blt rectangle in
2463 @param Height The height of a rectangle in the blt rectangle in
2465 @param Delta Not used for EfiBltVideoFill and
2466 EfiBltVideoToVideo operation. If a Delta of 0 is
2467 used, the entire BltBuffer will be operated on. If
2468 a subrectangle of the BltBuffer is used, then
2469 Delta represents the number of bytes in a row of
2472 @retval EFI_INVALID_PARAMETER Invalid parameter passed in
2473 @retval EFI_SUCCESS Blt operation success
2478 BiosVideoGraphicsOutputVbeBlt (
2479 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*This
,
2480 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
, OPTIONAL
2481 IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation
,
2484 IN UINTN DestinationX
,
2485 IN UINTN DestinationY
,
2491 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
2492 BIOS_VIDEO_MODE_DATA
*Mode
;
2495 return EFI_INVALID_PARAMETER
;
2498 BiosVideoPrivate
= BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (This
);
2499 Mode
= &BiosVideoPrivate
->ModeData
[This
->Mode
->Mode
];
2501 return BiosVideoVbeBltWorker (
2517 Write graphics controller registers.
2519 @param PciIo Pointer to PciIo protocol instance of the
2521 @param Address Register address
2522 @param Data Data to be written to register
2528 WriteGraphicsController (
2529 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
2534 Address
= Address
| (Data
<< 8);
2537 EfiPciIoWidthUint16
,
2538 EFI_PCI_IO_PASS_THROUGH_BAR
,
2539 VGA_GRAPHICS_CONTROLLER_ADDRESS_REGISTER
,
2547 Read the four bit plane of VGA frame buffer.
2549 @param PciIo Pointer to PciIo protocol instance of the
2551 @param HardwareBuffer Hardware VGA frame buffer address
2552 @param MemoryBuffer Memory buffer address
2553 @param WidthInBytes Number of bytes in a line to read
2554 @param Height Height of the area to read
2561 EFI_PCI_IO_PROTOCOL
*PciIo
,
2562 UINT8
*HardwareBuffer
,
2563 UINT8
*MemoryBuffer
,
2570 UINTN FrameBufferOffset
;
2575 // Program the Mode Register Write mode 0, Read mode 0
2577 WriteGraphicsController (
2579 VGA_GRAPHICS_CONTROLLER_MODE_REGISTER
,
2580 VGA_GRAPHICS_CONTROLLER_READ_MODE_0
| VGA_GRAPHICS_CONTROLLER_WRITE_MODE_0
2583 for (BitPlane
= 0, FrameBufferOffset
= 0;
2584 BitPlane
< VGA_NUMBER_OF_BIT_PLANES
;
2585 BitPlane
++, FrameBufferOffset
+= VGA_BYTES_PER_BIT_PLANE
2588 // Program the Read Map Select Register to select the correct bit plane
2590 WriteGraphicsController (
2592 VGA_GRAPHICS_CONTROLLER_READ_MAP_SELECT_REGISTER
,
2596 Source
= HardwareBuffer
;
2597 Destination
= MemoryBuffer
+ FrameBufferOffset
;
2599 for (Rows
= 0; Rows
< Height
; Rows
++, Source
+= VGA_BYTES_PER_SCAN_LINE
, Destination
+= VGA_BYTES_PER_SCAN_LINE
) {
2603 EFI_PCI_IO_PASS_THROUGH_BAR
,
2604 (UINT64
) (UINTN
) Source
,
2606 (VOID
*) Destination
2614 Internal routine to convert VGA color to Grahpics Output color.
2616 @param MemoryBuffer Buffer containing VGA color
2617 @param CoordinateX The X coordinate of pixel on screen
2618 @param CoordinateY The Y coordinate of pixel on screen
2619 @param BltBuffer Buffer to contain converted Grahpics Output color
2625 VgaConvertToGraphicsOutputColor (
2626 UINT8
*MemoryBuffer
,
2629 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
2636 MemoryBuffer
+= ((CoordinateY
<< 6) + (CoordinateY
<< 4) + (CoordinateX
>> 3));
2637 Mask
= mVgaBitMaskTable
[CoordinateX
& 0x07];
2638 for (Bit
= 0x01, Color
= 0; Bit
< 0x10; Bit
<<= 1, MemoryBuffer
+= VGA_BYTES_PER_BIT_PLANE
) {
2639 if ((*MemoryBuffer
& Mask
) != 0) {
2644 *BltBuffer
= mVgaColorToGraphicsOutputColor
[Color
];
2648 Internal routine to convert Grahpics Output color to VGA color.
2650 @param BltBuffer buffer containing Grahpics Output color
2652 @return Converted VGA color
2657 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
2662 Color
= (UINT8
) ((BltBuffer
->Blue
>> 7) | ((BltBuffer
->Green
>> 6) & 0x02) | ((BltBuffer
->Red
>> 5) & 0x04));
2663 if ((BltBuffer
->Red
+ BltBuffer
->Green
+ BltBuffer
->Blue
) > 0x180) {
2672 Grahpics Output protocol instance to block transfer for VGA device.
2674 @param This Pointer to Grahpics Output protocol instance
2675 @param BltBuffer The data to transfer to screen
2676 @param BltOperation The operation to perform
2677 @param SourceX The X coordinate of the source for BltOperation
2678 @param SourceY The Y coordinate of the source for BltOperation
2679 @param DestinationX The X coordinate of the destination for
2681 @param DestinationY The Y coordinate of the destination for
2683 @param Width The width of a rectangle in the blt rectangle in
2685 @param Height The height of a rectangle in the blt rectangle in
2687 @param Delta Not used for EfiBltVideoFill and
2688 EfiBltVideoToVideo operation. If a Delta of 0 is
2689 used, the entire BltBuffer will be operated on. If
2690 a subrectangle of the BltBuffer is used, then
2691 Delta represents the number of bytes in a row of
2694 @retval EFI_INVALID_PARAMETER Invalid parameter passed in
2695 @retval EFI_SUCCESS Blt operation success
2700 BiosVideoGraphicsOutputVgaBlt (
2701 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*This
,
2702 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
, OPTIONAL
2703 IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation
,
2706 IN UINTN DestinationX
,
2707 IN UINTN DestinationY
,
2713 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
2714 EFI_TPL OriginalTPL
;
2716 UINTN BytesPerScanLine
;
2728 UINT8
*SourceAddress
;
2729 UINT8
*DestinationAddress
;
2730 EFI_PCI_IO_PROTOCOL
*PciIo
;
2733 UINT8
*VgaFrameBuffer
;
2742 if (This
== NULL
|| ((UINTN
) BltOperation
) >= EfiGraphicsOutputBltOperationMax
) {
2743 return EFI_INVALID_PARAMETER
;
2746 BiosVideoPrivate
= BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (This
);
2748 CurrentMode
= This
->Mode
->Mode
;
2749 PciIo
= BiosVideoPrivate
->PciIo
;
2750 MemAddress
= BiosVideoPrivate
->ModeData
[CurrentMode
].LinearFrameBuffer
;
2751 BytesPerScanLine
= BiosVideoPrivate
->ModeData
[CurrentMode
].BytesPerScanLine
>> 3;
2752 VgaFrameBuffer
= BiosVideoPrivate
->VgaFrameBuffer
;
2755 if (Width
== 0 || Height
== 0) {
2756 return EFI_INVALID_PARAMETER
;
2759 // We need to fill the Virtual Screen buffer with the blt data.
2760 // The virtual screen is upside down, as the first row is the bootom row of
2763 if (BltOperation
== EfiBltVideoToBltBuffer
) {
2765 // Video to BltBuffer: Source is Video, destination is BltBuffer
2767 if (SourceY
+ Height
> BiosVideoPrivate
->ModeData
[CurrentMode
].VerticalResolution
) {
2768 return EFI_INVALID_PARAMETER
;
2771 if (SourceX
+ Width
> BiosVideoPrivate
->ModeData
[CurrentMode
].HorizontalResolution
) {
2772 return EFI_INVALID_PARAMETER
;
2776 // BltBuffer to Video: Source is BltBuffer, destination is Video
2778 if (DestinationY
+ Height
> BiosVideoPrivate
->ModeData
[CurrentMode
].VerticalResolution
) {
2779 return EFI_INVALID_PARAMETER
;
2782 if (DestinationX
+ Width
> BiosVideoPrivate
->ModeData
[CurrentMode
].HorizontalResolution
) {
2783 return EFI_INVALID_PARAMETER
;
2787 // If Delta is zero, then the entire BltBuffer is being used, so Delta
2788 // is the number of bytes in each row of BltBuffer. Since BltBuffer is Width pixels size,
2789 // the number of bytes in each row can be computed.
2792 Delta
= Width
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
);
2795 // We have to raise to TPL Notify, so we make an atomic write the frame buffer.
2796 // We would not want a timer based event (Cursor, ...) to come in while we are
2797 // doing this operation.
2799 OriginalTPL
= gBS
->RaiseTPL (TPL_NOTIFY
);
2802 // Compute some values we need for VGA
2804 switch (BltOperation
) {
2805 case EfiBltVideoToBltBuffer
:
2807 SourceOffset
= (SourceY
<< 6) + (SourceY
<< 4) + (SourceX
>> 3);
2808 SourceWidth
= ((SourceX
+ Width
- 1) >> 3) - (SourceX
>> 3) + 1;
2811 // Read all the pixels in the 4 bit planes into a memory buffer that looks like the VGA buffer
2815 MemAddress
+ SourceOffset
,
2816 VgaFrameBuffer
+ SourceOffset
,
2822 // Convert VGA Bit Planes to a Graphics Output 32-bit color value
2824 BltBuffer
+= (DestinationY
* (Delta
>> 2) + DestinationX
);
2825 for (Rows
= 0, CoordinateY
= SourceY
; Rows
< Height
; Rows
++, CoordinateY
++, BltBuffer
+= (Delta
>> 2)) {
2826 for (Columns
= 0, CoordinateX
= SourceX
; Columns
< Width
; Columns
++, CoordinateX
++, BltBuffer
++) {
2827 VgaConvertToGraphicsOutputColor (VgaFrameBuffer
, CoordinateX
, CoordinateY
, BltBuffer
);
2835 case EfiBltVideoToVideo
:
2837 // Check for an aligned Video to Video operation
2839 if ((SourceX
& 0x07) == 0x00 && (DestinationX
& 0x07) == 0x00 && (Width
& 0x07) == 0x00) {
2841 // Program the Mode Register Write mode 1, Read mode 0
2843 WriteGraphicsController (
2845 VGA_GRAPHICS_CONTROLLER_MODE_REGISTER
,
2846 VGA_GRAPHICS_CONTROLLER_READ_MODE_0
| VGA_GRAPHICS_CONTROLLER_WRITE_MODE_1
2849 SourceAddress
= (UINT8
*) (MemAddress
+ (SourceY
<< 6) + (SourceY
<< 4) + (SourceX
>> 3));
2850 DestinationAddress
= (UINT8
*) (MemAddress
+ (DestinationY
<< 6) + (DestinationY
<< 4) + (DestinationX
>> 3));
2852 for (Index
= 0, Offset
= 0; Index
< Height
; Index
++, Offset
+= BytesPerScanLine
) {
2856 EFI_PCI_IO_PASS_THROUGH_BAR
,
2857 (UINT64
) (UINTN
) (DestinationAddress
+ Offset
),
2858 EFI_PCI_IO_PASS_THROUGH_BAR
,
2859 (UINT64
) (UINTN
) (SourceAddress
+ Offset
),
2864 SourceOffset
= (SourceY
<< 6) + (SourceY
<< 4) + (SourceX
>> 3);
2865 SourceWidth
= ((SourceX
+ Width
- 1) >> 3) - (SourceX
>> 3) + 1;
2868 // Read all the pixels in the 4 bit planes into a memory buffer that looks like the VGA buffer
2872 MemAddress
+ SourceOffset
,
2873 VgaFrameBuffer
+ SourceOffset
,
2881 case EfiBltVideoFill
:
2882 StartAddress
= (UINTN
) (MemAddress
+ (DestinationY
<< 6) + (DestinationY
<< 4) + (DestinationX
>> 3));
2883 Bytes
= ((DestinationX
+ Width
- 1) >> 3) - (DestinationX
>> 3);
2884 LeftMask
= mVgaLeftMaskTable
[DestinationX
& 0x07];
2885 RightMask
= mVgaRightMaskTable
[(DestinationX
+ Width
- 1) & 0x07];
2887 LeftMask
= (UINT8
) (LeftMask
& RightMask
);
2891 if (LeftMask
== 0xff) {
2897 if (RightMask
== 0xff) {
2902 PixelColor
= VgaConvertColor (BltBuffer
);
2905 // Program the Mode Register Write mode 2, Read mode 0
2907 WriteGraphicsController (
2909 VGA_GRAPHICS_CONTROLLER_MODE_REGISTER
,
2910 VGA_GRAPHICS_CONTROLLER_READ_MODE_0
| VGA_GRAPHICS_CONTROLLER_WRITE_MODE_2
2914 // Program the Data Rotate/Function Select Register to replace
2916 WriteGraphicsController (
2918 VGA_GRAPHICS_CONTROLLER_DATA_ROTATE_REGISTER
,
2919 VGA_GRAPHICS_CONTROLLER_FUNCTION_REPLACE
2922 if (LeftMask
!= 0) {
2924 // Program the BitMask register with the Left column mask
2926 WriteGraphicsController (
2928 VGA_GRAPHICS_CONTROLLER_BIT_MASK_REGISTER
,
2932 for (Index
= 0, Address
= StartAddress
; Index
< Height
; Index
++, Address
+= BytesPerScanLine
) {
2934 // Read data from the bit planes into the latches
2939 EFI_PCI_IO_PASS_THROUGH_BAR
,
2940 (UINT64
) (UINTN
) Address
,
2945 // Write the lower 4 bits of PixelColor to the bit planes in the pixels enabled by BitMask
2950 EFI_PCI_IO_PASS_THROUGH_BAR
,
2951 (UINT64
) (UINTN
) Address
,
2960 // Program the BitMask register with the middle column mask of 0xff
2962 WriteGraphicsController (
2964 VGA_GRAPHICS_CONTROLLER_BIT_MASK_REGISTER
,
2968 for (Index
= 0, Address
= StartAddress
+ 1; Index
< Height
; Index
++, Address
+= BytesPerScanLine
) {
2971 EfiPciIoWidthFillUint8
,
2972 EFI_PCI_IO_PASS_THROUGH_BAR
,
2973 (UINT64
) (UINTN
) Address
,
2980 if (RightMask
!= 0) {
2982 // Program the BitMask register with the Right column mask
2984 WriteGraphicsController (
2986 VGA_GRAPHICS_CONTROLLER_BIT_MASK_REGISTER
,
2990 for (Index
= 0, Address
= StartAddress
+ Bytes
; Index
< Height
; Index
++, Address
+= BytesPerScanLine
) {
2992 // Read data from the bit planes into the latches
2997 EFI_PCI_IO_PASS_THROUGH_BAR
,
2998 (UINT64
) (UINTN
) Address
,
3003 // Write the lower 4 bits of PixelColor to the bit planes in the pixels enabled by BitMask
3008 EFI_PCI_IO_PASS_THROUGH_BAR
,
3009 (UINT64
) (UINTN
) Address
,
3017 case EfiBltBufferToVideo
:
3018 StartAddress
= (UINTN
) (MemAddress
+ (DestinationY
<< 6) + (DestinationY
<< 4) + (DestinationX
>> 3));
3019 LeftMask
= mVgaBitMaskTable
[DestinationX
& 0x07];
3022 // Program the Mode Register Write mode 2, Read mode 0
3024 WriteGraphicsController (
3026 VGA_GRAPHICS_CONTROLLER_MODE_REGISTER
,
3027 VGA_GRAPHICS_CONTROLLER_READ_MODE_0
| VGA_GRAPHICS_CONTROLLER_WRITE_MODE_2
3031 // Program the Data Rotate/Function Select Register to replace
3033 WriteGraphicsController (
3035 VGA_GRAPHICS_CONTROLLER_DATA_ROTATE_REGISTER
,
3036 VGA_GRAPHICS_CONTROLLER_FUNCTION_REPLACE
3039 for (Index
= 0, Address
= StartAddress
; Index
< Height
; Index
++, Address
+= BytesPerScanLine
) {
3040 for (Index1
= 0; Index1
< Width
; Index1
++) {
3041 BiosVideoPrivate
->LineBuffer
[Index1
] = VgaConvertColor (&BltBuffer
[(SourceY
+ Index
) * (Delta
>> 2) + SourceX
+ Index1
]);
3043 AddressFix
= Address
;
3045 for (Bit
= 0; Bit
< 8; Bit
++) {
3047 // Program the BitMask register with the Left column mask
3049 WriteGraphicsController (
3051 VGA_GRAPHICS_CONTROLLER_BIT_MASK_REGISTER
,
3055 for (Index1
= Bit
, Address1
= (UINT8
*) AddressFix
; Index1
< Width
; Index1
+= 8, Address1
++) {
3057 // Read data from the bit planes into the latches
3062 EFI_PCI_IO_PASS_THROUGH_BAR
,
3063 (UINT64
) (UINTN
) Address1
,
3071 EFI_PCI_IO_PASS_THROUGH_BAR
,
3072 (UINT64
) (UINTN
) Address1
,
3074 &BiosVideoPrivate
->LineBuffer
[Index1
]
3078 LeftMask
= (UINT8
) (LeftMask
>> 1);
3079 if (LeftMask
== 0) {
3091 gBS
->RestoreTPL (OriginalTPL
);
3097 // VGA Mini Port Protocol Functions
3101 VgaMiniPort protocol interface to set mode.
3103 @param This Pointer to VgaMiniPort protocol instance
3104 @param ModeNumber The index of the mode
3106 @retval EFI_UNSUPPORTED The requested mode is not supported
3107 @retval EFI_SUCCESS The requested mode is set successfully
3112 BiosVideoVgaMiniPortSetMode (
3113 IN EFI_VGA_MINI_PORT_PROTOCOL
*This
,
3117 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
3118 EFI_IA32_REGISTER_SET Regs
;
3121 return EFI_INVALID_PARAMETER
;
3125 // Make sure the ModeNumber is a valid value
3127 if (ModeNumber
>= This
->MaxMode
) {
3128 return EFI_UNSUPPORTED
;
3131 // Get the device structure for this device
3133 BiosVideoPrivate
= BIOS_VIDEO_DEV_FROM_VGA_MINI_PORT_THIS (This
);
3135 switch (ModeNumber
) {
3138 // Set the 80x25 Text VGA Mode
3142 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3147 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3152 // Set the 80x50 Text VGA Mode
3156 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3160 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3164 return EFI_UNSUPPORTED
;
3171 Event handler for Exit Boot Service.
3173 @param Event The event that be siganlled when exiting boot service.
3174 @param Context Pointer to instance of BIOS_VIDEO_DEV.
3179 BiosVideoNotifyExitBootServices (
3184 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
3185 EFI_IA32_REGISTER_SET Regs
;
3187 BiosVideoPrivate
= (BIOS_VIDEO_DEV
*)Context
;
3190 // Set the 80x25 Text VGA Mode
3194 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3198 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3203 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3207 The user Entry Point for module UefiBiosVideo. The user code starts with this function.
3209 @param[in] ImageHandle The firmware allocated handle for the EFI image.
3210 @param[in] SystemTable A pointer to the EFI System Table.
3212 @retval EFI_SUCCESS The entry point is executed successfully.
3213 @retval other Some error occurs when executing this entry point.
3218 BiosVideoEntryPoint(
3219 IN EFI_HANDLE ImageHandle
,
3220 IN EFI_SYSTEM_TABLE
*SystemTable
3226 // Install driver model protocol(s).
3228 Status
= EfiLibInstallDriverBindingComponentName2 (
3231 &gBiosVideoDriverBinding
,
3233 &gBiosVideoComponentName
,
3234 &gBiosVideoComponentName2
3236 ASSERT_EFI_ERROR (Status
);
3239 // Install Legacy BIOS GUID to mark this driver as a BIOS Thunk Driver
3241 return gBS
->InstallMultipleProtocolInterfaces (
3243 &gEfiLegacyBiosGuid
,