blob: a2d137d7a9e15408facd6bdfdd8061df1e5118e8 [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
Heinrich Schuchardtef6c69e2023-03-03 22:04:26 +010080/**
81 * term_get_char() - read a character from the console
82 *
83 * Wait for up to 100 ms to read a character from the console.
84 *
85 * @c: pointer to the buffer to receive the character
86 * Return: 0 on success, 1 otherwise
87 */
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010088static int term_get_char(s32 *c)
89{
90 u64 timeout;
91
92 /* Wait up to 100 ms for a character */
93 timeout = timer_get_us() + 100000;
94
95 while (!tstc())
96 if (timer_get_us() > timeout)
97 return 1;
98
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +020099 *c = getchar();
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100100 return 0;
101}
102
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200103/**
Heinrich Schuchardt77135c22018-05-16 18:17:38 +0200104 * Receive and parse a reply from the terminal.
105 *
106 * @n: array of return values
107 * @num: number of return values expected
108 * @end_char: character indicating end of terminal message
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200109 * Return: non-zero indicates error
Heinrich Schuchardt77135c22018-05-16 18:17:38 +0200110 */
111static int term_read_reply(int *n, int num, char end_char)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100112{
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100113 s32 c;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100114 int i = 0;
115
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100116 if (term_get_char(&c) || c != cESC)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100117 return -1;
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100118
119 if (term_get_char(&c) || c != '[')
Alexander Graf4aacacc2016-03-04 01:10:00 +0100120 return -1;
121
122 n[0] = 0;
123 while (1) {
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100124 if (!term_get_char(&c)) {
125 if (c == ';') {
126 i++;
127 if (i >= num)
128 return -1;
129 n[i] = 0;
130 continue;
131 } else if (c == end_char) {
132 break;
133 } else if (c > '9' || c < '0') {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100134 return -1;
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100135 }
136
137 /* Read one more decimal position */
138 n[i] *= 10;
139 n[i] += c - '0';
140 } else {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100141 return -1;
142 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100143 }
Heinrich Schuchardt77135c22018-05-16 18:17:38 +0200144 if (i != num - 1)
145 return -1;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100146
147 return 0;
148}
149
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200150/**
151 * efi_cout_output_string() - write Unicode string to console
152 *
153 * This function implements the OutputString service of the simple text output
154 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
155 * for details.
156 *
157 * @this: simple text output protocol
158 * @string: u16 string
159 * Return: status code
160 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100161static efi_status_t EFIAPI efi_cout_output_string(
162 struct efi_simple_text_output_protocol *this,
Heinrich Schuchardt84494282021-01-05 07:50:09 +0100163 const u16 *string)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100164{
Rob Clark1ef185d2017-09-13 18:05:44 -0400165 struct simple_text_output_mode *con = &efi_con_mode;
166 struct cout_mode *mode = &efi_cout_modes[con->mode];
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200167 char *buf, *pos;
Heinrich Schuchardt84494282021-01-05 07:50:09 +0100168 const u16 *p;
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200169 efi_status_t ret = EFI_SUCCESS;
Rob Clark1ef185d2017-09-13 18:05:44 -0400170
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200171 EFI_ENTRY("%p, %p", this, string);
Rob Clark1ef185d2017-09-13 18:05:44 -0400172
Heinrich Schuchardte60b2982019-05-18 18:11:54 +0200173 if (!this || !string) {
174 ret = EFI_INVALID_PARAMETER;
175 goto out;
176 }
177
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200178 buf = malloc(utf16_utf8_strlen(string) + 1);
179 if (!buf) {
180 ret = EFI_OUT_OF_RESOURCES;
181 goto out;
182 }
183 pos = buf;
184 utf16_utf8_strcpy(&pos, string);
Rob Clark1ef185d2017-09-13 18:05:44 -0400185 fputs(stdout, buf);
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200186 free(buf);
Rob Clark1ef185d2017-09-13 18:05:44 -0400187
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200188 /*
189 * Update the cursor position.
190 *
191 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200192 * and U000D. All other control characters are ignored. Any non-control
193 * character increase the column by one.
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200194 */
195 for (p = string; *p; ++p) {
Rob Clark1ef185d2017-09-13 18:05:44 -0400196 switch (*p) {
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200197 case '\b': /* U+0008, backspace */
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200198 if (con->cursor_column)
199 con->cursor_column--;
Rob Clark1ef185d2017-09-13 18:05:44 -0400200 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200201 case '\n': /* U+000A, newline */
Rob Clark1ef185d2017-09-13 18:05:44 -0400202 con->cursor_column = 0;
203 con->cursor_row++;
204 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200205 case '\r': /* U+000D, carriage-return */
206 con->cursor_column = 0;
Rob Clark1ef185d2017-09-13 18:05:44 -0400207 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200208 case 0xd800 ... 0xdbff:
209 /*
210 * Ignore high surrogates, we do not want to count a
211 * Unicode character twice.
212 */
Rob Clark1ef185d2017-09-13 18:05:44 -0400213 break;
214 default:
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200215 /* Exclude control codes */
216 if (*p > 0x1f)
217 con->cursor_column++;
Rob Clark1ef185d2017-09-13 18:05:44 -0400218 break;
219 }
220 if (con->cursor_column >= mode->columns) {
221 con->cursor_column = 0;
222 con->cursor_row++;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100223 }
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200224 /*
225 * When we exceed the row count the terminal will scroll up one
226 * line. We have to adjust the cursor position.
227 */
228 if (con->cursor_row >= mode->rows && con->cursor_row)
229 con->cursor_row--;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100230 }
231
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200232out:
233 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100234}
235
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200236/**
237 * efi_cout_test_string() - test writing Unicode string to console
238 *
239 * This function implements the TestString service of the simple text output
240 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
241 * for details.
242 *
243 * As in OutputString we simply convert UTF-16 to UTF-8 there are no unsupported
244 * code points and we can always return EFI_SUCCESS.
245 *
246 * @this: simple text output protocol
247 * @string: u16 string
248 * Return: status code
249 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100250static efi_status_t EFIAPI efi_cout_test_string(
251 struct efi_simple_text_output_protocol *this,
Heinrich Schuchardt84494282021-01-05 07:50:09 +0100252 const u16 *string)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100253{
254 EFI_ENTRY("%p, %p", this, string);
255 return EFI_EXIT(EFI_SUCCESS);
256}
257
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200258/**
259 * cout_mode_matches() - check if mode has given terminal size
260 *
261 * @mode: text mode
262 * @rows: number of rows
263 * @cols: number of columns
264 * Return: true if number of rows and columns matches the mode and
265 * the mode is present
266 */
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100267static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
268{
269 if (!mode->present)
270 return false;
271
272 return (mode->rows == rows) && (mode->columns == cols);
273}
274
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200275/**
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100276 * query_console_serial() - query serial console size
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200277 *
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200278 * When using a serial console or the net console we can only devise the
279 * terminal size by querying the terminal using ECMA-48 control sequences.
280 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200281 * @rows: pointer to return number of rows
282 * @cols: pointer to return number of columns
283 * Returns: 0 on success
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200284 */
Rob Clark1e948922017-09-13 18:05:42 -0400285static int query_console_serial(int *rows, int *cols)
286{
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200287 int ret = 0;
288 int n[2];
Rob Clark1e948922017-09-13 18:05:42 -0400289
290 /* Empty input buffer */
291 while (tstc())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200292 getchar();
Rob Clark1e948922017-09-13 18:05:42 -0400293
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200294 /*
295 * Not all terminals understand CSI [18t for querying the console size.
296 * We should adhere to escape sequences documented in the console_codes
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200297 * man page and the ECMA-48 standard.
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200298 *
299 * So here we follow a different approach. We position the cursor to the
300 * bottom right and query its position. Before leaving the function we
301 * restore the original cursor position.
302 */
303 printf(ESC "7" /* Save cursor position */
304 ESC "[r" /* Set scrolling region to full window */
305 ESC "[999;999H" /* Move to bottom right corner */
306 ESC "[6n"); /* Query cursor position */
Rob Clark1e948922017-09-13 18:05:42 -0400307
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200308 /* Read {rows,cols} */
309 if (term_read_reply(n, 2, 'R')) {
310 ret = 1;
311 goto out;
312 }
Rob Clark1e948922017-09-13 18:05:42 -0400313
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200314 *cols = n[1];
315 *rows = n[0];
316out:
317 printf(ESC "8"); /* Restore cursor position */
318 return ret;
Rob Clark1e948922017-09-13 18:05:42 -0400319}
320
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200321/**
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100322 * query_vidconsole() - query video console size
323 *
324 *
325 * @rows: pointer to return number of rows
326 * @cols: pointer to return number of columns
327 * Returns: 0 on success
328 */
329static int __maybe_unused query_vidconsole(int *rows, int *cols)
330{
331 const char *stdout_name = env_get("stdout");
332 struct stdio_dev *stdout_dev;
333 struct udevice *dev;
334 struct vidconsole_priv *priv;
335
336 if (!stdout_name || strncmp(stdout_name, "vidconsole", 10))
337 return -ENODEV;
338 stdout_dev = stdio_get_by_name("vidconsole");
339 if (!stdout_dev)
340 return -ENODEV;
341 dev = stdout_dev->priv;
342 if (!dev)
343 return -ENODEV;
344 priv = dev_get_uclass_priv(dev);
345 if (!priv)
346 return -ENODEV;
347 *rows = priv->rows;
348 *cols = priv->cols;
349 return 0;
350}
351
352/**
Heinrich Schuchardt761908e2022-06-14 08:02:03 +0200353 * efi_setup_console_size() - update the mode table.
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200354 *
355 * By default the only mode available is 80x25. If the console has at least 50
356 * lines, enable mode 80x50. If we can query the console size and it is neither
357 * 80x25 nor 80x50, set it as an additional mode.
358 */
Heinrich Schuchardt761908e2022-06-14 08:02:03 +0200359void efi_setup_console_size(void)
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200360{
Alexander Graf28795322018-06-03 15:51:17 +0200361 int rows = 25, cols = 80;
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100362 int ret = -ENODEV;
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200363
Simon Glass52cb5042022-10-18 07:46:31 -0600364 if (IS_ENABLED(CONFIG_VIDEO))
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100365 ret = query_vidconsole(&rows, &cols);
366 if (ret)
367 ret = query_console_serial(&rows, &cols);
368 if (ret)
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200369 return;
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200370
Heinrich Schuchardt761908e2022-06-14 08:02:03 +0200371 log_debug("Console size %dx%d\n", rows, cols);
372
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200373 /* Test if we can have Mode 1 */
374 if (cols >= 80 && rows >= 50) {
375 efi_cout_modes[1].present = 1;
376 efi_con_mode.max_mode = 2;
377 }
378
379 /*
380 * Install our mode as mode 2 if it is different
381 * than mode 0 or 1 and set it as the currently selected mode
382 */
383 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
384 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
385 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
386 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
387 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
388 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
389 efi_con_mode.mode = EFI_COUT_MODE_2;
390 }
391}
392
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200393/**
394 * efi_cout_query_mode() - get terminal size for a text mode
395 *
396 * This function implements the QueryMode service of the simple text output
397 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
398 * for details.
399 *
400 * @this: simple text output protocol
401 * @mode_number: mode number to retrieve information on
402 * @columns: number of columns
403 * @rows: number of rows
404 * Return: status code
405 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100406static efi_status_t EFIAPI efi_cout_query_mode(
407 struct efi_simple_text_output_protocol *this,
408 unsigned long mode_number, unsigned long *columns,
409 unsigned long *rows)
410{
411 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
412
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100413 if (mode_number >= efi_con_mode.max_mode)
414 return EFI_EXIT(EFI_UNSUPPORTED);
415
416 if (efi_cout_modes[mode_number].present != 1)
417 return EFI_EXIT(EFI_UNSUPPORTED);
418
Alexander Graf4aacacc2016-03-04 01:10:00 +0100419 if (columns)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100420 *columns = efi_cout_modes[mode_number].columns;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100421 if (rows)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100422 *rows = efi_cout_modes[mode_number].rows;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100423
424 return EFI_EXIT(EFI_SUCCESS);
425}
426
Rob Clark87ef0012017-10-10 08:23:01 -0400427static const struct {
428 unsigned int fg;
429 unsigned int bg;
430} color[] = {
431 { 30, 40 }, /* 0: black */
432 { 34, 44 }, /* 1: blue */
433 { 32, 42 }, /* 2: green */
434 { 36, 46 }, /* 3: cyan */
435 { 31, 41 }, /* 4: red */
436 { 35, 45 }, /* 5: magenta */
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +0200437 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
438 { 37, 47 }, /* 7: light gray, map to white */
Rob Clark87ef0012017-10-10 08:23:01 -0400439};
440
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200441/**
442 * efi_cout_set_attribute() - set fore- and background color
443 *
444 * This function implements the SetAttribute service of the simple text output
445 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
446 * for details.
447 *
448 * @this: simple text output protocol
449 * @attribute: foreground color - bits 0-3, background color - bits 4-6
450 * Return: status code
451 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100452static efi_status_t EFIAPI efi_cout_set_attribute(
453 struct efi_simple_text_output_protocol *this,
454 unsigned long attribute)
455{
Rob Clark87ef0012017-10-10 08:23:01 -0400456 unsigned int bold = EFI_ATTR_BOLD(attribute);
457 unsigned int fg = EFI_ATTR_FG(attribute);
458 unsigned int bg = EFI_ATTR_BG(attribute);
459
Alexander Graf4aacacc2016-03-04 01:10:00 +0100460 EFI_ENTRY("%p, %lx", this, attribute);
461
Heinrich Schuchardt1e129622019-06-14 07:16:57 +0200462 efi_con_mode.attribute = attribute;
Rob Clark87ef0012017-10-10 08:23:01 -0400463 if (attribute)
464 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
465 else
466 printf(ESC"[0;37;40m");
467
468 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100469}
470
Heinrich Schuchardt7995cbe2019-12-22 07:15:55 +0000471/**
Jan Kiszkad4d0fec2023-01-18 22:24:59 +0100472 * efi_clear_screen() - clear screen
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200473 */
474static void efi_clear_screen(void)
475{
Jan Kiszkad4d0fec2023-01-18 22:24:59 +0100476 if (CONFIG_IS_ENABLED(EFI_SCROLL_ON_CLEAR_SCREEN)) {
477 unsigned int row, screen_rows, screen_columns;
478
479 /* Avoid overwriting previous outputs on streaming consoles */
480 screen_rows = efi_cout_modes[efi_con_mode.mode].rows;
481 screen_columns = efi_cout_modes[efi_con_mode.mode].columns;
482 printf(ESC "[%u;%uH", screen_rows, screen_columns);
483 for (row = 1; row < screen_rows; row++)
484 printf("\n");
485 }
486
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200487 /*
488 * The Linux console wants both a clear and a home command. The video
489 * uclass does not support <ESC>[H without coordinates, yet.
490 */
491 printf(ESC "[2J" ESC "[1;1H");
492 efi_con_mode.cursor_column = 0;
493 efi_con_mode.cursor_row = 0;
494}
495
496/**
497 * efi_cout_clear_screen() - clear screen
Heinrich Schuchardt7995cbe2019-12-22 07:15:55 +0000498 *
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200499 * This function implements the ClearScreen service of the simple text output
500 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
501 * for details.
Heinrich Schuchardt7995cbe2019-12-22 07:15:55 +0000502 *
503 * @this: pointer to the protocol instance
504 * Return: status code
505 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100506static efi_status_t EFIAPI efi_cout_clear_screen(
507 struct efi_simple_text_output_protocol *this)
508{
509 EFI_ENTRY("%p", this);
510
Jan Kiszkafb181e22023-01-18 22:25:00 +0100511 /* Set default colors if not done yet */
512 if (efi_con_mode.attribute == 0) {
513 efi_con_mode.attribute = 0x07;
514 printf(ESC "[0;37;40m");
515 }
516
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200517 efi_clear_screen();
Alexander Graf4aacacc2016-03-04 01:10:00 +0100518
519 return EFI_EXIT(EFI_SUCCESS);
520}
521
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200522/**
523 * efi_cout_clear_set_mode() - set text model
524 *
525 * This function implements the SetMode service of the simple text output
526 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
527 * for details.
528 *
529 * @this: pointer to the protocol instance
530 * @mode_number: number of the text mode to set
531 * Return: status code
532 */
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200533static efi_status_t EFIAPI efi_cout_set_mode(
534 struct efi_simple_text_output_protocol *this,
535 unsigned long mode_number)
536{
537 EFI_ENTRY("%p, %ld", this, mode_number);
538
539 if (mode_number >= efi_con_mode.max_mode)
540 return EFI_EXIT(EFI_UNSUPPORTED);
Heinrich Schuchardt6a6afa72019-09-04 22:46:13 +0200541
542 if (!efi_cout_modes[mode_number].present)
543 return EFI_EXIT(EFI_UNSUPPORTED);
544
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200545 efi_con_mode.mode = mode_number;
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200546 efi_clear_screen();
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200547
548 return EFI_EXIT(EFI_SUCCESS);
549}
550
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200551/**
552 * efi_cout_reset() - reset the terminal
553 *
554 * This function implements the Reset service of the simple text output
555 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
556 * for details.
557 *
558 * @this: pointer to the protocol instance
559 * @extended_verification: if set an extended verification may be executed
560 * Return: status code
561 */
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200562static efi_status_t EFIAPI efi_cout_reset(
563 struct efi_simple_text_output_protocol *this,
564 char extended_verification)
565{
566 EFI_ENTRY("%p, %d", this, extended_verification);
567
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200568 /* Set default colors */
Heinrich Schuchardt1e129622019-06-14 07:16:57 +0200569 efi_con_mode.attribute = 0x07;
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200570 printf(ESC "[0;37;40m");
Heinrich Schuchardt30c9d672022-04-30 09:05:04 +0200571 /* Clear screen */
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200572 efi_clear_screen();
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200573
574 return EFI_EXIT(EFI_SUCCESS);
575}
576
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200577/**
578 * efi_cout_set_cursor_position() - reset the terminal
579 *
580 * This function implements the SetCursorPosition service of the simple text
581 * output protocol. See the Unified Extensible Firmware Interface (UEFI)
582 * specification for details.
583 *
584 * @this: pointer to the protocol instance
585 * @column: column to move to
586 * @row: row to move to
587 * Return: status code
588 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100589static efi_status_t EFIAPI efi_cout_set_cursor_position(
590 struct efi_simple_text_output_protocol *this,
591 unsigned long column, unsigned long row)
592{
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200593 efi_status_t ret = EFI_SUCCESS;
594 struct simple_text_output_mode *con = &efi_con_mode;
595 struct cout_mode *mode = &efi_cout_modes[con->mode];
596
Alexander Graf4aacacc2016-03-04 01:10:00 +0100597 EFI_ENTRY("%p, %ld, %ld", this, column, row);
598
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200599 /* Check parameters */
600 if (!this) {
601 ret = EFI_INVALID_PARAMETER;
602 goto out;
603 }
604 if (row >= mode->rows || column >= mode->columns) {
605 ret = EFI_UNSUPPORTED;
606 goto out;
607 }
608
609 /*
610 * Set cursor position by sending CSI H.
611 * EFI origin is [0, 0], terminal origin is [1, 1].
612 */
613 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100614 efi_con_mode.cursor_column = column;
615 efi_con_mode.cursor_row = row;
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200616out:
617 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100618}
619
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200620/**
621 * efi_cout_enable_cursor() - enable the cursor
622 *
623 * This function implements the EnableCursor service of the simple text output
624 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
625 * for details.
626 *
627 * @this: pointer to the protocol instance
628 * @enable: if true enable, if false disable the cursor
629 * Return: status code
630 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100631static efi_status_t EFIAPI efi_cout_enable_cursor(
632 struct efi_simple_text_output_protocol *this,
633 bool enable)
634{
635 EFI_ENTRY("%p, %d", this, enable);
636
637 printf(ESC"[?25%c", enable ? 'h' : 'l');
Heinrich Schuchardt53f26b92019-06-02 22:54:28 +0200638 efi_con_mode.cursor_visible = !!enable;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100639
640 return EFI_EXIT(EFI_SUCCESS);
641}
642
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +0200643struct efi_simple_text_output_protocol efi_con_out = {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100644 .reset = efi_cout_reset,
645 .output_string = efi_cout_output_string,
646 .test_string = efi_cout_test_string,
647 .query_mode = efi_cout_query_mode,
648 .set_mode = efi_cout_set_mode,
649 .set_attribute = efi_cout_set_attribute,
650 .clear_screen = efi_cout_clear_screen,
651 .set_cursor_position = efi_cout_set_cursor_position,
652 .enable_cursor = efi_cout_enable_cursor,
653 .mode = (void*)&efi_con_mode,
654};
655
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200656/**
657 * struct efi_cin_notify_function - registered console input notify function
658 *
659 * @link: link to list
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200660 * @key: key to notify
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200661 * @function: function to call
662 */
663struct efi_cin_notify_function {
664 struct list_head link;
665 struct efi_key_data key;
666 efi_status_t (EFIAPI *function)
667 (struct efi_key_data *key_data);
668};
669
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200670static bool key_available;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200671static struct efi_key_data next_key;
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200672static LIST_HEAD(cin_notify_functions);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100673
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200674/**
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200675 * set_shift_mask() - set shift mask
676 *
677 * @mod: Xterm shift mask
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200678 * @key_state: receives the state of the shift, alt, control, and logo keys
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200679 */
Heinrich Schuchardtf2e7d002023-02-10 08:51:41 +0100680static void set_shift_mask(int mod, struct efi_key_state *key_state)
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200681{
682 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
683 if (mod) {
684 --mod;
685 if (mod & 1)
686 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
687 if (mod & 2)
688 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
689 if (mod & 4)
690 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
Heinrich Schuchardtfea93d62019-06-16 22:33:20 +0200691 if (!mod || (mod & 8))
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200692 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200693 }
694}
695
696/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200697 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200698 *
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100699 * This gets called when we have already parsed CSI.
700 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200701 * @key_state: receives the state of the shift, alt, control, and logo keys
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200702 * Return: the unmodified code
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100703 */
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200704static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100705{
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200706 int c, mod = 0, ret = 0;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100707
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200708 c = getchar();
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100709
710 if (c != ';') {
711 ret = c;
712 if (c == '~')
713 goto out;
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200714 c = getchar();
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100715 }
716 for (;;) {
717 switch (c) {
718 case '0'...'9':
719 mod *= 10;
720 mod += c - '0';
721 /* fall through */
722 case ';':
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200723 c = getchar();
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100724 break;
725 default:
726 goto out;
727 }
728 }
729out:
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200730 set_shift_mask(mod, key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100731 if (!ret)
732 ret = c;
733 return ret;
734}
735
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200736/**
737 * efi_cin_read_key() - read a key from the console input
738 *
739 * @key: - key received
740 * Return: - status code
741 */
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200742static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100743{
744 struct efi_input_key pressed_key = {
745 .scan_code = 0,
746 .unicode_char = 0,
747 };
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200748 s32 ch;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100749
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200750 if (console_read_unicode(&ch))
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200751 return EFI_NOT_READY;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200752
753 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
754 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
755
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200756 /* We do not support multi-word codes */
757 if (ch >= 0x10000)
758 ch = '?';
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200759
760 switch (ch) {
761 case 0x1b:
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100762 /*
Heinrich Schuchardte5c93172020-12-27 14:47:50 +0100763 * If a second key is received within 10 ms, assume that we are
764 * dealing with an escape sequence. Otherwise consider this the
765 * escape key being hit. 10 ms is long enough to work fine at
766 * 1200 baud and above.
767 */
768 udelay(10000);
769 if (!tstc()) {
770 pressed_key.scan_code = 23;
771 break;
772 }
773 /*
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100774 * Xterm Control Sequences
775 * https://www.xfree86.org/4.8.0/ctlseqs.html
776 */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200777 ch = getchar();
Alexander Graf4aacacc2016-03-04 01:10:00 +0100778 switch (ch) {
779 case cESC: /* ESC */
780 pressed_key.scan_code = 23;
781 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200782 case 'O': /* F1 - F4, End */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200783 ch = getchar();
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200784 /* consider modifiers */
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200785 if (ch == 'F') { /* End */
786 pressed_key.scan_code = 6;
787 break;
788 } else if (ch < 'P') {
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200789 set_shift_mask(ch - '0', &key->key_state);
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200790 ch = getchar();
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200791 }
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100792 pressed_key.scan_code = ch - 'P' + 11;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100793 break;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100794 case '[':
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200795 ch = getchar();
Alexander Graf4aacacc2016-03-04 01:10:00 +0100796 switch (ch) {
797 case 'A'...'D': /* up, down right, left */
798 pressed_key.scan_code = ch - 'A' + 1;
799 break;
800 case 'F': /* End */
801 pressed_key.scan_code = 6;
802 break;
803 case 'H': /* Home */
804 pressed_key.scan_code = 5;
805 break;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100806 case '1':
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200807 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100808 switch (ch) {
809 case '1'...'5': /* F1 - F5 */
810 pressed_key.scan_code = ch - '1' + 11;
811 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200812 case '6'...'9': /* F5 - F8 */
813 pressed_key.scan_code = ch - '6' + 15;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100814 break;
815 case 'A'...'D': /* up, down right, left */
816 pressed_key.scan_code = ch - 'A' + 1;
817 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200818 case 'F': /* End */
819 pressed_key.scan_code = 6;
820 break;
821 case 'H': /* Home */
822 pressed_key.scan_code = 5;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100823 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200824 case '~': /* Home */
825 pressed_key.scan_code = 5;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100826 break;
827 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100828 break;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100829 case '2':
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200830 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100831 switch (ch) {
832 case '0'...'1': /* F9 - F10 */
833 pressed_key.scan_code = ch - '0' + 19;
834 break;
835 case '3'...'4': /* F11 - F12 */
836 pressed_key.scan_code = ch - '3' + 21;
837 break;
838 case '~': /* INS */
839 pressed_key.scan_code = 7;
840 break;
841 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100842 break;
843 case '3': /* DEL */
844 pressed_key.scan_code = 8;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200845 analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100846 break;
847 case '5': /* PG UP */
848 pressed_key.scan_code = 9;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200849 analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100850 break;
851 case '6': /* PG DOWN */
852 pressed_key.scan_code = 10;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200853 analyze_modifiers(&key->key_state);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100854 break;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200855 } /* [ */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100856 break;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200857 default:
858 /* ALT key */
859 set_shift_mask(3, &key->key_state);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100860 }
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200861 break;
862 case 0x7f:
Alexander Graf4aacacc2016-03-04 01:10:00 +0100863 /* Backspace */
864 ch = 0x08;
865 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200866 if (pressed_key.scan_code) {
867 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
868 } else {
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100869 pressed_key.unicode_char = ch;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200870
871 /*
872 * Assume left control key for control characters typically
873 * entered using the control key.
874 */
875 if (ch >= 0x01 && ch <= 0x1f) {
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200876 key->key_state.key_shift_state |=
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200877 EFI_SHIFT_STATE_VALID;
878 switch (ch) {
879 case 0x01 ... 0x07:
880 case 0x0b ... 0x0c:
881 case 0x0e ... 0x1f:
882 key->key_state.key_shift_state |=
883 EFI_LEFT_CONTROL_PRESSED;
884 }
885 }
886 }
887 key->key = pressed_key;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100888
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200889 return EFI_SUCCESS;
890}
891
892/**
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200893 * efi_cin_notify() - notify registered functions
894 */
895static void efi_cin_notify(void)
896{
897 struct efi_cin_notify_function *item;
898
899 list_for_each_entry(item, &cin_notify_functions, link) {
900 bool match = true;
901
902 /* We do not support toggle states */
903 if (item->key.key.unicode_char || item->key.key.scan_code) {
904 if (item->key.key.unicode_char !=
905 next_key.key.unicode_char ||
906 item->key.key.scan_code != next_key.key.scan_code)
907 match = false;
908 }
909 if (item->key.key_state.key_shift_state &&
910 item->key.key_state.key_shift_state !=
911 next_key.key_state.key_shift_state)
912 match = false;
913
914 if (match)
915 /* We don't bother about the return code */
916 EFI_CALL(item->function(&next_key));
917 }
918}
919
920/**
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200921 * efi_cin_check() - check if keyboard input is available
922 */
923static void efi_cin_check(void)
924{
925 efi_status_t ret;
926
927 if (key_available) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200928 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200929 return;
930 }
931
932 if (tstc()) {
933 ret = efi_cin_read_key(&next_key);
934 if (ret == EFI_SUCCESS) {
935 key_available = true;
936
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200937 /* Notify registered functions */
938 efi_cin_notify();
939
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200940 /* Queue the wait for key event */
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200941 if (key_available)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200942 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200943 }
944 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200945}
946
947/**
948 * efi_cin_empty_buffer() - empty input buffer
949 */
950static void efi_cin_empty_buffer(void)
951{
952 while (tstc())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200953 getchar();
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200954 key_available = false;
955}
956
957/**
958 * efi_cin_reset_ex() - reset console input
959 *
960 * @this: - the extended simple text input protocol
961 * @extended_verification: - extended verification
962 *
963 * This function implements the reset service of the
964 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
965 *
966 * See the Unified Extensible Firmware Interface (UEFI) specification for
967 * details.
968 *
969 * Return: old value of the task priority level
970 */
971static efi_status_t EFIAPI efi_cin_reset_ex(
972 struct efi_simple_text_input_ex_protocol *this,
973 bool extended_verification)
974{
975 efi_status_t ret = EFI_SUCCESS;
976
977 EFI_ENTRY("%p, %d", this, extended_verification);
978
979 /* Check parameters */
980 if (!this) {
981 ret = EFI_INVALID_PARAMETER;
982 goto out;
983 }
984
985 efi_cin_empty_buffer();
986out:
987 return EFI_EXIT(ret);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200988}
989
990/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200991 * efi_cin_read_key_stroke_ex() - read key stroke
992 *
993 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
994 * @key_data: key read from console
995 * Return: status code
996 *
997 * This function implements the ReadKeyStrokeEx service of the
998 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
999 *
1000 * See the Unified Extensible Firmware Interface (UEFI) specification for
1001 * details.
1002 */
1003static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
1004 struct efi_simple_text_input_ex_protocol *this,
1005 struct efi_key_data *key_data)
1006{
1007 efi_status_t ret = EFI_SUCCESS;
1008
1009 EFI_ENTRY("%p, %p", this, key_data);
1010
1011 /* Check parameters */
1012 if (!this || !key_data) {
1013 ret = EFI_INVALID_PARAMETER;
1014 goto out;
1015 }
1016
1017 /* We don't do interrupts, so check for timers cooperatively */
1018 efi_timer_check();
1019
1020 /* Enable console input after ExitBootServices */
1021 efi_cin_check();
1022
1023 if (!key_available) {
Heinrich Schuchardtb6697c22022-09-01 23:30:09 +02001024 memset(key_data, 0, sizeof(struct efi_key_data));
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001025 ret = EFI_NOT_READY;
1026 goto out;
1027 }
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +02001028 /*
1029 * CTRL+A - CTRL+Z have to be signaled as a - z.
1030 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
Heinrich Schuchardt068dc9c2022-09-02 00:49:12 +02001031 * CTRL+\ - CTRL+_ have to be signaled as \ - _.
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +02001032 */
1033 switch (next_key.key.unicode_char) {
1034 case 0x01 ... 0x07:
1035 case 0x0b ... 0x0c:
1036 case 0x0e ... 0x1a:
1037 if (!(next_key.key_state.key_toggle_state &
1038 EFI_CAPS_LOCK_ACTIVE) ^
1039 !(next_key.key_state.key_shift_state &
1040 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
1041 next_key.key.unicode_char += 0x40;
1042 else
1043 next_key.key.unicode_char += 0x60;
Heinrich Schuchardt068dc9c2022-09-02 00:49:12 +02001044 break;
1045 case 0x1c ... 0x1f:
1046 next_key.key.unicode_char += 0x40;
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +02001047 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001048 *key_data = next_key;
1049 key_available = false;
1050 efi_con_in.wait_for_key->is_signaled = false;
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +02001051
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001052out:
1053 return EFI_EXIT(ret);
1054}
1055
1056/**
1057 * efi_cin_set_state() - set toggle key state
1058 *
1059 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +02001060 * @key_toggle_state: pointer to key toggle state
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001061 * Return: status code
1062 *
1063 * This function implements the SetState service of the
1064 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1065 *
1066 * See the Unified Extensible Firmware Interface (UEFI) specification for
1067 * details.
1068 */
1069static efi_status_t EFIAPI efi_cin_set_state(
1070 struct efi_simple_text_input_ex_protocol *this,
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +02001071 u8 *key_toggle_state)
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001072{
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +02001073 EFI_ENTRY("%p, %p", this, key_toggle_state);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001074 /*
1075 * U-Boot supports multiple console input sources like serial and
1076 * net console for which a key toggle state cannot be set at all.
1077 *
1078 * According to the UEFI specification it is allowable to not implement
1079 * this service.
1080 */
1081 return EFI_EXIT(EFI_UNSUPPORTED);
1082}
1083
1084/**
1085 * efi_cin_register_key_notify() - register key notification function
1086 *
1087 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1088 * @key_data: key to be notified
1089 * @key_notify_function: function to be called if the key is pressed
1090 * @notify_handle: handle for unregistering the notification
1091 * Return: status code
1092 *
1093 * This function implements the SetState service of the
1094 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1095 *
1096 * See the Unified Extensible Firmware Interface (UEFI) specification for
1097 * details.
1098 */
1099static efi_status_t EFIAPI efi_cin_register_key_notify(
1100 struct efi_simple_text_input_ex_protocol *this,
1101 struct efi_key_data *key_data,
1102 efi_status_t (EFIAPI *key_notify_function)(
1103 struct efi_key_data *key_data),
1104 void **notify_handle)
1105{
Heinrich Schuchardt695691e2018-09-11 22:38:12 +02001106 efi_status_t ret = EFI_SUCCESS;
1107 struct efi_cin_notify_function *notify_function;
1108
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001109 EFI_ENTRY("%p, %p, %p, %p",
1110 this, key_data, key_notify_function, notify_handle);
Heinrich Schuchardt695691e2018-09-11 22:38:12 +02001111
1112 /* Check parameters */
1113 if (!this || !key_data || !key_notify_function || !notify_handle) {
1114 ret = EFI_INVALID_PARAMETER;
1115 goto out;
1116 }
1117
1118 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
1119 key_data->key.unicode_char,
1120 key_data->key.scan_code,
1121 key_data->key_state.key_shift_state,
1122 key_data->key_state.key_toggle_state);
1123
1124 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
1125 if (!notify_function) {
1126 ret = EFI_OUT_OF_RESOURCES;
1127 goto out;
1128 }
1129 notify_function->key = *key_data;
1130 notify_function->function = key_notify_function;
1131 list_add_tail(&notify_function->link, &cin_notify_functions);
1132 *notify_handle = notify_function;
1133out:
1134 return EFI_EXIT(ret);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001135}
1136
1137/**
1138 * efi_cin_unregister_key_notify() - unregister key notification function
1139 *
1140 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1141 * @notification_handle: handle received when registering
1142 * Return: status code
1143 *
1144 * This function implements the SetState service of the
1145 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1146 *
1147 * See the Unified Extensible Firmware Interface (UEFI) specification for
1148 * details.
1149 */
1150static efi_status_t EFIAPI efi_cin_unregister_key_notify(
1151 struct efi_simple_text_input_ex_protocol *this,
1152 void *notification_handle)
1153{
Heinrich Schuchardt695691e2018-09-11 22:38:12 +02001154 efi_status_t ret = EFI_INVALID_PARAMETER;
1155 struct efi_cin_notify_function *item, *notify_function =
1156 notification_handle;
1157
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001158 EFI_ENTRY("%p, %p", this, notification_handle);
Heinrich Schuchardt695691e2018-09-11 22:38:12 +02001159
1160 /* Check parameters */
1161 if (!this || !notification_handle)
1162 goto out;
1163
1164 list_for_each_entry(item, &cin_notify_functions, link) {
1165 if (item == notify_function) {
1166 ret = EFI_SUCCESS;
1167 break;
1168 }
1169 }
1170 if (ret != EFI_SUCCESS)
1171 goto out;
1172
1173 /* Remove the notify function */
1174 list_del(&notify_function->link);
1175 free(notify_function);
1176out:
1177 return EFI_EXIT(ret);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001178}
1179
1180
1181/**
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001182 * efi_cin_reset() - drain the input buffer
1183 *
1184 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1185 * @extended_verification: allow for exhaustive verification
1186 * Return: status code
1187 *
1188 * This function implements the Reset service of the
1189 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1190 *
1191 * See the Unified Extensible Firmware Interface (UEFI) specification for
1192 * details.
1193 */
1194static efi_status_t EFIAPI efi_cin_reset
1195 (struct efi_simple_text_input_protocol *this,
1196 bool extended_verification)
1197{
1198 efi_status_t ret = EFI_SUCCESS;
1199
1200 EFI_ENTRY("%p, %d", this, extended_verification);
1201
1202 /* Check parameters */
1203 if (!this) {
1204 ret = EFI_INVALID_PARAMETER;
1205 goto out;
1206 }
1207
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001208 efi_cin_empty_buffer();
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001209out:
1210 return EFI_EXIT(ret);
1211}
1212
1213/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001214 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001215 *
1216 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1217 * @key: key read from console
1218 * Return: status code
1219 *
1220 * This function implements the ReadKeyStroke service of the
1221 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1222 *
1223 * See the Unified Extensible Firmware Interface (UEFI) specification for
1224 * details.
1225 */
1226static efi_status_t EFIAPI efi_cin_read_key_stroke
1227 (struct efi_simple_text_input_protocol *this,
1228 struct efi_input_key *key)
1229{
1230 efi_status_t ret = EFI_SUCCESS;
1231
1232 EFI_ENTRY("%p, %p", this, key);
1233
1234 /* Check parameters */
1235 if (!this || !key) {
1236 ret = EFI_INVALID_PARAMETER;
1237 goto out;
1238 }
1239
1240 /* We don't do interrupts, so check for timers cooperatively */
1241 efi_timer_check();
1242
1243 /* Enable console input after ExitBootServices */
1244 efi_cin_check();
1245
1246 if (!key_available) {
1247 ret = EFI_NOT_READY;
1248 goto out;
1249 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001250 *key = next_key.key;
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001251 key_available = false;
1252 efi_con_in.wait_for_key->is_signaled = false;
1253out:
1254 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +01001255}
1256
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001257static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1258 .reset = efi_cin_reset_ex,
1259 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1260 .wait_for_key_ex = NULL,
1261 .set_state = efi_cin_set_state,
1262 .register_key_notify = efi_cin_register_key_notify,
1263 .unregister_key_notify = efi_cin_unregister_key_notify,
1264};
1265
Heinrich Schuchardt3dabffd2018-09-08 10:20:10 +02001266struct efi_simple_text_input_protocol efi_con_in = {
Alexander Graf4aacacc2016-03-04 01:10:00 +01001267 .reset = efi_cin_reset,
1268 .read_key_stroke = efi_cin_read_key_stroke,
1269 .wait_for_key = NULL,
1270};
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001271
1272static struct efi_event *console_timer_event;
1273
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001274/*
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001275 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001276 *
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001277 * @event: console timer event
1278 * @context: not used
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001279 */
xypron.glpk@gmx.de0ce42dc2017-07-20 05:26:07 +02001280static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1281 void *context)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001282{
1283 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001284 efi_cin_check();
1285 EFI_EXIT(EFI_SUCCESS);
1286}
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001287
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001288/**
1289 * efi_key_notify() - notify the wait for key event
1290 *
1291 * @event: wait for key event
1292 * @context: not used
1293 */
1294static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1295{
1296 EFI_ENTRY("%p, %p", event, context);
1297 efi_cin_check();
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001298 EFI_EXIT(EFI_SUCCESS);
1299}
1300
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001301/**
1302 * efi_console_register() - install the console protocols
1303 *
1304 * This function is called from do_bootefi_exec().
Heinrich Schuchardta92e9ab2018-10-02 06:08:26 +02001305 *
1306 * Return: status code
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001307 */
Heinrich Schuchardta92e9ab2018-10-02 06:08:26 +02001308efi_status_t efi_console_register(void)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001309{
1310 efi_status_t r;
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001311 struct efi_device_path *dp;
Rob Clark49f7b4b2017-07-24 10:39:01 -04001312
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001313 /* Install protocols on root node */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03001314 r = efi_install_multiple_protocol_interfaces(&efi_root,
1315 &efi_guid_text_output_protocol,
1316 &efi_con_out,
1317 &efi_guid_text_input_protocol,
1318 &efi_con_in,
1319 &efi_guid_text_input_ex_protocol,
1320 &efi_con_in_ex,
1321 NULL);
Alexander Graf36bab912018-09-04 14:59:11 +02001322
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001323 /* Create console node and install device path protocols */
1324 if (CONFIG_IS_ENABLED(DM_SERIAL)) {
1325 dp = efi_dp_from_uart();
1326 if (!dp)
1327 goto out_of_memory;
Alexander Graf36bab912018-09-04 14:59:11 +02001328
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001329 /* Hook UART up to the device list */
1330 efi_add_handle(&uart_obj);
Alexander Graf36bab912018-09-04 14:59:11 +02001331
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001332 /* Install device path */
1333 r = efi_add_protocol(&uart_obj, &efi_guid_device_path, dp);
1334 if (r != EFI_SUCCESS)
1335 goto out_of_memory;
1336 }
Rob Clark49f7b4b2017-07-24 10:39:01 -04001337
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001338 /* Create console events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001339 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1340 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001341 if (r != EFI_SUCCESS) {
1342 printf("ERROR: Failed to register WaitForKey event\n");
1343 return r;
1344 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001345 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001346 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001347 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001348 &console_timer_event);
1349 if (r != EFI_SUCCESS) {
1350 printf("ERROR: Failed to register console event\n");
1351 return r;
1352 }
1353 /* 5000 ns cycle is sufficient for 2 MBaud */
1354 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1355 if (r != EFI_SUCCESS)
1356 printf("ERROR: Failed to set console timer\n");
1357 return r;
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001358out_of_memory:
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +02001359 printf("ERROR: Out of memory\n");
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001360 return r;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001361}
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001362
1363/**
1364 * efi_console_get_u16_string() - get user input string
1365 *
1366 * @cin: protocol interface to EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1367 * @buf: buffer to store user input string in UTF16
1368 * @count: number of u16 string including NULL terminator that buf has
1369 * @filter_func: callback to filter user input
1370 * @row: row number to locate user input form
1371 * @col: column number to locate user input form
1372 * Return: status code
1373 */
1374efi_status_t efi_console_get_u16_string(struct efi_simple_text_input_protocol *cin,
1375 u16 *buf, efi_uintn_t count,
1376 efi_console_filter_func filter_func,
1377 int row, int col)
1378{
1379 efi_status_t ret;
1380 efi_uintn_t len = 0;
1381 struct efi_input_key key;
1382
1383 printf(ANSI_CURSOR_POSITION
1384 ANSI_CLEAR_LINE_TO_END
1385 ANSI_CURSOR_SHOW, row, col);
1386
Heinrich Schuchardt49dbcaf2022-10-15 12:22:37 +02001387 efi_cin_empty_buffer();
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001388
1389 for (;;) {
1390 do {
1391 ret = EFI_CALL(cin->read_key_stroke(cin, &key));
1392 mdelay(10);
1393 } while (ret == EFI_NOT_READY);
1394
1395 if (key.unicode_char == u'\b') {
1396 if (len > 0)
1397 buf[--len] = u'\0';
1398
1399 printf(ANSI_CURSOR_POSITION
1400 "%ls"
1401 ANSI_CLEAR_LINE_TO_END, row, col, buf);
1402 continue;
1403 } else if (key.unicode_char == u'\r') {
1404 buf[len] = u'\0';
1405 return EFI_SUCCESS;
1406 } else if (key.unicode_char == 0x3 || key.scan_code == 23) {
1407 return EFI_ABORTED;
1408 } else if (key.unicode_char < 0x20) {
1409 /* ignore control codes other than Ctrl+C, '\r' and '\b' */
1410 continue;
1411 } else if (key.scan_code != 0) {
1412 /* only accept single ESC press for cancel */
1413 continue;
1414 }
1415
1416 if (filter_func) {
1417 if (filter_func(&key) != EFI_SUCCESS)
1418 continue;
1419 }
1420
1421 if (len >= (count - 1))
1422 continue;
1423
1424 buf[len] = key.unicode_char;
1425 len++;
1426 printf(ANSI_CURSOR_POSITION "%ls", row, col, buf);
1427 }
1428}