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