1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application console interface
5 * Copyright (c) 2016 Alexander Graf
12 #include <dm/device.h>
13 #include <efi_loader.h>
15 #include <stdio_dev.h>
16 #include <video_console.h>
18 #define EFI_COUT_MODE_2 2
19 #define EFI_MAX_COUT_MODE 3
22 unsigned long columns;
27 static struct cout_mode efi_cout_modes[] = {
28 /* EFI Mode 0 is 80x25 and always present */
34 /* EFI Mode 1 is always 80x50 */
40 /* Value are unknown until we query the console */
48 const efi_guid_t efi_guid_text_input_ex_protocol =
49 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
50 const efi_guid_t efi_guid_text_input_protocol =
51 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
52 const efi_guid_t efi_guid_text_output_protocol =
53 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
58 /* Default to mode 0 */
59 static struct simple_text_output_mode efi_con_mode = {
68 static int term_get_char(s32 *c)
72 /* Wait up to 100 ms for a character */
73 timeout = timer_get_us() + 100000;
76 if (timer_get_us() > timeout)
84 * Receive and parse a reply from the terminal.
86 * @n: array of return values
87 * @num: number of return values expected
88 * @end_char: character indicating end of terminal message
89 * @return: non-zero indicates error
91 static int term_read_reply(int *n, int num, char end_char)
96 if (term_get_char(&c) || c != cESC)
99 if (term_get_char(&c) || c != '[')
104 if (!term_get_char(&c)) {
111 } else if (c == end_char) {
113 } else if (c > '9' || c < '0') {
117 /* Read one more decimal position */
130 static efi_status_t EFIAPI efi_cout_output_string(
131 struct efi_simple_text_output_protocol *this,
132 const efi_string_t string)
134 struct simple_text_output_mode *con = &efi_con_mode;
135 struct cout_mode *mode = &efi_cout_modes[con->mode];
138 efi_status_t ret = EFI_SUCCESS;
140 EFI_ENTRY("%p, %p", this, string);
142 if (!this || !string) {
143 ret = EFI_INVALID_PARAMETER;
147 buf = malloc(utf16_utf8_strlen(string) + 1);
149 ret = EFI_OUT_OF_RESOURCES;
153 utf16_utf8_strcpy(&pos, string);
158 * Update the cursor position.
160 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
161 * and U000D. All other control characters are ignored. Any non-control
162 * character increase the column by one.
164 for (p = string; *p; ++p) {
166 case '\b': /* U+0008, backspace */
167 if (con->cursor_column)
168 con->cursor_column--;
170 case '\n': /* U+000A, newline */
171 con->cursor_column = 0;
174 case '\r': /* U+000D, carriage-return */
175 con->cursor_column = 0;
177 case 0xd800 ... 0xdbff:
179 * Ignore high surrogates, we do not want to count a
180 * Unicode character twice.
184 /* Exclude control codes */
186 con->cursor_column++;
189 if (con->cursor_column >= mode->columns) {
190 con->cursor_column = 0;
194 * When we exceed the row count the terminal will scroll up one
195 * line. We have to adjust the cursor position.
197 if (con->cursor_row >= mode->rows && con->cursor_row)
202 return EFI_EXIT(ret);
205 static efi_status_t EFIAPI efi_cout_test_string(
206 struct efi_simple_text_output_protocol *this,
207 const efi_string_t string)
209 EFI_ENTRY("%p, %p", this, string);
210 return EFI_EXIT(EFI_SUCCESS);
213 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
218 return (mode->rows == rows) && (mode->columns == cols);
222 * query_console_serial() - query console size
224 * @rows: pointer to return number of rows
225 * @cols: pointer to return number of columns
226 * Returns: 0 on success
228 static int query_console_serial(int *rows, int *cols)
233 /* Empty input buffer */
238 * Not all terminals understand CSI [18t for querying the console size.
239 * We should adhere to escape sequences documented in the console_codes
240 * man page and the ECMA-48 standard.
242 * So here we follow a different approach. We position the cursor to the
243 * bottom right and query its position. Before leaving the function we
244 * restore the original cursor position.
246 printf(ESC "7" /* Save cursor position */
247 ESC "[r" /* Set scrolling region to full window */
248 ESC "[999;999H" /* Move to bottom right corner */
249 ESC "[6n"); /* Query cursor position */
251 /* Read {rows,cols} */
252 if (term_read_reply(n, 2, 'R')) {
260 printf(ESC "8"); /* Restore cursor position */
265 * Update the mode table.
267 * By default the only mode available is 80x25. If the console has at least 50
268 * lines, enable mode 80x50. If we can query the console size and it is neither
269 * 80x25 nor 80x50, set it as an additional mode.
271 static void query_console_size(void)
273 const char *stdout_name = env_get("stdout");
274 int rows = 25, cols = 80;
276 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
277 IS_ENABLED(CONFIG_DM_VIDEO)) {
278 struct stdio_dev *stdout_dev =
279 stdio_get_by_name("vidconsole");
280 struct udevice *dev = stdout_dev->priv;
281 struct vidconsole_priv *priv =
282 dev_get_uclass_priv(dev);
285 } else if (query_console_serial(&rows, &cols)) {
289 /* Test if we can have Mode 1 */
290 if (cols >= 80 && rows >= 50) {
291 efi_cout_modes[1].present = 1;
292 efi_con_mode.max_mode = 2;
296 * Install our mode as mode 2 if it is different
297 * than mode 0 or 1 and set it as the currently selected mode
299 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
300 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
301 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
302 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
303 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
304 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
305 efi_con_mode.mode = EFI_COUT_MODE_2;
309 static efi_status_t EFIAPI efi_cout_query_mode(
310 struct efi_simple_text_output_protocol *this,
311 unsigned long mode_number, unsigned long *columns,
314 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
316 if (mode_number >= efi_con_mode.max_mode)
317 return EFI_EXIT(EFI_UNSUPPORTED);
319 if (efi_cout_modes[mode_number].present != 1)
320 return EFI_EXIT(EFI_UNSUPPORTED);
323 *columns = efi_cout_modes[mode_number].columns;
325 *rows = efi_cout_modes[mode_number].rows;
327 return EFI_EXIT(EFI_SUCCESS);
330 static const struct {
334 { 30, 40 }, /* 0: black */
335 { 34, 44 }, /* 1: blue */
336 { 32, 42 }, /* 2: green */
337 { 36, 46 }, /* 3: cyan */
338 { 31, 41 }, /* 4: red */
339 { 35, 45 }, /* 5: magenta */
340 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
341 { 37, 47 }, /* 7: light gray, map to white */
344 /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
345 static efi_status_t EFIAPI efi_cout_set_attribute(
346 struct efi_simple_text_output_protocol *this,
347 unsigned long attribute)
349 unsigned int bold = EFI_ATTR_BOLD(attribute);
350 unsigned int fg = EFI_ATTR_FG(attribute);
351 unsigned int bg = EFI_ATTR_BG(attribute);
353 EFI_ENTRY("%p, %lx", this, attribute);
355 efi_con_mode.attribute = attribute;
357 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
359 printf(ESC"[0;37;40m");
361 return EFI_EXIT(EFI_SUCCESS);
365 * efi_cout_clear_screen() - clear screen
367 * This function implements the ClearScreen service of the
368 * EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL. See the Unified Extensible Firmware
369 * Interface (UEFI) specification for details.
371 * @this: pointer to the protocol instance
372 * Return: status code
374 static efi_status_t EFIAPI efi_cout_clear_screen(
375 struct efi_simple_text_output_protocol *this)
377 EFI_ENTRY("%p", this);
380 * The Linux console wants both a clear and a home command. The video
381 * uclass does not support <ESC>[H without coordinates, yet.
383 printf(ESC "[2J" ESC "[1;1H");
384 efi_con_mode.cursor_column = 0;
385 efi_con_mode.cursor_row = 0;
387 return EFI_EXIT(EFI_SUCCESS);
390 static efi_status_t EFIAPI efi_cout_set_mode(
391 struct efi_simple_text_output_protocol *this,
392 unsigned long mode_number)
394 EFI_ENTRY("%p, %ld", this, mode_number);
396 if (mode_number >= efi_con_mode.max_mode)
397 return EFI_EXIT(EFI_UNSUPPORTED);
399 if (!efi_cout_modes[mode_number].present)
400 return EFI_EXIT(EFI_UNSUPPORTED);
402 efi_con_mode.mode = mode_number;
403 EFI_CALL(efi_cout_clear_screen(this));
405 return EFI_EXIT(EFI_SUCCESS);
408 static efi_status_t EFIAPI efi_cout_reset(
409 struct efi_simple_text_output_protocol *this,
410 char extended_verification)
412 EFI_ENTRY("%p, %d", this, extended_verification);
415 EFI_CALL(efi_cout_clear_screen(this));
416 /* Set default colors */
417 efi_con_mode.attribute = 0x07;
418 printf(ESC "[0;37;40m");
420 return EFI_EXIT(EFI_SUCCESS);
423 static efi_status_t EFIAPI efi_cout_set_cursor_position(
424 struct efi_simple_text_output_protocol *this,
425 unsigned long column, unsigned long row)
427 efi_status_t ret = EFI_SUCCESS;
428 struct simple_text_output_mode *con = &efi_con_mode;
429 struct cout_mode *mode = &efi_cout_modes[con->mode];
431 EFI_ENTRY("%p, %ld, %ld", this, column, row);
433 /* Check parameters */
435 ret = EFI_INVALID_PARAMETER;
438 if (row >= mode->rows || column >= mode->columns) {
439 ret = EFI_UNSUPPORTED;
444 * Set cursor position by sending CSI H.
445 * EFI origin is [0, 0], terminal origin is [1, 1].
447 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
448 efi_con_mode.cursor_column = column;
449 efi_con_mode.cursor_row = row;
451 return EFI_EXIT(ret);
454 static efi_status_t EFIAPI efi_cout_enable_cursor(
455 struct efi_simple_text_output_protocol *this,
458 EFI_ENTRY("%p, %d", this, enable);
460 printf(ESC"[?25%c", enable ? 'h' : 'l');
461 efi_con_mode.cursor_visible = !!enable;
463 return EFI_EXIT(EFI_SUCCESS);
466 struct efi_simple_text_output_protocol efi_con_out = {
467 .reset = efi_cout_reset,
468 .output_string = efi_cout_output_string,
469 .test_string = efi_cout_test_string,
470 .query_mode = efi_cout_query_mode,
471 .set_mode = efi_cout_set_mode,
472 .set_attribute = efi_cout_set_attribute,
473 .clear_screen = efi_cout_clear_screen,
474 .set_cursor_position = efi_cout_set_cursor_position,
475 .enable_cursor = efi_cout_enable_cursor,
476 .mode = (void*)&efi_con_mode,
480 * struct efi_cin_notify_function - registered console input notify function
482 * @link: link to list
483 * @key: key to notify
484 * @function: function to call
486 struct efi_cin_notify_function {
487 struct list_head link;
488 struct efi_key_data key;
489 efi_status_t (EFIAPI *function)
490 (struct efi_key_data *key_data);
493 static bool key_available;
494 static struct efi_key_data next_key;
495 static LIST_HEAD(cin_notify_functions);
498 * set_shift_mask() - set shift mask
500 * @mod: Xterm shift mask
501 * @key_state: receives the state of the shift, alt, control, and logo keys
503 void set_shift_mask(int mod, struct efi_key_state *key_state)
505 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
509 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
511 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
513 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
514 if (!mod || (mod & 8))
515 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
520 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
522 * This gets called when we have already parsed CSI.
524 * @key_state: receives the state of the shift, alt, control, and logo keys
525 * @return: the unmodified code
527 static int analyze_modifiers(struct efi_key_state *key_state)
529 int c, mod = 0, ret = 0;
553 set_shift_mask(mod, key_state);
560 * efi_cin_read_key() - read a key from the console input
562 * @key: - key received
563 * Return: - status code
565 static efi_status_t efi_cin_read_key(struct efi_key_data *key)
567 struct efi_input_key pressed_key = {
573 if (console_read_unicode(&ch))
574 return EFI_NOT_READY;
576 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
577 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
579 /* We do not support multi-word codes */
586 * Xterm Control Sequences
587 * https://www.xfree86.org/4.8.0/ctlseqs.html
592 pressed_key.scan_code = 23;
594 case 'O': /* F1 - F4, End */
596 /* consider modifiers */
597 if (ch == 'F') { /* End */
598 pressed_key.scan_code = 6;
600 } else if (ch < 'P') {
601 set_shift_mask(ch - '0', &key->key_state);
604 pressed_key.scan_code = ch - 'P' + 11;
609 case 'A'...'D': /* up, down right, left */
610 pressed_key.scan_code = ch - 'A' + 1;
613 pressed_key.scan_code = 6;
616 pressed_key.scan_code = 5;
619 ch = analyze_modifiers(&key->key_state);
621 case '1'...'5': /* F1 - F5 */
622 pressed_key.scan_code = ch - '1' + 11;
624 case '6'...'9': /* F5 - F8 */
625 pressed_key.scan_code = ch - '6' + 15;
627 case 'A'...'D': /* up, down right, left */
628 pressed_key.scan_code = ch - 'A' + 1;
631 pressed_key.scan_code = 6;
634 pressed_key.scan_code = 5;
637 pressed_key.scan_code = 5;
642 ch = analyze_modifiers(&key->key_state);
644 case '0'...'1': /* F9 - F10 */
645 pressed_key.scan_code = ch - '0' + 19;
647 case '3'...'4': /* F11 - F12 */
648 pressed_key.scan_code = ch - '3' + 21;
651 pressed_key.scan_code = 7;
656 pressed_key.scan_code = 8;
657 analyze_modifiers(&key->key_state);
659 case '5': /* PG UP */
660 pressed_key.scan_code = 9;
661 analyze_modifiers(&key->key_state);
663 case '6': /* PG DOWN */
664 pressed_key.scan_code = 10;
665 analyze_modifiers(&key->key_state);
671 set_shift_mask(3, &key->key_state);
678 if (pressed_key.scan_code) {
679 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
681 pressed_key.unicode_char = ch;
684 * Assume left control key for control characters typically
685 * entered using the control key.
687 if (ch >= 0x01 && ch <= 0x1f) {
688 key->key_state.key_shift_state |=
689 EFI_SHIFT_STATE_VALID;
694 key->key_state.key_shift_state |=
695 EFI_LEFT_CONTROL_PRESSED;
699 key->key = pressed_key;
705 * efi_cin_notify() - notify registered functions
707 static void efi_cin_notify(void)
709 struct efi_cin_notify_function *item;
711 list_for_each_entry(item, &cin_notify_functions, link) {
714 /* We do not support toggle states */
715 if (item->key.key.unicode_char || item->key.key.scan_code) {
716 if (item->key.key.unicode_char !=
717 next_key.key.unicode_char ||
718 item->key.key.scan_code != next_key.key.scan_code)
721 if (item->key.key_state.key_shift_state &&
722 item->key.key_state.key_shift_state !=
723 next_key.key_state.key_shift_state)
727 /* We don't bother about the return code */
728 EFI_CALL(item->function(&next_key));
733 * efi_cin_check() - check if keyboard input is available
735 static void efi_cin_check(void)
740 efi_signal_event(efi_con_in.wait_for_key);
745 ret = efi_cin_read_key(&next_key);
746 if (ret == EFI_SUCCESS) {
747 key_available = true;
749 /* Notify registered functions */
752 /* Queue the wait for key event */
754 efi_signal_event(efi_con_in.wait_for_key);
760 * efi_cin_empty_buffer() - empty input buffer
762 static void efi_cin_empty_buffer(void)
766 key_available = false;
770 * efi_cin_reset_ex() - reset console input
772 * @this: - the extended simple text input protocol
773 * @extended_verification: - extended verification
775 * This function implements the reset service of the
776 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
778 * See the Unified Extensible Firmware Interface (UEFI) specification for
781 * Return: old value of the task priority level
783 static efi_status_t EFIAPI efi_cin_reset_ex(
784 struct efi_simple_text_input_ex_protocol *this,
785 bool extended_verification)
787 efi_status_t ret = EFI_SUCCESS;
789 EFI_ENTRY("%p, %d", this, extended_verification);
791 /* Check parameters */
793 ret = EFI_INVALID_PARAMETER;
797 efi_cin_empty_buffer();
799 return EFI_EXIT(ret);
803 * efi_cin_read_key_stroke_ex() - read key stroke
805 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
806 * @key_data: key read from console
807 * Return: status code
809 * This function implements the ReadKeyStrokeEx service of the
810 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
812 * See the Unified Extensible Firmware Interface (UEFI) specification for
815 static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
816 struct efi_simple_text_input_ex_protocol *this,
817 struct efi_key_data *key_data)
819 efi_status_t ret = EFI_SUCCESS;
821 EFI_ENTRY("%p, %p", this, key_data);
823 /* Check parameters */
824 if (!this || !key_data) {
825 ret = EFI_INVALID_PARAMETER;
829 /* We don't do interrupts, so check for timers cooperatively */
832 /* Enable console input after ExitBootServices */
835 if (!key_available) {
840 * CTRL+A - CTRL+Z have to be signaled as a - z.
841 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
843 switch (next_key.key.unicode_char) {
847 if (!(next_key.key_state.key_toggle_state &
848 EFI_CAPS_LOCK_ACTIVE) ^
849 !(next_key.key_state.key_shift_state &
850 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
851 next_key.key.unicode_char += 0x40;
853 next_key.key.unicode_char += 0x60;
855 *key_data = next_key;
856 key_available = false;
857 efi_con_in.wait_for_key->is_signaled = false;
860 return EFI_EXIT(ret);
864 * efi_cin_set_state() - set toggle key state
866 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
867 * @key_toggle_state: pointer to key toggle state
868 * Return: status code
870 * This function implements the SetState service of the
871 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
873 * See the Unified Extensible Firmware Interface (UEFI) specification for
876 static efi_status_t EFIAPI efi_cin_set_state(
877 struct efi_simple_text_input_ex_protocol *this,
878 u8 *key_toggle_state)
880 EFI_ENTRY("%p, %p", this, key_toggle_state);
882 * U-Boot supports multiple console input sources like serial and
883 * net console for which a key toggle state cannot be set at all.
885 * According to the UEFI specification it is allowable to not implement
888 return EFI_EXIT(EFI_UNSUPPORTED);
892 * efi_cin_register_key_notify() - register key notification function
894 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
895 * @key_data: key to be notified
896 * @key_notify_function: function to be called if the key is pressed
897 * @notify_handle: handle for unregistering the notification
898 * Return: status code
900 * This function implements the SetState service of the
901 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
903 * See the Unified Extensible Firmware Interface (UEFI) specification for
906 static efi_status_t EFIAPI efi_cin_register_key_notify(
907 struct efi_simple_text_input_ex_protocol *this,
908 struct efi_key_data *key_data,
909 efi_status_t (EFIAPI *key_notify_function)(
910 struct efi_key_data *key_data),
911 void **notify_handle)
913 efi_status_t ret = EFI_SUCCESS;
914 struct efi_cin_notify_function *notify_function;
916 EFI_ENTRY("%p, %p, %p, %p",
917 this, key_data, key_notify_function, notify_handle);
919 /* Check parameters */
920 if (!this || !key_data || !key_notify_function || !notify_handle) {
921 ret = EFI_INVALID_PARAMETER;
925 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
926 key_data->key.unicode_char,
927 key_data->key.scan_code,
928 key_data->key_state.key_shift_state,
929 key_data->key_state.key_toggle_state);
931 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
932 if (!notify_function) {
933 ret = EFI_OUT_OF_RESOURCES;
936 notify_function->key = *key_data;
937 notify_function->function = key_notify_function;
938 list_add_tail(¬ify_function->link, &cin_notify_functions);
939 *notify_handle = notify_function;
941 return EFI_EXIT(ret);
945 * efi_cin_unregister_key_notify() - unregister key notification function
947 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
948 * @notification_handle: handle received when registering
949 * Return: status code
951 * This function implements the SetState service of the
952 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
954 * See the Unified Extensible Firmware Interface (UEFI) specification for
957 static efi_status_t EFIAPI efi_cin_unregister_key_notify(
958 struct efi_simple_text_input_ex_protocol *this,
959 void *notification_handle)
961 efi_status_t ret = EFI_INVALID_PARAMETER;
962 struct efi_cin_notify_function *item, *notify_function =
965 EFI_ENTRY("%p, %p", this, notification_handle);
967 /* Check parameters */
968 if (!this || !notification_handle)
971 list_for_each_entry(item, &cin_notify_functions, link) {
972 if (item == notify_function) {
977 if (ret != EFI_SUCCESS)
980 /* Remove the notify function */
981 list_del(¬ify_function->link);
982 free(notify_function);
984 return EFI_EXIT(ret);
989 * efi_cin_reset() - drain the input buffer
991 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
992 * @extended_verification: allow for exhaustive verification
993 * Return: status code
995 * This function implements the Reset service of the
996 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
998 * See the Unified Extensible Firmware Interface (UEFI) specification for
1001 static efi_status_t EFIAPI efi_cin_reset
1002 (struct efi_simple_text_input_protocol *this,
1003 bool extended_verification)
1005 efi_status_t ret = EFI_SUCCESS;
1007 EFI_ENTRY("%p, %d", this, extended_verification);
1009 /* Check parameters */
1011 ret = EFI_INVALID_PARAMETER;
1015 efi_cin_empty_buffer();
1017 return EFI_EXIT(ret);
1021 * efi_cin_read_key_stroke() - read key stroke
1023 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1024 * @key: key read from console
1025 * Return: status code
1027 * This function implements the ReadKeyStroke service of the
1028 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1030 * See the Unified Extensible Firmware Interface (UEFI) specification for
1033 static efi_status_t EFIAPI efi_cin_read_key_stroke
1034 (struct efi_simple_text_input_protocol *this,
1035 struct efi_input_key *key)
1037 efi_status_t ret = EFI_SUCCESS;
1039 EFI_ENTRY("%p, %p", this, key);
1041 /* Check parameters */
1042 if (!this || !key) {
1043 ret = EFI_INVALID_PARAMETER;
1047 /* We don't do interrupts, so check for timers cooperatively */
1050 /* Enable console input after ExitBootServices */
1053 if (!key_available) {
1054 ret = EFI_NOT_READY;
1057 *key = next_key.key;
1058 key_available = false;
1059 efi_con_in.wait_for_key->is_signaled = false;
1061 return EFI_EXIT(ret);
1064 static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1065 .reset = efi_cin_reset_ex,
1066 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1067 .wait_for_key_ex = NULL,
1068 .set_state = efi_cin_set_state,
1069 .register_key_notify = efi_cin_register_key_notify,
1070 .unregister_key_notify = efi_cin_unregister_key_notify,
1073 struct efi_simple_text_input_protocol efi_con_in = {
1074 .reset = efi_cin_reset,
1075 .read_key_stroke = efi_cin_read_key_stroke,
1076 .wait_for_key = NULL,
1079 static struct efi_event *console_timer_event;
1082 * efi_console_timer_notify() - notify the console timer event
1084 * @event: console timer event
1085 * @context: not used
1087 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1090 EFI_ENTRY("%p, %p", event, context);
1092 EFI_EXIT(EFI_SUCCESS);
1096 * efi_key_notify() - notify the wait for key event
1098 * @event: wait for key event
1099 * @context: not used
1101 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1103 EFI_ENTRY("%p, %p", event, context);
1105 EFI_EXIT(EFI_SUCCESS);
1109 * efi_console_register() - install the console protocols
1111 * This function is called from do_bootefi_exec().
1113 * Return: status code
1115 efi_status_t efi_console_register(void)
1118 efi_handle_t console_output_handle;
1119 efi_handle_t console_input_handle;
1121 /* Set up mode information */
1122 query_console_size();
1124 /* Create handles */
1125 r = efi_create_handle(&console_output_handle);
1126 if (r != EFI_SUCCESS)
1129 r = efi_add_protocol(console_output_handle,
1130 &efi_guid_text_output_protocol, &efi_con_out);
1131 if (r != EFI_SUCCESS)
1133 systab.con_out_handle = console_output_handle;
1134 systab.stderr_handle = console_output_handle;
1136 r = efi_create_handle(&console_input_handle);
1137 if (r != EFI_SUCCESS)
1140 r = efi_add_protocol(console_input_handle,
1141 &efi_guid_text_input_protocol, &efi_con_in);
1142 if (r != EFI_SUCCESS)
1144 systab.con_in_handle = console_input_handle;
1145 r = efi_add_protocol(console_input_handle,
1146 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1147 if (r != EFI_SUCCESS)
1150 /* Create console events */
1151 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1152 NULL, NULL, &efi_con_in.wait_for_key);
1153 if (r != EFI_SUCCESS) {
1154 printf("ERROR: Failed to register WaitForKey event\n");
1157 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
1158 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
1159 efi_console_timer_notify, NULL, NULL,
1160 &console_timer_event);
1161 if (r != EFI_SUCCESS) {
1162 printf("ERROR: Failed to register console event\n");
1165 /* 5000 ns cycle is sufficient for 2 MBaud */
1166 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1167 if (r != EFI_SUCCESS)
1168 printf("ERROR: Failed to set console timer\n");
1171 printf("ERROR: Out of memory\n");