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