xref: /illumos-gate/usr/src/boot/efi/loader/framebuffer.c (revision e433f0306e6b08f85cf88ca7be8095a24f5ad402)
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 <eficonsctl.h>
42 #include <Guid/ConsoleOutDevice.h>
43 #include <Protocol/GraphicsOutput.h>
44 #include <Protocol/UgaDraw.h>
45 #include <Protocol/UgaIo.h>
46 #include <Protocol/EdidActive.h>
47 #include <Protocol/EdidDiscovered.h>
48 #include <Protocol/PciIo.h>
49 #include <machine/metadata.h>
50 
51 #include "gfx_fb.h"
52 #include "framebuffer.h"
53 
54 EFI_GUID gEfiGraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
55 EFI_GUID gEfiPciIoProtocolGuid = EFI_PCI_IO_PROTOCOL_GUID;
56 EFI_GUID gEfiUgaDrawProtocolGuid = EFI_UGA_DRAW_PROTOCOL_GUID;
57 EFI_GUID gEfiUgaIoProtocolGuid = EFI_UGA_IO_PROTOCOL_GUID;
58 EFI_GUID gEfiEdidActiveProtocolGuid = EFI_EDID_ACTIVE_PROTOCOL_GUID;
59 EFI_GUID gEfiEdidDiscoveredProtocolGuid = EFI_EDID_DISCOVERED_PROTOCOL_GUID;
60 
61 bool has_framebuffer = false;
62 static EFI_HANDLE gop_handle;
63 
64 /* Saved initial GOP mode. */
65 static uint32_t default_mode = UINT32_MAX;
66 /* Cached EDID. */
67 struct vesa_edid_info *edid_info = NULL;
68 
69 static uint32_t gop_default_mode(void);
70 static int efifb_set_mode(EFI_GRAPHICS_OUTPUT_PROTOCOL *, uint_t);
71 
72 static uint_t
73 efifb_color_depth(struct efi_fb *efifb)
74 {
75 	uint32_t mask;
76 	uint_t depth;
77 
78 	mask = efifb->fb_mask_red | efifb->fb_mask_green |
79 	    efifb->fb_mask_blue | efifb->fb_mask_reserved;
80 	if (mask == 0)
81 		return (0);
82 	for (depth = 1; mask != 1; depth++)
83 		mask >>= 1;
84 	return (depth);
85 }
86 
87 static int
88 efifb_mask_from_pixfmt(struct efi_fb *efifb, EFI_GRAPHICS_PIXEL_FORMAT pixfmt,
89     EFI_PIXEL_BITMASK *pixinfo)
90 {
91 	int result;
92 
93 	result = 0;
94 	switch (pixfmt) {
95 	case PixelRedGreenBlueReserved8BitPerColor:
96 	case PixelBltOnly:
97 		efifb->fb_mask_red = 0x000000ff;
98 		efifb->fb_mask_green = 0x0000ff00;
99 		efifb->fb_mask_blue = 0x00ff0000;
100 		efifb->fb_mask_reserved = 0xff000000;
101 		break;
102 	case PixelBlueGreenRedReserved8BitPerColor:
103 		efifb->fb_mask_red = 0x00ff0000;
104 		efifb->fb_mask_green = 0x0000ff00;
105 		efifb->fb_mask_blue = 0x000000ff;
106 		efifb->fb_mask_reserved = 0xff000000;
107 		break;
108 	case PixelBitMask:
109 		efifb->fb_mask_red = pixinfo->RedMask;
110 		efifb->fb_mask_green = pixinfo->GreenMask;
111 		efifb->fb_mask_blue = pixinfo->BlueMask;
112 		efifb->fb_mask_reserved = pixinfo->ReservedMask;
113 		break;
114 	default:
115 		result = 1;
116 		break;
117 	}
118 	return (result);
119 }
120 
121 static int
122 efifb_from_gop(struct efi_fb *efifb, EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode,
123     EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info)
124 {
125 	int result;
126 
127 	efifb->fb_addr = mode->FrameBufferBase;
128 	efifb->fb_size = mode->FrameBufferSize;
129 	efifb->fb_height = info->VerticalResolution;
130 	efifb->fb_width = info->HorizontalResolution;
131 	efifb->fb_stride = info->PixelsPerScanLine;
132 	result = efifb_mask_from_pixfmt(efifb, info->PixelFormat,
133 	    &info->PixelInformation);
134 	if (efifb->fb_addr == 0)
135 		result = 1;
136 	return (result);
137 }
138 
139 static ssize_t
140 efifb_uga_find_pixel(EFI_UGA_DRAW_PROTOCOL *uga, uint_t line,
141     EFI_PCI_IO_PROTOCOL *pciio, uint64_t addr, uint64_t size)
142 {
143 	EFI_UGA_PIXEL pix0, pix1;
144 	uint8_t *data1, *data2;
145 	size_t count, maxcount = 1024;
146 	ssize_t ofs;
147 	EFI_STATUS status;
148 	uint_t idx;
149 
150 	status = uga->Blt(uga, &pix0, EfiUgaVideoToBltBuffer,
151 	    0, line, 0, 0, 1, 1, 0);
152 	if (EFI_ERROR(status)) {
153 		printf("UGA BLT operation failed (video->buffer)");
154 		return (-1);
155 	}
156 	pix1.Red = ~pix0.Red;
157 	pix1.Green = ~pix0.Green;
158 	pix1.Blue = ~pix0.Blue;
159 	pix1.Reserved = 0;
160 
161 	data1 = calloc(maxcount, 2);
162 	if (data1 == NULL) {
163 		printf("Unable to allocate memory");
164 		return (-1);
165 	}
166 	data2 = data1 + maxcount;
167 
168 	ofs = 0;
169 	while (size > 0) {
170 		count = min(size, maxcount);
171 
172 		status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
173 		    EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
174 		    data1);
175 		if (EFI_ERROR(status)) {
176 			printf("Error reading frame buffer (before)");
177 			goto fail;
178 		}
179 		status = uga->Blt(uga, &pix1, EfiUgaBltBufferToVideo,
180 		    0, 0, 0, line, 1, 1, 0);
181 		if (EFI_ERROR(status)) {
182 			printf("UGA BLT operation failed (modify)");
183 			goto fail;
184 		}
185 		status = pciio->Mem.Read(pciio, EfiPciIoWidthUint32,
186 		    EFI_PCI_IO_PASS_THROUGH_BAR, addr + ofs, count >> 2,
187 		    data2);
188 		if (EFI_ERROR(status)) {
189 			printf("Error reading frame buffer (after)");
190 			goto fail;
191 		}
192 		status = uga->Blt(uga, &pix0, EfiUgaBltBufferToVideo,
193 		    0, 0, 0, line, 1, 1, 0);
194 		if (EFI_ERROR(status)) {
195 			printf("UGA BLT operation failed (restore)");
196 			goto fail;
197 		}
198 		for (idx = 0; idx < count; idx++) {
199 			if (data1[idx] != data2[idx]) {
200 				free(data1);
201 				return (ofs + (idx & ~3));
202 			}
203 		}
204 		ofs += count;
205 		size -= count;
206 	}
207 	printf("No change detected in frame buffer");
208 
209 fail:
210 	printf(" -- error %lu\n", DECODE_ERROR(status));
211 	free(data1);
212 	return (-1);
213 }
214 
215 static EFI_PCI_IO_PROTOCOL *
216 efifb_uga_get_pciio(void)
217 {
218 	EFI_PCI_IO_PROTOCOL *pciio;
219 	EFI_HANDLE *handles;
220 	EFI_STATUS status;
221 	uint_t i, nhandles;
222 
223 	/* Get all handles that support the UGA protocol. */
224 	status = efi_get_protocol_handles(&gEfiUgaDrawProtocolGuid,
225 	    &nhandles, &handles);
226 	if (status != EFI_SUCCESS) {
227 		return (NULL);
228 	}
229 
230 	/* Get the PCI I/O interface of the first handle that supports it. */
231 	pciio = NULL;
232 	for (i = 0; i < nhandles; i++) {
233 		status = OpenProtocolByHandle(handles[i],
234 		    &gEfiPciIoProtocolGuid, (void **)&pciio);
235 		if (status == EFI_SUCCESS) {
236 			free(handles);
237 			return (pciio);
238 		}
239 	}
240 	free(handles);
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 = &gEfiEdidActiveProtocolGuid;
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 = &gEfiEdidDiscoveredProtocolGuid;
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 	uint_t nhandles, i;
518 	extern EFI_GRAPHICS_OUTPUT_PROTOCOL *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 	status = efi_get_protocol_handles(&gEfiGraphicsOutputProtocolGuid,
527 	    &nhandles, &hlist);
528 	if (EFI_ERROR(status))
529 		return (efi_status_to_errno(status));
530 
531 	/*
532 	 * Search for ConOut protocol, if not found, use first handle.
533 	 */
534 	gop_handle = NULL;
535 	for (i = 0; i < nhandles; i++) {
536 		EFI_GRAPHICS_OUTPUT_PROTOCOL *tgop;
537 		void *dummy;
538 
539 		status = OpenProtocolByHandle(hlist[i],
540 		    &gEfiGraphicsOutputProtocolGuid,
541 		    (void **)&tgop);
542 		if (status != EFI_SUCCESS)
543 			continue;
544 
545 		if (tgop->Mode->Info->PixelFormat == PixelBltOnly ||
546 		    tgop->Mode->Info->PixelFormat >= PixelFormatMax)
547 			continue;
548 
549 		status = OpenProtocolByHandle(hlist[i],
550 		    &gEfiConsoleOutDeviceGuid, &dummy);
551 		if (status == EFI_SUCCESS) {
552 			gop_handle = hlist[i];
553 			gop = tgop;
554 			break;
555 		} else if (gop_handle == NULL) {
556 			gop_handle = hlist[i];
557 			gop = tgop;
558 		}
559 	}
560 
561 	free(hlist);
562 
563 	if (gop != NULL) {
564 		has_framebuffer = true;
565 		/* Save default mode. */
566 		if (default_mode == UINT32_MAX) {
567 			default_mode = gop->Mode->Mode;
568 		}
569 		mode = gop_default_mode();
570 		if (mode != gop->Mode->Mode)
571 			efifb_set_mode(gop, mode);
572 		return (efifb_from_gop(efifb, gop->Mode, gop->Mode->Info));
573 	}
574 
575 	if (uga != NULL)
576 		return (efifb_from_uga(efifb, uga));
577 
578 	status = BS->LocateProtocol(&gEfiUgaDrawProtocolGuid, NULL,
579 	    (void **)&uga);
580 	if (status == EFI_SUCCESS) {
581 		has_framebuffer = true;
582 		return (efifb_from_uga(efifb, uga));
583 	}
584 
585 	has_framebuffer = false;
586 	return (1);
587 }
588 
589 static void
590 print_efifb(int mode, struct efi_fb *efifb, int verbose)
591 {
592 	uint_t depth;
593 	edid_res_list_t res;
594 	struct resolution *rp;
595 
596 	TAILQ_INIT(&res);
597 	if (verbose == 1) {
598 		printf("Framebuffer mode: %s\n",
599 		    plat_stdout_is_framebuffer() ? "on" : "off");
600 		if (efifb_get_edid(&res)) {
601 			printf("EDID");
602 			while ((rp = TAILQ_FIRST(&res)) != NULL) {
603 				printf(" %dx%d", rp->width, rp->height);
604 				TAILQ_REMOVE(&res, rp, next);
605 				free(rp);
606 			}
607 			printf("\n");
608 		}
609 	}
610 
611 	if (mode >= 0) {
612 		if (verbose == 1)
613 			printf("GOP ");
614 		printf("mode %d: ", mode);
615 	}
616 	depth = efifb_color_depth(efifb);
617 	printf("%ux%ux%u", efifb->fb_width, efifb->fb_height, depth);
618 	if (verbose)
619 		printf(", stride=%u", efifb->fb_stride);
620 	if (verbose) {
621 		printf("\n    frame buffer: address=%jx, size=%jx",
622 		    (uintmax_t)efifb->fb_addr, (uintmax_t)efifb->fb_size);
623 		printf("\n    color mask: R=%08x, G=%08x, B=%08x\n",
624 		    efifb->fb_mask_red, efifb->fb_mask_green,
625 		    efifb->fb_mask_blue);
626 		if (efifb->fb_addr == 0) {
627 			printf("Warning: this mode is not implementing the "
628 			    "linear framebuffer. The illumos\n\tconsole is "
629 			    "not available with this mode and will default to "
630 			    "ttya\n");
631 		}
632 	}
633 }
634 
635 static int
636 efifb_set_mode(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop, uint_t mode)
637 {
638 	EFI_STATUS status;
639 
640 	status = gop->SetMode(gop, mode);
641 	if (EFI_ERROR(status)) {
642 		snprintf(command_errbuf, sizeof (command_errbuf),
643 		    "Unable to set mode to %u (error=%lu)",
644 		    mode, DECODE_ERROR(status));
645 		return (CMD_ERROR);
646 	}
647 	return (CMD_OK);
648 }
649 
650 /*
651  * Verify existance of mode number or find mode by
652  * dimensions. If depth is not given, walk values 32, 24, 16, 8.
653  * Return MaxMode if mode is not found.
654  */
655 static int
656 efifb_find_mode_xydm(UINT32 x, UINT32 y, int depth, int m)
657 {
658 	extern EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
659 	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
660 	EFI_STATUS status;
661 	UINTN infosz;
662 	struct efi_fb fb;
663 	UINT32 mode;
664 	uint_t d, i;
665 
666 	if (m != -1)
667 		i = 8;
668 	else if (depth == -1)
669 		i = 32;
670 	else
671 		i = depth;
672 
673 	while (i > 0) {
674 		for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
675 			status = gop->QueryMode(gop, mode, &infosz, &info);
676 			if (EFI_ERROR(status))
677 				continue;
678 
679 			if (m != -1) {
680 				if ((UINT32)m == mode)
681 					return (mode);
682 				else
683 					continue;
684 			}
685 
686 			efifb_from_gop(&fb, gop->Mode, info);
687 			d = efifb_color_depth(&fb);
688 			if (x == fb.fb_width && y == fb.fb_height && d == i)
689 				return (mode);
690 		}
691 
692 		if (depth != -1)
693 			break;
694 
695 		i -= 8;
696 	}
697 
698 	return (gop->Mode->MaxMode);
699 }
700 
701 static int
702 efifb_find_mode(char *str)
703 {
704 	extern EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
705 	int x, y, depth;
706 
707 	if (!gfx_parse_mode_str(str, &x, &y, &depth))
708 		return (gop->Mode->MaxMode);
709 
710 	return (efifb_find_mode_xydm(x, y, depth, -1));
711 }
712 
713 /*
714  * gop_default_mode(). Try to set mode based on EDID.
715  */
716 static uint32_t
717 gop_default_mode(void)
718 {
719 	edid_res_list_t res;
720 	struct resolution *rp;
721 	extern EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
722 	UINT32 mode;
723 
724 	mode = gop->Mode->MaxMode;
725 	TAILQ_INIT(&res);
726 	if (efifb_get_edid(&res)) {
727 		while ((rp = TAILQ_FIRST(&res)) != NULL) {
728 			if (mode == gop->Mode->MaxMode) {
729 				mode = efifb_find_mode_xydm(
730 				    rp->width, rp->height, -1, -1);
731 			}
732 			TAILQ_REMOVE(&res, rp, next);
733 			free(rp);
734 		}
735 	}
736 
737 	if (mode == gop->Mode->MaxMode)
738 		mode = default_mode;
739 
740 	return (mode);
741 }
742 
743 COMMAND_SET(framebuffer, "framebuffer", "framebuffer mode management",
744     command_gop);
745 
746 static int
747 command_gop(int argc, char *argv[])
748 {
749 	extern struct efi_fb efifb;
750 	extern EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
751 	struct efi_fb fb;
752 	EFI_STATUS status;
753 	char *arg, *cp;
754 	uint_t mode;
755 
756 	if (gop == NULL) {
757 		snprintf(command_errbuf, sizeof (command_errbuf),
758 		    "%s: Graphics Output Protocol not present", argv[0]);
759 		return (CMD_ERROR);
760 	}
761 
762 	if (argc < 2)
763 		goto usage;
764 
765 	/*
766 	 * Note we can not turn the GOP itself off, but instead we instruct
767 	 * tem to use text mode.
768 	 */
769 	if (strcmp(argv[1], "off") == 0) {
770 		if (argc != 2)
771 			goto usage;
772 
773 		reset_font_flags();
774 		plat_cons_update_mode(EfiConsoleControlScreenText);
775 		return (CMD_OK);
776 	}
777 
778 	/*
779 	 * Set GOP to use default mode, then notify tem.
780 	 */
781 	if (strcmp(argv[1], "on") == 0) {
782 		if (argc != 2)
783 			goto usage;
784 
785 		reset_font_flags();
786 		mode = gop_default_mode();
787 		if (mode != gop->Mode->Mode)
788 			efifb_set_mode(gop, mode);
789 
790 		plat_cons_update_mode(EfiConsoleControlScreenGraphics);
791 		return (CMD_OK);
792 	}
793 
794 	if (strcmp(argv[1], "set") == 0) {
795 		int rv;
796 
797 		if (argc != 3)
798 			goto usage;
799 
800 		arg = argv[2];
801 		if (strchr(arg, 'x') == NULL) {
802 			errno = 0;
803 			mode = strtoul(arg, &cp, 0);
804 			if (errno != 0 || *arg == '\0' || cp[0] != '\0') {
805 				snprintf(command_errbuf,
806 				    sizeof (command_errbuf),
807 				    "mode should be an integer");
808 				return (CMD_ERROR);
809 			}
810 			mode = efifb_find_mode_xydm(0, 0, 0, mode);
811 		} else {
812 			mode = efifb_find_mode(arg);
813 		}
814 
815 		if (mode == gop->Mode->MaxMode)
816 			mode = gop->Mode->Mode;
817 
818 		reset_font_flags();
819 		rv = efifb_set_mode(gop, mode);
820 		plat_cons_update_mode(EfiConsoleControlScreenGraphics);
821 		return (rv);
822 	}
823 
824 	if (strcmp(argv[1], "get") == 0) {
825 		if (argc != 2)
826 			goto usage;
827 
828 		print_efifb(gop->Mode->Mode, &efifb, 1);
829 		printf("\n");
830 		return (CMD_OK);
831 	}
832 
833 	if (strcmp(argv[1], "list") == 0) {
834 		EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
835 		UINTN infosz;
836 		int depth, d = -1;
837 
838 		if (argc != 2 && argc != 3)
839 			goto usage;
840 
841 		if (argc == 3) {
842 			arg = argv[2];
843 			errno = 0;
844 			d = strtoul(arg, &cp, 0);
845 			if (errno != 0 || *arg == '\0' || cp[0] != '\0') {
846 				snprintf(command_errbuf,
847 				    sizeof (command_errbuf),
848 				    "depth should be an integer");
849 				return (CMD_ERROR);
850 			}
851 		}
852 		pager_open();
853 		for (mode = 0; mode < gop->Mode->MaxMode; mode++) {
854 			status = gop->QueryMode(gop, mode, &infosz, &info);
855 			if (EFI_ERROR(status))
856 				continue;
857 			efifb_from_gop(&fb, gop->Mode, info);
858 			depth = efifb_color_depth(&fb);
859 			if (d != -1 && d != depth)
860 				continue;
861 			print_efifb(mode, &fb, 0);
862 			if (pager_output("\n"))
863 				break;
864 		}
865 		pager_close();
866 		return (CMD_OK);
867 	}
868 
869 usage:
870 	snprintf(command_errbuf, sizeof (command_errbuf),
871 	    "usage: %s on | off | get | list [depth] | "
872 	    "set <display or GOP mode number>", argv[0]);
873 	return (CMD_ERROR);
874 }
875 
876 COMMAND_SET(uga, "uga", "universal graphics adapter", command_uga);
877 
878 static int
879 command_uga(int argc, char *argv[])
880 {
881 	extern struct efi_fb efifb;
882 	extern EFI_UGA_DRAW_PROTOCOL *uga;
883 
884 	if (uga == NULL) {
885 		snprintf(command_errbuf, sizeof (command_errbuf),
886 		    "%s: UGA Protocol not present", argv[0]);
887 		return (CMD_ERROR);
888 	}
889 
890 	if (argc != 1)
891 		goto usage;
892 
893 	if (efifb.fb_addr == 0) {
894 		snprintf(command_errbuf, sizeof (command_errbuf),
895 		    "%s: Unable to get UGA information", argv[0]);
896 		return (CMD_ERROR);
897 	}
898 
899 	print_efifb(-1, &efifb, 1);
900 	printf("\n");
901 	return (CMD_OK);
902 
903 usage:
904 	snprintf(command_errbuf, sizeof (command_errbuf), "usage: %s", argv[0]);
905 	return (CMD_ERROR);
906 }
907