2 * Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/efi/efi.h>
27 #include <ipxe/efi/Protocol/ConsoleControl/ConsoleControl.h>
28 #include <ipxe/ansiesc.h>
29 #include <ipxe/console.h>
30 #include <ipxe/init.h>
31 #include <config/console.h>
33 #define ATTR_BOLD 0x08
35 #define ATTR_FCOL_MASK 0x07
36 #define ATTR_FCOL_BLACK 0x00
37 #define ATTR_FCOL_BLUE 0x01
38 #define ATTR_FCOL_GREEN 0x02
39 #define ATTR_FCOL_CYAN 0x03
40 #define ATTR_FCOL_RED 0x04
41 #define ATTR_FCOL_MAGENTA 0x05
42 #define ATTR_FCOL_YELLOW 0x06
43 #define ATTR_FCOL_WHITE 0x07
45 #define ATTR_BCOL_MASK 0x70
46 #define ATTR_BCOL_BLACK 0x00
47 #define ATTR_BCOL_BLUE 0x10
48 #define ATTR_BCOL_GREEN 0x20
49 #define ATTR_BCOL_CYAN 0x30
50 #define ATTR_BCOL_RED 0x40
51 #define ATTR_BCOL_MAGENTA 0x50
52 #define ATTR_BCOL_YELLOW 0x60
53 #define ATTR_BCOL_WHITE 0x70
55 #define ATTR_DEFAULT ATTR_FCOL_WHITE
57 /* Set default console usage if applicable */
58 #if ! ( defined ( CONSOLE_EFI ) && CONSOLE_EXPLICIT ( CONSOLE_EFI ) )
60 #define CONSOLE_EFI ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
63 /** Current character attribute */
64 static unsigned int efi_attr = ATTR_DEFAULT;
66 /** Console control protocol */
67 static EFI_CONSOLE_CONTROL_PROTOCOL *conctrl;
68 EFI_REQUEST_PROTOCOL ( EFI_CONSOLE_CONTROL_PROTOCOL, &conctrl );
71 * Handle ANSI CUP (cursor position)
73 * @v ctx ANSI escape sequence context
74 * @v count Parameter count
75 * @v params[0] Row (1 is top)
76 * @v params[1] Column (1 is left)
78 static void efi_handle_cup ( struct ansiesc_context *ctx __unused,
79 unsigned int count __unused, int params[] ) {
80 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
81 int cx = ( params[1] - 1 );
82 int cy = ( params[0] - 1 );
89 conout->SetCursorPosition ( conout, cx, cy );
93 * Handle ANSI ED (erase in page)
95 * @v ctx ANSI escape sequence context
96 * @v count Parameter count
97 * @v params[0] Region to erase
99 static void efi_handle_ed ( struct ansiesc_context *ctx __unused,
100 unsigned int count __unused,
101 int params[] __unused ) {
102 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
104 /* We assume that we always clear the whole screen */
105 assert ( params[0] == ANSIESC_ED_ALL );
107 conout->ClearScreen ( conout );
111 * Handle ANSI SGR (set graphics rendition)
113 * @v ctx ANSI escape sequence context
114 * @v count Parameter count
115 * @v params List of graphic rendition aspects
117 static void efi_handle_sgr ( struct ansiesc_context *ctx __unused,
118 unsigned int count, int params[] ) {
119 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
120 static const uint8_t efi_attr_fcols[10] = {
121 ATTR_FCOL_BLACK, ATTR_FCOL_RED, ATTR_FCOL_GREEN,
122 ATTR_FCOL_YELLOW, ATTR_FCOL_BLUE, ATTR_FCOL_MAGENTA,
123 ATTR_FCOL_CYAN, ATTR_FCOL_WHITE,
124 ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
126 static const uint8_t efi_attr_bcols[10] = {
127 ATTR_BCOL_BLACK, ATTR_BCOL_RED, ATTR_BCOL_GREEN,
128 ATTR_BCOL_YELLOW, ATTR_BCOL_BLUE, ATTR_BCOL_MAGENTA,
129 ATTR_BCOL_CYAN, ATTR_BCOL_WHITE,
130 ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
135 for ( i = 0 ; i < count ; i++ ) {
138 efi_attr = ATTR_DEFAULT;
139 } else if ( aspect == 1 ) {
140 efi_attr |= ATTR_BOLD;
141 } else if ( aspect == 22 ) {
142 efi_attr &= ~ATTR_BOLD;
143 } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) {
144 efi_attr &= ~ATTR_FCOL_MASK;
145 efi_attr |= efi_attr_fcols[ aspect - 30 ];
146 } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
147 efi_attr &= ~ATTR_BCOL_MASK;
148 efi_attr |= efi_attr_bcols[ aspect - 40 ];
152 conout->SetAttribute ( conout, efi_attr );
156 * Handle ANSI DECTCEM set (show cursor)
158 * @v ctx ANSI escape sequence context
159 * @v count Parameter count
160 * @v params List of graphic rendition aspects
162 static void efi_handle_dectcem_set ( struct ansiesc_context *ctx __unused,
163 unsigned int count __unused,
164 int params[] __unused ) {
165 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
167 conout->EnableCursor ( conout, TRUE );
171 * Handle ANSI DECTCEM reset (hide cursor)
173 * @v ctx ANSI escape sequence context
174 * @v count Parameter count
175 * @v params List of graphic rendition aspects
177 static void efi_handle_dectcem_reset ( struct ansiesc_context *ctx __unused,
178 unsigned int count __unused,
179 int params[] __unused ) {
180 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
182 conout->EnableCursor ( conout, FALSE );
185 /** EFI console ANSI escape sequence handlers */
186 static struct ansiesc_handler efi_ansiesc_handlers[] = {
187 { ANSIESC_CUP, efi_handle_cup },
188 { ANSIESC_ED, efi_handle_ed },
189 { ANSIESC_SGR, efi_handle_sgr },
190 { ANSIESC_DECTCEM_SET, efi_handle_dectcem_set },
191 { ANSIESC_DECTCEM_RESET, efi_handle_dectcem_reset },
195 /** EFI console ANSI escape sequence context */
196 static struct ansiesc_context efi_ansiesc_ctx = {
197 .handlers = efi_ansiesc_handlers,
201 * Print a character to EFI console
203 * @v character Character to be printed
205 static void efi_putchar ( int character ) {
206 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
207 wchar_t wstr[] = { character, 0 };
209 /* Intercept ANSI escape sequences */
210 character = ansiesc_process ( &efi_ansiesc_ctx, character );
214 conout->OutputString ( conout, wstr );
218 * Pointer to current ANSI output sequence
220 * While we are in the middle of returning an ANSI sequence for a
221 * special key, this will point to the next character to return. When
222 * not in the middle of such a sequence, this will point to a NUL
223 * (note: not "will be NULL").
225 static const char *ansi_input = "";
227 /** Mapping from EFI scan codes to ANSI escape sequences */
228 static const char *ansi_sequences[] = {
235 [SCAN_INSERT] = "[2~",
236 /* EFI translates an incoming backspace via the serial console
237 * into a SCAN_DELETE. There's not much we can do about this.
239 [SCAN_DELETE] = "[3~",
240 [SCAN_PAGE_UP] = "[5~",
241 [SCAN_PAGE_DOWN] = "[6~",
242 /* EFI translates some (but not all) incoming escape sequences
243 * via the serial console into equivalent scancodes. When it
244 * doesn't recognise a sequence, it helpfully(!) translates
245 * the initial ESC and passes the remainder through verbatim.
246 * Treating SCAN_ESC as equivalent to an empty escape sequence
247 * works around this bug.
253 * Get ANSI escape sequence corresponding to EFI scancode
255 * @v scancode EFI scancode
256 * @ret ansi_seq ANSI escape sequence, if any, otherwise NULL
258 static const char * scancode_to_ansi_seq ( unsigned int scancode ) {
259 if ( scancode < ( sizeof ( ansi_sequences ) /
260 sizeof ( ansi_sequences[0] ) ) ) {
261 return ansi_sequences[scancode];
267 * Get character from EFI console
269 * @ret character Character read from console
271 static int efi_getchar ( void ) {
272 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
273 const char *ansi_seq;
278 /* If we are mid-sequence, pass out the next byte */
280 return *(ansi_input++);
282 /* Read key from real EFI console */
283 if ( ( efirc = conin->ReadKeyStroke ( conin, &key ) ) != 0 ) {
284 rc = -EEFI ( efirc );
285 DBG ( "EFI could not read keystroke: %s\n", strerror ( rc ) );
288 DBG2 ( "EFI read key stroke with unicode %04x scancode %04x\n",
289 key.UnicodeChar, key.ScanCode );
291 /* If key has a Unicode representation, return it */
292 if ( key.UnicodeChar )
293 return key.UnicodeChar;
295 /* Otherwise, check for a special key that we know about */
296 if ( ( ansi_seq = scancode_to_ansi_seq ( key.ScanCode ) ) ) {
297 /* Start of escape sequence: return ESC (0x1b) */
298 ansi_input = ansi_seq;
306 * Check for character ready to read from EFI console
308 * @ret True Character available to read
309 * @ret False No character available to read
311 static int efi_iskey ( void ) {
312 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
313 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
316 /* If we are mid-sequence, we are always ready */
320 /* Check to see if the WaitForKey event has fired */
321 if ( ( efirc = bs->CheckEvent ( conin->WaitForKey ) ) == 0 )
327 /** EFI console driver */
328 struct console_driver efi_console __console_driver = {
329 .putchar = efi_putchar,
330 .getchar = efi_getchar,
332 .usage = CONSOLE_EFI,
336 * Initialise EFI console
339 static void efi_console_init ( void ) {
340 EFI_CONSOLE_CONTROL_SCREEN_MODE mode;
342 /* On some older EFI 1.10 implementations, we must use the
343 * (now obsolete) EFI_CONSOLE_CONTROL_PROTOCOL to switch the
344 * console into text mode.
347 conctrl->GetMode ( conctrl, &mode, NULL, NULL );
348 if ( mode != EfiConsoleControlScreenText ) {
349 conctrl->SetMode ( conctrl,
350 EfiConsoleControlScreenText );
356 * EFI console initialisation function
358 struct init_fn efi_console_init_fn __init_fn ( INIT_EARLY ) = {
359 .initialise = efi_console_init,