xref: /freebsd/stand/i386/libi386/vidconsole.c (revision f540a43052c12c76d3453ead881248d5467a1ab0)
1 /*-
2  * Copyright (c) 1998 Michael Smith (msmith@freebsd.org)
3  * Copyright (c) 1997 Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * 	Id: probe_keyboard.c,v 1.13 1997/06/09 05:10:55 bde Exp
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <stand.h>
34 #include <sys/param.h>
35 #include <bootstrap.h>
36 #include <btxv86.h>
37 #include <gfx_fb.h>
38 #include <teken.h>
39 #include "vbe.h"
40 
41 #include <dev/vt/hw/vga/vt_vga_reg.h>
42 
43 #include "libi386.h"
44 
45 #if KEYBOARD_PROBE
46 static int	probe_keyboard(void);
47 #endif
48 static void	vidc_probe(struct console *cp);
49 static int	vidc_init(int arg);
50 static void	vidc_putchar(int c);
51 static int	vidc_getchar(void);
52 static int	vidc_ischar(void);
53 static void	cons_draw_frame(teken_attr_t *);
54 
55 static bool	vidc_started;
56 static uint16_t	*vgatext;
57 
58 static tf_bell_t	vidc_cons_bell;
59 static tf_cursor_t	vidc_text_cursor;
60 static tf_putchar_t	vidc_text_putchar;
61 static tf_fill_t	vidc_text_fill;
62 static tf_copy_t	vidc_text_copy;
63 static tf_param_t	vidc_text_param;
64 static tf_respond_t	vidc_cons_respond;
65 
66 static teken_funcs_t tf = {
67 	.tf_bell	= vidc_cons_bell,
68 	.tf_cursor	= vidc_text_cursor,
69 	.tf_putchar	= vidc_text_putchar,
70 	.tf_fill	= vidc_text_fill,
71 	.tf_copy	= vidc_text_copy,
72 	.tf_param	= vidc_text_param,
73 	.tf_respond	= vidc_cons_respond,
74 };
75 
76 static teken_funcs_t tfx = {
77 	.tf_bell	= vidc_cons_bell,
78 	.tf_cursor	= gfx_fb_cursor,
79 	.tf_putchar	= gfx_fb_putchar,
80 	.tf_fill	= gfx_fb_fill,
81 	.tf_copy	= gfx_fb_copy,
82 	.tf_param	= gfx_fb_param,
83 	.tf_respond	= vidc_cons_respond,
84 };
85 
86 #define	KEYBUFSZ	10
87 
88 static uint8_t	keybuf[KEYBUFSZ];	/* keybuf for extended codes */
89 
90 struct console vidconsole = {
91 	.c_name = "vidconsole",
92 	.c_desc = "internal video/keyboard",
93 	.c_flags = 0,
94 	.c_probe = vidc_probe,
95 	.c_init = vidc_init,
96 	.c_out = vidc_putchar,
97 	.c_in = vidc_getchar,
98 	.c_ready = vidc_ischar
99 };
100 
101 /*
102  * This function is used to mark a rectangular image area so the scrolling
103  * will know we need to copy the data from there.
104  */
105 void
106 term_image_display(teken_gfx_t *state, const teken_rect_t *r)
107 {
108 	teken_pos_t p;
109 	int idx;
110 
111 	if (screen_buffer == NULL)
112 		return;
113 
114 	for (p.tp_row = r->tr_begin.tp_row;
115 	    p.tp_row < r->tr_end.tp_row; p.tp_row++) {
116                 for (p.tp_col = r->tr_begin.tp_col;
117                     p.tp_col < r->tr_end.tp_col; p.tp_col++) {
118 			idx = p.tp_col + p.tp_row * state->tg_tp.tp_col;
119 			if (idx >= state->tg_tp.tp_col * state->tg_tp.tp_row)
120 				return;
121 			screen_buffer[idx].a.ta_format |= TF_IMAGE;
122 		}
123 	}
124 }
125 
126 static void
127 vidc_text_set_cursor(teken_unit_t row, teken_unit_t col, bool visible)
128 {
129         uint16_t addr;
130         uint8_t msl, s, e;
131 
132         msl = vga_get_crtc(VGA_REG_BASE, VGA_CRTC_MAX_SCAN_LINE) & 0x1f;
133         s = vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_START) & 0xC0;
134         e = vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_END);
135 
136         if (visible == true) {
137                 addr = row * TEXT_COLS + col;
138                 vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_HIGH, addr >> 8);
139                 vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_LOW,
140 		    addr & 0xff);
141                 e = msl;
142         } else {
143                 s |= (1<<5);
144         }
145         vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_START, s);
146         vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_END, e);
147 }
148 
149 static void
150 vidc_text_get_cursor(teken_unit_t *row, teken_unit_t *col)
151 {
152 	uint16_t addr;
153 
154 	addr = (vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_HIGH) << 8) +
155 	    vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_LOW);
156 
157 	*row = addr / TEXT_COLS;
158 	*col = addr % TEXT_COLS;
159 }
160 
161 /*
162  * Not implemented.
163  */
164 static void
165 vidc_cons_bell(void *s __unused)
166 {
167 }
168 
169 static void
170 vidc_text_cursor(void *s __unused, const teken_pos_t *p)
171 {
172 	teken_unit_t row, col;
173 
174 	if (p->tp_col == TEXT_COLS)
175 		col = p->tp_col - 1;
176 	else
177 		col = p->tp_col;
178 
179 	if (p->tp_row == TEXT_ROWS)
180 		row = p->tp_row - 1;
181 	else
182 		row = p->tp_row;
183 
184 	vidc_text_set_cursor(row, col, true);
185 }
186 
187 /*
188  * Binary searchable table for Unicode to CP437 conversion.
189  */
190 struct unicp437 {
191 	uint16_t	unicode_base;
192 	uint8_t		cp437_base;
193 	uint8_t		length;
194 };
195 
196 static const struct unicp437 cp437table[] = {
197 	{ 0x0020, 0x20, 0x5e }, { 0x00a0, 0x20, 0x00 },
198 	{ 0x00a1, 0xad, 0x00 }, { 0x00a2, 0x9b, 0x00 },
199 	{ 0x00a3, 0x9c, 0x00 }, { 0x00a5, 0x9d, 0x00 },
200 	{ 0x00a7, 0x15, 0x00 }, { 0x00aa, 0xa6, 0x00 },
201 	{ 0x00ab, 0xae, 0x00 }, { 0x00ac, 0xaa, 0x00 },
202 	{ 0x00b0, 0xf8, 0x00 }, { 0x00b1, 0xf1, 0x00 },
203 	{ 0x00b2, 0xfd, 0x00 }, { 0x00b5, 0xe6, 0x00 },
204 	{ 0x00b6, 0x14, 0x00 }, { 0x00b7, 0xfa, 0x00 },
205 	{ 0x00ba, 0xa7, 0x00 }, { 0x00bb, 0xaf, 0x00 },
206 	{ 0x00bc, 0xac, 0x00 }, { 0x00bd, 0xab, 0x00 },
207 	{ 0x00bf, 0xa8, 0x00 }, { 0x00c4, 0x8e, 0x01 },
208 	{ 0x00c6, 0x92, 0x00 }, { 0x00c7, 0x80, 0x00 },
209 	{ 0x00c9, 0x90, 0x00 }, { 0x00d1, 0xa5, 0x00 },
210 	{ 0x00d6, 0x99, 0x00 }, { 0x00dc, 0x9a, 0x00 },
211 	{ 0x00df, 0xe1, 0x00 }, { 0x00e0, 0x85, 0x00 },
212 	{ 0x00e1, 0xa0, 0x00 }, { 0x00e2, 0x83, 0x00 },
213 	{ 0x00e4, 0x84, 0x00 }, { 0x00e5, 0x86, 0x00 },
214 	{ 0x00e6, 0x91, 0x00 }, { 0x00e7, 0x87, 0x00 },
215 	{ 0x00e8, 0x8a, 0x00 }, { 0x00e9, 0x82, 0x00 },
216 	{ 0x00ea, 0x88, 0x01 }, { 0x00ec, 0x8d, 0x00 },
217 	{ 0x00ed, 0xa1, 0x00 }, { 0x00ee, 0x8c, 0x00 },
218 	{ 0x00ef, 0x8b, 0x00 }, { 0x00f0, 0xeb, 0x00 },
219 	{ 0x00f1, 0xa4, 0x00 }, { 0x00f2, 0x95, 0x00 },
220 	{ 0x00f3, 0xa2, 0x00 }, { 0x00f4, 0x93, 0x00 },
221 	{ 0x00f6, 0x94, 0x00 }, { 0x00f7, 0xf6, 0x00 },
222 	{ 0x00f8, 0xed, 0x00 }, { 0x00f9, 0x97, 0x00 },
223 	{ 0x00fa, 0xa3, 0x00 }, { 0x00fb, 0x96, 0x00 },
224 	{ 0x00fc, 0x81, 0x00 }, { 0x00ff, 0x98, 0x00 },
225 	{ 0x0192, 0x9f, 0x00 }, { 0x0393, 0xe2, 0x00 },
226 	{ 0x0398, 0xe9, 0x00 }, { 0x03a3, 0xe4, 0x00 },
227 	{ 0x03a6, 0xe8, 0x00 }, { 0x03a9, 0xea, 0x00 },
228 	{ 0x03b1, 0xe0, 0x01 }, { 0x03b4, 0xeb, 0x00 },
229 	{ 0x03b5, 0xee, 0x00 }, { 0x03bc, 0xe6, 0x00 },
230 	{ 0x03c0, 0xe3, 0x00 }, { 0x03c3, 0xe5, 0x00 },
231 	{ 0x03c4, 0xe7, 0x00 }, { 0x03c6, 0xed, 0x00 },
232 	{ 0x03d5, 0xed, 0x00 }, { 0x2010, 0x2d, 0x00 },
233 	{ 0x2014, 0x2d, 0x00 }, { 0x2018, 0x60, 0x00 },
234 	{ 0x2019, 0x27, 0x00 }, { 0x201c, 0x22, 0x00 },
235 	{ 0x201d, 0x22, 0x00 }, { 0x2022, 0x07, 0x00 },
236 	{ 0x203c, 0x13, 0x00 }, { 0x207f, 0xfc, 0x00 },
237 	{ 0x20a7, 0x9e, 0x00 }, { 0x20ac, 0xee, 0x00 },
238 	{ 0x2126, 0xea, 0x00 }, { 0x2190, 0x1b, 0x00 },
239 	{ 0x2191, 0x18, 0x00 }, { 0x2192, 0x1a, 0x00 },
240 	{ 0x2193, 0x19, 0x00 }, { 0x2194, 0x1d, 0x00 },
241 	{ 0x2195, 0x12, 0x00 }, { 0x21a8, 0x17, 0x00 },
242 	{ 0x2202, 0xeb, 0x00 }, { 0x2208, 0xee, 0x00 },
243 	{ 0x2211, 0xe4, 0x00 }, { 0x2212, 0x2d, 0x00 },
244 	{ 0x2219, 0xf9, 0x00 }, { 0x221a, 0xfb, 0x00 },
245 	{ 0x221e, 0xec, 0x00 }, { 0x221f, 0x1c, 0x00 },
246 	{ 0x2229, 0xef, 0x00 }, { 0x2248, 0xf7, 0x00 },
247 	{ 0x2261, 0xf0, 0x00 }, { 0x2264, 0xf3, 0x00 },
248 	{ 0x2265, 0xf2, 0x00 }, { 0x2302, 0x7f, 0x00 },
249 	{ 0x2310, 0xa9, 0x00 }, { 0x2320, 0xf4, 0x00 },
250 	{ 0x2321, 0xf5, 0x00 }, { 0x2500, 0xc4, 0x00 },
251 	{ 0x2502, 0xb3, 0x00 }, { 0x250c, 0xda, 0x00 },
252 	{ 0x2510, 0xbf, 0x00 }, { 0x2514, 0xc0, 0x00 },
253 	{ 0x2518, 0xd9, 0x00 }, { 0x251c, 0xc3, 0x00 },
254 	{ 0x2524, 0xb4, 0x00 }, { 0x252c, 0xc2, 0x00 },
255 	{ 0x2534, 0xc1, 0x00 }, { 0x253c, 0xc5, 0x00 },
256 	{ 0x2550, 0xcd, 0x00 }, { 0x2551, 0xba, 0x00 },
257 	{ 0x2552, 0xd5, 0x00 }, { 0x2553, 0xd6, 0x00 },
258 	{ 0x2554, 0xc9, 0x00 }, { 0x2555, 0xb8, 0x00 },
259 	{ 0x2556, 0xb7, 0x00 }, { 0x2557, 0xbb, 0x00 },
260 	{ 0x2558, 0xd4, 0x00 }, { 0x2559, 0xd3, 0x00 },
261 	{ 0x255a, 0xc8, 0x00 }, { 0x255b, 0xbe, 0x00 },
262 	{ 0x255c, 0xbd, 0x00 }, { 0x255d, 0xbc, 0x00 },
263 	{ 0x255e, 0xc6, 0x01 }, { 0x2560, 0xcc, 0x00 },
264 	{ 0x2561, 0xb5, 0x00 }, { 0x2562, 0xb6, 0x00 },
265 	{ 0x2563, 0xb9, 0x00 }, { 0x2564, 0xd1, 0x01 },
266 	{ 0x2566, 0xcb, 0x00 }, { 0x2567, 0xcf, 0x00 },
267 	{ 0x2568, 0xd0, 0x00 }, { 0x2569, 0xca, 0x00 },
268 	{ 0x256a, 0xd8, 0x00 }, { 0x256b, 0xd7, 0x00 },
269 	{ 0x256c, 0xce, 0x00 }, { 0x2580, 0xdf, 0x00 },
270 	{ 0x2584, 0xdc, 0x00 }, { 0x2588, 0xdb, 0x00 },
271 	{ 0x258c, 0xdd, 0x00 }, { 0x2590, 0xde, 0x00 },
272 	{ 0x2591, 0xb0, 0x02 }, { 0x25a0, 0xfe, 0x00 },
273 	{ 0x25ac, 0x16, 0x00 }, { 0x25b2, 0x1e, 0x00 },
274 	{ 0x25ba, 0x10, 0x00 }, { 0x25bc, 0x1f, 0x00 },
275 	{ 0x25c4, 0x11, 0x00 }, { 0x25cb, 0x09, 0x00 },
276 	{ 0x25d8, 0x08, 0x00 }, { 0x25d9, 0x0a, 0x00 },
277 	{ 0x263a, 0x01, 0x01 }, { 0x263c, 0x0f, 0x00 },
278 	{ 0x2640, 0x0c, 0x00 }, { 0x2642, 0x0b, 0x00 },
279 	{ 0x2660, 0x06, 0x00 }, { 0x2663, 0x05, 0x00 },
280 	{ 0x2665, 0x03, 0x01 }, { 0x266a, 0x0d, 0x00 },
281 	{ 0x266c, 0x0e, 0x00 }
282 };
283 
284 static uint8_t
285 vga_get_cp437(teken_char_t c)
286 {
287 	int min, mid, max;
288 
289 	min = 0;
290 	max = (sizeof(cp437table) / sizeof(struct unicp437)) - 1;
291 
292 	if (c < cp437table[0].unicode_base ||
293 	    c > cp437table[max].unicode_base + cp437table[max].length)
294 		return ('?');
295 
296 	while (max >= min) {
297 		mid = (min + max) / 2;
298 		if (c < cp437table[mid].unicode_base)
299 			max = mid - 1;
300 		else if (c > cp437table[mid].unicode_base +
301 		    cp437table[mid].length)
302 			min = mid + 1;
303 		else
304 			return (c - cp437table[mid].unicode_base +
305 			    cp437table[mid].cp437_base);
306 	}
307 
308 	return ('?');
309 }
310 
311 static void
312 vidc_text_printchar(teken_gfx_t *state, const teken_pos_t *p)
313 {
314 	int idx;
315 	uint8_t attr;
316 	struct text_pixel *px;
317 	teken_color_t fg, bg, tmp;
318 	struct cgatext {
319 		uint8_t ch;
320 		uint8_t attr;
321 	} *addr;
322 
323 	idx = p->tp_col + p->tp_row * state->tg_tp.tp_col;
324 	px = &screen_buffer[idx];
325 	fg = teken_256to16(px->a.ta_fgcolor);
326 	bg = teken_256to16(px->a.ta_bgcolor);
327 	if (px->a.ta_format & TF_BOLD)
328 		fg |= TC_LIGHT;
329 	if (px->a.ta_format & TF_BLINK)
330 		bg |= TC_LIGHT;
331 
332 	if (px->a.ta_format & TF_REVERSE) {
333 		tmp = fg;
334 		fg = bg;
335 		bg = tmp;
336 	}
337 
338 	attr = (cmap[bg & 0xf] << 4) | cmap[fg & 0xf];
339 	addr = (struct cgatext *)vgatext;
340 	addr[idx].ch = vga_get_cp437(px->c);
341 	addr[idx].attr = attr;
342 }
343 
344 static void
345 vidc_text_putchar(void *s, const teken_pos_t *p, teken_char_t c,
346     const teken_attr_t *a)
347 {
348 	teken_gfx_t *state = s;
349 	int attr, idx;
350 
351 	idx = p->tp_col + p->tp_row * state->tg_tp.tp_col;
352 	if (idx >= state->tg_tp.tp_col * state->tg_tp.tp_row)
353 		return;
354 
355 	screen_buffer[idx].c = c;
356 	screen_buffer[idx].a = *a;
357 
358 	vidc_text_printchar(state, p);
359 }
360 
361 static void
362 vidc_text_fill(void *arg, const teken_rect_t *r, teken_char_t c,
363     const teken_attr_t *a)
364 {
365 	teken_gfx_t *state = arg;
366 	teken_pos_t p;
367 	teken_unit_t row, col;
368 
369 	vidc_text_get_cursor(&row, &col);
370 	vidc_text_set_cursor(row, col, false);
371 	for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row;
372 	    p.tp_row++)
373 		for (p.tp_col = r->tr_begin.tp_col;
374 		    p.tp_col < r->tr_end.tp_col; p.tp_col++)
375 			vidc_text_putchar(state, &p, c, a);
376 	vidc_text_set_cursor(row, col, true);
377 }
378 
379 static void
380 vidc_text_copy(void *ptr, const teken_rect_t *r, const teken_pos_t *p)
381 {
382 	teken_gfx_t *state = ptr;
383 	int srow, drow;
384 	int nrow, ncol, x, y; /* Has to be signed - >= 0 comparison */
385 	teken_pos_t d, s;
386 	teken_unit_t row, col;
387 
388 	/*
389 	 * Copying is a little tricky. We must make sure we do it in
390 	 * correct order, to make sure we don't overwrite our own data.
391 	 */
392 
393 	nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
394 	ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
395 
396 	vidc_text_get_cursor(&row, &col);
397 	vidc_text_set_cursor(row, col, false);
398 	if (p->tp_row < r->tr_begin.tp_row) {
399 		/* Copy from bottom to top. */
400 		for (y = 0; y < nrow; y++) {
401 			d.tp_row = p->tp_row + y;
402 			s.tp_row = r->tr_begin.tp_row + y;
403 			drow = d.tp_row * state->tg_tp.tp_col;
404 			srow = s.tp_row * state->tg_tp.tp_col;
405 			for (x = 0; x < ncol; x++) {
406 				d.tp_col = p->tp_col + x;
407 				s.tp_col = r->tr_begin.tp_col + x;
408 
409 				if (!is_same_pixel(
410 				    &screen_buffer[d.tp_col + drow],
411 				    &screen_buffer[s.tp_col + srow])) {
412 					screen_buffer[d.tp_col + drow] =
413 					    screen_buffer[s.tp_col + srow];
414 					vidc_text_printchar(state, &d);
415 				}
416 			}
417 		}
418 	} else {
419 		/* Copy from top to bottom. */
420 		if (p->tp_col < r->tr_begin.tp_col) {
421 			/* Copy from right to left. */
422 			for (y = nrow - 1; y >= 0; y--) {
423 				d.tp_row = p->tp_row + y;
424 				s.tp_row = r->tr_begin.tp_row + y;
425 				drow = d.tp_row * state->tg_tp.tp_col;
426 				srow = s.tp_row * state->tg_tp.tp_col;
427 				for (x = 0; x < ncol; x++) {
428 					d.tp_col = p->tp_col + x;
429 					s.tp_col = r->tr_begin.tp_col + x;
430 
431 					if (!is_same_pixel(
432 					    &screen_buffer[d.tp_col + drow],
433 					    &screen_buffer[s.tp_col + srow])) {
434 						screen_buffer[d.tp_col + drow] =
435 						    screen_buffer[s.tp_col + srow];
436 						vidc_text_printchar(state, &d);
437 					}
438 				}
439 			}
440 		} else {
441 			/* Copy from left to right. */
442 			for (y = nrow - 1; y >= 0; y--) {
443 				d.tp_row = p->tp_row + y;
444 				s.tp_row = r->tr_begin.tp_row + y;
445 				drow = d.tp_row * state->tg_tp.tp_col;
446 				srow = s.tp_row * state->tg_tp.tp_col;
447 				for (x = ncol - 1; x >= 0; x--) {
448 					d.tp_col = p->tp_col + x;
449 					s.tp_col = r->tr_begin.tp_col + x;
450 
451 					if (!is_same_pixel(
452 					    &screen_buffer[d.tp_col + drow],
453 					    &screen_buffer[s.tp_col + srow])) {
454 						screen_buffer[d.tp_col + drow] =
455 						    screen_buffer[s.tp_col + srow];
456 						vidc_text_printchar(state, &d);
457 					}
458 				}
459 			}
460 		}
461 	}
462 	vidc_text_set_cursor(row, col, true);
463 }
464 
465 static void
466 vidc_text_param(void *arg, int cmd, unsigned int value)
467 {
468 	teken_gfx_t *state = arg;
469 	teken_unit_t row, col;
470 
471 	switch (cmd) {
472 	case TP_SETLOCALCURSOR:
473 		/*
474 		 * 0 means normal (usually block), 1 means hidden, and
475 		 * 2 means blinking (always block) for compatibility with
476 		 * syscons.  We don't support any changes except hiding,
477 		 * so must map 2 to 0.
478 		 */
479 		value = (value == 1) ? 0 : 1;
480 		/* FALLTHROUGH */
481 	case TP_SHOWCURSOR:
482 		vidc_text_get_cursor(&row, &col);
483 		if (value != 0) {
484 			vidc_text_set_cursor(row, col, true);
485 			state->tg_cursor_visible = true;
486 		} else {
487 			vidc_text_set_cursor(row, col, false);
488 			state->tg_cursor_visible = false;
489 		}
490 		break;
491 	default:
492 		/* Not yet implemented */
493 		break;
494 	}
495 }
496 
497 /*
498  * Not implemented.
499  */
500 static void
501 vidc_cons_respond(void *s __unused, const void *buf __unused,
502     size_t len __unused)
503 {
504 }
505 
506 static void
507 vidc_probe(struct console *cp)
508 {
509 
510     /* look for a keyboard */
511 #if KEYBOARD_PROBE
512     if (probe_keyboard())
513 #endif
514     {
515 
516 	cp->c_flags |= C_PRESENTIN;
517     }
518 
519     /* XXX for now, always assume we can do BIOS screen output */
520     cp->c_flags |= C_PRESENTOUT;
521 }
522 
523 static bool
524 color_name_to_teken(const char *name, int *val)
525 {
526 	int light = 0;
527 	if (strncasecmp(name, "light", 5) == 0) {
528 		name += 5;
529 		light = TC_LIGHT;
530 	} else if (strncasecmp(name, "bright", 6) == 0) {
531 		name += 6;
532 		light = TC_LIGHT;
533 	}
534 	if (strcasecmp(name, "black") == 0) {
535 		*val = TC_BLACK | light;
536 		return (true);
537 	}
538 	if (strcasecmp(name, "red") == 0) {
539 		*val = TC_RED | light;
540 		return (true);
541 	}
542 	if (strcasecmp(name, "green") == 0) {
543 		*val = TC_GREEN | light;
544 		return (true);
545 	}
546 	if (strcasecmp(name, "yellow") == 0 || strcasecmp(name, "brown") == 0) {
547 		*val = TC_YELLOW | light;
548 		return (true);
549 	}
550 	if (strcasecmp(name, "blue") == 0) {
551 		*val = TC_BLUE | light;
552 		return (true);
553 	}
554 	if (strcasecmp(name, "magenta") == 0) {
555 		*val = TC_MAGENTA | light;
556 		return (true);
557 	}
558 	if (strcasecmp(name, "cyan") == 0) {
559 		*val = TC_CYAN | light;
560 		return (true);
561 	}
562 	if (strcasecmp(name, "white") == 0) {
563 		*val = TC_WHITE | light;
564 		return (true);
565 	}
566 	return (false);
567 }
568 
569 static int
570 vidc_set_colors(struct env_var *ev, int flags, const void *value)
571 {
572 	int val = 0;
573 	char buf[3];
574 	const void *evalue;
575 	const teken_attr_t *ap;
576 	teken_attr_t a;
577 
578 	if (value == NULL)
579 		return (CMD_OK);
580 
581 	if (color_name_to_teken(value, &val)) {
582 		snprintf(buf, sizeof (buf), "%d", val);
583 		evalue = buf;
584 	} else {
585 		char *end;
586 		long lval;
587 
588 		errno = 0;
589 		lval = strtol(value, &end, 0);
590 		if (errno != 0 || *end != '\0' || lval < 0 || lval > 15) {
591 			printf("Allowed values are either ansi color name or "
592 			    "number from range [0-15].\n");
593 			return (CMD_OK);
594 		}
595 		val = (int)lval;
596 		evalue = value;
597 	}
598 
599 	ap = teken_get_defattr(&gfx_state.tg_teken);
600 	a = *ap;
601 	if (strcmp(ev->ev_name, "teken.fg_color") == 0) {
602 		/* is it already set? */
603 		if (ap->ta_fgcolor == val)
604 			return (CMD_OK);
605 		a.ta_fgcolor = val;
606 	}
607 	if (strcmp(ev->ev_name, "teken.bg_color") == 0) {
608 		/* is it already set? */
609 		if (ap->ta_bgcolor == val)
610 			return (CMD_OK);
611 		a.ta_bgcolor = val;
612 	}
613 
614 	/* Improve visibility */
615 	if (a.ta_bgcolor == TC_WHITE)
616 		a.ta_bgcolor |= TC_LIGHT;
617 
618 	teken_set_defattr(&gfx_state.tg_teken, &a);
619 	cons_draw_frame(&a);
620 	env_setenv(ev->ev_name, flags | EV_NOHOOK, evalue, NULL, NULL);
621 	teken_input(&gfx_state.tg_teken, "\e[2J", 4);
622 
623 	return (CMD_OK);
624 }
625 
626 static int
627 env_screen_nounset(struct env_var *ev __unused)
628 {
629 	if (gfx_state.tg_fb_type == FB_TEXT)
630 		return (0);
631 	return (EPERM);
632 }
633 
634 static int
635 vidc_load_palette(void)
636 {
637 	int i, roff, goff, boff, rc;
638 
639 	if (pe8 == NULL)
640 		pe8 = calloc(sizeof(*pe8), NCMAP);
641 	if (pe8 == NULL)
642 		return (ENOMEM);
643 
644 	/* Generate VGA colors */
645 	roff = ffs(gfx_state.tg_fb.fb_mask_red) - 1;
646 	goff = ffs(gfx_state.tg_fb.fb_mask_green) - 1;
647 	boff = ffs(gfx_state.tg_fb.fb_mask_blue) - 1;
648 	rc = generate_cons_palette((uint32_t *)pe8, COLOR_FORMAT_RGB,
649 	    gfx_state.tg_fb.fb_mask_red >> roff, roff,
650 	    gfx_state.tg_fb.fb_mask_green >> goff, goff,
651 	    gfx_state.tg_fb.fb_mask_blue >> boff, boff);
652 
653 	if (rc == 0) {
654 		for (i = 0; i < NCMAP; i++) {
655 			int idx;
656 
657 			if (i < NCOLORS)
658 				idx = cons_to_vga_colors[i];
659 			else
660 				idx = i;
661 
662 			rc = vbe_set_palette(&pe8[i], idx);
663 			if (rc != 0)
664 				break;
665 		}
666 	}
667 	return (rc);
668 }
669 
670 static void
671 cons_draw_frame(teken_attr_t *a)
672 {
673 	teken_attr_t attr = *a;
674 	teken_color_t fg = a->ta_fgcolor;
675 
676 	attr.ta_fgcolor = attr.ta_bgcolor;
677 	teken_set_defattr(&gfx_state.tg_teken, &attr);
678 
679 	gfx_fb_drawrect(0, 0, gfx_state.tg_fb.fb_width,
680 	    gfx_state.tg_origin.tp_row, 1);
681 	gfx_fb_drawrect(0,
682 	    gfx_state.tg_fb.fb_height - gfx_state.tg_origin.tp_row - 1,
683 	    gfx_state.tg_fb.fb_width, gfx_state.tg_fb.fb_height, 1);
684 	gfx_fb_drawrect(0, gfx_state.tg_origin.tp_row,
685 	    gfx_state.tg_origin.tp_col,
686 	    gfx_state.tg_fb.fb_height - gfx_state.tg_origin.tp_row - 1, 1);
687 	gfx_fb_drawrect(
688 	    gfx_state.tg_fb.fb_width - gfx_state.tg_origin.tp_col - 1,
689 	    gfx_state.tg_origin.tp_row, gfx_state.tg_fb.fb_width,
690 	    gfx_state.tg_fb.fb_height, 1);
691 
692 	attr.ta_fgcolor = fg;
693 	teken_set_defattr(&gfx_state.tg_teken, &attr);
694 }
695 
696 /*
697  * Binary searchable table for CP437 to Unicode conversion.
698  */
699 struct cp437uni {
700 	uint8_t		cp437_base;
701 	uint16_t	unicode_base;
702 	uint8_t		length;
703 };
704 
705 static const struct cp437uni cp437unitable[] = {
706 	{   0, 0x0000, 0 }, {   1, 0x263A, 1 }, {   3, 0x2665, 1 },
707 	{   5, 0x2663, 0 }, {   6, 0x2660, 0 }, {   7, 0x2022, 0 },
708 	{   8, 0x25D8, 0 }, {   9, 0x25CB, 0 }, {  10, 0x25D9, 0 },
709 	{  11, 0x2642, 0 }, {  12, 0x2640, 0 }, {  13, 0x266A, 1 },
710 	{  15, 0x263C, 0 }, {  16, 0x25BA, 0 }, {  17, 0x25C4, 0 },
711 	{  18, 0x2195, 0 }, {  19, 0x203C, 0 }, {  20, 0x00B6, 0 },
712 	{  21, 0x00A7, 0 }, {  22, 0x25AC, 0 }, {  23, 0x21A8, 0 },
713 	{  24, 0x2191, 0 }, {  25, 0x2193, 0 }, {  26, 0x2192, 0 },
714 	{  27, 0x2190, 0 }, {  28, 0x221F, 0 }, {  29, 0x2194, 0 },
715 	{  30, 0x25B2, 0 }, {  31, 0x25BC, 0 }, {  32, 0x0020, 0x5e },
716 	{ 127, 0x2302, 0 }, { 128, 0x00C7, 0 }, { 129, 0x00FC, 0 },
717 	{ 130, 0x00E9, 0 }, { 131, 0x00E2, 0 }, { 132, 0x00E4, 0 },
718 	{ 133, 0x00E0, 0 }, { 134, 0x00E5, 0 }, { 135, 0x00E7, 0 },
719 	{ 136, 0x00EA, 1 }, { 138, 0x00E8, 0 }, { 139, 0x00EF, 0 },
720 	{ 140, 0x00EE, 0 }, { 141, 0x00EC, 0 }, { 142, 0x00C4, 1 },
721 	{ 144, 0x00C9, 0 }, { 145, 0x00E6, 0 }, { 146, 0x00C6, 0 },
722 	{ 147, 0x00F4, 0 }, { 148, 0x00F6, 0 }, { 149, 0x00F2, 0 },
723 	{ 150, 0x00FB, 0 }, { 151, 0x00F9, 0 }, { 152, 0x00FF, 0 },
724 	{ 153, 0x00D6, 0 }, { 154, 0x00DC, 0 }, { 155, 0x00A2, 1 },
725 	{ 157, 0x00A5, 0 }, { 158, 0x20A7, 0 }, { 159, 0x0192, 0 },
726 	{ 160, 0x00E1, 0 }, { 161, 0x00ED, 0 }, { 162, 0x00F3, 0 },
727 	{ 163, 0x00FA, 0 }, { 164, 0x00F1, 0 }, { 165, 0x00D1, 0 },
728 	{ 166, 0x00AA, 0 }, { 167, 0x00BA, 0 }, { 168, 0x00BF, 0 },
729 	{ 169, 0x2310, 0 }, { 170, 0x00AC, 0 }, { 171, 0x00BD, 0 },
730 	{ 172, 0x00BC, 0 }, { 173, 0x00A1, 0 }, { 174, 0x00AB, 0 },
731 	{ 175, 0x00BB, 0 }, { 176, 0x2591, 2 }, { 179, 0x2502, 0 },
732 	{ 180, 0x2524, 0 }, { 181, 0x2561, 1 }, { 183, 0x2556, 0 },
733 	{ 184, 0x2555, 0 }, { 185, 0x2563, 0 }, { 186, 0x2551, 0 },
734 	{ 187, 0x2557, 0 }, { 188, 0x255D, 0 }, { 189, 0x255C, 0 },
735 	{ 190, 0x255B, 0 }, { 191, 0x2510, 0 }, { 192, 0x2514, 0 },
736 	{ 193, 0x2534, 0 }, { 194, 0x252C, 0 }, { 195, 0x251C, 0 },
737 	{ 196, 0x2500, 0 }, { 197, 0x253C, 0 }, { 198, 0x255E, 1 },
738 	{ 200, 0x255A, 0 }, { 201, 0x2554, 0 }, { 202, 0x2569, 0 },
739 	{ 203, 0x2566, 0 }, { 204, 0x2560, 0 }, { 205, 0x2550, 0 },
740 	{ 206, 0x256C, 0 }, { 207, 0x2567, 1 }, { 209, 0x2564, 1 },
741 	{ 211, 0x2559, 0 }, { 212, 0x2558, 0 }, { 213, 0x2552, 1 },
742 	{ 215, 0x256B, 0 }, { 216, 0x256A, 0 }, { 217, 0x2518, 0 },
743 	{ 218, 0x250C, 0 }, { 219, 0x2588, 0 }, { 220, 0x2584, 0 },
744 	{ 221, 0x258C, 0 }, { 222, 0x2590, 0 }, { 223, 0x2580, 0 },
745 	{ 224, 0x03B1, 0 }, { 225, 0x00DF, 0 }, { 226, 0x0393, 0 },
746 	{ 227, 0x03C0, 0 }, { 228, 0x03A3, 0 }, { 229, 0x03C3, 0 },
747 	{ 230, 0x00B5, 0 }, { 231, 0x03C4, 0 }, { 232, 0x03A6, 0 },
748 	{ 233, 0x0398, 0 }, { 234, 0x03A9, 0 }, { 235, 0x03B4, 0 },
749 	{ 236, 0x221E, 0 }, { 237, 0x03C6, 0 }, { 238, 0x03B5, 0 },
750 	{ 239, 0x2229, 0 }, { 240, 0x2261, 0 }, { 241, 0x00B1, 0 },
751 	{ 242, 0x2265, 0 }, { 243, 0x2264, 0 }, { 244, 0x2320, 1 },
752 	{ 246, 0x00F7, 0 }, { 247, 0x2248, 0 }, { 248, 0x00B0, 0 },
753 	{ 249, 0x2219, 0 }, { 250, 0x00B7, 0 }, { 251, 0x221A, 0 },
754 	{ 252, 0x207F, 0 }, { 253, 0x00B2, 0 }, { 254, 0x25A0, 0 },
755 	{ 255, 0x00A0, 0 }
756 };
757 
758 static uint16_t
759 vga_cp437_to_uni(uint8_t c)
760 {
761 	int min, mid, max;
762 
763 	min = 0;
764 	max = (sizeof(cp437unitable) / sizeof(struct cp437uni)) - 1;
765 
766 	while (max >= min) {
767 		mid = (min + max) / 2;
768 		if (c < cp437unitable[mid].cp437_base)
769 			max = mid - 1;
770 		else if (c > cp437unitable[mid].cp437_base +
771 		    cp437unitable[mid].length)
772 			min = mid + 1;
773 		else
774 			return (c - cp437unitable[mid].cp437_base +
775 			    cp437unitable[mid].unicode_base);
776 	}
777 
778 	return ('?');
779 }
780 
781 /*
782  * install font for text mode
783  */
784 static void
785 vidc_install_font(void)
786 {
787 	uint8_t reg[7];
788 	const uint8_t *from;
789 	uint8_t volatile *to;
790 	uint16_t c;
791 	int i, j, s;
792 	int bpc, f_offset;
793 	teken_attr_t a = { 0 };
794 
795 	/* We can only program VGA registers. */
796 	if (!vbe_is_vga())
797 		return;
798 
799 	if (gfx_state.tg_fb_type != FB_TEXT)
800 		return;
801 
802 	/* Sync-reset the sequencer registers */
803 	vga_set_seq(VGA_REG_BASE, VGA_SEQ_RESET, VGA_SEQ_RST_NAR);
804 
805 	reg[0] = vga_get_seq(VGA_REG_BASE, VGA_SEQ_MAP_MASK);
806 	reg[1] = vga_get_seq(VGA_REG_BASE, VGA_SEQ_CLOCKING_MODE);
807 	reg[2] = vga_get_seq(VGA_REG_BASE, VGA_SEQ_MEMORY_MODE);
808 	reg[3] = vga_get_grc(VGA_REG_BASE, VGA_GC_READ_MAP_SELECT);
809 	reg[4] = vga_get_grc(VGA_REG_BASE, VGA_GC_MODE);
810 	reg[5] = vga_get_grc(VGA_REG_BASE, VGA_GC_MISCELLANEOUS);
811 	reg[6] = vga_get_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL);
812 
813 	/* Screen off */
814 	vga_set_seq(VGA_REG_BASE, VGA_SEQ_CLOCKING_MODE,
815 	    reg[1] | VGA_SEQ_CM_SO);
816 
817 	/*
818 	 * enable write to plane2, since fonts
819 	 * could only be loaded into plane2
820 	 */
821 	vga_set_seq(VGA_REG_BASE, VGA_SEQ_MAP_MASK, VGA_SEQ_MM_EM2);
822 	/*
823 	 * sequentially access data in the bit map being
824 	 * selected by MapMask register (index 0x02)
825 	 */
826 	vga_set_seq(VGA_REG_BASE, VGA_SEQ_MEMORY_MODE, 0x07);
827 	/* Sync-reset ended, and allow the sequencer to operate */
828 	vga_set_seq(VGA_REG_BASE, VGA_SEQ_RESET,
829 	    VGA_SEQ_RST_SR | VGA_SEQ_RST_NAR);
830 
831 	/*
832 	 * select plane 2 on Read Mode 0
833 	 */
834 	vga_set_grc(VGA_REG_BASE, VGA_GC_READ_MAP_SELECT, 0x02);
835 	/*
836 	 * system addresses sequentially access data, follow
837 	 * Memory Mode register bit 2 in the sequencer
838 	 */
839 	vga_set_grc(VGA_REG_BASE, VGA_GC_MODE, 0x00);
840 	/*
841 	 * set range of host memory addresses decoded by VGA
842 	 * hardware -- A0000h-BFFFFh (128K region)
843 	 */
844 	vga_set_grc(VGA_REG_BASE, VGA_GC_MISCELLANEOUS, 0x00);
845 
846 	/*
847 	 * This assumes 8x16 characters, which yield the traditional 80x25
848 	 * screen.
849 	 */
850 	bpc = 16;
851 	s = 0;	/* font slot, maybe should use tunable there. */
852 	f_offset = s * 8 * 1024;
853 	for (i = 0; i < 256; i++) {
854 		c = vga_cp437_to_uni(i);
855 		from = font_lookup(&gfx_state.tg_font, c, &a);
856 		to = (unsigned char *)ptov(VGA_MEM_BASE) + f_offset +
857 		    i * 0x20;
858 		for (j = 0; j < bpc; j++)
859 			*to++ = *from++;
860 	}
861 
862 	vga_set_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL, reg[6]);
863 
864 	/* Sync-reset the sequencer registers */
865 	vga_set_seq(VGA_REG_BASE, VGA_SEQ_RESET, VGA_SEQ_RST_NAR);
866 	vga_set_seq(VGA_REG_BASE, VGA_SEQ_MAP_MASK, reg[0]);
867 	vga_set_seq(VGA_REG_BASE, VGA_SEQ_MEMORY_MODE, reg[2]);
868 	/* Sync-reset ended, and allow the sequencer to operate */
869 	vga_set_seq(VGA_REG_BASE, VGA_SEQ_RESET,
870 	    VGA_SEQ_RST_SR | VGA_SEQ_RST_NAR);
871 
872 	/* restore graphic registers */
873 	vga_set_grc(VGA_REG_BASE, VGA_GC_READ_MAP_SELECT, reg[3]);
874 	vga_set_grc(VGA_REG_BASE, VGA_GC_MODE, reg[4]);
875 	vga_set_grc(VGA_REG_BASE, VGA_GC_MISCELLANEOUS, (reg[5] & 0x03) | 0x0c);
876 
877 	/* Screen on */
878 	vga_set_seq(VGA_REG_BASE, VGA_SEQ_CLOCKING_MODE, reg[1] & 0xdf);
879 }
880 
881 bool
882 cons_update_mode(bool use_gfx_mode)
883 {
884 	const teken_attr_t *a;
885 	teken_attr_t attr;
886 	char env[10], *ptr;
887 	int format, roff, goff, boff;
888 
889 	/* vidc_init() is not called yet. */
890 	if (!vidc_started)
891 		return (false);
892 
893 	gfx_state.tg_tp.tp_row = TEXT_ROWS;
894 	gfx_state.tg_tp.tp_col = TEXT_COLS;
895 
896 	if (use_gfx_mode) {
897 		setup_font(&gfx_state, gfx_state.tg_fb.fb_height,
898 		    gfx_state.tg_fb.fb_width);
899 		/* Point of origin in pixels. */
900 		gfx_state.tg_origin.tp_row = (gfx_state.tg_fb.fb_height -
901 		    (gfx_state.tg_tp.tp_row * gfx_state.tg_font.vf_height)) / 2;
902 		gfx_state.tg_origin.tp_col = (gfx_state.tg_fb.fb_width -
903 		    (gfx_state.tg_tp.tp_col * gfx_state.tg_font.vf_width)) / 2;
904 
905 		gfx_state.tg_glyph_size = gfx_state.tg_font.vf_height *
906 		    gfx_state.tg_font.vf_width * 4;
907 		free(gfx_state.tg_glyph);
908 		gfx_state.tg_glyph = malloc(gfx_state.tg_glyph_size);
909 		if (gfx_state.tg_glyph == NULL)
910 			return (false);
911 		gfx_state.tg_functions = &tfx;
912 		snprintf(env, sizeof (env), "%d", gfx_state.tg_fb.fb_height);
913 		env_setenv("screen.height", EV_VOLATILE | EV_NOHOOK, env,
914 		    env_noset, env_screen_nounset);
915 		snprintf(env, sizeof (env), "%d", gfx_state.tg_fb.fb_width);
916 		env_setenv("screen.width", EV_VOLATILE | EV_NOHOOK, env,
917 		    env_noset, env_screen_nounset);
918 		snprintf(env, sizeof (env), "%d", gfx_state.tg_fb.fb_bpp);
919 		env_setenv("screen.depth", EV_VOLATILE | EV_NOHOOK, env,
920 		    env_noset, env_screen_nounset);
921 	} else {
922 		/* Trigger loading of 8x16 font. */
923 		setup_font(&gfx_state,
924 		    16 * gfx_state.tg_fb.fb_height,
925 		    8 * gfx_state.tg_fb.fb_width);
926 		gfx_state.tg_functions = &tf;
927 		/* ensure the following are not set for text mode */
928 		unsetenv("screen.height");
929 		unsetenv("screen.width");
930 		unsetenv("screen.depth");
931 		vidc_install_font();
932 	}
933 
934 	free(screen_buffer);
935 	screen_buffer = malloc(gfx_state.tg_tp.tp_row * gfx_state.tg_tp.tp_col *
936 	    sizeof(*screen_buffer));
937 	if (screen_buffer == NULL)
938 		return (false);
939 
940 	teken_init(&gfx_state.tg_teken, gfx_state.tg_functions, &gfx_state);
941 
942 	if (gfx_state.tg_ctype == CT_INDEXED)
943 		format = COLOR_FORMAT_VGA;
944 	else
945 		format = COLOR_FORMAT_RGB;
946 
947 	roff = ffs(gfx_state.tg_fb.fb_mask_red) - 1;
948 	goff = ffs(gfx_state.tg_fb.fb_mask_green) - 1;
949 	boff = ffs(gfx_state.tg_fb.fb_mask_blue) - 1;
950 	(void) generate_cons_palette(cmap, format,
951 	    gfx_state.tg_fb.fb_mask_red >> roff, roff,
952 	    gfx_state.tg_fb.fb_mask_green >> goff, goff,
953 	    gfx_state.tg_fb.fb_mask_blue >> boff, boff);
954 
955 	if (gfx_state.tg_ctype == CT_INDEXED && use_gfx_mode)
956 		vidc_load_palette();
957 
958 	teken_set_winsize(&gfx_state.tg_teken, &gfx_state.tg_tp);
959 	a = teken_get_defattr(&gfx_state.tg_teken);
960 	attr = *a;
961 
962 	/*
963 	 * On first run, we set up the vidc_set_colors()
964 	 * callback. If the env is already set, we
965 	 * pick up fg and bg color values from the environment.
966 	 */
967 	ptr = getenv("teken.fg_color");
968 	if (ptr != NULL) {
969 		attr.ta_fgcolor = strtol(ptr, NULL, 10);
970 		ptr = getenv("teken.bg_color");
971 		attr.ta_bgcolor = strtol(ptr, NULL, 10);
972 
973 		teken_set_defattr(&gfx_state.tg_teken, &attr);
974 	} else {
975 		snprintf(env, sizeof(env), "%d", attr.ta_fgcolor);
976 		env_setenv("teken.fg_color", EV_VOLATILE, env,
977 		    vidc_set_colors, env_nounset);
978 		snprintf(env, sizeof(env), "%d", attr.ta_bgcolor);
979 		env_setenv("teken.bg_color", EV_VOLATILE, env,
980 		    vidc_set_colors, env_nounset);
981 	}
982 
983 	/* Improve visibility */
984 	if (attr.ta_bgcolor == TC_WHITE)
985 		attr.ta_bgcolor |= TC_LIGHT;
986 	teken_set_defattr(&gfx_state.tg_teken, &attr);
987 
988 	snprintf(env, sizeof (env), "%u", (unsigned)gfx_state.tg_tp.tp_row);
989 	setenv("LINES", env, 1);
990 	snprintf(env, sizeof (env), "%u", (unsigned)gfx_state.tg_tp.tp_col);
991 	setenv("COLUMNS", env, 1);
992 
993 	/* Draw frame around terminal area. */
994 	cons_draw_frame(&attr);
995 	/* Erase display, this will also fill our screen buffer. */
996 	teken_input(&gfx_state.tg_teken, "\e[2J", 4);
997 	gfx_state.tg_functions->tf_param(&gfx_state, TP_SHOWCURSOR, 1);
998 
999         return (true);
1000 }
1001 
1002 static int
1003 vidc_init(int arg)
1004 {
1005 	const teken_attr_t *a;
1006 	int val;
1007 	char env[8];
1008 
1009 	if (vidc_started && arg == 0)
1010 		return (0);
1011 
1012 	vidc_started = true;
1013 	vbe_init();
1014 
1015 	/*
1016 	 * Check Miscellaneous Output Register (Read at 3CCh, Write at 3C2h)
1017 	 * for bit 1 (Input/Output Address Select), which means
1018 	 * color/graphics adapter.
1019 	 */
1020 	if (vga_get_reg(VGA_REG_BASE, VGA_GEN_MISC_OUTPUT_R) & VGA_GEN_MO_IOA)
1021 		vgatext = (uint16_t *)PTOV(VGA_TXT_BASE);
1022 	else
1023 		vgatext = (uint16_t *)PTOV(VGA_MEM_BASE + VGA_MEM_SIZE);
1024 
1025         /* set 16bit colors */
1026         val = vga_get_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL);
1027         val &= ~VGA_AC_MC_BI;
1028         val &= ~VGA_AC_MC_ELG;
1029         vga_set_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL, val);
1030 
1031 #if defined(FRAMEBUFFER_MODE)
1032 	val = vbe_default_mode();
1033 	/* if val is not legal VBE mode, use text mode */
1034 	if (VBE_VALID_MODE(val)) {
1035 		if (vbe_set_mode(val) != 0)
1036 			bios_set_text_mode(VGA_TEXT_MODE);
1037 	}
1038 #endif
1039 
1040 	gfx_framework_init();
1041 
1042 	if (!cons_update_mode(VBE_VALID_MODE(vbe_get_mode())))
1043 		return (1);
1044 
1045 	for (int i = 0; i < 10 && vidc_ischar(); i++)
1046 		(void) vidc_getchar();
1047 
1048 	return (0);	/* XXX reinit? */
1049 }
1050 
1051 void
1052 vidc_biosputchar(int c)
1053 {
1054 
1055     v86.ctl = 0;
1056     v86.addr = 0x10;
1057     v86.eax = 0xe00 | (c & 0xff);
1058     v86.ebx = 0x7;
1059     v86int();
1060 }
1061 
1062 static void
1063 vidc_putchar(int c)
1064 {
1065 	unsigned char ch = c;
1066 
1067 	if (screen_buffer != NULL)
1068 		teken_input(&gfx_state.tg_teken, &ch, sizeof (ch));
1069 	else
1070 		vidc_biosputchar(c);
1071 }
1072 
1073 static int
1074 vidc_getchar(void)
1075 {
1076 	int i, c;
1077 
1078 	for (i = 0; i < KEYBUFSZ; i++) {
1079 		if (keybuf[i] != 0) {
1080 			c = keybuf[i];
1081 			keybuf[i] = 0;
1082 			return (c);
1083 		}
1084 	}
1085 
1086 	if (vidc_ischar()) {
1087 		v86.ctl = 0;
1088 		v86.addr = 0x16;
1089 		v86.eax = 0x0;
1090 		v86int();
1091 		if ((v86.eax & 0xff) != 0) {
1092 			return (v86.eax & 0xff);
1093 		}
1094 
1095 		/* extended keys */
1096 		switch (v86.eax & 0xff00) {
1097 		case 0x4800:	/* up */
1098 			keybuf[0] = '[';
1099 			keybuf[1] = 'A';
1100 			return (0x1b);	/* esc */
1101 		case 0x4b00:	/* left */
1102 			keybuf[0] = '[';
1103 			keybuf[1] = 'D';
1104 			return (0x1b);	/* esc */
1105 		case 0x4d00:	/* right */
1106 			keybuf[0] = '[';
1107 			keybuf[1] = 'C';
1108 			return (0x1b);	/* esc */
1109 		case 0x5000:	/* down */
1110 			keybuf[0] = '[';
1111 			keybuf[1] = 'B';
1112 			return (0x1b);	/* esc */
1113 		default:
1114 			return (-1);
1115 		}
1116 	} else {
1117 		return (-1);
1118 	}
1119 }
1120 
1121 static int
1122 vidc_ischar(void)
1123 {
1124 	int i;
1125 
1126 	for (i = 0; i < KEYBUFSZ; i++) {
1127 		if (keybuf[i] != 0) {
1128 			return (1);
1129 		}
1130 	}
1131 
1132 	v86.ctl = V86_FLAGS;
1133 	v86.addr = 0x16;
1134 	v86.eax = 0x100;
1135 	v86int();
1136 	return (!V86_ZR(v86.efl));
1137 }
1138 
1139 #if KEYBOARD_PROBE
1140 
1141 #define	PROBE_MAXRETRY	5
1142 #define	PROBE_MAXWAIT	400
1143 #define	IO_DUMMY	0x84
1144 #define	IO_KBD		0x060		/* 8042 Keyboard */
1145 
1146 /* selected defines from kbdio.h */
1147 #define	KBD_STATUS_PORT 	4	/* status port, read */
1148 #define	KBD_DATA_PORT		0	/* data port, read/write
1149 					 * also used as keyboard command
1150 					 * and mouse command port
1151 					 */
1152 #define	KBDC_ECHO		0x00ee
1153 #define	KBDS_ANY_BUFFER_FULL	0x0001
1154 #define	KBDS_INPUT_BUFFER_FULL	0x0002
1155 #define	KBD_ECHO		0x00ee
1156 
1157 /* 7 microsec delay necessary for some keyboard controllers */
1158 static void
1159 delay7(void)
1160 {
1161 	/*
1162 	 * I know this is broken, but no timer is available yet at this stage...
1163 	 * See also comments in `delay1ms()'.
1164 	 */
1165 	inb(IO_DUMMY); inb(IO_DUMMY);
1166 	inb(IO_DUMMY); inb(IO_DUMMY);
1167 	inb(IO_DUMMY); inb(IO_DUMMY);
1168 }
1169 
1170 /*
1171  * This routine uses an inb to an unused port, the time to execute that
1172  * inb is approximately 1.25uS.  This value is pretty constant across
1173  * all CPU's and all buses, with the exception of some PCI implentations
1174  * that do not forward this I/O address to the ISA bus as they know it
1175  * is not a valid ISA bus address, those machines execute this inb in
1176  * 60 nS :-(.
1177  *
1178  */
1179 static void
1180 delay1ms(void)
1181 {
1182 	int i = 800;
1183 	while (--i >= 0)
1184 		(void) inb(0x84);
1185 }
1186 
1187 /*
1188  * We use the presence/absence of a keyboard to determine whether the internal
1189  * console can be used for input.
1190  *
1191  * Perform a simple test on the keyboard; issue the ECHO command and see
1192  * if the right answer is returned. We don't do anything as drastic as
1193  * full keyboard reset; it will be too troublesome and take too much time.
1194  */
1195 static int
1196 probe_keyboard(void)
1197 {
1198 	int retry = PROBE_MAXRETRY;
1199 	int wait;
1200 	int i;
1201 
1202 	while (--retry >= 0) {
1203 		/* flush any noise */
1204 		while (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) {
1205 			delay7();
1206 			inb(IO_KBD + KBD_DATA_PORT);
1207 			delay1ms();
1208 		}
1209 
1210 		/* wait until the controller can accept a command */
1211 		for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
1212 			if (((i = inb(IO_KBD + KBD_STATUS_PORT))
1213 			    & (KBDS_INPUT_BUFFER_FULL | KBDS_ANY_BUFFER_FULL))
1214 			    == 0)
1215 				break;
1216 			if (i & KBDS_ANY_BUFFER_FULL) {
1217 				delay7();
1218 				inb(IO_KBD + KBD_DATA_PORT);
1219 			}
1220 			delay1ms();
1221 		}
1222 		if (wait <= 0)
1223 			continue;
1224 
1225 		/* send the ECHO command */
1226 		outb(IO_KBD + KBD_DATA_PORT, KBDC_ECHO);
1227 
1228 		/* wait for a response */
1229 		for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
1230 			if (inb(IO_KBD + KBD_STATUS_PORT) &
1231 			    KBDS_ANY_BUFFER_FULL)
1232 				break;
1233 			delay1ms();
1234 		}
1235 		if (wait <= 0)
1236 			continue;
1237 
1238 		delay7();
1239 		i = inb(IO_KBD + KBD_DATA_PORT);
1240 #ifdef PROBE_KBD_BEBUG
1241 		printf("probe_keyboard: got 0x%x.\n", i);
1242 #endif
1243 		if (i == KBD_ECHO) {
1244 			/* got the right answer */
1245 			return (1);
1246 		}
1247 	}
1248 
1249 	return (0);
1250 }
1251 #endif /* KEYBOARD_PROBE */
1252