blob: ac0dec1146f63d5af338113365a00d529074840a [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf4aacacc2016-03-04 01:10:00 +01002/*
3 * EFI application console interface
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf4aacacc2016-03-04 01:10:00 +01006 */
7
8#include <common.h>
Rob Clark0d138cf2017-09-09 06:47:40 -04009#include <charset.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070011#include <time.h>
Rob Clark3863b712017-09-13 18:05:43 -040012#include <dm/device.h>
Alexander Graf4aacacc2016-03-04 01:10:00 +010013#include <efi_loader.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060014#include <env.h>
Rob Clark3863b712017-09-13 18:05:43 -040015#include <stdio_dev.h>
16#include <video_console.h>
Alexander Graf4aacacc2016-03-04 01:10:00 +010017
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
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +020048const efi_guid_t efi_guid_text_input_ex_protocol =
49 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +020050const efi_guid_t efi_guid_text_input_protocol =
51 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +020052const efi_guid_t efi_guid_text_output_protocol =
53 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
Alexander Graf4aacacc2016-03-04 01:10:00 +010054
55#define cESC '\x1b'
56#define ESC "\x1b"
57
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010058/* Default to mode 0 */
Alexander Graf4aacacc2016-03-04 01:10:00 +010059static struct simple_text_output_mode efi_con_mode = {
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010060 .max_mode = 1,
Alexander Graf4aacacc2016-03-04 01:10:00 +010061 .mode = 0,
62 .attribute = 0,
63 .cursor_column = 0,
64 .cursor_row = 0,
65 .cursor_visible = 1,
66};
67
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010068static int term_get_char(s32 *c)
69{
70 u64 timeout;
71
72 /* Wait up to 100 ms for a character */
73 timeout = timer_get_us() + 100000;
74
75 while (!tstc())
76 if (timer_get_us() > timeout)
77 return 1;
78
79 *c = getc();
80 return 0;
81}
82
Heinrich Schuchardt77135c22018-05-16 18:17:38 +020083/*
84 * Receive and parse a reply from the terminal.
85 *
86 * @n: array of return values
87 * @num: number of return values expected
88 * @end_char: character indicating end of terminal message
89 * @return: non-zero indicates error
90 */
91static int term_read_reply(int *n, int num, char end_char)
Alexander Graf4aacacc2016-03-04 01:10:00 +010092{
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010093 s32 c;
Alexander Graf4aacacc2016-03-04 01:10:00 +010094 int i = 0;
95
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010096 if (term_get_char(&c) || c != cESC)
Alexander Graf4aacacc2016-03-04 01:10:00 +010097 return -1;
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010098
99 if (term_get_char(&c) || c != '[')
Alexander Graf4aacacc2016-03-04 01:10:00 +0100100 return -1;
101
102 n[0] = 0;
103 while (1) {
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100104 if (!term_get_char(&c)) {
105 if (c == ';') {
106 i++;
107 if (i >= num)
108 return -1;
109 n[i] = 0;
110 continue;
111 } else if (c == end_char) {
112 break;
113 } else if (c > '9' || c < '0') {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100114 return -1;
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100115 }
116
117 /* Read one more decimal position */
118 n[i] *= 10;
119 n[i] += c - '0';
120 } else {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100121 return -1;
122 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100123 }
Heinrich Schuchardt77135c22018-05-16 18:17:38 +0200124 if (i != num - 1)
125 return -1;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100126
127 return 0;
128}
129
Alexander Graf4aacacc2016-03-04 01:10:00 +0100130static efi_status_t EFIAPI efi_cout_output_string(
131 struct efi_simple_text_output_protocol *this,
Rob Clark1ef185d2017-09-13 18:05:44 -0400132 const efi_string_t string)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100133{
Rob Clark1ef185d2017-09-13 18:05:44 -0400134 struct simple_text_output_mode *con = &efi_con_mode;
135 struct cout_mode *mode = &efi_cout_modes[con->mode];
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200136 char *buf, *pos;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200137 u16 *p;
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200138 efi_status_t ret = EFI_SUCCESS;
Rob Clark1ef185d2017-09-13 18:05:44 -0400139
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200140 EFI_ENTRY("%p, %p", this, string);
Rob Clark1ef185d2017-09-13 18:05:44 -0400141
Heinrich Schuchardte60b2982019-05-18 18:11:54 +0200142 if (!this || !string) {
143 ret = EFI_INVALID_PARAMETER;
144 goto out;
145 }
146
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200147 buf = malloc(utf16_utf8_strlen(string) + 1);
148 if (!buf) {
149 ret = EFI_OUT_OF_RESOURCES;
150 goto out;
151 }
152 pos = buf;
153 utf16_utf8_strcpy(&pos, string);
Rob Clark1ef185d2017-09-13 18:05:44 -0400154 fputs(stdout, buf);
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200155 free(buf);
Rob Clark1ef185d2017-09-13 18:05:44 -0400156
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200157 /*
158 * Update the cursor position.
159 *
160 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200161 * and U000D. All other control characters are ignored. Any non-control
162 * character increase the column by one.
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200163 */
164 for (p = string; *p; ++p) {
Rob Clark1ef185d2017-09-13 18:05:44 -0400165 switch (*p) {
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200166 case '\b': /* U+0008, backspace */
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200167 if (con->cursor_column)
168 con->cursor_column--;
Rob Clark1ef185d2017-09-13 18:05:44 -0400169 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200170 case '\n': /* U+000A, newline */
Rob Clark1ef185d2017-09-13 18:05:44 -0400171 con->cursor_column = 0;
172 con->cursor_row++;
173 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200174 case '\r': /* U+000D, carriage-return */
175 con->cursor_column = 0;
Rob Clark1ef185d2017-09-13 18:05:44 -0400176 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200177 case 0xd800 ... 0xdbff:
178 /*
179 * Ignore high surrogates, we do not want to count a
180 * Unicode character twice.
181 */
Rob Clark1ef185d2017-09-13 18:05:44 -0400182 break;
183 default:
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200184 /* Exclude control codes */
185 if (*p > 0x1f)
186 con->cursor_column++;
Rob Clark1ef185d2017-09-13 18:05:44 -0400187 break;
188 }
189 if (con->cursor_column >= mode->columns) {
190 con->cursor_column = 0;
191 con->cursor_row++;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100192 }
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200193 /*
194 * When we exceed the row count the terminal will scroll up one
195 * line. We have to adjust the cursor position.
196 */
197 if (con->cursor_row >= mode->rows && con->cursor_row)
198 con->cursor_row--;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100199 }
200
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200201out:
202 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100203}
204
205static efi_status_t EFIAPI efi_cout_test_string(
206 struct efi_simple_text_output_protocol *this,
Rob Clark1ef185d2017-09-13 18:05:44 -0400207 const efi_string_t string)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100208{
209 EFI_ENTRY("%p, %p", this, string);
210 return EFI_EXIT(EFI_SUCCESS);
211}
212
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100213static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
214{
215 if (!mode->present)
216 return false;
217
218 return (mode->rows == rows) && (mode->columns == cols);
219}
220
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200221/**
222 * query_console_serial() - query console size
223 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200224 * @rows: pointer to return number of rows
225 * @cols: pointer to return number of columns
226 * Returns: 0 on success
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200227 */
Rob Clark1e948922017-09-13 18:05:42 -0400228static int query_console_serial(int *rows, int *cols)
229{
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200230 int ret = 0;
231 int n[2];
Rob Clark1e948922017-09-13 18:05:42 -0400232
233 /* Empty input buffer */
234 while (tstc())
235 getc();
236
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200237 /*
238 * Not all terminals understand CSI [18t for querying the console size.
239 * We should adhere to escape sequences documented in the console_codes
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200240 * man page and the ECMA-48 standard.
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200241 *
242 * So here we follow a different approach. We position the cursor to the
243 * bottom right and query its position. Before leaving the function we
244 * restore the original cursor position.
245 */
246 printf(ESC "7" /* Save cursor position */
247 ESC "[r" /* Set scrolling region to full window */
248 ESC "[999;999H" /* Move to bottom right corner */
249 ESC "[6n"); /* Query cursor position */
Rob Clark1e948922017-09-13 18:05:42 -0400250
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200251 /* Read {rows,cols} */
252 if (term_read_reply(n, 2, 'R')) {
253 ret = 1;
254 goto out;
255 }
Rob Clark1e948922017-09-13 18:05:42 -0400256
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200257 *cols = n[1];
258 *rows = n[0];
259out:
260 printf(ESC "8"); /* Restore cursor position */
261 return ret;
Rob Clark1e948922017-09-13 18:05:42 -0400262}
263
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200264/*
265 * Update the mode table.
266 *
267 * By default the only mode available is 80x25. If the console has at least 50
268 * lines, enable mode 80x50. If we can query the console size and it is neither
269 * 80x25 nor 80x50, set it as an additional mode.
270 */
271static void query_console_size(void)
272{
273 const char *stdout_name = env_get("stdout");
Alexander Graf28795322018-06-03 15:51:17 +0200274 int rows = 25, cols = 80;
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200275
276 if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
277 IS_ENABLED(CONFIG_DM_VIDEO)) {
278 struct stdio_dev *stdout_dev =
279 stdio_get_by_name("vidconsole");
280 struct udevice *dev = stdout_dev->priv;
281 struct vidconsole_priv *priv =
282 dev_get_uclass_priv(dev);
283 rows = priv->rows;
284 cols = priv->cols;
285 } else if (query_console_serial(&rows, &cols)) {
286 return;
287 }
288
289 /* Test if we can have Mode 1 */
290 if (cols >= 80 && rows >= 50) {
291 efi_cout_modes[1].present = 1;
292 efi_con_mode.max_mode = 2;
293 }
294
295 /*
296 * Install our mode as mode 2 if it is different
297 * than mode 0 or 1 and set it as the currently selected mode
298 */
299 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
300 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
301 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
302 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
303 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
304 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
305 efi_con_mode.mode = EFI_COUT_MODE_2;
306 }
307}
308
Alexander Graf4aacacc2016-03-04 01:10:00 +0100309static efi_status_t EFIAPI efi_cout_query_mode(
310 struct efi_simple_text_output_protocol *this,
311 unsigned long mode_number, unsigned long *columns,
312 unsigned long *rows)
313{
314 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
315
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100316 if (mode_number >= efi_con_mode.max_mode)
317 return EFI_EXIT(EFI_UNSUPPORTED);
318
319 if (efi_cout_modes[mode_number].present != 1)
320 return EFI_EXIT(EFI_UNSUPPORTED);
321
Alexander Graf4aacacc2016-03-04 01:10:00 +0100322 if (columns)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100323 *columns = efi_cout_modes[mode_number].columns;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100324 if (rows)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100325 *rows = efi_cout_modes[mode_number].rows;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100326
327 return EFI_EXIT(EFI_SUCCESS);
328}
329
Rob Clark87ef0012017-10-10 08:23:01 -0400330static const struct {
331 unsigned int fg;
332 unsigned int bg;
333} color[] = {
334 { 30, 40 }, /* 0: black */
335 { 34, 44 }, /* 1: blue */
336 { 32, 42 }, /* 2: green */
337 { 36, 46 }, /* 3: cyan */
338 { 31, 41 }, /* 4: red */
339 { 35, 45 }, /* 5: magenta */
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +0200340 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
341 { 37, 47 }, /* 7: light gray, map to white */
Rob Clark87ef0012017-10-10 08:23:01 -0400342};
343
344/* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100345static efi_status_t EFIAPI efi_cout_set_attribute(
346 struct efi_simple_text_output_protocol *this,
347 unsigned long attribute)
348{
Rob Clark87ef0012017-10-10 08:23:01 -0400349 unsigned int bold = EFI_ATTR_BOLD(attribute);
350 unsigned int fg = EFI_ATTR_FG(attribute);
351 unsigned int bg = EFI_ATTR_BG(attribute);
352
Alexander Graf4aacacc2016-03-04 01:10:00 +0100353 EFI_ENTRY("%p, %lx", this, attribute);
354
Heinrich Schuchardt1e129622019-06-14 07:16:57 +0200355 efi_con_mode.attribute = attribute;
Rob Clark87ef0012017-10-10 08:23:01 -0400356 if (attribute)
357 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
358 else
359 printf(ESC"[0;37;40m");
360
361 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100362}
363
Heinrich Schuchardt7995cbe2019-12-22 07:15:55 +0000364/**
365 * efi_cout_clear_screen() - clear screen
366 *
367 * This function implements the ClearScreen service of the
368 * EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL. See the Unified Extensible Firmware
369 * Interface (UEFI) specification for details.
370 *
371 * @this: pointer to the protocol instance
372 * Return: status code
373 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100374static efi_status_t EFIAPI efi_cout_clear_screen(
375 struct efi_simple_text_output_protocol *this)
376{
377 EFI_ENTRY("%p", this);
378
Heinrich Schuchardt7995cbe2019-12-22 07:15:55 +0000379 /*
380 * The Linux console wants both a clear and a home command. The video
381 * uclass does not support <ESC>[H without coordinates, yet.
382 */
383 printf(ESC "[2J" ESC "[1;1H");
Heinrich Schuchardt2d099322018-07-05 08:18:00 +0200384 efi_con_mode.cursor_column = 0;
385 efi_con_mode.cursor_row = 0;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100386
387 return EFI_EXIT(EFI_SUCCESS);
388}
389
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200390static efi_status_t EFIAPI efi_cout_set_mode(
391 struct efi_simple_text_output_protocol *this,
392 unsigned long mode_number)
393{
394 EFI_ENTRY("%p, %ld", this, mode_number);
395
396 if (mode_number >= efi_con_mode.max_mode)
397 return EFI_EXIT(EFI_UNSUPPORTED);
Heinrich Schuchardt6a6afa72019-09-04 22:46:13 +0200398
399 if (!efi_cout_modes[mode_number].present)
400 return EFI_EXIT(EFI_UNSUPPORTED);
401
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200402 efi_con_mode.mode = mode_number;
403 EFI_CALL(efi_cout_clear_screen(this));
404
405 return EFI_EXIT(EFI_SUCCESS);
406}
407
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200408static efi_status_t EFIAPI efi_cout_reset(
409 struct efi_simple_text_output_protocol *this,
410 char extended_verification)
411{
412 EFI_ENTRY("%p, %d", this, extended_verification);
413
414 /* Clear screen */
415 EFI_CALL(efi_cout_clear_screen(this));
416 /* Set default colors */
Heinrich Schuchardt1e129622019-06-14 07:16:57 +0200417 efi_con_mode.attribute = 0x07;
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200418 printf(ESC "[0;37;40m");
419
420 return EFI_EXIT(EFI_SUCCESS);
421}
422
Alexander Graf4aacacc2016-03-04 01:10:00 +0100423static efi_status_t EFIAPI efi_cout_set_cursor_position(
424 struct efi_simple_text_output_protocol *this,
425 unsigned long column, unsigned long row)
426{
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200427 efi_status_t ret = EFI_SUCCESS;
428 struct simple_text_output_mode *con = &efi_con_mode;
429 struct cout_mode *mode = &efi_cout_modes[con->mode];
430
Alexander Graf4aacacc2016-03-04 01:10:00 +0100431 EFI_ENTRY("%p, %ld, %ld", this, column, row);
432
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200433 /* Check parameters */
434 if (!this) {
435 ret = EFI_INVALID_PARAMETER;
436 goto out;
437 }
438 if (row >= mode->rows || column >= mode->columns) {
439 ret = EFI_UNSUPPORTED;
440 goto out;
441 }
442
443 /*
444 * Set cursor position by sending CSI H.
445 * EFI origin is [0, 0], terminal origin is [1, 1].
446 */
447 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100448 efi_con_mode.cursor_column = column;
449 efi_con_mode.cursor_row = row;
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200450out:
451 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100452}
453
454static efi_status_t EFIAPI efi_cout_enable_cursor(
455 struct efi_simple_text_output_protocol *this,
456 bool enable)
457{
458 EFI_ENTRY("%p, %d", this, enable);
459
460 printf(ESC"[?25%c", enable ? 'h' : 'l');
Heinrich Schuchardt53f26b92019-06-02 22:54:28 +0200461 efi_con_mode.cursor_visible = !!enable;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100462
463 return EFI_EXIT(EFI_SUCCESS);
464}
465
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +0200466struct efi_simple_text_output_protocol efi_con_out = {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100467 .reset = efi_cout_reset,
468 .output_string = efi_cout_output_string,
469 .test_string = efi_cout_test_string,
470 .query_mode = efi_cout_query_mode,
471 .set_mode = efi_cout_set_mode,
472 .set_attribute = efi_cout_set_attribute,
473 .clear_screen = efi_cout_clear_screen,
474 .set_cursor_position = efi_cout_set_cursor_position,
475 .enable_cursor = efi_cout_enable_cursor,
476 .mode = (void*)&efi_con_mode,
477};
478
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200479/**
480 * struct efi_cin_notify_function - registered console input notify function
481 *
482 * @link: link to list
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200483 * @key: key to notify
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200484 * @function: function to call
485 */
486struct efi_cin_notify_function {
487 struct list_head link;
488 struct efi_key_data key;
489 efi_status_t (EFIAPI *function)
490 (struct efi_key_data *key_data);
491};
492
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200493static bool key_available;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200494static struct efi_key_data next_key;
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200495static LIST_HEAD(cin_notify_functions);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100496
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200497/**
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200498 * set_shift_mask() - set shift mask
499 *
500 * @mod: Xterm shift mask
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200501 * @key_state: receives the state of the shift, alt, control, and logo keys
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200502 */
503void set_shift_mask(int mod, struct efi_key_state *key_state)
504{
505 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
506 if (mod) {
507 --mod;
508 if (mod & 1)
509 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
510 if (mod & 2)
511 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
512 if (mod & 4)
513 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
Heinrich Schuchardtfea93d62019-06-16 22:33:20 +0200514 if (!mod || (mod & 8))
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200515 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200516 }
517}
518
519/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200520 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200521 *
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100522 * This gets called when we have already parsed CSI.
523 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200524 * @key_state: receives the state of the shift, alt, control, and logo keys
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100525 * @return: the unmodified code
526 */
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200527static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100528{
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200529 int c, mod = 0, ret = 0;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100530
531 c = getc();
532
533 if (c != ';') {
534 ret = c;
535 if (c == '~')
536 goto out;
537 c = getc();
538 }
539 for (;;) {
540 switch (c) {
541 case '0'...'9':
542 mod *= 10;
543 mod += c - '0';
544 /* fall through */
545 case ';':
546 c = getc();
547 break;
548 default:
549 goto out;
550 }
551 }
552out:
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200553 set_shift_mask(mod, key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100554 if (!ret)
555 ret = c;
556 return ret;
557}
558
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200559/**
560 * efi_cin_read_key() - read a key from the console input
561 *
562 * @key: - key received
563 * Return: - status code
564 */
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200565static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100566{
567 struct efi_input_key pressed_key = {
568 .scan_code = 0,
569 .unicode_char = 0,
570 };
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200571 s32 ch;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100572
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200573 if (console_read_unicode(&ch))
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200574 return EFI_NOT_READY;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200575
576 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
577 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
578
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200579 /* We do not support multi-word codes */
580 if (ch >= 0x10000)
581 ch = '?';
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200582
583 switch (ch) {
584 case 0x1b:
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100585 /*
586 * Xterm Control Sequences
587 * https://www.xfree86.org/4.8.0/ctlseqs.html
588 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100589 ch = getc();
590 switch (ch) {
591 case cESC: /* ESC */
592 pressed_key.scan_code = 23;
593 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200594 case 'O': /* F1 - F4, End */
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100595 ch = getc();
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200596 /* consider modifiers */
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200597 if (ch == 'F') { /* End */
598 pressed_key.scan_code = 6;
599 break;
600 } else if (ch < 'P') {
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200601 set_shift_mask(ch - '0', &key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100602 ch = getc();
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200603 }
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100604 pressed_key.scan_code = ch - 'P' + 11;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100605 break;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100606 case '[':
607 ch = getc();
608 switch (ch) {
609 case 'A'...'D': /* up, down right, left */
610 pressed_key.scan_code = ch - 'A' + 1;
611 break;
612 case 'F': /* End */
613 pressed_key.scan_code = 6;
614 break;
615 case 'H': /* Home */
616 pressed_key.scan_code = 5;
617 break;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100618 case '1':
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200619 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100620 switch (ch) {
621 case '1'...'5': /* F1 - F5 */
622 pressed_key.scan_code = ch - '1' + 11;
623 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200624 case '6'...'9': /* F5 - F8 */
625 pressed_key.scan_code = ch - '6' + 15;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100626 break;
627 case 'A'...'D': /* up, down right, left */
628 pressed_key.scan_code = ch - 'A' + 1;
629 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200630 case 'F': /* End */
631 pressed_key.scan_code = 6;
632 break;
633 case 'H': /* Home */
634 pressed_key.scan_code = 5;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100635 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200636 case '~': /* Home */
637 pressed_key.scan_code = 5;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100638 break;
639 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100640 break;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100641 case '2':
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200642 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100643 switch (ch) {
644 case '0'...'1': /* F9 - F10 */
645 pressed_key.scan_code = ch - '0' + 19;
646 break;
647 case '3'...'4': /* F11 - F12 */
648 pressed_key.scan_code = ch - '3' + 21;
649 break;
650 case '~': /* INS */
651 pressed_key.scan_code = 7;
652 break;
653 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100654 break;
655 case '3': /* DEL */
656 pressed_key.scan_code = 8;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200657 analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100658 break;
659 case '5': /* PG UP */
660 pressed_key.scan_code = 9;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200661 analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100662 break;
663 case '6': /* PG DOWN */
664 pressed_key.scan_code = 10;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200665 analyze_modifiers(&key->key_state);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100666 break;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200667 } /* [ */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100668 break;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200669 default:
670 /* ALT key */
671 set_shift_mask(3, &key->key_state);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100672 }
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200673 break;
674 case 0x7f:
Alexander Graf4aacacc2016-03-04 01:10:00 +0100675 /* Backspace */
676 ch = 0x08;
677 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200678 if (pressed_key.scan_code) {
679 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
680 } else {
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100681 pressed_key.unicode_char = ch;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200682
683 /*
684 * Assume left control key for control characters typically
685 * entered using the control key.
686 */
687 if (ch >= 0x01 && ch <= 0x1f) {
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200688 key->key_state.key_shift_state |=
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200689 EFI_SHIFT_STATE_VALID;
690 switch (ch) {
691 case 0x01 ... 0x07:
692 case 0x0b ... 0x0c:
693 case 0x0e ... 0x1f:
694 key->key_state.key_shift_state |=
695 EFI_LEFT_CONTROL_PRESSED;
696 }
697 }
698 }
699 key->key = pressed_key;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100700
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200701 return EFI_SUCCESS;
702}
703
704/**
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200705 * efi_cin_notify() - notify registered functions
706 */
707static void efi_cin_notify(void)
708{
709 struct efi_cin_notify_function *item;
710
711 list_for_each_entry(item, &cin_notify_functions, link) {
712 bool match = true;
713
714 /* We do not support toggle states */
715 if (item->key.key.unicode_char || item->key.key.scan_code) {
716 if (item->key.key.unicode_char !=
717 next_key.key.unicode_char ||
718 item->key.key.scan_code != next_key.key.scan_code)
719 match = false;
720 }
721 if (item->key.key_state.key_shift_state &&
722 item->key.key_state.key_shift_state !=
723 next_key.key_state.key_shift_state)
724 match = false;
725
726 if (match)
727 /* We don't bother about the return code */
728 EFI_CALL(item->function(&next_key));
729 }
730}
731
732/**
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200733 * efi_cin_check() - check if keyboard input is available
734 */
735static void efi_cin_check(void)
736{
737 efi_status_t ret;
738
739 if (key_available) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200740 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200741 return;
742 }
743
744 if (tstc()) {
745 ret = efi_cin_read_key(&next_key);
746 if (ret == EFI_SUCCESS) {
747 key_available = true;
748
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200749 /* Notify registered functions */
750 efi_cin_notify();
751
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200752 /* Queue the wait for key event */
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200753 if (key_available)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200754 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200755 }
756 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200757}
758
759/**
760 * efi_cin_empty_buffer() - empty input buffer
761 */
762static void efi_cin_empty_buffer(void)
763{
764 while (tstc())
765 getc();
766 key_available = false;
767}
768
769/**
770 * efi_cin_reset_ex() - reset console input
771 *
772 * @this: - the extended simple text input protocol
773 * @extended_verification: - extended verification
774 *
775 * This function implements the reset service of the
776 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
777 *
778 * See the Unified Extensible Firmware Interface (UEFI) specification for
779 * details.
780 *
781 * Return: old value of the task priority level
782 */
783static efi_status_t EFIAPI efi_cin_reset_ex(
784 struct efi_simple_text_input_ex_protocol *this,
785 bool extended_verification)
786{
787 efi_status_t ret = EFI_SUCCESS;
788
789 EFI_ENTRY("%p, %d", this, extended_verification);
790
791 /* Check parameters */
792 if (!this) {
793 ret = EFI_INVALID_PARAMETER;
794 goto out;
795 }
796
797 efi_cin_empty_buffer();
798out:
799 return EFI_EXIT(ret);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200800}
801
802/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200803 * efi_cin_read_key_stroke_ex() - read key stroke
804 *
805 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
806 * @key_data: key read from console
807 * Return: status code
808 *
809 * This function implements the ReadKeyStrokeEx service of the
810 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
811 *
812 * See the Unified Extensible Firmware Interface (UEFI) specification for
813 * details.
814 */
815static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
816 struct efi_simple_text_input_ex_protocol *this,
817 struct efi_key_data *key_data)
818{
819 efi_status_t ret = EFI_SUCCESS;
820
821 EFI_ENTRY("%p, %p", this, key_data);
822
823 /* Check parameters */
824 if (!this || !key_data) {
825 ret = EFI_INVALID_PARAMETER;
826 goto out;
827 }
828
829 /* We don't do interrupts, so check for timers cooperatively */
830 efi_timer_check();
831
832 /* Enable console input after ExitBootServices */
833 efi_cin_check();
834
835 if (!key_available) {
836 ret = EFI_NOT_READY;
837 goto out;
838 }
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +0200839 /*
840 * CTRL+A - CTRL+Z have to be signaled as a - z.
841 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
842 */
843 switch (next_key.key.unicode_char) {
844 case 0x01 ... 0x07:
845 case 0x0b ... 0x0c:
846 case 0x0e ... 0x1a:
847 if (!(next_key.key_state.key_toggle_state &
848 EFI_CAPS_LOCK_ACTIVE) ^
849 !(next_key.key_state.key_shift_state &
850 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
851 next_key.key.unicode_char += 0x40;
852 else
853 next_key.key.unicode_char += 0x60;
854 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200855 *key_data = next_key;
856 key_available = false;
857 efi_con_in.wait_for_key->is_signaled = false;
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +0200858
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200859out:
860 return EFI_EXIT(ret);
861}
862
863/**
864 * efi_cin_set_state() - set toggle key state
865 *
866 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +0200867 * @key_toggle_state: pointer to key toggle state
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200868 * Return: status code
869 *
870 * This function implements the SetState service of the
871 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
872 *
873 * See the Unified Extensible Firmware Interface (UEFI) specification for
874 * details.
875 */
876static efi_status_t EFIAPI efi_cin_set_state(
877 struct efi_simple_text_input_ex_protocol *this,
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +0200878 u8 *key_toggle_state)
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200879{
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +0200880 EFI_ENTRY("%p, %p", this, key_toggle_state);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200881 /*
882 * U-Boot supports multiple console input sources like serial and
883 * net console for which a key toggle state cannot be set at all.
884 *
885 * According to the UEFI specification it is allowable to not implement
886 * this service.
887 */
888 return EFI_EXIT(EFI_UNSUPPORTED);
889}
890
891/**
892 * efi_cin_register_key_notify() - register key notification function
893 *
894 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
895 * @key_data: key to be notified
896 * @key_notify_function: function to be called if the key is pressed
897 * @notify_handle: handle for unregistering the notification
898 * Return: status code
899 *
900 * This function implements the SetState service of the
901 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
902 *
903 * See the Unified Extensible Firmware Interface (UEFI) specification for
904 * details.
905 */
906static efi_status_t EFIAPI efi_cin_register_key_notify(
907 struct efi_simple_text_input_ex_protocol *this,
908 struct efi_key_data *key_data,
909 efi_status_t (EFIAPI *key_notify_function)(
910 struct efi_key_data *key_data),
911 void **notify_handle)
912{
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200913 efi_status_t ret = EFI_SUCCESS;
914 struct efi_cin_notify_function *notify_function;
915
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200916 EFI_ENTRY("%p, %p, %p, %p",
917 this, key_data, key_notify_function, notify_handle);
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200918
919 /* Check parameters */
920 if (!this || !key_data || !key_notify_function || !notify_handle) {
921 ret = EFI_INVALID_PARAMETER;
922 goto out;
923 }
924
925 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
926 key_data->key.unicode_char,
927 key_data->key.scan_code,
928 key_data->key_state.key_shift_state,
929 key_data->key_state.key_toggle_state);
930
931 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
932 if (!notify_function) {
933 ret = EFI_OUT_OF_RESOURCES;
934 goto out;
935 }
936 notify_function->key = *key_data;
937 notify_function->function = key_notify_function;
938 list_add_tail(&notify_function->link, &cin_notify_functions);
939 *notify_handle = notify_function;
940out:
941 return EFI_EXIT(ret);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200942}
943
944/**
945 * efi_cin_unregister_key_notify() - unregister key notification function
946 *
947 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
948 * @notification_handle: handle received when registering
949 * Return: status code
950 *
951 * This function implements the SetState service of the
952 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
953 *
954 * See the Unified Extensible Firmware Interface (UEFI) specification for
955 * details.
956 */
957static efi_status_t EFIAPI efi_cin_unregister_key_notify(
958 struct efi_simple_text_input_ex_protocol *this,
959 void *notification_handle)
960{
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200961 efi_status_t ret = EFI_INVALID_PARAMETER;
962 struct efi_cin_notify_function *item, *notify_function =
963 notification_handle;
964
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200965 EFI_ENTRY("%p, %p", this, notification_handle);
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200966
967 /* Check parameters */
968 if (!this || !notification_handle)
969 goto out;
970
971 list_for_each_entry(item, &cin_notify_functions, link) {
972 if (item == notify_function) {
973 ret = EFI_SUCCESS;
974 break;
975 }
976 }
977 if (ret != EFI_SUCCESS)
978 goto out;
979
980 /* Remove the notify function */
981 list_del(&notify_function->link);
982 free(notify_function);
983out:
984 return EFI_EXIT(ret);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200985}
986
987
988/**
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200989 * efi_cin_reset() - drain the input buffer
990 *
991 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
992 * @extended_verification: allow for exhaustive verification
993 * Return: status code
994 *
995 * This function implements the Reset service of the
996 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
997 *
998 * See the Unified Extensible Firmware Interface (UEFI) specification for
999 * details.
1000 */
1001static efi_status_t EFIAPI efi_cin_reset
1002 (struct efi_simple_text_input_protocol *this,
1003 bool extended_verification)
1004{
1005 efi_status_t ret = EFI_SUCCESS;
1006
1007 EFI_ENTRY("%p, %d", this, extended_verification);
1008
1009 /* Check parameters */
1010 if (!this) {
1011 ret = EFI_INVALID_PARAMETER;
1012 goto out;
1013 }
1014
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001015 efi_cin_empty_buffer();
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001016out:
1017 return EFI_EXIT(ret);
1018}
1019
1020/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001021 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001022 *
1023 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1024 * @key: key read from console
1025 * Return: status code
1026 *
1027 * This function implements the ReadKeyStroke service of the
1028 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1029 *
1030 * See the Unified Extensible Firmware Interface (UEFI) specification for
1031 * details.
1032 */
1033static efi_status_t EFIAPI efi_cin_read_key_stroke
1034 (struct efi_simple_text_input_protocol *this,
1035 struct efi_input_key *key)
1036{
1037 efi_status_t ret = EFI_SUCCESS;
1038
1039 EFI_ENTRY("%p, %p", this, key);
1040
1041 /* Check parameters */
1042 if (!this || !key) {
1043 ret = EFI_INVALID_PARAMETER;
1044 goto out;
1045 }
1046
1047 /* We don't do interrupts, so check for timers cooperatively */
1048 efi_timer_check();
1049
1050 /* Enable console input after ExitBootServices */
1051 efi_cin_check();
1052
1053 if (!key_available) {
1054 ret = EFI_NOT_READY;
1055 goto out;
1056 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001057 *key = next_key.key;
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001058 key_available = false;
1059 efi_con_in.wait_for_key->is_signaled = false;
1060out:
1061 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +01001062}
1063
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001064static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1065 .reset = efi_cin_reset_ex,
1066 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1067 .wait_for_key_ex = NULL,
1068 .set_state = efi_cin_set_state,
1069 .register_key_notify = efi_cin_register_key_notify,
1070 .unregister_key_notify = efi_cin_unregister_key_notify,
1071};
1072
Heinrich Schuchardt3dabffd2018-09-08 10:20:10 +02001073struct efi_simple_text_input_protocol efi_con_in = {
Alexander Graf4aacacc2016-03-04 01:10:00 +01001074 .reset = efi_cin_reset,
1075 .read_key_stroke = efi_cin_read_key_stroke,
1076 .wait_for_key = NULL,
1077};
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001078
1079static struct efi_event *console_timer_event;
1080
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001081/*
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001082 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001083 *
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001084 * @event: console timer event
1085 * @context: not used
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001086 */
xypron.glpk@gmx.de0ce42dc2017-07-20 05:26:07 +02001087static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1088 void *context)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001089{
1090 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001091 efi_cin_check();
1092 EFI_EXIT(EFI_SUCCESS);
1093}
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001094
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001095/**
1096 * efi_key_notify() - notify the wait for key event
1097 *
1098 * @event: wait for key event
1099 * @context: not used
1100 */
1101static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1102{
1103 EFI_ENTRY("%p, %p", event, context);
1104 efi_cin_check();
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001105 EFI_EXIT(EFI_SUCCESS);
1106}
1107
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001108/**
1109 * efi_console_register() - install the console protocols
1110 *
1111 * This function is called from do_bootefi_exec().
Heinrich Schuchardta92e9ab2018-10-02 06:08:26 +02001112 *
1113 * Return: status code
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001114 */
Heinrich Schuchardta92e9ab2018-10-02 06:08:26 +02001115efi_status_t efi_console_register(void)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001116{
1117 efi_status_t r;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001118 efi_handle_t console_output_handle;
1119 efi_handle_t console_input_handle;
Rob Clark49f7b4b2017-07-24 10:39:01 -04001120
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +02001121 /* Set up mode information */
1122 query_console_size();
1123
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001124 /* Create handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001125 r = efi_create_handle(&console_output_handle);
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001126 if (r != EFI_SUCCESS)
1127 goto out_of_memory;
Alexander Graf36bab912018-09-04 14:59:11 +02001128
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001129 r = efi_add_protocol(console_output_handle,
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001130 &efi_guid_text_output_protocol, &efi_con_out);
1131 if (r != EFI_SUCCESS)
1132 goto out_of_memory;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001133 systab.con_out_handle = console_output_handle;
1134 systab.stderr_handle = console_output_handle;
Alexander Graf36bab912018-09-04 14:59:11 +02001135
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001136 r = efi_create_handle(&console_input_handle);
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001137 if (r != EFI_SUCCESS)
1138 goto out_of_memory;
Alexander Graf36bab912018-09-04 14:59:11 +02001139
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001140 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001141 &efi_guid_text_input_protocol, &efi_con_in);
1142 if (r != EFI_SUCCESS)
1143 goto out_of_memory;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001144 systab.con_in_handle = console_input_handle;
1145 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001146 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1147 if (r != EFI_SUCCESS)
1148 goto out_of_memory;
Rob Clark49f7b4b2017-07-24 10:39:01 -04001149
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001150 /* Create console events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001151 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1152 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001153 if (r != EFI_SUCCESS) {
1154 printf("ERROR: Failed to register WaitForKey event\n");
1155 return r;
1156 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001157 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001158 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001159 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001160 &console_timer_event);
1161 if (r != EFI_SUCCESS) {
1162 printf("ERROR: Failed to register console event\n");
1163 return r;
1164 }
1165 /* 5000 ns cycle is sufficient for 2 MBaud */
1166 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1167 if (r != EFI_SUCCESS)
1168 printf("ERROR: Failed to set console timer\n");
1169 return r;
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001170out_of_memory:
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +02001171 printf("ERROR: Out of memory\n");
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001172 return r;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001173}