1 /* 2 * Copyright (c) 2013 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Benno Rice under sponsorship from 6 * the FreeBSD Foundation. 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 31 #include <stand.h> 32 #include <bootstrap.h> 33 #include <sys/endian.h> 34 #include <sys/font.h> 35 #include <sys/consplat.h> 36 #include <sys/limits.h> 37 38 #include <efi.h> 39 #include <efilib.h> 40 #include <efiuga.h> 41 #include <efipciio.h> 42 #include <Protocol/EdidActive.h> 43 #include <Protocol/EdidDiscovered.h> 44 #include <machine/metadata.h> 45 46 #include "gfx_fb.h" 47 #include "framebuffer.h" 48 49 EFI_GUID conout_guid = EFI_CONSOLE_OUT_DEVICE_GUID; 50 EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 51 static EFI_GUID pciio_guid = EFI_PCI_IO_PROTOCOL_GUID; 52 EFI_GUID uga_guid = EFI_UGA_DRAW_PROTOCOL_GUID; 53 static EFI_GUID active_edid_guid = EFI_EDID_ACTIVE_PROTOCOL_GUID; 54 55 /* Saved initial GOP mode. */ 56 static uint32_t default_mode = UINT32_MAX; 57 /* Cached EDID. */ 58 static struct vesa_edid_info *edid_info; 59 60 static uint32_t gop_default_mode(void); 61 static int efifb_set_mode(EFI_GRAPHICS_OUTPUT *, uint_t); 62 63 static uint_t 64 efifb_color_depth(struct efi_fb *efifb) 65 { 66 uint32_t mask; 67 uint_t depth; 68 69 mask = efifb->fb_mask_red | efifb->fb_mask_green | 70 efifb->fb_mask_blue | efifb->fb_mask_reserved; 71 if (mask == 0) 72 return (0); 73 for (depth = 1; mask != 1; depth++) 74 mask >>= 1; 75 return (depth); 76 } 77 78 static int 79 efifb_mask_from_pixfmt(struct efi_fb *efifb, EFI_GRAPHICS_PIXEL_FORMAT pixfmt, 80 EFI_PIXEL_BITMASK *pixinfo) 81 { 82 int result; 83 84 result = 0; 85 switch (pixfmt) { 86 case PixelRedGreenBlueReserved8BitPerColor: 87 case PixelBltOnly: 88 efifb->fb_mask_red = 0x000000ff; 89 efifb->fb_mask_green = 0x0000ff00; 90 efifb->fb_mask_blue = 0x00ff0000; 91 efifb->fb_mask_reserved = 0xff000000; 92 break; 93 case PixelBlueGreenRedReserved8BitPerColor: 94 efifb->fb_mask_red = 0x00ff0000; 95 efifb->fb_mask_green = 0x0000ff00; 96 efifb->fb_mask_blue = 0x000000ff; 97 efifb->fb_mask_reserved = 0xff000000; 98 break; 99 case PixelBitMask: 100 efifb->fb_mask_red = pixinfo->RedMask; 101 efifb->fb_mask_green = pixinfo->GreenMask; 102 efifb->fb_mask_blue = pixinfo->BlueMask; 103 efifb->fb_mask_reserved = pixinfo->ReservedMask; 104 break; 105 default: 106 result = 1; 107 break; 108 } 109 return (result); 110 } 111 112 static int 113 efifb_from_gop(struct efi_fb *efifb, EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode, 114 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info) 115 { 116 int result; 117 118 efifb->fb_addr = mode->FrameBufferBase; 119 efifb->fb_size = mode->FrameBufferSize; 120 efifb->fb_height = info->VerticalResolution; 121 efifb->fb_width = info->HorizontalResolution; 122 efifb->fb_stride = info->PixelsPerScanLine; 123 result = efifb_mask_from_pixfmt(efifb, info->PixelFormat, 124 &info->PixelInformation); 125 if (efifb->fb_addr == 0) 126 result = 1; 127 return (result); 128 } 129 130 static ssize_t 131 efifb_uga_find_pixel(EFI_UGA_DRAW_PROTOCOL *uga, uint_t line, 132 EFI_PCI_IO_PROTOCOL *pciio, uint64_t addr, uint64_t size) 133 { 134 EFI_UGA_PIXEL pix0, pix1; 135 uint8_t *data1, *data2; 136 size_t count, maxcount = 1024; 137 ssize_t ofs; 138 EFI_STATUS status; 139 uint_t idx; 140 141 status = uga->Blt(uga, &pix0, EfiUgaVideoToBltBuffer, 142 0, line, 0, 0, 1, 1, 0); 143 if (EFI_ERROR(status)) { 144 printf("UGA BLT operation failed (video->buffer)"); 145 return (-1); 146 } 147 pix1.Red = ~pix0.Red; 148 pix1.Green = ~pix0.Green; 149 pix1.Blue = ~pix0.Blue; 150 pix1.Reserved = 0; 151 152 data1 = calloc(maxcount, 2); 153 if (data1 == NULL) { 154 printf("Unable to allocate memory"); 155 return (-1); 156 } 157 data2 = data1 + maxcount; 158 159 ofs = 0; 160 while (size > 0) { 161 count = min(size, maxcount); 162 163 status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32, 164 EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2, 165 data1); 166 if (EFI_ERROR(status)) { 167 printf("Error reading frame buffer (before)"); 168 goto fail; 169 } 170 status = uga->Blt(uga, &pix1, EfiUgaBltBufferToVideo, 171 0, 0, 0, line, 1, 1, 0); 172 if (EFI_ERROR(status)) { 173 printf("UGA BLT operation failed (modify)"); 174 goto fail; 175 } 176 status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32, 177 EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2, 178 data2); 179 if (EFI_ERROR(status)) { 180 printf("Error reading frame buffer (after)"); 181 goto fail; 182 } 183 status = uga->Blt(uga, &pix0, EfiUgaBltBufferToVideo, 184 0, 0, 0, line, 1, 1, 0); 185 if (EFI_ERROR(status)) { 186 printf("UGA BLT operation failed (restore)"); 187 goto fail; 188 } 189 for (idx = 0; idx < count; idx++) { 190 if (data1[idx] != data2[idx]) { 191 free(data1); 192 return (ofs + (idx & ~3)); 193 } 194 } 195 ofs += count; 196 size -= count; 197 } 198 printf("No change detected in frame buffer"); 199 200 fail: 201 printf(" -- error %lu\n", EFI_ERROR_CODE(status)); 202 free(data1); 203 return (-1); 204 } 205 206 static EFI_PCI_IO_PROTOCOL * 207 efifb_uga_get_pciio(void) 208 { 209 EFI_PCI_IO_PROTOCOL *pciio; 210 EFI_HANDLE *buf, *hp; 211 EFI_STATUS status; 212 UINTN bufsz; 213 214 /* Get all handles that support the UGA protocol. */ 215 bufsz = 0; 216 status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, NULL); 217 if (status != EFI_BUFFER_TOO_SMALL) 218 return (NULL); 219 buf = malloc(bufsz); 220 status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, buf); 221 if (status != EFI_SUCCESS) { 222 free(buf); 223 return (NULL); 224 } 225 bufsz /= sizeof (EFI_HANDLE); 226 227 /* Get the PCI I/O interface of the first handle that supports it. */ 228 pciio = NULL; 229 for (hp = buf; hp < buf + bufsz; hp++) { 230 status = OpenProtocolByHandle(*hp, &pciio_guid, 231 (void **)&pciio); 232 if (status == EFI_SUCCESS) { 233 free(buf); 234 return (pciio); 235 } 236 } 237 free(buf); 238 return (NULL); 239 } 240 241 static EFI_STATUS 242 efifb_uga_locate_framebuffer(EFI_PCI_IO_PROTOCOL *pciio, uint64_t *addrp, 243 uint64_t *sizep) 244 { 245 uint8_t *resattr; 246 uint64_t addr, size; 247 EFI_STATUS status; 248 uint_t bar; 249 250 if (pciio == NULL) 251 return (EFI_DEVICE_ERROR); 252 253 /* Attempt to get the frame buffer address (imprecise). */ 254 *addrp = 0; 255 *sizep = 0; 256 for (bar = 0; bar < 6; bar++) { 257 status = pciio->GetBarAttributes(pciio, bar, NULL, 258 (void **)&resattr); 259 if (status != EFI_SUCCESS) 260 continue; 261 /* XXX magic offsets and constants. */ 262 if (resattr[0] == 0x87 && resattr[3] == 0) { 263 /* 32-bit address space descriptor (MEMIO) */ 264 addr = le32dec(resattr + 10); 265 size = le32dec(resattr + 22); 266 } else if (resattr[0] == 0x8a && resattr[3] == 0) { 267 /* 64-bit address space descriptor (MEMIO) */ 268 addr = le64dec(resattr + 14); 269 size = le64dec(resattr + 38); 270 } else { 271 addr = 0; 272 size = 0; 273 } 274 BS->FreePool(resattr); 275 if (addr == 0 || size == 0) 276 continue; 277 278 /* We assume the largest BAR is the frame buffer. */ 279 if (size > *sizep) { 280 *addrp = addr; 281 *sizep = size; 282 } 283 } 284 return ((*addrp == 0 || *sizep == 0) ? EFI_DEVICE_ERROR : 0); 285 } 286 287 static int 288 efifb_from_uga(struct efi_fb *efifb, EFI_UGA_DRAW_PROTOCOL *uga) 289 { 290 EFI_PCI_IO_PROTOCOL *pciio; 291 char *ev, *p; 292 EFI_STATUS status; 293 ssize_t offset; 294 uint64_t fbaddr; 295 uint32_t horiz, vert, stride; 296 uint32_t np, depth, refresh; 297 298 status = uga->GetMode(uga, &horiz, &vert, &depth, &refresh); 299 if (EFI_ERROR(status)) 300 return (1); 301 efifb->fb_height = vert; 302 efifb->fb_width = horiz; 303 /* Paranoia... */ 304 if (efifb->fb_height == 0 || efifb->fb_width == 0) 305 return (1); 306 307 /* The color masks are fixed AFAICT. */ 308 efifb_mask_from_pixfmt(efifb, PixelBlueGreenRedReserved8BitPerColor, 309 NULL); 310 311 /* pciio can be NULL on return! */ 312 pciio = efifb_uga_get_pciio(); 313 314 /* Try to find the frame buffer. */ 315 status = efifb_uga_locate_framebuffer(pciio, &efifb->fb_addr, 316 &efifb->fb_size); 317 if (EFI_ERROR(status)) { 318 efifb->fb_addr = 0; 319 efifb->fb_size = 0; 320 } 321 322 /* 323 * There's no reliable way to detect the frame buffer or the 324 * offset within the frame buffer of the visible region, nor 325 * the stride. Our only option is to look at the system and 326 * fill in the blanks based on that. Luckily, UGA was mostly 327 * only used on Apple hardware. 328 */ 329 offset = -1; 330 ev = getenv("smbios.system.maker"); 331 if (ev != NULL && strcmp(ev, "Apple Inc.") == 0) { 332 ev = getenv("smbios.system.product"); 333 if (ev != NULL && strcmp(ev, "iMac7,1") == 0) { 334 /* These are the expected values we should have. */ 335 horiz = 1680; 336 vert = 1050; 337 fbaddr = 0xc0000000; 338 /* These are the missing bits. */ 339 offset = 0x10000; 340 stride = 1728; 341 } else if (ev != NULL && strcmp(ev, "MacBook3,1") == 0) { 342 /* These are the expected values we should have. */ 343 horiz = 1280; 344 vert = 800; 345 fbaddr = 0xc0000000; 346 /* These are the missing bits. */ 347 offset = 0x0; 348 stride = 2048; 349 } 350 } 351 352 /* 353 * If this is hardware we know, make sure that it looks familiar 354 * before we accept our hardcoded values. 355 */ 356 if (offset >= 0 && efifb->fb_width == horiz && 357 efifb->fb_height == vert && efifb->fb_addr == fbaddr) { 358 efifb->fb_addr += offset; 359 efifb->fb_size -= offset; 360 efifb->fb_stride = stride; 361 return (0); 362 } else if (offset >= 0) { 363 printf("Hardware make/model known, but graphics not " 364 "as expected.\n"); 365 printf("Console may not work!\n"); 366 } 367 368 /* 369 * The stride is equal or larger to the width. Often it's the 370 * next larger power of two. We'll start with that... 371 */ 372 efifb->fb_stride = efifb->fb_width; 373 do { 374 np = efifb->fb_stride & (efifb->fb_stride - 1); 375 if (np) { 376 efifb->fb_stride |= (np - 1); 377 efifb->fb_stride++; 378 } 379 } while (np); 380 381 ev = getenv("hw.efifb.address"); 382 if (ev == NULL) { 383 if (efifb->fb_addr == 0) { 384 printf("Please set hw.efifb.address and " 385 "hw.efifb.stride.\n"); 386 return (1); 387 } 388 389 /* 390 * The visible part of the frame buffer may not start at 391 * offset 0, so try to detect it. Note that we may not 392 * always be able to read from the frame buffer, which 393 * means that we may not be able to detect anything. In 394 * that case, we would take a long time scanning for a 395 * pixel change in the frame buffer, which would have it 396 * appear that we're hanging, so we limit the scan to 397 * 1/256th of the frame buffer. This number is mostly 398 * based on PR 202730 and the fact that on a MacBoook, 399 * where we can't read from the frame buffer the offset 400 * of the visible region is 0. In short: we want to scan 401 * enough to handle all adapters that have an offset 402 * larger than 0 and we want to scan as little as we can 403 * to not appear to hang when we can't read from the 404 * frame buffer. 405 */ 406 offset = efifb_uga_find_pixel(uga, 0, pciio, efifb->fb_addr, 407 efifb->fb_size >> 8); 408 if (offset == -1) { 409 printf("Unable to reliably detect frame buffer.\n"); 410 } else if (offset > 0) { 411 efifb->fb_addr += offset; 412 efifb->fb_size -= offset; 413 } 414 } else { 415 offset = 0; 416 efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4; 417 efifb->fb_addr = strtoul(ev, &p, 0); 418 if (*p != '\0') 419 return (1); 420 } 421 422 ev = getenv("hw.efifb.stride"); 423 if (ev == NULL) { 424 if (pciio != NULL && offset != -1) { 425 /* Determine the stride. */ 426 offset = efifb_uga_find_pixel(uga, 1, pciio, 427 efifb->fb_addr, horiz * 8); 428 if (offset != -1) 429 efifb->fb_stride = offset >> 2; 430 } else { 431 printf("Unable to reliably detect the stride.\n"); 432 } 433 } else { 434 efifb->fb_stride = strtoul(ev, &p, 0); 435 if (*p != '\0') 436 return (1); 437 } 438 439 /* 440 * We finalized on the stride, so recalculate the size of the 441 * frame buffer. 442 */ 443 efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4; 444 if (efifb->fb_addr == 0) 445 return (1); 446 return (0); 447 } 448 449 /* 450 * Fetch EDID info. Caller must free the buffer. 451 */ 452 static struct vesa_edid_info * 453 efifb_gop_get_edid(EFI_HANDLE gop) 454 { 455 const uint8_t magic[] = EDID_MAGIC; 456 EFI_EDID_ACTIVE_PROTOCOL *edid; 457 struct vesa_edid_info *edid_infop; 458 EFI_GUID *guid; 459 EFI_STATUS status; 460 size_t size; 461 462 guid = &active_edid_guid; 463 status = OpenProtocolByHandle(gop, guid, (void **)&edid); 464 if (status != EFI_SUCCESS) 465 return (NULL); 466 467 size = sizeof (*edid_infop); 468 if (size < edid->SizeOfEdid) 469 size = edid->SizeOfEdid; 470 471 edid_infop = calloc(1, size); 472 if (edid_infop == NULL) { 473 status = BS->CloseProtocol(gop, guid, IH, NULL); 474 return (NULL); 475 } 476 477 memcpy(edid_infop, edid->Edid, edid->SizeOfEdid); 478 status = BS->CloseProtocol(gop, guid, IH, NULL); 479 480 /* Validate EDID */ 481 if (memcmp(edid_infop, magic, sizeof (magic)) != 0) 482 goto error; 483 484 if (edid_infop->header.version != 1) 485 goto error; 486 487 return (edid_infop); 488 error: 489 free(edid_infop); 490 return (NULL); 491 } 492 493 static bool 494 efifb_get_edid(edid_res_list_t *res) 495 { 496 extern EFI_GRAPHICS_OUTPUT *gop; 497 bool rv = false; 498 499 if (edid_info == NULL) 500 edid_info = efifb_gop_get_edid(gop); 501 502 if (edid_info != NULL) 503 rv = gfx_get_edid_resolution(edid_info, res); 504 505 return (rv); 506 } 507 508 int 509 efi_find_framebuffer(struct efi_fb *efifb) 510 { 511 EFI_HANDLE h, *hlist; 512 UINTN nhandles, i, hsize; 513 extern EFI_GRAPHICS_OUTPUT *gop; 514 extern EFI_UGA_DRAW_PROTOCOL *uga; 515 EFI_STATUS status; 516 uint32_t mode; 517 518 if (gop != NULL) 519 return (efifb_from_gop(efifb, gop->Mode, gop->Mode->Info)); 520 521 hsize = 0; 522 hlist = NULL; 523 status = BS->LocateHandle(ByProtocol, &gop_guid, NULL, &hsize, hlist); 524 if (status == EFI_BUFFER_TOO_SMALL) { 525 hlist = malloc(hsize); 526 if (hlist == NULL) 527 return (ENOMEM); 528 status = BS->LocateHandle(ByProtocol, &gop_guid, NULL, &hsize, 529 hlist); 530 if (EFI_ERROR(status)) 531 free(hlist); 532 } 533 if (EFI_ERROR(status)) 534 return (efi_status_to_errno(status)); 535 536 nhandles = hsize / sizeof (*hlist); 537 538 /* 539 * Search for ConOut protocol, if not found, use first handle. 540 */ 541 h = *hlist; 542 for (i = 0; i < nhandles; i++) { 543 void *dummy = NULL; 544 545 status = OpenProtocolByHandle(hlist[i], &conout_guid, &dummy); 546 if (status == EFI_SUCCESS) { 547 h = hlist[i]; 548 break; 549 } 550 } 551 552 status = OpenProtocolByHandle(h, &gop_guid, (void **)&gop); 553 free(hlist); 554 555 if (status == EFI_SUCCESS) { 556 /* Save default mode. */ 557 if (default_mode == UINT32_MAX) { 558 default_mode = gop->Mode->Mode; 559 } 560 mode = gop_default_mode(); 561 if (mode != gop->Mode->Mode) 562 efifb_set_mode(gop, mode); 563 return (efifb_from_gop(efifb, gop->Mode, gop->Mode->Info)); 564 } 565 566 if (uga != NULL) 567 return (efifb_from_uga(efifb, uga)); 568 569 status = BS->LocateProtocol(&uga_guid, NULL, (void **)&uga); 570 if (status == EFI_SUCCESS) 571 return (efifb_from_uga(efifb, uga)); 572 573 return (1); 574 } 575 576 static void 577 print_efifb(int mode, struct efi_fb *efifb, int verbose) 578 { 579 uint_t depth; 580 edid_res_list_t res; 581 struct resolution *rp; 582 583 TAILQ_INIT(&res); 584 if (verbose == 1) { 585 printf("Framebuffer mode: %s\n", 586 plat_stdout_is_framebuffer() ? "on" : "off"); 587 if (efifb_get_edid(&res)) { 588 printf("EDID"); 589 while ((rp = TAILQ_FIRST(&res)) != NULL) { 590 printf(" %dx%d", rp->width, rp->height); 591 TAILQ_REMOVE(&res, rp, next); 592 free(rp); 593 } 594 printf("\n"); 595 } 596 } 597 598 if (mode >= 0) { 599 if (verbose == 1) 600 printf("GOP "); 601 printf("mode %d: ", mode); 602 } 603 depth = efifb_color_depth(efifb); 604 printf("%ux%ux%u", efifb->fb_width, efifb->fb_height, depth); 605 if (verbose) 606 printf(", stride=%u", efifb->fb_stride); 607 if (verbose) { 608 printf("\n frame buffer: address=%jx, size=%jx", 609 (uintmax_t)efifb->fb_addr, (uintmax_t)efifb->fb_size); 610 printf("\n color mask: R=%08x, G=%08x, B=%08x\n", 611 efifb->fb_mask_red, efifb->fb_mask_green, 612 efifb->fb_mask_blue); 613 if (efifb->fb_addr == 0) { 614 printf("Warning: this mode is not implementing the " 615 "linear framebuffer. The illumos\n\tconsole is " 616 "not available with this mode and will default to " 617 "ttya\n"); 618 } 619 } 620 } 621 622 static int 623 efifb_set_mode(EFI_GRAPHICS_OUTPUT *gop, uint_t mode) 624 { 625 EFI_STATUS status; 626 627 status = gop->SetMode(gop, mode); 628 if (EFI_ERROR(status)) { 629 snprintf(command_errbuf, sizeof (command_errbuf), 630 "Unable to set mode to %u (error=%lu)", 631 mode, EFI_ERROR_CODE(status)); 632 return (CMD_ERROR); 633 } 634 return (CMD_OK); 635 } 636 637 /* 638 * Verify existance of mode number or find mode by 639 * dimensions. If depth is not given, walk values 32, 24, 16, 8. 640 * Return MaxMode if mode is not found. 641 */ 642 static int 643 efifb_find_mode_xydm(UINT32 x, UINT32 y, int depth, int m) 644 { 645 extern EFI_GRAPHICS_OUTPUT *gop; 646 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info; 647 EFI_STATUS status; 648 UINTN infosz; 649 struct efi_fb fb; 650 UINT32 mode; 651 uint_t d, i; 652 653 if (m != -1) 654 i = 8; 655 else if (depth == -1) 656 i = 32; 657 else 658 i = depth; 659 660 while (i > 0) { 661 for (mode = 0; mode < gop->Mode->MaxMode; mode++) { 662 status = gop->QueryMode(gop, mode, &infosz, &info); 663 if (EFI_ERROR(status)) 664 continue; 665 666 if (m != -1) { 667 if ((UINT32)m == mode) 668 return (mode); 669 else 670 continue; 671 } 672 673 efifb_from_gop(&fb, gop->Mode, info); 674 d = efifb_color_depth(&fb); 675 if (x == fb.fb_width && y == fb.fb_height && d == i) 676 return (mode); 677 } 678 679 if (depth != -1) 680 break; 681 682 i -= 8; 683 } 684 685 return (gop->Mode->MaxMode); 686 } 687 688 static int 689 efifb_find_mode(char *str) 690 { 691 extern EFI_GRAPHICS_OUTPUT *gop; 692 int x, y, depth; 693 694 if (!gfx_parse_mode_str(str, &x, &y, &depth)) 695 return (gop->Mode->MaxMode); 696 697 return (efifb_find_mode_xydm(x, y, depth, -1)); 698 } 699 700 /* 701 * gop_default_mode(). Try to set mode based on EDID. 702 */ 703 static uint32_t 704 gop_default_mode(void) 705 { 706 edid_res_list_t res; 707 struct resolution *rp; 708 extern EFI_GRAPHICS_OUTPUT *gop; 709 UINT32 mode; 710 711 mode = gop->Mode->MaxMode; 712 TAILQ_INIT(&res); 713 if (efifb_get_edid(&res)) { 714 while ((rp = TAILQ_FIRST(&res)) != NULL) { 715 if (mode == gop->Mode->MaxMode) { 716 mode = efifb_find_mode_xydm( 717 rp->width, rp->height, -1, -1); 718 } 719 TAILQ_REMOVE(&res, rp, next); 720 free(rp); 721 } 722 } 723 724 if (mode == gop->Mode->MaxMode) 725 mode = default_mode; 726 727 return (mode); 728 } 729 730 COMMAND_SET(framebuffer, "framebuffer", "framebuffer mode management", 731 command_gop); 732 733 static int 734 command_gop(int argc, char *argv[]) 735 { 736 extern struct efi_fb efifb; 737 extern EFI_GRAPHICS_OUTPUT *gop; 738 struct efi_fb fb; 739 EFI_STATUS status; 740 char *arg, *cp; 741 uint_t mode; 742 743 if (gop == NULL) { 744 snprintf(command_errbuf, sizeof (command_errbuf), 745 "%s: Graphics Output Protocol not present", argv[0]); 746 return (CMD_ERROR); 747 } 748 749 if (argc < 2) 750 goto usage; 751 752 /* 753 * Note we can not turn the GOP itself off, but instead we instruct 754 * tem to use text mode. 755 */ 756 if (strcmp(argv[1], "off") == 0) { 757 if (argc != 2) 758 goto usage; 759 760 reset_font_flags(); 761 plat_cons_update_mode(EfiConsoleControlScreenText); 762 return (CMD_OK); 763 } 764 765 /* 766 * Set GOP to use default mode, then notify tem. 767 */ 768 if (strcmp(argv[1], "on") == 0) { 769 if (argc != 2) 770 goto usage; 771 772 reset_font_flags(); 773 mode = gop_default_mode(); 774 if (mode != gop->Mode->Mode) 775 efifb_set_mode(gop, mode); 776 777 plat_cons_update_mode(EfiConsoleControlScreenGraphics); 778 return (CMD_OK); 779 } 780 781 if (strcmp(argv[1], "set") == 0) { 782 int rv; 783 784 if (argc != 3) 785 goto usage; 786 787 arg = argv[2]; 788 if (strchr(arg, 'x') == NULL) { 789 errno = 0; 790 mode = strtoul(arg, &cp, 0); 791 if (errno != 0 || *arg == '\0' || cp[0] != '\0') { 792 snprintf(command_errbuf, 793 sizeof (command_errbuf), 794 "mode should be an integer"); 795 return (CMD_ERROR); 796 } 797 mode = efifb_find_mode_xydm(0, 0, 0, mode); 798 } else { 799 mode = efifb_find_mode(arg); 800 } 801 802 if (mode == gop->Mode->MaxMode) 803 mode = gop->Mode->Mode; 804 805 reset_font_flags(); 806 rv = efifb_set_mode(gop, mode); 807 plat_cons_update_mode(EfiConsoleControlScreenGraphics); 808 return (rv); 809 } 810 811 if (strcmp(argv[1], "get") == 0) { 812 if (argc != 2) 813 goto usage; 814 815 print_efifb(gop->Mode->Mode, &efifb, 1); 816 printf("\n"); 817 return (CMD_OK); 818 } 819 820 if (strcmp(argv[1], "list") == 0) { 821 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info; 822 UINTN infosz; 823 int depth, d = -1; 824 825 if (argc != 2 && argc != 3) 826 goto usage; 827 828 if (argc == 3) { 829 arg = argv[2]; 830 errno = 0; 831 d = strtoul(arg, &cp, 0); 832 if (errno != 0 || *arg == '\0' || cp[0] != '\0') { 833 snprintf(command_errbuf, 834 sizeof (command_errbuf), 835 "depth should be an integer"); 836 return (CMD_ERROR); 837 } 838 } 839 pager_open(); 840 for (mode = 0; mode < gop->Mode->MaxMode; mode++) { 841 status = gop->QueryMode(gop, mode, &infosz, &info); 842 if (EFI_ERROR(status)) 843 continue; 844 efifb_from_gop(&fb, gop->Mode, info); 845 depth = efifb_color_depth(&fb); 846 if (d != -1 && d != depth) 847 continue; 848 print_efifb(mode, &fb, 0); 849 if (pager_output("\n")) 850 break; 851 } 852 pager_close(); 853 return (CMD_OK); 854 } 855 856 usage: 857 snprintf(command_errbuf, sizeof (command_errbuf), 858 "usage: %s on | off | get | list [depth] | " 859 "set <display or GOP mode number>", argv[0]); 860 return (CMD_ERROR); 861 } 862 863 COMMAND_SET(uga, "uga", "universal graphics adapter", command_uga); 864 865 static int 866 command_uga(int argc, char *argv[]) 867 { 868 extern struct efi_fb efifb; 869 extern EFI_UGA_DRAW_PROTOCOL *uga; 870 871 if (uga == NULL) { 872 snprintf(command_errbuf, sizeof (command_errbuf), 873 "%s: UGA Protocol not present", argv[0]); 874 return (CMD_ERROR); 875 } 876 877 if (argc != 1) 878 goto usage; 879 880 if (efifb.fb_addr == 0) { 881 snprintf(command_errbuf, sizeof (command_errbuf), 882 "%s: Unable to get UGA information", argv[0]); 883 return (CMD_ERROR); 884 } 885 886 print_efifb(-1, &efifb, 1); 887 printf("\n"); 888 return (CMD_OK); 889 890 usage: 891 snprintf(command_errbuf, sizeof (command_errbuf), "usage: %s", argv[0]); 892 return (CMD_ERROR); 893 } 894