blob: 953f6831466e2a0551d257053f7c135c56c1d5fa [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>
Rob Clark0d138cf2017-09-09 06:47:40 -040011#include <charset.h>
Simon Glass37972f42025-05-24 11:28:21 -060012#include <efi_device_path.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
Simon Glassab312512025-05-10 14:54:38 +020034/*
35 * suppress emission of ANSI escape-characters for use by unit tests. Leave it
36 * as 0 for the default behaviour
37 */
38static bool no_ansi;
39
40void efi_console_set_ansi(bool allow_ansi)
41{
42 no_ansi = !allow_ansi;
43}
44
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010045static struct cout_mode efi_cout_modes[] = {
46 /* EFI Mode 0 is 80x25 and always present */
47 {
48 .columns = 80,
49 .rows = 25,
50 .present = 1,
51 },
52 /* EFI Mode 1 is always 80x50 */
53 {
54 .columns = 80,
55 .rows = 50,
56 .present = 0,
57 },
58 /* Value are unknown until we query the console */
59 {
60 .columns = 0,
61 .rows = 0,
62 .present = 0,
63 },
64};
65
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +020066const efi_guid_t efi_guid_text_input_ex_protocol =
67 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +020068const efi_guid_t efi_guid_text_input_protocol =
69 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +020070const efi_guid_t efi_guid_text_output_protocol =
71 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
Alexander Graf4aacacc2016-03-04 01:10:00 +010072
73#define cESC '\x1b'
74#define ESC "\x1b"
75
Heinrich Schuchardt761908e2022-06-14 08:02:03 +020076/*
77 * efi_con_mode - mode information of the Simple Text Output Protocol
78 *
79 * Use safe settings before efi_setup_console_size() is called.
80 * By default enable only the 80x25 mode which must always exist.
81 */
Alexander Graf4aacacc2016-03-04 01:10:00 +010082static struct simple_text_output_mode efi_con_mode = {
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010083 .max_mode = 1,
Alexander Graf4aacacc2016-03-04 01:10:00 +010084 .mode = 0,
85 .attribute = 0,
86 .cursor_column = 0,
87 .cursor_row = 0,
88 .cursor_visible = 1,
89};
90
Heinrich Schuchardtef6c69e2023-03-03 22:04:26 +010091/**
92 * term_get_char() - read a character from the console
93 *
94 * Wait for up to 100 ms to read a character from the console.
95 *
96 * @c: pointer to the buffer to receive the character
97 * Return: 0 on success, 1 otherwise
98 */
Matthias Brugger29c6f5f2019-03-05 12:50:18 +010099static int term_get_char(s32 *c)
100{
101 u64 timeout;
102
103 /* Wait up to 100 ms for a character */
104 timeout = timer_get_us() + 100000;
105
106 while (!tstc())
107 if (timer_get_us() > timeout)
108 return 1;
109
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200110 *c = getchar();
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100111 return 0;
112}
113
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200114/**
Heinrich Schuchardtb0102502024-09-18 23:37:28 +0200115 * term_read_reply() - receive and parse a reply from the terminal
Heinrich Schuchardt77135c22018-05-16 18:17:38 +0200116 *
117 * @n: array of return values
118 * @num: number of return values expected
119 * @end_char: character indicating end of terminal message
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200120 * Return: non-zero indicates error
Heinrich Schuchardt77135c22018-05-16 18:17:38 +0200121 */
122static int term_read_reply(int *n, int num, char end_char)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100123{
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100124 s32 c;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100125 int i = 0;
126
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100127 if (term_get_char(&c) || c != cESC)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100128 return -1;
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100129
130 if (term_get_char(&c) || c != '[')
Alexander Graf4aacacc2016-03-04 01:10:00 +0100131 return -1;
132
133 n[0] = 0;
134 while (1) {
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100135 if (!term_get_char(&c)) {
136 if (c == ';') {
137 i++;
138 if (i >= num)
139 return -1;
140 n[i] = 0;
141 continue;
142 } else if (c == end_char) {
143 break;
144 } else if (c > '9' || c < '0') {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100145 return -1;
Matthias Brugger29c6f5f2019-03-05 12:50:18 +0100146 }
147
148 /* Read one more decimal position */
149 n[i] *= 10;
150 n[i] += c - '0';
151 } else {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100152 return -1;
153 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100154 }
Heinrich Schuchardt77135c22018-05-16 18:17:38 +0200155 if (i != num - 1)
156 return -1;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100157
158 return 0;
159}
160
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200161/**
162 * efi_cout_output_string() - write Unicode string to console
163 *
164 * This function implements the OutputString service of the simple text output
165 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
166 * for details.
167 *
168 * @this: simple text output protocol
169 * @string: u16 string
170 * Return: status code
171 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100172static efi_status_t EFIAPI efi_cout_output_string(
173 struct efi_simple_text_output_protocol *this,
Heinrich Schuchardt84494282021-01-05 07:50:09 +0100174 const u16 *string)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100175{
Rob Clark1ef185d2017-09-13 18:05:44 -0400176 struct simple_text_output_mode *con = &efi_con_mode;
177 struct cout_mode *mode = &efi_cout_modes[con->mode];
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200178 char *buf, *pos;
Heinrich Schuchardt84494282021-01-05 07:50:09 +0100179 const u16 *p;
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200180 efi_status_t ret = EFI_SUCCESS;
Rob Clark1ef185d2017-09-13 18:05:44 -0400181
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200182 EFI_ENTRY("%p, %p", this, string);
Rob Clark1ef185d2017-09-13 18:05:44 -0400183
Heinrich Schuchardte60b2982019-05-18 18:11:54 +0200184 if (!this || !string) {
185 ret = EFI_INVALID_PARAMETER;
186 goto out;
187 }
188
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200189 buf = malloc(utf16_utf8_strlen(string) + 1);
190 if (!buf) {
191 ret = EFI_OUT_OF_RESOURCES;
192 goto out;
193 }
194 pos = buf;
195 utf16_utf8_strcpy(&pos, string);
Simon Glass380c843a2024-09-01 19:18:12 -0600196 puts(buf);
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200197 free(buf);
Rob Clark1ef185d2017-09-13 18:05:44 -0400198
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200199 /*
200 * Update the cursor position.
201 *
202 * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200203 * and U000D. All other control characters are ignored. Any non-control
204 * character increase the column by one.
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200205 */
206 for (p = string; *p; ++p) {
Rob Clark1ef185d2017-09-13 18:05:44 -0400207 switch (*p) {
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200208 case '\b': /* U+0008, backspace */
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200209 if (con->cursor_column)
210 con->cursor_column--;
Rob Clark1ef185d2017-09-13 18:05:44 -0400211 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200212 case '\n': /* U+000A, newline */
Rob Clark1ef185d2017-09-13 18:05:44 -0400213 con->cursor_column = 0;
214 con->cursor_row++;
215 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200216 case '\r': /* U+000D, carriage-return */
217 con->cursor_column = 0;
Rob Clark1ef185d2017-09-13 18:05:44 -0400218 break;
Heinrich Schuchardt8aaeed92018-04-29 16:24:25 +0200219 case 0xd800 ... 0xdbff:
220 /*
221 * Ignore high surrogates, we do not want to count a
222 * Unicode character twice.
223 */
Rob Clark1ef185d2017-09-13 18:05:44 -0400224 break;
225 default:
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200226 /* Exclude control codes */
227 if (*p > 0x1f)
228 con->cursor_column++;
Rob Clark1ef185d2017-09-13 18:05:44 -0400229 break;
230 }
231 if (con->cursor_column >= mode->columns) {
232 con->cursor_column = 0;
233 con->cursor_row++;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100234 }
Heinrich Schuchardtda88da82019-09-04 21:13:45 +0200235 /*
236 * When we exceed the row count the terminal will scroll up one
237 * line. We have to adjust the cursor position.
238 */
239 if (con->cursor_row >= mode->rows && con->cursor_row)
240 con->cursor_row--;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100241 }
242
Heinrich Schuchardta419d1d2018-08-31 21:31:32 +0200243out:
244 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100245}
246
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200247/**
248 * efi_cout_test_string() - test writing Unicode string to console
249 *
250 * This function implements the TestString service of the simple text output
251 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
252 * for details.
253 *
254 * As in OutputString we simply convert UTF-16 to UTF-8 there are no unsupported
255 * code points and we can always return EFI_SUCCESS.
256 *
257 * @this: simple text output protocol
258 * @string: u16 string
259 * Return: status code
260 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100261static efi_status_t EFIAPI efi_cout_test_string(
262 struct efi_simple_text_output_protocol *this,
Heinrich Schuchardt84494282021-01-05 07:50:09 +0100263 const u16 *string)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100264{
265 EFI_ENTRY("%p, %p", this, string);
266 return EFI_EXIT(EFI_SUCCESS);
267}
268
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200269/**
270 * cout_mode_matches() - check if mode has given terminal size
271 *
272 * @mode: text mode
273 * @rows: number of rows
274 * @cols: number of columns
275 * Return: true if number of rows and columns matches the mode and
276 * the mode is present
277 */
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100278static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
279{
280 if (!mode->present)
281 return false;
282
283 return (mode->rows == rows) && (mode->columns == cols);
284}
285
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200286/**
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100287 * query_console_serial() - query serial console size
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200288 *
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200289 * When using a serial console or the net console we can only devise the
290 * terminal size by querying the terminal using ECMA-48 control sequences.
291 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200292 * @rows: pointer to return number of rows
293 * @cols: pointer to return number of columns
294 * Returns: 0 on success
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200295 */
Rob Clark1e948922017-09-13 18:05:42 -0400296static int query_console_serial(int *rows, int *cols)
297{
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200298 int ret = 0;
299 int n[2];
Rob Clark1e948922017-09-13 18:05:42 -0400300
301 /* Empty input buffer */
302 while (tstc())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200303 getchar();
Rob Clark1e948922017-09-13 18:05:42 -0400304
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200305 /*
306 * Not all terminals understand CSI [18t for querying the console size.
307 * We should adhere to escape sequences documented in the console_codes
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200308 * man page and the ECMA-48 standard.
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200309 *
310 * So here we follow a different approach. We position the cursor to the
311 * bottom right and query its position. Before leaving the function we
312 * restore the original cursor position.
313 */
314 printf(ESC "7" /* Save cursor position */
315 ESC "[r" /* Set scrolling region to full window */
316 ESC "[999;999H" /* Move to bottom right corner */
317 ESC "[6n"); /* Query cursor position */
Rob Clark1e948922017-09-13 18:05:42 -0400318
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200319 /* Read {rows,cols} */
320 if (term_read_reply(n, 2, 'R')) {
321 ret = 1;
322 goto out;
323 }
Rob Clark1e948922017-09-13 18:05:42 -0400324
Heinrich Schuchardt5965d2e2018-09-15 23:52:07 +0200325 *cols = n[1];
326 *rows = n[0];
327out:
328 printf(ESC "8"); /* Restore cursor position */
329 return ret;
Rob Clark1e948922017-09-13 18:05:42 -0400330}
331
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200332/**
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100333 * query_vidconsole() - query video console size
334 *
335 *
336 * @rows: pointer to return number of rows
337 * @cols: pointer to return number of columns
338 * Returns: 0 on success
339 */
340static int __maybe_unused query_vidconsole(int *rows, int *cols)
341{
342 const char *stdout_name = env_get("stdout");
343 struct stdio_dev *stdout_dev;
344 struct udevice *dev;
345 struct vidconsole_priv *priv;
346
347 if (!stdout_name || strncmp(stdout_name, "vidconsole", 10))
348 return -ENODEV;
349 stdout_dev = stdio_get_by_name("vidconsole");
350 if (!stdout_dev)
351 return -ENODEV;
352 dev = stdout_dev->priv;
353 if (!dev)
354 return -ENODEV;
355 priv = dev_get_uclass_priv(dev);
356 if (!priv)
357 return -ENODEV;
358 *rows = priv->rows;
359 *cols = priv->cols;
360 return 0;
361}
362
Heinrich Schuchardt761908e2022-06-14 08:02:03 +0200363void efi_setup_console_size(void)
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200364{
Alexander Graf28795322018-06-03 15:51:17 +0200365 int rows = 25, cols = 80;
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100366 int ret = -ENODEV;
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200367
Simon Glass52cb5042022-10-18 07:46:31 -0600368 if (IS_ENABLED(CONFIG_VIDEO))
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100369 ret = query_vidconsole(&rows, &cols);
Simon Glassab312512025-05-10 14:54:38 +0200370 if (ret) {
371 if (no_ansi)
372 ret = 0;
373 else
374 ret = query_console_serial(&rows, &cols);
375 }
Heinrich Schuchardtae165ef2021-03-16 12:56:57 +0100376 if (ret)
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200377 return;
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200378
Heinrich Schuchardt761908e2022-06-14 08:02:03 +0200379 log_debug("Console size %dx%d\n", rows, cols);
380
Heinrich Schuchardt75b4e692018-04-29 20:02:46 +0200381 /* Test if we can have Mode 1 */
382 if (cols >= 80 && rows >= 50) {
383 efi_cout_modes[1].present = 1;
384 efi_con_mode.max_mode = 2;
385 }
386
387 /*
388 * Install our mode as mode 2 if it is different
389 * than mode 0 or 1 and set it as the currently selected mode
390 */
391 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
392 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
393 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
394 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
395 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
396 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
397 efi_con_mode.mode = EFI_COUT_MODE_2;
398 }
399}
400
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200401/**
402 * efi_cout_query_mode() - get terminal size for a text mode
403 *
404 * This function implements the QueryMode service of the simple text output
405 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
406 * for details.
407 *
408 * @this: simple text output protocol
409 * @mode_number: mode number to retrieve information on
410 * @columns: number of columns
411 * @rows: number of rows
412 * Return: status code
413 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100414static efi_status_t EFIAPI efi_cout_query_mode(
415 struct efi_simple_text_output_protocol *this,
416 unsigned long mode_number, unsigned long *columns,
417 unsigned long *rows)
418{
419 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
420
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100421 if (mode_number >= efi_con_mode.max_mode)
422 return EFI_EXIT(EFI_UNSUPPORTED);
423
424 if (efi_cout_modes[mode_number].present != 1)
425 return EFI_EXIT(EFI_UNSUPPORTED);
426
Alexander Graf4aacacc2016-03-04 01:10:00 +0100427 if (columns)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100428 *columns = efi_cout_modes[mode_number].columns;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100429 if (rows)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100430 *rows = efi_cout_modes[mode_number].rows;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100431
432 return EFI_EXIT(EFI_SUCCESS);
433}
434
Rob Clark87ef0012017-10-10 08:23:01 -0400435static const struct {
436 unsigned int fg;
437 unsigned int bg;
438} color[] = {
439 { 30, 40 }, /* 0: black */
440 { 34, 44 }, /* 1: blue */
441 { 32, 42 }, /* 2: green */
442 { 36, 46 }, /* 3: cyan */
443 { 31, 41 }, /* 4: red */
444 { 35, 45 }, /* 5: magenta */
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +0200445 { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
446 { 37, 47 }, /* 7: light gray, map to white */
Rob Clark87ef0012017-10-10 08:23:01 -0400447};
448
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200449/**
450 * efi_cout_set_attribute() - set fore- and background color
451 *
452 * This function implements the SetAttribute service of the simple text output
453 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
454 * for details.
455 *
456 * @this: simple text output protocol
457 * @attribute: foreground color - bits 0-3, background color - bits 4-6
458 * Return: status code
459 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100460static efi_status_t EFIAPI efi_cout_set_attribute(
461 struct efi_simple_text_output_protocol *this,
462 unsigned long attribute)
463{
Rob Clark87ef0012017-10-10 08:23:01 -0400464 unsigned int bold = EFI_ATTR_BOLD(attribute);
465 unsigned int fg = EFI_ATTR_FG(attribute);
466 unsigned int bg = EFI_ATTR_BG(attribute);
467
Alexander Graf4aacacc2016-03-04 01:10:00 +0100468 EFI_ENTRY("%p, %lx", this, attribute);
469
Heinrich Schuchardt1e129622019-06-14 07:16:57 +0200470 efi_con_mode.attribute = attribute;
Rob Clark87ef0012017-10-10 08:23:01 -0400471 if (attribute)
472 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
473 else
474 printf(ESC"[0;37;40m");
475
476 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100477}
478
Heinrich Schuchardt7995cbe2019-12-22 07:15:55 +0000479/**
Jan Kiszkad4d0fec2023-01-18 22:24:59 +0100480 * efi_clear_screen() - clear screen
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200481 */
482static void efi_clear_screen(void)
483{
Jan Kiszkad4d0fec2023-01-18 22:24:59 +0100484 if (CONFIG_IS_ENABLED(EFI_SCROLL_ON_CLEAR_SCREEN)) {
485 unsigned int row, screen_rows, screen_columns;
486
487 /* Avoid overwriting previous outputs on streaming consoles */
488 screen_rows = efi_cout_modes[efi_con_mode.mode].rows;
489 screen_columns = efi_cout_modes[efi_con_mode.mode].columns;
490 printf(ESC "[%u;%uH", screen_rows, screen_columns);
491 for (row = 1; row < screen_rows; row++)
492 printf("\n");
493 }
494
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200495 /*
496 * The Linux console wants both a clear and a home command. The video
497 * uclass does not support <ESC>[H without coordinates, yet.
498 */
499 printf(ESC "[2J" ESC "[1;1H");
500 efi_con_mode.cursor_column = 0;
501 efi_con_mode.cursor_row = 0;
502}
503
504/**
505 * efi_cout_clear_screen() - clear screen
Heinrich Schuchardt7995cbe2019-12-22 07:15:55 +0000506 *
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200507 * This function implements the ClearScreen service of the simple text output
508 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
509 * for details.
Heinrich Schuchardt7995cbe2019-12-22 07:15:55 +0000510 *
511 * @this: pointer to the protocol instance
512 * Return: status code
513 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100514static efi_status_t EFIAPI efi_cout_clear_screen(
515 struct efi_simple_text_output_protocol *this)
516{
517 EFI_ENTRY("%p", this);
518
Jan Kiszkafb181e22023-01-18 22:25:00 +0100519 /* Set default colors if not done yet */
520 if (efi_con_mode.attribute == 0) {
521 efi_con_mode.attribute = 0x07;
522 printf(ESC "[0;37;40m");
523 }
524
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200525 efi_clear_screen();
Alexander Graf4aacacc2016-03-04 01:10:00 +0100526
527 return EFI_EXIT(EFI_SUCCESS);
528}
529
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200530/**
531 * efi_cout_clear_set_mode() - set text model
532 *
533 * This function implements the SetMode service of the simple text output
534 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
535 * for details.
536 *
537 * @this: pointer to the protocol instance
538 * @mode_number: number of the text mode to set
539 * Return: status code
540 */
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200541static efi_status_t EFIAPI efi_cout_set_mode(
542 struct efi_simple_text_output_protocol *this,
543 unsigned long mode_number)
544{
545 EFI_ENTRY("%p, %ld", this, mode_number);
546
547 if (mode_number >= efi_con_mode.max_mode)
548 return EFI_EXIT(EFI_UNSUPPORTED);
Heinrich Schuchardt6a6afa72019-09-04 22:46:13 +0200549
550 if (!efi_cout_modes[mode_number].present)
551 return EFI_EXIT(EFI_UNSUPPORTED);
552
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200553 efi_con_mode.mode = mode_number;
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200554 efi_clear_screen();
Heinrich Schuchardt757cc4a2019-06-14 07:20:51 +0200555
556 return EFI_EXIT(EFI_SUCCESS);
557}
558
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200559/**
560 * efi_cout_reset() - reset the terminal
561 *
562 * This function implements the Reset service of the simple text output
563 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
564 * for details.
565 *
566 * @this: pointer to the protocol instance
567 * @extended_verification: if set an extended verification may be executed
568 * Return: status code
569 */
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200570static efi_status_t EFIAPI efi_cout_reset(
571 struct efi_simple_text_output_protocol *this,
572 char extended_verification)
573{
574 EFI_ENTRY("%p, %d", this, extended_verification);
575
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200576 /* Set default colors */
Heinrich Schuchardt1e129622019-06-14 07:16:57 +0200577 efi_con_mode.attribute = 0x07;
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200578 printf(ESC "[0;37;40m");
Heinrich Schuchardt30c9d672022-04-30 09:05:04 +0200579 /* Clear screen */
Heinrich Schuchardta13136f2022-10-15 11:13:59 +0200580 efi_clear_screen();
Heinrich Schuchardt09866c22018-07-05 19:58:07 +0200581
582 return EFI_EXIT(EFI_SUCCESS);
583}
584
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200585/**
586 * efi_cout_set_cursor_position() - reset the terminal
587 *
588 * This function implements the SetCursorPosition service of the simple text
589 * output protocol. See the Unified Extensible Firmware Interface (UEFI)
590 * specification for details.
591 *
592 * @this: pointer to the protocol instance
593 * @column: column to move to
594 * @row: row to move to
595 * Return: status code
596 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100597static efi_status_t EFIAPI efi_cout_set_cursor_position(
598 struct efi_simple_text_output_protocol *this,
599 unsigned long column, unsigned long row)
600{
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200601 efi_status_t ret = EFI_SUCCESS;
602 struct simple_text_output_mode *con = &efi_con_mode;
603 struct cout_mode *mode = &efi_cout_modes[con->mode];
604
Alexander Graf4aacacc2016-03-04 01:10:00 +0100605 EFI_ENTRY("%p, %ld, %ld", this, column, row);
606
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200607 /* Check parameters */
608 if (!this) {
609 ret = EFI_INVALID_PARAMETER;
610 goto out;
611 }
612 if (row >= mode->rows || column >= mode->columns) {
613 ret = EFI_UNSUPPORTED;
614 goto out;
615 }
616
617 /*
618 * Set cursor position by sending CSI H.
619 * EFI origin is [0, 0], terminal origin is [1, 1].
620 */
621 printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100622 efi_con_mode.cursor_column = column;
623 efi_con_mode.cursor_row = row;
Heinrich Schuchardtdcac1d92018-09-14 18:49:26 +0200624out:
625 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100626}
627
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200628/**
629 * efi_cout_enable_cursor() - enable the cursor
630 *
631 * This function implements the EnableCursor service of the simple text output
632 * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
633 * for details.
634 *
635 * @this: pointer to the protocol instance
636 * @enable: if true enable, if false disable the cursor
637 * Return: status code
638 */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100639static efi_status_t EFIAPI efi_cout_enable_cursor(
640 struct efi_simple_text_output_protocol *this,
641 bool enable)
642{
643 EFI_ENTRY("%p, %d", this, enable);
644
645 printf(ESC"[?25%c", enable ? 'h' : 'l');
Heinrich Schuchardt53f26b92019-06-02 22:54:28 +0200646 efi_con_mode.cursor_visible = !!enable;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100647
648 return EFI_EXIT(EFI_SUCCESS);
649}
650
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +0200651struct efi_simple_text_output_protocol efi_con_out = {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100652 .reset = efi_cout_reset,
653 .output_string = efi_cout_output_string,
654 .test_string = efi_cout_test_string,
655 .query_mode = efi_cout_query_mode,
656 .set_mode = efi_cout_set_mode,
657 .set_attribute = efi_cout_set_attribute,
658 .clear_screen = efi_cout_clear_screen,
659 .set_cursor_position = efi_cout_set_cursor_position,
660 .enable_cursor = efi_cout_enable_cursor,
661 .mode = (void*)&efi_con_mode,
662};
663
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200664/**
665 * struct efi_cin_notify_function - registered console input notify function
666 *
667 * @link: link to list
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200668 * @key: key to notify
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200669 * @function: function to call
670 */
671struct efi_cin_notify_function {
672 struct list_head link;
673 struct efi_key_data key;
674 efi_status_t (EFIAPI *function)
675 (struct efi_key_data *key_data);
676};
677
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200678static bool key_available;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200679static struct efi_key_data next_key;
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200680static LIST_HEAD(cin_notify_functions);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100681
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200682/**
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200683 * set_shift_mask() - set shift mask
684 *
685 * @mod: Xterm shift mask
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200686 * @key_state: receives the state of the shift, alt, control, and logo keys
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200687 */
Heinrich Schuchardtf2e7d002023-02-10 08:51:41 +0100688static void set_shift_mask(int mod, struct efi_key_state *key_state)
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200689{
690 key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
691 if (mod) {
692 --mod;
693 if (mod & 1)
694 key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
695 if (mod & 2)
696 key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
697 if (mod & 4)
698 key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
Heinrich Schuchardtfea93d62019-06-16 22:33:20 +0200699 if (!mod || (mod & 8))
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200700 key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200701 }
702}
703
704/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200705 * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200706 *
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100707 * This gets called when we have already parsed CSI.
708 *
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200709 * @key_state: receives the state of the shift, alt, control, and logo keys
Heinrich Schuchardt8bc44ed2020-06-04 18:40:44 +0200710 * Return: the unmodified code
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100711 */
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200712static int analyze_modifiers(struct efi_key_state *key_state)
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100713{
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200714 int c, mod = 0, ret = 0;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100715
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200716 c = getchar();
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100717
718 if (c != ';') {
719 ret = c;
720 if (c == '~')
721 goto out;
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200722 c = getchar();
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100723 }
724 for (;;) {
725 switch (c) {
726 case '0'...'9':
727 mod *= 10;
728 mod += c - '0';
729 /* fall through */
730 case ';':
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200731 c = getchar();
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100732 break;
733 default:
734 goto out;
735 }
736 }
737out:
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200738 set_shift_mask(mod, key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100739 if (!ret)
740 ret = c;
741 return ret;
742}
743
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200744/**
745 * efi_cin_read_key() - read a key from the console input
746 *
747 * @key: - key received
748 * Return: - status code
749 */
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200750static efi_status_t efi_cin_read_key(struct efi_key_data *key)
Alexander Graf4aacacc2016-03-04 01:10:00 +0100751{
752 struct efi_input_key pressed_key = {
753 .scan_code = 0,
754 .unicode_char = 0,
755 };
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200756 s32 ch;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100757
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200758 if (console_read_unicode(&ch))
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200759 return EFI_NOT_READY;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200760
761 key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
762 key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
763
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200764 /* We do not support multi-word codes */
765 if (ch >= 0x10000)
766 ch = '?';
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200767
768 switch (ch) {
769 case 0x1b:
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100770 /*
Heinrich Schuchardte5c93172020-12-27 14:47:50 +0100771 * If a second key is received within 10 ms, assume that we are
772 * dealing with an escape sequence. Otherwise consider this the
773 * escape key being hit. 10 ms is long enough to work fine at
774 * 1200 baud and above.
775 */
776 udelay(10000);
777 if (!tstc()) {
778 pressed_key.scan_code = 23;
779 break;
780 }
781 /*
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100782 * Xterm Control Sequences
783 * https://www.xfree86.org/4.8.0/ctlseqs.html
784 */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200785 ch = getchar();
Alexander Graf4aacacc2016-03-04 01:10:00 +0100786 switch (ch) {
787 case cESC: /* ESC */
788 pressed_key.scan_code = 23;
789 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200790 case 'O': /* F1 - F4, End */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200791 ch = getchar();
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200792 /* consider modifiers */
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200793 if (ch == 'F') { /* End */
794 pressed_key.scan_code = 6;
795 break;
796 } else if (ch < 'P') {
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200797 set_shift_mask(ch - '0', &key->key_state);
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200798 ch = getchar();
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200799 }
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100800 pressed_key.scan_code = ch - 'P' + 11;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100801 break;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100802 case '[':
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200803 ch = getchar();
Alexander Graf4aacacc2016-03-04 01:10:00 +0100804 switch (ch) {
805 case 'A'...'D': /* up, down right, left */
806 pressed_key.scan_code = ch - 'A' + 1;
807 break;
808 case 'F': /* End */
809 pressed_key.scan_code = 6;
810 break;
811 case 'H': /* Home */
812 pressed_key.scan_code = 5;
813 break;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100814 case '1':
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200815 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100816 switch (ch) {
817 case '1'...'5': /* F1 - F5 */
818 pressed_key.scan_code = ch - '1' + 11;
819 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200820 case '6'...'9': /* F5 - F8 */
821 pressed_key.scan_code = ch - '6' + 15;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100822 break;
823 case 'A'...'D': /* up, down right, left */
824 pressed_key.scan_code = ch - 'A' + 1;
825 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200826 case 'F': /* End */
827 pressed_key.scan_code = 6;
828 break;
829 case 'H': /* Home */
830 pressed_key.scan_code = 5;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100831 break;
Heinrich Schuchardt46e41d02019-06-16 21:41:13 +0200832 case '~': /* Home */
833 pressed_key.scan_code = 5;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100834 break;
835 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100836 break;
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100837 case '2':
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200838 ch = analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100839 switch (ch) {
840 case '0'...'1': /* F9 - F10 */
841 pressed_key.scan_code = ch - '0' + 19;
842 break;
843 case '3'...'4': /* F11 - F12 */
844 pressed_key.scan_code = ch - '3' + 21;
845 break;
846 case '~': /* INS */
847 pressed_key.scan_code = 7;
848 break;
849 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100850 break;
851 case '3': /* DEL */
852 pressed_key.scan_code = 8;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200853 analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100854 break;
855 case '5': /* PG UP */
856 pressed_key.scan_code = 9;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200857 analyze_modifiers(&key->key_state);
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100858 break;
859 case '6': /* PG DOWN */
860 pressed_key.scan_code = 10;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200861 analyze_modifiers(&key->key_state);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100862 break;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200863 } /* [ */
Alexander Graf4aacacc2016-03-04 01:10:00 +0100864 break;
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200865 default:
866 /* ALT key */
867 set_shift_mask(3, &key->key_state);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100868 }
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200869 break;
870 case 0x7f:
Alexander Graf4aacacc2016-03-04 01:10:00 +0100871 /* Backspace */
872 ch = 0x08;
873 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200874 if (pressed_key.scan_code) {
875 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
876 } else {
Heinrich Schuchardtaa503da2018-02-19 18:53:29 +0100877 pressed_key.unicode_char = ch;
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200878
879 /*
880 * Assume left control key for control characters typically
881 * entered using the control key.
882 */
883 if (ch >= 0x01 && ch <= 0x1f) {
Heinrich Schuchardt587ae792018-09-11 22:38:09 +0200884 key->key_state.key_shift_state |=
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200885 EFI_SHIFT_STATE_VALID;
886 switch (ch) {
887 case 0x01 ... 0x07:
888 case 0x0b ... 0x0c:
889 case 0x0e ... 0x1f:
890 key->key_state.key_shift_state |=
891 EFI_LEFT_CONTROL_PRESSED;
892 }
893 }
894 }
895 key->key = pressed_key;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100896
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200897 return EFI_SUCCESS;
898}
899
900/**
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200901 * efi_cin_notify() - notify registered functions
902 */
903static void efi_cin_notify(void)
904{
905 struct efi_cin_notify_function *item;
906
907 list_for_each_entry(item, &cin_notify_functions, link) {
908 bool match = true;
909
910 /* We do not support toggle states */
911 if (item->key.key.unicode_char || item->key.key.scan_code) {
912 if (item->key.key.unicode_char !=
913 next_key.key.unicode_char ||
914 item->key.key.scan_code != next_key.key.scan_code)
915 match = false;
916 }
917 if (item->key.key_state.key_shift_state &&
918 item->key.key_state.key_shift_state !=
919 next_key.key_state.key_shift_state)
920 match = false;
921
922 if (match)
923 /* We don't bother about the return code */
924 EFI_CALL(item->function(&next_key));
925 }
926}
927
928/**
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200929 * efi_cin_check() - check if keyboard input is available
930 */
931static void efi_cin_check(void)
932{
933 efi_status_t ret;
934
935 if (key_available) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200936 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200937 return;
938 }
939
940 if (tstc()) {
941 ret = efi_cin_read_key(&next_key);
942 if (ret == EFI_SUCCESS) {
943 key_available = true;
944
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200945 /* Notify registered functions */
946 efi_cin_notify();
947
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200948 /* Queue the wait for key event */
Heinrich Schuchardt695691e2018-09-11 22:38:12 +0200949 if (key_available)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200950 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200951 }
952 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200953}
954
955/**
956 * efi_cin_empty_buffer() - empty input buffer
957 */
958static void efi_cin_empty_buffer(void)
959{
960 while (tstc())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200961 getchar();
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200962 key_available = false;
963}
964
965/**
966 * efi_cin_reset_ex() - reset console input
967 *
968 * @this: - the extended simple text input protocol
969 * @extended_verification: - extended verification
970 *
971 * This function implements the reset service of the
972 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
973 *
974 * See the Unified Extensible Firmware Interface (UEFI) specification for
975 * details.
976 *
977 * Return: old value of the task priority level
978 */
979static efi_status_t EFIAPI efi_cin_reset_ex(
980 struct efi_simple_text_input_ex_protocol *this,
981 bool extended_verification)
982{
983 efi_status_t ret = EFI_SUCCESS;
984
985 EFI_ENTRY("%p, %d", this, extended_verification);
986
987 /* Check parameters */
988 if (!this) {
989 ret = EFI_INVALID_PARAMETER;
990 goto out;
991 }
992
993 efi_cin_empty_buffer();
994out:
995 return EFI_EXIT(ret);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +0200996}
997
998/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +0200999 * efi_cin_read_key_stroke_ex() - read key stroke
1000 *
1001 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1002 * @key_data: key read from console
1003 * Return: status code
1004 *
1005 * This function implements the ReadKeyStrokeEx service of the
1006 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1007 *
1008 * See the Unified Extensible Firmware Interface (UEFI) specification for
1009 * details.
1010 */
1011static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
1012 struct efi_simple_text_input_ex_protocol *this,
1013 struct efi_key_data *key_data)
1014{
1015 efi_status_t ret = EFI_SUCCESS;
1016
1017 EFI_ENTRY("%p, %p", this, key_data);
1018
1019 /* Check parameters */
1020 if (!this || !key_data) {
1021 ret = EFI_INVALID_PARAMETER;
1022 goto out;
1023 }
1024
1025 /* We don't do interrupts, so check for timers cooperatively */
1026 efi_timer_check();
1027
1028 /* Enable console input after ExitBootServices */
1029 efi_cin_check();
1030
1031 if (!key_available) {
Heinrich Schuchardtb6697c22022-09-01 23:30:09 +02001032 memset(key_data, 0, sizeof(struct efi_key_data));
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001033 ret = EFI_NOT_READY;
1034 goto out;
1035 }
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +02001036 /*
1037 * CTRL+A - CTRL+Z have to be signaled as a - z.
1038 * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
Heinrich Schuchardt068dc9c2022-09-02 00:49:12 +02001039 * CTRL+\ - CTRL+_ have to be signaled as \ - _.
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +02001040 */
1041 switch (next_key.key.unicode_char) {
1042 case 0x01 ... 0x07:
1043 case 0x0b ... 0x0c:
1044 case 0x0e ... 0x1a:
1045 if (!(next_key.key_state.key_toggle_state &
1046 EFI_CAPS_LOCK_ACTIVE) ^
1047 !(next_key.key_state.key_shift_state &
1048 (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
1049 next_key.key.unicode_char += 0x40;
1050 else
1051 next_key.key.unicode_char += 0x60;
Heinrich Schuchardt068dc9c2022-09-02 00:49:12 +02001052 break;
1053 case 0x1c ... 0x1f:
1054 next_key.key.unicode_char += 0x40;
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +02001055 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001056 *key_data = next_key;
1057 key_available = false;
1058 efi_con_in.wait_for_key->is_signaled = false;
Heinrich Schuchardt00b00a12019-04-06 20:59:24 +02001059
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001060out:
1061 return EFI_EXIT(ret);
1062}
1063
1064/**
1065 * efi_cin_set_state() - set toggle key state
1066 *
1067 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +02001068 * @key_toggle_state: pointer to key toggle state
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001069 * Return: status code
1070 *
1071 * This function implements the SetState service of the
1072 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1073 *
1074 * See the Unified Extensible Firmware Interface (UEFI) specification for
1075 * details.
1076 */
1077static efi_status_t EFIAPI efi_cin_set_state(
1078 struct efi_simple_text_input_ex_protocol *this,
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +02001079 u8 *key_toggle_state)
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001080{
Heinrich Schuchardta41ccf92019-05-18 17:07:52 +02001081 EFI_ENTRY("%p, %p", this, key_toggle_state);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001082 /*
1083 * U-Boot supports multiple console input sources like serial and
1084 * net console for which a key toggle state cannot be set at all.
1085 *
1086 * According to the UEFI specification it is allowable to not implement
1087 * this service.
1088 */
1089 return EFI_EXIT(EFI_UNSUPPORTED);
1090}
1091
1092/**
1093 * efi_cin_register_key_notify() - register key notification function
1094 *
1095 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1096 * @key_data: key to be notified
1097 * @key_notify_function: function to be called if the key is pressed
1098 * @notify_handle: handle for unregistering the notification
1099 * Return: status code
1100 *
1101 * This function implements the SetState service of the
1102 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1103 *
1104 * See the Unified Extensible Firmware Interface (UEFI) specification for
1105 * details.
1106 */
1107static efi_status_t EFIAPI efi_cin_register_key_notify(
1108 struct efi_simple_text_input_ex_protocol *this,
1109 struct efi_key_data *key_data,
1110 efi_status_t (EFIAPI *key_notify_function)(
1111 struct efi_key_data *key_data),
1112 void **notify_handle)
1113{
Heinrich Schuchardt695691e2018-09-11 22:38:12 +02001114 efi_status_t ret = EFI_SUCCESS;
1115 struct efi_cin_notify_function *notify_function;
1116
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001117 EFI_ENTRY("%p, %p, %p, %p",
1118 this, key_data, key_notify_function, notify_handle);
Heinrich Schuchardt695691e2018-09-11 22:38:12 +02001119
1120 /* Check parameters */
1121 if (!this || !key_data || !key_notify_function || !notify_handle) {
1122 ret = EFI_INVALID_PARAMETER;
1123 goto out;
1124 }
1125
1126 EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
1127 key_data->key.unicode_char,
1128 key_data->key.scan_code,
1129 key_data->key_state.key_shift_state,
1130 key_data->key_state.key_toggle_state);
1131
1132 notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
1133 if (!notify_function) {
1134 ret = EFI_OUT_OF_RESOURCES;
1135 goto out;
1136 }
1137 notify_function->key = *key_data;
1138 notify_function->function = key_notify_function;
1139 list_add_tail(&notify_function->link, &cin_notify_functions);
1140 *notify_handle = notify_function;
1141out:
1142 return EFI_EXIT(ret);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001143}
1144
1145/**
1146 * efi_cin_unregister_key_notify() - unregister key notification function
1147 *
1148 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1149 * @notification_handle: handle received when registering
1150 * Return: status code
1151 *
1152 * This function implements the SetState service of the
1153 * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1154 *
1155 * See the Unified Extensible Firmware Interface (UEFI) specification for
1156 * details.
1157 */
1158static efi_status_t EFIAPI efi_cin_unregister_key_notify(
1159 struct efi_simple_text_input_ex_protocol *this,
1160 void *notification_handle)
1161{
Heinrich Schuchardt695691e2018-09-11 22:38:12 +02001162 efi_status_t ret = EFI_INVALID_PARAMETER;
1163 struct efi_cin_notify_function *item, *notify_function =
1164 notification_handle;
1165
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001166 EFI_ENTRY("%p, %p", this, notification_handle);
Heinrich Schuchardt695691e2018-09-11 22:38:12 +02001167
1168 /* Check parameters */
1169 if (!this || !notification_handle)
1170 goto out;
1171
1172 list_for_each_entry(item, &cin_notify_functions, link) {
1173 if (item == notify_function) {
1174 ret = EFI_SUCCESS;
1175 break;
1176 }
1177 }
1178 if (ret != EFI_SUCCESS)
1179 goto out;
1180
1181 /* Remove the notify function */
1182 list_del(&notify_function->link);
1183 free(notify_function);
1184out:
1185 return EFI_EXIT(ret);
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001186}
1187
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001188/**
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001189 * efi_cin_reset() - drain the input buffer
1190 *
1191 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1192 * @extended_verification: allow for exhaustive verification
1193 * Return: status code
1194 *
1195 * This function implements the Reset service of the
1196 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1197 *
1198 * See the Unified Extensible Firmware Interface (UEFI) specification for
1199 * details.
1200 */
1201static efi_status_t EFIAPI efi_cin_reset
1202 (struct efi_simple_text_input_protocol *this,
1203 bool extended_verification)
1204{
1205 efi_status_t ret = EFI_SUCCESS;
1206
1207 EFI_ENTRY("%p, %d", this, extended_verification);
1208
1209 /* Check parameters */
1210 if (!this) {
1211 ret = EFI_INVALID_PARAMETER;
1212 goto out;
1213 }
1214
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001215 efi_cin_empty_buffer();
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001216out:
1217 return EFI_EXIT(ret);
1218}
1219
1220/**
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001221 * efi_cin_read_key_stroke() - read key stroke
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001222 *
1223 * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1224 * @key: key read from console
1225 * Return: status code
1226 *
1227 * This function implements the ReadKeyStroke service of the
1228 * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1229 *
1230 * See the Unified Extensible Firmware Interface (UEFI) specification for
1231 * details.
1232 */
1233static efi_status_t EFIAPI efi_cin_read_key_stroke
1234 (struct efi_simple_text_input_protocol *this,
1235 struct efi_input_key *key)
1236{
1237 efi_status_t ret = EFI_SUCCESS;
1238
1239 EFI_ENTRY("%p, %p", this, key);
1240
1241 /* Check parameters */
1242 if (!this || !key) {
1243 ret = EFI_INVALID_PARAMETER;
1244 goto out;
1245 }
1246
1247 /* We don't do interrupts, so check for timers cooperatively */
1248 efi_timer_check();
1249
1250 /* Enable console input after ExitBootServices */
1251 efi_cin_check();
1252
1253 if (!key_available) {
1254 ret = EFI_NOT_READY;
1255 goto out;
1256 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001257 *key = next_key.key;
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001258 key_available = false;
1259 efi_con_in.wait_for_key->is_signaled = false;
1260out:
1261 return EFI_EXIT(ret);
Alexander Graf4aacacc2016-03-04 01:10:00 +01001262}
1263
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001264static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1265 .reset = efi_cin_reset_ex,
1266 .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1267 .wait_for_key_ex = NULL,
1268 .set_state = efi_cin_set_state,
1269 .register_key_notify = efi_cin_register_key_notify,
1270 .unregister_key_notify = efi_cin_unregister_key_notify,
1271};
1272
Heinrich Schuchardt3dabffd2018-09-08 10:20:10 +02001273struct efi_simple_text_input_protocol efi_con_in = {
Alexander Graf4aacacc2016-03-04 01:10:00 +01001274 .reset = efi_cin_reset,
1275 .read_key_stroke = efi_cin_read_key_stroke,
1276 .wait_for_key = NULL,
1277};
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001278
1279static struct efi_event *console_timer_event;
1280
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001281/*
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001282 * efi_console_timer_notify() - notify the console timer event
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001283 *
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001284 * @event: console timer event
1285 * @context: not used
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001286 */
xypron.glpk@gmx.de0ce42dc2017-07-20 05:26:07 +02001287static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1288 void *context)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001289{
1290 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001291 efi_cin_check();
1292 EFI_EXIT(EFI_SUCCESS);
1293}
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +01001294
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001295/**
1296 * efi_key_notify() - notify the wait for key event
1297 *
1298 * @event: wait for key event
1299 * @context: not used
1300 */
1301static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1302{
1303 EFI_ENTRY("%p, %p", event, context);
1304 efi_cin_check();
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001305 EFI_EXIT(EFI_SUCCESS);
1306}
1307
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001308/**
1309 * efi_console_register() - install the console protocols
1310 *
1311 * This function is called from do_bootefi_exec().
Heinrich Schuchardta92e9ab2018-10-02 06:08:26 +02001312 *
1313 * Return: status code
Heinrich Schuchardt49574bc2018-09-11 22:38:05 +02001314 */
Heinrich Schuchardta92e9ab2018-10-02 06:08:26 +02001315efi_status_t efi_console_register(void)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001316{
1317 efi_status_t r;
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001318 struct efi_device_path *dp;
Rob Clark49f7b4b2017-07-24 10:39:01 -04001319
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001320 /* Install protocols on root node */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03001321 r = efi_install_multiple_protocol_interfaces(&efi_root,
1322 &efi_guid_text_output_protocol,
1323 &efi_con_out,
1324 &efi_guid_text_input_protocol,
1325 &efi_con_in,
1326 &efi_guid_text_input_ex_protocol,
1327 &efi_con_in_ex,
1328 NULL);
Alexander Graf36bab912018-09-04 14:59:11 +02001329
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001330 /* Create console node and install device path protocols */
1331 if (CONFIG_IS_ENABLED(DM_SERIAL)) {
1332 dp = efi_dp_from_uart();
1333 if (!dp)
1334 goto out_of_memory;
Alexander Graf36bab912018-09-04 14:59:11 +02001335
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001336 /* Hook UART up to the device list */
1337 efi_add_handle(&uart_obj);
Alexander Graf36bab912018-09-04 14:59:11 +02001338
Heinrich Schuchardt81244ea2022-02-04 20:47:09 +01001339 /* Install device path */
1340 r = efi_add_protocol(&uart_obj, &efi_guid_device_path, dp);
1341 if (r != EFI_SUCCESS)
1342 goto out_of_memory;
1343 }
Rob Clark49f7b4b2017-07-24 10:39:01 -04001344
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001345 /* Create console events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001346 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1347 NULL, NULL, &efi_con_in.wait_for_key);
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001348 if (r != EFI_SUCCESS) {
1349 printf("ERROR: Failed to register WaitForKey event\n");
1350 return r;
1351 }
Heinrich Schuchardt6c4cd262018-09-11 22:38:08 +02001352 efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001353 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001354 efi_console_timer_notify, NULL, NULL,
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001355 &console_timer_event);
1356 if (r != EFI_SUCCESS) {
1357 printf("ERROR: Failed to register console event\n");
1358 return r;
1359 }
1360 /* 5000 ns cycle is sufficient for 2 MBaud */
1361 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1362 if (r != EFI_SUCCESS)
1363 printf("ERROR: Failed to set console timer\n");
1364 return r;
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001365out_of_memory:
Heinrich Schuchardt6e8eff22018-09-08 19:57:24 +02001366 printf("ERROR: Out of memory\n");
Heinrich Schuchardt580c13b2017-10-26 19:25:59 +02001367 return r;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +02001368}
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001369
1370/**
1371 * efi_console_get_u16_string() - get user input string
1372 *
1373 * @cin: protocol interface to EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1374 * @buf: buffer to store user input string in UTF16
1375 * @count: number of u16 string including NULL terminator that buf has
1376 * @filter_func: callback to filter user input
1377 * @row: row number to locate user input form
1378 * @col: column number to locate user input form
1379 * Return: status code
1380 */
1381efi_status_t efi_console_get_u16_string(struct efi_simple_text_input_protocol *cin,
1382 u16 *buf, efi_uintn_t count,
1383 efi_console_filter_func filter_func,
1384 int row, int col)
1385{
1386 efi_status_t ret;
1387 efi_uintn_t len = 0;
1388 struct efi_input_key key;
1389
1390 printf(ANSI_CURSOR_POSITION
1391 ANSI_CLEAR_LINE_TO_END
1392 ANSI_CURSOR_SHOW, row, col);
1393
Heinrich Schuchardt49dbcaf2022-10-15 12:22:37 +02001394 efi_cin_empty_buffer();
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09001395
1396 for (;;) {
1397 do {
1398 ret = EFI_CALL(cin->read_key_stroke(cin, &key));
1399 mdelay(10);
1400 } while (ret == EFI_NOT_READY);
1401
1402 if (key.unicode_char == u'\b') {
1403 if (len > 0)
1404 buf[--len] = u'\0';
1405
1406 printf(ANSI_CURSOR_POSITION
1407 "%ls"
1408 ANSI_CLEAR_LINE_TO_END, row, col, buf);
1409 continue;
1410 } else if (key.unicode_char == u'\r') {
1411 buf[len] = u'\0';
1412 return EFI_SUCCESS;
1413 } else if (key.unicode_char == 0x3 || key.scan_code == 23) {
1414 return EFI_ABORTED;
1415 } else if (key.unicode_char < 0x20) {
1416 /* ignore control codes other than Ctrl+C, '\r' and '\b' */
1417 continue;
1418 } else if (key.scan_code != 0) {
1419 /* only accept single ESC press for cancel */
1420 continue;
1421 }
1422
1423 if (filter_func) {
1424 if (filter_func(&key) != EFI_SUCCESS)
1425 continue;
1426 }
1427
1428 if (len >= (count - 1))
1429 continue;
1430
1431 buf[len] = key.unicode_char;
1432 len++;
1433 printf(ANSI_CURSOR_POSITION "%ls", row, col, buf);
1434 }
1435}