xref: /freebsd/stand/efi/loader/framebuffer.c (revision c2a55efd74cccb3d4e7b9037b240ad062c203bb8)
1 /*-
2  * Copyright (c) 2013 The FreeBSD Foundation
3  *
4  * This software was developed by Benno Rice under sponsorship from
5  * the FreeBSD Foundation.
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <bootstrap.h>
29 #include <sys/endian.h>
30 #include <sys/param.h>
31 #include <stand.h>
32 
33 #include <efi.h>
34 #include <efilib.h>
35 #include <efipciio.h>
36 #include <Protocol/EdidActive.h>
37 #include <Protocol/EdidDiscovered.h>
38 #include <Protocol/GraphicsOutput.h>
39 #include <Protocol/UgaDraw.h>
40 #include <machine/metadata.h>
41 
42 #include "bootstrap.h"
43 #include "framebuffer.h"
44 
45 /* XXX This may be obsolete -- edk2 doesn't define it anywhere */
46 #define EFI_CONSOLE_OUT_DEVICE_GUID    \
47 { 0xd3b36f2c, 0xd551, 0x11d4, {0x9a, 0x46, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d} }
48 
49 static EFI_GUID conout_guid = EFI_CONSOLE_OUT_DEVICE_GUID;
50 EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
51 static EFI_GUID pciio_guid = EFI_PCI_IO_PROTOCOL_GUID;
52 static EFI_GUID uga_guid = EFI_UGA_DRAW_PROTOCOL_GUID;
53 static EFI_GUID active_edid_guid = EFI_EDID_ACTIVE_PROTOCOL_GUID;
54 static EFI_GUID discovered_edid_guid = EFI_EDID_DISCOVERED_PROTOCOL_GUID;
55 static EFI_HANDLE gop_handle;
56 
57 /* Cached EDID. */
58 struct vesa_edid_info *edid_info = NULL;
59 
60 static EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
61 static EFI_UGA_DRAW_PROTOCOL *uga;
62 
63 static struct named_resolution {
64 	const char *name;
65 	const char *alias;
66 	unsigned int width;
67 	unsigned int height;
68 } resolutions[] = {
69 	{
70 		.name = "480p",
71 		.width = 640,
72 		.height = 480,
73 	},
74 	{
75 		.name = "720p",
76 		.width = 1280,
77 		.height = 720,
78 	},
79 	{
80 		.name = "1080p",
81 		.width = 1920,
82 		.height = 1080,
83 	},
84 	{
85 		.name = "1440p",
86 		.width = 2560,
87 		.height = 1440,
88 	},
89 	{
90 		.name = "2160p",
91 		.alias = "4k",
92 		.width = 3840,
93 		.height = 2160,
94 	},
95 	{
96 		.name = "5k",
97 		.width = 5120,
98 		.height = 2880,
99 	}
100 };
101 
102 static u_int
103 efifb_color_depth(struct efi_fb *efifb)
104 {
105 	uint32_t mask;
106 	u_int depth;
107 
108 	mask = efifb->fb_mask_red | efifb->fb_mask_green |
109 	    efifb->fb_mask_blue | efifb->fb_mask_reserved;
110 	if (mask == 0)
111 		return (0);
112 	for (depth = 1; mask != 1; depth++)
113 		mask >>= 1;
114 	return (depth);
115 }
116 
117 static int
118 efifb_mask_from_pixfmt(struct efi_fb *efifb, EFI_GRAPHICS_PIXEL_FORMAT pixfmt,
119     EFI_PIXEL_BITMASK *pixinfo)
120 {
121 	int result;
122 
123 	result = 0;
124 	switch (pixfmt) {
125 	case PixelRedGreenBlueReserved8BitPerColor:
126 	case PixelBltOnly:
127 		efifb->fb_mask_red = 0x000000ff;
128 		efifb->fb_mask_green = 0x0000ff00;
129 		efifb->fb_mask_blue = 0x00ff0000;
130 		efifb->fb_mask_reserved = 0xff000000;
131 		break;
132 	case PixelBlueGreenRedReserved8BitPerColor:
133 		efifb->fb_mask_red = 0x00ff0000;
134 		efifb->fb_mask_green = 0x0000ff00;
135 		efifb->fb_mask_blue = 0x000000ff;
136 		efifb->fb_mask_reserved = 0xff000000;
137 		break;
138 	case PixelBitMask:
139 		efifb->fb_mask_red = pixinfo->RedMask;
140 		efifb->fb_mask_green = pixinfo->GreenMask;
141 		efifb->fb_mask_blue = pixinfo->BlueMask;
142 		efifb->fb_mask_reserved = pixinfo->ReservedMask;
143 		break;
144 	default:
145 		result = 1;
146 		break;
147 	}
148 	return (result);
149 }
150 
151 static int
152 efifb_from_gop(struct efi_fb *efifb, EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode,
153     EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info)
154 {
155 	int result;
156 
157 	/*
158 	 * The Asus EEEPC 1025C, and possibly others,
159 	 * require the address to be masked.
160 	 */
161 	efifb->fb_addr =
162 #ifdef __i386__
163 	    mode->FrameBufferBase & 0xffffffff;
164 #else
165 	    mode->FrameBufferBase;
166 #endif
167 	efifb->fb_size = mode->FrameBufferSize;
168 	efifb->fb_height = info->VerticalResolution;
169 	efifb->fb_width = info->HorizontalResolution;
170 	efifb->fb_stride = info->PixelsPerScanLine;
171 	result = efifb_mask_from_pixfmt(efifb, info->PixelFormat,
172 	    &info->PixelInformation);
173 	return (result);
174 }
175 
176 static ssize_t
177 efifb_uga_find_pixel(EFI_UGA_DRAW_PROTOCOL *uga, u_int line,
178     EFI_PCI_IO_PROTOCOL *pciio, uint64_t addr, uint64_t size)
179 {
180 	EFI_UGA_PIXEL pix0, pix1;
181 	uint8_t *data1, *data2;
182 	size_t count, maxcount = 1024;
183 	ssize_t ofs;
184 	EFI_STATUS status;
185 	u_int idx;
186 
187 	status = uga->Blt(uga, &pix0, EfiUgaVideoToBltBuffer,
188 	    0, line, 0, 0, 1, 1, 0);
189 	if (EFI_ERROR(status)) {
190 		printf("UGA BLT operation failed (video->buffer)");
191 		return (-1);
192 	}
193 	pix1.Red = ~pix0.Red;
194 	pix1.Green = ~pix0.Green;
195 	pix1.Blue = ~pix0.Blue;
196 	pix1.Reserved = 0;
197 
198 	data1 = calloc(maxcount, 2);
199 	if (data1 == NULL) {
200 		printf("Unable to allocate memory");
201 		return (-1);
202 	}
203 	data2 = data1 + maxcount;
204 
205 	ofs = 0;
206 	while (size > 0) {
207 		count = min(size, maxcount);
208 
209 		status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
210 		    EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
211 		    data1);
212 		if (EFI_ERROR(status)) {
213 			printf("Error reading frame buffer (before)");
214 			goto fail;
215 		}
216 		status = uga->Blt(uga, &pix1, EfiUgaBltBufferToVideo,
217 		    0, 0, 0, line, 1, 1, 0);
218 		if (EFI_ERROR(status)) {
219 			printf("UGA BLT operation failed (modify)");
220 			goto fail;
221 		}
222 		status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
223 		    EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
224 		    data2);
225 		if (EFI_ERROR(status)) {
226 			printf("Error reading frame buffer (after)");
227 			goto fail;
228 		}
229 		status = uga->Blt(uga, &pix0, EfiUgaBltBufferToVideo,
230 		    0, 0, 0, line, 1, 1, 0);
231 		if (EFI_ERROR(status)) {
232 			printf("UGA BLT operation failed (restore)");
233 			goto fail;
234 		}
235 		for (idx = 0; idx < count; idx++) {
236 			if (data1[idx] != data2[idx]) {
237 				free(data1);
238 				return (ofs + (idx & ~3));
239 			}
240 		}
241 		ofs += count;
242 		size -= count;
243 	}
244 	printf("No change detected in frame buffer");
245 
246  fail:
247 	printf(" -- error %lu\n", DECODE_ERROR(status));
248 	free(data1);
249 	return (-1);
250 }
251 
252 static EFI_PCI_IO_PROTOCOL *
253 efifb_uga_get_pciio(void)
254 {
255 	EFI_PCI_IO_PROTOCOL *pciio;
256 	EFI_HANDLE *buf, *hp;
257 	EFI_STATUS status;
258 	UINTN bufsz;
259 
260 	/* Get all handles that support the UGA protocol. */
261 	bufsz = 0;
262 	status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, NULL);
263 	if (status != EFI_BUFFER_TOO_SMALL)
264 		return (NULL);
265 	buf = malloc(bufsz);
266 	status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, buf);
267 	if (status != EFI_SUCCESS) {
268 		free(buf);
269 		return (NULL);
270 	}
271 	bufsz /= sizeof(EFI_HANDLE);
272 
273 	/* Get the PCI I/O interface of the first handle that supports it. */
274 	pciio = NULL;
275 	for (hp = buf; hp < buf + bufsz; hp++) {
276 		status = OpenProtocolByHandle(*hp, &pciio_guid,
277 		    (void **)&pciio);
278 		if (status == EFI_SUCCESS) {
279 			free(buf);
280 			return (pciio);
281 		}
282 	}
283 	free(buf);
284 	return (NULL);
285 }
286 
287 static EFI_STATUS
288 efifb_uga_locate_framebuffer(EFI_PCI_IO_PROTOCOL *pciio, uint64_t *addrp,
289     uint64_t *sizep)
290 {
291 	uint8_t *resattr;
292 	uint64_t addr, size;
293 	EFI_STATUS status;
294 	u_int bar;
295 
296 	if (pciio == NULL)
297 		return (EFI_DEVICE_ERROR);
298 
299 	/* Attempt to get the frame buffer address (imprecise). */
300 	*addrp = 0;
301 	*sizep = 0;
302 	for (bar = 0; bar < 6; bar++) {
303 		status = pciio->GetBarAttributes(pciio, bar, NULL,
304 		    (void **)&resattr);
305 		if (status != EFI_SUCCESS)
306 			continue;
307 		/* XXX magic offsets and constants. */
308 		if (resattr[0] == 0x87 && resattr[3] == 0) {
309 			/* 32-bit address space descriptor (MEMIO) */
310 			addr = le32dec(resattr + 10);
311 			size = le32dec(resattr + 22);
312 		} else if (resattr[0] == 0x8a && resattr[3] == 0) {
313 			/* 64-bit address space descriptor (MEMIO) */
314 			addr = le64dec(resattr + 14);
315 			size = le64dec(resattr + 38);
316 		} else {
317 			addr = 0;
318 			size = 0;
319 		}
320 		BS->FreePool(resattr);
321 		if (addr == 0 || size == 0)
322 			continue;
323 
324 		/* We assume the largest BAR is the frame buffer. */
325 		if (size > *sizep) {
326 			*addrp = addr;
327 			*sizep = size;
328 		}
329 	}
330 	return ((*addrp == 0 || *sizep == 0) ? EFI_DEVICE_ERROR : 0);
331 }
332 
333 static int
334 efifb_from_uga(struct efi_fb *efifb)
335 {
336 	EFI_PCI_IO_PROTOCOL *pciio;
337 	char *ev, *p;
338 	EFI_STATUS status;
339 	ssize_t offset;
340 	uint64_t fbaddr;
341 	uint32_t horiz, vert, stride;
342 	uint32_t np, depth, refresh;
343 
344 	status = uga->GetMode(uga, &horiz, &vert, &depth, &refresh);
345 	if (EFI_ERROR(status))
346 		return (1);
347 	efifb->fb_height = vert;
348 	efifb->fb_width = horiz;
349 	/* Paranoia... */
350 	if (efifb->fb_height == 0 || efifb->fb_width == 0)
351 		return (1);
352 
353 	/* The color masks are fixed AFAICT. */
354 	efifb_mask_from_pixfmt(efifb, PixelBlueGreenRedReserved8BitPerColor,
355 	    NULL);
356 
357 	/* pciio can be NULL on return! */
358 	pciio = efifb_uga_get_pciio();
359 
360 	/* Try to find the frame buffer. */
361 	status = efifb_uga_locate_framebuffer(pciio, &efifb->fb_addr,
362 	    &efifb->fb_size);
363 	if (EFI_ERROR(status)) {
364 		efifb->fb_addr = 0;
365 		efifb->fb_size = 0;
366 	}
367 
368 	/*
369 	 * There's no reliable way to detect the frame buffer or the
370 	 * offset within the frame buffer of the visible region, nor
371 	 * the stride. Our only option is to look at the system and
372 	 * fill in the blanks based on that. Luckily, UGA was mostly
373 	 * only used on Apple hardware.
374 	 */
375 	offset = -1;
376 	ev = getenv("smbios.system.maker");
377 	if (ev != NULL && !strcmp(ev, "Apple Inc.")) {
378 		ev = getenv("smbios.system.product");
379 		if (ev != NULL && !strcmp(ev, "iMac7,1")) {
380 			/* These are the expected values we should have. */
381 			horiz = 1680;
382 			vert = 1050;
383 			fbaddr = 0xc0000000;
384 			/* These are the missing bits. */
385 			offset = 0x10000;
386 			stride = 1728;
387 		} else if (ev != NULL && !strcmp(ev, "MacBook3,1")) {
388 			/* These are the expected values we should have. */
389 			horiz = 1280;
390 			vert = 800;
391 			fbaddr = 0xc0000000;
392 			/* These are the missing bits. */
393 			offset = 0x0;
394 			stride = 2048;
395 		} else if (ev != NULL && !strcmp(ev, "MacBookPro3,1")) {
396 			/*
397 			 * Valid for MacBookPro 17" with standard resolution.
398 			 * Other Models are:
399 			 *   MacBookPro 15" with horiz=1440
400 			 *   MacBookPro 17" with horiz=1920
401 			 */
402 
403 			/* These are the expected values we should have. */
404 			if (horiz == 1680) {
405 				vert = 1050;
406 				fbaddr = 0xc0000000;
407 				/* These are the missing bits. */
408 				stride = 2048;
409 				/* 24 scan lines down */
410 				offset = stride * 4 * 24;
411 			}
412 		}
413 	}
414 
415 	/*
416 	 * If this is hardware we know, make sure that it looks familiar
417 	 * before we accept our hardcoded values.
418 	 */
419 	if (offset >= 0 && efifb->fb_width == horiz &&
420 	    efifb->fb_height == vert && efifb->fb_addr == fbaddr) {
421 		efifb->fb_addr += offset;
422 		efifb->fb_size -= offset;
423 		efifb->fb_stride = stride;
424 		return (0);
425 	} else if (offset >= 0) {
426 		printf("Hardware make/model known, but graphics not "
427 		    "as expected.\n");
428 		printf("Console may not work!\n");
429 	}
430 
431 	/*
432 	 * The stride is equal or larger to the width. Often it's the
433 	 * next larger power of two. We'll start with that...
434 	 */
435 	efifb->fb_stride = efifb->fb_width;
436 	do {
437 		np = efifb->fb_stride & (efifb->fb_stride - 1);
438 		if (np) {
439 			efifb->fb_stride |= (np - 1);
440 			efifb->fb_stride++;
441 		}
442 	} while (np);
443 
444 	ev = getenv("hw.efifb.address");
445 	if (ev == NULL) {
446 		if (efifb->fb_addr == 0) {
447 			printf("Please set hw.efifb.address and "
448 			    "hw.efifb.stride.\n");
449 			return (1);
450 		}
451 
452 		/*
453 		 * The visible part of the frame buffer may not start at
454 		 * offset 0, so try to detect it. Note that we may not
455 		 * always be able to read from the frame buffer, which
456 		 * means that we may not be able to detect anything. In
457 		 * that case, we would take a long time scanning for a
458 		 * pixel change in the frame buffer, which would have it
459 		 * appear that we're hanging, so we limit the scan to
460 		 * 1/256th of the frame buffer. This number is mostly
461 		 * based on PR 202730 and the fact that on a MacBoook,
462 		 * where we can't read from the frame buffer the offset
463 		 * of the visible region is 0. In short: we want to scan
464 		 * enough to handle all adapters that have an offset
465 		 * larger than 0 and we want to scan as little as we can
466 		 * to not appear to hang when we can't read from the
467 		 * frame buffer.
468 		 */
469 		offset = efifb_uga_find_pixel(uga, 0, pciio, efifb->fb_addr,
470 		    efifb->fb_size >> 8);
471 		if (offset == -1) {
472 			printf("Unable to reliably detect frame buffer.\n");
473 		} else if (offset > 0) {
474 			efifb->fb_addr += offset;
475 			efifb->fb_size -= offset;
476 		}
477 	} else {
478 		offset = 0;
479 		efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
480 		efifb->fb_addr = strtoul(ev, &p, 0);
481 		if (*p != '\0')
482 			return (1);
483 	}
484 
485 	ev = getenv("hw.efifb.stride");
486 	if (ev == NULL) {
487 		if (pciio != NULL && offset != -1) {
488 			/* Determine the stride. */
489 			offset = efifb_uga_find_pixel(uga, 1, pciio,
490 			    efifb->fb_addr, horiz * 8);
491 			if (offset != -1)
492 				efifb->fb_stride = offset >> 2;
493 		} else {
494 			printf("Unable to reliably detect the stride.\n");
495 		}
496 	} else {
497 		efifb->fb_stride = strtoul(ev, &p, 0);
498 		if (*p != '\0')
499 			return (1);
500 	}
501 
502 	/*
503 	 * We finalized on the stride, so recalculate the size of the
504 	 * frame buffer.
505 	 */
506 	efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
507 	return (0);
508 }
509 
510 /*
511  * Fetch EDID info. Caller must free the buffer.
512  */
513 static struct vesa_edid_info *
514 efifb_gop_get_edid(EFI_HANDLE h)
515 {
516 	const uint8_t magic[] = EDID_MAGIC;
517 	EFI_EDID_ACTIVE_PROTOCOL *edid;
518 	struct vesa_edid_info *edid_infop;
519 	EFI_GUID *guid;
520 	EFI_STATUS status;
521 	size_t size;
522 
523 	guid = &active_edid_guid;
524 	status = BS->OpenProtocol(h, guid, (void **)&edid, IH, NULL,
525 	    EFI_OPEN_PROTOCOL_GET_PROTOCOL);
526 	if (status != EFI_SUCCESS ||
527 	    edid->SizeOfEdid == 0) {
528 		guid = &discovered_edid_guid;
529 		status = BS->OpenProtocol(h, guid, (void **)&edid, IH, NULL,
530 		    EFI_OPEN_PROTOCOL_GET_PROTOCOL);
531 		if (status != EFI_SUCCESS ||
532 		    edid->SizeOfEdid == 0)
533 			return (NULL);
534 	}
535 
536 	size = MAX(sizeof(*edid_infop), edid->SizeOfEdid);
537 
538 	edid_infop = calloc(1, size);
539 	if (edid_infop == NULL)
540 		return (NULL);
541 
542 	memcpy(edid_infop, edid->Edid, edid->SizeOfEdid);
543 
544 	/* Validate EDID */
545 	if (memcmp(edid_infop, magic, sizeof (magic)) != 0)
546 		goto error;
547 
548 	if (edid_infop->header.version != 1)
549 		goto error;
550 
551 	return (edid_infop);
552 error:
553 	free(edid_infop);
554 	return (NULL);
555 }
556 
557 static bool
558 efifb_get_edid(edid_res_list_t *res)
559 {
560 	bool rv = false;
561 
562 	if (edid_info == NULL)
563 		edid_info = efifb_gop_get_edid(gop_handle);
564 
565 	if (edid_info != NULL)
566 		rv = gfx_get_edid_resolution(edid_info, res);
567 
568 	return (rv);
569 }
570 
571 bool
572 efi_has_gop(void)
573 {
574 	EFI_STATUS status;
575 	EFI_HANDLE *hlist;
576 	UINTN hsize;
577 
578 	hsize = 0;
579 	hlist = NULL;
580 	status = BS->LocateHandle(ByProtocol, &gop_guid, NULL, &hsize, hlist);
581 
582 	return (status == EFI_BUFFER_TOO_SMALL);
583 }
584 
585 
586 int
587 efi_find_framebuffer(teken_gfx_t *gfx_state)
588 {
589 	EFI_PHYSICAL_ADDRESS ptr;
590 	EFI_HANDLE *hlist;
591 	UINTN nhandles, i, hsize;
592 	struct efi_fb efifb;
593 	EFI_STATUS status;
594 	int rv;
595 
596 	gfx_state->tg_fb_type = FB_TEXT;
597 
598 	hsize = 0;
599 	hlist = NULL;
600 	status = BS->LocateHandle(ByProtocol, &gop_guid, NULL, &hsize, hlist);
601 	if (status == EFI_BUFFER_TOO_SMALL) {
602 		hlist = malloc(hsize);
603 		if (hlist == NULL)
604 			return (ENOMEM);
605 		status = BS->LocateHandle(ByProtocol, &gop_guid, NULL, &hsize,
606 		    hlist);
607 		if (EFI_ERROR(status))
608 			free(hlist);
609 	}
610 
611 	if (EFI_ERROR(status)) {
612 		status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
613 		if (status == EFI_SUCCESS) {
614 			gfx_state->tg_fb_type = FB_UGA;
615 			gfx_state->tg_private = uga;
616 		} else {
617 			return (efi_status_to_errno(status));
618 		}
619 	} else {
620 		nhandles = hsize / sizeof(*hlist);
621 
622 		/*
623 		 * Search for ConOut protocol, if not found, use first handle.
624 		 */
625 		gop_handle = NULL;
626 		for (i = 0; i < nhandles; i++) {
627 			EFI_GRAPHICS_OUTPUT_PROTOCOL *tgop;
628 			void *dummy;
629 
630 			status = OpenProtocolByHandle(hlist[i], &gop_guid,
631 			    (void **)&tgop);
632 			if (status != EFI_SUCCESS)
633 				continue;
634 
635 			if (tgop->Mode->Info->PixelFormat == PixelBltOnly ||
636 			    tgop->Mode->Info->PixelFormat >= PixelFormatMax)
637 				continue;
638 
639 			status = OpenProtocolByHandle(hlist[i], &conout_guid,
640 			    &dummy);
641 			if (status == EFI_SUCCESS) {
642 				gop_handle = hlist[i];
643 				gop = tgop;
644 				break;
645 			} else if (gop_handle == NULL) {
646 				gop_handle = hlist[i];
647 				gop = tgop;
648 			}
649 		}
650 
651 		free(hlist);
652 		if (gop_handle == NULL)
653 			return (ENXIO);
654 
655 		gfx_state->tg_fb_type = FB_GOP;
656 		gfx_state->tg_private = gop;
657 		if (edid_info == NULL)
658 			edid_info = efifb_gop_get_edid(gop_handle);
659 	}
660 
661 	switch (gfx_state->tg_fb_type) {
662 	case FB_GOP:
663 		rv = efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info);
664 		break;
665 
666 	case FB_UGA:
667 		rv = efifb_from_uga(&efifb);
668 		break;
669 
670 	default:
671 		return (EINVAL);
672 	}
673 
674 	if (rv != 0)
675 		return (rv);
676 
677 	gfx_state->tg_fb.fb_addr = efifb.fb_addr;
678 	gfx_state->tg_fb.fb_size = efifb.fb_size;
679 	gfx_state->tg_fb.fb_height = efifb.fb_height;
680 	gfx_state->tg_fb.fb_width = efifb.fb_width;
681 	gfx_state->tg_fb.fb_stride = efifb.fb_stride;
682 	gfx_state->tg_fb.fb_mask_red = efifb.fb_mask_red;
683 	gfx_state->tg_fb.fb_mask_green = efifb.fb_mask_green;
684 	gfx_state->tg_fb.fb_mask_blue = efifb.fb_mask_blue;
685 	gfx_state->tg_fb.fb_mask_reserved = efifb.fb_mask_reserved;
686 
687 	gfx_state->tg_fb.fb_bpp = fls(efifb.fb_mask_red | efifb.fb_mask_green |
688 	    efifb.fb_mask_blue | efifb.fb_mask_reserved);
689 
690 	if (gfx_state->tg_shadow_fb != NULL)
691 		BS->FreePages((uintptr_t)gfx_state->tg_shadow_fb,
692 		    gfx_state->tg_shadow_sz);
693 	gfx_state->tg_shadow_sz =
694 	    EFI_SIZE_TO_PAGES(efifb.fb_height * efifb.fb_width *
695 	    sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
696 	status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
697 	    gfx_state->tg_shadow_sz, &ptr);
698 	gfx_state->tg_shadow_fb = status == EFI_SUCCESS ?
699 	    (uint32_t *)(uintptr_t)ptr : NULL;
700 
701 	return (0);
702 }
703 
704 static void
705 print_efifb(int mode, struct efi_fb *efifb, int verbose)
706 {
707 	u_int depth;
708 
709 	if (mode >= 0)
710 		printf("mode %d: ", mode);
711 	depth = efifb_color_depth(efifb);
712 	printf("%ux%ux%u, stride=%u", efifb->fb_width, efifb->fb_height,
713 	    depth, efifb->fb_stride);
714 	if (verbose) {
715 		printf("\n    frame buffer: address=%jx, size=%jx",
716 		    (uintmax_t)efifb->fb_addr, (uintmax_t)efifb->fb_size);
717 		printf("\n    color mask: R=%08x, G=%08x, B=%08x\n",
718 		    efifb->fb_mask_red, efifb->fb_mask_green,
719 		    efifb->fb_mask_blue);
720 	}
721 }
722 
723 static bool
724 efi_resolution_compare(struct named_resolution *res, const char *cmp)
725 {
726 
727 	if (strcasecmp(res->name, cmp) == 0)
728 		return (true);
729 	if (res->alias != NULL && strcasecmp(res->alias, cmp) == 0)
730 		return (true);
731 	return (false);
732 }
733 
734 
735 static void
736 efi_get_max_resolution(int *width, int *height)
737 {
738 	struct named_resolution *res;
739 	char *maxres;
740 	char *height_start, *width_start;
741 	int idx;
742 
743 	*width = *height = 0;
744 	maxres = getenv("efi_max_resolution");
745 	/* No max_resolution set? Bail out; choose highest resolution */
746 	if (maxres == NULL)
747 		return;
748 	/* See if it matches one of our known resolutions */
749 	for (idx = 0; idx < nitems(resolutions); ++idx) {
750 		res = &resolutions[idx];
751 		if (efi_resolution_compare(res, maxres)) {
752 			*width = res->width;
753 			*height = res->height;
754 			return;
755 		}
756 	}
757 	/* Not a known resolution, try to parse it; make a copy we can modify */
758 	maxres = strdup(maxres);
759 	if (maxres == NULL)
760 		return;
761 	height_start = strchr(maxres, 'x');
762 	if (height_start == NULL) {
763 		free(maxres);
764 		return;
765 	}
766 	width_start = maxres;
767 	*height_start++ = 0;
768 	/* Errors from this will effectively mean "no max" */
769 	*width = (int)strtol(width_start, NULL, 0);
770 	*height = (int)strtol(height_start, NULL, 0);
771 	free(maxres);
772 }
773 
774 static int
775 gop_autoresize(void)
776 {
777 	struct efi_fb efifb;
778 	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
779 	EFI_STATUS status;
780 	UINTN infosz;
781 	UINT32 best_mode, currdim, maxdim, mode;
782 	int height, max_height, max_width, width;
783 
784 	best_mode = maxdim = 0;
785 	efi_get_max_resolution(&max_width, &max_height);
786 	for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
787 		status = gop->QueryMode(gop, mode, &infosz, &info);
788 		if (EFI_ERROR(status))
789 			continue;
790 		efifb_from_gop(&efifb, gop->Mode, info);
791 		width = info->HorizontalResolution;
792 		height = info->VerticalResolution;
793 		currdim = width * height;
794 		if (currdim > maxdim) {
795 			if ((max_width != 0 && width > max_width) ||
796 			    (max_height != 0 && height > max_height))
797 				continue;
798 			maxdim = currdim;
799 			best_mode = mode;
800 		}
801 	}
802 
803 	if (maxdim != 0) {
804 		status = gop->SetMode(gop, best_mode);
805 		if (EFI_ERROR(status)) {
806 			snprintf(command_errbuf, sizeof(command_errbuf),
807 			    "gop_autoresize: Unable to set mode to %u (error=%lu)",
808 			    mode, DECODE_ERROR(status));
809 			return (CMD_ERROR);
810 		}
811 		(void) cons_update_mode(true);
812 	}
813 	return (CMD_OK);
814 }
815 
816 static int
817 text_autoresize()
818 {
819 	SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
820 	EFI_STATUS status;
821 	UINTN i, max_dim, best_mode, cols, rows;
822 
823 	conout = ST->ConOut;
824 	max_dim = best_mode = 0;
825 	for (i = 0; i < conout->Mode->MaxMode; i++) {
826 		status = conout->QueryMode(conout, i, &cols, &rows);
827 		if (EFI_ERROR(status))
828 			continue;
829 		if (cols * rows > max_dim) {
830 			max_dim = cols * rows;
831 			best_mode = i;
832 		}
833 	}
834 	if (max_dim > 0)
835 		conout->SetMode(conout, best_mode);
836 	(void) cons_update_mode(true);
837 	return (CMD_OK);
838 }
839 
840 static int
841 uga_autoresize(void)
842 {
843 
844 	return (text_autoresize());
845 }
846 
847 COMMAND_SET(efi_autoresize, "efi-autoresizecons", "EFI Auto-resize Console", command_autoresize);
848 
849 static int
850 command_autoresize(int argc, char *argv[])
851 {
852 	char *textmode;
853 
854 	textmode = getenv("hw.vga.textmode");
855 	/* If it's set and non-zero, we'll select a console mode instead */
856 	if (textmode != NULL && strcmp(textmode, "0") != 0)
857 		return (text_autoresize());
858 
859 	if (gop != NULL)
860 		return (gop_autoresize());
861 
862 	if (uga != NULL)
863 		return (uga_autoresize());
864 
865 	snprintf(command_errbuf, sizeof(command_errbuf),
866 	    "%s: Neither Graphics Output Protocol nor Universal Graphics Adapter present",
867 	    argv[0]);
868 
869 	/*
870 	 * Default to text_autoresize if we have neither GOP or UGA.  This won't
871 	 * give us the most ideal resolution, but it will at least leave us
872 	 * functional rather than failing the boot for an objectively bad
873 	 * reason.
874 	 */
875 	return (text_autoresize());
876 }
877 
878 COMMAND_SET(gop, "gop", "graphics output protocol", command_gop);
879 
880 static int
881 command_gop(int argc, char *argv[])
882 {
883 	struct efi_fb efifb;
884 	EFI_STATUS status;
885 	u_int mode;
886 	extern bool ignore_gop_blt;
887 
888 	if (gop == NULL) {
889 		snprintf(command_errbuf, sizeof(command_errbuf),
890 		    "%s: Graphics Output Protocol not present", argv[0]);
891 		return (CMD_ERROR);
892 	}
893 
894 	if (argc < 2)
895 		goto usage;
896 
897 	if (strcmp(argv[1], "set") == 0) {
898 		char *cp;
899 
900 		if (argc != 3)
901 			goto usage;
902 		mode = strtol(argv[2], &cp, 0);
903 		if (cp[0] != '\0') {
904 			sprintf(command_errbuf, "mode is an integer");
905 			return (CMD_ERROR);
906 		}
907 		status = gop->SetMode(gop, mode);
908 		if (EFI_ERROR(status)) {
909 			snprintf(command_errbuf, sizeof(command_errbuf),
910 			    "%s: Unable to set mode to %u (error=%lu)",
911 			    argv[0], mode, DECODE_ERROR(status));
912 			return (CMD_ERROR);
913 		}
914 		(void) cons_update_mode(true);
915 	} else if (strcmp(argv[1], "blt") == 0) {
916 		/*
917 		 * "blt on" does allow gop->Blt() to be used (default).
918 		 * "blt off" does block gop->Blt() to be used and use
919 		 * software rendering instead.
920 		 */
921 		if (argc != 3)
922 			goto usage;
923 		if (strcmp(argv[2], "on") == 0)
924 			ignore_gop_blt = false;
925 		else if (strcmp(argv[2], "off") == 0)
926 			ignore_gop_blt = true;
927 		else
928 			goto usage;
929 	} else if (strcmp(argv[1], "off") == 0) {
930 		/*
931 		 * Tell console to use SimpleTextOutput protocol.
932 		 * This means that we do not render the glyphs, but rely on
933 		 * UEFI firmware to draw on ConsOut device(s).
934 		 */
935 		(void) cons_update_mode(false);
936 	} else if (strcmp(argv[1], "get") == 0) {
937 		edid_res_list_t res;
938 
939 		if (argc != 2)
940 			goto usage;
941 		TAILQ_INIT(&res);
942 		efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info);
943 		if (efifb_get_edid(&res)) {
944 			struct resolution *rp;
945 
946 			printf("EDID");
947 			while ((rp = TAILQ_FIRST(&res)) != NULL) {
948 				printf(" %dx%d", rp->width, rp->height);
949 				TAILQ_REMOVE(&res, rp, next);
950 				free(rp);
951 			}
952 			printf("\n");
953 		} else {
954 			printf("no EDID information\n");
955 		}
956 		print_efifb(gop->Mode->Mode, &efifb, 1);
957 		printf("\n");
958 	} else if (strcmp(argv[1], "list") == 0) {
959 		EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
960 		UINTN infosz;
961 
962 		if (argc != 2)
963 			goto usage;
964 
965 		pager_open();
966 		for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
967 			status = gop->QueryMode(gop, mode, &infosz, &info);
968 			if (EFI_ERROR(status))
969 				continue;
970 			efifb_from_gop(&efifb, gop->Mode, info);
971 			print_efifb(mode, &efifb, 0);
972 			if (pager_output("\n"))
973 				break;
974 		}
975 		pager_close();
976 	}
977 	return (CMD_OK);
978 
979  usage:
980 	snprintf(command_errbuf, sizeof(command_errbuf),
981 	    "usage: %s [list | get | set <mode> | off | blt <on|off>]", argv[0]);
982 	return (CMD_ERROR);
983 }
984 
985 COMMAND_SET(uga, "uga", "universal graphics adapter", command_uga);
986 
987 static int
988 command_uga(int argc, char *argv[])
989 {
990 	struct efi_fb efifb;
991 
992 	if (uga == NULL) {
993 		snprintf(command_errbuf, sizeof(command_errbuf),
994 		    "%s: UGA Protocol not present", argv[0]);
995 		return (CMD_ERROR);
996 	}
997 
998 	if (argc != 1)
999 		goto usage;
1000 
1001 	if (efifb_from_uga(&efifb) != CMD_OK) {
1002 		snprintf(command_errbuf, sizeof(command_errbuf),
1003 		    "%s: Unable to get UGA information", argv[0]);
1004 		return (CMD_ERROR);
1005 	}
1006 
1007 	print_efifb(-1, &efifb, 1);
1008 	printf("\n");
1009 	return (CMD_OK);
1010 
1011  usage:
1012 	snprintf(command_errbuf, sizeof(command_errbuf), "usage: %s", argv[0]);
1013 	return (CMD_ERROR);
1014 }
1015