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