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 ) {
152 /* Intercept ANSI escape sequences */
153 character = ansiesc_process ( &bios_ansiesc_ctx, character );
157 /* Set attribute for printable characters */
158 if ( character >= 0x20 ) {
159 __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
162 : : "a" ( 0x0920 ), "b" ( bios_attr ),
166 /* Print the character */
167 __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
170 : : "a" ( character | 0x0e00 ), "b" ( 0 )
175 * Get character from BIOS console
177 * @ret character Character read from console
179 static int bios_getchar ( void ) {
182 __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
185 : "=a" ( character ) : "a" ( 0x0000 ) );
191 * Check for character ready to read from BIOS console
193 * @ret True Character available to read
194 * @ret False No character available to read
196 static int bios_iskey ( void ) {
197 unsigned int discard_a;
200 __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
205 : "=r" ( flags ), "=a" ( discard_a )
208 return ( ! ( flags & ZF ) );
211 struct console_driver bios_console __console_driver = {
212 .putchar = bios_putchar,
213 .getchar = bios_getchar,