2 * EFI application console interface
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
10 #include <efi_loader.h>
12 static bool console_size_queried;
14 #define EFI_COUT_MODE_2 2
15 #define EFI_MAX_COUT_MODE 3
18 unsigned long columns;
23 static struct cout_mode efi_cout_modes[] = {
24 /* EFI Mode 0 is 80x25 and always present */
30 /* EFI Mode 1 is always 80x50 */
36 /* Value are unknown until we query the console */
44 const efi_guid_t efi_guid_console_control = CONSOLE_CONTROL_GUID;
49 static efi_status_t EFIAPI efi_cin_get_mode(
50 struct efi_console_control_protocol *this,
51 int *mode, char *uga_exists, char *std_in_locked)
53 EFI_ENTRY("%p, %p, %p, %p", this, mode, uga_exists, std_in_locked);
56 *mode = EFI_CONSOLE_MODE_TEXT;
62 return EFI_EXIT(EFI_SUCCESS);
65 static efi_status_t EFIAPI efi_cin_set_mode(
66 struct efi_console_control_protocol *this, int mode)
68 EFI_ENTRY("%p, %d", this, mode);
69 return EFI_EXIT(EFI_UNSUPPORTED);
72 static efi_status_t EFIAPI efi_cin_lock_std_in(
73 struct efi_console_control_protocol *this,
76 EFI_ENTRY("%p, %p", this, password);
77 return EFI_EXIT(EFI_UNSUPPORTED);
80 const struct efi_console_control_protocol efi_console_control = {
81 .get_mode = efi_cin_get_mode,
82 .set_mode = efi_cin_set_mode,
83 .lock_std_in = efi_cin_lock_std_in,
86 /* Default to mode 0 */
87 static struct simple_text_output_mode efi_con_mode = {
96 static int term_read_reply(int *n, int maxnum, char end_char)
117 } else if (c == end_char) {
119 } else if (c > '9' || c < '0') {
123 /* Read one more decimal position */
131 static efi_status_t EFIAPI efi_cout_reset(
132 struct efi_simple_text_output_protocol *this,
133 char extended_verification)
135 EFI_ENTRY("%p, %d", this, extended_verification);
136 return EFI_EXIT(EFI_UNSUPPORTED);
139 static void print_unicode_in_utf8(u16 c)
141 char utf8[4] = { 0 };
146 } else if (c < 0x800) {
147 *(b++) = 192 + c / 64;
148 *(b++) = 128 + c % 64;
150 *(b++) = 224 + c / 4096;
151 *(b++) = 128 + c / 64 % 64;
152 *(b++) = 128 + c % 64;
158 static efi_status_t EFIAPI efi_cout_output_string(
159 struct efi_simple_text_output_protocol *this,
160 const unsigned short *string)
162 struct cout_mode *mode;
165 mode = &efi_cout_modes[efi_con_mode.mode];
166 EFI_ENTRY("%p, %p", this, string);
167 for (;(ch = *string); string++) {
168 print_unicode_in_utf8(ch);
169 efi_con_mode.cursor_column++;
171 efi_con_mode.cursor_column = 1;
172 efi_con_mode.cursor_row++;
173 } else if (efi_con_mode.cursor_column > mode->columns) {
174 efi_con_mode.cursor_column = 1;
175 efi_con_mode.cursor_row++;
177 if (efi_con_mode.cursor_row > mode->rows)
178 efi_con_mode.cursor_row = mode->rows;
181 return EFI_EXIT(EFI_SUCCESS);
184 static efi_status_t EFIAPI efi_cout_test_string(
185 struct efi_simple_text_output_protocol *this,
186 const unsigned short *string)
188 EFI_ENTRY("%p, %p", this, string);
189 return EFI_EXIT(EFI_SUCCESS);
192 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
197 return (mode->rows == rows) && (mode->columns == cols);
200 static efi_status_t EFIAPI efi_cout_query_mode(
201 struct efi_simple_text_output_protocol *this,
202 unsigned long mode_number, unsigned long *columns,
205 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
207 if (!console_size_queried) {
208 /* Ask the terminal about its size */
214 console_size_queried = true;
216 /* Empty input buffer */
222 /* Check if we have a terminal that understands */
223 timeout = timer_get_us() + 1000000;
225 if (timer_get_us() > timeout)
228 /* Read {depth,rows,cols} */
229 if (term_read_reply(n, 3, 't')) {
236 /* Test if we can have Mode 1 */
237 if (cols >= 80 && rows >= 50) {
238 efi_cout_modes[1].present = 1;
239 efi_con_mode.max_mode = 2;
243 * Install our mode as mode 2 if it is different
244 * than mode 0 or 1 and set it as the currently selected mode
246 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
247 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
248 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
249 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
250 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
251 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
252 efi_con_mode.mode = EFI_COUT_MODE_2;
256 if (mode_number >= efi_con_mode.max_mode)
257 return EFI_EXIT(EFI_UNSUPPORTED);
259 if (efi_cout_modes[mode_number].present != 1)
260 return EFI_EXIT(EFI_UNSUPPORTED);
264 *columns = efi_cout_modes[mode_number].columns;
266 *rows = efi_cout_modes[mode_number].rows;
268 return EFI_EXIT(EFI_SUCCESS);
271 static efi_status_t EFIAPI efi_cout_set_mode(
272 struct efi_simple_text_output_protocol *this,
273 unsigned long mode_number)
275 EFI_ENTRY("%p, %ld", this, mode_number);
278 if (mode_number > efi_con_mode.max_mode)
279 return EFI_EXIT(EFI_UNSUPPORTED);
281 efi_con_mode.mode = mode_number;
282 efi_con_mode.cursor_column = 0;
283 efi_con_mode.cursor_row = 0;
285 return EFI_EXIT(EFI_SUCCESS);
288 static efi_status_t EFIAPI efi_cout_set_attribute(
289 struct efi_simple_text_output_protocol *this,
290 unsigned long attribute)
292 EFI_ENTRY("%p, %lx", this, attribute);
294 /* Just ignore attributes (colors) for now */
295 return EFI_EXIT(EFI_UNSUPPORTED);
298 static efi_status_t EFIAPI efi_cout_clear_screen(
299 struct efi_simple_text_output_protocol *this)
301 EFI_ENTRY("%p", this);
305 return EFI_EXIT(EFI_SUCCESS);
308 static efi_status_t EFIAPI efi_cout_set_cursor_position(
309 struct efi_simple_text_output_protocol *this,
310 unsigned long column, unsigned long row)
312 EFI_ENTRY("%p, %ld, %ld", this, column, row);
314 printf(ESC"[%d;%df", (int)row, (int)column);
315 efi_con_mode.cursor_column = column;
316 efi_con_mode.cursor_row = row;
318 return EFI_EXIT(EFI_SUCCESS);
321 static efi_status_t EFIAPI efi_cout_enable_cursor(
322 struct efi_simple_text_output_protocol *this,
325 EFI_ENTRY("%p, %d", this, enable);
327 printf(ESC"[?25%c", enable ? 'h' : 'l');
329 return EFI_EXIT(EFI_SUCCESS);
332 const struct efi_simple_text_output_protocol efi_con_out = {
333 .reset = efi_cout_reset,
334 .output_string = efi_cout_output_string,
335 .test_string = efi_cout_test_string,
336 .query_mode = efi_cout_query_mode,
337 .set_mode = efi_cout_set_mode,
338 .set_attribute = efi_cout_set_attribute,
339 .clear_screen = efi_cout_clear_screen,
340 .set_cursor_position = efi_cout_set_cursor_position,
341 .enable_cursor = efi_cout_enable_cursor,
342 .mode = (void*)&efi_con_mode,
345 static efi_status_t EFIAPI efi_cin_reset(
346 struct efi_simple_input_interface *this,
347 bool extended_verification)
349 EFI_ENTRY("%p, %d", this, extended_verification);
350 return EFI_EXIT(EFI_UNSUPPORTED);
353 static efi_status_t EFIAPI efi_cin_read_key_stroke(
354 struct efi_simple_input_interface *this,
355 struct efi_input_key *key)
357 struct efi_input_key pressed_key = {
363 EFI_ENTRY("%p, %p", this, key);
365 /* We don't do interrupts, so check for timers cooperatively */
370 return EFI_EXIT(EFI_NOT_READY);
375 /* Escape Sequence */
379 pressed_key.scan_code = 23;
381 case 'O': /* F1 - F4 */
382 pressed_key.scan_code = getc() - 'P' + 11;
390 case 'A'...'D': /* up, down right, left */
391 pressed_key.scan_code = ch - 'A' + 1;
394 pressed_key.scan_code = 6;
397 pressed_key.scan_code = 5;
399 case '1': /* F5 - F8 */
400 pressed_key.scan_code = getc() - '0' + 11;
403 case '2': /* F9 - F12 */
404 pressed_key.scan_code = getc() - '0' + 19;
408 pressed_key.scan_code = 8;
414 } else if (ch == 0x7f) {
418 pressed_key.unicode_char = ch;
421 return EFI_EXIT(EFI_SUCCESS);
424 struct efi_simple_input_interface efi_con_in = {
425 .reset = efi_cin_reset,
426 .read_key_stroke = efi_cin_read_key_stroke,
427 .wait_for_key = NULL,
430 static struct efi_event *console_timer_event;
432 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
436 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
439 EFI_ENTRY("%p, %p", event, context);
441 efi_signal_event(efi_con_in.wait_for_key);
442 EFI_EXIT(EFI_SUCCESS);
446 static struct efi_object efi_console_control_obj =
447 EFI_PROTOCOL_OBJECT(efi_guid_console_control, &efi_console_control);
448 static struct efi_object efi_console_output_obj =
449 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, &efi_con_out);
450 static struct efi_object efi_console_input_obj =
451 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, &efi_con_in);
453 /* This gets called from do_bootefi_exec(). */
454 int efi_console_register(void)
458 /* Hook up to the device list */
459 list_add_tail(&efi_console_control_obj.link, &efi_obj_list);
460 list_add_tail(&efi_console_output_obj.link, &efi_obj_list);
461 list_add_tail(&efi_console_input_obj.link, &efi_obj_list);
463 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
464 efi_key_notify, NULL, &efi_con_in.wait_for_key);
465 if (r != EFI_SUCCESS) {
466 printf("ERROR: Failed to register WaitForKey event\n");
469 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
470 efi_console_timer_notify, NULL,
471 &console_timer_event);
472 if (r != EFI_SUCCESS) {
473 printf("ERROR: Failed to register console event\n");
476 /* 5000 ns cycle is sufficient for 2 MBaud */
477 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
478 if (r != EFI_SUCCESS)
479 printf("ERROR: Failed to set console timer\n");