1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * EFI application console interface
4 *
5 * Copyright (c) 2016 Alexander Graf
6 */
7
8 #define LOG_CATEGORY LOGC_EFI
9
10 #include <ansi.h>
11 #include <common.h>
12 #include <charset.h>
13 #include <malloc.h>
14 #include <time.h>
15 #include <dm/device.h>
16 #include <efi_loader.h>
17 #include <env.h>
18 #include <log.h>
19 #include <stdio_dev.h>
20 #include <video_console.h>
21 #include <linux/delay.h>
22
23 #define EFI_COUT_MODE_2 2
24 #define EFI_MAX_COUT_MODE 3
25
26 struct cout_mode {
27 unsigned long columns;
28 unsigned long rows;
29 int present;
30 };
31
32 __maybe_unused static struct efi_object uart_obj;
33
34 static struct cout_mode efi_cout_modes[] = {
35 /* EFI Mode 0 is 80x25 and always present */
36 {
37 .columns = 80,
38 .rows = 25,
39 .present = 1,
40 },
41 /* EFI Mode 1 is always 80x50 */
42 {
43 .columns = 80,
44 .rows = 50,
45 .present = 0,
46 },
47 /* Value are unknown until we query the console */
48 {
49 .columns = 0,
50 .rows = 0,
51 .present = 0,
52 },
53 };
54
55 const efi_guid_t efi_guid_text_input_ex_protocol =
56 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
57 const efi_guid_t efi_guid_text_input_protocol =
58 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
59 const efi_guid_t efi_guid_text_output_protocol =
60 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
61
62 #define cESC '\x1b'
63 #define ESC "\x1b"
64
65 /*
66 * efi_con_mode - mode information of the Simple Text Output Protocol
67 *
68 * Use safe settings before efi_setup_console_size() is called.
69 * By default enable only the 80x25 mode which must always exist.
70 */
71 static struct simple_text_output_mode efi_con_mode = {
72 .max_mode = 1,
73 .mode = 0,
74 .attribute = 0,
75 .cursor_column = 0,
76 .cursor_row = 0,
77 .cursor_visible = 1,
78 };
79
80 /**
81 * term_get_char() - read a character from the console
82 *
83 * Wait for up to 100 ms to read a character from the console.
84 *
85 * @c: pointer to the buffer to receive the character
86 * Return: 0 on success, 1 otherwise
87 */
term_get_char(s32 * c)88 static int term_get_char(s32 *c)
89 {
90 u64 timeout;
91
92 /* Wait up to 100 ms for a character */
93 timeout = timer_get_us() + 100000;
94
95 while (!tstc())
96 if (timer_get_us() > timeout)
97 return 1;
98
99 *c = getchar();
100 return 0;
101 }
102
103 /**
104 * Receive and parse a reply from the terminal.
105 *
106 * @n: array of return values
107 * @num: number of return values expected
108 * @end_char: character indicating end of terminal message
109 * Return: non-zero indicates error
110 */
term_read_reply(int * n,int num,char end_char)111 static int term_read_reply(int *n, int num, char end_char)
112 {
113 s32 c;
114 int i = 0;
115
116 if (term_get_char(&c) || c != cESC)
117 return -1;
118
119 if (term_get_char(&c) || c != '[')
120 return -1;
121
122 n[0] = 0;
123 while (1) {
124 if (!term_get_char(&c)) {
125 if (c == ';') {
126 i++;
127 if (i >= num)
128 return -1;
129 n[i] = 0;
130 continue;
131 } else if (c == end_char) {
132 break;
133 } else if (c > '9' || c < '0') {
134 return -1;
135 }
136
137 /* Read one more decimal position */
138 n[i] *= 10;
139 n[i] += c - '0';
140 } else {
141 return -1;
142 }
143 }
144 if (i != num - 1)
145 return -1;
146
147 return 0;
148 }
149
150 /**
151 * efi_cout_output_string() - write Unicode string to console
152 *
153 * This function implements the OutputString service of the simple text output
154 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
155 * for details.
156 *
157 * @this: simple text output protocol
158 * @string: u16 string
159 * Return: status code
160 */
efi_cout_output_string(struct efi_simple_text_output_protocol * this,const u16 * string)161 static efi_status_t EFIAPI efi_cout_output_string(
162 struct efi_simple_text_output_protocol *this,
163 const u16 *string)
164 {
165 struct simple_text_output_mode *con = &efi_con_mode;
166 struct cout_mode *mode = &efi_cout_modes[con->mode];
167 char *buf, *pos;
168 const u16 *p;
169 efi_status_t ret = EFI_SUCCESS;
170
171 EFI_ENTRY("%p, %p", this, string);
172
173 if (!this || !string) {
174 ret = EFI_INVALID_PARAMETER;
175 goto out;
176 }
177
178 buf = malloc(utf16_utf8_strlen(string) + 1);
179 if (!buf) {
180 ret = EFI_OUT_OF_RESOURCES;
181 goto out;
182 }
183 pos = buf;
184 utf16_utf8_strcpy(&pos, string);
185 fputs(stdout, buf);
186 free(buf);
187
188 /*
189 * Update the cursor position.
190 *
191 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
192 * and U000D. All other control characters are ignored. Any non-control
193 * character increase the column by one.
194 */
195 for (p = string; *p; ++p) {
196 switch (*p) {
197 case '\b': /* U+0008, backspace */
198 if (con->cursor_column)
199 con->cursor_column--;
200 break;
201 case '\n': /* U+000A, newline */
202 con->cursor_column = 0;
203 con->cursor_row++;
204 break;
205 case '\r': /* U+000D, carriage-return */
206 con->cursor_column = 0;
207 break;
208 case 0xd800 ... 0xdbff:
209 /*
210 * Ignore high surrogates, we do not want to count a
211 * Unicode character twice.
212 */
213 break;
214 default:
215 /* Exclude control codes */
216 if (*p > 0x1f)
217 con->cursor_column++;
218 break;
219 }
220 if (con->cursor_column >= mode->columns) {
221 con->cursor_column = 0;
222 con->cursor_row++;
223 }
224 /*
225 * When we exceed the row count the terminal will scroll up one
226 * line. We have to adjust the cursor position.
227 */
228 if (con->cursor_row >= mode->rows && con->cursor_row)
229 con->cursor_row--;
230 }
231
232 out:
233 return EFI_EXIT(ret);
234 }
235
236 /**
237 * efi_cout_test_string() - test writing Unicode string to console
238 *
239 * This function implements the TestString service of the simple text output
240 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
241 * for details.
242 *
243 * As in OutputString we simply convert UTF-16 to UTF-8 there are no unsupported
244 * code points and we can always return EFI_SUCCESS.
245 *
246 * @this: simple text output protocol
247 * @string: u16 string
248 * Return: status code
249 */
efi_cout_test_string(struct efi_simple_text_output_protocol * this,const u16 * string)250 static efi_status_t EFIAPI efi_cout_test_string(
251 struct efi_simple_text_output_protocol *this,
252 const u16 *string)
253 {
254 EFI_ENTRY("%p, %p", this, string);
255 return EFI_EXIT(EFI_SUCCESS);
256 }
257
258 /**
259 * cout_mode_matches() - check if mode has given terminal size
260 *
261 * @mode: text mode
262 * @rows: number of rows
263 * @cols: number of columns
264 * Return: true if number of rows and columns matches the mode and
265 * the mode is present
266 */
cout_mode_matches(struct cout_mode * mode,int rows,int cols)267 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
268 {
269 if (!mode->present)
270 return false;
271
272 return (mode->rows == rows) && (mode->columns == cols);
273 }
274
275 /**
276 * query_console_serial() - query serial console size
277 *
278 * When using a serial console or the net console we can only devise the
279 * terminal size by querying the terminal using ECMA-48 control sequences.
280 *
281 * @rows: pointer to return number of rows
282 * @cols: pointer to return number of columns
283 * Returns: 0 on success
284 */
query_console_serial(int * rows,int * cols)285 static int query_console_serial(int *rows, int *cols)
286 {
287 int ret = 0;
288 int n[2];
289
290 /* Empty input buffer */
291 while (tstc())
292 getchar();
293
294 /*
295 * Not all terminals understand CSI [18t for querying the console size.
296 * We should adhere to escape sequences documented in the console_codes
297 * man page and the ECMA-48 standard.
298 *
299 * So here we follow a different approach. We position the cursor to the
300 * bottom right and query its position. Before leaving the function we
301 * restore the original cursor position.
302 */
303 printf(ESC "7" /* Save cursor position */
304 ESC "[r" /* Set scrolling region to full window */
305 ESC "[999;999H" /* Move to bottom right corner */
306 ESC "[6n"); /* Query cursor position */
307
308 /* Read {rows,cols} */
309 if (term_read_reply(n, 2, 'R')) {
310 ret = 1;
311 goto out;
312 }
313
314 *cols = n[1];
315 *rows = n[0];
316 out:
317 printf(ESC "8"); /* Restore cursor position */
318 return ret;
319 }
320
321 /**
322 * query_vidconsole() - query video console size
323 *
324 *
325 * @rows: pointer to return number of rows
326 * @cols: pointer to return number of columns
327 * Returns: 0 on success
328 */
query_vidconsole(int * rows,int * cols)329 static int __maybe_unused query_vidconsole(int *rows, int *cols)
330 {
331 const char *stdout_name = env_get("stdout");
332 struct stdio_dev *stdout_dev;
333 struct udevice *dev;
334 struct vidconsole_priv *priv;
335
336 if (!stdout_name || strncmp(stdout_name, "vidconsole", 10))
337 return -ENODEV;
338 stdout_dev = stdio_get_by_name("vidconsole");
339 if (!stdout_dev)
340 return -ENODEV;
341 dev = stdout_dev->priv;
342 if (!dev)
343 return -ENODEV;
344 priv = dev_get_uclass_priv(dev);
345 if (!priv)
346 return -ENODEV;
347 *rows = priv->rows;
348 *cols = priv->cols;
349 return 0;
350 }
351
352 /**
353 * efi_setup_console_size() - update the mode table.
354 *
355 * By default the only mode available is 80x25. If the console has at least 50
356 * lines, enable mode 80x50. If we can query the console size and it is neither
357 * 80x25 nor 80x50, set it as an additional mode.
358 */
efi_setup_console_size(void)359 void efi_setup_console_size(void)
360 {
361 int rows = 25, cols = 80;
362 int ret = -ENODEV;
363
364 if (IS_ENABLED(CONFIG_VIDEO))
365 ret = query_vidconsole(&rows, &cols);
366 if (ret)
367 ret = query_console_serial(&rows, &cols);
368 if (ret)
369 return;
370
371 log_debug("Console size %dx%d\n", rows, cols);
372
373 /* Test if we can have Mode 1 */
374 if (cols >= 80 && rows >= 50) {
375 efi_cout_modes[1].present = 1;
376 efi_con_mode.max_mode = 2;
377 }
378
379 /*
380 * Install our mode as mode 2 if it is different
381 * than mode 0 or 1 and set it as the currently selected mode
382 */
383 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
384 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
385 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
386 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
387 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
388 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
389 efi_con_mode.mode = EFI_COUT_MODE_2;
390 }
391 }
392
393 /**
394 * efi_cout_query_mode() - get terminal size for a text mode
395 *
396 * This function implements the QueryMode service of the simple text output
397 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
398 * for details.
399 *
400 * @this: simple text output protocol
401 * @mode_number: mode number to retrieve information on
402 * @columns: number of columns
403 * @rows: number of rows
404 * Return: status code
405 */
efi_cout_query_mode(struct efi_simple_text_output_protocol * this,unsigned long mode_number,unsigned long * columns,unsigned long * rows)406 static efi_status_t EFIAPI efi_cout_query_mode(
407 struct efi_simple_text_output_protocol *this,
408 unsigned long mode_number, unsigned long *columns,
409 unsigned long *rows)
410 {
411 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
412
413 if (mode_number >= efi_con_mode.max_mode)
414 return EFI_EXIT(EFI_UNSUPPORTED);
415
416 if (efi_cout_modes[mode_number].present != 1)
417 return EFI_EXIT(EFI_UNSUPPORTED);
418
419 if (columns)
420 *columns = efi_cout_modes[mode_number].columns;
421 if (rows)
422 *rows = efi_cout_modes[mode_number].rows;
423
424 return EFI_EXIT(EFI_SUCCESS);
425 }
426
427 static const struct {
428 unsigned int fg;
429 unsigned int bg;
430 } color[] = {
431 { 30, 40 }, /* 0: black */
432 { 34, 44 }, /* 1: blue */
433 { 32, 42 }, /* 2: green */
434 { 36, 46 }, /* 3: cyan */
435 { 31, 41 }, /* 4: red */
436 { 35, 45 }, /* 5: magenta */
437 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
438 { 37, 47 }, /* 7: light gray, map to white */
439 };
440
441 /**
442 * efi_cout_set_attribute() - set fore- and background color
443 *
444 * This function implements the SetAttribute service of the simple text output
445 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
446 * for details.
447 *
448 * @this: simple text output protocol
449 * @attribute: foreground color - bits 0-3, background color - bits 4-6
450 * Return: status code
451 */
efi_cout_set_attribute(struct efi_simple_text_output_protocol * this,unsigned long attribute)452 static efi_status_t EFIAPI efi_cout_set_attribute(
453 struct efi_simple_text_output_protocol *this,
454 unsigned long attribute)
455 {
456 unsigned int bold = EFI_ATTR_BOLD(attribute);
457 unsigned int fg = EFI_ATTR_FG(attribute);
458 unsigned int bg = EFI_ATTR_BG(attribute);
459
460 EFI_ENTRY("%p, %lx", this, attribute);
461
462 efi_con_mode.attribute = attribute;
463 if (attribute)
464 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
465 else
466 printf(ESC"[0;37;40m");
467
468 return EFI_EXIT(EFI_SUCCESS);
469 }
470
471 /**
472 * efi_clear_screen() - clear screen
473 */
efi_clear_screen(void)474 static void efi_clear_screen(void)
475 {
476 if (CONFIG_IS_ENABLED(EFI_SCROLL_ON_CLEAR_SCREEN)) {
477 unsigned int row, screen_rows, screen_columns;
478
479 /* Avoid overwriting previous outputs on streaming consoles */
480 screen_rows = efi_cout_modes[efi_con_mode.mode].rows;
481 screen_columns = efi_cout_modes[efi_con_mode.mode].columns;
482 printf(ESC "[%u;%uH", screen_rows, screen_columns);
483 for (row = 1; row < screen_rows; row++)
484 printf("\n");
485 }
486
487 /*
488 * The Linux console wants both a clear and a home command. The video
489 * uclass does not support <ESC>[H without coordinates, yet.
490 */
491 printf(ESC "[2J" ESC "[1;1H");
492 efi_con_mode.cursor_column = 0;
493 efi_con_mode.cursor_row = 0;
494 }
495
496 /**
497 * efi_cout_clear_screen() - clear screen
498 *
499 * This function implements the ClearScreen service of the simple text output
500 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
501 * for details.
502 *
503 * @this: pointer to the protocol instance
504 * Return: status code
505 */
efi_cout_clear_screen(struct efi_simple_text_output_protocol * this)506 static efi_status_t EFIAPI efi_cout_clear_screen(
507 struct efi_simple_text_output_protocol *this)
508 {
509 EFI_ENTRY("%p", this);
510
511 /* Set default colors if not done yet */
512 if (efi_con_mode.attribute == 0) {
513 efi_con_mode.attribute = 0x07;
514 printf(ESC "[0;37;40m");
515 }
516
517 efi_clear_screen();
518
519 return EFI_EXIT(EFI_SUCCESS);
520 }
521
522 /**
523 * efi_cout_clear_set_mode() - set text model
524 *
525 * This function implements the SetMode service of the simple text output
526 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
527 * for details.
528 *
529 * @this: pointer to the protocol instance
530 * @mode_number: number of the text mode to set
531 * Return: status code
532 */
efi_cout_set_mode(struct efi_simple_text_output_protocol * this,unsigned long mode_number)533 static efi_status_t EFIAPI efi_cout_set_mode(
534 struct efi_simple_text_output_protocol *this,
535 unsigned long mode_number)
536 {
537 EFI_ENTRY("%p, %ld", this, mode_number);
538
539 if (mode_number >= efi_con_mode.max_mode)
540 return EFI_EXIT(EFI_UNSUPPORTED);
541
542 if (!efi_cout_modes[mode_number].present)
543 return EFI_EXIT(EFI_UNSUPPORTED);
544
545 efi_con_mode.mode = mode_number;
546 efi_clear_screen();
547
548 return EFI_EXIT(EFI_SUCCESS);
549 }
550
551 /**
552 * efi_cout_reset() - reset the terminal
553 *
554 * This function implements the Reset service of the simple text output
555 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
556 * for details.
557 *
558 * @this: pointer to the protocol instance
559 * @extended_verification: if set an extended verification may be executed
560 * Return: status code
561 */
efi_cout_reset(struct efi_simple_text_output_protocol * this,char extended_verification)562 static efi_status_t EFIAPI efi_cout_reset(
563 struct efi_simple_text_output_protocol *this,
564 char extended_verification)
565 {
566 EFI_ENTRY("%p, %d", this, extended_verification);
567
568 /* Set default colors */
569 efi_con_mode.attribute = 0x07;
570 printf(ESC "[0;37;40m");
571 /* Clear screen */
572 efi_clear_screen();
573
574 return EFI_EXIT(EFI_SUCCESS);
575 }
576
577 /**
578 * efi_cout_set_cursor_position() - reset the terminal
579 *
580 * This function implements the SetCursorPosition service of the simple text
581 * output protocol. See the Unified Extensible Firmware Interface (UEFI)
582 * specification for details.
583 *
584 * @this: pointer to the protocol instance
585 * @column: column to move to
586 * @row: row to move to
587 * Return: status code
588 */
efi_cout_set_cursor_position(struct efi_simple_text_output_protocol * this,unsigned long column,unsigned long row)589 static efi_status_t EFIAPI efi_cout_set_cursor_position(
590 struct efi_simple_text_output_protocol *this,
591 unsigned long column, unsigned long row)
592 {
593 efi_status_t ret = EFI_SUCCESS;
594 struct simple_text_output_mode *con = &efi_con_mode;
595 struct cout_mode *mode = &efi_cout_modes[con->mode];
596
597 EFI_ENTRY("%p, %ld, %ld", this, column, row);
598
599 /* Check parameters */
600 if (!this) {
601 ret = EFI_INVALID_PARAMETER;
602 goto out;
603 }
604 if (row >= mode->rows || column >= mode->columns) {
605 ret = EFI_UNSUPPORTED;
606 goto out;
607 }
608
609 /*
610 * Set cursor position by sending CSI H.
611 * EFI origin is [0, 0], terminal origin is [1, 1].
612 */
613 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
614 efi_con_mode.cursor_column = column;
615 efi_con_mode.cursor_row = row;
616 out:
617 return EFI_EXIT(ret);
618 }
619
620 /**
621 * efi_cout_enable_cursor() - enable the cursor
622 *
623 * This function implements the EnableCursor service of the simple text output
624 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
625 * for details.
626 *
627 * @this: pointer to the protocol instance
628 * @enable: if true enable, if false disable the cursor
629 * Return: status code
630 */
efi_cout_enable_cursor(struct efi_simple_text_output_protocol * this,bool enable)631 static efi_status_t EFIAPI efi_cout_enable_cursor(
632 struct efi_simple_text_output_protocol *this,
633 bool enable)
634 {
635 EFI_ENTRY("%p, %d", this, enable);
636
637 printf(ESC"[?25%c", enable ? 'h' : 'l');
638 efi_con_mode.cursor_visible = !!enable;
639
640 return EFI_EXIT(EFI_SUCCESS);
641 }
642
643 struct efi_simple_text_output_protocol efi_con_out = {
644 .reset = efi_cout_reset,
645 .output_string = efi_cout_output_string,
646 .test_string = efi_cout_test_string,
647 .query_mode = efi_cout_query_mode,
648 .set_mode = efi_cout_set_mode,
649 .set_attribute = efi_cout_set_attribute,
650 .clear_screen = efi_cout_clear_screen,
651 .set_cursor_position = efi_cout_set_cursor_position,
652 .enable_cursor = efi_cout_enable_cursor,
653 .mode = (void*)&efi_con_mode,
654 };
655
656 /**
657 * struct efi_cin_notify_function - registered console input notify function
658 *
659 * @link: link to list
660 * @key: key to notify
661 * @function: function to call
662 */
663 struct efi_cin_notify_function {
664 struct list_head link;
665 struct efi_key_data key;
666 efi_status_t (EFIAPI *function)
667 (struct efi_key_data *key_data);
668 };
669
670 static bool key_available;
671 static struct efi_key_data next_key;
672 static LIST_HEAD(cin_notify_functions);
673
674 /**
675 * set_shift_mask() - set shift mask
676 *
677 * @mod: Xterm shift mask
678 * @key_state: receives the state of the shift, alt, control, and logo keys
679 */
set_shift_mask(int mod,struct efi_key_state * key_state)680 static void set_shift_mask(int mod, struct efi_key_state *key_state)
681 {
682 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
683 if (mod) {
684 --mod;
685 if (mod & 1)
686 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
687 if (mod & 2)
688 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
689 if (mod & 4)
690 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
691 if (!mod || (mod & 8))
692 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
693 }
694 }
695
696 /**
697 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
698 *
699 * This gets called when we have already parsed CSI.
700 *
701 * @key_state: receives the state of the shift, alt, control, and logo keys
702 * Return: the unmodified code
703 */
analyze_modifiers(struct efi_key_state * key_state)704 static int analyze_modifiers(struct efi_key_state *key_state)
705 {
706 int c, mod = 0, ret = 0;
707
708 c = getchar();
709
710 if (c != ';') {
711 ret = c;
712 if (c == '~')
713 goto out;
714 c = getchar();
715 }
716 for (;;) {
717 switch (c) {
718 case '0'...'9':
719 mod *= 10;
720 mod += c - '0';
721 /* fall through */
722 case ';':
723 c = getchar();
724 break;
725 default:
726 goto out;
727 }
728 }
729 out:
730 set_shift_mask(mod, key_state);
731 if (!ret)
732 ret = c;
733 return ret;
734 }
735
736 /**
737 * efi_cin_read_key() - read a key from the console input
738 *
739 * @key: - key received
740 * Return: - status code
741 */
efi_cin_read_key(struct efi_key_data * key)742 static efi_status_t efi_cin_read_key(struct efi_key_data *key)
743 {
744 struct efi_input_key pressed_key = {
745 .scan_code = 0,
746 .unicode_char = 0,
747 };
748 s32 ch;
749
750 if (console_read_unicode(&ch))
751 return EFI_NOT_READY;
752
753 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
754 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
755
756 /* We do not support multi-word codes */
757 if (ch >= 0x10000)
758 ch = '?';
759
760 switch (ch) {
761 case 0x1b:
762 /*
763 * If a second key is received within 10 ms, assume that we are
764 * dealing with an escape sequence. Otherwise consider this the
765 * escape key being hit. 10 ms is long enough to work fine at
766 * 1200 baud and above.
767 */
768 udelay(10000);
769 if (!tstc()) {
770 pressed_key.scan_code = 23;
771 break;
772 }
773 /*
774 * Xterm Control Sequences
775 * https://www.xfree86.org/4.8.0/ctlseqs.html
776 */
777 ch = getchar();
778 switch (ch) {
779 case cESC: /* ESC */
780 pressed_key.scan_code = 23;
781 break;
782 case 'O': /* F1 - F4, End */
783 ch = getchar();
784 /* consider modifiers */
785 if (ch == 'F') { /* End */
786 pressed_key.scan_code = 6;
787 break;
788 } else if (ch < 'P') {
789 set_shift_mask(ch - '0', &key->key_state);
790 ch = getchar();
791 }
792 pressed_key.scan_code = ch - 'P' + 11;
793 break;
794 case '[':
795 ch = getchar();
796 switch (ch) {
797 case 'A'...'D': /* up, down right, left */
798 pressed_key.scan_code = ch - 'A' + 1;
799 break;
800 case 'F': /* End */
801 pressed_key.scan_code = 6;
802 break;
803 case 'H': /* Home */
804 pressed_key.scan_code = 5;
805 break;
806 case '1':
807 ch = analyze_modifiers(&key->key_state);
808 switch (ch) {
809 case '1'...'5': /* F1 - F5 */
810 pressed_key.scan_code = ch - '1' + 11;
811 break;
812 case '6'...'9': /* F5 - F8 */
813 pressed_key.scan_code = ch - '6' + 15;
814 break;
815 case 'A'...'D': /* up, down right, left */
816 pressed_key.scan_code = ch - 'A' + 1;
817 break;
818 case 'F': /* End */
819 pressed_key.scan_code = 6;
820 break;
821 case 'H': /* Home */
822 pressed_key.scan_code = 5;
823 break;
824 case '~': /* Home */
825 pressed_key.scan_code = 5;
826 break;
827 }
828 break;
829 case '2':
830 ch = analyze_modifiers(&key->key_state);
831 switch (ch) {
832 case '0'...'1': /* F9 - F10 */
833 pressed_key.scan_code = ch - '0' + 19;
834 break;
835 case '3'...'4': /* F11 - F12 */
836 pressed_key.scan_code = ch - '3' + 21;
837 break;
838 case '~': /* INS */
839 pressed_key.scan_code = 7;
840 break;
841 }
842 break;
843 case '3': /* DEL */
844 pressed_key.scan_code = 8;
845 analyze_modifiers(&key->key_state);
846 break;
847 case '5': /* PG UP */
848 pressed_key.scan_code = 9;
849 analyze_modifiers(&key->key_state);
850 break;
851 case '6': /* PG DOWN */
852 pressed_key.scan_code = 10;
853 analyze_modifiers(&key->key_state);
854 break;
855 } /* [ */
856 break;
857 default:
858 /* ALT key */
859 set_shift_mask(3, &key->key_state);
860 }
861 break;
862 case 0x7f:
863 /* Backspace */
864 ch = 0x08;
865 }
866 if (pressed_key.scan_code) {
867 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
868 } else {
869 pressed_key.unicode_char = ch;
870
871 /*
872 * Assume left control key for control characters typically
873 * entered using the control key.
874 */
875 if (ch >= 0x01 && ch <= 0x1f) {
876 key->key_state.key_shift_state |=
877 EFI_SHIFT_STATE_VALID;
878 switch (ch) {
879 case 0x01 ... 0x07:
880 case 0x0b ... 0x0c:
881 case 0x0e ... 0x1f:
882 key->key_state.key_shift_state |=
883 EFI_LEFT_CONTROL_PRESSED;
884 }
885 }
886 }
887 key->key = pressed_key;
888
889 return EFI_SUCCESS;
890 }
891
892 /**
893 * efi_cin_notify() - notify registered functions
894 */
efi_cin_notify(void)895 static void efi_cin_notify(void)
896 {
897 struct efi_cin_notify_function *item;
898
899 list_for_each_entry(item, &cin_notify_functions, link) {
900 bool match = true;
901
902 /* We do not support toggle states */
903 if (item->key.key.unicode_char || item->key.key.scan_code) {
904 if (item->key.key.unicode_char !=
905 next_key.key.unicode_char ||
906 item->key.key.scan_code != next_key.key.scan_code)
907 match = false;
908 }
909 if (item->key.key_state.key_shift_state &&
910 item->key.key_state.key_shift_state !=
911 next_key.key_state.key_shift_state)
912 match = false;
913
914 if (match)
915 /* We don't bother about the return code */
916 EFI_CALL(item->function(&next_key));
917 }
918 }
919
920 /**
921 * efi_cin_check() - check if keyboard input is available
922 */
efi_cin_check(void)923 static void efi_cin_check(void)
924 {
925 efi_status_t ret;
926
927 if (key_available) {
928 efi_signal_event(efi_con_in.wait_for_key);
929 return;
930 }
931
932 if (tstc()) {
933 ret = efi_cin_read_key(&next_key);
934 if (ret == EFI_SUCCESS) {
935 key_available = true;
936
937 /* Notify registered functions */
938 efi_cin_notify();
939
940 /* Queue the wait for key event */
941 if (key_available)
942 efi_signal_event(efi_con_in.wait_for_key);
943 }
944 }
945 }
946
947 /**
948 * efi_cin_empty_buffer() - empty input buffer
949 */
efi_cin_empty_buffer(void)950 static void efi_cin_empty_buffer(void)
951 {
952 while (tstc())
953 getchar();
954 key_available = false;
955 }
956
957 /**
958 * efi_cin_reset_ex() - reset console input
959 *
960 * @this: - the extended simple text input protocol
961 * @extended_verification: - extended verification
962 *
963 * This function implements the reset service of the
964 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
965 *
966 * See the Unified Extensible Firmware Interface (UEFI) specification for
967 * details.
968 *
969 * Return: old value of the task priority level
970 */
efi_cin_reset_ex(struct efi_simple_text_input_ex_protocol * this,bool extended_verification)971 static efi_status_t EFIAPI efi_cin_reset_ex(
972 struct efi_simple_text_input_ex_protocol *this,
973 bool extended_verification)
974 {
975 efi_status_t ret = EFI_SUCCESS;
976
977 EFI_ENTRY("%p, %d", this, extended_verification);
978
979 /* Check parameters */
980 if (!this) {
981 ret = EFI_INVALID_PARAMETER;
982 goto out;
983 }
984
985 efi_cin_empty_buffer();
986 out:
987 return EFI_EXIT(ret);
988 }
989
990 /**
991 * efi_cin_read_key_stroke_ex() - read key stroke
992 *
993 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
994 * @key_data: key read from console
995 * Return: status code
996 *
997 * This function implements the ReadKeyStrokeEx service of the
998 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
999 *
1000 * See the Unified Extensible Firmware Interface (UEFI) specification for
1001 * details.
1002 */
efi_cin_read_key_stroke_ex(struct efi_simple_text_input_ex_protocol * this,struct efi_key_data * key_data)1003 static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
1004 struct efi_simple_text_input_ex_protocol *this,
1005 struct efi_key_data *key_data)
1006 {
1007 efi_status_t ret = EFI_SUCCESS;
1008
1009 EFI_ENTRY("%p, %p", this, key_data);
1010
1011 /* Check parameters */
1012 if (!this || !key_data) {
1013 ret = EFI_INVALID_PARAMETER;
1014 goto out;
1015 }
1016
1017 /* We don't do interrupts, so check for timers cooperatively */
1018 efi_timer_check();
1019
1020 /* Enable console input after ExitBootServices */
1021 efi_cin_check();
1022
1023 if (!key_available) {
1024 memset(key_data, 0, sizeof(struct efi_key_data));
1025 ret = EFI_NOT_READY;
1026 goto out;
1027 }
1028 /*
1029 * CTRL+A - CTRL+Z have to be signaled as a - z.
1030 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
1031 * CTRL+\ - CTRL+_ have to be signaled as \ - _.
1032 */
1033 switch (next_key.key.unicode_char) {
1034 case 0x01 ... 0x07:
1035 case 0x0b ... 0x0c:
1036 case 0x0e ... 0x1a:
1037 if (!(next_key.key_state.key_toggle_state &
1038 EFI_CAPS_LOCK_ACTIVE) ^
1039 !(next_key.key_state.key_shift_state &
1040 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
1041 next_key.key.unicode_char += 0x40;
1042 else
1043 next_key.key.unicode_char += 0x60;
1044 break;
1045 case 0x1c ... 0x1f:
1046 next_key.key.unicode_char += 0x40;
1047 }
1048 *key_data = next_key;
1049 key_available = false;
1050 efi_con_in.wait_for_key->is_signaled = false;
1051
1052 out:
1053 return EFI_EXIT(ret);
1054 }
1055
1056 /**
1057 * efi_cin_set_state() - set toggle key state
1058 *
1059 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1060 * @key_toggle_state: pointer to key toggle state
1061 * Return: status code
1062 *
1063 * This function implements the SetState service of the
1064 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1065 *
1066 * See the Unified Extensible Firmware Interface (UEFI) specification for
1067 * details.
1068 */
efi_cin_set_state(struct efi_simple_text_input_ex_protocol * this,u8 * key_toggle_state)1069 static efi_status_t EFIAPI efi_cin_set_state(
1070 struct efi_simple_text_input_ex_protocol *this,
1071 u8 *key_toggle_state)
1072 {
1073 EFI_ENTRY("%p, %p", this, key_toggle_state);
1074 /*
1075 * U-Boot supports multiple console input sources like serial and
1076 * net console for which a key toggle state cannot be set at all.
1077 *
1078 * According to the UEFI specification it is allowable to not implement
1079 * this service.
1080 */
1081 return EFI_EXIT(EFI_UNSUPPORTED);
1082 }
1083
1084 /**
1085 * efi_cin_register_key_notify() - register key notification function
1086 *
1087 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1088 * @key_data: key to be notified
1089 * @key_notify_function: function to be called if the key is pressed
1090 * @notify_handle: handle for unregistering the notification
1091 * Return: status code
1092 *
1093 * This function implements the SetState service of the
1094 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1095 *
1096 * See the Unified Extensible Firmware Interface (UEFI) specification for
1097 * details.
1098 */
efi_cin_register_key_notify(struct efi_simple_text_input_ex_protocol * this,struct efi_key_data * key_data,efi_status_t (EFIAPI * key_notify_function)(struct efi_key_data * key_data),void ** notify_handle)1099 static efi_status_t EFIAPI efi_cin_register_key_notify(
1100 struct efi_simple_text_input_ex_protocol *this,
1101 struct efi_key_data *key_data,
1102 efi_status_t (EFIAPI *key_notify_function)(
1103 struct efi_key_data *key_data),
1104 void **notify_handle)
1105 {
1106 efi_status_t ret = EFI_SUCCESS;
1107 struct efi_cin_notify_function *notify_function;
1108
1109 EFI_ENTRY("%p, %p, %p, %p",
1110 this, key_data, key_notify_function, notify_handle);
1111
1112 /* Check parameters */
1113 if (!this || !key_data || !key_notify_function || !notify_handle) {
1114 ret = EFI_INVALID_PARAMETER;
1115 goto out;
1116 }
1117
1118 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
1119 key_data->key.unicode_char,
1120 key_data->key.scan_code,
1121 key_data->key_state.key_shift_state,
1122 key_data->key_state.key_toggle_state);
1123
1124 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
1125 if (!notify_function) {
1126 ret = EFI_OUT_OF_RESOURCES;
1127 goto out;
1128 }
1129 notify_function->key = *key_data;
1130 notify_function->function = key_notify_function;
1131 list_add_tail(¬ify_function->link, &cin_notify_functions);
1132 *notify_handle = notify_function;
1133 out:
1134 return EFI_EXIT(ret);
1135 }
1136
1137 /**
1138 * efi_cin_unregister_key_notify() - unregister key notification function
1139 *
1140 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1141 * @notification_handle: handle received when registering
1142 * Return: status code
1143 *
1144 * This function implements the SetState service of the
1145 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1146 *
1147 * See the Unified Extensible Firmware Interface (UEFI) specification for
1148 * details.
1149 */
efi_cin_unregister_key_notify(struct efi_simple_text_input_ex_protocol * this,void * notification_handle)1150 static efi_status_t EFIAPI efi_cin_unregister_key_notify(
1151 struct efi_simple_text_input_ex_protocol *this,
1152 void *notification_handle)
1153 {
1154 efi_status_t ret = EFI_INVALID_PARAMETER;
1155 struct efi_cin_notify_function *item, *notify_function =
1156 notification_handle;
1157
1158 EFI_ENTRY("%p, %p", this, notification_handle);
1159
1160 /* Check parameters */
1161 if (!this || !notification_handle)
1162 goto out;
1163
1164 list_for_each_entry(item, &cin_notify_functions, link) {
1165 if (item == notify_function) {
1166 ret = EFI_SUCCESS;
1167 break;
1168 }
1169 }
1170 if (ret != EFI_SUCCESS)
1171 goto out;
1172
1173 /* Remove the notify function */
1174 list_del(¬ify_function->link);
1175 free(notify_function);
1176 out:
1177 return EFI_EXIT(ret);
1178 }
1179
1180
1181 /**
1182 * efi_cin_reset() - drain the input buffer
1183 *
1184 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1185 * @extended_verification: allow for exhaustive verification
1186 * Return: status code
1187 *
1188 * This function implements the Reset service of the
1189 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1190 *
1191 * See the Unified Extensible Firmware Interface (UEFI) specification for
1192 * details.
1193 */
efi_cin_reset(struct efi_simple_text_input_protocol * this,bool extended_verification)1194 static efi_status_t EFIAPI efi_cin_reset
1195 (struct efi_simple_text_input_protocol *this,
1196 bool extended_verification)
1197 {
1198 efi_status_t ret = EFI_SUCCESS;
1199
1200 EFI_ENTRY("%p, %d", this, extended_verification);
1201
1202 /* Check parameters */
1203 if (!this) {
1204 ret = EFI_INVALID_PARAMETER;
1205 goto out;
1206 }
1207
1208 efi_cin_empty_buffer();
1209 out:
1210 return EFI_EXIT(ret);
1211 }
1212
1213 /**
1214 * efi_cin_read_key_stroke() - read key stroke
1215 *
1216 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1217 * @key: key read from console
1218 * Return: status code
1219 *
1220 * This function implements the ReadKeyStroke service of the
1221 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1222 *
1223 * See the Unified Extensible Firmware Interface (UEFI) specification for
1224 * details.
1225 */
efi_cin_read_key_stroke(struct efi_simple_text_input_protocol * this,struct efi_input_key * key)1226 static efi_status_t EFIAPI efi_cin_read_key_stroke
1227 (struct efi_simple_text_input_protocol *this,
1228 struct efi_input_key *key)
1229 {
1230 efi_status_t ret = EFI_SUCCESS;
1231
1232 EFI_ENTRY("%p, %p", this, key);
1233
1234 /* Check parameters */
1235 if (!this || !key) {
1236 ret = EFI_INVALID_PARAMETER;
1237 goto out;
1238 }
1239
1240 /* We don't do interrupts, so check for timers cooperatively */
1241 efi_timer_check();
1242
1243 /* Enable console input after ExitBootServices */
1244 efi_cin_check();
1245
1246 if (!key_available) {
1247 ret = EFI_NOT_READY;
1248 goto out;
1249 }
1250 *key = next_key.key;
1251 key_available = false;
1252 efi_con_in.wait_for_key->is_signaled = false;
1253 out:
1254 return EFI_EXIT(ret);
1255 }
1256
1257 static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1258 .reset = efi_cin_reset_ex,
1259 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1260 .wait_for_key_ex = NULL,
1261 .set_state = efi_cin_set_state,
1262 .register_key_notify = efi_cin_register_key_notify,
1263 .unregister_key_notify = efi_cin_unregister_key_notify,
1264 };
1265
1266 struct efi_simple_text_input_protocol efi_con_in = {
1267 .reset = efi_cin_reset,
1268 .read_key_stroke = efi_cin_read_key_stroke,
1269 .wait_for_key = NULL,
1270 };
1271
1272 static struct efi_event *console_timer_event;
1273
1274 /*
1275 * efi_console_timer_notify() - notify the console timer event
1276 *
1277 * @event: console timer event
1278 * @context: not used
1279 */
efi_console_timer_notify(struct efi_event * event,void * context)1280 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1281 void *context)
1282 {
1283 EFI_ENTRY("%p, %p", event, context);
1284 efi_cin_check();
1285 EFI_EXIT(EFI_SUCCESS);
1286 }
1287
1288 /**
1289 * efi_key_notify() - notify the wait for key event
1290 *
1291 * @event: wait for key event
1292 * @context: not used
1293 */
efi_key_notify(struct efi_event * event,void * context)1294 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1295 {
1296 EFI_ENTRY("%p, %p", event, context);
1297 efi_cin_check();
1298 EFI_EXIT(EFI_SUCCESS);
1299 }
1300
1301 /**
1302 * efi_console_register() - install the console protocols
1303 *
1304 * This function is called from do_bootefi_exec().
1305 *
1306 * Return: status code
1307 */
efi_console_register(void)1308 efi_status_t efi_console_register(void)
1309 {
1310 efi_status_t r;
1311 struct efi_device_path *dp;
1312
1313 /* Install protocols on root node */
1314 r = efi_install_multiple_protocol_interfaces(&efi_root,
1315 &efi_guid_text_output_protocol,
1316 &efi_con_out,
1317 &efi_guid_text_input_protocol,
1318 &efi_con_in,
1319 &efi_guid_text_input_ex_protocol,
1320 &efi_con_in_ex,
1321 NULL);
1322
1323 /* Create console node and install device path protocols */
1324 if (CONFIG_IS_ENABLED(DM_SERIAL)) {
1325 dp = efi_dp_from_uart();
1326 if (!dp)
1327 goto out_of_memory;
1328
1329 /* Hook UART up to the device list */
1330 efi_add_handle(&uart_obj);
1331
1332 /* Install device path */
1333 r = efi_add_protocol(&uart_obj, &efi_guid_device_path, dp);
1334 if (r != EFI_SUCCESS)
1335 goto out_of_memory;
1336 }
1337
1338 /* Create console events */
1339 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1340 NULL, NULL, &efi_con_in.wait_for_key);
1341 if (r != EFI_SUCCESS) {
1342 printf("ERROR: Failed to register WaitForKey event\n");
1343 return r;
1344 }
1345 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
1346 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
1347 efi_console_timer_notify, NULL, NULL,
1348 &console_timer_event);
1349 if (r != EFI_SUCCESS) {
1350 printf("ERROR: Failed to register console event\n");
1351 return r;
1352 }
1353 /* 5000 ns cycle is sufficient for 2 MBaud */
1354 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1355 if (r != EFI_SUCCESS)
1356 printf("ERROR: Failed to set console timer\n");
1357 return r;
1358 out_of_memory:
1359 printf("ERROR: Out of memory\n");
1360 return r;
1361 }
1362
1363 /**
1364 * efi_console_get_u16_string() - get user input string
1365 *
1366 * @cin: protocol interface to EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1367 * @buf: buffer to store user input string in UTF16
1368 * @count: number of u16 string including NULL terminator that buf has
1369 * @filter_func: callback to filter user input
1370 * @row: row number to locate user input form
1371 * @col: column number to locate user input form
1372 * Return: status code
1373 */
efi_console_get_u16_string(struct efi_simple_text_input_protocol * cin,u16 * buf,efi_uintn_t count,efi_console_filter_func filter_func,int row,int col)1374 efi_status_t efi_console_get_u16_string(struct efi_simple_text_input_protocol *cin,
1375 u16 *buf, efi_uintn_t count,
1376 efi_console_filter_func filter_func,
1377 int row, int col)
1378 {
1379 efi_status_t ret;
1380 efi_uintn_t len = 0;
1381 struct efi_input_key key;
1382
1383 printf(ANSI_CURSOR_POSITION
1384 ANSI_CLEAR_LINE_TO_END
1385 ANSI_CURSOR_SHOW, row, col);
1386
1387 efi_cin_empty_buffer();
1388
1389 for (;;) {
1390 do {
1391 ret = EFI_CALL(cin->read_key_stroke(cin, &key));
1392 mdelay(10);
1393 } while (ret == EFI_NOT_READY);
1394
1395 if (key.unicode_char == u'\b') {
1396 if (len > 0)
1397 buf[--len] = u'\0';
1398
1399 printf(ANSI_CURSOR_POSITION
1400 "%ls"
1401 ANSI_CLEAR_LINE_TO_END, row, col, buf);
1402 continue;
1403 } else if (key.unicode_char == u'\r') {
1404 buf[len] = u'\0';
1405 return EFI_SUCCESS;
1406 } else if (key.unicode_char == 0x3 || key.scan_code == 23) {
1407 return EFI_ABORTED;
1408 } else if (key.unicode_char < 0x20) {
1409 /* ignore control codes other than Ctrl+C, '\r' and '\b' */
1410 continue;
1411 } else if (key.scan_code != 0) {
1412 /* only accept single ESC press for cancel */
1413 continue;
1414 }
1415
1416 if (filter_func) {
1417 if (filter_func(&key) != EFI_SUCCESS)
1418 continue;
1419 }
1420
1421 if (len >= (count - 1))
1422 continue;
1423
1424 buf[len] = key.unicode_char;
1425 len++;
1426 printf(ANSI_CURSOR_POSITION "%ls", row, col, buf);
1427 }
1428 }
1429