blob: d596c5824133f8bb4ee65b8942744341bf5ea9e6 [file] [log] [blame]
Alexander Graf4aacacc2016-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 Clark0d138cf2017-09-09 06:47:40 -040010#include <charset.h>
Rob Clark3863b712017-09-13 18:05:43 -040011#include <dm/device.h>
Alexander Graf4aacacc2016-03-04 01:10:00 +010012#include <efi_loader.h>
Rob Clark3863b712017-09-13 18:05:43 -040013#include <stdio_dev.h>
14#include <video_console.h>
Alexander Graf4aacacc2016-03-04 01:10:00 +010015
Alexander Graf4aacacc2016-03-04 01:10:00 +010016static bool console_size_queried;
17
Emmanuel Vadota074d5d2016-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 Graf4aacacc2016-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 Vadota074d5d2016-11-08 06:03:29 +010090/* Default to mode 0 */
Alexander Graf4aacacc2016-03-04 01:10:00 +010091static struct simple_text_output_mode efi_con_mode = {
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010092 .max_mode = 1,
Alexander Graf4aacacc2016-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
143static void print_unicode_in_utf8(u16 c)
144{
Rob Clark0d138cf2017-09-09 06:47:40 -0400145 char utf8[MAX_UTF8_PER_UTF16] = { 0 };
146 utf16_to_utf8((u8 *)utf8, &c, 1);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100147 puts(utf8);
148}
149
150static efi_status_t EFIAPI efi_cout_output_string(
151 struct efi_simple_text_output_protocol *this,
152 const unsigned short *string)
153{
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100154 struct cout_mode *mode;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100155 u16 ch;
156
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100157 mode = &efi_cout_modes[efi_con_mode.mode];
Alexander Graf4aacacc2016-03-04 01:10:00 +0100158 EFI_ENTRY("%p, %p", this, string);
159 for (;(ch = *string); string++) {
160 print_unicode_in_utf8(ch);
161 efi_con_mode.cursor_column++;
162 if (ch == '\n') {
163 efi_con_mode.cursor_column = 1;
164 efi_con_mode.cursor_row++;
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100165 } else if (efi_con_mode.cursor_column > mode->columns) {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100166 efi_con_mode.cursor_column = 1;
167 efi_con_mode.cursor_row++;
168 }
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100169 if (efi_con_mode.cursor_row > mode->rows)
170 efi_con_mode.cursor_row = mode->rows;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100171 }
172
173 return EFI_EXIT(EFI_SUCCESS);
174}
175
176static efi_status_t EFIAPI efi_cout_test_string(
177 struct efi_simple_text_output_protocol *this,
178 const unsigned short *string)
179{
180 EFI_ENTRY("%p, %p", this, string);
181 return EFI_EXIT(EFI_SUCCESS);
182}
183
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100184static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
185{
186 if (!mode->present)
187 return false;
188
189 return (mode->rows == rows) && (mode->columns == cols);
190}
191
Rob Clark1e948922017-09-13 18:05:42 -0400192static int query_console_serial(int *rows, int *cols)
193{
194 /* Ask the terminal about its size */
195 int n[3];
196 u64 timeout;
197
198 /* Empty input buffer */
199 while (tstc())
200 getc();
201
202 printf(ESC"[18t");
203
204 /* Check if we have a terminal that understands */
205 timeout = timer_get_us() + 1000000;
206 while (!tstc())
207 if (timer_get_us() > timeout)
208 return -1;
209
210 /* Read {depth,rows,cols} */
211 if (term_read_reply(n, 3, 't'))
212 return -1;
213
214 *cols = n[2];
215 *rows = n[1];
216
217 return 0;
218}
219
Alexander Graf4aacacc2016-03-04 01:10:00 +0100220static efi_status_t EFIAPI efi_cout_query_mode(
221 struct efi_simple_text_output_protocol *this,
222 unsigned long mode_number, unsigned long *columns,
223 unsigned long *rows)
224{
225 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
226
227 if (!console_size_queried) {
Rob Clark3863b712017-09-13 18:05:43 -0400228 const char *stdout_name = env_get("stdout");
Rob Clark1e948922017-09-13 18:05:42 -0400229 int rows, cols;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100230
231 console_size_queried = true;
232
Rob Clark3863b712017-09-13 18:05:43 -0400233 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);
240 rows = priv->rows;
241 cols = priv->cols;
242 } else if (query_console_serial(&rows, &cols)) {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100243 goto out;
Rob Clark3863b712017-09-13 18:05:43 -0400244 }
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100245
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;
250 }
251
252 /*
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
255 */
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;
263 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100264 }
265
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100266 if (mode_number >= efi_con_mode.max_mode)
267 return EFI_EXIT(EFI_UNSUPPORTED);
268
269 if (efi_cout_modes[mode_number].present != 1)
270 return EFI_EXIT(EFI_UNSUPPORTED);
271
Alexander Graf4aacacc2016-03-04 01:10:00 +0100272out:
273 if (columns)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100274 *columns = efi_cout_modes[mode_number].columns;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100275 if (rows)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100276 *rows = efi_cout_modes[mode_number].rows;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100277
278 return EFI_EXIT(EFI_SUCCESS);
279}
280
281static efi_status_t EFIAPI efi_cout_set_mode(
282 struct efi_simple_text_output_protocol *this,
283 unsigned long mode_number)
284{
285 EFI_ENTRY("%p, %ld", this, mode_number);
286
Alexander Graf4aacacc2016-03-04 01:10:00 +0100287
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100288 if (mode_number > efi_con_mode.max_mode)
289 return EFI_EXIT(EFI_UNSUPPORTED);
290
291 efi_con_mode.mode = mode_number;
292 efi_con_mode.cursor_column = 0;
293 efi_con_mode.cursor_row = 0;
294
295 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100296}
297
298static efi_status_t EFIAPI efi_cout_set_attribute(
299 struct efi_simple_text_output_protocol *this,
300 unsigned long attribute)
301{
302 EFI_ENTRY("%p, %lx", this, attribute);
303
304 /* Just ignore attributes (colors) for now */
305 return EFI_EXIT(EFI_UNSUPPORTED);
306}
307
308static efi_status_t EFIAPI efi_cout_clear_screen(
309 struct efi_simple_text_output_protocol *this)
310{
311 EFI_ENTRY("%p", this);
312
313 printf(ESC"[2J");
314
315 return EFI_EXIT(EFI_SUCCESS);
316}
317
318static efi_status_t EFIAPI efi_cout_set_cursor_position(
319 struct efi_simple_text_output_protocol *this,
320 unsigned long column, unsigned long row)
321{
322 EFI_ENTRY("%p, %ld, %ld", this, column, row);
323
324 printf(ESC"[%d;%df", (int)row, (int)column);
325 efi_con_mode.cursor_column = column;
326 efi_con_mode.cursor_row = row;
327
328 return EFI_EXIT(EFI_SUCCESS);
329}
330
331static efi_status_t EFIAPI efi_cout_enable_cursor(
332 struct efi_simple_text_output_protocol *this,
333 bool enable)
334{
335 EFI_ENTRY("%p, %d", this, enable);
336
337 printf(ESC"[?25%c", enable ? 'h' : 'l');
338
339 return EFI_EXIT(EFI_SUCCESS);
340}
341
342const 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,
353};
354
355static efi_status_t EFIAPI efi_cin_reset(
356 struct efi_simple_input_interface *this,
357 bool extended_verification)
358{
359 EFI_ENTRY("%p, %d", this, extended_verification);
360 return EFI_EXIT(EFI_UNSUPPORTED);
361}
362
363static efi_status_t EFIAPI efi_cin_read_key_stroke(
364 struct efi_simple_input_interface *this,
365 struct efi_input_key *key)
366{
367 struct efi_input_key pressed_key = {
368 .scan_code = 0,
369 .unicode_char = 0,
370 };
371 char ch;
372
373 EFI_ENTRY("%p, %p", this, key);
374
375 /* We don't do interrupts, so check for timers cooperatively */
376 efi_timer_check();
377
378 if (!tstc()) {
379 /* No key pressed */
380 return EFI_EXIT(EFI_NOT_READY);
381 }
382
383 ch = getc();
384 if (ch == cESC) {
385 /* Escape Sequence */
386 ch = getc();
387 switch (ch) {
388 case cESC: /* ESC */
389 pressed_key.scan_code = 23;
390 break;
391 case 'O': /* F1 - F4 */
392 pressed_key.scan_code = getc() - 'P' + 11;
393 break;
394 case 'a'...'z':
395 ch = ch - 'a';
396 break;
397 case '[':
398 ch = getc();
399 switch (ch) {
400 case 'A'...'D': /* up, down right, left */
401 pressed_key.scan_code = ch - 'A' + 1;
402 break;
403 case 'F': /* End */
404 pressed_key.scan_code = 6;
405 break;
406 case 'H': /* Home */
407 pressed_key.scan_code = 5;
408 break;
409 case '1': /* F5 - F8 */
410 pressed_key.scan_code = getc() - '0' + 11;
411 getc();
412 break;
413 case '2': /* F9 - F12 */
414 pressed_key.scan_code = getc() - '0' + 19;
415 getc();
416 break;
417 case '3': /* DEL */
418 pressed_key.scan_code = 8;
419 getc();
420 break;
421 }
422 break;
423 }
424 } else if (ch == 0x7f) {
425 /* Backspace */
426 ch = 0x08;
427 }
428 pressed_key.unicode_char = ch;
429 *key = pressed_key;
430
431 return EFI_EXIT(EFI_SUCCESS);
432}
433
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200434struct efi_simple_input_interface efi_con_in = {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100435 .reset = efi_cin_reset,
436 .read_key_stroke = efi_cin_read_key_stroke,
437 .wait_for_key = NULL,
438};
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200439
440static struct efi_event *console_timer_event;
441
xypron.glpk@gmx.de0ce42dc2017-07-20 05:26:07 +0200442static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200443{
444}
445
xypron.glpk@gmx.de0ce42dc2017-07-20 05:26:07 +0200446static void EFIAPI efi_console_timer_notify(struct efi_event *event,
447 void *context)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200448{
449 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200450 if (tstc()) {
451 efi_con_in.wait_for_key->signaled = 1;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200452 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200453 }
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200454 EFI_EXIT(EFI_SUCCESS);
455}
456
Rob Clark49f7b4b2017-07-24 10:39:01 -0400457
458static struct efi_object efi_console_control_obj =
459 EFI_PROTOCOL_OBJECT(efi_guid_console_control, &efi_console_control);
460static struct efi_object efi_console_output_obj =
461 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, &efi_con_out);
462static struct efi_object efi_console_input_obj =
463 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, &efi_con_in);
464
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200465/* This gets called from do_bootefi_exec(). */
466int efi_console_register(void)
467{
468 efi_status_t r;
Rob Clark49f7b4b2017-07-24 10:39:01 -0400469
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);
474
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200475 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");
479 return r;
480 }
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");
486 return r;
487 }
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");
492 return r;
493}