xref: /freebsd/stand/efi/loader/framebuffer.c (revision ef698fabe42827bad43bf046143ef26e47d57514)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (c) 2013 The FreeBSD Foundation
3ca987d46SWarner Losh  * All rights reserved.
4ca987d46SWarner Losh  *
5ca987d46SWarner Losh  * This software was developed by Benno Rice under sponsorship from
6ca987d46SWarner Losh  * the FreeBSD Foundation.
7ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
8ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
9ca987d46SWarner Losh  * are met:
10ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
11ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
12ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
13ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
14ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
15ca987d46SWarner Losh  *
16ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ca987d46SWarner Losh  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ca987d46SWarner Losh  * SUCH DAMAGE.
27ca987d46SWarner Losh  */
28ca987d46SWarner Losh 
29ca987d46SWarner Losh #include <sys/cdefs.h>
30ca987d46SWarner Losh __FBSDID("$FreeBSD$");
31ca987d46SWarner Losh 
32ca987d46SWarner Losh #include <bootstrap.h>
33ca987d46SWarner Losh #include <sys/endian.h>
3405e8899dSKyle Evans #include <sys/param.h>
35ca987d46SWarner Losh #include <stand.h>
36ca987d46SWarner Losh 
37ca987d46SWarner Losh #include <efi.h>
38ca987d46SWarner Losh #include <efilib.h>
39ca987d46SWarner Losh #include <efiuga.h>
40ca987d46SWarner Losh #include <efipciio.h>
41ca987d46SWarner Losh #include <machine/metadata.h>
42ca987d46SWarner Losh 
433630506bSToomas Soome #include "bootstrap.h"
44ca987d46SWarner Losh #include "framebuffer.h"
45ca987d46SWarner Losh 
46*ef698fabSToomas Soome EFI_GUID conout_guid = EFI_CONSOLE_OUT_DEVICE_GUID;
473630506bSToomas Soome EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
48ca987d46SWarner Losh static EFI_GUID pciio_guid = EFI_PCI_IO_PROTOCOL_GUID;
49ca987d46SWarner Losh static EFI_GUID uga_guid = EFI_UGA_DRAW_PROTOCOL_GUID;
50ca987d46SWarner Losh 
5105e8899dSKyle Evans static struct named_resolution {
5205e8899dSKyle Evans 	const char *name;
5305e8899dSKyle Evans 	const char *alias;
5405e8899dSKyle Evans 	unsigned int width;
5505e8899dSKyle Evans 	unsigned int height;
5605e8899dSKyle Evans } resolutions[] = {
5705e8899dSKyle Evans 	{
5805e8899dSKyle Evans 		.name = "480p",
5905e8899dSKyle Evans 		.width = 640,
6005e8899dSKyle Evans 		.height = 480,
6105e8899dSKyle Evans 	},
6205e8899dSKyle Evans 	{
6305e8899dSKyle Evans 		.name = "720p",
6405e8899dSKyle Evans 		.width = 1280,
6505e8899dSKyle Evans 		.height = 720,
6605e8899dSKyle Evans 	},
6705e8899dSKyle Evans 	{
6805e8899dSKyle Evans 		.name = "1080p",
6905e8899dSKyle Evans 		.width = 1920,
7005e8899dSKyle Evans 		.height = 1080,
7105e8899dSKyle Evans 	},
7205e8899dSKyle Evans 	{
7305e8899dSKyle Evans 		.name = "2160p",
7405e8899dSKyle Evans 		.alias = "4k",
7505e8899dSKyle Evans 		.width = 3840,
7605e8899dSKyle Evans 		.height = 2160,
7705e8899dSKyle Evans 	},
7805e8899dSKyle Evans 	{
7905e8899dSKyle Evans 		.name = "5k",
8005e8899dSKyle Evans 		.width = 5120,
8105e8899dSKyle Evans 		.height = 2880,
8205e8899dSKyle Evans 	}
8305e8899dSKyle Evans };
8405e8899dSKyle Evans 
85ca987d46SWarner Losh static u_int
86ca987d46SWarner Losh efifb_color_depth(struct efi_fb *efifb)
87ca987d46SWarner Losh {
88ca987d46SWarner Losh 	uint32_t mask;
89ca987d46SWarner Losh 	u_int depth;
90ca987d46SWarner Losh 
91ca987d46SWarner Losh 	mask = efifb->fb_mask_red | efifb->fb_mask_green |
92ca987d46SWarner Losh 	    efifb->fb_mask_blue | efifb->fb_mask_reserved;
93ca987d46SWarner Losh 	if (mask == 0)
94ca987d46SWarner Losh 		return (0);
95ca987d46SWarner Losh 	for (depth = 1; mask != 1; depth++)
96ca987d46SWarner Losh 		mask >>= 1;
97ca987d46SWarner Losh 	return (depth);
98ca987d46SWarner Losh }
99ca987d46SWarner Losh 
100ca987d46SWarner Losh static int
101ca987d46SWarner Losh efifb_mask_from_pixfmt(struct efi_fb *efifb, EFI_GRAPHICS_PIXEL_FORMAT pixfmt,
102ca987d46SWarner Losh     EFI_PIXEL_BITMASK *pixinfo)
103ca987d46SWarner Losh {
104ca987d46SWarner Losh 	int result;
105ca987d46SWarner Losh 
106ca987d46SWarner Losh 	result = 0;
107ca987d46SWarner Losh 	switch (pixfmt) {
108ca987d46SWarner Losh 	case PixelRedGreenBlueReserved8BitPerColor:
1091a816c75SToomas Soome 	case PixelBltOnly:
110ca987d46SWarner Losh 		efifb->fb_mask_red = 0x000000ff;
111ca987d46SWarner Losh 		efifb->fb_mask_green = 0x0000ff00;
112ca987d46SWarner Losh 		efifb->fb_mask_blue = 0x00ff0000;
113ca987d46SWarner Losh 		efifb->fb_mask_reserved = 0xff000000;
114ca987d46SWarner Losh 		break;
115ca987d46SWarner Losh 	case PixelBlueGreenRedReserved8BitPerColor:
116ca987d46SWarner Losh 		efifb->fb_mask_red = 0x00ff0000;
117ca987d46SWarner Losh 		efifb->fb_mask_green = 0x0000ff00;
118ca987d46SWarner Losh 		efifb->fb_mask_blue = 0x000000ff;
119ca987d46SWarner Losh 		efifb->fb_mask_reserved = 0xff000000;
120ca987d46SWarner Losh 		break;
121ca987d46SWarner Losh 	case PixelBitMask:
122ca987d46SWarner Losh 		efifb->fb_mask_red = pixinfo->RedMask;
123ca987d46SWarner Losh 		efifb->fb_mask_green = pixinfo->GreenMask;
124ca987d46SWarner Losh 		efifb->fb_mask_blue = pixinfo->BlueMask;
125ca987d46SWarner Losh 		efifb->fb_mask_reserved = pixinfo->ReservedMask;
126ca987d46SWarner Losh 		break;
127ca987d46SWarner Losh 	default:
128ca987d46SWarner Losh 		result = 1;
129ca987d46SWarner Losh 		break;
130ca987d46SWarner Losh 	}
131ca987d46SWarner Losh 	return (result);
132ca987d46SWarner Losh }
133ca987d46SWarner Losh 
134ca987d46SWarner Losh static int
135ca987d46SWarner Losh efifb_from_gop(struct efi_fb *efifb, EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode,
136ca987d46SWarner Losh     EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info)
137ca987d46SWarner Losh {
138ca987d46SWarner Losh 	int result;
139ca987d46SWarner Losh 
140ca987d46SWarner Losh 	efifb->fb_addr = mode->FrameBufferBase;
141ca987d46SWarner Losh 	efifb->fb_size = mode->FrameBufferSize;
142ca987d46SWarner Losh 	efifb->fb_height = info->VerticalResolution;
143ca987d46SWarner Losh 	efifb->fb_width = info->HorizontalResolution;
144ca987d46SWarner Losh 	efifb->fb_stride = info->PixelsPerScanLine;
145ca987d46SWarner Losh 	result = efifb_mask_from_pixfmt(efifb, info->PixelFormat,
146ca987d46SWarner Losh 	    &info->PixelInformation);
147ca987d46SWarner Losh 	return (result);
148ca987d46SWarner Losh }
149ca987d46SWarner Losh 
150ca987d46SWarner Losh static ssize_t
151ca987d46SWarner Losh efifb_uga_find_pixel(EFI_UGA_DRAW_PROTOCOL *uga, u_int line,
152ca987d46SWarner Losh     EFI_PCI_IO_PROTOCOL *pciio, uint64_t addr, uint64_t size)
153ca987d46SWarner Losh {
154ca987d46SWarner Losh 	EFI_UGA_PIXEL pix0, pix1;
155ca987d46SWarner Losh 	uint8_t *data1, *data2;
156ca987d46SWarner Losh 	size_t count, maxcount = 1024;
157ca987d46SWarner Losh 	ssize_t ofs;
158ca987d46SWarner Losh 	EFI_STATUS status;
159ca987d46SWarner Losh 	u_int idx;
160ca987d46SWarner Losh 
161ca987d46SWarner Losh 	status = uga->Blt(uga, &pix0, EfiUgaVideoToBltBuffer,
162ca987d46SWarner Losh 	    0, line, 0, 0, 1, 1, 0);
163ca987d46SWarner Losh 	if (EFI_ERROR(status)) {
164ca987d46SWarner Losh 		printf("UGA BLT operation failed (video->buffer)");
165ca987d46SWarner Losh 		return (-1);
166ca987d46SWarner Losh 	}
167ca987d46SWarner Losh 	pix1.Red = ~pix0.Red;
168ca987d46SWarner Losh 	pix1.Green = ~pix0.Green;
169ca987d46SWarner Losh 	pix1.Blue = ~pix0.Blue;
170ca987d46SWarner Losh 	pix1.Reserved = 0;
171ca987d46SWarner Losh 
172ca987d46SWarner Losh 	data1 = calloc(maxcount, 2);
173ca987d46SWarner Losh 	if (data1 == NULL) {
174ca987d46SWarner Losh 		printf("Unable to allocate memory");
175ca987d46SWarner Losh 		return (-1);
176ca987d46SWarner Losh 	}
177ca987d46SWarner Losh 	data2 = data1 + maxcount;
178ca987d46SWarner Losh 
179ca987d46SWarner Losh 	ofs = 0;
180ca987d46SWarner Losh 	while (size > 0) {
181ca987d46SWarner Losh 		count = min(size, maxcount);
182ca987d46SWarner Losh 
183ca987d46SWarner Losh 		status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
184ca987d46SWarner Losh 		    EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
185ca987d46SWarner Losh 		    data1);
186ca987d46SWarner Losh 		if (EFI_ERROR(status)) {
187ca987d46SWarner Losh 			printf("Error reading frame buffer (before)");
188ca987d46SWarner Losh 			goto fail;
189ca987d46SWarner Losh 		}
190ca987d46SWarner Losh 		status = uga->Blt(uga, &pix1, EfiUgaBltBufferToVideo,
191ca987d46SWarner Losh 		    0, 0, 0, line, 1, 1, 0);
192ca987d46SWarner Losh 		if (EFI_ERROR(status)) {
193ca987d46SWarner Losh 			printf("UGA BLT operation failed (modify)");
194ca987d46SWarner Losh 			goto fail;
195ca987d46SWarner Losh 		}
196ca987d46SWarner Losh 		status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
197ca987d46SWarner Losh 		    EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
198ca987d46SWarner Losh 		    data2);
199ca987d46SWarner Losh 		if (EFI_ERROR(status)) {
200ca987d46SWarner Losh 			printf("Error reading frame buffer (after)");
201ca987d46SWarner Losh 			goto fail;
202ca987d46SWarner Losh 		}
203ca987d46SWarner Losh 		status = uga->Blt(uga, &pix0, EfiUgaBltBufferToVideo,
204ca987d46SWarner Losh 		    0, 0, 0, line, 1, 1, 0);
205ca987d46SWarner Losh 		if (EFI_ERROR(status)) {
206ca987d46SWarner Losh 			printf("UGA BLT operation failed (restore)");
207ca987d46SWarner Losh 			goto fail;
208ca987d46SWarner Losh 		}
209ca987d46SWarner Losh 		for (idx = 0; idx < count; idx++) {
210ca987d46SWarner Losh 			if (data1[idx] != data2[idx]) {
211ca987d46SWarner Losh 				free(data1);
212ca987d46SWarner Losh 				return (ofs + (idx & ~3));
213ca987d46SWarner Losh 			}
214ca987d46SWarner Losh 		}
215ca987d46SWarner Losh 		ofs += count;
216ca987d46SWarner Losh 		size -= count;
217ca987d46SWarner Losh 	}
218ca987d46SWarner Losh 	printf("No change detected in frame buffer");
219ca987d46SWarner Losh 
220ca987d46SWarner Losh  fail:
221ca987d46SWarner Losh 	printf(" -- error %lu\n", EFI_ERROR_CODE(status));
222ca987d46SWarner Losh 	free(data1);
223ca987d46SWarner Losh 	return (-1);
224ca987d46SWarner Losh }
225ca987d46SWarner Losh 
226ca987d46SWarner Losh static EFI_PCI_IO_PROTOCOL *
227ca987d46SWarner Losh efifb_uga_get_pciio(void)
228ca987d46SWarner Losh {
229ca987d46SWarner Losh 	EFI_PCI_IO_PROTOCOL *pciio;
230ca987d46SWarner Losh 	EFI_HANDLE *buf, *hp;
231ca987d46SWarner Losh 	EFI_STATUS status;
232ca987d46SWarner Losh 	UINTN bufsz;
233ca987d46SWarner Losh 
234ca987d46SWarner Losh 	/* Get all handles that support the UGA protocol. */
235ca987d46SWarner Losh 	bufsz = 0;
236ca987d46SWarner Losh 	status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, NULL);
237ca987d46SWarner Losh 	if (status != EFI_BUFFER_TOO_SMALL)
238ca987d46SWarner Losh 		return (NULL);
239ca987d46SWarner Losh 	buf = malloc(bufsz);
240ca987d46SWarner Losh 	status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, buf);
241ca987d46SWarner Losh 	if (status != EFI_SUCCESS) {
242ca987d46SWarner Losh 		free(buf);
243ca987d46SWarner Losh 		return (NULL);
244ca987d46SWarner Losh 	}
245ca987d46SWarner Losh 	bufsz /= sizeof(EFI_HANDLE);
246ca987d46SWarner Losh 
247ca987d46SWarner Losh 	/* Get the PCI I/O interface of the first handle that supports it. */
248ca987d46SWarner Losh 	pciio = NULL;
249ca987d46SWarner Losh 	for (hp = buf; hp < buf + bufsz; hp++) {
250110d56cbSToomas Soome 		status = OpenProtocolByHandle(*hp, &pciio_guid,
251110d56cbSToomas Soome 		    (void **)&pciio);
252ca987d46SWarner Losh 		if (status == EFI_SUCCESS) {
253ca987d46SWarner Losh 			free(buf);
254ca987d46SWarner Losh 			return (pciio);
255ca987d46SWarner Losh 		}
256ca987d46SWarner Losh 	}
257ca987d46SWarner Losh 	free(buf);
258ca987d46SWarner Losh 	return (NULL);
259ca987d46SWarner Losh }
260ca987d46SWarner Losh 
261ca987d46SWarner Losh static EFI_STATUS
262ca987d46SWarner Losh efifb_uga_locate_framebuffer(EFI_PCI_IO_PROTOCOL *pciio, uint64_t *addrp,
263ca987d46SWarner Losh     uint64_t *sizep)
264ca987d46SWarner Losh {
265ca987d46SWarner Losh 	uint8_t *resattr;
266ca987d46SWarner Losh 	uint64_t addr, size;
267ca987d46SWarner Losh 	EFI_STATUS status;
268ca987d46SWarner Losh 	u_int bar;
269ca987d46SWarner Losh 
270ca987d46SWarner Losh 	if (pciio == NULL)
271ca987d46SWarner Losh 		return (EFI_DEVICE_ERROR);
272ca987d46SWarner Losh 
273ca987d46SWarner Losh 	/* Attempt to get the frame buffer address (imprecise). */
274ca987d46SWarner Losh 	*addrp = 0;
275ca987d46SWarner Losh 	*sizep = 0;
276ca987d46SWarner Losh 	for (bar = 0; bar < 6; bar++) {
277ca987d46SWarner Losh 		status = pciio->GetBarAttributes(pciio, bar, NULL,
278ca987d46SWarner Losh 		    (void **)&resattr);
279ca987d46SWarner Losh 		if (status != EFI_SUCCESS)
280ca987d46SWarner Losh 			continue;
281ca987d46SWarner Losh 		/* XXX magic offsets and constants. */
282ca987d46SWarner Losh 		if (resattr[0] == 0x87 && resattr[3] == 0) {
283ca987d46SWarner Losh 			/* 32-bit address space descriptor (MEMIO) */
284ca987d46SWarner Losh 			addr = le32dec(resattr + 10);
285ca987d46SWarner Losh 			size = le32dec(resattr + 22);
286ca987d46SWarner Losh 		} else if (resattr[0] == 0x8a && resattr[3] == 0) {
287ca987d46SWarner Losh 			/* 64-bit address space descriptor (MEMIO) */
288ca987d46SWarner Losh 			addr = le64dec(resattr + 14);
289ca987d46SWarner Losh 			size = le64dec(resattr + 38);
290ca987d46SWarner Losh 		} else {
291ca987d46SWarner Losh 			addr = 0;
292ca987d46SWarner Losh 			size = 0;
293ca987d46SWarner Losh 		}
294ca987d46SWarner Losh 		BS->FreePool(resattr);
295ca987d46SWarner Losh 		if (addr == 0 || size == 0)
296ca987d46SWarner Losh 			continue;
297ca987d46SWarner Losh 
298ca987d46SWarner Losh 		/* We assume the largest BAR is the frame buffer. */
299ca987d46SWarner Losh 		if (size > *sizep) {
300ca987d46SWarner Losh 			*addrp = addr;
301ca987d46SWarner Losh 			*sizep = size;
302ca987d46SWarner Losh 		}
303ca987d46SWarner Losh 	}
304ca987d46SWarner Losh 	return ((*addrp == 0 || *sizep == 0) ? EFI_DEVICE_ERROR : 0);
305ca987d46SWarner Losh }
306ca987d46SWarner Losh 
307ca987d46SWarner Losh static int
308ca987d46SWarner Losh efifb_from_uga(struct efi_fb *efifb, EFI_UGA_DRAW_PROTOCOL *uga)
309ca987d46SWarner Losh {
310ca987d46SWarner Losh 	EFI_PCI_IO_PROTOCOL *pciio;
311ca987d46SWarner Losh 	char *ev, *p;
312ca987d46SWarner Losh 	EFI_STATUS status;
313ca987d46SWarner Losh 	ssize_t offset;
314ca987d46SWarner Losh 	uint64_t fbaddr;
315ca987d46SWarner Losh 	uint32_t horiz, vert, stride;
316ca987d46SWarner Losh 	uint32_t np, depth, refresh;
317ca987d46SWarner Losh 
318ca987d46SWarner Losh 	status = uga->GetMode(uga, &horiz, &vert, &depth, &refresh);
319ca987d46SWarner Losh 	if (EFI_ERROR(status))
320ca987d46SWarner Losh 		return (1);
321ca987d46SWarner Losh 	efifb->fb_height = vert;
322ca987d46SWarner Losh 	efifb->fb_width = horiz;
323ca987d46SWarner Losh 	/* Paranoia... */
324ca987d46SWarner Losh 	if (efifb->fb_height == 0 || efifb->fb_width == 0)
325ca987d46SWarner Losh 		return (1);
326ca987d46SWarner Losh 
327ca987d46SWarner Losh 	/* The color masks are fixed AFAICT. */
328ca987d46SWarner Losh 	efifb_mask_from_pixfmt(efifb, PixelBlueGreenRedReserved8BitPerColor,
329ca987d46SWarner Losh 	    NULL);
330ca987d46SWarner Losh 
331ca987d46SWarner Losh 	/* pciio can be NULL on return! */
332ca987d46SWarner Losh 	pciio = efifb_uga_get_pciio();
333ca987d46SWarner Losh 
334ca987d46SWarner Losh 	/* Try to find the frame buffer. */
335ca987d46SWarner Losh 	status = efifb_uga_locate_framebuffer(pciio, &efifb->fb_addr,
336ca987d46SWarner Losh 	    &efifb->fb_size);
337ca987d46SWarner Losh 	if (EFI_ERROR(status)) {
338ca987d46SWarner Losh 		efifb->fb_addr = 0;
339ca987d46SWarner Losh 		efifb->fb_size = 0;
340ca987d46SWarner Losh 	}
341ca987d46SWarner Losh 
342ca987d46SWarner Losh 	/*
343ca987d46SWarner Losh 	 * There's no reliable way to detect the frame buffer or the
344ca987d46SWarner Losh 	 * offset within the frame buffer of the visible region, nor
345ca987d46SWarner Losh 	 * the stride. Our only option is to look at the system and
346ca987d46SWarner Losh 	 * fill in the blanks based on that. Luckily, UGA was mostly
347ca987d46SWarner Losh 	 * only used on Apple hardware.
348ca987d46SWarner Losh 	 */
349ca987d46SWarner Losh 	offset = -1;
350ca987d46SWarner Losh 	ev = getenv("smbios.system.maker");
351ca987d46SWarner Losh 	if (ev != NULL && !strcmp(ev, "Apple Inc.")) {
352ca987d46SWarner Losh 		ev = getenv("smbios.system.product");
353ca987d46SWarner Losh 		if (ev != NULL && !strcmp(ev, "iMac7,1")) {
354ca987d46SWarner Losh 			/* These are the expected values we should have. */
355ca987d46SWarner Losh 			horiz = 1680;
356ca987d46SWarner Losh 			vert = 1050;
357ca987d46SWarner Losh 			fbaddr = 0xc0000000;
358ca987d46SWarner Losh 			/* These are the missing bits. */
359ca987d46SWarner Losh 			offset = 0x10000;
360ca987d46SWarner Losh 			stride = 1728;
361ca987d46SWarner Losh 		} else if (ev != NULL && !strcmp(ev, "MacBook3,1")) {
362ca987d46SWarner Losh 			/* These are the expected values we should have. */
363ca987d46SWarner Losh 			horiz = 1280;
364ca987d46SWarner Losh 			vert = 800;
365ca987d46SWarner Losh 			fbaddr = 0xc0000000;
366ca987d46SWarner Losh 			/* These are the missing bits. */
367ca987d46SWarner Losh 			offset = 0x0;
368ca987d46SWarner Losh 			stride = 2048;
369ca987d46SWarner Losh 		}
370ca987d46SWarner Losh 	}
371ca987d46SWarner Losh 
372ca987d46SWarner Losh 	/*
373ca987d46SWarner Losh 	 * If this is hardware we know, make sure that it looks familiar
374ca987d46SWarner Losh 	 * before we accept our hardcoded values.
375ca987d46SWarner Losh 	 */
376ca987d46SWarner Losh 	if (offset >= 0 && efifb->fb_width == horiz &&
377ca987d46SWarner Losh 	    efifb->fb_height == vert && efifb->fb_addr == fbaddr) {
378ca987d46SWarner Losh 		efifb->fb_addr += offset;
379ca987d46SWarner Losh 		efifb->fb_size -= offset;
380ca987d46SWarner Losh 		efifb->fb_stride = stride;
381ca987d46SWarner Losh 		return (0);
382ca987d46SWarner Losh 	} else if (offset >= 0) {
383ca987d46SWarner Losh 		printf("Hardware make/model known, but graphics not "
384ca987d46SWarner Losh 		    "as expected.\n");
385ca987d46SWarner Losh 		printf("Console may not work!\n");
386ca987d46SWarner Losh 	}
387ca987d46SWarner Losh 
388ca987d46SWarner Losh 	/*
389ca987d46SWarner Losh 	 * The stride is equal or larger to the width. Often it's the
390ca987d46SWarner Losh 	 * next larger power of two. We'll start with that...
391ca987d46SWarner Losh 	 */
392ca987d46SWarner Losh 	efifb->fb_stride = efifb->fb_width;
393ca987d46SWarner Losh 	do {
394ca987d46SWarner Losh 		np = efifb->fb_stride & (efifb->fb_stride - 1);
395ca987d46SWarner Losh 		if (np) {
396ca987d46SWarner Losh 			efifb->fb_stride |= (np - 1);
397ca987d46SWarner Losh 			efifb->fb_stride++;
398ca987d46SWarner Losh 		}
399ca987d46SWarner Losh 	} while (np);
400ca987d46SWarner Losh 
401ca987d46SWarner Losh 	ev = getenv("hw.efifb.address");
402ca987d46SWarner Losh 	if (ev == NULL) {
403ca987d46SWarner Losh 		if (efifb->fb_addr == 0) {
404ca987d46SWarner Losh 			printf("Please set hw.efifb.address and "
405ca987d46SWarner Losh 			    "hw.efifb.stride.\n");
406ca987d46SWarner Losh 			return (1);
407ca987d46SWarner Losh 		}
408ca987d46SWarner Losh 
409ca987d46SWarner Losh 		/*
410ca987d46SWarner Losh 		 * The visible part of the frame buffer may not start at
411ca987d46SWarner Losh 		 * offset 0, so try to detect it. Note that we may not
412ca987d46SWarner Losh 		 * always be able to read from the frame buffer, which
413ca987d46SWarner Losh 		 * means that we may not be able to detect anything. In
414ca987d46SWarner Losh 		 * that case, we would take a long time scanning for a
415ca987d46SWarner Losh 		 * pixel change in the frame buffer, which would have it
416ca987d46SWarner Losh 		 * appear that we're hanging, so we limit the scan to
417ca987d46SWarner Losh 		 * 1/256th of the frame buffer. This number is mostly
418ca987d46SWarner Losh 		 * based on PR 202730 and the fact that on a MacBoook,
419ca987d46SWarner Losh 		 * where we can't read from the frame buffer the offset
420ca987d46SWarner Losh 		 * of the visible region is 0. In short: we want to scan
421ca987d46SWarner Losh 		 * enough to handle all adapters that have an offset
422ca987d46SWarner Losh 		 * larger than 0 and we want to scan as little as we can
423ca987d46SWarner Losh 		 * to not appear to hang when we can't read from the
424ca987d46SWarner Losh 		 * frame buffer.
425ca987d46SWarner Losh 		 */
426ca987d46SWarner Losh 		offset = efifb_uga_find_pixel(uga, 0, pciio, efifb->fb_addr,
427ca987d46SWarner Losh 		    efifb->fb_size >> 8);
428ca987d46SWarner Losh 		if (offset == -1) {
429ca987d46SWarner Losh 			printf("Unable to reliably detect frame buffer.\n");
430ca987d46SWarner Losh 		} else if (offset > 0) {
431ca987d46SWarner Losh 			efifb->fb_addr += offset;
432ca987d46SWarner Losh 			efifb->fb_size -= offset;
433ca987d46SWarner Losh 		}
434ca987d46SWarner Losh 	} else {
435ca987d46SWarner Losh 		offset = 0;
436ca987d46SWarner Losh 		efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
437ca987d46SWarner Losh 		efifb->fb_addr = strtoul(ev, &p, 0);
438ca987d46SWarner Losh 		if (*p != '\0')
439ca987d46SWarner Losh 			return (1);
440ca987d46SWarner Losh 	}
441ca987d46SWarner Losh 
442ca987d46SWarner Losh 	ev = getenv("hw.efifb.stride");
443ca987d46SWarner Losh 	if (ev == NULL) {
444ca987d46SWarner Losh 		if (pciio != NULL && offset != -1) {
445ca987d46SWarner Losh 			/* Determine the stride. */
446ca987d46SWarner Losh 			offset = efifb_uga_find_pixel(uga, 1, pciio,
447ca987d46SWarner Losh 			    efifb->fb_addr, horiz * 8);
448ca987d46SWarner Losh 			if (offset != -1)
449ca987d46SWarner Losh 				efifb->fb_stride = offset >> 2;
450ca987d46SWarner Losh 		} else {
451ca987d46SWarner Losh 			printf("Unable to reliably detect the stride.\n");
452ca987d46SWarner Losh 		}
453ca987d46SWarner Losh 	} else {
454ca987d46SWarner Losh 		efifb->fb_stride = strtoul(ev, &p, 0);
455ca987d46SWarner Losh 		if (*p != '\0')
456ca987d46SWarner Losh 			return (1);
457ca987d46SWarner Losh 	}
458ca987d46SWarner Losh 
459ca987d46SWarner Losh 	/*
460ca987d46SWarner Losh 	 * We finalized on the stride, so recalculate the size of the
461ca987d46SWarner Losh 	 * frame buffer.
462ca987d46SWarner Losh 	 */
463ca987d46SWarner Losh 	efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
464ca987d46SWarner Losh 	return (0);
465ca987d46SWarner Losh }
466ca987d46SWarner Losh 
467ca987d46SWarner Losh int
46850180d2bSToomas Soome efi_find_framebuffer(teken_gfx_t *gfx_state)
469ca987d46SWarner Losh {
470*ef698fabSToomas Soome 	EFI_HANDLE h, *hlist;
471*ef698fabSToomas Soome 	UINTN nhandles, i, hsize;
47250180d2bSToomas Soome 	struct efi_fb efifb;
473ca987d46SWarner Losh 	EFI_GRAPHICS_OUTPUT *gop;
474ca987d46SWarner Losh 	EFI_UGA_DRAW_PROTOCOL *uga;
475ca987d46SWarner Losh 	EFI_STATUS status;
47650180d2bSToomas Soome 	int rv;
47750180d2bSToomas Soome 
47850180d2bSToomas Soome 	gfx_state->tg_fb_type = FB_TEXT;
479ca987d46SWarner Losh 
480*ef698fabSToomas Soome 	hsize = 0;
481*ef698fabSToomas Soome 	status = BS->LocateHandle(ByProtocol, &gop_guid, NULL, &hsize, hlist);
482*ef698fabSToomas Soome 	if (status == EFI_BUFFER_TOO_SMALL) {
483*ef698fabSToomas Soome 		hlist = malloc(hsize);
484*ef698fabSToomas Soome 		if (hlist == NULL)
485*ef698fabSToomas Soome 			return (ENOMEM);
486*ef698fabSToomas Soome 		status = BS->LocateHandle(ByProtocol, &gop_guid, NULL, &hsize,
487*ef698fabSToomas Soome 		    hlist);
488*ef698fabSToomas Soome 		if (EFI_ERROR(status))
489*ef698fabSToomas Soome 			free(hlist);
490*ef698fabSToomas Soome 	}
491*ef698fabSToomas Soome 	if (EFI_ERROR(status))
492*ef698fabSToomas Soome 		return (efi_status_to_errno(status));
493*ef698fabSToomas Soome 
494*ef698fabSToomas Soome 	nhandles = hsize / sizeof(*hlist);
495*ef698fabSToomas Soome 
496*ef698fabSToomas Soome 	/*
497*ef698fabSToomas Soome 	 * Search for ConOut protocol, if not found, use first handle.
498*ef698fabSToomas Soome 	 */
499*ef698fabSToomas Soome 	h = *hlist;
500*ef698fabSToomas Soome 	for (i = 0; i < nhandles; i++) {
501*ef698fabSToomas Soome 		void *dummy = NULL;
502*ef698fabSToomas Soome 
503*ef698fabSToomas Soome 		status = OpenProtocolByHandle(hlist[i], &conout_guid, &dummy);
504*ef698fabSToomas Soome 		if (status == EFI_SUCCESS) {
505*ef698fabSToomas Soome 			h = hlist[i];
506*ef698fabSToomas Soome 			break;
507*ef698fabSToomas Soome 		}
508*ef698fabSToomas Soome 	}
509*ef698fabSToomas Soome 
510*ef698fabSToomas Soome 	status = OpenProtocolByHandle(h, &gop_guid, (void **)&gop);
511*ef698fabSToomas Soome 	free(hlist);
512*ef698fabSToomas Soome 
51350180d2bSToomas Soome 	if (status == EFI_SUCCESS) {
51450180d2bSToomas Soome 		gfx_state->tg_fb_type = FB_GOP;
51550180d2bSToomas Soome 		gfx_state->tg_private = gop;
51650180d2bSToomas Soome 	} else {
517ca987d46SWarner Losh 		status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
51850180d2bSToomas Soome 		if (status == EFI_SUCCESS) {
51950180d2bSToomas Soome 			gfx_state->tg_fb_type = FB_UGA;
52050180d2bSToomas Soome 			gfx_state->tg_private = uga;
52150180d2bSToomas Soome 		} else {
522ca987d46SWarner Losh 			return (1);
523ca987d46SWarner Losh 		}
52450180d2bSToomas Soome 	}
52550180d2bSToomas Soome 
52650180d2bSToomas Soome 	switch (gfx_state->tg_fb_type) {
52750180d2bSToomas Soome 	case FB_GOP:
52850180d2bSToomas Soome 		rv = efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info);
52950180d2bSToomas Soome 		break;
53050180d2bSToomas Soome 
53150180d2bSToomas Soome 	case FB_UGA:
53250180d2bSToomas Soome 		rv = efifb_from_uga(&efifb, uga);
53350180d2bSToomas Soome 		break;
53450180d2bSToomas Soome 
53550180d2bSToomas Soome 	default:
53650180d2bSToomas Soome 		return (1);
53750180d2bSToomas Soome 	}
53850180d2bSToomas Soome 
53950180d2bSToomas Soome 	gfx_state->tg_fb.fb_addr = efifb.fb_addr;
54050180d2bSToomas Soome 	gfx_state->tg_fb.fb_size = efifb.fb_size;
54150180d2bSToomas Soome 	gfx_state->tg_fb.fb_height = efifb.fb_height;
54250180d2bSToomas Soome 	gfx_state->tg_fb.fb_width = efifb.fb_width;
54350180d2bSToomas Soome 	gfx_state->tg_fb.fb_stride = efifb.fb_stride;
54450180d2bSToomas Soome 	gfx_state->tg_fb.fb_mask_red = efifb.fb_mask_red;
54550180d2bSToomas Soome 	gfx_state->tg_fb.fb_mask_green = efifb.fb_mask_green;
54650180d2bSToomas Soome 	gfx_state->tg_fb.fb_mask_blue = efifb.fb_mask_blue;
54750180d2bSToomas Soome 	gfx_state->tg_fb.fb_mask_reserved = efifb.fb_mask_reserved;
54850180d2bSToomas Soome 
54950180d2bSToomas Soome 	gfx_state->tg_fb.fb_bpp = fls(efifb.fb_mask_red | efifb.fb_mask_green |
55050180d2bSToomas Soome 	    efifb.fb_mask_blue | efifb.fb_mask_reserved);
55150180d2bSToomas Soome 
55250180d2bSToomas Soome 	return (0);
55350180d2bSToomas Soome }
554ca987d46SWarner Losh 
555ca987d46SWarner Losh static void
556ca987d46SWarner Losh print_efifb(int mode, struct efi_fb *efifb, int verbose)
557ca987d46SWarner Losh {
558ca987d46SWarner Losh 	u_int depth;
559ca987d46SWarner Losh 
560ca987d46SWarner Losh 	if (mode >= 0)
561ca987d46SWarner Losh 		printf("mode %d: ", mode);
562ca987d46SWarner Losh 	depth = efifb_color_depth(efifb);
563ca987d46SWarner Losh 	printf("%ux%ux%u, stride=%u", efifb->fb_width, efifb->fb_height,
564ca987d46SWarner Losh 	    depth, efifb->fb_stride);
565ca987d46SWarner Losh 	if (verbose) {
566ca987d46SWarner Losh 		printf("\n    frame buffer: address=%jx, size=%jx",
567ca987d46SWarner Losh 		    (uintmax_t)efifb->fb_addr, (uintmax_t)efifb->fb_size);
568ca987d46SWarner Losh 		printf("\n    color mask: R=%08x, G=%08x, B=%08x\n",
569ca987d46SWarner Losh 		    efifb->fb_mask_red, efifb->fb_mask_green,
570ca987d46SWarner Losh 		    efifb->fb_mask_blue);
571ca987d46SWarner Losh 	}
572ca987d46SWarner Losh }
573ca987d46SWarner Losh 
57405e8899dSKyle Evans static bool
57505e8899dSKyle Evans efi_resolution_compare(struct named_resolution *res, const char *cmp)
57605e8899dSKyle Evans {
57705e8899dSKyle Evans 
57805e8899dSKyle Evans 	if (strcasecmp(res->name, cmp) == 0)
57905e8899dSKyle Evans 		return (true);
58005e8899dSKyle Evans 	if (res->alias != NULL && strcasecmp(res->alias, cmp) == 0)
58105e8899dSKyle Evans 		return (true);
58205e8899dSKyle Evans 	return (false);
58305e8899dSKyle Evans }
58405e8899dSKyle Evans 
58505e8899dSKyle Evans 
58605e8899dSKyle Evans static void
58705e8899dSKyle Evans efi_get_max_resolution(int *width, int *height)
58805e8899dSKyle Evans {
58905e8899dSKyle Evans 	struct named_resolution *res;
59005e8899dSKyle Evans 	char *maxres;
59105e8899dSKyle Evans 	char *height_start, *width_start;
59205e8899dSKyle Evans 	int idx;
59305e8899dSKyle Evans 
59405e8899dSKyle Evans 	*width = *height = 0;
59505e8899dSKyle Evans 	maxres = getenv("efi_max_resolution");
59605e8899dSKyle Evans 	/* No max_resolution set? Bail out; choose highest resolution */
59705e8899dSKyle Evans 	if (maxres == NULL)
59805e8899dSKyle Evans 		return;
59905e8899dSKyle Evans 	/* See if it matches one of our known resolutions */
60005e8899dSKyle Evans 	for (idx = 0; idx < nitems(resolutions); ++idx) {
60105e8899dSKyle Evans 		res = &resolutions[idx];
60205e8899dSKyle Evans 		if (efi_resolution_compare(res, maxres)) {
60305e8899dSKyle Evans 			*width = res->width;
60405e8899dSKyle Evans 			*height = res->height;
60505e8899dSKyle Evans 			return;
60605e8899dSKyle Evans 		}
60705e8899dSKyle Evans 	}
60805e8899dSKyle Evans 	/* Not a known resolution, try to parse it; make a copy we can modify */
60905e8899dSKyle Evans 	maxres = strdup(maxres);
61005e8899dSKyle Evans 	if (maxres == NULL)
61105e8899dSKyle Evans 		return;
61205e8899dSKyle Evans 	height_start = strchr(maxres, 'x');
61305e8899dSKyle Evans 	if (height_start == NULL) {
61405e8899dSKyle Evans 		free(maxres);
61505e8899dSKyle Evans 		return;
61605e8899dSKyle Evans 	}
61705e8899dSKyle Evans 	width_start = maxres;
61805e8899dSKyle Evans 	*height_start++ = 0;
61905e8899dSKyle Evans 	/* Errors from this will effectively mean "no max" */
62005e8899dSKyle Evans 	*width = (int)strtol(width_start, NULL, 0);
62105e8899dSKyle Evans 	*height = (int)strtol(height_start, NULL, 0);
62205e8899dSKyle Evans 	free(maxres);
62305e8899dSKyle Evans }
62405e8899dSKyle Evans 
6255f8cfbe1SKyle Evans static int
6265f8cfbe1SKyle Evans gop_autoresize(EFI_GRAPHICS_OUTPUT *gop)
6275f8cfbe1SKyle Evans {
6285f8cfbe1SKyle Evans 	struct efi_fb efifb;
6295f8cfbe1SKyle Evans 	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
6305f8cfbe1SKyle Evans 	EFI_STATUS status;
6315f8cfbe1SKyle Evans 	UINTN infosz;
6325f8cfbe1SKyle Evans 	UINT32 best_mode, currdim, maxdim, mode;
63305e8899dSKyle Evans 	int height, max_height, max_width, width;
6345f8cfbe1SKyle Evans 
6355f8cfbe1SKyle Evans 	best_mode = maxdim = 0;
63605e8899dSKyle Evans 	efi_get_max_resolution(&max_width, &max_height);
6375f8cfbe1SKyle Evans 	for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
6385f8cfbe1SKyle Evans 		status = gop->QueryMode(gop, mode, &infosz, &info);
6395f8cfbe1SKyle Evans 		if (EFI_ERROR(status))
6405f8cfbe1SKyle Evans 			continue;
6415f8cfbe1SKyle Evans 		efifb_from_gop(&efifb, gop->Mode, info);
64205e8899dSKyle Evans 		width = info->HorizontalResolution;
64305e8899dSKyle Evans 		height = info->VerticalResolution;
64405e8899dSKyle Evans 		currdim = width * height;
6455f8cfbe1SKyle Evans 		if (currdim > maxdim) {
64605e8899dSKyle Evans 			if ((max_width != 0 && width > max_width) ||
64705e8899dSKyle Evans 			    (max_height != 0 && height > max_height))
64805e8899dSKyle Evans 				continue;
6495f8cfbe1SKyle Evans 			maxdim = currdim;
6505f8cfbe1SKyle Evans 			best_mode = mode;
6515f8cfbe1SKyle Evans 		}
6525f8cfbe1SKyle Evans 	}
6535f8cfbe1SKyle Evans 
654807dbf2bSKyle Evans 	if (maxdim != 0) {
6555f8cfbe1SKyle Evans 		status = gop->SetMode(gop, best_mode);
6565f8cfbe1SKyle Evans 		if (EFI_ERROR(status)) {
6575f8cfbe1SKyle Evans 			snprintf(command_errbuf, sizeof(command_errbuf),
6585f8cfbe1SKyle Evans 			    "gop_autoresize: Unable to set mode to %u (error=%lu)",
6595f8cfbe1SKyle Evans 			    mode, EFI_ERROR_CODE(status));
6605f8cfbe1SKyle Evans 			return (CMD_ERROR);
6615f8cfbe1SKyle Evans 		}
6623630506bSToomas Soome 		(void) cons_update_mode(true);
663807dbf2bSKyle Evans 	}
6645f8cfbe1SKyle Evans 	return (CMD_OK);
6655f8cfbe1SKyle Evans }
6665f8cfbe1SKyle Evans 
6675f8cfbe1SKyle Evans static int
6681c169279SKyle Evans text_autoresize()
6691c169279SKyle Evans {
6701c169279SKyle Evans 	SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
6711c169279SKyle Evans 	EFI_STATUS status;
6721c169279SKyle Evans 	UINTN i, max_dim, best_mode, cols, rows;
6731c169279SKyle Evans 
6741c169279SKyle Evans 	conout = ST->ConOut;
6751c169279SKyle Evans 	max_dim = best_mode = 0;
6761c169279SKyle Evans 	for (i = 0; i < conout->Mode->MaxMode; i++) {
6771c169279SKyle Evans 		status = conout->QueryMode(conout, i, &cols, &rows);
6781c169279SKyle Evans 		if (EFI_ERROR(status))
6791c169279SKyle Evans 			continue;
6801c169279SKyle Evans 		if (cols * rows > max_dim) {
6811c169279SKyle Evans 			max_dim = cols * rows;
6821c169279SKyle Evans 			best_mode = i;
6831c169279SKyle Evans 		}
6841c169279SKyle Evans 	}
6851c169279SKyle Evans 	if (max_dim > 0)
6861c169279SKyle Evans 		conout->SetMode(conout, best_mode);
6873630506bSToomas Soome 	(void) cons_update_mode(true);
6881c169279SKyle Evans 	return (CMD_OK);
6891c169279SKyle Evans }
6901c169279SKyle Evans 
6911c169279SKyle Evans static int
69276e00374SKyle Evans uga_autoresize(EFI_UGA_DRAW_PROTOCOL *uga)
6935f8cfbe1SKyle Evans {
6945f8cfbe1SKyle Evans 
69576e00374SKyle Evans 	return (text_autoresize());
6965f8cfbe1SKyle Evans }
6975f8cfbe1SKyle Evans 
6985f8cfbe1SKyle Evans COMMAND_SET(efi_autoresize, "efi-autoresizecons", "EFI Auto-resize Console", command_autoresize);
6995f8cfbe1SKyle Evans 
7005f8cfbe1SKyle Evans static int
7015f8cfbe1SKyle Evans command_autoresize(int argc, char *argv[])
7025f8cfbe1SKyle Evans {
7035f8cfbe1SKyle Evans 	EFI_GRAPHICS_OUTPUT *gop;
7045f8cfbe1SKyle Evans 	EFI_UGA_DRAW_PROTOCOL *uga;
7051c169279SKyle Evans 	char *textmode;
7065f8cfbe1SKyle Evans 	EFI_STATUS status;
7075f8cfbe1SKyle Evans 	u_int mode;
7085f8cfbe1SKyle Evans 
7091c169279SKyle Evans 	textmode = getenv("hw.vga.textmode");
7101c169279SKyle Evans 	/* If it's set and non-zero, we'll select a console mode instead */
7111c169279SKyle Evans 	if (textmode != NULL && strcmp(textmode, "0") != 0)
7121c169279SKyle Evans 		return (text_autoresize());
7131c169279SKyle Evans 
7145f8cfbe1SKyle Evans 	gop = NULL;
7155f8cfbe1SKyle Evans 	uga = NULL;
7165f8cfbe1SKyle Evans 	status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
7175f8cfbe1SKyle Evans 	if (EFI_ERROR(status) == 0)
7185f8cfbe1SKyle Evans 		return (gop_autoresize(gop));
7195f8cfbe1SKyle Evans 
7205f8cfbe1SKyle Evans 	status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
7215f8cfbe1SKyle Evans 	if (EFI_ERROR(status) == 0)
7225f8cfbe1SKyle Evans 		return (uga_autoresize(uga));
7235f8cfbe1SKyle Evans 
7245f8cfbe1SKyle Evans 	snprintf(command_errbuf, sizeof(command_errbuf),
7255f8cfbe1SKyle Evans 	    "%s: Neither Graphics Output Protocol nor Universal Graphics Adapter present",
7265f8cfbe1SKyle Evans 	    argv[0]);
72737528fb4SKyle Evans 
72837528fb4SKyle Evans 	/*
72937528fb4SKyle Evans 	 * Default to text_autoresize if we have neither GOP or UGA.  This won't
73037528fb4SKyle Evans 	 * give us the most ideal resolution, but it will at least leave us
73137528fb4SKyle Evans 	 * functional rather than failing the boot for an objectively bad
73237528fb4SKyle Evans 	 * reason.
73337528fb4SKyle Evans 	 */
73437528fb4SKyle Evans 	return (text_autoresize());
7355f8cfbe1SKyle Evans }
7365f8cfbe1SKyle Evans 
737ca987d46SWarner Losh COMMAND_SET(gop, "gop", "graphics output protocol", command_gop);
738ca987d46SWarner Losh 
739ca987d46SWarner Losh static int
740ca987d46SWarner Losh command_gop(int argc, char *argv[])
741ca987d46SWarner Losh {
742ca987d46SWarner Losh 	struct efi_fb efifb;
743ca987d46SWarner Losh 	EFI_GRAPHICS_OUTPUT *gop;
744ca987d46SWarner Losh 	EFI_STATUS status;
745ca987d46SWarner Losh 	u_int mode;
746ca987d46SWarner Losh 
747ca987d46SWarner Losh 	status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
748ca987d46SWarner Losh 	if (EFI_ERROR(status)) {
749ca987d46SWarner Losh 		snprintf(command_errbuf, sizeof(command_errbuf),
750ca987d46SWarner Losh 		    "%s: Graphics Output Protocol not present (error=%lu)",
751ca987d46SWarner Losh 		    argv[0], EFI_ERROR_CODE(status));
752ca987d46SWarner Losh 		return (CMD_ERROR);
753ca987d46SWarner Losh 	}
754ca987d46SWarner Losh 
755ca987d46SWarner Losh 	if (argc < 2)
756ca987d46SWarner Losh 		goto usage;
757ca987d46SWarner Losh 
758ca987d46SWarner Losh 	if (!strcmp(argv[1], "set")) {
759ca987d46SWarner Losh 		char *cp;
760ca987d46SWarner Losh 
761ca987d46SWarner Losh 		if (argc != 3)
762ca987d46SWarner Losh 			goto usage;
763ca987d46SWarner Losh 		mode = strtol(argv[2], &cp, 0);
764ca987d46SWarner Losh 		if (cp[0] != '\0') {
765ca987d46SWarner Losh 			sprintf(command_errbuf, "mode is an integer");
766ca987d46SWarner Losh 			return (CMD_ERROR);
767ca987d46SWarner Losh 		}
768ca987d46SWarner Losh 		status = gop->SetMode(gop, mode);
769ca987d46SWarner Losh 		if (EFI_ERROR(status)) {
770ca987d46SWarner Losh 			snprintf(command_errbuf, sizeof(command_errbuf),
771ca987d46SWarner Losh 			    "%s: Unable to set mode to %u (error=%lu)",
772ca987d46SWarner Losh 			    argv[0], mode, EFI_ERROR_CODE(status));
773ca987d46SWarner Losh 			return (CMD_ERROR);
774ca987d46SWarner Losh 		}
7753630506bSToomas Soome 		(void) cons_update_mode(true);
7763630506bSToomas Soome 	} else if (strcmp(argv[1], "off") == 0) {
7773630506bSToomas Soome 		(void) cons_update_mode(false);
7783630506bSToomas Soome 	} else if (strcmp(argv[1], "get") == 0) {
779ca987d46SWarner Losh 		if (argc != 2)
780ca987d46SWarner Losh 			goto usage;
781ca987d46SWarner Losh 		efifb_from_gop(&efifb, gop->Mode, gop->Mode->Info);
782ca987d46SWarner Losh 		print_efifb(gop->Mode->Mode, &efifb, 1);
783ca987d46SWarner Losh 		printf("\n");
784ca987d46SWarner Losh 	} else if (!strcmp(argv[1], "list")) {
785ca987d46SWarner Losh 		EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
786ca987d46SWarner Losh 		UINTN infosz;
787ca987d46SWarner Losh 
788ca987d46SWarner Losh 		if (argc != 2)
789ca987d46SWarner Losh 			goto usage;
790ca987d46SWarner Losh 		pager_open();
791ca987d46SWarner Losh 		for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
792ca987d46SWarner Losh 			status = gop->QueryMode(gop, mode, &infosz, &info);
793ca987d46SWarner Losh 			if (EFI_ERROR(status))
794ca987d46SWarner Losh 				continue;
795ca987d46SWarner Losh 			efifb_from_gop(&efifb, gop->Mode, info);
796ca987d46SWarner Losh 			print_efifb(mode, &efifb, 0);
797ca987d46SWarner Losh 			if (pager_output("\n"))
798ca987d46SWarner Losh 				break;
799ca987d46SWarner Losh 		}
800ca987d46SWarner Losh 		pager_close();
801ca987d46SWarner Losh 	}
802ca987d46SWarner Losh 	return (CMD_OK);
803ca987d46SWarner Losh 
804ca987d46SWarner Losh  usage:
805ca987d46SWarner Losh 	snprintf(command_errbuf, sizeof(command_errbuf),
8063630506bSToomas Soome 	    "usage: %s [list | get | set <mode> | off]", argv[0]);
807ca987d46SWarner Losh 	return (CMD_ERROR);
808ca987d46SWarner Losh }
809ca987d46SWarner Losh 
810ca987d46SWarner Losh COMMAND_SET(uga, "uga", "universal graphics adapter", command_uga);
811ca987d46SWarner Losh 
812ca987d46SWarner Losh static int
813ca987d46SWarner Losh command_uga(int argc, char *argv[])
814ca987d46SWarner Losh {
815ca987d46SWarner Losh 	struct efi_fb efifb;
816ca987d46SWarner Losh 	EFI_UGA_DRAW_PROTOCOL *uga;
817ca987d46SWarner Losh 	EFI_STATUS status;
818ca987d46SWarner Losh 
819ca987d46SWarner Losh 	status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
820ca987d46SWarner Losh 	if (EFI_ERROR(status)) {
821ca987d46SWarner Losh 		snprintf(command_errbuf, sizeof(command_errbuf),
822ca987d46SWarner Losh 		    "%s: UGA Protocol not present (error=%lu)",
823ca987d46SWarner Losh 		    argv[0], EFI_ERROR_CODE(status));
824ca987d46SWarner Losh 		return (CMD_ERROR);
825ca987d46SWarner Losh 	}
826ca987d46SWarner Losh 
827ca987d46SWarner Losh 	if (argc != 1)
828ca987d46SWarner Losh 		goto usage;
829ca987d46SWarner Losh 
830ca987d46SWarner Losh 	if (efifb_from_uga(&efifb, uga) != CMD_OK) {
831ca987d46SWarner Losh 		snprintf(command_errbuf, sizeof(command_errbuf),
832ca987d46SWarner Losh 		    "%s: Unable to get UGA information", argv[0]);
833ca987d46SWarner Losh 		return (CMD_ERROR);
834ca987d46SWarner Losh 	}
835ca987d46SWarner Losh 
836ca987d46SWarner Losh 	print_efifb(-1, &efifb, 1);
837ca987d46SWarner Losh 	printf("\n");
838ca987d46SWarner Losh 	return (CMD_OK);
839ca987d46SWarner Losh 
840ca987d46SWarner Losh  usage:
841ca987d46SWarner Losh 	snprintf(command_errbuf, sizeof(command_errbuf), "usage: %s", argv[0]);
842ca987d46SWarner Losh 	return (CMD_ERROR);
843ca987d46SWarner Losh }
844