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_console_control = CONSOLE_CONTROL_GUID;
53 static efi_status_t EFIAPI efi_cin_get_mode(
54 struct efi_console_control_protocol *this,
55 int *mode, char *uga_exists, char *std_in_locked)
57 EFI_ENTRY("%p, %p, %p, %p", this, mode, uga_exists, std_in_locked);
60 *mode = EFI_CONSOLE_MODE_TEXT;
66 return EFI_EXIT(EFI_SUCCESS);
69 static efi_status_t EFIAPI efi_cin_set_mode(
70 struct efi_console_control_protocol *this, int mode)
72 EFI_ENTRY("%p, %d", this, mode);
73 return EFI_EXIT(EFI_UNSUPPORTED);
76 static efi_status_t EFIAPI efi_cin_lock_std_in(
77 struct efi_console_control_protocol *this,
80 EFI_ENTRY("%p, %p", this, password);
81 return EFI_EXIT(EFI_UNSUPPORTED);
84 const struct efi_console_control_protocol efi_console_control = {
85 .get_mode = efi_cin_get_mode,
86 .set_mode = efi_cin_set_mode,
87 .lock_std_in = efi_cin_lock_std_in,
90 /* Default to mode 0 */
91 static struct simple_text_output_mode efi_con_mode = {
100 static int term_read_reply(int *n, int maxnum, char end_char)
121 } else if (c == end_char) {
123 } else if (c > '9' || c < '0') {
127 /* Read one more decimal position */
135 static efi_status_t EFIAPI efi_cout_reset(
136 struct efi_simple_text_output_protocol *this,
137 char extended_verification)
139 EFI_ENTRY("%p, %d", this, extended_verification);
140 return EFI_EXIT(EFI_UNSUPPORTED);
143 static efi_status_t EFIAPI efi_cout_output_string(
144 struct efi_simple_text_output_protocol *this,
145 const efi_string_t string)
147 struct simple_text_output_mode *con = &efi_con_mode;
148 struct cout_mode *mode = &efi_cout_modes[con->mode];
150 EFI_ENTRY("%p, %p", this, string);
152 unsigned int n16 = utf16_strlen(string);
153 char buf[MAX_UTF8_PER_UTF16 * n16 + 1];
156 *utf16_to_utf8((u8 *)buf, string, n16) = '\0';
160 for (p = buf; *p; p++) {
162 case '\r': /* carriage-return */
163 con->cursor_column = 0;
165 case '\n': /* newline */
166 con->cursor_column = 0;
169 case '\t': /* tab, assume 8 char align */
171 case '\b': /* backspace */
172 con->cursor_column = max(0, con->cursor_column - 1);
175 con->cursor_column++;
178 if (con->cursor_column >= mode->columns) {
179 con->cursor_column = 0;
182 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
185 return EFI_EXIT(EFI_SUCCESS);
188 static efi_status_t EFIAPI efi_cout_test_string(
189 struct efi_simple_text_output_protocol *this,
190 const efi_string_t string)
192 EFI_ENTRY("%p, %p", this, string);
193 return EFI_EXIT(EFI_SUCCESS);
196 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
201 return (mode->rows == rows) && (mode->columns == cols);
204 static int query_console_serial(int *rows, int *cols)
206 /* Ask the terminal about its size */
210 /* Empty input buffer */
216 /* Check if we have a terminal that understands */
217 timeout = timer_get_us() + 1000000;
219 if (timer_get_us() > timeout)
222 /* Read {depth,rows,cols} */
223 if (term_read_reply(n, 3, 't'))
232 static efi_status_t EFIAPI efi_cout_query_mode(
233 struct efi_simple_text_output_protocol *this,
234 unsigned long mode_number, unsigned long *columns,
237 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
239 if (!console_size_queried) {
240 const char *stdout_name = env_get("stdout");
243 console_size_queried = true;
245 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
246 IS_ENABLED(CONFIG_DM_VIDEO)) {
247 struct stdio_dev *stdout_dev =
248 stdio_get_by_name("vidconsole");
249 struct udevice *dev = stdout_dev->priv;
250 struct vidconsole_priv *priv =
251 dev_get_uclass_priv(dev);
254 } else if (query_console_serial(&rows, &cols)) {
258 /* Test if we can have Mode 1 */
259 if (cols >= 80 && rows >= 50) {
260 efi_cout_modes[1].present = 1;
261 efi_con_mode.max_mode = 2;
265 * Install our mode as mode 2 if it is different
266 * than mode 0 or 1 and set it as the currently selected mode
268 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
269 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
270 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
271 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
272 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
273 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
274 efi_con_mode.mode = EFI_COUT_MODE_2;
278 if (mode_number >= efi_con_mode.max_mode)
279 return EFI_EXIT(EFI_UNSUPPORTED);
281 if (efi_cout_modes[mode_number].present != 1)
282 return EFI_EXIT(EFI_UNSUPPORTED);
286 *columns = efi_cout_modes[mode_number].columns;
288 *rows = efi_cout_modes[mode_number].rows;
290 return EFI_EXIT(EFI_SUCCESS);
293 static efi_status_t EFIAPI efi_cout_set_mode(
294 struct efi_simple_text_output_protocol *this,
295 unsigned long mode_number)
297 EFI_ENTRY("%p, %ld", this, mode_number);
300 if (mode_number > efi_con_mode.max_mode)
301 return EFI_EXIT(EFI_UNSUPPORTED);
303 efi_con_mode.mode = mode_number;
304 efi_con_mode.cursor_column = 0;
305 efi_con_mode.cursor_row = 0;
307 return EFI_EXIT(EFI_SUCCESS);
310 static efi_status_t EFIAPI efi_cout_set_attribute(
311 struct efi_simple_text_output_protocol *this,
312 unsigned long attribute)
314 EFI_ENTRY("%p, %lx", this, attribute);
316 /* Just ignore attributes (colors) for now */
317 return EFI_EXIT(EFI_UNSUPPORTED);
320 static efi_status_t EFIAPI efi_cout_clear_screen(
321 struct efi_simple_text_output_protocol *this)
323 EFI_ENTRY("%p", this);
327 return EFI_EXIT(EFI_SUCCESS);
330 static efi_status_t EFIAPI efi_cout_set_cursor_position(
331 struct efi_simple_text_output_protocol *this,
332 unsigned long column, unsigned long row)
334 EFI_ENTRY("%p, %ld, %ld", this, column, row);
336 printf(ESC"[%d;%df", (int)row, (int)column);
337 efi_con_mode.cursor_column = column;
338 efi_con_mode.cursor_row = row;
340 return EFI_EXIT(EFI_SUCCESS);
343 static efi_status_t EFIAPI efi_cout_enable_cursor(
344 struct efi_simple_text_output_protocol *this,
347 EFI_ENTRY("%p, %d", this, enable);
349 printf(ESC"[?25%c", enable ? 'h' : 'l');
351 return EFI_EXIT(EFI_SUCCESS);
354 const struct efi_simple_text_output_protocol efi_con_out = {
355 .reset = efi_cout_reset,
356 .output_string = efi_cout_output_string,
357 .test_string = efi_cout_test_string,
358 .query_mode = efi_cout_query_mode,
359 .set_mode = efi_cout_set_mode,
360 .set_attribute = efi_cout_set_attribute,
361 .clear_screen = efi_cout_clear_screen,
362 .set_cursor_position = efi_cout_set_cursor_position,
363 .enable_cursor = efi_cout_enable_cursor,
364 .mode = (void*)&efi_con_mode,
367 static efi_status_t EFIAPI efi_cin_reset(
368 struct efi_simple_input_interface *this,
369 bool extended_verification)
371 EFI_ENTRY("%p, %d", this, extended_verification);
372 return EFI_EXIT(EFI_UNSUPPORTED);
375 static efi_status_t EFIAPI efi_cin_read_key_stroke(
376 struct efi_simple_input_interface *this,
377 struct efi_input_key *key)
379 struct efi_input_key pressed_key = {
385 EFI_ENTRY("%p, %p", this, key);
387 /* We don't do interrupts, so check for timers cooperatively */
392 return EFI_EXIT(EFI_NOT_READY);
397 /* Escape Sequence */
401 pressed_key.scan_code = 23;
403 case 'O': /* F1 - F4 */
404 pressed_key.scan_code = getc() - 'P' + 11;
412 case 'A'...'D': /* up, down right, left */
413 pressed_key.scan_code = ch - 'A' + 1;
416 pressed_key.scan_code = 6;
419 pressed_key.scan_code = 5;
421 case '1': /* F5 - F8 */
422 pressed_key.scan_code = getc() - '0' + 11;
425 case '2': /* F9 - F12 */
426 pressed_key.scan_code = getc() - '0' + 19;
430 pressed_key.scan_code = 8;
436 } else if (ch == 0x7f) {
440 pressed_key.unicode_char = ch;
443 return EFI_EXIT(EFI_SUCCESS);
446 struct efi_simple_input_interface efi_con_in = {
447 .reset = efi_cin_reset,
448 .read_key_stroke = efi_cin_read_key_stroke,
449 .wait_for_key = NULL,
452 static struct efi_event *console_timer_event;
454 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
458 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
461 EFI_ENTRY("%p, %p", event, context);
463 efi_con_in.wait_for_key->is_signaled = true;
464 efi_signal_event(efi_con_in.wait_for_key);
466 EFI_EXIT(EFI_SUCCESS);
470 static struct efi_object efi_console_control_obj =
471 EFI_PROTOCOL_OBJECT(efi_guid_console_control, &efi_console_control);
472 static struct efi_object efi_console_output_obj =
473 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, &efi_con_out);
474 static struct efi_object efi_console_input_obj =
475 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, &efi_con_in);
477 /* This gets called from do_bootefi_exec(). */
478 int efi_console_register(void)
482 /* Hook up to the device list */
483 list_add_tail(&efi_console_control_obj.link, &efi_obj_list);
484 list_add_tail(&efi_console_output_obj.link, &efi_obj_list);
485 list_add_tail(&efi_console_input_obj.link, &efi_obj_list);
487 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
488 efi_key_notify, NULL, &efi_con_in.wait_for_key);
489 if (r != EFI_SUCCESS) {
490 printf("ERROR: Failed to register WaitForKey event\n");
493 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
494 efi_console_timer_notify, NULL,
495 &console_timer_event);
496 if (r != EFI_SUCCESS) {
497 printf("ERROR: Failed to register console event\n");
500 /* 5000 ns cycle is sufficient for 2 MBaud */
501 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
502 if (r != EFI_SUCCESS)
503 printf("ERROR: Failed to set console timer\n");