2 * EFI application console interface
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <dm/device.h>
12 #include <efi_loader.h>
13 #include <stdio_dev.h>
14 #include <video_console.h>
16 static bool console_size_queried;
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_output_protocol =
49 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
50 const efi_guid_t efi_guid_text_input_protocol =
51 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
56 /* Default to mode 0 */
57 static struct simple_text_output_mode efi_con_mode = {
66 static int term_read_reply(int *n, int maxnum, char end_char)
87 } else if (c == end_char) {
89 } else if (c > '9' || c < '0') {
93 /* Read one more decimal position */
101 static efi_status_t EFIAPI efi_cout_reset(
102 struct efi_simple_text_output_protocol *this,
103 char extended_verification)
105 EFI_ENTRY("%p, %d", this, extended_verification);
106 return EFI_EXIT(EFI_UNSUPPORTED);
109 static efi_status_t EFIAPI efi_cout_output_string(
110 struct efi_simple_text_output_protocol *this,
111 const efi_string_t string)
113 struct simple_text_output_mode *con = &efi_con_mode;
114 struct cout_mode *mode = &efi_cout_modes[con->mode];
116 EFI_ENTRY("%p, %p", this, string);
118 unsigned int n16 = utf16_strlen(string);
119 char buf[MAX_UTF8_PER_UTF16 * n16 + 1];
122 *utf16_to_utf8((u8 *)buf, string, n16) = '\0';
126 for (p = buf; *p; p++) {
128 case '\r': /* carriage-return */
129 con->cursor_column = 0;
131 case '\n': /* newline */
132 con->cursor_column = 0;
135 case '\t': /* tab, assume 8 char align */
137 case '\b': /* backspace */
138 con->cursor_column = max(0, con->cursor_column - 1);
141 con->cursor_column++;
144 if (con->cursor_column >= mode->columns) {
145 con->cursor_column = 0;
148 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
151 return EFI_EXIT(EFI_SUCCESS);
154 static efi_status_t EFIAPI efi_cout_test_string(
155 struct efi_simple_text_output_protocol *this,
156 const efi_string_t string)
158 EFI_ENTRY("%p, %p", this, string);
159 return EFI_EXIT(EFI_SUCCESS);
162 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
167 return (mode->rows == rows) && (mode->columns == cols);
170 static int query_console_serial(int *rows, int *cols)
172 /* Ask the terminal about its size */
176 /* Empty input buffer */
182 /* Check if we have a terminal that understands */
183 timeout = timer_get_us() + 1000000;
185 if (timer_get_us() > timeout)
188 /* Read {depth,rows,cols} */
189 if (term_read_reply(n, 3, 't'))
198 static efi_status_t EFIAPI efi_cout_query_mode(
199 struct efi_simple_text_output_protocol *this,
200 unsigned long mode_number, unsigned long *columns,
203 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
205 if (!console_size_queried) {
206 const char *stdout_name = env_get("stdout");
209 console_size_queried = true;
211 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
212 IS_ENABLED(CONFIG_DM_VIDEO)) {
213 struct stdio_dev *stdout_dev =
214 stdio_get_by_name("vidconsole");
215 struct udevice *dev = stdout_dev->priv;
216 struct vidconsole_priv *priv =
217 dev_get_uclass_priv(dev);
220 } else if (query_console_serial(&rows, &cols)) {
224 /* Test if we can have Mode 1 */
225 if (cols >= 80 && rows >= 50) {
226 efi_cout_modes[1].present = 1;
227 efi_con_mode.max_mode = 2;
231 * Install our mode as mode 2 if it is different
232 * than mode 0 or 1 and set it as the currently selected mode
234 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
235 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
236 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
237 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
238 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
239 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
240 efi_con_mode.mode = EFI_COUT_MODE_2;
244 if (mode_number >= efi_con_mode.max_mode)
245 return EFI_EXIT(EFI_UNSUPPORTED);
247 if (efi_cout_modes[mode_number].present != 1)
248 return EFI_EXIT(EFI_UNSUPPORTED);
252 *columns = efi_cout_modes[mode_number].columns;
254 *rows = efi_cout_modes[mode_number].rows;
256 return EFI_EXIT(EFI_SUCCESS);
259 static efi_status_t EFIAPI efi_cout_set_mode(
260 struct efi_simple_text_output_protocol *this,
261 unsigned long mode_number)
263 EFI_ENTRY("%p, %ld", this, mode_number);
266 if (mode_number > efi_con_mode.max_mode)
267 return EFI_EXIT(EFI_UNSUPPORTED);
269 efi_con_mode.mode = mode_number;
270 efi_con_mode.cursor_column = 0;
271 efi_con_mode.cursor_row = 0;
273 return EFI_EXIT(EFI_SUCCESS);
276 static const struct {
280 { 30, 40 }, /* 0: black */
281 { 34, 44 }, /* 1: blue */
282 { 32, 42 }, /* 2: green */
283 { 36, 46 }, /* 3: cyan */
284 { 31, 41 }, /* 4: red */
285 { 35, 45 }, /* 5: magenta */
286 { 33, 43 }, /* 6: brown, map to yellow as edk2 does*/
287 { 37, 47 }, /* 7: light grey, map to white */
290 /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
291 static efi_status_t EFIAPI efi_cout_set_attribute(
292 struct efi_simple_text_output_protocol *this,
293 unsigned long attribute)
295 unsigned int bold = EFI_ATTR_BOLD(attribute);
296 unsigned int fg = EFI_ATTR_FG(attribute);
297 unsigned int bg = EFI_ATTR_BG(attribute);
299 EFI_ENTRY("%p, %lx", this, attribute);
302 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
304 printf(ESC"[0;37;40m");
306 return EFI_EXIT(EFI_SUCCESS);
309 static efi_status_t EFIAPI efi_cout_clear_screen(
310 struct efi_simple_text_output_protocol *this)
312 EFI_ENTRY("%p", this);
316 return EFI_EXIT(EFI_SUCCESS);
319 static efi_status_t EFIAPI efi_cout_set_cursor_position(
320 struct efi_simple_text_output_protocol *this,
321 unsigned long column, unsigned long row)
323 EFI_ENTRY("%p, %ld, %ld", this, column, row);
325 printf(ESC"[%d;%df", (int)row, (int)column);
326 efi_con_mode.cursor_column = column;
327 efi_con_mode.cursor_row = row;
329 return EFI_EXIT(EFI_SUCCESS);
332 static efi_status_t EFIAPI efi_cout_enable_cursor(
333 struct efi_simple_text_output_protocol *this,
336 EFI_ENTRY("%p, %d", this, enable);
338 printf(ESC"[?25%c", enable ? 'h' : 'l');
340 return EFI_EXIT(EFI_SUCCESS);
343 struct efi_simple_text_output_protocol efi_con_out = {
344 .reset = efi_cout_reset,
345 .output_string = efi_cout_output_string,
346 .test_string = efi_cout_test_string,
347 .query_mode = efi_cout_query_mode,
348 .set_mode = efi_cout_set_mode,
349 .set_attribute = efi_cout_set_attribute,
350 .clear_screen = efi_cout_clear_screen,
351 .set_cursor_position = efi_cout_set_cursor_position,
352 .enable_cursor = efi_cout_enable_cursor,
353 .mode = (void*)&efi_con_mode,
356 static efi_status_t EFIAPI efi_cin_reset(
357 struct efi_simple_input_interface *this,
358 bool extended_verification)
360 EFI_ENTRY("%p, %d", this, extended_verification);
361 return EFI_EXIT(EFI_UNSUPPORTED);
365 * Analyze modifiers (shift, alt, ctrl) for function keys.
366 * This gets called when we have already parsed CSI.
368 * @modifiers: bitmask (shift, alt, ctrl)
369 * @return: the unmodified code
371 static char skip_modifiers(int *modifiers)
373 char c, mod = 0, ret = 0;
406 static efi_status_t EFIAPI efi_cin_read_key_stroke(
407 struct efi_simple_input_interface *this,
408 struct efi_input_key *key)
410 struct efi_input_key pressed_key = {
416 EFI_ENTRY("%p, %p", this, key);
418 /* We don't do interrupts, so check for timers cooperatively */
423 return EFI_EXIT(EFI_NOT_READY);
429 * Xterm Control Sequences
430 * https://www.xfree86.org/4.8.0/ctlseqs.html
435 pressed_key.scan_code = 23;
437 case 'O': /* F1 - F4 */
442 pressed_key.scan_code = ch - 'P' + 11;
450 case 'A'...'D': /* up, down right, left */
451 pressed_key.scan_code = ch - 'A' + 1;
454 pressed_key.scan_code = 6;
457 pressed_key.scan_code = 5;
460 ch = skip_modifiers(NULL);
462 case '1'...'5': /* F1 - F5 */
463 pressed_key.scan_code = ch - '1' + 11;
465 case '7'...'9': /* F6 - F8 */
466 pressed_key.scan_code = ch - '7' + 16;
468 case 'A'...'D': /* up, down right, left */
469 pressed_key.scan_code = ch - 'A' + 1;
472 pressed_key.scan_code = 6; /* End */
475 pressed_key.scan_code = 5; /* Home */
480 ch = skip_modifiers(NULL);
482 case '0'...'1': /* F9 - F10 */
483 pressed_key.scan_code = ch - '0' + 19;
485 case '3'...'4': /* F11 - F12 */
486 pressed_key.scan_code = ch - '3' + 21;
489 pressed_key.scan_code = 7;
494 pressed_key.scan_code = 8;
495 skip_modifiers(NULL);
497 case '5': /* PG UP */
498 pressed_key.scan_code = 9;
499 skip_modifiers(NULL);
501 case '6': /* PG DOWN */
502 pressed_key.scan_code = 10;
503 skip_modifiers(NULL);
508 } else if (ch == 0x7f) {
512 if (!pressed_key.scan_code)
513 pressed_key.unicode_char = ch;
516 return EFI_EXIT(EFI_SUCCESS);
519 struct efi_simple_input_interface efi_con_in = {
520 .reset = efi_cin_reset,
521 .read_key_stroke = efi_cin_read_key_stroke,
522 .wait_for_key = NULL,
525 static struct efi_event *console_timer_event;
527 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
532 * Notification function of the console timer event.
534 * event: console timer event
537 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
540 EFI_ENTRY("%p, %p", event, context);
542 /* Check if input is available */
544 /* Queue the wait for key event */
545 efi_con_in.wait_for_key->is_signaled = true;
546 efi_signal_event(efi_con_in.wait_for_key, true);
548 EFI_EXIT(EFI_SUCCESS);
551 /* This gets called from do_bootefi_exec(). */
552 int efi_console_register(void)
555 struct efi_object *efi_console_output_obj;
556 struct efi_object *efi_console_input_obj;
559 r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
560 if (r != EFI_SUCCESS)
562 r = efi_add_protocol(efi_console_output_obj->handle,
563 &efi_guid_text_output_protocol, &efi_con_out);
564 if (r != EFI_SUCCESS)
566 r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
567 if (r != EFI_SUCCESS)
569 r = efi_add_protocol(efi_console_input_obj->handle,
570 &efi_guid_text_input_protocol, &efi_con_in);
571 if (r != EFI_SUCCESS)
574 /* Create console events */
575 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
576 NULL, NULL, &efi_con_in.wait_for_key);
577 if (r != EFI_SUCCESS) {
578 printf("ERROR: Failed to register WaitForKey event\n");
581 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
582 efi_console_timer_notify, NULL, NULL,
583 &console_timer_event);
584 if (r != EFI_SUCCESS) {
585 printf("ERROR: Failed to register console event\n");
588 /* 5000 ns cycle is sufficient for 2 MBaud */
589 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
590 if (r != EFI_SUCCESS)
591 printf("ERROR: Failed to set console timer\n");
594 printf("ERROR: Out of meemory\n");