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>
13 #include <stdio_dev.h>
14 #include <video_console.h>
16 #define EFI_COUT_MODE_2 2
17 #define EFI_MAX_COUT_MODE 3
20 unsigned long columns;
25 static struct cout_mode efi_cout_modes[] = {
26 /* EFI Mode 0 is 80x25 and always present */
32 /* EFI Mode 1 is always 80x50 */
38 /* Value are unknown until we query the console */
46 const efi_guid_t efi_guid_text_input_ex_protocol =
47 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
48 const efi_guid_t efi_guid_text_input_protocol =
49 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
50 const efi_guid_t efi_guid_text_output_protocol =
51 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
56 /* Default to mode 0 */
57 static struct simple_text_output_mode efi_con_mode = {
66 static int term_get_char(s32 *c)
70 /* Wait up to 100 ms for a character */
71 timeout = timer_get_us() + 100000;
74 if (timer_get_us() > timeout)
82 * Receive and parse a reply from the terminal.
84 * @n: array of return values
85 * @num: number of return values expected
86 * @end_char: character indicating end of terminal message
87 * @return: non-zero indicates error
89 static int term_read_reply(int *n, int num, char end_char)
94 if (term_get_char(&c) || c != cESC)
97 if (term_get_char(&c) || c != '[')
102 if (!term_get_char(&c)) {
109 } else if (c == end_char) {
111 } else if (c > '9' || c < '0') {
115 /* Read one more decimal position */
128 static efi_status_t EFIAPI efi_cout_output_string(
129 struct efi_simple_text_output_protocol *this,
130 const efi_string_t string)
132 struct simple_text_output_mode *con = &efi_con_mode;
133 struct cout_mode *mode = &efi_cout_modes[con->mode];
136 efi_status_t ret = EFI_SUCCESS;
138 EFI_ENTRY("%p, %p", this, string);
140 if (!this || !string) {
141 ret = EFI_INVALID_PARAMETER;
145 buf = malloc(utf16_utf8_strlen(string) + 1);
147 ret = EFI_OUT_OF_RESOURCES;
151 utf16_utf8_strcpy(&pos, string);
156 * Update the cursor position.
158 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
159 * and U000D. All other control characters are ignored. Any non-control
160 * character increase the column by one.
162 for (p = string; *p; ++p) {
164 case '\b': /* U+0008, backspace */
165 if (con->cursor_column)
166 con->cursor_column--;
168 case '\n': /* U+000A, newline */
169 con->cursor_column = 0;
172 case '\r': /* U+000D, carriage-return */
173 con->cursor_column = 0;
175 case 0xd800 ... 0xdbff:
177 * Ignore high surrogates, we do not want to count a
178 * Unicode character twice.
182 /* Exclude control codes */
184 con->cursor_column++;
187 if (con->cursor_column >= mode->columns) {
188 con->cursor_column = 0;
192 * When we exceed the row count the terminal will scroll up one
193 * line. We have to adjust the cursor position.
195 if (con->cursor_row >= mode->rows && con->cursor_row)
200 return EFI_EXIT(ret);
203 static efi_status_t EFIAPI efi_cout_test_string(
204 struct efi_simple_text_output_protocol *this,
205 const efi_string_t string)
207 EFI_ENTRY("%p, %p", this, string);
208 return EFI_EXIT(EFI_SUCCESS);
211 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
216 return (mode->rows == rows) && (mode->columns == cols);
220 * query_console_serial() - query console size
222 * @rows: pointer to return number of rows
223 * @cols: pointer to return number of columns
224 * Returns: 0 on success
226 static int query_console_serial(int *rows, int *cols)
231 /* Empty input buffer */
236 * Not all terminals understand CSI [18t for querying the console size.
237 * We should adhere to escape sequences documented in the console_codes
238 * man page and the ECMA-48 standard.
240 * So here we follow a different approach. We position the cursor to the
241 * bottom right and query its position. Before leaving the function we
242 * restore the original cursor position.
244 printf(ESC "7" /* Save cursor position */
245 ESC "[r" /* Set scrolling region to full window */
246 ESC "[999;999H" /* Move to bottom right corner */
247 ESC "[6n"); /* Query cursor position */
249 /* Read {rows,cols} */
250 if (term_read_reply(n, 2, 'R')) {
258 printf(ESC "8"); /* Restore cursor position */
263 * Update the mode table.
265 * By default the only mode available is 80x25. If the console has at least 50
266 * lines, enable mode 80x50. If we can query the console size and it is neither
267 * 80x25 nor 80x50, set it as an additional mode.
269 static void query_console_size(void)
271 const char *stdout_name = env_get("stdout");
272 int rows = 25, cols = 80;
274 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
275 IS_ENABLED(CONFIG_DM_VIDEO)) {
276 struct stdio_dev *stdout_dev =
277 stdio_get_by_name("vidconsole");
278 struct udevice *dev = stdout_dev->priv;
279 struct vidconsole_priv *priv =
280 dev_get_uclass_priv(dev);
283 } else if (query_console_serial(&rows, &cols)) {
287 /* Test if we can have Mode 1 */
288 if (cols >= 80 && rows >= 50) {
289 efi_cout_modes[1].present = 1;
290 efi_con_mode.max_mode = 2;
294 * Install our mode as mode 2 if it is different
295 * than mode 0 or 1 and set it as the currently selected mode
297 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
298 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
299 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
300 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
301 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
302 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
303 efi_con_mode.mode = EFI_COUT_MODE_2;
307 static efi_status_t EFIAPI efi_cout_query_mode(
308 struct efi_simple_text_output_protocol *this,
309 unsigned long mode_number, unsigned long *columns,
312 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
314 if (mode_number >= efi_con_mode.max_mode)
315 return EFI_EXIT(EFI_UNSUPPORTED);
317 if (efi_cout_modes[mode_number].present != 1)
318 return EFI_EXIT(EFI_UNSUPPORTED);
321 *columns = efi_cout_modes[mode_number].columns;
323 *rows = efi_cout_modes[mode_number].rows;
325 return EFI_EXIT(EFI_SUCCESS);
328 static const struct {
332 { 30, 40 }, /* 0: black */
333 { 34, 44 }, /* 1: blue */
334 { 32, 42 }, /* 2: green */
335 { 36, 46 }, /* 3: cyan */
336 { 31, 41 }, /* 4: red */
337 { 35, 45 }, /* 5: magenta */
338 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
339 { 37, 47 }, /* 7: light gray, map to white */
342 /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
343 static efi_status_t EFIAPI efi_cout_set_attribute(
344 struct efi_simple_text_output_protocol *this,
345 unsigned long attribute)
347 unsigned int bold = EFI_ATTR_BOLD(attribute);
348 unsigned int fg = EFI_ATTR_FG(attribute);
349 unsigned int bg = EFI_ATTR_BG(attribute);
351 EFI_ENTRY("%p, %lx", this, attribute);
353 efi_con_mode.attribute = attribute;
355 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
357 printf(ESC"[0;37;40m");
359 return EFI_EXIT(EFI_SUCCESS);
362 static efi_status_t EFIAPI efi_cout_clear_screen(
363 struct efi_simple_text_output_protocol *this)
365 EFI_ENTRY("%p", this);
368 efi_con_mode.cursor_column = 0;
369 efi_con_mode.cursor_row = 0;
371 return EFI_EXIT(EFI_SUCCESS);
374 static efi_status_t EFIAPI efi_cout_set_mode(
375 struct efi_simple_text_output_protocol *this,
376 unsigned long mode_number)
378 EFI_ENTRY("%p, %ld", this, mode_number);
380 if (mode_number >= efi_con_mode.max_mode)
381 return EFI_EXIT(EFI_UNSUPPORTED);
383 if (!efi_cout_modes[mode_number].present)
384 return EFI_EXIT(EFI_UNSUPPORTED);
386 efi_con_mode.mode = mode_number;
387 EFI_CALL(efi_cout_clear_screen(this));
389 return EFI_EXIT(EFI_SUCCESS);
392 static efi_status_t EFIAPI efi_cout_reset(
393 struct efi_simple_text_output_protocol *this,
394 char extended_verification)
396 EFI_ENTRY("%p, %d", this, extended_verification);
399 EFI_CALL(efi_cout_clear_screen(this));
400 /* Set default colors */
401 efi_con_mode.attribute = 0x07;
402 printf(ESC "[0;37;40m");
404 return EFI_EXIT(EFI_SUCCESS);
407 static efi_status_t EFIAPI efi_cout_set_cursor_position(
408 struct efi_simple_text_output_protocol *this,
409 unsigned long column, unsigned long row)
411 efi_status_t ret = EFI_SUCCESS;
412 struct simple_text_output_mode *con = &efi_con_mode;
413 struct cout_mode *mode = &efi_cout_modes[con->mode];
415 EFI_ENTRY("%p, %ld, %ld", this, column, row);
417 /* Check parameters */
419 ret = EFI_INVALID_PARAMETER;
422 if (row >= mode->rows || column >= mode->columns) {
423 ret = EFI_UNSUPPORTED;
428 * Set cursor position by sending CSI H.
429 * EFI origin is [0, 0], terminal origin is [1, 1].
431 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
432 efi_con_mode.cursor_column = column;
433 efi_con_mode.cursor_row = row;
435 return EFI_EXIT(ret);
438 static efi_status_t EFIAPI efi_cout_enable_cursor(
439 struct efi_simple_text_output_protocol *this,
442 EFI_ENTRY("%p, %d", this, enable);
444 printf(ESC"[?25%c", enable ? 'h' : 'l');
445 efi_con_mode.cursor_visible = !!enable;
447 return EFI_EXIT(EFI_SUCCESS);
450 struct efi_simple_text_output_protocol efi_con_out = {
451 .reset = efi_cout_reset,
452 .output_string = efi_cout_output_string,
453 .test_string = efi_cout_test_string,
454 .query_mode = efi_cout_query_mode,
455 .set_mode = efi_cout_set_mode,
456 .set_attribute = efi_cout_set_attribute,
457 .clear_screen = efi_cout_clear_screen,
458 .set_cursor_position = efi_cout_set_cursor_position,
459 .enable_cursor = efi_cout_enable_cursor,
460 .mode = (void*)&efi_con_mode,
464 * struct efi_cin_notify_function - registered console input notify function
466 * @link: link to list
467 * @key: key to notify
468 * @function: function to call
470 struct efi_cin_notify_function {
471 struct list_head link;
472 struct efi_key_data key;
473 efi_status_t (EFIAPI *function)
474 (struct efi_key_data *key_data);
477 static bool key_available;
478 static struct efi_key_data next_key;
479 static LIST_HEAD(cin_notify_functions);
482 * set_shift_mask() - set shift mask
484 * @mod: Xterm shift mask
485 * @key_state: receives the state of the shift, alt, control, and logo keys
487 void set_shift_mask(int mod, struct efi_key_state *key_state)
489 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
493 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
495 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
497 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
498 if (!mod || (mod & 8))
499 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
504 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
506 * This gets called when we have already parsed CSI.
508 * @key_state: receives the state of the shift, alt, control, and logo keys
509 * @return: the unmodified code
511 static int analyze_modifiers(struct efi_key_state *key_state)
513 int c, mod = 0, ret = 0;
537 set_shift_mask(mod, key_state);
544 * efi_cin_read_key() - read a key from the console input
546 * @key: - key received
547 * Return: - status code
549 static efi_status_t efi_cin_read_key(struct efi_key_data *key)
551 struct efi_input_key pressed_key = {
557 if (console_read_unicode(&ch))
558 return EFI_NOT_READY;
560 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
561 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
563 /* We do not support multi-word codes */
570 * Xterm Control Sequences
571 * https://www.xfree86.org/4.8.0/ctlseqs.html
576 pressed_key.scan_code = 23;
578 case 'O': /* F1 - F4, End */
580 /* consider modifiers */
581 if (ch == 'F') { /* End */
582 pressed_key.scan_code = 6;
584 } else if (ch < 'P') {
585 set_shift_mask(ch - '0', &key->key_state);
588 pressed_key.scan_code = ch - 'P' + 11;
593 case 'A'...'D': /* up, down right, left */
594 pressed_key.scan_code = ch - 'A' + 1;
597 pressed_key.scan_code = 6;
600 pressed_key.scan_code = 5;
603 ch = analyze_modifiers(&key->key_state);
605 case '1'...'5': /* F1 - F5 */
606 pressed_key.scan_code = ch - '1' + 11;
608 case '6'...'9': /* F5 - F8 */
609 pressed_key.scan_code = ch - '6' + 15;
611 case 'A'...'D': /* up, down right, left */
612 pressed_key.scan_code = ch - 'A' + 1;
615 pressed_key.scan_code = 6;
618 pressed_key.scan_code = 5;
621 pressed_key.scan_code = 5;
626 ch = analyze_modifiers(&key->key_state);
628 case '0'...'1': /* F9 - F10 */
629 pressed_key.scan_code = ch - '0' + 19;
631 case '3'...'4': /* F11 - F12 */
632 pressed_key.scan_code = ch - '3' + 21;
635 pressed_key.scan_code = 7;
640 pressed_key.scan_code = 8;
641 analyze_modifiers(&key->key_state);
643 case '5': /* PG UP */
644 pressed_key.scan_code = 9;
645 analyze_modifiers(&key->key_state);
647 case '6': /* PG DOWN */
648 pressed_key.scan_code = 10;
649 analyze_modifiers(&key->key_state);
655 set_shift_mask(3, &key->key_state);
662 if (pressed_key.scan_code) {
663 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
665 pressed_key.unicode_char = ch;
668 * Assume left control key for control characters typically
669 * entered using the control key.
671 if (ch >= 0x01 && ch <= 0x1f) {
672 key->key_state.key_shift_state |=
673 EFI_SHIFT_STATE_VALID;
678 key->key_state.key_shift_state |=
679 EFI_LEFT_CONTROL_PRESSED;
683 key->key = pressed_key;
689 * efi_cin_notify() - notify registered functions
691 static void efi_cin_notify(void)
693 struct efi_cin_notify_function *item;
695 list_for_each_entry(item, &cin_notify_functions, link) {
698 /* We do not support toggle states */
699 if (item->key.key.unicode_char || item->key.key.scan_code) {
700 if (item->key.key.unicode_char !=
701 next_key.key.unicode_char ||
702 item->key.key.scan_code != next_key.key.scan_code)
705 if (item->key.key_state.key_shift_state &&
706 item->key.key_state.key_shift_state !=
707 next_key.key_state.key_shift_state)
711 /* We don't bother about the return code */
712 EFI_CALL(item->function(&next_key));
717 * efi_cin_check() - check if keyboard input is available
719 static void efi_cin_check(void)
724 efi_signal_event(efi_con_in.wait_for_key);
729 ret = efi_cin_read_key(&next_key);
730 if (ret == EFI_SUCCESS) {
731 key_available = true;
733 /* Notify registered functions */
736 /* Queue the wait for key event */
738 efi_signal_event(efi_con_in.wait_for_key);
744 * efi_cin_empty_buffer() - empty input buffer
746 static void efi_cin_empty_buffer(void)
750 key_available = false;
754 * efi_cin_reset_ex() - reset console input
756 * @this: - the extended simple text input protocol
757 * @extended_verification: - extended verification
759 * This function implements the reset service of the
760 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
762 * See the Unified Extensible Firmware Interface (UEFI) specification for
765 * Return: old value of the task priority level
767 static efi_status_t EFIAPI efi_cin_reset_ex(
768 struct efi_simple_text_input_ex_protocol *this,
769 bool extended_verification)
771 efi_status_t ret = EFI_SUCCESS;
773 EFI_ENTRY("%p, %d", this, extended_verification);
775 /* Check parameters */
777 ret = EFI_INVALID_PARAMETER;
781 efi_cin_empty_buffer();
783 return EFI_EXIT(ret);
787 * efi_cin_read_key_stroke_ex() - read key stroke
789 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
790 * @key_data: key read from console
791 * Return: status code
793 * This function implements the ReadKeyStrokeEx service of the
794 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
796 * See the Unified Extensible Firmware Interface (UEFI) specification for
799 static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
800 struct efi_simple_text_input_ex_protocol *this,
801 struct efi_key_data *key_data)
803 efi_status_t ret = EFI_SUCCESS;
805 EFI_ENTRY("%p, %p", this, key_data);
807 /* Check parameters */
808 if (!this || !key_data) {
809 ret = EFI_INVALID_PARAMETER;
813 /* We don't do interrupts, so check for timers cooperatively */
816 /* Enable console input after ExitBootServices */
819 if (!key_available) {
824 * CTRL+A - CTRL+Z have to be signaled as a - z.
825 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
827 switch (next_key.key.unicode_char) {
831 if (!(next_key.key_state.key_toggle_state &
832 EFI_CAPS_LOCK_ACTIVE) ^
833 !(next_key.key_state.key_shift_state &
834 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
835 next_key.key.unicode_char += 0x40;
837 next_key.key.unicode_char += 0x60;
839 *key_data = next_key;
840 key_available = false;
841 efi_con_in.wait_for_key->is_signaled = false;
844 return EFI_EXIT(ret);
848 * efi_cin_set_state() - set toggle key state
850 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
851 * @key_toggle_state: pointer to key toggle state
852 * Return: status code
854 * This function implements the SetState service of the
855 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
857 * See the Unified Extensible Firmware Interface (UEFI) specification for
860 static efi_status_t EFIAPI efi_cin_set_state(
861 struct efi_simple_text_input_ex_protocol *this,
862 u8 *key_toggle_state)
864 EFI_ENTRY("%p, %p", this, key_toggle_state);
866 * U-Boot supports multiple console input sources like serial and
867 * net console for which a key toggle state cannot be set at all.
869 * According to the UEFI specification it is allowable to not implement
872 return EFI_EXIT(EFI_UNSUPPORTED);
876 * efi_cin_register_key_notify() - register key notification function
878 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
879 * @key_data: key to be notified
880 * @key_notify_function: function to be called if the key is pressed
881 * @notify_handle: handle for unregistering the notification
882 * Return: status code
884 * This function implements the SetState service of the
885 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
887 * See the Unified Extensible Firmware Interface (UEFI) specification for
890 static efi_status_t EFIAPI efi_cin_register_key_notify(
891 struct efi_simple_text_input_ex_protocol *this,
892 struct efi_key_data *key_data,
893 efi_status_t (EFIAPI *key_notify_function)(
894 struct efi_key_data *key_data),
895 void **notify_handle)
897 efi_status_t ret = EFI_SUCCESS;
898 struct efi_cin_notify_function *notify_function;
900 EFI_ENTRY("%p, %p, %p, %p",
901 this, key_data, key_notify_function, notify_handle);
903 /* Check parameters */
904 if (!this || !key_data || !key_notify_function || !notify_handle) {
905 ret = EFI_INVALID_PARAMETER;
909 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
910 key_data->key.unicode_char,
911 key_data->key.scan_code,
912 key_data->key_state.key_shift_state,
913 key_data->key_state.key_toggle_state);
915 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
916 if (!notify_function) {
917 ret = EFI_OUT_OF_RESOURCES;
920 notify_function->key = *key_data;
921 notify_function->function = key_notify_function;
922 list_add_tail(¬ify_function->link, &cin_notify_functions);
923 *notify_handle = notify_function;
925 return EFI_EXIT(ret);
929 * efi_cin_unregister_key_notify() - unregister key notification function
931 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
932 * @notification_handle: handle received when registering
933 * Return: status code
935 * This function implements the SetState service of the
936 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
938 * See the Unified Extensible Firmware Interface (UEFI) specification for
941 static efi_status_t EFIAPI efi_cin_unregister_key_notify(
942 struct efi_simple_text_input_ex_protocol *this,
943 void *notification_handle)
945 efi_status_t ret = EFI_INVALID_PARAMETER;
946 struct efi_cin_notify_function *item, *notify_function =
949 EFI_ENTRY("%p, %p", this, notification_handle);
951 /* Check parameters */
952 if (!this || !notification_handle)
955 list_for_each_entry(item, &cin_notify_functions, link) {
956 if (item == notify_function) {
961 if (ret != EFI_SUCCESS)
964 /* Remove the notify function */
965 list_del(¬ify_function->link);
966 free(notify_function);
968 return EFI_EXIT(ret);
973 * efi_cin_reset() - drain the input buffer
975 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
976 * @extended_verification: allow for exhaustive verification
977 * Return: status code
979 * This function implements the Reset service of the
980 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
982 * See the Unified Extensible Firmware Interface (UEFI) specification for
985 static efi_status_t EFIAPI efi_cin_reset
986 (struct efi_simple_text_input_protocol *this,
987 bool extended_verification)
989 efi_status_t ret = EFI_SUCCESS;
991 EFI_ENTRY("%p, %d", this, extended_verification);
993 /* Check parameters */
995 ret = EFI_INVALID_PARAMETER;
999 efi_cin_empty_buffer();
1001 return EFI_EXIT(ret);
1005 * efi_cin_read_key_stroke() - read key stroke
1007 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1008 * @key: key read from console
1009 * Return: status code
1011 * This function implements the ReadKeyStroke service of the
1012 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1014 * See the Unified Extensible Firmware Interface (UEFI) specification for
1017 static efi_status_t EFIAPI efi_cin_read_key_stroke
1018 (struct efi_simple_text_input_protocol *this,
1019 struct efi_input_key *key)
1021 efi_status_t ret = EFI_SUCCESS;
1023 EFI_ENTRY("%p, %p", this, key);
1025 /* Check parameters */
1026 if (!this || !key) {
1027 ret = EFI_INVALID_PARAMETER;
1031 /* We don't do interrupts, so check for timers cooperatively */
1034 /* Enable console input after ExitBootServices */
1037 if (!key_available) {
1038 ret = EFI_NOT_READY;
1041 *key = next_key.key;
1042 key_available = false;
1043 efi_con_in.wait_for_key->is_signaled = false;
1045 return EFI_EXIT(ret);
1048 static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1049 .reset = efi_cin_reset_ex,
1050 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1051 .wait_for_key_ex = NULL,
1052 .set_state = efi_cin_set_state,
1053 .register_key_notify = efi_cin_register_key_notify,
1054 .unregister_key_notify = efi_cin_unregister_key_notify,
1057 struct efi_simple_text_input_protocol efi_con_in = {
1058 .reset = efi_cin_reset,
1059 .read_key_stroke = efi_cin_read_key_stroke,
1060 .wait_for_key = NULL,
1063 static struct efi_event *console_timer_event;
1066 * efi_console_timer_notify() - notify the console timer event
1068 * @event: console timer event
1069 * @context: not used
1071 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1074 EFI_ENTRY("%p, %p", event, context);
1076 EFI_EXIT(EFI_SUCCESS);
1080 * efi_key_notify() - notify the wait for key event
1082 * @event: wait for key event
1083 * @context: not used
1085 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1087 EFI_ENTRY("%p, %p", event, context);
1089 EFI_EXIT(EFI_SUCCESS);
1093 * efi_console_register() - install the console protocols
1095 * This function is called from do_bootefi_exec().
1097 * Return: status code
1099 efi_status_t efi_console_register(void)
1102 efi_handle_t console_output_handle;
1103 efi_handle_t console_input_handle;
1105 /* Set up mode information */
1106 query_console_size();
1108 /* Create handles */
1109 r = efi_create_handle(&console_output_handle);
1110 if (r != EFI_SUCCESS)
1113 r = efi_add_protocol(console_output_handle,
1114 &efi_guid_text_output_protocol, &efi_con_out);
1115 if (r != EFI_SUCCESS)
1117 systab.con_out_handle = console_output_handle;
1118 systab.stderr_handle = console_output_handle;
1120 r = efi_create_handle(&console_input_handle);
1121 if (r != EFI_SUCCESS)
1124 r = efi_add_protocol(console_input_handle,
1125 &efi_guid_text_input_protocol, &efi_con_in);
1126 if (r != EFI_SUCCESS)
1128 systab.con_in_handle = console_input_handle;
1129 r = efi_add_protocol(console_input_handle,
1130 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1131 if (r != EFI_SUCCESS)
1134 /* Create console events */
1135 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1136 NULL, NULL, &efi_con_in.wait_for_key);
1137 if (r != EFI_SUCCESS) {
1138 printf("ERROR: Failed to register WaitForKey event\n");
1141 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
1142 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
1143 efi_console_timer_notify, NULL, NULL,
1144 &console_timer_event);
1145 if (r != EFI_SUCCESS) {
1146 printf("ERROR: Failed to register console event\n");
1149 /* 5000 ns cycle is sufficient for 2 MBaud */
1150 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1151 if (r != EFI_SUCCESS)
1152 printf("ERROR: Failed to set console timer\n");
1155 printf("ERROR: Out of memory\n");