1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application console interface
5 * Copyright (c) 2016 Alexander Graf
10 #include <dm/device.h>
11 #include <efi_loader.h>
12 #include <stdio_dev.h>
13 #include <video_console.h>
15 #define EFI_COUT_MODE_2 2
16 #define EFI_MAX_COUT_MODE 3
19 unsigned long columns;
24 static struct cout_mode efi_cout_modes[] = {
25 /* EFI Mode 0 is 80x25 and always present */
31 /* EFI Mode 1 is always 80x50 */
37 /* Value are unknown until we query the console */
45 const efi_guid_t efi_guid_text_input_ex_protocol =
46 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
47 const efi_guid_t efi_guid_text_input_protocol =
48 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
49 const efi_guid_t efi_guid_text_output_protocol =
50 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
55 /* Default to mode 0 */
56 static struct simple_text_output_mode efi_con_mode = {
65 static int term_get_char(s32 *c)
69 /* Wait up to 100 ms for a character */
70 timeout = timer_get_us() + 100000;
73 if (timer_get_us() > timeout)
81 * Receive and parse a reply from the terminal.
83 * @n: array of return values
84 * @num: number of return values expected
85 * @end_char: character indicating end of terminal message
86 * @return: non-zero indicates error
88 static int term_read_reply(int *n, int num, char end_char)
93 if (term_get_char(&c) || c != cESC)
96 if (term_get_char(&c) || c != '[')
101 if (!term_get_char(&c)) {
108 } else if (c == end_char) {
110 } else if (c > '9' || c < '0') {
114 /* Read one more decimal position */
127 static efi_status_t EFIAPI efi_cout_output_string(
128 struct efi_simple_text_output_protocol *this,
129 const efi_string_t string)
131 struct simple_text_output_mode *con = &efi_con_mode;
132 struct cout_mode *mode = &efi_cout_modes[con->mode];
135 efi_status_t ret = EFI_SUCCESS;
137 EFI_ENTRY("%p, %p", this, string);
139 if (!this || !string) {
140 ret = EFI_INVALID_PARAMETER;
144 buf = malloc(utf16_utf8_strlen(string) + 1);
146 ret = EFI_OUT_OF_RESOURCES;
150 utf16_utf8_strcpy(&pos, string);
155 * Update the cursor position.
157 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
158 * and U000D. All other characters, including control characters
159 * U+0007 (BEL) and U+0009 (TAB), have to increase the column by one.
161 for (p = string; *p; ++p) {
163 case '\b': /* U+0008, backspace */
164 con->cursor_column = max(0, con->cursor_column - 1);
166 case '\n': /* U+000A, newline */
167 con->cursor_column = 0;
170 case '\r': /* U+000D, carriage-return */
171 con->cursor_column = 0;
173 case 0xd800 ... 0xdbff:
175 * Ignore high surrogates, we do not want to count a
176 * Unicode character twice.
180 con->cursor_column++;
183 if (con->cursor_column >= mode->columns) {
184 con->cursor_column = 0;
187 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
191 return EFI_EXIT(ret);
194 static efi_status_t EFIAPI efi_cout_test_string(
195 struct efi_simple_text_output_protocol *this,
196 const efi_string_t string)
198 EFI_ENTRY("%p, %p", this, string);
199 return EFI_EXIT(EFI_SUCCESS);
202 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
207 return (mode->rows == rows) && (mode->columns == cols);
211 * query_console_serial() - query console size
213 * @rows pointer to return number of rows
214 * @columns pointer to return number of columns
215 * Returns 0 on success
217 static int query_console_serial(int *rows, int *cols)
222 /* Empty input buffer */
227 * Not all terminals understand CSI [18t for querying the console size.
228 * We should adhere to escape sequences documented in the console_codes
229 * man page and the ECMA-48 standard.
231 * So here we follow a different approach. We position the cursor to the
232 * bottom right and query its position. Before leaving the function we
233 * restore the original cursor position.
235 printf(ESC "7" /* Save cursor position */
236 ESC "[r" /* Set scrolling region to full window */
237 ESC "[999;999H" /* Move to bottom right corner */
238 ESC "[6n"); /* Query cursor position */
240 /* Read {rows,cols} */
241 if (term_read_reply(n, 2, 'R')) {
249 printf(ESC "8"); /* Restore cursor position */
254 * Update the mode table.
256 * By default the only mode available is 80x25. If the console has at least 50
257 * lines, enable mode 80x50. If we can query the console size and it is neither
258 * 80x25 nor 80x50, set it as an additional mode.
260 static void query_console_size(void)
262 const char *stdout_name = env_get("stdout");
263 int rows = 25, cols = 80;
265 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
266 IS_ENABLED(CONFIG_DM_VIDEO)) {
267 struct stdio_dev *stdout_dev =
268 stdio_get_by_name("vidconsole");
269 struct udevice *dev = stdout_dev->priv;
270 struct vidconsole_priv *priv =
271 dev_get_uclass_priv(dev);
274 } else if (query_console_serial(&rows, &cols)) {
278 /* Test if we can have Mode 1 */
279 if (cols >= 80 && rows >= 50) {
280 efi_cout_modes[1].present = 1;
281 efi_con_mode.max_mode = 2;
285 * Install our mode as mode 2 if it is different
286 * than mode 0 or 1 and set it as the currently selected mode
288 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
289 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
290 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
291 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
292 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
293 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
294 efi_con_mode.mode = EFI_COUT_MODE_2;
298 static efi_status_t EFIAPI efi_cout_query_mode(
299 struct efi_simple_text_output_protocol *this,
300 unsigned long mode_number, unsigned long *columns,
303 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
305 if (mode_number >= efi_con_mode.max_mode)
306 return EFI_EXIT(EFI_UNSUPPORTED);
308 if (efi_cout_modes[mode_number].present != 1)
309 return EFI_EXIT(EFI_UNSUPPORTED);
312 *columns = efi_cout_modes[mode_number].columns;
314 *rows = efi_cout_modes[mode_number].rows;
316 return EFI_EXIT(EFI_SUCCESS);
319 static const struct {
323 { 30, 40 }, /* 0: black */
324 { 34, 44 }, /* 1: blue */
325 { 32, 42 }, /* 2: green */
326 { 36, 46 }, /* 3: cyan */
327 { 31, 41 }, /* 4: red */
328 { 35, 45 }, /* 5: magenta */
329 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
330 { 37, 47 }, /* 7: light gray, map to white */
333 /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
334 static efi_status_t EFIAPI efi_cout_set_attribute(
335 struct efi_simple_text_output_protocol *this,
336 unsigned long attribute)
338 unsigned int bold = EFI_ATTR_BOLD(attribute);
339 unsigned int fg = EFI_ATTR_FG(attribute);
340 unsigned int bg = EFI_ATTR_BG(attribute);
342 EFI_ENTRY("%p, %lx", this, attribute);
344 efi_con_mode.attribute = attribute;
346 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
348 printf(ESC"[0;37;40m");
350 return EFI_EXIT(EFI_SUCCESS);
353 static efi_status_t EFIAPI efi_cout_clear_screen(
354 struct efi_simple_text_output_protocol *this)
356 EFI_ENTRY("%p", this);
359 efi_con_mode.cursor_column = 0;
360 efi_con_mode.cursor_row = 0;
362 return EFI_EXIT(EFI_SUCCESS);
365 static efi_status_t EFIAPI efi_cout_set_mode(
366 struct efi_simple_text_output_protocol *this,
367 unsigned long mode_number)
369 EFI_ENTRY("%p, %ld", this, mode_number);
371 if (mode_number >= efi_con_mode.max_mode)
372 return EFI_EXIT(EFI_UNSUPPORTED);
373 efi_con_mode.mode = mode_number;
374 EFI_CALL(efi_cout_clear_screen(this));
376 return EFI_EXIT(EFI_SUCCESS);
379 static efi_status_t EFIAPI efi_cout_reset(
380 struct efi_simple_text_output_protocol *this,
381 char extended_verification)
383 EFI_ENTRY("%p, %d", this, extended_verification);
386 EFI_CALL(efi_cout_clear_screen(this));
387 /* Set default colors */
388 efi_con_mode.attribute = 0x07;
389 printf(ESC "[0;37;40m");
391 return EFI_EXIT(EFI_SUCCESS);
394 static efi_status_t EFIAPI efi_cout_set_cursor_position(
395 struct efi_simple_text_output_protocol *this,
396 unsigned long column, unsigned long row)
398 efi_status_t ret = EFI_SUCCESS;
399 struct simple_text_output_mode *con = &efi_con_mode;
400 struct cout_mode *mode = &efi_cout_modes[con->mode];
402 EFI_ENTRY("%p, %ld, %ld", this, column, row);
404 /* Check parameters */
406 ret = EFI_INVALID_PARAMETER;
409 if (row >= mode->rows || column >= mode->columns) {
410 ret = EFI_UNSUPPORTED;
415 * Set cursor position by sending CSI H.
416 * EFI origin is [0, 0], terminal origin is [1, 1].
418 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
419 efi_con_mode.cursor_column = column;
420 efi_con_mode.cursor_row = row;
422 return EFI_EXIT(ret);
425 static efi_status_t EFIAPI efi_cout_enable_cursor(
426 struct efi_simple_text_output_protocol *this,
429 EFI_ENTRY("%p, %d", this, enable);
431 printf(ESC"[?25%c", enable ? 'h' : 'l');
432 efi_con_mode.cursor_visible = !!enable;
434 return EFI_EXIT(EFI_SUCCESS);
437 struct efi_simple_text_output_protocol efi_con_out = {
438 .reset = efi_cout_reset,
439 .output_string = efi_cout_output_string,
440 .test_string = efi_cout_test_string,
441 .query_mode = efi_cout_query_mode,
442 .set_mode = efi_cout_set_mode,
443 .set_attribute = efi_cout_set_attribute,
444 .clear_screen = efi_cout_clear_screen,
445 .set_cursor_position = efi_cout_set_cursor_position,
446 .enable_cursor = efi_cout_enable_cursor,
447 .mode = (void*)&efi_con_mode,
451 * struct efi_cin_notify_function - registered console input notify function
453 * @link: link to list
454 * @data: key to notify
455 * @function: function to call
457 struct efi_cin_notify_function {
458 struct list_head link;
459 struct efi_key_data key;
460 efi_status_t (EFIAPI *function)
461 (struct efi_key_data *key_data);
464 static bool key_available;
465 static struct efi_key_data next_key;
466 static LIST_HEAD(cin_notify_functions);
469 * set_shift_mask() - set shift mask
471 * @mod: Xterm shift mask
473 void set_shift_mask(int mod, struct efi_key_state *key_state)
475 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
479 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
481 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
483 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
485 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
487 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
492 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
494 * This gets called when we have already parsed CSI.
496 * @modifiers: bit mask (shift, alt, ctrl)
497 * @return: the unmodified code
499 static int analyze_modifiers(struct efi_key_state *key_state)
501 int c, mod = 0, ret = 0;
525 set_shift_mask(mod, key_state);
532 * efi_cin_read_key() - read a key from the console input
534 * @key: - key received
535 * Return: - status code
537 static efi_status_t efi_cin_read_key(struct efi_key_data *key)
539 struct efi_input_key pressed_key = {
545 if (console_read_unicode(&ch))
546 return EFI_NOT_READY;
548 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
549 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
551 /* We do not support multi-word codes */
558 * Xterm Control Sequences
559 * https://www.xfree86.org/4.8.0/ctlseqs.html
564 pressed_key.scan_code = 23;
566 case 'O': /* F1 - F4, End */
568 /* consider modifiers */
569 if (ch == 'F') { /* End */
570 pressed_key.scan_code = 6;
572 } else if (ch < 'P') {
573 set_shift_mask(ch - '0', &key->key_state);
576 pressed_key.scan_code = ch - 'P' + 11;
581 case 'A'...'D': /* up, down right, left */
582 pressed_key.scan_code = ch - 'A' + 1;
585 pressed_key.scan_code = 6;
588 pressed_key.scan_code = 5;
591 ch = analyze_modifiers(&key->key_state);
593 case '1'...'5': /* F1 - F5 */
594 pressed_key.scan_code = ch - '1' + 11;
596 case '6'...'9': /* F5 - F8 */
597 pressed_key.scan_code = ch - '6' + 15;
599 case 'A'...'D': /* up, down right, left */
600 pressed_key.scan_code = ch - 'A' + 1;
603 pressed_key.scan_code = 6;
606 pressed_key.scan_code = 5;
609 pressed_key.scan_code = 5;
614 ch = analyze_modifiers(&key->key_state);
616 case '0'...'1': /* F9 - F10 */
617 pressed_key.scan_code = ch - '0' + 19;
619 case '3'...'4': /* F11 - F12 */
620 pressed_key.scan_code = ch - '3' + 21;
623 pressed_key.scan_code = 7;
628 pressed_key.scan_code = 8;
629 analyze_modifiers(&key->key_state);
631 case '5': /* PG UP */
632 pressed_key.scan_code = 9;
633 analyze_modifiers(&key->key_state);
635 case '6': /* PG DOWN */
636 pressed_key.scan_code = 10;
637 analyze_modifiers(&key->key_state);
643 set_shift_mask(3, &key->key_state);
650 if (pressed_key.scan_code) {
651 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
653 pressed_key.unicode_char = ch;
656 * Assume left control key for control characters typically
657 * entered using the control key.
659 if (ch >= 0x01 && ch <= 0x1f) {
660 key->key_state.key_shift_state |=
661 EFI_SHIFT_STATE_VALID;
666 key->key_state.key_shift_state |=
667 EFI_LEFT_CONTROL_PRESSED;
671 key->key = pressed_key;
677 * efi_cin_notify() - notify registered functions
679 static void efi_cin_notify(void)
681 struct efi_cin_notify_function *item;
683 list_for_each_entry(item, &cin_notify_functions, link) {
686 /* We do not support toggle states */
687 if (item->key.key.unicode_char || item->key.key.scan_code) {
688 if (item->key.key.unicode_char !=
689 next_key.key.unicode_char ||
690 item->key.key.scan_code != next_key.key.scan_code)
693 if (item->key.key_state.key_shift_state &&
694 item->key.key_state.key_shift_state !=
695 next_key.key_state.key_shift_state)
699 /* We don't bother about the return code */
700 EFI_CALL(item->function(&next_key));
705 * efi_cin_check() - check if keyboard input is available
707 static void efi_cin_check(void)
712 efi_signal_event(efi_con_in.wait_for_key);
717 ret = efi_cin_read_key(&next_key);
718 if (ret == EFI_SUCCESS) {
719 key_available = true;
721 /* Notify registered functions */
724 /* Queue the wait for key event */
726 efi_signal_event(efi_con_in.wait_for_key);
732 * efi_cin_empty_buffer() - empty input buffer
734 static void efi_cin_empty_buffer(void)
738 key_available = false;
742 * efi_cin_reset_ex() - reset console input
744 * @this: - the extended simple text input protocol
745 * @extended_verification: - extended verification
747 * This function implements the reset service of the
748 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
750 * See the Unified Extensible Firmware Interface (UEFI) specification for
753 * Return: old value of the task priority level
755 static efi_status_t EFIAPI efi_cin_reset_ex(
756 struct efi_simple_text_input_ex_protocol *this,
757 bool extended_verification)
759 efi_status_t ret = EFI_SUCCESS;
761 EFI_ENTRY("%p, %d", this, extended_verification);
763 /* Check parameters */
765 ret = EFI_INVALID_PARAMETER;
769 efi_cin_empty_buffer();
771 return EFI_EXIT(ret);
775 * efi_cin_read_key_stroke_ex() - read key stroke
777 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
778 * @key_data: key read from console
779 * Return: status code
781 * This function implements the ReadKeyStrokeEx service of the
782 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
784 * See the Unified Extensible Firmware Interface (UEFI) specification for
787 static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
788 struct efi_simple_text_input_ex_protocol *this,
789 struct efi_key_data *key_data)
791 efi_status_t ret = EFI_SUCCESS;
793 EFI_ENTRY("%p, %p", this, key_data);
795 /* Check parameters */
796 if (!this || !key_data) {
797 ret = EFI_INVALID_PARAMETER;
801 /* We don't do interrupts, so check for timers cooperatively */
804 /* Enable console input after ExitBootServices */
807 if (!key_available) {
812 * CTRL+A - CTRL+Z have to be signaled as a - z.
813 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
815 switch (next_key.key.unicode_char) {
819 if (!(next_key.key_state.key_toggle_state &
820 EFI_CAPS_LOCK_ACTIVE) ^
821 !(next_key.key_state.key_shift_state &
822 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
823 next_key.key.unicode_char += 0x40;
825 next_key.key.unicode_char += 0x60;
827 *key_data = next_key;
828 key_available = false;
829 efi_con_in.wait_for_key->is_signaled = false;
832 return EFI_EXIT(ret);
836 * efi_cin_set_state() - set toggle key state
838 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
839 * @key_toggle_state: pointer to key toggle state
840 * Return: status code
842 * This function implements the SetState service of the
843 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
845 * See the Unified Extensible Firmware Interface (UEFI) specification for
848 static efi_status_t EFIAPI efi_cin_set_state(
849 struct efi_simple_text_input_ex_protocol *this,
850 u8 *key_toggle_state)
852 EFI_ENTRY("%p, %p", this, key_toggle_state);
854 * U-Boot supports multiple console input sources like serial and
855 * net console for which a key toggle state cannot be set at all.
857 * According to the UEFI specification it is allowable to not implement
860 return EFI_EXIT(EFI_UNSUPPORTED);
864 * efi_cin_register_key_notify() - register key notification function
866 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
867 * @key_data: key to be notified
868 * @key_notify_function: function to be called if the key is pressed
869 * @notify_handle: handle for unregistering the notification
870 * Return: status code
872 * This function implements the SetState service of the
873 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
875 * See the Unified Extensible Firmware Interface (UEFI) specification for
878 static efi_status_t EFIAPI efi_cin_register_key_notify(
879 struct efi_simple_text_input_ex_protocol *this,
880 struct efi_key_data *key_data,
881 efi_status_t (EFIAPI *key_notify_function)(
882 struct efi_key_data *key_data),
883 void **notify_handle)
885 efi_status_t ret = EFI_SUCCESS;
886 struct efi_cin_notify_function *notify_function;
888 EFI_ENTRY("%p, %p, %p, %p",
889 this, key_data, key_notify_function, notify_handle);
891 /* Check parameters */
892 if (!this || !key_data || !key_notify_function || !notify_handle) {
893 ret = EFI_INVALID_PARAMETER;
897 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
898 key_data->key.unicode_char,
899 key_data->key.scan_code,
900 key_data->key_state.key_shift_state,
901 key_data->key_state.key_toggle_state);
903 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
904 if (!notify_function) {
905 ret = EFI_OUT_OF_RESOURCES;
908 notify_function->key = *key_data;
909 notify_function->function = key_notify_function;
910 list_add_tail(¬ify_function->link, &cin_notify_functions);
911 *notify_handle = notify_function;
913 return EFI_EXIT(ret);
917 * efi_cin_unregister_key_notify() - unregister key notification function
919 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
920 * @notification_handle: handle received when registering
921 * Return: status code
923 * This function implements the SetState service of the
924 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
926 * See the Unified Extensible Firmware Interface (UEFI) specification for
929 static efi_status_t EFIAPI efi_cin_unregister_key_notify(
930 struct efi_simple_text_input_ex_protocol *this,
931 void *notification_handle)
933 efi_status_t ret = EFI_INVALID_PARAMETER;
934 struct efi_cin_notify_function *item, *notify_function =
937 EFI_ENTRY("%p, %p", this, notification_handle);
939 /* Check parameters */
940 if (!this || !notification_handle)
943 list_for_each_entry(item, &cin_notify_functions, link) {
944 if (item == notify_function) {
949 if (ret != EFI_SUCCESS)
952 /* Remove the notify function */
953 list_del(¬ify_function->link);
954 free(notify_function);
956 return EFI_EXIT(ret);
961 * efi_cin_reset() - drain the input buffer
963 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
964 * @extended_verification: allow for exhaustive verification
965 * Return: status code
967 * This function implements the Reset service of the
968 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
970 * See the Unified Extensible Firmware Interface (UEFI) specification for
973 static efi_status_t EFIAPI efi_cin_reset
974 (struct efi_simple_text_input_protocol *this,
975 bool extended_verification)
977 efi_status_t ret = EFI_SUCCESS;
979 EFI_ENTRY("%p, %d", this, extended_verification);
981 /* Check parameters */
983 ret = EFI_INVALID_PARAMETER;
987 efi_cin_empty_buffer();
989 return EFI_EXIT(ret);
993 * efi_cin_read_key_stroke() - read key stroke
995 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
996 * @key: key read from console
997 * Return: status code
999 * This function implements the ReadKeyStroke service of the
1000 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1002 * See the Unified Extensible Firmware Interface (UEFI) specification for
1005 static efi_status_t EFIAPI efi_cin_read_key_stroke
1006 (struct efi_simple_text_input_protocol *this,
1007 struct efi_input_key *key)
1009 efi_status_t ret = EFI_SUCCESS;
1011 EFI_ENTRY("%p, %p", this, key);
1013 /* Check parameters */
1014 if (!this || !key) {
1015 ret = EFI_INVALID_PARAMETER;
1019 /* We don't do interrupts, so check for timers cooperatively */
1022 /* Enable console input after ExitBootServices */
1025 if (!key_available) {
1026 ret = EFI_NOT_READY;
1029 *key = next_key.key;
1030 key_available = false;
1031 efi_con_in.wait_for_key->is_signaled = false;
1033 return EFI_EXIT(ret);
1036 static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1037 .reset = efi_cin_reset_ex,
1038 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1039 .wait_for_key_ex = NULL,
1040 .set_state = efi_cin_set_state,
1041 .register_key_notify = efi_cin_register_key_notify,
1042 .unregister_key_notify = efi_cin_unregister_key_notify,
1045 struct efi_simple_text_input_protocol efi_con_in = {
1046 .reset = efi_cin_reset,
1047 .read_key_stroke = efi_cin_read_key_stroke,
1048 .wait_for_key = NULL,
1051 static struct efi_event *console_timer_event;
1054 * efi_console_timer_notify() - notify the console timer event
1056 * @event: console timer event
1057 * @context: not used
1059 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1062 EFI_ENTRY("%p, %p", event, context);
1064 EFI_EXIT(EFI_SUCCESS);
1068 * efi_key_notify() - notify the wait for key event
1070 * @event: wait for key event
1071 * @context: not used
1073 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1075 EFI_ENTRY("%p, %p", event, context);
1077 EFI_EXIT(EFI_SUCCESS);
1081 * efi_console_register() - install the console protocols
1083 * This function is called from do_bootefi_exec().
1085 * Return: status code
1087 efi_status_t efi_console_register(void)
1090 efi_handle_t console_output_handle;
1091 efi_handle_t console_input_handle;
1093 /* Set up mode information */
1094 query_console_size();
1096 /* Create handles */
1097 r = efi_create_handle(&console_output_handle);
1098 if (r != EFI_SUCCESS)
1101 r = efi_add_protocol(console_output_handle,
1102 &efi_guid_text_output_protocol, &efi_con_out);
1103 if (r != EFI_SUCCESS)
1105 systab.con_out_handle = console_output_handle;
1106 systab.stderr_handle = console_output_handle;
1108 r = efi_create_handle(&console_input_handle);
1109 if (r != EFI_SUCCESS)
1112 r = efi_add_protocol(console_input_handle,
1113 &efi_guid_text_input_protocol, &efi_con_in);
1114 if (r != EFI_SUCCESS)
1116 systab.con_in_handle = console_input_handle;
1117 r = efi_add_protocol(console_input_handle,
1118 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1119 if (r != EFI_SUCCESS)
1122 /* Create console events */
1123 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1124 NULL, NULL, &efi_con_in.wait_for_key);
1125 if (r != EFI_SUCCESS) {
1126 printf("ERROR: Failed to register WaitForKey event\n");
1129 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
1130 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
1131 efi_console_timer_notify, NULL, NULL,
1132 &console_timer_event);
1133 if (r != EFI_SUCCESS) {
1134 printf("ERROR: Failed to register console event\n");
1137 /* 5000 ns cycle is sufficient for 2 MBaud */
1138 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1139 if (r != EFI_SUCCESS)
1140 printf("ERROR: Failed to set console timer\n");
1143 printf("ERROR: Out of memory\n");