1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application console interface
5 * Copyright (c) 2016 Alexander Graf
11 #include <dm/device.h>
12 #include <efi_loader.h>
14 #include <stdio_dev.h>
15 #include <video_console.h>
17 #define EFI_COUT_MODE_2 2
18 #define EFI_MAX_COUT_MODE 3
21 unsigned long columns;
26 static struct cout_mode efi_cout_modes[] = {
27 /* EFI Mode 0 is 80x25 and always present */
33 /* EFI Mode 1 is always 80x50 */
39 /* Value are unknown until we query the console */
47 const efi_guid_t efi_guid_text_input_ex_protocol =
48 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
49 const efi_guid_t efi_guid_text_input_protocol =
50 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
51 const efi_guid_t efi_guid_text_output_protocol =
52 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
57 /* Default to mode 0 */
58 static struct simple_text_output_mode efi_con_mode = {
67 static int term_get_char(s32 *c)
71 /* Wait up to 100 ms for a character */
72 timeout = timer_get_us() + 100000;
75 if (timer_get_us() > timeout)
83 * Receive and parse a reply from the terminal.
85 * @n: array of return values
86 * @num: number of return values expected
87 * @end_char: character indicating end of terminal message
88 * @return: non-zero indicates error
90 static int term_read_reply(int *n, int num, char end_char)
95 if (term_get_char(&c) || c != cESC)
98 if (term_get_char(&c) || c != '[')
103 if (!term_get_char(&c)) {
110 } else if (c == end_char) {
112 } else if (c > '9' || c < '0') {
116 /* Read one more decimal position */
129 static efi_status_t EFIAPI efi_cout_output_string(
130 struct efi_simple_text_output_protocol *this,
131 const efi_string_t string)
133 struct simple_text_output_mode *con = &efi_con_mode;
134 struct cout_mode *mode = &efi_cout_modes[con->mode];
137 efi_status_t ret = EFI_SUCCESS;
139 EFI_ENTRY("%p, %p", this, string);
141 if (!this || !string) {
142 ret = EFI_INVALID_PARAMETER;
146 buf = malloc(utf16_utf8_strlen(string) + 1);
148 ret = EFI_OUT_OF_RESOURCES;
152 utf16_utf8_strcpy(&pos, string);
157 * Update the cursor position.
159 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
160 * and U000D. All other control characters are ignored. Any non-control
161 * character increase the column by one.
163 for (p = string; *p; ++p) {
165 case '\b': /* U+0008, backspace */
166 if (con->cursor_column)
167 con->cursor_column--;
169 case '\n': /* U+000A, newline */
170 con->cursor_column = 0;
173 case '\r': /* U+000D, carriage-return */
174 con->cursor_column = 0;
176 case 0xd800 ... 0xdbff:
178 * Ignore high surrogates, we do not want to count a
179 * Unicode character twice.
183 /* Exclude control codes */
185 con->cursor_column++;
188 if (con->cursor_column >= mode->columns) {
189 con->cursor_column = 0;
193 * When we exceed the row count the terminal will scroll up one
194 * line. We have to adjust the cursor position.
196 if (con->cursor_row >= mode->rows && con->cursor_row)
201 return EFI_EXIT(ret);
204 static efi_status_t EFIAPI efi_cout_test_string(
205 struct efi_simple_text_output_protocol *this,
206 const efi_string_t string)
208 EFI_ENTRY("%p, %p", this, string);
209 return EFI_EXIT(EFI_SUCCESS);
212 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
217 return (mode->rows == rows) && (mode->columns == cols);
221 * query_console_serial() - query console size
223 * @rows: pointer to return number of rows
224 * @cols: pointer to return number of columns
225 * Returns: 0 on success
227 static int query_console_serial(int *rows, int *cols)
232 /* Empty input buffer */
237 * Not all terminals understand CSI [18t for querying the console size.
238 * We should adhere to escape sequences documented in the console_codes
239 * man page and the ECMA-48 standard.
241 * So here we follow a different approach. We position the cursor to the
242 * bottom right and query its position. Before leaving the function we
243 * restore the original cursor position.
245 printf(ESC "7" /* Save cursor position */
246 ESC "[r" /* Set scrolling region to full window */
247 ESC "[999;999H" /* Move to bottom right corner */
248 ESC "[6n"); /* Query cursor position */
250 /* Read {rows,cols} */
251 if (term_read_reply(n, 2, 'R')) {
259 printf(ESC "8"); /* Restore cursor position */
264 * Update the mode table.
266 * By default the only mode available is 80x25. If the console has at least 50
267 * lines, enable mode 80x50. If we can query the console size and it is neither
268 * 80x25 nor 80x50, set it as an additional mode.
270 static void query_console_size(void)
272 const char *stdout_name = env_get("stdout");
273 int rows = 25, cols = 80;
275 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
276 IS_ENABLED(CONFIG_DM_VIDEO)) {
277 struct stdio_dev *stdout_dev =
278 stdio_get_by_name("vidconsole");
279 struct udevice *dev = stdout_dev->priv;
280 struct vidconsole_priv *priv =
281 dev_get_uclass_priv(dev);
284 } else if (query_console_serial(&rows, &cols)) {
288 /* Test if we can have Mode 1 */
289 if (cols >= 80 && rows >= 50) {
290 efi_cout_modes[1].present = 1;
291 efi_con_mode.max_mode = 2;
295 * Install our mode as mode 2 if it is different
296 * than mode 0 or 1 and set it as the currently selected mode
298 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
299 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
300 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
301 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
302 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
303 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
304 efi_con_mode.mode = EFI_COUT_MODE_2;
308 static efi_status_t EFIAPI efi_cout_query_mode(
309 struct efi_simple_text_output_protocol *this,
310 unsigned long mode_number, unsigned long *columns,
313 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
315 if (mode_number >= efi_con_mode.max_mode)
316 return EFI_EXIT(EFI_UNSUPPORTED);
318 if (efi_cout_modes[mode_number].present != 1)
319 return EFI_EXIT(EFI_UNSUPPORTED);
322 *columns = efi_cout_modes[mode_number].columns;
324 *rows = efi_cout_modes[mode_number].rows;
326 return EFI_EXIT(EFI_SUCCESS);
329 static const struct {
333 { 30, 40 }, /* 0: black */
334 { 34, 44 }, /* 1: blue */
335 { 32, 42 }, /* 2: green */
336 { 36, 46 }, /* 3: cyan */
337 { 31, 41 }, /* 4: red */
338 { 35, 45 }, /* 5: magenta */
339 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
340 { 37, 47 }, /* 7: light gray, map to white */
343 /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
344 static efi_status_t EFIAPI efi_cout_set_attribute(
345 struct efi_simple_text_output_protocol *this,
346 unsigned long attribute)
348 unsigned int bold = EFI_ATTR_BOLD(attribute);
349 unsigned int fg = EFI_ATTR_FG(attribute);
350 unsigned int bg = EFI_ATTR_BG(attribute);
352 EFI_ENTRY("%p, %lx", this, attribute);
354 efi_con_mode.attribute = attribute;
356 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
358 printf(ESC"[0;37;40m");
360 return EFI_EXIT(EFI_SUCCESS);
364 * efi_cout_clear_screen() - clear screen
366 * This function implements the ClearScreen service of the
367 * EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL. See the Unified Extensible Firmware
368 * Interface (UEFI) specification for details.
370 * @this: pointer to the protocol instance
371 * Return: status code
373 static efi_status_t EFIAPI efi_cout_clear_screen(
374 struct efi_simple_text_output_protocol *this)
376 EFI_ENTRY("%p", this);
379 * The Linux console wants both a clear and a home command. The video
380 * uclass does not support <ESC>[H without coordinates, yet.
382 printf(ESC "[2J" ESC "[1;1H");
383 efi_con_mode.cursor_column = 0;
384 efi_con_mode.cursor_row = 0;
386 return EFI_EXIT(EFI_SUCCESS);
389 static efi_status_t EFIAPI efi_cout_set_mode(
390 struct efi_simple_text_output_protocol *this,
391 unsigned long mode_number)
393 EFI_ENTRY("%p, %ld", this, mode_number);
395 if (mode_number >= efi_con_mode.max_mode)
396 return EFI_EXIT(EFI_UNSUPPORTED);
398 if (!efi_cout_modes[mode_number].present)
399 return EFI_EXIT(EFI_UNSUPPORTED);
401 efi_con_mode.mode = mode_number;
402 EFI_CALL(efi_cout_clear_screen(this));
404 return EFI_EXIT(EFI_SUCCESS);
407 static efi_status_t EFIAPI efi_cout_reset(
408 struct efi_simple_text_output_protocol *this,
409 char extended_verification)
411 EFI_ENTRY("%p, %d", this, extended_verification);
414 EFI_CALL(efi_cout_clear_screen(this));
415 /* Set default colors */
416 efi_con_mode.attribute = 0x07;
417 printf(ESC "[0;37;40m");
419 return EFI_EXIT(EFI_SUCCESS);
422 static efi_status_t EFIAPI efi_cout_set_cursor_position(
423 struct efi_simple_text_output_protocol *this,
424 unsigned long column, unsigned long row)
426 efi_status_t ret = EFI_SUCCESS;
427 struct simple_text_output_mode *con = &efi_con_mode;
428 struct cout_mode *mode = &efi_cout_modes[con->mode];
430 EFI_ENTRY("%p, %ld, %ld", this, column, row);
432 /* Check parameters */
434 ret = EFI_INVALID_PARAMETER;
437 if (row >= mode->rows || column >= mode->columns) {
438 ret = EFI_UNSUPPORTED;
443 * Set cursor position by sending CSI H.
444 * EFI origin is [0, 0], terminal origin is [1, 1].
446 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
447 efi_con_mode.cursor_column = column;
448 efi_con_mode.cursor_row = row;
450 return EFI_EXIT(ret);
453 static efi_status_t EFIAPI efi_cout_enable_cursor(
454 struct efi_simple_text_output_protocol *this,
457 EFI_ENTRY("%p, %d", this, enable);
459 printf(ESC"[?25%c", enable ? 'h' : 'l');
460 efi_con_mode.cursor_visible = !!enable;
462 return EFI_EXIT(EFI_SUCCESS);
465 struct efi_simple_text_output_protocol efi_con_out = {
466 .reset = efi_cout_reset,
467 .output_string = efi_cout_output_string,
468 .test_string = efi_cout_test_string,
469 .query_mode = efi_cout_query_mode,
470 .set_mode = efi_cout_set_mode,
471 .set_attribute = efi_cout_set_attribute,
472 .clear_screen = efi_cout_clear_screen,
473 .set_cursor_position = efi_cout_set_cursor_position,
474 .enable_cursor = efi_cout_enable_cursor,
475 .mode = (void*)&efi_con_mode,
479 * struct efi_cin_notify_function - registered console input notify function
481 * @link: link to list
482 * @key: key to notify
483 * @function: function to call
485 struct efi_cin_notify_function {
486 struct list_head link;
487 struct efi_key_data key;
488 efi_status_t (EFIAPI *function)
489 (struct efi_key_data *key_data);
492 static bool key_available;
493 static struct efi_key_data next_key;
494 static LIST_HEAD(cin_notify_functions);
497 * set_shift_mask() - set shift mask
499 * @mod: Xterm shift mask
500 * @key_state: receives the state of the shift, alt, control, and logo keys
502 void set_shift_mask(int mod, struct efi_key_state *key_state)
504 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
508 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
510 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
512 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
513 if (!mod || (mod & 8))
514 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
519 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
521 * This gets called when we have already parsed CSI.
523 * @key_state: receives the state of the shift, alt, control, and logo keys
524 * @return: the unmodified code
526 static int analyze_modifiers(struct efi_key_state *key_state)
528 int c, mod = 0, ret = 0;
552 set_shift_mask(mod, key_state);
559 * efi_cin_read_key() - read a key from the console input
561 * @key: - key received
562 * Return: - status code
564 static efi_status_t efi_cin_read_key(struct efi_key_data *key)
566 struct efi_input_key pressed_key = {
572 if (console_read_unicode(&ch))
573 return EFI_NOT_READY;
575 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
576 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
578 /* We do not support multi-word codes */
585 * Xterm Control Sequences
586 * https://www.xfree86.org/4.8.0/ctlseqs.html
591 pressed_key.scan_code = 23;
593 case 'O': /* F1 - F4, End */
595 /* consider modifiers */
596 if (ch == 'F') { /* End */
597 pressed_key.scan_code = 6;
599 } else if (ch < 'P') {
600 set_shift_mask(ch - '0', &key->key_state);
603 pressed_key.scan_code = ch - 'P' + 11;
608 case 'A'...'D': /* up, down right, left */
609 pressed_key.scan_code = ch - 'A' + 1;
612 pressed_key.scan_code = 6;
615 pressed_key.scan_code = 5;
618 ch = analyze_modifiers(&key->key_state);
620 case '1'...'5': /* F1 - F5 */
621 pressed_key.scan_code = ch - '1' + 11;
623 case '6'...'9': /* F5 - F8 */
624 pressed_key.scan_code = ch - '6' + 15;
626 case 'A'...'D': /* up, down right, left */
627 pressed_key.scan_code = ch - 'A' + 1;
630 pressed_key.scan_code = 6;
633 pressed_key.scan_code = 5;
636 pressed_key.scan_code = 5;
641 ch = analyze_modifiers(&key->key_state);
643 case '0'...'1': /* F9 - F10 */
644 pressed_key.scan_code = ch - '0' + 19;
646 case '3'...'4': /* F11 - F12 */
647 pressed_key.scan_code = ch - '3' + 21;
650 pressed_key.scan_code = 7;
655 pressed_key.scan_code = 8;
656 analyze_modifiers(&key->key_state);
658 case '5': /* PG UP */
659 pressed_key.scan_code = 9;
660 analyze_modifiers(&key->key_state);
662 case '6': /* PG DOWN */
663 pressed_key.scan_code = 10;
664 analyze_modifiers(&key->key_state);
670 set_shift_mask(3, &key->key_state);
677 if (pressed_key.scan_code) {
678 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
680 pressed_key.unicode_char = ch;
683 * Assume left control key for control characters typically
684 * entered using the control key.
686 if (ch >= 0x01 && ch <= 0x1f) {
687 key->key_state.key_shift_state |=
688 EFI_SHIFT_STATE_VALID;
693 key->key_state.key_shift_state |=
694 EFI_LEFT_CONTROL_PRESSED;
698 key->key = pressed_key;
704 * efi_cin_notify() - notify registered functions
706 static void efi_cin_notify(void)
708 struct efi_cin_notify_function *item;
710 list_for_each_entry(item, &cin_notify_functions, link) {
713 /* We do not support toggle states */
714 if (item->key.key.unicode_char || item->key.key.scan_code) {
715 if (item->key.key.unicode_char !=
716 next_key.key.unicode_char ||
717 item->key.key.scan_code != next_key.key.scan_code)
720 if (item->key.key_state.key_shift_state &&
721 item->key.key_state.key_shift_state !=
722 next_key.key_state.key_shift_state)
726 /* We don't bother about the return code */
727 EFI_CALL(item->function(&next_key));
732 * efi_cin_check() - check if keyboard input is available
734 static void efi_cin_check(void)
739 efi_signal_event(efi_con_in.wait_for_key);
744 ret = efi_cin_read_key(&next_key);
745 if (ret == EFI_SUCCESS) {
746 key_available = true;
748 /* Notify registered functions */
751 /* Queue the wait for key event */
753 efi_signal_event(efi_con_in.wait_for_key);
759 * efi_cin_empty_buffer() - empty input buffer
761 static void efi_cin_empty_buffer(void)
765 key_available = false;
769 * efi_cin_reset_ex() - reset console input
771 * @this: - the extended simple text input protocol
772 * @extended_verification: - extended verification
774 * This function implements the reset service of the
775 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
777 * See the Unified Extensible Firmware Interface (UEFI) specification for
780 * Return: old value of the task priority level
782 static efi_status_t EFIAPI efi_cin_reset_ex(
783 struct efi_simple_text_input_ex_protocol *this,
784 bool extended_verification)
786 efi_status_t ret = EFI_SUCCESS;
788 EFI_ENTRY("%p, %d", this, extended_verification);
790 /* Check parameters */
792 ret = EFI_INVALID_PARAMETER;
796 efi_cin_empty_buffer();
798 return EFI_EXIT(ret);
802 * efi_cin_read_key_stroke_ex() - read key stroke
804 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
805 * @key_data: key read from console
806 * Return: status code
808 * This function implements the ReadKeyStrokeEx service of the
809 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
811 * See the Unified Extensible Firmware Interface (UEFI) specification for
814 static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
815 struct efi_simple_text_input_ex_protocol *this,
816 struct efi_key_data *key_data)
818 efi_status_t ret = EFI_SUCCESS;
820 EFI_ENTRY("%p, %p", this, key_data);
822 /* Check parameters */
823 if (!this || !key_data) {
824 ret = EFI_INVALID_PARAMETER;
828 /* We don't do interrupts, so check for timers cooperatively */
831 /* Enable console input after ExitBootServices */
834 if (!key_available) {
839 * CTRL+A - CTRL+Z have to be signaled as a - z.
840 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
842 switch (next_key.key.unicode_char) {
846 if (!(next_key.key_state.key_toggle_state &
847 EFI_CAPS_LOCK_ACTIVE) ^
848 !(next_key.key_state.key_shift_state &
849 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
850 next_key.key.unicode_char += 0x40;
852 next_key.key.unicode_char += 0x60;
854 *key_data = next_key;
855 key_available = false;
856 efi_con_in.wait_for_key->is_signaled = false;
859 return EFI_EXIT(ret);
863 * efi_cin_set_state() - set toggle key state
865 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
866 * @key_toggle_state: pointer to key toggle state
867 * Return: status code
869 * This function implements the SetState service of the
870 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
872 * See the Unified Extensible Firmware Interface (UEFI) specification for
875 static efi_status_t EFIAPI efi_cin_set_state(
876 struct efi_simple_text_input_ex_protocol *this,
877 u8 *key_toggle_state)
879 EFI_ENTRY("%p, %p", this, key_toggle_state);
881 * U-Boot supports multiple console input sources like serial and
882 * net console for which a key toggle state cannot be set at all.
884 * According to the UEFI specification it is allowable to not implement
887 return EFI_EXIT(EFI_UNSUPPORTED);
891 * efi_cin_register_key_notify() - register key notification function
893 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
894 * @key_data: key to be notified
895 * @key_notify_function: function to be called if the key is pressed
896 * @notify_handle: handle for unregistering the notification
897 * Return: status code
899 * This function implements the SetState service of the
900 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
902 * See the Unified Extensible Firmware Interface (UEFI) specification for
905 static efi_status_t EFIAPI efi_cin_register_key_notify(
906 struct efi_simple_text_input_ex_protocol *this,
907 struct efi_key_data *key_data,
908 efi_status_t (EFIAPI *key_notify_function)(
909 struct efi_key_data *key_data),
910 void **notify_handle)
912 efi_status_t ret = EFI_SUCCESS;
913 struct efi_cin_notify_function *notify_function;
915 EFI_ENTRY("%p, %p, %p, %p",
916 this, key_data, key_notify_function, notify_handle);
918 /* Check parameters */
919 if (!this || !key_data || !key_notify_function || !notify_handle) {
920 ret = EFI_INVALID_PARAMETER;
924 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
925 key_data->key.unicode_char,
926 key_data->key.scan_code,
927 key_data->key_state.key_shift_state,
928 key_data->key_state.key_toggle_state);
930 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
931 if (!notify_function) {
932 ret = EFI_OUT_OF_RESOURCES;
935 notify_function->key = *key_data;
936 notify_function->function = key_notify_function;
937 list_add_tail(¬ify_function->link, &cin_notify_functions);
938 *notify_handle = notify_function;
940 return EFI_EXIT(ret);
944 * efi_cin_unregister_key_notify() - unregister key notification function
946 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
947 * @notification_handle: handle received when registering
948 * Return: status code
950 * This function implements the SetState service of the
951 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
953 * See the Unified Extensible Firmware Interface (UEFI) specification for
956 static efi_status_t EFIAPI efi_cin_unregister_key_notify(
957 struct efi_simple_text_input_ex_protocol *this,
958 void *notification_handle)
960 efi_status_t ret = EFI_INVALID_PARAMETER;
961 struct efi_cin_notify_function *item, *notify_function =
964 EFI_ENTRY("%p, %p", this, notification_handle);
966 /* Check parameters */
967 if (!this || !notification_handle)
970 list_for_each_entry(item, &cin_notify_functions, link) {
971 if (item == notify_function) {
976 if (ret != EFI_SUCCESS)
979 /* Remove the notify function */
980 list_del(¬ify_function->link);
981 free(notify_function);
983 return EFI_EXIT(ret);
988 * efi_cin_reset() - drain the input buffer
990 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
991 * @extended_verification: allow for exhaustive verification
992 * Return: status code
994 * This function implements the Reset service of the
995 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
997 * See the Unified Extensible Firmware Interface (UEFI) specification for
1000 static efi_status_t EFIAPI efi_cin_reset
1001 (struct efi_simple_text_input_protocol *this,
1002 bool extended_verification)
1004 efi_status_t ret = EFI_SUCCESS;
1006 EFI_ENTRY("%p, %d", this, extended_verification);
1008 /* Check parameters */
1010 ret = EFI_INVALID_PARAMETER;
1014 efi_cin_empty_buffer();
1016 return EFI_EXIT(ret);
1020 * efi_cin_read_key_stroke() - read key stroke
1022 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1023 * @key: key read from console
1024 * Return: status code
1026 * This function implements the ReadKeyStroke service of the
1027 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1029 * See the Unified Extensible Firmware Interface (UEFI) specification for
1032 static efi_status_t EFIAPI efi_cin_read_key_stroke
1033 (struct efi_simple_text_input_protocol *this,
1034 struct efi_input_key *key)
1036 efi_status_t ret = EFI_SUCCESS;
1038 EFI_ENTRY("%p, %p", this, key);
1040 /* Check parameters */
1041 if (!this || !key) {
1042 ret = EFI_INVALID_PARAMETER;
1046 /* We don't do interrupts, so check for timers cooperatively */
1049 /* Enable console input after ExitBootServices */
1052 if (!key_available) {
1053 ret = EFI_NOT_READY;
1056 *key = next_key.key;
1057 key_available = false;
1058 efi_con_in.wait_for_key->is_signaled = false;
1060 return EFI_EXIT(ret);
1063 static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1064 .reset = efi_cin_reset_ex,
1065 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1066 .wait_for_key_ex = NULL,
1067 .set_state = efi_cin_set_state,
1068 .register_key_notify = efi_cin_register_key_notify,
1069 .unregister_key_notify = efi_cin_unregister_key_notify,
1072 struct efi_simple_text_input_protocol efi_con_in = {
1073 .reset = efi_cin_reset,
1074 .read_key_stroke = efi_cin_read_key_stroke,
1075 .wait_for_key = NULL,
1078 static struct efi_event *console_timer_event;
1081 * efi_console_timer_notify() - notify the console timer event
1083 * @event: console timer event
1084 * @context: not used
1086 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1089 EFI_ENTRY("%p, %p", event, context);
1091 EFI_EXIT(EFI_SUCCESS);
1095 * efi_key_notify() - notify the wait for key event
1097 * @event: wait for key event
1098 * @context: not used
1100 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1102 EFI_ENTRY("%p, %p", event, context);
1104 EFI_EXIT(EFI_SUCCESS);
1108 * efi_console_register() - install the console protocols
1110 * This function is called from do_bootefi_exec().
1112 * Return: status code
1114 efi_status_t efi_console_register(void)
1117 efi_handle_t console_output_handle;
1118 efi_handle_t console_input_handle;
1120 /* Set up mode information */
1121 query_console_size();
1123 /* Create handles */
1124 r = efi_create_handle(&console_output_handle);
1125 if (r != EFI_SUCCESS)
1128 r = efi_add_protocol(console_output_handle,
1129 &efi_guid_text_output_protocol, &efi_con_out);
1130 if (r != EFI_SUCCESS)
1132 systab.con_out_handle = console_output_handle;
1133 systab.stderr_handle = console_output_handle;
1135 r = efi_create_handle(&console_input_handle);
1136 if (r != EFI_SUCCESS)
1139 r = efi_add_protocol(console_input_handle,
1140 &efi_guid_text_input_protocol, &efi_con_in);
1141 if (r != EFI_SUCCESS)
1143 systab.con_in_handle = console_input_handle;
1144 r = efi_add_protocol(console_input_handle,
1145 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1146 if (r != EFI_SUCCESS)
1149 /* Create console events */
1150 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1151 NULL, NULL, &efi_con_in.wait_for_key);
1152 if (r != EFI_SUCCESS) {
1153 printf("ERROR: Failed to register WaitForKey event\n");
1156 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
1157 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
1158 efi_console_timer_notify, NULL, NULL,
1159 &console_timer_event);
1160 if (r != EFI_SUCCESS) {
1161 printf("ERROR: Failed to register console event\n");
1164 /* 5000 ns cycle is sufficient for 2 MBaud */
1165 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1166 if (r != EFI_SUCCESS)
1167 printf("ERROR: Failed to set console timer\n");
1170 printf("ERROR: Out of memory\n");