1ca987d46SWarner Losh /*- 2ca987d46SWarner Losh * Copyright (c) 2013 The FreeBSD Foundation 3ca987d46SWarner Losh * All rights reserved. 4ca987d46SWarner Losh * 5ca987d46SWarner Losh * This software was developed by Benno Rice under sponsorship from 6ca987d46SWarner Losh * the FreeBSD Foundation. 7ca987d46SWarner Losh * Redistribution and use in source and binary forms, with or without 8ca987d46SWarner Losh * modification, are permitted provided that the following conditions 9ca987d46SWarner Losh * are met: 10ca987d46SWarner Losh * 1. Redistributions of source code must retain the above copyright 11ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer. 12ca987d46SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 13ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer in the 14ca987d46SWarner Losh * documentation and/or other materials provided with the distribution. 15ca987d46SWarner Losh * 16ca987d46SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17ca987d46SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18ca987d46SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19ca987d46SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20ca987d46SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21ca987d46SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22ca987d46SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23ca987d46SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24ca987d46SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25ca987d46SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26ca987d46SWarner Losh * SUCH DAMAGE. 27ca987d46SWarner Losh */ 28ca987d46SWarner Losh 29ca987d46SWarner Losh #include <sys/cdefs.h> 30ca987d46SWarner Losh __FBSDID("$FreeBSD$"); 31ca987d46SWarner Losh 32ca987d46SWarner Losh #include <bootstrap.h> 33ca987d46SWarner Losh #include <sys/endian.h> 34*05e8899dSKyle Evans #include <sys/param.h> 35ca987d46SWarner Losh #include <stand.h> 36ca987d46SWarner Losh 37ca987d46SWarner Losh #include <efi.h> 38ca987d46SWarner Losh #include <efilib.h> 39ca987d46SWarner Losh #include <efiuga.h> 40ca987d46SWarner Losh #include <efipciio.h> 41ca987d46SWarner Losh #include <machine/metadata.h> 42ca987d46SWarner Losh 43ca987d46SWarner Losh #include "framebuffer.h" 44ca987d46SWarner Losh 45ca987d46SWarner Losh static EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 46ca987d46SWarner Losh static EFI_GUID pciio_guid = EFI_PCI_IO_PROTOCOL_GUID; 47ca987d46SWarner Losh static EFI_GUID uga_guid = EFI_UGA_DRAW_PROTOCOL_GUID; 48ca987d46SWarner Losh 49*05e8899dSKyle Evans static struct named_resolution { 50*05e8899dSKyle Evans const char *name; 51*05e8899dSKyle Evans const char *alias; 52*05e8899dSKyle Evans unsigned int width; 53*05e8899dSKyle Evans unsigned int height; 54*05e8899dSKyle Evans } resolutions[] = { 55*05e8899dSKyle Evans { 56*05e8899dSKyle Evans .name = "480p", 57*05e8899dSKyle Evans .width = 640, 58*05e8899dSKyle Evans .height = 480, 59*05e8899dSKyle Evans }, 60*05e8899dSKyle Evans { 61*05e8899dSKyle Evans .name = "720p", 62*05e8899dSKyle Evans .width = 1280, 63*05e8899dSKyle Evans .height = 720, 64*05e8899dSKyle Evans }, 65*05e8899dSKyle Evans { 66*05e8899dSKyle Evans .name = "1080p", 67*05e8899dSKyle Evans .width = 1920, 68*05e8899dSKyle Evans .height = 1080, 69*05e8899dSKyle Evans }, 70*05e8899dSKyle Evans { 71*05e8899dSKyle Evans .name = "2160p", 72*05e8899dSKyle Evans .alias = "4k", 73*05e8899dSKyle Evans .width = 3840, 74*05e8899dSKyle Evans .height = 2160, 75*05e8899dSKyle Evans }, 76*05e8899dSKyle Evans { 77*05e8899dSKyle Evans .name = "5k", 78*05e8899dSKyle Evans .width = 5120, 79*05e8899dSKyle Evans .height = 2880, 80*05e8899dSKyle Evans } 81*05e8899dSKyle Evans }; 82*05e8899dSKyle Evans 83ca987d46SWarner Losh static u_int 84ca987d46SWarner Losh efifb_color_depth(struct efi_fb *efifb) 85ca987d46SWarner Losh { 86ca987d46SWarner Losh uint32_t mask; 87ca987d46SWarner Losh u_int depth; 88ca987d46SWarner Losh 89ca987d46SWarner Losh mask = efifb->fb_mask_red | efifb->fb_mask_green | 90ca987d46SWarner Losh efifb->fb_mask_blue | efifb->fb_mask_reserved; 91ca987d46SWarner Losh if (mask == 0) 92ca987d46SWarner Losh return (0); 93ca987d46SWarner Losh for (depth = 1; mask != 1; depth++) 94ca987d46SWarner Losh mask >>= 1; 95ca987d46SWarner Losh return (depth); 96ca987d46SWarner Losh } 97ca987d46SWarner Losh 98ca987d46SWarner Losh static int 99ca987d46SWarner Losh efifb_mask_from_pixfmt(struct efi_fb *efifb, EFI_GRAPHICS_PIXEL_FORMAT pixfmt, 100ca987d46SWarner Losh EFI_PIXEL_BITMASK *pixinfo) 101ca987d46SWarner Losh { 102ca987d46SWarner Losh int result; 103ca987d46SWarner Losh 104ca987d46SWarner Losh result = 0; 105ca987d46SWarner Losh switch (pixfmt) { 106ca987d46SWarner Losh case PixelRedGreenBlueReserved8BitPerColor: 107ca987d46SWarner Losh efifb->fb_mask_red = 0x000000ff; 108ca987d46SWarner Losh efifb->fb_mask_green = 0x0000ff00; 109ca987d46SWarner Losh efifb->fb_mask_blue = 0x00ff0000; 110ca987d46SWarner Losh efifb->fb_mask_reserved = 0xff000000; 111ca987d46SWarner Losh break; 112ca987d46SWarner Losh case PixelBlueGreenRedReserved8BitPerColor: 113ca987d46SWarner Losh efifb->fb_mask_red = 0x00ff0000; 114ca987d46SWarner Losh efifb->fb_mask_green = 0x0000ff00; 115ca987d46SWarner Losh efifb->fb_mask_blue = 0x000000ff; 116ca987d46SWarner Losh efifb->fb_mask_reserved = 0xff000000; 117ca987d46SWarner Losh break; 118ca987d46SWarner Losh case PixelBitMask: 119ca987d46SWarner Losh efifb->fb_mask_red = pixinfo->RedMask; 120ca987d46SWarner Losh efifb->fb_mask_green = pixinfo->GreenMask; 121ca987d46SWarner Losh efifb->fb_mask_blue = pixinfo->BlueMask; 122ca987d46SWarner Losh efifb->fb_mask_reserved = pixinfo->ReservedMask; 123ca987d46SWarner Losh break; 124ca987d46SWarner Losh default: 125ca987d46SWarner Losh result = 1; 126ca987d46SWarner Losh break; 127ca987d46SWarner Losh } 128ca987d46SWarner Losh return (result); 129ca987d46SWarner Losh } 130ca987d46SWarner Losh 131ca987d46SWarner Losh static int 132ca987d46SWarner Losh efifb_from_gop(struct efi_fb *efifb, EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode, 133ca987d46SWarner Losh EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info) 134ca987d46SWarner Losh { 135ca987d46SWarner Losh int result; 136ca987d46SWarner Losh 137ca987d46SWarner Losh efifb->fb_addr = mode->FrameBufferBase; 138ca987d46SWarner Losh efifb->fb_size = mode->FrameBufferSize; 139ca987d46SWarner Losh efifb->fb_height = info->VerticalResolution; 140ca987d46SWarner Losh efifb->fb_width = info->HorizontalResolution; 141ca987d46SWarner Losh efifb->fb_stride = info->PixelsPerScanLine; 142ca987d46SWarner Losh result = efifb_mask_from_pixfmt(efifb, info->PixelFormat, 143ca987d46SWarner Losh &info->PixelInformation); 144ca987d46SWarner Losh return (result); 145ca987d46SWarner Losh } 146ca987d46SWarner Losh 147ca987d46SWarner Losh static ssize_t 148ca987d46SWarner Losh efifb_uga_find_pixel(EFI_UGA_DRAW_PROTOCOL *uga, u_int line, 149ca987d46SWarner Losh EFI_PCI_IO_PROTOCOL *pciio, uint64_t addr, uint64_t size) 150ca987d46SWarner Losh { 151ca987d46SWarner Losh EFI_UGA_PIXEL pix0, pix1; 152ca987d46SWarner Losh uint8_t *data1, *data2; 153ca987d46SWarner Losh size_t count, maxcount = 1024; 154ca987d46SWarner Losh ssize_t ofs; 155ca987d46SWarner Losh EFI_STATUS status; 156ca987d46SWarner Losh u_int idx; 157ca987d46SWarner Losh 158ca987d46SWarner Losh status = uga->Blt(uga, &pix0, EfiUgaVideoToBltBuffer, 159ca987d46SWarner Losh 0, line, 0, 0, 1, 1, 0); 160ca987d46SWarner Losh if (EFI_ERROR(status)) { 161ca987d46SWarner Losh printf("UGA BLT operation failed (video->buffer)"); 162ca987d46SWarner Losh return (-1); 163ca987d46SWarner Losh } 164ca987d46SWarner Losh pix1.Red = ~pix0.Red; 165ca987d46SWarner Losh pix1.Green = ~pix0.Green; 166ca987d46SWarner Losh pix1.Blue = ~pix0.Blue; 167ca987d46SWarner Losh pix1.Reserved = 0; 168ca987d46SWarner Losh 169ca987d46SWarner Losh data1 = calloc(maxcount, 2); 170ca987d46SWarner Losh if (data1 == NULL) { 171ca987d46SWarner Losh printf("Unable to allocate memory"); 172ca987d46SWarner Losh return (-1); 173ca987d46SWarner Losh } 174ca987d46SWarner Losh data2 = data1 + maxcount; 175ca987d46SWarner Losh 176ca987d46SWarner Losh ofs = 0; 177ca987d46SWarner Losh while (size > 0) { 178ca987d46SWarner Losh count = min(size, maxcount); 179ca987d46SWarner Losh 180ca987d46SWarner Losh status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32, 181ca987d46SWarner Losh EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2, 182ca987d46SWarner Losh data1); 183ca987d46SWarner Losh if (EFI_ERROR(status)) { 184ca987d46SWarner Losh printf("Error reading frame buffer (before)"); 185ca987d46SWarner Losh goto fail; 186ca987d46SWarner Losh } 187ca987d46SWarner Losh status = uga->Blt(uga, &pix1, EfiUgaBltBufferToVideo, 188ca987d46SWarner Losh 0, 0, 0, line, 1, 1, 0); 189ca987d46SWarner Losh if (EFI_ERROR(status)) { 190ca987d46SWarner Losh printf("UGA BLT operation failed (modify)"); 191ca987d46SWarner Losh goto fail; 192ca987d46SWarner Losh } 193ca987d46SWarner Losh status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32, 194ca987d46SWarner Losh EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2, 195ca987d46SWarner Losh data2); 196ca987d46SWarner Losh if (EFI_ERROR(status)) { 197ca987d46SWarner Losh printf("Error reading frame buffer (after)"); 198ca987d46SWarner Losh goto fail; 199ca987d46SWarner Losh } 200ca987d46SWarner Losh status = uga->Blt(uga, &pix0, EfiUgaBltBufferToVideo, 201ca987d46SWarner Losh 0, 0, 0, line, 1, 1, 0); 202ca987d46SWarner Losh if (EFI_ERROR(status)) { 203ca987d46SWarner Losh printf("UGA BLT operation failed (restore)"); 204ca987d46SWarner Losh goto fail; 205ca987d46SWarner Losh } 206ca987d46SWarner Losh for (idx = 0; idx < count; idx++) { 207ca987d46SWarner Losh if (data1[idx] != data2[idx]) { 208ca987d46SWarner Losh free(data1); 209ca987d46SWarner Losh return (ofs + (idx & ~3)); 210ca987d46SWarner Losh } 211ca987d46SWarner Losh } 212ca987d46SWarner Losh ofs += count; 213ca987d46SWarner Losh size -= count; 214ca987d46SWarner Losh } 215ca987d46SWarner Losh printf("No change detected in frame buffer"); 216ca987d46SWarner Losh 217ca987d46SWarner Losh fail: 218ca987d46SWarner Losh printf(" -- error %lu\n", EFI_ERROR_CODE(status)); 219ca987d46SWarner Losh free(data1); 220ca987d46SWarner Losh return (-1); 221ca987d46SWarner Losh } 222ca987d46SWarner Losh 223ca987d46SWarner Losh static EFI_PCI_IO_PROTOCOL * 224ca987d46SWarner Losh efifb_uga_get_pciio(void) 225ca987d46SWarner Losh { 226ca987d46SWarner Losh EFI_PCI_IO_PROTOCOL *pciio; 227ca987d46SWarner Losh EFI_HANDLE *buf, *hp; 228ca987d46SWarner Losh EFI_STATUS status; 229ca987d46SWarner Losh UINTN bufsz; 230ca987d46SWarner Losh 231ca987d46SWarner Losh /* Get all handles that support the UGA protocol. */ 232ca987d46SWarner Losh bufsz = 0; 233ca987d46SWarner Losh status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, NULL); 234ca987d46SWarner Losh if (status != EFI_BUFFER_TOO_SMALL) 235ca987d46SWarner Losh return (NULL); 236ca987d46SWarner Losh buf = malloc(bufsz); 237ca987d46SWarner Losh status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, buf); 238ca987d46SWarner Losh if (status != EFI_SUCCESS) { 239ca987d46SWarner Losh free(buf); 240ca987d46SWarner Losh return (NULL); 241ca987d46SWarner Losh } 242ca987d46SWarner Losh bufsz /= sizeof(EFI_HANDLE); 243ca987d46SWarner Losh 244ca987d46SWarner Losh /* Get the PCI I/O interface of the first handle that supports it. */ 245ca987d46SWarner Losh pciio = NULL; 246ca987d46SWarner Losh for (hp = buf; hp < buf + bufsz; hp++) { 247ca987d46SWarner Losh status = BS->HandleProtocol(*hp, &pciio_guid, (void **)&pciio); 248ca987d46SWarner Losh if (status == EFI_SUCCESS) { 249ca987d46SWarner Losh free(buf); 250ca987d46SWarner Losh return (pciio); 251ca987d46SWarner Losh } 252ca987d46SWarner Losh } 253ca987d46SWarner Losh free(buf); 254ca987d46SWarner Losh return (NULL); 255ca987d46SWarner Losh } 256ca987d46SWarner Losh 257ca987d46SWarner Losh static EFI_STATUS 258ca987d46SWarner Losh efifb_uga_locate_framebuffer(EFI_PCI_IO_PROTOCOL *pciio, uint64_t *addrp, 259ca987d46SWarner Losh uint64_t *sizep) 260ca987d46SWarner Losh { 261ca987d46SWarner Losh uint8_t *resattr; 262ca987d46SWarner Losh uint64_t addr, size; 263ca987d46SWarner Losh EFI_STATUS status; 264ca987d46SWarner Losh u_int bar; 265ca987d46SWarner Losh 266ca987d46SWarner Losh if (pciio == NULL) 267ca987d46SWarner Losh return (EFI_DEVICE_ERROR); 268ca987d46SWarner Losh 269ca987d46SWarner Losh /* Attempt to get the frame buffer address (imprecise). */ 270ca987d46SWarner Losh *addrp = 0; 271ca987d46SWarner Losh *sizep = 0; 272ca987d46SWarner Losh for (bar = 0; bar < 6; bar++) { 273ca987d46SWarner Losh status = pciio->GetBarAttributes(pciio, bar, NULL, 274ca987d46SWarner Losh (void **)&resattr); 275ca987d46SWarner Losh if (status != EFI_SUCCESS) 276ca987d46SWarner Losh continue; 277ca987d46SWarner Losh /* XXX magic offsets and constants. */ 278ca987d46SWarner Losh if (resattr[0] == 0x87 && resattr[3] == 0) { 279ca987d46SWarner Losh /* 32-bit address space descriptor (MEMIO) */ 280ca987d46SWarner Losh addr = le32dec(resattr + 10); 281ca987d46SWarner Losh size = le32dec(resattr + 22); 282ca987d46SWarner Losh } else if (resattr[0] == 0x8a && resattr[3] == 0) { 283ca987d46SWarner Losh /* 64-bit address space descriptor (MEMIO) */ 284ca987d46SWarner Losh addr = le64dec(resattr + 14); 285ca987d46SWarner Losh size = le64dec(resattr + 38); 286ca987d46SWarner Losh } else { 287ca987d46SWarner Losh addr = 0; 288ca987d46SWarner Losh size = 0; 289ca987d46SWarner Losh } 290ca987d46SWarner Losh BS->FreePool(resattr); 291ca987d46SWarner Losh if (addr == 0 || size == 0) 292ca987d46SWarner Losh continue; 293ca987d46SWarner Losh 294ca987d46SWarner Losh /* We assume the largest BAR is the frame buffer. */ 295ca987d46SWarner Losh if (size > *sizep) { 296ca987d46SWarner Losh *addrp = addr; 297ca987d46SWarner Losh *sizep = size; 298ca987d46SWarner Losh } 299ca987d46SWarner Losh } 300ca987d46SWarner Losh return ((*addrp == 0 || *sizep == 0) ? EFI_DEVICE_ERROR : 0); 301ca987d46SWarner Losh } 302ca987d46SWarner Losh 303ca987d46SWarner Losh static int 304ca987d46SWarner Losh efifb_from_uga(struct efi_fb *efifb, EFI_UGA_DRAW_PROTOCOL *uga) 305ca987d46SWarner Losh { 306ca987d46SWarner Losh EFI_PCI_IO_PROTOCOL *pciio; 307ca987d46SWarner Losh char *ev, *p; 308ca987d46SWarner Losh EFI_STATUS status; 309ca987d46SWarner Losh ssize_t offset; 310ca987d46SWarner Losh uint64_t fbaddr; 311ca987d46SWarner Losh uint32_t horiz, vert, stride; 312ca987d46SWarner Losh uint32_t np, depth, refresh; 313ca987d46SWarner Losh 314ca987d46SWarner Losh status = uga->GetMode(uga, &horiz, &vert, &depth, &refresh); 315ca987d46SWarner Losh if (EFI_ERROR(status)) 316ca987d46SWarner Losh return (1); 317ca987d46SWarner Losh efifb->fb_height = vert; 318ca987d46SWarner Losh efifb->fb_width = horiz; 319ca987d46SWarner Losh /* Paranoia... */ 320ca987d46SWarner Losh if (efifb->fb_height == 0 || efifb->fb_width == 0) 321ca987d46SWarner Losh return (1); 322ca987d46SWarner Losh 323ca987d46SWarner Losh /* The color masks are fixed AFAICT. */ 324ca987d46SWarner Losh efifb_mask_from_pixfmt(efifb, PixelBlueGreenRedReserved8BitPerColor, 325ca987d46SWarner Losh NULL); 326ca987d46SWarner Losh 327ca987d46SWarner Losh /* pciio can be NULL on return! */ 328ca987d46SWarner Losh pciio = efifb_uga_get_pciio(); 329ca987d46SWarner Losh 330ca987d46SWarner Losh /* Try to find the frame buffer. */ 331ca987d46SWarner Losh status = efifb_uga_locate_framebuffer(pciio, &efifb->fb_addr, 332ca987d46SWarner Losh &efifb->fb_size); 333ca987d46SWarner Losh if (EFI_ERROR(status)) { 334ca987d46SWarner Losh efifb->fb_addr = 0; 335ca987d46SWarner Losh efifb->fb_size = 0; 336ca987d46SWarner Losh } 337ca987d46SWarner Losh 338ca987d46SWarner Losh /* 339ca987d46SWarner Losh * There's no reliable way to detect the frame buffer or the 340ca987d46SWarner Losh * offset within the frame buffer of the visible region, nor 341ca987d46SWarner Losh * the stride. Our only option is to look at the system and 342ca987d46SWarner Losh * fill in the blanks based on that. Luckily, UGA was mostly 343ca987d46SWarner Losh * only used on Apple hardware. 344ca987d46SWarner Losh */ 345ca987d46SWarner Losh offset = -1; 346ca987d46SWarner Losh ev = getenv("smbios.system.maker"); 347ca987d46SWarner Losh if (ev != NULL && !strcmp(ev, "Apple Inc.")) { 348ca987d46SWarner Losh ev = getenv("smbios.system.product"); 349ca987d46SWarner Losh if (ev != NULL && !strcmp(ev, "iMac7,1")) { 350ca987d46SWarner Losh /* These are the expected values we should have. */ 351ca987d46SWarner Losh horiz = 1680; 352ca987d46SWarner Losh vert = 1050; 353ca987d46SWarner Losh fbaddr = 0xc0000000; 354ca987d46SWarner Losh /* These are the missing bits. */ 355ca987d46SWarner Losh offset = 0x10000; 356ca987d46SWarner Losh stride = 1728; 357ca987d46SWarner Losh } else if (ev != NULL && !strcmp(ev, "MacBook3,1")) { 358ca987d46SWarner Losh /* These are the expected values we should have. */ 359ca987d46SWarner Losh horiz = 1280; 360ca987d46SWarner Losh vert = 800; 361ca987d46SWarner Losh fbaddr = 0xc0000000; 362ca987d46SWarner Losh /* These are the missing bits. */ 363ca987d46SWarner Losh offset = 0x0; 364ca987d46SWarner Losh stride = 2048; 365ca987d46SWarner Losh } 366ca987d46SWarner Losh } 367ca987d46SWarner Losh 368ca987d46SWarner Losh /* 369ca987d46SWarner Losh * If this is hardware we know, make sure that it looks familiar 370ca987d46SWarner Losh * before we accept our hardcoded values. 371ca987d46SWarner Losh */ 372ca987d46SWarner Losh if (offset >= 0 && efifb->fb_width == horiz && 373ca987d46SWarner Losh efifb->fb_height == vert && efifb->fb_addr == fbaddr) { 374ca987d46SWarner Losh efifb->fb_addr += offset; 375ca987d46SWarner Losh efifb->fb_size -= offset; 376ca987d46SWarner Losh efifb->fb_stride = stride; 377ca987d46SWarner Losh return (0); 378ca987d46SWarner Losh } else if (offset >= 0) { 379ca987d46SWarner Losh printf("Hardware make/model known, but graphics not " 380ca987d46SWarner Losh "as expected.\n"); 381ca987d46SWarner Losh printf("Console may not work!\n"); 382ca987d46SWarner Losh } 383ca987d46SWarner Losh 384ca987d46SWarner Losh /* 385ca987d46SWarner Losh * The stride is equal or larger to the width. Often it's the 386ca987d46SWarner Losh * next larger power of two. We'll start with that... 387ca987d46SWarner Losh */ 388ca987d46SWarner Losh efifb->fb_stride = efifb->fb_width; 389ca987d46SWarner Losh do { 390ca987d46SWarner Losh np = efifb->fb_stride & (efifb->fb_stride - 1); 391ca987d46SWarner Losh if (np) { 392ca987d46SWarner Losh efifb->fb_stride |= (np - 1); 393ca987d46SWarner Losh efifb->fb_stride++; 394ca987d46SWarner Losh } 395ca987d46SWarner Losh } while (np); 396ca987d46SWarner Losh 397ca987d46SWarner Losh ev = getenv("hw.efifb.address"); 398ca987d46SWarner Losh if (ev == NULL) { 399ca987d46SWarner Losh if (efifb->fb_addr == 0) { 400ca987d46SWarner Losh printf("Please set hw.efifb.address and " 401ca987d46SWarner Losh "hw.efifb.stride.\n"); 402ca987d46SWarner Losh return (1); 403ca987d46SWarner Losh } 404ca987d46SWarner Losh 405ca987d46SWarner Losh /* 406ca987d46SWarner Losh * The visible part of the frame buffer may not start at 407ca987d46SWarner Losh * offset 0, so try to detect it. Note that we may not 408ca987d46SWarner Losh * always be able to read from the frame buffer, which 409ca987d46SWarner Losh * means that we may not be able to detect anything. In 410ca987d46SWarner Losh * that case, we would take a long time scanning for a 411ca987d46SWarner Losh * pixel change in the frame buffer, which would have it 412ca987d46SWarner Losh * appear that we're hanging, so we limit the scan to 413ca987d46SWarner Losh * 1/256th of the frame buffer. This number is mostly 414ca987d46SWarner Losh * based on PR 202730 and the fact that on a MacBoook, 415ca987d46SWarner Losh * where we can't read from the frame buffer the offset 416ca987d46SWarner Losh * of the visible region is 0. In short: we want to scan 417ca987d46SWarner Losh * enough to handle all adapters that have an offset 418ca987d46SWarner Losh * larger than 0 and we want to scan as little as we can 419ca987d46SWarner Losh * to not appear to hang when we can't read from the 420ca987d46SWarner Losh * frame buffer. 421ca987d46SWarner Losh */ 422ca987d46SWarner Losh offset = efifb_uga_find_pixel(uga, 0, pciio, efifb->fb_addr, 423ca987d46SWarner Losh efifb->fb_size >> 8); 424ca987d46SWarner Losh if (offset == -1) { 425ca987d46SWarner Losh printf("Unable to reliably detect frame buffer.\n"); 426ca987d46SWarner Losh } else if (offset > 0) { 427ca987d46SWarner Losh efifb->fb_addr += offset; 428ca987d46SWarner Losh efifb->fb_size -= offset; 429ca987d46SWarner Losh } 430ca987d46SWarner Losh } else { 431ca987d46SWarner Losh offset = 0; 432ca987d46SWarner Losh efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4; 433ca987d46SWarner Losh efifb->fb_addr = strtoul(ev, &p, 0); 434ca987d46SWarner Losh if (*p != '\0') 435ca987d46SWarner Losh return (1); 436ca987d46SWarner Losh } 437ca987d46SWarner Losh 438ca987d46SWarner Losh ev = getenv("hw.efifb.stride"); 439ca987d46SWarner Losh if (ev == NULL) { 440ca987d46SWarner Losh if (pciio != NULL && offset != -1) { 441ca987d46SWarner Losh /* Determine the stride. */ 442ca987d46SWarner Losh offset = efifb_uga_find_pixel(uga, 1, pciio, 443ca987d46SWarner Losh efifb->fb_addr, horiz * 8); 444ca987d46SWarner Losh if (offset != -1) 445ca987d46SWarner Losh efifb->fb_stride = offset >> 2; 446ca987d46SWarner Losh } else { 447ca987d46SWarner Losh printf("Unable to reliably detect the stride.\n"); 448ca987d46SWarner Losh } 449ca987d46SWarner Losh } else { 450ca987d46SWarner Losh efifb->fb_stride = strtoul(ev, &p, 0); 451ca987d46SWarner Losh if (*p != '\0') 452ca987d46SWarner Losh return (1); 453ca987d46SWarner Losh } 454ca987d46SWarner Losh 455ca987d46SWarner Losh /* 456ca987d46SWarner Losh * We finalized on the stride, so recalculate the size of the 457ca987d46SWarner Losh * frame buffer. 458ca987d46SWarner Losh */ 459ca987d46SWarner Losh efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4; 460ca987d46SWarner Losh return (0); 461ca987d46SWarner Losh } 462ca987d46SWarner Losh 463ca987d46SWarner Losh int 464ca987d46SWarner Losh efi_find_framebuffer(struct efi_fb *efifb) 465ca987d46SWarner Losh { 466ca987d46SWarner Losh EFI_GRAPHICS_OUTPUT *gop; 467ca987d46SWarner Losh EFI_UGA_DRAW_PROTOCOL *uga; 468ca987d46SWarner Losh EFI_STATUS status; 469ca987d46SWarner Losh 470ca987d46SWarner Losh status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop); 471ca987d46SWarner Losh if (status == EFI_SUCCESS) 472ca987d46SWarner Losh return (efifb_from_gop(efifb, gop->Mode, gop->Mode->Info)); 473ca987d46SWarner Losh 474ca987d46SWarner Losh status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga); 475ca987d46SWarner Losh if (status == EFI_SUCCESS) 476ca987d46SWarner Losh return (efifb_from_uga(efifb, uga)); 477ca987d46SWarner Losh 478ca987d46SWarner Losh return (1); 479ca987d46SWarner Losh } 480ca987d46SWarner Losh 481ca987d46SWarner Losh static void 482ca987d46SWarner Losh print_efifb(int mode, struct efi_fb *efifb, int verbose) 483ca987d46SWarner Losh { 484ca987d46SWarner Losh u_int depth; 485ca987d46SWarner Losh 486ca987d46SWarner Losh if (mode >= 0) 487ca987d46SWarner Losh printf("mode %d: ", mode); 488ca987d46SWarner Losh depth = efifb_color_depth(efifb); 489ca987d46SWarner Losh printf("%ux%ux%u, stride=%u", efifb->fb_width, efifb->fb_height, 490ca987d46SWarner Losh depth, efifb->fb_stride); 491ca987d46SWarner Losh if (verbose) { 492ca987d46SWarner Losh printf("\n frame buffer: address=%jx, size=%jx", 493ca987d46SWarner Losh (uintmax_t)efifb->fb_addr, (uintmax_t)efifb->fb_size); 494ca987d46SWarner Losh printf("\n color mask: R=%08x, G=%08x, B=%08x\n", 495ca987d46SWarner Losh efifb->fb_mask_red, efifb->fb_mask_green, 496ca987d46SWarner Losh efifb->fb_mask_blue); 497ca987d46SWarner Losh } 498ca987d46SWarner Losh } 499ca987d46SWarner Losh 500*05e8899dSKyle Evans static bool 501*05e8899dSKyle Evans efi_resolution_compare(struct named_resolution *res, const char *cmp) 502*05e8899dSKyle Evans { 503*05e8899dSKyle Evans 504*05e8899dSKyle Evans if (strcasecmp(res->name, cmp) == 0) 505*05e8899dSKyle Evans return (true); 506*05e8899dSKyle Evans if (res->alias != NULL && strcasecmp(res->alias, cmp) == 0) 507*05e8899dSKyle Evans return (true); 508*05e8899dSKyle Evans return (false); 509*05e8899dSKyle Evans } 510*05e8899dSKyle Evans 511*05e8899dSKyle Evans 512*05e8899dSKyle Evans static void 513*05e8899dSKyle Evans efi_get_max_resolution(int *width, int *height) 514*05e8899dSKyle Evans { 515*05e8899dSKyle Evans struct named_resolution *res; 516*05e8899dSKyle Evans char *maxres; 517*05e8899dSKyle Evans char *height_start, *width_start; 518*05e8899dSKyle Evans int idx; 519*05e8899dSKyle Evans 520*05e8899dSKyle Evans *width = *height = 0; 521*05e8899dSKyle Evans maxres = getenv("efi_max_resolution"); 522*05e8899dSKyle Evans /* No max_resolution set? Bail out; choose highest resolution */ 523*05e8899dSKyle Evans if (maxres == NULL) 524*05e8899dSKyle Evans return; 525*05e8899dSKyle Evans /* See if it matches one of our known resolutions */ 526*05e8899dSKyle Evans for (idx = 0; idx < nitems(resolutions); ++idx) { 527*05e8899dSKyle Evans res = &resolutions[idx]; 528*05e8899dSKyle Evans if (efi_resolution_compare(res, maxres)) { 529*05e8899dSKyle Evans *width = res->width; 530*05e8899dSKyle Evans *height = res->height; 531*05e8899dSKyle Evans return; 532*05e8899dSKyle Evans } 533*05e8899dSKyle Evans } 534*05e8899dSKyle Evans /* Not a known resolution, try to parse it; make a copy we can modify */ 535*05e8899dSKyle Evans maxres = strdup(maxres); 536*05e8899dSKyle Evans if (maxres == NULL) 537*05e8899dSKyle Evans return; 538*05e8899dSKyle Evans height_start = strchr(maxres, 'x'); 539*05e8899dSKyle Evans if (height_start == NULL) { 540*05e8899dSKyle Evans free(maxres); 541*05e8899dSKyle Evans return; 542*05e8899dSKyle Evans } 543*05e8899dSKyle Evans width_start = maxres; 544*05e8899dSKyle Evans *height_start++ = 0; 545*05e8899dSKyle Evans /* Errors from this will effectively mean "no max" */ 546*05e8899dSKyle Evans *width = (int)strtol(width_start, NULL, 0); 547*05e8899dSKyle Evans *height = (int)strtol(height_start, NULL, 0); 548*05e8899dSKyle Evans free(maxres); 549*05e8899dSKyle Evans } 550*05e8899dSKyle Evans 5515f8cfbe1SKyle Evans static int 5525f8cfbe1SKyle Evans gop_autoresize(EFI_GRAPHICS_OUTPUT *gop) 5535f8cfbe1SKyle Evans { 5545f8cfbe1SKyle Evans struct efi_fb efifb; 5555f8cfbe1SKyle Evans EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info; 5565f8cfbe1SKyle Evans EFI_STATUS status; 5575f8cfbe1SKyle Evans UINTN infosz; 5585f8cfbe1SKyle Evans UINT32 best_mode, currdim, maxdim, mode; 559*05e8899dSKyle Evans int height, max_height, max_width, width; 5605f8cfbe1SKyle Evans 5615f8cfbe1SKyle Evans best_mode = maxdim = 0; 562*05e8899dSKyle Evans efi_get_max_resolution(&max_width, &max_height); 5635f8cfbe1SKyle Evans for (mode = 0; mode < gop->Mode->MaxMode; mode++) { 5645f8cfbe1SKyle Evans status = gop->QueryMode(gop, mode, &infosz, &info); 5655f8cfbe1SKyle Evans if (EFI_ERROR(status)) 5665f8cfbe1SKyle Evans continue; 5675f8cfbe1SKyle Evans efifb_from_gop(&efifb, gop->Mode, info); 568*05e8899dSKyle Evans width = info->HorizontalResolution; 569*05e8899dSKyle Evans height = info->VerticalResolution; 570*05e8899dSKyle Evans currdim = width * height; 5715f8cfbe1SKyle Evans if (currdim > maxdim) { 572*05e8899dSKyle Evans if ((max_width != 0 && width > max_width) || 573*05e8899dSKyle Evans (max_height != 0 && height > max_height)) 574*05e8899dSKyle Evans continue; 5755f8cfbe1SKyle Evans maxdim = currdim; 5765f8cfbe1SKyle Evans best_mode = mode; 5775f8cfbe1SKyle Evans } 5785f8cfbe1SKyle Evans } 5795f8cfbe1SKyle Evans 5805f8cfbe1SKyle Evans status = gop->SetMode(gop, best_mode); 5815f8cfbe1SKyle Evans if (EFI_ERROR(status)) { 5825f8cfbe1SKyle Evans snprintf(command_errbuf, sizeof(command_errbuf), 5835f8cfbe1SKyle Evans "gop_autoresize: Unable to set mode to %u (error=%lu)", 5845f8cfbe1SKyle Evans mode, EFI_ERROR_CODE(status)); 5855f8cfbe1SKyle Evans return (CMD_ERROR); 5865f8cfbe1SKyle Evans } 5875f8cfbe1SKyle Evans return (CMD_OK); 5885f8cfbe1SKyle Evans } 5895f8cfbe1SKyle Evans 5905f8cfbe1SKyle Evans static int 5915f8cfbe1SKyle Evans uga_autoresize(EFI_UGA_DRAW_PROTOCOL *gop) 5925f8cfbe1SKyle Evans { 5935f8cfbe1SKyle Evans 5945f8cfbe1SKyle Evans return (CMD_OK); 5955f8cfbe1SKyle Evans } 5965f8cfbe1SKyle Evans 5975f8cfbe1SKyle Evans COMMAND_SET(efi_autoresize, "efi-autoresizecons", "EFI Auto-resize Console", command_autoresize); 5985f8cfbe1SKyle Evans 5995f8cfbe1SKyle Evans static int 6005f8cfbe1SKyle Evans command_autoresize(int argc, char *argv[]) 6015f8cfbe1SKyle Evans { 6025f8cfbe1SKyle Evans EFI_GRAPHICS_OUTPUT *gop; 6035f8cfbe1SKyle Evans EFI_UGA_DRAW_PROTOCOL *uga; 6045f8cfbe1SKyle Evans EFI_STATUS status; 6055f8cfbe1SKyle Evans u_int mode; 6065f8cfbe1SKyle Evans 6075f8cfbe1SKyle Evans gop = NULL; 6085f8cfbe1SKyle Evans uga = NULL; 6095f8cfbe1SKyle Evans status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop); 6105f8cfbe1SKyle Evans if (EFI_ERROR(status) == 0) 6115f8cfbe1SKyle Evans return (gop_autoresize(gop)); 6125f8cfbe1SKyle Evans 6135f8cfbe1SKyle Evans status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga); 6145f8cfbe1SKyle Evans if (EFI_ERROR(status) == 0) 6155f8cfbe1SKyle Evans return (uga_autoresize(uga)); 6165f8cfbe1SKyle Evans 6175f8cfbe1SKyle Evans snprintf(command_errbuf, sizeof(command_errbuf), 6185f8cfbe1SKyle Evans "%s: Neither Graphics Output Protocol nor Universal Graphics Adapter present", 6195f8cfbe1SKyle Evans argv[0]); 6205f8cfbe1SKyle Evans return (CMD_ERROR); 6215f8cfbe1SKyle Evans } 6225f8cfbe1SKyle Evans 623ca987d46SWarner Losh COMMAND_SET(gop, "gop", "graphics output protocol", command_gop); 624ca987d46SWarner Losh 625ca987d46SWarner Losh static int 626ca987d46SWarner Losh command_gop(int argc, char *argv[]) 627ca987d46SWarner Losh { 628ca987d46SWarner Losh struct efi_fb efifb; 629ca987d46SWarner Losh EFI_GRAPHICS_OUTPUT *gop; 630ca987d46SWarner Losh EFI_STATUS status; 631ca987d46SWarner Losh u_int mode; 632ca987d46SWarner Losh 633ca987d46SWarner Losh status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop); 634ca987d46SWarner Losh if (EFI_ERROR(status)) { 635ca987d46SWarner Losh snprintf(command_errbuf, sizeof(command_errbuf), 636ca987d46SWarner Losh "%s: Graphics Output Protocol not present (error=%lu)", 637ca987d46SWarner Losh argv[0], EFI_ERROR_CODE(status)); 638ca987d46SWarner Losh return (CMD_ERROR); 639ca987d46SWarner Losh } 640ca987d46SWarner Losh 641ca987d46SWarner Losh if (argc < 2) 642ca987d46SWarner Losh goto usage; 643ca987d46SWarner Losh 644ca987d46SWarner Losh if (!strcmp(argv[1], "set")) { 645ca987d46SWarner Losh char *cp; 646ca987d46SWarner Losh 647ca987d46SWarner Losh if (argc != 3) 648ca987d46SWarner Losh goto usage; 649ca987d46SWarner Losh mode = strtol(argv[2], &cp, 0); 650ca987d46SWarner Losh if (cp[0] != '\0') { 651ca987d46SWarner Losh sprintf(command_errbuf, "mode is an integer"); 652ca987d46SWarner Losh return (CMD_ERROR); 653ca987d46SWarner Losh } 654ca987d46SWarner Losh status = gop->SetMode(gop, mode); 655ca987d46SWarner Losh if (EFI_ERROR(status)) { 656ca987d46SWarner Losh snprintf(command_errbuf, sizeof(command_errbuf), 657ca987d46SWarner Losh "%s: Unable to set mode to %u (error=%lu)", 658ca987d46SWarner Losh argv[0], mode, EFI_ERROR_CODE(status)); 659ca987d46SWarner Losh return (CMD_ERROR); 660ca987d46SWarner Losh } 661ca987d46SWarner Losh } else if (!strcmp(argv[1], "get")) { 662ca987d46SWarner Losh if (argc != 2) 663ca987d46SWarner Losh goto usage; 664ca987d46SWarner Losh efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info); 665ca987d46SWarner Losh print_efifb(gop->Mode->Mode, &efifb, 1); 666ca987d46SWarner Losh printf("\n"); 667ca987d46SWarner Losh } else if (!strcmp(argv[1], "list")) { 668ca987d46SWarner Losh EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info; 669ca987d46SWarner Losh UINTN infosz; 670ca987d46SWarner Losh 671ca987d46SWarner Losh if (argc != 2) 672ca987d46SWarner Losh goto usage; 673ca987d46SWarner Losh pager_open(); 674ca987d46SWarner Losh for (mode = 0; mode < gop->Mode->MaxMode; mode++) { 675ca987d46SWarner Losh status = gop->QueryMode(gop, mode, &infosz, &info); 676ca987d46SWarner Losh if (EFI_ERROR(status)) 677ca987d46SWarner Losh continue; 678ca987d46SWarner Losh efifb_from_gop(&efifb, gop->Mode, info); 679ca987d46SWarner Losh print_efifb(mode, &efifb, 0); 680ca987d46SWarner Losh if (pager_output("\n")) 681ca987d46SWarner Losh break; 682ca987d46SWarner Losh } 683ca987d46SWarner Losh pager_close(); 684ca987d46SWarner Losh } 685ca987d46SWarner Losh return (CMD_OK); 686ca987d46SWarner Losh 687ca987d46SWarner Losh usage: 688ca987d46SWarner Losh snprintf(command_errbuf, sizeof(command_errbuf), 689ca987d46SWarner Losh "usage: %s [list | get | set <mode>]", argv[0]); 690ca987d46SWarner Losh return (CMD_ERROR); 691ca987d46SWarner Losh } 692ca987d46SWarner Losh 693ca987d46SWarner Losh COMMAND_SET(uga, "uga", "universal graphics adapter", command_uga); 694ca987d46SWarner Losh 695ca987d46SWarner Losh static int 696ca987d46SWarner Losh command_uga(int argc, char *argv[]) 697ca987d46SWarner Losh { 698ca987d46SWarner Losh struct efi_fb efifb; 699ca987d46SWarner Losh EFI_UGA_DRAW_PROTOCOL *uga; 700ca987d46SWarner Losh EFI_STATUS status; 701ca987d46SWarner Losh 702ca987d46SWarner Losh status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga); 703ca987d46SWarner Losh if (EFI_ERROR(status)) { 704ca987d46SWarner Losh snprintf(command_errbuf, sizeof(command_errbuf), 705ca987d46SWarner Losh "%s: UGA Protocol not present (error=%lu)", 706ca987d46SWarner Losh argv[0], EFI_ERROR_CODE(status)); 707ca987d46SWarner Losh return (CMD_ERROR); 708ca987d46SWarner Losh } 709ca987d46SWarner Losh 710ca987d46SWarner Losh if (argc != 1) 711ca987d46SWarner Losh goto usage; 712ca987d46SWarner Losh 713ca987d46SWarner Losh if (efifb_from_uga(&efifb, uga) != CMD_OK) { 714ca987d46SWarner Losh snprintf(command_errbuf, sizeof(command_errbuf), 715ca987d46SWarner Losh "%s: Unable to get UGA information", argv[0]); 716ca987d46SWarner Losh return (CMD_ERROR); 717ca987d46SWarner Losh } 718ca987d46SWarner Losh 719ca987d46SWarner Losh print_efifb(-1, &efifb, 1); 720ca987d46SWarner Losh printf("\n"); 721ca987d46SWarner Losh return (CMD_OK); 722ca987d46SWarner Losh 723ca987d46SWarner Losh usage: 724ca987d46SWarner Losh snprintf(command_errbuf, sizeof(command_errbuf), "usage: %s", argv[0]); 725ca987d46SWarner Losh return (CMD_ERROR); 726ca987d46SWarner Losh } 727