2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <gpxe/ansiesc.h>
24 #define ATTR_BOLD 0x08
26 #define ATTR_FCOL_MASK 0x07
27 #define ATTR_FCOL_BLACK 0x00
28 #define ATTR_FCOL_BLUE 0x01
29 #define ATTR_FCOL_GREEN 0x02
30 #define ATTR_FCOL_CYAN 0x03
31 #define ATTR_FCOL_RED 0x04
32 #define ATTR_FCOL_MAGENTA 0x05
33 #define ATTR_FCOL_YELLOW 0x06
34 #define ATTR_FCOL_WHITE 0x07
36 #define ATTR_BCOL_MASK 0x70
37 #define ATTR_BCOL_BLACK 0x00
38 #define ATTR_BCOL_BLUE 0x10
39 #define ATTR_BCOL_GREEN 0x20
40 #define ATTR_BCOL_CYAN 0x30
41 #define ATTR_BCOL_RED 0x40
42 #define ATTR_BCOL_MAGENTA 0x50
43 #define ATTR_BCOL_YELLOW 0x60
44 #define ATTR_BCOL_WHITE 0x70
46 #define ATTR_DEFAULT ATTR_FCOL_WHITE
48 /** Current character attribute */
49 static unsigned int bios_attr = ATTR_DEFAULT;
52 * Handle ANSI CUP (cursor position)
54 * @v count Parameter count
55 * @v params[0] Row (1 is top)
56 * @v params[1] Column (1 is left)
58 static void bios_handle_cup ( unsigned int count __unused, int params[] ) {
59 int cx = ( params[1] - 1 );
60 int cy = ( params[0] - 1 );
67 __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
70 : : "a" ( 0x0200 ), "b" ( 1 ),
71 "d" ( ( cy << 8 ) | cx ) );
75 * Handle ANSI ED (erase in page)
77 * @v count Parameter count
78 * @v params[0] Region to erase
80 static void bios_handle_ed ( unsigned int count __unused,
81 int params[] __unused ) {
82 /* We assume that we always clear the whole screen */
83 assert ( params[0] == ANSIESC_ED_ALL );
85 __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
88 : : "a" ( 0x0600 ), "b" ( bios_attr << 8 ),
89 "c" ( 0 ), "d" ( 0xffff ) );
93 * Handle ANSI SGR (set graphics rendition)
95 * @v count Parameter count
96 * @v params List of graphic rendition aspects
98 static void bios_handle_sgr ( unsigned int count, int params[] ) {
99 static const uint8_t bios_attr_fcols[10] = {
100 ATTR_FCOL_BLACK, ATTR_FCOL_RED, ATTR_FCOL_GREEN,
101 ATTR_FCOL_YELLOW, ATTR_FCOL_BLUE, ATTR_FCOL_MAGENTA,
102 ATTR_FCOL_CYAN, ATTR_FCOL_WHITE,
103 ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
105 static const uint8_t bios_attr_bcols[10] = {
106 ATTR_BCOL_BLACK, ATTR_BCOL_RED, ATTR_BCOL_GREEN,
107 ATTR_BCOL_YELLOW, ATTR_BCOL_BLUE, ATTR_BCOL_MAGENTA,
108 ATTR_BCOL_CYAN, ATTR_BCOL_WHITE,
109 ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
114 for ( i = 0 ; i < count ; i++ ) {
117 bios_attr = ATTR_DEFAULT;
118 } else if ( aspect == 1 ) {
119 bios_attr |= ATTR_BOLD;
120 } else if ( aspect == 22 ) {
121 bios_attr &= ~ATTR_BOLD;
122 } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) {
123 bios_attr &= ~ATTR_FCOL_MASK;
124 bios_attr |= bios_attr_fcols[ aspect - 30 ];
125 } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
126 bios_attr &= ~ATTR_BCOL_MASK;
127 bios_attr |= bios_attr_bcols[ aspect - 40 ];
132 /** BIOS console ANSI escape sequence handlers */
133 static struct ansiesc_handler bios_ansiesc_handlers[] = {
134 { ANSIESC_CUP, bios_handle_cup },
135 { ANSIESC_ED, bios_handle_ed },
136 { ANSIESC_SGR, bios_handle_sgr },
140 /** BIOS console ANSI escape sequence context */
141 static struct ansiesc_context bios_ansiesc_ctx = {
142 .handlers = bios_ansiesc_handlers,
146 * Print a character to BIOS console
148 * @v character Character to be printed
150 static void bios_putchar ( int character ) {
151 int discard_a, discard_b, discard_c;
153 /* Intercept ANSI escape sequences */
154 character = ansiesc_process ( &bios_ansiesc_ctx, character );
158 /* Print character with attribute */
159 __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
160 /* Skip non-printable characters */
161 "cmpb $0x20, %%al\n\t"
164 "movw $0x0001, %%cx\n\t"
165 "movb $0x09, %%ah\n\t"
168 /* Print character */
169 "xorw %%bx, %%bx\n\t"
170 "movb $0x0e, %%ah\n\t"
173 : "=a" ( discard_a ), "=b" ( discard_b ),
175 : "a" ( character ), "b" ( bios_attr )
180 * Pointer to current ANSI output sequence
182 * While we are in the middle of returning an ANSI sequence for a
183 * special key, this will point to the next character to return. When
184 * not in the middle of such a sequence, this will point to a NUL
185 * (note: not "will be NULL").
187 static const char *ansi_input = "";
190 * Lowest BIOS scancode of interest
192 * Most of the BIOS key scancodes that we are interested in are in a
193 * dense range, so subtracting a constant and treating them as offsets
194 * into an array works efficiently.
196 #define BIOS_KEY_MIN 0x47
198 /** Offset into list of interesting BIOS scancodes */
199 #define BIOS_KEY(scancode) ( (scancode) - BIOS_KEY_MIN )
201 /** Mapping from BIOS scan codes to ANSI escape sequences */
202 static const char *ansi_sequences[] = {
203 [ BIOS_KEY ( 0x47 ) ] = "[H", /* Home */
204 [ BIOS_KEY ( 0x48 ) ] = "[A", /* Up arrow */
205 [ BIOS_KEY ( 0x4b ) ] = "[D", /* Left arrow */
206 [ BIOS_KEY ( 0x4d ) ] = "[C", /* Right arrow */
207 [ BIOS_KEY ( 0x4f ) ] = "[F", /* End */
208 [ BIOS_KEY ( 0x50 ) ] = "[B", /* Down arrow */
209 [ BIOS_KEY ( 0x53 ) ] = "[3~", /* Delete */
213 * Get ANSI escape sequence corresponding to BIOS scancode
215 * @v scancode BIOS scancode
216 * @ret ansi_seq ANSI escape sequence, if any, otherwise NULL
218 static const char * scancode_to_ansi_seq ( unsigned int scancode ) {
219 unsigned int bios_key = BIOS_KEY ( scancode );
221 if ( bios_key < ( sizeof ( ansi_sequences ) /
222 sizeof ( ansi_sequences[0] ) ) ) {
223 return ansi_sequences[bios_key];
229 * Get character from BIOS console
231 * @ret character Character read from console
233 static int bios_getchar ( void ) {
235 unsigned int character;
238 /* If we are mid-sequence, pass out the next byte */
239 if ( ( character = *ansi_input ) ) {
244 /* Read character from real BIOS console */
245 __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
248 : "=a" ( keypress ) : "a" ( 0x1000 ) );
249 character = ( keypress & 0xff );
251 /* If it's a normal character, just return it */
252 if ( character < 0x80 )
255 /* Otherwise, check for a special key that we know about */
256 if ( ( ansi_seq = scancode_to_ansi_seq ( keypress >> 8 ) ) ) {
257 /* Start of escape sequence: return ESC (0x1b) */
258 ansi_input = ansi_seq;
266 * Check for character ready to read from BIOS console
268 * @ret True Character available to read
269 * @ret False No character available to read
271 static int bios_iskey ( void ) {
272 unsigned int discard_a;
275 /* If we are mid-sequence, we are always ready */
279 /* Otherwise check the real BIOS console */
280 __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
285 : "=r" ( flags ), "=a" ( discard_a )
287 return ( ! ( flags & ZF ) );
290 struct console_driver bios_console __console_driver = {
291 .putchar = bios_putchar,
292 .getchar = bios_getchar,