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 const struct {
314 { 30, 40 }, /* 0: black */
315 { 34, 44 }, /* 1: blue */
316 { 32, 42 }, /* 2: green */
317 { 36, 46 }, /* 3: cyan */
318 { 31, 41 }, /* 4: red */
319 { 35, 45 }, /* 5: magenta */
320 { 33, 43 }, /* 6: brown, map to yellow as edk2 does*/
321 { 37, 47 }, /* 7: light grey, map to white */
324 /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
325 static efi_status_t EFIAPI
efi_cout_set_attribute(
326 struct efi_simple_text_output_protocol
*this,
327 unsigned long attribute
)
329 unsigned int bold
= EFI_ATTR_BOLD(attribute
);
330 unsigned int fg
= EFI_ATTR_FG(attribute
);
331 unsigned int bg
= EFI_ATTR_BG(attribute
);
333 EFI_ENTRY("%p, %lx", this, attribute
);
336 printf(ESC
"[%u;%u;%um", bold
, color
[fg
].fg
, color
[bg
].bg
);
338 printf(ESC
"[0;37;40m");
340 return EFI_EXIT(EFI_SUCCESS
);
343 static efi_status_t EFIAPI
efi_cout_clear_screen(
344 struct efi_simple_text_output_protocol
*this)
346 EFI_ENTRY("%p", this);
350 return EFI_EXIT(EFI_SUCCESS
);
353 static efi_status_t EFIAPI
efi_cout_set_cursor_position(
354 struct efi_simple_text_output_protocol
*this,
355 unsigned long column
, unsigned long row
)
357 EFI_ENTRY("%p, %ld, %ld", this, column
, row
);
359 printf(ESC
"[%d;%df", (int)row
, (int)column
);
360 efi_con_mode
.cursor_column
= column
;
361 efi_con_mode
.cursor_row
= row
;
363 return EFI_EXIT(EFI_SUCCESS
);
366 static efi_status_t EFIAPI
efi_cout_enable_cursor(
367 struct efi_simple_text_output_protocol
*this,
370 EFI_ENTRY("%p, %d", this, enable
);
372 printf(ESC
"[?25%c", enable
? 'h' : 'l');
374 return EFI_EXIT(EFI_SUCCESS
);
377 const struct efi_simple_text_output_protocol efi_con_out
= {
378 .reset
= efi_cout_reset
,
379 .output_string
= efi_cout_output_string
,
380 .test_string
= efi_cout_test_string
,
381 .query_mode
= efi_cout_query_mode
,
382 .set_mode
= efi_cout_set_mode
,
383 .set_attribute
= efi_cout_set_attribute
,
384 .clear_screen
= efi_cout_clear_screen
,
385 .set_cursor_position
= efi_cout_set_cursor_position
,
386 .enable_cursor
= efi_cout_enable_cursor
,
387 .mode
= (void*)&efi_con_mode
,
390 static efi_status_t EFIAPI
efi_cin_reset(
391 struct efi_simple_input_interface
*this,
392 bool extended_verification
)
394 EFI_ENTRY("%p, %d", this, extended_verification
);
395 return EFI_EXIT(EFI_UNSUPPORTED
);
398 static efi_status_t EFIAPI
efi_cin_read_key_stroke(
399 struct efi_simple_input_interface
*this,
400 struct efi_input_key
*key
)
402 struct efi_input_key pressed_key
= {
408 EFI_ENTRY("%p, %p", this, key
);
410 /* We don't do interrupts, so check for timers cooperatively */
415 return EFI_EXIT(EFI_NOT_READY
);
420 /* Escape Sequence */
424 pressed_key
.scan_code
= 23;
426 case 'O': /* F1 - F4 */
427 pressed_key
.scan_code
= getc() - 'P' + 11;
435 case 'A'...'D': /* up, down right, left */
436 pressed_key
.scan_code
= ch
- 'A' + 1;
439 pressed_key
.scan_code
= 6;
442 pressed_key
.scan_code
= 5;
444 case '1': /* F5 - F8 */
445 pressed_key
.scan_code
= getc() - '0' + 11;
448 case '2': /* F9 - F12 */
449 pressed_key
.scan_code
= getc() - '0' + 19;
453 pressed_key
.scan_code
= 8;
459 } else if (ch
== 0x7f) {
463 pressed_key
.unicode_char
= ch
;
466 return EFI_EXIT(EFI_SUCCESS
);
469 struct efi_simple_input_interface efi_con_in
= {
470 .reset
= efi_cin_reset
,
471 .read_key_stroke
= efi_cin_read_key_stroke
,
472 .wait_for_key
= NULL
,
475 static struct efi_event
*console_timer_event
;
477 static void EFIAPI
efi_key_notify(struct efi_event
*event
, void *context
)
481 static void EFIAPI
efi_console_timer_notify(struct efi_event
*event
,
484 EFI_ENTRY("%p, %p", event
, context
);
486 efi_con_in
.wait_for_key
->is_signaled
= true;
487 efi_signal_event(efi_con_in
.wait_for_key
);
489 EFI_EXIT(EFI_SUCCESS
);
493 static struct efi_object efi_console_control_obj
=
494 EFI_PROTOCOL_OBJECT(efi_guid_console_control
, &efi_console_control
);
495 static struct efi_object efi_console_output_obj
=
496 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID
, &efi_con_out
);
497 static struct efi_object efi_console_input_obj
=
498 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID
, &efi_con_in
);
500 /* This gets called from do_bootefi_exec(). */
501 int efi_console_register(void)
505 /* Hook up to the device list */
506 list_add_tail(&efi_console_control_obj
.link
, &efi_obj_list
);
507 list_add_tail(&efi_console_output_obj
.link
, &efi_obj_list
);
508 list_add_tail(&efi_console_input_obj
.link
, &efi_obj_list
);
510 r
= efi_create_event(EVT_NOTIFY_WAIT
, TPL_CALLBACK
,
511 efi_key_notify
, NULL
, &efi_con_in
.wait_for_key
);
512 if (r
!= EFI_SUCCESS
) {
513 printf("ERROR: Failed to register WaitForKey event\n");
516 r
= efi_create_event(EVT_TIMER
| EVT_NOTIFY_SIGNAL
, TPL_CALLBACK
,
517 efi_console_timer_notify
, NULL
,
518 &console_timer_event
);
519 if (r
!= EFI_SUCCESS
) {
520 printf("ERROR: Failed to register console event\n");
523 /* 5000 ns cycle is sufficient for 2 MBaud */
524 r
= efi_set_timer(console_timer_event
, EFI_TIMER_PERIODIC
, 50);
525 if (r
!= EFI_SUCCESS
)
526 printf("ERROR: Failed to set console timer\n");