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 */
131 * efi_cout_output_string() - write Unicode string to console
133 * This function implements the OutputString service of the simple text output
134 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
137 * @this: simple text output protocol
138 * @string: u16 string
139 * Return: status code
141 static efi_status_t EFIAPI efi_cout_output_string(
142 struct efi_simple_text_output_protocol *this,
143 const efi_string_t string)
145 struct simple_text_output_mode *con = &efi_con_mode;
146 struct cout_mode *mode = &efi_cout_modes[con->mode];
149 efi_status_t ret = EFI_SUCCESS;
151 EFI_ENTRY("%p, %p", this, string);
153 if (!this || !string) {
154 ret = EFI_INVALID_PARAMETER;
158 buf = malloc(utf16_utf8_strlen(string) + 1);
160 ret = EFI_OUT_OF_RESOURCES;
164 utf16_utf8_strcpy(&pos, string);
169 * Update the cursor position.
171 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
172 * and U000D. All other control characters are ignored. Any non-control
173 * character increase the column by one.
175 for (p = string; *p; ++p) {
177 case '\b': /* U+0008, backspace */
178 if (con->cursor_column)
179 con->cursor_column--;
181 case '\n': /* U+000A, newline */
182 con->cursor_column = 0;
185 case '\r': /* U+000D, carriage-return */
186 con->cursor_column = 0;
188 case 0xd800 ... 0xdbff:
190 * Ignore high surrogates, we do not want to count a
191 * Unicode character twice.
195 /* Exclude control codes */
197 con->cursor_column++;
200 if (con->cursor_column >= mode->columns) {
201 con->cursor_column = 0;
205 * When we exceed the row count the terminal will scroll up one
206 * line. We have to adjust the cursor position.
208 if (con->cursor_row >= mode->rows && con->cursor_row)
213 return EFI_EXIT(ret);
217 * efi_cout_test_string() - test writing Unicode string to console
219 * This function implements the TestString service of the simple text output
220 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
223 * As in OutputString we simply convert UTF-16 to UTF-8 there are no unsupported
224 * code points and we can always return EFI_SUCCESS.
226 * @this: simple text output protocol
227 * @string: u16 string
228 * Return: status code
230 static efi_status_t EFIAPI efi_cout_test_string(
231 struct efi_simple_text_output_protocol *this,
232 const efi_string_t string)
234 EFI_ENTRY("%p, %p", this, string);
235 return EFI_EXIT(EFI_SUCCESS);
239 * cout_mode_matches() - check if mode has given terminal size
242 * @rows: number of rows
243 * @cols: number of columns
244 * Return: true if number of rows and columns matches the mode and
245 * the mode is present
247 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
252 return (mode->rows == rows) && (mode->columns == cols);
256 * query_console_serial() - query console size
258 * When using a serial console or the net console we can only devise the
259 * terminal size by querying the terminal using ECMA-48 control sequences.
261 * @rows: pointer to return number of rows
262 * @cols: pointer to return number of columns
263 * Returns: 0 on success
265 static int query_console_serial(int *rows, int *cols)
270 /* Empty input buffer */
275 * Not all terminals understand CSI [18t for querying the console size.
276 * We should adhere to escape sequences documented in the console_codes
277 * man page and the ECMA-48 standard.
279 * So here we follow a different approach. We position the cursor to the
280 * bottom right and query its position. Before leaving the function we
281 * restore the original cursor position.
283 printf(ESC "7" /* Save cursor position */
284 ESC "[r" /* Set scrolling region to full window */
285 ESC "[999;999H" /* Move to bottom right corner */
286 ESC "[6n"); /* Query cursor position */
288 /* Read {rows,cols} */
289 if (term_read_reply(n, 2, 'R')) {
297 printf(ESC "8"); /* Restore cursor position */
302 * query_console_size() - update the mode table.
304 * By default the only mode available is 80x25. If the console has at least 50
305 * lines, enable mode 80x50. If we can query the console size and it is neither
306 * 80x25 nor 80x50, set it as an additional mode.
308 static void query_console_size(void)
310 const char *stdout_name = env_get("stdout");
311 int rows = 25, cols = 80;
313 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
314 IS_ENABLED(CONFIG_DM_VIDEO)) {
315 struct stdio_dev *stdout_dev =
316 stdio_get_by_name("vidconsole");
317 struct udevice *dev = stdout_dev->priv;
318 struct vidconsole_priv *priv =
319 dev_get_uclass_priv(dev);
322 } else if (query_console_serial(&rows, &cols)) {
326 /* Test if we can have Mode 1 */
327 if (cols >= 80 && rows >= 50) {
328 efi_cout_modes[1].present = 1;
329 efi_con_mode.max_mode = 2;
333 * Install our mode as mode 2 if it is different
334 * than mode 0 or 1 and set it as the currently selected mode
336 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
337 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
338 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
339 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
340 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
341 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
342 efi_con_mode.mode = EFI_COUT_MODE_2;
348 * efi_cout_query_mode() - get terminal size for a text mode
350 * This function implements the QueryMode service of the simple text output
351 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
354 * @this: simple text output protocol
355 * @mode_number: mode number to retrieve information on
356 * @columns: number of columns
357 * @rows: number of rows
358 * Return: status code
360 static efi_status_t EFIAPI efi_cout_query_mode(
361 struct efi_simple_text_output_protocol *this,
362 unsigned long mode_number, unsigned long *columns,
365 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
367 if (mode_number >= efi_con_mode.max_mode)
368 return EFI_EXIT(EFI_UNSUPPORTED);
370 if (efi_cout_modes[mode_number].present != 1)
371 return EFI_EXIT(EFI_UNSUPPORTED);
374 *columns = efi_cout_modes[mode_number].columns;
376 *rows = efi_cout_modes[mode_number].rows;
378 return EFI_EXIT(EFI_SUCCESS);
381 static const struct {
385 { 30, 40 }, /* 0: black */
386 { 34, 44 }, /* 1: blue */
387 { 32, 42 }, /* 2: green */
388 { 36, 46 }, /* 3: cyan */
389 { 31, 41 }, /* 4: red */
390 { 35, 45 }, /* 5: magenta */
391 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
392 { 37, 47 }, /* 7: light gray, map to white */
396 * efi_cout_set_attribute() - set fore- and background color
398 * This function implements the SetAttribute service of the simple text output
399 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
402 * @this: simple text output protocol
403 * @attribute: foreground color - bits 0-3, background color - bits 4-6
404 * Return: status code
406 static efi_status_t EFIAPI efi_cout_set_attribute(
407 struct efi_simple_text_output_protocol *this,
408 unsigned long attribute)
410 unsigned int bold = EFI_ATTR_BOLD(attribute);
411 unsigned int fg = EFI_ATTR_FG(attribute);
412 unsigned int bg = EFI_ATTR_BG(attribute);
414 EFI_ENTRY("%p, %lx", this, attribute);
416 efi_con_mode.attribute = attribute;
418 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
420 printf(ESC"[0;37;40m");
422 return EFI_EXIT(EFI_SUCCESS);
426 * efi_cout_clear_screen() - clear screen
428 * This function implements the ClearScreen service of the simple text output
429 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
432 * @this: pointer to the protocol instance
433 * Return: status code
435 static efi_status_t EFIAPI efi_cout_clear_screen(
436 struct efi_simple_text_output_protocol *this)
438 EFI_ENTRY("%p", this);
441 * The Linux console wants both a clear and a home command. The video
442 * uclass does not support <ESC>[H without coordinates, yet.
444 printf(ESC "[2J" ESC "[1;1H");
445 efi_con_mode.cursor_column = 0;
446 efi_con_mode.cursor_row = 0;
448 return EFI_EXIT(EFI_SUCCESS);
452 * efi_cout_clear_set_mode() - set text model
454 * This function implements the SetMode service of the simple text output
455 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
458 * @this: pointer to the protocol instance
459 * @mode_number: number of the text mode to set
460 * Return: status code
462 static efi_status_t EFIAPI efi_cout_set_mode(
463 struct efi_simple_text_output_protocol *this,
464 unsigned long mode_number)
466 EFI_ENTRY("%p, %ld", this, mode_number);
468 if (mode_number >= efi_con_mode.max_mode)
469 return EFI_EXIT(EFI_UNSUPPORTED);
471 if (!efi_cout_modes[mode_number].present)
472 return EFI_EXIT(EFI_UNSUPPORTED);
474 efi_con_mode.mode = mode_number;
475 EFI_CALL(efi_cout_clear_screen(this));
477 return EFI_EXIT(EFI_SUCCESS);
481 * efi_cout_reset() - reset the terminal
483 * This function implements the Reset service of the simple text output
484 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
487 * @this: pointer to the protocol instance
488 * @extended_verification: if set an extended verification may be executed
489 * Return: status code
491 static efi_status_t EFIAPI efi_cout_reset(
492 struct efi_simple_text_output_protocol *this,
493 char extended_verification)
495 EFI_ENTRY("%p, %d", this, extended_verification);
498 EFI_CALL(efi_cout_clear_screen(this));
499 /* Set default colors */
500 efi_con_mode.attribute = 0x07;
501 printf(ESC "[0;37;40m");
503 return EFI_EXIT(EFI_SUCCESS);
507 * efi_cout_set_cursor_position() - reset the terminal
509 * This function implements the SetCursorPosition service of the simple text
510 * output protocol. See the Unified Extensible Firmware Interface (UEFI)
511 * specification for details.
513 * @this: pointer to the protocol instance
514 * @column: column to move to
515 * @row: row to move to
516 * Return: status code
518 static efi_status_t EFIAPI efi_cout_set_cursor_position(
519 struct efi_simple_text_output_protocol *this,
520 unsigned long column, unsigned long row)
522 efi_status_t ret = EFI_SUCCESS;
523 struct simple_text_output_mode *con = &efi_con_mode;
524 struct cout_mode *mode = &efi_cout_modes[con->mode];
526 EFI_ENTRY("%p, %ld, %ld", this, column, row);
528 /* Check parameters */
530 ret = EFI_INVALID_PARAMETER;
533 if (row >= mode->rows || column >= mode->columns) {
534 ret = EFI_UNSUPPORTED;
539 * Set cursor position by sending CSI H.
540 * EFI origin is [0, 0], terminal origin is [1, 1].
542 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
543 efi_con_mode.cursor_column = column;
544 efi_con_mode.cursor_row = row;
546 return EFI_EXIT(ret);
550 * efi_cout_enable_cursor() - enable the cursor
552 * This function implements the EnableCursor service of the simple text output
553 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
556 * @this: pointer to the protocol instance
557 * @enable: if true enable, if false disable the cursor
558 * Return: status code
560 static efi_status_t EFIAPI efi_cout_enable_cursor(
561 struct efi_simple_text_output_protocol *this,
564 EFI_ENTRY("%p, %d", this, enable);
566 printf(ESC"[?25%c", enable ? 'h' : 'l');
567 efi_con_mode.cursor_visible = !!enable;
569 return EFI_EXIT(EFI_SUCCESS);
572 struct efi_simple_text_output_protocol efi_con_out = {
573 .reset = efi_cout_reset,
574 .output_string = efi_cout_output_string,
575 .test_string = efi_cout_test_string,
576 .query_mode = efi_cout_query_mode,
577 .set_mode = efi_cout_set_mode,
578 .set_attribute = efi_cout_set_attribute,
579 .clear_screen = efi_cout_clear_screen,
580 .set_cursor_position = efi_cout_set_cursor_position,
581 .enable_cursor = efi_cout_enable_cursor,
582 .mode = (void*)&efi_con_mode,
586 * struct efi_cin_notify_function - registered console input notify function
588 * @link: link to list
589 * @key: key to notify
590 * @function: function to call
592 struct efi_cin_notify_function {
593 struct list_head link;
594 struct efi_key_data key;
595 efi_status_t (EFIAPI *function)
596 (struct efi_key_data *key_data);
599 static bool key_available;
600 static struct efi_key_data next_key;
601 static LIST_HEAD(cin_notify_functions);
604 * set_shift_mask() - set shift mask
606 * @mod: Xterm shift mask
607 * @key_state: receives the state of the shift, alt, control, and logo keys
609 void set_shift_mask(int mod, struct efi_key_state *key_state)
611 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
615 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
617 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
619 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
620 if (!mod || (mod & 8))
621 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
626 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
628 * This gets called when we have already parsed CSI.
630 * @key_state: receives the state of the shift, alt, control, and logo keys
631 * Return: the unmodified code
633 static int analyze_modifiers(struct efi_key_state *key_state)
635 int c, mod = 0, ret = 0;
659 set_shift_mask(mod, key_state);
666 * efi_cin_read_key() - read a key from the console input
668 * @key: - key received
669 * Return: - status code
671 static efi_status_t efi_cin_read_key(struct efi_key_data *key)
673 struct efi_input_key pressed_key = {
679 if (console_read_unicode(&ch))
680 return EFI_NOT_READY;
682 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
683 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
685 /* We do not support multi-word codes */
692 * Xterm Control Sequences
693 * https://www.xfree86.org/4.8.0/ctlseqs.html
698 pressed_key.scan_code = 23;
700 case 'O': /* F1 - F4, End */
702 /* consider modifiers */
703 if (ch == 'F') { /* End */
704 pressed_key.scan_code = 6;
706 } else if (ch < 'P') {
707 set_shift_mask(ch - '0', &key->key_state);
710 pressed_key.scan_code = ch - 'P' + 11;
715 case 'A'...'D': /* up, down right, left */
716 pressed_key.scan_code = ch - 'A' + 1;
719 pressed_key.scan_code = 6;
722 pressed_key.scan_code = 5;
725 ch = analyze_modifiers(&key->key_state);
727 case '1'...'5': /* F1 - F5 */
728 pressed_key.scan_code = ch - '1' + 11;
730 case '6'...'9': /* F5 - F8 */
731 pressed_key.scan_code = ch - '6' + 15;
733 case 'A'...'D': /* up, down right, left */
734 pressed_key.scan_code = ch - 'A' + 1;
737 pressed_key.scan_code = 6;
740 pressed_key.scan_code = 5;
743 pressed_key.scan_code = 5;
748 ch = analyze_modifiers(&key->key_state);
750 case '0'...'1': /* F9 - F10 */
751 pressed_key.scan_code = ch - '0' + 19;
753 case '3'...'4': /* F11 - F12 */
754 pressed_key.scan_code = ch - '3' + 21;
757 pressed_key.scan_code = 7;
762 pressed_key.scan_code = 8;
763 analyze_modifiers(&key->key_state);
765 case '5': /* PG UP */
766 pressed_key.scan_code = 9;
767 analyze_modifiers(&key->key_state);
769 case '6': /* PG DOWN */
770 pressed_key.scan_code = 10;
771 analyze_modifiers(&key->key_state);
777 set_shift_mask(3, &key->key_state);
784 if (pressed_key.scan_code) {
785 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
787 pressed_key.unicode_char = ch;
790 * Assume left control key for control characters typically
791 * entered using the control key.
793 if (ch >= 0x01 && ch <= 0x1f) {
794 key->key_state.key_shift_state |=
795 EFI_SHIFT_STATE_VALID;
800 key->key_state.key_shift_state |=
801 EFI_LEFT_CONTROL_PRESSED;
805 key->key = pressed_key;
811 * efi_cin_notify() - notify registered functions
813 static void efi_cin_notify(void)
815 struct efi_cin_notify_function *item;
817 list_for_each_entry(item, &cin_notify_functions, link) {
820 /* We do not support toggle states */
821 if (item->key.key.unicode_char || item->key.key.scan_code) {
822 if (item->key.key.unicode_char !=
823 next_key.key.unicode_char ||
824 item->key.key.scan_code != next_key.key.scan_code)
827 if (item->key.key_state.key_shift_state &&
828 item->key.key_state.key_shift_state !=
829 next_key.key_state.key_shift_state)
833 /* We don't bother about the return code */
834 EFI_CALL(item->function(&next_key));
839 * efi_cin_check() - check if keyboard input is available
841 static void efi_cin_check(void)
846 efi_signal_event(efi_con_in.wait_for_key);
851 ret = efi_cin_read_key(&next_key);
852 if (ret == EFI_SUCCESS) {
853 key_available = true;
855 /* Notify registered functions */
858 /* Queue the wait for key event */
860 efi_signal_event(efi_con_in.wait_for_key);
866 * efi_cin_empty_buffer() - empty input buffer
868 static void efi_cin_empty_buffer(void)
872 key_available = false;
876 * efi_cin_reset_ex() - reset console input
878 * @this: - the extended simple text input protocol
879 * @extended_verification: - extended verification
881 * This function implements the reset service of the
882 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
884 * See the Unified Extensible Firmware Interface (UEFI) specification for
887 * Return: old value of the task priority level
889 static efi_status_t EFIAPI efi_cin_reset_ex(
890 struct efi_simple_text_input_ex_protocol *this,
891 bool extended_verification)
893 efi_status_t ret = EFI_SUCCESS;
895 EFI_ENTRY("%p, %d", this, extended_verification);
897 /* Check parameters */
899 ret = EFI_INVALID_PARAMETER;
903 efi_cin_empty_buffer();
905 return EFI_EXIT(ret);
909 * efi_cin_read_key_stroke_ex() - read key stroke
911 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
912 * @key_data: key read from console
913 * Return: status code
915 * This function implements the ReadKeyStrokeEx 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_read_key_stroke_ex(
922 struct efi_simple_text_input_ex_protocol *this,
923 struct efi_key_data *key_data)
925 efi_status_t ret = EFI_SUCCESS;
927 EFI_ENTRY("%p, %p", this, key_data);
929 /* Check parameters */
930 if (!this || !key_data) {
931 ret = EFI_INVALID_PARAMETER;
935 /* We don't do interrupts, so check for timers cooperatively */
938 /* Enable console input after ExitBootServices */
941 if (!key_available) {
946 * CTRL+A - CTRL+Z have to be signaled as a - z.
947 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
949 switch (next_key.key.unicode_char) {
953 if (!(next_key.key_state.key_toggle_state &
954 EFI_CAPS_LOCK_ACTIVE) ^
955 !(next_key.key_state.key_shift_state &
956 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
957 next_key.key.unicode_char += 0x40;
959 next_key.key.unicode_char += 0x60;
961 *key_data = next_key;
962 key_available = false;
963 efi_con_in.wait_for_key->is_signaled = false;
966 return EFI_EXIT(ret);
970 * efi_cin_set_state() - set toggle key state
972 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
973 * @key_toggle_state: pointer to key toggle state
974 * Return: status code
976 * This function implements the SetState service of the
977 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
979 * See the Unified Extensible Firmware Interface (UEFI) specification for
982 static efi_status_t EFIAPI efi_cin_set_state(
983 struct efi_simple_text_input_ex_protocol *this,
984 u8 *key_toggle_state)
986 EFI_ENTRY("%p, %p", this, key_toggle_state);
988 * U-Boot supports multiple console input sources like serial and
989 * net console for which a key toggle state cannot be set at all.
991 * According to the UEFI specification it is allowable to not implement
994 return EFI_EXIT(EFI_UNSUPPORTED);
998 * efi_cin_register_key_notify() - register key notification function
1000 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1001 * @key_data: key to be notified
1002 * @key_notify_function: function to be called if the key is pressed
1003 * @notify_handle: handle for unregistering the notification
1004 * Return: status code
1006 * This function implements the SetState service of the
1007 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1009 * See the Unified Extensible Firmware Interface (UEFI) specification for
1012 static efi_status_t EFIAPI efi_cin_register_key_notify(
1013 struct efi_simple_text_input_ex_protocol *this,
1014 struct efi_key_data *key_data,
1015 efi_status_t (EFIAPI *key_notify_function)(
1016 struct efi_key_data *key_data),
1017 void **notify_handle)
1019 efi_status_t ret = EFI_SUCCESS;
1020 struct efi_cin_notify_function *notify_function;
1022 EFI_ENTRY("%p, %p, %p, %p",
1023 this, key_data, key_notify_function, notify_handle);
1025 /* Check parameters */
1026 if (!this || !key_data || !key_notify_function || !notify_handle) {
1027 ret = EFI_INVALID_PARAMETER;
1031 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
1032 key_data->key.unicode_char,
1033 key_data->key.scan_code,
1034 key_data->key_state.key_shift_state,
1035 key_data->key_state.key_toggle_state);
1037 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
1038 if (!notify_function) {
1039 ret = EFI_OUT_OF_RESOURCES;
1042 notify_function->key = *key_data;
1043 notify_function->function = key_notify_function;
1044 list_add_tail(¬ify_function->link, &cin_notify_functions);
1045 *notify_handle = notify_function;
1047 return EFI_EXIT(ret);
1051 * efi_cin_unregister_key_notify() - unregister key notification function
1053 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1054 * @notification_handle: handle received when registering
1055 * Return: status code
1057 * This function implements the SetState service of the
1058 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1060 * See the Unified Extensible Firmware Interface (UEFI) specification for
1063 static efi_status_t EFIAPI efi_cin_unregister_key_notify(
1064 struct efi_simple_text_input_ex_protocol *this,
1065 void *notification_handle)
1067 efi_status_t ret = EFI_INVALID_PARAMETER;
1068 struct efi_cin_notify_function *item, *notify_function =
1069 notification_handle;
1071 EFI_ENTRY("%p, %p", this, notification_handle);
1073 /* Check parameters */
1074 if (!this || !notification_handle)
1077 list_for_each_entry(item, &cin_notify_functions, link) {
1078 if (item == notify_function) {
1083 if (ret != EFI_SUCCESS)
1086 /* Remove the notify function */
1087 list_del(¬ify_function->link);
1088 free(notify_function);
1090 return EFI_EXIT(ret);
1095 * efi_cin_reset() - drain the input buffer
1097 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1098 * @extended_verification: allow for exhaustive verification
1099 * Return: status code
1101 * This function implements the Reset service of the
1102 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1104 * See the Unified Extensible Firmware Interface (UEFI) specification for
1107 static efi_status_t EFIAPI efi_cin_reset
1108 (struct efi_simple_text_input_protocol *this,
1109 bool extended_verification)
1111 efi_status_t ret = EFI_SUCCESS;
1113 EFI_ENTRY("%p, %d", this, extended_verification);
1115 /* Check parameters */
1117 ret = EFI_INVALID_PARAMETER;
1121 efi_cin_empty_buffer();
1123 return EFI_EXIT(ret);
1127 * efi_cin_read_key_stroke() - read key stroke
1129 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1130 * @key: key read from console
1131 * Return: status code
1133 * This function implements the ReadKeyStroke service of the
1134 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1136 * See the Unified Extensible Firmware Interface (UEFI) specification for
1139 static efi_status_t EFIAPI efi_cin_read_key_stroke
1140 (struct efi_simple_text_input_protocol *this,
1141 struct efi_input_key *key)
1143 efi_status_t ret = EFI_SUCCESS;
1145 EFI_ENTRY("%p, %p", this, key);
1147 /* Check parameters */
1148 if (!this || !key) {
1149 ret = EFI_INVALID_PARAMETER;
1153 /* We don't do interrupts, so check for timers cooperatively */
1156 /* Enable console input after ExitBootServices */
1159 if (!key_available) {
1160 ret = EFI_NOT_READY;
1163 *key = next_key.key;
1164 key_available = false;
1165 efi_con_in.wait_for_key->is_signaled = false;
1167 return EFI_EXIT(ret);
1170 static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1171 .reset = efi_cin_reset_ex,
1172 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1173 .wait_for_key_ex = NULL,
1174 .set_state = efi_cin_set_state,
1175 .register_key_notify = efi_cin_register_key_notify,
1176 .unregister_key_notify = efi_cin_unregister_key_notify,
1179 struct efi_simple_text_input_protocol efi_con_in = {
1180 .reset = efi_cin_reset,
1181 .read_key_stroke = efi_cin_read_key_stroke,
1182 .wait_for_key = NULL,
1185 static struct efi_event *console_timer_event;
1188 * efi_console_timer_notify() - notify the console timer event
1190 * @event: console timer event
1191 * @context: not used
1193 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1196 EFI_ENTRY("%p, %p", event, context);
1198 EFI_EXIT(EFI_SUCCESS);
1202 * efi_key_notify() - notify the wait for key event
1204 * @event: wait for key event
1205 * @context: not used
1207 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1209 EFI_ENTRY("%p, %p", event, context);
1211 EFI_EXIT(EFI_SUCCESS);
1215 * efi_console_register() - install the console protocols
1217 * This function is called from do_bootefi_exec().
1219 * Return: status code
1221 efi_status_t efi_console_register(void)
1224 efi_handle_t console_output_handle;
1225 efi_handle_t console_input_handle;
1227 /* Set up mode information */
1228 query_console_size();
1230 /* Create handles */
1231 r = efi_create_handle(&console_output_handle);
1232 if (r != EFI_SUCCESS)
1235 r = efi_add_protocol(console_output_handle,
1236 &efi_guid_text_output_protocol, &efi_con_out);
1237 if (r != EFI_SUCCESS)
1239 systab.con_out_handle = console_output_handle;
1240 systab.stderr_handle = console_output_handle;
1242 r = efi_create_handle(&console_input_handle);
1243 if (r != EFI_SUCCESS)
1246 r = efi_add_protocol(console_input_handle,
1247 &efi_guid_text_input_protocol, &efi_con_in);
1248 if (r != EFI_SUCCESS)
1250 systab.con_in_handle = console_input_handle;
1251 r = efi_add_protocol(console_input_handle,
1252 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1253 if (r != EFI_SUCCESS)
1256 /* Create console events */
1257 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1258 NULL, NULL, &efi_con_in.wait_for_key);
1259 if (r != EFI_SUCCESS) {
1260 printf("ERROR: Failed to register WaitForKey event\n");
1263 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
1264 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
1265 efi_console_timer_notify, NULL, NULL,
1266 &console_timer_event);
1267 if (r != EFI_SUCCESS) {
1268 printf("ERROR: Failed to register console event\n");
1271 /* 5000 ns cycle is sufficient for 2 MBaud */
1272 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1273 if (r != EFI_SUCCESS)
1274 printf("ERROR: Failed to set console timer\n");
1277 printf("ERROR: Out of memory\n");