Greg Hartman | 76d05dc | 2016-11-23 15:51:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>. |
| 3 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | * |
| 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. |
| 17 | */ |
| 18 | |
| 19 | FILE_LICENCE ( GPL2_OR_LATER ); |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <realmode.h> |
| 23 | #include <console.h> |
| 24 | #include <gpxe/ansiesc.h> |
| 25 | |
| 26 | #define ATTR_BOLD 0x08 |
| 27 | |
| 28 | #define ATTR_FCOL_MASK 0x07 |
| 29 | #define ATTR_FCOL_BLACK 0x00 |
| 30 | #define ATTR_FCOL_BLUE 0x01 |
| 31 | #define ATTR_FCOL_GREEN 0x02 |
| 32 | #define ATTR_FCOL_CYAN 0x03 |
| 33 | #define ATTR_FCOL_RED 0x04 |
| 34 | #define ATTR_FCOL_MAGENTA 0x05 |
| 35 | #define ATTR_FCOL_YELLOW 0x06 |
| 36 | #define ATTR_FCOL_WHITE 0x07 |
| 37 | |
| 38 | #define ATTR_BCOL_MASK 0x70 |
| 39 | #define ATTR_BCOL_BLACK 0x00 |
| 40 | #define ATTR_BCOL_BLUE 0x10 |
| 41 | #define ATTR_BCOL_GREEN 0x20 |
| 42 | #define ATTR_BCOL_CYAN 0x30 |
| 43 | #define ATTR_BCOL_RED 0x40 |
| 44 | #define ATTR_BCOL_MAGENTA 0x50 |
| 45 | #define ATTR_BCOL_YELLOW 0x60 |
| 46 | #define ATTR_BCOL_WHITE 0x70 |
| 47 | |
| 48 | #define ATTR_DEFAULT ATTR_FCOL_WHITE |
| 49 | |
| 50 | /** Current character attribute */ |
| 51 | static unsigned int bios_attr = ATTR_DEFAULT; |
| 52 | |
| 53 | /** |
| 54 | * Handle ANSI CUP (cursor position) |
| 55 | * |
| 56 | * @v count Parameter count |
| 57 | * @v params[0] Row (1 is top) |
| 58 | * @v params[1] Column (1 is left) |
| 59 | */ |
| 60 | static void bios_handle_cup ( unsigned int count __unused, int params[] ) { |
| 61 | int cx = ( params[1] - 1 ); |
| 62 | int cy = ( params[0] - 1 ); |
| 63 | |
| 64 | if ( cx < 0 ) |
| 65 | cx = 0; |
| 66 | if ( cy < 0 ) |
| 67 | cy = 0; |
| 68 | |
| 69 | __asm__ __volatile__ ( REAL_CODE ( "sti\n\t" |
| 70 | "int $0x10\n\t" |
| 71 | "cli\n\t" ) |
| 72 | : : "a" ( 0x0200 ), "b" ( 1 ), |
| 73 | "d" ( ( cy << 8 ) | cx ) ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Handle ANSI ED (erase in page) |
| 78 | * |
| 79 | * @v count Parameter count |
| 80 | * @v params[0] Region to erase |
| 81 | */ |
| 82 | static void bios_handle_ed ( unsigned int count __unused, |
| 83 | int params[] __unused ) { |
| 84 | /* We assume that we always clear the whole screen */ |
| 85 | assert ( params[0] == ANSIESC_ED_ALL ); |
| 86 | |
| 87 | __asm__ __volatile__ ( REAL_CODE ( "sti\n\t" |
| 88 | "int $0x10\n\t" |
| 89 | "cli\n\t" ) |
| 90 | : : "a" ( 0x0600 ), "b" ( bios_attr << 8 ), |
| 91 | "c" ( 0 ), "d" ( 0xffff ) ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Handle ANSI SGR (set graphics rendition) |
| 96 | * |
| 97 | * @v count Parameter count |
| 98 | * @v params List of graphic rendition aspects |
| 99 | */ |
| 100 | static void bios_handle_sgr ( unsigned int count, int params[] ) { |
| 101 | static const uint8_t bios_attr_fcols[10] = { |
| 102 | ATTR_FCOL_BLACK, ATTR_FCOL_RED, ATTR_FCOL_GREEN, |
| 103 | ATTR_FCOL_YELLOW, ATTR_FCOL_BLUE, ATTR_FCOL_MAGENTA, |
| 104 | ATTR_FCOL_CYAN, ATTR_FCOL_WHITE, |
| 105 | ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */ |
| 106 | }; |
| 107 | static const uint8_t bios_attr_bcols[10] = { |
| 108 | ATTR_BCOL_BLACK, ATTR_BCOL_RED, ATTR_BCOL_GREEN, |
| 109 | ATTR_BCOL_YELLOW, ATTR_BCOL_BLUE, ATTR_BCOL_MAGENTA, |
| 110 | ATTR_BCOL_CYAN, ATTR_BCOL_WHITE, |
| 111 | ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */ |
| 112 | }; |
| 113 | unsigned int i; |
| 114 | int aspect; |
| 115 | |
| 116 | for ( i = 0 ; i < count ; i++ ) { |
| 117 | aspect = params[i]; |
| 118 | if ( aspect == 0 ) { |
| 119 | bios_attr = ATTR_DEFAULT; |
| 120 | } else if ( aspect == 1 ) { |
| 121 | bios_attr |= ATTR_BOLD; |
| 122 | } else if ( aspect == 22 ) { |
| 123 | bios_attr &= ~ATTR_BOLD; |
| 124 | } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) { |
| 125 | bios_attr &= ~ATTR_FCOL_MASK; |
| 126 | bios_attr |= bios_attr_fcols[ aspect - 30 ]; |
| 127 | } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) { |
| 128 | bios_attr &= ~ATTR_BCOL_MASK; |
| 129 | bios_attr |= bios_attr_bcols[ aspect - 40 ]; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** BIOS console ANSI escape sequence handlers */ |
| 135 | static struct ansiesc_handler bios_ansiesc_handlers[] = { |
| 136 | { ANSIESC_CUP, bios_handle_cup }, |
| 137 | { ANSIESC_ED, bios_handle_ed }, |
| 138 | { ANSIESC_SGR, bios_handle_sgr }, |
| 139 | { 0, NULL } |
| 140 | }; |
| 141 | |
| 142 | /** BIOS console ANSI escape sequence context */ |
| 143 | static struct ansiesc_context bios_ansiesc_ctx = { |
| 144 | .handlers = bios_ansiesc_handlers, |
| 145 | }; |
| 146 | |
| 147 | /** |
| 148 | * Print a character to BIOS console |
| 149 | * |
| 150 | * @v character Character to be printed |
| 151 | */ |
| 152 | static void bios_putchar ( int character ) { |
| 153 | int discard_a, discard_b, discard_c; |
| 154 | |
| 155 | /* Intercept ANSI escape sequences */ |
| 156 | character = ansiesc_process ( &bios_ansiesc_ctx, character ); |
| 157 | if ( character < 0 ) |
| 158 | return; |
| 159 | |
| 160 | /* Print character with attribute */ |
| 161 | __asm__ __volatile__ ( REAL_CODE ( "sti\n\t" |
| 162 | /* Skip non-printable characters */ |
| 163 | "cmpb $0x20, %%al\n\t" |
| 164 | "jb 1f\n\t" |
| 165 | /* Set attribute */ |
| 166 | "movw $0x0001, %%cx\n\t" |
| 167 | "movb $0x09, %%ah\n\t" |
| 168 | "int $0x10\n\t" |
| 169 | "\n1:\n\t" |
| 170 | /* Print character */ |
| 171 | "xorw %%bx, %%bx\n\t" |
| 172 | "movb $0x0e, %%ah\n\t" |
| 173 | "int $0x10\n\t" |
| 174 | "cli\n\t" ) |
| 175 | : "=a" ( discard_a ), "=b" ( discard_b ), |
| 176 | "=c" ( discard_c ) |
| 177 | : "a" ( character ), "b" ( bios_attr ) |
| 178 | : "ebp" ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Pointer to current ANSI output sequence |
| 183 | * |
| 184 | * While we are in the middle of returning an ANSI sequence for a |
| 185 | * special key, this will point to the next character to return. When |
| 186 | * not in the middle of such a sequence, this will point to a NUL |
| 187 | * (note: not "will be NULL"). |
| 188 | */ |
| 189 | static const char *ansi_input = ""; |
| 190 | |
| 191 | /** |
| 192 | * Lowest BIOS scancode of interest |
| 193 | * |
| 194 | * Most of the BIOS key scancodes that we are interested in are in a |
| 195 | * dense range, so subtracting a constant and treating them as offsets |
| 196 | * into an array works efficiently. |
| 197 | */ |
| 198 | #define BIOS_KEY_MIN 0x42 |
| 199 | |
| 200 | /** Offset into list of interesting BIOS scancodes */ |
| 201 | #define BIOS_KEY(scancode) ( (scancode) - BIOS_KEY_MIN ) |
| 202 | |
| 203 | /** Mapping from BIOS scan codes to ANSI escape sequences */ |
| 204 | static const char *ansi_sequences[] = { |
| 205 | [ BIOS_KEY ( 0x42 ) ] = "[19~", /* F8 (required for PXE) */ |
| 206 | [ BIOS_KEY ( 0x47 ) ] = "[H", /* Home */ |
| 207 | [ BIOS_KEY ( 0x48 ) ] = "[A", /* Up arrow */ |
| 208 | [ BIOS_KEY ( 0x4b ) ] = "[D", /* Left arrow */ |
| 209 | [ BIOS_KEY ( 0x4d ) ] = "[C", /* Right arrow */ |
| 210 | [ BIOS_KEY ( 0x4f ) ] = "[F", /* End */ |
| 211 | [ BIOS_KEY ( 0x50 ) ] = "[B", /* Down arrow */ |
| 212 | [ BIOS_KEY ( 0x53 ) ] = "[3~", /* Delete */ |
| 213 | }; |
| 214 | |
| 215 | /** |
| 216 | * Get ANSI escape sequence corresponding to BIOS scancode |
| 217 | * |
| 218 | * @v scancode BIOS scancode |
| 219 | * @ret ansi_seq ANSI escape sequence, if any, otherwise NULL |
| 220 | */ |
| 221 | static const char * scancode_to_ansi_seq ( unsigned int scancode ) { |
| 222 | unsigned int bios_key = BIOS_KEY ( scancode ); |
| 223 | |
| 224 | if ( bios_key < ( sizeof ( ansi_sequences ) / |
| 225 | sizeof ( ansi_sequences[0] ) ) ) { |
| 226 | return ansi_sequences[bios_key]; |
| 227 | } |
| 228 | DBG ( "Unrecognised BIOS scancode %02x\n", scancode ); |
| 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Get character from BIOS console |
| 234 | * |
| 235 | * @ret character Character read from console |
| 236 | */ |
| 237 | static int bios_getchar ( void ) { |
| 238 | uint16_t keypress; |
| 239 | unsigned int character; |
| 240 | const char *ansi_seq; |
| 241 | |
| 242 | /* If we are mid-sequence, pass out the next byte */ |
| 243 | if ( ( character = *ansi_input ) ) { |
| 244 | ansi_input++; |
| 245 | return character; |
| 246 | } |
| 247 | |
| 248 | /* Read character from real BIOS console */ |
| 249 | __asm__ __volatile__ ( REAL_CODE ( "sti\n\t" |
| 250 | "int $0x16\n\t" |
| 251 | "cli\n\t" ) |
| 252 | : "=a" ( keypress ) : "a" ( 0x1000 ) ); |
| 253 | character = ( keypress & 0xff ); |
| 254 | |
| 255 | /* If it's a normal character, just return it */ |
| 256 | if ( character && ( character < 0x80 ) ) |
| 257 | return character; |
| 258 | |
| 259 | /* Otherwise, check for a special key that we know about */ |
| 260 | if ( ( ansi_seq = scancode_to_ansi_seq ( keypress >> 8 ) ) ) { |
| 261 | /* Start of escape sequence: return ESC (0x1b) */ |
| 262 | ansi_input = ansi_seq; |
| 263 | return 0x1b; |
| 264 | } |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Check for character ready to read from BIOS console |
| 271 | * |
| 272 | * @ret True Character available to read |
| 273 | * @ret False No character available to read |
| 274 | */ |
| 275 | static int bios_iskey ( void ) { |
| 276 | unsigned int discard_a; |
| 277 | unsigned int flags; |
| 278 | |
| 279 | /* If we are mid-sequence, we are always ready */ |
| 280 | if ( *ansi_input ) |
| 281 | return 1; |
| 282 | |
| 283 | /* Otherwise check the real BIOS console */ |
| 284 | __asm__ __volatile__ ( REAL_CODE ( "sti\n\t" |
| 285 | "int $0x16\n\t" |
| 286 | "pushfw\n\t" |
| 287 | "popw %w0\n\t" |
| 288 | "cli\n\t" ) |
| 289 | : "=r" ( flags ), "=a" ( discard_a ) |
| 290 | : "a" ( 0x0100 ) ); |
| 291 | return ( ! ( flags & ZF ) ); |
| 292 | } |
| 293 | |
| 294 | struct console_driver bios_console __console_driver = { |
| 295 | .putchar = bios_putchar, |
| 296 | .getchar = bios_getchar, |
| 297 | .iskey = bios_iskey, |
| 298 | }; |