blob: 2fc25e118f2ddb07141b6895326defba89888882 [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
319static efi_status_t EFIAPI efi_cout_set_mode(
320 struct efi_simple_text_output_protocol *this,
321 unsigned long mode_number)
322{
323 EFI_ENTRY("%p, %ld", this, mode_number);
324
Alexander Grafc1311ad2016-03-04 01:10:00 +0100325
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100326 if (mode_number > efi_con_mode.max_mode)
327 return EFI_EXIT(EFI_UNSUPPORTED);
328
329 efi_con_mode.mode = mode_number;
330 efi_con_mode.cursor_column = 0;
331 efi_con_mode.cursor_row = 0;
332
333 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100334}
335
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400336static const struct {
337 unsigned int fg;
338 unsigned int bg;
339} color[] = {
340 { 30, 40 }, /* 0: black */
341 { 34, 44 }, /* 1: blue */
342 { 32, 42 }, /* 2: green */
343 { 36, 46 }, /* 3: cyan */
344 { 31, 41 }, /* 4: red */
345 { 35, 45 }, /* 5: magenta */
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +0200346 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
347 { 37, 47 }, /* 7: light gray, map to white */
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400348};
349
350/* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100351static efi_status_t EFIAPI efi_cout_set_attribute(
352 struct efi_simple_text_output_protocol *this,
353 unsigned long attribute)
354{
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400355 unsigned int bold = EFI_ATTR_BOLD(attribute);
356 unsigned int fg = EFI_ATTR_FG(attribute);
357 unsigned int bg = EFI_ATTR_BG(attribute);
358
Alexander Grafc1311ad2016-03-04 01:10:00 +0100359 EFI_ENTRY("%p, %lx", this, attribute);
360
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400361 if (attribute)
362 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
363 else
364 printf(ESC"[0;37;40m");
365
366 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100367}
368
369static efi_status_t EFIAPI efi_cout_clear_screen(
370 struct efi_simple_text_output_protocol *this)
371{
372 EFI_ENTRY("%p", this);
373
374 printf(ESC"[2J");
Heinrich Schuchardte67ff942018-07-05 08:18:00 +0200375 efi_con_mode.cursor_column = 0;
376 efi_con_mode.cursor_row = 0;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100377
378 return EFI_EXIT(EFI_SUCCESS);
379}
380
Heinrich Schuchardt9d12daf2018-07-05 19:58:07 +0200381static efi_status_t EFIAPI efi_cout_reset(
382 struct efi_simple_text_output_protocol *this,
383 char extended_verification)
384{
385 EFI_ENTRY("%p, %d", this, extended_verification);
386
387 /* Clear screen */
388 EFI_CALL(efi_cout_clear_screen(this));
389 /* Set default colors */
390 printf(ESC "[0;37;40m");
391
392 return EFI_EXIT(EFI_SUCCESS);
393}
394
Alexander Grafc1311ad2016-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 Schuchardtaaace4b2018-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 Grafc1311ad2016-03-04 01:10:00 +0100403 EFI_ENTRY("%p, %ld, %ld", this, column, row);
404
Heinrich Schuchardtaaace4b2018-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 Grafc1311ad2016-03-04 01:10:00 +0100420 efi_con_mode.cursor_column = column;
421 efi_con_mode.cursor_row = row;
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200422out:
423 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-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 Schuchardt25e6fb52019-06-02 22:54:28 +0200433 efi_con_mode.cursor_visible = !!enable;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100434
435 return EFI_EXIT(EFI_SUCCESS);
436}
437
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200438struct efi_simple_text_output_protocol efi_con_out = {
Alexander Grafc1311ad2016-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 Schuchardt4fdcf062018-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 Schuchardt0dfd13a2018-09-11 22:38:05 +0200465static bool key_available;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200466static struct efi_key_data next_key;
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200467static LIST_HEAD(cin_notify_functions);
Heinrich Schuchardt4f187892018-07-05 08:17:59 +0200468
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200469/**
Heinrich Schuchardt55fbdf92018-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;
485 if (mod & 8)
486 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
487 } else {
488 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
489 }
490}
491
492/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200493 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200494 *
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100495 * This gets called when we have already parsed CSI.
496 *
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200497 * @modifiers: bit mask (shift, alt, ctrl)
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100498 * @return: the unmodified code
499 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200500static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100501{
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200502 int c, mod = 0, ret = 0;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100503
504 c = getc();
505
506 if (c != ';') {
507 ret = c;
508 if (c == '~')
509 goto out;
510 c = getc();
511 }
512 for (;;) {
513 switch (c) {
514 case '0'...'9':
515 mod *= 10;
516 mod += c - '0';
517 /* fall through */
518 case ';':
519 c = getc();
520 break;
521 default:
522 goto out;
523 }
524 }
525out:
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200526 set_shift_mask(mod, key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100527 if (!ret)
528 ret = c;
529 return ret;
530}
531
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200532/**
533 * efi_cin_read_key() - read a key from the console input
534 *
535 * @key: - key received
536 * Return: - status code
537 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200538static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100539{
540 struct efi_input_key pressed_key = {
541 .scan_code = 0,
542 .unicode_char = 0,
543 };
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200544 s32 ch;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100545
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200546 if (console_read_unicode(&ch))
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200547 return EFI_NOT_READY;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200548
549 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
550 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
551
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200552 /* We do not support multi-word codes */
553 if (ch >= 0x10000)
554 ch = '?';
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200555
556 switch (ch) {
557 case 0x1b:
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100558 /*
559 * Xterm Control Sequences
560 * https://www.xfree86.org/4.8.0/ctlseqs.html
561 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100562 ch = getc();
563 switch (ch) {
564 case cESC: /* ESC */
565 pressed_key.scan_code = 23;
566 break;
567 case 'O': /* F1 - F4 */
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100568 ch = getc();
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200569 /* consider modifiers */
570 if (ch < 'P') {
571 set_shift_mask(ch - '0', &key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100572 ch = getc();
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200573 }
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100574 pressed_key.scan_code = ch - 'P' + 11;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100575 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100576 case '[':
577 ch = getc();
578 switch (ch) {
579 case 'A'...'D': /* up, down right, left */
580 pressed_key.scan_code = ch - 'A' + 1;
581 break;
582 case 'F': /* End */
583 pressed_key.scan_code = 6;
584 break;
585 case 'H': /* Home */
586 pressed_key.scan_code = 5;
587 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100588 case '1':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200589 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100590 switch (ch) {
591 case '1'...'5': /* F1 - F5 */
592 pressed_key.scan_code = ch - '1' + 11;
593 break;
594 case '7'...'9': /* F6 - F8 */
595 pressed_key.scan_code = ch - '7' + 16;
596 break;
597 case 'A'...'D': /* up, down right, left */
598 pressed_key.scan_code = ch - 'A' + 1;
599 break;
600 case 'F':
601 pressed_key.scan_code = 6; /* End */
602 break;
603 case 'H':
604 pressed_key.scan_code = 5; /* Home */
605 break;
606 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100607 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100608 case '2':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200609 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100610 switch (ch) {
611 case '0'...'1': /* F9 - F10 */
612 pressed_key.scan_code = ch - '0' + 19;
613 break;
614 case '3'...'4': /* F11 - F12 */
615 pressed_key.scan_code = ch - '3' + 21;
616 break;
617 case '~': /* INS */
618 pressed_key.scan_code = 7;
619 break;
620 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100621 break;
622 case '3': /* DEL */
623 pressed_key.scan_code = 8;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200624 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100625 break;
626 case '5': /* PG UP */
627 pressed_key.scan_code = 9;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200628 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100629 break;
630 case '6': /* PG DOWN */
631 pressed_key.scan_code = 10;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200632 analyze_modifiers(&key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100633 break;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200634 } /* [ */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100635 break;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200636 default:
637 /* ALT key */
638 set_shift_mask(3, &key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100639 }
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200640 break;
641 case 0x7f:
Alexander Grafc1311ad2016-03-04 01:10:00 +0100642 /* Backspace */
643 ch = 0x08;
644 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200645 if (pressed_key.scan_code) {
646 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
647 } else {
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100648 pressed_key.unicode_char = ch;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200649
650 /*
651 * Assume left control key for control characters typically
652 * entered using the control key.
653 */
654 if (ch >= 0x01 && ch <= 0x1f) {
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200655 key->key_state.key_shift_state |=
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200656 EFI_SHIFT_STATE_VALID;
657 switch (ch) {
658 case 0x01 ... 0x07:
659 case 0x0b ... 0x0c:
660 case 0x0e ... 0x1f:
661 key->key_state.key_shift_state |=
662 EFI_LEFT_CONTROL_PRESSED;
663 }
664 }
665 }
666 key->key = pressed_key;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100667
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200668 return EFI_SUCCESS;
669}
670
671/**
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200672 * efi_cin_notify() - notify registered functions
673 */
674static void efi_cin_notify(void)
675{
676 struct efi_cin_notify_function *item;
677
678 list_for_each_entry(item, &cin_notify_functions, link) {
679 bool match = true;
680
681 /* We do not support toggle states */
682 if (item->key.key.unicode_char || item->key.key.scan_code) {
683 if (item->key.key.unicode_char !=
684 next_key.key.unicode_char ||
685 item->key.key.scan_code != next_key.key.scan_code)
686 match = false;
687 }
688 if (item->key.key_state.key_shift_state &&
689 item->key.key_state.key_shift_state !=
690 next_key.key_state.key_shift_state)
691 match = false;
692
693 if (match)
694 /* We don't bother about the return code */
695 EFI_CALL(item->function(&next_key));
696 }
697}
698
699/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200700 * efi_cin_check() - check if keyboard input is available
701 */
702static void efi_cin_check(void)
703{
704 efi_status_t ret;
705
706 if (key_available) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200707 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200708 return;
709 }
710
711 if (tstc()) {
712 ret = efi_cin_read_key(&next_key);
713 if (ret == EFI_SUCCESS) {
714 key_available = true;
715
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200716 /* Notify registered functions */
717 efi_cin_notify();
718
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200719 /* Queue the wait for key event */
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200720 if (key_available)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200721 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200722 }
723 }
724}
725
726/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200727 * efi_cin_empty_buffer() - empty input buffer
728 */
729static void efi_cin_empty_buffer(void)
730{
731 while (tstc())
732 getc();
733 key_available = false;
734}
735
736/**
737 * efi_cin_reset_ex() - reset console input
738 *
739 * @this: - the extended simple text input protocol
740 * @extended_verification: - extended verification
741 *
742 * This function implements the reset service of the
743 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
744 *
745 * See the Unified Extensible Firmware Interface (UEFI) specification for
746 * details.
747 *
748 * Return: old value of the task priority level
749 */
750static efi_status_t EFIAPI efi_cin_reset_ex(
751 struct efi_simple_text_input_ex_protocol *this,
752 bool extended_verification)
753{
754 efi_status_t ret = EFI_SUCCESS;
755
756 EFI_ENTRY("%p, %d", this, extended_verification);
757
758 /* Check parameters */
759 if (!this) {
760 ret = EFI_INVALID_PARAMETER;
761 goto out;
762 }
763
764 efi_cin_empty_buffer();
765out:
766 return EFI_EXIT(ret);
767}
768
769/**
770 * efi_cin_read_key_stroke_ex() - read key stroke
771 *
772 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
773 * @key_data: key read from console
774 * Return: status code
775 *
776 * This function implements the ReadKeyStrokeEx service of the
777 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
778 *
779 * See the Unified Extensible Firmware Interface (UEFI) specification for
780 * details.
781 */
782static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
783 struct efi_simple_text_input_ex_protocol *this,
784 struct efi_key_data *key_data)
785{
786 efi_status_t ret = EFI_SUCCESS;
787
788 EFI_ENTRY("%p, %p", this, key_data);
789
790 /* Check parameters */
791 if (!this || !key_data) {
792 ret = EFI_INVALID_PARAMETER;
793 goto out;
794 }
795
796 /* We don't do interrupts, so check for timers cooperatively */
797 efi_timer_check();
798
799 /* Enable console input after ExitBootServices */
800 efi_cin_check();
801
802 if (!key_available) {
803 ret = EFI_NOT_READY;
804 goto out;
805 }
Heinrich Schuchardtbfc2dd52019-04-06 20:59:24 +0200806 /*
807 * CTRL+A - CTRL+Z have to be signaled as a - z.
808 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
809 */
810 switch (next_key.key.unicode_char) {
811 case 0x01 ... 0x07:
812 case 0x0b ... 0x0c:
813 case 0x0e ... 0x1a:
814 if (!(next_key.key_state.key_toggle_state &
815 EFI_CAPS_LOCK_ACTIVE) ^
816 !(next_key.key_state.key_shift_state &
817 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
818 next_key.key.unicode_char += 0x40;
819 else
820 next_key.key.unicode_char += 0x60;
821 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200822 *key_data = next_key;
823 key_available = false;
824 efi_con_in.wait_for_key->is_signaled = false;
Heinrich Schuchardtbfc2dd52019-04-06 20:59:24 +0200825
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200826out:
827 return EFI_EXIT(ret);
828}
829
830/**
831 * efi_cin_set_state() - set toggle key state
832 *
833 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
Heinrich Schuchardtacee9652019-05-18 17:07:52 +0200834 * @key_toggle_state: pointer to key toggle state
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200835 * Return: status code
836 *
837 * This function implements the SetState service of the
838 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
839 *
840 * See the Unified Extensible Firmware Interface (UEFI) specification for
841 * details.
842 */
843static efi_status_t EFIAPI efi_cin_set_state(
844 struct efi_simple_text_input_ex_protocol *this,
Heinrich Schuchardtacee9652019-05-18 17:07:52 +0200845 u8 *key_toggle_state)
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200846{
Heinrich Schuchardtacee9652019-05-18 17:07:52 +0200847 EFI_ENTRY("%p, %p", this, key_toggle_state);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200848 /*
849 * U-Boot supports multiple console input sources like serial and
850 * net console for which a key toggle state cannot be set at all.
851 *
852 * According to the UEFI specification it is allowable to not implement
853 * this service.
854 */
855 return EFI_EXIT(EFI_UNSUPPORTED);
856}
857
858/**
859 * efi_cin_register_key_notify() - register key notification function
860 *
861 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
862 * @key_data: key to be notified
863 * @key_notify_function: function to be called if the key is pressed
864 * @notify_handle: handle for unregistering the notification
865 * Return: status code
866 *
867 * This function implements the SetState service of the
868 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
869 *
870 * See the Unified Extensible Firmware Interface (UEFI) specification for
871 * details.
872 */
873static efi_status_t EFIAPI efi_cin_register_key_notify(
874 struct efi_simple_text_input_ex_protocol *this,
875 struct efi_key_data *key_data,
876 efi_status_t (EFIAPI *key_notify_function)(
877 struct efi_key_data *key_data),
878 void **notify_handle)
879{
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200880 efi_status_t ret = EFI_SUCCESS;
881 struct efi_cin_notify_function *notify_function;
882
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200883 EFI_ENTRY("%p, %p, %p, %p",
884 this, key_data, key_notify_function, notify_handle);
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200885
886 /* Check parameters */
887 if (!this || !key_data || !key_notify_function || !notify_handle) {
888 ret = EFI_INVALID_PARAMETER;
889 goto out;
890 }
891
892 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
893 key_data->key.unicode_char,
894 key_data->key.scan_code,
895 key_data->key_state.key_shift_state,
896 key_data->key_state.key_toggle_state);
897
898 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
899 if (!notify_function) {
900 ret = EFI_OUT_OF_RESOURCES;
901 goto out;
902 }
903 notify_function->key = *key_data;
904 notify_function->function = key_notify_function;
905 list_add_tail(&notify_function->link, &cin_notify_functions);
906 *notify_handle = notify_function;
907out:
908 return EFI_EXIT(ret);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200909}
910
911/**
912 * efi_cin_unregister_key_notify() - unregister key notification function
913 *
914 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
915 * @notification_handle: handle received when registering
916 * Return: status code
917 *
918 * This function implements the SetState service of the
919 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
920 *
921 * See the Unified Extensible Firmware Interface (UEFI) specification for
922 * details.
923 */
924static efi_status_t EFIAPI efi_cin_unregister_key_notify(
925 struct efi_simple_text_input_ex_protocol *this,
926 void *notification_handle)
927{
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200928 efi_status_t ret = EFI_INVALID_PARAMETER;
929 struct efi_cin_notify_function *item, *notify_function =
930 notification_handle;
931
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200932 EFI_ENTRY("%p, %p", this, notification_handle);
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200933
934 /* Check parameters */
935 if (!this || !notification_handle)
936 goto out;
937
938 list_for_each_entry(item, &cin_notify_functions, link) {
939 if (item == notify_function) {
940 ret = EFI_SUCCESS;
941 break;
942 }
943 }
944 if (ret != EFI_SUCCESS)
945 goto out;
946
947 /* Remove the notify function */
948 list_del(&notify_function->link);
949 free(notify_function);
950out:
951 return EFI_EXIT(ret);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200952}
953
954
955/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200956 * efi_cin_reset() - drain the input buffer
957 *
958 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
959 * @extended_verification: allow for exhaustive verification
960 * Return: status code
961 *
962 * This function implements the Reset service of the
963 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
964 *
965 * See the Unified Extensible Firmware Interface (UEFI) specification for
966 * details.
967 */
968static efi_status_t EFIAPI efi_cin_reset
969 (struct efi_simple_text_input_protocol *this,
970 bool extended_verification)
971{
972 efi_status_t ret = EFI_SUCCESS;
973
974 EFI_ENTRY("%p, %d", this, extended_verification);
975
976 /* Check parameters */
977 if (!this) {
978 ret = EFI_INVALID_PARAMETER;
979 goto out;
980 }
981
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200982 efi_cin_empty_buffer();
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200983out:
984 return EFI_EXIT(ret);
985}
986
987/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200988 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200989 *
990 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
991 * @key: key read from console
992 * Return: status code
993 *
994 * This function implements the ReadKeyStroke service of the
995 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
996 *
997 * See the Unified Extensible Firmware Interface (UEFI) specification for
998 * details.
999 */
1000static efi_status_t EFIAPI efi_cin_read_key_stroke
1001 (struct efi_simple_text_input_protocol *this,
1002 struct efi_input_key *key)
1003{
1004 efi_status_t ret = EFI_SUCCESS;
1005
1006 EFI_ENTRY("%p, %p", this, key);
1007
1008 /* Check parameters */
1009 if (!this || !key) {
1010 ret = EFI_INVALID_PARAMETER;
1011 goto out;
1012 }
1013
1014 /* We don't do interrupts, so check for timers cooperatively */
1015 efi_timer_check();
1016
1017 /* Enable console input after ExitBootServices */
1018 efi_cin_check();
1019
1020 if (!key_available) {
1021 ret = EFI_NOT_READY;
1022 goto out;
1023 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001024 *key = next_key.key;
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001025 key_available = false;
1026 efi_con_in.wait_for_key->is_signaled = false;
1027out:
1028 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +01001029}
1030
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001031static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1032 .reset = efi_cin_reset_ex,
1033 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1034 .wait_for_key_ex = NULL,
1035 .set_state = efi_cin_set_state,
1036 .register_key_notify = efi_cin_register_key_notify,
1037 .unregister_key_notify = efi_cin_unregister_key_notify,
1038};
1039
Heinrich Schuchardt3e603ec2018-09-08 10:20:10 +02001040struct efi_simple_text_input_protocol efi_con_in = {
Alexander Grafc1311ad2016-03-04 01:10:00 +01001041 .reset = efi_cin_reset,
1042 .read_key_stroke = efi_cin_read_key_stroke,
1043 .wait_for_key = NULL,
1044};
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001045
1046static struct efi_event *console_timer_event;
1047
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001048/*
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001049 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001050 *
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001051 * @event: console timer event
1052 * @context: not used
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001053 */
xypron.glpk@gmx.deff925932017-07-20 05:26:07 +02001054static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1055 void *context)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001056{
1057 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001058 efi_cin_check();
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001059 EFI_EXIT(EFI_SUCCESS);
1060}
1061
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001062/**
1063 * efi_key_notify() - notify the wait for key event
1064 *
1065 * @event: wait for key event
1066 * @context: not used
1067 */
1068static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1069{
1070 EFI_ENTRY("%p, %p", event, context);
1071 efi_cin_check();
1072 EFI_EXIT(EFI_SUCCESS);
1073}
1074
1075/**
1076 * efi_console_register() - install the console protocols
1077 *
1078 * This function is called from do_bootefi_exec().
Heinrich Schuchardt6f566c22018-10-02 06:08:26 +02001079 *
1080 * Return: status code
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001081 */
Heinrich Schuchardt6f566c22018-10-02 06:08:26 +02001082efi_status_t efi_console_register(void)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001083{
1084 efi_status_t r;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001085 efi_handle_t console_output_handle;
1086 efi_handle_t console_input_handle;
Rob Clarka17e62c2017-07-24 10:39:01 -04001087
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +02001088 /* Set up mode information */
1089 query_console_size();
1090
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001091 /* Create handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001092 r = efi_create_handle(&console_output_handle);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001093 if (r != EFI_SUCCESS)
1094 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +02001095
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001096 r = efi_add_protocol(console_output_handle,
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001097 &efi_guid_text_output_protocol, &efi_con_out);
1098 if (r != EFI_SUCCESS)
1099 goto out_of_memory;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001100 systab.con_out_handle = console_output_handle;
1101 systab.stderr_handle = console_output_handle;
Alexander Graf40e3e752018-09-04 14:59:11 +02001102
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001103 r = efi_create_handle(&console_input_handle);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001104 if (r != EFI_SUCCESS)
1105 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +02001106
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001107 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001108 &efi_guid_text_input_protocol, &efi_con_in);
1109 if (r != EFI_SUCCESS)
1110 goto out_of_memory;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001111 systab.con_in_handle = console_input_handle;
1112 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001113 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1114 if (r != EFI_SUCCESS)
1115 goto out_of_memory;
Rob Clarka17e62c2017-07-24 10:39:01 -04001116
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001117 /* Create console events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001118 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1119 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001120 if (r != EFI_SUCCESS) {
1121 printf("ERROR: Failed to register WaitForKey event\n");
1122 return r;
1123 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001124 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001125 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001126 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001127 &console_timer_event);
1128 if (r != EFI_SUCCESS) {
1129 printf("ERROR: Failed to register console event\n");
1130 return r;
1131 }
1132 /* 5000 ns cycle is sufficient for 2 MBaud */
1133 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1134 if (r != EFI_SUCCESS)
1135 printf("ERROR: Failed to set console timer\n");
1136 return r;
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001137out_of_memory:
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +02001138 printf("ERROR: Out of memory\n");
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001139 return r;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001140}