blob: fef0b2e1e253d09b2907c44009d1eb90102bf299 [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
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +010065static int term_get_char(s32 *c)
66{
67 u64 timeout;
68
69 /* Wait up to 100 ms for a character */
70 timeout = timer_get_us() + 100000;
71
72 while (!tstc())
73 if (timer_get_us() > timeout)
74 return 1;
75
76 *c = getc();
77 return 0;
78}
79
Heinrich Schuchardt62217292018-05-16 18:17:38 +020080/*
81 * Receive and parse a reply from the terminal.
82 *
83 * @n: array of return values
84 * @num: number of return values expected
85 * @end_char: character indicating end of terminal message
86 * @return: non-zero indicates error
87 */
88static int term_read_reply(int *n, int num, char end_char)
Alexander Grafc1311ad2016-03-04 01:10:00 +010089{
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +010090 s32 c;
Alexander Grafc1311ad2016-03-04 01:10:00 +010091 int i = 0;
92
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +010093 if (term_get_char(&c) || c != cESC)
Alexander Grafc1311ad2016-03-04 01:10:00 +010094 return -1;
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +010095
96 if (term_get_char(&c) || c != '[')
Alexander Grafc1311ad2016-03-04 01:10:00 +010097 return -1;
98
99 n[0] = 0;
100 while (1) {
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +0100101 if (!term_get_char(&c)) {
102 if (c == ';') {
103 i++;
104 if (i >= num)
105 return -1;
106 n[i] = 0;
107 continue;
108 } else if (c == end_char) {
109 break;
110 } else if (c > '9' || c < '0') {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100111 return -1;
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +0100112 }
113
114 /* Read one more decimal position */
115 n[i] *= 10;
116 n[i] += c - '0';
117 } else {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100118 return -1;
119 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100120 }
Heinrich Schuchardt62217292018-05-16 18:17:38 +0200121 if (i != num - 1)
122 return -1;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100123
124 return 0;
125}
126
Alexander Grafc1311ad2016-03-04 01:10:00 +0100127static efi_status_t EFIAPI efi_cout_output_string(
128 struct efi_simple_text_output_protocol *this,
Rob Clark3a45bc72017-09-13 18:05:44 -0400129 const efi_string_t string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100130{
Rob Clark3a45bc72017-09-13 18:05:44 -0400131 struct simple_text_output_mode *con = &efi_con_mode;
132 struct cout_mode *mode = &efi_cout_modes[con->mode];
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200133 char *buf, *pos;
134 u16 *p;
135 efi_status_t ret = EFI_SUCCESS;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100136
137 EFI_ENTRY("%p, %p", this, string);
Rob Clark3a45bc72017-09-13 18:05:44 -0400138
Heinrich Schuchardtb31ca6b2019-05-18 18:11:54 +0200139 if (!this || !string) {
140 ret = EFI_INVALID_PARAMETER;
141 goto out;
142 }
143
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200144 buf = malloc(utf16_utf8_strlen(string) + 1);
145 if (!buf) {
146 ret = EFI_OUT_OF_RESOURCES;
147 goto out;
148 }
149 pos = buf;
150 utf16_utf8_strcpy(&pos, string);
Rob Clark3a45bc72017-09-13 18:05:44 -0400151 fputs(stdout, buf);
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200152 free(buf);
Rob Clark3a45bc72017-09-13 18:05:44 -0400153
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200154 /*
155 * Update the cursor position.
156 *
157 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
158 * and U000D. All other characters, including control characters
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +0200159 * U+0007 (BEL) and U+0009 (TAB), have to increase the column by one.
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200160 */
161 for (p = string; *p; ++p) {
Rob Clark3a45bc72017-09-13 18:05:44 -0400162 switch (*p) {
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200163 case '\b': /* U+0008, backspace */
164 con->cursor_column = max(0, con->cursor_column - 1);
Rob Clark3a45bc72017-09-13 18:05:44 -0400165 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200166 case '\n': /* U+000A, newline */
Rob Clark3a45bc72017-09-13 18:05:44 -0400167 con->cursor_column = 0;
168 con->cursor_row++;
169 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200170 case '\r': /* U+000D, carriage-return */
171 con->cursor_column = 0;
Rob Clark3a45bc72017-09-13 18:05:44 -0400172 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200173 case 0xd800 ... 0xdbff:
174 /*
175 * Ignore high surrogates, we do not want to count a
176 * Unicode character twice.
177 */
Rob Clark3a45bc72017-09-13 18:05:44 -0400178 break;
179 default:
180 con->cursor_column++;
181 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100182 }
Rob Clark3a45bc72017-09-13 18:05:44 -0400183 if (con->cursor_column >= mode->columns) {
184 con->cursor_column = 0;
185 con->cursor_row++;
186 }
187 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100188 }
189
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200190out:
191 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100192}
193
194static efi_status_t EFIAPI efi_cout_test_string(
195 struct efi_simple_text_output_protocol *this,
Rob Clark3a45bc72017-09-13 18:05:44 -0400196 const efi_string_t string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100197{
198 EFI_ENTRY("%p, %p", this, string);
199 return EFI_EXIT(EFI_SUCCESS);
200}
201
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100202static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
203{
204 if (!mode->present)
205 return false;
206
207 return (mode->rows == rows) && (mode->columns == cols);
208}
209
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200210/**
211 * query_console_serial() - query console size
212 *
213 * @rows pointer to return number of rows
214 * @columns pointer to return number of columns
215 * Returns 0 on success
216 */
Rob Clark71cc25c2017-09-13 18:05:42 -0400217static int query_console_serial(int *rows, int *cols)
218{
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200219 int ret = 0;
220 int n[2];
Rob Clark71cc25c2017-09-13 18:05:42 -0400221
222 /* Empty input buffer */
223 while (tstc())
224 getc();
225
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200226 /*
227 * Not all terminals understand CSI [18t for querying the console size.
228 * We should adhere to escape sequences documented in the console_codes
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200229 * man page and the ECMA-48 standard.
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200230 *
231 * So here we follow a different approach. We position the cursor to the
232 * bottom right and query its position. Before leaving the function we
233 * restore the original cursor position.
234 */
235 printf(ESC "7" /* Save cursor position */
236 ESC "[r" /* Set scrolling region to full window */
237 ESC "[999;999H" /* Move to bottom right corner */
238 ESC "[6n"); /* Query cursor position */
Rob Clark71cc25c2017-09-13 18:05:42 -0400239
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200240 /* Read {rows,cols} */
241 if (term_read_reply(n, 2, 'R')) {
242 ret = 1;
243 goto out;
244 }
Rob Clark71cc25c2017-09-13 18:05:42 -0400245
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200246 *cols = n[1];
247 *rows = n[0];
248out:
249 printf(ESC "8"); /* Restore cursor position */
250 return ret;
Rob Clark71cc25c2017-09-13 18:05:42 -0400251}
252
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200253/*
254 * Update the mode table.
255 *
256 * By default the only mode available is 80x25. If the console has at least 50
257 * lines, enable mode 80x50. If we can query the console size and it is neither
258 * 80x25 nor 80x50, set it as an additional mode.
259 */
260static void query_console_size(void)
261{
262 const char *stdout_name = env_get("stdout");
Alexander Graf80483b22018-06-03 15:51:17 +0200263 int rows = 25, cols = 80;
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200264
265 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
266 IS_ENABLED(CONFIG_DM_VIDEO)) {
267 struct stdio_dev *stdout_dev =
268 stdio_get_by_name("vidconsole");
269 struct udevice *dev = stdout_dev->priv;
270 struct vidconsole_priv *priv =
271 dev_get_uclass_priv(dev);
272 rows = priv->rows;
273 cols = priv->cols;
274 } else if (query_console_serial(&rows, &cols)) {
275 return;
276 }
277
278 /* Test if we can have Mode 1 */
279 if (cols >= 80 && rows >= 50) {
280 efi_cout_modes[1].present = 1;
281 efi_con_mode.max_mode = 2;
282 }
283
284 /*
285 * Install our mode as mode 2 if it is different
286 * than mode 0 or 1 and set it as the currently selected mode
287 */
288 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
289 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
290 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
291 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
292 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
293 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
294 efi_con_mode.mode = EFI_COUT_MODE_2;
295 }
296}
297
Alexander Grafc1311ad2016-03-04 01:10:00 +0100298static efi_status_t EFIAPI efi_cout_query_mode(
299 struct efi_simple_text_output_protocol *this,
300 unsigned long mode_number, unsigned long *columns,
301 unsigned long *rows)
302{
303 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
304
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100305 if (mode_number >= efi_con_mode.max_mode)
306 return EFI_EXIT(EFI_UNSUPPORTED);
307
308 if (efi_cout_modes[mode_number].present != 1)
309 return EFI_EXIT(EFI_UNSUPPORTED);
310
Alexander Grafc1311ad2016-03-04 01:10:00 +0100311 if (columns)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100312 *columns = efi_cout_modes[mode_number].columns;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100313 if (rows)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100314 *rows = efi_cout_modes[mode_number].rows;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100315
316 return EFI_EXIT(EFI_SUCCESS);
317}
318
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400319static const struct {
320 unsigned int fg;
321 unsigned int bg;
322} color[] = {
323 { 30, 40 }, /* 0: black */
324 { 34, 44 }, /* 1: blue */
325 { 32, 42 }, /* 2: green */
326 { 36, 46 }, /* 3: cyan */
327 { 31, 41 }, /* 4: red */
328 { 35, 45 }, /* 5: magenta */
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +0200329 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
330 { 37, 47 }, /* 7: light gray, map to white */
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400331};
332
333/* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100334static efi_status_t EFIAPI efi_cout_set_attribute(
335 struct efi_simple_text_output_protocol *this,
336 unsigned long attribute)
337{
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400338 unsigned int bold = EFI_ATTR_BOLD(attribute);
339 unsigned int fg = EFI_ATTR_FG(attribute);
340 unsigned int bg = EFI_ATTR_BG(attribute);
341
Alexander Grafc1311ad2016-03-04 01:10:00 +0100342 EFI_ENTRY("%p, %lx", this, attribute);
343
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400344 if (attribute)
345 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
346 else
347 printf(ESC"[0;37;40m");
348
349 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100350}
351
352static efi_status_t EFIAPI efi_cout_clear_screen(
353 struct efi_simple_text_output_protocol *this)
354{
355 EFI_ENTRY("%p", this);
356
357 printf(ESC"[2J");
Heinrich Schuchardte67ff942018-07-05 08:18:00 +0200358 efi_con_mode.cursor_column = 0;
359 efi_con_mode.cursor_row = 0;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100360
361 return EFI_EXIT(EFI_SUCCESS);
362}
363
Heinrich Schuchardt2ad238f2019-06-14 07:20:51 +0200364static efi_status_t EFIAPI efi_cout_set_mode(
365 struct efi_simple_text_output_protocol *this,
366 unsigned long mode_number)
367{
368 EFI_ENTRY("%p, %ld", this, mode_number);
369
370 if (mode_number >= efi_con_mode.max_mode)
371 return EFI_EXIT(EFI_UNSUPPORTED);
372 efi_con_mode.mode = mode_number;
373 EFI_CALL(efi_cout_clear_screen(this));
374
375 return EFI_EXIT(EFI_SUCCESS);
376}
377
Heinrich Schuchardt9d12daf2018-07-05 19:58:07 +0200378static efi_status_t EFIAPI efi_cout_reset(
379 struct efi_simple_text_output_protocol *this,
380 char extended_verification)
381{
382 EFI_ENTRY("%p, %d", this, extended_verification);
383
384 /* Clear screen */
385 EFI_CALL(efi_cout_clear_screen(this));
386 /* Set default colors */
387 printf(ESC "[0;37;40m");
388
389 return EFI_EXIT(EFI_SUCCESS);
390}
391
Alexander Grafc1311ad2016-03-04 01:10:00 +0100392static efi_status_t EFIAPI efi_cout_set_cursor_position(
393 struct efi_simple_text_output_protocol *this,
394 unsigned long column, unsigned long row)
395{
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200396 efi_status_t ret = EFI_SUCCESS;
397 struct simple_text_output_mode *con = &efi_con_mode;
398 struct cout_mode *mode = &efi_cout_modes[con->mode];
399
Alexander Grafc1311ad2016-03-04 01:10:00 +0100400 EFI_ENTRY("%p, %ld, %ld", this, column, row);
401
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200402 /* Check parameters */
403 if (!this) {
404 ret = EFI_INVALID_PARAMETER;
405 goto out;
406 }
407 if (row >= mode->rows || column >= mode->columns) {
408 ret = EFI_UNSUPPORTED;
409 goto out;
410 }
411
412 /*
413 * Set cursor position by sending CSI H.
414 * EFI origin is [0, 0], terminal origin is [1, 1].
415 */
416 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100417 efi_con_mode.cursor_column = column;
418 efi_con_mode.cursor_row = row;
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200419out:
420 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100421}
422
423static efi_status_t EFIAPI efi_cout_enable_cursor(
424 struct efi_simple_text_output_protocol *this,
425 bool enable)
426{
427 EFI_ENTRY("%p, %d", this, enable);
428
429 printf(ESC"[?25%c", enable ? 'h' : 'l');
Heinrich Schuchardt25e6fb52019-06-02 22:54:28 +0200430 efi_con_mode.cursor_visible = !!enable;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100431
432 return EFI_EXIT(EFI_SUCCESS);
433}
434
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200435struct efi_simple_text_output_protocol efi_con_out = {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100436 .reset = efi_cout_reset,
437 .output_string = efi_cout_output_string,
438 .test_string = efi_cout_test_string,
439 .query_mode = efi_cout_query_mode,
440 .set_mode = efi_cout_set_mode,
441 .set_attribute = efi_cout_set_attribute,
442 .clear_screen = efi_cout_clear_screen,
443 .set_cursor_position = efi_cout_set_cursor_position,
444 .enable_cursor = efi_cout_enable_cursor,
445 .mode = (void*)&efi_con_mode,
446};
447
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200448/**
449 * struct efi_cin_notify_function - registered console input notify function
450 *
451 * @link: link to list
452 * @data: key to notify
453 * @function: function to call
454 */
455struct efi_cin_notify_function {
456 struct list_head link;
457 struct efi_key_data key;
458 efi_status_t (EFIAPI *function)
459 (struct efi_key_data *key_data);
460};
461
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200462static bool key_available;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200463static struct efi_key_data next_key;
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200464static LIST_HEAD(cin_notify_functions);
Heinrich Schuchardt4f187892018-07-05 08:17:59 +0200465
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200466/**
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200467 * set_shift_mask() - set shift mask
468 *
469 * @mod: Xterm shift mask
470 */
471void set_shift_mask(int mod, struct efi_key_state *key_state)
472{
473 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
474 if (mod) {
475 --mod;
476 if (mod & 1)
477 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
478 if (mod & 2)
479 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
480 if (mod & 4)
481 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
482 if (mod & 8)
483 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
484 } else {
485 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
486 }
487}
488
489/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200490 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200491 *
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100492 * This gets called when we have already parsed CSI.
493 *
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200494 * @modifiers: bit mask (shift, alt, ctrl)
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100495 * @return: the unmodified code
496 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200497static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100498{
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200499 int c, mod = 0, ret = 0;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100500
501 c = getc();
502
503 if (c != ';') {
504 ret = c;
505 if (c == '~')
506 goto out;
507 c = getc();
508 }
509 for (;;) {
510 switch (c) {
511 case '0'...'9':
512 mod *= 10;
513 mod += c - '0';
514 /* fall through */
515 case ';':
516 c = getc();
517 break;
518 default:
519 goto out;
520 }
521 }
522out:
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200523 set_shift_mask(mod, key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100524 if (!ret)
525 ret = c;
526 return ret;
527}
528
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200529/**
530 * efi_cin_read_key() - read a key from the console input
531 *
532 * @key: - key received
533 * Return: - status code
534 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200535static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100536{
537 struct efi_input_key pressed_key = {
538 .scan_code = 0,
539 .unicode_char = 0,
540 };
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200541 s32 ch;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100542
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200543 if (console_read_unicode(&ch))
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200544 return EFI_NOT_READY;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200545
546 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
547 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
548
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200549 /* We do not support multi-word codes */
550 if (ch >= 0x10000)
551 ch = '?';
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200552
553 switch (ch) {
554 case 0x1b:
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100555 /*
556 * Xterm Control Sequences
557 * https://www.xfree86.org/4.8.0/ctlseqs.html
558 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100559 ch = getc();
560 switch (ch) {
561 case cESC: /* ESC */
562 pressed_key.scan_code = 23;
563 break;
564 case 'O': /* F1 - F4 */
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100565 ch = getc();
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200566 /* consider modifiers */
567 if (ch < 'P') {
568 set_shift_mask(ch - '0', &key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100569 ch = getc();
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200570 }
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100571 pressed_key.scan_code = ch - 'P' + 11;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100572 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100573 case '[':
574 ch = getc();
575 switch (ch) {
576 case 'A'...'D': /* up, down right, left */
577 pressed_key.scan_code = ch - 'A' + 1;
578 break;
579 case 'F': /* End */
580 pressed_key.scan_code = 6;
581 break;
582 case 'H': /* Home */
583 pressed_key.scan_code = 5;
584 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100585 case '1':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200586 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100587 switch (ch) {
588 case '1'...'5': /* F1 - F5 */
589 pressed_key.scan_code = ch - '1' + 11;
590 break;
591 case '7'...'9': /* F6 - F8 */
592 pressed_key.scan_code = ch - '7' + 16;
593 break;
594 case 'A'...'D': /* up, down right, left */
595 pressed_key.scan_code = ch - 'A' + 1;
596 break;
597 case 'F':
598 pressed_key.scan_code = 6; /* End */
599 break;
600 case 'H':
601 pressed_key.scan_code = 5; /* Home */
602 break;
603 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100604 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100605 case '2':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200606 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100607 switch (ch) {
608 case '0'...'1': /* F9 - F10 */
609 pressed_key.scan_code = ch - '0' + 19;
610 break;
611 case '3'...'4': /* F11 - F12 */
612 pressed_key.scan_code = ch - '3' + 21;
613 break;
614 case '~': /* INS */
615 pressed_key.scan_code = 7;
616 break;
617 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100618 break;
619 case '3': /* DEL */
620 pressed_key.scan_code = 8;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200621 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100622 break;
623 case '5': /* PG UP */
624 pressed_key.scan_code = 9;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200625 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100626 break;
627 case '6': /* PG DOWN */
628 pressed_key.scan_code = 10;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200629 analyze_modifiers(&key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100630 break;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200631 } /* [ */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100632 break;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200633 default:
634 /* ALT key */
635 set_shift_mask(3, &key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100636 }
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200637 break;
638 case 0x7f:
Alexander Grafc1311ad2016-03-04 01:10:00 +0100639 /* Backspace */
640 ch = 0x08;
641 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200642 if (pressed_key.scan_code) {
643 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
644 } else {
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100645 pressed_key.unicode_char = ch;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200646
647 /*
648 * Assume left control key for control characters typically
649 * entered using the control key.
650 */
651 if (ch >= 0x01 && ch <= 0x1f) {
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200652 key->key_state.key_shift_state |=
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200653 EFI_SHIFT_STATE_VALID;
654 switch (ch) {
655 case 0x01 ... 0x07:
656 case 0x0b ... 0x0c:
657 case 0x0e ... 0x1f:
658 key->key_state.key_shift_state |=
659 EFI_LEFT_CONTROL_PRESSED;
660 }
661 }
662 }
663 key->key = pressed_key;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100664
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200665 return EFI_SUCCESS;
666}
667
668/**
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200669 * efi_cin_notify() - notify registered functions
670 */
671static void efi_cin_notify(void)
672{
673 struct efi_cin_notify_function *item;
674
675 list_for_each_entry(item, &cin_notify_functions, link) {
676 bool match = true;
677
678 /* We do not support toggle states */
679 if (item->key.key.unicode_char || item->key.key.scan_code) {
680 if (item->key.key.unicode_char !=
681 next_key.key.unicode_char ||
682 item->key.key.scan_code != next_key.key.scan_code)
683 match = false;
684 }
685 if (item->key.key_state.key_shift_state &&
686 item->key.key_state.key_shift_state !=
687 next_key.key_state.key_shift_state)
688 match = false;
689
690 if (match)
691 /* We don't bother about the return code */
692 EFI_CALL(item->function(&next_key));
693 }
694}
695
696/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200697 * efi_cin_check() - check if keyboard input is available
698 */
699static void efi_cin_check(void)
700{
701 efi_status_t ret;
702
703 if (key_available) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200704 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200705 return;
706 }
707
708 if (tstc()) {
709 ret = efi_cin_read_key(&next_key);
710 if (ret == EFI_SUCCESS) {
711 key_available = true;
712
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200713 /* Notify registered functions */
714 efi_cin_notify();
715
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200716 /* Queue the wait for key event */
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200717 if (key_available)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200718 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200719 }
720 }
721}
722
723/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200724 * efi_cin_empty_buffer() - empty input buffer
725 */
726static void efi_cin_empty_buffer(void)
727{
728 while (tstc())
729 getc();
730 key_available = false;
731}
732
733/**
734 * efi_cin_reset_ex() - reset console input
735 *
736 * @this: - the extended simple text input protocol
737 * @extended_verification: - extended verification
738 *
739 * This function implements the reset service of the
740 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
741 *
742 * See the Unified Extensible Firmware Interface (UEFI) specification for
743 * details.
744 *
745 * Return: old value of the task priority level
746 */
747static efi_status_t EFIAPI efi_cin_reset_ex(
748 struct efi_simple_text_input_ex_protocol *this,
749 bool extended_verification)
750{
751 efi_status_t ret = EFI_SUCCESS;
752
753 EFI_ENTRY("%p, %d", this, extended_verification);
754
755 /* Check parameters */
756 if (!this) {
757 ret = EFI_INVALID_PARAMETER;
758 goto out;
759 }
760
761 efi_cin_empty_buffer();
762out:
763 return EFI_EXIT(ret);
764}
765
766/**
767 * efi_cin_read_key_stroke_ex() - read key stroke
768 *
769 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
770 * @key_data: key read from console
771 * Return: status code
772 *
773 * This function implements the ReadKeyStrokeEx service of the
774 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
775 *
776 * See the Unified Extensible Firmware Interface (UEFI) specification for
777 * details.
778 */
779static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
780 struct efi_simple_text_input_ex_protocol *this,
781 struct efi_key_data *key_data)
782{
783 efi_status_t ret = EFI_SUCCESS;
784
785 EFI_ENTRY("%p, %p", this, key_data);
786
787 /* Check parameters */
788 if (!this || !key_data) {
789 ret = EFI_INVALID_PARAMETER;
790 goto out;
791 }
792
793 /* We don't do interrupts, so check for timers cooperatively */
794 efi_timer_check();
795
796 /* Enable console input after ExitBootServices */
797 efi_cin_check();
798
799 if (!key_available) {
800 ret = EFI_NOT_READY;
801 goto out;
802 }
Heinrich Schuchardtbfc2dd52019-04-06 20:59:24 +0200803 /*
804 * CTRL+A - CTRL+Z have to be signaled as a - z.
805 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
806 */
807 switch (next_key.key.unicode_char) {
808 case 0x01 ... 0x07:
809 case 0x0b ... 0x0c:
810 case 0x0e ... 0x1a:
811 if (!(next_key.key_state.key_toggle_state &
812 EFI_CAPS_LOCK_ACTIVE) ^
813 !(next_key.key_state.key_shift_state &
814 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
815 next_key.key.unicode_char += 0x40;
816 else
817 next_key.key.unicode_char += 0x60;
818 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200819 *key_data = next_key;
820 key_available = false;
821 efi_con_in.wait_for_key->is_signaled = false;
Heinrich Schuchardtbfc2dd52019-04-06 20:59:24 +0200822
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200823out:
824 return EFI_EXIT(ret);
825}
826
827/**
828 * efi_cin_set_state() - set toggle key state
829 *
830 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
Heinrich Schuchardtacee9652019-05-18 17:07:52 +0200831 * @key_toggle_state: pointer to key toggle state
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200832 * Return: status code
833 *
834 * This function implements the SetState service of the
835 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
836 *
837 * See the Unified Extensible Firmware Interface (UEFI) specification for
838 * details.
839 */
840static efi_status_t EFIAPI efi_cin_set_state(
841 struct efi_simple_text_input_ex_protocol *this,
Heinrich Schuchardtacee9652019-05-18 17:07:52 +0200842 u8 *key_toggle_state)
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200843{
Heinrich Schuchardtacee9652019-05-18 17:07:52 +0200844 EFI_ENTRY("%p, %p", this, key_toggle_state);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200845 /*
846 * U-Boot supports multiple console input sources like serial and
847 * net console for which a key toggle state cannot be set at all.
848 *
849 * According to the UEFI specification it is allowable to not implement
850 * this service.
851 */
852 return EFI_EXIT(EFI_UNSUPPORTED);
853}
854
855/**
856 * efi_cin_register_key_notify() - register key notification function
857 *
858 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
859 * @key_data: key to be notified
860 * @key_notify_function: function to be called if the key is pressed
861 * @notify_handle: handle for unregistering the notification
862 * Return: status code
863 *
864 * This function implements the SetState service of the
865 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
866 *
867 * See the Unified Extensible Firmware Interface (UEFI) specification for
868 * details.
869 */
870static efi_status_t EFIAPI efi_cin_register_key_notify(
871 struct efi_simple_text_input_ex_protocol *this,
872 struct efi_key_data *key_data,
873 efi_status_t (EFIAPI *key_notify_function)(
874 struct efi_key_data *key_data),
875 void **notify_handle)
876{
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200877 efi_status_t ret = EFI_SUCCESS;
878 struct efi_cin_notify_function *notify_function;
879
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200880 EFI_ENTRY("%p, %p, %p, %p",
881 this, key_data, key_notify_function, notify_handle);
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200882
883 /* Check parameters */
884 if (!this || !key_data || !key_notify_function || !notify_handle) {
885 ret = EFI_INVALID_PARAMETER;
886 goto out;
887 }
888
889 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
890 key_data->key.unicode_char,
891 key_data->key.scan_code,
892 key_data->key_state.key_shift_state,
893 key_data->key_state.key_toggle_state);
894
895 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
896 if (!notify_function) {
897 ret = EFI_OUT_OF_RESOURCES;
898 goto out;
899 }
900 notify_function->key = *key_data;
901 notify_function->function = key_notify_function;
902 list_add_tail(&notify_function->link, &cin_notify_functions);
903 *notify_handle = notify_function;
904out:
905 return EFI_EXIT(ret);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200906}
907
908/**
909 * efi_cin_unregister_key_notify() - unregister key notification function
910 *
911 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
912 * @notification_handle: handle received when registering
913 * Return: status code
914 *
915 * This function implements the SetState service of the
916 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
917 *
918 * See the Unified Extensible Firmware Interface (UEFI) specification for
919 * details.
920 */
921static efi_status_t EFIAPI efi_cin_unregister_key_notify(
922 struct efi_simple_text_input_ex_protocol *this,
923 void *notification_handle)
924{
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200925 efi_status_t ret = EFI_INVALID_PARAMETER;
926 struct efi_cin_notify_function *item, *notify_function =
927 notification_handle;
928
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200929 EFI_ENTRY("%p, %p", this, notification_handle);
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200930
931 /* Check parameters */
932 if (!this || !notification_handle)
933 goto out;
934
935 list_for_each_entry(item, &cin_notify_functions, link) {
936 if (item == notify_function) {
937 ret = EFI_SUCCESS;
938 break;
939 }
940 }
941 if (ret != EFI_SUCCESS)
942 goto out;
943
944 /* Remove the notify function */
945 list_del(&notify_function->link);
946 free(notify_function);
947out:
948 return EFI_EXIT(ret);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200949}
950
951
952/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200953 * efi_cin_reset() - drain the input buffer
954 *
955 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
956 * @extended_verification: allow for exhaustive verification
957 * Return: status code
958 *
959 * This function implements the Reset service of the
960 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
961 *
962 * See the Unified Extensible Firmware Interface (UEFI) specification for
963 * details.
964 */
965static efi_status_t EFIAPI efi_cin_reset
966 (struct efi_simple_text_input_protocol *this,
967 bool extended_verification)
968{
969 efi_status_t ret = EFI_SUCCESS;
970
971 EFI_ENTRY("%p, %d", this, extended_verification);
972
973 /* Check parameters */
974 if (!this) {
975 ret = EFI_INVALID_PARAMETER;
976 goto out;
977 }
978
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200979 efi_cin_empty_buffer();
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200980out:
981 return EFI_EXIT(ret);
982}
983
984/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200985 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200986 *
987 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
988 * @key: key read from console
989 * Return: status code
990 *
991 * This function implements the ReadKeyStroke service of the
992 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
993 *
994 * See the Unified Extensible Firmware Interface (UEFI) specification for
995 * details.
996 */
997static efi_status_t EFIAPI efi_cin_read_key_stroke
998 (struct efi_simple_text_input_protocol *this,
999 struct efi_input_key *key)
1000{
1001 efi_status_t ret = EFI_SUCCESS;
1002
1003 EFI_ENTRY("%p, %p", this, key);
1004
1005 /* Check parameters */
1006 if (!this || !key) {
1007 ret = EFI_INVALID_PARAMETER;
1008 goto out;
1009 }
1010
1011 /* We don't do interrupts, so check for timers cooperatively */
1012 efi_timer_check();
1013
1014 /* Enable console input after ExitBootServices */
1015 efi_cin_check();
1016
1017 if (!key_available) {
1018 ret = EFI_NOT_READY;
1019 goto out;
1020 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001021 *key = next_key.key;
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001022 key_available = false;
1023 efi_con_in.wait_for_key->is_signaled = false;
1024out:
1025 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +01001026}
1027
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001028static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1029 .reset = efi_cin_reset_ex,
1030 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1031 .wait_for_key_ex = NULL,
1032 .set_state = efi_cin_set_state,
1033 .register_key_notify = efi_cin_register_key_notify,
1034 .unregister_key_notify = efi_cin_unregister_key_notify,
1035};
1036
Heinrich Schuchardt3e603ec2018-09-08 10:20:10 +02001037struct efi_simple_text_input_protocol efi_con_in = {
Alexander Grafc1311ad2016-03-04 01:10:00 +01001038 .reset = efi_cin_reset,
1039 .read_key_stroke = efi_cin_read_key_stroke,
1040 .wait_for_key = NULL,
1041};
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001042
1043static struct efi_event *console_timer_event;
1044
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001045/*
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001046 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001047 *
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001048 * @event: console timer event
1049 * @context: not used
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001050 */
xypron.glpk@gmx.deff925932017-07-20 05:26:07 +02001051static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1052 void *context)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001053{
1054 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001055 efi_cin_check();
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001056 EFI_EXIT(EFI_SUCCESS);
1057}
1058
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001059/**
1060 * efi_key_notify() - notify the wait for key event
1061 *
1062 * @event: wait for key event
1063 * @context: not used
1064 */
1065static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1066{
1067 EFI_ENTRY("%p, %p", event, context);
1068 efi_cin_check();
1069 EFI_EXIT(EFI_SUCCESS);
1070}
1071
1072/**
1073 * efi_console_register() - install the console protocols
1074 *
1075 * This function is called from do_bootefi_exec().
Heinrich Schuchardt6f566c22018-10-02 06:08:26 +02001076 *
1077 * Return: status code
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001078 */
Heinrich Schuchardt6f566c22018-10-02 06:08:26 +02001079efi_status_t efi_console_register(void)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001080{
1081 efi_status_t r;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001082 efi_handle_t console_output_handle;
1083 efi_handle_t console_input_handle;
Rob Clarka17e62c2017-07-24 10:39:01 -04001084
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +02001085 /* Set up mode information */
1086 query_console_size();
1087
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001088 /* Create handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001089 r = efi_create_handle(&console_output_handle);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001090 if (r != EFI_SUCCESS)
1091 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +02001092
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001093 r = efi_add_protocol(console_output_handle,
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001094 &efi_guid_text_output_protocol, &efi_con_out);
1095 if (r != EFI_SUCCESS)
1096 goto out_of_memory;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001097 systab.con_out_handle = console_output_handle;
1098 systab.stderr_handle = console_output_handle;
Alexander Graf40e3e752018-09-04 14:59:11 +02001099
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001100 r = efi_create_handle(&console_input_handle);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001101 if (r != EFI_SUCCESS)
1102 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +02001103
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001104 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001105 &efi_guid_text_input_protocol, &efi_con_in);
1106 if (r != EFI_SUCCESS)
1107 goto out_of_memory;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001108 systab.con_in_handle = console_input_handle;
1109 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001110 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1111 if (r != EFI_SUCCESS)
1112 goto out_of_memory;
Rob Clarka17e62c2017-07-24 10:39:01 -04001113
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001114 /* Create console events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001115 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1116 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001117 if (r != EFI_SUCCESS) {
1118 printf("ERROR: Failed to register WaitForKey event\n");
1119 return r;
1120 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001121 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001122 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001123 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001124 &console_timer_event);
1125 if (r != EFI_SUCCESS) {
1126 printf("ERROR: Failed to register console event\n");
1127 return r;
1128 }
1129 /* 5000 ns cycle is sufficient for 2 MBaud */
1130 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1131 if (r != EFI_SUCCESS)
1132 printf("ERROR: Failed to set console timer\n");
1133 return r;
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001134out_of_memory:
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +02001135 printf("ERROR: Out of memory\n");
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001136 return r;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001137}