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