1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application console interface
5 * Copyright (c) 2016 Alexander Graf
8 #define LOG_CATEGORY LOGC_EFI
14 #include <dm/device.h>
15 #include <efi_loader.h>
18 #include <stdio_dev.h>
19 #include <video_console.h>
20 #include <linux/delay.h>
22 #define EFI_COUT_MODE_2 2
23 #define EFI_MAX_COUT_MODE 3
26 unsigned long columns;
31 __maybe_unused static struct efi_object uart_obj;
33 static struct cout_mode efi_cout_modes[] = {
34 /* EFI Mode 0 is 80x25 and always present */
40 /* EFI Mode 1 is always 80x50 */
46 /* Value are unknown until we query the console */
54 const efi_guid_t efi_guid_text_input_ex_protocol =
55 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
56 const efi_guid_t efi_guid_text_input_protocol =
57 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
58 const efi_guid_t efi_guid_text_output_protocol =
59 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
65 * efi_con_mode - mode information of the Simple Text Output Protocol
67 * Use safe settings before efi_setup_console_size() is called.
68 * By default enable only the 80x25 mode which must always exist.
70 static struct simple_text_output_mode efi_con_mode = {
79 static int term_get_char(s32 *c)
83 /* Wait up to 100 ms for a character */
84 timeout = timer_get_us() + 100000;
87 if (timer_get_us() > timeout)
95 * Receive and parse a reply from the terminal.
97 * @n: array of return values
98 * @num: number of return values expected
99 * @end_char: character indicating end of terminal message
100 * Return: non-zero indicates error
102 static int term_read_reply(int *n, int num, char end_char)
107 if (term_get_char(&c) || c != cESC)
110 if (term_get_char(&c) || c != '[')
115 if (!term_get_char(&c)) {
122 } else if (c == end_char) {
124 } else if (c > '9' || c < '0') {
128 /* Read one more decimal position */
142 * efi_cout_output_string() - write Unicode string to console
144 * This function implements the OutputString service of the simple text output
145 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
148 * @this: simple text output protocol
149 * @string: u16 string
150 * Return: status code
152 static efi_status_t EFIAPI efi_cout_output_string(
153 struct efi_simple_text_output_protocol *this,
156 struct simple_text_output_mode *con = &efi_con_mode;
157 struct cout_mode *mode = &efi_cout_modes[con->mode];
160 efi_status_t ret = EFI_SUCCESS;
162 EFI_ENTRY("%p, %p", this, string);
164 if (!this || !string) {
165 ret = EFI_INVALID_PARAMETER;
169 buf = malloc(utf16_utf8_strlen(string) + 1);
171 ret = EFI_OUT_OF_RESOURCES;
175 utf16_utf8_strcpy(&pos, string);
180 * Update the cursor position.
182 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
183 * and U000D. All other control characters are ignored. Any non-control
184 * character increase the column by one.
186 for (p = string; *p; ++p) {
188 case '\b': /* U+0008, backspace */
189 if (con->cursor_column)
190 con->cursor_column--;
192 case '\n': /* U+000A, newline */
193 con->cursor_column = 0;
196 case '\r': /* U+000D, carriage-return */
197 con->cursor_column = 0;
199 case 0xd800 ... 0xdbff:
201 * Ignore high surrogates, we do not want to count a
202 * Unicode character twice.
206 /* Exclude control codes */
208 con->cursor_column++;
211 if (con->cursor_column >= mode->columns) {
212 con->cursor_column = 0;
216 * When we exceed the row count the terminal will scroll up one
217 * line. We have to adjust the cursor position.
219 if (con->cursor_row >= mode->rows && con->cursor_row)
224 return EFI_EXIT(ret);
228 * efi_cout_test_string() - test writing Unicode string to console
230 * This function implements the TestString service of the simple text output
231 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
234 * As in OutputString we simply convert UTF-16 to UTF-8 there are no unsupported
235 * code points and we can always return EFI_SUCCESS.
237 * @this: simple text output protocol
238 * @string: u16 string
239 * Return: status code
241 static efi_status_t EFIAPI efi_cout_test_string(
242 struct efi_simple_text_output_protocol *this,
245 EFI_ENTRY("%p, %p", this, string);
246 return EFI_EXIT(EFI_SUCCESS);
250 * cout_mode_matches() - check if mode has given terminal size
253 * @rows: number of rows
254 * @cols: number of columns
255 * Return: true if number of rows and columns matches the mode and
256 * the mode is present
258 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
263 return (mode->rows == rows) && (mode->columns == cols);
267 * query_console_serial() - query serial console size
269 * When using a serial console or the net console we can only devise the
270 * terminal size by querying the terminal using ECMA-48 control sequences.
272 * @rows: pointer to return number of rows
273 * @cols: pointer to return number of columns
274 * Returns: 0 on success
276 static int query_console_serial(int *rows, int *cols)
281 /* Empty input buffer */
286 * Not all terminals understand CSI [18t for querying the console size.
287 * We should adhere to escape sequences documented in the console_codes
288 * man page and the ECMA-48 standard.
290 * So here we follow a different approach. We position the cursor to the
291 * bottom right and query its position. Before leaving the function we
292 * restore the original cursor position.
294 printf(ESC "7" /* Save cursor position */
295 ESC "[r" /* Set scrolling region to full window */
296 ESC "[999;999H" /* Move to bottom right corner */
297 ESC "[6n"); /* Query cursor position */
299 /* Read {rows,cols} */
300 if (term_read_reply(n, 2, 'R')) {
308 printf(ESC "8"); /* Restore cursor position */
313 * query_vidconsole() - query video console size
316 * @rows: pointer to return number of rows
317 * @cols: pointer to return number of columns
318 * Returns: 0 on success
320 static int __maybe_unused query_vidconsole(int *rows, int *cols)
322 const char *stdout_name = env_get("stdout");
323 struct stdio_dev *stdout_dev;
325 struct vidconsole_priv *priv;
327 if (!stdout_name || strncmp(stdout_name, "vidconsole", 10))
329 stdout_dev = stdio_get_by_name("vidconsole");
332 dev = stdout_dev->priv;
335 priv = dev_get_uclass_priv(dev);
344 * efi_setup_console_size() - update the mode table.
346 * By default the only mode available is 80x25. If the console has at least 50
347 * lines, enable mode 80x50. If we can query the console size and it is neither
348 * 80x25 nor 80x50, set it as an additional mode.
350 void efi_setup_console_size(void)
352 int rows = 25, cols = 80;
355 if (IS_ENABLED(CONFIG_DM_VIDEO))
356 ret = query_vidconsole(&rows, &cols);
358 ret = query_console_serial(&rows, &cols);
362 log_debug("Console size %dx%d\n", rows, cols);
364 /* Test if we can have Mode 1 */
365 if (cols >= 80 && rows >= 50) {
366 efi_cout_modes[1].present = 1;
367 efi_con_mode.max_mode = 2;
371 * Install our mode as mode 2 if it is different
372 * than mode 0 or 1 and set it as the currently selected mode
374 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
375 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
376 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
377 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
378 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
379 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
380 efi_con_mode.mode = EFI_COUT_MODE_2;
385 * efi_cout_query_mode() - get terminal size for a text mode
387 * This function implements the QueryMode service of the simple text output
388 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
391 * @this: simple text output protocol
392 * @mode_number: mode number to retrieve information on
393 * @columns: number of columns
394 * @rows: number of rows
395 * Return: status code
397 static efi_status_t EFIAPI efi_cout_query_mode(
398 struct efi_simple_text_output_protocol *this,
399 unsigned long mode_number, unsigned long *columns,
402 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
404 if (mode_number >= efi_con_mode.max_mode)
405 return EFI_EXIT(EFI_UNSUPPORTED);
407 if (efi_cout_modes[mode_number].present != 1)
408 return EFI_EXIT(EFI_UNSUPPORTED);
411 *columns = efi_cout_modes[mode_number].columns;
413 *rows = efi_cout_modes[mode_number].rows;
415 return EFI_EXIT(EFI_SUCCESS);
418 static const struct {
422 { 30, 40 }, /* 0: black */
423 { 34, 44 }, /* 1: blue */
424 { 32, 42 }, /* 2: green */
425 { 36, 46 }, /* 3: cyan */
426 { 31, 41 }, /* 4: red */
427 { 35, 45 }, /* 5: magenta */
428 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
429 { 37, 47 }, /* 7: light gray, map to white */
433 * efi_cout_set_attribute() - set fore- and background color
435 * This function implements the SetAttribute service of the simple text output
436 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
439 * @this: simple text output protocol
440 * @attribute: foreground color - bits 0-3, background color - bits 4-6
441 * Return: status code
443 static efi_status_t EFIAPI efi_cout_set_attribute(
444 struct efi_simple_text_output_protocol *this,
445 unsigned long attribute)
447 unsigned int bold = EFI_ATTR_BOLD(attribute);
448 unsigned int fg = EFI_ATTR_FG(attribute);
449 unsigned int bg = EFI_ATTR_BG(attribute);
451 EFI_ENTRY("%p, %lx", this, attribute);
453 efi_con_mode.attribute = attribute;
455 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
457 printf(ESC"[0;37;40m");
459 return EFI_EXIT(EFI_SUCCESS);
463 * efi_cout_clear_screen() - clear screen
465 * This function implements the ClearScreen service of the simple text output
466 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
469 * @this: pointer to the protocol instance
470 * Return: status code
472 static efi_status_t EFIAPI efi_cout_clear_screen(
473 struct efi_simple_text_output_protocol *this)
475 EFI_ENTRY("%p", this);
478 * The Linux console wants both a clear and a home command. The video
479 * uclass does not support <ESC>[H without coordinates, yet.
481 printf(ESC "[2J" ESC "[1;1H");
482 efi_con_mode.cursor_column = 0;
483 efi_con_mode.cursor_row = 0;
485 return EFI_EXIT(EFI_SUCCESS);
489 * efi_cout_clear_set_mode() - set text model
491 * This function implements the SetMode service of the simple text output
492 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
495 * @this: pointer to the protocol instance
496 * @mode_number: number of the text mode to set
497 * Return: status code
499 static efi_status_t EFIAPI efi_cout_set_mode(
500 struct efi_simple_text_output_protocol *this,
501 unsigned long mode_number)
503 EFI_ENTRY("%p, %ld", this, mode_number);
505 if (mode_number >= efi_con_mode.max_mode)
506 return EFI_EXIT(EFI_UNSUPPORTED);
508 if (!efi_cout_modes[mode_number].present)
509 return EFI_EXIT(EFI_UNSUPPORTED);
511 efi_con_mode.mode = mode_number;
512 EFI_CALL(efi_cout_clear_screen(this));
514 return EFI_EXIT(EFI_SUCCESS);
518 * efi_cout_reset() - reset the terminal
520 * This function implements the Reset service of the simple text output
521 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
524 * @this: pointer to the protocol instance
525 * @extended_verification: if set an extended verification may be executed
526 * Return: status code
528 static efi_status_t EFIAPI efi_cout_reset(
529 struct efi_simple_text_output_protocol *this,
530 char extended_verification)
532 EFI_ENTRY("%p, %d", this, extended_verification);
534 /* Set default colors */
535 efi_con_mode.attribute = 0x07;
536 printf(ESC "[0;37;40m");
538 EFI_CALL(efi_cout_clear_screen(this));
540 return EFI_EXIT(EFI_SUCCESS);
544 * efi_cout_set_cursor_position() - reset the terminal
546 * This function implements the SetCursorPosition service of the simple text
547 * output protocol. See the Unified Extensible Firmware Interface (UEFI)
548 * specification for details.
550 * @this: pointer to the protocol instance
551 * @column: column to move to
552 * @row: row to move to
553 * Return: status code
555 static efi_status_t EFIAPI efi_cout_set_cursor_position(
556 struct efi_simple_text_output_protocol *this,
557 unsigned long column, unsigned long row)
559 efi_status_t ret = EFI_SUCCESS;
560 struct simple_text_output_mode *con = &efi_con_mode;
561 struct cout_mode *mode = &efi_cout_modes[con->mode];
563 EFI_ENTRY("%p, %ld, %ld", this, column, row);
565 /* Check parameters */
567 ret = EFI_INVALID_PARAMETER;
570 if (row >= mode->rows || column >= mode->columns) {
571 ret = EFI_UNSUPPORTED;
576 * Set cursor position by sending CSI H.
577 * EFI origin is [0, 0], terminal origin is [1, 1].
579 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
580 efi_con_mode.cursor_column = column;
581 efi_con_mode.cursor_row = row;
583 return EFI_EXIT(ret);
587 * efi_cout_enable_cursor() - enable the cursor
589 * This function implements the EnableCursor service of the simple text output
590 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
593 * @this: pointer to the protocol instance
594 * @enable: if true enable, if false disable the cursor
595 * Return: status code
597 static efi_status_t EFIAPI efi_cout_enable_cursor(
598 struct efi_simple_text_output_protocol *this,
601 EFI_ENTRY("%p, %d", this, enable);
603 printf(ESC"[?25%c", enable ? 'h' : 'l');
604 efi_con_mode.cursor_visible = !!enable;
606 return EFI_EXIT(EFI_SUCCESS);
609 struct efi_simple_text_output_protocol efi_con_out = {
610 .reset = efi_cout_reset,
611 .output_string = efi_cout_output_string,
612 .test_string = efi_cout_test_string,
613 .query_mode = efi_cout_query_mode,
614 .set_mode = efi_cout_set_mode,
615 .set_attribute = efi_cout_set_attribute,
616 .clear_screen = efi_cout_clear_screen,
617 .set_cursor_position = efi_cout_set_cursor_position,
618 .enable_cursor = efi_cout_enable_cursor,
619 .mode = (void*)&efi_con_mode,
623 * struct efi_cin_notify_function - registered console input notify function
625 * @link: link to list
626 * @key: key to notify
627 * @function: function to call
629 struct efi_cin_notify_function {
630 struct list_head link;
631 struct efi_key_data key;
632 efi_status_t (EFIAPI *function)
633 (struct efi_key_data *key_data);
636 static bool key_available;
637 static struct efi_key_data next_key;
638 static LIST_HEAD(cin_notify_functions);
641 * set_shift_mask() - set shift mask
643 * @mod: Xterm shift mask
644 * @key_state: receives the state of the shift, alt, control, and logo keys
646 void set_shift_mask(int mod, struct efi_key_state *key_state)
648 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
652 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
654 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
656 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
657 if (!mod || (mod & 8))
658 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
663 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
665 * This gets called when we have already parsed CSI.
667 * @key_state: receives the state of the shift, alt, control, and logo keys
668 * Return: the unmodified code
670 static int analyze_modifiers(struct efi_key_state *key_state)
672 int c, mod = 0, ret = 0;
696 set_shift_mask(mod, key_state);
703 * efi_cin_read_key() - read a key from the console input
705 * @key: - key received
706 * Return: - status code
708 static efi_status_t efi_cin_read_key(struct efi_key_data *key)
710 struct efi_input_key pressed_key = {
716 if (console_read_unicode(&ch))
717 return EFI_NOT_READY;
719 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
720 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
722 /* We do not support multi-word codes */
729 * If a second key is received within 10 ms, assume that we are
730 * dealing with an escape sequence. Otherwise consider this the
731 * escape key being hit. 10 ms is long enough to work fine at
732 * 1200 baud and above.
736 pressed_key.scan_code = 23;
740 * Xterm Control Sequences
741 * https://www.xfree86.org/4.8.0/ctlseqs.html
746 pressed_key.scan_code = 23;
748 case 'O': /* F1 - F4, End */
750 /* consider modifiers */
751 if (ch == 'F') { /* End */
752 pressed_key.scan_code = 6;
754 } else if (ch < 'P') {
755 set_shift_mask(ch - '0', &key->key_state);
758 pressed_key.scan_code = ch - 'P' + 11;
763 case 'A'...'D': /* up, down right, left */
764 pressed_key.scan_code = ch - 'A' + 1;
767 pressed_key.scan_code = 6;
770 pressed_key.scan_code = 5;
773 ch = analyze_modifiers(&key->key_state);
775 case '1'...'5': /* F1 - F5 */
776 pressed_key.scan_code = ch - '1' + 11;
778 case '6'...'9': /* F5 - F8 */
779 pressed_key.scan_code = ch - '6' + 15;
781 case 'A'...'D': /* up, down right, left */
782 pressed_key.scan_code = ch - 'A' + 1;
785 pressed_key.scan_code = 6;
788 pressed_key.scan_code = 5;
791 pressed_key.scan_code = 5;
796 ch = analyze_modifiers(&key->key_state);
798 case '0'...'1': /* F9 - F10 */
799 pressed_key.scan_code = ch - '0' + 19;
801 case '3'...'4': /* F11 - F12 */
802 pressed_key.scan_code = ch - '3' + 21;
805 pressed_key.scan_code = 7;
810 pressed_key.scan_code = 8;
811 analyze_modifiers(&key->key_state);
813 case '5': /* PG UP */
814 pressed_key.scan_code = 9;
815 analyze_modifiers(&key->key_state);
817 case '6': /* PG DOWN */
818 pressed_key.scan_code = 10;
819 analyze_modifiers(&key->key_state);
825 set_shift_mask(3, &key->key_state);
832 if (pressed_key.scan_code) {
833 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
835 pressed_key.unicode_char = ch;
838 * Assume left control key for control characters typically
839 * entered using the control key.
841 if (ch >= 0x01 && ch <= 0x1f) {
842 key->key_state.key_shift_state |=
843 EFI_SHIFT_STATE_VALID;
848 key->key_state.key_shift_state |=
849 EFI_LEFT_CONTROL_PRESSED;
853 key->key = pressed_key;
859 * efi_cin_notify() - notify registered functions
861 static void efi_cin_notify(void)
863 struct efi_cin_notify_function *item;
865 list_for_each_entry(item, &cin_notify_functions, link) {
868 /* We do not support toggle states */
869 if (item->key.key.unicode_char || item->key.key.scan_code) {
870 if (item->key.key.unicode_char !=
871 next_key.key.unicode_char ||
872 item->key.key.scan_code != next_key.key.scan_code)
875 if (item->key.key_state.key_shift_state &&
876 item->key.key_state.key_shift_state !=
877 next_key.key_state.key_shift_state)
881 /* We don't bother about the return code */
882 EFI_CALL(item->function(&next_key));
887 * efi_cin_check() - check if keyboard input is available
889 static void efi_cin_check(void)
894 efi_signal_event(efi_con_in.wait_for_key);
899 ret = efi_cin_read_key(&next_key);
900 if (ret == EFI_SUCCESS) {
901 key_available = true;
903 /* Notify registered functions */
906 /* Queue the wait for key event */
908 efi_signal_event(efi_con_in.wait_for_key);
914 * efi_cin_empty_buffer() - empty input buffer
916 static void efi_cin_empty_buffer(void)
920 key_available = false;
924 * efi_cin_reset_ex() - reset console input
926 * @this: - the extended simple text input protocol
927 * @extended_verification: - extended verification
929 * This function implements the reset service of the
930 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
932 * See the Unified Extensible Firmware Interface (UEFI) specification for
935 * Return: old value of the task priority level
937 static efi_status_t EFIAPI efi_cin_reset_ex(
938 struct efi_simple_text_input_ex_protocol *this,
939 bool extended_verification)
941 efi_status_t ret = EFI_SUCCESS;
943 EFI_ENTRY("%p, %d", this, extended_verification);
945 /* Check parameters */
947 ret = EFI_INVALID_PARAMETER;
951 efi_cin_empty_buffer();
953 return EFI_EXIT(ret);
957 * efi_cin_read_key_stroke_ex() - read key stroke
959 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
960 * @key_data: key read from console
961 * Return: status code
963 * This function implements the ReadKeyStrokeEx service of the
964 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
966 * See the Unified Extensible Firmware Interface (UEFI) specification for
969 static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
970 struct efi_simple_text_input_ex_protocol *this,
971 struct efi_key_data *key_data)
973 efi_status_t ret = EFI_SUCCESS;
975 EFI_ENTRY("%p, %p", this, key_data);
977 /* Check parameters */
978 if (!this || !key_data) {
979 ret = EFI_INVALID_PARAMETER;
983 /* We don't do interrupts, so check for timers cooperatively */
986 /* Enable console input after ExitBootServices */
989 if (!key_available) {
994 * CTRL+A - CTRL+Z have to be signaled as a - z.
995 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
997 switch (next_key.key.unicode_char) {
1001 if (!(next_key.key_state.key_toggle_state &
1002 EFI_CAPS_LOCK_ACTIVE) ^
1003 !(next_key.key_state.key_shift_state &
1004 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
1005 next_key.key.unicode_char += 0x40;
1007 next_key.key.unicode_char += 0x60;
1009 *key_data = next_key;
1010 key_available = false;
1011 efi_con_in.wait_for_key->is_signaled = false;
1014 return EFI_EXIT(ret);
1018 * efi_cin_set_state() - set toggle key state
1020 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1021 * @key_toggle_state: pointer to key toggle state
1022 * Return: status code
1024 * This function implements the SetState service of the
1025 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1027 * See the Unified Extensible Firmware Interface (UEFI) specification for
1030 static efi_status_t EFIAPI efi_cin_set_state(
1031 struct efi_simple_text_input_ex_protocol *this,
1032 u8 *key_toggle_state)
1034 EFI_ENTRY("%p, %p", this, key_toggle_state);
1036 * U-Boot supports multiple console input sources like serial and
1037 * net console for which a key toggle state cannot be set at all.
1039 * According to the UEFI specification it is allowable to not implement
1042 return EFI_EXIT(EFI_UNSUPPORTED);
1046 * efi_cin_register_key_notify() - register key notification function
1048 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1049 * @key_data: key to be notified
1050 * @key_notify_function: function to be called if the key is pressed
1051 * @notify_handle: handle for unregistering the notification
1052 * Return: status code
1054 * This function implements the SetState service of the
1055 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1057 * See the Unified Extensible Firmware Interface (UEFI) specification for
1060 static efi_status_t EFIAPI efi_cin_register_key_notify(
1061 struct efi_simple_text_input_ex_protocol *this,
1062 struct efi_key_data *key_data,
1063 efi_status_t (EFIAPI *key_notify_function)(
1064 struct efi_key_data *key_data),
1065 void **notify_handle)
1067 efi_status_t ret = EFI_SUCCESS;
1068 struct efi_cin_notify_function *notify_function;
1070 EFI_ENTRY("%p, %p, %p, %p",
1071 this, key_data, key_notify_function, notify_handle);
1073 /* Check parameters */
1074 if (!this || !key_data || !key_notify_function || !notify_handle) {
1075 ret = EFI_INVALID_PARAMETER;
1079 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
1080 key_data->key.unicode_char,
1081 key_data->key.scan_code,
1082 key_data->key_state.key_shift_state,
1083 key_data->key_state.key_toggle_state);
1085 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
1086 if (!notify_function) {
1087 ret = EFI_OUT_OF_RESOURCES;
1090 notify_function->key = *key_data;
1091 notify_function->function = key_notify_function;
1092 list_add_tail(¬ify_function->link, &cin_notify_functions);
1093 *notify_handle = notify_function;
1095 return EFI_EXIT(ret);
1099 * efi_cin_unregister_key_notify() - unregister key notification function
1101 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1102 * @notification_handle: handle received when registering
1103 * Return: status code
1105 * This function implements the SetState service of the
1106 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1108 * See the Unified Extensible Firmware Interface (UEFI) specification for
1111 static efi_status_t EFIAPI efi_cin_unregister_key_notify(
1112 struct efi_simple_text_input_ex_protocol *this,
1113 void *notification_handle)
1115 efi_status_t ret = EFI_INVALID_PARAMETER;
1116 struct efi_cin_notify_function *item, *notify_function =
1117 notification_handle;
1119 EFI_ENTRY("%p, %p", this, notification_handle);
1121 /* Check parameters */
1122 if (!this || !notification_handle)
1125 list_for_each_entry(item, &cin_notify_functions, link) {
1126 if (item == notify_function) {
1131 if (ret != EFI_SUCCESS)
1134 /* Remove the notify function */
1135 list_del(¬ify_function->link);
1136 free(notify_function);
1138 return EFI_EXIT(ret);
1143 * efi_cin_reset() - drain the input buffer
1145 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1146 * @extended_verification: allow for exhaustive verification
1147 * Return: status code
1149 * This function implements the Reset service of the
1150 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1152 * See the Unified Extensible Firmware Interface (UEFI) specification for
1155 static efi_status_t EFIAPI efi_cin_reset
1156 (struct efi_simple_text_input_protocol *this,
1157 bool extended_verification)
1159 efi_status_t ret = EFI_SUCCESS;
1161 EFI_ENTRY("%p, %d", this, extended_verification);
1163 /* Check parameters */
1165 ret = EFI_INVALID_PARAMETER;
1169 efi_cin_empty_buffer();
1171 return EFI_EXIT(ret);
1175 * efi_cin_read_key_stroke() - read key stroke
1177 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1178 * @key: key read from console
1179 * Return: status code
1181 * This function implements the ReadKeyStroke service of the
1182 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1184 * See the Unified Extensible Firmware Interface (UEFI) specification for
1187 static efi_status_t EFIAPI efi_cin_read_key_stroke
1188 (struct efi_simple_text_input_protocol *this,
1189 struct efi_input_key *key)
1191 efi_status_t ret = EFI_SUCCESS;
1193 EFI_ENTRY("%p, %p", this, key);
1195 /* Check parameters */
1196 if (!this || !key) {
1197 ret = EFI_INVALID_PARAMETER;
1201 /* We don't do interrupts, so check for timers cooperatively */
1204 /* Enable console input after ExitBootServices */
1207 if (!key_available) {
1208 ret = EFI_NOT_READY;
1211 *key = next_key.key;
1212 key_available = false;
1213 efi_con_in.wait_for_key->is_signaled = false;
1215 return EFI_EXIT(ret);
1218 static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1219 .reset = efi_cin_reset_ex,
1220 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1221 .wait_for_key_ex = NULL,
1222 .set_state = efi_cin_set_state,
1223 .register_key_notify = efi_cin_register_key_notify,
1224 .unregister_key_notify = efi_cin_unregister_key_notify,
1227 struct efi_simple_text_input_protocol efi_con_in = {
1228 .reset = efi_cin_reset,
1229 .read_key_stroke = efi_cin_read_key_stroke,
1230 .wait_for_key = NULL,
1233 static struct efi_event *console_timer_event;
1236 * efi_console_timer_notify() - notify the console timer event
1238 * @event: console timer event
1239 * @context: not used
1241 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1244 EFI_ENTRY("%p, %p", event, context);
1246 EFI_EXIT(EFI_SUCCESS);
1250 * efi_key_notify() - notify the wait for key event
1252 * @event: wait for key event
1253 * @context: not used
1255 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1257 EFI_ENTRY("%p, %p", event, context);
1259 EFI_EXIT(EFI_SUCCESS);
1263 * efi_console_register() - install the console protocols
1265 * This function is called from do_bootefi_exec().
1267 * Return: status code
1269 efi_status_t efi_console_register(void)
1272 struct efi_device_path *dp;
1274 /* Install protocols on root node */
1275 r = EFI_CALL(efi_install_multiple_protocol_interfaces
1277 &efi_guid_text_output_protocol, &efi_con_out,
1278 &efi_guid_text_input_protocol, &efi_con_in,
1279 &efi_guid_text_input_ex_protocol, &efi_con_in_ex,
1282 /* Create console node and install device path protocols */
1283 if (CONFIG_IS_ENABLED(DM_SERIAL)) {
1284 dp = efi_dp_from_uart();
1288 /* Hook UART up to the device list */
1289 efi_add_handle(&uart_obj);
1291 /* Install device path */
1292 r = efi_add_protocol(&uart_obj, &efi_guid_device_path, dp);
1293 if (r != EFI_SUCCESS)
1297 /* Create console events */
1298 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1299 NULL, NULL, &efi_con_in.wait_for_key);
1300 if (r != EFI_SUCCESS) {
1301 printf("ERROR: Failed to register WaitForKey event\n");
1304 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
1305 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
1306 efi_console_timer_notify, NULL, NULL,
1307 &console_timer_event);
1308 if (r != EFI_SUCCESS) {
1309 printf("ERROR: Failed to register console event\n");
1312 /* 5000 ns cycle is sufficient for 2 MBaud */
1313 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1314 if (r != EFI_SUCCESS)
1315 printf("ERROR: Failed to set console timer\n");
1318 printf("ERROR: Out of memory\n");