blob: fd5398d61d94ff40fa83884de8c3dea26a8944c2 [file] [log] [blame]
Alexander Grafc1311ad2016-03-04 01:10:00 +01001/*
2 * EFI application console interface
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
Rob Clark78178bb2017-09-09 06:47:40 -040010#include <charset.h>
Rob Clarka18c5a82017-09-13 18:05:43 -040011#include <dm/device.h>
Alexander Grafc1311ad2016-03-04 01:10:00 +010012#include <efi_loader.h>
Rob Clarka18c5a82017-09-13 18:05:43 -040013#include <stdio_dev.h>
14#include <video_console.h>
Alexander Grafc1311ad2016-03-04 01:10:00 +010015
Alexander Grafc1311ad2016-03-04 01:10:00 +010016static bool console_size_queried;
17
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +010018#define EFI_COUT_MODE_2 2
19#define EFI_MAX_COUT_MODE 3
20
21struct cout_mode {
22 unsigned long columns;
23 unsigned long rows;
24 int present;
25};
26
27static struct cout_mode efi_cout_modes[] = {
28 /* EFI Mode 0 is 80x25 and always present */
29 {
30 .columns = 80,
31 .rows = 25,
32 .present = 1,
33 },
34 /* EFI Mode 1 is always 80x50 */
35 {
36 .columns = 80,
37 .rows = 50,
38 .present = 0,
39 },
40 /* Value are unknown until we query the console */
41 {
42 .columns = 0,
43 .rows = 0,
44 .present = 0,
45 },
46};
47
Alexander Grafc1311ad2016-03-04 01:10:00 +010048const efi_guid_t efi_guid_console_control = CONSOLE_CONTROL_GUID;
49
50#define cESC '\x1b'
51#define ESC "\x1b"
52
53static 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)
56{
57 EFI_ENTRY("%p, %p, %p, %p", this, mode, uga_exists, std_in_locked);
58
59 if (mode)
60 *mode = EFI_CONSOLE_MODE_TEXT;
61 if (uga_exists)
62 *uga_exists = 0;
63 if (std_in_locked)
64 *std_in_locked = 0;
65
66 return EFI_EXIT(EFI_SUCCESS);
67}
68
69static efi_status_t EFIAPI efi_cin_set_mode(
70 struct efi_console_control_protocol *this, int mode)
71{
72 EFI_ENTRY("%p, %d", this, mode);
73 return EFI_EXIT(EFI_UNSUPPORTED);
74}
75
76static efi_status_t EFIAPI efi_cin_lock_std_in(
77 struct efi_console_control_protocol *this,
78 uint16_t *password)
79{
80 EFI_ENTRY("%p, %p", this, password);
81 return EFI_EXIT(EFI_UNSUPPORTED);
82}
83
84const 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,
88};
89
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +010090/* Default to mode 0 */
Alexander Grafc1311ad2016-03-04 01:10:00 +010091static struct simple_text_output_mode efi_con_mode = {
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +010092 .max_mode = 1,
Alexander Grafc1311ad2016-03-04 01:10:00 +010093 .mode = 0,
94 .attribute = 0,
95 .cursor_column = 0,
96 .cursor_row = 0,
97 .cursor_visible = 1,
98};
99
100static int term_read_reply(int *n, int maxnum, char end_char)
101{
102 char c;
103 int i = 0;
104
105 c = getc();
106 if (c != cESC)
107 return -1;
108 c = getc();
109 if (c != '[')
110 return -1;
111
112 n[0] = 0;
113 while (1) {
114 c = getc();
115 if (c == ';') {
116 i++;
117 if (i >= maxnum)
118 return -1;
119 n[i] = 0;
120 continue;
121 } else if (c == end_char) {
122 break;
123 } else if (c > '9' || c < '0') {
124 return -1;
125 }
126
127 /* Read one more decimal position */
128 n[i] *= 10;
129 n[i] += c - '0';
130 }
131
132 return 0;
133}
134
135static efi_status_t EFIAPI efi_cout_reset(
136 struct efi_simple_text_output_protocol *this,
137 char extended_verification)
138{
139 EFI_ENTRY("%p, %d", this, extended_verification);
140 return EFI_EXIT(EFI_UNSUPPORTED);
141}
142
Alexander Grafc1311ad2016-03-04 01:10:00 +0100143static efi_status_t EFIAPI efi_cout_output_string(
144 struct efi_simple_text_output_protocol *this,
Rob Clark3a45bc72017-09-13 18:05:44 -0400145 const efi_string_t string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100146{
Rob Clark3a45bc72017-09-13 18:05:44 -0400147 struct simple_text_output_mode *con = &efi_con_mode;
148 struct cout_mode *mode = &efi_cout_modes[con->mode];
Alexander Grafc1311ad2016-03-04 01:10:00 +0100149
150 EFI_ENTRY("%p, %p", this, string);
Rob Clark3a45bc72017-09-13 18:05:44 -0400151
152 unsigned int n16 = utf16_strlen(string);
153 char buf[MAX_UTF8_PER_UTF16 * n16 + 1];
154 char *p;
155
156 *utf16_to_utf8((u8 *)buf, string, n16) = '\0';
157
158 fputs(stdout, buf);
159
160 for (p = buf; *p; p++) {
161 switch (*p) {
162 case '\r': /* carriage-return */
163 con->cursor_column = 0;
164 break;
165 case '\n': /* newline */
166 con->cursor_column = 0;
167 con->cursor_row++;
168 break;
169 case '\t': /* tab, assume 8 char align */
170 break;
171 case '\b': /* backspace */
172 con->cursor_column = max(0, con->cursor_column - 1);
173 break;
174 default:
175 con->cursor_column++;
176 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100177 }
Rob Clark3a45bc72017-09-13 18:05:44 -0400178 if (con->cursor_column >= mode->columns) {
179 con->cursor_column = 0;
180 con->cursor_row++;
181 }
182 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100183 }
184
185 return EFI_EXIT(EFI_SUCCESS);
186}
187
188static efi_status_t EFIAPI efi_cout_test_string(
189 struct efi_simple_text_output_protocol *this,
Rob Clark3a45bc72017-09-13 18:05:44 -0400190 const efi_string_t string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100191{
192 EFI_ENTRY("%p, %p", this, string);
193 return EFI_EXIT(EFI_SUCCESS);
194}
195
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100196static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
197{
198 if (!mode->present)
199 return false;
200
201 return (mode->rows == rows) && (mode->columns == cols);
202}
203
Rob Clark71cc25c2017-09-13 18:05:42 -0400204static int query_console_serial(int *rows, int *cols)
205{
206 /* Ask the terminal about its size */
207 int n[3];
208 u64 timeout;
209
210 /* Empty input buffer */
211 while (tstc())
212 getc();
213
214 printf(ESC"[18t");
215
216 /* Check if we have a terminal that understands */
217 timeout = timer_get_us() + 1000000;
218 while (!tstc())
219 if (timer_get_us() > timeout)
220 return -1;
221
222 /* Read {depth,rows,cols} */
223 if (term_read_reply(n, 3, 't'))
224 return -1;
225
226 *cols = n[2];
227 *rows = n[1];
228
229 return 0;
230}
231
Alexander Grafc1311ad2016-03-04 01:10:00 +0100232static efi_status_t EFIAPI efi_cout_query_mode(
233 struct efi_simple_text_output_protocol *this,
234 unsigned long mode_number, unsigned long *columns,
235 unsigned long *rows)
236{
237 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
238
239 if (!console_size_queried) {
Rob Clarka18c5a82017-09-13 18:05:43 -0400240 const char *stdout_name = env_get("stdout");
Rob Clark71cc25c2017-09-13 18:05:42 -0400241 int rows, cols;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100242
243 console_size_queried = true;
244
Rob Clarka18c5a82017-09-13 18:05:43 -0400245 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);
252 rows = priv->rows;
253 cols = priv->cols;
254 } else if (query_console_serial(&rows, &cols)) {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100255 goto out;
Rob Clarka18c5a82017-09-13 18:05:43 -0400256 }
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100257
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;
262 }
263
264 /*
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
267 */
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;
275 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100276 }
277
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100278 if (mode_number >= efi_con_mode.max_mode)
279 return EFI_EXIT(EFI_UNSUPPORTED);
280
281 if (efi_cout_modes[mode_number].present != 1)
282 return EFI_EXIT(EFI_UNSUPPORTED);
283
Alexander Grafc1311ad2016-03-04 01:10:00 +0100284out:
285 if (columns)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100286 *columns = efi_cout_modes[mode_number].columns;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100287 if (rows)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100288 *rows = efi_cout_modes[mode_number].rows;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100289
290 return EFI_EXIT(EFI_SUCCESS);
291}
292
293static efi_status_t EFIAPI efi_cout_set_mode(
294 struct efi_simple_text_output_protocol *this,
295 unsigned long mode_number)
296{
297 EFI_ENTRY("%p, %ld", this, mode_number);
298
Alexander Grafc1311ad2016-03-04 01:10:00 +0100299
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100300 if (mode_number > efi_con_mode.max_mode)
301 return EFI_EXIT(EFI_UNSUPPORTED);
302
303 efi_con_mode.mode = mode_number;
304 efi_con_mode.cursor_column = 0;
305 efi_con_mode.cursor_row = 0;
306
307 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100308}
309
310static efi_status_t EFIAPI efi_cout_set_attribute(
311 struct efi_simple_text_output_protocol *this,
312 unsigned long attribute)
313{
314 EFI_ENTRY("%p, %lx", this, attribute);
315
316 /* Just ignore attributes (colors) for now */
317 return EFI_EXIT(EFI_UNSUPPORTED);
318}
319
320static efi_status_t EFIAPI efi_cout_clear_screen(
321 struct efi_simple_text_output_protocol *this)
322{
323 EFI_ENTRY("%p", this);
324
325 printf(ESC"[2J");
326
327 return EFI_EXIT(EFI_SUCCESS);
328}
329
330static efi_status_t EFIAPI efi_cout_set_cursor_position(
331 struct efi_simple_text_output_protocol *this,
332 unsigned long column, unsigned long row)
333{
334 EFI_ENTRY("%p, %ld, %ld", this, column, row);
335
336 printf(ESC"[%d;%df", (int)row, (int)column);
337 efi_con_mode.cursor_column = column;
338 efi_con_mode.cursor_row = row;
339
340 return EFI_EXIT(EFI_SUCCESS);
341}
342
343static efi_status_t EFIAPI efi_cout_enable_cursor(
344 struct efi_simple_text_output_protocol *this,
345 bool enable)
346{
347 EFI_ENTRY("%p, %d", this, enable);
348
349 printf(ESC"[?25%c", enable ? 'h' : 'l');
350
351 return EFI_EXIT(EFI_SUCCESS);
352}
353
354const struct efi_simple_text_output_protocol efi_con_out = {
355 .reset = efi_cout_reset,
356 .output_string = efi_cout_output_string,
357 .test_string = efi_cout_test_string,
358 .query_mode = efi_cout_query_mode,
359 .set_mode = efi_cout_set_mode,
360 .set_attribute = efi_cout_set_attribute,
361 .clear_screen = efi_cout_clear_screen,
362 .set_cursor_position = efi_cout_set_cursor_position,
363 .enable_cursor = efi_cout_enable_cursor,
364 .mode = (void*)&efi_con_mode,
365};
366
367static efi_status_t EFIAPI efi_cin_reset(
368 struct efi_simple_input_interface *this,
369 bool extended_verification)
370{
371 EFI_ENTRY("%p, %d", this, extended_verification);
372 return EFI_EXIT(EFI_UNSUPPORTED);
373}
374
375static efi_status_t EFIAPI efi_cin_read_key_stroke(
376 struct efi_simple_input_interface *this,
377 struct efi_input_key *key)
378{
379 struct efi_input_key pressed_key = {
380 .scan_code = 0,
381 .unicode_char = 0,
382 };
383 char ch;
384
385 EFI_ENTRY("%p, %p", this, key);
386
387 /* We don't do interrupts, so check for timers cooperatively */
388 efi_timer_check();
389
390 if (!tstc()) {
391 /* No key pressed */
392 return EFI_EXIT(EFI_NOT_READY);
393 }
394
395 ch = getc();
396 if (ch == cESC) {
397 /* Escape Sequence */
398 ch = getc();
399 switch (ch) {
400 case cESC: /* ESC */
401 pressed_key.scan_code = 23;
402 break;
403 case 'O': /* F1 - F4 */
404 pressed_key.scan_code = getc() - 'P' + 11;
405 break;
406 case 'a'...'z':
407 ch = ch - 'a';
408 break;
409 case '[':
410 ch = getc();
411 switch (ch) {
412 case 'A'...'D': /* up, down right, left */
413 pressed_key.scan_code = ch - 'A' + 1;
414 break;
415 case 'F': /* End */
416 pressed_key.scan_code = 6;
417 break;
418 case 'H': /* Home */
419 pressed_key.scan_code = 5;
420 break;
421 case '1': /* F5 - F8 */
422 pressed_key.scan_code = getc() - '0' + 11;
423 getc();
424 break;
425 case '2': /* F9 - F12 */
426 pressed_key.scan_code = getc() - '0' + 19;
427 getc();
428 break;
429 case '3': /* DEL */
430 pressed_key.scan_code = 8;
431 getc();
432 break;
433 }
434 break;
435 }
436 } else if (ch == 0x7f) {
437 /* Backspace */
438 ch = 0x08;
439 }
440 pressed_key.unicode_char = ch;
441 *key = pressed_key;
442
443 return EFI_EXIT(EFI_SUCCESS);
444}
445
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200446struct efi_simple_input_interface efi_con_in = {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100447 .reset = efi_cin_reset,
448 .read_key_stroke = efi_cin_read_key_stroke,
449 .wait_for_key = NULL,
450};
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200451
452static struct efi_event *console_timer_event;
453
xypron.glpk@gmx.deff925932017-07-20 05:26:07 +0200454static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200455{
456}
457
xypron.glpk@gmx.deff925932017-07-20 05:26:07 +0200458static void EFIAPI efi_console_timer_notify(struct efi_event *event,
459 void *context)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200460{
461 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200462 if (tstc()) {
463 efi_con_in.wait_for_key->signaled = 1;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200464 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200465 }
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200466 EFI_EXIT(EFI_SUCCESS);
467}
468
Rob Clarka17e62c2017-07-24 10:39:01 -0400469
470static struct efi_object efi_console_control_obj =
471 EFI_PROTOCOL_OBJECT(efi_guid_console_control, &efi_console_control);
472static struct efi_object efi_console_output_obj =
473 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, &efi_con_out);
474static struct efi_object efi_console_input_obj =
475 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, &efi_con_in);
476
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200477/* This gets called from do_bootefi_exec(). */
478int efi_console_register(void)
479{
480 efi_status_t r;
Rob Clarka17e62c2017-07-24 10:39:01 -0400481
482 /* Hook up to the device list */
483 list_add_tail(&efi_console_control_obj.link, &efi_obj_list);
484 list_add_tail(&efi_console_output_obj.link, &efi_obj_list);
485 list_add_tail(&efi_console_input_obj.link, &efi_obj_list);
486
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200487 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
488 efi_key_notify, NULL, &efi_con_in.wait_for_key);
489 if (r != EFI_SUCCESS) {
490 printf("ERROR: Failed to register WaitForKey event\n");
491 return r;
492 }
493 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
494 efi_console_timer_notify, NULL,
495 &console_timer_event);
496 if (r != EFI_SUCCESS) {
497 printf("ERROR: Failed to register console event\n");
498 return r;
499 }
500 /* 5000 ns cycle is sufficient for 2 MBaud */
501 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
502 if (r != EFI_SUCCESS)
503 printf("ERROR: Failed to set console timer\n");
504 return r;
505}