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 = NULL; 571 for (i = 0; i < nhandles; i++) { 572 EFI_GRAPHICS_OUTPUT *tgop; 573 void *dummy; 574 575 status = OpenProtocolByHandle(hlist[i], &gop_guid, (void **)&tgop); 576 if (status != EFI_SUCCESS) 577 continue; 578 579 if (tgop->Mode->Info->PixelFormat == PixelBltOnly || 580 tgop->Mode->Info->PixelFormat >= PixelFormatMax) 581 continue; 582 583 status = OpenProtocolByHandle(hlist[i], &conout_guid, &dummy); 584 if (status == EFI_SUCCESS) { 585 gop_handle = hlist[i]; 586 gop = tgop; 587 break; 588 } else if (gop_handle == NULL) { 589 gop_handle = hlist[i]; 590 gop = tgop; 591 } 592 } 593 594 free(hlist); 595 596 if (gop_handle != NULL) { 597 gfx_state->tg_fb_type = FB_GOP; 598 gfx_state->tg_private = gop; 599 if (edid_info == NULL) 600 edid_info = efifb_gop_get_edid(gop_handle); 601 } else { 602 status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga); 603 if (status == EFI_SUCCESS) { 604 gfx_state->tg_fb_type = FB_UGA; 605 gfx_state->tg_private = uga; 606 } else { 607 return (1); 608 } 609 } 610 611 switch (gfx_state->tg_fb_type) { 612 case FB_GOP: 613 rv = efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info); 614 break; 615 616 case FB_UGA: 617 rv = efifb_from_uga(&efifb); 618 break; 619 620 default: 621 return (1); 622 } 623 624 gfx_state->tg_fb.fb_addr = efifb.fb_addr; 625 gfx_state->tg_fb.fb_size = efifb.fb_size; 626 gfx_state->tg_fb.fb_height = efifb.fb_height; 627 gfx_state->tg_fb.fb_width = efifb.fb_width; 628 gfx_state->tg_fb.fb_stride = efifb.fb_stride; 629 gfx_state->tg_fb.fb_mask_red = efifb.fb_mask_red; 630 gfx_state->tg_fb.fb_mask_green = efifb.fb_mask_green; 631 gfx_state->tg_fb.fb_mask_blue = efifb.fb_mask_blue; 632 gfx_state->tg_fb.fb_mask_reserved = efifb.fb_mask_reserved; 633 634 gfx_state->tg_fb.fb_bpp = fls(efifb.fb_mask_red | efifb.fb_mask_green | 635 efifb.fb_mask_blue | efifb.fb_mask_reserved); 636 637 if (gfx_state->tg_shadow_fb != NULL) 638 BS->FreePages((EFI_PHYSICAL_ADDRESS)gfx_state->tg_shadow_fb, 639 gfx_state->tg_shadow_sz); 640 gfx_state->tg_shadow_sz = 641 EFI_SIZE_TO_PAGES(efifb.fb_height * efifb.fb_width * 642 sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL)); 643 status = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 644 gfx_state->tg_shadow_sz, 645 (EFI_PHYSICAL_ADDRESS *)&gfx_state->tg_shadow_fb); 646 if (status != EFI_SUCCESS) 647 gfx_state->tg_shadow_fb = NULL; 648 649 return (0); 650 } 651 652 static void 653 print_efifb(int mode, struct efi_fb *efifb, int verbose) 654 { 655 u_int depth; 656 657 if (mode >= 0) 658 printf("mode %d: ", mode); 659 depth = efifb_color_depth(efifb); 660 printf("%ux%ux%u, stride=%u", efifb->fb_width, efifb->fb_height, 661 depth, efifb->fb_stride); 662 if (verbose) { 663 printf("\n frame buffer: address=%jx, size=%jx", 664 (uintmax_t)efifb->fb_addr, (uintmax_t)efifb->fb_size); 665 printf("\n color mask: R=%08x, G=%08x, B=%08x\n", 666 efifb->fb_mask_red, efifb->fb_mask_green, 667 efifb->fb_mask_blue); 668 } 669 } 670 671 static bool 672 efi_resolution_compare(struct named_resolution *res, const char *cmp) 673 { 674 675 if (strcasecmp(res->name, cmp) == 0) 676 return (true); 677 if (res->alias != NULL && strcasecmp(res->alias, cmp) == 0) 678 return (true); 679 return (false); 680 } 681 682 683 static void 684 efi_get_max_resolution(int *width, int *height) 685 { 686 struct named_resolution *res; 687 char *maxres; 688 char *height_start, *width_start; 689 int idx; 690 691 *width = *height = 0; 692 maxres = getenv("efi_max_resolution"); 693 /* No max_resolution set? Bail out; choose highest resolution */ 694 if (maxres == NULL) 695 return; 696 /* See if it matches one of our known resolutions */ 697 for (idx = 0; idx < nitems(resolutions); ++idx) { 698 res = &resolutions[idx]; 699 if (efi_resolution_compare(res, maxres)) { 700 *width = res->width; 701 *height = res->height; 702 return; 703 } 704 } 705 /* Not a known resolution, try to parse it; make a copy we can modify */ 706 maxres = strdup(maxres); 707 if (maxres == NULL) 708 return; 709 height_start = strchr(maxres, 'x'); 710 if (height_start == NULL) { 711 free(maxres); 712 return; 713 } 714 width_start = maxres; 715 *height_start++ = 0; 716 /* Errors from this will effectively mean "no max" */ 717 *width = (int)strtol(width_start, NULL, 0); 718 *height = (int)strtol(height_start, NULL, 0); 719 free(maxres); 720 } 721 722 static int 723 gop_autoresize(void) 724 { 725 struct efi_fb efifb; 726 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info; 727 EFI_STATUS status; 728 UINTN infosz; 729 UINT32 best_mode, currdim, maxdim, mode; 730 int height, max_height, max_width, width; 731 732 best_mode = maxdim = 0; 733 efi_get_max_resolution(&max_width, &max_height); 734 for (mode = 0; mode < gop->Mode->MaxMode; mode++) { 735 status = gop->QueryMode(gop, mode, &infosz, &info); 736 if (EFI_ERROR(status)) 737 continue; 738 efifb_from_gop(&efifb, gop->Mode, info); 739 width = info->HorizontalResolution; 740 height = info->VerticalResolution; 741 currdim = width * height; 742 if (currdim > maxdim) { 743 if ((max_width != 0 && width > max_width) || 744 (max_height != 0 && height > max_height)) 745 continue; 746 maxdim = currdim; 747 best_mode = mode; 748 } 749 } 750 751 if (maxdim != 0) { 752 status = gop->SetMode(gop, best_mode); 753 if (EFI_ERROR(status)) { 754 snprintf(command_errbuf, sizeof(command_errbuf), 755 "gop_autoresize: Unable to set mode to %u (error=%lu)", 756 mode, EFI_ERROR_CODE(status)); 757 return (CMD_ERROR); 758 } 759 (void) cons_update_mode(true); 760 } 761 return (CMD_OK); 762 } 763 764 static int 765 text_autoresize() 766 { 767 SIMPLE_TEXT_OUTPUT_INTERFACE *conout; 768 EFI_STATUS status; 769 UINTN i, max_dim, best_mode, cols, rows; 770 771 conout = ST->ConOut; 772 max_dim = best_mode = 0; 773 for (i = 0; i < conout->Mode->MaxMode; i++) { 774 status = conout->QueryMode(conout, i, &cols, &rows); 775 if (EFI_ERROR(status)) 776 continue; 777 if (cols * rows > max_dim) { 778 max_dim = cols * rows; 779 best_mode = i; 780 } 781 } 782 if (max_dim > 0) 783 conout->SetMode(conout, best_mode); 784 (void) cons_update_mode(true); 785 return (CMD_OK); 786 } 787 788 static int 789 uga_autoresize(void) 790 { 791 792 return (text_autoresize()); 793 } 794 795 COMMAND_SET(efi_autoresize, "efi-autoresizecons", "EFI Auto-resize Console", command_autoresize); 796 797 static int 798 command_autoresize(int argc, char *argv[]) 799 { 800 char *textmode; 801 802 textmode = getenv("hw.vga.textmode"); 803 /* If it's set and non-zero, we'll select a console mode instead */ 804 if (textmode != NULL && strcmp(textmode, "0") != 0) 805 return (text_autoresize()); 806 807 if (gop != NULL) 808 return (gop_autoresize()); 809 810 if (uga != NULL) 811 return (uga_autoresize()); 812 813 snprintf(command_errbuf, sizeof(command_errbuf), 814 "%s: Neither Graphics Output Protocol nor Universal Graphics Adapter present", 815 argv[0]); 816 817 /* 818 * Default to text_autoresize if we have neither GOP or UGA. This won't 819 * give us the most ideal resolution, but it will at least leave us 820 * functional rather than failing the boot for an objectively bad 821 * reason. 822 */ 823 return (text_autoresize()); 824 } 825 826 COMMAND_SET(gop, "gop", "graphics output protocol", command_gop); 827 828 static int 829 command_gop(int argc, char *argv[]) 830 { 831 struct efi_fb efifb; 832 EFI_STATUS status; 833 u_int mode; 834 835 if (gop == NULL) { 836 snprintf(command_errbuf, sizeof(command_errbuf), 837 "%s: Graphics Output Protocol not present", argv[0]); 838 return (CMD_ERROR); 839 } 840 841 if (argc < 2) 842 goto usage; 843 844 if (!strcmp(argv[1], "set")) { 845 char *cp; 846 847 if (argc != 3) 848 goto usage; 849 mode = strtol(argv[2], &cp, 0); 850 if (cp[0] != '\0') { 851 sprintf(command_errbuf, "mode is an integer"); 852 return (CMD_ERROR); 853 } 854 status = gop->SetMode(gop, mode); 855 if (EFI_ERROR(status)) { 856 snprintf(command_errbuf, sizeof(command_errbuf), 857 "%s: Unable to set mode to %u (error=%lu)", 858 argv[0], mode, EFI_ERROR_CODE(status)); 859 return (CMD_ERROR); 860 } 861 (void) cons_update_mode(true); 862 } else if (strcmp(argv[1], "off") == 0) { 863 (void) cons_update_mode(false); 864 } else if (strcmp(argv[1], "get") == 0) { 865 edid_res_list_t res; 866 867 if (argc != 2) 868 goto usage; 869 TAILQ_INIT(&res); 870 efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info); 871 if (efifb_get_edid(&res)) { 872 struct resolution *rp; 873 874 printf("EDID"); 875 while ((rp = TAILQ_FIRST(&res)) != NULL) { 876 printf(" %dx%d", rp->width, rp->height); 877 TAILQ_REMOVE(&res, rp, next); 878 free(rp); 879 } 880 printf("\n"); 881 } else { 882 printf("no EDID information\n"); 883 } 884 print_efifb(gop->Mode->Mode, &efifb, 1); 885 printf("\n"); 886 } else if (!strcmp(argv[1], "list")) { 887 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info; 888 UINTN infosz; 889 890 if (argc != 2) 891 goto usage; 892 893 pager_open(); 894 for (mode = 0; mode < gop->Mode->MaxMode; mode++) { 895 status = gop->QueryMode(gop, mode, &infosz, &info); 896 if (EFI_ERROR(status)) 897 continue; 898 efifb_from_gop(&efifb, gop->Mode, info); 899 print_efifb(mode, &efifb, 0); 900 if (pager_output("\n")) 901 break; 902 } 903 pager_close(); 904 } 905 return (CMD_OK); 906 907 usage: 908 snprintf(command_errbuf, sizeof(command_errbuf), 909 "usage: %s [list | get | set <mode> | off]", argv[0]); 910 return (CMD_ERROR); 911 } 912 913 COMMAND_SET(uga, "uga", "universal graphics adapter", command_uga); 914 915 static int 916 command_uga(int argc, char *argv[]) 917 { 918 struct efi_fb efifb; 919 920 if (uga == NULL) { 921 snprintf(command_errbuf, sizeof(command_errbuf), 922 "%s: UGA Protocol not present", argv[0]); 923 return (CMD_ERROR); 924 } 925 926 if (argc != 1) 927 goto usage; 928 929 if (efifb_from_uga(&efifb) != CMD_OK) { 930 snprintf(command_errbuf, sizeof(command_errbuf), 931 "%s: Unable to get UGA information", argv[0]); 932 return (CMD_ERROR); 933 } 934 935 print_efifb(-1, &efifb, 1); 936 printf("\n"); 937 return (CMD_OK); 938 939 usage: 940 snprintf(command_errbuf, sizeof(command_errbuf), "usage: %s", argv[0]); 941 return (CMD_ERROR); 942 } 943