blob: 43176309077caa78d6158f463a4a98f29f4f146f [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
Heinrich Schuchardt761908e2022-06-14 08:02:03 +02008#define LOG_CATEGORY LOGC_EFI
9
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +090010#include <ansi.h>
Alexander Graf4aacacc2016-03-04 01:10:00 +010011#include <common.h>
Rob Clark0d138cf2017-09-09 06:47:40 -040012#include <charset.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070014#include <time.h>
Rob Clark3863b712017-09-13 18:05:43 -040015#include <dm/device.h>
Alexander Graf4aacacc2016-03-04 01:10:00 +010016#include <efi_loader.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060017#include <env.h>
Heinrich Schuchardt761908e2022-06-14 08:02:03 +020018#include <log.h>
Rob Clark3863b712017-09-13 18:05:43 -040019#include <stdio_dev.h>
20#include <video_console.h>
Heinrich Schuchardte5c93172020-12-27 14:47:50 +010021#include <linux/delay.h>
Alexander Graf4aacacc2016-03-04 01:10:00 +010022
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010023#define EFI_COUT_MODE_2 2
24#define EFI_MAX_COUT_MODE 3
25
26struct cout_mode {
27 unsigned long columns;
28 unsigned long rows;
29 int present;
30};
31
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +010032__maybe_unused static struct efi_object uart_obj;
33
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010034static struct cout_mode efi_cout_modes[] = {
35 /* EFI Mode 0 is 80x25 and always present */
36 {
37 .columns = 80,
38 .rows = 25,
39 .present = 1,
40 },
41 /* EFI Mode 1 is always 80x50 */
42 {
43 .columns = 80,
44 .rows = 50,
45 .present = 0,
46 },
47 /* Value are unknown until we query the console */
48 {
49 .columns = 0,
50 .rows = 0,
51 .present = 0,
52 },
53};
54
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +020055const efi_guid_t efi_guid_text_input_ex_protocol =
56 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +020057const efi_guid_t efi_guid_text_input_protocol =
58 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +020059const efi_guid_t efi_guid_text_output_protocol =
60 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
Alexander Graf4aacacc2016-03-04 01:10:00 +010061
62#define cESC '\x1b'
63#define ESC "\x1b"
64
Heinrich Schuchardt761908e2022-06-14 08:02:03 +020065/*
66 * efi_con_mode - mode information of the Simple Text Output Protocol
67 *
68 * Use safe settings before efi_setup_console_size() is called.
69 * By default enable only the 80x25 mode which must always exist.
70 */
Alexander Graf4aacacc2016-03-04 01:10:00 +010071static struct simple_text_output_mode efi_con_mode = {
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010072 .max_mode = 1,
Alexander Graf4aacacc2016-03-04 01:10:00 +010073 .mode = 0,
74 .attribute = 0,
75 .cursor_column = 0,
76 .cursor_row = 0,
77 .cursor_visible = 1,
78};
79
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010080static int term_get_char(s32 *c)
81{
82 u64 timeout;
83
84 /* Wait up to 100 ms for a character */
85 timeout = timer_get_us() + 100000;
86
87 while (!tstc())
88 if (timer_get_us() > timeout)
89 return 1;
90
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +020091 *c = getchar();
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010092 return 0;
93}
94
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +020095/**
Heinrich Schuchardt77135c22018-05-16 18:17:38 +020096 * Receive and parse a reply from the terminal.
97 *
98 * @n: array of return values
99 * @num: number of return values expected
100 * @end_char: character indicating end of terminal message
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200101 * Return: non-zero indicates error
Heinrich Schuchardt77135c22018-05-16 18:17:38 +0200102 */
103static int term_read_reply(int *n, int num, char end_char)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100104{
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100105 s32 c;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100106 int i = 0;
107
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100108 if (term_get_char(&c) || c != cESC)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100109 return -1;
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100110
111 if (term_get_char(&c) || c != '[')
Alexander Graf4aacacc2016-03-04 01:10:00 +0100112 return -1;
113
114 n[0] = 0;
115 while (1) {
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100116 if (!term_get_char(&c)) {
117 if (c == ';') {
118 i++;
119 if (i >= num)
120 return -1;
121 n[i] = 0;
122 continue;
123 } else if (c == end_char) {
124 break;
125 } else if (c > '9' || c < '0') {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100126 return -1;
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100127 }
128
129 /* Read one more decimal position */
130 n[i] *= 10;
131 n[i] += c - '0';
132 } else {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100133 return -1;
134 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100135 }
Heinrich Schuchardt77135c22018-05-16 18:17:38 +0200136 if (i != num - 1)
137 return -1;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100138
139 return 0;
140}
141
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200142/**
143 * efi_cout_output_string() - write Unicode string to console
144 *
145 * This function implements the OutputString service of the simple text output
146 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
147 * for details.
148 *
149 * @this: simple text output protocol
150 * @string: u16 string
151 * Return: status code
152 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100153static efi_status_t EFIAPI efi_cout_output_string(
154 struct efi_simple_text_output_protocol *this,
Heinrich Schuchardt84494282021-01-05 07:50:09 +0100155 const u16 *string)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100156{
Rob Clark1ef185d2017-09-13 18:05:44 -0400157 struct simple_text_output_mode *con = &efi_con_mode;
158 struct cout_mode *mode = &efi_cout_modes[con->mode];
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200159 char *buf, *pos;
Heinrich Schuchardt84494282021-01-05 07:50:09 +0100160 const u16 *p;
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200161 efi_status_t ret = EFI_SUCCESS;
Rob Clark1ef185d2017-09-13 18:05:44 -0400162
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200163 EFI_ENTRY("%p, %p", this, string);
Rob Clark1ef185d2017-09-13 18:05:44 -0400164
Heinrich Schuchardte60b2982019-05-18 18:11:54 +0200165 if (!this || !string) {
166 ret = EFI_INVALID_PARAMETER;
167 goto out;
168 }
169
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200170 buf = malloc(utf16_utf8_strlen(string) + 1);
171 if (!buf) {
172 ret = EFI_OUT_OF_RESOURCES;
173 goto out;
174 }
175 pos = buf;
176 utf16_utf8_strcpy(&pos, string);
Rob Clark1ef185d2017-09-13 18:05:44 -0400177 fputs(stdout, buf);
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200178 free(buf);
Rob Clark1ef185d2017-09-13 18:05:44 -0400179
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200180 /*
181 * Update the cursor position.
182 *
183 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200184 * and U000D. All other control characters are ignored. Any non-control
185 * character increase the column by one.
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200186 */
187 for (p = string; *p; ++p) {
Rob Clark1ef185d2017-09-13 18:05:44 -0400188 switch (*p) {
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200189 case '\b': /* U+0008, backspace */
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200190 if (con->cursor_column)
191 con->cursor_column--;
Rob Clark1ef185d2017-09-13 18:05:44 -0400192 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200193 case '\n': /* U+000A, newline */
Rob Clark1ef185d2017-09-13 18:05:44 -0400194 con->cursor_column = 0;
195 con->cursor_row++;
196 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200197 case '\r': /* U+000D, carriage-return */
198 con->cursor_column = 0;
Rob Clark1ef185d2017-09-13 18:05:44 -0400199 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200200 case 0xd800 ... 0xdbff:
201 /*
202 * Ignore high surrogates, we do not want to count a
203 * Unicode character twice.
204 */
Rob Clark1ef185d2017-09-13 18:05:44 -0400205 break;
206 default:
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200207 /* Exclude control codes */
208 if (*p > 0x1f)
209 con->cursor_column++;
Rob Clark1ef185d2017-09-13 18:05:44 -0400210 break;
211 }
212 if (con->cursor_column >= mode->columns) {
213 con->cursor_column = 0;
214 con->cursor_row++;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100215 }
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200216 /*
217 * When we exceed the row count the terminal will scroll up one
218 * line. We have to adjust the cursor position.
219 */
220 if (con->cursor_row >= mode->rows && con->cursor_row)
221 con->cursor_row--;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100222 }
223
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200224out:
225 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100226}
227
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200228/**
229 * efi_cout_test_string() - test writing Unicode string to console
230 *
231 * This function implements the TestString service of the simple text output
232 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
233 * for details.
234 *
235 * As in OutputString we simply convert UTF-16 to UTF-8 there are no unsupported
236 * code points and we can always return EFI_SUCCESS.
237 *
238 * @this: simple text output protocol
239 * @string: u16 string
240 * Return: status code
241 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100242static efi_status_t EFIAPI efi_cout_test_string(
243 struct efi_simple_text_output_protocol *this,
Heinrich Schuchardt84494282021-01-05 07:50:09 +0100244 const u16 *string)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100245{
246 EFI_ENTRY("%p, %p", this, string);
247 return EFI_EXIT(EFI_SUCCESS);
248}
249
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200250/**
251 * cout_mode_matches() - check if mode has given terminal size
252 *
253 * @mode: text mode
254 * @rows: number of rows
255 * @cols: number of columns
256 * Return: true if number of rows and columns matches the mode and
257 * the mode is present
258 */
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100259static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
260{
261 if (!mode->present)
262 return false;
263
264 return (mode->rows == rows) && (mode->columns == cols);
265}
266
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200267/**
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100268 * query_console_serial() - query serial console size
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200269 *
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200270 * When using a serial console or the net console we can only devise the
271 * terminal size by querying the terminal using ECMA-48 control sequences.
272 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200273 * @rows: pointer to return number of rows
274 * @cols: pointer to return number of columns
275 * Returns: 0 on success
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200276 */
Rob Clark1e948922017-09-13 18:05:42 -0400277static int query_console_serial(int *rows, int *cols)
278{
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200279 int ret = 0;
280 int n[2];
Rob Clark1e948922017-09-13 18:05:42 -0400281
282 /* Empty input buffer */
283 while (tstc())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200284 getchar();
Rob Clark1e948922017-09-13 18:05:42 -0400285
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200286 /*
287 * Not all terminals understand CSI [18t for querying the console size.
288 * We should adhere to escape sequences documented in the console_codes
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200289 * man page and the ECMA-48 standard.
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200290 *
291 * So here we follow a different approach. We position the cursor to the
292 * bottom right and query its position. Before leaving the function we
293 * restore the original cursor position.
294 */
295 printf(ESC "7" /* Save cursor position */
296 ESC "[r" /* Set scrolling region to full window */
297 ESC "[999;999H" /* Move to bottom right corner */
298 ESC "[6n"); /* Query cursor position */
Rob Clark1e948922017-09-13 18:05:42 -0400299
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200300 /* Read {rows,cols} */
301 if (term_read_reply(n, 2, 'R')) {
302 ret = 1;
303 goto out;
304 }
Rob Clark1e948922017-09-13 18:05:42 -0400305
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200306 *cols = n[1];
307 *rows = n[0];
308out:
309 printf(ESC "8"); /* Restore cursor position */
310 return ret;
Rob Clark1e948922017-09-13 18:05:42 -0400311}
312
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200313/**
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100314 * query_vidconsole() - query video console size
315 *
316 *
317 * @rows: pointer to return number of rows
318 * @cols: pointer to return number of columns
319 * Returns: 0 on success
320 */
321static int __maybe_unused query_vidconsole(int *rows, int *cols)
322{
323 const char *stdout_name = env_get("stdout");
324 struct stdio_dev *stdout_dev;
325 struct udevice *dev;
326 struct vidconsole_priv *priv;
327
328 if (!stdout_name || strncmp(stdout_name, "vidconsole", 10))
329 return -ENODEV;
330 stdout_dev = stdio_get_by_name("vidconsole");
331 if (!stdout_dev)
332 return -ENODEV;
333 dev = stdout_dev->priv;
334 if (!dev)
335 return -ENODEV;
336 priv = dev_get_uclass_priv(dev);
337 if (!priv)
338 return -ENODEV;
339 *rows = priv->rows;
340 *cols = priv->cols;
341 return 0;
342}
343
344/**
Heinrich Schuchardt761908e2022-06-14 08:02:03 +0200345 * efi_setup_console_size() - update the mode table.
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200346 *
347 * By default the only mode available is 80x25. If the console has at least 50
348 * lines, enable mode 80x50. If we can query the console size and it is neither
349 * 80x25 nor 80x50, set it as an additional mode.
350 */
Heinrich Schuchardt761908e2022-06-14 08:02:03 +0200351void efi_setup_console_size(void)
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200352{
Alexander Graf28795322018-06-03 15:51:17 +0200353 int rows = 25, cols = 80;
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100354 int ret = -ENODEV;
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200355
Simon Glass52cb5042022-10-18 07:46:31 -0600356 if (IS_ENABLED(CONFIG_VIDEO))
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100357 ret = query_vidconsole(&rows, &cols);
358 if (ret)
359 ret = query_console_serial(&rows, &cols);
360 if (ret)
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200361 return;
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200362
Heinrich Schuchardt761908e2022-06-14 08:02:03 +0200363 log_debug("Console size %dx%d\n", rows, cols);
364
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200365 /* Test if we can have Mode 1 */
366 if (cols >= 80 && rows >= 50) {
367 efi_cout_modes[1].present = 1;
368 efi_con_mode.max_mode = 2;
369 }
370
371 /*
372 * Install our mode as mode 2 if it is different
373 * than mode 0 or 1 and set it as the currently selected mode
374 */
375 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
376 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
377 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
378 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
379 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
380 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
381 efi_con_mode.mode = EFI_COUT_MODE_2;
382 }
383}
384
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200385/**
386 * efi_cout_query_mode() - get terminal size for a text mode
387 *
388 * This function implements the QueryMode service of the simple text output
389 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
390 * for details.
391 *
392 * @this: simple text output protocol
393 * @mode_number: mode number to retrieve information on
394 * @columns: number of columns
395 * @rows: number of rows
396 * Return: status code
397 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100398static efi_status_t EFIAPI efi_cout_query_mode(
399 struct efi_simple_text_output_protocol *this,
400 unsigned long mode_number, unsigned long *columns,
401 unsigned long *rows)
402{
403 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
404
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100405 if (mode_number >= efi_con_mode.max_mode)
406 return EFI_EXIT(EFI_UNSUPPORTED);
407
408 if (efi_cout_modes[mode_number].present != 1)
409 return EFI_EXIT(EFI_UNSUPPORTED);
410
Alexander Graf4aacacc2016-03-04 01:10:00 +0100411 if (columns)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100412 *columns = efi_cout_modes[mode_number].columns;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100413 if (rows)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100414 *rows = efi_cout_modes[mode_number].rows;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100415
416 return EFI_EXIT(EFI_SUCCESS);
417}
418
Rob Clark87ef0012017-10-10 08:23:01 -0400419static const struct {
420 unsigned int fg;
421 unsigned int bg;
422} color[] = {
423 { 30, 40 }, /* 0: black */
424 { 34, 44 }, /* 1: blue */
425 { 32, 42 }, /* 2: green */
426 { 36, 46 }, /* 3: cyan */
427 { 31, 41 }, /* 4: red */
428 { 35, 45 }, /* 5: magenta */
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +0200429 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
430 { 37, 47 }, /* 7: light gray, map to white */
Rob Clark87ef0012017-10-10 08:23:01 -0400431};
432
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200433/**
434 * efi_cout_set_attribute() - set fore- and background color
435 *
436 * This function implements the SetAttribute service of the simple text output
437 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
438 * for details.
439 *
440 * @this: simple text output protocol
441 * @attribute: foreground color - bits 0-3, background color - bits 4-6
442 * Return: status code
443 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100444static efi_status_t EFIAPI efi_cout_set_attribute(
445 struct efi_simple_text_output_protocol *this,
446 unsigned long attribute)
447{
Rob Clark87ef0012017-10-10 08:23:01 -0400448 unsigned int bold = EFI_ATTR_BOLD(attribute);
449 unsigned int fg = EFI_ATTR_FG(attribute);
450 unsigned int bg = EFI_ATTR_BG(attribute);
451
Alexander Graf4aacacc2016-03-04 01:10:00 +0100452 EFI_ENTRY("%p, %lx", this, attribute);
453
Heinrich Schuchardt1e129622019-06-14 07:16:57 +0200454 efi_con_mode.attribute = attribute;
Rob Clark87ef0012017-10-10 08:23:01 -0400455 if (attribute)
456 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
457 else
458 printf(ESC"[0;37;40m");
459
460 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100461}
462
Heinrich Schuchardt7995cbe2019-12-22 07:15:55 +0000463/**
Jan Kiszkad4d0fec2023-01-18 22:24:59 +0100464 * efi_clear_screen() - clear screen
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200465 */
466static void efi_clear_screen(void)
467{
Jan Kiszkad4d0fec2023-01-18 22:24:59 +0100468 if (CONFIG_IS_ENABLED(EFI_SCROLL_ON_CLEAR_SCREEN)) {
469 unsigned int row, screen_rows, screen_columns;
470
471 /* Avoid overwriting previous outputs on streaming consoles */
472 screen_rows = efi_cout_modes[efi_con_mode.mode].rows;
473 screen_columns = efi_cout_modes[efi_con_mode.mode].columns;
474 printf(ESC "[%u;%uH", screen_rows, screen_columns);
475 for (row = 1; row < screen_rows; row++)
476 printf("\n");
477 }
478
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200479 /*
480 * The Linux console wants both a clear and a home command. The video
481 * uclass does not support <ESC>[H without coordinates, yet.
482 */
483 printf(ESC "[2J" ESC "[1;1H");
484 efi_con_mode.cursor_column = 0;
485 efi_con_mode.cursor_row = 0;
486}
487
488/**
489 * efi_cout_clear_screen() - clear screen
Heinrich Schuchardt7995cbe2019-12-22 07:15:55 +0000490 *
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200491 * This function implements the ClearScreen service of the simple text output
492 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
493 * for details.
Heinrich Schuchardt7995cbe2019-12-22 07:15:55 +0000494 *
495 * @this: pointer to the protocol instance
496 * Return: status code
497 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100498static efi_status_t EFIAPI efi_cout_clear_screen(
499 struct efi_simple_text_output_protocol *this)
500{
501 EFI_ENTRY("%p", this);
502
Jan Kiszkafb181e22023-01-18 22:25:00 +0100503 /* Set default colors if not done yet */
504 if (efi_con_mode.attribute == 0) {
505 efi_con_mode.attribute = 0x07;
506 printf(ESC "[0;37;40m");
507 }
508
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200509 efi_clear_screen();
Alexander Graf4aacacc2016-03-04 01:10:00 +0100510
511 return EFI_EXIT(EFI_SUCCESS);
512}
513
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200514/**
515 * efi_cout_clear_set_mode() - set text model
516 *
517 * This function implements the SetMode service of the simple text output
518 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
519 * for details.
520 *
521 * @this: pointer to the protocol instance
522 * @mode_number: number of the text mode to set
523 * Return: status code
524 */
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200525static efi_status_t EFIAPI efi_cout_set_mode(
526 struct efi_simple_text_output_protocol *this,
527 unsigned long mode_number)
528{
529 EFI_ENTRY("%p, %ld", this, mode_number);
530
531 if (mode_number >= efi_con_mode.max_mode)
532 return EFI_EXIT(EFI_UNSUPPORTED);
Heinrich Schuchardt6a6afa72019-09-04 22:46:13 +0200533
534 if (!efi_cout_modes[mode_number].present)
535 return EFI_EXIT(EFI_UNSUPPORTED);
536
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200537 efi_con_mode.mode = mode_number;
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200538 efi_clear_screen();
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200539
540 return EFI_EXIT(EFI_SUCCESS);
541}
542
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200543/**
544 * efi_cout_reset() - reset the terminal
545 *
546 * This function implements the Reset service of the simple text output
547 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
548 * for details.
549 *
550 * @this: pointer to the protocol instance
551 * @extended_verification: if set an extended verification may be executed
552 * Return: status code
553 */
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200554static efi_status_t EFIAPI efi_cout_reset(
555 struct efi_simple_text_output_protocol *this,
556 char extended_verification)
557{
558 EFI_ENTRY("%p, %d", this, extended_verification);
559
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200560 /* Set default colors */
Heinrich Schuchardt1e129622019-06-14 07:16:57 +0200561 efi_con_mode.attribute = 0x07;
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200562 printf(ESC "[0;37;40m");
Heinrich Schuchardt30c9d672022-04-30 09:05:04 +0200563 /* Clear screen */
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200564 efi_clear_screen();
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200565
566 return EFI_EXIT(EFI_SUCCESS);
567}
568
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200569/**
570 * efi_cout_set_cursor_position() - reset the terminal
571 *
572 * This function implements the SetCursorPosition service of the simple text
573 * output protocol. See the Unified Extensible Firmware Interface (UEFI)
574 * specification for details.
575 *
576 * @this: pointer to the protocol instance
577 * @column: column to move to
578 * @row: row to move to
579 * Return: status code
580 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100581static efi_status_t EFIAPI efi_cout_set_cursor_position(
582 struct efi_simple_text_output_protocol *this,
583 unsigned long column, unsigned long row)
584{
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200585 efi_status_t ret = EFI_SUCCESS;
586 struct simple_text_output_mode *con = &efi_con_mode;
587 struct cout_mode *mode = &efi_cout_modes[con->mode];
588
Alexander Graf4aacacc2016-03-04 01:10:00 +0100589 EFI_ENTRY("%p, %ld, %ld", this, column, row);
590
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200591 /* Check parameters */
592 if (!this) {
593 ret = EFI_INVALID_PARAMETER;
594 goto out;
595 }
596 if (row >= mode->rows || column >= mode->columns) {
597 ret = EFI_UNSUPPORTED;
598 goto out;
599 }
600
601 /*
602 * Set cursor position by sending CSI H.
603 * EFI origin is [0, 0], terminal origin is [1, 1].
604 */
605 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100606 efi_con_mode.cursor_column = column;
607 efi_con_mode.cursor_row = row;
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200608out:
609 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100610}
611
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200612/**
613 * efi_cout_enable_cursor() - enable the cursor
614 *
615 * This function implements the EnableCursor service of the simple text output
616 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
617 * for details.
618 *
619 * @this: pointer to the protocol instance
620 * @enable: if true enable, if false disable the cursor
621 * Return: status code
622 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100623static efi_status_t EFIAPI efi_cout_enable_cursor(
624 struct efi_simple_text_output_protocol *this,
625 bool enable)
626{
627 EFI_ENTRY("%p, %d", this, enable);
628
629 printf(ESC"[?25%c", enable ? 'h' : 'l');
Heinrich Schuchardt53f26b92019-06-02 22:54:28 +0200630 efi_con_mode.cursor_visible = !!enable;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100631
632 return EFI_EXIT(EFI_SUCCESS);
633}
634
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +0200635struct efi_simple_text_output_protocol efi_con_out = {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100636 .reset = efi_cout_reset,
637 .output_string = efi_cout_output_string,
638 .test_string = efi_cout_test_string,
639 .query_mode = efi_cout_query_mode,
640 .set_mode = efi_cout_set_mode,
641 .set_attribute = efi_cout_set_attribute,
642 .clear_screen = efi_cout_clear_screen,
643 .set_cursor_position = efi_cout_set_cursor_position,
644 .enable_cursor = efi_cout_enable_cursor,
645 .mode = (void*)&efi_con_mode,
646};
647
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200648/**
649 * struct efi_cin_notify_function - registered console input notify function
650 *
651 * @link: link to list
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200652 * @key: key to notify
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200653 * @function: function to call
654 */
655struct efi_cin_notify_function {
656 struct list_head link;
657 struct efi_key_data key;
658 efi_status_t (EFIAPI *function)
659 (struct efi_key_data *key_data);
660};
661
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200662static bool key_available;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200663static struct efi_key_data next_key;
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200664static LIST_HEAD(cin_notify_functions);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100665
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200666/**
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200667 * set_shift_mask() - set shift mask
668 *
669 * @mod: Xterm shift mask
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200670 * @key_state: receives the state of the shift, alt, control, and logo keys
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200671 */
Heinrich Schuchardtf2e7d002023-02-10 08:51:41 +0100672static void set_shift_mask(int mod, struct efi_key_state *key_state)
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200673{
674 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
675 if (mod) {
676 --mod;
677 if (mod & 1)
678 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
679 if (mod & 2)
680 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
681 if (mod & 4)
682 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
Heinrich Schuchardtfea93d62019-06-16 22:33:20 +0200683 if (!mod || (mod & 8))
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200684 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200685 }
686}
687
688/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200689 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200690 *
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100691 * This gets called when we have already parsed CSI.
692 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200693 * @key_state: receives the state of the shift, alt, control, and logo keys
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200694 * Return: the unmodified code
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100695 */
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200696static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100697{
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200698 int c, mod = 0, ret = 0;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100699
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200700 c = getchar();
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100701
702 if (c != ';') {
703 ret = c;
704 if (c == '~')
705 goto out;
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200706 c = getchar();
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100707 }
708 for (;;) {
709 switch (c) {
710 case '0'...'9':
711 mod *= 10;
712 mod += c - '0';
713 /* fall through */
714 case ';':
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200715 c = getchar();
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100716 break;
717 default:
718 goto out;
719 }
720 }
721out:
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200722 set_shift_mask(mod, key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100723 if (!ret)
724 ret = c;
725 return ret;
726}
727
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200728/**
729 * efi_cin_read_key() - read a key from the console input
730 *
731 * @key: - key received
732 * Return: - status code
733 */
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200734static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100735{
736 struct efi_input_key pressed_key = {
737 .scan_code = 0,
738 .unicode_char = 0,
739 };
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200740 s32 ch;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100741
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200742 if (console_read_unicode(&ch))
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200743 return EFI_NOT_READY;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200744
745 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
746 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
747
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200748 /* We do not support multi-word codes */
749 if (ch >= 0x10000)
750 ch = '?';
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200751
752 switch (ch) {
753 case 0x1b:
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100754 /*
Heinrich Schuchardte5c93172020-12-27 14:47:50 +0100755 * If a second key is received within 10 ms, assume that we are
756 * dealing with an escape sequence. Otherwise consider this the
757 * escape key being hit. 10 ms is long enough to work fine at
758 * 1200 baud and above.
759 */
760 udelay(10000);
761 if (!tstc()) {
762 pressed_key.scan_code = 23;
763 break;
764 }
765 /*
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100766 * Xterm Control Sequences
767 * https://www.xfree86.org/4.8.0/ctlseqs.html
768 */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200769 ch = getchar();
Alexander Graf4aacacc2016-03-04 01:10:00 +0100770 switch (ch) {
771 case cESC: /* ESC */
772 pressed_key.scan_code = 23;
773 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200774 case 'O': /* F1 - F4, End */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200775 ch = getchar();
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200776 /* consider modifiers */
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200777 if (ch == 'F') { /* End */
778 pressed_key.scan_code = 6;
779 break;
780 } else if (ch < 'P') {
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200781 set_shift_mask(ch - '0', &key->key_state);
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200782 ch = getchar();
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200783 }
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100784 pressed_key.scan_code = ch - 'P' + 11;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100785 break;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100786 case '[':
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200787 ch = getchar();
Alexander Graf4aacacc2016-03-04 01:10:00 +0100788 switch (ch) {
789 case 'A'...'D': /* up, down right, left */
790 pressed_key.scan_code = ch - 'A' + 1;
791 break;
792 case 'F': /* End */
793 pressed_key.scan_code = 6;
794 break;
795 case 'H': /* Home */
796 pressed_key.scan_code = 5;
797 break;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100798 case '1':
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200799 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100800 switch (ch) {
801 case '1'...'5': /* F1 - F5 */
802 pressed_key.scan_code = ch - '1' + 11;
803 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200804 case '6'...'9': /* F5 - F8 */
805 pressed_key.scan_code = ch - '6' + 15;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100806 break;
807 case 'A'...'D': /* up, down right, left */
808 pressed_key.scan_code = ch - 'A' + 1;
809 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200810 case 'F': /* End */
811 pressed_key.scan_code = 6;
812 break;
813 case 'H': /* Home */
814 pressed_key.scan_code = 5;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100815 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200816 case '~': /* Home */
817 pressed_key.scan_code = 5;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100818 break;
819 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100820 break;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100821 case '2':
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200822 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100823 switch (ch) {
824 case '0'...'1': /* F9 - F10 */
825 pressed_key.scan_code = ch - '0' + 19;
826 break;
827 case '3'...'4': /* F11 - F12 */
828 pressed_key.scan_code = ch - '3' + 21;
829 break;
830 case '~': /* INS */
831 pressed_key.scan_code = 7;
832 break;
833 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100834 break;
835 case '3': /* DEL */
836 pressed_key.scan_code = 8;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200837 analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100838 break;
839 case '5': /* PG UP */
840 pressed_key.scan_code = 9;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200841 analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100842 break;
843 case '6': /* PG DOWN */
844 pressed_key.scan_code = 10;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200845 analyze_modifiers(&key->key_state);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100846 break;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200847 } /* [ */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100848 break;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200849 default:
850 /* ALT key */
851 set_shift_mask(3, &key->key_state);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100852 }
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200853 break;
854 case 0x7f:
Alexander Graf4aacacc2016-03-04 01:10:00 +0100855 /* Backspace */
856 ch = 0x08;
857 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200858 if (pressed_key.scan_code) {
859 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
860 } else {
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100861 pressed_key.unicode_char = ch;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200862
863 /*
864 * Assume left control key for control characters typically
865 * entered using the control key.
866 */
867 if (ch >= 0x01 && ch <= 0x1f) {
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200868 key->key_state.key_shift_state |=
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200869 EFI_SHIFT_STATE_VALID;
870 switch (ch) {
871 case 0x01 ... 0x07:
872 case 0x0b ... 0x0c:
873 case 0x0e ... 0x1f:
874 key->key_state.key_shift_state |=
875 EFI_LEFT_CONTROL_PRESSED;
876 }
877 }
878 }
879 key->key = pressed_key;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100880
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200881 return EFI_SUCCESS;
882}
883
884/**
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200885 * efi_cin_notify() - notify registered functions
886 */
887static void efi_cin_notify(void)
888{
889 struct efi_cin_notify_function *item;
890
891 list_for_each_entry(item, &cin_notify_functions, link) {
892 bool match = true;
893
894 /* We do not support toggle states */
895 if (item->key.key.unicode_char || item->key.key.scan_code) {
896 if (item->key.key.unicode_char !=
897 next_key.key.unicode_char ||
898 item->key.key.scan_code != next_key.key.scan_code)
899 match = false;
900 }
901 if (item->key.key_state.key_shift_state &&
902 item->key.key_state.key_shift_state !=
903 next_key.key_state.key_shift_state)
904 match = false;
905
906 if (match)
907 /* We don't bother about the return code */
908 EFI_CALL(item->function(&next_key));
909 }
910}
911
912/**
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200913 * efi_cin_check() - check if keyboard input is available
914 */
915static void efi_cin_check(void)
916{
917 efi_status_t ret;
918
919 if (key_available) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200920 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200921 return;
922 }
923
924 if (tstc()) {
925 ret = efi_cin_read_key(&next_key);
926 if (ret == EFI_SUCCESS) {
927 key_available = true;
928
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200929 /* Notify registered functions */
930 efi_cin_notify();
931
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200932 /* Queue the wait for key event */
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200933 if (key_available)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200934 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200935 }
936 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200937}
938
939/**
940 * efi_cin_empty_buffer() - empty input buffer
941 */
942static void efi_cin_empty_buffer(void)
943{
944 while (tstc())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200945 getchar();
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200946 key_available = false;
947}
948
949/**
950 * efi_cin_reset_ex() - reset console input
951 *
952 * @this: - the extended simple text input protocol
953 * @extended_verification: - extended verification
954 *
955 * This function implements the reset service of the
956 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
957 *
958 * See the Unified Extensible Firmware Interface (UEFI) specification for
959 * details.
960 *
961 * Return: old value of the task priority level
962 */
963static efi_status_t EFIAPI efi_cin_reset_ex(
964 struct efi_simple_text_input_ex_protocol *this,
965 bool extended_verification)
966{
967 efi_status_t ret = EFI_SUCCESS;
968
969 EFI_ENTRY("%p, %d", this, extended_verification);
970
971 /* Check parameters */
972 if (!this) {
973 ret = EFI_INVALID_PARAMETER;
974 goto out;
975 }
976
977 efi_cin_empty_buffer();
978out:
979 return EFI_EXIT(ret);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200980}
981
982/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200983 * efi_cin_read_key_stroke_ex() - read key stroke
984 *
985 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
986 * @key_data: key read from console
987 * Return: status code
988 *
989 * This function implements the ReadKeyStrokeEx service of the
990 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
991 *
992 * See the Unified Extensible Firmware Interface (UEFI) specification for
993 * details.
994 */
995static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
996 struct efi_simple_text_input_ex_protocol *this,
997 struct efi_key_data *key_data)
998{
999 efi_status_t ret = EFI_SUCCESS;
1000
1001 EFI_ENTRY("%p, %p", this, key_data);
1002
1003 /* Check parameters */
1004 if (!this || !key_data) {
1005 ret = EFI_INVALID_PARAMETER;
1006 goto out;
1007 }
1008
1009 /* We don't do interrupts, so check for timers cooperatively */
1010 efi_timer_check();
1011
1012 /* Enable console input after ExitBootServices */
1013 efi_cin_check();
1014
1015 if (!key_available) {
Heinrich Schuchardtb6697c22022-09-01 23:30:09 +02001016 memset(key_data, 0, sizeof(struct efi_key_data));
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001017 ret = EFI_NOT_READY;
1018 goto out;
1019 }
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +02001020 /*
1021 * CTRL+A - CTRL+Z have to be signaled as a - z.
1022 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
Heinrich Schuchardt068dc9c2022-09-02 00:49:12 +02001023 * CTRL+\ - CTRL+_ have to be signaled as \ - _.
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +02001024 */
1025 switch (next_key.key.unicode_char) {
1026 case 0x01 ... 0x07:
1027 case 0x0b ... 0x0c:
1028 case 0x0e ... 0x1a:
1029 if (!(next_key.key_state.key_toggle_state &
1030 EFI_CAPS_LOCK_ACTIVE) ^
1031 !(next_key.key_state.key_shift_state &
1032 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
1033 next_key.key.unicode_char += 0x40;
1034 else
1035 next_key.key.unicode_char += 0x60;
Heinrich Schuchardt068dc9c2022-09-02 00:49:12 +02001036 break;
1037 case 0x1c ... 0x1f:
1038 next_key.key.unicode_char += 0x40;
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +02001039 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001040 *key_data = next_key;
1041 key_available = false;
1042 efi_con_in.wait_for_key->is_signaled = false;
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +02001043
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001044out:
1045 return EFI_EXIT(ret);
1046}
1047
1048/**
1049 * efi_cin_set_state() - set toggle key state
1050 *
1051 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +02001052 * @key_toggle_state: pointer to key toggle state
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001053 * Return: status code
1054 *
1055 * This function implements the SetState service of the
1056 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1057 *
1058 * See the Unified Extensible Firmware Interface (UEFI) specification for
1059 * details.
1060 */
1061static efi_status_t EFIAPI efi_cin_set_state(
1062 struct efi_simple_text_input_ex_protocol *this,
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +02001063 u8 *key_toggle_state)
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001064{
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +02001065 EFI_ENTRY("%p, %p", this, key_toggle_state);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001066 /*
1067 * U-Boot supports multiple console input sources like serial and
1068 * net console for which a key toggle state cannot be set at all.
1069 *
1070 * According to the UEFI specification it is allowable to not implement
1071 * this service.
1072 */
1073 return EFI_EXIT(EFI_UNSUPPORTED);
1074}
1075
1076/**
1077 * efi_cin_register_key_notify() - register key notification function
1078 *
1079 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1080 * @key_data: key to be notified
1081 * @key_notify_function: function to be called if the key is pressed
1082 * @notify_handle: handle for unregistering the notification
1083 * Return: status code
1084 *
1085 * This function implements the SetState service of the
1086 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1087 *
1088 * See the Unified Extensible Firmware Interface (UEFI) specification for
1089 * details.
1090 */
1091static efi_status_t EFIAPI efi_cin_register_key_notify(
1092 struct efi_simple_text_input_ex_protocol *this,
1093 struct efi_key_data *key_data,
1094 efi_status_t (EFIAPI *key_notify_function)(
1095 struct efi_key_data *key_data),
1096 void **notify_handle)
1097{
Heinrich Schuchardt695691e2018-09-11 22:38:12 +02001098 efi_status_t ret = EFI_SUCCESS;
1099 struct efi_cin_notify_function *notify_function;
1100
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001101 EFI_ENTRY("%p, %p, %p, %p",
1102 this, key_data, key_notify_function, notify_handle);
Heinrich Schuchardt695691e2018-09-11 22:38:12 +02001103
1104 /* Check parameters */
1105 if (!this || !key_data || !key_notify_function || !notify_handle) {
1106 ret = EFI_INVALID_PARAMETER;
1107 goto out;
1108 }
1109
1110 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
1111 key_data->key.unicode_char,
1112 key_data->key.scan_code,
1113 key_data->key_state.key_shift_state,
1114 key_data->key_state.key_toggle_state);
1115
1116 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
1117 if (!notify_function) {
1118 ret = EFI_OUT_OF_RESOURCES;
1119 goto out;
1120 }
1121 notify_function->key = *key_data;
1122 notify_function->function = key_notify_function;
1123 list_add_tail(&notify_function->link, &cin_notify_functions);
1124 *notify_handle = notify_function;
1125out:
1126 return EFI_EXIT(ret);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001127}
1128
1129/**
1130 * efi_cin_unregister_key_notify() - unregister key notification function
1131 *
1132 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1133 * @notification_handle: handle received when registering
1134 * Return: status code
1135 *
1136 * This function implements the SetState service of the
1137 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1138 *
1139 * See the Unified Extensible Firmware Interface (UEFI) specification for
1140 * details.
1141 */
1142static efi_status_t EFIAPI efi_cin_unregister_key_notify(
1143 struct efi_simple_text_input_ex_protocol *this,
1144 void *notification_handle)
1145{
Heinrich Schuchardt695691e2018-09-11 22:38:12 +02001146 efi_status_t ret = EFI_INVALID_PARAMETER;
1147 struct efi_cin_notify_function *item, *notify_function =
1148 notification_handle;
1149
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001150 EFI_ENTRY("%p, %p", this, notification_handle);
Heinrich Schuchardt695691e2018-09-11 22:38:12 +02001151
1152 /* Check parameters */
1153 if (!this || !notification_handle)
1154 goto out;
1155
1156 list_for_each_entry(item, &cin_notify_functions, link) {
1157 if (item == notify_function) {
1158 ret = EFI_SUCCESS;
1159 break;
1160 }
1161 }
1162 if (ret != EFI_SUCCESS)
1163 goto out;
1164
1165 /* Remove the notify function */
1166 list_del(&notify_function->link);
1167 free(notify_function);
1168out:
1169 return EFI_EXIT(ret);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001170}
1171
1172
1173/**
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001174 * efi_cin_reset() - drain the input buffer
1175 *
1176 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1177 * @extended_verification: allow for exhaustive verification
1178 * Return: status code
1179 *
1180 * This function implements the Reset service of the
1181 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1182 *
1183 * See the Unified Extensible Firmware Interface (UEFI) specification for
1184 * details.
1185 */
1186static efi_status_t EFIAPI efi_cin_reset
1187 (struct efi_simple_text_input_protocol *this,
1188 bool extended_verification)
1189{
1190 efi_status_t ret = EFI_SUCCESS;
1191
1192 EFI_ENTRY("%p, %d", this, extended_verification);
1193
1194 /* Check parameters */
1195 if (!this) {
1196 ret = EFI_INVALID_PARAMETER;
1197 goto out;
1198 }
1199
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001200 efi_cin_empty_buffer();
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001201out:
1202 return EFI_EXIT(ret);
1203}
1204
1205/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001206 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001207 *
1208 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1209 * @key: key read from console
1210 * Return: status code
1211 *
1212 * This function implements the ReadKeyStroke service of the
1213 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1214 *
1215 * See the Unified Extensible Firmware Interface (UEFI) specification for
1216 * details.
1217 */
1218static efi_status_t EFIAPI efi_cin_read_key_stroke
1219 (struct efi_simple_text_input_protocol *this,
1220 struct efi_input_key *key)
1221{
1222 efi_status_t ret = EFI_SUCCESS;
1223
1224 EFI_ENTRY("%p, %p", this, key);
1225
1226 /* Check parameters */
1227 if (!this || !key) {
1228 ret = EFI_INVALID_PARAMETER;
1229 goto out;
1230 }
1231
1232 /* We don't do interrupts, so check for timers cooperatively */
1233 efi_timer_check();
1234
1235 /* Enable console input after ExitBootServices */
1236 efi_cin_check();
1237
1238 if (!key_available) {
1239 ret = EFI_NOT_READY;
1240 goto out;
1241 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001242 *key = next_key.key;
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001243 key_available = false;
1244 efi_con_in.wait_for_key->is_signaled = false;
1245out:
1246 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +01001247}
1248
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001249static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1250 .reset = efi_cin_reset_ex,
1251 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1252 .wait_for_key_ex = NULL,
1253 .set_state = efi_cin_set_state,
1254 .register_key_notify = efi_cin_register_key_notify,
1255 .unregister_key_notify = efi_cin_unregister_key_notify,
1256};
1257
Heinrich Schuchardt3dabffd2018-09-08 10:20:10 +02001258struct efi_simple_text_input_protocol efi_con_in = {
Alexander Graf4aacacc2016-03-04 01:10:00 +01001259 .reset = efi_cin_reset,
1260 .read_key_stroke = efi_cin_read_key_stroke,
1261 .wait_for_key = NULL,
1262};
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001263
1264static struct efi_event *console_timer_event;
1265
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001266/*
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001267 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001268 *
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001269 * @event: console timer event
1270 * @context: not used
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001271 */
xypron.glpk@gmx.de0ce42dc2017-07-20 05:26:07 +02001272static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1273 void *context)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001274{
1275 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001276 efi_cin_check();
1277 EFI_EXIT(EFI_SUCCESS);
1278}
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001279
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001280/**
1281 * efi_key_notify() - notify the wait for key event
1282 *
1283 * @event: wait for key event
1284 * @context: not used
1285 */
1286static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1287{
1288 EFI_ENTRY("%p, %p", event, context);
1289 efi_cin_check();
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001290 EFI_EXIT(EFI_SUCCESS);
1291}
1292
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001293/**
1294 * efi_console_register() - install the console protocols
1295 *
1296 * This function is called from do_bootefi_exec().
Heinrich Schuchardta92e9ab2018-10-02 06:08:26 +02001297 *
1298 * Return: status code
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001299 */
Heinrich Schuchardta92e9ab2018-10-02 06:08:26 +02001300efi_status_t efi_console_register(void)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001301{
1302 efi_status_t r;
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001303 struct efi_device_path *dp;
Rob Clark49f7b4b2017-07-24 10:39:01 -04001304
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001305 /* Install protocols on root node */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03001306 r = efi_install_multiple_protocol_interfaces(&efi_root,
1307 &efi_guid_text_output_protocol,
1308 &efi_con_out,
1309 &efi_guid_text_input_protocol,
1310 &efi_con_in,
1311 &efi_guid_text_input_ex_protocol,
1312 &efi_con_in_ex,
1313 NULL);
Alexander Graf36bab912018-09-04 14:59:11 +02001314
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001315 /* Create console node and install device path protocols */
1316 if (CONFIG_IS_ENABLED(DM_SERIAL)) {
1317 dp = efi_dp_from_uart();
1318 if (!dp)
1319 goto out_of_memory;
Alexander Graf36bab912018-09-04 14:59:11 +02001320
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001321 /* Hook UART up to the device list */
1322 efi_add_handle(&uart_obj);
Alexander Graf36bab912018-09-04 14:59:11 +02001323
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001324 /* Install device path */
1325 r = efi_add_protocol(&uart_obj, &efi_guid_device_path, dp);
1326 if (r != EFI_SUCCESS)
1327 goto out_of_memory;
1328 }
Rob Clark49f7b4b2017-07-24 10:39:01 -04001329
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001330 /* Create console events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001331 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1332 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001333 if (r != EFI_SUCCESS) {
1334 printf("ERROR: Failed to register WaitForKey event\n");
1335 return r;
1336 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001337 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001338 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001339 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001340 &console_timer_event);
1341 if (r != EFI_SUCCESS) {
1342 printf("ERROR: Failed to register console event\n");
1343 return r;
1344 }
1345 /* 5000 ns cycle is sufficient for 2 MBaud */
1346 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1347 if (r != EFI_SUCCESS)
1348 printf("ERROR: Failed to set console timer\n");
1349 return r;
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001350out_of_memory:
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +02001351 printf("ERROR: Out of memory\n");
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001352 return r;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001353}
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001354
1355/**
1356 * efi_console_get_u16_string() - get user input string
1357 *
1358 * @cin: protocol interface to EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1359 * @buf: buffer to store user input string in UTF16
1360 * @count: number of u16 string including NULL terminator that buf has
1361 * @filter_func: callback to filter user input
1362 * @row: row number to locate user input form
1363 * @col: column number to locate user input form
1364 * Return: status code
1365 */
1366efi_status_t efi_console_get_u16_string(struct efi_simple_text_input_protocol *cin,
1367 u16 *buf, efi_uintn_t count,
1368 efi_console_filter_func filter_func,
1369 int row, int col)
1370{
1371 efi_status_t ret;
1372 efi_uintn_t len = 0;
1373 struct efi_input_key key;
1374
1375 printf(ANSI_CURSOR_POSITION
1376 ANSI_CLEAR_LINE_TO_END
1377 ANSI_CURSOR_SHOW, row, col);
1378
Heinrich Schuchardt49dbcaf2022-10-15 12:22:37 +02001379 efi_cin_empty_buffer();
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001380
1381 for (;;) {
1382 do {
1383 ret = EFI_CALL(cin->read_key_stroke(cin, &key));
1384 mdelay(10);
1385 } while (ret == EFI_NOT_READY);
1386
1387 if (key.unicode_char == u'\b') {
1388 if (len > 0)
1389 buf[--len] = u'\0';
1390
1391 printf(ANSI_CURSOR_POSITION
1392 "%ls"
1393 ANSI_CLEAR_LINE_TO_END, row, col, buf);
1394 continue;
1395 } else if (key.unicode_char == u'\r') {
1396 buf[len] = u'\0';
1397 return EFI_SUCCESS;
1398 } else if (key.unicode_char == 0x3 || key.scan_code == 23) {
1399 return EFI_ABORTED;
1400 } else if (key.unicode_char < 0x20) {
1401 /* ignore control codes other than Ctrl+C, '\r' and '\b' */
1402 continue;
1403 } else if (key.scan_code != 0) {
1404 /* only accept single ESC press for cancel */
1405 continue;
1406 }
1407
1408 if (filter_func) {
1409 if (filter_func(&key) != EFI_SUCCESS)
1410 continue;
1411 }
1412
1413 if (len >= (count - 1))
1414 continue;
1415
1416 buf[len] = key.unicode_char;
1417 len++;
1418 printf(ANSI_CURSOR_POSITION "%ls", row, col, buf);
1419 }
1420}