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 /* Set default console usage if applicable */
58 #if ! ( defined ( CONSOLE_EFI ) && CONSOLE_EXPLICIT ( CONSOLE_EFI ) )
59 #undef CONSOLE_EFI
60 #define CONSOLE_EFI ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
61 #endif
62 
63 /** Current character attribute */
64 static unsigned int efi_attr = ATTR_DEFAULT;
65 
66 /** Console control protocol */
69 
70 /**
71  * Handle ANSI CUP (cursor position)
72  *
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)
77  */
79  unsigned int count __unused, int params[] ) {
81  int cx = ( params[1] - 1 );
82  int cy = ( params[0] - 1 );
83 
84  if ( cx < 0 )
85  cx = 0;
86  if ( cy < 0 )
87  cy = 0;
88 
89  conout->SetCursorPosition ( conout, cx, cy );
90 }
91 
92 /**
93  * Handle ANSI ED (erase in page)
94  *
95  * @v ctx ANSI escape sequence context
96  * @v count Parameter count
97  * @v params[0] Region to erase
98  */
99 static void efi_handle_ed ( struct ansiesc_context *ctx __unused,
100  unsigned int count __unused,
101  int params[] __unused ) {
103 
104  /* We assume that we always clear the whole screen */
105  assert ( params[0] == ANSIESC_ED_ALL );
106 
107  conout->ClearScreen ( conout );
108 }
109 
110 /**
111  * Handle ANSI SGR (set graphics rendition)
112  *
113  * @v ctx ANSI escape sequence context
114  * @v count Parameter count
115  * @v params List of graphic rendition aspects
116  */
118  unsigned int count, int params[] ) {
120  static const uint8_t efi_attr_fcols[10] = {
124  ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
125  };
126  static const uint8_t efi_attr_bcols[10] = {
130  ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
131  };
132  unsigned int i;
133  int aspect;
134 
135  for ( i = 0 ; i < count ; i++ ) {
136  aspect = params[i];
137  if ( aspect == 0 ) {
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 ) ) {
145  efi_attr |= efi_attr_fcols[ aspect - 30 ];
146  } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
148  efi_attr |= efi_attr_bcols[ aspect - 40 ];
149  }
150  }
151 
152  conout->SetAttribute ( conout, efi_attr );
153 }
154 
155 /**
156  * Handle ANSI DECTCEM set (show cursor)
157  *
158  * @v ctx ANSI escape sequence context
159  * @v count Parameter count
160  * @v params List of graphic rendition aspects
161  */
163  unsigned int count __unused,
164  int params[] __unused ) {
166 
167  conout->EnableCursor ( conout, TRUE );
168 }
169 
170 /**
171  * Handle ANSI DECTCEM reset (hide cursor)
172  *
173  * @v ctx ANSI escape sequence context
174  * @v count Parameter count
175  * @v params List of graphic rendition aspects
176  */
178  unsigned int count __unused,
179  int params[] __unused ) {
181 
182  conout->EnableCursor ( conout, FALSE );
183 }
184 
185 /** EFI console ANSI escape sequence handlers */
192  { 0, NULL }
193 };
194 
195 /** EFI console ANSI escape sequence context */
198 };
199 
200 /**
201  * Print a character to EFI console
202  *
203  * @v character Character to be printed
204  */
205 static void efi_putchar ( int character ) {
207  wchar_t wstr[] = { character, 0 };
208 
209  /* Intercept ANSI escape sequences */
210  character = ansiesc_process ( &efi_ansiesc_ctx, character );
211  if ( character < 0 )
212  return;
213 
214  conout->OutputString ( conout, wstr );
215 }
216 
217 /**
218  * Pointer to current ANSI output sequence
219  *
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").
224  */
225 static const char *ansi_input = "";
226 
227 /** Mapping from EFI scan codes to ANSI escape sequences */
228 static const char *ansi_sequences[] = {
229  [SCAN_UP] = "[A",
230  [SCAN_DOWN] = "[B",
231  [SCAN_RIGHT] = "[C",
232  [SCAN_LEFT] = "[D",
233  [SCAN_HOME] = "[H",
234  [SCAN_END] = "[F",
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.
238  */
239  [SCAN_DELETE] = "[3~",
240  [SCAN_PAGE_UP] = "[5~",
241  [SCAN_PAGE_DOWN] = "[6~",
242  [SCAN_F5] = "[15~",
243  [SCAN_F6] = "[17~",
244  [SCAN_F7] = "[18~",
245  [SCAN_F8] = "[19~",
246  [SCAN_F9] = "[20~",
247  [SCAN_F10] = "[21~",
248  [SCAN_F11] = "[23~",
249  [SCAN_F12] = "[24~",
250  /* EFI translates some (but not all) incoming escape sequences
251  * via the serial console into equivalent scancodes. When it
252  * doesn't recognise a sequence, it helpfully(!) translates
253  * the initial ESC and passes the remainder through verbatim.
254  * Treating SCAN_ESC as equivalent to an empty escape sequence
255  * works around this bug.
256  */
257  [SCAN_ESC] = "",
258 };
259 
260 /**
261  * Get ANSI escape sequence corresponding to EFI scancode
262  *
263  * @v scancode EFI scancode
264  * @ret ansi_seq ANSI escape sequence, if any, otherwise NULL
265  */
266 static const char * scancode_to_ansi_seq ( unsigned int scancode ) {
267  if ( scancode < ( sizeof ( ansi_sequences ) /
268  sizeof ( ansi_sequences[0] ) ) ) {
269  return ansi_sequences[scancode];
270  }
271  return NULL;
272 }
273 
274 /**
275  * Get character from EFI console
276  *
277  * @ret character Character read from console
278  */
279 static int efi_getchar ( void ) {
281  const char *ansi_seq;
283  EFI_STATUS efirc;
284  int rc;
285 
286  /* If we are mid-sequence, pass out the next byte */
287  if ( *ansi_input )
288  return *(ansi_input++);
289 
290  /* Read key from real EFI console */
291  if ( ( efirc = conin->ReadKeyStroke ( conin, &key ) ) != 0 ) {
292  rc = -EEFI ( efirc );
293  DBG ( "EFI could not read keystroke: %s\n", strerror ( rc ) );
294  return 0;
295  }
296  DBG2 ( "EFI read key stroke with unicode %04x scancode %04x\n",
297  key.UnicodeChar, key.ScanCode );
298 
299  /* If key has a Unicode representation, return it */
300  if ( key.UnicodeChar )
301  return key.UnicodeChar;
302 
303  /* Otherwise, check for a special key that we know about */
304  if ( ( ansi_seq = scancode_to_ansi_seq ( key.ScanCode ) ) ) {
305  /* Start of escape sequence: return ESC (0x1b) */
306  ansi_input = ansi_seq;
307  return 0x1b;
308  }
309 
310  return 0;
311 }
312 
313 /**
314  * Check for character ready to read from EFI console
315  *
316  * @ret True Character available to read
317  * @ret False No character available to read
318  */
319 static int efi_iskey ( void ) {
322  EFI_STATUS efirc;
323 
324  /* If we are mid-sequence, we are always ready */
325  if ( *ansi_input )
326  return 1;
327 
328  /* Check to see if the WaitForKey event has fired */
329  if ( ( efirc = bs->CheckEvent ( conin->WaitForKey ) ) == 0 )
330  return 1;
331 
332  return 0;
333 }
334 
335 /** EFI console driver */
337  .putchar = efi_putchar,
338  .getchar = efi_getchar,
339  .iskey = efi_iskey,
340  .usage = CONSOLE_EFI,
341 };
342 
343 /**
344  * Initialise EFI console
345  *
346  */
347 static void efi_console_init ( void ) {
349 
350  /* On some older EFI 1.10 implementations, we must use the
351  * (now obsolete) EFI_CONSOLE_CONTROL_PROTOCOL to switch the
352  * console into text mode.
353  */
354  if ( conctrl ) {
355  conctrl->GetMode ( conctrl, &mode, NULL, NULL );
356  if ( mode != EfiConsoleControlScreenText ) {
359  }
360  }
361 }
362 
363 /**
364  * EFI console initialisation function
365  */
366 struct init_fn efi_console_init_fn __init_fn ( INIT_EARLY ) = {
368 };
#define ATTR_FCOL_MAGENTA
Definition: efi_console.c:41
struct console_driver efi_console __console_driver
EFI console driver.
Definition: efi_console.c:336
static const char * scancode_to_ansi_seq(unsigned int scancode)
Get ANSI escape sequence corresponding to EFI scancode.
Definition: efi_console.c:266
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:157
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
#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
static unsigned int efi_attr
Current character attribute.
Definition: efi_console.c:64
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:60
#define ATTR_FCOL_GREEN
Definition: efi_console.c:38
#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:177
#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:67
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_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:279
Assertions.
#define ATTR_BCOL_MAGENTA
Definition: efi_console.c:51
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#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 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 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
EFI Boot Services Table.
Definition: UefiSpec.h:1836
static void efi_console_init(void)
Initialise EFI console.
Definition: efi_console.c:347
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:162
EFI_TEXT_CLEAR_SCREEN ClearScreen
The keystroke information for the key that was pressed.
Definition: SimpleTextIn.h:43
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:117
#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_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:205
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
#define FALSE
Definition: tlan.h:45
static struct ansiesc_handler efi_ansiesc_handlers[]
EFI console ANSI escape sequence handlers.
Definition: efi_console.c:186
static struct ansiesc_context efi_ansiesc_ctx
EFI console ANSI escape sequence context.
Definition: efi_console.c:196
static int efi_iskey(void)
Check for character ready to read from EFI console.
Definition: efi_console.c:319
EFI_REQUEST_PROTOCOL(EFI_CONSOLE_CONTROL_PROTOCOL, &conctrl)
EFI_SYSTEM_TABLE * efi_systab
static const char * ansi_input
Pointer to current ANSI output sequence.
Definition: efi_console.c:225
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:99
#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:228
#define SCAN_F5
Definition: SimpleTextIn.h:74
#define ATTR_BCOL_BLACK
Definition: efi_console.c:46
union @375 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:78
#define DBG2(...)
Definition: compiler.h:515
#define SCAN_F8
Definition: SimpleTextIn.h:77