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