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