blob: b487288785347a5c03c9e7d8d9a15d0a2b5a7085 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafc1311ad2016-03-04 01:10:00 +01002/*
3 * EFI application console interface
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafc1311ad2016-03-04 01:10:00 +01006 */
7
8#include <common.h>
Rob Clark78178bb2017-09-09 06:47:40 -04009#include <charset.h>
Rob Clarka18c5a82017-09-13 18:05:43 -040010#include <dm/device.h>
Alexander Grafc1311ad2016-03-04 01:10:00 +010011#include <efi_loader.h>
Rob Clarka18c5a82017-09-13 18:05:43 -040012#include <stdio_dev.h>
13#include <video_console.h>
Alexander Grafc1311ad2016-03-04 01:10:00 +010014
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +010015#define EFI_COUT_MODE_2 2
16#define EFI_MAX_COUT_MODE 3
17
18struct cout_mode {
19 unsigned long columns;
20 unsigned long rows;
21 int present;
22};
23
24static struct cout_mode efi_cout_modes[] = {
25 /* EFI Mode 0 is 80x25 and always present */
26 {
27 .columns = 80,
28 .rows = 25,
29 .present = 1,
30 },
31 /* EFI Mode 1 is always 80x50 */
32 {
33 .columns = 80,
34 .rows = 50,
35 .present = 0,
36 },
37 /* Value are unknown until we query the console */
38 {
39 .columns = 0,
40 .rows = 0,
41 .present = 0,
42 },
43};
44
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +020045const efi_guid_t efi_guid_text_output_protocol =
46 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
47const efi_guid_t efi_guid_text_input_protocol =
48 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
Alexander Grafc1311ad2016-03-04 01:10:00 +010049
50#define cESC '\x1b'
51#define ESC "\x1b"
52
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +010053/* Default to mode 0 */
Alexander Grafc1311ad2016-03-04 01:10:00 +010054static struct simple_text_output_mode efi_con_mode = {
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +010055 .max_mode = 1,
Alexander Grafc1311ad2016-03-04 01:10:00 +010056 .mode = 0,
57 .attribute = 0,
58 .cursor_column = 0,
59 .cursor_row = 0,
60 .cursor_visible = 1,
61};
62
Heinrich Schuchardt62217292018-05-16 18:17:38 +020063/*
64 * Receive and parse a reply from the terminal.
65 *
66 * @n: array of return values
67 * @num: number of return values expected
68 * @end_char: character indicating end of terminal message
69 * @return: non-zero indicates error
70 */
71static int term_read_reply(int *n, int num, char end_char)
Alexander Grafc1311ad2016-03-04 01:10:00 +010072{
73 char c;
74 int i = 0;
75
76 c = getc();
77 if (c != cESC)
78 return -1;
79 c = getc();
80 if (c != '[')
81 return -1;
82
83 n[0] = 0;
84 while (1) {
85 c = getc();
86 if (c == ';') {
87 i++;
Heinrich Schuchardt62217292018-05-16 18:17:38 +020088 if (i >= num)
Alexander Grafc1311ad2016-03-04 01:10:00 +010089 return -1;
90 n[i] = 0;
91 continue;
92 } else if (c == end_char) {
93 break;
94 } else if (c > '9' || c < '0') {
95 return -1;
96 }
97
98 /* Read one more decimal position */
99 n[i] *= 10;
100 n[i] += c - '0';
101 }
Heinrich Schuchardt62217292018-05-16 18:17:38 +0200102 if (i != num - 1)
103 return -1;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100104
105 return 0;
106}
107
Alexander Grafc1311ad2016-03-04 01:10:00 +0100108static efi_status_t EFIAPI efi_cout_output_string(
109 struct efi_simple_text_output_protocol *this,
Rob Clark3a45bc72017-09-13 18:05:44 -0400110 const efi_string_t string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100111{
Rob Clark3a45bc72017-09-13 18:05:44 -0400112 struct simple_text_output_mode *con = &efi_con_mode;
113 struct cout_mode *mode = &efi_cout_modes[con->mode];
Alexander Grafc1311ad2016-03-04 01:10:00 +0100114
115 EFI_ENTRY("%p, %p", this, string);
Rob Clark3a45bc72017-09-13 18:05:44 -0400116
117 unsigned int n16 = utf16_strlen(string);
118 char buf[MAX_UTF8_PER_UTF16 * n16 + 1];
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200119 u16 *p;
Rob Clark3a45bc72017-09-13 18:05:44 -0400120
121 *utf16_to_utf8((u8 *)buf, string, n16) = '\0';
122
123 fputs(stdout, buf);
124
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200125 /*
126 * Update the cursor position.
127 *
128 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
129 * and U000D. All other characters, including control characters
130 * U+0007 (bel) and U+0009 (tab), have to increase the column by one.
131 */
132 for (p = string; *p; ++p) {
Rob Clark3a45bc72017-09-13 18:05:44 -0400133 switch (*p) {
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200134 case '\b': /* U+0008, backspace */
135 con->cursor_column = max(0, con->cursor_column - 1);
Rob Clark3a45bc72017-09-13 18:05:44 -0400136 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200137 case '\n': /* U+000A, newline */
Rob Clark3a45bc72017-09-13 18:05:44 -0400138 con->cursor_column = 0;
139 con->cursor_row++;
140 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200141 case '\r': /* U+000D, carriage-return */
142 con->cursor_column = 0;
Rob Clark3a45bc72017-09-13 18:05:44 -0400143 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200144 case 0xd800 ... 0xdbff:
145 /*
146 * Ignore high surrogates, we do not want to count a
147 * Unicode character twice.
148 */
Rob Clark3a45bc72017-09-13 18:05:44 -0400149 break;
150 default:
151 con->cursor_column++;
152 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100153 }
Rob Clark3a45bc72017-09-13 18:05:44 -0400154 if (con->cursor_column >= mode->columns) {
155 con->cursor_column = 0;
156 con->cursor_row++;
157 }
158 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100159 }
160
161 return EFI_EXIT(EFI_SUCCESS);
162}
163
164static efi_status_t EFIAPI efi_cout_test_string(
165 struct efi_simple_text_output_protocol *this,
Rob Clark3a45bc72017-09-13 18:05:44 -0400166 const efi_string_t string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100167{
168 EFI_ENTRY("%p, %p", this, string);
169 return EFI_EXIT(EFI_SUCCESS);
170}
171
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100172static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
173{
174 if (!mode->present)
175 return false;
176
177 return (mode->rows == rows) && (mode->columns == cols);
178}
179
Rob Clark71cc25c2017-09-13 18:05:42 -0400180static int query_console_serial(int *rows, int *cols)
181{
182 /* Ask the terminal about its size */
183 int n[3];
184 u64 timeout;
185
186 /* Empty input buffer */
187 while (tstc())
188 getc();
189
190 printf(ESC"[18t");
191
192 /* Check if we have a terminal that understands */
193 timeout = timer_get_us() + 1000000;
194 while (!tstc())
195 if (timer_get_us() > timeout)
196 return -1;
197
198 /* Read {depth,rows,cols} */
199 if (term_read_reply(n, 3, 't'))
200 return -1;
201
202 *cols = n[2];
203 *rows = n[1];
204
205 return 0;
206}
207
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200208/*
209 * Update the mode table.
210 *
211 * By default the only mode available is 80x25. If the console has at least 50
212 * lines, enable mode 80x50. If we can query the console size and it is neither
213 * 80x25 nor 80x50, set it as an additional mode.
214 */
215static void query_console_size(void)
216{
217 const char *stdout_name = env_get("stdout");
Alexander Graf80483b22018-06-03 15:51:17 +0200218 int rows = 25, cols = 80;
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200219
220 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
221 IS_ENABLED(CONFIG_DM_VIDEO)) {
222 struct stdio_dev *stdout_dev =
223 stdio_get_by_name("vidconsole");
224 struct udevice *dev = stdout_dev->priv;
225 struct vidconsole_priv *priv =
226 dev_get_uclass_priv(dev);
227 rows = priv->rows;
228 cols = priv->cols;
229 } else if (query_console_serial(&rows, &cols)) {
230 return;
231 }
232
233 /* Test if we can have Mode 1 */
234 if (cols >= 80 && rows >= 50) {
235 efi_cout_modes[1].present = 1;
236 efi_con_mode.max_mode = 2;
237 }
238
239 /*
240 * Install our mode as mode 2 if it is different
241 * than mode 0 or 1 and set it as the currently selected mode
242 */
243 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
244 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
245 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
246 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
247 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
248 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
249 efi_con_mode.mode = EFI_COUT_MODE_2;
250 }
251}
252
Alexander Grafc1311ad2016-03-04 01:10:00 +0100253static efi_status_t EFIAPI efi_cout_query_mode(
254 struct efi_simple_text_output_protocol *this,
255 unsigned long mode_number, unsigned long *columns,
256 unsigned long *rows)
257{
258 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
259
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100260 if (mode_number >= efi_con_mode.max_mode)
261 return EFI_EXIT(EFI_UNSUPPORTED);
262
263 if (efi_cout_modes[mode_number].present != 1)
264 return EFI_EXIT(EFI_UNSUPPORTED);
265
Alexander Grafc1311ad2016-03-04 01:10:00 +0100266 if (columns)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100267 *columns = efi_cout_modes[mode_number].columns;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100268 if (rows)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100269 *rows = efi_cout_modes[mode_number].rows;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100270
271 return EFI_EXIT(EFI_SUCCESS);
272}
273
274static efi_status_t EFIAPI efi_cout_set_mode(
275 struct efi_simple_text_output_protocol *this,
276 unsigned long mode_number)
277{
278 EFI_ENTRY("%p, %ld", this, mode_number);
279
Alexander Grafc1311ad2016-03-04 01:10:00 +0100280
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100281 if (mode_number > efi_con_mode.max_mode)
282 return EFI_EXIT(EFI_UNSUPPORTED);
283
284 efi_con_mode.mode = mode_number;
285 efi_con_mode.cursor_column = 0;
286 efi_con_mode.cursor_row = 0;
287
288 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100289}
290
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400291static const struct {
292 unsigned int fg;
293 unsigned int bg;
294} color[] = {
295 { 30, 40 }, /* 0: black */
296 { 34, 44 }, /* 1: blue */
297 { 32, 42 }, /* 2: green */
298 { 36, 46 }, /* 3: cyan */
299 { 31, 41 }, /* 4: red */
300 { 35, 45 }, /* 5: magenta */
301 { 33, 43 }, /* 6: brown, map to yellow as edk2 does*/
302 { 37, 47 }, /* 7: light grey, map to white */
303};
304
305/* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100306static efi_status_t EFIAPI efi_cout_set_attribute(
307 struct efi_simple_text_output_protocol *this,
308 unsigned long attribute)
309{
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400310 unsigned int bold = EFI_ATTR_BOLD(attribute);
311 unsigned int fg = EFI_ATTR_FG(attribute);
312 unsigned int bg = EFI_ATTR_BG(attribute);
313
Alexander Grafc1311ad2016-03-04 01:10:00 +0100314 EFI_ENTRY("%p, %lx", this, attribute);
315
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400316 if (attribute)
317 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
318 else
319 printf(ESC"[0;37;40m");
320
321 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100322}
323
324static efi_status_t EFIAPI efi_cout_clear_screen(
325 struct efi_simple_text_output_protocol *this)
326{
327 EFI_ENTRY("%p", this);
328
329 printf(ESC"[2J");
Heinrich Schuchardte67ff942018-07-05 08:18:00 +0200330 efi_con_mode.cursor_column = 0;
331 efi_con_mode.cursor_row = 0;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100332
333 return EFI_EXIT(EFI_SUCCESS);
334}
335
Heinrich Schuchardt9d12daf2018-07-05 19:58:07 +0200336static efi_status_t EFIAPI efi_cout_reset(
337 struct efi_simple_text_output_protocol *this,
338 char extended_verification)
339{
340 EFI_ENTRY("%p, %d", this, extended_verification);
341
342 /* Clear screen */
343 EFI_CALL(efi_cout_clear_screen(this));
344 /* Set default colors */
345 printf(ESC "[0;37;40m");
346
347 return EFI_EXIT(EFI_SUCCESS);
348}
349
Alexander Grafc1311ad2016-03-04 01:10:00 +0100350static efi_status_t EFIAPI efi_cout_set_cursor_position(
351 struct efi_simple_text_output_protocol *this,
352 unsigned long column, unsigned long row)
353{
354 EFI_ENTRY("%p, %ld, %ld", this, column, row);
355
356 printf(ESC"[%d;%df", (int)row, (int)column);
357 efi_con_mode.cursor_column = column;
358 efi_con_mode.cursor_row = row;
359
360 return EFI_EXIT(EFI_SUCCESS);
361}
362
363static efi_status_t EFIAPI efi_cout_enable_cursor(
364 struct efi_simple_text_output_protocol *this,
365 bool enable)
366{
367 EFI_ENTRY("%p, %d", this, enable);
368
369 printf(ESC"[?25%c", enable ? 'h' : 'l');
370
371 return EFI_EXIT(EFI_SUCCESS);
372}
373
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200374struct efi_simple_text_output_protocol efi_con_out = {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100375 .reset = efi_cout_reset,
376 .output_string = efi_cout_output_string,
377 .test_string = efi_cout_test_string,
378 .query_mode = efi_cout_query_mode,
379 .set_mode = efi_cout_set_mode,
380 .set_attribute = efi_cout_set_attribute,
381 .clear_screen = efi_cout_clear_screen,
382 .set_cursor_position = efi_cout_set_cursor_position,
383 .enable_cursor = efi_cout_enable_cursor,
384 .mode = (void*)&efi_con_mode,
385};
386
387static efi_status_t EFIAPI efi_cin_reset(
388 struct efi_simple_input_interface *this,
389 bool extended_verification)
390{
391 EFI_ENTRY("%p, %d", this, extended_verification);
Heinrich Schuchardt4f187892018-07-05 08:17:59 +0200392
393 /* Empty input buffer */
394 while (tstc())
395 getc();
396
397 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100398}
399
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100400/*
401 * Analyze modifiers (shift, alt, ctrl) for function keys.
402 * This gets called when we have already parsed CSI.
403 *
404 * @modifiers: bitmask (shift, alt, ctrl)
405 * @return: the unmodified code
406 */
407static char skip_modifiers(int *modifiers)
408{
409 char c, mod = 0, ret = 0;
410
411 c = getc();
412
413 if (c != ';') {
414 ret = c;
415 if (c == '~')
416 goto out;
417 c = getc();
418 }
419 for (;;) {
420 switch (c) {
421 case '0'...'9':
422 mod *= 10;
423 mod += c - '0';
424 /* fall through */
425 case ';':
426 c = getc();
427 break;
428 default:
429 goto out;
430 }
431 }
432out:
433 if (mod)
434 --mod;
435 if (modifiers)
436 *modifiers = mod;
437 if (!ret)
438 ret = c;
439 return ret;
440}
441
Alexander Grafc1311ad2016-03-04 01:10:00 +0100442static efi_status_t EFIAPI efi_cin_read_key_stroke(
443 struct efi_simple_input_interface *this,
444 struct efi_input_key *key)
445{
446 struct efi_input_key pressed_key = {
447 .scan_code = 0,
448 .unicode_char = 0,
449 };
450 char ch;
451
452 EFI_ENTRY("%p, %p", this, key);
453
454 /* We don't do interrupts, so check for timers cooperatively */
455 efi_timer_check();
456
457 if (!tstc()) {
458 /* No key pressed */
459 return EFI_EXIT(EFI_NOT_READY);
460 }
461
462 ch = getc();
463 if (ch == cESC) {
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100464 /*
465 * Xterm Control Sequences
466 * https://www.xfree86.org/4.8.0/ctlseqs.html
467 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100468 ch = getc();
469 switch (ch) {
470 case cESC: /* ESC */
471 pressed_key.scan_code = 23;
472 break;
473 case 'O': /* F1 - F4 */
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100474 ch = getc();
475 /* skip modifiers */
476 if (ch <= '9')
477 ch = getc();
478 pressed_key.scan_code = ch - 'P' + 11;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100479 break;
480 case 'a'...'z':
481 ch = ch - 'a';
482 break;
483 case '[':
484 ch = getc();
485 switch (ch) {
486 case 'A'...'D': /* up, down right, left */
487 pressed_key.scan_code = ch - 'A' + 1;
488 break;
489 case 'F': /* End */
490 pressed_key.scan_code = 6;
491 break;
492 case 'H': /* Home */
493 pressed_key.scan_code = 5;
494 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100495 case '1':
496 ch = skip_modifiers(NULL);
497 switch (ch) {
498 case '1'...'5': /* F1 - F5 */
499 pressed_key.scan_code = ch - '1' + 11;
500 break;
501 case '7'...'9': /* F6 - F8 */
502 pressed_key.scan_code = ch - '7' + 16;
503 break;
504 case 'A'...'D': /* up, down right, left */
505 pressed_key.scan_code = ch - 'A' + 1;
506 break;
507 case 'F':
508 pressed_key.scan_code = 6; /* End */
509 break;
510 case 'H':
511 pressed_key.scan_code = 5; /* Home */
512 break;
513 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100514 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100515 case '2':
516 ch = skip_modifiers(NULL);
517 switch (ch) {
518 case '0'...'1': /* F9 - F10 */
519 pressed_key.scan_code = ch - '0' + 19;
520 break;
521 case '3'...'4': /* F11 - F12 */
522 pressed_key.scan_code = ch - '3' + 21;
523 break;
524 case '~': /* INS */
525 pressed_key.scan_code = 7;
526 break;
527 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100528 break;
529 case '3': /* DEL */
530 pressed_key.scan_code = 8;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100531 skip_modifiers(NULL);
532 break;
533 case '5': /* PG UP */
534 pressed_key.scan_code = 9;
535 skip_modifiers(NULL);
536 break;
537 case '6': /* PG DOWN */
538 pressed_key.scan_code = 10;
539 skip_modifiers(NULL);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100540 break;
541 }
542 break;
543 }
544 } else if (ch == 0x7f) {
545 /* Backspace */
546 ch = 0x08;
547 }
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100548 if (!pressed_key.scan_code)
549 pressed_key.unicode_char = ch;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100550 *key = pressed_key;
551
552 return EFI_EXIT(EFI_SUCCESS);
553}
554
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200555struct efi_simple_input_interface efi_con_in = {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100556 .reset = efi_cin_reset,
557 .read_key_stroke = efi_cin_read_key_stroke,
558 .wait_for_key = NULL,
559};
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200560
561static struct efi_event *console_timer_event;
562
xypron.glpk@gmx.deff925932017-07-20 05:26:07 +0200563static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200564{
565}
566
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100567/*
568 * Notification function of the console timer event.
569 *
570 * event: console timer event
571 * context: not used
572 */
xypron.glpk@gmx.deff925932017-07-20 05:26:07 +0200573static void EFIAPI efi_console_timer_notify(struct efi_event *event,
574 void *context)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200575{
576 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100577
578 /* Check if input is available */
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200579 if (tstc()) {
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100580 /* Queue the wait for key event */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200581 efi_con_in.wait_for_key->is_signaled = true;
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100582 efi_signal_event(efi_con_in.wait_for_key, true);
583 }
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200584 EFI_EXIT(EFI_SUCCESS);
585}
586
587/* This gets called from do_bootefi_exec(). */
588int efi_console_register(void)
589{
590 efi_status_t r;
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200591 struct efi_object *efi_console_output_obj;
592 struct efi_object *efi_console_input_obj;
Rob Clarka17e62c2017-07-24 10:39:01 -0400593
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200594 /* Set up mode information */
595 query_console_size();
596
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200597 /* Create handles */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100598 r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200599 if (r != EFI_SUCCESS)
600 goto out_of_memory;
601 r = efi_add_protocol(efi_console_output_obj->handle,
602 &efi_guid_text_output_protocol, &efi_con_out);
603 if (r != EFI_SUCCESS)
604 goto out_of_memory;
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100605 r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200606 if (r != EFI_SUCCESS)
607 goto out_of_memory;
608 r = efi_add_protocol(efi_console_input_obj->handle,
609 &efi_guid_text_input_protocol, &efi_con_in);
610 if (r != EFI_SUCCESS)
611 goto out_of_memory;
Rob Clarka17e62c2017-07-24 10:39:01 -0400612
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200613 /* Create console events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100614 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
615 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200616 if (r != EFI_SUCCESS) {
617 printf("ERROR: Failed to register WaitForKey event\n");
618 return r;
619 }
620 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100621 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200622 &console_timer_event);
623 if (r != EFI_SUCCESS) {
624 printf("ERROR: Failed to register console event\n");
625 return r;
626 }
627 /* 5000 ns cycle is sufficient for 2 MBaud */
628 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
629 if (r != EFI_SUCCESS)
630 printf("ERROR: Failed to set console timer\n");
631 return r;
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200632out_of_memory:
633 printf("ERROR: Out of meemory\n");
634 return r;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200635}