blob: 218f7caa12f85051a099e00842d687d69c8f3011 [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>
Simon Glass495a5dc2019-11-14 12:57:30 -070010#include <time.h>
Rob Clark3863b712017-09-13 18:05:43 -040011#include <dm/device.h>
Alexander Graf4aacacc2016-03-04 01:10:00 +010012#include <efi_loader.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060013#include <env.h>
Rob Clark3863b712017-09-13 18:05:43 -040014#include <stdio_dev.h>
15#include <video_console.h>
Alexander Graf4aacacc2016-03-04 01:10:00 +010016
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010017#define EFI_COUT_MODE_2 2
18#define EFI_MAX_COUT_MODE 3
19
20struct cout_mode {
21 unsigned long columns;
22 unsigned long rows;
23 int present;
24};
25
26static struct cout_mode efi_cout_modes[] = {
27 /* EFI Mode 0 is 80x25 and always present */
28 {
29 .columns = 80,
30 .rows = 25,
31 .present = 1,
32 },
33 /* EFI Mode 1 is always 80x50 */
34 {
35 .columns = 80,
36 .rows = 50,
37 .present = 0,
38 },
39 /* Value are unknown until we query the console */
40 {
41 .columns = 0,
42 .rows = 0,
43 .present = 0,
44 },
45};
46
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +020047const efi_guid_t efi_guid_text_input_ex_protocol =
48 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +020049const efi_guid_t efi_guid_text_input_protocol =
50 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +020051const efi_guid_t efi_guid_text_output_protocol =
52 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
Alexander Graf4aacacc2016-03-04 01:10:00 +010053
54#define cESC '\x1b'
55#define ESC "\x1b"
56
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010057/* Default to mode 0 */
Alexander Graf4aacacc2016-03-04 01:10:00 +010058static struct simple_text_output_mode efi_con_mode = {
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010059 .max_mode = 1,
Alexander Graf4aacacc2016-03-04 01:10:00 +010060 .mode = 0,
61 .attribute = 0,
62 .cursor_column = 0,
63 .cursor_row = 0,
64 .cursor_visible = 1,
65};
66
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010067static int term_get_char(s32 *c)
68{
69 u64 timeout;
70
71 /* Wait up to 100 ms for a character */
72 timeout = timer_get_us() + 100000;
73
74 while (!tstc())
75 if (timer_get_us() > timeout)
76 return 1;
77
78 *c = getc();
79 return 0;
80}
81
Heinrich Schuchardt77135c22018-05-16 18:17:38 +020082/*
83 * Receive and parse a reply from the terminal.
84 *
85 * @n: array of return values
86 * @num: number of return values expected
87 * @end_char: character indicating end of terminal message
88 * @return: non-zero indicates error
89 */
90static int term_read_reply(int *n, int num, char end_char)
Alexander Graf4aacacc2016-03-04 01:10:00 +010091{
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010092 s32 c;
Alexander Graf4aacacc2016-03-04 01:10:00 +010093 int i = 0;
94
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010095 if (term_get_char(&c) || c != cESC)
Alexander Graf4aacacc2016-03-04 01:10:00 +010096 return -1;
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010097
98 if (term_get_char(&c) || c != '[')
Alexander Graf4aacacc2016-03-04 01:10:00 +010099 return -1;
100
101 n[0] = 0;
102 while (1) {
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100103 if (!term_get_char(&c)) {
104 if (c == ';') {
105 i++;
106 if (i >= num)
107 return -1;
108 n[i] = 0;
109 continue;
110 } else if (c == end_char) {
111 break;
112 } else if (c > '9' || c < '0') {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100113 return -1;
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100114 }
115
116 /* Read one more decimal position */
117 n[i] *= 10;
118 n[i] += c - '0';
119 } else {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100120 return -1;
121 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100122 }
Heinrich Schuchardt77135c22018-05-16 18:17:38 +0200123 if (i != num - 1)
124 return -1;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100125
126 return 0;
127}
128
Alexander Graf4aacacc2016-03-04 01:10:00 +0100129static efi_status_t EFIAPI efi_cout_output_string(
130 struct efi_simple_text_output_protocol *this,
Rob Clark1ef185d2017-09-13 18:05:44 -0400131 const efi_string_t string)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100132{
Rob Clark1ef185d2017-09-13 18:05:44 -0400133 struct simple_text_output_mode *con = &efi_con_mode;
134 struct cout_mode *mode = &efi_cout_modes[con->mode];
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200135 char *buf, *pos;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200136 u16 *p;
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200137 efi_status_t ret = EFI_SUCCESS;
Rob Clark1ef185d2017-09-13 18:05:44 -0400138
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200139 EFI_ENTRY("%p, %p", this, string);
Rob Clark1ef185d2017-09-13 18:05:44 -0400140
Heinrich Schuchardte60b2982019-05-18 18:11:54 +0200141 if (!this || !string) {
142 ret = EFI_INVALID_PARAMETER;
143 goto out;
144 }
145
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200146 buf = malloc(utf16_utf8_strlen(string) + 1);
147 if (!buf) {
148 ret = EFI_OUT_OF_RESOURCES;
149 goto out;
150 }
151 pos = buf;
152 utf16_utf8_strcpy(&pos, string);
Rob Clark1ef185d2017-09-13 18:05:44 -0400153 fputs(stdout, buf);
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200154 free(buf);
Rob Clark1ef185d2017-09-13 18:05:44 -0400155
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200156 /*
157 * Update the cursor position.
158 *
159 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200160 * and U000D. All other control characters are ignored. Any non-control
161 * character increase the column by one.
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200162 */
163 for (p = string; *p; ++p) {
Rob Clark1ef185d2017-09-13 18:05:44 -0400164 switch (*p) {
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200165 case '\b': /* U+0008, backspace */
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200166 if (con->cursor_column)
167 con->cursor_column--;
Rob Clark1ef185d2017-09-13 18:05:44 -0400168 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200169 case '\n': /* U+000A, newline */
Rob Clark1ef185d2017-09-13 18:05:44 -0400170 con->cursor_column = 0;
171 con->cursor_row++;
172 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200173 case '\r': /* U+000D, carriage-return */
174 con->cursor_column = 0;
Rob Clark1ef185d2017-09-13 18:05:44 -0400175 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200176 case 0xd800 ... 0xdbff:
177 /*
178 * Ignore high surrogates, we do not want to count a
179 * Unicode character twice.
180 */
Rob Clark1ef185d2017-09-13 18:05:44 -0400181 break;
182 default:
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200183 /* Exclude control codes */
184 if (*p > 0x1f)
185 con->cursor_column++;
Rob Clark1ef185d2017-09-13 18:05:44 -0400186 break;
187 }
188 if (con->cursor_column >= mode->columns) {
189 con->cursor_column = 0;
190 con->cursor_row++;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100191 }
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200192 /*
193 * When we exceed the row count the terminal will scroll up one
194 * line. We have to adjust the cursor position.
195 */
196 if (con->cursor_row >= mode->rows && con->cursor_row)
197 con->cursor_row--;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100198 }
199
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200200out:
201 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100202}
203
204static efi_status_t EFIAPI efi_cout_test_string(
205 struct efi_simple_text_output_protocol *this,
Rob Clark1ef185d2017-09-13 18:05:44 -0400206 const efi_string_t string)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100207{
208 EFI_ENTRY("%p, %p", this, string);
209 return EFI_EXIT(EFI_SUCCESS);
210}
211
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100212static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
213{
214 if (!mode->present)
215 return false;
216
217 return (mode->rows == rows) && (mode->columns == cols);
218}
219
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200220/**
221 * query_console_serial() - query console size
222 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200223 * @rows: pointer to return number of rows
224 * @cols: pointer to return number of columns
225 * Returns: 0 on success
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200226 */
Rob Clark1e948922017-09-13 18:05:42 -0400227static int query_console_serial(int *rows, int *cols)
228{
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200229 int ret = 0;
230 int n[2];
Rob Clark1e948922017-09-13 18:05:42 -0400231
232 /* Empty input buffer */
233 while (tstc())
234 getc();
235
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200236 /*
237 * Not all terminals understand CSI [18t for querying the console size.
238 * We should adhere to escape sequences documented in the console_codes
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200239 * man page and the ECMA-48 standard.
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200240 *
241 * So here we follow a different approach. We position the cursor to the
242 * bottom right and query its position. Before leaving the function we
243 * restore the original cursor position.
244 */
245 printf(ESC "7" /* Save cursor position */
246 ESC "[r" /* Set scrolling region to full window */
247 ESC "[999;999H" /* Move to bottom right corner */
248 ESC "[6n"); /* Query cursor position */
Rob Clark1e948922017-09-13 18:05:42 -0400249
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200250 /* Read {rows,cols} */
251 if (term_read_reply(n, 2, 'R')) {
252 ret = 1;
253 goto out;
254 }
Rob Clark1e948922017-09-13 18:05:42 -0400255
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200256 *cols = n[1];
257 *rows = n[0];
258out:
259 printf(ESC "8"); /* Restore cursor position */
260 return ret;
Rob Clark1e948922017-09-13 18:05:42 -0400261}
262
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200263/*
264 * Update the mode table.
265 *
266 * By default the only mode available is 80x25. If the console has at least 50
267 * lines, enable mode 80x50. If we can query the console size and it is neither
268 * 80x25 nor 80x50, set it as an additional mode.
269 */
270static void query_console_size(void)
271{
272 const char *stdout_name = env_get("stdout");
Alexander Graf28795322018-06-03 15:51:17 +0200273 int rows = 25, cols = 80;
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200274
275 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
276 IS_ENABLED(CONFIG_DM_VIDEO)) {
277 struct stdio_dev *stdout_dev =
278 stdio_get_by_name("vidconsole");
279 struct udevice *dev = stdout_dev->priv;
280 struct vidconsole_priv *priv =
281 dev_get_uclass_priv(dev);
282 rows = priv->rows;
283 cols = priv->cols;
284 } else if (query_console_serial(&rows, &cols)) {
285 return;
286 }
287
288 /* Test if we can have Mode 1 */
289 if (cols >= 80 && rows >= 50) {
290 efi_cout_modes[1].present = 1;
291 efi_con_mode.max_mode = 2;
292 }
293
294 /*
295 * Install our mode as mode 2 if it is different
296 * than mode 0 or 1 and set it as the currently selected mode
297 */
298 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
299 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
300 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
301 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
302 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
303 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
304 efi_con_mode.mode = EFI_COUT_MODE_2;
305 }
306}
307
Alexander Graf4aacacc2016-03-04 01:10:00 +0100308static efi_status_t EFIAPI efi_cout_query_mode(
309 struct efi_simple_text_output_protocol *this,
310 unsigned long mode_number, unsigned long *columns,
311 unsigned long *rows)
312{
313 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
314
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100315 if (mode_number >= efi_con_mode.max_mode)
316 return EFI_EXIT(EFI_UNSUPPORTED);
317
318 if (efi_cout_modes[mode_number].present != 1)
319 return EFI_EXIT(EFI_UNSUPPORTED);
320
Alexander Graf4aacacc2016-03-04 01:10:00 +0100321 if (columns)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100322 *columns = efi_cout_modes[mode_number].columns;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100323 if (rows)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100324 *rows = efi_cout_modes[mode_number].rows;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100325
326 return EFI_EXIT(EFI_SUCCESS);
327}
328
Rob Clark87ef0012017-10-10 08:23:01 -0400329static const struct {
330 unsigned int fg;
331 unsigned int bg;
332} color[] = {
333 { 30, 40 }, /* 0: black */
334 { 34, 44 }, /* 1: blue */
335 { 32, 42 }, /* 2: green */
336 { 36, 46 }, /* 3: cyan */
337 { 31, 41 }, /* 4: red */
338 { 35, 45 }, /* 5: magenta */
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +0200339 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
340 { 37, 47 }, /* 7: light gray, map to white */
Rob Clark87ef0012017-10-10 08:23:01 -0400341};
342
343/* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100344static efi_status_t EFIAPI efi_cout_set_attribute(
345 struct efi_simple_text_output_protocol *this,
346 unsigned long attribute)
347{
Rob Clark87ef0012017-10-10 08:23:01 -0400348 unsigned int bold = EFI_ATTR_BOLD(attribute);
349 unsigned int fg = EFI_ATTR_FG(attribute);
350 unsigned int bg = EFI_ATTR_BG(attribute);
351
Alexander Graf4aacacc2016-03-04 01:10:00 +0100352 EFI_ENTRY("%p, %lx", this, attribute);
353
Heinrich Schuchardt1e129622019-06-14 07:16:57 +0200354 efi_con_mode.attribute = attribute;
Rob Clark87ef0012017-10-10 08:23:01 -0400355 if (attribute)
356 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
357 else
358 printf(ESC"[0;37;40m");
359
360 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100361}
362
363static efi_status_t EFIAPI efi_cout_clear_screen(
364 struct efi_simple_text_output_protocol *this)
365{
366 EFI_ENTRY("%p", this);
367
368 printf(ESC"[2J");
Heinrich Schuchardt2d099322018-07-05 08:18:00 +0200369 efi_con_mode.cursor_column = 0;
370 efi_con_mode.cursor_row = 0;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100371
372 return EFI_EXIT(EFI_SUCCESS);
373}
374
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200375static efi_status_t EFIAPI efi_cout_set_mode(
376 struct efi_simple_text_output_protocol *this,
377 unsigned long mode_number)
378{
379 EFI_ENTRY("%p, %ld", this, mode_number);
380
381 if (mode_number >= efi_con_mode.max_mode)
382 return EFI_EXIT(EFI_UNSUPPORTED);
Heinrich Schuchardt6a6afa72019-09-04 22:46:13 +0200383
384 if (!efi_cout_modes[mode_number].present)
385 return EFI_EXIT(EFI_UNSUPPORTED);
386
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200387 efi_con_mode.mode = mode_number;
388 EFI_CALL(efi_cout_clear_screen(this));
389
390 return EFI_EXIT(EFI_SUCCESS);
391}
392
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200393static efi_status_t EFIAPI efi_cout_reset(
394 struct efi_simple_text_output_protocol *this,
395 char extended_verification)
396{
397 EFI_ENTRY("%p, %d", this, extended_verification);
398
399 /* Clear screen */
400 EFI_CALL(efi_cout_clear_screen(this));
401 /* Set default colors */
Heinrich Schuchardt1e129622019-06-14 07:16:57 +0200402 efi_con_mode.attribute = 0x07;
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200403 printf(ESC "[0;37;40m");
404
405 return EFI_EXIT(EFI_SUCCESS);
406}
407
Alexander Graf4aacacc2016-03-04 01:10:00 +0100408static efi_status_t EFIAPI efi_cout_set_cursor_position(
409 struct efi_simple_text_output_protocol *this,
410 unsigned long column, unsigned long row)
411{
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200412 efi_status_t ret = EFI_SUCCESS;
413 struct simple_text_output_mode *con = &efi_con_mode;
414 struct cout_mode *mode = &efi_cout_modes[con->mode];
415
Alexander Graf4aacacc2016-03-04 01:10:00 +0100416 EFI_ENTRY("%p, %ld, %ld", this, column, row);
417
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200418 /* Check parameters */
419 if (!this) {
420 ret = EFI_INVALID_PARAMETER;
421 goto out;
422 }
423 if (row >= mode->rows || column >= mode->columns) {
424 ret = EFI_UNSUPPORTED;
425 goto out;
426 }
427
428 /*
429 * Set cursor position by sending CSI H.
430 * EFI origin is [0, 0], terminal origin is [1, 1].
431 */
432 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100433 efi_con_mode.cursor_column = column;
434 efi_con_mode.cursor_row = row;
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200435out:
436 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100437}
438
439static efi_status_t EFIAPI efi_cout_enable_cursor(
440 struct efi_simple_text_output_protocol *this,
441 bool enable)
442{
443 EFI_ENTRY("%p, %d", this, enable);
444
445 printf(ESC"[?25%c", enable ? 'h' : 'l');
Heinrich Schuchardt53f26b92019-06-02 22:54:28 +0200446 efi_con_mode.cursor_visible = !!enable;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100447
448 return EFI_EXIT(EFI_SUCCESS);
449}
450
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +0200451struct efi_simple_text_output_protocol efi_con_out = {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100452 .reset = efi_cout_reset,
453 .output_string = efi_cout_output_string,
454 .test_string = efi_cout_test_string,
455 .query_mode = efi_cout_query_mode,
456 .set_mode = efi_cout_set_mode,
457 .set_attribute = efi_cout_set_attribute,
458 .clear_screen = efi_cout_clear_screen,
459 .set_cursor_position = efi_cout_set_cursor_position,
460 .enable_cursor = efi_cout_enable_cursor,
461 .mode = (void*)&efi_con_mode,
462};
463
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200464/**
465 * struct efi_cin_notify_function - registered console input notify function
466 *
467 * @link: link to list
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200468 * @key: key to notify
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200469 * @function: function to call
470 */
471struct efi_cin_notify_function {
472 struct list_head link;
473 struct efi_key_data key;
474 efi_status_t (EFIAPI *function)
475 (struct efi_key_data *key_data);
476};
477
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200478static bool key_available;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200479static struct efi_key_data next_key;
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200480static LIST_HEAD(cin_notify_functions);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100481
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200482/**
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200483 * set_shift_mask() - set shift mask
484 *
485 * @mod: Xterm shift mask
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200486 * @key_state: receives the state of the shift, alt, control, and logo keys
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200487 */
488void set_shift_mask(int mod, struct efi_key_state *key_state)
489{
490 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
491 if (mod) {
492 --mod;
493 if (mod & 1)
494 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
495 if (mod & 2)
496 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
497 if (mod & 4)
498 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
Heinrich Schuchardtfea93d62019-06-16 22:33:20 +0200499 if (!mod || (mod & 8))
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200500 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200501 }
502}
503
504/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200505 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200506 *
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100507 * This gets called when we have already parsed CSI.
508 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200509 * @key_state: receives the state of the shift, alt, control, and logo keys
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100510 * @return: the unmodified code
511 */
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200512static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100513{
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200514 int c, mod = 0, ret = 0;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100515
516 c = getc();
517
518 if (c != ';') {
519 ret = c;
520 if (c == '~')
521 goto out;
522 c = getc();
523 }
524 for (;;) {
525 switch (c) {
526 case '0'...'9':
527 mod *= 10;
528 mod += c - '0';
529 /* fall through */
530 case ';':
531 c = getc();
532 break;
533 default:
534 goto out;
535 }
536 }
537out:
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200538 set_shift_mask(mod, key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100539 if (!ret)
540 ret = c;
541 return ret;
542}
543
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200544/**
545 * efi_cin_read_key() - read a key from the console input
546 *
547 * @key: - key received
548 * Return: - status code
549 */
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200550static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100551{
552 struct efi_input_key pressed_key = {
553 .scan_code = 0,
554 .unicode_char = 0,
555 };
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200556 s32 ch;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100557
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200558 if (console_read_unicode(&ch))
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200559 return EFI_NOT_READY;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200560
561 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
562 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
563
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200564 /* We do not support multi-word codes */
565 if (ch >= 0x10000)
566 ch = '?';
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200567
568 switch (ch) {
569 case 0x1b:
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100570 /*
571 * Xterm Control Sequences
572 * https://www.xfree86.org/4.8.0/ctlseqs.html
573 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100574 ch = getc();
575 switch (ch) {
576 case cESC: /* ESC */
577 pressed_key.scan_code = 23;
578 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200579 case 'O': /* F1 - F4, End */
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100580 ch = getc();
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200581 /* consider modifiers */
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200582 if (ch == 'F') { /* End */
583 pressed_key.scan_code = 6;
584 break;
585 } else if (ch < 'P') {
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200586 set_shift_mask(ch - '0', &key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100587 ch = getc();
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200588 }
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100589 pressed_key.scan_code = ch - 'P' + 11;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100590 break;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100591 case '[':
592 ch = getc();
593 switch (ch) {
594 case 'A'...'D': /* up, down right, left */
595 pressed_key.scan_code = ch - 'A' + 1;
596 break;
597 case 'F': /* End */
598 pressed_key.scan_code = 6;
599 break;
600 case 'H': /* Home */
601 pressed_key.scan_code = 5;
602 break;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100603 case '1':
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200604 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100605 switch (ch) {
606 case '1'...'5': /* F1 - F5 */
607 pressed_key.scan_code = ch - '1' + 11;
608 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200609 case '6'...'9': /* F5 - F8 */
610 pressed_key.scan_code = ch - '6' + 15;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100611 break;
612 case 'A'...'D': /* up, down right, left */
613 pressed_key.scan_code = ch - 'A' + 1;
614 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200615 case 'F': /* End */
616 pressed_key.scan_code = 6;
617 break;
618 case 'H': /* Home */
619 pressed_key.scan_code = 5;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100620 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200621 case '~': /* Home */
622 pressed_key.scan_code = 5;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100623 break;
624 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100625 break;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100626 case '2':
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200627 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100628 switch (ch) {
629 case '0'...'1': /* F9 - F10 */
630 pressed_key.scan_code = ch - '0' + 19;
631 break;
632 case '3'...'4': /* F11 - F12 */
633 pressed_key.scan_code = ch - '3' + 21;
634 break;
635 case '~': /* INS */
636 pressed_key.scan_code = 7;
637 break;
638 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100639 break;
640 case '3': /* DEL */
641 pressed_key.scan_code = 8;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200642 analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100643 break;
644 case '5': /* PG UP */
645 pressed_key.scan_code = 9;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200646 analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100647 break;
648 case '6': /* PG DOWN */
649 pressed_key.scan_code = 10;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200650 analyze_modifiers(&key->key_state);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100651 break;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200652 } /* [ */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100653 break;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200654 default:
655 /* ALT key */
656 set_shift_mask(3, &key->key_state);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100657 }
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200658 break;
659 case 0x7f:
Alexander Graf4aacacc2016-03-04 01:10:00 +0100660 /* Backspace */
661 ch = 0x08;
662 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200663 if (pressed_key.scan_code) {
664 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
665 } else {
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100666 pressed_key.unicode_char = ch;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200667
668 /*
669 * Assume left control key for control characters typically
670 * entered using the control key.
671 */
672 if (ch >= 0x01 && ch <= 0x1f) {
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200673 key->key_state.key_shift_state |=
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200674 EFI_SHIFT_STATE_VALID;
675 switch (ch) {
676 case 0x01 ... 0x07:
677 case 0x0b ... 0x0c:
678 case 0x0e ... 0x1f:
679 key->key_state.key_shift_state |=
680 EFI_LEFT_CONTROL_PRESSED;
681 }
682 }
683 }
684 key->key = pressed_key;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100685
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200686 return EFI_SUCCESS;
687}
688
689/**
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200690 * efi_cin_notify() - notify registered functions
691 */
692static void efi_cin_notify(void)
693{
694 struct efi_cin_notify_function *item;
695
696 list_for_each_entry(item, &cin_notify_functions, link) {
697 bool match = true;
698
699 /* We do not support toggle states */
700 if (item->key.key.unicode_char || item->key.key.scan_code) {
701 if (item->key.key.unicode_char !=
702 next_key.key.unicode_char ||
703 item->key.key.scan_code != next_key.key.scan_code)
704 match = false;
705 }
706 if (item->key.key_state.key_shift_state &&
707 item->key.key_state.key_shift_state !=
708 next_key.key_state.key_shift_state)
709 match = false;
710
711 if (match)
712 /* We don't bother about the return code */
713 EFI_CALL(item->function(&next_key));
714 }
715}
716
717/**
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200718 * efi_cin_check() - check if keyboard input is available
719 */
720static void efi_cin_check(void)
721{
722 efi_status_t ret;
723
724 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 return;
727 }
728
729 if (tstc()) {
730 ret = efi_cin_read_key(&next_key);
731 if (ret == EFI_SUCCESS) {
732 key_available = true;
733
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200734 /* Notify registered functions */
735 efi_cin_notify();
736
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200737 /* Queue the wait for key event */
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200738 if (key_available)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200739 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200740 }
741 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200742}
743
744/**
745 * efi_cin_empty_buffer() - empty input buffer
746 */
747static void efi_cin_empty_buffer(void)
748{
749 while (tstc())
750 getc();
751 key_available = false;
752}
753
754/**
755 * efi_cin_reset_ex() - reset console input
756 *
757 * @this: - the extended simple text input protocol
758 * @extended_verification: - extended verification
759 *
760 * This function implements the reset service of the
761 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
762 *
763 * See the Unified Extensible Firmware Interface (UEFI) specification for
764 * details.
765 *
766 * Return: old value of the task priority level
767 */
768static efi_status_t EFIAPI efi_cin_reset_ex(
769 struct efi_simple_text_input_ex_protocol *this,
770 bool extended_verification)
771{
772 efi_status_t ret = EFI_SUCCESS;
773
774 EFI_ENTRY("%p, %d", this, extended_verification);
775
776 /* Check parameters */
777 if (!this) {
778 ret = EFI_INVALID_PARAMETER;
779 goto out;
780 }
781
782 efi_cin_empty_buffer();
783out:
784 return EFI_EXIT(ret);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200785}
786
787/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200788 * efi_cin_read_key_stroke_ex() - read key stroke
789 *
790 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
791 * @key_data: key read from console
792 * Return: status code
793 *
794 * This function implements the ReadKeyStrokeEx service of the
795 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
796 *
797 * See the Unified Extensible Firmware Interface (UEFI) specification for
798 * details.
799 */
800static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
801 struct efi_simple_text_input_ex_protocol *this,
802 struct efi_key_data *key_data)
803{
804 efi_status_t ret = EFI_SUCCESS;
805
806 EFI_ENTRY("%p, %p", this, key_data);
807
808 /* Check parameters */
809 if (!this || !key_data) {
810 ret = EFI_INVALID_PARAMETER;
811 goto out;
812 }
813
814 /* We don't do interrupts, so check for timers cooperatively */
815 efi_timer_check();
816
817 /* Enable console input after ExitBootServices */
818 efi_cin_check();
819
820 if (!key_available) {
821 ret = EFI_NOT_READY;
822 goto out;
823 }
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +0200824 /*
825 * CTRL+A - CTRL+Z have to be signaled as a - z.
826 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
827 */
828 switch (next_key.key.unicode_char) {
829 case 0x01 ... 0x07:
830 case 0x0b ... 0x0c:
831 case 0x0e ... 0x1a:
832 if (!(next_key.key_state.key_toggle_state &
833 EFI_CAPS_LOCK_ACTIVE) ^
834 !(next_key.key_state.key_shift_state &
835 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
836 next_key.key.unicode_char += 0x40;
837 else
838 next_key.key.unicode_char += 0x60;
839 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200840 *key_data = next_key;
841 key_available = false;
842 efi_con_in.wait_for_key->is_signaled = false;
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +0200843
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200844out:
845 return EFI_EXIT(ret);
846}
847
848/**
849 * efi_cin_set_state() - set toggle key state
850 *
851 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +0200852 * @key_toggle_state: pointer to key toggle state
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200853 * Return: status code
854 *
855 * This function implements the SetState service of the
856 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
857 *
858 * See the Unified Extensible Firmware Interface (UEFI) specification for
859 * details.
860 */
861static efi_status_t EFIAPI efi_cin_set_state(
862 struct efi_simple_text_input_ex_protocol *this,
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +0200863 u8 *key_toggle_state)
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200864{
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +0200865 EFI_ENTRY("%p, %p", this, key_toggle_state);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200866 /*
867 * U-Boot supports multiple console input sources like serial and
868 * net console for which a key toggle state cannot be set at all.
869 *
870 * According to the UEFI specification it is allowable to not implement
871 * this service.
872 */
873 return EFI_EXIT(EFI_UNSUPPORTED);
874}
875
876/**
877 * efi_cin_register_key_notify() - register key notification function
878 *
879 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
880 * @key_data: key to be notified
881 * @key_notify_function: function to be called if the key is pressed
882 * @notify_handle: handle for unregistering the notification
883 * Return: status code
884 *
885 * This function implements the SetState service of the
886 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
887 *
888 * See the Unified Extensible Firmware Interface (UEFI) specification for
889 * details.
890 */
891static efi_status_t EFIAPI efi_cin_register_key_notify(
892 struct efi_simple_text_input_ex_protocol *this,
893 struct efi_key_data *key_data,
894 efi_status_t (EFIAPI *key_notify_function)(
895 struct efi_key_data *key_data),
896 void **notify_handle)
897{
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200898 efi_status_t ret = EFI_SUCCESS;
899 struct efi_cin_notify_function *notify_function;
900
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200901 EFI_ENTRY("%p, %p, %p, %p",
902 this, key_data, key_notify_function, notify_handle);
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200903
904 /* Check parameters */
905 if (!this || !key_data || !key_notify_function || !notify_handle) {
906 ret = EFI_INVALID_PARAMETER;
907 goto out;
908 }
909
910 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
911 key_data->key.unicode_char,
912 key_data->key.scan_code,
913 key_data->key_state.key_shift_state,
914 key_data->key_state.key_toggle_state);
915
916 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
917 if (!notify_function) {
918 ret = EFI_OUT_OF_RESOURCES;
919 goto out;
920 }
921 notify_function->key = *key_data;
922 notify_function->function = key_notify_function;
923 list_add_tail(&notify_function->link, &cin_notify_functions);
924 *notify_handle = notify_function;
925out:
926 return EFI_EXIT(ret);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200927}
928
929/**
930 * efi_cin_unregister_key_notify() - unregister key notification function
931 *
932 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
933 * @notification_handle: handle received when registering
934 * Return: status code
935 *
936 * This function implements the SetState service of the
937 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
938 *
939 * See the Unified Extensible Firmware Interface (UEFI) specification for
940 * details.
941 */
942static efi_status_t EFIAPI efi_cin_unregister_key_notify(
943 struct efi_simple_text_input_ex_protocol *this,
944 void *notification_handle)
945{
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200946 efi_status_t ret = EFI_INVALID_PARAMETER;
947 struct efi_cin_notify_function *item, *notify_function =
948 notification_handle;
949
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200950 EFI_ENTRY("%p, %p", this, notification_handle);
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200951
952 /* Check parameters */
953 if (!this || !notification_handle)
954 goto out;
955
956 list_for_each_entry(item, &cin_notify_functions, link) {
957 if (item == notify_function) {
958 ret = EFI_SUCCESS;
959 break;
960 }
961 }
962 if (ret != EFI_SUCCESS)
963 goto out;
964
965 /* Remove the notify function */
966 list_del(&notify_function->link);
967 free(notify_function);
968out:
969 return EFI_EXIT(ret);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200970}
971
972
973/**
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200974 * efi_cin_reset() - drain the input buffer
975 *
976 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
977 * @extended_verification: allow for exhaustive verification
978 * Return: status code
979 *
980 * This function implements the Reset service of the
981 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
982 *
983 * See the Unified Extensible Firmware Interface (UEFI) specification for
984 * details.
985 */
986static efi_status_t EFIAPI efi_cin_reset
987 (struct efi_simple_text_input_protocol *this,
988 bool extended_verification)
989{
990 efi_status_t ret = EFI_SUCCESS;
991
992 EFI_ENTRY("%p, %d", this, extended_verification);
993
994 /* Check parameters */
995 if (!this) {
996 ret = EFI_INVALID_PARAMETER;
997 goto out;
998 }
999
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001000 efi_cin_empty_buffer();
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001001out:
1002 return EFI_EXIT(ret);
1003}
1004
1005/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001006 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001007 *
1008 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1009 * @key: key read from console
1010 * Return: status code
1011 *
1012 * This function implements the ReadKeyStroke service of the
1013 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1014 *
1015 * See the Unified Extensible Firmware Interface (UEFI) specification for
1016 * details.
1017 */
1018static efi_status_t EFIAPI efi_cin_read_key_stroke
1019 (struct efi_simple_text_input_protocol *this,
1020 struct efi_input_key *key)
1021{
1022 efi_status_t ret = EFI_SUCCESS;
1023
1024 EFI_ENTRY("%p, %p", this, key);
1025
1026 /* Check parameters */
1027 if (!this || !key) {
1028 ret = EFI_INVALID_PARAMETER;
1029 goto out;
1030 }
1031
1032 /* We don't do interrupts, so check for timers cooperatively */
1033 efi_timer_check();
1034
1035 /* Enable console input after ExitBootServices */
1036 efi_cin_check();
1037
1038 if (!key_available) {
1039 ret = EFI_NOT_READY;
1040 goto out;
1041 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001042 *key = next_key.key;
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001043 key_available = false;
1044 efi_con_in.wait_for_key->is_signaled = false;
1045out:
1046 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +01001047}
1048
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001049static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1050 .reset = efi_cin_reset_ex,
1051 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1052 .wait_for_key_ex = NULL,
1053 .set_state = efi_cin_set_state,
1054 .register_key_notify = efi_cin_register_key_notify,
1055 .unregister_key_notify = efi_cin_unregister_key_notify,
1056};
1057
Heinrich Schuchardt3dabffd2018-09-08 10:20:10 +02001058struct efi_simple_text_input_protocol efi_con_in = {
Alexander Graf4aacacc2016-03-04 01:10:00 +01001059 .reset = efi_cin_reset,
1060 .read_key_stroke = efi_cin_read_key_stroke,
1061 .wait_for_key = NULL,
1062};
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001063
1064static struct efi_event *console_timer_event;
1065
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001066/*
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001067 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001068 *
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001069 * @event: console timer event
1070 * @context: not used
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001071 */
xypron.glpk@gmx.de0ce42dc2017-07-20 05:26:07 +02001072static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1073 void *context)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001074{
1075 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001076 efi_cin_check();
1077 EFI_EXIT(EFI_SUCCESS);
1078}
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001079
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001080/**
1081 * efi_key_notify() - notify the wait for key event
1082 *
1083 * @event: wait for key event
1084 * @context: not used
1085 */
1086static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1087{
1088 EFI_ENTRY("%p, %p", event, context);
1089 efi_cin_check();
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001090 EFI_EXIT(EFI_SUCCESS);
1091}
1092
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001093/**
1094 * efi_console_register() - install the console protocols
1095 *
1096 * This function is called from do_bootefi_exec().
Heinrich Schuchardta92e9ab2018-10-02 06:08:26 +02001097 *
1098 * Return: status code
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001099 */
Heinrich Schuchardta92e9ab2018-10-02 06:08:26 +02001100efi_status_t efi_console_register(void)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001101{
1102 efi_status_t r;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001103 efi_handle_t console_output_handle;
1104 efi_handle_t console_input_handle;
Rob Clark49f7b4b2017-07-24 10:39:01 -04001105
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +02001106 /* Set up mode information */
1107 query_console_size();
1108
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001109 /* Create handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001110 r = efi_create_handle(&console_output_handle);
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001111 if (r != EFI_SUCCESS)
1112 goto out_of_memory;
Alexander Graf36bab912018-09-04 14:59:11 +02001113
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001114 r = efi_add_protocol(console_output_handle,
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001115 &efi_guid_text_output_protocol, &efi_con_out);
1116 if (r != EFI_SUCCESS)
1117 goto out_of_memory;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001118 systab.con_out_handle = console_output_handle;
1119 systab.stderr_handle = console_output_handle;
Alexander Graf36bab912018-09-04 14:59:11 +02001120
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001121 r = efi_create_handle(&console_input_handle);
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001122 if (r != EFI_SUCCESS)
1123 goto out_of_memory;
Alexander Graf36bab912018-09-04 14:59:11 +02001124
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001125 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001126 &efi_guid_text_input_protocol, &efi_con_in);
1127 if (r != EFI_SUCCESS)
1128 goto out_of_memory;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001129 systab.con_in_handle = console_input_handle;
1130 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001131 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1132 if (r != EFI_SUCCESS)
1133 goto out_of_memory;
Rob Clark49f7b4b2017-07-24 10:39:01 -04001134
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001135 /* Create console events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001136 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1137 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001138 if (r != EFI_SUCCESS) {
1139 printf("ERROR: Failed to register WaitForKey event\n");
1140 return r;
1141 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001142 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001143 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001144 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001145 &console_timer_event);
1146 if (r != EFI_SUCCESS) {
1147 printf("ERROR: Failed to register console event\n");
1148 return r;
1149 }
1150 /* 5000 ns cycle is sufficient for 2 MBaud */
1151 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1152 if (r != EFI_SUCCESS)
1153 printf("ERROR: Failed to set console timer\n");
1154 return r;
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001155out_of_memory:
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +02001156 printf("ERROR: Out of memory\n");
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001157 return r;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001158}