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 = OpenProtocolByHandle(*hp, &pciio_guid,
249 if (status == EFI_SUCCESS) {
259 efifb_uga_locate_framebuffer(EFI_PCI_IO_PROTOCOL *pciio, uint64_t *addrp,
268 return (EFI_DEVICE_ERROR);
270 /* Attempt to get the frame buffer address (imprecise). */
273 for (bar = 0; bar < 6; bar++) {
274 status = pciio->GetBarAttributes(pciio, bar, NULL,
276 if (status != EFI_SUCCESS)
278 /* XXX magic offsets and constants. */
279 if (resattr[0] == 0x87 && resattr[3] == 0) {
280 /* 32-bit address space descriptor (MEMIO) */
281 addr = le32dec(resattr + 10);
282 size = le32dec(resattr + 22);
283 } else if (resattr[0] == 0x8a && resattr[3] == 0) {
284 /* 64-bit address space descriptor (MEMIO) */
285 addr = le64dec(resattr + 14);
286 size = le64dec(resattr + 38);
291 BS->FreePool(resattr);
292 if (addr == 0 || size == 0)
295 /* We assume the largest BAR is the frame buffer. */
301 return ((*addrp == 0 || *sizep == 0) ? EFI_DEVICE_ERROR : 0);
305 efifb_from_uga(struct efi_fb *efifb, EFI_UGA_DRAW_PROTOCOL *uga)
307 EFI_PCI_IO_PROTOCOL *pciio;
312 uint32_t horiz, vert, stride;
313 uint32_t np, depth, refresh;
315 status = uga->GetMode(uga, &horiz, &vert, &depth, &refresh);
316 if (EFI_ERROR(status))
318 efifb->fb_height = vert;
319 efifb->fb_width = horiz;
321 if (efifb->fb_height == 0 || efifb->fb_width == 0)
324 /* The color masks are fixed AFAICT. */
325 efifb_mask_from_pixfmt(efifb, PixelBlueGreenRedReserved8BitPerColor,
328 /* pciio can be NULL on return! */
329 pciio = efifb_uga_get_pciio();
331 /* Try to find the frame buffer. */
332 status = efifb_uga_locate_framebuffer(pciio, &efifb->fb_addr,
334 if (EFI_ERROR(status)) {
340 * There's no reliable way to detect the frame buffer or the
341 * offset within the frame buffer of the visible region, nor
342 * the stride. Our only option is to look at the system and
343 * fill in the blanks based on that. Luckily, UGA was mostly
344 * only used on Apple hardware.
347 ev = getenv("smbios.system.maker");
348 if (ev != NULL && !strcmp(ev, "Apple Inc.")) {
349 ev = getenv("smbios.system.product");
350 if (ev != NULL && !strcmp(ev, "iMac7,1")) {
351 /* These are the expected values we should have. */
355 /* These are the missing bits. */
358 } else if (ev != NULL && !strcmp(ev, "MacBook3,1")) {
359 /* These are the expected values we should have. */
363 /* These are the missing bits. */
370 * If this is hardware we know, make sure that it looks familiar
371 * before we accept our hardcoded values.
373 if (offset >= 0 && efifb->fb_width == horiz &&
374 efifb->fb_height == vert && efifb->fb_addr == fbaddr) {
375 efifb->fb_addr += offset;
376 efifb->fb_size -= offset;
377 efifb->fb_stride = stride;
379 } else if (offset >= 0) {
380 printf("Hardware make/model known, but graphics not "
382 printf("Console may not work!\n");
386 * The stride is equal or larger to the width. Often it's the
387 * next larger power of two. We'll start with that...
389 efifb->fb_stride = efifb->fb_width;
391 np = efifb->fb_stride & (efifb->fb_stride - 1);
393 efifb->fb_stride |= (np - 1);
398 ev = getenv("hw.efifb.address");
400 if (efifb->fb_addr == 0) {
401 printf("Please set hw.efifb.address and "
402 "hw.efifb.stride.\n");
407 * The visible part of the frame buffer may not start at
408 * offset 0, so try to detect it. Note that we may not
409 * always be able to read from the frame buffer, which
410 * means that we may not be able to detect anything. In
411 * that case, we would take a long time scanning for a
412 * pixel change in the frame buffer, which would have it
413 * appear that we're hanging, so we limit the scan to
414 * 1/256th of the frame buffer. This number is mostly
415 * based on PR 202730 and the fact that on a MacBoook,
416 * where we can't read from the frame buffer the offset
417 * of the visible region is 0. In short: we want to scan
418 * enough to handle all adapters that have an offset
419 * larger than 0 and we want to scan as little as we can
420 * to not appear to hang when we can't read from the
423 offset = efifb_uga_find_pixel(uga, 0, pciio, efifb->fb_addr,
424 efifb->fb_size >> 8);
426 printf("Unable to reliably detect frame buffer.\n");
427 } else if (offset > 0) {
428 efifb->fb_addr += offset;
429 efifb->fb_size -= offset;
433 efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
434 efifb->fb_addr = strtoul(ev, &p, 0);
439 ev = getenv("hw.efifb.stride");
441 if (pciio != NULL && offset != -1) {
442 /* Determine the stride. */
443 offset = efifb_uga_find_pixel(uga, 1, pciio,
444 efifb->fb_addr, horiz * 8);
446 efifb->fb_stride = offset >> 2;
448 printf("Unable to reliably detect the stride.\n");
451 efifb->fb_stride = strtoul(ev, &p, 0);
457 * We finalized on the stride, so recalculate the size of the
460 efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
465 efi_find_framebuffer(struct efi_fb *efifb)
467 EFI_GRAPHICS_OUTPUT *gop;
468 EFI_UGA_DRAW_PROTOCOL *uga;
471 status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
472 if (status == EFI_SUCCESS)
473 return (efifb_from_gop(efifb, gop->Mode, gop->Mode->Info));
475 status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
476 if (status == EFI_SUCCESS)
477 return (efifb_from_uga(efifb, uga));
483 print_efifb(int mode, struct efi_fb *efifb, int verbose)
488 printf("mode %d: ", mode);
489 depth = efifb_color_depth(efifb);
490 printf("%ux%ux%u, stride=%u", efifb->fb_width, efifb->fb_height,
491 depth, efifb->fb_stride);
493 printf("\n frame buffer: address=%jx, size=%jx",
494 (uintmax_t)efifb->fb_addr, (uintmax_t)efifb->fb_size);
495 printf("\n color mask: R=%08x, G=%08x, B=%08x\n",
496 efifb->fb_mask_red, efifb->fb_mask_green,
497 efifb->fb_mask_blue);
502 efi_resolution_compare(struct named_resolution *res, const char *cmp)
505 if (strcasecmp(res->name, cmp) == 0)
507 if (res->alias != NULL && strcasecmp(res->alias, cmp) == 0)
514 efi_get_max_resolution(int *width, int *height)
516 struct named_resolution *res;
518 char *height_start, *width_start;
521 *width = *height = 0;
522 maxres = getenv("efi_max_resolution");
523 /* No max_resolution set? Bail out; choose highest resolution */
526 /* See if it matches one of our known resolutions */
527 for (idx = 0; idx < nitems(resolutions); ++idx) {
528 res = &resolutions[idx];
529 if (efi_resolution_compare(res, maxres)) {
531 *height = res->height;
535 /* Not a known resolution, try to parse it; make a copy we can modify */
536 maxres = strdup(maxres);
539 height_start = strchr(maxres, 'x');
540 if (height_start == NULL) {
544 width_start = maxres;
546 /* Errors from this will effectively mean "no max" */
547 *width = (int)strtol(width_start, NULL, 0);
548 *height = (int)strtol(height_start, NULL, 0);
553 gop_autoresize(EFI_GRAPHICS_OUTPUT *gop)
556 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
559 UINT32 best_mode, currdim, maxdim, mode;
560 int height, max_height, max_width, width;
562 best_mode = maxdim = 0;
563 efi_get_max_resolution(&max_width, &max_height);
564 for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
565 status = gop->QueryMode(gop, mode, &infosz, &info);
566 if (EFI_ERROR(status))
568 efifb_from_gop(&efifb, gop->Mode, info);
569 width = info->HorizontalResolution;
570 height = info->VerticalResolution;
571 currdim = width * height;
572 if (currdim > maxdim) {
573 if ((max_width != 0 && width > max_width) ||
574 (max_height != 0 && height > max_height))
582 status = gop->SetMode(gop, best_mode);
583 if (EFI_ERROR(status)) {
584 snprintf(command_errbuf, sizeof(command_errbuf),
585 "gop_autoresize: Unable to set mode to %u (error=%lu)",
586 mode, EFI_ERROR_CODE(status));
596 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
598 UINTN i, max_dim, best_mode, cols, rows;
601 max_dim = best_mode = 0;
602 for (i = 0; i < conout->Mode->MaxMode; i++) {
603 status = conout->QueryMode(conout, i, &cols, &rows);
604 if (EFI_ERROR(status))
606 if (cols * rows > max_dim) {
607 max_dim = cols * rows;
612 conout->SetMode(conout, best_mode);
617 uga_autoresize(EFI_UGA_DRAW_PROTOCOL *uga)
620 return (text_autoresize());
623 COMMAND_SET(efi_autoresize, "efi-autoresizecons", "EFI Auto-resize Console", command_autoresize);
626 command_autoresize(int argc, char *argv[])
628 EFI_GRAPHICS_OUTPUT *gop;
629 EFI_UGA_DRAW_PROTOCOL *uga;
634 textmode = getenv("hw.vga.textmode");
635 /* If it's set and non-zero, we'll select a console mode instead */
636 if (textmode != NULL && strcmp(textmode, "0") != 0)
637 return (text_autoresize());
641 status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
642 if (EFI_ERROR(status) == 0)
643 return (gop_autoresize(gop));
645 status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
646 if (EFI_ERROR(status) == 0)
647 return (uga_autoresize(uga));
649 snprintf(command_errbuf, sizeof(command_errbuf),
650 "%s: Neither Graphics Output Protocol nor Universal Graphics Adapter present",
654 * Default to text_autoresize if we have neither GOP or UGA. This won't
655 * give us the most ideal resolution, but it will at least leave us
656 * functional rather than failing the boot for an objectively bad
659 return (text_autoresize());
662 COMMAND_SET(gop, "gop", "graphics output protocol", command_gop);
665 command_gop(int argc, char *argv[])
668 EFI_GRAPHICS_OUTPUT *gop;
672 status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
673 if (EFI_ERROR(status)) {
674 snprintf(command_errbuf, sizeof(command_errbuf),
675 "%s: Graphics Output Protocol not present (error=%lu)",
676 argv[0], EFI_ERROR_CODE(status));
683 if (!strcmp(argv[1], "set")) {
688 mode = strtol(argv[2], &cp, 0);
690 sprintf(command_errbuf, "mode is an integer");
693 status = gop->SetMode(gop, mode);
694 if (EFI_ERROR(status)) {
695 snprintf(command_errbuf, sizeof(command_errbuf),
696 "%s: Unable to set mode to %u (error=%lu)",
697 argv[0], mode, EFI_ERROR_CODE(status));
700 } else if (!strcmp(argv[1], "get")) {
703 efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info);
704 print_efifb(gop->Mode->Mode, &efifb, 1);
706 } else if (!strcmp(argv[1], "list")) {
707 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
713 for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
714 status = gop->QueryMode(gop, mode, &infosz, &info);
715 if (EFI_ERROR(status))
717 efifb_from_gop(&efifb, gop->Mode, info);
718 print_efifb(mode, &efifb, 0);
719 if (pager_output("\n"))
727 snprintf(command_errbuf, sizeof(command_errbuf),
728 "usage: %s [list | get | set <mode>]", argv[0]);
732 COMMAND_SET(uga, "uga", "universal graphics adapter", command_uga);
735 command_uga(int argc, char *argv[])
738 EFI_UGA_DRAW_PROTOCOL *uga;
741 status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
742 if (EFI_ERROR(status)) {
743 snprintf(command_errbuf, sizeof(command_errbuf),
744 "%s: UGA Protocol not present (error=%lu)",
745 argv[0], EFI_ERROR_CODE(status));
752 if (efifb_from_uga(&efifb, uga) != CMD_OK) {
753 snprintf(command_errbuf, sizeof(command_errbuf),
754 "%s: Unable to get UGA information", argv[0]);
758 print_efifb(-1, &efifb, 1);
763 snprintf(command_errbuf, sizeof(command_errbuf), "usage: %s", argv[0]);