blob: 7274d7520459b2801ee65477cc22ae0569ffd908 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafc1311ad2016-03-04 01:10:00 +01002/*
3 * EFI application console interface
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafc1311ad2016-03-04 01:10:00 +01006 */
7
8#include <common.h>
Rob Clark78178bb2017-09-09 06:47:40 -04009#include <charset.h>
Rob Clarka18c5a82017-09-13 18:05:43 -040010#include <dm/device.h>
Alexander Grafc1311ad2016-03-04 01:10:00 +010011#include <efi_loader.h>
Rob Clarka18c5a82017-09-13 18:05:43 -040012#include <stdio_dev.h>
13#include <video_console.h>
Alexander Grafc1311ad2016-03-04 01:10:00 +010014
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +010015#define EFI_COUT_MODE_2 2
16#define EFI_MAX_COUT_MODE 3
17
18struct cout_mode {
19 unsigned long columns;
20 unsigned long rows;
21 int present;
22};
23
24static struct cout_mode efi_cout_modes[] = {
25 /* EFI Mode 0 is 80x25 and always present */
26 {
27 .columns = 80,
28 .rows = 25,
29 .present = 1,
30 },
31 /* EFI Mode 1 is always 80x50 */
32 {
33 .columns = 80,
34 .rows = 50,
35 .present = 0,
36 },
37 /* Value are unknown until we query the console */
38 {
39 .columns = 0,
40 .rows = 0,
41 .present = 0,
42 },
43};
44
Heinrich Schuchardt110c6282018-09-11 22:38:08 +020045const efi_guid_t efi_guid_text_input_ex_protocol =
46 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +020047const efi_guid_t efi_guid_text_input_protocol =
48 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +020049const efi_guid_t efi_guid_text_output_protocol =
50 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
Alexander Grafc1311ad2016-03-04 01:10:00 +010051
52#define cESC '\x1b'
53#define ESC "\x1b"
54
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +010055/* Default to mode 0 */
Alexander Grafc1311ad2016-03-04 01:10:00 +010056static struct simple_text_output_mode efi_con_mode = {
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +010057 .max_mode = 1,
Alexander Grafc1311ad2016-03-04 01:10:00 +010058 .mode = 0,
59 .attribute = 0,
60 .cursor_column = 0,
61 .cursor_row = 0,
62 .cursor_visible = 1,
63};
64
Heinrich Schuchardt62217292018-05-16 18:17:38 +020065/*
66 * Receive and parse a reply from the terminal.
67 *
68 * @n: array of return values
69 * @num: number of return values expected
70 * @end_char: character indicating end of terminal message
71 * @return: non-zero indicates error
72 */
73static int term_read_reply(int *n, int num, char end_char)
Alexander Grafc1311ad2016-03-04 01:10:00 +010074{
75 char c;
76 int i = 0;
77
78 c = getc();
79 if (c != cESC)
80 return -1;
81 c = getc();
82 if (c != '[')
83 return -1;
84
85 n[0] = 0;
86 while (1) {
87 c = getc();
88 if (c == ';') {
89 i++;
Heinrich Schuchardt62217292018-05-16 18:17:38 +020090 if (i >= num)
Alexander Grafc1311ad2016-03-04 01:10:00 +010091 return -1;
92 n[i] = 0;
93 continue;
94 } else if (c == end_char) {
95 break;
96 } else if (c > '9' || c < '0') {
97 return -1;
98 }
99
100 /* Read one more decimal position */
101 n[i] *= 10;
102 n[i] += c - '0';
103 }
Heinrich Schuchardt62217292018-05-16 18:17:38 +0200104 if (i != num - 1)
105 return -1;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100106
107 return 0;
108}
109
Alexander Grafc1311ad2016-03-04 01:10:00 +0100110static efi_status_t EFIAPI efi_cout_output_string(
111 struct efi_simple_text_output_protocol *this,
Rob Clark3a45bc72017-09-13 18:05:44 -0400112 const efi_string_t string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100113{
Rob Clark3a45bc72017-09-13 18:05:44 -0400114 struct simple_text_output_mode *con = &efi_con_mode;
115 struct cout_mode *mode = &efi_cout_modes[con->mode];
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200116 char *buf, *pos;
117 u16 *p;
118 efi_status_t ret = EFI_SUCCESS;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100119
120 EFI_ENTRY("%p, %p", this, string);
Rob Clark3a45bc72017-09-13 18:05:44 -0400121
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200122 buf = malloc(utf16_utf8_strlen(string) + 1);
123 if (!buf) {
124 ret = EFI_OUT_OF_RESOURCES;
125 goto out;
126 }
127 pos = buf;
128 utf16_utf8_strcpy(&pos, string);
Rob Clark3a45bc72017-09-13 18:05:44 -0400129 fputs(stdout, buf);
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200130 free(buf);
Rob Clark3a45bc72017-09-13 18:05:44 -0400131
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200132 /*
133 * Update the cursor position.
134 *
135 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
136 * and U000D. All other characters, including control characters
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +0200137 * U+0007 (BEL) and U+0009 (TAB), have to increase the column by one.
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200138 */
139 for (p = string; *p; ++p) {
Rob Clark3a45bc72017-09-13 18:05:44 -0400140 switch (*p) {
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200141 case '\b': /* U+0008, backspace */
142 con->cursor_column = max(0, con->cursor_column - 1);
Rob Clark3a45bc72017-09-13 18:05:44 -0400143 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200144 case '\n': /* U+000A, newline */
Rob Clark3a45bc72017-09-13 18:05:44 -0400145 con->cursor_column = 0;
146 con->cursor_row++;
147 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200148 case '\r': /* U+000D, carriage-return */
149 con->cursor_column = 0;
Rob Clark3a45bc72017-09-13 18:05:44 -0400150 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200151 case 0xd800 ... 0xdbff:
152 /*
153 * Ignore high surrogates, we do not want to count a
154 * Unicode character twice.
155 */
Rob Clark3a45bc72017-09-13 18:05:44 -0400156 break;
157 default:
158 con->cursor_column++;
159 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100160 }
Rob Clark3a45bc72017-09-13 18:05:44 -0400161 if (con->cursor_column >= mode->columns) {
162 con->cursor_column = 0;
163 con->cursor_row++;
164 }
165 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100166 }
167
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200168out:
169 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100170}
171
172static efi_status_t EFIAPI efi_cout_test_string(
173 struct efi_simple_text_output_protocol *this,
Rob Clark3a45bc72017-09-13 18:05:44 -0400174 const efi_string_t string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100175{
176 EFI_ENTRY("%p, %p", this, string);
177 return EFI_EXIT(EFI_SUCCESS);
178}
179
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100180static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
181{
182 if (!mode->present)
183 return false;
184
185 return (mode->rows == rows) && (mode->columns == cols);
186}
187
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200188/**
189 * query_console_serial() - query console size
190 *
191 * @rows pointer to return number of rows
192 * @columns pointer to return number of columns
193 * Returns 0 on success
194 */
Rob Clark71cc25c2017-09-13 18:05:42 -0400195static int query_console_serial(int *rows, int *cols)
196{
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200197 int ret = 0;
198 int n[2];
Rob Clark71cc25c2017-09-13 18:05:42 -0400199 u64 timeout;
200
201 /* Empty input buffer */
202 while (tstc())
203 getc();
204
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200205 /*
206 * Not all terminals understand CSI [18t for querying the console size.
207 * We should adhere to escape sequences documented in the console_codes
208 * manpage and the ECMA-48 standard.
209 *
210 * So here we follow a different approach. We position the cursor to the
211 * bottom right and query its position. Before leaving the function we
212 * restore the original cursor position.
213 */
214 printf(ESC "7" /* Save cursor position */
215 ESC "[r" /* Set scrolling region to full window */
216 ESC "[999;999H" /* Move to bottom right corner */
217 ESC "[6n"); /* Query cursor position */
Rob Clark71cc25c2017-09-13 18:05:42 -0400218
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200219 /* Allow up to one second for a response */
Rob Clark71cc25c2017-09-13 18:05:42 -0400220 timeout = timer_get_us() + 1000000;
221 while (!tstc())
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200222 if (timer_get_us() > timeout) {
223 ret = -1;
224 goto out;
225 }
Rob Clark71cc25c2017-09-13 18:05:42 -0400226
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200227 /* Read {rows,cols} */
228 if (term_read_reply(n, 2, 'R')) {
229 ret = 1;
230 goto out;
231 }
Rob Clark71cc25c2017-09-13 18:05:42 -0400232
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200233 *cols = n[1];
234 *rows = n[0];
235out:
236 printf(ESC "8"); /* Restore cursor position */
237 return ret;
Rob Clark71cc25c2017-09-13 18:05:42 -0400238}
239
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200240/*
241 * Update the mode table.
242 *
243 * By default the only mode available is 80x25. If the console has at least 50
244 * lines, enable mode 80x50. If we can query the console size and it is neither
245 * 80x25 nor 80x50, set it as an additional mode.
246 */
247static void query_console_size(void)
248{
249 const char *stdout_name = env_get("stdout");
Alexander Graf80483b22018-06-03 15:51:17 +0200250 int rows = 25, cols = 80;
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200251
252 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
253 IS_ENABLED(CONFIG_DM_VIDEO)) {
254 struct stdio_dev *stdout_dev =
255 stdio_get_by_name("vidconsole");
256 struct udevice *dev = stdout_dev->priv;
257 struct vidconsole_priv *priv =
258 dev_get_uclass_priv(dev);
259 rows = priv->rows;
260 cols = priv->cols;
261 } else if (query_console_serial(&rows, &cols)) {
262 return;
263 }
264
265 /* Test if we can have Mode 1 */
266 if (cols >= 80 && rows >= 50) {
267 efi_cout_modes[1].present = 1;
268 efi_con_mode.max_mode = 2;
269 }
270
271 /*
272 * Install our mode as mode 2 if it is different
273 * than mode 0 or 1 and set it as the currently selected mode
274 */
275 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
276 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
277 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
278 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
279 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
280 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
281 efi_con_mode.mode = EFI_COUT_MODE_2;
282 }
283}
284
Alexander Grafc1311ad2016-03-04 01:10:00 +0100285static efi_status_t EFIAPI efi_cout_query_mode(
286 struct efi_simple_text_output_protocol *this,
287 unsigned long mode_number, unsigned long *columns,
288 unsigned long *rows)
289{
290 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
291
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100292 if (mode_number >= efi_con_mode.max_mode)
293 return EFI_EXIT(EFI_UNSUPPORTED);
294
295 if (efi_cout_modes[mode_number].present != 1)
296 return EFI_EXIT(EFI_UNSUPPORTED);
297
Alexander Grafc1311ad2016-03-04 01:10:00 +0100298 if (columns)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100299 *columns = efi_cout_modes[mode_number].columns;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100300 if (rows)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100301 *rows = efi_cout_modes[mode_number].rows;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100302
303 return EFI_EXIT(EFI_SUCCESS);
304}
305
306static efi_status_t EFIAPI efi_cout_set_mode(
307 struct efi_simple_text_output_protocol *this,
308 unsigned long mode_number)
309{
310 EFI_ENTRY("%p, %ld", this, mode_number);
311
Alexander Grafc1311ad2016-03-04 01:10:00 +0100312
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100313 if (mode_number > efi_con_mode.max_mode)
314 return EFI_EXIT(EFI_UNSUPPORTED);
315
316 efi_con_mode.mode = mode_number;
317 efi_con_mode.cursor_column = 0;
318 efi_con_mode.cursor_row = 0;
319
320 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100321}
322
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400323static const struct {
324 unsigned int fg;
325 unsigned int bg;
326} color[] = {
327 { 30, 40 }, /* 0: black */
328 { 34, 44 }, /* 1: blue */
329 { 32, 42 }, /* 2: green */
330 { 36, 46 }, /* 3: cyan */
331 { 31, 41 }, /* 4: red */
332 { 35, 45 }, /* 5: magenta */
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +0200333 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
334 { 37, 47 }, /* 7: light gray, map to white */
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400335};
336
337/* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100338static efi_status_t EFIAPI efi_cout_set_attribute(
339 struct efi_simple_text_output_protocol *this,
340 unsigned long attribute)
341{
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400342 unsigned int bold = EFI_ATTR_BOLD(attribute);
343 unsigned int fg = EFI_ATTR_FG(attribute);
344 unsigned int bg = EFI_ATTR_BG(attribute);
345
Alexander Grafc1311ad2016-03-04 01:10:00 +0100346 EFI_ENTRY("%p, %lx", this, attribute);
347
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400348 if (attribute)
349 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
350 else
351 printf(ESC"[0;37;40m");
352
353 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100354}
355
356static efi_status_t EFIAPI efi_cout_clear_screen(
357 struct efi_simple_text_output_protocol *this)
358{
359 EFI_ENTRY("%p", this);
360
361 printf(ESC"[2J");
Heinrich Schuchardte67ff942018-07-05 08:18:00 +0200362 efi_con_mode.cursor_column = 0;
363 efi_con_mode.cursor_row = 0;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100364
365 return EFI_EXIT(EFI_SUCCESS);
366}
367
Heinrich Schuchardt9d12daf2018-07-05 19:58:07 +0200368static efi_status_t EFIAPI efi_cout_reset(
369 struct efi_simple_text_output_protocol *this,
370 char extended_verification)
371{
372 EFI_ENTRY("%p, %d", this, extended_verification);
373
374 /* Clear screen */
375 EFI_CALL(efi_cout_clear_screen(this));
376 /* Set default colors */
377 printf(ESC "[0;37;40m");
378
379 return EFI_EXIT(EFI_SUCCESS);
380}
381
Alexander Grafc1311ad2016-03-04 01:10:00 +0100382static efi_status_t EFIAPI efi_cout_set_cursor_position(
383 struct efi_simple_text_output_protocol *this,
384 unsigned long column, unsigned long row)
385{
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200386 efi_status_t ret = EFI_SUCCESS;
387 struct simple_text_output_mode *con = &efi_con_mode;
388 struct cout_mode *mode = &efi_cout_modes[con->mode];
389
Alexander Grafc1311ad2016-03-04 01:10:00 +0100390 EFI_ENTRY("%p, %ld, %ld", this, column, row);
391
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200392 /* Check parameters */
393 if (!this) {
394 ret = EFI_INVALID_PARAMETER;
395 goto out;
396 }
397 if (row >= mode->rows || column >= mode->columns) {
398 ret = EFI_UNSUPPORTED;
399 goto out;
400 }
401
402 /*
403 * Set cursor position by sending CSI H.
404 * EFI origin is [0, 0], terminal origin is [1, 1].
405 */
406 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100407 efi_con_mode.cursor_column = column;
408 efi_con_mode.cursor_row = row;
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200409out:
410 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100411}
412
413static efi_status_t EFIAPI efi_cout_enable_cursor(
414 struct efi_simple_text_output_protocol *this,
415 bool enable)
416{
417 EFI_ENTRY("%p, %d", this, enable);
418
419 printf(ESC"[?25%c", enable ? 'h' : 'l');
420
421 return EFI_EXIT(EFI_SUCCESS);
422}
423
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200424struct efi_simple_text_output_protocol efi_con_out = {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100425 .reset = efi_cout_reset,
426 .output_string = efi_cout_output_string,
427 .test_string = efi_cout_test_string,
428 .query_mode = efi_cout_query_mode,
429 .set_mode = efi_cout_set_mode,
430 .set_attribute = efi_cout_set_attribute,
431 .clear_screen = efi_cout_clear_screen,
432 .set_cursor_position = efi_cout_set_cursor_position,
433 .enable_cursor = efi_cout_enable_cursor,
434 .mode = (void*)&efi_con_mode,
435};
436
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200437/**
438 * struct efi_cin_notify_function - registered console input notify function
439 *
440 * @link: link to list
441 * @data: key to notify
442 * @function: function to call
443 */
444struct efi_cin_notify_function {
445 struct list_head link;
446 struct efi_key_data key;
447 efi_status_t (EFIAPI *function)
448 (struct efi_key_data *key_data);
449};
450
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200451static bool key_available;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200452static struct efi_key_data next_key;
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200453static LIST_HEAD(cin_notify_functions);
Heinrich Schuchardt4f187892018-07-05 08:17:59 +0200454
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200455/**
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200456 * set_shift_mask() - set shift mask
457 *
458 * @mod: Xterm shift mask
459 */
460void set_shift_mask(int mod, struct efi_key_state *key_state)
461{
462 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
463 if (mod) {
464 --mod;
465 if (mod & 1)
466 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
467 if (mod & 2)
468 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
469 if (mod & 4)
470 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
471 if (mod & 8)
472 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
473 } else {
474 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
475 }
476}
477
478/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200479 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200480 *
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100481 * This gets called when we have already parsed CSI.
482 *
483 * @modifiers: bitmask (shift, alt, ctrl)
484 * @return: the unmodified code
485 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200486static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100487{
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200488 int c, mod = 0, ret = 0;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100489
490 c = getc();
491
492 if (c != ';') {
493 ret = c;
494 if (c == '~')
495 goto out;
496 c = getc();
497 }
498 for (;;) {
499 switch (c) {
500 case '0'...'9':
501 mod *= 10;
502 mod += c - '0';
503 /* fall through */
504 case ';':
505 c = getc();
506 break;
507 default:
508 goto out;
509 }
510 }
511out:
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200512 set_shift_mask(mod, key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100513 if (!ret)
514 ret = c;
515 return ret;
516}
517
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200518/**
519 * efi_cin_read_key() - read a key from the console input
520 *
521 * @key: - key received
522 * Return: - status code
523 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200524static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100525{
526 struct efi_input_key pressed_key = {
527 .scan_code = 0,
528 .unicode_char = 0,
529 };
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200530 s32 ch;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100531
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200532 if (console_read_unicode(&ch))
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200533 return EFI_NOT_READY;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200534
535 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
536 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
537
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200538 /* We do not support multi-word codes */
539 if (ch >= 0x10000)
540 ch = '?';
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200541
542 switch (ch) {
543 case 0x1b:
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100544 /*
545 * Xterm Control Sequences
546 * https://www.xfree86.org/4.8.0/ctlseqs.html
547 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100548 ch = getc();
549 switch (ch) {
550 case cESC: /* ESC */
551 pressed_key.scan_code = 23;
552 break;
553 case 'O': /* F1 - F4 */
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100554 ch = getc();
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200555 /* consider modifiers */
556 if (ch < 'P') {
557 set_shift_mask(ch - '0', &key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100558 ch = getc();
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200559 }
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100560 pressed_key.scan_code = ch - 'P' + 11;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100561 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100562 case '[':
563 ch = getc();
564 switch (ch) {
565 case 'A'...'D': /* up, down right, left */
566 pressed_key.scan_code = ch - 'A' + 1;
567 break;
568 case 'F': /* End */
569 pressed_key.scan_code = 6;
570 break;
571 case 'H': /* Home */
572 pressed_key.scan_code = 5;
573 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100574 case '1':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200575 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100576 switch (ch) {
577 case '1'...'5': /* F1 - F5 */
578 pressed_key.scan_code = ch - '1' + 11;
579 break;
580 case '7'...'9': /* F6 - F8 */
581 pressed_key.scan_code = ch - '7' + 16;
582 break;
583 case 'A'...'D': /* up, down right, left */
584 pressed_key.scan_code = ch - 'A' + 1;
585 break;
586 case 'F':
587 pressed_key.scan_code = 6; /* End */
588 break;
589 case 'H':
590 pressed_key.scan_code = 5; /* Home */
591 break;
592 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100593 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100594 case '2':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200595 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100596 switch (ch) {
597 case '0'...'1': /* F9 - F10 */
598 pressed_key.scan_code = ch - '0' + 19;
599 break;
600 case '3'...'4': /* F11 - F12 */
601 pressed_key.scan_code = ch - '3' + 21;
602 break;
603 case '~': /* INS */
604 pressed_key.scan_code = 7;
605 break;
606 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100607 break;
608 case '3': /* DEL */
609 pressed_key.scan_code = 8;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200610 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100611 break;
612 case '5': /* PG UP */
613 pressed_key.scan_code = 9;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200614 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100615 break;
616 case '6': /* PG DOWN */
617 pressed_key.scan_code = 10;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200618 analyze_modifiers(&key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100619 break;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200620 } /* [ */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100621 break;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200622 default:
623 /* ALT key */
624 set_shift_mask(3, &key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100625 }
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200626 break;
627 case 0x7f:
Alexander Grafc1311ad2016-03-04 01:10:00 +0100628 /* Backspace */
629 ch = 0x08;
630 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200631 if (pressed_key.scan_code) {
632 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
633 } else {
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100634 pressed_key.unicode_char = ch;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200635
636 /*
637 * Assume left control key for control characters typically
638 * entered using the control key.
639 */
640 if (ch >= 0x01 && ch <= 0x1f) {
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200641 key->key_state.key_shift_state |=
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200642 EFI_SHIFT_STATE_VALID;
643 switch (ch) {
644 case 0x01 ... 0x07:
645 case 0x0b ... 0x0c:
646 case 0x0e ... 0x1f:
647 key->key_state.key_shift_state |=
648 EFI_LEFT_CONTROL_PRESSED;
649 }
650 }
651 }
652 key->key = pressed_key;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100653
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200654 return EFI_SUCCESS;
655}
656
657/**
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200658 * efi_cin_notify() - notify registered functions
659 */
660static void efi_cin_notify(void)
661{
662 struct efi_cin_notify_function *item;
663
664 list_for_each_entry(item, &cin_notify_functions, link) {
665 bool match = true;
666
667 /* We do not support toggle states */
668 if (item->key.key.unicode_char || item->key.key.scan_code) {
669 if (item->key.key.unicode_char !=
670 next_key.key.unicode_char ||
671 item->key.key.scan_code != next_key.key.scan_code)
672 match = false;
673 }
674 if (item->key.key_state.key_shift_state &&
675 item->key.key_state.key_shift_state !=
676 next_key.key_state.key_shift_state)
677 match = false;
678
679 if (match)
680 /* We don't bother about the return code */
681 EFI_CALL(item->function(&next_key));
682 }
683}
684
685/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200686 * efi_cin_check() - check if keyboard input is available
687 */
688static void efi_cin_check(void)
689{
690 efi_status_t ret;
691
692 if (key_available) {
693 efi_signal_event(efi_con_in.wait_for_key, true);
694 return;
695 }
696
697 if (tstc()) {
698 ret = efi_cin_read_key(&next_key);
699 if (ret == EFI_SUCCESS) {
700 key_available = true;
701
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200702 /* Notify registered functions */
703 efi_cin_notify();
704
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200705 /* Queue the wait for key event */
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200706 if (key_available)
707 efi_signal_event(efi_con_in.wait_for_key, true);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200708 }
709 }
710}
711
712/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200713 * efi_cin_empty_buffer() - empty input buffer
714 */
715static void efi_cin_empty_buffer(void)
716{
717 while (tstc())
718 getc();
719 key_available = false;
720}
721
722/**
723 * efi_cin_reset_ex() - reset console input
724 *
725 * @this: - the extended simple text input protocol
726 * @extended_verification: - extended verification
727 *
728 * This function implements the reset service of the
729 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
730 *
731 * See the Unified Extensible Firmware Interface (UEFI) specification for
732 * details.
733 *
734 * Return: old value of the task priority level
735 */
736static efi_status_t EFIAPI efi_cin_reset_ex(
737 struct efi_simple_text_input_ex_protocol *this,
738 bool extended_verification)
739{
740 efi_status_t ret = EFI_SUCCESS;
741
742 EFI_ENTRY("%p, %d", this, extended_verification);
743
744 /* Check parameters */
745 if (!this) {
746 ret = EFI_INVALID_PARAMETER;
747 goto out;
748 }
749
750 efi_cin_empty_buffer();
751out:
752 return EFI_EXIT(ret);
753}
754
755/**
756 * efi_cin_read_key_stroke_ex() - read key stroke
757 *
758 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
759 * @key_data: key read from console
760 * Return: status code
761 *
762 * This function implements the ReadKeyStrokeEx service of the
763 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
764 *
765 * See the Unified Extensible Firmware Interface (UEFI) specification for
766 * details.
767 */
768static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
769 struct efi_simple_text_input_ex_protocol *this,
770 struct efi_key_data *key_data)
771{
772 efi_status_t ret = EFI_SUCCESS;
773
774 EFI_ENTRY("%p, %p", this, key_data);
775
776 /* Check parameters */
777 if (!this || !key_data) {
778 ret = EFI_INVALID_PARAMETER;
779 goto out;
780 }
781
782 /* We don't do interrupts, so check for timers cooperatively */
783 efi_timer_check();
784
785 /* Enable console input after ExitBootServices */
786 efi_cin_check();
787
788 if (!key_available) {
789 ret = EFI_NOT_READY;
790 goto out;
791 }
792 *key_data = next_key;
793 key_available = false;
794 efi_con_in.wait_for_key->is_signaled = false;
795out:
796 return EFI_EXIT(ret);
797}
798
799/**
800 * efi_cin_set_state() - set toggle key state
801 *
802 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
803 * @key_toggle_state: key toggle state
804 * Return: status code
805 *
806 * This function implements the SetState service of the
807 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
808 *
809 * See the Unified Extensible Firmware Interface (UEFI) specification for
810 * details.
811 */
812static efi_status_t EFIAPI efi_cin_set_state(
813 struct efi_simple_text_input_ex_protocol *this,
814 u8 key_toggle_state)
815{
816 EFI_ENTRY("%p, %u", this, key_toggle_state);
817 /*
818 * U-Boot supports multiple console input sources like serial and
819 * net console for which a key toggle state cannot be set at all.
820 *
821 * According to the UEFI specification it is allowable to not implement
822 * this service.
823 */
824 return EFI_EXIT(EFI_UNSUPPORTED);
825}
826
827/**
828 * efi_cin_register_key_notify() - register key notification function
829 *
830 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
831 * @key_data: key to be notified
832 * @key_notify_function: function to be called if the key is pressed
833 * @notify_handle: handle for unregistering the notification
834 * Return: status code
835 *
836 * This function implements the SetState service of the
837 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
838 *
839 * See the Unified Extensible Firmware Interface (UEFI) specification for
840 * details.
841 */
842static efi_status_t EFIAPI efi_cin_register_key_notify(
843 struct efi_simple_text_input_ex_protocol *this,
844 struct efi_key_data *key_data,
845 efi_status_t (EFIAPI *key_notify_function)(
846 struct efi_key_data *key_data),
847 void **notify_handle)
848{
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200849 efi_status_t ret = EFI_SUCCESS;
850 struct efi_cin_notify_function *notify_function;
851
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200852 EFI_ENTRY("%p, %p, %p, %p",
853 this, key_data, key_notify_function, notify_handle);
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200854
855 /* Check parameters */
856 if (!this || !key_data || !key_notify_function || !notify_handle) {
857 ret = EFI_INVALID_PARAMETER;
858 goto out;
859 }
860
861 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
862 key_data->key.unicode_char,
863 key_data->key.scan_code,
864 key_data->key_state.key_shift_state,
865 key_data->key_state.key_toggle_state);
866
867 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
868 if (!notify_function) {
869 ret = EFI_OUT_OF_RESOURCES;
870 goto out;
871 }
872 notify_function->key = *key_data;
873 notify_function->function = key_notify_function;
874 list_add_tail(&notify_function->link, &cin_notify_functions);
875 *notify_handle = notify_function;
876out:
877 return EFI_EXIT(ret);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200878}
879
880/**
881 * efi_cin_unregister_key_notify() - unregister key notification function
882 *
883 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
884 * @notification_handle: handle received when registering
885 * Return: status code
886 *
887 * This function implements the SetState service of the
888 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
889 *
890 * See the Unified Extensible Firmware Interface (UEFI) specification for
891 * details.
892 */
893static efi_status_t EFIAPI efi_cin_unregister_key_notify(
894 struct efi_simple_text_input_ex_protocol *this,
895 void *notification_handle)
896{
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200897 efi_status_t ret = EFI_INVALID_PARAMETER;
898 struct efi_cin_notify_function *item, *notify_function =
899 notification_handle;
900
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200901 EFI_ENTRY("%p, %p", this, notification_handle);
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200902
903 /* Check parameters */
904 if (!this || !notification_handle)
905 goto out;
906
907 list_for_each_entry(item, &cin_notify_functions, link) {
908 if (item == notify_function) {
909 ret = EFI_SUCCESS;
910 break;
911 }
912 }
913 if (ret != EFI_SUCCESS)
914 goto out;
915
916 /* Remove the notify function */
917 list_del(&notify_function->link);
918 free(notify_function);
919out:
920 return EFI_EXIT(ret);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200921}
922
923
924/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200925 * efi_cin_reset() - drain the input buffer
926 *
927 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
928 * @extended_verification: allow for exhaustive verification
929 * Return: status code
930 *
931 * This function implements the Reset service of the
932 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
933 *
934 * See the Unified Extensible Firmware Interface (UEFI) specification for
935 * details.
936 */
937static efi_status_t EFIAPI efi_cin_reset
938 (struct efi_simple_text_input_protocol *this,
939 bool extended_verification)
940{
941 efi_status_t ret = EFI_SUCCESS;
942
943 EFI_ENTRY("%p, %d", this, extended_verification);
944
945 /* Check parameters */
946 if (!this) {
947 ret = EFI_INVALID_PARAMETER;
948 goto out;
949 }
950
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200951 efi_cin_empty_buffer();
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200952out:
953 return EFI_EXIT(ret);
954}
955
956/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200957 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200958 *
959 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
960 * @key: key read from console
961 * Return: status code
962 *
963 * This function implements the ReadKeyStroke service of the
964 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
965 *
966 * See the Unified Extensible Firmware Interface (UEFI) specification for
967 * details.
968 */
969static efi_status_t EFIAPI efi_cin_read_key_stroke
970 (struct efi_simple_text_input_protocol *this,
971 struct efi_input_key *key)
972{
973 efi_status_t ret = EFI_SUCCESS;
974
975 EFI_ENTRY("%p, %p", this, key);
976
977 /* Check parameters */
978 if (!this || !key) {
979 ret = EFI_INVALID_PARAMETER;
980 goto out;
981 }
982
983 /* We don't do interrupts, so check for timers cooperatively */
984 efi_timer_check();
985
986 /* Enable console input after ExitBootServices */
987 efi_cin_check();
988
989 if (!key_available) {
990 ret = EFI_NOT_READY;
991 goto out;
992 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200993 *key = next_key.key;
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200994 key_available = false;
995 efi_con_in.wait_for_key->is_signaled = false;
996out:
997 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100998}
999
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001000static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1001 .reset = efi_cin_reset_ex,
1002 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1003 .wait_for_key_ex = NULL,
1004 .set_state = efi_cin_set_state,
1005 .register_key_notify = efi_cin_register_key_notify,
1006 .unregister_key_notify = efi_cin_unregister_key_notify,
1007};
1008
Heinrich Schuchardt3e603ec2018-09-08 10:20:10 +02001009struct efi_simple_text_input_protocol efi_con_in = {
Alexander Grafc1311ad2016-03-04 01:10:00 +01001010 .reset = efi_cin_reset,
1011 .read_key_stroke = efi_cin_read_key_stroke,
1012 .wait_for_key = NULL,
1013};
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001014
1015static struct efi_event *console_timer_event;
1016
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001017/*
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001018 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001019 *
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001020 * @event: console timer event
1021 * @context: not used
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001022 */
xypron.glpk@gmx.deff925932017-07-20 05:26:07 +02001023static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1024 void *context)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001025{
1026 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001027 efi_cin_check();
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001028 EFI_EXIT(EFI_SUCCESS);
1029}
1030
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001031/**
1032 * efi_key_notify() - notify the wait for key event
1033 *
1034 * @event: wait for key event
1035 * @context: not used
1036 */
1037static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1038{
1039 EFI_ENTRY("%p, %p", event, context);
1040 efi_cin_check();
1041 EFI_EXIT(EFI_SUCCESS);
1042}
1043
1044/**
1045 * efi_console_register() - install the console protocols
1046 *
1047 * This function is called from do_bootefi_exec().
Heinrich Schuchardt6f566c22018-10-02 06:08:26 +02001048 *
1049 * Return: status code
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001050 */
Heinrich Schuchardt6f566c22018-10-02 06:08:26 +02001051efi_status_t efi_console_register(void)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001052{
1053 efi_status_t r;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001054 efi_handle_t console_output_handle;
1055 efi_handle_t console_input_handle;
Rob Clarka17e62c2017-07-24 10:39:01 -04001056
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +02001057 /* Set up mode information */
1058 query_console_size();
1059
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001060 /* Create handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001061 r = efi_create_handle(&console_output_handle);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001062 if (r != EFI_SUCCESS)
1063 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +02001064
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001065 r = efi_add_protocol(console_output_handle,
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001066 &efi_guid_text_output_protocol, &efi_con_out);
1067 if (r != EFI_SUCCESS)
1068 goto out_of_memory;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001069 systab.con_out_handle = console_output_handle;
1070 systab.stderr_handle = console_output_handle;
Alexander Graf40e3e752018-09-04 14:59:11 +02001071
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001072 r = efi_create_handle(&console_input_handle);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001073 if (r != EFI_SUCCESS)
1074 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +02001075
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001076 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001077 &efi_guid_text_input_protocol, &efi_con_in);
1078 if (r != EFI_SUCCESS)
1079 goto out_of_memory;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001080 systab.con_in_handle = console_input_handle;
1081 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001082 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1083 if (r != EFI_SUCCESS)
1084 goto out_of_memory;
Rob Clarka17e62c2017-07-24 10:39:01 -04001085
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001086 /* Create console events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001087 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1088 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001089 if (r != EFI_SUCCESS) {
1090 printf("ERROR: Failed to register WaitForKey event\n");
1091 return r;
1092 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001093 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001094 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001095 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001096 &console_timer_event);
1097 if (r != EFI_SUCCESS) {
1098 printf("ERROR: Failed to register console event\n");
1099 return r;
1100 }
1101 /* 5000 ns cycle is sufficient for 2 MBaud */
1102 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1103 if (r != EFI_SUCCESS)
1104 printf("ERROR: Failed to set console timer\n");
1105 return r;
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001106out_of_memory:
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +02001107 printf("ERROR: Out of memory\n");
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001108 return r;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001109}