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_output_protocol =
46 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
47 const efi_guid_t efi_guid_text_input_protocol =
48 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
53 /* Default to mode 0 */
54 static struct simple_text_output_mode efi_con_mode = {
64 * Receive and parse a reply from the terminal.
66 * @n: array of return values
67 * @num: number of return values expected
68 * @end_char: character indicating end of terminal message
69 * @return: non-zero indicates error
71 static int term_read_reply(int *n, int num, char end_char)
92 } else if (c == end_char) {
94 } else if (c > '9' || c < '0') {
98 /* Read one more decimal position */
108 static efi_status_t EFIAPI efi_cout_output_string(
109 struct efi_simple_text_output_protocol *this,
110 const efi_string_t string)
112 struct simple_text_output_mode *con = &efi_con_mode;
113 struct cout_mode *mode = &efi_cout_modes[con->mode];
116 efi_status_t ret = EFI_SUCCESS;
118 EFI_ENTRY("%p, %p", this, string);
120 buf = malloc(utf16_utf8_strlen(string) + 1);
122 ret = EFI_OUT_OF_RESOURCES;
126 utf16_utf8_strcpy(&pos, string);
131 * Update the cursor position.
133 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
134 * and U000D. All other characters, including control characters
135 * U+0007 (BEL) and U+0009 (TAB), have to increase the column by one.
137 for (p = string; *p; ++p) {
139 case '\b': /* U+0008, backspace */
140 con->cursor_column = max(0, con->cursor_column - 1);
142 case '\n': /* U+000A, newline */
143 con->cursor_column = 0;
146 case '\r': /* U+000D, carriage-return */
147 con->cursor_column = 0;
149 case 0xd800 ... 0xdbff:
151 * Ignore high surrogates, we do not want to count a
152 * Unicode character twice.
156 con->cursor_column++;
159 if (con->cursor_column >= mode->columns) {
160 con->cursor_column = 0;
163 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
167 return EFI_EXIT(ret);
170 static efi_status_t EFIAPI efi_cout_test_string(
171 struct efi_simple_text_output_protocol *this,
172 const efi_string_t string)
174 EFI_ENTRY("%p, %p", this, string);
175 return EFI_EXIT(EFI_SUCCESS);
178 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
183 return (mode->rows == rows) && (mode->columns == cols);
186 static int query_console_serial(int *rows, int *cols)
188 /* Ask the terminal about its size */
192 /* Empty input buffer */
198 /* Check if we have a terminal that understands */
199 timeout = timer_get_us() + 1000000;
201 if (timer_get_us() > timeout)
204 /* Read {depth,rows,cols} */
205 if (term_read_reply(n, 3, 't'))
215 * Update the mode table.
217 * By default the only mode available is 80x25. If the console has at least 50
218 * lines, enable mode 80x50. If we can query the console size and it is neither
219 * 80x25 nor 80x50, set it as an additional mode.
221 static void query_console_size(void)
223 const char *stdout_name = env_get("stdout");
224 int rows = 25, cols = 80;
226 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
227 IS_ENABLED(CONFIG_DM_VIDEO)) {
228 struct stdio_dev *stdout_dev =
229 stdio_get_by_name("vidconsole");
230 struct udevice *dev = stdout_dev->priv;
231 struct vidconsole_priv *priv =
232 dev_get_uclass_priv(dev);
235 } else if (query_console_serial(&rows, &cols)) {
239 /* Test if we can have Mode 1 */
240 if (cols >= 80 && rows >= 50) {
241 efi_cout_modes[1].present = 1;
242 efi_con_mode.max_mode = 2;
246 * Install our mode as mode 2 if it is different
247 * than mode 0 or 1 and set it as the currently selected mode
249 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
250 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
251 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
252 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
253 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
254 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
255 efi_con_mode.mode = EFI_COUT_MODE_2;
259 static efi_status_t EFIAPI efi_cout_query_mode(
260 struct efi_simple_text_output_protocol *this,
261 unsigned long mode_number, unsigned long *columns,
264 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
266 if (mode_number >= efi_con_mode.max_mode)
267 return EFI_EXIT(EFI_UNSUPPORTED);
269 if (efi_cout_modes[mode_number].present != 1)
270 return EFI_EXIT(EFI_UNSUPPORTED);
273 *columns = efi_cout_modes[mode_number].columns;
275 *rows = efi_cout_modes[mode_number].rows;
277 return EFI_EXIT(EFI_SUCCESS);
280 static efi_status_t EFIAPI efi_cout_set_mode(
281 struct efi_simple_text_output_protocol *this,
282 unsigned long mode_number)
284 EFI_ENTRY("%p, %ld", this, mode_number);
287 if (mode_number > efi_con_mode.max_mode)
288 return EFI_EXIT(EFI_UNSUPPORTED);
290 efi_con_mode.mode = mode_number;
291 efi_con_mode.cursor_column = 0;
292 efi_con_mode.cursor_row = 0;
294 return EFI_EXIT(EFI_SUCCESS);
297 static const struct {
301 { 30, 40 }, /* 0: black */
302 { 34, 44 }, /* 1: blue */
303 { 32, 42 }, /* 2: green */
304 { 36, 46 }, /* 3: cyan */
305 { 31, 41 }, /* 4: red */
306 { 35, 45 }, /* 5: magenta */
307 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
308 { 37, 47 }, /* 7: light gray, map to white */
311 /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
312 static efi_status_t EFIAPI efi_cout_set_attribute(
313 struct efi_simple_text_output_protocol *this,
314 unsigned long attribute)
316 unsigned int bold = EFI_ATTR_BOLD(attribute);
317 unsigned int fg = EFI_ATTR_FG(attribute);
318 unsigned int bg = EFI_ATTR_BG(attribute);
320 EFI_ENTRY("%p, %lx", this, attribute);
323 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
325 printf(ESC"[0;37;40m");
327 return EFI_EXIT(EFI_SUCCESS);
330 static efi_status_t EFIAPI efi_cout_clear_screen(
331 struct efi_simple_text_output_protocol *this)
333 EFI_ENTRY("%p", this);
336 efi_con_mode.cursor_column = 0;
337 efi_con_mode.cursor_row = 0;
339 return EFI_EXIT(EFI_SUCCESS);
342 static efi_status_t EFIAPI efi_cout_reset(
343 struct efi_simple_text_output_protocol *this,
344 char extended_verification)
346 EFI_ENTRY("%p, %d", this, extended_verification);
349 EFI_CALL(efi_cout_clear_screen(this));
350 /* Set default colors */
351 printf(ESC "[0;37;40m");
353 return EFI_EXIT(EFI_SUCCESS);
356 static efi_status_t EFIAPI efi_cout_set_cursor_position(
357 struct efi_simple_text_output_protocol *this,
358 unsigned long column, unsigned long row)
360 EFI_ENTRY("%p, %ld, %ld", this, column, row);
362 printf(ESC"[%d;%df", (int)row, (int)column);
363 efi_con_mode.cursor_column = column;
364 efi_con_mode.cursor_row = row;
366 return EFI_EXIT(EFI_SUCCESS);
369 static efi_status_t EFIAPI efi_cout_enable_cursor(
370 struct efi_simple_text_output_protocol *this,
373 EFI_ENTRY("%p, %d", this, enable);
375 printf(ESC"[?25%c", enable ? 'h' : 'l');
377 return EFI_EXIT(EFI_SUCCESS);
380 struct efi_simple_text_output_protocol efi_con_out = {
381 .reset = efi_cout_reset,
382 .output_string = efi_cout_output_string,
383 .test_string = efi_cout_test_string,
384 .query_mode = efi_cout_query_mode,
385 .set_mode = efi_cout_set_mode,
386 .set_attribute = efi_cout_set_attribute,
387 .clear_screen = efi_cout_clear_screen,
388 .set_cursor_position = efi_cout_set_cursor_position,
389 .enable_cursor = efi_cout_enable_cursor,
390 .mode = (void*)&efi_con_mode,
393 static bool key_available;
394 static struct efi_input_key next_key;
397 * skip_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
399 * This gets called when we have already parsed CSI.
401 * @modifiers: bitmask (shift, alt, ctrl)
402 * @return: the unmodified code
404 static char skip_modifiers(int *modifiers)
406 char c, mod = 0, ret = 0;
440 * efi_cin_read_key() - read a key from the console input
442 * @key: - key received
443 * Return: - status code
445 static efi_status_t efi_cin_read_key(struct efi_input_key *key)
448 struct efi_input_key pressed_key = {
454 ret = console_read_unicode(&ch);
456 return EFI_NOT_READY;
457 /* We do not support multi-word codes */
462 * Xterm Control Sequences
463 * https://www.xfree86.org/4.8.0/ctlseqs.html
468 pressed_key.scan_code = 23;
470 case 'O': /* F1 - F4 */
475 pressed_key.scan_code = ch - 'P' + 11;
483 case 'A'...'D': /* up, down right, left */
484 pressed_key.scan_code = ch - 'A' + 1;
487 pressed_key.scan_code = 6;
490 pressed_key.scan_code = 5;
493 ch = skip_modifiers(NULL);
495 case '1'...'5': /* F1 - F5 */
496 pressed_key.scan_code = ch - '1' + 11;
498 case '7'...'9': /* F6 - F8 */
499 pressed_key.scan_code = ch - '7' + 16;
501 case 'A'...'D': /* up, down right, left */
502 pressed_key.scan_code = ch - 'A' + 1;
505 pressed_key.scan_code = 6; /* End */
508 pressed_key.scan_code = 5; /* Home */
513 ch = skip_modifiers(NULL);
515 case '0'...'1': /* F9 - F10 */
516 pressed_key.scan_code = ch - '0' + 19;
518 case '3'...'4': /* F11 - F12 */
519 pressed_key.scan_code = ch - '3' + 21;
522 pressed_key.scan_code = 7;
527 pressed_key.scan_code = 8;
528 skip_modifiers(NULL);
530 case '5': /* PG UP */
531 pressed_key.scan_code = 9;
532 skip_modifiers(NULL);
534 case '6': /* PG DOWN */
535 pressed_key.scan_code = 10;
536 skip_modifiers(NULL);
541 } else if (ch == 0x7f) {
545 if (!pressed_key.scan_code)
546 pressed_key.unicode_char = ch;
553 * efi_cin_check() - check if keyboard input is available
555 static void efi_cin_check(void)
560 efi_signal_event(efi_con_in.wait_for_key, true);
565 ret = efi_cin_read_key(&next_key);
566 if (ret == EFI_SUCCESS) {
567 key_available = true;
569 /* Queue the wait for key event */
570 efi_signal_event(efi_con_in.wait_for_key, true);
576 * efi_cin_reset() - drain the input buffer
578 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
579 * @extended_verification: allow for exhaustive verification
580 * Return: status code
582 * This function implements the Reset service of the
583 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
585 * See the Unified Extensible Firmware Interface (UEFI) specification for
588 static efi_status_t EFIAPI efi_cin_reset
589 (struct efi_simple_text_input_protocol *this,
590 bool extended_verification)
592 efi_status_t ret = EFI_SUCCESS;
594 EFI_ENTRY("%p, %d", this, extended_verification);
596 /* Check parameters */
598 ret = EFI_INVALID_PARAMETER;
602 /* Empty input buffer */
605 key_available = false;
607 return EFI_EXIT(ret);
611 * efi_cin_reset() - drain the input buffer
613 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
614 * @key: key read from console
615 * Return: status code
617 * This function implements the ReadKeyStroke service of the
618 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
620 * See the Unified Extensible Firmware Interface (UEFI) specification for
623 static efi_status_t EFIAPI efi_cin_read_key_stroke
624 (struct efi_simple_text_input_protocol *this,
625 struct efi_input_key *key)
627 efi_status_t ret = EFI_SUCCESS;
629 EFI_ENTRY("%p, %p", this, key);
631 /* Check parameters */
633 ret = EFI_INVALID_PARAMETER;
637 /* We don't do interrupts, so check for timers cooperatively */
640 /* Enable console input after ExitBootServices */
643 if (!key_available) {
648 key_available = false;
649 efi_con_in.wait_for_key->is_signaled = false;
651 return EFI_EXIT(ret);
654 struct efi_simple_text_input_protocol efi_con_in = {
655 .reset = efi_cin_reset,
656 .read_key_stroke = efi_cin_read_key_stroke,
657 .wait_for_key = NULL,
660 static struct efi_event *console_timer_event;
663 * efi_console_timer_notify() - notify the console timer event
665 * @event: console timer event
668 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
671 EFI_ENTRY("%p, %p", event, context);
673 EFI_EXIT(EFI_SUCCESS);
677 * efi_key_notify() - notify the wait for key event
679 * @event: wait for key event
682 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
684 EFI_ENTRY("%p, %p", event, context);
686 EFI_EXIT(EFI_SUCCESS);
690 * efi_console_register() - install the console protocols
692 * This function is called from do_bootefi_exec().
694 int efi_console_register(void)
697 struct efi_object *efi_console_output_obj;
698 struct efi_object *efi_console_input_obj;
700 /* Set up mode information */
701 query_console_size();
704 r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
705 if (r != EFI_SUCCESS)
708 r = efi_add_protocol(efi_console_output_obj->handle,
709 &efi_guid_text_output_protocol, &efi_con_out);
710 if (r != EFI_SUCCESS)
712 systab.con_out_handle = efi_console_output_obj->handle;
713 systab.stderr_handle = efi_console_output_obj->handle;
715 r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
716 if (r != EFI_SUCCESS)
719 r = efi_add_protocol(efi_console_input_obj->handle,
720 &efi_guid_text_input_protocol, &efi_con_in);
721 if (r != EFI_SUCCESS)
723 systab.con_in_handle = efi_console_input_obj->handle;
725 /* Create console events */
726 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
727 NULL, NULL, &efi_con_in.wait_for_key);
728 if (r != EFI_SUCCESS) {
729 printf("ERROR: Failed to register WaitForKey event\n");
732 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
733 efi_console_timer_notify, NULL, NULL,
734 &console_timer_event);
735 if (r != EFI_SUCCESS) {
736 printf("ERROR: Failed to register console event\n");
739 /* 5000 ns cycle is sufficient for 2 MBaud */
740 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
741 if (r != EFI_SUCCESS)
742 printf("ERROR: Failed to set console timer\n");
745 printf("ERROR: Out of memory\n");