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