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);
345 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
347 printf(ESC"[0;37;40m");
349 return EFI_EXIT(EFI_SUCCESS);
352 static efi_status_t EFIAPI efi_cout_clear_screen(
353 struct efi_simple_text_output_protocol *this)
355 EFI_ENTRY("%p", this);
358 efi_con_mode.cursor_column = 0;
359 efi_con_mode.cursor_row = 0;
361 return EFI_EXIT(EFI_SUCCESS);
364 static efi_status_t EFIAPI efi_cout_set_mode(
365 struct efi_simple_text_output_protocol *this,
366 unsigned long mode_number)
368 EFI_ENTRY("%p, %ld", this, mode_number);
370 if (mode_number >= efi_con_mode.max_mode)
371 return EFI_EXIT(EFI_UNSUPPORTED);
372 efi_con_mode.mode = mode_number;
373 EFI_CALL(efi_cout_clear_screen(this));
375 return EFI_EXIT(EFI_SUCCESS);
378 static efi_status_t EFIAPI efi_cout_reset(
379 struct efi_simple_text_output_protocol *this,
380 char extended_verification)
382 EFI_ENTRY("%p, %d", this, extended_verification);
385 EFI_CALL(efi_cout_clear_screen(this));
386 /* Set default colors */
387 printf(ESC "[0;37;40m");
389 return EFI_EXIT(EFI_SUCCESS);
392 static efi_status_t EFIAPI efi_cout_set_cursor_position(
393 struct efi_simple_text_output_protocol *this,
394 unsigned long column, unsigned long row)
396 efi_status_t ret = EFI_SUCCESS;
397 struct simple_text_output_mode *con = &efi_con_mode;
398 struct cout_mode *mode = &efi_cout_modes[con->mode];
400 EFI_ENTRY("%p, %ld, %ld", this, column, row);
402 /* Check parameters */
404 ret = EFI_INVALID_PARAMETER;
407 if (row >= mode->rows || column >= mode->columns) {
408 ret = EFI_UNSUPPORTED;
413 * Set cursor position by sending CSI H.
414 * EFI origin is [0, 0], terminal origin is [1, 1].
416 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
417 efi_con_mode.cursor_column = column;
418 efi_con_mode.cursor_row = row;
420 return EFI_EXIT(ret);
423 static efi_status_t EFIAPI efi_cout_enable_cursor(
424 struct efi_simple_text_output_protocol *this,
427 EFI_ENTRY("%p, %d", this, enable);
429 printf(ESC"[?25%c", enable ? 'h' : 'l');
430 efi_con_mode.cursor_visible = !!enable;
432 return EFI_EXIT(EFI_SUCCESS);
435 struct efi_simple_text_output_protocol efi_con_out = {
436 .reset = efi_cout_reset,
437 .output_string = efi_cout_output_string,
438 .test_string = efi_cout_test_string,
439 .query_mode = efi_cout_query_mode,
440 .set_mode = efi_cout_set_mode,
441 .set_attribute = efi_cout_set_attribute,
442 .clear_screen = efi_cout_clear_screen,
443 .set_cursor_position = efi_cout_set_cursor_position,
444 .enable_cursor = efi_cout_enable_cursor,
445 .mode = (void*)&efi_con_mode,
449 * struct efi_cin_notify_function - registered console input notify function
451 * @link: link to list
452 * @data: key to notify
453 * @function: function to call
455 struct efi_cin_notify_function {
456 struct list_head link;
457 struct efi_key_data key;
458 efi_status_t (EFIAPI *function)
459 (struct efi_key_data *key_data);
462 static bool key_available;
463 static struct efi_key_data next_key;
464 static LIST_HEAD(cin_notify_functions);
467 * set_shift_mask() - set shift mask
469 * @mod: Xterm shift mask
471 void set_shift_mask(int mod, struct efi_key_state *key_state)
473 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
477 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
479 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
481 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
483 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
485 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
490 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
492 * This gets called when we have already parsed CSI.
494 * @modifiers: bit mask (shift, alt, ctrl)
495 * @return: the unmodified code
497 static int analyze_modifiers(struct efi_key_state *key_state)
499 int c, mod = 0, ret = 0;
523 set_shift_mask(mod, key_state);
530 * efi_cin_read_key() - read a key from the console input
532 * @key: - key received
533 * Return: - status code
535 static efi_status_t efi_cin_read_key(struct efi_key_data *key)
537 struct efi_input_key pressed_key = {
543 if (console_read_unicode(&ch))
544 return EFI_NOT_READY;
546 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
547 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
549 /* We do not support multi-word codes */
556 * Xterm Control Sequences
557 * https://www.xfree86.org/4.8.0/ctlseqs.html
562 pressed_key.scan_code = 23;
564 case 'O': /* F1 - F4 */
566 /* consider modifiers */
568 set_shift_mask(ch - '0', &key->key_state);
571 pressed_key.scan_code = ch - 'P' + 11;
576 case 'A'...'D': /* up, down right, left */
577 pressed_key.scan_code = ch - 'A' + 1;
580 pressed_key.scan_code = 6;
583 pressed_key.scan_code = 5;
586 ch = analyze_modifiers(&key->key_state);
588 case '1'...'5': /* F1 - F5 */
589 pressed_key.scan_code = ch - '1' + 11;
591 case '7'...'9': /* F6 - F8 */
592 pressed_key.scan_code = ch - '7' + 16;
594 case 'A'...'D': /* up, down right, left */
595 pressed_key.scan_code = ch - 'A' + 1;
598 pressed_key.scan_code = 6; /* End */
601 pressed_key.scan_code = 5; /* Home */
606 ch = analyze_modifiers(&key->key_state);
608 case '0'...'1': /* F9 - F10 */
609 pressed_key.scan_code = ch - '0' + 19;
611 case '3'...'4': /* F11 - F12 */
612 pressed_key.scan_code = ch - '3' + 21;
615 pressed_key.scan_code = 7;
620 pressed_key.scan_code = 8;
621 analyze_modifiers(&key->key_state);
623 case '5': /* PG UP */
624 pressed_key.scan_code = 9;
625 analyze_modifiers(&key->key_state);
627 case '6': /* PG DOWN */
628 pressed_key.scan_code = 10;
629 analyze_modifiers(&key->key_state);
635 set_shift_mask(3, &key->key_state);
642 if (pressed_key.scan_code) {
643 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
645 pressed_key.unicode_char = ch;
648 * Assume left control key for control characters typically
649 * entered using the control key.
651 if (ch >= 0x01 && ch <= 0x1f) {
652 key->key_state.key_shift_state |=
653 EFI_SHIFT_STATE_VALID;
658 key->key_state.key_shift_state |=
659 EFI_LEFT_CONTROL_PRESSED;
663 key->key = pressed_key;
669 * efi_cin_notify() - notify registered functions
671 static void efi_cin_notify(void)
673 struct efi_cin_notify_function *item;
675 list_for_each_entry(item, &cin_notify_functions, link) {
678 /* We do not support toggle states */
679 if (item->key.key.unicode_char || item->key.key.scan_code) {
680 if (item->key.key.unicode_char !=
681 next_key.key.unicode_char ||
682 item->key.key.scan_code != next_key.key.scan_code)
685 if (item->key.key_state.key_shift_state &&
686 item->key.key_state.key_shift_state !=
687 next_key.key_state.key_shift_state)
691 /* We don't bother about the return code */
692 EFI_CALL(item->function(&next_key));
697 * efi_cin_check() - check if keyboard input is available
699 static void efi_cin_check(void)
704 efi_signal_event(efi_con_in.wait_for_key);
709 ret = efi_cin_read_key(&next_key);
710 if (ret == EFI_SUCCESS) {
711 key_available = true;
713 /* Notify registered functions */
716 /* Queue the wait for key event */
718 efi_signal_event(efi_con_in.wait_for_key);
724 * efi_cin_empty_buffer() - empty input buffer
726 static void efi_cin_empty_buffer(void)
730 key_available = false;
734 * efi_cin_reset_ex() - reset console input
736 * @this: - the extended simple text input protocol
737 * @extended_verification: - extended verification
739 * This function implements the reset service of the
740 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
742 * See the Unified Extensible Firmware Interface (UEFI) specification for
745 * Return: old value of the task priority level
747 static efi_status_t EFIAPI efi_cin_reset_ex(
748 struct efi_simple_text_input_ex_protocol *this,
749 bool extended_verification)
751 efi_status_t ret = EFI_SUCCESS;
753 EFI_ENTRY("%p, %d", this, extended_verification);
755 /* Check parameters */
757 ret = EFI_INVALID_PARAMETER;
761 efi_cin_empty_buffer();
763 return EFI_EXIT(ret);
767 * efi_cin_read_key_stroke_ex() - read key stroke
769 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
770 * @key_data: key read from console
771 * Return: status code
773 * This function implements the ReadKeyStrokeEx service of the
774 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
776 * See the Unified Extensible Firmware Interface (UEFI) specification for
779 static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
780 struct efi_simple_text_input_ex_protocol *this,
781 struct efi_key_data *key_data)
783 efi_status_t ret = EFI_SUCCESS;
785 EFI_ENTRY("%p, %p", this, key_data);
787 /* Check parameters */
788 if (!this || !key_data) {
789 ret = EFI_INVALID_PARAMETER;
793 /* We don't do interrupts, so check for timers cooperatively */
796 /* Enable console input after ExitBootServices */
799 if (!key_available) {
804 * CTRL+A - CTRL+Z have to be signaled as a - z.
805 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
807 switch (next_key.key.unicode_char) {
811 if (!(next_key.key_state.key_toggle_state &
812 EFI_CAPS_LOCK_ACTIVE) ^
813 !(next_key.key_state.key_shift_state &
814 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
815 next_key.key.unicode_char += 0x40;
817 next_key.key.unicode_char += 0x60;
819 *key_data = next_key;
820 key_available = false;
821 efi_con_in.wait_for_key->is_signaled = false;
824 return EFI_EXIT(ret);
828 * efi_cin_set_state() - set toggle key state
830 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
831 * @key_toggle_state: pointer to key toggle state
832 * Return: status code
834 * This function implements the SetState service of the
835 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
837 * See the Unified Extensible Firmware Interface (UEFI) specification for
840 static efi_status_t EFIAPI efi_cin_set_state(
841 struct efi_simple_text_input_ex_protocol *this,
842 u8 *key_toggle_state)
844 EFI_ENTRY("%p, %p", this, key_toggle_state);
846 * U-Boot supports multiple console input sources like serial and
847 * net console for which a key toggle state cannot be set at all.
849 * According to the UEFI specification it is allowable to not implement
852 return EFI_EXIT(EFI_UNSUPPORTED);
856 * efi_cin_register_key_notify() - register key notification function
858 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
859 * @key_data: key to be notified
860 * @key_notify_function: function to be called if the key is pressed
861 * @notify_handle: handle for unregistering the notification
862 * Return: status code
864 * This function implements the SetState service of the
865 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
867 * See the Unified Extensible Firmware Interface (UEFI) specification for
870 static efi_status_t EFIAPI efi_cin_register_key_notify(
871 struct efi_simple_text_input_ex_protocol *this,
872 struct efi_key_data *key_data,
873 efi_status_t (EFIAPI *key_notify_function)(
874 struct efi_key_data *key_data),
875 void **notify_handle)
877 efi_status_t ret = EFI_SUCCESS;
878 struct efi_cin_notify_function *notify_function;
880 EFI_ENTRY("%p, %p, %p, %p",
881 this, key_data, key_notify_function, notify_handle);
883 /* Check parameters */
884 if (!this || !key_data || !key_notify_function || !notify_handle) {
885 ret = EFI_INVALID_PARAMETER;
889 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
890 key_data->key.unicode_char,
891 key_data->key.scan_code,
892 key_data->key_state.key_shift_state,
893 key_data->key_state.key_toggle_state);
895 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
896 if (!notify_function) {
897 ret = EFI_OUT_OF_RESOURCES;
900 notify_function->key = *key_data;
901 notify_function->function = key_notify_function;
902 list_add_tail(¬ify_function->link, &cin_notify_functions);
903 *notify_handle = notify_function;
905 return EFI_EXIT(ret);
909 * efi_cin_unregister_key_notify() - unregister key notification function
911 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
912 * @notification_handle: handle received when registering
913 * Return: status code
915 * This function implements the SetState service of the
916 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
918 * See the Unified Extensible Firmware Interface (UEFI) specification for
921 static efi_status_t EFIAPI efi_cin_unregister_key_notify(
922 struct efi_simple_text_input_ex_protocol *this,
923 void *notification_handle)
925 efi_status_t ret = EFI_INVALID_PARAMETER;
926 struct efi_cin_notify_function *item, *notify_function =
929 EFI_ENTRY("%p, %p", this, notification_handle);
931 /* Check parameters */
932 if (!this || !notification_handle)
935 list_for_each_entry(item, &cin_notify_functions, link) {
936 if (item == notify_function) {
941 if (ret != EFI_SUCCESS)
944 /* Remove the notify function */
945 list_del(¬ify_function->link);
946 free(notify_function);
948 return EFI_EXIT(ret);
953 * efi_cin_reset() - drain the input buffer
955 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
956 * @extended_verification: allow for exhaustive verification
957 * Return: status code
959 * This function implements the Reset service of the
960 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
962 * See the Unified Extensible Firmware Interface (UEFI) specification for
965 static efi_status_t EFIAPI efi_cin_reset
966 (struct efi_simple_text_input_protocol *this,
967 bool extended_verification)
969 efi_status_t ret = EFI_SUCCESS;
971 EFI_ENTRY("%p, %d", this, extended_verification);
973 /* Check parameters */
975 ret = EFI_INVALID_PARAMETER;
979 efi_cin_empty_buffer();
981 return EFI_EXIT(ret);
985 * efi_cin_read_key_stroke() - read key stroke
987 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
988 * @key: key read from console
989 * Return: status code
991 * This function implements the ReadKeyStroke service of the
992 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
994 * See the Unified Extensible Firmware Interface (UEFI) specification for
997 static efi_status_t EFIAPI efi_cin_read_key_stroke
998 (struct efi_simple_text_input_protocol *this,
999 struct efi_input_key *key)
1001 efi_status_t ret = EFI_SUCCESS;
1003 EFI_ENTRY("%p, %p", this, key);
1005 /* Check parameters */
1006 if (!this || !key) {
1007 ret = EFI_INVALID_PARAMETER;
1011 /* We don't do interrupts, so check for timers cooperatively */
1014 /* Enable console input after ExitBootServices */
1017 if (!key_available) {
1018 ret = EFI_NOT_READY;
1021 *key = next_key.key;
1022 key_available = false;
1023 efi_con_in.wait_for_key->is_signaled = false;
1025 return EFI_EXIT(ret);
1028 static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1029 .reset = efi_cin_reset_ex,
1030 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1031 .wait_for_key_ex = NULL,
1032 .set_state = efi_cin_set_state,
1033 .register_key_notify = efi_cin_register_key_notify,
1034 .unregister_key_notify = efi_cin_unregister_key_notify,
1037 struct efi_simple_text_input_protocol efi_con_in = {
1038 .reset = efi_cin_reset,
1039 .read_key_stroke = efi_cin_read_key_stroke,
1040 .wait_for_key = NULL,
1043 static struct efi_event *console_timer_event;
1046 * efi_console_timer_notify() - notify the console timer event
1048 * @event: console timer event
1049 * @context: not used
1051 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1054 EFI_ENTRY("%p, %p", event, context);
1056 EFI_EXIT(EFI_SUCCESS);
1060 * efi_key_notify() - notify the wait for key event
1062 * @event: wait for key event
1063 * @context: not used
1065 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1067 EFI_ENTRY("%p, %p", event, context);
1069 EFI_EXIT(EFI_SUCCESS);
1073 * efi_console_register() - install the console protocols
1075 * This function is called from do_bootefi_exec().
1077 * Return: status code
1079 efi_status_t efi_console_register(void)
1082 efi_handle_t console_output_handle;
1083 efi_handle_t console_input_handle;
1085 /* Set up mode information */
1086 query_console_size();
1088 /* Create handles */
1089 r = efi_create_handle(&console_output_handle);
1090 if (r != EFI_SUCCESS)
1093 r = efi_add_protocol(console_output_handle,
1094 &efi_guid_text_output_protocol, &efi_con_out);
1095 if (r != EFI_SUCCESS)
1097 systab.con_out_handle = console_output_handle;
1098 systab.stderr_handle = console_output_handle;
1100 r = efi_create_handle(&console_input_handle);
1101 if (r != EFI_SUCCESS)
1104 r = efi_add_protocol(console_input_handle,
1105 &efi_guid_text_input_protocol, &efi_con_in);
1106 if (r != EFI_SUCCESS)
1108 systab.con_in_handle = console_input_handle;
1109 r = efi_add_protocol(console_input_handle,
1110 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1111 if (r != EFI_SUCCESS)
1114 /* Create console events */
1115 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1116 NULL, NULL, &efi_con_in.wait_for_key);
1117 if (r != EFI_SUCCESS) {
1118 printf("ERROR: Failed to register WaitForKey event\n");
1121 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
1122 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
1123 efi_console_timer_notify, NULL, NULL,
1124 &console_timer_event);
1125 if (r != EFI_SUCCESS) {
1126 printf("ERROR: Failed to register console event\n");
1129 /* 5000 ns cycle is sufficient for 2 MBaud */
1130 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1131 if (r != EFI_SUCCESS)
1132 printf("ERROR: Failed to set console timer\n");
1135 printf("ERROR: Out of memory\n");