blob: 3b012e1a669ddc95a0f8e566c4384d4f243adfa4 [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>
Simon Glass336d4612020-02-03 07:36:16 -070010#include <malloc.h>
Simon Glass10453152019-11-14 12:57:30 -070011#include <time.h>
Rob Clarka18c5a82017-09-13 18:05:43 -040012#include <dm/device.h>
Alexander Grafc1311ad2016-03-04 01:10:00 +010013#include <efi_loader.h>
Simon Glass7b51b572019-08-01 09:46:52 -060014#include <env.h>
Rob Clarka18c5a82017-09-13 18:05:43 -040015#include <stdio_dev.h>
16#include <video_console.h>
Heinrich Schuchardt9abb01a2020-12-27 14:47:50 +010017#include <linux/delay.h>
Alexander Grafc1311ad2016-03-04 01:10:00 +010018
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +010019#define EFI_COUT_MODE_2 2
20#define EFI_MAX_COUT_MODE 3
21
22struct cout_mode {
23 unsigned long columns;
24 unsigned long rows;
25 int present;
26};
27
28static struct cout_mode efi_cout_modes[] = {
29 /* EFI Mode 0 is 80x25 and always present */
30 {
31 .columns = 80,
32 .rows = 25,
33 .present = 1,
34 },
35 /* EFI Mode 1 is always 80x50 */
36 {
37 .columns = 80,
38 .rows = 50,
39 .present = 0,
40 },
41 /* Value are unknown until we query the console */
42 {
43 .columns = 0,
44 .rows = 0,
45 .present = 0,
46 },
47};
48
Heinrich Schuchardt110c6282018-09-11 22:38:08 +020049const efi_guid_t efi_guid_text_input_ex_protocol =
50 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +020051const efi_guid_t efi_guid_text_input_protocol =
52 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +020053const efi_guid_t efi_guid_text_output_protocol =
54 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
Alexander Grafc1311ad2016-03-04 01:10:00 +010055
56#define cESC '\x1b'
57#define ESC "\x1b"
58
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +010059/* Default to mode 0 */
Alexander Grafc1311ad2016-03-04 01:10:00 +010060static struct simple_text_output_mode efi_con_mode = {
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +010061 .max_mode = 1,
Alexander Grafc1311ad2016-03-04 01:10:00 +010062 .mode = 0,
63 .attribute = 0,
64 .cursor_column = 0,
65 .cursor_row = 0,
66 .cursor_visible = 1,
67};
68
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +010069static int term_get_char(s32 *c)
70{
71 u64 timeout;
72
73 /* Wait up to 100 ms for a character */
74 timeout = timer_get_us() + 100000;
75
76 while (!tstc())
77 if (timer_get_us() > timeout)
78 return 1;
79
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +020080 *c = getchar();
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +010081 return 0;
82}
83
Heinrich Schuchardt325567d2020-06-04 18:40:44 +020084/**
Heinrich Schuchardt62217292018-05-16 18:17:38 +020085 * Receive and parse a reply from the terminal.
86 *
87 * @n: array of return values
88 * @num: number of return values expected
89 * @end_char: character indicating end of terminal message
Heinrich Schuchardt325567d2020-06-04 18:40:44 +020090 * Return: non-zero indicates error
Heinrich Schuchardt62217292018-05-16 18:17:38 +020091 */
92static int term_read_reply(int *n, int num, char end_char)
Alexander Grafc1311ad2016-03-04 01:10:00 +010093{
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +010094 s32 c;
Alexander Grafc1311ad2016-03-04 01:10:00 +010095 int i = 0;
96
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +010097 if (term_get_char(&c) || c != cESC)
Alexander Grafc1311ad2016-03-04 01:10:00 +010098 return -1;
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +010099
100 if (term_get_char(&c) || c != '[')
Alexander Grafc1311ad2016-03-04 01:10:00 +0100101 return -1;
102
103 n[0] = 0;
104 while (1) {
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +0100105 if (!term_get_char(&c)) {
106 if (c == ';') {
107 i++;
108 if (i >= num)
109 return -1;
110 n[i] = 0;
111 continue;
112 } else if (c == end_char) {
113 break;
114 } else if (c > '9' || c < '0') {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100115 return -1;
Matthias Bruggerdd1a1ec2019-03-05 12:50:18 +0100116 }
117
118 /* Read one more decimal position */
119 n[i] *= 10;
120 n[i] += c - '0';
121 } else {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100122 return -1;
123 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100124 }
Heinrich Schuchardt62217292018-05-16 18:17:38 +0200125 if (i != num - 1)
126 return -1;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100127
128 return 0;
129}
130
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200131/**
132 * efi_cout_output_string() - write Unicode string to console
133 *
134 * This function implements the OutputString service of the simple text output
135 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
136 * for details.
137 *
138 * @this: simple text output protocol
139 * @string: u16 string
140 * Return: status code
141 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100142static efi_status_t EFIAPI efi_cout_output_string(
143 struct efi_simple_text_output_protocol *this,
Heinrich Schuchardt7913c7d2021-01-05 07:50:09 +0100144 const u16 *string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100145{
Rob Clark3a45bc72017-09-13 18:05:44 -0400146 struct simple_text_output_mode *con = &efi_con_mode;
147 struct cout_mode *mode = &efi_cout_modes[con->mode];
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200148 char *buf, *pos;
Heinrich Schuchardt7913c7d2021-01-05 07:50:09 +0100149 const u16 *p;
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200150 efi_status_t ret = EFI_SUCCESS;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100151
152 EFI_ENTRY("%p, %p", this, string);
Rob Clark3a45bc72017-09-13 18:05:44 -0400153
Heinrich Schuchardtb31ca6b2019-05-18 18:11:54 +0200154 if (!this || !string) {
155 ret = EFI_INVALID_PARAMETER;
156 goto out;
157 }
158
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200159 buf = malloc(utf16_utf8_strlen(string) + 1);
160 if (!buf) {
161 ret = EFI_OUT_OF_RESOURCES;
162 goto out;
163 }
164 pos = buf;
165 utf16_utf8_strcpy(&pos, string);
Rob Clark3a45bc72017-09-13 18:05:44 -0400166 fputs(stdout, buf);
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200167 free(buf);
Rob Clark3a45bc72017-09-13 18:05:44 -0400168
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200169 /*
170 * Update the cursor position.
171 *
172 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
Heinrich Schuchardt97ea0692019-09-04 21:13:45 +0200173 * and U000D. All other control characters are ignored. Any non-control
174 * character increase the column by one.
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200175 */
176 for (p = string; *p; ++p) {
Rob Clark3a45bc72017-09-13 18:05:44 -0400177 switch (*p) {
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200178 case '\b': /* U+0008, backspace */
Heinrich Schuchardt97ea0692019-09-04 21:13:45 +0200179 if (con->cursor_column)
180 con->cursor_column--;
Rob Clark3a45bc72017-09-13 18:05:44 -0400181 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200182 case '\n': /* U+000A, newline */
Rob Clark3a45bc72017-09-13 18:05:44 -0400183 con->cursor_column = 0;
184 con->cursor_row++;
185 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200186 case '\r': /* U+000D, carriage-return */
187 con->cursor_column = 0;
Rob Clark3a45bc72017-09-13 18:05:44 -0400188 break;
Heinrich Schuchardt7ca7c3c2018-04-29 16:24:25 +0200189 case 0xd800 ... 0xdbff:
190 /*
191 * Ignore high surrogates, we do not want to count a
192 * Unicode character twice.
193 */
Rob Clark3a45bc72017-09-13 18:05:44 -0400194 break;
195 default:
Heinrich Schuchardt97ea0692019-09-04 21:13:45 +0200196 /* Exclude control codes */
197 if (*p > 0x1f)
198 con->cursor_column++;
Rob Clark3a45bc72017-09-13 18:05:44 -0400199 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100200 }
Rob Clark3a45bc72017-09-13 18:05:44 -0400201 if (con->cursor_column >= mode->columns) {
202 con->cursor_column = 0;
203 con->cursor_row++;
204 }
Heinrich Schuchardt97ea0692019-09-04 21:13:45 +0200205 /*
206 * When we exceed the row count the terminal will scroll up one
207 * line. We have to adjust the cursor position.
208 */
209 if (con->cursor_row >= mode->rows && con->cursor_row)
210 con->cursor_row--;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100211 }
212
Heinrich Schuchardtba7bd5c2018-08-31 21:31:32 +0200213out:
214 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100215}
216
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200217/**
218 * efi_cout_test_string() - test writing Unicode string to console
219 *
220 * This function implements the TestString service of the simple text output
221 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
222 * for details.
223 *
224 * As in OutputString we simply convert UTF-16 to UTF-8 there are no unsupported
225 * code points and we can always return EFI_SUCCESS.
226 *
227 * @this: simple text output protocol
228 * @string: u16 string
229 * Return: status code
230 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100231static efi_status_t EFIAPI efi_cout_test_string(
232 struct efi_simple_text_output_protocol *this,
Heinrich Schuchardt7913c7d2021-01-05 07:50:09 +0100233 const u16 *string)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100234{
235 EFI_ENTRY("%p, %p", this, string);
236 return EFI_EXIT(EFI_SUCCESS);
237}
238
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200239/**
240 * cout_mode_matches() - check if mode has given terminal size
241 *
242 * @mode: text mode
243 * @rows: number of rows
244 * @cols: number of columns
245 * Return: true if number of rows and columns matches the mode and
246 * the mode is present
247 */
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100248static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
249{
250 if (!mode->present)
251 return false;
252
253 return (mode->rows == rows) && (mode->columns == cols);
254}
255
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200256/**
Heinrich Schuchardta95f4c82021-03-16 12:56:57 +0100257 * query_console_serial() - query serial console size
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200258 *
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200259 * When using a serial console or the net console we can only devise the
260 * terminal size by querying the terminal using ECMA-48 control sequences.
261 *
Heinrich Schuchardtfe1a81c2019-09-05 20:37:13 +0200262 * @rows: pointer to return number of rows
263 * @cols: pointer to return number of columns
264 * Returns: 0 on success
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200265 */
Rob Clark71cc25c2017-09-13 18:05:42 -0400266static int query_console_serial(int *rows, int *cols)
267{
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200268 int ret = 0;
269 int n[2];
Rob Clark71cc25c2017-09-13 18:05:42 -0400270
271 /* Empty input buffer */
272 while (tstc())
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200273 getchar();
Rob Clark71cc25c2017-09-13 18:05:42 -0400274
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200275 /*
276 * Not all terminals understand CSI [18t for querying the console size.
277 * We should adhere to escape sequences documented in the console_codes
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200278 * man page and the ECMA-48 standard.
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200279 *
280 * So here we follow a different approach. We position the cursor to the
281 * bottom right and query its position. Before leaving the function we
282 * restore the original cursor position.
283 */
284 printf(ESC "7" /* Save cursor position */
285 ESC "[r" /* Set scrolling region to full window */
286 ESC "[999;999H" /* Move to bottom right corner */
287 ESC "[6n"); /* Query cursor position */
Rob Clark71cc25c2017-09-13 18:05:42 -0400288
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200289 /* Read {rows,cols} */
290 if (term_read_reply(n, 2, 'R')) {
291 ret = 1;
292 goto out;
293 }
Rob Clark71cc25c2017-09-13 18:05:42 -0400294
Heinrich Schuchardt6bb591f2018-09-15 23:52:07 +0200295 *cols = n[1];
296 *rows = n[0];
297out:
298 printf(ESC "8"); /* Restore cursor position */
299 return ret;
Rob Clark71cc25c2017-09-13 18:05:42 -0400300}
301
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200302/**
Heinrich Schuchardta95f4c82021-03-16 12:56:57 +0100303 * query_vidconsole() - query video console size
304 *
305 *
306 * @rows: pointer to return number of rows
307 * @cols: pointer to return number of columns
308 * Returns: 0 on success
309 */
310static int __maybe_unused query_vidconsole(int *rows, int *cols)
311{
312 const char *stdout_name = env_get("stdout");
313 struct stdio_dev *stdout_dev;
314 struct udevice *dev;
315 struct vidconsole_priv *priv;
316
317 if (!stdout_name || strncmp(stdout_name, "vidconsole", 10))
318 return -ENODEV;
319 stdout_dev = stdio_get_by_name("vidconsole");
320 if (!stdout_dev)
321 return -ENODEV;
322 dev = stdout_dev->priv;
323 if (!dev)
324 return -ENODEV;
325 priv = dev_get_uclass_priv(dev);
326 if (!priv)
327 return -ENODEV;
328 *rows = priv->rows;
329 *cols = priv->cols;
330 return 0;
331}
332
333/**
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200334 * query_console_size() - update the mode table.
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200335 *
336 * By default the only mode available is 80x25. If the console has at least 50
337 * lines, enable mode 80x50. If we can query the console size and it is neither
338 * 80x25 nor 80x50, set it as an additional mode.
339 */
340static void query_console_size(void)
341{
Alexander Graf80483b22018-06-03 15:51:17 +0200342 int rows = 25, cols = 80;
Heinrich Schuchardta95f4c82021-03-16 12:56:57 +0100343 int ret = -ENODEV;
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200344
Heinrich Schuchardtabd62e42021-06-29 10:09:14 +0200345 if (IS_ENABLED(CONFIG_DM_VIDEO))
Heinrich Schuchardta95f4c82021-03-16 12:56:57 +0100346 ret = query_vidconsole(&rows, &cols);
347 if (ret)
348 ret = query_console_serial(&rows, &cols);
349 if (ret)
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200350 return;
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +0200351
352 /* Test if we can have Mode 1 */
353 if (cols >= 80 && rows >= 50) {
354 efi_cout_modes[1].present = 1;
355 efi_con_mode.max_mode = 2;
356 }
357
358 /*
359 * Install our mode as mode 2 if it is different
360 * than mode 0 or 1 and set it as the currently selected mode
361 */
362 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
363 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
364 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
365 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
366 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
367 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
368 efi_con_mode.mode = EFI_COUT_MODE_2;
369 }
370}
371
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200372
373/**
374 * efi_cout_query_mode() - get terminal size for a text mode
375 *
376 * This function implements the QueryMode service of the simple text output
377 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
378 * for details.
379 *
380 * @this: simple text output protocol
381 * @mode_number: mode number to retrieve information on
382 * @columns: number of columns
383 * @rows: number of rows
384 * Return: status code
385 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100386static efi_status_t EFIAPI efi_cout_query_mode(
387 struct efi_simple_text_output_protocol *this,
388 unsigned long mode_number, unsigned long *columns,
389 unsigned long *rows)
390{
391 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
392
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100393 if (mode_number >= efi_con_mode.max_mode)
394 return EFI_EXIT(EFI_UNSUPPORTED);
395
396 if (efi_cout_modes[mode_number].present != 1)
397 return EFI_EXIT(EFI_UNSUPPORTED);
398
Alexander Grafc1311ad2016-03-04 01:10:00 +0100399 if (columns)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100400 *columns = efi_cout_modes[mode_number].columns;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100401 if (rows)
Emmanuel Vadot5be8b0a2016-11-08 06:03:29 +0100402 *rows = efi_cout_modes[mode_number].rows;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100403
404 return EFI_EXIT(EFI_SUCCESS);
405}
406
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400407static const struct {
408 unsigned int fg;
409 unsigned int bg;
410} color[] = {
411 { 30, 40 }, /* 0: black */
412 { 34, 44 }, /* 1: blue */
413 { 32, 42 }, /* 2: green */
414 { 36, 46 }, /* 3: cyan */
415 { 31, 41 }, /* 4: red */
416 { 35, 45 }, /* 5: magenta */
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +0200417 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
418 { 37, 47 }, /* 7: light gray, map to white */
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400419};
420
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200421/**
422 * efi_cout_set_attribute() - set fore- and background color
423 *
424 * This function implements the SetAttribute service of the simple text output
425 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
426 * for details.
427 *
428 * @this: simple text output protocol
429 * @attribute: foreground color - bits 0-3, background color - bits 4-6
430 * Return: status code
431 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100432static efi_status_t EFIAPI efi_cout_set_attribute(
433 struct efi_simple_text_output_protocol *this,
434 unsigned long attribute)
435{
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400436 unsigned int bold = EFI_ATTR_BOLD(attribute);
437 unsigned int fg = EFI_ATTR_FG(attribute);
438 unsigned int bg = EFI_ATTR_BG(attribute);
439
Alexander Grafc1311ad2016-03-04 01:10:00 +0100440 EFI_ENTRY("%p, %lx", this, attribute);
441
Heinrich Schuchardt3950f0f2019-06-14 07:16:57 +0200442 efi_con_mode.attribute = attribute;
Rob Clark2d5dc2a2017-10-10 08:23:01 -0400443 if (attribute)
444 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
445 else
446 printf(ESC"[0;37;40m");
447
448 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100449}
450
Heinrich Schuchardtb0ad9b52019-12-22 07:15:55 +0000451/**
452 * efi_cout_clear_screen() - clear screen
453 *
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200454 * This function implements the ClearScreen service of the simple text output
455 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
456 * for details.
Heinrich Schuchardtb0ad9b52019-12-22 07:15:55 +0000457 *
458 * @this: pointer to the protocol instance
459 * Return: status code
460 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100461static efi_status_t EFIAPI efi_cout_clear_screen(
462 struct efi_simple_text_output_protocol *this)
463{
464 EFI_ENTRY("%p", this);
465
Heinrich Schuchardtb0ad9b52019-12-22 07:15:55 +0000466 /*
467 * The Linux console wants both a clear and a home command. The video
468 * uclass does not support <ESC>[H without coordinates, yet.
469 */
470 printf(ESC "[2J" ESC "[1;1H");
Heinrich Schuchardte67ff942018-07-05 08:18:00 +0200471 efi_con_mode.cursor_column = 0;
472 efi_con_mode.cursor_row = 0;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100473
474 return EFI_EXIT(EFI_SUCCESS);
475}
476
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200477/**
478 * efi_cout_clear_set_mode() - set text model
479 *
480 * This function implements the SetMode service of the simple text output
481 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
482 * for details.
483 *
484 * @this: pointer to the protocol instance
485 * @mode_number: number of the text mode to set
486 * Return: status code
487 */
Heinrich Schuchardt2ad238f2019-06-14 07:20:51 +0200488static efi_status_t EFIAPI efi_cout_set_mode(
489 struct efi_simple_text_output_protocol *this,
490 unsigned long mode_number)
491{
492 EFI_ENTRY("%p, %ld", this, mode_number);
493
494 if (mode_number >= efi_con_mode.max_mode)
495 return EFI_EXIT(EFI_UNSUPPORTED);
Heinrich Schuchardt03446982019-09-04 22:46:13 +0200496
497 if (!efi_cout_modes[mode_number].present)
498 return EFI_EXIT(EFI_UNSUPPORTED);
499
Heinrich Schuchardt2ad238f2019-06-14 07:20:51 +0200500 efi_con_mode.mode = mode_number;
501 EFI_CALL(efi_cout_clear_screen(this));
502
503 return EFI_EXIT(EFI_SUCCESS);
504}
505
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200506/**
507 * efi_cout_reset() - reset the terminal
508 *
509 * This function implements the Reset service of the simple text output
510 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
511 * for details.
512 *
513 * @this: pointer to the protocol instance
514 * @extended_verification: if set an extended verification may be executed
515 * Return: status code
516 */
Heinrich Schuchardt9d12daf2018-07-05 19:58:07 +0200517static efi_status_t EFIAPI efi_cout_reset(
518 struct efi_simple_text_output_protocol *this,
519 char extended_verification)
520{
521 EFI_ENTRY("%p, %d", this, extended_verification);
522
523 /* Clear screen */
524 EFI_CALL(efi_cout_clear_screen(this));
525 /* Set default colors */
Heinrich Schuchardt3950f0f2019-06-14 07:16:57 +0200526 efi_con_mode.attribute = 0x07;
Heinrich Schuchardt9d12daf2018-07-05 19:58:07 +0200527 printf(ESC "[0;37;40m");
528
529 return EFI_EXIT(EFI_SUCCESS);
530}
531
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200532/**
533 * efi_cout_set_cursor_position() - reset the terminal
534 *
535 * This function implements the SetCursorPosition service of the simple text
536 * output protocol. See the Unified Extensible Firmware Interface (UEFI)
537 * specification for details.
538 *
539 * @this: pointer to the protocol instance
540 * @column: column to move to
541 * @row: row to move to
542 * Return: status code
543 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100544static efi_status_t EFIAPI efi_cout_set_cursor_position(
545 struct efi_simple_text_output_protocol *this,
546 unsigned long column, unsigned long row)
547{
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200548 efi_status_t ret = EFI_SUCCESS;
549 struct simple_text_output_mode *con = &efi_con_mode;
550 struct cout_mode *mode = &efi_cout_modes[con->mode];
551
Alexander Grafc1311ad2016-03-04 01:10:00 +0100552 EFI_ENTRY("%p, %ld, %ld", this, column, row);
553
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200554 /* Check parameters */
555 if (!this) {
556 ret = EFI_INVALID_PARAMETER;
557 goto out;
558 }
559 if (row >= mode->rows || column >= mode->columns) {
560 ret = EFI_UNSUPPORTED;
561 goto out;
562 }
563
564 /*
565 * Set cursor position by sending CSI H.
566 * EFI origin is [0, 0], terminal origin is [1, 1].
567 */
568 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100569 efi_con_mode.cursor_column = column;
570 efi_con_mode.cursor_row = row;
Heinrich Schuchardtaaace4b2018-09-14 18:49:26 +0200571out:
572 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100573}
574
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200575/**
576 * efi_cout_enable_cursor() - enable the cursor
577 *
578 * This function implements the EnableCursor service of the simple text output
579 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
580 * for details.
581 *
582 * @this: pointer to the protocol instance
583 * @enable: if true enable, if false disable the cursor
584 * Return: status code
585 */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100586static efi_status_t EFIAPI efi_cout_enable_cursor(
587 struct efi_simple_text_output_protocol *this,
588 bool enable)
589{
590 EFI_ENTRY("%p, %d", this, enable);
591
592 printf(ESC"[?25%c", enable ? 'h' : 'l');
Heinrich Schuchardt25e6fb52019-06-02 22:54:28 +0200593 efi_con_mode.cursor_visible = !!enable;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100594
595 return EFI_EXIT(EFI_SUCCESS);
596}
597
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +0200598struct efi_simple_text_output_protocol efi_con_out = {
Alexander Grafc1311ad2016-03-04 01:10:00 +0100599 .reset = efi_cout_reset,
600 .output_string = efi_cout_output_string,
601 .test_string = efi_cout_test_string,
602 .query_mode = efi_cout_query_mode,
603 .set_mode = efi_cout_set_mode,
604 .set_attribute = efi_cout_set_attribute,
605 .clear_screen = efi_cout_clear_screen,
606 .set_cursor_position = efi_cout_set_cursor_position,
607 .enable_cursor = efi_cout_enable_cursor,
608 .mode = (void*)&efi_con_mode,
609};
610
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200611/**
612 * struct efi_cin_notify_function - registered console input notify function
613 *
614 * @link: link to list
Heinrich Schuchardtfe1a81c2019-09-05 20:37:13 +0200615 * @key: key to notify
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200616 * @function: function to call
617 */
618struct efi_cin_notify_function {
619 struct list_head link;
620 struct efi_key_data key;
621 efi_status_t (EFIAPI *function)
622 (struct efi_key_data *key_data);
623};
624
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200625static bool key_available;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200626static struct efi_key_data next_key;
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200627static LIST_HEAD(cin_notify_functions);
Heinrich Schuchardt4f187892018-07-05 08:17:59 +0200628
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200629/**
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200630 * set_shift_mask() - set shift mask
631 *
632 * @mod: Xterm shift mask
Heinrich Schuchardtfe1a81c2019-09-05 20:37:13 +0200633 * @key_state: receives the state of the shift, alt, control, and logo keys
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200634 */
635void set_shift_mask(int mod, struct efi_key_state *key_state)
636{
637 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
638 if (mod) {
639 --mod;
640 if (mod & 1)
641 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
642 if (mod & 2)
643 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
644 if (mod & 4)
645 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
Heinrich Schuchardt3b435c12019-06-16 22:33:20 +0200646 if (!mod || (mod & 8))
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200647 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200648 }
649}
650
651/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200652 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200653 *
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100654 * This gets called when we have already parsed CSI.
655 *
Heinrich Schuchardtfe1a81c2019-09-05 20:37:13 +0200656 * @key_state: receives the state of the shift, alt, control, and logo keys
Heinrich Schuchardt325567d2020-06-04 18:40:44 +0200657 * Return: the unmodified code
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100658 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200659static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100660{
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200661 int c, mod = 0, ret = 0;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100662
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200663 c = getchar();
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100664
665 if (c != ';') {
666 ret = c;
667 if (c == '~')
668 goto out;
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200669 c = getchar();
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100670 }
671 for (;;) {
672 switch (c) {
673 case '0'...'9':
674 mod *= 10;
675 mod += c - '0';
676 /* fall through */
677 case ';':
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200678 c = getchar();
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100679 break;
680 default:
681 goto out;
682 }
683 }
684out:
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200685 set_shift_mask(mod, key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100686 if (!ret)
687 ret = c;
688 return ret;
689}
690
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200691/**
692 * efi_cin_read_key() - read a key from the console input
693 *
694 * @key: - key received
695 * Return: - status code
696 */
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200697static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Grafc1311ad2016-03-04 01:10:00 +0100698{
699 struct efi_input_key pressed_key = {
700 .scan_code = 0,
701 .unicode_char = 0,
702 };
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200703 s32 ch;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100704
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200705 if (console_read_unicode(&ch))
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200706 return EFI_NOT_READY;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200707
708 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
709 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
710
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200711 /* We do not support multi-word codes */
712 if (ch >= 0x10000)
713 ch = '?';
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200714
715 switch (ch) {
716 case 0x1b:
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100717 /*
Heinrich Schuchardt9abb01a2020-12-27 14:47:50 +0100718 * If a second key is received within 10 ms, assume that we are
719 * dealing with an escape sequence. Otherwise consider this the
720 * escape key being hit. 10 ms is long enough to work fine at
721 * 1200 baud and above.
722 */
723 udelay(10000);
724 if (!tstc()) {
725 pressed_key.scan_code = 23;
726 break;
727 }
728 /*
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100729 * Xterm Control Sequences
730 * https://www.xfree86.org/4.8.0/ctlseqs.html
731 */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200732 ch = getchar();
Alexander Grafc1311ad2016-03-04 01:10:00 +0100733 switch (ch) {
734 case cESC: /* ESC */
735 pressed_key.scan_code = 23;
736 break;
Heinrich Schuchardt3b2b2de2019-06-16 21:41:13 +0200737 case 'O': /* F1 - F4, End */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200738 ch = getchar();
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200739 /* consider modifiers */
Heinrich Schuchardt3b2b2de2019-06-16 21:41:13 +0200740 if (ch == 'F') { /* End */
741 pressed_key.scan_code = 6;
742 break;
743 } else if (ch < 'P') {
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200744 set_shift_mask(ch - '0', &key->key_state);
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200745 ch = getchar();
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200746 }
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100747 pressed_key.scan_code = ch - 'P' + 11;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100748 break;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100749 case '[':
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200750 ch = getchar();
Alexander Grafc1311ad2016-03-04 01:10:00 +0100751 switch (ch) {
752 case 'A'...'D': /* up, down right, left */
753 pressed_key.scan_code = ch - 'A' + 1;
754 break;
755 case 'F': /* End */
756 pressed_key.scan_code = 6;
757 break;
758 case 'H': /* Home */
759 pressed_key.scan_code = 5;
760 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100761 case '1':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200762 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100763 switch (ch) {
764 case '1'...'5': /* F1 - F5 */
765 pressed_key.scan_code = ch - '1' + 11;
766 break;
Heinrich Schuchardt3b2b2de2019-06-16 21:41:13 +0200767 case '6'...'9': /* F5 - F8 */
768 pressed_key.scan_code = ch - '6' + 15;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100769 break;
770 case 'A'...'D': /* up, down right, left */
771 pressed_key.scan_code = ch - 'A' + 1;
772 break;
Heinrich Schuchardt3b2b2de2019-06-16 21:41:13 +0200773 case 'F': /* End */
774 pressed_key.scan_code = 6;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100775 break;
Heinrich Schuchardt3b2b2de2019-06-16 21:41:13 +0200776 case 'H': /* Home */
777 pressed_key.scan_code = 5;
778 break;
779 case '~': /* Home */
780 pressed_key.scan_code = 5;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100781 break;
782 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100783 break;
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100784 case '2':
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200785 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100786 switch (ch) {
787 case '0'...'1': /* F9 - F10 */
788 pressed_key.scan_code = ch - '0' + 19;
789 break;
790 case '3'...'4': /* F11 - F12 */
791 pressed_key.scan_code = ch - '3' + 21;
792 break;
793 case '~': /* INS */
794 pressed_key.scan_code = 7;
795 break;
796 }
Alexander Grafc1311ad2016-03-04 01:10:00 +0100797 break;
798 case '3': /* DEL */
799 pressed_key.scan_code = 8;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200800 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100801 break;
802 case '5': /* PG UP */
803 pressed_key.scan_code = 9;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200804 analyze_modifiers(&key->key_state);
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100805 break;
806 case '6': /* PG DOWN */
807 pressed_key.scan_code = 10;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200808 analyze_modifiers(&key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100809 break;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200810 } /* [ */
Alexander Grafc1311ad2016-03-04 01:10:00 +0100811 break;
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200812 default:
813 /* ALT key */
814 set_shift_mask(3, &key->key_state);
Alexander Grafc1311ad2016-03-04 01:10:00 +0100815 }
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200816 break;
817 case 0x7f:
Alexander Grafc1311ad2016-03-04 01:10:00 +0100818 /* Backspace */
819 ch = 0x08;
820 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200821 if (pressed_key.scan_code) {
822 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
823 } else {
Heinrich Schuchardt0fb41692018-02-19 18:53:29 +0100824 pressed_key.unicode_char = ch;
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200825
826 /*
827 * Assume left control key for control characters typically
828 * entered using the control key.
829 */
830 if (ch >= 0x01 && ch <= 0x1f) {
Heinrich Schuchardt55fbdf92018-09-11 22:38:09 +0200831 key->key_state.key_shift_state |=
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200832 EFI_SHIFT_STATE_VALID;
833 switch (ch) {
834 case 0x01 ... 0x07:
835 case 0x0b ... 0x0c:
836 case 0x0e ... 0x1f:
837 key->key_state.key_shift_state |=
838 EFI_LEFT_CONTROL_PRESSED;
839 }
840 }
841 }
842 key->key = pressed_key;
Alexander Grafc1311ad2016-03-04 01:10:00 +0100843
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200844 return EFI_SUCCESS;
845}
846
847/**
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200848 * efi_cin_notify() - notify registered functions
849 */
850static void efi_cin_notify(void)
851{
852 struct efi_cin_notify_function *item;
853
854 list_for_each_entry(item, &cin_notify_functions, link) {
855 bool match = true;
856
857 /* We do not support toggle states */
858 if (item->key.key.unicode_char || item->key.key.scan_code) {
859 if (item->key.key.unicode_char !=
860 next_key.key.unicode_char ||
861 item->key.key.scan_code != next_key.key.scan_code)
862 match = false;
863 }
864 if (item->key.key_state.key_shift_state &&
865 item->key.key_state.key_shift_state !=
866 next_key.key_state.key_shift_state)
867 match = false;
868
869 if (match)
870 /* We don't bother about the return code */
871 EFI_CALL(item->function(&next_key));
872 }
873}
874
875/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200876 * efi_cin_check() - check if keyboard input is available
877 */
878static void efi_cin_check(void)
879{
880 efi_status_t ret;
881
882 if (key_available) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200883 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200884 return;
885 }
886
887 if (tstc()) {
888 ret = efi_cin_read_key(&next_key);
889 if (ret == EFI_SUCCESS) {
890 key_available = true;
891
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200892 /* Notify registered functions */
893 efi_cin_notify();
894
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200895 /* Queue the wait for key event */
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +0200896 if (key_available)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200897 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +0200898 }
899 }
900}
901
902/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200903 * efi_cin_empty_buffer() - empty input buffer
904 */
905static void efi_cin_empty_buffer(void)
906{
907 while (tstc())
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200908 getchar();
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200909 key_available = false;
910}
911
912/**
913 * efi_cin_reset_ex() - reset console input
914 *
915 * @this: - the extended simple text input protocol
916 * @extended_verification: - extended verification
917 *
918 * This function implements the reset service of the
919 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
920 *
921 * See the Unified Extensible Firmware Interface (UEFI) specification for
922 * details.
923 *
924 * Return: old value of the task priority level
925 */
926static efi_status_t EFIAPI efi_cin_reset_ex(
927 struct efi_simple_text_input_ex_protocol *this,
928 bool extended_verification)
929{
930 efi_status_t ret = EFI_SUCCESS;
931
932 EFI_ENTRY("%p, %d", this, extended_verification);
933
934 /* Check parameters */
935 if (!this) {
936 ret = EFI_INVALID_PARAMETER;
937 goto out;
938 }
939
940 efi_cin_empty_buffer();
941out:
942 return EFI_EXIT(ret);
943}
944
945/**
946 * efi_cin_read_key_stroke_ex() - read key stroke
947 *
948 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
949 * @key_data: key read from console
950 * Return: status code
951 *
952 * This function implements the ReadKeyStrokeEx service of the
953 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
954 *
955 * See the Unified Extensible Firmware Interface (UEFI) specification for
956 * details.
957 */
958static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
959 struct efi_simple_text_input_ex_protocol *this,
960 struct efi_key_data *key_data)
961{
962 efi_status_t ret = EFI_SUCCESS;
963
964 EFI_ENTRY("%p, %p", this, key_data);
965
966 /* Check parameters */
967 if (!this || !key_data) {
968 ret = EFI_INVALID_PARAMETER;
969 goto out;
970 }
971
972 /* We don't do interrupts, so check for timers cooperatively */
973 efi_timer_check();
974
975 /* Enable console input after ExitBootServices */
976 efi_cin_check();
977
978 if (!key_available) {
979 ret = EFI_NOT_READY;
980 goto out;
981 }
Heinrich Schuchardtbfc2dd52019-04-06 20:59:24 +0200982 /*
983 * CTRL+A - CTRL+Z have to be signaled as a - z.
984 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
985 */
986 switch (next_key.key.unicode_char) {
987 case 0x01 ... 0x07:
988 case 0x0b ... 0x0c:
989 case 0x0e ... 0x1a:
990 if (!(next_key.key_state.key_toggle_state &
991 EFI_CAPS_LOCK_ACTIVE) ^
992 !(next_key.key_state.key_shift_state &
993 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
994 next_key.key.unicode_char += 0x40;
995 else
996 next_key.key.unicode_char += 0x60;
997 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +0200998 *key_data = next_key;
999 key_available = false;
1000 efi_con_in.wait_for_key->is_signaled = false;
Heinrich Schuchardtbfc2dd52019-04-06 20:59:24 +02001001
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001002out:
1003 return EFI_EXIT(ret);
1004}
1005
1006/**
1007 * efi_cin_set_state() - set toggle key state
1008 *
1009 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
Heinrich Schuchardtacee9652019-05-18 17:07:52 +02001010 * @key_toggle_state: pointer to key toggle state
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001011 * Return: status code
1012 *
1013 * This function implements the SetState service of the
1014 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1015 *
1016 * See the Unified Extensible Firmware Interface (UEFI) specification for
1017 * details.
1018 */
1019static efi_status_t EFIAPI efi_cin_set_state(
1020 struct efi_simple_text_input_ex_protocol *this,
Heinrich Schuchardtacee9652019-05-18 17:07:52 +02001021 u8 *key_toggle_state)
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001022{
Heinrich Schuchardtacee9652019-05-18 17:07:52 +02001023 EFI_ENTRY("%p, %p", this, key_toggle_state);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001024 /*
1025 * U-Boot supports multiple console input sources like serial and
1026 * net console for which a key toggle state cannot be set at all.
1027 *
1028 * According to the UEFI specification it is allowable to not implement
1029 * this service.
1030 */
1031 return EFI_EXIT(EFI_UNSUPPORTED);
1032}
1033
1034/**
1035 * efi_cin_register_key_notify() - register key notification function
1036 *
1037 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1038 * @key_data: key to be notified
1039 * @key_notify_function: function to be called if the key is pressed
1040 * @notify_handle: handle for unregistering the notification
1041 * Return: status code
1042 *
1043 * This function implements the SetState service of the
1044 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1045 *
1046 * See the Unified Extensible Firmware Interface (UEFI) specification for
1047 * details.
1048 */
1049static efi_status_t EFIAPI efi_cin_register_key_notify(
1050 struct efi_simple_text_input_ex_protocol *this,
1051 struct efi_key_data *key_data,
1052 efi_status_t (EFIAPI *key_notify_function)(
1053 struct efi_key_data *key_data),
1054 void **notify_handle)
1055{
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +02001056 efi_status_t ret = EFI_SUCCESS;
1057 struct efi_cin_notify_function *notify_function;
1058
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001059 EFI_ENTRY("%p, %p, %p, %p",
1060 this, key_data, key_notify_function, notify_handle);
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +02001061
1062 /* Check parameters */
1063 if (!this || !key_data || !key_notify_function || !notify_handle) {
1064 ret = EFI_INVALID_PARAMETER;
1065 goto out;
1066 }
1067
1068 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
1069 key_data->key.unicode_char,
1070 key_data->key.scan_code,
1071 key_data->key_state.key_shift_state,
1072 key_data->key_state.key_toggle_state);
1073
1074 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
1075 if (!notify_function) {
1076 ret = EFI_OUT_OF_RESOURCES;
1077 goto out;
1078 }
1079 notify_function->key = *key_data;
1080 notify_function->function = key_notify_function;
1081 list_add_tail(&notify_function->link, &cin_notify_functions);
1082 *notify_handle = notify_function;
1083out:
1084 return EFI_EXIT(ret);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001085}
1086
1087/**
1088 * efi_cin_unregister_key_notify() - unregister key notification function
1089 *
1090 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1091 * @notification_handle: handle received when registering
1092 * Return: status code
1093 *
1094 * This function implements the SetState service of the
1095 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1096 *
1097 * See the Unified Extensible Firmware Interface (UEFI) specification for
1098 * details.
1099 */
1100static efi_status_t EFIAPI efi_cin_unregister_key_notify(
1101 struct efi_simple_text_input_ex_protocol *this,
1102 void *notification_handle)
1103{
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +02001104 efi_status_t ret = EFI_INVALID_PARAMETER;
1105 struct efi_cin_notify_function *item, *notify_function =
1106 notification_handle;
1107
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001108 EFI_ENTRY("%p, %p", this, notification_handle);
Heinrich Schuchardt4fdcf062018-09-11 22:38:12 +02001109
1110 /* Check parameters */
1111 if (!this || !notification_handle)
1112 goto out;
1113
1114 list_for_each_entry(item, &cin_notify_functions, link) {
1115 if (item == notify_function) {
1116 ret = EFI_SUCCESS;
1117 break;
1118 }
1119 }
1120 if (ret != EFI_SUCCESS)
1121 goto out;
1122
1123 /* Remove the notify function */
1124 list_del(&notify_function->link);
1125 free(notify_function);
1126out:
1127 return EFI_EXIT(ret);
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001128}
1129
1130
1131/**
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001132 * efi_cin_reset() - drain the input buffer
1133 *
1134 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1135 * @extended_verification: allow for exhaustive verification
1136 * Return: status code
1137 *
1138 * This function implements the Reset service of the
1139 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1140 *
1141 * See the Unified Extensible Firmware Interface (UEFI) specification for
1142 * details.
1143 */
1144static efi_status_t EFIAPI efi_cin_reset
1145 (struct efi_simple_text_input_protocol *this,
1146 bool extended_verification)
1147{
1148 efi_status_t ret = EFI_SUCCESS;
1149
1150 EFI_ENTRY("%p, %d", this, extended_verification);
1151
1152 /* Check parameters */
1153 if (!this) {
1154 ret = EFI_INVALID_PARAMETER;
1155 goto out;
1156 }
1157
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001158 efi_cin_empty_buffer();
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001159out:
1160 return EFI_EXIT(ret);
1161}
1162
1163/**
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001164 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001165 *
1166 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1167 * @key: key read from console
1168 * Return: status code
1169 *
1170 * This function implements the ReadKeyStroke service of the
1171 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1172 *
1173 * See the Unified Extensible Firmware Interface (UEFI) specification for
1174 * details.
1175 */
1176static efi_status_t EFIAPI efi_cin_read_key_stroke
1177 (struct efi_simple_text_input_protocol *this,
1178 struct efi_input_key *key)
1179{
1180 efi_status_t ret = EFI_SUCCESS;
1181
1182 EFI_ENTRY("%p, %p", this, key);
1183
1184 /* Check parameters */
1185 if (!this || !key) {
1186 ret = EFI_INVALID_PARAMETER;
1187 goto out;
1188 }
1189
1190 /* We don't do interrupts, so check for timers cooperatively */
1191 efi_timer_check();
1192
1193 /* Enable console input after ExitBootServices */
1194 efi_cin_check();
1195
1196 if (!key_available) {
1197 ret = EFI_NOT_READY;
1198 goto out;
1199 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001200 *key = next_key.key;
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001201 key_available = false;
1202 efi_con_in.wait_for_key->is_signaled = false;
1203out:
1204 return EFI_EXIT(ret);
Alexander Grafc1311ad2016-03-04 01:10:00 +01001205}
1206
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001207static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1208 .reset = efi_cin_reset_ex,
1209 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1210 .wait_for_key_ex = NULL,
1211 .set_state = efi_cin_set_state,
1212 .register_key_notify = efi_cin_register_key_notify,
1213 .unregister_key_notify = efi_cin_unregister_key_notify,
1214};
1215
Heinrich Schuchardt3e603ec2018-09-08 10:20:10 +02001216struct efi_simple_text_input_protocol efi_con_in = {
Alexander Grafc1311ad2016-03-04 01:10:00 +01001217 .reset = efi_cin_reset,
1218 .read_key_stroke = efi_cin_read_key_stroke,
1219 .wait_for_key = NULL,
1220};
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001221
1222static struct efi_event *console_timer_event;
1223
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001224/*
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001225 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001226 *
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001227 * @event: console timer event
1228 * @context: not used
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +01001229 */
xypron.glpk@gmx.deff925932017-07-20 05:26:07 +02001230static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1231 void *context)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001232{
1233 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001234 efi_cin_check();
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001235 EFI_EXIT(EFI_SUCCESS);
1236}
1237
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001238/**
1239 * efi_key_notify() - notify the wait for key event
1240 *
1241 * @event: wait for key event
1242 * @context: not used
1243 */
1244static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1245{
1246 EFI_ENTRY("%p, %p", event, context);
1247 efi_cin_check();
1248 EFI_EXIT(EFI_SUCCESS);
1249}
1250
1251/**
1252 * efi_console_register() - install the console protocols
1253 *
1254 * This function is called from do_bootefi_exec().
Heinrich Schuchardt6f566c22018-10-02 06:08:26 +02001255 *
1256 * Return: status code
Heinrich Schuchardt0dfd13a2018-09-11 22:38:05 +02001257 */
Heinrich Schuchardt6f566c22018-10-02 06:08:26 +02001258efi_status_t efi_console_register(void)
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001259{
1260 efi_status_t r;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001261 efi_handle_t console_output_handle;
1262 efi_handle_t console_input_handle;
Rob Clarka17e62c2017-07-24 10:39:01 -04001263
Heinrich Schuchardta4aa7be2018-04-29 20:02:46 +02001264 /* Set up mode information */
1265 query_console_size();
1266
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001267 /* Create handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001268 r = efi_create_handle(&console_output_handle);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001269 if (r != EFI_SUCCESS)
1270 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +02001271
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001272 r = efi_add_protocol(console_output_handle,
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001273 &efi_guid_text_output_protocol, &efi_con_out);
1274 if (r != EFI_SUCCESS)
1275 goto out_of_memory;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001276 systab.con_out_handle = console_output_handle;
1277 systab.stderr_handle = console_output_handle;
Alexander Graf40e3e752018-09-04 14:59:11 +02001278
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001279 r = efi_create_handle(&console_input_handle);
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001280 if (r != EFI_SUCCESS)
1281 goto out_of_memory;
Alexander Graf40e3e752018-09-04 14:59:11 +02001282
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001283 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001284 &efi_guid_text_input_protocol, &efi_con_in);
1285 if (r != EFI_SUCCESS)
1286 goto out_of_memory;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001287 systab.con_in_handle = console_input_handle;
1288 r = efi_add_protocol(console_input_handle,
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001289 &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
1290 if (r != EFI_SUCCESS)
1291 goto out_of_memory;
Rob Clarka17e62c2017-07-24 10:39:01 -04001292
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001293 /* Create console events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001294 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1295 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001296 if (r != EFI_SUCCESS) {
1297 printf("ERROR: Failed to register WaitForKey event\n");
1298 return r;
1299 }
Heinrich Schuchardt110c6282018-09-11 22:38:08 +02001300 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001301 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001302 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001303 &console_timer_event);
1304 if (r != EFI_SUCCESS) {
1305 printf("ERROR: Failed to register console event\n");
1306 return r;
1307 }
1308 /* 5000 ns cycle is sufficient for 2 MBaud */
1309 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1310 if (r != EFI_SUCCESS)
1311 printf("ERROR: Failed to set console timer\n");
1312 return r;
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001313out_of_memory:
Heinrich Schuchardt14d103b2018-09-08 19:57:24 +02001314 printf("ERROR: Out of memory\n");
Heinrich Schuchardtebb4dd52017-10-26 19:25:59 +02001315 return r;
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +02001316}