iPXE
efi_console.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 */
19
20FILE_LICENCE ( GPL2_OR_LATER );
21FILE_SECBOOT ( PERMITTED );
22
23#include <stddef.h>
24#include <string.h>
25#include <errno.h>
26#include <assert.h>
27#include <ipxe/efi/efi.h>
29#include <ipxe/ansiesc.h>
30#include <ipxe/utf8.h>
31#include <ipxe/console.h>
32#include <ipxe/keymap.h>
33#include <ipxe/init.h>
34#include <config/console.h>
35
36#define ATTR_BOLD 0x08
37
38#define ATTR_FCOL_MASK 0x07
39#define ATTR_FCOL_BLACK 0x00
40#define ATTR_FCOL_BLUE 0x01
41#define ATTR_FCOL_GREEN 0x02
42#define ATTR_FCOL_CYAN 0x03
43#define ATTR_FCOL_RED 0x04
44#define ATTR_FCOL_MAGENTA 0x05
45#define ATTR_FCOL_YELLOW 0x06
46#define ATTR_FCOL_WHITE 0x07
47
48#define ATTR_BCOL_MASK 0x70
49#define ATTR_BCOL_BLACK 0x00
50#define ATTR_BCOL_BLUE 0x10
51#define ATTR_BCOL_GREEN 0x20
52#define ATTR_BCOL_CYAN 0x30
53#define ATTR_BCOL_RED 0x40
54#define ATTR_BCOL_MAGENTA 0x50
55#define ATTR_BCOL_YELLOW 0x60
56#define ATTR_BCOL_WHITE 0x70
57
58#define ATTR_DEFAULT ATTR_FCOL_WHITE
59
60/* Set default console usage if applicable */
61#if ! ( defined ( CONSOLE_EFI ) && CONSOLE_EXPLICIT ( CONSOLE_EFI ) )
62#undef CONSOLE_EFI
63#define CONSOLE_EFI ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
64#endif
65
66/** Current character attribute */
67static unsigned int efi_attr = ATTR_DEFAULT;
68
69/** Console control protocol */
72
73/** Extended simple text input protocol, if present */
75
76/**
77 * Handle ANSI CUP (cursor position)
78 *
79 * @v ctx ANSI escape sequence context
80 * @v count Parameter count
81 * @v params[0] Row (1 is top)
82 * @v params[1] Column (1 is left)
83 */
85 unsigned int count __unused, int params[] ) {
87 int cx = ( params[1] - 1 );
88 int cy = ( params[0] - 1 );
89
90 if ( cx < 0 )
91 cx = 0;
92 if ( cy < 0 )
93 cy = 0;
94
95 conout->SetCursorPosition ( conout, cx, cy );
96}
97
98/**
99 * Handle ANSI ED (erase in page)
100 *
101 * @v ctx ANSI escape sequence context
102 * @v count Parameter count
103 * @v params[0] Region to erase
104 */
106 unsigned int count __unused,
107 int params[] __unused ) {
109
110 /* We assume that we always clear the whole screen */
111 assert ( params[0] == ANSIESC_ED_ALL );
112
113 conout->ClearScreen ( conout );
114}
115
116/**
117 * Handle ANSI SGR (set graphics rendition)
118 *
119 * @v ctx ANSI escape sequence context
120 * @v count Parameter count
121 * @v params List of graphic rendition aspects
122 */
124 unsigned int count, int params[] ) {
126 static const uint8_t efi_attr_fcols[10] = {
130 ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
131 };
132 static const uint8_t efi_attr_bcols[10] = {
136 ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
137 };
138 unsigned int i;
139 int aspect;
140
141 for ( i = 0 ; i < count ; i++ ) {
142 aspect = params[i];
143 if ( aspect == 0 ) {
145 } else if ( aspect == 1 ) {
147 } else if ( aspect == 22 ) {
149 } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) {
151 efi_attr |= efi_attr_fcols[ aspect - 30 ];
152 } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
154 efi_attr |= efi_attr_bcols[ aspect - 40 ];
155 }
156 }
157
158 conout->SetAttribute ( conout, efi_attr );
159}
160
161/**
162 * Handle ANSI DECTCEM set (show cursor)
163 *
164 * @v ctx ANSI escape sequence context
165 * @v count Parameter count
166 * @v params List of graphic rendition aspects
167 */
169 unsigned int count __unused,
170 int params[] __unused ) {
172
173 conout->EnableCursor ( conout, TRUE );
174}
175
176/**
177 * Handle ANSI DECTCEM reset (hide cursor)
178 *
179 * @v ctx ANSI escape sequence context
180 * @v count Parameter count
181 * @v params List of graphic rendition aspects
182 */
184 unsigned int count __unused,
185 int params[] __unused ) {
187
188 conout->EnableCursor ( conout, FALSE );
189}
190
191/** EFI console ANSI escape sequence handlers */
200
201/** EFI console ANSI escape sequence context */
203 .handlers = efi_ansiesc_handlers,
204};
205
206/** EFI console UTF-8 accumulator */
208
209/**
210 * Print a character to EFI console
211 *
212 * @v character Character to be printed
213 */
214static void efi_putchar ( int character ) {
216 wchar_t wstr[2];
217
218 /* Intercept ANSI escape sequences */
220 if ( character < 0 )
221 return;
222
223 /* Accumulate Unicode characters */
225 if ( character == 0 )
226 return;
227
228 /* Treat unrepresentable (non-UCS2) characters as invalid */
229 if ( character & ~( ( wchar_t ) -1UL ) )
231
232 /* Output character */
233 wstr[0] = character;
234 wstr[1] = L'\0';
235 conout->OutputString ( conout, wstr );
236}
237
238/**
239 * Pointer to current ANSI output sequence
240 *
241 * While we are in the middle of returning an ANSI sequence for a
242 * special key, this will point to the next character to return. When
243 * not in the middle of such a sequence, this will point to a NUL
244 * (note: not "will be NULL").
245 */
246static const char *ansi_input = "";
247
248/** Mapping from EFI scan codes to ANSI escape sequences */
249static const char *ansi_sequences[] = {
250 [SCAN_UP] = "[A",
251 [SCAN_DOWN] = "[B",
252 [SCAN_RIGHT] = "[C",
253 [SCAN_LEFT] = "[D",
254 [SCAN_HOME] = "[H",
255 [SCAN_END] = "[F",
256 [SCAN_INSERT] = "[2~",
257 /* EFI translates an incoming backspace via the serial console
258 * into a SCAN_DELETE. There's not much we can do about this.
259 */
260 [SCAN_DELETE] = "[3~",
261 [SCAN_PAGE_UP] = "[5~",
262 [SCAN_PAGE_DOWN] = "[6~",
263 [SCAN_F5] = "[15~",
264 [SCAN_F6] = "[17~",
265 [SCAN_F7] = "[18~",
266 [SCAN_F8] = "[19~",
267 [SCAN_F9] = "[20~",
268 [SCAN_F10] = "[21~",
269 [SCAN_F11] = "[23~",
270 [SCAN_F12] = "[24~",
271 /* EFI translates some (but not all) incoming escape sequences
272 * via the serial console into equivalent scancodes. When it
273 * doesn't recognise a sequence, it helpfully(!) translates
274 * the initial ESC and passes the remainder through verbatim.
275 * Treating SCAN_ESC as equivalent to an empty escape sequence
276 * works around this bug.
277 */
278 [SCAN_ESC] = "",
279};
280
281/**
282 * Get ANSI escape sequence corresponding to EFI scancode
283 *
284 * @v scancode EFI scancode
285 * @ret ansi_seq ANSI escape sequence, if any, otherwise NULL
286 */
287static const char * scancode_to_ansi_seq ( unsigned int scancode ) {
288 if ( scancode < ( sizeof ( ansi_sequences ) /
289 sizeof ( ansi_sequences[0] ) ) ) {
290 return ansi_sequences[scancode];
291 }
292 return NULL;
293}
294
295/**
296 * Get character from EFI console
297 *
298 * @ret character Character read from console
299 */
300static int efi_getchar ( void ) {
303 const char *ansi_seq;
304 unsigned int character;
305 unsigned int shift;
306 unsigned int toggle;
308 EFI_STATUS efirc;
309 int rc;
310
311 /* If we are mid-sequence, pass out the next byte */
312 if ( *ansi_input )
313 return *(ansi_input++);
314
315 /* Read key from real EFI console */
316 memset ( &key, 0, sizeof ( key ) );
317 if ( conin_ex ) {
318 if ( ( efirc = conin_ex->ReadKeyStrokeEx ( conin_ex,
319 &key ) ) != 0 ) {
320 rc = -EEFI ( efirc );
321 DBG ( "EFI could not read extended keystroke: %s\n",
322 strerror ( rc ) );
323 return 0;
324 }
325 } else {
326 if ( ( efirc = conin->ReadKeyStroke ( conin,
327 &key.Key ) ) != 0 ) {
328 rc = -EEFI ( efirc );
329 DBG ( "EFI could not read keystroke: %s\n",
330 strerror ( rc ) );
331 return 0;
332 }
333 }
334 DBG2 ( "EFI read key stroke shift %08x toggle %02x unicode %04x "
335 "scancode %04x\n", key.KeyState.KeyShiftState,
336 key.KeyState.KeyToggleState, key.Key.UnicodeChar,
337 key.Key.ScanCode );
338
339 /* If key has a Unicode representation, remap and return it.
340 * There is unfortunately no way to avoid remapping the
341 * numeric keypad, since EFI destroys the scan code
342 * information that would allow us to differentiate between
343 * main keyboard and numeric keypad.
344 */
345 if ( ( character = key.Key.UnicodeChar ) != 0 ) {
346
347 /* Apply shift state */
348 shift = key.KeyState.KeyShiftState;
349 if ( shift & EFI_SHIFT_STATE_VALID ) {
350 if ( shift & ( EFI_LEFT_CONTROL_PRESSED |
353 }
354 if ( shift & EFI_RIGHT_ALT_PRESSED ) {
356 }
357 }
358
359 /* Apply toggle state */
360 toggle = key.KeyState.KeyToggleState;
361 if ( toggle & EFI_TOGGLE_STATE_VALID ) {
362 if ( toggle & EFI_CAPS_LOCK_ACTIVE ) {
364 }
365 }
366
367 /* Remap and return key */
368 return key_remap ( character );
369 }
370
371 /* Otherwise, check for a special key that we know about */
372 if ( ( ansi_seq = scancode_to_ansi_seq ( key.Key.ScanCode ) ) ) {
373 /* Start of escape sequence: return ESC (0x1b) */
374 ansi_input = ansi_seq;
375 return 0x1b;
376 }
377
378 return 0;
379}
380
381/**
382 * Check for character ready to read from EFI console
383 *
384 * @ret True Character available to read
385 * @ret False No character available to read
386 */
387static int efi_iskey ( void ) {
388 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
390 EFI_STATUS efirc;
391
392 /* If we are mid-sequence, we are always ready */
393 if ( *ansi_input )
394 return 1;
395
396 /* Check to see if the WaitForKey event has fired */
397 if ( ( efirc = bs->CheckEvent ( conin->WaitForKey ) ) == 0 )
398 return 1;
399
400 return 0;
401}
402
403/** EFI console driver */
405 .putchar = efi_putchar,
406 .getchar = efi_getchar,
407 .iskey = efi_iskey,
408 .usage = CONSOLE_EFI,
409};
410
411/**
412 * Initialise EFI console
413 *
414 */
415static void efi_console_init ( void ) {
417 int rc;
418
419 /* On some older EFI 1.10 implementations, we must use the
420 * (now obsolete) EFI_CONSOLE_CONTROL_PROTOCOL to switch the
421 * console into text mode.
422 */
423 if ( conctrl ) {
424 conctrl->GetMode ( conctrl, &mode, NULL, NULL );
426 conctrl->SetMode ( conctrl,
428 }
429 }
430
431 /* Attempt to open the Simple Text Input Ex protocol on the
432 * console input handle. This is provably unsafe, but is
433 * apparently the expected behaviour for all UEFI
434 * applications. Don't ask.
435 */
436 if ( ( rc = efi_open_unsafe ( efi_systab->ConsoleInHandle,
438 &efi_conin_ex ) ) == 0 ) {
439 DBG ( "EFI using SimpleTextInputEx\n" );
440 } else {
441 DBG ( "EFI has no SimpleTextInputEx: %s\n", strerror ( rc ) );
442 }
443}
444
445/**
446 * EFI console initialisation function
447 */
448struct init_fn efi_console_init_fn __init_fn ( INIT_EARLY ) = {
449 .name = "eficonsole",
450 .initialise = efi_console_init,
451};
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
union @162305117151260234136356364136041353210355154177 key
Sense key.
Definition scsi.h:3
struct golan_eq_context ctx
Definition CIB_PRM.h:0
EFI_CONSOLE_CONTROL_SCREEN_MODE
@ EfiConsoleControlScreenText
struct _EFI_CONSOLE_CONTROL_PROTOCOL EFI_CONSOLE_CONTROL_PROTOCOL
#define EFI_SHIFT_STATE_VALID
#define EFI_RIGHT_ALT_PRESSED
#define EFI_CAPS_LOCK_ACTIVE
struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
#define EFI_LEFT_CONTROL_PRESSED
#define EFI_RIGHT_CONTROL_PRESSED
#define SCAN_F11
#define SCAN_F12
#define EFI_TOGGLE_STATE_VALID
#define SCAN_PAGE_UP
#define SCAN_LEFT
#define SCAN_PAGE_DOWN
#define SCAN_HOME
#define SCAN_F6
#define SCAN_RIGHT
#define SCAN_F9
#define SCAN_UP
#define SCAN_F8
#define SCAN_F5
#define SCAN_F10
#define SCAN_ESC
#define SCAN_DELETE
#define SCAN_DOWN
#define SCAN_END
#define SCAN_INSERT
struct _EFI_SIMPLE_TEXT_INPUT_PROTOCOL EFI_SIMPLE_TEXT_INPUT_PROTOCOL
#define SCAN_F7
struct _EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
int ansiesc_process(struct ansiesc_context *ctx, int c)
Process character that may be part of ANSI escape sequence.
Definition ansiesc.c:75
ANSI escape sequences.
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
unsigned char uint8_t
Definition stdint.h:10
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
Console configuration.
static struct utf8_accumulator efi_utf8_acc
EFI console UTF-8 accumulator.
#define ATTR_FCOL_BLACK
Definition efi_console.c:39
static const char * scancode_to_ansi_seq(unsigned int scancode)
Get ANSI escape sequence corresponding to EFI scancode.
#define ATTR_BCOL_CYAN
Definition efi_console.c:52
static void efi_console_init(void)
Initialise EFI console.
static void efi_handle_dectcem_set(struct ansiesc_context *ctx __unused, unsigned int count __unused, int params[] __unused)
Handle ANSI DECTCEM set (show cursor)
#define CONSOLE_EFI
Definition efi_console.c:63
static int efi_iskey(void)
Check for character ready to read from EFI console.
#define ATTR_FCOL_YELLOW
Definition efi_console.c:45
#define ATTR_BCOL_GREEN
Definition efi_console.c:51
static struct ansiesc_handler efi_ansiesc_handlers[]
EFI console ANSI escape sequence handlers.
static int efi_getchar(void)
Get character from EFI console.
#define ATTR_BOLD
Definition efi_console.c:36
static EFI_CONSOLE_CONTROL_PROTOCOL * conctrl
Console control protocol.
Definition efi_console.c:70
#define ATTR_FCOL_MAGENTA
Definition efi_console.c:44
#define ATTR_BCOL_BLACK
Definition efi_console.c:49
#define ATTR_BCOL_WHITE
Definition efi_console.c:56
#define ATTR_FCOL_WHITE
Definition efi_console.c:46
#define ATTR_FCOL_CYAN
Definition efi_console.c:42
#define ATTR_BCOL_BLUE
Definition efi_console.c:50
#define ATTR_BCOL_MAGENTA
Definition efi_console.c:54
static unsigned int efi_attr
Current character attribute.
Definition efi_console.c:67
#define ATTR_BCOL_YELLOW
Definition efi_console.c:55
static struct ansiesc_context efi_ansiesc_ctx
EFI console ANSI escape sequence context.
static EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL * efi_conin_ex
Extended simple text input protocol, if present.
Definition efi_console.c:74
#define ATTR_FCOL_BLUE
Definition efi_console.c:40
static void efi_handle_ed(struct ansiesc_context *ctx __unused, unsigned int count __unused, int params[] __unused)
Handle ANSI ED (erase in page)
static const char * ansi_input
Pointer to current ANSI output sequence.
static void efi_handle_dectcem_reset(struct ansiesc_context *ctx __unused, unsigned int count __unused, int params[] __unused)
Handle ANSI DECTCEM reset (hide cursor)
#define ATTR_BCOL_RED
Definition efi_console.c:53
static void efi_handle_cup(struct ansiesc_context *ctx __unused, unsigned int count __unused, int params[])
Handle ANSI CUP (cursor position)
Definition efi_console.c:84
#define ATTR_FCOL_GREEN
Definition efi_console.c:41
#define ATTR_FCOL_MASK
Definition efi_console.c:38
#define ATTR_BCOL_MASK
Definition efi_console.c:48
static const char * ansi_sequences[]
Mapping from EFI scan codes to ANSI escape sequences.
#define ATTR_FCOL_RED
Definition efi_console.c:43
static void efi_handle_sgr(struct ansiesc_context *ctx __unused, unsigned int count, int params[])
Handle ANSI SGR (set graphics rendition)
#define ATTR_DEFAULT
Definition efi_console.c:58
static void efi_putchar(int character)
Print a character to EFI console.
struct console_driver efi_console
Definition efi_fbcon.c:53
EFI_GUID efi_simple_text_input_ex_protocol_guid
Simple text input extension protocol GUID.
Definition efi_guid.c:353
uint16_t mode
Acceleration mode.
Definition ena.h:15
Error codes.
#define ANSIESC_DECTCEM_SET
Show cursor.
Definition ansiesc.h:129
#define ANSIESC_ED
Erase in page.
Definition ansiesc.h:107
#define ANSIESC_DECTCEM_RESET
Hide cursor.
Definition ansiesc.h:132
#define ANSIESC_SGR
Select graphic rendition.
Definition ansiesc.h:119
#define ANSIESC_CUP
Cursor position.
Definition ansiesc.h:104
#define ANSIESC_ED_ALL
Erase whole page.
Definition ansiesc.h:116
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define DBG2(...)
Definition compiler.h:515
#define DBG(...)
Print a debugging message.
Definition compiler.h:498
#define INIT_EARLY
Early initialisation.
Definition init.h:30
static unsigned int count
Number of entries.
Definition dwmac.h:220
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
User interaction.
#define __console_driver
Mark a struct console_driver as being part of the console drivers table.
Definition console.h:134
EFI API.
#define EFI_REQUEST_PROTOCOL(_protocol, _ptr)
Declare an EFI protocol to be requested by iPXE.
Definition efi.h:122
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition efi.h:175
#define efi_open_unsafe(handle, protocol, interface)
Open protocol for unsafe persistent use.
Definition efi.h:459
EFI_SYSTEM_TABLE * efi_systab
String functions.
void * memset(void *dest, int character, size_t len) __nonnull
#define __init_fn(init_order)
Declare an initialisation functon.
Definition init.h:24
unsigned int key_remap(unsigned int character)
Remap a key.
Definition keymap.c:62
Keyboard mappings.
#define KEYMAP_ALTGR
AltGr key flag.
Definition keymap.h:74
#define KEYMAP_CTRL
Ctrl key flag.
Definition keymap.h:56
#define KEYMAP_CAPSLOCK_REDO
Undo and redo CapsLock key flags.
Definition keymap.h:71
uint16_t cx
Definition registers.h:37
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
EFI Boot Services Table.
Definition UefiSpec.h:1931
EFI_CHECK_EVENT CheckEvent
Definition UefiSpec.h:1960
EFI_INPUT_READ_KEY_EX ReadKeyStrokeEx
EFI_INPUT_READ_KEY ReadKeyStroke
EFI_EVENT WaitForKey
Event to use with WaitForEvent() to wait for a key to be available.
EFI_TEXT_ENABLE_CURSOR EnableCursor
EFI_TEXT_CLEAR_SCREEN ClearScreen
EFI_TEXT_SET_CURSOR_POSITION SetCursorPosition
EFI_TEXT_SET_ATTRIBUTE SetAttribute
ANSI escape sequence context.
Definition ansiesc.h:74
A handler for an escape sequence.
Definition ansiesc.h:35
A console driver.
Definition console.h:56
An initialisation function.
Definition init.h:15
A UTF-8 character accumulator.
Definition utf8.h:58
unsigned int character
Character in progress.
Definition utf8.h:60
#define TRUE
Definition tlan.h:46
#define FALSE
Definition tlan.h:45
unsigned int utf8_accumulate(struct utf8_accumulator *utf8, uint8_t byte)
Accumulate Unicode character from UTF-8 byte sequence.
Definition utf8.c:44
UTF-8 Unicode encoding.
#define UTF8_INVALID
Invalid character returned when decoding fails.
Definition utf8.h:55