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 
20 FILE_LICENCE ( GPL2_OR_LATER );
21 
22 #include <stddef.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <ipxe/efi/efi.h>
28 #include <ipxe/ansiesc.h>
29 #include <ipxe/console.h>
30 #include <ipxe/init.h>
31 #include <config/console.h>
32 
33 #define ATTR_BOLD 0x08
34 
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
44 
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
54 
55 #define ATTR_DEFAULT ATTR_FCOL_WHITE
56 
57 #define CTRL_MASK 0x1f
58 
59 /* Set default console usage if applicable */
60 #if ! ( defined ( CONSOLE_EFI ) && CONSOLE_EXPLICIT ( CONSOLE_EFI ) )
61 #undef CONSOLE_EFI
62 #define CONSOLE_EFI ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
63 #endif
64 
65 /** Current character attribute */
66 static unsigned int efi_attr = ATTR_DEFAULT;
67 
68 /** Console control protocol */
71 
72 /** Extended simple text input protocol, if present */
74 
75 /**
76  * Handle ANSI CUP (cursor position)
77  *
78  * @v ctx ANSI escape sequence context
79  * @v count Parameter count
80  * @v params[0] Row (1 is top)
81  * @v params[1] Column (1 is left)
82  */
84  unsigned int count __unused, int params[] ) {
86  int cx = ( params[1] - 1 );
87  int cy = ( params[0] - 1 );
88 
89  if ( cx < 0 )
90  cx = 0;
91  if ( cy < 0 )
92  cy = 0;
93 
94  conout->SetCursorPosition ( conout, cx, cy );
95 }
96 
97 /**
98  * Handle ANSI ED (erase in page)
99  *
100  * @v ctx ANSI escape sequence context
101  * @v count Parameter count
102  * @v params[0] Region to erase
103  */
105  unsigned int count __unused,
106  int params[] __unused ) {
108 
109  /* We assume that we always clear the whole screen */
110  assert ( params[0] == ANSIESC_ED_ALL );
111 
112  conout->ClearScreen ( conout );
113 }
114 
115 /**
116  * Handle ANSI SGR (set graphics rendition)
117  *
118  * @v ctx ANSI escape sequence context
119  * @v count Parameter count
120  * @v params List of graphic rendition aspects
121  */
123  unsigned int count, int params[] ) {
125  static const uint8_t efi_attr_fcols[10] = {
129  ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
130  };
131  static const uint8_t efi_attr_bcols[10] = {
135  ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
136  };
137  unsigned int i;
138  int aspect;
139 
140  for ( i = 0 ; i < count ; i++ ) {
141  aspect = params[i];
142  if ( aspect == 0 ) {
144  } else if ( aspect == 1 ) {
145  efi_attr |= ATTR_BOLD;
146  } else if ( aspect == 22 ) {
147  efi_attr &= ~ATTR_BOLD;
148  } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) {
150  efi_attr |= efi_attr_fcols[ aspect - 30 ];
151  } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
153  efi_attr |= efi_attr_bcols[ aspect - 40 ];
154  }
155  }
156 
157  conout->SetAttribute ( conout, efi_attr );
158 }
159 
160 /**
161  * Handle ANSI DECTCEM set (show cursor)
162  *
163  * @v ctx ANSI escape sequence context
164  * @v count Parameter count
165  * @v params List of graphic rendition aspects
166  */
168  unsigned int count __unused,
169  int params[] __unused ) {
171 
172  conout->EnableCursor ( conout, TRUE );
173 }
174 
175 /**
176  * Handle ANSI DECTCEM reset (hide cursor)
177  *
178  * @v ctx ANSI escape sequence context
179  * @v count Parameter count
180  * @v params List of graphic rendition aspects
181  */
183  unsigned int count __unused,
184  int params[] __unused ) {
186 
187  conout->EnableCursor ( conout, FALSE );
188 }
189 
190 /** EFI console ANSI escape sequence handlers */
197  { 0, NULL }
198 };
199 
200 /** EFI console ANSI escape sequence context */
203 };
204 
205 /**
206  * Print a character to EFI console
207  *
208  * @v character Character to be printed
209  */
210 static void efi_putchar ( int character ) {
212  wchar_t wstr[] = { character, 0 };
213 
214  /* Intercept ANSI escape sequences */
215  character = ansiesc_process ( &efi_ansiesc_ctx, character );
216  if ( character < 0 )
217  return;
218 
219  conout->OutputString ( conout, wstr );
220 }
221 
222 /**
223  * Pointer to current ANSI output sequence
224  *
225  * While we are in the middle of returning an ANSI sequence for a
226  * special key, this will point to the next character to return. When
227  * not in the middle of such a sequence, this will point to a NUL
228  * (note: not "will be NULL").
229  */
230 static const char *ansi_input = "";
231 
232 /** Mapping from EFI scan codes to ANSI escape sequences */
233 static const char *ansi_sequences[] = {
234  [SCAN_UP] = "[A",
235  [SCAN_DOWN] = "[B",
236  [SCAN_RIGHT] = "[C",
237  [SCAN_LEFT] = "[D",
238  [SCAN_HOME] = "[H",
239  [SCAN_END] = "[F",
240  [SCAN_INSERT] = "[2~",
241  /* EFI translates an incoming backspace via the serial console
242  * into a SCAN_DELETE. There's not much we can do about this.
243  */
244  [SCAN_DELETE] = "[3~",
245  [SCAN_PAGE_UP] = "[5~",
246  [SCAN_PAGE_DOWN] = "[6~",
247  [SCAN_F5] = "[15~",
248  [SCAN_F6] = "[17~",
249  [SCAN_F7] = "[18~",
250  [SCAN_F8] = "[19~",
251  [SCAN_F9] = "[20~",
252  [SCAN_F10] = "[21~",
253  [SCAN_F11] = "[23~",
254  [SCAN_F12] = "[24~",
255  /* EFI translates some (but not all) incoming escape sequences
256  * via the serial console into equivalent scancodes. When it
257  * doesn't recognise a sequence, it helpfully(!) translates
258  * the initial ESC and passes the remainder through verbatim.
259  * Treating SCAN_ESC as equivalent to an empty escape sequence
260  * works around this bug.
261  */
262  [SCAN_ESC] = "",
263 };
264 
265 /**
266  * Get ANSI escape sequence corresponding to EFI scancode
267  *
268  * @v scancode EFI scancode
269  * @ret ansi_seq ANSI escape sequence, if any, otherwise NULL
270  */
271 static const char * scancode_to_ansi_seq ( unsigned int scancode ) {
272  if ( scancode < ( sizeof ( ansi_sequences ) /
273  sizeof ( ansi_sequences[0] ) ) ) {
274  return ansi_sequences[scancode];
275  }
276  return NULL;
277 }
278 
279 /**
280  * Get character from EFI console
281  *
282  * @ret character Character read from console
283  */
284 static int efi_getchar ( void ) {
287  const char *ansi_seq;
289  EFI_STATUS efirc;
290  int rc;
291 
292  /* If we are mid-sequence, pass out the next byte */
293  if ( *ansi_input )
294  return *(ansi_input++);
295 
296  /* Read key from real EFI console */
297  memset ( &key, 0, sizeof ( key ) );
298  if ( conin_ex ) {
299  if ( ( efirc = conin_ex->ReadKeyStrokeEx ( conin_ex,
300  &key ) ) != 0 ) {
301  rc = -EEFI ( efirc );
302  DBG ( "EFI could not read extended keystroke: %s\n",
303  strerror ( rc ) );
304  return 0;
305  }
306  } else {
307  if ( ( efirc = conin->ReadKeyStroke ( conin,
308  &key.Key ) ) != 0 ) {
309  rc = -EEFI ( efirc );
310  DBG ( "EFI could not read keystroke: %s\n",
311  strerror ( rc ) );
312  return 0;
313  }
314  }
315  DBG2 ( "EFI read key stroke shift %08x toggle %02x unicode %04x "
316  "scancode %04x\n", key.KeyState.KeyShiftState,
317  key.KeyState.KeyToggleState, key.Key.UnicodeChar,
318  key.Key.ScanCode );
319 
320  /* Translate Ctrl-<key> */
321  if ( ( key.KeyState.KeyShiftState & EFI_SHIFT_STATE_VALID ) &&
322  ( key.KeyState.KeyShiftState & ( EFI_LEFT_CONTROL_PRESSED |
324  key.Key.UnicodeChar &= CTRL_MASK;
325  }
326 
327  /* If key has a Unicode representation, return it */
328  if ( key.Key.UnicodeChar )
329  return key.Key.UnicodeChar;
330 
331  /* Otherwise, check for a special key that we know about */
332  if ( ( ansi_seq = scancode_to_ansi_seq ( key.Key.ScanCode ) ) ) {
333  /* Start of escape sequence: return ESC (0x1b) */
334  ansi_input = ansi_seq;
335  return 0x1b;
336  }
337 
338  return 0;
339 }
340 
341 /**
342  * Check for character ready to read from EFI console
343  *
344  * @ret True Character available to read
345  * @ret False No character available to read
346  */
347 static int efi_iskey ( void ) {
351  EFI_EVENT *event;
352  EFI_STATUS efirc;
353 
354  /* If we are mid-sequence, we are always ready */
355  if ( *ansi_input )
356  return 1;
357 
358  /* Check to see if the WaitForKey event has fired */
359  event = ( conin_ex ? conin_ex->WaitForKeyEx : conin->WaitForKey );
360  if ( ( efirc = bs->CheckEvent ( event ) ) == 0 )
361  return 1;
362 
363  return 0;
364 }
365 
366 /** EFI console driver */
368  .putchar = efi_putchar,
369  .getchar = efi_getchar,
370  .iskey = efi_iskey,
371  .usage = CONSOLE_EFI,
372 };
373 
374 /**
375  * Initialise EFI console
376  *
377  */
378 static void efi_console_init ( void ) {
381  union {
382  void *interface;
384  } u;
385  EFI_STATUS efirc;
386  int rc;
387 
388  /* On some older EFI 1.10 implementations, we must use the
389  * (now obsolete) EFI_CONSOLE_CONTROL_PROTOCOL to switch the
390  * console into text mode.
391  */
392  if ( conctrl ) {
393  conctrl->GetMode ( conctrl, &mode, NULL, NULL );
394  if ( mode != EfiConsoleControlScreenText ) {
397  }
398  }
399 
400  /* Attempt to open the Simple Text Input Ex protocol on the
401  * console input handle. This is provably unsafe, but is
402  * apparently the expected behaviour for all UEFI
403  * applications. Don't ask.
404  */
405  if ( ( efirc = bs->OpenProtocol ( efi_systab->ConsoleInHandle,
407  &u.interface, efi_image_handle,
409  EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) == 0 ) {
410  efi_conin_ex = u.wtf;
411  DBG ( "EFI using SimpleTextInputEx\n" );
412  } else {
413  rc = -EEFI ( efirc );
414  DBG ( "EFI has no SimpleTextInputEx: %s\n", strerror ( rc ) );
415  }
416 }
417 
418 /**
419  * EFI console initialisation function
420  */
421 struct init_fn efi_console_init_fn __init_fn ( INIT_EARLY ) = {
423 };
#define ATTR_FCOL_MAGENTA
Definition: efi_console.c:41
struct console_driver efi_console __console_driver
EFI console driver.
Definition: efi_console.c:367
static const char * scancode_to_ansi_seq(unsigned int scancode)
Get ANSI escape sequence corresponding to EFI scancode.
Definition: efi_console.c:271
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2000
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A handler for an escape sequence.
Definition: ansiesc.h:34
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:162
struct ansiesc_handler * handlers
Array of handlers.
Definition: ansiesc.h:79
EFI_TEXT_SET_CURSOR_POSITION SetCursorPosition
void(* initialise)(void)
Definition: init.h:15
#define SCAN_ESC
Definition: SimpleTextIn.h:80
static EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL * efi_conin_ex
Extended simple text input protocol, if present.
Definition: efi_console.c:73
#define ATTR_BCOL_GREEN
Definition: efi_console.c:48
EFI_CONSOLE_CONTROL_SCREEN_MODE
Error codes.
The EFI_SIMPLE_TEXT_INPUT_PROTOCOL is used on the ConsoleIn device.
Definition: SimpleTextIn.h:124
#define INIT_EARLY
Early initialisation.
Definition: init.h:28
#define ATTR_FCOL_CYAN
Definition: efi_console.c:39
VOID * EFI_EVENT
Handle to an event structure.
Definition: UefiBaseType.h:43
static unsigned int efi_attr
Current character attribute.
Definition: efi_console.c:66
EFI_HANDLE ConsoleInHandle
The handle for the active console input device.
Definition: UefiSpec.h:1968
ANSI escape sequence context.
Definition: ansiesc.h:73
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
#define SCAN_LEFT
Definition: SimpleTextIn.h:63
#define CONSOLE_EFI
Definition: efi_console.c:62
#define ATTR_FCOL_GREEN
Definition: efi_console.c:38
#define EFI_RIGHT_CONTROL_PRESSED
#define ATTR_FCOL_MASK
Definition: efi_console.c:35
uint16_t cx
Definition: registers.h:51
static void efi_handle_dectcem_reset(struct ansiesc_context *ctx __unused, unsigned int count __unused, int params[] __unused)
Handle ANSI DECTCEM reset (hide cursor)
Definition: efi_console.c:182
#define SCAN_DOWN
Definition: SimpleTextIn.h:61
EFI_CONSOLE_CONTROL_PROTOCOL_GET_MODE GetMode
EFI_EVENT WaitForKey
Event to use with WaitForEvent() to wait for a key to be available.
Definition: SimpleTextIn.h:130
static EFI_CONSOLE_CONTROL_PROTOCOL * conctrl
Console control protocol.
Definition: efi_console.c:69
The SIMPLE_TEXT_OUTPUT protocol is used to control text-based output devices.
#define ANSIESC_ED
Erase in page.
Definition: ansiesc.h:106
int ansiesc_process(struct ansiesc_context *ctx, int c)
Process character that may be part of ANSI escape sequence.
Definition: ansiesc.c:74
#define ANSIESC_ED_ALL
Erase whole page.
Definition: ansiesc.h:115
#define ATTR_BOLD
Definition: efi_console.c:33
#define ATTR_BCOL_BLUE
Definition: efi_console.c:47
EFI_EVENT WaitForKeyEx
Event to use with WaitForEvent() to wait for a key to be available.
EFI_TEXT_SET_ATTRIBUTE SetAttribute
#define ANSIESC_CUP
Cursor position.
Definition: ansiesc.h:103
#define SCAN_F12
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * ConOut
A pointer to the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL interface that is associated with ConsoleOutHandle.
Definition: UefiSpec.h:1982
An initialisation function.
Definition: init.h:14
void(* putchar)(int character)
Write a character to the console.
Definition: console.h:68
#define SCAN_HOME
Definition: SimpleTextIn.h:64
static int efi_getchar(void)
Get character from EFI console.
Definition: efi_console.c:284
Assertions.
#define ATTR_BCOL_MAGENTA
Definition: efi_console.c:51
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
An object interface.
Definition: interface.h:124
#define SCAN_F6
Definition: SimpleTextIn.h:75
#define SCAN_F10
Definition: SimpleTextIn.h:79
EFI_INPUT_READ_KEY ReadKeyStroke
Definition: SimpleTextIn.h:126
#define ATTR_BCOL_YELLOW
Definition: efi_console.c:52
ANSI escape sequences.
EFI_SIMPLE_TEXT_INPUT_PROTOCOL * ConIn
A pointer to the EFI_SIMPLE_TEXT_INPUT_PROTOCOL interface that is associated with ConsoleInHandle.
Definition: UefiSpec.h:1973
#define ATTR_FCOL_WHITE
Definition: efi_console.c:43
#define ANSIESC_SGR
Select graphic rendition.
Definition: ansiesc.h:118
#define EFI_LEFT_CONTROL_PRESSED
The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL is used on the ConsoleIn device.
#define SCAN_DELETE
Definition: SimpleTextIn.h:67
#define SCAN_F7
Definition: SimpleTextIn.h:76
struct console_driver efi_console
Definition: efi_fbcon.c:51
User interaction.
#define CTRL_MASK
Definition: efi_console.c:57
#define EFI_OPEN_PROTOCOL_GET_PROTOCOL
Definition: UefiSpec.h:1271
#define ATTR_FCOL_YELLOW
Definition: efi_console.c:42
#define SCAN_PAGE_DOWN
Definition: SimpleTextIn.h:69
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define SCAN_F9
Definition: SimpleTextIn.h:78
#define EFI_SHIFT_STATE_VALID
EFI Boot Services Table.
Definition: UefiSpec.h:1836
EFI_HANDLE efi_image_handle
Image handle passed to entry point.
Definition: efi_init.c:33
static void efi_console_init(void)
Initialise EFI console.
Definition: efi_console.c:378
static void efi_handle_dectcem_set(struct ansiesc_context *ctx __unused, unsigned int count __unused, int params[] __unused)
Handle ANSI DECTCEM set (show cursor)
Definition: efi_console.c:167
EFI_TEXT_CLEAR_SCREEN ClearScreen
unsigned char uint8_t
Definition: stdint.h:10
static void efi_handle_sgr(struct ansiesc_context *ctx __unused, unsigned int count, int params[])
Handle ANSI SGR (set graphics rendition)
Definition: efi_console.c:122
#define SCAN_UP
Definition: SimpleTextIn.h:60
struct init_fn efi_console_init_fn __init_fn(INIT_EARLY)
EFI console initialisation function.
#define SCAN_F11
EFI_GUID efi_simple_text_input_ex_protocol_guid
Simple text input extension protocol GUID.
Definition: efi_guid.c:248
EFI_TEXT_ENABLE_CURSOR EnableCursor
Console configuration.
#define TRUE
Definition: tlan.h:46
#define ATTR_BCOL_RED
Definition: efi_console.c:50
EFI API.
#define ATTR_BCOL_MASK
Definition: efi_console.c:45
#define ANSIESC_DECTCEM_RESET
Hide cursor.
Definition: ansiesc.h:131
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
#define SCAN_INSERT
Definition: SimpleTextIn.h:66
#define ANSIESC_DECTCEM_SET
Show cursor.
Definition: ansiesc.h:128
FILE_LICENCE(GPL2_OR_LATER)
#define ATTR_FCOL_BLACK
Definition: efi_console.c:36
static void efi_putchar(int character)
Print a character to EFI console.
Definition: efi_console.c:210
EFI_CHECK_EVENT CheckEvent
Definition: UefiSpec.h:1865
#define ATTR_FCOL_BLUE
Definition: efi_console.c:37
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:35
#define ATTR_BCOL_WHITE
Definition: efi_console.c:53
uint16_t count
Number of entries.
Definition: ena.h:22
#define SCAN_END
Definition: SimpleTextIn.h:65
#define SCAN_RIGHT
Definition: SimpleTextIn.h:62
EFI_INPUT_READ_KEY_EX ReadKeyStrokeEx
union @17 u
#define FALSE
Definition: tlan.h:45
static struct ansiesc_handler efi_ansiesc_handlers[]
EFI console ANSI escape sequence handlers.
Definition: efi_console.c:191
static struct ansiesc_context efi_ansiesc_ctx
EFI console ANSI escape sequence context.
Definition: efi_console.c:201
static int efi_iskey(void)
Check for character ready to read from EFI console.
Definition: efi_console.c:347
EFI_REQUEST_PROTOCOL(EFI_CONSOLE_CONTROL_PROTOCOL, &conctrl)
EFI_SYSTEM_TABLE * efi_systab
EFI_OPEN_PROTOCOL OpenProtocol
Definition: UefiSpec.h:1905
static const char * ansi_input
Pointer to current ANSI output sequence.
Definition: efi_console.c:230
A console driver.
Definition: console.h:55
#define ATTR_BCOL_CYAN
Definition: efi_console.c:49
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
EFI_CONSOLE_CONTROL_PROTOCOL_SET_MODE SetMode
#define ATTR_DEFAULT
Definition: efi_console.c:55
#define SCAN_PAGE_UP
Definition: SimpleTextIn.h:68
static void efi_handle_ed(struct ansiesc_context *ctx __unused, unsigned int count __unused, int params[] __unused)
Handle ANSI ED (erase in page)
Definition: efi_console.c:104
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
String functions.
static const char * ansi_sequences[]
Mapping from EFI scan codes to ANSI escape sequences.
Definition: efi_console.c:233
#define SCAN_F5
Definition: SimpleTextIn.h:74
#define ATTR_BCOL_BLACK
Definition: efi_console.c:46
union @376 key
Sense key.
Definition: scsi.h:18
#define ATTR_FCOL_RED
Definition: efi_console.c:40
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:83
#define DBG2(...)
Definition: compiler.h:515
#define SCAN_F8
Definition: SimpleTextIn.h:77
void * memset(void *dest, int character, size_t len) __nonnull