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 void print_unicode_in_utf8(u16 c)
145 char utf8[MAX_UTF8_PER_UTF16] = { 0 };
146 utf16_to_utf8((u8 *)utf8, &c, 1);
150 static efi_status_t EFIAPI efi_cout_output_string(
151 struct efi_simple_text_output_protocol *this,
152 const unsigned short *string)
154 struct cout_mode *mode;
157 mode = &efi_cout_modes[efi_con_mode.mode];
158 EFI_ENTRY("%p, %p", this, string);
159 for (;(ch = *string); string++) {
160 print_unicode_in_utf8(ch);
161 efi_con_mode.cursor_column++;
163 efi_con_mode.cursor_column = 1;
164 efi_con_mode.cursor_row++;
165 } else if (efi_con_mode.cursor_column > mode->columns) {
166 efi_con_mode.cursor_column = 1;
167 efi_con_mode.cursor_row++;
169 if (efi_con_mode.cursor_row > mode->rows)
170 efi_con_mode.cursor_row = mode->rows;
173 return EFI_EXIT(EFI_SUCCESS);
176 static efi_status_t EFIAPI efi_cout_test_string(
177 struct efi_simple_text_output_protocol *this,
178 const unsigned short *string)
180 EFI_ENTRY("%p, %p", this, string);
181 return EFI_EXIT(EFI_SUCCESS);
184 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
189 return (mode->rows == rows) && (mode->columns == cols);
192 static int query_console_serial(int *rows, int *cols)
194 /* Ask the terminal about its size */
198 /* Empty input buffer */
204 /* Check if we have a terminal that understands */
205 timeout = timer_get_us() + 1000000;
207 if (timer_get_us() > timeout)
210 /* Read {depth,rows,cols} */
211 if (term_read_reply(n, 3, 't'))
220 static efi_status_t EFIAPI efi_cout_query_mode(
221 struct efi_simple_text_output_protocol *this,
222 unsigned long mode_number, unsigned long *columns,
225 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
227 if (!console_size_queried) {
228 const char *stdout_name = env_get("stdout");
231 console_size_queried = true;
233 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
234 IS_ENABLED(CONFIG_DM_VIDEO)) {
235 struct stdio_dev *stdout_dev =
236 stdio_get_by_name("vidconsole");
237 struct udevice *dev = stdout_dev->priv;
238 struct vidconsole_priv *priv =
239 dev_get_uclass_priv(dev);
242 } else if (query_console_serial(&rows, &cols)) {
246 /* Test if we can have Mode 1 */
247 if (cols >= 80 && rows >= 50) {
248 efi_cout_modes[1].present = 1;
249 efi_con_mode.max_mode = 2;
253 * Install our mode as mode 2 if it is different
254 * than mode 0 or 1 and set it as the currently selected mode
256 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
257 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
258 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
259 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
260 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
261 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
262 efi_con_mode.mode = EFI_COUT_MODE_2;
266 if (mode_number >= efi_con_mode.max_mode)
267 return EFI_EXIT(EFI_UNSUPPORTED);
269 if (efi_cout_modes[mode_number].present != 1)
270 return EFI_EXIT(EFI_UNSUPPORTED);
274 *columns = efi_cout_modes[mode_number].columns;
276 *rows = efi_cout_modes[mode_number].rows;
278 return EFI_EXIT(EFI_SUCCESS);
281 static efi_status_t EFIAPI efi_cout_set_mode(
282 struct efi_simple_text_output_protocol *this,
283 unsigned long mode_number)
285 EFI_ENTRY("%p, %ld", this, mode_number);
288 if (mode_number > efi_con_mode.max_mode)
289 return EFI_EXIT(EFI_UNSUPPORTED);
291 efi_con_mode.mode = mode_number;
292 efi_con_mode.cursor_column = 0;
293 efi_con_mode.cursor_row = 0;
295 return EFI_EXIT(EFI_SUCCESS);
298 static efi_status_t EFIAPI efi_cout_set_attribute(
299 struct efi_simple_text_output_protocol *this,
300 unsigned long attribute)
302 EFI_ENTRY("%p, %lx", this, attribute);
304 /* Just ignore attributes (colors) for now */
305 return EFI_EXIT(EFI_UNSUPPORTED);
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 const 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);
363 static efi_status_t EFIAPI efi_cin_read_key_stroke(
364 struct efi_simple_input_interface *this,
365 struct efi_input_key *key)
367 struct efi_input_key pressed_key = {
373 EFI_ENTRY("%p, %p", this, key);
375 /* We don't do interrupts, so check for timers cooperatively */
380 return EFI_EXIT(EFI_NOT_READY);
385 /* Escape Sequence */
389 pressed_key.scan_code = 23;
391 case 'O': /* F1 - F4 */
392 pressed_key.scan_code = getc() - 'P' + 11;
400 case 'A'...'D': /* up, down right, left */
401 pressed_key.scan_code = ch - 'A' + 1;
404 pressed_key.scan_code = 6;
407 pressed_key.scan_code = 5;
409 case '1': /* F5 - F8 */
410 pressed_key.scan_code = getc() - '0' + 11;
413 case '2': /* F9 - F12 */
414 pressed_key.scan_code = getc() - '0' + 19;
418 pressed_key.scan_code = 8;
424 } else if (ch == 0x7f) {
428 pressed_key.unicode_char = ch;
431 return EFI_EXIT(EFI_SUCCESS);
434 struct efi_simple_input_interface efi_con_in = {
435 .reset = efi_cin_reset,
436 .read_key_stroke = efi_cin_read_key_stroke,
437 .wait_for_key = NULL,
440 static struct efi_event *console_timer_event;
442 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
446 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
449 EFI_ENTRY("%p, %p", event, context);
451 efi_con_in.wait_for_key->signaled = 1;
452 efi_signal_event(efi_con_in.wait_for_key);
454 EFI_EXIT(EFI_SUCCESS);
458 static struct efi_object efi_console_control_obj =
459 EFI_PROTOCOL_OBJECT(efi_guid_console_control, &efi_console_control);
460 static struct efi_object efi_console_output_obj =
461 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, &efi_con_out);
462 static struct efi_object efi_console_input_obj =
463 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, &efi_con_in);
465 /* This gets called from do_bootefi_exec(). */
466 int efi_console_register(void)
470 /* Hook up to the device list */
471 list_add_tail(&efi_console_control_obj.link, &efi_obj_list);
472 list_add_tail(&efi_console_output_obj.link, &efi_obj_list);
473 list_add_tail(&efi_console_input_obj.link, &efi_obj_list);
475 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
476 efi_key_notify, NULL, &efi_con_in.wait_for_key);
477 if (r != EFI_SUCCESS) {
478 printf("ERROR: Failed to register WaitForKey event\n");
481 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
482 efi_console_timer_notify, NULL,
483 &console_timer_event);
484 if (r != EFI_SUCCESS) {
485 printf("ERROR: Failed to register console event\n");
488 /* 5000 ns cycle is sufficient for 2 MBaud */
489 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
490 if (r != EFI_SUCCESS)
491 printf("ERROR: Failed to set console timer\n");