]> git.ipfire.org Git - thirdparty/u-boot.git/blame - lib/efi_loader/efi_console.c
efi_loader: fix SetMode()
[thirdparty/u-boot.git] / lib / efi_loader / efi_console.c
CommitLineData
f739fcd8 1// SPDX-License-Identifier: GPL-2.0+
c1311ad4
AG
2/*
3 * EFI application console interface
4 *
5 * Copyright (c) 2016 Alexander Graf
c1311ad4
AG
6 */
7
8#include <common.h>
78178bb0 9#include <charset.h>
a18c5a83 10#include <dm/device.h>
c1311ad4 11#include <efi_loader.h>
a18c5a83
RC
12#include <stdio_dev.h>
13#include <video_console.h>
c1311ad4 14
5be8b0a3
EV
15#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
110c6280
HS
45const efi_guid_t efi_guid_text_input_ex_protocol =
46 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
ebb4dd5b
HS
47const efi_guid_t efi_guid_text_input_protocol =
48 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
110c6280
HS
49const efi_guid_t efi_guid_text_output_protocol =
50 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
c1311ad4
AG
51
52#define cESC '\x1b'
53#define ESC "\x1b"
54
5be8b0a3 55/* Default to mode 0 */
c1311ad4 56static struct simple_text_output_mode efi_con_mode = {
5be8b0a3 57 .max_mode = 1,
c1311ad4
AG
58 .mode = 0,
59 .attribute = 0,
60 .cursor_column = 0,
61 .cursor_row = 0,
62 .cursor_visible = 1,
63};
64
dd1a1ec2
MB
65static int term_get_char(s32 *c)
66{
67 u64 timeout;
68
69 /* Wait up to 100 ms for a character */
70 timeout = timer_get_us() + 100000;
71
72 while (!tstc())
73 if (timer_get_us() > timeout)
74 return 1;
75
76 *c = getc();
77 return 0;
78}
79
62217295
HS
80/*
81 * Receive and parse a reply from the terminal.
82 *
83 * @n: array of return values
84 * @num: number of return values expected
85 * @end_char: character indicating end of terminal message
86 * @return: non-zero indicates error
87 */
88static int term_read_reply(int *n, int num, char end_char)
c1311ad4 89{
dd1a1ec2 90 s32 c;
c1311ad4
AG
91 int i = 0;
92
dd1a1ec2 93 if (term_get_char(&c) || c != cESC)
c1311ad4 94 return -1;
dd1a1ec2
MB
95
96 if (term_get_char(&c) || c != '[')
c1311ad4
AG
97 return -1;
98
99 n[0] = 0;
100 while (1) {
dd1a1ec2
MB
101 if (!term_get_char(&c)) {
102 if (c == ';') {
103 i++;
104 if (i >= num)
105 return -1;
106 n[i] = 0;
107 continue;
108 } else if (c == end_char) {
109 break;
110 } else if (c > '9' || c < '0') {
c1311ad4 111 return -1;
dd1a1ec2
MB
112 }
113
114 /* Read one more decimal position */
115 n[i] *= 10;
116 n[i] += c - '0';
117 } else {
c1311ad4
AG
118 return -1;
119 }
c1311ad4 120 }
62217295
HS
121 if (i != num - 1)
122 return -1;
c1311ad4
AG
123
124 return 0;
125}
126
c1311ad4
AG
127static efi_status_t EFIAPI efi_cout_output_string(
128 struct efi_simple_text_output_protocol *this,
3a45bc7f 129 const efi_string_t string)
c1311ad4 130{
3a45bc7f
RC
131 struct simple_text_output_mode *con = &efi_con_mode;
132 struct cout_mode *mode = &efi_cout_modes[con->mode];
ba7bd5c2 133 char *buf, *pos;
7ca7c3c0 134 u16 *p;
ba7bd5c2 135 efi_status_t ret = EFI_SUCCESS;
3a45bc7f 136
ba7bd5c2 137 EFI_ENTRY("%p, %p", this, string);
3a45bc7f 138
b31ca6bf
HS
139 if (!this || !string) {
140 ret = EFI_INVALID_PARAMETER;
141 goto out;
142 }
143
ba7bd5c2
HS
144 buf = malloc(utf16_utf8_strlen(string) + 1);
145 if (!buf) {
146 ret = EFI_OUT_OF_RESOURCES;
147 goto out;
148 }
149 pos = buf;
150 utf16_utf8_strcpy(&pos, string);
3a45bc7f 151 fputs(stdout, buf);
ba7bd5c2 152 free(buf);
3a45bc7f 153
7ca7c3c0
HS
154 /*
155 * Update the cursor position.
156 *
157 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
158 * and U000D. All other characters, including control characters
14d103bb 159 * U+0007 (BEL) and U+0009 (TAB), have to increase the column by one.
7ca7c3c0
HS
160 */
161 for (p = string; *p; ++p) {
3a45bc7f 162 switch (*p) {
7ca7c3c0
HS
163 case '\b': /* U+0008, backspace */
164 con->cursor_column = max(0, con->cursor_column - 1);
3a45bc7f 165 break;
7ca7c3c0 166 case '\n': /* U+000A, newline */
3a45bc7f
RC
167 con->cursor_column = 0;
168 con->cursor_row++;
169 break;
7ca7c3c0
HS
170 case '\r': /* U+000D, carriage-return */
171 con->cursor_column = 0;
3a45bc7f 172 break;
7ca7c3c0
HS
173 case 0xd800 ... 0xdbff:
174 /*
175 * Ignore high surrogates, we do not want to count a
176 * Unicode character twice.
177 */
3a45bc7f
RC
178 break;
179 default:
180 con->cursor_column++;
181 break;
182 }
183 if (con->cursor_column >= mode->columns) {
184 con->cursor_column = 0;
185 con->cursor_row++;
c1311ad4 186 }
3a45bc7f 187 con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
c1311ad4
AG
188 }
189
ba7bd5c2
HS
190out:
191 return EFI_EXIT(ret);
c1311ad4
AG
192}
193
194static efi_status_t EFIAPI efi_cout_test_string(
195 struct efi_simple_text_output_protocol *this,
3a45bc7f 196 const efi_string_t string)
c1311ad4
AG
197{
198 EFI_ENTRY("%p, %p", this, string);
199 return EFI_EXIT(EFI_SUCCESS);
200}
201
5be8b0a3
EV
202static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
203{
204 if (!mode->present)
205 return false;
206
207 return (mode->rows == rows) && (mode->columns == cols);
208}
209
6bb591f7
HS
210/**
211 * query_console_serial() - query console size
212 *
213 * @rows pointer to return number of rows
214 * @columns pointer to return number of columns
215 * Returns 0 on success
216 */
71cc25c3
RC
217static int query_console_serial(int *rows, int *cols)
218{
6bb591f7
HS
219 int ret = 0;
220 int n[2];
71cc25c3
RC
221
222 /* Empty input buffer */
223 while (tstc())
224 getc();
225
6bb591f7
HS
226 /*
227 * Not all terminals understand CSI [18t for querying the console size.
228 * We should adhere to escape sequences documented in the console_codes
e1fec152 229 * man page and the ECMA-48 standard.
6bb591f7
HS
230 *
231 * So here we follow a different approach. We position the cursor to the
232 * bottom right and query its position. Before leaving the function we
233 * restore the original cursor position.
234 */
235 printf(ESC "7" /* Save cursor position */
236 ESC "[r" /* Set scrolling region to full window */
237 ESC "[999;999H" /* Move to bottom right corner */
238 ESC "[6n"); /* Query cursor position */
71cc25c3 239
6bb591f7
HS
240 /* Read {rows,cols} */
241 if (term_read_reply(n, 2, 'R')) {
242 ret = 1;
243 goto out;
244 }
71cc25c3 245
6bb591f7
HS
246 *cols = n[1];
247 *rows = n[0];
248out:
249 printf(ESC "8"); /* Restore cursor position */
250 return ret;
71cc25c3
RC
251}
252
a4aa7bef
HS
253/*
254 * Update the mode table.
255 *
256 * By default the only mode available is 80x25. If the console has at least 50
257 * lines, enable mode 80x50. If we can query the console size and it is neither
258 * 80x25 nor 80x50, set it as an additional mode.
259 */
260static void query_console_size(void)
261{
262 const char *stdout_name = env_get("stdout");
80483b2a 263 int rows = 25, cols = 80;
a4aa7bef
HS
264
265 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
266 IS_ENABLED(CONFIG_DM_VIDEO)) {
267 struct stdio_dev *stdout_dev =
268 stdio_get_by_name("vidconsole");
269 struct udevice *dev = stdout_dev->priv;
270 struct vidconsole_priv *priv =
271 dev_get_uclass_priv(dev);
272 rows = priv->rows;
273 cols = priv->cols;
274 } else if (query_console_serial(&rows, &cols)) {
275 return;
276 }
277
278 /* Test if we can have Mode 1 */
279 if (cols >= 80 && rows >= 50) {
280 efi_cout_modes[1].present = 1;
281 efi_con_mode.max_mode = 2;
282 }
283
284 /*
285 * Install our mode as mode 2 if it is different
286 * than mode 0 or 1 and set it as the currently selected mode
287 */
288 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
289 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
290 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
291 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
292 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
293 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
294 efi_con_mode.mode = EFI_COUT_MODE_2;
295 }
296}
297
c1311ad4
AG
298static efi_status_t EFIAPI efi_cout_query_mode(
299 struct efi_simple_text_output_protocol *this,
300 unsigned long mode_number, unsigned long *columns,
301 unsigned long *rows)
302{
303 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
304
5be8b0a3
EV
305 if (mode_number >= efi_con_mode.max_mode)
306 return EFI_EXIT(EFI_UNSUPPORTED);
307
308 if (efi_cout_modes[mode_number].present != 1)
309 return EFI_EXIT(EFI_UNSUPPORTED);
310
c1311ad4 311 if (columns)
5be8b0a3 312 *columns = efi_cout_modes[mode_number].columns;
c1311ad4 313 if (rows)
5be8b0a3 314 *rows = efi_cout_modes[mode_number].rows;
c1311ad4
AG
315
316 return EFI_EXIT(EFI_SUCCESS);
317}
318
2d5dc2a5
RC
319static const struct {
320 unsigned int fg;
321 unsigned int bg;
322} color[] = {
323 { 30, 40 }, /* 0: black */
324 { 34, 44 }, /* 1: blue */
325 { 32, 42 }, /* 2: green */
326 { 36, 46 }, /* 3: cyan */
327 { 31, 41 }, /* 4: red */
328 { 35, 45 }, /* 5: magenta */
14d103bb
HS
329 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
330 { 37, 47 }, /* 7: light gray, map to white */
2d5dc2a5
RC
331};
332
333/* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
c1311ad4
AG
334static efi_status_t EFIAPI efi_cout_set_attribute(
335 struct efi_simple_text_output_protocol *this,
336 unsigned long attribute)
337{
2d5dc2a5
RC
338 unsigned int bold = EFI_ATTR_BOLD(attribute);
339 unsigned int fg = EFI_ATTR_FG(attribute);
340 unsigned int bg = EFI_ATTR_BG(attribute);
341
c1311ad4
AG
342 EFI_ENTRY("%p, %lx", this, attribute);
343
2d5dc2a5
RC
344 if (attribute)
345 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
346 else
347 printf(ESC"[0;37;40m");
348
349 return EFI_EXIT(EFI_SUCCESS);
c1311ad4
AG
350}
351
352static efi_status_t EFIAPI efi_cout_clear_screen(
353 struct efi_simple_text_output_protocol *this)
354{
355 EFI_ENTRY("%p", this);
356
357 printf(ESC"[2J");
e67ff94d
HS
358 efi_con_mode.cursor_column = 0;
359 efi_con_mode.cursor_row = 0;
c1311ad4
AG
360
361 return EFI_EXIT(EFI_SUCCESS);
362}
363
2ad238fc
HS
364static efi_status_t EFIAPI efi_cout_set_mode(
365 struct efi_simple_text_output_protocol *this,
366 unsigned long mode_number)
367{
368 EFI_ENTRY("%p, %ld", this, mode_number);
369
370 if (mode_number >= efi_con_mode.max_mode)
371 return EFI_EXIT(EFI_UNSUPPORTED);
372 efi_con_mode.mode = mode_number;
373 EFI_CALL(efi_cout_clear_screen(this));
374
375 return EFI_EXIT(EFI_SUCCESS);
376}
377
9d12daff
HS
378static efi_status_t EFIAPI efi_cout_reset(
379 struct efi_simple_text_output_protocol *this,
380 char extended_verification)
381{
382 EFI_ENTRY("%p, %d", this, extended_verification);
383
384 /* Clear screen */
385 EFI_CALL(efi_cout_clear_screen(this));
386 /* Set default colors */
387 printf(ESC "[0;37;40m");
388
389 return EFI_EXIT(EFI_SUCCESS);
390}
391
c1311ad4
AG
392static efi_status_t EFIAPI efi_cout_set_cursor_position(
393 struct efi_simple_text_output_protocol *this,
394 unsigned long column, unsigned long row)
395{
aaace4b0
HS
396 efi_status_t ret = EFI_SUCCESS;
397 struct simple_text_output_mode *con = &efi_con_mode;
398 struct cout_mode *mode = &efi_cout_modes[con->mode];
399
c1311ad4
AG
400 EFI_ENTRY("%p, %ld, %ld", this, column, row);
401
aaace4b0
HS
402 /* Check parameters */
403 if (!this) {
404 ret = EFI_INVALID_PARAMETER;
405 goto out;
406 }
407 if (row >= mode->rows || column >= mode->columns) {
408 ret = EFI_UNSUPPORTED;
409 goto out;
410 }
411
412 /*
413 * Set cursor position by sending CSI H.
414 * EFI origin is [0, 0], terminal origin is [1, 1].
415 */
416 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
c1311ad4
AG
417 efi_con_mode.cursor_column = column;
418 efi_con_mode.cursor_row = row;
aaace4b0
HS
419out:
420 return EFI_EXIT(ret);
c1311ad4
AG
421}
422
423static efi_status_t EFIAPI efi_cout_enable_cursor(
424 struct efi_simple_text_output_protocol *this,
425 bool enable)
426{
427 EFI_ENTRY("%p, %d", this, enable);
428
429 printf(ESC"[?25%c", enable ? 'h' : 'l');
25e6fb5e 430 efi_con_mode.cursor_visible = !!enable;
c1311ad4
AG
431
432 return EFI_EXIT(EFI_SUCCESS);
433}
434
ebb4dd5b 435struct efi_simple_text_output_protocol efi_con_out = {
c1311ad4
AG
436 .reset = efi_cout_reset,
437 .output_string = efi_cout_output_string,
438 .test_string = efi_cout_test_string,
439 .query_mode = efi_cout_query_mode,
440 .set_mode = efi_cout_set_mode,
441 .set_attribute = efi_cout_set_attribute,
442 .clear_screen = efi_cout_clear_screen,
443 .set_cursor_position = efi_cout_set_cursor_position,
444 .enable_cursor = efi_cout_enable_cursor,
445 .mode = (void*)&efi_con_mode,
446};
447
4fdcf066
HS
448/**
449 * struct efi_cin_notify_function - registered console input notify function
450 *
451 * @link: link to list
452 * @data: key to notify
453 * @function: function to call
454 */
455struct efi_cin_notify_function {
456 struct list_head link;
457 struct efi_key_data key;
458 efi_status_t (EFIAPI *function)
459 (struct efi_key_data *key_data);
460};
461
0dfd13a4 462static bool key_available;
110c6280 463static struct efi_key_data next_key;
4fdcf066 464static LIST_HEAD(cin_notify_functions);
4f187897 465
55fbdf99
HS
466/**
467 * set_shift_mask() - set shift mask
468 *
469 * @mod: Xterm shift mask
470 */
471void set_shift_mask(int mod, struct efi_key_state *key_state)
472{
473 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
474 if (mod) {
475 --mod;
476 if (mod & 1)
477 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
478 if (mod & 2)
479 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
480 if (mod & 4)
481 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
482 if (mod & 8)
483 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
484 } else {
485 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
486 }
487}
488
0dfd13a4 489/**
110c6280 490 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
0dfd13a4 491 *
0fb4169e
HS
492 * This gets called when we have already parsed CSI.
493 *
e1fec152 494 * @modifiers: bit mask (shift, alt, ctrl)
0fb4169e
HS
495 * @return: the unmodified code
496 */
110c6280 497static int analyze_modifiers(struct efi_key_state *key_state)
0fb4169e 498{
110c6280 499 int c, mod = 0, ret = 0;
0fb4169e
HS
500
501 c = getc();
502
503 if (c != ';') {
504 ret = c;
505 if (c == '~')
506 goto out;
507 c = getc();
508 }
509 for (;;) {
510 switch (c) {
511 case '0'...'9':
512 mod *= 10;
513 mod += c - '0';
514 /* fall through */
515 case ';':
516 c = getc();
517 break;
518 default:
519 goto out;
520 }
521 }
522out:
55fbdf99 523 set_shift_mask(mod, key_state);
0fb4169e
HS
524 if (!ret)
525 ret = c;
526 return ret;
527}
528
0dfd13a4
HS
529/**
530 * efi_cin_read_key() - read a key from the console input
531 *
532 * @key: - key received
533 * Return: - status code
534 */
110c6280 535static efi_status_t efi_cin_read_key(struct efi_key_data *key)
c1311ad4
AG
536{
537 struct efi_input_key pressed_key = {
538 .scan_code = 0,
539 .unicode_char = 0,
540 };
35cbb796 541 s32 ch;
c1311ad4 542
55fbdf99 543 if (console_read_unicode(&ch))
0dfd13a4 544 return EFI_NOT_READY;
110c6280
HS
545
546 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
547 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
548
35cbb796
HS
549 /* We do not support multi-word codes */
550 if (ch >= 0x10000)
551 ch = '?';
55fbdf99
HS
552
553 switch (ch) {
554 case 0x1b:
0fb4169e
HS
555 /*
556 * Xterm Control Sequences
557 * https://www.xfree86.org/4.8.0/ctlseqs.html
558 */
c1311ad4
AG
559 ch = getc();
560 switch (ch) {
561 case cESC: /* ESC */
562 pressed_key.scan_code = 23;
563 break;
564 case 'O': /* F1 - F4 */
0fb4169e 565 ch = getc();
55fbdf99
HS
566 /* consider modifiers */
567 if (ch < 'P') {
568 set_shift_mask(ch - '0', &key->key_state);
0fb4169e 569 ch = getc();
55fbdf99 570 }
0fb4169e 571 pressed_key.scan_code = ch - 'P' + 11;
c1311ad4 572 break;
c1311ad4
AG
573 case '[':
574 ch = getc();
575 switch (ch) {
576 case 'A'...'D': /* up, down right, left */
577 pressed_key.scan_code = ch - 'A' + 1;
578 break;
579 case 'F': /* End */
580 pressed_key.scan_code = 6;
581 break;
582 case 'H': /* Home */
583 pressed_key.scan_code = 5;
584 break;
0fb4169e 585 case '1':
110c6280 586 ch = analyze_modifiers(&key->key_state);
0fb4169e
HS
587 switch (ch) {
588 case '1'...'5': /* F1 - F5 */
589 pressed_key.scan_code = ch - '1' + 11;
590 break;
591 case '7'...'9': /* F6 - F8 */
592 pressed_key.scan_code = ch - '7' + 16;
593 break;
594 case 'A'...'D': /* up, down right, left */
595 pressed_key.scan_code = ch - 'A' + 1;
596 break;
597 case 'F':
598 pressed_key.scan_code = 6; /* End */
599 break;
600 case 'H':
601 pressed_key.scan_code = 5; /* Home */
602 break;
603 }
c1311ad4 604 break;
0fb4169e 605 case '2':
110c6280 606 ch = analyze_modifiers(&key->key_state);
0fb4169e
HS
607 switch (ch) {
608 case '0'...'1': /* F9 - F10 */
609 pressed_key.scan_code = ch - '0' + 19;
610 break;
611 case '3'...'4': /* F11 - F12 */
612 pressed_key.scan_code = ch - '3' + 21;
613 break;
614 case '~': /* INS */
615 pressed_key.scan_code = 7;
616 break;
617 }
c1311ad4
AG
618 break;
619 case '3': /* DEL */
620 pressed_key.scan_code = 8;
110c6280 621 analyze_modifiers(&key->key_state);
0fb4169e
HS
622 break;
623 case '5': /* PG UP */
624 pressed_key.scan_code = 9;
110c6280 625 analyze_modifiers(&key->key_state);
0fb4169e
HS
626 break;
627 case '6': /* PG DOWN */
628 pressed_key.scan_code = 10;
110c6280 629 analyze_modifiers(&key->key_state);
c1311ad4 630 break;
55fbdf99 631 } /* [ */
c1311ad4 632 break;
55fbdf99
HS
633 default:
634 /* ALT key */
635 set_shift_mask(3, &key->key_state);
c1311ad4 636 }
55fbdf99
HS
637 break;
638 case 0x7f:
c1311ad4
AG
639 /* Backspace */
640 ch = 0x08;
641 }
110c6280
HS
642 if (pressed_key.scan_code) {
643 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
644 } else {
0fb4169e 645 pressed_key.unicode_char = ch;
110c6280
HS
646
647 /*
648 * Assume left control key for control characters typically
649 * entered using the control key.
650 */
651 if (ch >= 0x01 && ch <= 0x1f) {
55fbdf99 652 key->key_state.key_shift_state |=
110c6280
HS
653 EFI_SHIFT_STATE_VALID;
654 switch (ch) {
655 case 0x01 ... 0x07:
656 case 0x0b ... 0x0c:
657 case 0x0e ... 0x1f:
658 key->key_state.key_shift_state |=
659 EFI_LEFT_CONTROL_PRESSED;
660 }
661 }
662 }
663 key->key = pressed_key;
c1311ad4 664
0dfd13a4
HS
665 return EFI_SUCCESS;
666}
667
4fdcf066
HS
668/**
669 * efi_cin_notify() - notify registered functions
670 */
671static void efi_cin_notify(void)
672{
673 struct efi_cin_notify_function *item;
674
675 list_for_each_entry(item, &cin_notify_functions, link) {
676 bool match = true;
677
678 /* We do not support toggle states */
679 if (item->key.key.unicode_char || item->key.key.scan_code) {
680 if (item->key.key.unicode_char !=
681 next_key.key.unicode_char ||
682 item->key.key.scan_code != next_key.key.scan_code)
683 match = false;
684 }
685 if (item->key.key_state.key_shift_state &&
686 item->key.key_state.key_shift_state !=
687 next_key.key_state.key_shift_state)
688 match = false;
689
690 if (match)
691 /* We don't bother about the return code */
692 EFI_CALL(item->function(&next_key));
693 }
694}
695
0dfd13a4
HS
696/**
697 * efi_cin_check() - check if keyboard input is available
698 */
699static void efi_cin_check(void)
700{
701 efi_status_t ret;
702
703 if (key_available) {
7eaa900e 704 efi_signal_event(efi_con_in.wait_for_key);
0dfd13a4
HS
705 return;
706 }
707
708 if (tstc()) {
709 ret = efi_cin_read_key(&next_key);
710 if (ret == EFI_SUCCESS) {
711 key_available = true;
712
4fdcf066
HS
713 /* Notify registered functions */
714 efi_cin_notify();
715
0dfd13a4 716 /* Queue the wait for key event */
4fdcf066 717 if (key_available)
7eaa900e 718 efi_signal_event(efi_con_in.wait_for_key);
0dfd13a4
HS
719 }
720 }
721}
722
110c6280
HS
723/**
724 * efi_cin_empty_buffer() - empty input buffer
725 */
726static void efi_cin_empty_buffer(void)
727{
728 while (tstc())
729 getc();
730 key_available = false;
731}
732
733/**
734 * efi_cin_reset_ex() - reset console input
735 *
736 * @this: - the extended simple text input protocol
737 * @extended_verification: - extended verification
738 *
739 * This function implements the reset service of the
740 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
741 *
742 * See the Unified Extensible Firmware Interface (UEFI) specification for
743 * details.
744 *
745 * Return: old value of the task priority level
746 */
747static efi_status_t EFIAPI efi_cin_reset_ex(
748 struct efi_simple_text_input_ex_protocol *this,
749 bool extended_verification)
750{
751 efi_status_t ret = EFI_SUCCESS;
752
753 EFI_ENTRY("%p, %d", this, extended_verification);
754
755 /* Check parameters */
756 if (!this) {
757 ret = EFI_INVALID_PARAMETER;
758 goto out;
759 }
760
761 efi_cin_empty_buffer();
762out:
763 return EFI_EXIT(ret);
764}
765
766/**
767 * efi_cin_read_key_stroke_ex() - read key stroke
768 *
769 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
770 * @key_data: key read from console
771 * Return: status code
772 *
773 * This function implements the ReadKeyStrokeEx service of the
774 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
775 *
776 * See the Unified Extensible Firmware Interface (UEFI) specification for
777 * details.
778 */
779static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
780 struct efi_simple_text_input_ex_protocol *this,
781 struct efi_key_data *key_data)
782{
783 efi_status_t ret = EFI_SUCCESS;
784
785 EFI_ENTRY("%p, %p", this, key_data);
786
787 /* Check parameters */
788 if (!this || !key_data) {
789 ret = EFI_INVALID_PARAMETER;
790 goto out;
791 }
792
793 /* We don't do interrupts, so check for timers cooperatively */
794 efi_timer_check();
795
796 /* Enable console input after ExitBootServices */
797 efi_cin_check();
798
799 if (!key_available) {
800 ret = EFI_NOT_READY;
801 goto out;
802 }
bfc2dd53
HS
803 /*
804 * CTRL+A - CTRL+Z have to be signaled as a - z.
805 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
806 */
807 switch (next_key.key.unicode_char) {
808 case 0x01 ... 0x07:
809 case 0x0b ... 0x0c:
810 case 0x0e ... 0x1a:
811 if (!(next_key.key_state.key_toggle_state &
812 EFI_CAPS_LOCK_ACTIVE) ^
813 !(next_key.key_state.key_shift_state &
814 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
815 next_key.key.unicode_char += 0x40;
816 else
817 next_key.key.unicode_char += 0x60;
818 }
110c6280
HS
819 *key_data = next_key;
820 key_available = false;
821 efi_con_in.wait_for_key->is_signaled = false;
bfc2dd53 822
110c6280
HS
823out:
824 return EFI_EXIT(ret);
825}
826
827/**
828 * efi_cin_set_state() - set toggle key state
829 *
830 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
acee9652 831 * @key_toggle_state: pointer to key toggle state
110c6280
HS
832 * Return: status code
833 *
834 * This function implements the SetState service of the
835 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
836 *
837 * See the Unified Extensible Firmware Interface (UEFI) specification for
838 * details.
839 */
840static efi_status_t EFIAPI efi_cin_set_state(
841 struct efi_simple_text_input_ex_protocol *this,
acee9652 842 u8 *key_toggle_state)
110c6280 843{
acee9652 844 EFI_ENTRY("%p, %p", this, key_toggle_state);
110c6280
HS
845 /*
846 * U-Boot supports multiple console input sources like serial and
847 * net console for which a key toggle state cannot be set at all.
848 *
849 * According to the UEFI specification it is allowable to not implement
850 * this service.
851 */
852 return EFI_EXIT(EFI_UNSUPPORTED);
853}
854
855/**
856 * efi_cin_register_key_notify() - register key notification function
857 *
858 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
859 * @key_data: key to be notified
860 * @key_notify_function: function to be called if the key is pressed
861 * @notify_handle: handle for unregistering the notification
862 * Return: status code
863 *
864 * This function implements the SetState service of the
865 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
866 *
867 * See the Unified Extensible Firmware Interface (UEFI) specification for
868 * details.
869 */
870static efi_status_t EFIAPI efi_cin_register_key_notify(
871 struct efi_simple_text_input_ex_protocol *this,
872 struct efi_key_data *key_data,
873 efi_status_t (EFIAPI *key_notify_function)(
874 struct efi_key_data *key_data),
875 void **notify_handle)
876{
4fdcf066
HS
877 efi_status_t ret = EFI_SUCCESS;
878 struct efi_cin_notify_function *notify_function;
879
110c6280
HS
880 EFI_ENTRY("%p, %p, %p, %p",
881 this, key_data, key_notify_function, notify_handle);
4fdcf066
HS
882
883 /* Check parameters */
884 if (!this || !key_data || !key_notify_function || !notify_handle) {
885 ret = EFI_INVALID_PARAMETER;
886 goto out;
887 }
888
889 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
890 key_data->key.unicode_char,
891 key_data->key.scan_code,
892 key_data->key_state.key_shift_state,
893 key_data->key_state.key_toggle_state);
894
895 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
896 if (!notify_function) {
897 ret = EFI_OUT_OF_RESOURCES;
898 goto out;
899 }
900 notify_function->key = *key_data;
901 notify_function->function = key_notify_function;
902 list_add_tail(&notify_function->link, &cin_notify_functions);
903 *notify_handle = notify_function;
904out:
905 return EFI_EXIT(ret);
110c6280
HS
906}
907
908/**
909 * efi_cin_unregister_key_notify() - unregister key notification function
910 *
911 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
912 * @notification_handle: handle received when registering
913 * Return: status code
914 *
915 * This function implements the SetState service of the
916 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
917 *
918 * See the Unified Extensible Firmware Interface (UEFI) specification for
919 * details.
920 */
921static efi_status_t EFIAPI efi_cin_unregister_key_notify(
922 struct efi_simple_text_input_ex_protocol *this,
923 void *notification_handle)
924{
4fdcf066
HS
925 efi_status_t ret = EFI_INVALID_PARAMETER;
926 struct efi_cin_notify_function *item, *notify_function =
927 notification_handle;
928
110c6280 929 EFI_ENTRY("%p, %p", this, notification_handle);
4fdcf066
HS
930
931 /* Check parameters */
932 if (!this || !notification_handle)
933 goto out;
934
935 list_for_each_entry(item, &cin_notify_functions, link) {
936 if (item == notify_function) {
937 ret = EFI_SUCCESS;
938 break;
939 }
940 }
941 if (ret != EFI_SUCCESS)
942 goto out;
943
944 /* Remove the notify function */
945 list_del(&notify_function->link);
946 free(notify_function);
947out:
948 return EFI_EXIT(ret);
110c6280
HS
949}
950
951
0dfd13a4
HS
952/**
953 * efi_cin_reset() - drain the input buffer
954 *
955 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
956 * @extended_verification: allow for exhaustive verification
957 * Return: status code
958 *
959 * This function implements the Reset service of the
960 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
961 *
962 * See the Unified Extensible Firmware Interface (UEFI) specification for
963 * details.
964 */
965static efi_status_t EFIAPI efi_cin_reset
966 (struct efi_simple_text_input_protocol *this,
967 bool extended_verification)
968{
969 efi_status_t ret = EFI_SUCCESS;
970
971 EFI_ENTRY("%p, %d", this, extended_verification);
972
973 /* Check parameters */
974 if (!this) {
975 ret = EFI_INVALID_PARAMETER;
976 goto out;
977 }
978
110c6280 979 efi_cin_empty_buffer();
0dfd13a4
HS
980out:
981 return EFI_EXIT(ret);
982}
983
984/**
110c6280 985 * efi_cin_read_key_stroke() - read key stroke
0dfd13a4
HS
986 *
987 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
988 * @key: key read from console
989 * Return: status code
990 *
991 * This function implements the ReadKeyStroke service of the
992 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
993 *
994 * See the Unified Extensible Firmware Interface (UEFI) specification for
995 * details.
996 */
997static efi_status_t EFIAPI efi_cin_read_key_stroke
998 (struct efi_simple_text_input_protocol *this,
999 struct efi_input_key *key)
1000{
1001 efi_status_t ret = EFI_SUCCESS;
1002
1003 EFI_ENTRY("%p, %p", this, key);
1004
1005 /* Check parameters */
1006 if (!this || !key) {
1007 ret = EFI_INVALID_PARAMETER;
1008 goto out;
1009 }
1010
1011 /* We don't do interrupts, so check for timers cooperatively */
1012 efi_timer_check();
1013
1014 /* Enable console input after ExitBootServices */
1015 efi_cin_check();
1016
1017 if (!key_available) {
1018 ret = EFI_NOT_READY;
1019 goto out;
1020 }
110c6280 1021 *key = next_key.key;
0dfd13a4
HS
1022 key_available = false;
1023 efi_con_in.wait_for_key->is_signaled = false;
1024out:
1025 return EFI_EXIT(ret);
c1311ad4
AG
1026}
1027
110c6280
HS
1028static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1029 .reset = efi_cin_reset_ex,
1030 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1031 .wait_for_key_ex = NULL,
1032 .set_state = efi_cin_set_state,
1033 .register_key_notify = efi_cin_register_key_notify,
1034 .unregister_key_notify = efi_cin_unregister_key_notify,
1035};
1036
3e603ec7 1037struct efi_simple_text_input_protocol efi_con_in = {
c1311ad4
AG
1038 .reset = efi_cin_reset,
1039 .read_key_stroke = efi_cin_read_key_stroke,
1040 .wait_for_key = NULL,
1041};
91be9a77
HS
1042
1043static struct efi_event *console_timer_event;
1044
9bc9664d 1045/*
0dfd13a4 1046 * efi_console_timer_notify() - notify the console timer event
9bc9664d 1047 *
0dfd13a4
HS
1048 * @event: console timer event
1049 * @context: not used
9bc9664d 1050 */
ff925938
HS
1051static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1052 void *context)
91be9a77
HS
1053{
1054 EFI_ENTRY("%p, %p", event, context);
0dfd13a4
HS
1055 efi_cin_check();
1056 EFI_EXIT(EFI_SUCCESS);
1057}
9bc9664d 1058
0dfd13a4
HS
1059/**
1060 * efi_key_notify() - notify the wait for key event
1061 *
1062 * @event: wait for key event
1063 * @context: not used
1064 */
1065static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1066{
1067 EFI_ENTRY("%p, %p", event, context);
1068 efi_cin_check();
91be9a77
HS
1069 EFI_EXIT(EFI_SUCCESS);
1070}
1071
0dfd13a4
HS
1072/**
1073 * efi_console_register() - install the console protocols
1074 *
1075 * This function is called from do_bootefi_exec().
6f566c23
HS
1076 *
1077 * Return: status code
0dfd13a4 1078 */
6f566c23 1079efi_status_t efi_console_register(void)
91be9a77
HS
1080{
1081 efi_status_t r;
fae0118e
HS
1082 efi_handle_t console_output_handle;
1083 efi_handle_t console_input_handle;
a17e62cc 1084
a4aa7bef
HS
1085 /* Set up mode information */
1086 query_console_size();
1087
ebb4dd5b 1088 /* Create handles */
fae0118e 1089 r = efi_create_handle(&console_output_handle);
ebb4dd5b
HS
1090 if (r != EFI_SUCCESS)
1091 goto out_of_memory;
40e3e757 1092
fae0118e 1093 r = efi_add_protocol(console_output_handle,
ebb4dd5b
HS
1094 &efi_guid_text_output_protocol, &efi_con_out);
1095 if (r != EFI_SUCCESS)
1096 goto out_of_memory;
fae0118e
HS
1097 systab.con_out_handle = console_output_handle;
1098 systab.stderr_handle = console_output_handle;
40e3e757 1099
fae0118e 1100 r = efi_create_handle(&console_input_handle);
ebb4dd5b
HS
1101 if (r != EFI_SUCCESS)
1102 goto out_of_memory;
40e3e757 1103
fae0118e 1104 r = efi_add_protocol(console_input_handle,
ebb4dd5b
HS
1105 &efi_guid_text_input_protocol, &efi_con_in);
1106 if (r != EFI_SUCCESS)
1107 goto out_of_memory;
fae0118e
HS
1108 systab.con_in_handle = console_input_handle;
1109 r = efi_add_protocol(console_input_handle,
110c6280
HS
1110 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1111 if (r != EFI_SUCCESS)
1112 goto out_of_memory;
a17e62cc 1113
ebb4dd5b 1114 /* Create console events */
b095f3c8
HS
1115 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1116 NULL, NULL, &efi_con_in.wait_for_key);
91be9a77
HS
1117 if (r != EFI_SUCCESS) {
1118 printf("ERROR: Failed to register WaitForKey event\n");
1119 return r;
1120 }
110c6280 1121 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
91be9a77 1122 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
b095f3c8 1123 efi_console_timer_notify, NULL, NULL,
91be9a77
HS
1124 &console_timer_event);
1125 if (r != EFI_SUCCESS) {
1126 printf("ERROR: Failed to register console event\n");
1127 return r;
1128 }
1129 /* 5000 ns cycle is sufficient for 2 MBaud */
1130 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1131 if (r != EFI_SUCCESS)
1132 printf("ERROR: Failed to set console timer\n");
1133 return r;
ebb4dd5b 1134out_of_memory:
14d103bb 1135 printf("ERROR: Out of memory\n");
ebb4dd5b 1136 return r;
91be9a77 1137}