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 = {
63 static int term_read_reply(int *n, int maxnum, char end_char)
84 } else if (c == end_char) {
86 } else if (c > '9' || c < '0') {
90 /* Read one more decimal position */
98 static efi_status_t EFIAPI efi_cout_reset(
99 struct efi_simple_text_output_protocol *this,
100 char extended_verification)
102 EFI_ENTRY("%p, %d", this, extended_verification);
103 return EFI_EXIT(EFI_UNSUPPORTED);
106 static efi_status_t EFIAPI efi_cout_output_string(
107 struct efi_simple_text_output_protocol *this,
108 const efi_string_t string)
110 struct simple_text_output_mode *con = &efi_con_mode;
111 struct cout_mode *mode = &efi_cout_modes[con->mode];
113 EFI_ENTRY("%p, %p", this, string);
115 unsigned int n16 = utf16_strlen(string);
116 char buf[MAX_UTF8_PER_UTF16 * n16 + 1];
119 *utf16_to_utf8((u8 *)buf, string, n16) = '\0';
124 * Update the cursor position.
126 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
127 * and U000D. All other characters, including control characters
128 * U+0007 (bel) and U+0009 (tab), have to increase the column by one.
130 for (p = string; *p; ++p) {
132 case '\b': /* U+0008, backspace */
133 con->cursor_column = max(0, con->cursor_column - 1);
135 case '\n': /* U+000A, newline */
136 con->cursor_column = 0;
139 case '\r': /* U+000D, carriage-return */
140 con->cursor_column = 0;
142 case 0xd800 ... 0xdbff:
144 * Ignore high surrogates, we do not want to count a
145 * Unicode character twice.
149 con->cursor_column++;
152 if (con->cursor_column >= mode->columns) {
153 con->cursor_column = 0;
156 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
159 return EFI_EXIT(EFI_SUCCESS);
162 static efi_status_t EFIAPI efi_cout_test_string(
163 struct efi_simple_text_output_protocol *this,
164 const efi_string_t string)
166 EFI_ENTRY("%p, %p", this, string);
167 return EFI_EXIT(EFI_SUCCESS);
170 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
175 return (mode->rows == rows) && (mode->columns == cols);
178 static int query_console_serial(int *rows, int *cols)
180 /* Ask the terminal about its size */
184 /* Empty input buffer */
190 /* Check if we have a terminal that understands */
191 timeout = timer_get_us() + 1000000;
193 if (timer_get_us() > timeout)
196 /* Read {depth,rows,cols} */
197 if (term_read_reply(n, 3, 't'))
207 * Update the mode table.
209 * By default the only mode available is 80x25. If the console has at least 50
210 * lines, enable mode 80x50. If we can query the console size and it is neither
211 * 80x25 nor 80x50, set it as an additional mode.
213 static void query_console_size(void)
215 const char *stdout_name = env_get("stdout");
218 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
219 IS_ENABLED(CONFIG_DM_VIDEO)) {
220 struct stdio_dev *stdout_dev =
221 stdio_get_by_name("vidconsole");
222 struct udevice *dev = stdout_dev->priv;
223 struct vidconsole_priv *priv =
224 dev_get_uclass_priv(dev);
227 } else if (query_console_serial(&rows, &cols)) {
231 /* Test if we can have Mode 1 */
232 if (cols >= 80 && rows >= 50) {
233 efi_cout_modes[1].present = 1;
234 efi_con_mode.max_mode = 2;
238 * Install our mode as mode 2 if it is different
239 * than mode 0 or 1 and set it as the currently selected mode
241 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
242 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
243 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
244 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
245 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
246 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
247 efi_con_mode.mode = EFI_COUT_MODE_2;
251 static efi_status_t EFIAPI efi_cout_query_mode(
252 struct efi_simple_text_output_protocol *this,
253 unsigned long mode_number, unsigned long *columns,
256 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
258 if (mode_number >= efi_con_mode.max_mode)
259 return EFI_EXIT(EFI_UNSUPPORTED);
261 if (efi_cout_modes[mode_number].present != 1)
262 return EFI_EXIT(EFI_UNSUPPORTED);
265 *columns = efi_cout_modes[mode_number].columns;
267 *rows = efi_cout_modes[mode_number].rows;
269 return EFI_EXIT(EFI_SUCCESS);
272 static efi_status_t EFIAPI efi_cout_set_mode(
273 struct efi_simple_text_output_protocol *this,
274 unsigned long mode_number)
276 EFI_ENTRY("%p, %ld", this, mode_number);
279 if (mode_number > efi_con_mode.max_mode)
280 return EFI_EXIT(EFI_UNSUPPORTED);
282 efi_con_mode.mode = mode_number;
283 efi_con_mode.cursor_column = 0;
284 efi_con_mode.cursor_row = 0;
286 return EFI_EXIT(EFI_SUCCESS);
289 static const struct {
293 { 30, 40 }, /* 0: black */
294 { 34, 44 }, /* 1: blue */
295 { 32, 42 }, /* 2: green */
296 { 36, 46 }, /* 3: cyan */
297 { 31, 41 }, /* 4: red */
298 { 35, 45 }, /* 5: magenta */
299 { 33, 43 }, /* 6: brown, map to yellow as edk2 does*/
300 { 37, 47 }, /* 7: light grey, map to white */
303 /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
304 static efi_status_t EFIAPI efi_cout_set_attribute(
305 struct efi_simple_text_output_protocol *this,
306 unsigned long attribute)
308 unsigned int bold = EFI_ATTR_BOLD(attribute);
309 unsigned int fg = EFI_ATTR_FG(attribute);
310 unsigned int bg = EFI_ATTR_BG(attribute);
312 EFI_ENTRY("%p, %lx", this, attribute);
315 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
317 printf(ESC"[0;37;40m");
319 return EFI_EXIT(EFI_SUCCESS);
322 static efi_status_t EFIAPI efi_cout_clear_screen(
323 struct efi_simple_text_output_protocol *this)
325 EFI_ENTRY("%p", this);
329 return EFI_EXIT(EFI_SUCCESS);
332 static efi_status_t EFIAPI efi_cout_set_cursor_position(
333 struct efi_simple_text_output_protocol *this,
334 unsigned long column, unsigned long row)
336 EFI_ENTRY("%p, %ld, %ld", this, column, row);
338 printf(ESC"[%d;%df", (int)row, (int)column);
339 efi_con_mode.cursor_column = column;
340 efi_con_mode.cursor_row = row;
342 return EFI_EXIT(EFI_SUCCESS);
345 static efi_status_t EFIAPI efi_cout_enable_cursor(
346 struct efi_simple_text_output_protocol *this,
349 EFI_ENTRY("%p, %d", this, enable);
351 printf(ESC"[?25%c", enable ? 'h' : 'l');
353 return EFI_EXIT(EFI_SUCCESS);
356 struct efi_simple_text_output_protocol efi_con_out = {
357 .reset = efi_cout_reset,
358 .output_string = efi_cout_output_string,
359 .test_string = efi_cout_test_string,
360 .query_mode = efi_cout_query_mode,
361 .set_mode = efi_cout_set_mode,
362 .set_attribute = efi_cout_set_attribute,
363 .clear_screen = efi_cout_clear_screen,
364 .set_cursor_position = efi_cout_set_cursor_position,
365 .enable_cursor = efi_cout_enable_cursor,
366 .mode = (void*)&efi_con_mode,
369 static efi_status_t EFIAPI efi_cin_reset(
370 struct efi_simple_input_interface *this,
371 bool extended_verification)
373 EFI_ENTRY("%p, %d", this, extended_verification);
374 return EFI_EXIT(EFI_UNSUPPORTED);
378 * Analyze modifiers (shift, alt, ctrl) for function keys.
379 * This gets called when we have already parsed CSI.
381 * @modifiers: bitmask (shift, alt, ctrl)
382 * @return: the unmodified code
384 static char skip_modifiers(int *modifiers)
386 char c, mod = 0, ret = 0;
419 static efi_status_t EFIAPI efi_cin_read_key_stroke(
420 struct efi_simple_input_interface *this,
421 struct efi_input_key *key)
423 struct efi_input_key pressed_key = {
429 EFI_ENTRY("%p, %p", this, key);
431 /* We don't do interrupts, so check for timers cooperatively */
436 return EFI_EXIT(EFI_NOT_READY);
442 * Xterm Control Sequences
443 * https://www.xfree86.org/4.8.0/ctlseqs.html
448 pressed_key.scan_code = 23;
450 case 'O': /* F1 - F4 */
455 pressed_key.scan_code = ch - 'P' + 11;
463 case 'A'...'D': /* up, down right, left */
464 pressed_key.scan_code = ch - 'A' + 1;
467 pressed_key.scan_code = 6;
470 pressed_key.scan_code = 5;
473 ch = skip_modifiers(NULL);
475 case '1'...'5': /* F1 - F5 */
476 pressed_key.scan_code = ch - '1' + 11;
478 case '7'...'9': /* F6 - F8 */
479 pressed_key.scan_code = ch - '7' + 16;
481 case 'A'...'D': /* up, down right, left */
482 pressed_key.scan_code = ch - 'A' + 1;
485 pressed_key.scan_code = 6; /* End */
488 pressed_key.scan_code = 5; /* Home */
493 ch = skip_modifiers(NULL);
495 case '0'...'1': /* F9 - F10 */
496 pressed_key.scan_code = ch - '0' + 19;
498 case '3'...'4': /* F11 - F12 */
499 pressed_key.scan_code = ch - '3' + 21;
502 pressed_key.scan_code = 7;
507 pressed_key.scan_code = 8;
508 skip_modifiers(NULL);
510 case '5': /* PG UP */
511 pressed_key.scan_code = 9;
512 skip_modifiers(NULL);
514 case '6': /* PG DOWN */
515 pressed_key.scan_code = 10;
516 skip_modifiers(NULL);
521 } else if (ch == 0x7f) {
525 if (!pressed_key.scan_code)
526 pressed_key.unicode_char = ch;
529 return EFI_EXIT(EFI_SUCCESS);
532 struct efi_simple_input_interface efi_con_in = {
533 .reset = efi_cin_reset,
534 .read_key_stroke = efi_cin_read_key_stroke,
535 .wait_for_key = NULL,
538 static struct efi_event *console_timer_event;
540 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
545 * Notification function of the console timer event.
547 * event: console timer event
550 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
553 EFI_ENTRY("%p, %p", event, context);
555 /* Check if input is available */
557 /* Queue the wait for key event */
558 efi_con_in.wait_for_key->is_signaled = true;
559 efi_signal_event(efi_con_in.wait_for_key, true);
561 EFI_EXIT(EFI_SUCCESS);
564 /* This gets called from do_bootefi_exec(). */
565 int efi_console_register(void)
568 struct efi_object *efi_console_output_obj;
569 struct efi_object *efi_console_input_obj;
571 /* Set up mode information */
572 query_console_size();
575 r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
576 if (r != EFI_SUCCESS)
578 r = efi_add_protocol(efi_console_output_obj->handle,
579 &efi_guid_text_output_protocol, &efi_con_out);
580 if (r != EFI_SUCCESS)
582 r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
583 if (r != EFI_SUCCESS)
585 r = efi_add_protocol(efi_console_input_obj->handle,
586 &efi_guid_text_input_protocol, &efi_con_in);
587 if (r != EFI_SUCCESS)
590 /* Create console events */
591 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
592 NULL, NULL, &efi_con_in.wait_for_key);
593 if (r != EFI_SUCCESS) {
594 printf("ERROR: Failed to register WaitForKey event\n");
597 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
598 efi_console_timer_notify, NULL, NULL,
599 &console_timer_event);
600 if (r != EFI_SUCCESS) {
601 printf("ERROR: Failed to register console event\n");
604 /* 5000 ns cycle is sufficient for 2 MBaud */
605 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
606 if (r != EFI_SUCCESS)
607 printf("ERROR: Failed to set console timer\n");
610 printf("ERROR: Out of meemory\n");