blob: a04cbf678a70e07a5ba26877892bc3661f01010f [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
Heinrich Schuchardt3950f0f2019-06-14 07:16:57 +0200344 efi_con_mode.attribute = attribute;
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400345 if (attribute)
346 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
347 else
348 printf(ESC"[0;37;40m");
349
350 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100351}
352
353static efi_status_t EFIAPI efi_cout_clear_screen(
354 struct efi_simple_text_output_protocol *this)
355{
356 EFI_ENTRY("%p", this);
357
358 printf(ESC"[2J");
Heinrich Schuchardte67ff942018-07-05 08:18:00 +0200359 efi_con_mode.cursor_column = 0;
360 efi_con_mode.cursor_row = 0;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100361
362 return EFI_EXIT(EFI_SUCCESS);
363}
364
Heinrich Schuchardt2ad238f2019-06-14 07:20:51 +0200365static efi_status_t EFIAPI efi_cout_set_mode(
366 struct efi_simple_text_output_protocol *this,
367 unsigned long mode_number)
368{
369 EFI_ENTRY("%p, %ld", this, mode_number);
370
371 if (mode_number >= efi_con_mode.max_mode)
372 return EFI_EXIT(EFI_UNSUPPORTED);
373 efi_con_mode.mode = mode_number;
374 EFI_CALL(efi_cout_clear_screen(this));
375
376 return EFI_EXIT(EFI_SUCCESS);
377}
378
Heinrich Schuchardt9d12daf2018-07-05 19:58:07 +0200379static efi_status_t EFIAPI efi_cout_reset(
380 struct efi_simple_text_output_protocol *this,
381 char extended_verification)
382{
383 EFI_ENTRY("%p, %d", this, extended_verification);
384
385 /* Clear screen */
386 EFI_CALL(efi_cout_clear_screen(this));
387 /* Set default colors */
Heinrich Schuchardt3950f0f2019-06-14 07:16:57 +0200388 efi_con_mode.attribute = 0x07;
Heinrich Schuchardt9d12daf2018-07-05 19:58:07 +0200389 printf(ESC "[0;37;40m");
390
391 return EFI_EXIT(EFI_SUCCESS);
392}
393
Alexander Grafc1311ad2016-03-04 01:10:00 +0100394static efi_status_t EFIAPI efi_cout_set_cursor_position(
395 struct efi_simple_text_output_protocol *this,
396 unsigned long column, unsigned long row)
397{
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200398 efi_status_t ret = EFI_SUCCESS;
399 struct simple_text_output_mode *con = &efi_con_mode;
400 struct cout_mode *mode = &efi_cout_modes[con->mode];
401
Alexander Grafc1311ad2016-03-04 01:10:00 +0100402 EFI_ENTRY("%p, %ld, %ld", this, column, row);
403
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200404 /* Check parameters */
405 if (!this) {
406 ret = EFI_INVALID_PARAMETER;
407 goto out;
408 }
409 if (row >= mode->rows || column >= mode->columns) {
410 ret = EFI_UNSUPPORTED;
411 goto out;
412 }
413
414 /*
415 * Set cursor position by sending CSI H.
416 * EFI origin is [0, 0], terminal origin is [1, 1].
417 */
418 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100419 efi_con_mode.cursor_column = column;
420 efi_con_mode.cursor_row = row;
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200421out:
422 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100423}
424
425static efi_status_t EFIAPI efi_cout_enable_cursor(
426 struct efi_simple_text_output_protocol *this,
427 bool enable)
428{
429 EFI_ENTRY("%p, %d", this, enable);
430
431 printf(ESC"[?25%c", enable ? 'h' : 'l');
Heinrich Schuchardt25e6fb52019-06-02 22:54:28 +0200432 efi_con_mode.cursor_visible = !!enable;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100433
434 return EFI_EXIT(EFI_SUCCESS);
435}
436
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200437struct efi_simple_text_output_protocol efi_con_out = {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100438 .reset = efi_cout_reset,
439 .output_string = efi_cout_output_string,
440 .test_string = efi_cout_test_string,
441 .query_mode = efi_cout_query_mode,
442 .set_mode = efi_cout_set_mode,
443 .set_attribute = efi_cout_set_attribute,
444 .clear_screen = efi_cout_clear_screen,
445 .set_cursor_position = efi_cout_set_cursor_position,
446 .enable_cursor = efi_cout_enable_cursor,
447 .mode = (void*)&efi_con_mode,
448};
449
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200450/**
451 * struct efi_cin_notify_function - registered console input notify function
452 *
453 * @link: link to list
454 * @data: key to notify
455 * @function: function to call
456 */
457struct efi_cin_notify_function {
458 struct list_head link;
459 struct efi_key_data key;
460 efi_status_t (EFIAPI *function)
461 (struct efi_key_data *key_data);
462};
463
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200464static bool key_available;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200465static struct efi_key_data next_key;
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200466static LIST_HEAD(cin_notify_functions);
Heinrich Schuchardt4f187892018-07-05 08:17:59 +0200467
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200468/**
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200469 * set_shift_mask() - set shift mask
470 *
471 * @mod: Xterm shift mask
472 */
473void set_shift_mask(int mod, struct efi_key_state *key_state)
474{
475 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
476 if (mod) {
477 --mod;
478 if (mod & 1)
479 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
480 if (mod & 2)
481 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
482 if (mod & 4)
483 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
484 if (mod & 8)
485 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
486 } else {
487 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
488 }
489}
490
491/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200492 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200493 *
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100494 * This gets called when we have already parsed CSI.
495 *
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200496 * @modifiers: bit mask (shift, alt, ctrl)
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100497 * @return: the unmodified code
498 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200499static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100500{
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200501 int c, mod = 0, ret = 0;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100502
503 c = getc();
504
505 if (c != ';') {
506 ret = c;
507 if (c == '~')
508 goto out;
509 c = getc();
510 }
511 for (;;) {
512 switch (c) {
513 case '0'...'9':
514 mod *= 10;
515 mod += c - '0';
516 /* fall through */
517 case ';':
518 c = getc();
519 break;
520 default:
521 goto out;
522 }
523 }
524out:
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200525 set_shift_mask(mod, key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100526 if (!ret)
527 ret = c;
528 return ret;
529}
530
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200531/**
532 * efi_cin_read_key() - read a key from the console input
533 *
534 * @key: - key received
535 * Return: - status code
536 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200537static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100538{
539 struct efi_input_key pressed_key = {
540 .scan_code = 0,
541 .unicode_char = 0,
542 };
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200543 s32 ch;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100544
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200545 if (console_read_unicode(&ch))
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200546 return EFI_NOT_READY;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200547
548 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
549 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
550
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200551 /* We do not support multi-word codes */
552 if (ch >= 0x10000)
553 ch = '?';
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200554
555 switch (ch) {
556 case 0x1b:
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100557 /*
558 * Xterm Control Sequences
559 * https://www.xfree86.org/4.8.0/ctlseqs.html
560 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100561 ch = getc();
562 switch (ch) {
563 case cESC: /* ESC */
564 pressed_key.scan_code = 23;
565 break;
Heinrich Schuchardt3b2b2de2019-06-16 21:41:13 +0200566 case 'O': /* F1 - F4, End */
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100567 ch = getc();
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200568 /* consider modifiers */
Heinrich Schuchardt3b2b2de2019-06-16 21:41:13 +0200569 if (ch == 'F') { /* End */
570 pressed_key.scan_code = 6;
571 break;
572 } else if (ch < 'P') {
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200573 set_shift_mask(ch - '0', &key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100574 ch = getc();
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200575 }
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100576 pressed_key.scan_code = ch - 'P' + 11;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100577 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100578 case '[':
579 ch = getc();
580 switch (ch) {
581 case 'A'...'D': /* up, down right, left */
582 pressed_key.scan_code = ch - 'A' + 1;
583 break;
584 case 'F': /* End */
585 pressed_key.scan_code = 6;
586 break;
587 case 'H': /* Home */
588 pressed_key.scan_code = 5;
589 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100590 case '1':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200591 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100592 switch (ch) {
593 case '1'...'5': /* F1 - F5 */
594 pressed_key.scan_code = ch - '1' + 11;
595 break;
Heinrich Schuchardt3b2b2de2019-06-16 21:41:13 +0200596 case '6'...'9': /* F5 - F8 */
597 pressed_key.scan_code = ch - '6' + 15;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100598 break;
599 case 'A'...'D': /* up, down right, left */
600 pressed_key.scan_code = ch - 'A' + 1;
601 break;
Heinrich Schuchardt3b2b2de2019-06-16 21:41:13 +0200602 case 'F': /* End */
603 pressed_key.scan_code = 6;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100604 break;
Heinrich Schuchardt3b2b2de2019-06-16 21:41:13 +0200605 case 'H': /* Home */
606 pressed_key.scan_code = 5;
607 break;
608 case '~': /* Home */
609 pressed_key.scan_code = 5;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100610 break;
611 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100612 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100613 case '2':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200614 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100615 switch (ch) {
616 case '0'...'1': /* F9 - F10 */
617 pressed_key.scan_code = ch - '0' + 19;
618 break;
619 case '3'...'4': /* F11 - F12 */
620 pressed_key.scan_code = ch - '3' + 21;
621 break;
622 case '~': /* INS */
623 pressed_key.scan_code = 7;
624 break;
625 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100626 break;
627 case '3': /* DEL */
628 pressed_key.scan_code = 8;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200629 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100630 break;
631 case '5': /* PG UP */
632 pressed_key.scan_code = 9;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200633 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100634 break;
635 case '6': /* PG DOWN */
636 pressed_key.scan_code = 10;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200637 analyze_modifiers(&key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100638 break;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200639 } /* [ */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100640 break;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200641 default:
642 /* ALT key */
643 set_shift_mask(3, &key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100644 }
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200645 break;
646 case 0x7f:
Alexander Grafc1311ad2016-03-04 01:10:00 +0100647 /* Backspace */
648 ch = 0x08;
649 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200650 if (pressed_key.scan_code) {
651 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
652 } else {
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100653 pressed_key.unicode_char = ch;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200654
655 /*
656 * Assume left control key for control characters typically
657 * entered using the control key.
658 */
659 if (ch >= 0x01 && ch <= 0x1f) {
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200660 key->key_state.key_shift_state |=
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200661 EFI_SHIFT_STATE_VALID;
662 switch (ch) {
663 case 0x01 ... 0x07:
664 case 0x0b ... 0x0c:
665 case 0x0e ... 0x1f:
666 key->key_state.key_shift_state |=
667 EFI_LEFT_CONTROL_PRESSED;
668 }
669 }
670 }
671 key->key = pressed_key;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100672
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200673 return EFI_SUCCESS;
674}
675
676/**
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200677 * efi_cin_notify() - notify registered functions
678 */
679static void efi_cin_notify(void)
680{
681 struct efi_cin_notify_function *item;
682
683 list_for_each_entry(item, &cin_notify_functions, link) {
684 bool match = true;
685
686 /* We do not support toggle states */
687 if (item->key.key.unicode_char || item->key.key.scan_code) {
688 if (item->key.key.unicode_char !=
689 next_key.key.unicode_char ||
690 item->key.key.scan_code != next_key.key.scan_code)
691 match = false;
692 }
693 if (item->key.key_state.key_shift_state &&
694 item->key.key_state.key_shift_state !=
695 next_key.key_state.key_shift_state)
696 match = false;
697
698 if (match)
699 /* We don't bother about the return code */
700 EFI_CALL(item->function(&next_key));
701 }
702}
703
704/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200705 * efi_cin_check() - check if keyboard input is available
706 */
707static void efi_cin_check(void)
708{
709 efi_status_t ret;
710
711 if (key_available) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200712 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200713 return;
714 }
715
716 if (tstc()) {
717 ret = efi_cin_read_key(&next_key);
718 if (ret == EFI_SUCCESS) {
719 key_available = true;
720
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200721 /* Notify registered functions */
722 efi_cin_notify();
723
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200724 /* Queue the wait for key event */
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200725 if (key_available)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200726 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200727 }
728 }
729}
730
731/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200732 * efi_cin_empty_buffer() - empty input buffer
733 */
734static void efi_cin_empty_buffer(void)
735{
736 while (tstc())
737 getc();
738 key_available = false;
739}
740
741/**
742 * efi_cin_reset_ex() - reset console input
743 *
744 * @this: - the extended simple text input protocol
745 * @extended_verification: - extended verification
746 *
747 * This function implements the reset service of the
748 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
749 *
750 * See the Unified Extensible Firmware Interface (UEFI) specification for
751 * details.
752 *
753 * Return: old value of the task priority level
754 */
755static efi_status_t EFIAPI efi_cin_reset_ex(
756 struct efi_simple_text_input_ex_protocol *this,
757 bool extended_verification)
758{
759 efi_status_t ret = EFI_SUCCESS;
760
761 EFI_ENTRY("%p, %d", this, extended_verification);
762
763 /* Check parameters */
764 if (!this) {
765 ret = EFI_INVALID_PARAMETER;
766 goto out;
767 }
768
769 efi_cin_empty_buffer();
770out:
771 return EFI_EXIT(ret);
772}
773
774/**
775 * efi_cin_read_key_stroke_ex() - read key stroke
776 *
777 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
778 * @key_data: key read from console
779 * Return: status code
780 *
781 * This function implements the ReadKeyStrokeEx service of the
782 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
783 *
784 * See the Unified Extensible Firmware Interface (UEFI) specification for
785 * details.
786 */
787static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
788 struct efi_simple_text_input_ex_protocol *this,
789 struct efi_key_data *key_data)
790{
791 efi_status_t ret = EFI_SUCCESS;
792
793 EFI_ENTRY("%p, %p", this, key_data);
794
795 /* Check parameters */
796 if (!this || !key_data) {
797 ret = EFI_INVALID_PARAMETER;
798 goto out;
799 }
800
801 /* We don't do interrupts, so check for timers cooperatively */
802 efi_timer_check();
803
804 /* Enable console input after ExitBootServices */
805 efi_cin_check();
806
807 if (!key_available) {
808 ret = EFI_NOT_READY;
809 goto out;
810 }
Heinrich Schuchardtbfc2dd52019-04-06 20:59:24 +0200811 /*
812 * CTRL+A - CTRL+Z have to be signaled as a - z.
813 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
814 */
815 switch (next_key.key.unicode_char) {
816 case 0x01 ... 0x07:
817 case 0x0b ... 0x0c:
818 case 0x0e ... 0x1a:
819 if (!(next_key.key_state.key_toggle_state &
820 EFI_CAPS_LOCK_ACTIVE) ^
821 !(next_key.key_state.key_shift_state &
822 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
823 next_key.key.unicode_char += 0x40;
824 else
825 next_key.key.unicode_char += 0x60;
826 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200827 *key_data = next_key;
828 key_available = false;
829 efi_con_in.wait_for_key->is_signaled = false;
Heinrich Schuchardtbfc2dd52019-04-06 20:59:24 +0200830
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200831out:
832 return EFI_EXIT(ret);
833}
834
835/**
836 * efi_cin_set_state() - set toggle key state
837 *
838 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
Heinrich Schuchardtacee9652019-05-18 17:07:52 +0200839 * @key_toggle_state: pointer to key toggle state
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200840 * Return: status code
841 *
842 * This function implements the SetState service of the
843 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
844 *
845 * See the Unified Extensible Firmware Interface (UEFI) specification for
846 * details.
847 */
848static efi_status_t EFIAPI efi_cin_set_state(
849 struct efi_simple_text_input_ex_protocol *this,
Heinrich Schuchardtacee9652019-05-18 17:07:52 +0200850 u8 *key_toggle_state)
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200851{
Heinrich Schuchardtacee9652019-05-18 17:07:52 +0200852 EFI_ENTRY("%p, %p", this, key_toggle_state);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200853 /*
854 * U-Boot supports multiple console input sources like serial and
855 * net console for which a key toggle state cannot be set at all.
856 *
857 * According to the UEFI specification it is allowable to not implement
858 * this service.
859 */
860 return EFI_EXIT(EFI_UNSUPPORTED);
861}
862
863/**
864 * efi_cin_register_key_notify() - register key notification function
865 *
866 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
867 * @key_data: key to be notified
868 * @key_notify_function: function to be called if the key is pressed
869 * @notify_handle: handle for unregistering the notification
870 * Return: status code
871 *
872 * This function implements the SetState service of the
873 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
874 *
875 * See the Unified Extensible Firmware Interface (UEFI) specification for
876 * details.
877 */
878static efi_status_t EFIAPI efi_cin_register_key_notify(
879 struct efi_simple_text_input_ex_protocol *this,
880 struct efi_key_data *key_data,
881 efi_status_t (EFIAPI *key_notify_function)(
882 struct efi_key_data *key_data),
883 void **notify_handle)
884{
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200885 efi_status_t ret = EFI_SUCCESS;
886 struct efi_cin_notify_function *notify_function;
887
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200888 EFI_ENTRY("%p, %p, %p, %p",
889 this, key_data, key_notify_function, notify_handle);
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200890
891 /* Check parameters */
892 if (!this || !key_data || !key_notify_function || !notify_handle) {
893 ret = EFI_INVALID_PARAMETER;
894 goto out;
895 }
896
897 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
898 key_data->key.unicode_char,
899 key_data->key.scan_code,
900 key_data->key_state.key_shift_state,
901 key_data->key_state.key_toggle_state);
902
903 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
904 if (!notify_function) {
905 ret = EFI_OUT_OF_RESOURCES;
906 goto out;
907 }
908 notify_function->key = *key_data;
909 notify_function->function = key_notify_function;
910 list_add_tail(&notify_function->link, &cin_notify_functions);
911 *notify_handle = notify_function;
912out:
913 return EFI_EXIT(ret);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200914}
915
916/**
917 * efi_cin_unregister_key_notify() - unregister key notification function
918 *
919 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
920 * @notification_handle: handle received when registering
921 * Return: status code
922 *
923 * This function implements the SetState service of the
924 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
925 *
926 * See the Unified Extensible Firmware Interface (UEFI) specification for
927 * details.
928 */
929static efi_status_t EFIAPI efi_cin_unregister_key_notify(
930 struct efi_simple_text_input_ex_protocol *this,
931 void *notification_handle)
932{
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200933 efi_status_t ret = EFI_INVALID_PARAMETER;
934 struct efi_cin_notify_function *item, *notify_function =
935 notification_handle;
936
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200937 EFI_ENTRY("%p, %p", this, notification_handle);
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200938
939 /* Check parameters */
940 if (!this || !notification_handle)
941 goto out;
942
943 list_for_each_entry(item, &cin_notify_functions, link) {
944 if (item == notify_function) {
945 ret = EFI_SUCCESS;
946 break;
947 }
948 }
949 if (ret != EFI_SUCCESS)
950 goto out;
951
952 /* Remove the notify function */
953 list_del(&notify_function->link);
954 free(notify_function);
955out:
956 return EFI_EXIT(ret);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200957}
958
959
960/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200961 * efi_cin_reset() - drain the input buffer
962 *
963 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
964 * @extended_verification: allow for exhaustive verification
965 * Return: status code
966 *
967 * This function implements the Reset service of the
968 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
969 *
970 * See the Unified Extensible Firmware Interface (UEFI) specification for
971 * details.
972 */
973static efi_status_t EFIAPI efi_cin_reset
974 (struct efi_simple_text_input_protocol *this,
975 bool extended_verification)
976{
977 efi_status_t ret = EFI_SUCCESS;
978
979 EFI_ENTRY("%p, %d", this, extended_verification);
980
981 /* Check parameters */
982 if (!this) {
983 ret = EFI_INVALID_PARAMETER;
984 goto out;
985 }
986
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200987 efi_cin_empty_buffer();
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200988out:
989 return EFI_EXIT(ret);
990}
991
992/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200993 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200994 *
995 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
996 * @key: key read from console
997 * Return: status code
998 *
999 * This function implements the ReadKeyStroke service of the
1000 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1001 *
1002 * See the Unified Extensible Firmware Interface (UEFI) specification for
1003 * details.
1004 */
1005static efi_status_t EFIAPI efi_cin_read_key_stroke
1006 (struct efi_simple_text_input_protocol *this,
1007 struct efi_input_key *key)
1008{
1009 efi_status_t ret = EFI_SUCCESS;
1010
1011 EFI_ENTRY("%p, %p", this, key);
1012
1013 /* Check parameters */
1014 if (!this || !key) {
1015 ret = EFI_INVALID_PARAMETER;
1016 goto out;
1017 }
1018
1019 /* We don't do interrupts, so check for timers cooperatively */
1020 efi_timer_check();
1021
1022 /* Enable console input after ExitBootServices */
1023 efi_cin_check();
1024
1025 if (!key_available) {
1026 ret = EFI_NOT_READY;
1027 goto out;
1028 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001029 *key = next_key.key;
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001030 key_available = false;
1031 efi_con_in.wait_for_key->is_signaled = false;
1032out:
1033 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +01001034}
1035
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001036static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1037 .reset = efi_cin_reset_ex,
1038 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1039 .wait_for_key_ex = NULL,
1040 .set_state = efi_cin_set_state,
1041 .register_key_notify = efi_cin_register_key_notify,
1042 .unregister_key_notify = efi_cin_unregister_key_notify,
1043};
1044
Heinrich Schuchardt3e603ec2018-09-08 10:20:10 +02001045struct efi_simple_text_input_protocol efi_con_in = {
Alexander Grafc1311ad2016-03-04 01:10:00 +01001046 .reset = efi_cin_reset,
1047 .read_key_stroke = efi_cin_read_key_stroke,
1048 .wait_for_key = NULL,
1049};
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001050
1051static struct efi_event *console_timer_event;
1052
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001053/*
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001054 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001055 *
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001056 * @event: console timer event
1057 * @context: not used
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001058 */
xypron.glpk@gmx.deff925932017-07-20 05:26:07 +02001059static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1060 void *context)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001061{
1062 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001063 efi_cin_check();
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001064 EFI_EXIT(EFI_SUCCESS);
1065}
1066
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001067/**
1068 * efi_key_notify() - notify the wait for key event
1069 *
1070 * @event: wait for key event
1071 * @context: not used
1072 */
1073static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1074{
1075 EFI_ENTRY("%p, %p", event, context);
1076 efi_cin_check();
1077 EFI_EXIT(EFI_SUCCESS);
1078}
1079
1080/**
1081 * efi_console_register() - install the console protocols
1082 *
1083 * This function is called from do_bootefi_exec().
Heinrich Schuchardt6f566c22018-10-02 06:08:26 +02001084 *
1085 * Return: status code
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001086 */
Heinrich Schuchardt6f566c22018-10-02 06:08:26 +02001087efi_status_t efi_console_register(void)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001088{
1089 efi_status_t r;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001090 efi_handle_t console_output_handle;
1091 efi_handle_t console_input_handle;
Rob Clarka17e62c2017-07-24 10:39:01 -04001092
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +02001093 /* Set up mode information */
1094 query_console_size();
1095
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001096 /* Create handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001097 r = efi_create_handle(&console_output_handle);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001098 if (r != EFI_SUCCESS)
1099 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +02001100
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001101 r = efi_add_protocol(console_output_handle,
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001102 &efi_guid_text_output_protocol, &efi_con_out);
1103 if (r != EFI_SUCCESS)
1104 goto out_of_memory;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001105 systab.con_out_handle = console_output_handle;
1106 systab.stderr_handle = console_output_handle;
Alexander Graf40e3e752018-09-04 14:59:11 +02001107
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001108 r = efi_create_handle(&console_input_handle);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001109 if (r != EFI_SUCCESS)
1110 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +02001111
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001112 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001113 &efi_guid_text_input_protocol, &efi_con_in);
1114 if (r != EFI_SUCCESS)
1115 goto out_of_memory;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001116 systab.con_in_handle = console_input_handle;
1117 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001118 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1119 if (r != EFI_SUCCESS)
1120 goto out_of_memory;
Rob Clarka17e62c2017-07-24 10:39:01 -04001121
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001122 /* Create console events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001123 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1124 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001125 if (r != EFI_SUCCESS) {
1126 printf("ERROR: Failed to register WaitForKey event\n");
1127 return r;
1128 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001129 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001130 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001131 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001132 &console_timer_event);
1133 if (r != EFI_SUCCESS) {
1134 printf("ERROR: Failed to register console event\n");
1135 return r;
1136 }
1137 /* 5000 ns cycle is sufficient for 2 MBaud */
1138 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1139 if (r != EFI_SUCCESS)
1140 printf("ERROR: Failed to set console timer\n");
1141 return r;
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001142out_of_memory:
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +02001143 printf("ERROR: Out of memory\n");
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001144 return r;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001145}