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