blob: d4765afb98498600b9d97cd2c4a3454046eb057d [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf4aacacc2016-03-04 01:10:00 +01002/*
3 * EFI application console interface
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf4aacacc2016-03-04 01:10:00 +01006 */
7
8#include <common.h>
Rob Clark0d138cf2017-09-09 06:47:40 -04009#include <charset.h>
Rob Clark3863b712017-09-13 18:05:43 -040010#include <dm/device.h>
Alexander Graf4aacacc2016-03-04 01:10:00 +010011#include <efi_loader.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060012#include <env.h>
Rob Clark3863b712017-09-13 18:05:43 -040013#include <stdio_dev.h>
14#include <video_console.h>
Alexander Graf4aacacc2016-03-04 01:10:00 +010015
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010016#define EFI_COUT_MODE_2 2
17#define EFI_MAX_COUT_MODE 3
18
19struct cout_mode {
20 unsigned long columns;
21 unsigned long rows;
22 int present;
23};
24
25static struct cout_mode efi_cout_modes[] = {
26 /* EFI Mode 0 is 80x25 and always present */
27 {
28 .columns = 80,
29 .rows = 25,
30 .present = 1,
31 },
32 /* EFI Mode 1 is always 80x50 */
33 {
34 .columns = 80,
35 .rows = 50,
36 .present = 0,
37 },
38 /* Value are unknown until we query the console */
39 {
40 .columns = 0,
41 .rows = 0,
42 .present = 0,
43 },
44};
45
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +020046const efi_guid_t efi_guid_text_input_ex_protocol =
47 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +020048const efi_guid_t efi_guid_text_input_protocol =
49 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +020050const efi_guid_t efi_guid_text_output_protocol =
51 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
Alexander Graf4aacacc2016-03-04 01:10:00 +010052
53#define cESC '\x1b'
54#define ESC "\x1b"
55
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010056/* Default to mode 0 */
Alexander Graf4aacacc2016-03-04 01:10:00 +010057static struct simple_text_output_mode efi_con_mode = {
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010058 .max_mode = 1,
Alexander Graf4aacacc2016-03-04 01:10:00 +010059 .mode = 0,
60 .attribute = 0,
61 .cursor_column = 0,
62 .cursor_row = 0,
63 .cursor_visible = 1,
64};
65
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010066static int term_get_char(s32 *c)
67{
68 u64 timeout;
69
70 /* Wait up to 100 ms for a character */
71 timeout = timer_get_us() + 100000;
72
73 while (!tstc())
74 if (timer_get_us() > timeout)
75 return 1;
76
77 *c = getc();
78 return 0;
79}
80
Heinrich Schuchardt77135c22018-05-16 18:17:38 +020081/*
82 * Receive and parse a reply from the terminal.
83 *
84 * @n: array of return values
85 * @num: number of return values expected
86 * @end_char: character indicating end of terminal message
87 * @return: non-zero indicates error
88 */
89static int term_read_reply(int *n, int num, char end_char)
Alexander Graf4aacacc2016-03-04 01:10:00 +010090{
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010091 s32 c;
Alexander Graf4aacacc2016-03-04 01:10:00 +010092 int i = 0;
93
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010094 if (term_get_char(&c) || c != cESC)
Alexander Graf4aacacc2016-03-04 01:10:00 +010095 return -1;
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010096
97 if (term_get_char(&c) || c != '[')
Alexander Graf4aacacc2016-03-04 01:10:00 +010098 return -1;
99
100 n[0] = 0;
101 while (1) {
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100102 if (!term_get_char(&c)) {
103 if (c == ';') {
104 i++;
105 if (i >= num)
106 return -1;
107 n[i] = 0;
108 continue;
109 } else if (c == end_char) {
110 break;
111 } else if (c > '9' || c < '0') {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100112 return -1;
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100113 }
114
115 /* Read one more decimal position */
116 n[i] *= 10;
117 n[i] += c - '0';
118 } else {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100119 return -1;
120 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100121 }
Heinrich Schuchardt77135c22018-05-16 18:17:38 +0200122 if (i != num - 1)
123 return -1;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100124
125 return 0;
126}
127
Alexander Graf4aacacc2016-03-04 01:10:00 +0100128static efi_status_t EFIAPI efi_cout_output_string(
129 struct efi_simple_text_output_protocol *this,
Rob Clark1ef185d2017-09-13 18:05:44 -0400130 const efi_string_t string)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100131{
Rob Clark1ef185d2017-09-13 18:05:44 -0400132 struct simple_text_output_mode *con = &efi_con_mode;
133 struct cout_mode *mode = &efi_cout_modes[con->mode];
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200134 char *buf, *pos;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200135 u16 *p;
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200136 efi_status_t ret = EFI_SUCCESS;
Rob Clark1ef185d2017-09-13 18:05:44 -0400137
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200138 EFI_ENTRY("%p, %p", this, string);
Rob Clark1ef185d2017-09-13 18:05:44 -0400139
Heinrich Schuchardte60b2982019-05-18 18:11:54 +0200140 if (!this || !string) {
141 ret = EFI_INVALID_PARAMETER;
142 goto out;
143 }
144
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200145 buf = malloc(utf16_utf8_strlen(string) + 1);
146 if (!buf) {
147 ret = EFI_OUT_OF_RESOURCES;
148 goto out;
149 }
150 pos = buf;
151 utf16_utf8_strcpy(&pos, string);
Rob Clark1ef185d2017-09-13 18:05:44 -0400152 fputs(stdout, buf);
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200153 free(buf);
Rob Clark1ef185d2017-09-13 18:05:44 -0400154
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200155 /*
156 * Update the cursor position.
157 *
158 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
159 * and U000D. All other characters, including control characters
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +0200160 * U+0007 (BEL) and U+0009 (TAB), have to increase the column by one.
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200161 */
162 for (p = string; *p; ++p) {
Rob Clark1ef185d2017-09-13 18:05:44 -0400163 switch (*p) {
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200164 case '\b': /* U+0008, backspace */
165 con->cursor_column = max(0, con->cursor_column - 1);
Rob Clark1ef185d2017-09-13 18:05:44 -0400166 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200167 case '\n': /* U+000A, newline */
Rob Clark1ef185d2017-09-13 18:05:44 -0400168 con->cursor_column = 0;
169 con->cursor_row++;
170 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200171 case '\r': /* U+000D, carriage-return */
172 con->cursor_column = 0;
Rob Clark1ef185d2017-09-13 18:05:44 -0400173 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200174 case 0xd800 ... 0xdbff:
175 /*
176 * Ignore high surrogates, we do not want to count a
177 * Unicode character twice.
178 */
Rob Clark1ef185d2017-09-13 18:05:44 -0400179 break;
180 default:
181 con->cursor_column++;
182 break;
183 }
184 if (con->cursor_column >= mode->columns) {
185 con->cursor_column = 0;
186 con->cursor_row++;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100187 }
Rob Clark1ef185d2017-09-13 18:05:44 -0400188 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100189 }
190
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200191out:
192 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100193}
194
195static efi_status_t EFIAPI efi_cout_test_string(
196 struct efi_simple_text_output_protocol *this,
Rob Clark1ef185d2017-09-13 18:05:44 -0400197 const efi_string_t string)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100198{
199 EFI_ENTRY("%p, %p", this, string);
200 return EFI_EXIT(EFI_SUCCESS);
201}
202
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100203static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
204{
205 if (!mode->present)
206 return false;
207
208 return (mode->rows == rows) && (mode->columns == cols);
209}
210
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200211/**
212 * query_console_serial() - query console size
213 *
214 * @rows pointer to return number of rows
215 * @columns pointer to return number of columns
216 * Returns 0 on success
217 */
Rob Clark1e948922017-09-13 18:05:42 -0400218static int query_console_serial(int *rows, int *cols)
219{
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200220 int ret = 0;
221 int n[2];
Rob Clark1e948922017-09-13 18:05:42 -0400222
223 /* Empty input buffer */
224 while (tstc())
225 getc();
226
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200227 /*
228 * Not all terminals understand CSI [18t for querying the console size.
229 * We should adhere to escape sequences documented in the console_codes
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200230 * man page and the ECMA-48 standard.
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200231 *
232 * So here we follow a different approach. We position the cursor to the
233 * bottom right and query its position. Before leaving the function we
234 * restore the original cursor position.
235 */
236 printf(ESC "7" /* Save cursor position */
237 ESC "[r" /* Set scrolling region to full window */
238 ESC "[999;999H" /* Move to bottom right corner */
239 ESC "[6n"); /* Query cursor position */
Rob Clark1e948922017-09-13 18:05:42 -0400240
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200241 /* Read {rows,cols} */
242 if (term_read_reply(n, 2, 'R')) {
243 ret = 1;
244 goto out;
245 }
Rob Clark1e948922017-09-13 18:05:42 -0400246
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200247 *cols = n[1];
248 *rows = n[0];
249out:
250 printf(ESC "8"); /* Restore cursor position */
251 return ret;
Rob Clark1e948922017-09-13 18:05:42 -0400252}
253
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200254/*
255 * Update the mode table.
256 *
257 * By default the only mode available is 80x25. If the console has at least 50
258 * lines, enable mode 80x50. If we can query the console size and it is neither
259 * 80x25 nor 80x50, set it as an additional mode.
260 */
261static void query_console_size(void)
262{
263 const char *stdout_name = env_get("stdout");
Alexander Graf28795322018-06-03 15:51:17 +0200264 int rows = 25, cols = 80;
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200265
266 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
267 IS_ENABLED(CONFIG_DM_VIDEO)) {
268 struct stdio_dev *stdout_dev =
269 stdio_get_by_name("vidconsole");
270 struct udevice *dev = stdout_dev->priv;
271 struct vidconsole_priv *priv =
272 dev_get_uclass_priv(dev);
273 rows = priv->rows;
274 cols = priv->cols;
275 } else if (query_console_serial(&rows, &cols)) {
276 return;
277 }
278
279 /* Test if we can have Mode 1 */
280 if (cols >= 80 && rows >= 50) {
281 efi_cout_modes[1].present = 1;
282 efi_con_mode.max_mode = 2;
283 }
284
285 /*
286 * Install our mode as mode 2 if it is different
287 * than mode 0 or 1 and set it as the currently selected mode
288 */
289 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
290 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
291 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
292 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
293 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
294 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
295 efi_con_mode.mode = EFI_COUT_MODE_2;
296 }
297}
298
Alexander Graf4aacacc2016-03-04 01:10:00 +0100299static efi_status_t EFIAPI efi_cout_query_mode(
300 struct efi_simple_text_output_protocol *this,
301 unsigned long mode_number, unsigned long *columns,
302 unsigned long *rows)
303{
304 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
305
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100306 if (mode_number >= efi_con_mode.max_mode)
307 return EFI_EXIT(EFI_UNSUPPORTED);
308
309 if (efi_cout_modes[mode_number].present != 1)
310 return EFI_EXIT(EFI_UNSUPPORTED);
311
Alexander Graf4aacacc2016-03-04 01:10:00 +0100312 if (columns)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100313 *columns = efi_cout_modes[mode_number].columns;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100314 if (rows)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100315 *rows = efi_cout_modes[mode_number].rows;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100316
317 return EFI_EXIT(EFI_SUCCESS);
318}
319
Rob Clark87ef0012017-10-10 08:23:01 -0400320static const struct {
321 unsigned int fg;
322 unsigned int bg;
323} color[] = {
324 { 30, 40 }, /* 0: black */
325 { 34, 44 }, /* 1: blue */
326 { 32, 42 }, /* 2: green */
327 { 36, 46 }, /* 3: cyan */
328 { 31, 41 }, /* 4: red */
329 { 35, 45 }, /* 5: magenta */
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +0200330 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
331 { 37, 47 }, /* 7: light gray, map to white */
Rob Clark87ef0012017-10-10 08:23:01 -0400332};
333
334/* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100335static efi_status_t EFIAPI efi_cout_set_attribute(
336 struct efi_simple_text_output_protocol *this,
337 unsigned long attribute)
338{
Rob Clark87ef0012017-10-10 08:23:01 -0400339 unsigned int bold = EFI_ATTR_BOLD(attribute);
340 unsigned int fg = EFI_ATTR_FG(attribute);
341 unsigned int bg = EFI_ATTR_BG(attribute);
342
Alexander Graf4aacacc2016-03-04 01:10:00 +0100343 EFI_ENTRY("%p, %lx", this, attribute);
344
Heinrich Schuchardt1e129622019-06-14 07:16:57 +0200345 efi_con_mode.attribute = attribute;
Rob Clark87ef0012017-10-10 08:23:01 -0400346 if (attribute)
347 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
348 else
349 printf(ESC"[0;37;40m");
350
351 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100352}
353
354static efi_status_t EFIAPI efi_cout_clear_screen(
355 struct efi_simple_text_output_protocol *this)
356{
357 EFI_ENTRY("%p", this);
358
359 printf(ESC"[2J");
Heinrich Schuchardt2d099322018-07-05 08:18:00 +0200360 efi_con_mode.cursor_column = 0;
361 efi_con_mode.cursor_row = 0;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100362
363 return EFI_EXIT(EFI_SUCCESS);
364}
365
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200366static efi_status_t EFIAPI efi_cout_set_mode(
367 struct efi_simple_text_output_protocol *this,
368 unsigned long mode_number)
369{
370 EFI_ENTRY("%p, %ld", this, mode_number);
371
372 if (mode_number >= efi_con_mode.max_mode)
373 return EFI_EXIT(EFI_UNSUPPORTED);
374 efi_con_mode.mode = mode_number;
375 EFI_CALL(efi_cout_clear_screen(this));
376
377 return EFI_EXIT(EFI_SUCCESS);
378}
379
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200380static efi_status_t EFIAPI efi_cout_reset(
381 struct efi_simple_text_output_protocol *this,
382 char extended_verification)
383{
384 EFI_ENTRY("%p, %d", this, extended_verification);
385
386 /* Clear screen */
387 EFI_CALL(efi_cout_clear_screen(this));
388 /* Set default colors */
Heinrich Schuchardt1e129622019-06-14 07:16:57 +0200389 efi_con_mode.attribute = 0x07;
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200390 printf(ESC "[0;37;40m");
391
392 return EFI_EXIT(EFI_SUCCESS);
393}
394
Alexander Graf4aacacc2016-03-04 01:10:00 +0100395static efi_status_t EFIAPI efi_cout_set_cursor_position(
396 struct efi_simple_text_output_protocol *this,
397 unsigned long column, unsigned long row)
398{
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200399 efi_status_t ret = EFI_SUCCESS;
400 struct simple_text_output_mode *con = &efi_con_mode;
401 struct cout_mode *mode = &efi_cout_modes[con->mode];
402
Alexander Graf4aacacc2016-03-04 01:10:00 +0100403 EFI_ENTRY("%p, %ld, %ld", this, column, row);
404
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200405 /* Check parameters */
406 if (!this) {
407 ret = EFI_INVALID_PARAMETER;
408 goto out;
409 }
410 if (row >= mode->rows || column >= mode->columns) {
411 ret = EFI_UNSUPPORTED;
412 goto out;
413 }
414
415 /*
416 * Set cursor position by sending CSI H.
417 * EFI origin is [0, 0], terminal origin is [1, 1].
418 */
419 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100420 efi_con_mode.cursor_column = column;
421 efi_con_mode.cursor_row = row;
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200422out:
423 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100424}
425
426static efi_status_t EFIAPI efi_cout_enable_cursor(
427 struct efi_simple_text_output_protocol *this,
428 bool enable)
429{
430 EFI_ENTRY("%p, %d", this, enable);
431
432 printf(ESC"[?25%c", enable ? 'h' : 'l');
Heinrich Schuchardt53f26b92019-06-02 22:54:28 +0200433 efi_con_mode.cursor_visible = !!enable;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100434
435 return EFI_EXIT(EFI_SUCCESS);
436}
437
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +0200438struct efi_simple_text_output_protocol efi_con_out = {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100439 .reset = efi_cout_reset,
440 .output_string = efi_cout_output_string,
441 .test_string = efi_cout_test_string,
442 .query_mode = efi_cout_query_mode,
443 .set_mode = efi_cout_set_mode,
444 .set_attribute = efi_cout_set_attribute,
445 .clear_screen = efi_cout_clear_screen,
446 .set_cursor_position = efi_cout_set_cursor_position,
447 .enable_cursor = efi_cout_enable_cursor,
448 .mode = (void*)&efi_con_mode,
449};
450
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200451/**
452 * struct efi_cin_notify_function - registered console input notify function
453 *
454 * @link: link to list
455 * @data: key to notify
456 * @function: function to call
457 */
458struct efi_cin_notify_function {
459 struct list_head link;
460 struct efi_key_data key;
461 efi_status_t (EFIAPI *function)
462 (struct efi_key_data *key_data);
463};
464
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200465static bool key_available;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200466static struct efi_key_data next_key;
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200467static LIST_HEAD(cin_notify_functions);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100468
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200469/**
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200470 * set_shift_mask() - set shift mask
471 *
472 * @mod: Xterm shift mask
473 */
474void set_shift_mask(int mod, struct efi_key_state *key_state)
475{
476 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
477 if (mod) {
478 --mod;
479 if (mod & 1)
480 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
481 if (mod & 2)
482 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
483 if (mod & 4)
484 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
Heinrich Schuchardtfea93d62019-06-16 22:33:20 +0200485 if (!mod || (mod & 8))
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200486 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200487 }
488}
489
490/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200491 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200492 *
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100493 * This gets called when we have already parsed CSI.
494 *
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200495 * @modifiers: bit mask (shift, alt, ctrl)
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100496 * @return: the unmodified code
497 */
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200498static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100499{
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200500 int c, mod = 0, ret = 0;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100501
502 c = getc();
503
504 if (c != ';') {
505 ret = c;
506 if (c == '~')
507 goto out;
508 c = getc();
509 }
510 for (;;) {
511 switch (c) {
512 case '0'...'9':
513 mod *= 10;
514 mod += c - '0';
515 /* fall through */
516 case ';':
517 c = getc();
518 break;
519 default:
520 goto out;
521 }
522 }
523out:
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200524 set_shift_mask(mod, key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100525 if (!ret)
526 ret = c;
527 return ret;
528}
529
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200530/**
531 * efi_cin_read_key() - read a key from the console input
532 *
533 * @key: - key received
534 * Return: - status code
535 */
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200536static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100537{
538 struct efi_input_key pressed_key = {
539 .scan_code = 0,
540 .unicode_char = 0,
541 };
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200542 s32 ch;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100543
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200544 if (console_read_unicode(&ch))
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200545 return EFI_NOT_READY;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200546
547 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
548 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
549
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200550 /* We do not support multi-word codes */
551 if (ch >= 0x10000)
552 ch = '?';
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200553
554 switch (ch) {
555 case 0x1b:
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100556 /*
557 * Xterm Control Sequences
558 * https://www.xfree86.org/4.8.0/ctlseqs.html
559 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100560 ch = getc();
561 switch (ch) {
562 case cESC: /* ESC */
563 pressed_key.scan_code = 23;
564 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200565 case 'O': /* F1 - F4, End */
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100566 ch = getc();
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200567 /* consider modifiers */
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200568 if (ch == 'F') { /* End */
569 pressed_key.scan_code = 6;
570 break;
571 } else if (ch < 'P') {
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200572 set_shift_mask(ch - '0', &key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100573 ch = getc();
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200574 }
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100575 pressed_key.scan_code = ch - 'P' + 11;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100576 break;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100577 case '[':
578 ch = getc();
579 switch (ch) {
580 case 'A'...'D': /* up, down right, left */
581 pressed_key.scan_code = ch - 'A' + 1;
582 break;
583 case 'F': /* End */
584 pressed_key.scan_code = 6;
585 break;
586 case 'H': /* Home */
587 pressed_key.scan_code = 5;
588 break;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100589 case '1':
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200590 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100591 switch (ch) {
592 case '1'...'5': /* F1 - F5 */
593 pressed_key.scan_code = ch - '1' + 11;
594 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200595 case '6'...'9': /* F5 - F8 */
596 pressed_key.scan_code = ch - '6' + 15;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100597 break;
598 case 'A'...'D': /* up, down right, left */
599 pressed_key.scan_code = ch - 'A' + 1;
600 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200601 case 'F': /* End */
602 pressed_key.scan_code = 6;
603 break;
604 case 'H': /* Home */
605 pressed_key.scan_code = 5;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100606 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200607 case '~': /* Home */
608 pressed_key.scan_code = 5;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100609 break;
610 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100611 break;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100612 case '2':
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200613 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100614 switch (ch) {
615 case '0'...'1': /* F9 - F10 */
616 pressed_key.scan_code = ch - '0' + 19;
617 break;
618 case '3'...'4': /* F11 - F12 */
619 pressed_key.scan_code = ch - '3' + 21;
620 break;
621 case '~': /* INS */
622 pressed_key.scan_code = 7;
623 break;
624 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100625 break;
626 case '3': /* DEL */
627 pressed_key.scan_code = 8;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200628 analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100629 break;
630 case '5': /* PG UP */
631 pressed_key.scan_code = 9;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200632 analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100633 break;
634 case '6': /* PG DOWN */
635 pressed_key.scan_code = 10;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200636 analyze_modifiers(&key->key_state);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100637 break;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200638 } /* [ */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100639 break;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200640 default:
641 /* ALT key */
642 set_shift_mask(3, &key->key_state);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100643 }
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200644 break;
645 case 0x7f:
Alexander Graf4aacacc2016-03-04 01:10:00 +0100646 /* Backspace */
647 ch = 0x08;
648 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200649 if (pressed_key.scan_code) {
650 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
651 } else {
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100652 pressed_key.unicode_char = ch;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200653
654 /*
655 * Assume left control key for control characters typically
656 * entered using the control key.
657 */
658 if (ch >= 0x01 && ch <= 0x1f) {
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200659 key->key_state.key_shift_state |=
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200660 EFI_SHIFT_STATE_VALID;
661 switch (ch) {
662 case 0x01 ... 0x07:
663 case 0x0b ... 0x0c:
664 case 0x0e ... 0x1f:
665 key->key_state.key_shift_state |=
666 EFI_LEFT_CONTROL_PRESSED;
667 }
668 }
669 }
670 key->key = pressed_key;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100671
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200672 return EFI_SUCCESS;
673}
674
675/**
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200676 * efi_cin_notify() - notify registered functions
677 */
678static void efi_cin_notify(void)
679{
680 struct efi_cin_notify_function *item;
681
682 list_for_each_entry(item, &cin_notify_functions, link) {
683 bool match = true;
684
685 /* We do not support toggle states */
686 if (item->key.key.unicode_char || item->key.key.scan_code) {
687 if (item->key.key.unicode_char !=
688 next_key.key.unicode_char ||
689 item->key.key.scan_code != next_key.key.scan_code)
690 match = false;
691 }
692 if (item->key.key_state.key_shift_state &&
693 item->key.key_state.key_shift_state !=
694 next_key.key_state.key_shift_state)
695 match = false;
696
697 if (match)
698 /* We don't bother about the return code */
699 EFI_CALL(item->function(&next_key));
700 }
701}
702
703/**
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200704 * efi_cin_check() - check if keyboard input is available
705 */
706static void efi_cin_check(void)
707{
708 efi_status_t ret;
709
710 if (key_available) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200711 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200712 return;
713 }
714
715 if (tstc()) {
716 ret = efi_cin_read_key(&next_key);
717 if (ret == EFI_SUCCESS) {
718 key_available = true;
719
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200720 /* Notify registered functions */
721 efi_cin_notify();
722
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200723 /* Queue the wait for key event */
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200724 if (key_available)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200725 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200726 }
727 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200728}
729
730/**
731 * efi_cin_empty_buffer() - empty input buffer
732 */
733static void efi_cin_empty_buffer(void)
734{
735 while (tstc())
736 getc();
737 key_available = false;
738}
739
740/**
741 * efi_cin_reset_ex() - reset console input
742 *
743 * @this: - the extended simple text input protocol
744 * @extended_verification: - extended verification
745 *
746 * This function implements the reset service of the
747 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
748 *
749 * See the Unified Extensible Firmware Interface (UEFI) specification for
750 * details.
751 *
752 * Return: old value of the task priority level
753 */
754static efi_status_t EFIAPI efi_cin_reset_ex(
755 struct efi_simple_text_input_ex_protocol *this,
756 bool extended_verification)
757{
758 efi_status_t ret = EFI_SUCCESS;
759
760 EFI_ENTRY("%p, %d", this, extended_verification);
761
762 /* Check parameters */
763 if (!this) {
764 ret = EFI_INVALID_PARAMETER;
765 goto out;
766 }
767
768 efi_cin_empty_buffer();
769out:
770 return EFI_EXIT(ret);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200771}
772
773/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200774 * efi_cin_read_key_stroke_ex() - read key stroke
775 *
776 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
777 * @key_data: key read from console
778 * Return: status code
779 *
780 * This function implements the ReadKeyStrokeEx service of the
781 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
782 *
783 * See the Unified Extensible Firmware Interface (UEFI) specification for
784 * details.
785 */
786static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
787 struct efi_simple_text_input_ex_protocol *this,
788 struct efi_key_data *key_data)
789{
790 efi_status_t ret = EFI_SUCCESS;
791
792 EFI_ENTRY("%p, %p", this, key_data);
793
794 /* Check parameters */
795 if (!this || !key_data) {
796 ret = EFI_INVALID_PARAMETER;
797 goto out;
798 }
799
800 /* We don't do interrupts, so check for timers cooperatively */
801 efi_timer_check();
802
803 /* Enable console input after ExitBootServices */
804 efi_cin_check();
805
806 if (!key_available) {
807 ret = EFI_NOT_READY;
808 goto out;
809 }
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +0200810 /*
811 * CTRL+A - CTRL+Z have to be signaled as a - z.
812 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
813 */
814 switch (next_key.key.unicode_char) {
815 case 0x01 ... 0x07:
816 case 0x0b ... 0x0c:
817 case 0x0e ... 0x1a:
818 if (!(next_key.key_state.key_toggle_state &
819 EFI_CAPS_LOCK_ACTIVE) ^
820 !(next_key.key_state.key_shift_state &
821 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
822 next_key.key.unicode_char += 0x40;
823 else
824 next_key.key.unicode_char += 0x60;
825 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200826 *key_data = next_key;
827 key_available = false;
828 efi_con_in.wait_for_key->is_signaled = false;
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +0200829
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200830out:
831 return EFI_EXIT(ret);
832}
833
834/**
835 * efi_cin_set_state() - set toggle key state
836 *
837 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +0200838 * @key_toggle_state: pointer to key toggle state
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200839 * Return: status code
840 *
841 * This function implements the SetState service of the
842 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
843 *
844 * See the Unified Extensible Firmware Interface (UEFI) specification for
845 * details.
846 */
847static efi_status_t EFIAPI efi_cin_set_state(
848 struct efi_simple_text_input_ex_protocol *this,
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +0200849 u8 *key_toggle_state)
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200850{
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +0200851 EFI_ENTRY("%p, %p", this, key_toggle_state);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200852 /*
853 * U-Boot supports multiple console input sources like serial and
854 * net console for which a key toggle state cannot be set at all.
855 *
856 * According to the UEFI specification it is allowable to not implement
857 * this service.
858 */
859 return EFI_EXIT(EFI_UNSUPPORTED);
860}
861
862/**
863 * efi_cin_register_key_notify() - register key notification function
864 *
865 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
866 * @key_data: key to be notified
867 * @key_notify_function: function to be called if the key is pressed
868 * @notify_handle: handle for unregistering the notification
869 * Return: status code
870 *
871 * This function implements the SetState service of the
872 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
873 *
874 * See the Unified Extensible Firmware Interface (UEFI) specification for
875 * details.
876 */
877static efi_status_t EFIAPI efi_cin_register_key_notify(
878 struct efi_simple_text_input_ex_protocol *this,
879 struct efi_key_data *key_data,
880 efi_status_t (EFIAPI *key_notify_function)(
881 struct efi_key_data *key_data),
882 void **notify_handle)
883{
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200884 efi_status_t ret = EFI_SUCCESS;
885 struct efi_cin_notify_function *notify_function;
886
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200887 EFI_ENTRY("%p, %p, %p, %p",
888 this, key_data, key_notify_function, notify_handle);
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200889
890 /* Check parameters */
891 if (!this || !key_data || !key_notify_function || !notify_handle) {
892 ret = EFI_INVALID_PARAMETER;
893 goto out;
894 }
895
896 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
897 key_data->key.unicode_char,
898 key_data->key.scan_code,
899 key_data->key_state.key_shift_state,
900 key_data->key_state.key_toggle_state);
901
902 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
903 if (!notify_function) {
904 ret = EFI_OUT_OF_RESOURCES;
905 goto out;
906 }
907 notify_function->key = *key_data;
908 notify_function->function = key_notify_function;
909 list_add_tail(&notify_function->link, &cin_notify_functions);
910 *notify_handle = notify_function;
911out:
912 return EFI_EXIT(ret);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200913}
914
915/**
916 * efi_cin_unregister_key_notify() - unregister key notification function
917 *
918 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
919 * @notification_handle: handle received when registering
920 * Return: status code
921 *
922 * This function implements the SetState service of the
923 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
924 *
925 * See the Unified Extensible Firmware Interface (UEFI) specification for
926 * details.
927 */
928static efi_status_t EFIAPI efi_cin_unregister_key_notify(
929 struct efi_simple_text_input_ex_protocol *this,
930 void *notification_handle)
931{
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200932 efi_status_t ret = EFI_INVALID_PARAMETER;
933 struct efi_cin_notify_function *item, *notify_function =
934 notification_handle;
935
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200936 EFI_ENTRY("%p, %p", this, notification_handle);
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200937
938 /* Check parameters */
939 if (!this || !notification_handle)
940 goto out;
941
942 list_for_each_entry(item, &cin_notify_functions, link) {
943 if (item == notify_function) {
944 ret = EFI_SUCCESS;
945 break;
946 }
947 }
948 if (ret != EFI_SUCCESS)
949 goto out;
950
951 /* Remove the notify function */
952 list_del(&notify_function->link);
953 free(notify_function);
954out:
955 return EFI_EXIT(ret);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200956}
957
958
959/**
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200960 * efi_cin_reset() - drain the input buffer
961 *
962 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
963 * @extended_verification: allow for exhaustive verification
964 * Return: status code
965 *
966 * This function implements the Reset service of the
967 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
968 *
969 * See the Unified Extensible Firmware Interface (UEFI) specification for
970 * details.
971 */
972static efi_status_t EFIAPI efi_cin_reset
973 (struct efi_simple_text_input_protocol *this,
974 bool extended_verification)
975{
976 efi_status_t ret = EFI_SUCCESS;
977
978 EFI_ENTRY("%p, %d", this, extended_verification);
979
980 /* Check parameters */
981 if (!this) {
982 ret = EFI_INVALID_PARAMETER;
983 goto out;
984 }
985
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200986 efi_cin_empty_buffer();
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200987out:
988 return EFI_EXIT(ret);
989}
990
991/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200992 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200993 *
994 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
995 * @key: key read from console
996 * Return: status code
997 *
998 * This function implements the ReadKeyStroke service of the
999 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1000 *
1001 * See the Unified Extensible Firmware Interface (UEFI) specification for
1002 * details.
1003 */
1004static efi_status_t EFIAPI efi_cin_read_key_stroke
1005 (struct efi_simple_text_input_protocol *this,
1006 struct efi_input_key *key)
1007{
1008 efi_status_t ret = EFI_SUCCESS;
1009
1010 EFI_ENTRY("%p, %p", this, key);
1011
1012 /* Check parameters */
1013 if (!this || !key) {
1014 ret = EFI_INVALID_PARAMETER;
1015 goto out;
1016 }
1017
1018 /* We don't do interrupts, so check for timers cooperatively */
1019 efi_timer_check();
1020
1021 /* Enable console input after ExitBootServices */
1022 efi_cin_check();
1023
1024 if (!key_available) {
1025 ret = EFI_NOT_READY;
1026 goto out;
1027 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001028 *key = next_key.key;
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001029 key_available = false;
1030 efi_con_in.wait_for_key->is_signaled = false;
1031out:
1032 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +01001033}
1034
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001035static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1036 .reset = efi_cin_reset_ex,
1037 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1038 .wait_for_key_ex = NULL,
1039 .set_state = efi_cin_set_state,
1040 .register_key_notify = efi_cin_register_key_notify,
1041 .unregister_key_notify = efi_cin_unregister_key_notify,
1042};
1043
Heinrich Schuchardt3dabffd2018-09-08 10:20:10 +02001044struct efi_simple_text_input_protocol efi_con_in = {
Alexander Graf4aacacc2016-03-04 01:10:00 +01001045 .reset = efi_cin_reset,
1046 .read_key_stroke = efi_cin_read_key_stroke,
1047 .wait_for_key = NULL,
1048};
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001049
1050static struct efi_event *console_timer_event;
1051
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001052/*
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001053 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001054 *
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001055 * @event: console timer event
1056 * @context: not used
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001057 */
xypron.glpk@gmx.de0ce42dc2017-07-20 05:26:07 +02001058static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1059 void *context)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001060{
1061 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001062 efi_cin_check();
1063 EFI_EXIT(EFI_SUCCESS);
1064}
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001065
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001066/**
1067 * efi_key_notify() - notify the wait for key event
1068 *
1069 * @event: wait for key event
1070 * @context: not used
1071 */
1072static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1073{
1074 EFI_ENTRY("%p, %p", event, context);
1075 efi_cin_check();
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001076 EFI_EXIT(EFI_SUCCESS);
1077}
1078
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001079/**
1080 * efi_console_register() - install the console protocols
1081 *
1082 * This function is called from do_bootefi_exec().
Heinrich Schuchardta92e9ab2018-10-02 06:08:26 +02001083 *
1084 * Return: status code
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001085 */
Heinrich Schuchardta92e9ab2018-10-02 06:08:26 +02001086efi_status_t efi_console_register(void)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001087{
1088 efi_status_t r;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001089 efi_handle_t console_output_handle;
1090 efi_handle_t console_input_handle;
Rob Clark49f7b4b2017-07-24 10:39:01 -04001091
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +02001092 /* Set up mode information */
1093 query_console_size();
1094
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001095 /* Create handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001096 r = efi_create_handle(&console_output_handle);
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001097 if (r != EFI_SUCCESS)
1098 goto out_of_memory;
Alexander Graf36bab912018-09-04 14:59:11 +02001099
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001100 r = efi_add_protocol(console_output_handle,
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001101 &efi_guid_text_output_protocol, &efi_con_out);
1102 if (r != EFI_SUCCESS)
1103 goto out_of_memory;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001104 systab.con_out_handle = console_output_handle;
1105 systab.stderr_handle = console_output_handle;
Alexander Graf36bab912018-09-04 14:59:11 +02001106
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001107 r = efi_create_handle(&console_input_handle);
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001108 if (r != EFI_SUCCESS)
1109 goto out_of_memory;
Alexander Graf36bab912018-09-04 14:59:11 +02001110
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001111 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001112 &efi_guid_text_input_protocol, &efi_con_in);
1113 if (r != EFI_SUCCESS)
1114 goto out_of_memory;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001115 systab.con_in_handle = console_input_handle;
1116 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001117 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1118 if (r != EFI_SUCCESS)
1119 goto out_of_memory;
Rob Clark49f7b4b2017-07-24 10:39:01 -04001120
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001121 /* Create console events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001122 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1123 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001124 if (r != EFI_SUCCESS) {
1125 printf("ERROR: Failed to register WaitForKey event\n");
1126 return r;
1127 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001128 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001129 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001130 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001131 &console_timer_event);
1132 if (r != EFI_SUCCESS) {
1133 printf("ERROR: Failed to register console event\n");
1134 return r;
1135 }
1136 /* 5000 ns cycle is sufficient for 2 MBaud */
1137 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1138 if (r != EFI_SUCCESS)
1139 printf("ERROR: Failed to set console timer\n");
1140 return r;
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001141out_of_memory:
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +02001142 printf("ERROR: Out of memory\n");
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001143 return r;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001144}