xref: /illumos-gate/usr/src/boot/efi/loader/framebuffer.c (revision 311330e6823a3a919ff127757c2f0cf9eb17aa0e)
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 
31 #include <stand.h>
32 #include <bootstrap.h>
33 #include <sys/endian.h>
34 #include <sys/param.h>
35 #include <sys/font.h>
36 #include <sys/consplat.h>
37 #include <sys/limits.h>
38 
39 #include <efi.h>
40 #include <efilib.h>
41 #include <efiuga.h>
42 #include <efipciio.h>
43 #include <Protocol/EdidActive.h>
44 #include <Protocol/EdidDiscovered.h>
45 #include <machine/metadata.h>
46 
47 #include "gfx_fb.h"
48 #include "framebuffer.h"
49 
50 EFI_GUID conout_guid = EFI_CONSOLE_OUT_DEVICE_GUID;
51 EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
52 static EFI_GUID pciio_guid = EFI_PCI_IO_PROTOCOL_GUID;
53 EFI_GUID uga_guid = EFI_UGA_DRAW_PROTOCOL_GUID;
54 static EFI_GUID active_edid_guid = EFI_EDID_ACTIVE_PROTOCOL_GUID;
55 static EFI_GUID discovered_edid_guid = EFI_EDID_DISCOVERED_PROTOCOL_GUID;
56 static EFI_HANDLE gop_handle;
57 
58 /* Saved initial GOP mode. */
59 static uint32_t default_mode = UINT32_MAX;
60 /* Cached EDID. */
61 struct vesa_edid_info *edid_info = NULL;
62 
63 static uint32_t gop_default_mode(void);
64 static int efifb_set_mode(EFI_GRAPHICS_OUTPUT *, uint_t);
65 
66 static uint_t
67 efifb_color_depth(struct efi_fb *efifb)
68 {
69 	uint32_t mask;
70 	uint_t depth;
71 
72 	mask = efifb->fb_mask_red | efifb->fb_mask_green |
73 	    efifb->fb_mask_blue | efifb->fb_mask_reserved;
74 	if (mask == 0)
75 		return (0);
76 	for (depth = 1; mask != 1; depth++)
77 		mask >>= 1;
78 	return (depth);
79 }
80 
81 static int
82 efifb_mask_from_pixfmt(struct efi_fb *efifb, EFI_GRAPHICS_PIXEL_FORMAT pixfmt,
83     EFI_PIXEL_BITMASK *pixinfo)
84 {
85 	int result;
86 
87 	result = 0;
88 	switch (pixfmt) {
89 	case PixelRedGreenBlueReserved8BitPerColor:
90 	case PixelBltOnly:
91 		efifb->fb_mask_red = 0x000000ff;
92 		efifb->fb_mask_green = 0x0000ff00;
93 		efifb->fb_mask_blue = 0x00ff0000;
94 		efifb->fb_mask_reserved = 0xff000000;
95 		break;
96 	case PixelBlueGreenRedReserved8BitPerColor:
97 		efifb->fb_mask_red = 0x00ff0000;
98 		efifb->fb_mask_green = 0x0000ff00;
99 		efifb->fb_mask_blue = 0x000000ff;
100 		efifb->fb_mask_reserved = 0xff000000;
101 		break;
102 	case PixelBitMask:
103 		efifb->fb_mask_red = pixinfo->RedMask;
104 		efifb->fb_mask_green = pixinfo->GreenMask;
105 		efifb->fb_mask_blue = pixinfo->BlueMask;
106 		efifb->fb_mask_reserved = pixinfo->ReservedMask;
107 		break;
108 	default:
109 		result = 1;
110 		break;
111 	}
112 	return (result);
113 }
114 
115 static int
116 efifb_from_gop(struct efi_fb *efifb, EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode,
117     EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info)
118 {
119 	int result;
120 
121 	efifb->fb_addr = mode->FrameBufferBase;
122 	efifb->fb_size = mode->FrameBufferSize;
123 	efifb->fb_height = info->VerticalResolution;
124 	efifb->fb_width = info->HorizontalResolution;
125 	efifb->fb_stride = info->PixelsPerScanLine;
126 	result = efifb_mask_from_pixfmt(efifb, info->PixelFormat,
127 	    &info->PixelInformation);
128 	if (efifb->fb_addr == 0)
129 		result = 1;
130 	return (result);
131 }
132 
133 static ssize_t
134 efifb_uga_find_pixel(EFI_UGA_DRAW_PROTOCOL *uga, uint_t line,
135     EFI_PCI_IO_PROTOCOL *pciio, uint64_t addr, uint64_t size)
136 {
137 	EFI_UGA_PIXEL pix0, pix1;
138 	uint8_t *data1, *data2;
139 	size_t count, maxcount = 1024;
140 	ssize_t ofs;
141 	EFI_STATUS status;
142 	uint_t idx;
143 
144 	status = uga->Blt(uga, &pix0, EfiUgaVideoToBltBuffer,
145 	    0, line, 0, 0, 1, 1, 0);
146 	if (EFI_ERROR(status)) {
147 		printf("UGA BLT operation failed (video->buffer)");
148 		return (-1);
149 	}
150 	pix1.Red = ~pix0.Red;
151 	pix1.Green = ~pix0.Green;
152 	pix1.Blue = ~pix0.Blue;
153 	pix1.Reserved = 0;
154 
155 	data1 = calloc(maxcount, 2);
156 	if (data1 == NULL) {
157 		printf("Unable to allocate memory");
158 		return (-1);
159 	}
160 	data2 = data1 + maxcount;
161 
162 	ofs = 0;
163 	while (size > 0) {
164 		count = min(size, maxcount);
165 
166 		status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
167 		    EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
168 		    data1);
169 		if (EFI_ERROR(status)) {
170 			printf("Error reading frame buffer (before)");
171 			goto fail;
172 		}
173 		status = uga->Blt(uga, &pix1, EfiUgaBltBufferToVideo,
174 		    0, 0, 0, line, 1, 1, 0);
175 		if (EFI_ERROR(status)) {
176 			printf("UGA BLT operation failed (modify)");
177 			goto fail;
178 		}
179 		status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
180 		    EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
181 		    data2);
182 		if (EFI_ERROR(status)) {
183 			printf("Error reading frame buffer (after)");
184 			goto fail;
185 		}
186 		status = uga->Blt(uga, &pix0, EfiUgaBltBufferToVideo,
187 		    0, 0, 0, line, 1, 1, 0);
188 		if (EFI_ERROR(status)) {
189 			printf("UGA BLT operation failed (restore)");
190 			goto fail;
191 		}
192 		for (idx = 0; idx < count; idx++) {
193 			if (data1[idx] != data2[idx]) {
194 				free(data1);
195 				return (ofs + (idx & ~3));
196 			}
197 		}
198 		ofs += count;
199 		size -= count;
200 	}
201 	printf("No change detected in frame buffer");
202 
203 fail:
204 	printf(" -- error %lu\n", EFI_ERROR_CODE(status));
205 	free(data1);
206 	return (-1);
207 }
208 
209 static EFI_PCI_IO_PROTOCOL *
210 efifb_uga_get_pciio(void)
211 {
212 	EFI_PCI_IO_PROTOCOL *pciio;
213 	EFI_HANDLE *buf, *hp;
214 	EFI_STATUS status;
215 	UINTN bufsz;
216 
217 	/* Get all handles that support the UGA protocol. */
218 	bufsz = 0;
219 	status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, NULL);
220 	if (status != EFI_BUFFER_TOO_SMALL)
221 		return (NULL);
222 	buf = malloc(bufsz);
223 	status = BS->LocateHandle(ByProtocol, &uga_guid, NULL, &bufsz, buf);
224 	if (status != EFI_SUCCESS) {
225 		free(buf);
226 		return (NULL);
227 	}
228 	bufsz /= sizeof (EFI_HANDLE);
229 
230 	/* Get the PCI I/O interface of the first handle that supports it. */
231 	pciio = NULL;
232 	for (hp = buf; hp < buf + bufsz; hp++) {
233 		status = OpenProtocolByHandle(*hp, &pciio_guid,
234 		    (void **)&pciio);
235 		if (status == EFI_SUCCESS) {
236 			free(buf);
237 			return (pciio);
238 		}
239 	}
240 	free(buf);
241 	return (NULL);
242 }
243 
244 static EFI_STATUS
245 efifb_uga_locate_framebuffer(EFI_PCI_IO_PROTOCOL *pciio, uint64_t *addrp,
246     uint64_t *sizep)
247 {
248 	uint8_t *resattr;
249 	uint64_t addr, size;
250 	EFI_STATUS status;
251 	uint_t bar;
252 
253 	if (pciio == NULL)
254 		return (EFI_DEVICE_ERROR);
255 
256 	/* Attempt to get the frame buffer address (imprecise). */
257 	*addrp = 0;
258 	*sizep = 0;
259 	for (bar = 0; bar < 6; bar++) {
260 		status = pciio->GetBarAttributes(pciio, bar, NULL,
261 		    (void **)&resattr);
262 		if (status != EFI_SUCCESS)
263 			continue;
264 		/* XXX magic offsets and constants. */
265 		if (resattr[0] == 0x87 && resattr[3] == 0) {
266 			/* 32-bit address space descriptor (MEMIO) */
267 			addr = le32dec(resattr + 10);
268 			size = le32dec(resattr + 22);
269 		} else if (resattr[0] == 0x8a && resattr[3] == 0) {
270 			/* 64-bit address space descriptor (MEMIO) */
271 			addr = le64dec(resattr + 14);
272 			size = le64dec(resattr + 38);
273 		} else {
274 			addr = 0;
275 			size = 0;
276 		}
277 		BS->FreePool(resattr);
278 		if (addr == 0 || size == 0)
279 			continue;
280 
281 		/* We assume the largest BAR is the frame buffer. */
282 		if (size > *sizep) {
283 			*addrp = addr;
284 			*sizep = size;
285 		}
286 	}
287 	return ((*addrp == 0 || *sizep == 0) ? EFI_DEVICE_ERROR : 0);
288 }
289 
290 static int
291 efifb_from_uga(struct efi_fb *efifb, EFI_UGA_DRAW_PROTOCOL *uga)
292 {
293 	EFI_PCI_IO_PROTOCOL *pciio;
294 	char *ev, *p;
295 	EFI_STATUS status;
296 	ssize_t offset;
297 	uint64_t fbaddr;
298 	uint32_t horiz, vert, stride;
299 	uint32_t np, depth, refresh;
300 
301 	status = uga->GetMode(uga, &horiz, &vert, &depth, &refresh);
302 	if (EFI_ERROR(status))
303 		return (1);
304 	efifb->fb_height = vert;
305 	efifb->fb_width = horiz;
306 	/* Paranoia... */
307 	if (efifb->fb_height == 0 || efifb->fb_width == 0)
308 		return (1);
309 
310 	/* The color masks are fixed AFAICT. */
311 	efifb_mask_from_pixfmt(efifb, PixelBlueGreenRedReserved8BitPerColor,
312 	    NULL);
313 
314 	/* pciio can be NULL on return! */
315 	pciio = efifb_uga_get_pciio();
316 
317 	/* Try to find the frame buffer. */
318 	status = efifb_uga_locate_framebuffer(pciio, &efifb->fb_addr,
319 	    &efifb->fb_size);
320 	if (EFI_ERROR(status)) {
321 		efifb->fb_addr = 0;
322 		efifb->fb_size = 0;
323 	}
324 
325 	/*
326 	 * There's no reliable way to detect the frame buffer or the
327 	 * offset within the frame buffer of the visible region, nor
328 	 * the stride. Our only option is to look at the system and
329 	 * fill in the blanks based on that. Luckily, UGA was mostly
330 	 * only used on Apple hardware.
331 	 */
332 	offset = -1;
333 	ev = getenv("smbios.system.maker");
334 	if (ev != NULL && strcmp(ev, "Apple Inc.") == 0) {
335 		ev = getenv("smbios.system.product");
336 		if (ev != NULL && strcmp(ev, "iMac7,1") == 0) {
337 			/* These are the expected values we should have. */
338 			horiz = 1680;
339 			vert = 1050;
340 			fbaddr = 0xc0000000;
341 			/* These are the missing bits. */
342 			offset = 0x10000;
343 			stride = 1728;
344 		} else if (ev != NULL && strcmp(ev, "MacBook3,1") == 0) {
345 			/* These are the expected values we should have. */
346 			horiz = 1280;
347 			vert = 800;
348 			fbaddr = 0xc0000000;
349 			/* These are the missing bits. */
350 			offset = 0x0;
351 			stride = 2048;
352 		}
353 	}
354 
355 	/*
356 	 * If this is hardware we know, make sure that it looks familiar
357 	 * before we accept our hardcoded values.
358 	 */
359 	if (offset >= 0 && efifb->fb_width == horiz &&
360 	    efifb->fb_height == vert && efifb->fb_addr == fbaddr) {
361 		efifb->fb_addr += offset;
362 		efifb->fb_size -= offset;
363 		efifb->fb_stride = stride;
364 		return (0);
365 	} else if (offset >= 0) {
366 		printf("Hardware make/model known, but graphics not "
367 		    "as expected.\n");
368 		printf("Console may not work!\n");
369 	}
370 
371 	/*
372 	 * The stride is equal or larger to the width. Often it's the
373 	 * next larger power of two. We'll start with that...
374 	 */
375 	efifb->fb_stride = efifb->fb_width;
376 	do {
377 		np = efifb->fb_stride & (efifb->fb_stride - 1);
378 		if (np) {
379 			efifb->fb_stride |= (np - 1);
380 			efifb->fb_stride++;
381 		}
382 	} while (np);
383 
384 	ev = getenv("hw.efifb.address");
385 	if (ev == NULL) {
386 		if (efifb->fb_addr == 0) {
387 			printf("Please set hw.efifb.address and "
388 			    "hw.efifb.stride.\n");
389 			return (1);
390 		}
391 
392 		/*
393 		 * The visible part of the frame buffer may not start at
394 		 * offset 0, so try to detect it. Note that we may not
395 		 * always be able to read from the frame buffer, which
396 		 * means that we may not be able to detect anything. In
397 		 * that case, we would take a long time scanning for a
398 		 * pixel change in the frame buffer, which would have it
399 		 * appear that we're hanging, so we limit the scan to
400 		 * 1/256th of the frame buffer. This number is mostly
401 		 * based on PR 202730 and the fact that on a MacBoook,
402 		 * where we can't read from the frame buffer the offset
403 		 * of the visible region is 0. In short: we want to scan
404 		 * enough to handle all adapters that have an offset
405 		 * larger than 0 and we want to scan as little as we can
406 		 * to not appear to hang when we can't read from the
407 		 * frame buffer.
408 		 */
409 		offset = efifb_uga_find_pixel(uga, 0, pciio, efifb->fb_addr,
410 		    efifb->fb_size >> 8);
411 		if (offset == -1) {
412 			printf("Unable to reliably detect frame buffer.\n");
413 		} else if (offset > 0) {
414 			efifb->fb_addr += offset;
415 			efifb->fb_size -= offset;
416 		}
417 	} else {
418 		offset = 0;
419 		efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
420 		efifb->fb_addr = strtoul(ev, &p, 0);
421 		if (*p != '\0')
422 			return (1);
423 	}
424 
425 	ev = getenv("hw.efifb.stride");
426 	if (ev == NULL) {
427 		if (pciio != NULL && offset != -1) {
428 			/* Determine the stride. */
429 			offset = efifb_uga_find_pixel(uga, 1, pciio,
430 			    efifb->fb_addr, horiz * 8);
431 			if (offset != -1)
432 				efifb->fb_stride = offset >> 2;
433 		} else {
434 			printf("Unable to reliably detect the stride.\n");
435 		}
436 	} else {
437 		efifb->fb_stride = strtoul(ev, &p, 0);
438 		if (*p != '\0')
439 			return (1);
440 	}
441 
442 	/*
443 	 * We finalized on the stride, so recalculate the size of the
444 	 * frame buffer.
445 	 */
446 	efifb->fb_size = efifb->fb_height * efifb->fb_stride * 4;
447 	if (efifb->fb_addr == 0)
448 		return (1);
449 	return (0);
450 }
451 
452 /*
453  * Fetch EDID info. Caller must free the buffer.
454  */
455 static struct vesa_edid_info *
456 efifb_gop_get_edid(EFI_HANDLE h)
457 {
458 	const uint8_t magic[] = EDID_MAGIC;
459 	EFI_EDID_ACTIVE_PROTOCOL *edid;
460 	struct vesa_edid_info *edid_infop;
461 	EFI_GUID *guid;
462 	EFI_STATUS status;
463 	size_t size;
464 
465 	guid = &active_edid_guid;
466 	status = BS->OpenProtocol(h, guid, (void **)&edid, IH, NULL,
467 	    EFI_OPEN_PROTOCOL_GET_PROTOCOL);
468 	if (status != EFI_SUCCESS ||
469 	    edid->SizeOfEdid == 0) {
470 		guid = &discovered_edid_guid;
471 		status = BS->OpenProtocol(h, guid, (void **)&edid, IH, NULL,
472 		    EFI_OPEN_PROTOCOL_GET_PROTOCOL);
473 		if (status != EFI_SUCCESS ||
474 		    edid->SizeOfEdid == 0)
475 			return (NULL);
476 	}
477 
478 	size = MAX(sizeof (*edid_infop), edid->SizeOfEdid);
479 
480 	edid_infop = calloc(1, size);
481 	if (edid_infop == NULL)
482 		return (NULL);
483 
484 	memcpy(edid_infop, edid->Edid, edid->SizeOfEdid);
485 
486 	/* Validate EDID */
487 	if (memcmp(edid_infop, magic, sizeof (magic)) != 0)
488 		goto error;
489 
490 	if (edid_infop->header.version != 1)
491 		goto error;
492 
493 	return (edid_infop);
494 error:
495 	free(edid_infop);
496 	return (NULL);
497 }
498 
499 static bool
500 efifb_get_edid(edid_res_list_t *res)
501 {
502 	bool rv = false;
503 
504 	if (edid_info == NULL)
505 		edid_info = efifb_gop_get_edid(gop_handle);
506 
507 	if (edid_info != NULL)
508 		rv = gfx_get_edid_resolution(edid_info, res);
509 
510 	return (rv);
511 }
512 
513 int
514 efi_find_framebuffer(struct efi_fb *efifb)
515 {
516 	EFI_HANDLE *hlist;
517 	UINTN nhandles, i, hsize;
518 	extern EFI_GRAPHICS_OUTPUT *gop;
519 	extern EFI_UGA_DRAW_PROTOCOL *uga;
520 	EFI_STATUS status;
521 	uint32_t mode;
522 
523 	if (gop != NULL)
524 		return (efifb_from_gop(efifb, gop->Mode, gop->Mode->Info));
525 
526 	hsize = 0;
527 	hlist = NULL;
528 	status = BS->LocateHandle(ByProtocol, &gop_guid, NULL, &hsize, hlist);
529 	if (status == EFI_BUFFER_TOO_SMALL) {
530 		hlist = malloc(hsize);
531 		if (hlist == NULL)
532 			return (ENOMEM);
533 		status = BS->LocateHandle(ByProtocol, &gop_guid, NULL, &hsize,
534 		    hlist);
535 		if (EFI_ERROR(status))
536 			free(hlist);
537 	}
538 	if (EFI_ERROR(status))
539 		return (efi_status_to_errno(status));
540 
541 	nhandles = hsize / sizeof (*hlist);
542 
543 	/*
544 	 * Search for ConOut protocol, if not found, use first handle.
545 	 */
546 	gop_handle = *hlist;
547 	for (i = 0; i < nhandles; i++) {
548 		void *dummy = NULL;
549 
550 		status = OpenProtocolByHandle(hlist[i], &conout_guid, &dummy);
551 		if (status == EFI_SUCCESS) {
552 			gop_handle = hlist[i];
553 			break;
554 		}
555 	}
556 
557 	status = OpenProtocolByHandle(gop_handle, &gop_guid, (void **)&gop);
558 	free(hlist);
559 
560 	if (status == EFI_SUCCESS) {
561 		/* Save default mode. */
562 		if (default_mode == UINT32_MAX) {
563 			default_mode = gop->Mode->Mode;
564 		}
565 		mode = gop_default_mode();
566 		if (mode != gop->Mode->Mode)
567 			efifb_set_mode(gop, mode);
568 		return (efifb_from_gop(efifb, gop->Mode, gop->Mode->Info));
569 	}
570 
571 	if (uga != NULL)
572 		return (efifb_from_uga(efifb, uga));
573 
574 	status = BS->LocateProtocol(&uga_guid, NULL, (void **)&uga);
575 	if (status == EFI_SUCCESS)
576 		return (efifb_from_uga(efifb, uga));
577 
578 	return (1);
579 }
580 
581 static void
582 print_efifb(int mode, struct efi_fb *efifb, int verbose)
583 {
584 	uint_t depth;
585 	edid_res_list_t res;
586 	struct resolution *rp;
587 
588 	TAILQ_INIT(&res);
589 	if (verbose == 1) {
590 		printf("Framebuffer mode: %s\n",
591 		    plat_stdout_is_framebuffer() ? "on" : "off");
592 		if (efifb_get_edid(&res)) {
593 			printf("EDID");
594 			while ((rp = TAILQ_FIRST(&res)) != NULL) {
595 				printf(" %dx%d", rp->width, rp->height);
596 				TAILQ_REMOVE(&res, rp, next);
597 				free(rp);
598 			}
599 			printf("\n");
600 		}
601 	}
602 
603 	if (mode >= 0) {
604 		if (verbose == 1)
605 			printf("GOP ");
606 		printf("mode %d: ", mode);
607 	}
608 	depth = efifb_color_depth(efifb);
609 	printf("%ux%ux%u", efifb->fb_width, efifb->fb_height, depth);
610 	if (verbose)
611 		printf(", stride=%u", efifb->fb_stride);
612 	if (verbose) {
613 		printf("\n    frame buffer: address=%jx, size=%jx",
614 		    (uintmax_t)efifb->fb_addr, (uintmax_t)efifb->fb_size);
615 		printf("\n    color mask: R=%08x, G=%08x, B=%08x\n",
616 		    efifb->fb_mask_red, efifb->fb_mask_green,
617 		    efifb->fb_mask_blue);
618 		if (efifb->fb_addr == 0) {
619 			printf("Warning: this mode is not implementing the "
620 			    "linear framebuffer. The illumos\n\tconsole is "
621 			    "not available with this mode and will default to "
622 			    "ttya\n");
623 		}
624 	}
625 }
626 
627 static int
628 efifb_set_mode(EFI_GRAPHICS_OUTPUT *gop, uint_t mode)
629 {
630 	EFI_STATUS status;
631 
632 	status = gop->SetMode(gop, mode);
633 	if (EFI_ERROR(status)) {
634 		snprintf(command_errbuf, sizeof (command_errbuf),
635 		    "Unable to set mode to %u (error=%lu)",
636 		    mode, EFI_ERROR_CODE(status));
637 		return (CMD_ERROR);
638 	}
639 	return (CMD_OK);
640 }
641 
642 /*
643  * Verify existance of mode number or find mode by
644  * dimensions. If depth is not given, walk values 32, 24, 16, 8.
645  * Return MaxMode if mode is not found.
646  */
647 static int
648 efifb_find_mode_xydm(UINT32 x, UINT32 y, int depth, int m)
649 {
650 	extern EFI_GRAPHICS_OUTPUT *gop;
651 	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
652 	EFI_STATUS status;
653 	UINTN infosz;
654 	struct efi_fb fb;
655 	UINT32 mode;
656 	uint_t d, i;
657 
658 	if (m != -1)
659 		i = 8;
660 	else if (depth == -1)
661 		i = 32;
662 	else
663 		i = depth;
664 
665 	while (i > 0) {
666 		for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
667 			status = gop->QueryMode(gop, mode, &infosz, &info);
668 			if (EFI_ERROR(status))
669 				continue;
670 
671 			if (m != -1) {
672 				if ((UINT32)m == mode)
673 					return (mode);
674 				else
675 					continue;
676 			}
677 
678 			efifb_from_gop(&fb, gop->Mode, info);
679 			d = efifb_color_depth(&fb);
680 			if (x == fb.fb_width && y == fb.fb_height && d == i)
681 				return (mode);
682 		}
683 
684 		if (depth != -1)
685 			break;
686 
687 		i -= 8;
688 	}
689 
690 	return (gop->Mode->MaxMode);
691 }
692 
693 static int
694 efifb_find_mode(char *str)
695 {
696 	extern EFI_GRAPHICS_OUTPUT *gop;
697 	int x, y, depth;
698 
699 	if (!gfx_parse_mode_str(str, &x, &y, &depth))
700 		return (gop->Mode->MaxMode);
701 
702 	return (efifb_find_mode_xydm(x, y, depth, -1));
703 }
704 
705 /*
706  * gop_default_mode(). Try to set mode based on EDID.
707  */
708 static uint32_t
709 gop_default_mode(void)
710 {
711 	edid_res_list_t res;
712 	struct resolution *rp;
713 	extern EFI_GRAPHICS_OUTPUT *gop;
714 	UINT32 mode;
715 
716 	mode = gop->Mode->MaxMode;
717 	TAILQ_INIT(&res);
718 	if (efifb_get_edid(&res)) {
719 		while ((rp = TAILQ_FIRST(&res)) != NULL) {
720 			if (mode == gop->Mode->MaxMode) {
721 				mode = efifb_find_mode_xydm(
722 				    rp->width, rp->height, -1, -1);
723 			}
724 			TAILQ_REMOVE(&res, rp, next);
725 			free(rp);
726 		}
727 	}
728 
729 	if (mode == gop->Mode->MaxMode)
730 		mode = default_mode;
731 
732 	return (mode);
733 }
734 
735 COMMAND_SET(framebuffer, "framebuffer", "framebuffer mode management",
736     command_gop);
737 
738 static int
739 command_gop(int argc, char *argv[])
740 {
741 	extern struct efi_fb efifb;
742 	extern EFI_GRAPHICS_OUTPUT *gop;
743 	struct efi_fb fb;
744 	EFI_STATUS status;
745 	char *arg, *cp;
746 	uint_t mode;
747 
748 	if (gop == NULL) {
749 		snprintf(command_errbuf, sizeof (command_errbuf),
750 		    "%s: Graphics Output Protocol not present", argv[0]);
751 		return (CMD_ERROR);
752 	}
753 
754 	if (argc < 2)
755 		goto usage;
756 
757 	/*
758 	 * Note we can not turn the GOP itself off, but instead we instruct
759 	 * tem to use text mode.
760 	 */
761 	if (strcmp(argv[1], "off") == 0) {
762 		if (argc != 2)
763 			goto usage;
764 
765 		reset_font_flags();
766 		plat_cons_update_mode(EfiConsoleControlScreenText);
767 		return (CMD_OK);
768 	}
769 
770 	/*
771 	 * Set GOP to use default mode, then notify tem.
772 	 */
773 	if (strcmp(argv[1], "on") == 0) {
774 		if (argc != 2)
775 			goto usage;
776 
777 		reset_font_flags();
778 		mode = gop_default_mode();
779 		if (mode != gop->Mode->Mode)
780 			efifb_set_mode(gop, mode);
781 
782 		plat_cons_update_mode(EfiConsoleControlScreenGraphics);
783 		return (CMD_OK);
784 	}
785 
786 	if (strcmp(argv[1], "set") == 0) {
787 		int rv;
788 
789 		if (argc != 3)
790 			goto usage;
791 
792 		arg = argv[2];
793 		if (strchr(arg, 'x') == NULL) {
794 			errno = 0;
795 			mode = strtoul(arg, &cp, 0);
796 			if (errno != 0 || *arg == '\0' || cp[0] != '\0') {
797 				snprintf(command_errbuf,
798 				    sizeof (command_errbuf),
799 				    "mode should be an integer");
800 				return (CMD_ERROR);
801 			}
802 			mode = efifb_find_mode_xydm(0, 0, 0, mode);
803 		} else {
804 			mode = efifb_find_mode(arg);
805 		}
806 
807 		if (mode == gop->Mode->MaxMode)
808 			mode = gop->Mode->Mode;
809 
810 		reset_font_flags();
811 		rv = efifb_set_mode(gop, mode);
812 		plat_cons_update_mode(EfiConsoleControlScreenGraphics);
813 		return (rv);
814 	}
815 
816 	if (strcmp(argv[1], "get") == 0) {
817 		if (argc != 2)
818 			goto usage;
819 
820 		print_efifb(gop->Mode->Mode, &efifb, 1);
821 		printf("\n");
822 		return (CMD_OK);
823 	}
824 
825 	if (strcmp(argv[1], "list") == 0) {
826 		EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
827 		UINTN infosz;
828 		int depth, d = -1;
829 
830 		if (argc != 2 && argc != 3)
831 			goto usage;
832 
833 		if (argc == 3) {
834 			arg = argv[2];
835 			errno = 0;
836 			d = strtoul(arg, &cp, 0);
837 			if (errno != 0 || *arg == '\0' || cp[0] != '\0') {
838 				snprintf(command_errbuf,
839 				    sizeof (command_errbuf),
840 				    "depth should be an integer");
841 				return (CMD_ERROR);
842 			}
843 		}
844 		pager_open();
845 		for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
846 			status = gop->QueryMode(gop, mode, &infosz, &info);
847 			if (EFI_ERROR(status))
848 				continue;
849 			efifb_from_gop(&fb, gop->Mode, info);
850 			depth = efifb_color_depth(&fb);
851 			if (d != -1 && d != depth)
852 				continue;
853 			print_efifb(mode, &fb, 0);
854 			if (pager_output("\n"))
855 				break;
856 		}
857 		pager_close();
858 		return (CMD_OK);
859 	}
860 
861 usage:
862 	snprintf(command_errbuf, sizeof (command_errbuf),
863 	    "usage: %s on | off | get | list [depth] | "
864 	    "set <display or GOP mode number>", argv[0]);
865 	return (CMD_ERROR);
866 }
867 
868 COMMAND_SET(uga, "uga", "universal graphics adapter", command_uga);
869 
870 static int
871 command_uga(int argc, char *argv[])
872 {
873 	extern struct efi_fb efifb;
874 	extern EFI_UGA_DRAW_PROTOCOL *uga;
875 
876 	if (uga == NULL) {
877 		snprintf(command_errbuf, sizeof (command_errbuf),
878 		    "%s: UGA Protocol not present", argv[0]);
879 		return (CMD_ERROR);
880 	}
881 
882 	if (argc != 1)
883 		goto usage;
884 
885 	if (efifb.fb_addr == 0) {
886 		snprintf(command_errbuf, sizeof (command_errbuf),
887 		    "%s: Unable to get UGA information", argv[0]);
888 		return (CMD_ERROR);
889 	}
890 
891 	print_efifb(-1, &efifb, 1);
892 	printf("\n");
893 	return (CMD_OK);
894 
895 usage:
896 	snprintf(command_errbuf, sizeof (command_errbuf), "usage: %s", argv[0]);
897 	return (CMD_ERROR);
898 }
899