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 = {
66 * Receive and parse a reply from the terminal.
68 * @n: array of return values
69 * @num: number of return values expected
70 * @end_char: character indicating end of terminal message
71 * @return: non-zero indicates error
73 static int term_read_reply(int *n, int num, char end_char)
94 } else if (c == end_char) {
96 } else if (c > '9' || c < '0') {
100 /* Read one more decimal position */
110 static efi_status_t EFIAPI efi_cout_output_string(
111 struct efi_simple_text_output_protocol *this,
112 const efi_string_t string)
114 struct simple_text_output_mode *con = &efi_con_mode;
115 struct cout_mode *mode = &efi_cout_modes[con->mode];
118 efi_status_t ret = EFI_SUCCESS;
120 EFI_ENTRY("%p, %p", this, string);
122 buf = malloc(utf16_utf8_strlen(string) + 1);
124 ret = EFI_OUT_OF_RESOURCES;
128 utf16_utf8_strcpy(&pos, string);
133 * Update the cursor position.
135 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
136 * and U000D. All other characters, including control characters
137 * U+0007 (BEL) and U+0009 (TAB), have to increase the column by one.
139 for (p = string; *p; ++p) {
141 case '\b': /* U+0008, backspace */
142 con->cursor_column = max(0, con->cursor_column - 1);
144 case '\n': /* U+000A, newline */
145 con->cursor_column = 0;
148 case '\r': /* U+000D, carriage-return */
149 con->cursor_column = 0;
151 case 0xd800 ... 0xdbff:
153 * Ignore high surrogates, we do not want to count a
154 * Unicode character twice.
158 con->cursor_column++;
161 if (con->cursor_column >= mode->columns) {
162 con->cursor_column = 0;
165 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
169 return EFI_EXIT(ret);
172 static efi_status_t EFIAPI efi_cout_test_string(
173 struct efi_simple_text_output_protocol *this,
174 const efi_string_t string)
176 EFI_ENTRY("%p, %p", this, string);
177 return EFI_EXIT(EFI_SUCCESS);
180 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
185 return (mode->rows == rows) && (mode->columns == cols);
188 static int query_console_serial(int *rows, int *cols)
190 /* Ask the terminal about its size */
194 /* Empty input buffer */
200 /* Check if we have a terminal that understands */
201 timeout = timer_get_us() + 1000000;
203 if (timer_get_us() > timeout)
206 /* Read {depth,rows,cols} */
207 if (term_read_reply(n, 3, 't'))
217 * Update the mode table.
219 * By default the only mode available is 80x25. If the console has at least 50
220 * lines, enable mode 80x50. If we can query the console size and it is neither
221 * 80x25 nor 80x50, set it as an additional mode.
223 static void query_console_size(void)
225 const char *stdout_name = env_get("stdout");
226 int rows = 25, cols = 80;
228 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
229 IS_ENABLED(CONFIG_DM_VIDEO)) {
230 struct stdio_dev *stdout_dev =
231 stdio_get_by_name("vidconsole");
232 struct udevice *dev = stdout_dev->priv;
233 struct vidconsole_priv *priv =
234 dev_get_uclass_priv(dev);
237 } else if (query_console_serial(&rows, &cols)) {
241 /* Test if we can have Mode 1 */
242 if (cols >= 80 && rows >= 50) {
243 efi_cout_modes[1].present = 1;
244 efi_con_mode.max_mode = 2;
248 * Install our mode as mode 2 if it is different
249 * than mode 0 or 1 and set it as the currently selected mode
251 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
252 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
253 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
254 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
255 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
256 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
257 efi_con_mode.mode = EFI_COUT_MODE_2;
261 static efi_status_t EFIAPI efi_cout_query_mode(
262 struct efi_simple_text_output_protocol *this,
263 unsigned long mode_number, unsigned long *columns,
266 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
268 if (mode_number >= efi_con_mode.max_mode)
269 return EFI_EXIT(EFI_UNSUPPORTED);
271 if (efi_cout_modes[mode_number].present != 1)
272 return EFI_EXIT(EFI_UNSUPPORTED);
275 *columns = efi_cout_modes[mode_number].columns;
277 *rows = efi_cout_modes[mode_number].rows;
279 return EFI_EXIT(EFI_SUCCESS);
282 static efi_status_t EFIAPI efi_cout_set_mode(
283 struct efi_simple_text_output_protocol *this,
284 unsigned long mode_number)
286 EFI_ENTRY("%p, %ld", this, mode_number);
289 if (mode_number > efi_con_mode.max_mode)
290 return EFI_EXIT(EFI_UNSUPPORTED);
292 efi_con_mode.mode = mode_number;
293 efi_con_mode.cursor_column = 0;
294 efi_con_mode.cursor_row = 0;
296 return EFI_EXIT(EFI_SUCCESS);
299 static const struct {
303 { 30, 40 }, /* 0: black */
304 { 34, 44 }, /* 1: blue */
305 { 32, 42 }, /* 2: green */
306 { 36, 46 }, /* 3: cyan */
307 { 31, 41 }, /* 4: red */
308 { 35, 45 }, /* 5: magenta */
309 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
310 { 37, 47 }, /* 7: light gray, map to white */
313 /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
314 static efi_status_t EFIAPI efi_cout_set_attribute(
315 struct efi_simple_text_output_protocol *this,
316 unsigned long attribute)
318 unsigned int bold = EFI_ATTR_BOLD(attribute);
319 unsigned int fg = EFI_ATTR_FG(attribute);
320 unsigned int bg = EFI_ATTR_BG(attribute);
322 EFI_ENTRY("%p, %lx", this, attribute);
325 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
327 printf(ESC"[0;37;40m");
329 return EFI_EXIT(EFI_SUCCESS);
332 static efi_status_t EFIAPI efi_cout_clear_screen(
333 struct efi_simple_text_output_protocol *this)
335 EFI_ENTRY("%p", this);
338 efi_con_mode.cursor_column = 0;
339 efi_con_mode.cursor_row = 0;
341 return EFI_EXIT(EFI_SUCCESS);
344 static efi_status_t EFIAPI efi_cout_reset(
345 struct efi_simple_text_output_protocol *this,
346 char extended_verification)
348 EFI_ENTRY("%p, %d", this, extended_verification);
351 EFI_CALL(efi_cout_clear_screen(this));
352 /* Set default colors */
353 printf(ESC "[0;37;40m");
355 return EFI_EXIT(EFI_SUCCESS);
358 static efi_status_t EFIAPI efi_cout_set_cursor_position(
359 struct efi_simple_text_output_protocol *this,
360 unsigned long column, unsigned long row)
362 EFI_ENTRY("%p, %ld, %ld", this, column, row);
364 printf(ESC"[%d;%df", (int)row, (int)column);
365 efi_con_mode.cursor_column = column;
366 efi_con_mode.cursor_row = row;
368 return EFI_EXIT(EFI_SUCCESS);
371 static efi_status_t EFIAPI efi_cout_enable_cursor(
372 struct efi_simple_text_output_protocol *this,
375 EFI_ENTRY("%p, %d", this, enable);
377 printf(ESC"[?25%c", enable ? 'h' : 'l');
379 return EFI_EXIT(EFI_SUCCESS);
382 struct efi_simple_text_output_protocol efi_con_out = {
383 .reset = efi_cout_reset,
384 .output_string = efi_cout_output_string,
385 .test_string = efi_cout_test_string,
386 .query_mode = efi_cout_query_mode,
387 .set_mode = efi_cout_set_mode,
388 .set_attribute = efi_cout_set_attribute,
389 .clear_screen = efi_cout_clear_screen,
390 .set_cursor_position = efi_cout_set_cursor_position,
391 .enable_cursor = efi_cout_enable_cursor,
392 .mode = (void*)&efi_con_mode,
395 static bool key_available;
396 static struct efi_key_data next_key;
399 * set_shift_mask() - set shift mask
401 * @mod: Xterm shift mask
403 void set_shift_mask(int mod, struct efi_key_state *key_state)
405 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
409 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
411 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
413 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
415 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
417 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
422 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
424 * This gets called when we have already parsed CSI.
426 * @modifiers: bitmask (shift, alt, ctrl)
427 * @return: the unmodified code
429 static int analyze_modifiers(struct efi_key_state *key_state)
431 int c, mod = 0, ret = 0;
455 set_shift_mask(mod, key_state);
462 * efi_cin_read_key() - read a key from the console input
464 * @key: - key received
465 * Return: - status code
467 static efi_status_t efi_cin_read_key(struct efi_key_data *key)
469 struct efi_input_key pressed_key = {
475 if (console_read_unicode(&ch))
476 return EFI_NOT_READY;
478 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
479 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
481 /* We do not support multi-word codes */
488 * Xterm Control Sequences
489 * https://www.xfree86.org/4.8.0/ctlseqs.html
494 pressed_key.scan_code = 23;
496 case 'O': /* F1 - F4 */
498 /* consider modifiers */
500 set_shift_mask(ch - '0', &key->key_state);
503 pressed_key.scan_code = ch - 'P' + 11;
508 case 'A'...'D': /* up, down right, left */
509 pressed_key.scan_code = ch - 'A' + 1;
512 pressed_key.scan_code = 6;
515 pressed_key.scan_code = 5;
518 ch = analyze_modifiers(&key->key_state);
520 case '1'...'5': /* F1 - F5 */
521 pressed_key.scan_code = ch - '1' + 11;
523 case '7'...'9': /* F6 - F8 */
524 pressed_key.scan_code = ch - '7' + 16;
526 case 'A'...'D': /* up, down right, left */
527 pressed_key.scan_code = ch - 'A' + 1;
530 pressed_key.scan_code = 6; /* End */
533 pressed_key.scan_code = 5; /* Home */
538 ch = analyze_modifiers(&key->key_state);
540 case '0'...'1': /* F9 - F10 */
541 pressed_key.scan_code = ch - '0' + 19;
543 case '3'...'4': /* F11 - F12 */
544 pressed_key.scan_code = ch - '3' + 21;
547 pressed_key.scan_code = 7;
552 pressed_key.scan_code = 8;
553 analyze_modifiers(&key->key_state);
555 case '5': /* PG UP */
556 pressed_key.scan_code = 9;
557 analyze_modifiers(&key->key_state);
559 case '6': /* PG DOWN */
560 pressed_key.scan_code = 10;
561 analyze_modifiers(&key->key_state);
567 set_shift_mask(3, &key->key_state);
574 if (pressed_key.scan_code) {
575 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
577 pressed_key.unicode_char = ch;
580 * Assume left control key for control characters typically
581 * entered using the control key.
583 if (ch >= 0x01 && ch <= 0x1f) {
584 key->key_state.key_shift_state |=
585 EFI_SHIFT_STATE_VALID;
590 key->key_state.key_shift_state |=
591 EFI_LEFT_CONTROL_PRESSED;
595 key->key = pressed_key;
601 * efi_cin_check() - check if keyboard input is available
603 static void efi_cin_check(void)
608 efi_signal_event(efi_con_in.wait_for_key, true);
613 ret = efi_cin_read_key(&next_key);
614 if (ret == EFI_SUCCESS) {
615 key_available = true;
617 /* Queue the wait for key event */
618 efi_signal_event(efi_con_in.wait_for_key, true);
624 * efi_cin_empty_buffer() - empty input buffer
626 static void efi_cin_empty_buffer(void)
630 key_available = false;
634 * efi_cin_reset_ex() - reset console input
636 * @this: - the extended simple text input protocol
637 * @extended_verification: - extended verification
639 * This function implements the reset service of the
640 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
642 * See the Unified Extensible Firmware Interface (UEFI) specification for
645 * Return: old value of the task priority level
647 static efi_status_t EFIAPI efi_cin_reset_ex(
648 struct efi_simple_text_input_ex_protocol *this,
649 bool extended_verification)
651 efi_status_t ret = EFI_SUCCESS;
653 EFI_ENTRY("%p, %d", this, extended_verification);
655 /* Check parameters */
657 ret = EFI_INVALID_PARAMETER;
661 efi_cin_empty_buffer();
663 return EFI_EXIT(ret);
667 * efi_cin_read_key_stroke_ex() - read key stroke
669 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
670 * @key_data: key read from console
671 * Return: status code
673 * This function implements the ReadKeyStrokeEx service of the
674 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
676 * See the Unified Extensible Firmware Interface (UEFI) specification for
679 static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
680 struct efi_simple_text_input_ex_protocol *this,
681 struct efi_key_data *key_data)
683 efi_status_t ret = EFI_SUCCESS;
685 EFI_ENTRY("%p, %p", this, key_data);
687 /* Check parameters */
688 if (!this || !key_data) {
689 ret = EFI_INVALID_PARAMETER;
693 /* We don't do interrupts, so check for timers cooperatively */
696 /* Enable console input after ExitBootServices */
699 if (!key_available) {
703 *key_data = next_key;
704 key_available = false;
705 efi_con_in.wait_for_key->is_signaled = false;
707 return EFI_EXIT(ret);
711 * efi_cin_set_state() - set toggle key state
713 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
714 * @key_toggle_state: key toggle state
715 * Return: status code
717 * This function implements the SetState service of the
718 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
720 * See the Unified Extensible Firmware Interface (UEFI) specification for
723 static efi_status_t EFIAPI efi_cin_set_state(
724 struct efi_simple_text_input_ex_protocol *this,
727 EFI_ENTRY("%p, %u", this, key_toggle_state);
729 * U-Boot supports multiple console input sources like serial and
730 * net console for which a key toggle state cannot be set at all.
732 * According to the UEFI specification it is allowable to not implement
735 return EFI_EXIT(EFI_UNSUPPORTED);
739 * efi_cin_register_key_notify() - register key notification function
741 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
742 * @key_data: key to be notified
743 * @key_notify_function: function to be called if the key is pressed
744 * @notify_handle: handle for unregistering the notification
745 * Return: status code
747 * This function implements the SetState service of the
748 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
750 * See the Unified Extensible Firmware Interface (UEFI) specification for
753 static efi_status_t EFIAPI efi_cin_register_key_notify(
754 struct efi_simple_text_input_ex_protocol *this,
755 struct efi_key_data *key_data,
756 efi_status_t (EFIAPI *key_notify_function)(
757 struct efi_key_data *key_data),
758 void **notify_handle)
760 EFI_ENTRY("%p, %p, %p, %p",
761 this, key_data, key_notify_function, notify_handle);
762 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
766 * efi_cin_unregister_key_notify() - unregister key notification function
768 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
769 * @notification_handle: handle received when registering
770 * Return: status code
772 * This function implements the SetState service of the
773 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
775 * See the Unified Extensible Firmware Interface (UEFI) specification for
778 static efi_status_t EFIAPI efi_cin_unregister_key_notify(
779 struct efi_simple_text_input_ex_protocol *this,
780 void *notification_handle)
782 EFI_ENTRY("%p, %p", this, notification_handle);
783 return EFI_EXIT(EFI_INVALID_PARAMETER);
788 * efi_cin_reset() - drain the input buffer
790 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
791 * @extended_verification: allow for exhaustive verification
792 * Return: status code
794 * This function implements the Reset service of the
795 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
797 * See the Unified Extensible Firmware Interface (UEFI) specification for
800 static efi_status_t EFIAPI efi_cin_reset
801 (struct efi_simple_text_input_protocol *this,
802 bool extended_verification)
804 efi_status_t ret = EFI_SUCCESS;
806 EFI_ENTRY("%p, %d", this, extended_verification);
808 /* Check parameters */
810 ret = EFI_INVALID_PARAMETER;
814 efi_cin_empty_buffer();
816 return EFI_EXIT(ret);
820 * efi_cin_read_key_stroke() - read key stroke
822 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
823 * @key: key read from console
824 * Return: status code
826 * This function implements the ReadKeyStroke service of the
827 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
829 * See the Unified Extensible Firmware Interface (UEFI) specification for
832 static efi_status_t EFIAPI efi_cin_read_key_stroke
833 (struct efi_simple_text_input_protocol *this,
834 struct efi_input_key *key)
836 efi_status_t ret = EFI_SUCCESS;
838 EFI_ENTRY("%p, %p", this, key);
840 /* Check parameters */
842 ret = EFI_INVALID_PARAMETER;
846 /* We don't do interrupts, so check for timers cooperatively */
849 /* Enable console input after ExitBootServices */
852 if (!key_available) {
857 key_available = false;
858 efi_con_in.wait_for_key->is_signaled = false;
860 return EFI_EXIT(ret);
863 static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
864 .reset = efi_cin_reset_ex,
865 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
866 .wait_for_key_ex = NULL,
867 .set_state = efi_cin_set_state,
868 .register_key_notify = efi_cin_register_key_notify,
869 .unregister_key_notify = efi_cin_unregister_key_notify,
872 struct efi_simple_text_input_protocol efi_con_in = {
873 .reset = efi_cin_reset,
874 .read_key_stroke = efi_cin_read_key_stroke,
875 .wait_for_key = NULL,
878 static struct efi_event *console_timer_event;
881 * efi_console_timer_notify() - notify the console timer event
883 * @event: console timer event
886 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
889 EFI_ENTRY("%p, %p", event, context);
891 EFI_EXIT(EFI_SUCCESS);
895 * efi_key_notify() - notify the wait for key event
897 * @event: wait for key event
900 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
902 EFI_ENTRY("%p, %p", event, context);
904 EFI_EXIT(EFI_SUCCESS);
908 * efi_console_register() - install the console protocols
910 * This function is called from do_bootefi_exec().
912 int efi_console_register(void)
915 struct efi_object *efi_console_output_obj;
916 struct efi_object *efi_console_input_obj;
918 /* Set up mode information */
919 query_console_size();
922 r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
923 if (r != EFI_SUCCESS)
926 r = efi_add_protocol(efi_console_output_obj->handle,
927 &efi_guid_text_output_protocol, &efi_con_out);
928 if (r != EFI_SUCCESS)
930 systab.con_out_handle = efi_console_output_obj->handle;
931 systab.stderr_handle = efi_console_output_obj->handle;
933 r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
934 if (r != EFI_SUCCESS)
937 r = efi_add_protocol(efi_console_input_obj->handle,
938 &efi_guid_text_input_protocol, &efi_con_in);
939 if (r != EFI_SUCCESS)
941 systab.con_in_handle = efi_console_input_obj->handle;
942 r = efi_add_protocol(efi_console_input_obj->handle,
943 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
944 if (r != EFI_SUCCESS)
947 /* Create console events */
948 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
949 NULL, NULL, &efi_con_in.wait_for_key);
950 if (r != EFI_SUCCESS) {
951 printf("ERROR: Failed to register WaitForKey event\n");
954 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
955 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
956 efi_console_timer_notify, NULL, NULL,
957 &console_timer_event);
958 if (r != EFI_SUCCESS) {
959 printf("ERROR: Failed to register console event\n");
962 /* 5000 ns cycle is sufficient for 2 MBaud */
963 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
964 if (r != EFI_SUCCESS)
965 printf("ERROR: Failed to set console timer\n");
968 printf("ERROR: Out of memory\n");