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_reset(
109 struct efi_simple_text_output_protocol *this,
110 char extended_verification)
112 EFI_ENTRY("%p, %d", this, extended_verification);
113 return EFI_EXIT(EFI_UNSUPPORTED);
116 static efi_status_t EFIAPI efi_cout_output_string(
117 struct efi_simple_text_output_protocol *this,
118 const efi_string_t string)
120 struct simple_text_output_mode *con = &efi_con_mode;
121 struct cout_mode *mode = &efi_cout_modes[con->mode];
123 EFI_ENTRY("%p, %p", this, string);
125 unsigned int n16 = utf16_strlen(string);
126 char buf[MAX_UTF8_PER_UTF16 * n16 + 1];
129 *utf16_to_utf8((u8 *)buf, string, n16) = '\0';
134 * Update the cursor position.
136 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
137 * and U000D. All other characters, including control characters
138 * U+0007 (bel) and U+0009 (tab), have to increase the column by one.
140 for (p = string; *p; ++p) {
142 case '\b': /* U+0008, backspace */
143 con->cursor_column = max(0, con->cursor_column - 1);
145 case '\n': /* U+000A, newline */
146 con->cursor_column = 0;
149 case '\r': /* U+000D, carriage-return */
150 con->cursor_column = 0;
152 case 0xd800 ... 0xdbff:
154 * Ignore high surrogates, we do not want to count a
155 * Unicode character twice.
159 con->cursor_column++;
162 if (con->cursor_column >= mode->columns) {
163 con->cursor_column = 0;
166 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
169 return EFI_EXIT(EFI_SUCCESS);
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 grey, 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);
339 return EFI_EXIT(EFI_SUCCESS);
342 static efi_status_t EFIAPI efi_cout_set_cursor_position(
343 struct efi_simple_text_output_protocol *this,
344 unsigned long column, unsigned long row)
346 EFI_ENTRY("%p, %ld, %ld", this, column, row);
348 printf(ESC"[%d;%df", (int)row, (int)column);
349 efi_con_mode.cursor_column = column;
350 efi_con_mode.cursor_row = row;
352 return EFI_EXIT(EFI_SUCCESS);
355 static efi_status_t EFIAPI efi_cout_enable_cursor(
356 struct efi_simple_text_output_protocol *this,
359 EFI_ENTRY("%p, %d", this, enable);
361 printf(ESC"[?25%c", enable ? 'h' : 'l');
363 return EFI_EXIT(EFI_SUCCESS);
366 struct efi_simple_text_output_protocol efi_con_out = {
367 .reset = efi_cout_reset,
368 .output_string = efi_cout_output_string,
369 .test_string = efi_cout_test_string,
370 .query_mode = efi_cout_query_mode,
371 .set_mode = efi_cout_set_mode,
372 .set_attribute = efi_cout_set_attribute,
373 .clear_screen = efi_cout_clear_screen,
374 .set_cursor_position = efi_cout_set_cursor_position,
375 .enable_cursor = efi_cout_enable_cursor,
376 .mode = (void*)&efi_con_mode,
379 static efi_status_t EFIAPI efi_cin_reset(
380 struct efi_simple_input_interface *this,
381 bool extended_verification)
383 EFI_ENTRY("%p, %d", this, extended_verification);
385 /* Empty input buffer */
389 return EFI_EXIT(EFI_SUCCESS);
393 * Analyze modifiers (shift, alt, ctrl) for function keys.
394 * This gets called when we have already parsed CSI.
396 * @modifiers: bitmask (shift, alt, ctrl)
397 * @return: the unmodified code
399 static char skip_modifiers(int *modifiers)
401 char c, mod = 0, ret = 0;
434 static efi_status_t EFIAPI efi_cin_read_key_stroke(
435 struct efi_simple_input_interface *this,
436 struct efi_input_key *key)
438 struct efi_input_key pressed_key = {
444 EFI_ENTRY("%p, %p", this, key);
446 /* We don't do interrupts, so check for timers cooperatively */
451 return EFI_EXIT(EFI_NOT_READY);
457 * Xterm Control Sequences
458 * https://www.xfree86.org/4.8.0/ctlseqs.html
463 pressed_key.scan_code = 23;
465 case 'O': /* F1 - F4 */
470 pressed_key.scan_code = ch - 'P' + 11;
478 case 'A'...'D': /* up, down right, left */
479 pressed_key.scan_code = ch - 'A' + 1;
482 pressed_key.scan_code = 6;
485 pressed_key.scan_code = 5;
488 ch = skip_modifiers(NULL);
490 case '1'...'5': /* F1 - F5 */
491 pressed_key.scan_code = ch - '1' + 11;
493 case '7'...'9': /* F6 - F8 */
494 pressed_key.scan_code = ch - '7' + 16;
496 case 'A'...'D': /* up, down right, left */
497 pressed_key.scan_code = ch - 'A' + 1;
500 pressed_key.scan_code = 6; /* End */
503 pressed_key.scan_code = 5; /* Home */
508 ch = skip_modifiers(NULL);
510 case '0'...'1': /* F9 - F10 */
511 pressed_key.scan_code = ch - '0' + 19;
513 case '3'...'4': /* F11 - F12 */
514 pressed_key.scan_code = ch - '3' + 21;
517 pressed_key.scan_code = 7;
522 pressed_key.scan_code = 8;
523 skip_modifiers(NULL);
525 case '5': /* PG UP */
526 pressed_key.scan_code = 9;
527 skip_modifiers(NULL);
529 case '6': /* PG DOWN */
530 pressed_key.scan_code = 10;
531 skip_modifiers(NULL);
536 } else if (ch == 0x7f) {
540 if (!pressed_key.scan_code)
541 pressed_key.unicode_char = ch;
544 return EFI_EXIT(EFI_SUCCESS);
547 struct efi_simple_input_interface efi_con_in = {
548 .reset = efi_cin_reset,
549 .read_key_stroke = efi_cin_read_key_stroke,
550 .wait_for_key = NULL,
553 static struct efi_event *console_timer_event;
555 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
560 * Notification function of the console timer event.
562 * event: console timer event
565 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
568 EFI_ENTRY("%p, %p", event, context);
570 /* Check if input is available */
572 /* Queue the wait for key event */
573 efi_con_in.wait_for_key->is_signaled = true;
574 efi_signal_event(efi_con_in.wait_for_key, true);
576 EFI_EXIT(EFI_SUCCESS);
579 /* This gets called from do_bootefi_exec(). */
580 int efi_console_register(void)
583 struct efi_object *efi_console_output_obj;
584 struct efi_object *efi_console_input_obj;
586 /* Set up mode information */
587 query_console_size();
590 r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
591 if (r != EFI_SUCCESS)
593 r = efi_add_protocol(efi_console_output_obj->handle,
594 &efi_guid_text_output_protocol, &efi_con_out);
595 if (r != EFI_SUCCESS)
597 r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
598 if (r != EFI_SUCCESS)
600 r = efi_add_protocol(efi_console_input_obj->handle,
601 &efi_guid_text_input_protocol, &efi_con_in);
602 if (r != EFI_SUCCESS)
605 /* Create console events */
606 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
607 NULL, NULL, &efi_con_in.wait_for_key);
608 if (r != EFI_SUCCESS) {
609 printf("ERROR: Failed to register WaitForKey event\n");
612 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
613 efi_console_timer_notify, NULL, NULL,
614 &console_timer_event);
615 if (r != EFI_SUCCESS) {
616 printf("ERROR: Failed to register console event\n");
619 /* 5000 ns cycle is sufficient for 2 MBaud */
620 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
621 if (r != EFI_SUCCESS)
622 printf("ERROR: Failed to set console timer\n");
625 printf("ERROR: Out of meemory\n");