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
;
1265 UINT16 VbeModeNumber
;
1268 BIOS_VIDEO_MODE_DATA
*ModeBuffer
;
1269 BIOS_VIDEO_MODE_DATA
*CurrentModeData
;
1272 VESA_BIOS_EXTENSIONS_EDID_TIMING Timing
;
1273 VESA_BIOS_EXTENSIONS_VALID_EDID_TIMING ValidEdidTiming
;
1274 EFI_EDID_OVERRIDE_PROTOCOL
*EdidOverride
;
1275 UINT32 EdidAttributes
;
1276 BOOLEAN EdidOverrideFound
;
1277 UINTN EdidOverrideDataSize
;
1278 UINT8
*EdidOverrideDataBlock
;
1279 UINTN EdidActiveDataSize
;
1280 UINT8
*EdidActiveDataBlock
;
1281 UINT32 HighestHorizontalResolution
;
1282 UINT32 HighestVerticalResolution
;
1283 UINTN HighestResolutionMode
;
1286 EdidOverrideFound
= FALSE
;
1287 EdidOverrideDataBlock
= NULL
;
1288 EdidActiveDataSize
= 0;
1289 EdidActiveDataBlock
= NULL
;
1290 HighestHorizontalResolution
= 0;
1291 HighestVerticalResolution
= 0;
1292 HighestResolutionMode
= 0;
1295 // Allocate buffer under 1MB for VBE data structures
1297 BiosVideoPrivate
->NumberOfPagesBelow1MB
= EFI_SIZE_TO_PAGES (
1298 sizeof (VESA_BIOS_EXTENSIONS_INFORMATION_BLOCK
) +
1299 sizeof (VESA_BIOS_EXTENSIONS_MODE_INFORMATION_BLOCK
) +
1300 sizeof (VESA_BIOS_EXTENSIONS_EDID_DATA_BLOCK
) +
1301 sizeof (VESA_BIOS_EXTENSIONS_CRTC_INFORMATION_BLOCK
)
1304 BiosVideoPrivate
->PagesBelow1MB
= 0x00100000 - 1;
1306 Status
= gBS
->AllocatePages (
1308 EfiBootServicesData
,
1309 BiosVideoPrivate
->NumberOfPagesBelow1MB
,
1310 &BiosVideoPrivate
->PagesBelow1MB
1312 if (EFI_ERROR (Status
)) {
1316 ZeroMem (&ValidEdidTiming
, sizeof (VESA_BIOS_EXTENSIONS_VALID_EDID_TIMING
));
1319 // Fill in the VBE related data structures
1321 BiosVideoPrivate
->VbeInformationBlock
= (VESA_BIOS_EXTENSIONS_INFORMATION_BLOCK
*) (UINTN
) (BiosVideoPrivate
->PagesBelow1MB
);
1322 BiosVideoPrivate
->VbeModeInformationBlock
= (VESA_BIOS_EXTENSIONS_MODE_INFORMATION_BLOCK
*) (BiosVideoPrivate
->VbeInformationBlock
+ 1);
1323 BiosVideoPrivate
->VbeEdidDataBlock
= (VESA_BIOS_EXTENSIONS_EDID_DATA_BLOCK
*) (BiosVideoPrivate
->VbeModeInformationBlock
+ 1);
1324 BiosVideoPrivate
->VbeCrtcInformationBlock
= (VESA_BIOS_EXTENSIONS_CRTC_INFORMATION_BLOCK
*) (BiosVideoPrivate
->VbeEdidDataBlock
+ 1);
1325 BiosVideoPrivate
->VbeSaveRestorePages
= 0;
1326 BiosVideoPrivate
->VbeSaveRestoreBuffer
= 0;
1329 // Test to see if the Video Adapter is compliant with VBE 3.0
1331 gBS
->SetMem (&Regs
, sizeof (Regs
), 0);
1332 Regs
.X
.AX
= VESA_BIOS_EXTENSIONS_RETURN_CONTROLLER_INFORMATION
;
1333 gBS
->SetMem (BiosVideoPrivate
->VbeInformationBlock
, sizeof (VESA_BIOS_EXTENSIONS_INFORMATION_BLOCK
), 0);
1334 BiosVideoPrivate
->VbeInformationBlock
->VESASignature
= VESA_BIOS_EXTENSIONS_VBE2_SIGNATURE
;
1335 Regs
.X
.ES
= EFI_SEGMENT ((UINTN
) BiosVideoPrivate
->VbeInformationBlock
);
1336 Regs
.X
.DI
= EFI_OFFSET ((UINTN
) BiosVideoPrivate
->VbeInformationBlock
);
1338 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
1340 Status
= EFI_DEVICE_ERROR
;
1343 // See if the VESA call succeeded
1345 if (Regs
.X
.AX
!= VESA_BIOS_EXTENSIONS_STATUS_SUCCESS
) {
1349 // Check for 'VESA' signature
1351 if (BiosVideoPrivate
->VbeInformationBlock
->VESASignature
!= VESA_BIOS_EXTENSIONS_VESA_SIGNATURE
) {
1355 // Check to see if this is VBE 2.0 or higher
1357 if (BiosVideoPrivate
->VbeInformationBlock
->VESAVersion
< VESA_BIOS_EXTENSIONS_VERSION_2_0
) {
1362 EdidAttributes
= 0xff;
1363 EdidOverrideDataSize
= 0;
1366 // Find EDID Override protocol firstly, this protocol is installed by platform if needed.
1368 Status
= gBS
->LocateProtocol (
1369 &gEfiEdidOverrideProtocolGuid
,
1371 (VOID
**) &EdidOverride
1373 if (!EFI_ERROR (Status
)) {
1375 // Allocate double size of VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE to avoid overflow
1377 EdidOverrideDataBlock
= AllocatePool (VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE
* 2);
1378 if (NULL
== EdidOverrideDataBlock
) {
1379 Status
= EFI_OUT_OF_RESOURCES
;
1383 Status
= EdidOverride
->GetEdid (
1385 BiosVideoPrivate
->Handle
,
1387 &EdidOverrideDataSize
,
1388 (UINT8
**) &EdidOverrideDataBlock
1390 if (!EFI_ERROR (Status
) &&
1391 EdidAttributes
== 0 &&
1392 EdidOverrideDataSize
!= 0) {
1394 // Succeeded to get EDID Override Data
1396 EdidOverrideFound
= TRUE
;
1400 if (!EdidOverrideFound
|| EdidAttributes
== EFI_EDID_OVERRIDE_DONT_OVERRIDE
) {
1402 // If EDID Override data doesn't exist or EFI_EDID_OVERRIDE_DONT_OVERRIDE returned,
1403 // read EDID information through INT10 call
1406 gBS
->SetMem (&Regs
, sizeof (Regs
), 0);
1407 Regs
.X
.AX
= VESA_BIOS_EXTENSIONS_EDID
;
1411 Regs
.X
.ES
= EFI_SEGMENT ((UINTN
) BiosVideoPrivate
->VbeEdidDataBlock
);
1412 Regs
.X
.DI
= EFI_OFFSET ((UINTN
) BiosVideoPrivate
->VbeEdidDataBlock
);
1414 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
1416 // See if the VESA call succeeded
1418 if (Regs
.X
.AX
== VESA_BIOS_EXTENSIONS_STATUS_SUCCESS
) {
1420 // Set EDID Discovered Data
1422 BiosVideoPrivate
->EdidDiscovered
.SizeOfEdid
= VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE
;
1423 BiosVideoPrivate
->EdidDiscovered
.Edid
= (UINT8
*) AllocateCopyPool (
1424 VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE
,
1425 BiosVideoPrivate
->VbeEdidDataBlock
1428 if (NULL
== BiosVideoPrivate
->EdidDiscovered
.Edid
) {
1429 Status
= EFI_OUT_OF_RESOURCES
;
1438 EdidActiveDataSize
= VESA_BIOS_EXTENSIONS_EDID_BLOCK_SIZE
;
1439 EdidActiveDataBlock
= BiosVideoPrivate
->EdidDiscovered
.Edid
;
1440 } else if (EdidOverrideFound
) {
1441 EdidActiveDataSize
= EdidOverrideDataSize
;
1442 EdidActiveDataBlock
= EdidOverrideDataBlock
;
1448 // Parse EDID data structure to retrieve modes supported by monitor
1450 if (ParseEdidData ((UINT8
*) EdidActiveDataBlock
, &ValidEdidTiming
)) {
1452 // Copy EDID Override Data to EDID Active Data
1454 BiosVideoPrivate
->EdidActive
.SizeOfEdid
= (UINT32
) EdidActiveDataSize
;
1455 BiosVideoPrivate
->EdidActive
.Edid
= (UINT8
*) AllocateCopyPool (
1459 if (NULL
== BiosVideoPrivate
->EdidActive
.Edid
) {
1460 Status
= EFI_OUT_OF_RESOURCES
;
1465 BiosVideoPrivate
->EdidActive
.SizeOfEdid
= 0;
1466 BiosVideoPrivate
->EdidActive
.Edid
= NULL
;
1471 // Walk through the mode list to see if there is at least one mode the is compatible with the EDID mode
1473 ModeNumberPtr
= (UINT16
*)
1475 (((UINTN
) BiosVideoPrivate
->VbeInformationBlock
->VideoModePtr
& 0xffff0000) >> 12) |
1476 ((UINTN
) BiosVideoPrivate
->VbeInformationBlock
->VideoModePtr
& 0x0000ffff)
1483 // ModeNumberPtr may be not 16-byte aligned, so ReadUnaligned16 is used to access the buffer pointed by ModeNumberPtr.
1485 for (VbeModeNumber
= ReadUnaligned16 (ModeNumberPtr
);
1486 VbeModeNumber
!= VESA_BIOS_EXTENSIONS_END_OF_MODE_LIST
;
1487 VbeModeNumber
= ReadUnaligned16 (++ModeNumberPtr
)) {
1489 // Make sure this is a mode number defined by the VESA VBE specification. If it isn'tm then skip this mode number.
1491 if ((VbeModeNumber
& VESA_BIOS_EXTENSIONS_MODE_NUMBER_VESA
) == 0) {
1495 // Get the information about the mode
1497 gBS
->SetMem (&Regs
, sizeof (Regs
), 0);
1498 Regs
.X
.AX
= VESA_BIOS_EXTENSIONS_RETURN_MODE_INFORMATION
;
1499 Regs
.X
.CX
= VbeModeNumber
;
1500 gBS
->SetMem (BiosVideoPrivate
->VbeModeInformationBlock
, sizeof (VESA_BIOS_EXTENSIONS_MODE_INFORMATION_BLOCK
), 0);
1501 Regs
.X
.ES
= EFI_SEGMENT ((UINTN
) BiosVideoPrivate
->VbeModeInformationBlock
);
1502 Regs
.X
.DI
= EFI_OFFSET ((UINTN
) BiosVideoPrivate
->VbeModeInformationBlock
);
1504 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
1507 // See if the call succeeded. If it didn't, then try the next mode.
1509 if (Regs
.X
.AX
!= VESA_BIOS_EXTENSIONS_STATUS_SUCCESS
) {
1513 // See if the mode supports color. If it doesn't then try the next mode.
1515 if ((BiosVideoPrivate
->VbeModeInformationBlock
->ModeAttributes
& VESA_BIOS_EXTENSIONS_MODE_ATTRIBUTE_COLOR
) == 0) {
1519 // See if the mode supports graphics. If it doesn't then try the next mode.
1521 if ((BiosVideoPrivate
->VbeModeInformationBlock
->ModeAttributes
& VESA_BIOS_EXTENSIONS_MODE_ATTRIBUTE_GRAPHICS
) == 0) {
1525 // See if the mode supports a linear frame buffer. If it doesn't then try the next mode.
1527 if ((BiosVideoPrivate
->VbeModeInformationBlock
->ModeAttributes
& VESA_BIOS_EXTENSIONS_MODE_ATTRIBUTE_LINEAR_FRAME_BUFFER
) == 0) {
1531 // See if the mode supports 32 bit color. If it doesn't then try the next mode.
1532 // 32 bit mode can be implemented by 24 Bits Per Pixels. Also make sure the
1533 // number of bits per pixel is a multiple of 8 or more than 32 bits per pixel
1535 if (BiosVideoPrivate
->VbeModeInformationBlock
->BitsPerPixel
< 24) {
1539 if (BiosVideoPrivate
->VbeModeInformationBlock
->BitsPerPixel
> 32) {
1543 if ((BiosVideoPrivate
->VbeModeInformationBlock
->BitsPerPixel
% 8) != 0) {
1547 // See if the physical base pointer for the linear mode is valid. If it isn't then try the next mode.
1549 if (BiosVideoPrivate
->VbeModeInformationBlock
->PhysBasePtr
== 0) {
1553 if (EdidFound
&& (ValidEdidTiming
.ValidNumber
> 0)) {
1555 // EDID exist, check whether this mode match with any mode in EDID
1557 Timing
.HorizontalResolution
= BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
;
1558 Timing
.VerticalResolution
= BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
;
1559 if (!SearchEdidTiming (&ValidEdidTiming
, &Timing
)) {
1565 // Select a reasonable mode to be set for current display mode
1569 if (BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
== 1024 &&
1570 BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
== 768
1574 if (BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
== 800 &&
1575 BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
== 600
1578 PreferMode
= ModeNumber
;
1580 if (BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
== 640 &&
1581 BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
== 480
1586 if ((!EdidFound
) && (!ModeFound
)) {
1588 // When no EDID exist, only select three possible resolutions, i.e. 1024x768, 800x600, 640x480
1594 // Record the highest resolution mode to set later
1596 if ((BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
>= HighestHorizontalResolution
) &&
1597 (BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
>= HighestVerticalResolution
)) {
1598 HighestHorizontalResolution
= BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
;
1599 HighestVerticalResolution
= BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
;
1600 HighestResolutionMode
= ModeNumber
;
1604 // Add mode to the list of available modes
1607 ModeBuffer
= (BIOS_VIDEO_MODE_DATA
*) AllocatePool (
1608 ModeNumber
* sizeof (BIOS_VIDEO_MODE_DATA
)
1610 if (NULL
== ModeBuffer
) {
1611 Status
= EFI_OUT_OF_RESOURCES
;
1615 if (ModeNumber
> 1) {
1618 BiosVideoPrivate
->ModeData
,
1619 (ModeNumber
- 1) * sizeof (BIOS_VIDEO_MODE_DATA
)
1623 if (BiosVideoPrivate
->ModeData
!= NULL
) {
1624 FreePool (BiosVideoPrivate
->ModeData
);
1627 CurrentModeData
= &ModeBuffer
[ModeNumber
- 1];
1628 CurrentModeData
->VbeModeNumber
= VbeModeNumber
;
1629 if (BiosVideoPrivate
->VbeInformationBlock
->VESAVersion
>= VESA_BIOS_EXTENSIONS_VERSION_3_0
) {
1630 CurrentModeData
->BytesPerScanLine
= BiosVideoPrivate
->VbeModeInformationBlock
->LinBytesPerScanLine
;
1631 CurrentModeData
->Red
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->LinRedFieldPosition
;
1632 CurrentModeData
->Red
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->LinRedMaskSize
) - 1);
1633 CurrentModeData
->Blue
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->LinBlueFieldPosition
;
1634 CurrentModeData
->Blue
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->LinBlueMaskSize
) - 1);
1635 CurrentModeData
->Green
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->LinGreenFieldPosition
;
1636 CurrentModeData
->Green
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->LinGreenMaskSize
) - 1);
1637 CurrentModeData
->Reserved
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->LinRsvdFieldPosition
;
1638 CurrentModeData
->Reserved
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->LinRsvdMaskSize
) - 1);
1640 CurrentModeData
->BytesPerScanLine
= BiosVideoPrivate
->VbeModeInformationBlock
->BytesPerScanLine
;
1641 CurrentModeData
->Red
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->RedFieldPosition
;
1642 CurrentModeData
->Red
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->RedMaskSize
) - 1);
1643 CurrentModeData
->Blue
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->BlueFieldPosition
;
1644 CurrentModeData
->Blue
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->BlueMaskSize
) - 1);
1645 CurrentModeData
->Green
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->GreenFieldPosition
;
1646 CurrentModeData
->Green
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->GreenMaskSize
) - 1);
1647 CurrentModeData
->Reserved
.Position
= BiosVideoPrivate
->VbeModeInformationBlock
->RsvdFieldPosition
;
1648 CurrentModeData
->Reserved
.Mask
= (UINT8
) ((1 << BiosVideoPrivate
->VbeModeInformationBlock
->RsvdMaskSize
) - 1);
1651 CurrentModeData
->PixelFormat
= PixelBitMask
;
1652 if ((BiosVideoPrivate
->VbeModeInformationBlock
->BitsPerPixel
== 32) &&
1653 (CurrentModeData
->Red
.Mask
== 0xff) && (CurrentModeData
->Green
.Mask
== 0xff) && (CurrentModeData
->Blue
.Mask
== 0xff)) {
1654 if ((CurrentModeData
->Red
.Position
== 0) && (CurrentModeData
->Green
.Position
== 8) && (CurrentModeData
->Blue
.Position
== 16)) {
1655 CurrentModeData
->PixelFormat
= PixelRedGreenBlueReserved8BitPerColor
;
1656 } else if ((CurrentModeData
->Blue
.Position
== 0) && (CurrentModeData
->Green
.Position
== 8) && (CurrentModeData
->Red
.Position
== 16)) {
1657 CurrentModeData
->PixelFormat
= PixelBlueGreenRedReserved8BitPerColor
;
1661 CurrentModeData
->PixelBitMask
.RedMask
= ((UINT32
) CurrentModeData
->Red
.Mask
) << CurrentModeData
->Red
.Position
;
1662 CurrentModeData
->PixelBitMask
.GreenMask
= ((UINT32
) CurrentModeData
->Green
.Mask
) << CurrentModeData
->Green
.Position
;
1663 CurrentModeData
->PixelBitMask
.BlueMask
= ((UINT32
) CurrentModeData
->Blue
.Mask
) << CurrentModeData
->Blue
.Position
;
1664 CurrentModeData
->PixelBitMask
.ReservedMask
= ((UINT32
) CurrentModeData
->Reserved
.Mask
) << CurrentModeData
->Reserved
.Position
;
1665 CurrentModeData
->FrameBufferSize
= BiosVideoPrivate
->VbeInformationBlock
->TotalMemory
* 64 * 1024;
1667 CurrentModeData
->LinearFrameBuffer
= (VOID
*) (UINTN
)BiosVideoPrivate
->VbeModeInformationBlock
->PhysBasePtr
;
1668 CurrentModeData
->HorizontalResolution
= BiosVideoPrivate
->VbeModeInformationBlock
->XResolution
;
1669 CurrentModeData
->VerticalResolution
= BiosVideoPrivate
->VbeModeInformationBlock
->YResolution
;
1671 CurrentModeData
->BitsPerPixel
= BiosVideoPrivate
->VbeModeInformationBlock
->BitsPerPixel
;
1673 BiosVideoPrivate
->ModeData
= ModeBuffer
;
1676 // Check to see if we found any modes that are compatible with GRAPHICS OUTPUT
1678 if (ModeNumber
== 0) {
1679 Status
= EFI_DEVICE_ERROR
;
1684 // Assign Gop's Blt function
1686 BiosVideoPrivate
->GraphicsOutput
.Blt
= BiosVideoGraphicsOutputVbeBlt
;
1688 BiosVideoPrivate
->GraphicsOutput
.Mode
->MaxMode
= (UINT32
) ModeNumber
;
1690 // Current mode is unknow till now, set it to an invalid mode.
1692 BiosVideoPrivate
->GraphicsOutput
.Mode
->Mode
= GRAPHICS_OUTPUT_INVALIDE_MODE_NUMBER
;
1695 // Find the best mode to initialize
1697 if ((PcdGet32 (PcdVideoHorizontalResolution
) == 0x0) || (PcdGet32 (PcdVideoVerticalResolution
) == 0x0)) {
1699 BIOS_VIDEO_MODE_DATA
*ModeData
;
1700 ModeData
= &BiosVideoPrivate
->ModeData
[HighestResolutionMode
];
1701 DEBUG ((EFI_D_INFO
, "BiosVideo set highest resolution %d x %d\n",
1702 ModeData
->HorizontalResolution
, ModeData
->VerticalResolution
));
1704 PreferMode
= HighestResolutionMode
;
1706 Status
= BiosVideoGraphicsOutputSetMode (&BiosVideoPrivate
->GraphicsOutput
, (UINT32
) PreferMode
);
1707 if (EFI_ERROR (Status
)) {
1708 for (PreferMode
= 0; PreferMode
< ModeNumber
; PreferMode
++) {
1709 Status
= BiosVideoGraphicsOutputSetMode (
1710 &BiosVideoPrivate
->GraphicsOutput
,
1713 if (!EFI_ERROR (Status
)) {
1717 if (PreferMode
== ModeNumber
) {
1719 // None mode is set successfully.
1727 // If there was an error, then free the mode structure
1729 if (EFI_ERROR (Status
)) {
1730 if (BiosVideoPrivate
->ModeData
!= NULL
) {
1731 FreePool (BiosVideoPrivate
->ModeData
);
1732 BiosVideoPrivate
->ModeData
= NULL
;
1733 BiosVideoPrivate
->MaxMode
= 0;
1735 if (EdidOverrideDataBlock
!= NULL
) {
1736 FreePool (EdidOverrideDataBlock
);
1745 Check for VGA device.
1747 @param BiosVideoPrivate Pointer to BIOS_VIDEO_DEV structure
1749 @retval EFI_SUCCESS Standard VGA device found
1753 BiosVideoCheckForVga (
1754 IN OUT BIOS_VIDEO_DEV
*BiosVideoPrivate
1758 BIOS_VIDEO_MODE_DATA
*ModeBuffer
;
1760 Status
= EFI_UNSUPPORTED
;
1763 // Assign Gop's Blt function
1765 BiosVideoPrivate
->GraphicsOutput
.Blt
= BiosVideoGraphicsOutputVgaBlt
;
1768 // Add mode to the list of available modes
1769 // caller should guarantee that Mode has been allocated.
1771 ASSERT (BiosVideoPrivate
->GraphicsOutput
.Mode
!= NULL
);
1772 BiosVideoPrivate
->GraphicsOutput
.Mode
->MaxMode
= 1;
1774 ModeBuffer
= (BIOS_VIDEO_MODE_DATA
*) AllocatePool (
1775 sizeof (BIOS_VIDEO_MODE_DATA
)
1777 if (NULL
== ModeBuffer
) {
1778 Status
= EFI_OUT_OF_RESOURCES
;
1782 ModeBuffer
->VbeModeNumber
= 0x0012;
1783 ModeBuffer
->BytesPerScanLine
= 640;
1784 ModeBuffer
->LinearFrameBuffer
= (VOID
*) (UINTN
) (0xa0000);
1785 ModeBuffer
->HorizontalResolution
= 640;
1786 ModeBuffer
->VerticalResolution
= 480;
1787 ModeBuffer
->PixelFormat
= PixelBltOnly
;
1788 ModeBuffer
->BitsPerPixel
= 8;
1789 ModeBuffer
->ColorDepth
= 32;
1790 ModeBuffer
->RefreshRate
= 60;
1792 BiosVideoPrivate
->ModeData
= ModeBuffer
;
1795 // Test to see if the Video Adapter support the 640x480 16 color mode
1797 BiosVideoPrivate
->GraphicsOutput
.Mode
->Mode
= GRAPHICS_OUTPUT_INVALIDE_MODE_NUMBER
;
1798 Status
= BiosVideoGraphicsOutputSetMode (&BiosVideoPrivate
->GraphicsOutput
, 0);
1802 // If there was an error, then free the mode structure
1804 if (EFI_ERROR (Status
)) {
1805 if (BiosVideoPrivate
->ModeData
!= NULL
) {
1806 FreePool (BiosVideoPrivate
->ModeData
);
1807 BiosVideoPrivate
->ModeData
= NULL
;
1809 if (BiosVideoPrivate
->GraphicsOutput
.Mode
!= NULL
) {
1810 if (BiosVideoPrivate
->GraphicsOutput
.Mode
->Info
!= NULL
) {
1811 FreePool (BiosVideoPrivate
->GraphicsOutput
.Mode
->Info
);
1812 BiosVideoPrivate
->GraphicsOutput
.Mode
->Info
= NULL
;
1814 FreePool (BiosVideoPrivate
->GraphicsOutput
.Mode
);
1815 BiosVideoPrivate
->GraphicsOutput
.Mode
= NULL
;
1822 // Graphics Output Protocol Member Functions for VESA BIOS Extensions
1826 Graphics Output protocol interface to get video mode.
1828 @param This Protocol instance pointer.
1829 @param ModeNumber The mode number to return information on.
1830 @param SizeOfInfo A pointer to the size, in bytes, of the Info
1832 @param Info Caller allocated buffer that returns information
1835 @retval EFI_SUCCESS Mode information returned.
1836 @retval EFI_DEVICE_ERROR A hardware error occurred trying to retrieve the
1838 @retval EFI_NOT_STARTED Video display is not initialized. Call SetMode ()
1839 @retval EFI_INVALID_PARAMETER One of the input args was NULL.
1844 BiosVideoGraphicsOutputQueryMode (
1845 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*This
,
1846 IN UINT32 ModeNumber
,
1847 OUT UINTN
*SizeOfInfo
,
1848 OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
**Info
1851 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
1852 BIOS_VIDEO_MODE_DATA
*ModeData
;
1854 BiosVideoPrivate
= BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (This
);
1856 if (BiosVideoPrivate
->HardwareNeedsStarting
) {
1857 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1858 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1859 EFI_PERIPHERAL_LOCAL_CONSOLE
| EFI_P_EC_OUTPUT_ERROR
,
1860 BiosVideoPrivate
->GopDevicePath
1862 return EFI_NOT_STARTED
;
1865 if (This
== NULL
|| Info
== NULL
|| SizeOfInfo
== NULL
|| ModeNumber
>= This
->Mode
->MaxMode
) {
1866 return EFI_INVALID_PARAMETER
;
1869 *Info
= (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*) AllocatePool (
1870 sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
)
1872 if (NULL
== *Info
) {
1873 return EFI_OUT_OF_RESOURCES
;
1876 *SizeOfInfo
= sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
);
1878 ModeData
= &BiosVideoPrivate
->ModeData
[ModeNumber
];
1879 (*Info
)->Version
= 0;
1880 (*Info
)->HorizontalResolution
= ModeData
->HorizontalResolution
;
1881 (*Info
)->VerticalResolution
= ModeData
->VerticalResolution
;
1882 (*Info
)->PixelFormat
= ModeData
->PixelFormat
;
1883 CopyMem (&((*Info
)->PixelInformation
), &(ModeData
->PixelBitMask
), sizeof(ModeData
->PixelBitMask
));
1885 (*Info
)->PixelsPerScanLine
= (ModeData
->BytesPerScanLine
* 8) / ModeData
->BitsPerPixel
;
1891 Worker function to set video mode.
1893 @param BiosVideoPrivate Instance of BIOS_VIDEO_DEV.
1894 @param ModeData The mode data to be set.
1895 @param DevicePath Pointer to Device Path Protocol.
1897 @retval EFI_SUCCESS Graphics mode was changed.
1898 @retval EFI_DEVICE_ERROR The device had an error and could not complete the
1900 @retval EFI_UNSUPPORTED ModeNumber is not supported by this device.
1904 BiosVideoSetModeWorker (
1905 IN BIOS_VIDEO_DEV
*BiosVideoPrivate
,
1906 IN BIOS_VIDEO_MODE_DATA
*ModeData
,
1907 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1911 EFI_IA32_REGISTER_SET Regs
;
1913 if (BiosVideoPrivate
->LineBuffer
!= NULL
) {
1914 FreePool (BiosVideoPrivate
->LineBuffer
);
1917 if (BiosVideoPrivate
->VgaFrameBuffer
!= NULL
) {
1918 FreePool (BiosVideoPrivate
->VgaFrameBuffer
);
1921 if (BiosVideoPrivate
->VbeFrameBuffer
!= NULL
) {
1922 FreePool (BiosVideoPrivate
->VbeFrameBuffer
);
1925 BiosVideoPrivate
->LineBuffer
= (UINT8
*) AllocatePool (
1926 ModeData
->BytesPerScanLine
1928 if (NULL
== BiosVideoPrivate
->LineBuffer
) {
1929 return EFI_OUT_OF_RESOURCES
;
1932 // Clear all registers
1934 ZeroMem (&Regs
, sizeof (Regs
));
1936 if (ModeData
->VbeModeNumber
< 0x100) {
1938 // Allocate a working buffer for BLT operations to the VGA frame buffer
1940 BiosVideoPrivate
->VgaFrameBuffer
= (UINT8
*) AllocatePool (4 * 480 * 80);
1941 if (NULL
== BiosVideoPrivate
->VgaFrameBuffer
) {
1942 return EFI_OUT_OF_RESOURCES
;
1947 Regs
.X
.AX
= ModeData
->VbeModeNumber
;
1948 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
1952 // Allocate a working buffer for BLT operations to the VBE frame buffer
1954 BiosVideoPrivate
->VbeFrameBuffer
=
1955 (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*) AllocatePool (
1956 ModeData
->BytesPerScanLine
* ModeData
->VerticalResolution
1958 if (NULL
== BiosVideoPrivate
->VbeFrameBuffer
) {
1959 return EFI_OUT_OF_RESOURCES
;
1964 Regs
.X
.AX
= VESA_BIOS_EXTENSIONS_SET_MODE
;
1965 Regs
.X
.BX
= (UINT16
) (ModeData
->VbeModeNumber
| VESA_BIOS_EXTENSIONS_MODE_NUMBER_LINEAR_FRAME_BUFFER
);
1966 ZeroMem (BiosVideoPrivate
->VbeCrtcInformationBlock
, sizeof (VESA_BIOS_EXTENSIONS_CRTC_INFORMATION_BLOCK
));
1967 Regs
.X
.ES
= EFI_SEGMENT ((UINTN
) BiosVideoPrivate
->VbeCrtcInformationBlock
);
1968 Regs
.X
.DI
= EFI_OFFSET ((UINTN
) BiosVideoPrivate
->VbeCrtcInformationBlock
);
1969 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
1972 // Check to see if the call succeeded
1974 if (Regs
.X
.AX
!= VESA_BIOS_EXTENSIONS_STATUS_SUCCESS
) {
1975 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1976 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1977 EFI_PERIPHERAL_LOCAL_CONSOLE
| EFI_P_EC_OUTPUT_ERROR
,
1980 return EFI_DEVICE_ERROR
;
1983 // Initialize the state of the VbeFrameBuffer
1985 Status
= BiosVideoPrivate
->PciIo
->Mem
.Read (
1986 BiosVideoPrivate
->PciIo
,
1987 EfiPciIoWidthUint32
,
1988 EFI_PCI_IO_PASS_THROUGH_BAR
,
1989 (UINT64
) (UINTN
) ModeData
->LinearFrameBuffer
,
1990 (ModeData
->BytesPerScanLine
* ModeData
->VerticalResolution
) >> 2,
1991 BiosVideoPrivate
->VbeFrameBuffer
1993 if (EFI_ERROR (Status
)) {
2002 Graphics Output protocol interface to set video mode.
2004 @param This Protocol instance pointer.
2005 @param ModeNumber The mode number to be set.
2007 @retval EFI_SUCCESS Graphics mode was changed.
2008 @retval EFI_DEVICE_ERROR The device had an error and could not complete the
2010 @retval EFI_UNSUPPORTED ModeNumber is not supported by this device.
2015 BiosVideoGraphicsOutputSetMode (
2016 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
* This
,
2017 IN UINT32 ModeNumber
2021 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
2022 BIOS_VIDEO_MODE_DATA
*ModeData
;
2023 EFI_GRAPHICS_OUTPUT_BLT_PIXEL Background
;
2026 return EFI_INVALID_PARAMETER
;
2029 BiosVideoPrivate
= BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (This
);
2031 ModeData
= &BiosVideoPrivate
->ModeData
[ModeNumber
];
2033 if (ModeNumber
>= This
->Mode
->MaxMode
) {
2034 return EFI_UNSUPPORTED
;
2037 if (ModeNumber
== This
->Mode
->Mode
) {
2039 // Clear screen to black
2041 ZeroMem (&Background
, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
));
2042 BiosVideoGraphicsOutputVbeBlt (
2050 ModeData
->HorizontalResolution
,
2051 ModeData
->VerticalResolution
,
2057 Status
= BiosVideoSetModeWorker (BiosVideoPrivate
, ModeData
, BiosVideoPrivate
->GopDevicePath
);
2058 if (EFI_ERROR (Status
)) {
2062 This
->Mode
->Mode
= ModeNumber
;
2063 This
->Mode
->Info
->Version
= 0;
2064 This
->Mode
->Info
->HorizontalResolution
= ModeData
->HorizontalResolution
;
2065 This
->Mode
->Info
->VerticalResolution
= ModeData
->VerticalResolution
;
2066 This
->Mode
->Info
->PixelFormat
= ModeData
->PixelFormat
;
2067 CopyMem (&(This
->Mode
->Info
->PixelInformation
), &(ModeData
->PixelBitMask
), sizeof (ModeData
->PixelBitMask
));
2068 This
->Mode
->Info
->PixelsPerScanLine
= (ModeData
->BytesPerScanLine
* 8) / ModeData
->BitsPerPixel
;
2069 This
->Mode
->SizeOfInfo
= sizeof(EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
);
2072 // Frame BufferSize remain unchanged
2074 This
->Mode
->FrameBufferBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) ModeData
->LinearFrameBuffer
;
2075 This
->Mode
->FrameBufferSize
= ModeData
->FrameBufferSize
;
2077 BiosVideoPrivate
->HardwareNeedsStarting
= FALSE
;
2083 Update physical frame buffer, copy 4 bytes block, then copy remaining bytes.
2085 @param PciIo The pointer of EFI_PCI_IO_PROTOCOL
2086 @param VbeBuffer The data to transfer to screen
2087 @param MemAddress Physical frame buffer base address
2088 @param DestinationX The X coordinate of the destination for BltOperation
2089 @param DestinationY The Y coordinate of the destination for BltOperation
2090 @param TotalBytes The total bytes of copy
2091 @param VbePixelWidth Bytes per pixel
2092 @param BytesPerScanLine Bytes per scan line
2097 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
2098 IN UINT8
*VbeBuffer
,
2099 IN VOID
*MemAddress
,
2100 IN UINTN DestinationX
,
2101 IN UINTN DestinationY
,
2102 IN UINTN TotalBytes
,
2103 IN UINT32 VbePixelWidth
,
2104 IN UINTN BytesPerScanLine
2107 UINTN FrameBufferAddr
;
2109 UINTN RemainingBytes
;
2110 UINTN UnalignedBytes
;
2113 FrameBufferAddr
= (UINTN
) MemAddress
+ (DestinationY
* BytesPerScanLine
) + DestinationX
* VbePixelWidth
;
2116 // If TotalBytes is less than 4 bytes, only start byte copy.
2118 if (TotalBytes
< 4) {
2119 Status
= PciIo
->Mem
.Write (
2122 EFI_PCI_IO_PASS_THROUGH_BAR
,
2123 (UINT64
) FrameBufferAddr
,
2127 ASSERT_EFI_ERROR (Status
);
2132 // If VbeBuffer is not 4-byte aligned, start byte copy.
2134 UnalignedBytes
= (4 - ((UINTN
) VbeBuffer
& 0x3)) & 0x3;
2136 if (UnalignedBytes
!= 0) {
2137 Status
= PciIo
->Mem
.Write (
2140 EFI_PCI_IO_PASS_THROUGH_BAR
,
2141 (UINT64
) FrameBufferAddr
,
2145 ASSERT_EFI_ERROR (Status
);
2146 FrameBufferAddr
+= UnalignedBytes
;
2147 VbeBuffer
+= UnalignedBytes
;
2151 // Calculate 4-byte block count and remaining bytes.
2153 CopyBlockNum
= (TotalBytes
- UnalignedBytes
) >> 2;
2154 RemainingBytes
= (TotalBytes
- UnalignedBytes
) & 3;
2157 // Copy 4-byte block and remaining bytes to physical frame buffer.
2159 if (CopyBlockNum
!= 0) {
2160 Status
= PciIo
->Mem
.Write (
2162 EfiPciIoWidthUint32
,
2163 EFI_PCI_IO_PASS_THROUGH_BAR
,
2164 (UINT64
) FrameBufferAddr
,
2168 ASSERT_EFI_ERROR (Status
);
2171 if (RemainingBytes
!= 0) {
2172 FrameBufferAddr
+= (CopyBlockNum
<< 2);
2173 VbeBuffer
+= (CopyBlockNum
<< 2);
2174 Status
= PciIo
->Mem
.Write (
2177 EFI_PCI_IO_PASS_THROUGH_BAR
,
2178 (UINT64
) FrameBufferAddr
,
2182 ASSERT_EFI_ERROR (Status
);
2187 Worker function to block transfer for VBE device.
2189 @param BiosVideoPrivate Instance of BIOS_VIDEO_DEV
2190 @param BltBuffer The data to transfer to screen
2191 @param BltOperation The operation to perform
2192 @param SourceX The X coordinate of the source for BltOperation
2193 @param SourceY The Y coordinate of the source for BltOperation
2194 @param DestinationX The X coordinate of the destination for
2196 @param DestinationY The Y coordinate of the destination for
2198 @param Width The width of a rectangle in the blt rectangle in
2200 @param Height The height of a rectangle in the blt rectangle in
2202 @param Delta Not used for EfiBltVideoFill and
2203 EfiBltVideoToVideo operation. If a Delta of 0 is
2204 used, the entire BltBuffer will be operated on. If
2205 a subrectangle of the BltBuffer is used, then
2206 Delta represents the number of bytes in a row of
2208 @param Mode Mode data.
2210 @retval EFI_INVALID_PARAMETER Invalid parameter passed in
2211 @retval EFI_SUCCESS Blt operation success
2215 BiosVideoVbeBltWorker (
2216 IN BIOS_VIDEO_DEV
*BiosVideoPrivate
,
2217 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
, OPTIONAL
2218 IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation
,
2221 IN UINTN DestinationX
,
2222 IN UINTN DestinationY
,
2226 IN BIOS_VIDEO_MODE_DATA
*Mode
2229 EFI_PCI_IO_PROTOCOL
*PciIo
;
2230 EFI_TPL OriginalTPL
;
2234 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*Blt
;
2236 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*VbeFrameBuffer
;
2237 UINTN BytesPerScanLine
;
2242 UINT32 VbePixelWidth
;
2246 PciIo
= BiosVideoPrivate
->PciIo
;
2248 VbeFrameBuffer
= BiosVideoPrivate
->VbeFrameBuffer
;
2249 MemAddress
= Mode
->LinearFrameBuffer
;
2250 BytesPerScanLine
= Mode
->BytesPerScanLine
;
2251 VbePixelWidth
= Mode
->BitsPerPixel
/ 8;
2252 BltUint8
= (UINT8
*) BltBuffer
;
2253 TotalBytes
= Width
* VbePixelWidth
;
2255 if (((UINTN
) BltOperation
) >= EfiGraphicsOutputBltOperationMax
) {
2256 return EFI_INVALID_PARAMETER
;
2259 if (Width
== 0 || Height
== 0) {
2260 return EFI_INVALID_PARAMETER
;
2263 // We need to fill the Virtual Screen buffer with the blt data.
2264 // The virtual screen is upside down, as the first row is the bootom row of
2267 if (BltOperation
== EfiBltVideoToBltBuffer
) {
2269 // Video to BltBuffer: Source is Video, destination is BltBuffer
2271 if (SourceY
+ Height
> Mode
->VerticalResolution
) {
2272 return EFI_INVALID_PARAMETER
;
2275 if (SourceX
+ Width
> Mode
->HorizontalResolution
) {
2276 return EFI_INVALID_PARAMETER
;
2280 // BltBuffer to Video: Source is BltBuffer, destination is Video
2282 if (DestinationY
+ Height
> Mode
->VerticalResolution
) {
2283 return EFI_INVALID_PARAMETER
;
2286 if (DestinationX
+ Width
> Mode
->HorizontalResolution
) {
2287 return EFI_INVALID_PARAMETER
;
2291 // If Delta is zero, then the entire BltBuffer is being used, so Delta
2292 // is the number of bytes in each row of BltBuffer. Since BltBuffer is Width pixels size,
2293 // the number of bytes in each row can be computed.
2296 Delta
= Width
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
);
2299 // We have to raise to TPL Notify, so we make an atomic write the frame buffer.
2300 // We would not want a timer based event (Cursor, ...) to come in while we are
2301 // doing this operation.
2303 OriginalTPL
= gBS
->RaiseTPL (TPL_NOTIFY
);
2305 switch (BltOperation
) {
2306 case EfiBltVideoToBltBuffer
:
2307 for (SrcY
= SourceY
, DstY
= DestinationY
; DstY
< (Height
+ DestinationY
); SrcY
++, DstY
++) {
2308 Blt
= (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*) (BltUint8
+ DstY
* Delta
+ DestinationX
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
));
2310 // Shuffle the packed bytes in the hardware buffer to match EFI_GRAPHICS_OUTPUT_BLT_PIXEL
2312 VbeBuffer
= ((UINT8
*) VbeFrameBuffer
+ (SrcY
* BytesPerScanLine
+ SourceX
* VbePixelWidth
));
2313 for (DstX
= DestinationX
; DstX
< (Width
+ DestinationX
); DstX
++) {
2314 Pixel
= VbeBuffer
[0] | VbeBuffer
[1] << 8 | VbeBuffer
[2] << 16 | VbeBuffer
[3] << 24;
2315 Blt
->Red
= (UINT8
) ((Pixel
>> Mode
->Red
.Position
) & Mode
->Red
.Mask
);
2316 Blt
->Blue
= (UINT8
) ((Pixel
>> Mode
->Blue
.Position
) & Mode
->Blue
.Mask
);
2317 Blt
->Green
= (UINT8
) ((Pixel
>> Mode
->Green
.Position
) & Mode
->Green
.Mask
);
2320 VbeBuffer
+= VbePixelWidth
;
2326 case EfiBltVideoToVideo
:
2327 for (Index
= 0; Index
< Height
; Index
++) {
2328 if (DestinationY
<= SourceY
) {
2329 SrcY
= SourceY
+ Index
;
2330 DstY
= DestinationY
+ Index
;
2332 SrcY
= SourceY
+ Height
- Index
- 1;
2333 DstY
= DestinationY
+ Height
- Index
- 1;
2336 VbeBuffer
= ((UINT8
*) VbeFrameBuffer
+ DstY
* BytesPerScanLine
+ DestinationX
* VbePixelWidth
);
2337 VbeBuffer1
= ((UINT8
*) VbeFrameBuffer
+ SrcY
* BytesPerScanLine
+ SourceX
* VbePixelWidth
);
2346 // Update physical frame buffer.
2361 case EfiBltVideoFill
:
2362 VbeBuffer
= (UINT8
*) ((UINTN
) VbeFrameBuffer
+ (DestinationY
* BytesPerScanLine
) + DestinationX
* VbePixelWidth
);
2363 Blt
= (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*) BltUint8
;
2365 // Shuffle the RGB fields in EFI_GRAPHICS_OUTPUT_BLT_PIXEL to match the hardware buffer
2367 Pixel
= ((Blt
->Red
& Mode
->Red
.Mask
) << Mode
->Red
.Position
) |
2369 (Blt
->Green
& Mode
->Green
.Mask
) <<
2370 Mode
->Green
.Position
2372 ((Blt
->Blue
& Mode
->Blue
.Mask
) << Mode
->Blue
.Position
);
2374 for (Index
= 0; Index
< Width
; Index
++) {
2380 VbeBuffer
+= VbePixelWidth
;
2383 VbeBuffer
= (UINT8
*) ((UINTN
) VbeFrameBuffer
+ (DestinationY
* BytesPerScanLine
) + DestinationX
* VbePixelWidth
);
2384 for (DstY
= DestinationY
+ 1; DstY
< (Height
+ DestinationY
); DstY
++) {
2386 (VOID
*) ((UINTN
) VbeFrameBuffer
+ (DstY
* BytesPerScanLine
) + DestinationX
* VbePixelWidth
),
2392 for (DstY
= DestinationY
; DstY
< (Height
+ DestinationY
); DstY
++) {
2394 // Update physical frame buffer.
2409 case EfiBltBufferToVideo
:
2410 for (SrcY
= SourceY
, DstY
= DestinationY
; SrcY
< (Height
+ SourceY
); SrcY
++, DstY
++) {
2411 Blt
= (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*) (BltUint8
+ (SrcY
* Delta
) + (SourceX
) * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
));
2412 VbeBuffer
= ((UINT8
*) VbeFrameBuffer
+ (DstY
* BytesPerScanLine
+ DestinationX
* VbePixelWidth
));
2413 for (DstX
= DestinationX
; DstX
< (Width
+ DestinationX
); DstX
++) {
2415 // Shuffle the RGB fields in EFI_GRAPHICS_OUTPUT_BLT_PIXEL to match the hardware buffer
2417 Pixel
= ((Blt
->Red
& Mode
->Red
.Mask
) << Mode
->Red
.Position
) |
2418 ((Blt
->Green
& Mode
->Green
.Mask
) << Mode
->Green
.Position
) |
2419 ((Blt
->Blue
& Mode
->Blue
.Mask
) << Mode
->Blue
.Position
);
2426 VbeBuffer
+= VbePixelWidth
;
2429 VbeBuffer
= ((UINT8
*) VbeFrameBuffer
+ (DstY
* BytesPerScanLine
+ DestinationX
* VbePixelWidth
));
2432 // Update physical frame buffer.
2450 gBS
->RestoreTPL (OriginalTPL
);
2456 Graphics Output protocol instance to block transfer for VBE device.
2458 @param This Pointer to Graphics Output protocol instance
2459 @param BltBuffer The data to transfer to screen
2460 @param BltOperation The operation to perform
2461 @param SourceX The X coordinate of the source for BltOperation
2462 @param SourceY The Y coordinate of the source for BltOperation
2463 @param DestinationX The X coordinate of the destination for
2465 @param DestinationY The Y coordinate of the destination for
2467 @param Width The width of a rectangle in the blt rectangle in
2469 @param Height The height of a rectangle in the blt rectangle in
2471 @param Delta Not used for EfiBltVideoFill and
2472 EfiBltVideoToVideo operation. If a Delta of 0 is
2473 used, the entire BltBuffer will be operated on. If
2474 a subrectangle of the BltBuffer is used, then
2475 Delta represents the number of bytes in a row of
2478 @retval EFI_INVALID_PARAMETER Invalid parameter passed in
2479 @retval EFI_SUCCESS Blt operation success
2484 BiosVideoGraphicsOutputVbeBlt (
2485 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*This
,
2486 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
, OPTIONAL
2487 IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation
,
2490 IN UINTN DestinationX
,
2491 IN UINTN DestinationY
,
2497 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
2498 BIOS_VIDEO_MODE_DATA
*Mode
;
2501 return EFI_INVALID_PARAMETER
;
2504 BiosVideoPrivate
= BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (This
);
2505 Mode
= &BiosVideoPrivate
->ModeData
[This
->Mode
->Mode
];
2507 return BiosVideoVbeBltWorker (
2523 Write graphics controller registers.
2525 @param PciIo Pointer to PciIo protocol instance of the
2527 @param Address Register address
2528 @param Data Data to be written to register
2534 WriteGraphicsController (
2535 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
2540 Address
= Address
| (Data
<< 8);
2543 EfiPciIoWidthUint16
,
2544 EFI_PCI_IO_PASS_THROUGH_BAR
,
2545 VGA_GRAPHICS_CONTROLLER_ADDRESS_REGISTER
,
2553 Read the four bit plane of VGA frame buffer.
2555 @param PciIo Pointer to PciIo protocol instance of the
2557 @param HardwareBuffer Hardware VGA frame buffer address
2558 @param MemoryBuffer Memory buffer address
2559 @param WidthInBytes Number of bytes in a line to read
2560 @param Height Height of the area to read
2567 EFI_PCI_IO_PROTOCOL
*PciIo
,
2568 UINT8
*HardwareBuffer
,
2569 UINT8
*MemoryBuffer
,
2576 UINTN FrameBufferOffset
;
2581 // Program the Mode Register Write mode 0, Read mode 0
2583 WriteGraphicsController (
2585 VGA_GRAPHICS_CONTROLLER_MODE_REGISTER
,
2586 VGA_GRAPHICS_CONTROLLER_READ_MODE_0
| VGA_GRAPHICS_CONTROLLER_WRITE_MODE_0
2589 for (BitPlane
= 0, FrameBufferOffset
= 0;
2590 BitPlane
< VGA_NUMBER_OF_BIT_PLANES
;
2591 BitPlane
++, FrameBufferOffset
+= VGA_BYTES_PER_BIT_PLANE
2594 // Program the Read Map Select Register to select the correct bit plane
2596 WriteGraphicsController (
2598 VGA_GRAPHICS_CONTROLLER_READ_MAP_SELECT_REGISTER
,
2602 Source
= HardwareBuffer
;
2603 Destination
= MemoryBuffer
+ FrameBufferOffset
;
2605 for (Rows
= 0; Rows
< Height
; Rows
++, Source
+= VGA_BYTES_PER_SCAN_LINE
, Destination
+= VGA_BYTES_PER_SCAN_LINE
) {
2609 EFI_PCI_IO_PASS_THROUGH_BAR
,
2610 (UINT64
) (UINTN
) Source
,
2612 (VOID
*) Destination
2620 Internal routine to convert VGA color to Grahpics Output color.
2622 @param MemoryBuffer Buffer containing VGA color
2623 @param CoordinateX The X coordinate of pixel on screen
2624 @param CoordinateY The Y coordinate of pixel on screen
2625 @param BltBuffer Buffer to contain converted Grahpics Output color
2631 VgaConvertToGraphicsOutputColor (
2632 UINT8
*MemoryBuffer
,
2635 EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
2642 MemoryBuffer
+= ((CoordinateY
<< 6) + (CoordinateY
<< 4) + (CoordinateX
>> 3));
2643 Mask
= mVgaBitMaskTable
[CoordinateX
& 0x07];
2644 for (Bit
= 0x01, Color
= 0; Bit
< 0x10; Bit
<<= 1, MemoryBuffer
+= VGA_BYTES_PER_BIT_PLANE
) {
2645 if ((*MemoryBuffer
& Mask
) != 0) {
2650 *BltBuffer
= mVgaColorToGraphicsOutputColor
[Color
];
2654 Internal routine to convert Grahpics Output color to VGA color.
2656 @param BltBuffer buffer containing Grahpics Output color
2658 @return Converted VGA color
2663 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
2668 Color
= (UINT8
) ((BltBuffer
->Blue
>> 7) | ((BltBuffer
->Green
>> 6) & 0x02) | ((BltBuffer
->Red
>> 5) & 0x04));
2669 if ((BltBuffer
->Red
+ BltBuffer
->Green
+ BltBuffer
->Blue
) > 0x180) {
2678 Grahpics Output protocol instance to block transfer for VGA device.
2680 @param This Pointer to Grahpics Output protocol instance
2681 @param BltBuffer The data to transfer to screen
2682 @param BltOperation The operation to perform
2683 @param SourceX The X coordinate of the source for BltOperation
2684 @param SourceY The Y coordinate of the source for BltOperation
2685 @param DestinationX The X coordinate of the destination for
2687 @param DestinationY The Y coordinate of the destination for
2689 @param Width The width of a rectangle in the blt rectangle in
2691 @param Height The height of a rectangle in the blt rectangle in
2693 @param Delta Not used for EfiBltVideoFill and
2694 EfiBltVideoToVideo operation. If a Delta of 0 is
2695 used, the entire BltBuffer will be operated on. If
2696 a subrectangle of the BltBuffer is used, then
2697 Delta represents the number of bytes in a row of
2700 @retval EFI_INVALID_PARAMETER Invalid parameter passed in
2701 @retval EFI_SUCCESS Blt operation success
2706 BiosVideoGraphicsOutputVgaBlt (
2707 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*This
,
2708 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
, OPTIONAL
2709 IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation
,
2712 IN UINTN DestinationX
,
2713 IN UINTN DestinationY
,
2719 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
2720 EFI_TPL OriginalTPL
;
2722 UINTN BytesPerScanLine
;
2734 UINT8
*SourceAddress
;
2735 UINT8
*DestinationAddress
;
2736 EFI_PCI_IO_PROTOCOL
*PciIo
;
2739 UINT8
*VgaFrameBuffer
;
2748 if (This
== NULL
|| ((UINTN
) BltOperation
) >= EfiGraphicsOutputBltOperationMax
) {
2749 return EFI_INVALID_PARAMETER
;
2752 BiosVideoPrivate
= BIOS_VIDEO_DEV_FROM_GRAPHICS_OUTPUT_THIS (This
);
2754 CurrentMode
= This
->Mode
->Mode
;
2755 PciIo
= BiosVideoPrivate
->PciIo
;
2756 MemAddress
= BiosVideoPrivate
->ModeData
[CurrentMode
].LinearFrameBuffer
;
2757 BytesPerScanLine
= BiosVideoPrivate
->ModeData
[CurrentMode
].BytesPerScanLine
>> 3;
2758 VgaFrameBuffer
= BiosVideoPrivate
->VgaFrameBuffer
;
2761 if (Width
== 0 || Height
== 0) {
2762 return EFI_INVALID_PARAMETER
;
2765 // We need to fill the Virtual Screen buffer with the blt data.
2766 // The virtual screen is upside down, as the first row is the bootom row of
2769 if (BltOperation
== EfiBltVideoToBltBuffer
) {
2771 // Video to BltBuffer: Source is Video, destination is BltBuffer
2773 if (SourceY
+ Height
> BiosVideoPrivate
->ModeData
[CurrentMode
].VerticalResolution
) {
2774 return EFI_INVALID_PARAMETER
;
2777 if (SourceX
+ Width
> BiosVideoPrivate
->ModeData
[CurrentMode
].HorizontalResolution
) {
2778 return EFI_INVALID_PARAMETER
;
2782 // BltBuffer to Video: Source is BltBuffer, destination is Video
2784 if (DestinationY
+ Height
> BiosVideoPrivate
->ModeData
[CurrentMode
].VerticalResolution
) {
2785 return EFI_INVALID_PARAMETER
;
2788 if (DestinationX
+ Width
> BiosVideoPrivate
->ModeData
[CurrentMode
].HorizontalResolution
) {
2789 return EFI_INVALID_PARAMETER
;
2793 // If Delta is zero, then the entire BltBuffer is being used, so Delta
2794 // is the number of bytes in each row of BltBuffer. Since BltBuffer is Width pixels size,
2795 // the number of bytes in each row can be computed.
2798 Delta
= Width
* sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
);
2801 // We have to raise to TPL Notify, so we make an atomic write the frame buffer.
2802 // We would not want a timer based event (Cursor, ...) to come in while we are
2803 // doing this operation.
2805 OriginalTPL
= gBS
->RaiseTPL (TPL_NOTIFY
);
2808 // Compute some values we need for VGA
2810 switch (BltOperation
) {
2811 case EfiBltVideoToBltBuffer
:
2813 SourceOffset
= (SourceY
<< 6) + (SourceY
<< 4) + (SourceX
>> 3);
2814 SourceWidth
= ((SourceX
+ Width
- 1) >> 3) - (SourceX
>> 3) + 1;
2817 // Read all the pixels in the 4 bit planes into a memory buffer that looks like the VGA buffer
2821 MemAddress
+ SourceOffset
,
2822 VgaFrameBuffer
+ SourceOffset
,
2828 // Convert VGA Bit Planes to a Graphics Output 32-bit color value
2830 BltBuffer
+= (DestinationY
* (Delta
>> 2) + DestinationX
);
2831 for (Rows
= 0, CoordinateY
= SourceY
; Rows
< Height
; Rows
++, CoordinateY
++, BltBuffer
+= (Delta
>> 2)) {
2832 for (Columns
= 0, CoordinateX
= SourceX
; Columns
< Width
; Columns
++, CoordinateX
++, BltBuffer
++) {
2833 VgaConvertToGraphicsOutputColor (VgaFrameBuffer
, CoordinateX
, CoordinateY
, BltBuffer
);
2841 case EfiBltVideoToVideo
:
2843 // Check for an aligned Video to Video operation
2845 if ((SourceX
& 0x07) == 0x00 && (DestinationX
& 0x07) == 0x00 && (Width
& 0x07) == 0x00) {
2847 // Program the Mode Register Write mode 1, Read mode 0
2849 WriteGraphicsController (
2851 VGA_GRAPHICS_CONTROLLER_MODE_REGISTER
,
2852 VGA_GRAPHICS_CONTROLLER_READ_MODE_0
| VGA_GRAPHICS_CONTROLLER_WRITE_MODE_1
2855 SourceAddress
= (UINT8
*) (MemAddress
+ (SourceY
<< 6) + (SourceY
<< 4) + (SourceX
>> 3));
2856 DestinationAddress
= (UINT8
*) (MemAddress
+ (DestinationY
<< 6) + (DestinationY
<< 4) + (DestinationX
>> 3));
2858 for (Index
= 0, Offset
= 0; Index
< Height
; Index
++, Offset
+= BytesPerScanLine
) {
2862 EFI_PCI_IO_PASS_THROUGH_BAR
,
2863 (UINT64
) (UINTN
) (DestinationAddress
+ Offset
),
2864 EFI_PCI_IO_PASS_THROUGH_BAR
,
2865 (UINT64
) (UINTN
) (SourceAddress
+ Offset
),
2870 SourceOffset
= (SourceY
<< 6) + (SourceY
<< 4) + (SourceX
>> 3);
2871 SourceWidth
= ((SourceX
+ Width
- 1) >> 3) - (SourceX
>> 3) + 1;
2874 // Read all the pixels in the 4 bit planes into a memory buffer that looks like the VGA buffer
2878 MemAddress
+ SourceOffset
,
2879 VgaFrameBuffer
+ SourceOffset
,
2887 case EfiBltVideoFill
:
2888 StartAddress
= (UINTN
) (MemAddress
+ (DestinationY
<< 6) + (DestinationY
<< 4) + (DestinationX
>> 3));
2889 Bytes
= ((DestinationX
+ Width
- 1) >> 3) - (DestinationX
>> 3);
2890 LeftMask
= mVgaLeftMaskTable
[DestinationX
& 0x07];
2891 RightMask
= mVgaRightMaskTable
[(DestinationX
+ Width
- 1) & 0x07];
2893 LeftMask
= (UINT8
) (LeftMask
& RightMask
);
2897 if (LeftMask
== 0xff) {
2903 if (RightMask
== 0xff) {
2908 PixelColor
= VgaConvertColor (BltBuffer
);
2911 // Program the Mode Register Write mode 2, Read mode 0
2913 WriteGraphicsController (
2915 VGA_GRAPHICS_CONTROLLER_MODE_REGISTER
,
2916 VGA_GRAPHICS_CONTROLLER_READ_MODE_0
| VGA_GRAPHICS_CONTROLLER_WRITE_MODE_2
2920 // Program the Data Rotate/Function Select Register to replace
2922 WriteGraphicsController (
2924 VGA_GRAPHICS_CONTROLLER_DATA_ROTATE_REGISTER
,
2925 VGA_GRAPHICS_CONTROLLER_FUNCTION_REPLACE
2928 if (LeftMask
!= 0) {
2930 // Program the BitMask register with the Left column mask
2932 WriteGraphicsController (
2934 VGA_GRAPHICS_CONTROLLER_BIT_MASK_REGISTER
,
2938 for (Index
= 0, Address
= StartAddress
; Index
< Height
; Index
++, Address
+= BytesPerScanLine
) {
2940 // Read data from the bit planes into the latches
2945 EFI_PCI_IO_PASS_THROUGH_BAR
,
2946 (UINT64
) (UINTN
) Address
,
2951 // Write the lower 4 bits of PixelColor to the bit planes in the pixels enabled by BitMask
2956 EFI_PCI_IO_PASS_THROUGH_BAR
,
2957 (UINT64
) (UINTN
) Address
,
2966 // Program the BitMask register with the middle column mask of 0xff
2968 WriteGraphicsController (
2970 VGA_GRAPHICS_CONTROLLER_BIT_MASK_REGISTER
,
2974 for (Index
= 0, Address
= StartAddress
+ 1; Index
< Height
; Index
++, Address
+= BytesPerScanLine
) {
2977 EfiPciIoWidthFillUint8
,
2978 EFI_PCI_IO_PASS_THROUGH_BAR
,
2979 (UINT64
) (UINTN
) Address
,
2986 if (RightMask
!= 0) {
2988 // Program the BitMask register with the Right column mask
2990 WriteGraphicsController (
2992 VGA_GRAPHICS_CONTROLLER_BIT_MASK_REGISTER
,
2996 for (Index
= 0, Address
= StartAddress
+ Bytes
; Index
< Height
; Index
++, Address
+= BytesPerScanLine
) {
2998 // Read data from the bit planes into the latches
3003 EFI_PCI_IO_PASS_THROUGH_BAR
,
3004 (UINT64
) (UINTN
) Address
,
3009 // Write the lower 4 bits of PixelColor to the bit planes in the pixels enabled by BitMask
3014 EFI_PCI_IO_PASS_THROUGH_BAR
,
3015 (UINT64
) (UINTN
) Address
,
3023 case EfiBltBufferToVideo
:
3024 StartAddress
= (UINTN
) (MemAddress
+ (DestinationY
<< 6) + (DestinationY
<< 4) + (DestinationX
>> 3));
3025 LeftMask
= mVgaBitMaskTable
[DestinationX
& 0x07];
3028 // Program the Mode Register Write mode 2, Read mode 0
3030 WriteGraphicsController (
3032 VGA_GRAPHICS_CONTROLLER_MODE_REGISTER
,
3033 VGA_GRAPHICS_CONTROLLER_READ_MODE_0
| VGA_GRAPHICS_CONTROLLER_WRITE_MODE_2
3037 // Program the Data Rotate/Function Select Register to replace
3039 WriteGraphicsController (
3041 VGA_GRAPHICS_CONTROLLER_DATA_ROTATE_REGISTER
,
3042 VGA_GRAPHICS_CONTROLLER_FUNCTION_REPLACE
3045 for (Index
= 0, Address
= StartAddress
; Index
< Height
; Index
++, Address
+= BytesPerScanLine
) {
3046 for (Index1
= 0; Index1
< Width
; Index1
++) {
3047 BiosVideoPrivate
->LineBuffer
[Index1
] = VgaConvertColor (&BltBuffer
[(SourceY
+ Index
) * (Delta
>> 2) + SourceX
+ Index1
]);
3049 AddressFix
= Address
;
3051 for (Bit
= 0; Bit
< 8; Bit
++) {
3053 // Program the BitMask register with the Left column mask
3055 WriteGraphicsController (
3057 VGA_GRAPHICS_CONTROLLER_BIT_MASK_REGISTER
,
3061 for (Index1
= Bit
, Address1
= (UINT8
*) AddressFix
; Index1
< Width
; Index1
+= 8, Address1
++) {
3063 // Read data from the bit planes into the latches
3068 EFI_PCI_IO_PASS_THROUGH_BAR
,
3069 (UINT64
) (UINTN
) Address1
,
3077 EFI_PCI_IO_PASS_THROUGH_BAR
,
3078 (UINT64
) (UINTN
) Address1
,
3080 &BiosVideoPrivate
->LineBuffer
[Index1
]
3084 LeftMask
= (UINT8
) (LeftMask
>> 1);
3085 if (LeftMask
== 0) {
3097 gBS
->RestoreTPL (OriginalTPL
);
3103 // VGA Mini Port Protocol Functions
3107 VgaMiniPort protocol interface to set mode.
3109 @param This Pointer to VgaMiniPort protocol instance
3110 @param ModeNumber The index of the mode
3112 @retval EFI_UNSUPPORTED The requested mode is not supported
3113 @retval EFI_SUCCESS The requested mode is set successfully
3118 BiosVideoVgaMiniPortSetMode (
3119 IN EFI_VGA_MINI_PORT_PROTOCOL
*This
,
3123 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
3124 EFI_IA32_REGISTER_SET Regs
;
3127 return EFI_INVALID_PARAMETER
;
3131 // Make sure the ModeNumber is a valid value
3133 if (ModeNumber
>= This
->MaxMode
) {
3134 return EFI_UNSUPPORTED
;
3137 // Get the device structure for this device
3139 BiosVideoPrivate
= BIOS_VIDEO_DEV_FROM_VGA_MINI_PORT_THIS (This
);
3141 switch (ModeNumber
) {
3144 // Set the 80x25 Text VGA Mode
3148 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3153 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3158 // Set the 80x50 Text VGA Mode
3162 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3166 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3170 return EFI_UNSUPPORTED
;
3177 Event handler for Exit Boot Service.
3179 @param Event The event that be siganlled when exiting boot service.
3180 @param Context Pointer to instance of BIOS_VIDEO_DEV.
3185 BiosVideoNotifyExitBootServices (
3190 BIOS_VIDEO_DEV
*BiosVideoPrivate
;
3191 EFI_IA32_REGISTER_SET Regs
;
3193 BiosVideoPrivate
= (BIOS_VIDEO_DEV
*)Context
;
3196 // Set the 80x25 Text VGA Mode
3200 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3204 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3209 BiosVideoPrivate
->LegacyBios
->Int86 (BiosVideoPrivate
->LegacyBios
, 0x10, &Regs
);
3213 The user Entry Point for module UefiBiosVideo. The user code starts with this function.
3215 @param[in] ImageHandle The firmware allocated handle for the EFI image.
3216 @param[in] SystemTable A pointer to the EFI System Table.
3218 @retval EFI_SUCCESS The entry point is executed successfully.
3219 @retval other Some error occurs when executing this entry point.
3224 BiosVideoEntryPoint(
3225 IN EFI_HANDLE ImageHandle
,
3226 IN EFI_SYSTEM_TABLE
*SystemTable
3232 // Install driver model protocol(s).
3234 Status
= EfiLibInstallDriverBindingComponentName2 (
3237 &gBiosVideoDriverBinding
,
3239 &gBiosVideoComponentName
,
3240 &gBiosVideoComponentName2
3242 ASSERT_EFI_ERROR (Status
);
3245 // Install Legacy BIOS GUID to mark this driver as a BIOS Thunk Driver
3247 return gBS
->InstallMultipleProtocolInterfaces (
3249 &gEfiLegacyBiosGuid
,