blob: d17f0a13f95d674d79364495944d3bf0a661e704 [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
Heinrich Schuchardt62217292018-05-16 18:17:38 +020065/*
66 * Receive and parse a reply from the terminal.
67 *
68 * @n: array of return values
69 * @num: number of return values expected
70 * @end_char: character indicating end of terminal message
71 * @return: non-zero indicates error
72 */
73static int term_read_reply(int *n, int num, char end_char)
Alexander Grafc1311ad2016-03-04 01:10:00 +010074{
75 char c;
76 int i = 0;
77
78 c = getc();
79 if (c != cESC)
80 return -1;
81 c = getc();
82 if (c != '[')
83 return -1;
84
85 n[0] = 0;
86 while (1) {
87 c = getc();
88 if (c == ';') {
89 i++;
Heinrich Schuchardt62217292018-05-16 18:17:38 +020090 if (i >= num)
Alexander Grafc1311ad2016-03-04 01:10:00 +010091 return -1;
92 n[i] = 0;
93 continue;
94 } else if (c == end_char) {
95 break;
96 } else if (c > '9' || c < '0') {
97 return -1;
98 }
99
100 /* Read one more decimal position */
101 n[i] *= 10;
102 n[i] += c - '0';
103 }
Heinrich Schuchardt62217292018-05-16 18:17:38 +0200104 if (i != num - 1)
105 return -1;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100106
107 return 0;
108}
109
Alexander Grafc1311ad2016-03-04 01:10:00 +0100110static efi_status_t EFIAPI efi_cout_output_string(
111 struct efi_simple_text_output_protocol *this,
Rob Clark3a45bc72017-09-13 18:05:44 -0400112 const efi_string_t string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100113{
Rob Clark3a45bc72017-09-13 18:05:44 -0400114 struct simple_text_output_mode *con = &efi_con_mode;
115 struct cout_mode *mode = &efi_cout_modes[con->mode];
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200116 char *buf, *pos;
117 u16 *p;
118 efi_status_t ret = EFI_SUCCESS;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100119
120 EFI_ENTRY("%p, %p", this, string);
Rob Clark3a45bc72017-09-13 18:05:44 -0400121
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200122 buf = malloc(utf16_utf8_strlen(string) + 1);
123 if (!buf) {
124 ret = EFI_OUT_OF_RESOURCES;
125 goto out;
126 }
127 pos = buf;
128 utf16_utf8_strcpy(&pos, string);
Rob Clark3a45bc72017-09-13 18:05:44 -0400129 fputs(stdout, buf);
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200130 free(buf);
Rob Clark3a45bc72017-09-13 18:05:44 -0400131
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200132 /*
133 * Update the cursor position.
134 *
135 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
136 * and U000D. All other characters, including control characters
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +0200137 * U+0007 (BEL) and U+0009 (TAB), have to increase the column by one.
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200138 */
139 for (p = string; *p; ++p) {
Rob Clark3a45bc72017-09-13 18:05:44 -0400140 switch (*p) {
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200141 case '\b': /* U+0008, backspace */
142 con->cursor_column = max(0, con->cursor_column - 1);
Rob Clark3a45bc72017-09-13 18:05:44 -0400143 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200144 case '\n': /* U+000A, newline */
Rob Clark3a45bc72017-09-13 18:05:44 -0400145 con->cursor_column = 0;
146 con->cursor_row++;
147 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200148 case '\r': /* U+000D, carriage-return */
149 con->cursor_column = 0;
Rob Clark3a45bc72017-09-13 18:05:44 -0400150 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200151 case 0xd800 ... 0xdbff:
152 /*
153 * Ignore high surrogates, we do not want to count a
154 * Unicode character twice.
155 */
Rob Clark3a45bc72017-09-13 18:05:44 -0400156 break;
157 default:
158 con->cursor_column++;
159 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100160 }
Rob Clark3a45bc72017-09-13 18:05:44 -0400161 if (con->cursor_column >= mode->columns) {
162 con->cursor_column = 0;
163 con->cursor_row++;
164 }
165 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100166 }
167
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200168out:
169 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100170}
171
172static efi_status_t EFIAPI efi_cout_test_string(
173 struct efi_simple_text_output_protocol *this,
Rob Clark3a45bc72017-09-13 18:05:44 -0400174 const efi_string_t string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100175{
176 EFI_ENTRY("%p, %p", this, string);
177 return EFI_EXIT(EFI_SUCCESS);
178}
179
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100180static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
181{
182 if (!mode->present)
183 return false;
184
185 return (mode->rows == rows) && (mode->columns == cols);
186}
187
Rob Clark71cc25c2017-09-13 18:05:42 -0400188static int query_console_serial(int *rows, int *cols)
189{
190 /* Ask the terminal about its size */
191 int n[3];
192 u64 timeout;
193
194 /* Empty input buffer */
195 while (tstc())
196 getc();
197
198 printf(ESC"[18t");
199
200 /* Check if we have a terminal that understands */
201 timeout = timer_get_us() + 1000000;
202 while (!tstc())
203 if (timer_get_us() > timeout)
204 return -1;
205
206 /* Read {depth,rows,cols} */
207 if (term_read_reply(n, 3, 't'))
208 return -1;
209
210 *cols = n[2];
211 *rows = n[1];
212
213 return 0;
214}
215
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200216/*
217 * Update the mode table.
218 *
219 * By default the only mode available is 80x25. If the console has at least 50
220 * lines, enable mode 80x50. If we can query the console size and it is neither
221 * 80x25 nor 80x50, set it as an additional mode.
222 */
223static void query_console_size(void)
224{
225 const char *stdout_name = env_get("stdout");
Alexander Graf80483b22018-06-03 15:51:17 +0200226 int rows = 25, cols = 80;
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200227
228 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
229 IS_ENABLED(CONFIG_DM_VIDEO)) {
230 struct stdio_dev *stdout_dev =
231 stdio_get_by_name("vidconsole");
232 struct udevice *dev = stdout_dev->priv;
233 struct vidconsole_priv *priv =
234 dev_get_uclass_priv(dev);
235 rows = priv->rows;
236 cols = priv->cols;
237 } else if (query_console_serial(&rows, &cols)) {
238 return;
239 }
240
241 /* Test if we can have Mode 1 */
242 if (cols >= 80 && rows >= 50) {
243 efi_cout_modes[1].present = 1;
244 efi_con_mode.max_mode = 2;
245 }
246
247 /*
248 * Install our mode as mode 2 if it is different
249 * than mode 0 or 1 and set it as the currently selected mode
250 */
251 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
252 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
253 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
254 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
255 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
256 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
257 efi_con_mode.mode = EFI_COUT_MODE_2;
258 }
259}
260
Alexander Grafc1311ad2016-03-04 01:10:00 +0100261static efi_status_t EFIAPI efi_cout_query_mode(
262 struct efi_simple_text_output_protocol *this,
263 unsigned long mode_number, unsigned long *columns,
264 unsigned long *rows)
265{
266 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
267
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100268 if (mode_number >= efi_con_mode.max_mode)
269 return EFI_EXIT(EFI_UNSUPPORTED);
270
271 if (efi_cout_modes[mode_number].present != 1)
272 return EFI_EXIT(EFI_UNSUPPORTED);
273
Alexander Grafc1311ad2016-03-04 01:10:00 +0100274 if (columns)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100275 *columns = efi_cout_modes[mode_number].columns;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100276 if (rows)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100277 *rows = efi_cout_modes[mode_number].rows;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100278
279 return EFI_EXIT(EFI_SUCCESS);
280}
281
282static efi_status_t EFIAPI efi_cout_set_mode(
283 struct efi_simple_text_output_protocol *this,
284 unsigned long mode_number)
285{
286 EFI_ENTRY("%p, %ld", this, mode_number);
287
Alexander Grafc1311ad2016-03-04 01:10:00 +0100288
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100289 if (mode_number > efi_con_mode.max_mode)
290 return EFI_EXIT(EFI_UNSUPPORTED);
291
292 efi_con_mode.mode = mode_number;
293 efi_con_mode.cursor_column = 0;
294 efi_con_mode.cursor_row = 0;
295
296 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100297}
298
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400299static const struct {
300 unsigned int fg;
301 unsigned int bg;
302} color[] = {
303 { 30, 40 }, /* 0: black */
304 { 34, 44 }, /* 1: blue */
305 { 32, 42 }, /* 2: green */
306 { 36, 46 }, /* 3: cyan */
307 { 31, 41 }, /* 4: red */
308 { 35, 45 }, /* 5: magenta */
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +0200309 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
310 { 37, 47 }, /* 7: light gray, map to white */
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400311};
312
313/* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100314static efi_status_t EFIAPI efi_cout_set_attribute(
315 struct efi_simple_text_output_protocol *this,
316 unsigned long attribute)
317{
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400318 unsigned int bold = EFI_ATTR_BOLD(attribute);
319 unsigned int fg = EFI_ATTR_FG(attribute);
320 unsigned int bg = EFI_ATTR_BG(attribute);
321
Alexander Grafc1311ad2016-03-04 01:10:00 +0100322 EFI_ENTRY("%p, %lx", this, attribute);
323
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400324 if (attribute)
325 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
326 else
327 printf(ESC"[0;37;40m");
328
329 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100330}
331
332static efi_status_t EFIAPI efi_cout_clear_screen(
333 struct efi_simple_text_output_protocol *this)
334{
335 EFI_ENTRY("%p", this);
336
337 printf(ESC"[2J");
Heinrich Schuchardte67ff942018-07-05 08:18:00 +0200338 efi_con_mode.cursor_column = 0;
339 efi_con_mode.cursor_row = 0;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100340
341 return EFI_EXIT(EFI_SUCCESS);
342}
343
Heinrich Schuchardt9d12daf2018-07-05 19:58:07 +0200344static efi_status_t EFIAPI efi_cout_reset(
345 struct efi_simple_text_output_protocol *this,
346 char extended_verification)
347{
348 EFI_ENTRY("%p, %d", this, extended_verification);
349
350 /* Clear screen */
351 EFI_CALL(efi_cout_clear_screen(this));
352 /* Set default colors */
353 printf(ESC "[0;37;40m");
354
355 return EFI_EXIT(EFI_SUCCESS);
356}
357
Alexander Grafc1311ad2016-03-04 01:10:00 +0100358static efi_status_t EFIAPI efi_cout_set_cursor_position(
359 struct efi_simple_text_output_protocol *this,
360 unsigned long column, unsigned long row)
361{
362 EFI_ENTRY("%p, %ld, %ld", this, column, row);
363
364 printf(ESC"[%d;%df", (int)row, (int)column);
365 efi_con_mode.cursor_column = column;
366 efi_con_mode.cursor_row = row;
367
368 return EFI_EXIT(EFI_SUCCESS);
369}
370
371static efi_status_t EFIAPI efi_cout_enable_cursor(
372 struct efi_simple_text_output_protocol *this,
373 bool enable)
374{
375 EFI_ENTRY("%p, %d", this, enable);
376
377 printf(ESC"[?25%c", enable ? 'h' : 'l');
378
379 return EFI_EXIT(EFI_SUCCESS);
380}
381
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200382struct efi_simple_text_output_protocol efi_con_out = {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100383 .reset = efi_cout_reset,
384 .output_string = efi_cout_output_string,
385 .test_string = efi_cout_test_string,
386 .query_mode = efi_cout_query_mode,
387 .set_mode = efi_cout_set_mode,
388 .set_attribute = efi_cout_set_attribute,
389 .clear_screen = efi_cout_clear_screen,
390 .set_cursor_position = efi_cout_set_cursor_position,
391 .enable_cursor = efi_cout_enable_cursor,
392 .mode = (void*)&efi_con_mode,
393};
394
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200395static bool key_available;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200396static struct efi_key_data next_key;
Heinrich Schuchardt4f187892018-07-05 08:17:59 +0200397
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200398/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200399 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200400 *
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100401 * This gets called when we have already parsed CSI.
402 *
403 * @modifiers: bitmask (shift, alt, ctrl)
404 * @return: the unmodified code
405 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200406static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100407{
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200408 int c, mod = 0, ret = 0;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100409
410 c = getc();
411
412 if (c != ';') {
413 ret = c;
414 if (c == '~')
415 goto out;
416 c = getc();
417 }
418 for (;;) {
419 switch (c) {
420 case '0'...'9':
421 mod *= 10;
422 mod += c - '0';
423 /* fall through */
424 case ';':
425 c = getc();
426 break;
427 default:
428 goto out;
429 }
430 }
431out:
432 if (mod)
433 --mod;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200434 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
435 if (mod) {
436 if (mod & 1)
437 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
438 if (mod & 2)
439 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
440 if (mod & 4)
441 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
442 if (mod & 8)
443 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
444 }
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100445 if (!ret)
446 ret = c;
447 return ret;
448}
449
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200450/**
451 * efi_cin_read_key() - read a key from the console input
452 *
453 * @key: - key received
454 * Return: - status code
455 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200456static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100457{
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200458 efi_status_t ret;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100459 struct efi_input_key pressed_key = {
460 .scan_code = 0,
461 .unicode_char = 0,
462 };
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200463 s32 ch;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100464
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200465 ret = console_read_unicode(&ch);
466 if (ret)
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200467 return EFI_NOT_READY;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200468
469 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
470 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
471
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200472 /* We do not support multi-word codes */
473 if (ch >= 0x10000)
474 ch = '?';
Alexander Grafc1311ad2016-03-04 01:10:00 +0100475 if (ch == cESC) {
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100476 /*
477 * Xterm Control Sequences
478 * https://www.xfree86.org/4.8.0/ctlseqs.html
479 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100480 ch = getc();
481 switch (ch) {
482 case cESC: /* ESC */
483 pressed_key.scan_code = 23;
484 break;
485 case 'O': /* F1 - F4 */
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100486 ch = getc();
487 /* skip modifiers */
488 if (ch <= '9')
489 ch = getc();
490 pressed_key.scan_code = ch - 'P' + 11;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100491 break;
492 case 'a'...'z':
493 ch = ch - 'a';
494 break;
495 case '[':
496 ch = getc();
497 switch (ch) {
498 case 'A'...'D': /* up, down right, left */
499 pressed_key.scan_code = ch - 'A' + 1;
500 break;
501 case 'F': /* End */
502 pressed_key.scan_code = 6;
503 break;
504 case 'H': /* Home */
505 pressed_key.scan_code = 5;
506 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100507 case '1':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200508 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100509 switch (ch) {
510 case '1'...'5': /* F1 - F5 */
511 pressed_key.scan_code = ch - '1' + 11;
512 break;
513 case '7'...'9': /* F6 - F8 */
514 pressed_key.scan_code = ch - '7' + 16;
515 break;
516 case 'A'...'D': /* up, down right, left */
517 pressed_key.scan_code = ch - 'A' + 1;
518 break;
519 case 'F':
520 pressed_key.scan_code = 6; /* End */
521 break;
522 case 'H':
523 pressed_key.scan_code = 5; /* Home */
524 break;
525 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100526 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100527 case '2':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200528 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100529 switch (ch) {
530 case '0'...'1': /* F9 - F10 */
531 pressed_key.scan_code = ch - '0' + 19;
532 break;
533 case '3'...'4': /* F11 - F12 */
534 pressed_key.scan_code = ch - '3' + 21;
535 break;
536 case '~': /* INS */
537 pressed_key.scan_code = 7;
538 break;
539 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100540 break;
541 case '3': /* DEL */
542 pressed_key.scan_code = 8;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200543 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100544 break;
545 case '5': /* PG UP */
546 pressed_key.scan_code = 9;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200547 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100548 break;
549 case '6': /* PG DOWN */
550 pressed_key.scan_code = 10;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200551 analyze_modifiers(&key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100552 break;
553 }
554 break;
555 }
556 } else if (ch == 0x7f) {
557 /* Backspace */
558 ch = 0x08;
559 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200560 if (pressed_key.scan_code) {
561 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
562 } else {
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100563 pressed_key.unicode_char = ch;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200564
565 /*
566 * Assume left control key for control characters typically
567 * entered using the control key.
568 */
569 if (ch >= 0x01 && ch <= 0x1f) {
570 key->key_state.key_shift_state =
571 EFI_SHIFT_STATE_VALID;
572 switch (ch) {
573 case 0x01 ... 0x07:
574 case 0x0b ... 0x0c:
575 case 0x0e ... 0x1f:
576 key->key_state.key_shift_state |=
577 EFI_LEFT_CONTROL_PRESSED;
578 }
579 }
580 }
581 key->key = pressed_key;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100582
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200583 return EFI_SUCCESS;
584}
585
586/**
587 * efi_cin_check() - check if keyboard input is available
588 */
589static void efi_cin_check(void)
590{
591 efi_status_t ret;
592
593 if (key_available) {
594 efi_signal_event(efi_con_in.wait_for_key, true);
595 return;
596 }
597
598 if (tstc()) {
599 ret = efi_cin_read_key(&next_key);
600 if (ret == EFI_SUCCESS) {
601 key_available = true;
602
603 /* Queue the wait for key event */
604 efi_signal_event(efi_con_in.wait_for_key, true);
605 }
606 }
607}
608
609/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200610 * efi_cin_empty_buffer() - empty input buffer
611 */
612static void efi_cin_empty_buffer(void)
613{
614 while (tstc())
615 getc();
616 key_available = false;
617}
618
619/**
620 * efi_cin_reset_ex() - reset console input
621 *
622 * @this: - the extended simple text input protocol
623 * @extended_verification: - extended verification
624 *
625 * This function implements the reset service of the
626 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
627 *
628 * See the Unified Extensible Firmware Interface (UEFI) specification for
629 * details.
630 *
631 * Return: old value of the task priority level
632 */
633static efi_status_t EFIAPI efi_cin_reset_ex(
634 struct efi_simple_text_input_ex_protocol *this,
635 bool extended_verification)
636{
637 efi_status_t ret = EFI_SUCCESS;
638
639 EFI_ENTRY("%p, %d", this, extended_verification);
640
641 /* Check parameters */
642 if (!this) {
643 ret = EFI_INVALID_PARAMETER;
644 goto out;
645 }
646
647 efi_cin_empty_buffer();
648out:
649 return EFI_EXIT(ret);
650}
651
652/**
653 * efi_cin_read_key_stroke_ex() - read key stroke
654 *
655 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
656 * @key_data: key read from console
657 * Return: status code
658 *
659 * This function implements the ReadKeyStrokeEx service of the
660 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
661 *
662 * See the Unified Extensible Firmware Interface (UEFI) specification for
663 * details.
664 */
665static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
666 struct efi_simple_text_input_ex_protocol *this,
667 struct efi_key_data *key_data)
668{
669 efi_status_t ret = EFI_SUCCESS;
670
671 EFI_ENTRY("%p, %p", this, key_data);
672
673 /* Check parameters */
674 if (!this || !key_data) {
675 ret = EFI_INVALID_PARAMETER;
676 goto out;
677 }
678
679 /* We don't do interrupts, so check for timers cooperatively */
680 efi_timer_check();
681
682 /* Enable console input after ExitBootServices */
683 efi_cin_check();
684
685 if (!key_available) {
686 ret = EFI_NOT_READY;
687 goto out;
688 }
689 *key_data = next_key;
690 key_available = false;
691 efi_con_in.wait_for_key->is_signaled = false;
692out:
693 return EFI_EXIT(ret);
694}
695
696/**
697 * efi_cin_set_state() - set toggle key state
698 *
699 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
700 * @key_toggle_state: key toggle state
701 * Return: status code
702 *
703 * This function implements the SetState service of the
704 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
705 *
706 * See the Unified Extensible Firmware Interface (UEFI) specification for
707 * details.
708 */
709static efi_status_t EFIAPI efi_cin_set_state(
710 struct efi_simple_text_input_ex_protocol *this,
711 u8 key_toggle_state)
712{
713 EFI_ENTRY("%p, %u", this, key_toggle_state);
714 /*
715 * U-Boot supports multiple console input sources like serial and
716 * net console for which a key toggle state cannot be set at all.
717 *
718 * According to the UEFI specification it is allowable to not implement
719 * this service.
720 */
721 return EFI_EXIT(EFI_UNSUPPORTED);
722}
723
724/**
725 * efi_cin_register_key_notify() - register key notification function
726 *
727 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
728 * @key_data: key to be notified
729 * @key_notify_function: function to be called if the key is pressed
730 * @notify_handle: handle for unregistering the notification
731 * Return: status code
732 *
733 * This function implements the SetState service of the
734 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
735 *
736 * See the Unified Extensible Firmware Interface (UEFI) specification for
737 * details.
738 */
739static efi_status_t EFIAPI efi_cin_register_key_notify(
740 struct efi_simple_text_input_ex_protocol *this,
741 struct efi_key_data *key_data,
742 efi_status_t (EFIAPI *key_notify_function)(
743 struct efi_key_data *key_data),
744 void **notify_handle)
745{
746 EFI_ENTRY("%p, %p, %p, %p",
747 this, key_data, key_notify_function, notify_handle);
748 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
749}
750
751/**
752 * efi_cin_unregister_key_notify() - unregister key notification function
753 *
754 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
755 * @notification_handle: handle received when registering
756 * Return: status code
757 *
758 * This function implements the SetState service of the
759 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
760 *
761 * See the Unified Extensible Firmware Interface (UEFI) specification for
762 * details.
763 */
764static efi_status_t EFIAPI efi_cin_unregister_key_notify(
765 struct efi_simple_text_input_ex_protocol *this,
766 void *notification_handle)
767{
768 EFI_ENTRY("%p, %p", this, notification_handle);
769 return EFI_EXIT(EFI_INVALID_PARAMETER);
770}
771
772
773/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200774 * efi_cin_reset() - drain the input buffer
775 *
776 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
777 * @extended_verification: allow for exhaustive verification
778 * Return: status code
779 *
780 * This function implements the Reset service of the
781 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
782 *
783 * See the Unified Extensible Firmware Interface (UEFI) specification for
784 * details.
785 */
786static efi_status_t EFIAPI efi_cin_reset
787 (struct efi_simple_text_input_protocol *this,
788 bool extended_verification)
789{
790 efi_status_t ret = EFI_SUCCESS;
791
792 EFI_ENTRY("%p, %d", this, extended_verification);
793
794 /* Check parameters */
795 if (!this) {
796 ret = EFI_INVALID_PARAMETER;
797 goto out;
798 }
799
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200800 efi_cin_empty_buffer();
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200801out:
802 return EFI_EXIT(ret);
803}
804
805/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200806 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200807 *
808 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
809 * @key: key read from console
810 * Return: status code
811 *
812 * This function implements the ReadKeyStroke service of the
813 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
814 *
815 * See the Unified Extensible Firmware Interface (UEFI) specification for
816 * details.
817 */
818static efi_status_t EFIAPI efi_cin_read_key_stroke
819 (struct efi_simple_text_input_protocol *this,
820 struct efi_input_key *key)
821{
822 efi_status_t ret = EFI_SUCCESS;
823
824 EFI_ENTRY("%p, %p", this, key);
825
826 /* Check parameters */
827 if (!this || !key) {
828 ret = EFI_INVALID_PARAMETER;
829 goto out;
830 }
831
832 /* We don't do interrupts, so check for timers cooperatively */
833 efi_timer_check();
834
835 /* Enable console input after ExitBootServices */
836 efi_cin_check();
837
838 if (!key_available) {
839 ret = EFI_NOT_READY;
840 goto out;
841 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200842 *key = next_key.key;
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200843 key_available = false;
844 efi_con_in.wait_for_key->is_signaled = false;
845out:
846 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100847}
848
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200849static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
850 .reset = efi_cin_reset_ex,
851 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
852 .wait_for_key_ex = NULL,
853 .set_state = efi_cin_set_state,
854 .register_key_notify = efi_cin_register_key_notify,
855 .unregister_key_notify = efi_cin_unregister_key_notify,
856};
857
Heinrich Schuchardt3e603ec2018-09-08 10:20:10 +0200858struct efi_simple_text_input_protocol efi_con_in = {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100859 .reset = efi_cin_reset,
860 .read_key_stroke = efi_cin_read_key_stroke,
861 .wait_for_key = NULL,
862};
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200863
864static struct efi_event *console_timer_event;
865
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100866/*
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200867 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100868 *
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200869 * @event: console timer event
870 * @context: not used
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100871 */
xypron.glpk@gmx.deff925932017-07-20 05:26:07 +0200872static void EFIAPI efi_console_timer_notify(struct efi_event *event,
873 void *context)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200874{
875 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200876 efi_cin_check();
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200877 EFI_EXIT(EFI_SUCCESS);
878}
879
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200880/**
881 * efi_key_notify() - notify the wait for key event
882 *
883 * @event: wait for key event
884 * @context: not used
885 */
886static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
887{
888 EFI_ENTRY("%p, %p", event, context);
889 efi_cin_check();
890 EFI_EXIT(EFI_SUCCESS);
891}
892
893/**
894 * efi_console_register() - install the console protocols
895 *
896 * This function is called from do_bootefi_exec().
897 */
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200898int efi_console_register(void)
899{
900 efi_status_t r;
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200901 struct efi_object *efi_console_output_obj;
902 struct efi_object *efi_console_input_obj;
Rob Clarka17e62c2017-07-24 10:39:01 -0400903
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200904 /* Set up mode information */
905 query_console_size();
906
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200907 /* Create handles */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100908 r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200909 if (r != EFI_SUCCESS)
910 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +0200911
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200912 r = efi_add_protocol(efi_console_output_obj->handle,
913 &efi_guid_text_output_protocol, &efi_con_out);
914 if (r != EFI_SUCCESS)
915 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +0200916 systab.con_out_handle = efi_console_output_obj->handle;
917 systab.stderr_handle = efi_console_output_obj->handle;
918
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100919 r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200920 if (r != EFI_SUCCESS)
921 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +0200922
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200923 r = efi_add_protocol(efi_console_input_obj->handle,
924 &efi_guid_text_input_protocol, &efi_con_in);
925 if (r != EFI_SUCCESS)
926 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +0200927 systab.con_in_handle = efi_console_input_obj->handle;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200928 r = efi_add_protocol(efi_console_input_obj->handle,
929 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
930 if (r != EFI_SUCCESS)
931 goto out_of_memory;
Rob Clarka17e62c2017-07-24 10:39:01 -0400932
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200933 /* Create console events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100934 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
935 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200936 if (r != EFI_SUCCESS) {
937 printf("ERROR: Failed to register WaitForKey event\n");
938 return r;
939 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200940 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200941 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100942 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200943 &console_timer_event);
944 if (r != EFI_SUCCESS) {
945 printf("ERROR: Failed to register console event\n");
946 return r;
947 }
948 /* 5000 ns cycle is sufficient for 2 MBaud */
949 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
950 if (r != EFI_SUCCESS)
951 printf("ERROR: Failed to set console timer\n");
952 return r;
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200953out_of_memory:
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +0200954 printf("ERROR: Out of memory\n");
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200955 return r;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200956}