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;
49 const efi_guid_t efi_guid_text_output_protocol =
50 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
51 const efi_guid_t efi_guid_text_input_protocol =
52 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
57 static efi_status_t EFIAPI efi_cin_get_mode(
58 struct efi_console_control_protocol *this,
59 int *mode, char *uga_exists, char *std_in_locked)
61 EFI_ENTRY("%p, %p, %p, %p", this, mode, uga_exists, std_in_locked);
64 *mode = EFI_CONSOLE_MODE_TEXT;
70 return EFI_EXIT(EFI_SUCCESS);
73 static efi_status_t EFIAPI efi_cin_set_mode(
74 struct efi_console_control_protocol *this, int mode)
76 EFI_ENTRY("%p, %d", this, mode);
77 return EFI_EXIT(EFI_UNSUPPORTED);
80 static efi_status_t EFIAPI efi_cin_lock_std_in(
81 struct efi_console_control_protocol *this,
84 EFI_ENTRY("%p, %p", this, password);
85 return EFI_EXIT(EFI_UNSUPPORTED);
88 struct efi_console_control_protocol efi_console_control = {
89 .get_mode = efi_cin_get_mode,
90 .set_mode = efi_cin_set_mode,
91 .lock_std_in = efi_cin_lock_std_in,
94 /* Default to mode 0 */
95 static struct simple_text_output_mode efi_con_mode = {
104 static int term_read_reply(int *n, int maxnum, char end_char)
125 } else if (c == end_char) {
127 } else if (c > '9' || c < '0') {
131 /* Read one more decimal position */
139 static efi_status_t EFIAPI efi_cout_reset(
140 struct efi_simple_text_output_protocol *this,
141 char extended_verification)
143 EFI_ENTRY("%p, %d", this, extended_verification);
144 return EFI_EXIT(EFI_UNSUPPORTED);
147 static efi_status_t EFIAPI efi_cout_output_string(
148 struct efi_simple_text_output_protocol *this,
149 const efi_string_t string)
151 struct simple_text_output_mode *con = &efi_con_mode;
152 struct cout_mode *mode = &efi_cout_modes[con->mode];
154 EFI_ENTRY("%p, %p", this, string);
156 unsigned int n16 = utf16_strlen(string);
157 char buf[MAX_UTF8_PER_UTF16 * n16 + 1];
160 *utf16_to_utf8((u8 *)buf, string, n16) = '\0';
164 for (p = buf; *p; p++) {
166 case '\r': /* carriage-return */
167 con->cursor_column = 0;
169 case '\n': /* newline */
170 con->cursor_column = 0;
173 case '\t': /* tab, assume 8 char align */
175 case '\b': /* backspace */
176 con->cursor_column = max(0, con->cursor_column - 1);
179 con->cursor_column++;
182 if (con->cursor_column >= mode->columns) {
183 con->cursor_column = 0;
186 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
189 return EFI_EXIT(EFI_SUCCESS);
192 static efi_status_t EFIAPI efi_cout_test_string(
193 struct efi_simple_text_output_protocol *this,
194 const efi_string_t string)
196 EFI_ENTRY("%p, %p", this, string);
197 return EFI_EXIT(EFI_SUCCESS);
200 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
205 return (mode->rows == rows) && (mode->columns == cols);
208 static int query_console_serial(int *rows, int *cols)
210 /* Ask the terminal about its size */
214 /* Empty input buffer */
220 /* Check if we have a terminal that understands */
221 timeout = timer_get_us() + 1000000;
223 if (timer_get_us() > timeout)
226 /* Read {depth,rows,cols} */
227 if (term_read_reply(n, 3, 't'))
236 static efi_status_t EFIAPI efi_cout_query_mode(
237 struct efi_simple_text_output_protocol *this,
238 unsigned long mode_number, unsigned long *columns,
241 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
243 if (!console_size_queried) {
244 const char *stdout_name = env_get("stdout");
247 console_size_queried = true;
249 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
250 IS_ENABLED(CONFIG_DM_VIDEO)) {
251 struct stdio_dev *stdout_dev =
252 stdio_get_by_name("vidconsole");
253 struct udevice *dev = stdout_dev->priv;
254 struct vidconsole_priv *priv =
255 dev_get_uclass_priv(dev);
258 } else if (query_console_serial(&rows, &cols)) {
262 /* Test if we can have Mode 1 */
263 if (cols >= 80 && rows >= 50) {
264 efi_cout_modes[1].present = 1;
265 efi_con_mode.max_mode = 2;
269 * Install our mode as mode 2 if it is different
270 * than mode 0 or 1 and set it as the currently selected mode
272 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
273 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
274 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
275 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
276 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
277 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
278 efi_con_mode.mode = EFI_COUT_MODE_2;
282 if (mode_number >= efi_con_mode.max_mode)
283 return EFI_EXIT(EFI_UNSUPPORTED);
285 if (efi_cout_modes[mode_number].present != 1)
286 return EFI_EXIT(EFI_UNSUPPORTED);
290 *columns = efi_cout_modes[mode_number].columns;
292 *rows = efi_cout_modes[mode_number].rows;
294 return EFI_EXIT(EFI_SUCCESS);
297 static efi_status_t EFIAPI efi_cout_set_mode(
298 struct efi_simple_text_output_protocol *this,
299 unsigned long mode_number)
301 EFI_ENTRY("%p, %ld", this, mode_number);
304 if (mode_number > efi_con_mode.max_mode)
305 return EFI_EXIT(EFI_UNSUPPORTED);
307 efi_con_mode.mode = mode_number;
308 efi_con_mode.cursor_column = 0;
309 efi_con_mode.cursor_row = 0;
311 return EFI_EXIT(EFI_SUCCESS);
314 static const struct {
318 { 30, 40 }, /* 0: black */
319 { 34, 44 }, /* 1: blue */
320 { 32, 42 }, /* 2: green */
321 { 36, 46 }, /* 3: cyan */
322 { 31, 41 }, /* 4: red */
323 { 35, 45 }, /* 5: magenta */
324 { 33, 43 }, /* 6: brown, map to yellow as edk2 does*/
325 { 37, 47 }, /* 7: light grey, map to white */
328 /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
329 static efi_status_t EFIAPI efi_cout_set_attribute(
330 struct efi_simple_text_output_protocol *this,
331 unsigned long attribute)
333 unsigned int bold = EFI_ATTR_BOLD(attribute);
334 unsigned int fg = EFI_ATTR_FG(attribute);
335 unsigned int bg = EFI_ATTR_BG(attribute);
337 EFI_ENTRY("%p, %lx", this, attribute);
340 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
342 printf(ESC"[0;37;40m");
344 return EFI_EXIT(EFI_SUCCESS);
347 static efi_status_t EFIAPI efi_cout_clear_screen(
348 struct efi_simple_text_output_protocol *this)
350 EFI_ENTRY("%p", this);
354 return EFI_EXIT(EFI_SUCCESS);
357 static efi_status_t EFIAPI efi_cout_set_cursor_position(
358 struct efi_simple_text_output_protocol *this,
359 unsigned long column, unsigned long row)
361 EFI_ENTRY("%p, %ld, %ld", this, column, row);
363 printf(ESC"[%d;%df", (int)row, (int)column);
364 efi_con_mode.cursor_column = column;
365 efi_con_mode.cursor_row = row;
367 return EFI_EXIT(EFI_SUCCESS);
370 static efi_status_t EFIAPI efi_cout_enable_cursor(
371 struct efi_simple_text_output_protocol *this,
374 EFI_ENTRY("%p, %d", this, enable);
376 printf(ESC"[?25%c", enable ? 'h' : 'l');
378 return EFI_EXIT(EFI_SUCCESS);
381 struct efi_simple_text_output_protocol efi_con_out = {
382 .reset = efi_cout_reset,
383 .output_string = efi_cout_output_string,
384 .test_string = efi_cout_test_string,
385 .query_mode = efi_cout_query_mode,
386 .set_mode = efi_cout_set_mode,
387 .set_attribute = efi_cout_set_attribute,
388 .clear_screen = efi_cout_clear_screen,
389 .set_cursor_position = efi_cout_set_cursor_position,
390 .enable_cursor = efi_cout_enable_cursor,
391 .mode = (void*)&efi_con_mode,
394 static efi_status_t EFIAPI efi_cin_reset(
395 struct efi_simple_input_interface *this,
396 bool extended_verification)
398 EFI_ENTRY("%p, %d", this, extended_verification);
399 return EFI_EXIT(EFI_UNSUPPORTED);
402 static efi_status_t EFIAPI efi_cin_read_key_stroke(
403 struct efi_simple_input_interface *this,
404 struct efi_input_key *key)
406 struct efi_input_key pressed_key = {
412 EFI_ENTRY("%p, %p", this, key);
414 /* We don't do interrupts, so check for timers cooperatively */
419 return EFI_EXIT(EFI_NOT_READY);
424 /* Escape Sequence */
428 pressed_key.scan_code = 23;
430 case 'O': /* F1 - F4 */
431 pressed_key.scan_code = getc() - 'P' + 11;
439 case 'A'...'D': /* up, down right, left */
440 pressed_key.scan_code = ch - 'A' + 1;
443 pressed_key.scan_code = 6;
446 pressed_key.scan_code = 5;
448 case '1': /* F5 - F8 */
449 pressed_key.scan_code = getc() - '0' + 11;
452 case '2': /* F9 - F12 */
453 pressed_key.scan_code = getc() - '0' + 19;
457 pressed_key.scan_code = 8;
463 } else if (ch == 0x7f) {
467 pressed_key.unicode_char = ch;
470 return EFI_EXIT(EFI_SUCCESS);
473 struct efi_simple_input_interface efi_con_in = {
474 .reset = efi_cin_reset,
475 .read_key_stroke = efi_cin_read_key_stroke,
476 .wait_for_key = NULL,
479 static struct efi_event *console_timer_event;
481 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
485 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
488 EFI_ENTRY("%p, %p", event, context);
490 efi_con_in.wait_for_key->is_signaled = true;
491 efi_signal_event(efi_con_in.wait_for_key);
493 EFI_EXIT(EFI_SUCCESS);
497 /* This gets called from do_bootefi_exec(). */
498 int efi_console_register(void)
501 struct efi_object *efi_console_control_obj;
502 struct efi_object *efi_console_output_obj;
503 struct efi_object *efi_console_input_obj;
506 r = efi_create_handle((void **)&efi_console_control_obj);
507 if (r != EFI_SUCCESS)
509 r = efi_add_protocol(efi_console_control_obj->handle,
510 &efi_guid_console_control, &efi_console_control);
511 if (r != EFI_SUCCESS)
513 r = efi_create_handle((void **)&efi_console_output_obj);
514 if (r != EFI_SUCCESS)
516 r = efi_add_protocol(efi_console_output_obj->handle,
517 &efi_guid_text_output_protocol, &efi_con_out);
518 if (r != EFI_SUCCESS)
520 r = efi_create_handle((void **)&efi_console_input_obj);
521 if (r != EFI_SUCCESS)
523 r = efi_add_protocol(efi_console_input_obj->handle,
524 &efi_guid_text_input_protocol, &efi_con_in);
525 if (r != EFI_SUCCESS)
528 /* Create console events */
529 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
530 efi_key_notify, NULL, &efi_con_in.wait_for_key);
531 if (r != EFI_SUCCESS) {
532 printf("ERROR: Failed to register WaitForKey event\n");
535 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
536 efi_console_timer_notify, NULL,
537 &console_timer_event);
538 if (r != EFI_SUCCESS) {
539 printf("ERROR: Failed to register console event\n");
542 /* 5000 ns cycle is sufficient for 2 MBaud */
543 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
544 if (r != EFI_SUCCESS)
545 printf("ERROR: Failed to set console timer\n");
548 printf("ERROR: Out of meemory\n");