blob: 051fc1d339f7b929f3a34111c0a9ddd47c34c08e [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 Schuchardtba7bd5c2018-08-31 21:31:32 +0200139 buf = malloc(utf16_utf8_strlen(string) + 1);
140 if (!buf) {
141 ret = EFI_OUT_OF_RESOURCES;
142 goto out;
143 }
144 pos = buf;
145 utf16_utf8_strcpy(&pos, string);
Rob Clark3a45bc72017-09-13 18:05:44 -0400146 fputs(stdout, buf);
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200147 free(buf);
Rob Clark3a45bc72017-09-13 18:05:44 -0400148
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200149 /*
150 * Update the cursor position.
151 *
152 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
153 * and U000D. All other characters, including control characters
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +0200154 * U+0007 (BEL) and U+0009 (TAB), have to increase the column by one.
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200155 */
156 for (p = string; *p; ++p) {
Rob Clark3a45bc72017-09-13 18:05:44 -0400157 switch (*p) {
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200158 case '\b': /* U+0008, backspace */
159 con->cursor_column = max(0, con->cursor_column - 1);
Rob Clark3a45bc72017-09-13 18:05:44 -0400160 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200161 case '\n': /* U+000A, newline */
Rob Clark3a45bc72017-09-13 18:05:44 -0400162 con->cursor_column = 0;
163 con->cursor_row++;
164 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200165 case '\r': /* U+000D, carriage-return */
166 con->cursor_column = 0;
Rob Clark3a45bc72017-09-13 18:05:44 -0400167 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200168 case 0xd800 ... 0xdbff:
169 /*
170 * Ignore high surrogates, we do not want to count a
171 * Unicode character twice.
172 */
Rob Clark3a45bc72017-09-13 18:05:44 -0400173 break;
174 default:
175 con->cursor_column++;
176 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100177 }
Rob Clark3a45bc72017-09-13 18:05:44 -0400178 if (con->cursor_column >= mode->columns) {
179 con->cursor_column = 0;
180 con->cursor_row++;
181 }
182 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100183 }
184
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200185out:
186 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100187}
188
189static efi_status_t EFIAPI efi_cout_test_string(
190 struct efi_simple_text_output_protocol *this,
Rob Clark3a45bc72017-09-13 18:05:44 -0400191 const efi_string_t string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100192{
193 EFI_ENTRY("%p, %p", this, string);
194 return EFI_EXIT(EFI_SUCCESS);
195}
196
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100197static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
198{
199 if (!mode->present)
200 return false;
201
202 return (mode->rows == rows) && (mode->columns == cols);
203}
204
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200205/**
206 * query_console_serial() - query console size
207 *
208 * @rows pointer to return number of rows
209 * @columns pointer to return number of columns
210 * Returns 0 on success
211 */
Rob Clark71cc25c2017-09-13 18:05:42 -0400212static int query_console_serial(int *rows, int *cols)
213{
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200214 int ret = 0;
215 int n[2];
Rob Clark71cc25c2017-09-13 18:05:42 -0400216
217 /* Empty input buffer */
218 while (tstc())
219 getc();
220
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200221 /*
222 * Not all terminals understand CSI [18t for querying the console size.
223 * We should adhere to escape sequences documented in the console_codes
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200224 * man page and the ECMA-48 standard.
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200225 *
226 * So here we follow a different approach. We position the cursor to the
227 * bottom right and query its position. Before leaving the function we
228 * restore the original cursor position.
229 */
230 printf(ESC "7" /* Save cursor position */
231 ESC "[r" /* Set scrolling region to full window */
232 ESC "[999;999H" /* Move to bottom right corner */
233 ESC "[6n"); /* Query cursor position */
Rob Clark71cc25c2017-09-13 18:05:42 -0400234
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200235 /* Read {rows,cols} */
236 if (term_read_reply(n, 2, 'R')) {
237 ret = 1;
238 goto out;
239 }
Rob Clark71cc25c2017-09-13 18:05:42 -0400240
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200241 *cols = n[1];
242 *rows = n[0];
243out:
244 printf(ESC "8"); /* Restore cursor position */
245 return ret;
Rob Clark71cc25c2017-09-13 18:05:42 -0400246}
247
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200248/*
249 * Update the mode table.
250 *
251 * By default the only mode available is 80x25. If the console has at least 50
252 * lines, enable mode 80x50. If we can query the console size and it is neither
253 * 80x25 nor 80x50, set it as an additional mode.
254 */
255static void query_console_size(void)
256{
257 const char *stdout_name = env_get("stdout");
Alexander Graf80483b22018-06-03 15:51:17 +0200258 int rows = 25, cols = 80;
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200259
260 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
261 IS_ENABLED(CONFIG_DM_VIDEO)) {
262 struct stdio_dev *stdout_dev =
263 stdio_get_by_name("vidconsole");
264 struct udevice *dev = stdout_dev->priv;
265 struct vidconsole_priv *priv =
266 dev_get_uclass_priv(dev);
267 rows = priv->rows;
268 cols = priv->cols;
269 } else if (query_console_serial(&rows, &cols)) {
270 return;
271 }
272
273 /* Test if we can have Mode 1 */
274 if (cols >= 80 && rows >= 50) {
275 efi_cout_modes[1].present = 1;
276 efi_con_mode.max_mode = 2;
277 }
278
279 /*
280 * Install our mode as mode 2 if it is different
281 * than mode 0 or 1 and set it as the currently selected mode
282 */
283 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
284 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
285 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
286 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
287 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
288 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
289 efi_con_mode.mode = EFI_COUT_MODE_2;
290 }
291}
292
Alexander Grafc1311ad2016-03-04 01:10:00 +0100293static efi_status_t EFIAPI efi_cout_query_mode(
294 struct efi_simple_text_output_protocol *this,
295 unsigned long mode_number, unsigned long *columns,
296 unsigned long *rows)
297{
298 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
299
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100300 if (mode_number >= efi_con_mode.max_mode)
301 return EFI_EXIT(EFI_UNSUPPORTED);
302
303 if (efi_cout_modes[mode_number].present != 1)
304 return EFI_EXIT(EFI_UNSUPPORTED);
305
Alexander Grafc1311ad2016-03-04 01:10:00 +0100306 if (columns)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100307 *columns = efi_cout_modes[mode_number].columns;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100308 if (rows)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100309 *rows = efi_cout_modes[mode_number].rows;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100310
311 return EFI_EXIT(EFI_SUCCESS);
312}
313
314static efi_status_t EFIAPI efi_cout_set_mode(
315 struct efi_simple_text_output_protocol *this,
316 unsigned long mode_number)
317{
318 EFI_ENTRY("%p, %ld", this, mode_number);
319
Alexander Grafc1311ad2016-03-04 01:10:00 +0100320
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100321 if (mode_number > efi_con_mode.max_mode)
322 return EFI_EXIT(EFI_UNSUPPORTED);
323
324 efi_con_mode.mode = mode_number;
325 efi_con_mode.cursor_column = 0;
326 efi_con_mode.cursor_row = 0;
327
328 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100329}
330
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400331static const struct {
332 unsigned int fg;
333 unsigned int bg;
334} color[] = {
335 { 30, 40 }, /* 0: black */
336 { 34, 44 }, /* 1: blue */
337 { 32, 42 }, /* 2: green */
338 { 36, 46 }, /* 3: cyan */
339 { 31, 41 }, /* 4: red */
340 { 35, 45 }, /* 5: magenta */
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +0200341 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
342 { 37, 47 }, /* 7: light gray, map to white */
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400343};
344
345/* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100346static efi_status_t EFIAPI efi_cout_set_attribute(
347 struct efi_simple_text_output_protocol *this,
348 unsigned long attribute)
349{
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400350 unsigned int bold = EFI_ATTR_BOLD(attribute);
351 unsigned int fg = EFI_ATTR_FG(attribute);
352 unsigned int bg = EFI_ATTR_BG(attribute);
353
Alexander Grafc1311ad2016-03-04 01:10:00 +0100354 EFI_ENTRY("%p, %lx", this, attribute);
355
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400356 if (attribute)
357 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
358 else
359 printf(ESC"[0;37;40m");
360
361 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100362}
363
364static efi_status_t EFIAPI efi_cout_clear_screen(
365 struct efi_simple_text_output_protocol *this)
366{
367 EFI_ENTRY("%p", this);
368
369 printf(ESC"[2J");
Heinrich Schuchardte67ff942018-07-05 08:18:00 +0200370 efi_con_mode.cursor_column = 0;
371 efi_con_mode.cursor_row = 0;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100372
373 return EFI_EXIT(EFI_SUCCESS);
374}
375
Heinrich Schuchardt9d12daf2018-07-05 19:58:07 +0200376static efi_status_t EFIAPI efi_cout_reset(
377 struct efi_simple_text_output_protocol *this,
378 char extended_verification)
379{
380 EFI_ENTRY("%p, %d", this, extended_verification);
381
382 /* Clear screen */
383 EFI_CALL(efi_cout_clear_screen(this));
384 /* Set default colors */
385 printf(ESC "[0;37;40m");
386
387 return EFI_EXIT(EFI_SUCCESS);
388}
389
Alexander Grafc1311ad2016-03-04 01:10:00 +0100390static efi_status_t EFIAPI efi_cout_set_cursor_position(
391 struct efi_simple_text_output_protocol *this,
392 unsigned long column, unsigned long row)
393{
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200394 efi_status_t ret = EFI_SUCCESS;
395 struct simple_text_output_mode *con = &efi_con_mode;
396 struct cout_mode *mode = &efi_cout_modes[con->mode];
397
Alexander Grafc1311ad2016-03-04 01:10:00 +0100398 EFI_ENTRY("%p, %ld, %ld", this, column, row);
399
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200400 /* Check parameters */
401 if (!this) {
402 ret = EFI_INVALID_PARAMETER;
403 goto out;
404 }
405 if (row >= mode->rows || column >= mode->columns) {
406 ret = EFI_UNSUPPORTED;
407 goto out;
408 }
409
410 /*
411 * Set cursor position by sending CSI H.
412 * EFI origin is [0, 0], terminal origin is [1, 1].
413 */
414 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100415 efi_con_mode.cursor_column = column;
416 efi_con_mode.cursor_row = row;
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200417out:
418 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100419}
420
421static efi_status_t EFIAPI efi_cout_enable_cursor(
422 struct efi_simple_text_output_protocol *this,
423 bool enable)
424{
425 EFI_ENTRY("%p, %d", this, enable);
426
427 printf(ESC"[?25%c", enable ? 'h' : 'l');
428
429 return EFI_EXIT(EFI_SUCCESS);
430}
431
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200432struct efi_simple_text_output_protocol efi_con_out = {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100433 .reset = efi_cout_reset,
434 .output_string = efi_cout_output_string,
435 .test_string = efi_cout_test_string,
436 .query_mode = efi_cout_query_mode,
437 .set_mode = efi_cout_set_mode,
438 .set_attribute = efi_cout_set_attribute,
439 .clear_screen = efi_cout_clear_screen,
440 .set_cursor_position = efi_cout_set_cursor_position,
441 .enable_cursor = efi_cout_enable_cursor,
442 .mode = (void*)&efi_con_mode,
443};
444
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200445/**
446 * struct efi_cin_notify_function - registered console input notify function
447 *
448 * @link: link to list
449 * @data: key to notify
450 * @function: function to call
451 */
452struct efi_cin_notify_function {
453 struct list_head link;
454 struct efi_key_data key;
455 efi_status_t (EFIAPI *function)
456 (struct efi_key_data *key_data);
457};
458
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200459static bool key_available;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200460static struct efi_key_data next_key;
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200461static LIST_HEAD(cin_notify_functions);
Heinrich Schuchardt4f187892018-07-05 08:17:59 +0200462
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200463/**
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200464 * set_shift_mask() - set shift mask
465 *
466 * @mod: Xterm shift mask
467 */
468void set_shift_mask(int mod, struct efi_key_state *key_state)
469{
470 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
471 if (mod) {
472 --mod;
473 if (mod & 1)
474 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
475 if (mod & 2)
476 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
477 if (mod & 4)
478 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
479 if (mod & 8)
480 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
481 } else {
482 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
483 }
484}
485
486/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200487 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200488 *
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100489 * This gets called when we have already parsed CSI.
490 *
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200491 * @modifiers: bit mask (shift, alt, ctrl)
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100492 * @return: the unmodified code
493 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200494static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100495{
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200496 int c, mod = 0, ret = 0;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100497
498 c = getc();
499
500 if (c != ';') {
501 ret = c;
502 if (c == '~')
503 goto out;
504 c = getc();
505 }
506 for (;;) {
507 switch (c) {
508 case '0'...'9':
509 mod *= 10;
510 mod += c - '0';
511 /* fall through */
512 case ';':
513 c = getc();
514 break;
515 default:
516 goto out;
517 }
518 }
519out:
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200520 set_shift_mask(mod, key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100521 if (!ret)
522 ret = c;
523 return ret;
524}
525
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200526/**
527 * efi_cin_read_key() - read a key from the console input
528 *
529 * @key: - key received
530 * Return: - status code
531 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200532static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100533{
534 struct efi_input_key pressed_key = {
535 .scan_code = 0,
536 .unicode_char = 0,
537 };
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200538 s32 ch;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100539
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200540 if (console_read_unicode(&ch))
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200541 return EFI_NOT_READY;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200542
543 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
544 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
545
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200546 /* We do not support multi-word codes */
547 if (ch >= 0x10000)
548 ch = '?';
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200549
550 switch (ch) {
551 case 0x1b:
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100552 /*
553 * Xterm Control Sequences
554 * https://www.xfree86.org/4.8.0/ctlseqs.html
555 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100556 ch = getc();
557 switch (ch) {
558 case cESC: /* ESC */
559 pressed_key.scan_code = 23;
560 break;
561 case 'O': /* F1 - F4 */
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100562 ch = getc();
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200563 /* consider modifiers */
564 if (ch < 'P') {
565 set_shift_mask(ch - '0', &key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100566 ch = getc();
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200567 }
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100568 pressed_key.scan_code = ch - 'P' + 11;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100569 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100570 case '[':
571 ch = getc();
572 switch (ch) {
573 case 'A'...'D': /* up, down right, left */
574 pressed_key.scan_code = ch - 'A' + 1;
575 break;
576 case 'F': /* End */
577 pressed_key.scan_code = 6;
578 break;
579 case 'H': /* Home */
580 pressed_key.scan_code = 5;
581 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100582 case '1':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200583 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100584 switch (ch) {
585 case '1'...'5': /* F1 - F5 */
586 pressed_key.scan_code = ch - '1' + 11;
587 break;
588 case '7'...'9': /* F6 - F8 */
589 pressed_key.scan_code = ch - '7' + 16;
590 break;
591 case 'A'...'D': /* up, down right, left */
592 pressed_key.scan_code = ch - 'A' + 1;
593 break;
594 case 'F':
595 pressed_key.scan_code = 6; /* End */
596 break;
597 case 'H':
598 pressed_key.scan_code = 5; /* Home */
599 break;
600 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100601 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100602 case '2':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200603 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100604 switch (ch) {
605 case '0'...'1': /* F9 - F10 */
606 pressed_key.scan_code = ch - '0' + 19;
607 break;
608 case '3'...'4': /* F11 - F12 */
609 pressed_key.scan_code = ch - '3' + 21;
610 break;
611 case '~': /* INS */
612 pressed_key.scan_code = 7;
613 break;
614 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100615 break;
616 case '3': /* DEL */
617 pressed_key.scan_code = 8;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200618 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100619 break;
620 case '5': /* PG UP */
621 pressed_key.scan_code = 9;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200622 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100623 break;
624 case '6': /* PG DOWN */
625 pressed_key.scan_code = 10;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200626 analyze_modifiers(&key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100627 break;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200628 } /* [ */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100629 break;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200630 default:
631 /* ALT key */
632 set_shift_mask(3, &key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100633 }
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200634 break;
635 case 0x7f:
Alexander Grafc1311ad2016-03-04 01:10:00 +0100636 /* Backspace */
637 ch = 0x08;
638 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200639 if (pressed_key.scan_code) {
640 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
641 } else {
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100642 pressed_key.unicode_char = ch;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200643
644 /*
645 * Assume left control key for control characters typically
646 * entered using the control key.
647 */
648 if (ch >= 0x01 && ch <= 0x1f) {
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200649 key->key_state.key_shift_state |=
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200650 EFI_SHIFT_STATE_VALID;
651 switch (ch) {
652 case 0x01 ... 0x07:
653 case 0x0b ... 0x0c:
654 case 0x0e ... 0x1f:
655 key->key_state.key_shift_state |=
656 EFI_LEFT_CONTROL_PRESSED;
657 }
658 }
659 }
660 key->key = pressed_key;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100661
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200662 return EFI_SUCCESS;
663}
664
665/**
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200666 * efi_cin_notify() - notify registered functions
667 */
668static void efi_cin_notify(void)
669{
670 struct efi_cin_notify_function *item;
671
672 list_for_each_entry(item, &cin_notify_functions, link) {
673 bool match = true;
674
675 /* We do not support toggle states */
676 if (item->key.key.unicode_char || item->key.key.scan_code) {
677 if (item->key.key.unicode_char !=
678 next_key.key.unicode_char ||
679 item->key.key.scan_code != next_key.key.scan_code)
680 match = false;
681 }
682 if (item->key.key_state.key_shift_state &&
683 item->key.key_state.key_shift_state !=
684 next_key.key_state.key_shift_state)
685 match = false;
686
687 if (match)
688 /* We don't bother about the return code */
689 EFI_CALL(item->function(&next_key));
690 }
691}
692
693/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200694 * efi_cin_check() - check if keyboard input is available
695 */
696static void efi_cin_check(void)
697{
698 efi_status_t ret;
699
700 if (key_available) {
701 efi_signal_event(efi_con_in.wait_for_key, true);
702 return;
703 }
704
705 if (tstc()) {
706 ret = efi_cin_read_key(&next_key);
707 if (ret == EFI_SUCCESS) {
708 key_available = true;
709
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200710 /* Notify registered functions */
711 efi_cin_notify();
712
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200713 /* Queue the wait for key event */
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200714 if (key_available)
715 efi_signal_event(efi_con_in.wait_for_key, true);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200716 }
717 }
718}
719
720/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200721 * efi_cin_empty_buffer() - empty input buffer
722 */
723static void efi_cin_empty_buffer(void)
724{
725 while (tstc())
726 getc();
727 key_available = false;
728}
729
730/**
731 * efi_cin_reset_ex() - reset console input
732 *
733 * @this: - the extended simple text input protocol
734 * @extended_verification: - extended verification
735 *
736 * This function implements the reset service of the
737 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
738 *
739 * See the Unified Extensible Firmware Interface (UEFI) specification for
740 * details.
741 *
742 * Return: old value of the task priority level
743 */
744static efi_status_t EFIAPI efi_cin_reset_ex(
745 struct efi_simple_text_input_ex_protocol *this,
746 bool extended_verification)
747{
748 efi_status_t ret = EFI_SUCCESS;
749
750 EFI_ENTRY("%p, %d", this, extended_verification);
751
752 /* Check parameters */
753 if (!this) {
754 ret = EFI_INVALID_PARAMETER;
755 goto out;
756 }
757
758 efi_cin_empty_buffer();
759out:
760 return EFI_EXIT(ret);
761}
762
763/**
764 * efi_cin_read_key_stroke_ex() - read key stroke
765 *
766 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
767 * @key_data: key read from console
768 * Return: status code
769 *
770 * This function implements the ReadKeyStrokeEx service of the
771 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
772 *
773 * See the Unified Extensible Firmware Interface (UEFI) specification for
774 * details.
775 */
776static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
777 struct efi_simple_text_input_ex_protocol *this,
778 struct efi_key_data *key_data)
779{
780 efi_status_t ret = EFI_SUCCESS;
781
782 EFI_ENTRY("%p, %p", this, key_data);
783
784 /* Check parameters */
785 if (!this || !key_data) {
786 ret = EFI_INVALID_PARAMETER;
787 goto out;
788 }
789
790 /* We don't do interrupts, so check for timers cooperatively */
791 efi_timer_check();
792
793 /* Enable console input after ExitBootServices */
794 efi_cin_check();
795
796 if (!key_available) {
797 ret = EFI_NOT_READY;
798 goto out;
799 }
Heinrich Schuchardtbfc2dd52019-04-06 20:59:24 +0200800 /*
801 * CTRL+A - CTRL+Z have to be signaled as a - z.
802 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
803 */
804 switch (next_key.key.unicode_char) {
805 case 0x01 ... 0x07:
806 case 0x0b ... 0x0c:
807 case 0x0e ... 0x1a:
808 if (!(next_key.key_state.key_toggle_state &
809 EFI_CAPS_LOCK_ACTIVE) ^
810 !(next_key.key_state.key_shift_state &
811 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
812 next_key.key.unicode_char += 0x40;
813 else
814 next_key.key.unicode_char += 0x60;
815 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200816 *key_data = next_key;
817 key_available = false;
818 efi_con_in.wait_for_key->is_signaled = false;
Heinrich Schuchardtbfc2dd52019-04-06 20:59:24 +0200819
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200820out:
821 return EFI_EXIT(ret);
822}
823
824/**
825 * efi_cin_set_state() - set toggle key state
826 *
827 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
828 * @key_toggle_state: key toggle state
829 * Return: status code
830 *
831 * This function implements the SetState service of the
832 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
833 *
834 * See the Unified Extensible Firmware Interface (UEFI) specification for
835 * details.
836 */
837static efi_status_t EFIAPI efi_cin_set_state(
838 struct efi_simple_text_input_ex_protocol *this,
839 u8 key_toggle_state)
840{
841 EFI_ENTRY("%p, %u", this, key_toggle_state);
842 /*
843 * U-Boot supports multiple console input sources like serial and
844 * net console for which a key toggle state cannot be set at all.
845 *
846 * According to the UEFI specification it is allowable to not implement
847 * this service.
848 */
849 return EFI_EXIT(EFI_UNSUPPORTED);
850}
851
852/**
853 * efi_cin_register_key_notify() - register key notification function
854 *
855 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
856 * @key_data: key to be notified
857 * @key_notify_function: function to be called if the key is pressed
858 * @notify_handle: handle for unregistering the notification
859 * Return: status code
860 *
861 * This function implements the SetState service of the
862 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
863 *
864 * See the Unified Extensible Firmware Interface (UEFI) specification for
865 * details.
866 */
867static efi_status_t EFIAPI efi_cin_register_key_notify(
868 struct efi_simple_text_input_ex_protocol *this,
869 struct efi_key_data *key_data,
870 efi_status_t (EFIAPI *key_notify_function)(
871 struct efi_key_data *key_data),
872 void **notify_handle)
873{
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200874 efi_status_t ret = EFI_SUCCESS;
875 struct efi_cin_notify_function *notify_function;
876
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200877 EFI_ENTRY("%p, %p, %p, %p",
878 this, key_data, key_notify_function, notify_handle);
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200879
880 /* Check parameters */
881 if (!this || !key_data || !key_notify_function || !notify_handle) {
882 ret = EFI_INVALID_PARAMETER;
883 goto out;
884 }
885
886 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
887 key_data->key.unicode_char,
888 key_data->key.scan_code,
889 key_data->key_state.key_shift_state,
890 key_data->key_state.key_toggle_state);
891
892 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
893 if (!notify_function) {
894 ret = EFI_OUT_OF_RESOURCES;
895 goto out;
896 }
897 notify_function->key = *key_data;
898 notify_function->function = key_notify_function;
899 list_add_tail(&notify_function->link, &cin_notify_functions);
900 *notify_handle = notify_function;
901out:
902 return EFI_EXIT(ret);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200903}
904
905/**
906 * efi_cin_unregister_key_notify() - unregister key notification function
907 *
908 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
909 * @notification_handle: handle received when registering
910 * Return: status code
911 *
912 * This function implements the SetState service of the
913 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
914 *
915 * See the Unified Extensible Firmware Interface (UEFI) specification for
916 * details.
917 */
918static efi_status_t EFIAPI efi_cin_unregister_key_notify(
919 struct efi_simple_text_input_ex_protocol *this,
920 void *notification_handle)
921{
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200922 efi_status_t ret = EFI_INVALID_PARAMETER;
923 struct efi_cin_notify_function *item, *notify_function =
924 notification_handle;
925
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200926 EFI_ENTRY("%p, %p", this, notification_handle);
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200927
928 /* Check parameters */
929 if (!this || !notification_handle)
930 goto out;
931
932 list_for_each_entry(item, &cin_notify_functions, link) {
933 if (item == notify_function) {
934 ret = EFI_SUCCESS;
935 break;
936 }
937 }
938 if (ret != EFI_SUCCESS)
939 goto out;
940
941 /* Remove the notify function */
942 list_del(&notify_function->link);
943 free(notify_function);
944out:
945 return EFI_EXIT(ret);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200946}
947
948
949/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200950 * efi_cin_reset() - drain the input buffer
951 *
952 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
953 * @extended_verification: allow for exhaustive verification
954 * Return: status code
955 *
956 * This function implements the Reset service of the
957 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
958 *
959 * See the Unified Extensible Firmware Interface (UEFI) specification for
960 * details.
961 */
962static efi_status_t EFIAPI efi_cin_reset
963 (struct efi_simple_text_input_protocol *this,
964 bool extended_verification)
965{
966 efi_status_t ret = EFI_SUCCESS;
967
968 EFI_ENTRY("%p, %d", this, extended_verification);
969
970 /* Check parameters */
971 if (!this) {
972 ret = EFI_INVALID_PARAMETER;
973 goto out;
974 }
975
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200976 efi_cin_empty_buffer();
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200977out:
978 return EFI_EXIT(ret);
979}
980
981/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200982 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200983 *
984 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
985 * @key: key read from console
986 * Return: status code
987 *
988 * This function implements the ReadKeyStroke service of the
989 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
990 *
991 * See the Unified Extensible Firmware Interface (UEFI) specification for
992 * details.
993 */
994static efi_status_t EFIAPI efi_cin_read_key_stroke
995 (struct efi_simple_text_input_protocol *this,
996 struct efi_input_key *key)
997{
998 efi_status_t ret = EFI_SUCCESS;
999
1000 EFI_ENTRY("%p, %p", this, key);
1001
1002 /* Check parameters */
1003 if (!this || !key) {
1004 ret = EFI_INVALID_PARAMETER;
1005 goto out;
1006 }
1007
1008 /* We don't do interrupts, so check for timers cooperatively */
1009 efi_timer_check();
1010
1011 /* Enable console input after ExitBootServices */
1012 efi_cin_check();
1013
1014 if (!key_available) {
1015 ret = EFI_NOT_READY;
1016 goto out;
1017 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001018 *key = next_key.key;
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001019 key_available = false;
1020 efi_con_in.wait_for_key->is_signaled = false;
1021out:
1022 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +01001023}
1024
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001025static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1026 .reset = efi_cin_reset_ex,
1027 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1028 .wait_for_key_ex = NULL,
1029 .set_state = efi_cin_set_state,
1030 .register_key_notify = efi_cin_register_key_notify,
1031 .unregister_key_notify = efi_cin_unregister_key_notify,
1032};
1033
Heinrich Schuchardt3e603ec2018-09-08 10:20:10 +02001034struct efi_simple_text_input_protocol efi_con_in = {
Alexander Grafc1311ad2016-03-04 01:10:00 +01001035 .reset = efi_cin_reset,
1036 .read_key_stroke = efi_cin_read_key_stroke,
1037 .wait_for_key = NULL,
1038};
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001039
1040static struct efi_event *console_timer_event;
1041
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001042/*
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001043 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001044 *
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001045 * @event: console timer event
1046 * @context: not used
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001047 */
xypron.glpk@gmx.deff925932017-07-20 05:26:07 +02001048static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1049 void *context)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001050{
1051 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001052 efi_cin_check();
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001053 EFI_EXIT(EFI_SUCCESS);
1054}
1055
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001056/**
1057 * efi_key_notify() - notify the wait for key event
1058 *
1059 * @event: wait for key event
1060 * @context: not used
1061 */
1062static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1063{
1064 EFI_ENTRY("%p, %p", event, context);
1065 efi_cin_check();
1066 EFI_EXIT(EFI_SUCCESS);
1067}
1068
1069/**
1070 * efi_console_register() - install the console protocols
1071 *
1072 * This function is called from do_bootefi_exec().
Heinrich Schuchardt6f566c22018-10-02 06:08:26 +02001073 *
1074 * Return: status code
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001075 */
Heinrich Schuchardt6f566c22018-10-02 06:08:26 +02001076efi_status_t efi_console_register(void)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001077{
1078 efi_status_t r;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001079 efi_handle_t console_output_handle;
1080 efi_handle_t console_input_handle;
Rob Clarka17e62c2017-07-24 10:39:01 -04001081
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +02001082 /* Set up mode information */
1083 query_console_size();
1084
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001085 /* Create handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001086 r = efi_create_handle(&console_output_handle);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001087 if (r != EFI_SUCCESS)
1088 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +02001089
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001090 r = efi_add_protocol(console_output_handle,
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001091 &efi_guid_text_output_protocol, &efi_con_out);
1092 if (r != EFI_SUCCESS)
1093 goto out_of_memory;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001094 systab.con_out_handle = console_output_handle;
1095 systab.stderr_handle = console_output_handle;
Alexander Graf40e3e752018-09-04 14:59:11 +02001096
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001097 r = efi_create_handle(&console_input_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_input_handle,
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001102 &efi_guid_text_input_protocol, &efi_con_in);
1103 if (r != EFI_SUCCESS)
1104 goto out_of_memory;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001105 systab.con_in_handle = console_input_handle;
1106 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001107 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1108 if (r != EFI_SUCCESS)
1109 goto out_of_memory;
Rob Clarka17e62c2017-07-24 10:39:01 -04001110
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001111 /* Create console events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001112 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1113 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001114 if (r != EFI_SUCCESS) {
1115 printf("ERROR: Failed to register WaitForKey event\n");
1116 return r;
1117 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001118 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001119 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001120 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001121 &console_timer_event);
1122 if (r != EFI_SUCCESS) {
1123 printf("ERROR: Failed to register console event\n");
1124 return r;
1125 }
1126 /* 5000 ns cycle is sufficient for 2 MBaud */
1127 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1128 if (r != EFI_SUCCESS)
1129 printf("ERROR: Failed to set console timer\n");
1130 return r;
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001131out_of_memory:
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +02001132 printf("ERROR: Out of memory\n");
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001133 return r;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001134}