2 * EFI application console interface
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <efi_loader.h>
13 static bool console_size_queried;
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_console_control = CONSOLE_CONTROL_GUID;
50 static efi_status_t EFIAPI efi_cin_get_mode(
51 struct efi_console_control_protocol *this,
52 int *mode, char *uga_exists, char *std_in_locked)
54 EFI_ENTRY("%p, %p, %p, %p", this, mode, uga_exists, std_in_locked);
57 *mode = EFI_CONSOLE_MODE_TEXT;
63 return EFI_EXIT(EFI_SUCCESS);
66 static efi_status_t EFIAPI efi_cin_set_mode(
67 struct efi_console_control_protocol *this, int mode)
69 EFI_ENTRY("%p, %d", this, mode);
70 return EFI_EXIT(EFI_UNSUPPORTED);
73 static efi_status_t EFIAPI efi_cin_lock_std_in(
74 struct efi_console_control_protocol *this,
77 EFI_ENTRY("%p, %p", this, password);
78 return EFI_EXIT(EFI_UNSUPPORTED);
81 const struct efi_console_control_protocol efi_console_control = {
82 .get_mode = efi_cin_get_mode,
83 .set_mode = efi_cin_set_mode,
84 .lock_std_in = efi_cin_lock_std_in,
87 /* Default to mode 0 */
88 static struct simple_text_output_mode efi_con_mode = {
97 static int term_read_reply(int *n, int maxnum, char end_char)
118 } else if (c == end_char) {
120 } else if (c > '9' || c < '0') {
124 /* Read one more decimal position */
132 static efi_status_t EFIAPI efi_cout_reset(
133 struct efi_simple_text_output_protocol *this,
134 char extended_verification)
136 EFI_ENTRY("%p, %d", this, extended_verification);
137 return EFI_EXIT(EFI_UNSUPPORTED);
140 static void print_unicode_in_utf8(u16 c)
142 char utf8[MAX_UTF8_PER_UTF16] = { 0 };
143 utf16_to_utf8((u8 *)utf8, &c, 1);
147 static efi_status_t EFIAPI efi_cout_output_string(
148 struct efi_simple_text_output_protocol *this,
149 const unsigned short *string)
151 struct cout_mode *mode;
154 mode = &efi_cout_modes[efi_con_mode.mode];
155 EFI_ENTRY("%p, %p", this, string);
156 for (;(ch = *string); string++) {
157 print_unicode_in_utf8(ch);
158 efi_con_mode.cursor_column++;
160 efi_con_mode.cursor_column = 1;
161 efi_con_mode.cursor_row++;
162 } else if (efi_con_mode.cursor_column > mode->columns) {
163 efi_con_mode.cursor_column = 1;
164 efi_con_mode.cursor_row++;
166 if (efi_con_mode.cursor_row > mode->rows)
167 efi_con_mode.cursor_row = mode->rows;
170 return EFI_EXIT(EFI_SUCCESS);
173 static efi_status_t EFIAPI efi_cout_test_string(
174 struct efi_simple_text_output_protocol *this,
175 const unsigned short *string)
177 EFI_ENTRY("%p, %p", this, string);
178 return EFI_EXIT(EFI_SUCCESS);
181 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
186 return (mode->rows == rows) && (mode->columns == cols);
189 static efi_status_t EFIAPI efi_cout_query_mode(
190 struct efi_simple_text_output_protocol *this,
191 unsigned long mode_number, unsigned long *columns,
194 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
196 if (!console_size_queried) {
197 /* Ask the terminal about its size */
203 console_size_queried = true;
205 /* Empty input buffer */
211 /* Check if we have a terminal that understands */
212 timeout = timer_get_us() + 1000000;
214 if (timer_get_us() > timeout)
217 /* Read {depth,rows,cols} */
218 if (term_read_reply(n, 3, 't')) {
225 /* Test if we can have Mode 1 */
226 if (cols >= 80 && rows >= 50) {
227 efi_cout_modes[1].present = 1;
228 efi_con_mode.max_mode = 2;
232 * Install our mode as mode 2 if it is different
233 * than mode 0 or 1 and set it as the currently selected mode
235 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
236 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
237 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
238 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
239 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
240 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
241 efi_con_mode.mode = EFI_COUT_MODE_2;
245 if (mode_number >= efi_con_mode.max_mode)
246 return EFI_EXIT(EFI_UNSUPPORTED);
248 if (efi_cout_modes[mode_number].present != 1)
249 return EFI_EXIT(EFI_UNSUPPORTED);
253 *columns = efi_cout_modes[mode_number].columns;
255 *rows = efi_cout_modes[mode_number].rows;
257 return EFI_EXIT(EFI_SUCCESS);
260 static efi_status_t EFIAPI efi_cout_set_mode(
261 struct efi_simple_text_output_protocol *this,
262 unsigned long mode_number)
264 EFI_ENTRY("%p, %ld", this, mode_number);
267 if (mode_number > efi_con_mode.max_mode)
268 return EFI_EXIT(EFI_UNSUPPORTED);
270 efi_con_mode.mode = mode_number;
271 efi_con_mode.cursor_column = 0;
272 efi_con_mode.cursor_row = 0;
274 return EFI_EXIT(EFI_SUCCESS);
277 static efi_status_t EFIAPI efi_cout_set_attribute(
278 struct efi_simple_text_output_protocol *this,
279 unsigned long attribute)
281 EFI_ENTRY("%p, %lx", this, attribute);
283 /* Just ignore attributes (colors) for now */
284 return EFI_EXIT(EFI_UNSUPPORTED);
287 static efi_status_t EFIAPI efi_cout_clear_screen(
288 struct efi_simple_text_output_protocol *this)
290 EFI_ENTRY("%p", this);
294 return EFI_EXIT(EFI_SUCCESS);
297 static efi_status_t EFIAPI efi_cout_set_cursor_position(
298 struct efi_simple_text_output_protocol *this,
299 unsigned long column, unsigned long row)
301 EFI_ENTRY("%p, %ld, %ld", this, column, row);
303 printf(ESC"[%d;%df", (int)row, (int)column);
304 efi_con_mode.cursor_column = column;
305 efi_con_mode.cursor_row = row;
307 return EFI_EXIT(EFI_SUCCESS);
310 static efi_status_t EFIAPI efi_cout_enable_cursor(
311 struct efi_simple_text_output_protocol *this,
314 EFI_ENTRY("%p, %d", this, enable);
316 printf(ESC"[?25%c", enable ? 'h' : 'l');
318 return EFI_EXIT(EFI_SUCCESS);
321 const struct efi_simple_text_output_protocol efi_con_out = {
322 .reset = efi_cout_reset,
323 .output_string = efi_cout_output_string,
324 .test_string = efi_cout_test_string,
325 .query_mode = efi_cout_query_mode,
326 .set_mode = efi_cout_set_mode,
327 .set_attribute = efi_cout_set_attribute,
328 .clear_screen = efi_cout_clear_screen,
329 .set_cursor_position = efi_cout_set_cursor_position,
330 .enable_cursor = efi_cout_enable_cursor,
331 .mode = (void*)&efi_con_mode,
334 static efi_status_t EFIAPI efi_cin_reset(
335 struct efi_simple_input_interface *this,
336 bool extended_verification)
338 EFI_ENTRY("%p, %d", this, extended_verification);
339 return EFI_EXIT(EFI_UNSUPPORTED);
342 static efi_status_t EFIAPI efi_cin_read_key_stroke(
343 struct efi_simple_input_interface *this,
344 struct efi_input_key *key)
346 struct efi_input_key pressed_key = {
352 EFI_ENTRY("%p, %p", this, key);
354 /* We don't do interrupts, so check for timers cooperatively */
359 return EFI_EXIT(EFI_NOT_READY);
364 /* Escape Sequence */
368 pressed_key.scan_code = 23;
370 case 'O': /* F1 - F4 */
371 pressed_key.scan_code = getc() - 'P' + 11;
379 case 'A'...'D': /* up, down right, left */
380 pressed_key.scan_code = ch - 'A' + 1;
383 pressed_key.scan_code = 6;
386 pressed_key.scan_code = 5;
388 case '1': /* F5 - F8 */
389 pressed_key.scan_code = getc() - '0' + 11;
392 case '2': /* F9 - F12 */
393 pressed_key.scan_code = getc() - '0' + 19;
397 pressed_key.scan_code = 8;
403 } else if (ch == 0x7f) {
407 pressed_key.unicode_char = ch;
410 return EFI_EXIT(EFI_SUCCESS);
413 struct efi_simple_input_interface efi_con_in = {
414 .reset = efi_cin_reset,
415 .read_key_stroke = efi_cin_read_key_stroke,
416 .wait_for_key = NULL,
419 static struct efi_event *console_timer_event;
421 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
425 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
428 EFI_ENTRY("%p, %p", event, context);
430 efi_signal_event(efi_con_in.wait_for_key);
431 EFI_EXIT(EFI_SUCCESS);
435 static struct efi_object efi_console_control_obj =
436 EFI_PROTOCOL_OBJECT(efi_guid_console_control, &efi_console_control);
437 static struct efi_object efi_console_output_obj =
438 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, &efi_con_out);
439 static struct efi_object efi_console_input_obj =
440 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, &efi_con_in);
442 /* This gets called from do_bootefi_exec(). */
443 int efi_console_register(void)
447 /* Hook up to the device list */
448 list_add_tail(&efi_console_control_obj.link, &efi_obj_list);
449 list_add_tail(&efi_console_output_obj.link, &efi_obj_list);
450 list_add_tail(&efi_console_input_obj.link, &efi_obj_list);
452 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
453 efi_key_notify, NULL, &efi_con_in.wait_for_key);
454 if (r != EFI_SUCCESS) {
455 printf("ERROR: Failed to register WaitForKey event\n");
458 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
459 efi_console_timer_notify, NULL,
460 &console_timer_event);
461 if (r != EFI_SUCCESS) {
462 printf("ERROR: Failed to register console event\n");
465 /* 5000 ns cycle is sufficient for 2 MBaud */
466 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
467 if (r != EFI_SUCCESS)
468 printf("ERROR: Failed to set console timer\n");