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>
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>
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 */
67 static EFI_CONSOLE_CONTROL_PROTOCOL *conctrl;
68 EFI_REQUEST_PROTOCOL ( EFI_CONSOLE_CONTROL_PROTOCOL, &conctrl );
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 */
efi_handle_cup(struct ansiesc_context * ctx __unused,unsigned int count __unused,int params[])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 );
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 */
efi_handle_ed(struct ansiesc_context * ctx __unused,unsigned int count __unused,int params[]__unused)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;
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 */
efi_handle_sgr(struct ansiesc_context * ctx __unused,unsigned int count,int params[])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 */
125 };
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 */
131 };
132 unsigned int i;
133 int aspect;
134
135 for ( i = 0 ; i < count ; i++ ) {
136 aspect = params[i];
137 if ( aspect == 0 ) {
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 ];
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 */
efi_handle_dectcem_set(struct ansiesc_context * ctx __unused,unsigned int count __unused,int params[]__unused)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;
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 */
efi_handle_dectcem_reset(struct ansiesc_context * ctx __unused,unsigned int count __unused,int params[]__unused)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;
181
182 conout->EnableCursor ( conout, FALSE );
183 }
184
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 },
192 { 0, NULL }
193 };
194
195 /** EFI console ANSI escape sequence context */
196 static struct ansiesc_context efi_ansiesc_ctx = {
197 .handlers = efi_ansiesc_handlers,
198 };
199
200 /**
201 * Print a character to EFI console
202 *
203 * @v character Character to be printed
204 */
efi_putchar(int character)205 static void efi_putchar ( int character ) {
206 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
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 */
scancode_to_ansi_seq(unsigned int scancode)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 */
efi_getchar(void)279 static int efi_getchar ( void ) {
280 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
281 const char *ansi_seq;
282 EFI_INPUT_KEY key;
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 */
efi_iskey(void)319 static int efi_iskey ( void ) {
320 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
321 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
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 */
336 struct console_driver efi_console __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 */
efi_console_init(void)347 static void efi_console_init ( void ) {
348 EFI_CONSOLE_CONTROL_SCREEN_MODE mode;
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 ) {
357 conctrl->SetMode ( conctrl,
358 EfiConsoleControlScreenText );
359 }
360 }
361 }
362
363 /**
364 * EFI console initialisation function
365 */
366 struct init_fn efi_console_init_fn __init_fn ( INIT_EARLY ) = {
367 .initialise = efi_console_init,
368 };
369