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