xref: /illumos-gate/usr/src/boot/sys/boot/i386/libi386/vbe.c (revision fa9eb222)
1 /*
2  * Copyright (c) 2009 Jared D. McNeill <jmcneill@invisible.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  * VESA BIOS Extensions routines
29  */
30 
31 #include <stand.h>
32 #include <stdbool.h>
33 #include <bootstrap.h>
34 #include <machine/bootinfo.h>
35 #include <machine/metadata.h>
36 #include <sys/multiboot2.h>
37 #include <btxv86.h>
38 #include "libi386.h"
39 #include "gfx_fb.h"	/* for EDID */
40 #include "vbe.h"
41 #include <sys/font.h>
42 #include <sys/rgb.h>
43 #include <sys/vgareg.h>
44 #include <sys/vgasubr.h>
45 
46 multiboot_tag_vbe_t vbestate;
47 static struct vbeinfoblock *vbe =
48 	(struct vbeinfoblock *)&vbestate.vbe_control_info;
49 static struct modeinfoblock *vbe_mode =
50 	(struct modeinfoblock *)&vbestate.vbe_mode_info;
51 static uint16_t *vbe_mode_list;
52 static size_t vbe_mode_list_size;
53 struct vesa_edid_info *edid_info = NULL;
54 multiboot_color_t *cmap;
55 /* The default VGA color palette format is 6 bits per primary color. */
56 int palette_format = 6;
57 
58 #define	VESA_MODE_BASE		0x100
59 #define	VESA_END_OF_MODE_LIST	0xffff
60 
61 /* Actually assuming mode 3. */
62 void
bios_set_text_mode(int mode)63 bios_set_text_mode(int mode)
64 {
65 	int atr;
66 
67 	if (vbe->Capabilities & VBE_CAP_DAC8) {
68 		int m;
69 
70 		/*
71 		 * The mode change should reset the palette format to
72 		 * 6 bits, but apparently some systems do fail with 8-bit
73 		 * palette, so we switch to 6-bit here.
74 		 */
75 		m = 0x0600;
76 		(void) biosvbe_palette_format(&m);
77 		palette_format = m;
78 	}
79 	v86.ctl = V86_FLAGS;
80 	v86.addr = 0x10;
81 	v86.eax = mode;				/* set VGA text mode */
82 	v86int();
83 	atr = vga_get_atr(VGA_REG_ADDR, VGA_ATR_MODE);
84 	atr &= ~VGA_ATR_MODE_BLINK;
85 	atr &= ~VGA_ATR_MODE_9WIDE;
86 	vga_set_atr(VGA_REG_ADDR, VGA_ATR_MODE, atr);
87 
88 	vbestate.vbe_mode = 0;			/* vbe is disabled */
89 	gfx_fb.framebuffer_common.framebuffer_type =
90 	    MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT;
91 	/* 16 bits per character */
92 	gfx_fb.framebuffer_common.framebuffer_bpp = 16;
93 	gfx_fb.framebuffer_common.framebuffer_addr =
94 	    VGA_MEM_ADDR + VGA_COLOR_BASE;
95 	gfx_fb.framebuffer_common.framebuffer_width = TEXT_COLS;
96 	gfx_fb.framebuffer_common.framebuffer_height = TEXT_ROWS;
97 	gfx_fb.framebuffer_common.framebuffer_pitch = TEXT_COLS * 2;
98 }
99 
100 /* Function 00h - Return VBE Controller Information */
101 static int
biosvbe_info(struct vbeinfoblock * vbe)102 biosvbe_info(struct vbeinfoblock *vbe)
103 {
104 	v86.ctl = V86_FLAGS;
105 	v86.addr = 0x10;
106 	v86.eax = 0x4f00;
107 	v86.es = VTOPSEG(vbe);
108 	v86.edi = VTOPOFF(vbe);
109 	v86int();
110 	return (v86.eax & 0xffff);
111 }
112 
113 /* Function 01h - Return VBE Mode Information */
114 static int
biosvbe_get_mode_info(int mode,struct modeinfoblock * mi)115 biosvbe_get_mode_info(int mode, struct modeinfoblock *mi)
116 {
117 	v86.ctl = V86_FLAGS;
118 	v86.addr = 0x10;
119 	v86.eax = 0x4f01;
120 	v86.ecx = mode;
121 	v86.es = VTOPSEG(mi);
122 	v86.edi = VTOPOFF(mi);
123 	v86int();
124 	return (v86.eax & 0xffff);
125 }
126 
127 /* Function 02h - Set VBE Mode */
128 static int
biosvbe_set_mode(int mode,struct crtciinfoblock * ci)129 biosvbe_set_mode(int mode, struct crtciinfoblock *ci)
130 {
131 	int rv;
132 
133 	if (vbe->Capabilities & VBE_CAP_DAC8) {
134 		int m;
135 
136 		/*
137 		 * The mode change should reset the palette format to
138 		 * 6 bits, but apparently some systems do fail with 8-bit
139 		 * palette, so we switch to 6-bit here.
140 		 */
141 		m = 0x0600;
142 		if (biosvbe_palette_format(&m) == VBE_SUCCESS)
143 			palette_format = m;
144 	}
145 	v86.ctl = V86_FLAGS;
146 	v86.addr = 0x10;
147 	v86.eax = 0x4f02;
148 	v86.ebx = mode | 0x4000;	/* set linear FB bit */
149 	v86.es = VTOPSEG(ci);
150 	v86.edi = VTOPOFF(ci);
151 	v86int();
152 	rv = v86.eax & 0xffff;
153 	if (vbe->Capabilities & VBE_CAP_DAC8) {
154 		int m;
155 
156 		/* Switch to 8-bits per primary color. */
157 		m = 0x0800;
158 		if (biosvbe_palette_format(&m) == VBE_SUCCESS)
159 			palette_format = m;
160 	}
161 	return (rv);
162 }
163 
164 /* Function 03h - Get VBE Mode */
165 static int
biosvbe_get_mode(int * mode)166 biosvbe_get_mode(int *mode)
167 {
168 	v86.ctl = V86_FLAGS;
169 	v86.addr = 0x10;
170 	v86.eax = 0x4f03;
171 	v86int();
172 	*mode = v86.ebx & 0x3fff;	/* Bits 0-13 */
173 	return (v86.eax & 0xffff);
174 }
175 
176 /* Function 08h - Set/Get DAC Palette Format */
177 int
biosvbe_palette_format(int * format)178 biosvbe_palette_format(int *format)
179 {
180 	v86.ctl = V86_FLAGS;
181 	v86.addr = 0x10;
182 	v86.eax = 0x4f08;
183 	v86.ebx = *format;
184 	v86int();
185 	*format = (v86.ebx >> 8) & 0xff;
186 	return (v86.eax & 0xffff);
187 }
188 
189 /* Function 09h - Set/Get Palette Data */
190 static int
biosvbe_palette_data(int mode,int reg,struct paletteentry * pe)191 biosvbe_palette_data(int mode, int reg, struct paletteentry *pe)
192 {
193 	v86.ctl = V86_FLAGS;
194 	v86.addr = 0x10;
195 	v86.eax = 0x4f09;
196 	v86.ebx = mode;
197 	v86.edx = reg;
198 	v86.ecx = 1;
199 	v86.es = VTOPSEG(pe);
200 	v86.edi = VTOPOFF(pe);
201 	v86int();
202 	return (v86.eax & 0xffff);
203 }
204 
205 /*
206  * Function 15h BL=00h - Report VBE/DDC Capabilities
207  *
208  * int biosvbe_ddc_caps(void)
209  * return: VBE/DDC capabilities
210  */
211 static int
biosvbe_ddc_caps(void)212 biosvbe_ddc_caps(void)
213 {
214 	v86.ctl = V86_FLAGS;
215 	v86.addr = 0x10;
216 	v86.eax = 0x4f15;	/* display identification extensions */
217 	v86.ebx = 0;		/* report DDC capabilities */
218 	v86.ecx = 0;		/* controller unit number (00h = primary) */
219 	v86.es = 0;
220 	v86.edi = 0;
221 	v86int();
222 	if (VBE_ERROR(v86.eax & 0xffff))
223 		return (0);
224 	return (v86.ebx & 0xffff);
225 }
226 
227 /* Function 11h BL=01h - Flat Panel status */
228 static int
biosvbe_ddc_read_flat_panel_info(void * buf)229 biosvbe_ddc_read_flat_panel_info(void *buf)
230 {
231 	v86.ctl = V86_FLAGS;
232 	v86.addr = 0x10;
233 	v86.eax = 0x4f11;	/* Flat Panel Interface extensions */
234 	v86.ebx = 1;		/* Return Flat Panel Information */
235 	v86.es = VTOPSEG(buf);
236 	v86.edi = VTOPOFF(buf);
237 	v86int();
238 	return (v86.eax & 0xffff);
239 }
240 
241 /* Function 15h BL=01h - Read EDID */
242 static int
biosvbe_ddc_read_edid(int blockno,void * buf)243 biosvbe_ddc_read_edid(int blockno, void *buf)
244 {
245 	v86.ctl = V86_FLAGS;
246 	v86.addr = 0x10;
247 	v86.eax = 0x4f15;	/* display identification extensions */
248 	v86.ebx = 1;		/* read EDID */
249 	v86.ecx = 0;		/* controller unit number (00h = primary) */
250 	v86.edx = blockno;
251 	v86.es = VTOPSEG(buf);
252 	v86.edi = VTOPOFF(buf);
253 	v86int();
254 	return (v86.eax & 0xffff);
255 }
256 
257 static int
vbe_mode_is_supported(struct modeinfoblock * mi)258 vbe_mode_is_supported(struct modeinfoblock *mi)
259 {
260 	if ((mi->ModeAttributes & 0x01) == 0)
261 		return (0);	/* mode not supported by hardware */
262 	if ((mi->ModeAttributes & 0x08) == 0)
263 		return (0);	/* linear fb not available */
264 	if ((mi->ModeAttributes & 0x10) == 0)
265 		return (0);	/* text mode */
266 	if (mi->NumberOfPlanes != 1)
267 		return (0);	/* planar mode not supported */
268 	if (mi->MemoryModel != 0x04 /* Packed pixel */ &&
269 	    mi->MemoryModel != 0x06 /* Direct Color */)
270 		return (0);	/* unsupported pixel format */
271 	return (1);
272 }
273 
274 static int
vbe_check(void)275 vbe_check(void)
276 {
277 	if (vbestate.mb_type != MULTIBOOT_TAG_TYPE_VBE) {
278 		printf("VBE not available\n");
279 		return (0);
280 	}
281 	return (1);
282 }
283 
284 /*
285  * Translate selector:offset style address to linear adress.
286  * selector = farptr >> 16;
287  * offset = farptr & 0xffff;
288  * linear = (selector * 4) + offset.
289  * By using mask 0xffff0000, we wil get the optimised line below.
290  * As a final step, translate physical address to loader virtual address.
291  */
292 static void *
vbe_farptr(uint32_t farptr)293 vbe_farptr(uint32_t farptr)
294 {
295 	return (PTOV((((farptr & 0xffff0000) >> 12) + (farptr & 0xffff))));
296 }
297 
298 void
vbe_init(void)299 vbe_init(void)
300 {
301 	uint16_t *p, *ml;
302 
303 	/* First set FB for text mode. */
304 	gfx_fb.framebuffer_common.mb_type = MULTIBOOT_TAG_TYPE_FRAMEBUFFER;
305 	gfx_fb.framebuffer_common.framebuffer_type =
306 	    MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT;
307 	/* 16 bits per character */
308 	gfx_fb.framebuffer_common.framebuffer_bpp = 16;
309 	gfx_fb.framebuffer_common.framebuffer_addr =
310 	    VGA_MEM_ADDR + VGA_COLOR_BASE;
311 	gfx_fb.framebuffer_common.framebuffer_width = TEXT_COLS;
312 	gfx_fb.framebuffer_common.framebuffer_height = TEXT_ROWS;
313 	gfx_fb.framebuffer_common.framebuffer_pitch = TEXT_COLS * 2;
314 
315 	/* Now check if we have vesa. */
316 	memset(vbe, 0, sizeof (*vbe));
317 	memcpy(vbe->VbeSignature, "VBE2", 4);
318 	if (biosvbe_info(vbe) != VBE_SUCCESS)
319 		return;
320 	if (memcmp(vbe->VbeSignature, "VESA", 4) != 0)
321 		return;
322 
323 	/*
324 	 * Copy mode list array. We must do this because some systems do
325 	 * place this array to scratch memory, which will be reused by
326 	 * subsequent VBE calls. (vbox 6.1 is one example).
327 	 */
328 	p = ml = vbe_farptr(vbe->VideoModePtr);
329 	while (*p++ != VESA_END_OF_MODE_LIST)
330 		;
331 
332 	vbe_mode_list_size = (uintptr_t)p - (uintptr_t)ml;
333 
334 	/*
335 	 * Since vbe_init() is used only once at very start of the loader,
336 	 * we assume malloc will not fail there. But in case it does,
337 	 * we point vbe_mode_list to memory pointed by VideoModePtr.
338 	 * If the VideoModePtr memory area is not valid, we will fail to
339 	 * pick usable VBE mode and fall back to use text mode.
340 	 */
341 	vbe_mode_list = malloc(vbe_mode_list_size);
342 	if (vbe_mode_list == NULL)
343 		vbe_mode_list = ml;
344 	else
345 		bcopy(ml, vbe_mode_list, vbe_mode_list_size);
346 
347 	/* reset VideoModePtr, to make sure, we only do use vbe_mode_list. */
348 	vbe->VideoModePtr = 0;
349 
350 	vbestate.mb_type = MULTIBOOT_TAG_TYPE_VBE;
351 	vbestate.mb_size = sizeof (vbestate);
352 	vbestate.vbe_mode = 0;
353 
354 	/* vbe_set_mode() will set up the rest. */
355 }
356 
357 int
vbe_available(void)358 vbe_available(void)
359 {
360 	return (vbestate.mb_type);
361 }
362 
363 int
vbe_set_palette(const struct paletteentry * entry,size_t slot)364 vbe_set_palette(const struct paletteentry *entry, size_t slot)
365 {
366 	struct paletteentry pe;
367 	int mode, ret;
368 
369 	if (!vbe_check() || (vbe->Capabilities & VBE_CAP_DAC8) == 0)
370 		return (1);
371 
372 	if (gfx_fb.framebuffer_common.framebuffer_type !=
373 	    MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED) {
374 		return (1);
375 	}
376 
377 	if (cmap == NULL)
378 		cmap = calloc(CMAP_SIZE, sizeof (*cmap));
379 
380 	pe.Blue = entry->Blue;
381 	pe.Green = entry->Green;
382 	pe.Red = entry->Red;
383 	pe.Reserved = entry->Reserved;
384 
385 	if (vbe->Capabilities & VBE_CAP_SNOW)
386 		mode = 0x80;
387 	else
388 		mode = 0;
389 
390 	ret = biosvbe_palette_data(mode, slot, &pe);
391 	if (cmap != NULL && slot < CMAP_SIZE) {
392 		cmap[slot].mb_red = entry->Red;
393 		cmap[slot].mb_green = entry->Green;
394 		cmap[slot].mb_blue = entry->Blue;
395 	}
396 
397 	return (ret == VBE_SUCCESS ? 0 : 1);
398 }
399 
400 int
vbe_get_mode(void)401 vbe_get_mode(void)
402 {
403 	return (vbestate.vbe_mode);
404 }
405 
406 int
vbe_set_mode(int modenum)407 vbe_set_mode(int modenum)
408 {
409 	struct modeinfoblock mi;
410 	int ret;
411 
412 	if (!vbe_check())
413 		return (1);
414 
415 	ret = biosvbe_get_mode_info(modenum, &mi);
416 	if (VBE_ERROR(ret)) {
417 		printf("mode 0x%x invalid\n", modenum);
418 		return (1);
419 	}
420 
421 	if (!vbe_mode_is_supported(&mi)) {
422 		printf("mode 0x%x not supported\n", modenum);
423 		return (1);
424 	}
425 
426 	/* calculate bytes per pixel */
427 	switch (mi.BitsPerPixel) {
428 	case 32:
429 	case 24:
430 	case 16:
431 	case 15:
432 	case 8:
433 		break;
434 	default:
435 		printf("BitsPerPixel %d is not supported\n", mi.BitsPerPixel);
436 		return (1);
437 	}
438 
439 	ret = biosvbe_set_mode(modenum, NULL);
440 	if (VBE_ERROR(ret)) {
441 		printf("mode 0x%x could not be set\n", modenum);
442 		return (1);
443 	}
444 
445 	/* make sure we have current MI in vbestate */
446 	memcpy(vbe_mode, &mi, sizeof (*vbe_mode));
447 	vbestate.vbe_mode = modenum;
448 
449 	gfx_fb.framebuffer_common.framebuffer_addr =
450 	    (uint64_t)mi.PhysBasePtr & 0xffffffff;
451 	gfx_fb.framebuffer_common.framebuffer_width = mi.XResolution;
452 	gfx_fb.framebuffer_common.framebuffer_height = mi.YResolution;
453 	gfx_fb.framebuffer_common.framebuffer_bpp = mi.BitsPerPixel;
454 
455 	/* vbe_mode_is_supported() excludes the rest */
456 	switch (mi.MemoryModel) {
457 	case 0x4:
458 		gfx_fb.framebuffer_common.framebuffer_type =
459 		    MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED;
460 		break;
461 	case 0x6:
462 		gfx_fb.framebuffer_common.framebuffer_type =
463 		    MULTIBOOT_FRAMEBUFFER_TYPE_RGB;
464 		break;
465 	}
466 
467 	if (vbe->VbeVersion >= 0x300) {
468 		gfx_fb.framebuffer_common.framebuffer_pitch =
469 		    mi.LinBytesPerScanLine;
470 		gfx_fb.u.fb2.framebuffer_red_field_position =
471 		    mi.LinRedFieldPosition;
472 		gfx_fb.u.fb2.framebuffer_red_mask_size = mi.LinRedMaskSize;
473 		gfx_fb.u.fb2.framebuffer_green_field_position =
474 		    mi.LinGreenFieldPosition;
475 		gfx_fb.u.fb2.framebuffer_green_mask_size = mi.LinGreenMaskSize;
476 		gfx_fb.u.fb2.framebuffer_blue_field_position =
477 		    mi.LinBlueFieldPosition;
478 		gfx_fb.u.fb2.framebuffer_blue_mask_size = mi.LinBlueMaskSize;
479 	} else {
480 		gfx_fb.framebuffer_common.framebuffer_pitch =
481 		    mi.BytesPerScanLine;
482 		gfx_fb.u.fb2.framebuffer_red_field_position =
483 		    mi.RedFieldPosition;
484 		gfx_fb.u.fb2.framebuffer_red_mask_size = mi.RedMaskSize;
485 		gfx_fb.u.fb2.framebuffer_green_field_position =
486 		    mi.GreenFieldPosition;
487 		gfx_fb.u.fb2.framebuffer_green_mask_size = mi.GreenMaskSize;
488 		gfx_fb.u.fb2.framebuffer_blue_field_position =
489 		    mi.BlueFieldPosition;
490 		gfx_fb.u.fb2.framebuffer_blue_mask_size = mi.BlueMaskSize;
491 	}
492 
493 	/*
494 	 * Support for color mapping.
495 	 * For 8, 24 and 32 bit depth, use mask size 8.
496 	 * 15/16 bit depth needs to use mask size from mode, or we will
497 	 * lose color information from 32-bit to 15/16 bit translation.
498 	 */
499 	if (mi.BitsPerPixel == 15 || mi.BitsPerPixel == 16) {
500 		rgb_info.red.size = gfx_fb.u.fb2.framebuffer_red_mask_size;
501 		rgb_info.green.size = gfx_fb.u.fb2.framebuffer_green_mask_size;
502 		rgb_info.blue.size = gfx_fb.u.fb2.framebuffer_blue_mask_size;
503 	} else {
504 		rgb_info.red.size = 8;
505 		rgb_info.green.size = 8;
506 		rgb_info.blue.size = 8;
507 	}
508 	rgb_info.red.pos = 16;
509 	rgb_info.green.pos = 8;
510 	rgb_info.blue.pos = 0;
511 
512 	return (0);
513 }
514 
515 /*
516  * Verify existance of mode number or find mode by
517  * dimensions. If depth is not given, walk values 32, 24, 16, 8.
518  */
519 static int
vbe_find_mode_xydm(int x,int y,int depth,int m)520 vbe_find_mode_xydm(int x, int y, int depth, int m)
521 {
522 	struct modeinfoblock mi;
523 	uint16_t mode;
524 	size_t idx, nentries;
525 	int i;
526 
527 	memset(vbe, 0, sizeof (vbe));
528 	memcpy(vbe->VbeSignature, "VBE2", 4);
529 	if (biosvbe_info(vbe) != VBE_SUCCESS)
530 		return (0);
531 	if (memcmp(vbe->VbeSignature, "VESA", 4) != 0)
532 		return (0);
533 
534 	if (m != -1)
535 		i = 8;
536 	else if (depth == -1)
537 		i = 32;
538 	else
539 		i = depth;
540 
541 	nentries = vbe_mode_list_size / sizeof (*vbe_mode_list);
542 	while (i > 0) {
543 		for (idx = 0; idx < nentries; idx++) {
544 			mode = vbe_mode_list[idx];
545 			if (mode == VESA_END_OF_MODE_LIST)
546 				break;
547 
548 			if (biosvbe_get_mode_info(mode, &mi) != VBE_SUCCESS)
549 				continue;
550 
551 			/* we only care about linear modes here */
552 			if (vbe_mode_is_supported(&mi) == 0)
553 				continue;
554 
555 			if (m != -1) {
556 				if (m == mode)
557 					return (mode);
558 				else
559 					continue;
560 			}
561 
562 			if (mi.XResolution == x &&
563 			    mi.YResolution == y &&
564 			    mi.BitsPerPixel == i)
565 				return (mode);
566 		}
567 		if (depth != -1)
568 			break;
569 
570 		i -= 8;
571 	}
572 
573 	return (0);
574 }
575 
576 static int
vbe_find_mode(char * str)577 vbe_find_mode(char *str)
578 {
579 	int x, y, depth;
580 
581 	if (!gfx_parse_mode_str(str, &x, &y, &depth))
582 		return (0);
583 
584 	return (vbe_find_mode_xydm(x, y, depth, -1));
585 }
586 
587 static void
vbe_dump_mode(int modenum,struct modeinfoblock * mi)588 vbe_dump_mode(int modenum, struct modeinfoblock *mi)
589 {
590 	printf("0x%x=%dx%dx%d", modenum,
591 	    mi->XResolution, mi->YResolution, mi->BitsPerPixel);
592 }
593 
594 static bool
vbe_get_edid(edid_res_list_t * res)595 vbe_get_edid(edid_res_list_t *res)
596 {
597 	struct vesa_edid_info *edidp;
598 	const uint8_t magic[] = EDID_MAGIC;
599 	int ddc_caps;
600 	bool ret = false;
601 
602 	if (edid_info != NULL)
603 		return (gfx_get_edid_resolution(edid_info, res));
604 
605 	ddc_caps = biosvbe_ddc_caps();
606 	if (ddc_caps == 0) {
607 		return (ret);
608 	}
609 
610 	edidp = bio_alloc(sizeof (*edidp));
611 	if (edidp == NULL)
612 		return (ret);
613 	memset(edidp, 0, sizeof (*edidp));
614 
615 	if (VBE_ERROR(biosvbe_ddc_read_edid(0, edidp)))
616 		goto done;
617 
618 	if (memcmp(edidp, magic, sizeof (magic)) != 0)
619 		goto done;
620 
621 	/* Unknown EDID version. */
622 	if (edidp->header.version != 1)
623 		goto done;
624 
625 	ret = gfx_get_edid_resolution(edidp, res);
626 	edid_info = malloc(sizeof (*edid_info));
627 	if (edid_info != NULL)
628 		memcpy(edid_info, edidp, sizeof (*edid_info));
629 done:
630 	bio_free(edidp, sizeof (*edidp));
631 	return (ret);
632 }
633 
634 static bool
vbe_get_flatpanel(uint_t * pwidth,uint_t * pheight)635 vbe_get_flatpanel(uint_t *pwidth, uint_t *pheight)
636 {
637 	struct flatpanelinfo *fp_info;
638 	bool ret = false;
639 
640 	fp_info = bio_alloc(sizeof (*fp_info));
641 	if (fp_info == NULL)
642 		return (ret);
643 	memset(fp_info, 0, sizeof (*fp_info));
644 
645 	if (VBE_ERROR(biosvbe_ddc_read_flat_panel_info(fp_info)))
646 		goto done;
647 
648 	*pwidth = fp_info->HorizontalSize;
649 	*pheight = fp_info->VerticalSize;
650 	ret = true;
651 
652 done:
653 	bio_free(fp_info, sizeof (*fp_info));
654 	return (ret);
655 }
656 
657 static void
vbe_print_vbe_info(struct vbeinfoblock * vbep)658 vbe_print_vbe_info(struct vbeinfoblock *vbep)
659 {
660 	char *oemstring = "";
661 	char *oemvendor = "", *oemproductname = "", *oemproductrev = "";
662 
663 	if (vbep->OemStringPtr != 0)
664 		oemstring = vbe_farptr(vbep->OemStringPtr);
665 
666 	if (vbep->OemVendorNamePtr != 0)
667 		oemvendor = vbe_farptr(vbep->OemVendorNamePtr);
668 
669 	if (vbep->OemProductNamePtr != 0)
670 		oemproductname = vbe_farptr(vbep->OemProductNamePtr);
671 
672 	if (vbep->OemProductRevPtr != 0)
673 		oemproductrev = vbe_farptr(vbep->OemProductRevPtr);
674 
675 	printf("VESA VBE Version %d.%d\n%s\n", vbep->VbeVersion >> 8,
676 	    vbep->VbeVersion & 0xF, oemstring);
677 
678 	if (vbep->OemSoftwareRev != 0) {
679 		printf("OEM Version %d.%d, %s (%s, %s)\n",
680 		    vbep->OemSoftwareRev >> 8, vbep->OemSoftwareRev & 0xF,
681 		    oemvendor, oemproductname, oemproductrev);
682 	}
683 }
684 
685 /* List available modes, filter by depth. If depth is -1, list all. */
686 void
vbe_modelist(int depth)687 vbe_modelist(int depth)
688 {
689 	struct modeinfoblock mi;
690 	uint16_t mode;
691 	int nmodes, idx, nentries;
692 	int ddc_caps;
693 	uint_t width, height;
694 	bool edid = false;
695 	edid_res_list_t res;
696 	struct resolution *rp;
697 
698 	if (!vbe_check())
699 		return;
700 
701 	ddc_caps = biosvbe_ddc_caps();
702 	if (ddc_caps & 3) {
703 		printf("DDC");
704 		if (ddc_caps & 1)
705 			printf(" [DDC1]");
706 		if (ddc_caps & 2)
707 			printf(" [DDC2]");
708 
709 		TAILQ_INIT(&res);
710 		edid = vbe_get_edid(&res);
711 		if (edid) {
712 			printf(": EDID");
713 			while ((rp = TAILQ_FIRST(&res)) != NULL) {
714 				printf(" %dx%d", rp->width, rp->height);
715 				TAILQ_REMOVE(&res, rp, next);
716 				free(rp);
717 			}
718 			printf("\n");
719 		} else {
720 			printf(": no EDID information\n");
721 		}
722 	}
723 	if (!edid)
724 		if (vbe_get_flatpanel(&width, &height))
725 			printf(": Panel %dx%d\n", width, height);
726 
727 	nmodes = 0;
728 	memset(vbe, 0, sizeof (vbe));
729 	memcpy(vbe->VbeSignature, "VBE2", 4);
730 	if (biosvbe_info(vbe) != VBE_SUCCESS)
731 		goto done;
732 	if (memcmp(vbe->VbeSignature, "VESA", 4) != 0)
733 		goto done;
734 
735 	vbe_print_vbe_info(vbe);
736 	printf("Modes: ");
737 
738 	nentries = vbe_mode_list_size / sizeof (*vbe_mode_list);
739 	for (idx = 0; idx < nentries; idx++) {
740 		mode = vbe_mode_list[idx];
741 		if (mode == VESA_END_OF_MODE_LIST)
742 			break;
743 
744 		if (biosvbe_get_mode_info(mode, &mi) != VBE_SUCCESS)
745 			continue;
746 
747 		/* we only care about linear modes here */
748 		if (vbe_mode_is_supported(&mi) == 0)
749 			continue;
750 
751 		/* apply requested filter */
752 		if (depth != -1 && mi.BitsPerPixel != depth)
753 			continue;
754 
755 		if (nmodes % 4 == 0)
756 			printf("\n");
757 		else
758 			printf("  ");
759 
760 		vbe_dump_mode(mode, &mi);
761 		nmodes++;
762 	}
763 
764 done:
765 	if (nmodes == 0)
766 		printf("none found");
767 	printf("\n");
768 }
769 
770 static void
vbe_print_mode(bool verbose)771 vbe_print_mode(bool verbose)
772 {
773 	int nc, mode, i, rc;
774 
775 	if (verbose)
776 		nc = 256;
777 	else
778 		nc = 16;
779 
780 	memset(vbe, 0, sizeof (vbe));
781 	memcpy(vbe->VbeSignature, "VBE2", 4);
782 	if (biosvbe_info(vbe) != VBE_SUCCESS)
783 		return;
784 
785 	if (memcmp(vbe->VbeSignature, "VESA", 4) != 0)
786 		return;
787 
788 	vbe_print_vbe_info(vbe);
789 
790 	if (biosvbe_get_mode(&mode) != VBE_SUCCESS) {
791 		printf("Error getting current VBE mode\n");
792 		return;
793 	}
794 
795 	if (biosvbe_get_mode_info(mode, vbe_mode) != VBE_SUCCESS ||
796 	    vbe_mode_is_supported(vbe_mode) == 0) {
797 		printf("VBE mode (0x%x) is not framebuffer mode\n", mode);
798 		return;
799 	}
800 
801 	printf("\nCurrent VBE mode: ");
802 	vbe_dump_mode(mode, vbe_mode);
803 	printf("\n");
804 
805 	printf("%ux%ux%u, stride=%u\n",
806 	    gfx_fb.framebuffer_common.framebuffer_width,
807 	    gfx_fb.framebuffer_common.framebuffer_height,
808 	    gfx_fb.framebuffer_common.framebuffer_bpp,
809 	    (gfx_fb.framebuffer_common.framebuffer_pitch << 3) /
810 	    gfx_fb.framebuffer_common.framebuffer_bpp);
811 	printf("    frame buffer: address=%jx, size=%jx\n",
812 	    (uintmax_t)gfx_fb.framebuffer_common.framebuffer_addr,
813 	    (uintmax_t)gfx_fb.framebuffer_common.framebuffer_height *
814 	    gfx_fb.framebuffer_common.framebuffer_pitch);
815 
816 	if (vbe_mode->MemoryModel == 0x6) {
817 		printf("    color mask: R=%08x, G=%08x, B=%08x\n",
818 		    ((1 << gfx_fb.u.fb2.framebuffer_red_mask_size) - 1) <<
819 		    gfx_fb.u.fb2.framebuffer_red_field_position,
820 		    ((1 << gfx_fb.u.fb2.framebuffer_green_mask_size) - 1) <<
821 		    gfx_fb.u.fb2.framebuffer_green_field_position,
822 		    ((1 << gfx_fb.u.fb2.framebuffer_blue_mask_size) - 1) <<
823 		    gfx_fb.u.fb2.framebuffer_blue_field_position);
824 		return;
825 	}
826 
827 	mode = 1;	/* get DAC palette width */
828 	rc = biosvbe_palette_format(&mode);
829 	if (rc != VBE_SUCCESS)
830 		return;
831 
832 	printf("    palette format: %x bits per primary\n", mode);
833 	if (cmap == NULL)
834 		return;
835 
836 	pager_open();
837 	for (i = 0; i < nc; i++) {
838 		printf("%d: R=%02x, G=%02x, B=%02x", i,
839 		    cmap[i].mb_red, cmap[i].mb_green, cmap[i].mb_blue);
840 		if (pager_output("\n") != 0)
841 			break;
842 	}
843 	pager_close();
844 }
845 
846 /*
847  * Try EDID preferred mode, if EDID or the suggested mode is not available,
848  * then try flat panel information.
849  * Fall back to VBE_DEFAULT_MODE.
850  */
851 int
vbe_default_mode(void)852 vbe_default_mode(void)
853 {
854 	edid_res_list_t res;
855 	struct resolution *rp;
856 	int modenum;
857 	uint_t width, height;
858 
859 	modenum = 0;
860 	TAILQ_INIT(&res);
861 	if (vbe_get_edid(&res)) {
862 		while ((rp = TAILQ_FIRST(&res)) != NULL) {
863 			if (modenum == 0) {
864 				modenum = vbe_find_mode_xydm(
865 				    rp->width, rp->height, -1, -1);
866 			}
867 			TAILQ_REMOVE(&res, rp, next);
868 			free(rp);
869 		}
870 	}
871 
872 	if (modenum == 0 &&
873 	    vbe_get_flatpanel(&width, &height)) {
874 		modenum = vbe_find_mode_xydm(width, height, -1, -1);
875 	}
876 
877 	/* Still no mode? Fall back to default. */
878 	if (modenum == 0)
879 		modenum = vbe_find_mode(VBE_DEFAULT_MODE);
880 	return (modenum);
881 }
882 
883 COMMAND_SET(framebuffer, "framebuffer", "framebuffer mode management",
884     command_vesa);
885 
886 int
command_vesa(int argc,char * argv[])887 command_vesa(int argc, char *argv[])
888 {
889 	char *arg, *cp;
890 	int modenum = -1, n;
891 
892 	if (!vbe_check())
893 		return (CMD_OK);
894 
895 	if (argc < 2)
896 		goto usage;
897 
898 	if (strcmp(argv[1], "list") == 0) {
899 		n = -1;
900 		if (argc != 2 && argc != 3)
901 			goto usage;
902 
903 		if (argc == 3) {
904 			arg = argv[2];
905 			errno = 0;
906 			n = strtoul(arg, &cp, 0);
907 			if (errno != 0 || *arg == '\0' || cp[0] != '\0') {
908 				snprintf(command_errbuf,
909 				    sizeof (command_errbuf),
910 				    "depth should be an integer");
911 				return (CMD_ERROR);
912 			}
913 		}
914 		vbe_modelist(n);
915 		return (CMD_OK);
916 	}
917 
918 	if (strcmp(argv[1], "get") == 0) {
919 		bool verbose = false;
920 
921 		if (argc > 2) {
922 			if (argc > 3 || strcmp(argv[2], "-v") != 0)
923 				goto usage;
924 			verbose = true;
925 		}
926 
927 		vbe_print_mode(verbose);
928 		return (CMD_OK);
929 	}
930 
931 	if (strcmp(argv[1], "off") == 0) {
932 		if (argc != 2)
933 			goto usage;
934 
935 		if (vbestate.vbe_mode == 0)
936 			return (CMD_OK);
937 
938 		reset_font_flags();
939 		bios_text_font(true);
940 		bios_set_text_mode(VGA_TEXT_MODE);
941 		plat_cons_update_mode(0);
942 		return (CMD_OK);
943 	}
944 
945 	if (strcmp(argv[1], "on") == 0) {
946 		if (argc != 2)
947 			goto usage;
948 
949 		modenum = vbe_default_mode();
950 		if (modenum == 0) {
951 			snprintf(command_errbuf, sizeof (command_errbuf),
952 			    "%s: no suitable VBE mode number found", argv[0]);
953 			return (CMD_ERROR);
954 		}
955 	} else if (strcmp(argv[1], "set") == 0) {
956 		if (argc != 3)
957 			goto usage;
958 
959 		if (strncmp(argv[2], "0x", 2) == 0) {
960 			arg = argv[2];
961 			errno = 0;
962 			n = strtoul(arg, &cp, 0);
963 			if (errno != 0 || *arg == '\0' || cp[0] != '\0') {
964 				snprintf(command_errbuf,
965 				    sizeof (command_errbuf),
966 				    "mode should be an integer");
967 				return (CMD_ERROR);
968 			}
969 			modenum = vbe_find_mode_xydm(0, 0, 0, n);
970 		} else if (strchr(argv[2], 'x') != NULL) {
971 			modenum = vbe_find_mode(argv[2]);
972 		}
973 	} else {
974 		goto usage;
975 	}
976 
977 	if (modenum == 0) {
978 		snprintf(command_errbuf, sizeof (command_errbuf),
979 		    "%s: mode %s not supported by firmware\n",
980 		    argv[0], argv[2]);
981 		return (CMD_ERROR);
982 	}
983 
984 	if (modenum >= VESA_MODE_BASE) {
985 		if (vbestate.vbe_mode != modenum) {
986 			reset_font_flags();
987 			bios_text_font(false);
988 			vbe_set_mode(modenum);
989 			plat_cons_update_mode(1);
990 		}
991 		return (CMD_OK);
992 	} else {
993 		snprintf(command_errbuf, sizeof (command_errbuf),
994 		    "%s: mode %s is not framebuffer mode\n", argv[0], argv[2]);
995 		return (CMD_ERROR);
996 	}
997 
998 usage:
999 	snprintf(command_errbuf, sizeof (command_errbuf),
1000 	    "usage: %s on | off | get [-v] | list [depth] | "
1001 	    "set <display or VBE mode number>", argv[0]);
1002 	return (CMD_ERROR);
1003 }
1004