1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application console interface
5 * Copyright (c) 2016 Alexander Graf
8 #define LOG_CATEGORY LOGC_EFI
15 #include <dm/device.h>
16 #include <efi_loader.h>
19 #include <stdio_dev.h>
20 #include <video_console.h>
21 #include <linux/delay.h>
23 #define EFI_COUT_MODE_2 2
24 #define EFI_MAX_COUT_MODE 3
27 unsigned long columns;
32 __maybe_unused static struct efi_object uart_obj;
34 static struct cout_mode efi_cout_modes[] = {
35 /* EFI Mode 0 is 80x25 and always present */
41 /* EFI Mode 1 is always 80x50 */
47 /* Value are unknown until we query the console */
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;
66 * efi_con_mode - mode information of the Simple Text Output Protocol
68 * Use safe settings before efi_setup_console_size() is called.
69 * By default enable only the 80x25 mode which must always exist.
71 static struct simple_text_output_mode efi_con_mode = {
81 * term_get_char() - read a character from the console
83 * Wait for up to 100 ms to read a character from the console.
85 * @c: pointer to the buffer to receive the character
86 * Return: 0 on success, 1 otherwise
88 static int term_get_char(s32 *c)
92 /* Wait up to 100 ms for a character */
93 timeout = timer_get_us() + 100000;
96 if (timer_get_us() > timeout)
104 * Receive and parse a reply from the terminal.
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
111 static int term_read_reply(int *n, int num, char end_char)
116 if (term_get_char(&c) || c != cESC)
119 if (term_get_char(&c) || c != '[')
124 if (!term_get_char(&c)) {
131 } else if (c == end_char) {
133 } else if (c > '9' || c < '0') {
137 /* Read one more decimal position */
151 * efi_cout_output_string() - write Unicode string to console
153 * This function implements the OutputString service of the simple text output
154 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
157 * @this: simple text output protocol
158 * @string: u16 string
159 * Return: status code
161 static efi_status_t EFIAPI efi_cout_output_string(
162 struct efi_simple_text_output_protocol *this,
165 struct simple_text_output_mode *con = &efi_con_mode;
166 struct cout_mode *mode = &efi_cout_modes[con->mode];
169 efi_status_t ret = EFI_SUCCESS;
171 EFI_ENTRY("%p, %p", this, string);
173 if (!this || !string) {
174 ret = EFI_INVALID_PARAMETER;
178 buf = malloc(utf16_utf8_strlen(string) + 1);
180 ret = EFI_OUT_OF_RESOURCES;
184 utf16_utf8_strcpy(&pos, string);
189 * Update the cursor position.
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.
195 for (p = string; *p; ++p) {
197 case '\b': /* U+0008, backspace */
198 if (con->cursor_column)
199 con->cursor_column--;
201 case '\n': /* U+000A, newline */
202 con->cursor_column = 0;
205 case '\r': /* U+000D, carriage-return */
206 con->cursor_column = 0;
208 case 0xd800 ... 0xdbff:
210 * Ignore high surrogates, we do not want to count a
211 * Unicode character twice.
215 /* Exclude control codes */
217 con->cursor_column++;
220 if (con->cursor_column >= mode->columns) {
221 con->cursor_column = 0;
225 * When we exceed the row count the terminal will scroll up one
226 * line. We have to adjust the cursor position.
228 if (con->cursor_row >= mode->rows && con->cursor_row)
233 return EFI_EXIT(ret);
237 * efi_cout_test_string() - test writing Unicode string to console
239 * This function implements the TestString service of the simple text output
240 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
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.
246 * @this: simple text output protocol
247 * @string: u16 string
248 * Return: status code
250 static efi_status_t EFIAPI efi_cout_test_string(
251 struct efi_simple_text_output_protocol *this,
254 EFI_ENTRY("%p, %p", this, string);
255 return EFI_EXIT(EFI_SUCCESS);
259 * cout_mode_matches() - check if mode has given terminal size
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
267 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
272 return (mode->rows == rows) && (mode->columns == cols);
276 * query_console_serial() - query serial console size
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.
281 * @rows: pointer to return number of rows
282 * @cols: pointer to return number of columns
283 * Returns: 0 on success
285 static int query_console_serial(int *rows, int *cols)
290 /* Empty input buffer */
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.
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.
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 */
308 /* Read {rows,cols} */
309 if (term_read_reply(n, 2, 'R')) {
317 printf(ESC "8"); /* Restore cursor position */
322 * query_vidconsole() - query video console size
325 * @rows: pointer to return number of rows
326 * @cols: pointer to return number of columns
327 * Returns: 0 on success
329 static int __maybe_unused query_vidconsole(int *rows, int *cols)
331 const char *stdout_name = env_get("stdout");
332 struct stdio_dev *stdout_dev;
334 struct vidconsole_priv *priv;
336 if (!stdout_name || strncmp(stdout_name, "vidconsole", 10))
338 stdout_dev = stdio_get_by_name("vidconsole");
341 dev = stdout_dev->priv;
344 priv = dev_get_uclass_priv(dev);
353 * efi_setup_console_size() - update the mode table.
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.
359 void efi_setup_console_size(void)
361 int rows = 25, cols = 80;
364 if (IS_ENABLED(CONFIG_VIDEO))
365 ret = query_vidconsole(&rows, &cols);
367 ret = query_console_serial(&rows, &cols);
371 log_debug("Console size %dx%d\n", rows, cols);
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;
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
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;
394 * efi_cout_query_mode() - get terminal size for a text mode
396 * This function implements the QueryMode service of the simple text output
397 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
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
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,
411 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
413 if (mode_number >= efi_con_mode.max_mode)
414 return EFI_EXIT(EFI_UNSUPPORTED);
416 if (efi_cout_modes[mode_number].present != 1)
417 return EFI_EXIT(EFI_UNSUPPORTED);
420 *columns = efi_cout_modes[mode_number].columns;
422 *rows = efi_cout_modes[mode_number].rows;
424 return EFI_EXIT(EFI_SUCCESS);
427 static const struct {
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 */
442 * efi_cout_set_attribute() - set fore- and background color
444 * This function implements the SetAttribute service of the simple text output
445 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
448 * @this: simple text output protocol
449 * @attribute: foreground color - bits 0-3, background color - bits 4-6
450 * Return: status code
452 static efi_status_t EFIAPI efi_cout_set_attribute(
453 struct efi_simple_text_output_protocol *this,
454 unsigned long attribute)
456 unsigned int bold = EFI_ATTR_BOLD(attribute);
457 unsigned int fg = EFI_ATTR_FG(attribute);
458 unsigned int bg = EFI_ATTR_BG(attribute);
460 EFI_ENTRY("%p, %lx", this, attribute);
462 efi_con_mode.attribute = attribute;
464 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
466 printf(ESC"[0;37;40m");
468 return EFI_EXIT(EFI_SUCCESS);
472 * efi_clear_screen() - clear screen
474 static void efi_clear_screen(void)
476 if (CONFIG_IS_ENABLED(EFI_SCROLL_ON_CLEAR_SCREEN)) {
477 unsigned int row, screen_rows, screen_columns;
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++)
488 * The Linux console wants both a clear and a home command. The video
489 * uclass does not support <ESC>[H without coordinates, yet.
491 printf(ESC "[2J" ESC "[1;1H");
492 efi_con_mode.cursor_column = 0;
493 efi_con_mode.cursor_row = 0;
497 * efi_cout_clear_screen() - clear screen
499 * This function implements the ClearScreen service of the simple text output
500 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
503 * @this: pointer to the protocol instance
504 * Return: status code
506 static efi_status_t EFIAPI efi_cout_clear_screen(
507 struct efi_simple_text_output_protocol *this)
509 EFI_ENTRY("%p", this);
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");
519 return EFI_EXIT(EFI_SUCCESS);
523 * efi_cout_clear_set_mode() - set text model
525 * This function implements the SetMode service of the simple text output
526 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
529 * @this: pointer to the protocol instance
530 * @mode_number: number of the text mode to set
531 * Return: status code
533 static efi_status_t EFIAPI efi_cout_set_mode(
534 struct efi_simple_text_output_protocol *this,
535 unsigned long mode_number)
537 EFI_ENTRY("%p, %ld", this, mode_number);
539 if (mode_number >= efi_con_mode.max_mode)
540 return EFI_EXIT(EFI_UNSUPPORTED);
542 if (!efi_cout_modes[mode_number].present)
543 return EFI_EXIT(EFI_UNSUPPORTED);
545 efi_con_mode.mode = mode_number;
548 return EFI_EXIT(EFI_SUCCESS);
552 * efi_cout_reset() - reset the terminal
554 * This function implements the Reset service of the simple text output
555 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
558 * @this: pointer to the protocol instance
559 * @extended_verification: if set an extended verification may be executed
560 * Return: status code
562 static efi_status_t EFIAPI efi_cout_reset(
563 struct efi_simple_text_output_protocol *this,
564 char extended_verification)
566 EFI_ENTRY("%p, %d", this, extended_verification);
568 /* Set default colors */
569 efi_con_mode.attribute = 0x07;
570 printf(ESC "[0;37;40m");
574 return EFI_EXIT(EFI_SUCCESS);
578 * efi_cout_set_cursor_position() - reset the terminal
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.
584 * @this: pointer to the protocol instance
585 * @column: column to move to
586 * @row: row to move to
587 * Return: status code
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)
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];
597 EFI_ENTRY("%p, %ld, %ld", this, column, row);
599 /* Check parameters */
601 ret = EFI_INVALID_PARAMETER;
604 if (row >= mode->rows || column >= mode->columns) {
605 ret = EFI_UNSUPPORTED;
610 * Set cursor position by sending CSI H.
611 * EFI origin is [0, 0], terminal origin is [1, 1].
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;
617 return EFI_EXIT(ret);
621 * efi_cout_enable_cursor() - enable the cursor
623 * This function implements the EnableCursor service of the simple text output
624 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
627 * @this: pointer to the protocol instance
628 * @enable: if true enable, if false disable the cursor
629 * Return: status code
631 static efi_status_t EFIAPI efi_cout_enable_cursor(
632 struct efi_simple_text_output_protocol *this,
635 EFI_ENTRY("%p, %d", this, enable);
637 printf(ESC"[?25%c", enable ? 'h' : 'l');
638 efi_con_mode.cursor_visible = !!enable;
640 return EFI_EXIT(EFI_SUCCESS);
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,
657 * struct efi_cin_notify_function - registered console input notify function
659 * @link: link to list
660 * @key: key to notify
661 * @function: function to call
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);
670 static bool key_available;
671 static struct efi_key_data next_key;
672 static LIST_HEAD(cin_notify_functions);
675 * set_shift_mask() - set shift mask
677 * @mod: Xterm shift mask
678 * @key_state: receives the state of the shift, alt, control, and logo keys
680 static void set_shift_mask(int mod, struct efi_key_state *key_state)
682 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
686 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
688 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
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;
697 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
699 * This gets called when we have already parsed CSI.
701 * @key_state: receives the state of the shift, alt, control, and logo keys
702 * Return: the unmodified code
704 static int analyze_modifiers(struct efi_key_state *key_state)
706 int c, mod = 0, ret = 0;
730 set_shift_mask(mod, key_state);
737 * efi_cin_read_key() - read a key from the console input
739 * @key: - key received
740 * Return: - status code
742 static efi_status_t efi_cin_read_key(struct efi_key_data *key)
744 struct efi_input_key pressed_key = {
750 if (console_read_unicode(&ch))
751 return EFI_NOT_READY;
753 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
754 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
756 /* We do not support multi-word codes */
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.
770 pressed_key.scan_code = 23;
774 * Xterm Control Sequences
775 * https://www.xfree86.org/4.8.0/ctlseqs.html
780 pressed_key.scan_code = 23;
782 case 'O': /* F1 - F4, End */
784 /* consider modifiers */
785 if (ch == 'F') { /* End */
786 pressed_key.scan_code = 6;
788 } else if (ch < 'P') {
789 set_shift_mask(ch - '0', &key->key_state);
792 pressed_key.scan_code = ch - 'P' + 11;
797 case 'A'...'D': /* up, down right, left */
798 pressed_key.scan_code = ch - 'A' + 1;
801 pressed_key.scan_code = 6;
804 pressed_key.scan_code = 5;
807 ch = analyze_modifiers(&key->key_state);
809 case '1'...'5': /* F1 - F5 */
810 pressed_key.scan_code = ch - '1' + 11;
812 case '6'...'9': /* F5 - F8 */
813 pressed_key.scan_code = ch - '6' + 15;
815 case 'A'...'D': /* up, down right, left */
816 pressed_key.scan_code = ch - 'A' + 1;
819 pressed_key.scan_code = 6;
822 pressed_key.scan_code = 5;
825 pressed_key.scan_code = 5;
830 ch = analyze_modifiers(&key->key_state);
832 case '0'...'1': /* F9 - F10 */
833 pressed_key.scan_code = ch - '0' + 19;
835 case '3'...'4': /* F11 - F12 */
836 pressed_key.scan_code = ch - '3' + 21;
839 pressed_key.scan_code = 7;
844 pressed_key.scan_code = 8;
845 analyze_modifiers(&key->key_state);
847 case '5': /* PG UP */
848 pressed_key.scan_code = 9;
849 analyze_modifiers(&key->key_state);
851 case '6': /* PG DOWN */
852 pressed_key.scan_code = 10;
853 analyze_modifiers(&key->key_state);
859 set_shift_mask(3, &key->key_state);
866 if (pressed_key.scan_code) {
867 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
869 pressed_key.unicode_char = ch;
872 * Assume left control key for control characters typically
873 * entered using the control key.
875 if (ch >= 0x01 && ch <= 0x1f) {
876 key->key_state.key_shift_state |=
877 EFI_SHIFT_STATE_VALID;
882 key->key_state.key_shift_state |=
883 EFI_LEFT_CONTROL_PRESSED;
887 key->key = pressed_key;
893 * efi_cin_notify() - notify registered functions
895 static void efi_cin_notify(void)
897 struct efi_cin_notify_function *item;
899 list_for_each_entry(item, &cin_notify_functions, link) {
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)
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)
915 /* We don't bother about the return code */
916 EFI_CALL(item->function(&next_key));
921 * efi_cin_check() - check if keyboard input is available
923 static void efi_cin_check(void)
928 efi_signal_event(efi_con_in.wait_for_key);
933 ret = efi_cin_read_key(&next_key);
934 if (ret == EFI_SUCCESS) {
935 key_available = true;
937 /* Notify registered functions */
940 /* Queue the wait for key event */
942 efi_signal_event(efi_con_in.wait_for_key);
948 * efi_cin_empty_buffer() - empty input buffer
950 static void efi_cin_empty_buffer(void)
954 key_available = false;
958 * efi_cin_reset_ex() - reset console input
960 * @this: - the extended simple text input protocol
961 * @extended_verification: - extended verification
963 * This function implements the reset service of the
964 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
966 * See the Unified Extensible Firmware Interface (UEFI) specification for
969 * Return: old value of the task priority level
971 static efi_status_t EFIAPI efi_cin_reset_ex(
972 struct efi_simple_text_input_ex_protocol *this,
973 bool extended_verification)
975 efi_status_t ret = EFI_SUCCESS;
977 EFI_ENTRY("%p, %d", this, extended_verification);
979 /* Check parameters */
981 ret = EFI_INVALID_PARAMETER;
985 efi_cin_empty_buffer();
987 return EFI_EXIT(ret);
991 * efi_cin_read_key_stroke_ex() - read key stroke
993 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
994 * @key_data: key read from console
995 * Return: status code
997 * This function implements the ReadKeyStrokeEx service of the
998 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1000 * See the Unified Extensible Firmware Interface (UEFI) specification for
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)
1007 efi_status_t ret = EFI_SUCCESS;
1009 EFI_ENTRY("%p, %p", this, key_data);
1011 /* Check parameters */
1012 if (!this || !key_data) {
1013 ret = EFI_INVALID_PARAMETER;
1017 /* We don't do interrupts, so check for timers cooperatively */
1020 /* Enable console input after ExitBootServices */
1023 if (!key_available) {
1024 memset(key_data, 0, sizeof(struct efi_key_data));
1025 ret = EFI_NOT_READY;
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 \ - _.
1033 switch (next_key.key.unicode_char) {
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;
1043 next_key.key.unicode_char += 0x60;
1046 next_key.key.unicode_char += 0x40;
1048 *key_data = next_key;
1049 key_available = false;
1050 efi_con_in.wait_for_key->is_signaled = false;
1053 return EFI_EXIT(ret);
1057 * efi_cin_set_state() - set toggle key state
1059 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1060 * @key_toggle_state: pointer to key toggle state
1061 * Return: status code
1063 * This function implements the SetState service of the
1064 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1066 * See the Unified Extensible Firmware Interface (UEFI) specification for
1069 static efi_status_t EFIAPI efi_cin_set_state(
1070 struct efi_simple_text_input_ex_protocol *this,
1071 u8 *key_toggle_state)
1073 EFI_ENTRY("%p, %p", this, key_toggle_state);
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.
1078 * According to the UEFI specification it is allowable to not implement
1081 return EFI_EXIT(EFI_UNSUPPORTED);
1085 * efi_cin_register_key_notify() - register key notification function
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
1093 * This function implements the SetState service of the
1094 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1096 * See the Unified Extensible Firmware Interface (UEFI) specification for
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)
1106 efi_status_t ret = EFI_SUCCESS;
1107 struct efi_cin_notify_function *notify_function;
1109 EFI_ENTRY("%p, %p, %p, %p",
1110 this, key_data, key_notify_function, notify_handle);
1112 /* Check parameters */
1113 if (!this || !key_data || !key_notify_function || !notify_handle) {
1114 ret = EFI_INVALID_PARAMETER;
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);
1124 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
1125 if (!notify_function) {
1126 ret = EFI_OUT_OF_RESOURCES;
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;
1134 return EFI_EXIT(ret);
1138 * efi_cin_unregister_key_notify() - unregister key notification function
1140 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1141 * @notification_handle: handle received when registering
1142 * Return: status code
1144 * This function implements the SetState service of the
1145 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1147 * See the Unified Extensible Firmware Interface (UEFI) specification for
1150 static efi_status_t EFIAPI efi_cin_unregister_key_notify(
1151 struct efi_simple_text_input_ex_protocol *this,
1152 void *notification_handle)
1154 efi_status_t ret = EFI_INVALID_PARAMETER;
1155 struct efi_cin_notify_function *item, *notify_function =
1156 notification_handle;
1158 EFI_ENTRY("%p, %p", this, notification_handle);
1160 /* Check parameters */
1161 if (!this || !notification_handle)
1164 list_for_each_entry(item, &cin_notify_functions, link) {
1165 if (item == notify_function) {
1170 if (ret != EFI_SUCCESS)
1173 /* Remove the notify function */
1174 list_del(¬ify_function->link);
1175 free(notify_function);
1177 return EFI_EXIT(ret);
1182 * efi_cin_reset() - drain the input buffer
1184 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1185 * @extended_verification: allow for exhaustive verification
1186 * Return: status code
1188 * This function implements the Reset service of the
1189 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1191 * See the Unified Extensible Firmware Interface (UEFI) specification for
1194 static efi_status_t EFIAPI efi_cin_reset
1195 (struct efi_simple_text_input_protocol *this,
1196 bool extended_verification)
1198 efi_status_t ret = EFI_SUCCESS;
1200 EFI_ENTRY("%p, %d", this, extended_verification);
1202 /* Check parameters */
1204 ret = EFI_INVALID_PARAMETER;
1208 efi_cin_empty_buffer();
1210 return EFI_EXIT(ret);
1214 * efi_cin_read_key_stroke() - read key stroke
1216 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1217 * @key: key read from console
1218 * Return: status code
1220 * This function implements the ReadKeyStroke service of the
1221 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1223 * See the Unified Extensible Firmware Interface (UEFI) specification for
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)
1230 efi_status_t ret = EFI_SUCCESS;
1232 EFI_ENTRY("%p, %p", this, key);
1234 /* Check parameters */
1235 if (!this || !key) {
1236 ret = EFI_INVALID_PARAMETER;
1240 /* We don't do interrupts, so check for timers cooperatively */
1243 /* Enable console input after ExitBootServices */
1246 if (!key_available) {
1247 ret = EFI_NOT_READY;
1250 *key = next_key.key;
1251 key_available = false;
1252 efi_con_in.wait_for_key->is_signaled = false;
1254 return EFI_EXIT(ret);
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,
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,
1272 static struct efi_event *console_timer_event;
1275 * efi_console_timer_notify() - notify the console timer event
1277 * @event: console timer event
1278 * @context: not used
1280 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1283 EFI_ENTRY("%p, %p", event, context);
1285 EFI_EXIT(EFI_SUCCESS);
1289 * efi_key_notify() - notify the wait for key event
1291 * @event: wait for key event
1292 * @context: not used
1294 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1296 EFI_ENTRY("%p, %p", event, context);
1298 EFI_EXIT(EFI_SUCCESS);
1302 * efi_console_register() - install the console protocols
1304 * This function is called from do_bootefi_exec().
1306 * Return: status code
1308 efi_status_t efi_console_register(void)
1311 struct efi_device_path *dp;
1313 /* Install protocols on root node */
1314 r = efi_install_multiple_protocol_interfaces(&efi_root,
1315 &efi_guid_text_output_protocol,
1317 &efi_guid_text_input_protocol,
1319 &efi_guid_text_input_ex_protocol,
1323 /* Create console node and install device path protocols */
1324 if (CONFIG_IS_ENABLED(DM_SERIAL)) {
1325 dp = efi_dp_from_uart();
1329 /* Hook UART up to the device list */
1330 efi_add_handle(&uart_obj);
1332 /* Install device path */
1333 r = efi_add_protocol(&uart_obj, &efi_guid_device_path, dp);
1334 if (r != EFI_SUCCESS)
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");
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");
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");
1359 printf("ERROR: Out of memory\n");
1364 * efi_console_get_u16_string() - get user input string
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
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,
1380 efi_uintn_t len = 0;
1381 struct efi_input_key key;
1383 printf(ANSI_CURSOR_POSITION
1384 ANSI_CLEAR_LINE_TO_END
1385 ANSI_CURSOR_SHOW, row, col);
1387 efi_cin_empty_buffer();
1391 ret = EFI_CALL(cin->read_key_stroke(cin, &key));
1393 } while (ret == EFI_NOT_READY);
1395 if (key.unicode_char == u'\b') {
1399 printf(ANSI_CURSOR_POSITION
1401 ANSI_CLEAR_LINE_TO_END, row, col, buf);
1403 } else if (key.unicode_char == u'\r') {
1406 } else if (key.unicode_char == 0x3 || key.scan_code == 23) {
1408 } else if (key.unicode_char < 0x20) {
1409 /* ignore control codes other than Ctrl+C, '\r' and '\b' */
1411 } else if (key.scan_code != 0) {
1412 /* only accept single ESC press for cancel */
1417 if (filter_func(&key) != EFI_SUCCESS)
1421 if (len >= (count - 1))
1424 buf[len] = key.unicode_char;
1426 printf(ANSI_CURSOR_POSITION "%ls", row, col, buf);