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 static bool console_size_queried;
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_output_protocol =
48 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
49 const efi_guid_t efi_guid_text_input_protocol =
50 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
55 /* Default to mode 0 */
56 static struct simple_text_output_mode efi_con_mode = {
65 static int term_read_reply(int *n, int maxnum, char end_char)
86 } else if (c == end_char) {
88 } else if (c > '9' || c < '0') {
92 /* Read one more decimal position */
100 static efi_status_t EFIAPI efi_cout_reset(
101 struct efi_simple_text_output_protocol *this,
102 char extended_verification)
104 EFI_ENTRY("%p, %d", this, extended_verification);
105 return EFI_EXIT(EFI_UNSUPPORTED);
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];
115 EFI_ENTRY("%p, %p", this, string);
117 unsigned int n16 = utf16_strlen(string);
118 char buf[MAX_UTF8_PER_UTF16 * n16 + 1];
121 *utf16_to_utf8((u8 *)buf, string, n16) = '\0';
125 for (p = buf; *p; p++) {
127 case '\r': /* carriage-return */
128 con->cursor_column = 0;
130 case '\n': /* newline */
131 con->cursor_column = 0;
134 case '\t': /* tab, assume 8 char align */
136 case '\b': /* backspace */
137 con->cursor_column = max(0, con->cursor_column - 1);
140 con->cursor_column++;
143 if (con->cursor_column >= mode->columns) {
144 con->cursor_column = 0;
147 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
150 return EFI_EXIT(EFI_SUCCESS);
153 static efi_status_t EFIAPI efi_cout_test_string(
154 struct efi_simple_text_output_protocol *this,
155 const efi_string_t string)
157 EFI_ENTRY("%p, %p", this, string);
158 return EFI_EXIT(EFI_SUCCESS);
161 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
166 return (mode->rows == rows) && (mode->columns == cols);
169 static int query_console_serial(int *rows, int *cols)
171 /* Ask the terminal about its size */
175 /* Empty input buffer */
181 /* Check if we have a terminal that understands */
182 timeout = timer_get_us() + 1000000;
184 if (timer_get_us() > timeout)
187 /* Read {depth,rows,cols} */
188 if (term_read_reply(n, 3, 't'))
197 static efi_status_t EFIAPI efi_cout_query_mode(
198 struct efi_simple_text_output_protocol *this,
199 unsigned long mode_number, unsigned long *columns,
202 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
204 if (!console_size_queried) {
205 const char *stdout_name = env_get("stdout");
208 console_size_queried = true;
210 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
211 IS_ENABLED(CONFIG_DM_VIDEO)) {
212 struct stdio_dev *stdout_dev =
213 stdio_get_by_name("vidconsole");
214 struct udevice *dev = stdout_dev->priv;
215 struct vidconsole_priv *priv =
216 dev_get_uclass_priv(dev);
219 } else if (query_console_serial(&rows, &cols)) {
223 /* Test if we can have Mode 1 */
224 if (cols >= 80 && rows >= 50) {
225 efi_cout_modes[1].present = 1;
226 efi_con_mode.max_mode = 2;
230 * Install our mode as mode 2 if it is different
231 * than mode 0 or 1 and set it as the currently selected mode
233 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
234 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
235 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
236 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
237 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
238 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
239 efi_con_mode.mode = EFI_COUT_MODE_2;
243 if (mode_number >= efi_con_mode.max_mode)
244 return EFI_EXIT(EFI_UNSUPPORTED);
246 if (efi_cout_modes[mode_number].present != 1)
247 return EFI_EXIT(EFI_UNSUPPORTED);
251 *columns = efi_cout_modes[mode_number].columns;
253 *rows = efi_cout_modes[mode_number].rows;
255 return EFI_EXIT(EFI_SUCCESS);
258 static efi_status_t EFIAPI efi_cout_set_mode(
259 struct efi_simple_text_output_protocol *this,
260 unsigned long mode_number)
262 EFI_ENTRY("%p, %ld", this, mode_number);
265 if (mode_number > efi_con_mode.max_mode)
266 return EFI_EXIT(EFI_UNSUPPORTED);
268 efi_con_mode.mode = mode_number;
269 efi_con_mode.cursor_column = 0;
270 efi_con_mode.cursor_row = 0;
272 return EFI_EXIT(EFI_SUCCESS);
275 static const struct {
279 { 30, 40 }, /* 0: black */
280 { 34, 44 }, /* 1: blue */
281 { 32, 42 }, /* 2: green */
282 { 36, 46 }, /* 3: cyan */
283 { 31, 41 }, /* 4: red */
284 { 35, 45 }, /* 5: magenta */
285 { 33, 43 }, /* 6: brown, map to yellow as edk2 does*/
286 { 37, 47 }, /* 7: light grey, map to white */
289 /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
290 static efi_status_t EFIAPI efi_cout_set_attribute(
291 struct efi_simple_text_output_protocol *this,
292 unsigned long attribute)
294 unsigned int bold = EFI_ATTR_BOLD(attribute);
295 unsigned int fg = EFI_ATTR_FG(attribute);
296 unsigned int bg = EFI_ATTR_BG(attribute);
298 EFI_ENTRY("%p, %lx", this, attribute);
301 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
303 printf(ESC"[0;37;40m");
305 return EFI_EXIT(EFI_SUCCESS);
308 static efi_status_t EFIAPI efi_cout_clear_screen(
309 struct efi_simple_text_output_protocol *this)
311 EFI_ENTRY("%p", this);
315 return EFI_EXIT(EFI_SUCCESS);
318 static efi_status_t EFIAPI efi_cout_set_cursor_position(
319 struct efi_simple_text_output_protocol *this,
320 unsigned long column, unsigned long row)
322 EFI_ENTRY("%p, %ld, %ld", this, column, row);
324 printf(ESC"[%d;%df", (int)row, (int)column);
325 efi_con_mode.cursor_column = column;
326 efi_con_mode.cursor_row = row;
328 return EFI_EXIT(EFI_SUCCESS);
331 static efi_status_t EFIAPI efi_cout_enable_cursor(
332 struct efi_simple_text_output_protocol *this,
335 EFI_ENTRY("%p, %d", this, enable);
337 printf(ESC"[?25%c", enable ? 'h' : 'l');
339 return EFI_EXIT(EFI_SUCCESS);
342 struct efi_simple_text_output_protocol efi_con_out = {
343 .reset = efi_cout_reset,
344 .output_string = efi_cout_output_string,
345 .test_string = efi_cout_test_string,
346 .query_mode = efi_cout_query_mode,
347 .set_mode = efi_cout_set_mode,
348 .set_attribute = efi_cout_set_attribute,
349 .clear_screen = efi_cout_clear_screen,
350 .set_cursor_position = efi_cout_set_cursor_position,
351 .enable_cursor = efi_cout_enable_cursor,
352 .mode = (void*)&efi_con_mode,
355 static efi_status_t EFIAPI efi_cin_reset(
356 struct efi_simple_input_interface *this,
357 bool extended_verification)
359 EFI_ENTRY("%p, %d", this, extended_verification);
360 return EFI_EXIT(EFI_UNSUPPORTED);
364 * Analyze modifiers (shift, alt, ctrl) for function keys.
365 * This gets called when we have already parsed CSI.
367 * @modifiers: bitmask (shift, alt, ctrl)
368 * @return: the unmodified code
370 static char skip_modifiers(int *modifiers)
372 char c, mod = 0, ret = 0;
405 static efi_status_t EFIAPI efi_cin_read_key_stroke(
406 struct efi_simple_input_interface *this,
407 struct efi_input_key *key)
409 struct efi_input_key pressed_key = {
415 EFI_ENTRY("%p, %p", this, key);
417 /* We don't do interrupts, so check for timers cooperatively */
422 return EFI_EXIT(EFI_NOT_READY);
428 * Xterm Control Sequences
429 * https://www.xfree86.org/4.8.0/ctlseqs.html
434 pressed_key.scan_code = 23;
436 case 'O': /* F1 - F4 */
441 pressed_key.scan_code = ch - 'P' + 11;
449 case 'A'...'D': /* up, down right, left */
450 pressed_key.scan_code = ch - 'A' + 1;
453 pressed_key.scan_code = 6;
456 pressed_key.scan_code = 5;
459 ch = skip_modifiers(NULL);
461 case '1'...'5': /* F1 - F5 */
462 pressed_key.scan_code = ch - '1' + 11;
464 case '7'...'9': /* F6 - F8 */
465 pressed_key.scan_code = ch - '7' + 16;
467 case 'A'...'D': /* up, down right, left */
468 pressed_key.scan_code = ch - 'A' + 1;
471 pressed_key.scan_code = 6; /* End */
474 pressed_key.scan_code = 5; /* Home */
479 ch = skip_modifiers(NULL);
481 case '0'...'1': /* F9 - F10 */
482 pressed_key.scan_code = ch - '0' + 19;
484 case '3'...'4': /* F11 - F12 */
485 pressed_key.scan_code = ch - '3' + 21;
488 pressed_key.scan_code = 7;
493 pressed_key.scan_code = 8;
494 skip_modifiers(NULL);
496 case '5': /* PG UP */
497 pressed_key.scan_code = 9;
498 skip_modifiers(NULL);
500 case '6': /* PG DOWN */
501 pressed_key.scan_code = 10;
502 skip_modifiers(NULL);
507 } else if (ch == 0x7f) {
511 if (!pressed_key.scan_code)
512 pressed_key.unicode_char = ch;
515 return EFI_EXIT(EFI_SUCCESS);
518 struct efi_simple_input_interface efi_con_in = {
519 .reset = efi_cin_reset,
520 .read_key_stroke = efi_cin_read_key_stroke,
521 .wait_for_key = NULL,
524 static struct efi_event *console_timer_event;
526 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
531 * Notification function of the console timer event.
533 * event: console timer event
536 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
539 EFI_ENTRY("%p, %p", event, context);
541 /* Check if input is available */
543 /* Queue the wait for key event */
544 efi_con_in.wait_for_key->is_signaled = true;
545 efi_signal_event(efi_con_in.wait_for_key, true);
547 EFI_EXIT(EFI_SUCCESS);
550 /* This gets called from do_bootefi_exec(). */
551 int efi_console_register(void)
554 struct efi_object *efi_console_output_obj;
555 struct efi_object *efi_console_input_obj;
558 r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
559 if (r != EFI_SUCCESS)
561 r = efi_add_protocol(efi_console_output_obj->handle,
562 &efi_guid_text_output_protocol, &efi_con_out);
563 if (r != EFI_SUCCESS)
565 r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
566 if (r != EFI_SUCCESS)
568 r = efi_add_protocol(efi_console_input_obj->handle,
569 &efi_guid_text_input_protocol, &efi_con_in);
570 if (r != EFI_SUCCESS)
573 /* Create console events */
574 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
575 NULL, NULL, &efi_con_in.wait_for_key);
576 if (r != EFI_SUCCESS) {
577 printf("ERROR: Failed to register WaitForKey event\n");
580 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
581 efi_console_timer_notify, NULL, NULL,
582 &console_timer_event);
583 if (r != EFI_SUCCESS) {
584 printf("ERROR: Failed to register console event\n");
587 /* 5000 ns cycle is sufficient for 2 MBaud */
588 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
589 if (r != EFI_SUCCESS)
590 printf("ERROR: Failed to set console timer\n");
593 printf("ERROR: Out of meemory\n");