2 * Copyright (c) 2013 The FreeBSD Foundation
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
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <bootstrap.h>
33 #include <sys/endian.h>
34 #include <sys/param.h>
41 #include <machine/metadata.h>
43 #include "framebuffer.h"
45 static EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
46 static EFI_GUID pciio_guid = EFI_PCI_IO_PROTOCOL_GUID;
47 static EFI_GUID uga_guid = EFI_UGA_DRAW_PROTOCOL_GUID;
49 static struct named_resolution {
84 efifb_color_depth(struct efi_fb *efifb)
89 mask = efifb->fb_mask_red | efifb->fb_mask_green |
90 efifb->fb_mask_blue | efifb->fb_mask_reserved;
93 for (depth = 1; mask != 1; depth++)
99 efifb_mask_from_pixfmt(struct efi_fb *efifb, EFI_GRAPHICS_PIXEL_FORMAT pixfmt,
100 EFI_PIXEL_BITMASK *pixinfo)
106 case PixelRedGreenBlueReserved8BitPerColor:
107 efifb->fb_mask_red = 0x000000ff;
108 efifb->fb_mask_green = 0x0000ff00;
109 efifb->fb_mask_blue = 0x00ff0000;
110 efifb->fb_mask_reserved = 0xff000000;
112 case PixelBlueGreenRedReserved8BitPerColor:
113 efifb->fb_mask_red = 0x00ff0000;
114 efifb->fb_mask_green = 0x0000ff00;
115 efifb->fb_mask_blue = 0x000000ff;
116 efifb->fb_mask_reserved = 0xff000000;
119 efifb->fb_mask_red = pixinfo->RedMask;
120 efifb->fb_mask_green = pixinfo->GreenMask;
121 efifb->fb_mask_blue = pixinfo->BlueMask;
122 efifb->fb_mask_reserved = pixinfo->ReservedMask;
132 efifb_from_gop(struct efi_fb *efifb, EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode,
133 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info)
137 efifb->fb_addr = mode->FrameBufferBase;
138 efifb->fb_size = mode->FrameBufferSize;
139 efifb->fb_height = info->VerticalResolution;
140 efifb->fb_width = info->HorizontalResolution;
141 efifb->fb_stride = info->PixelsPerScanLine;
142 result = efifb_mask_from_pixfmt(efifb, info->PixelFormat,
143 &info->PixelInformation);
148 efifb_uga_find_pixel(EFI_UGA_DRAW_PROTOCOL *uga, u_int line,
149 EFI_PCI_IO_PROTOCOL *pciio, uint64_t addr, uint64_t size)
151 EFI_UGA_PIXEL pix0, pix1;
152 uint8_t *data1, *data2;
153 size_t count, maxcount = 1024;
158 status = uga->Blt(uga, &pix0, EfiUgaVideoToBltBuffer,
159 0, line, 0, 0, 1, 1, 0);
160 if (EFI_ERROR(status)) {
161 printf("UGA BLT operation failed (video->buffer)");
164 pix1.Red = ~pix0.Red;
165 pix1.Green = ~pix0.Green;
166 pix1.Blue = ~pix0.Blue;
169 data1 = calloc(maxcount, 2);
171 printf("Unable to allocate memory");
174 data2 = data1 + maxcount;
178 count = min(size, maxcount);
180 status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
181 EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
183 if (EFI_ERROR(status)) {
184 printf("Error reading frame buffer (before)");
187 status = uga->Blt(uga, &pix1, EfiUgaBltBufferToVideo,
188 0, 0, 0, line, 1, 1, 0);
189 if (EFI_ERROR(status)) {
190 printf("UGA BLT operation failed (modify)");
193 status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
194 EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
196 if (EFI_ERROR(status)) {
197 printf("Error reading frame buffer (after)");
200 status = uga->Blt(uga, &pix0, EfiUgaBltBufferToVideo,
201 0, 0, 0, line, 1, 1, 0);
202 if (EFI_ERROR(status)) {
203 printf("UGA BLT operation failed (restore)");
206 for (idx = 0; idx < count; idx++) {
207 if (data1[idx] != data2[idx]) {
209 return (ofs + (idx & ~3));
215 printf("No change detected in frame buffer");
218 printf(" -- error %lu\n", EFI_ERROR_CODE(status));
223 static EFI_PCI_IO_PROTOCOL *
224 efifb_uga_get_pciio(void)
226 EFI_PCI_IO_PROTOCOL *pciio;
227 EFI_HANDLE *buf, *hp;
231 /* Get all handles that support the UGA protocol. */
233 status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, NULL);
234 if (status != EFI_BUFFER_TOO_SMALL)
237 status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, buf);
238 if (status != EFI_SUCCESS) {
242 bufsz /= sizeof(EFI_HANDLE);
244 /* Get the PCI I/O interface of the first handle that supports it. */
246 for (hp = buf; hp < buf + bufsz; hp++) {
247 status = BS->HandleProtocol(*hp, &pciio_guid, (void **)&pciio);
248 if (status == EFI_SUCCESS) {
258 efifb_uga_locate_framebuffer(EFI_PCI_IO_PROTOCOL *pciio, uint64_t *addrp,
267 return (EFI_DEVICE_ERROR);
269 /* Attempt to get the frame buffer address (imprecise). */
272 for (bar = 0; bar < 6; bar++) {
273 status = pciio->GetBarAttributes(pciio, bar, NULL,
275 if (status != EFI_SUCCESS)
277 /* XXX magic offsets and constants. */
278 if (resattr[0] == 0x87 && resattr[3] == 0) {
279 /* 32-bit address space descriptor (MEMIO) */
280 addr = le32dec(resattr + 10);
281 size = le32dec(resattr + 22);
282 } else if (resattr[0] == 0x8a && resattr[3] == 0) {
283 /* 64-bit address space descriptor (MEMIO) */
284 addr = le64dec(resattr + 14);
285 size = le64dec(resattr + 38);
290 BS->FreePool(resattr);
291 if (addr == 0 || size == 0)
294 /* We assume the largest BAR is the frame buffer. */
300 return ((*addrp == 0 || *sizep == 0) ? EFI_DEVICE_ERROR : 0);
304 efifb_from_uga(struct efi_fb *efifb, EFI_UGA_DRAW_PROTOCOL *uga)
306 EFI_PCI_IO_PROTOCOL *pciio;
311 uint32_t horiz, vert, stride;
312 uint32_t np, depth, refresh;
314 status = uga->GetMode(uga, &horiz, &vert, &depth, &refresh);
315 if (EFI_ERROR(status))
317 efifb->fb_height = vert;
318 efifb->fb_width = horiz;
320 if (efifb->fb_height == 0 || efifb->fb_width == 0)
323 /* The color masks are fixed AFAICT. */
324 efifb_mask_from_pixfmt(efifb, PixelBlueGreenRedReserved8BitPerColor,
327 /* pciio can be NULL on return! */
328 pciio = efifb_uga_get_pciio();
330 /* Try to find the frame buffer. */
331 status = efifb_uga_locate_framebuffer(pciio, &efifb->fb_addr,
333 if (EFI_ERROR(status)) {
339 * There's no reliable way to detect the frame buffer or the
340 * offset within the frame buffer of the visible region, nor
341 * the stride. Our only option is to look at the system and
342 * fill in the blanks based on that. Luckily, UGA was mostly
343 * only used on Apple hardware.
346 ev = getenv("smbios.system.maker");
347 if (ev != NULL && !strcmp(ev, "Apple Inc.")) {
348 ev = getenv("smbios.system.product");
349 if (ev != NULL && !strcmp(ev, "iMac7,1")) {
350 /* These are the expected values we should have. */
354 /* These are the missing bits. */
357 } else if (ev != NULL && !strcmp(ev, "MacBook3,1")) {
358 /* These are the expected values we should have. */
362 /* These are the missing bits. */
369 * If this is hardware we know, make sure that it looks familiar
370 * before we accept our hardcoded values.
372 if (offset >= 0 && efifb->fb_width == horiz &&
373 efifb->fb_height == vert && efifb->fb_addr == fbaddr) {
374 efifb->fb_addr += offset;
375 efifb->fb_size -= offset;
376 efifb->fb_stride = stride;
378 } else if (offset >= 0) {
379 printf("Hardware make/model known, but graphics not "
381 printf("Console may not work!\n");
385 * The stride is equal or larger to the width. Often it's the
386 * next larger power of two. We'll start with that...
388 efifb->fb_stride = efifb->fb_width;
390 np = efifb->fb_stride & (efifb->fb_stride - 1);
392 efifb->fb_stride |= (np - 1);
397 ev = getenv("hw.efifb.address");
399 if (efifb->fb_addr == 0) {
400 printf("Please set hw.efifb.address and "
401 "hw.efifb.stride.\n");
406 * The visible part of the frame buffer may not start at
407 * offset 0, so try to detect it. Note that we may not
408 * always be able to read from the frame buffer, which
409 * means that we may not be able to detect anything. In
410 * that case, we would take a long time scanning for a
411 * pixel change in the frame buffer, which would have it
412 * appear that we're hanging, so we limit the scan to
413 * 1/256th of the frame buffer. This number is mostly
414 * based on PR 202730 and the fact that on a MacBoook,
415 * where we can't read from the frame buffer the offset
416 * of the visible region is 0. In short: we want to scan
417 * enough to handle all adapters that have an offset
418 * larger than 0 and we want to scan as little as we can
419 * to not appear to hang when we can't read from the
422 offset = efifb_uga_find_pixel(uga, 0, pciio, efifb->fb_addr,
423 efifb->fb_size >> 8);
425 printf("Unable to reliably detect frame buffer.\n");
426 } else if (offset > 0) {
427 efifb->fb_addr += offset;
428 efifb->fb_size -= offset;
432 efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
433 efifb->fb_addr = strtoul(ev, &p, 0);
438 ev = getenv("hw.efifb.stride");
440 if (pciio != NULL && offset != -1) {
441 /* Determine the stride. */
442 offset = efifb_uga_find_pixel(uga, 1, pciio,
443 efifb->fb_addr, horiz * 8);
445 efifb->fb_stride = offset >> 2;
447 printf("Unable to reliably detect the stride.\n");
450 efifb->fb_stride = strtoul(ev, &p, 0);
456 * We finalized on the stride, so recalculate the size of the
459 efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
464 efi_find_framebuffer(struct efi_fb *efifb)
466 EFI_GRAPHICS_OUTPUT *gop;
467 EFI_UGA_DRAW_PROTOCOL *uga;
470 status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
471 if (status == EFI_SUCCESS)
472 return (efifb_from_gop(efifb, gop->Mode, gop->Mode->Info));
474 status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
475 if (status == EFI_SUCCESS)
476 return (efifb_from_uga(efifb, uga));
482 print_efifb(int mode, struct efi_fb *efifb, int verbose)
487 printf("mode %d: ", mode);
488 depth = efifb_color_depth(efifb);
489 printf("%ux%ux%u, stride=%u", efifb->fb_width, efifb->fb_height,
490 depth, efifb->fb_stride);
492 printf("\n frame buffer: address=%jx, size=%jx",
493 (uintmax_t)efifb->fb_addr, (uintmax_t)efifb->fb_size);
494 printf("\n color mask: R=%08x, G=%08x, B=%08x\n",
495 efifb->fb_mask_red, efifb->fb_mask_green,
496 efifb->fb_mask_blue);
501 efi_resolution_compare(struct named_resolution *res, const char *cmp)
504 if (strcasecmp(res->name, cmp) == 0)
506 if (res->alias != NULL && strcasecmp(res->alias, cmp) == 0)
513 efi_get_max_resolution(int *width, int *height)
515 struct named_resolution *res;
517 char *height_start, *width_start;
520 *width = *height = 0;
521 maxres = getenv("efi_max_resolution");
522 /* No max_resolution set? Bail out; choose highest resolution */
525 /* See if it matches one of our known resolutions */
526 for (idx = 0; idx < nitems(resolutions); ++idx) {
527 res = &resolutions[idx];
528 if (efi_resolution_compare(res, maxres)) {
530 *height = res->height;
534 /* Not a known resolution, try to parse it; make a copy we can modify */
535 maxres = strdup(maxres);
538 height_start = strchr(maxres, 'x');
539 if (height_start == NULL) {
543 width_start = maxres;
545 /* Errors from this will effectively mean "no max" */
546 *width = (int)strtol(width_start, NULL, 0);
547 *height = (int)strtol(height_start, NULL, 0);
552 gop_autoresize(EFI_GRAPHICS_OUTPUT *gop)
555 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
558 UINT32 best_mode, currdim, maxdim, mode;
559 int height, max_height, max_width, width;
561 best_mode = maxdim = 0;
562 efi_get_max_resolution(&max_width, &max_height);
563 for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
564 status = gop->QueryMode(gop, mode, &infosz, &info);
565 if (EFI_ERROR(status))
567 efifb_from_gop(&efifb, gop->Mode, info);
568 width = info->HorizontalResolution;
569 height = info->VerticalResolution;
570 currdim = width * height;
571 if (currdim > maxdim) {
572 if ((max_width != 0 && width > max_width) ||
573 (max_height != 0 && height > max_height))
581 status = gop->SetMode(gop, best_mode);
582 if (EFI_ERROR(status)) {
583 snprintf(command_errbuf, sizeof(command_errbuf),
584 "gop_autoresize: Unable to set mode to %u (error=%lu)",
585 mode, EFI_ERROR_CODE(status));
595 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
597 UINTN i, max_dim, best_mode, cols, rows;
600 max_dim = best_mode = 0;
601 for (i = 0; i < conout->Mode->MaxMode; i++) {
602 status = conout->QueryMode(conout, i, &cols, &rows);
603 if (EFI_ERROR(status))
605 if (cols * rows > max_dim) {
606 max_dim = cols * rows;
611 conout->SetMode(conout, best_mode);
616 uga_autoresize(EFI_UGA_DRAW_PROTOCOL *uga)
619 return (text_autoresize());
622 COMMAND_SET(efi_autoresize, "efi-autoresizecons", "EFI Auto-resize Console", command_autoresize);
625 command_autoresize(int argc, char *argv[])
627 EFI_GRAPHICS_OUTPUT *gop;
628 EFI_UGA_DRAW_PROTOCOL *uga;
633 textmode = getenv("hw.vga.textmode");
634 /* If it's set and non-zero, we'll select a console mode instead */
635 if (textmode != NULL && strcmp(textmode, "0") != 0)
636 return (text_autoresize());
640 status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
641 if (EFI_ERROR(status) == 0)
642 return (gop_autoresize(gop));
644 status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
645 if (EFI_ERROR(status) == 0)
646 return (uga_autoresize(uga));
648 snprintf(command_errbuf, sizeof(command_errbuf),
649 "%s: Neither Graphics Output Protocol nor Universal Graphics Adapter present",
653 * Default to text_autoresize if we have neither GOP or UGA. This won't
654 * give us the most ideal resolution, but it will at least leave us
655 * functional rather than failing the boot for an objectively bad
658 return (text_autoresize());
661 COMMAND_SET(gop, "gop", "graphics output protocol", command_gop);
664 command_gop(int argc, char *argv[])
667 EFI_GRAPHICS_OUTPUT *gop;
671 status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
672 if (EFI_ERROR(status)) {
673 snprintf(command_errbuf, sizeof(command_errbuf),
674 "%s: Graphics Output Protocol not present (error=%lu)",
675 argv[0], EFI_ERROR_CODE(status));
682 if (!strcmp(argv[1], "set")) {
687 mode = strtol(argv[2], &cp, 0);
689 sprintf(command_errbuf, "mode is an integer");
692 status = gop->SetMode(gop, mode);
693 if (EFI_ERROR(status)) {
694 snprintf(command_errbuf, sizeof(command_errbuf),
695 "%s: Unable to set mode to %u (error=%lu)",
696 argv[0], mode, EFI_ERROR_CODE(status));
699 } else if (!strcmp(argv[1], "get")) {
702 efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info);
703 print_efifb(gop->Mode->Mode, &efifb, 1);
705 } else if (!strcmp(argv[1], "list")) {
706 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
712 for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
713 status = gop->QueryMode(gop, mode, &infosz, &info);
714 if (EFI_ERROR(status))
716 efifb_from_gop(&efifb, gop->Mode, info);
717 print_efifb(mode, &efifb, 0);
718 if (pager_output("\n"))
726 snprintf(command_errbuf, sizeof(command_errbuf),
727 "usage: %s [list | get | set <mode>]", argv[0]);
731 COMMAND_SET(uga, "uga", "universal graphics adapter", command_uga);
734 command_uga(int argc, char *argv[])
737 EFI_UGA_DRAW_PROTOCOL *uga;
740 status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
741 if (EFI_ERROR(status)) {
742 snprintf(command_errbuf, sizeof(command_errbuf),
743 "%s: UGA Protocol not present (error=%lu)",
744 argv[0], EFI_ERROR_CODE(status));
751 if (efifb_from_uga(&efifb, uga) != CMD_OK) {
752 snprintf(command_errbuf, sizeof(command_errbuf),
753 "%s: Unable to get UGA information", argv[0]);
757 print_efifb(-1, &efifb, 1);
762 snprintf(command_errbuf, sizeof(command_errbuf), "usage: %s", argv[0]);