Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application console interface |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 761908e | 2022-06-14 08:02:03 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 10 | #include <ansi.h> |
Rob Clark | 0d138cf | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 11 | #include <charset.h> |
Simon Glass | 37972f4 | 2025-05-24 11:28:21 -0600 | [diff] [blame] | 12 | #include <efi_device_path.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 14 | #include <time.h> |
Rob Clark | 3863b71 | 2017-09-13 18:05:43 -0400 | [diff] [blame] | 15 | #include <dm/device.h> |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 16 | #include <efi_loader.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 17 | #include <env.h> |
Heinrich Schuchardt | 761908e | 2022-06-14 08:02:03 +0200 | [diff] [blame] | 18 | #include <log.h> |
Rob Clark | 3863b71 | 2017-09-13 18:05:43 -0400 | [diff] [blame] | 19 | #include <stdio_dev.h> |
| 20 | #include <video_console.h> |
Heinrich Schuchardt | e5c9317 | 2020-12-27 14:47:50 +0100 | [diff] [blame] | 21 | #include <linux/delay.h> |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 22 | |
Emmanuel Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 23 | #define EFI_COUT_MODE_2 2 |
| 24 | #define EFI_MAX_COUT_MODE 3 |
| 25 | |
| 26 | struct cout_mode { |
| 27 | unsigned long columns; |
| 28 | unsigned long rows; |
| 29 | int present; |
| 30 | }; |
| 31 | |
Heinrich Schuchardt | 81244ea | 2022-02-04 20:47:09 +0100 | [diff] [blame] | 32 | __maybe_unused static struct efi_object uart_obj; |
| 33 | |
Simon Glass | ab31251 | 2025-05-10 14:54:38 +0200 | [diff] [blame] | 34 | /* |
| 35 | * suppress emission of ANSI escape-characters for use by unit tests. Leave it |
| 36 | * as 0 for the default behaviour |
| 37 | */ |
| 38 | static bool no_ansi; |
| 39 | |
| 40 | void efi_console_set_ansi(bool allow_ansi) |
| 41 | { |
| 42 | no_ansi = !allow_ansi; |
| 43 | } |
| 44 | |
Emmanuel Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 45 | static 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 Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 66 | const efi_guid_t efi_guid_text_input_ex_protocol = |
| 67 | EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID; |
Heinrich Schuchardt | 580c13b | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 68 | const efi_guid_t efi_guid_text_input_protocol = |
| 69 | EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 70 | const efi_guid_t efi_guid_text_output_protocol = |
| 71 | EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 72 | |
| 73 | #define cESC '\x1b' |
| 74 | #define ESC "\x1b" |
| 75 | |
Heinrich Schuchardt | 761908e | 2022-06-14 08:02:03 +0200 | [diff] [blame] | 76 | /* |
| 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 82 | static struct simple_text_output_mode efi_con_mode = { |
Emmanuel Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 83 | .max_mode = 1, |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 84 | .mode = 0, |
| 85 | .attribute = 0, |
| 86 | .cursor_column = 0, |
| 87 | .cursor_row = 0, |
| 88 | .cursor_visible = 1, |
| 89 | }; |
| 90 | |
Heinrich Schuchardt | ef6c69e | 2023-03-03 22:04:26 +0100 | [diff] [blame] | 91 | /** |
| 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 Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 99 | static 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 Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 110 | *c = getchar(); |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 114 | /** |
Heinrich Schuchardt | b010250 | 2024-09-18 23:37:28 +0200 | [diff] [blame] | 115 | * term_read_reply() - receive and parse a reply from the terminal |
Heinrich Schuchardt | 77135c2 | 2018-05-16 18:17:38 +0200 | [diff] [blame] | 116 | * |
| 117 | * @n: array of return values |
| 118 | * @num: number of return values expected |
| 119 | * @end_char: character indicating end of terminal message |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 120 | * Return: non-zero indicates error |
Heinrich Schuchardt | 77135c2 | 2018-05-16 18:17:38 +0200 | [diff] [blame] | 121 | */ |
| 122 | static int term_read_reply(int *n, int num, char end_char) |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 123 | { |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 124 | s32 c; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 125 | int i = 0; |
| 126 | |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 127 | if (term_get_char(&c) || c != cESC) |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 128 | return -1; |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 129 | |
| 130 | if (term_get_char(&c) || c != '[') |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 131 | return -1; |
| 132 | |
| 133 | n[0] = 0; |
| 134 | while (1) { |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 135 | 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 145 | return -1; |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* Read one more decimal position */ |
| 149 | n[i] *= 10; |
| 150 | n[i] += c - '0'; |
| 151 | } else { |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 152 | return -1; |
| 153 | } |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 154 | } |
Heinrich Schuchardt | 77135c2 | 2018-05-16 18:17:38 +0200 | [diff] [blame] | 155 | if (i != num - 1) |
| 156 | return -1; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 161 | /** |
| 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 172 | static efi_status_t EFIAPI efi_cout_output_string( |
| 173 | struct efi_simple_text_output_protocol *this, |
Heinrich Schuchardt | 8449428 | 2021-01-05 07:50:09 +0100 | [diff] [blame] | 174 | const u16 *string) |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 175 | { |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 176 | struct simple_text_output_mode *con = &efi_con_mode; |
| 177 | struct cout_mode *mode = &efi_cout_modes[con->mode]; |
Heinrich Schuchardt | a419d1d | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 178 | char *buf, *pos; |
Heinrich Schuchardt | 8449428 | 2021-01-05 07:50:09 +0100 | [diff] [blame] | 179 | const u16 *p; |
Heinrich Schuchardt | a419d1d | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 180 | efi_status_t ret = EFI_SUCCESS; |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 181 | |
Heinrich Schuchardt | a419d1d | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 182 | EFI_ENTRY("%p, %p", this, string); |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 183 | |
Heinrich Schuchardt | e60b298 | 2019-05-18 18:11:54 +0200 | [diff] [blame] | 184 | if (!this || !string) { |
| 185 | ret = EFI_INVALID_PARAMETER; |
| 186 | goto out; |
| 187 | } |
| 188 | |
Heinrich Schuchardt | a419d1d | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 189 | 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 Glass | 380c843a | 2024-09-01 19:18:12 -0600 | [diff] [blame] | 196 | puts(buf); |
Heinrich Schuchardt | a419d1d | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 197 | free(buf); |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 198 | |
Heinrich Schuchardt | 8aaeed9 | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 199 | /* |
| 200 | * Update the cursor position. |
| 201 | * |
| 202 | * The UEFI spec provides advance rules for U+0000, U+0008, U+000A, |
Heinrich Schuchardt | da88da8 | 2019-09-04 21:13:45 +0200 | [diff] [blame] | 203 | * and U000D. All other control characters are ignored. Any non-control |
| 204 | * character increase the column by one. |
Heinrich Schuchardt | 8aaeed9 | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 205 | */ |
| 206 | for (p = string; *p; ++p) { |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 207 | switch (*p) { |
Heinrich Schuchardt | 8aaeed9 | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 208 | case '\b': /* U+0008, backspace */ |
Heinrich Schuchardt | da88da8 | 2019-09-04 21:13:45 +0200 | [diff] [blame] | 209 | if (con->cursor_column) |
| 210 | con->cursor_column--; |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 211 | break; |
Heinrich Schuchardt | 8aaeed9 | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 212 | case '\n': /* U+000A, newline */ |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 213 | con->cursor_column = 0; |
| 214 | con->cursor_row++; |
| 215 | break; |
Heinrich Schuchardt | 8aaeed9 | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 216 | case '\r': /* U+000D, carriage-return */ |
| 217 | con->cursor_column = 0; |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 218 | break; |
Heinrich Schuchardt | 8aaeed9 | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 219 | case 0xd800 ... 0xdbff: |
| 220 | /* |
| 221 | * Ignore high surrogates, we do not want to count a |
| 222 | * Unicode character twice. |
| 223 | */ |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 224 | break; |
| 225 | default: |
Heinrich Schuchardt | da88da8 | 2019-09-04 21:13:45 +0200 | [diff] [blame] | 226 | /* Exclude control codes */ |
| 227 | if (*p > 0x1f) |
| 228 | con->cursor_column++; |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | if (con->cursor_column >= mode->columns) { |
| 232 | con->cursor_column = 0; |
| 233 | con->cursor_row++; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 234 | } |
Heinrich Schuchardt | da88da8 | 2019-09-04 21:13:45 +0200 | [diff] [blame] | 235 | /* |
| 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Heinrich Schuchardt | a419d1d | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 243 | out: |
| 244 | return EFI_EXIT(ret); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 247 | /** |
| 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 261 | static efi_status_t EFIAPI efi_cout_test_string( |
| 262 | struct efi_simple_text_output_protocol *this, |
Heinrich Schuchardt | 8449428 | 2021-01-05 07:50:09 +0100 | [diff] [blame] | 263 | const u16 *string) |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 264 | { |
| 265 | EFI_ENTRY("%p, %p", this, string); |
| 266 | return EFI_EXIT(EFI_SUCCESS); |
| 267 | } |
| 268 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 269 | /** |
| 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 Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 278 | static 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 Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 286 | /** |
Heinrich Schuchardt | ae165ef | 2021-03-16 12:56:57 +0100 | [diff] [blame] | 287 | * query_console_serial() - query serial console size |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 288 | * |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 289 | * 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 Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 292 | * @rows: pointer to return number of rows |
| 293 | * @cols: pointer to return number of columns |
| 294 | * Returns: 0 on success |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 295 | */ |
Rob Clark | 1e94892 | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 296 | static int query_console_serial(int *rows, int *cols) |
| 297 | { |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 298 | int ret = 0; |
| 299 | int n[2]; |
Rob Clark | 1e94892 | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 300 | |
| 301 | /* Empty input buffer */ |
| 302 | while (tstc()) |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 303 | getchar(); |
Rob Clark | 1e94892 | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 304 | |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 305 | /* |
| 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 Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 308 | * man page and the ECMA-48 standard. |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 309 | * |
| 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 Clark | 1e94892 | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 318 | |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 319 | /* Read {rows,cols} */ |
| 320 | if (term_read_reply(n, 2, 'R')) { |
| 321 | ret = 1; |
| 322 | goto out; |
| 323 | } |
Rob Clark | 1e94892 | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 324 | |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 325 | *cols = n[1]; |
| 326 | *rows = n[0]; |
| 327 | out: |
| 328 | printf(ESC "8"); /* Restore cursor position */ |
| 329 | return ret; |
Rob Clark | 1e94892 | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 330 | } |
| 331 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 332 | /** |
Heinrich Schuchardt | ae165ef | 2021-03-16 12:56:57 +0100 | [diff] [blame] | 333 | * 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 | */ |
| 340 | static 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 Schuchardt | 761908e | 2022-06-14 08:02:03 +0200 | [diff] [blame] | 363 | void efi_setup_console_size(void) |
Heinrich Schuchardt | 75b4e69 | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 364 | { |
Alexander Graf | 2879532 | 2018-06-03 15:51:17 +0200 | [diff] [blame] | 365 | int rows = 25, cols = 80; |
Heinrich Schuchardt | ae165ef | 2021-03-16 12:56:57 +0100 | [diff] [blame] | 366 | int ret = -ENODEV; |
Heinrich Schuchardt | 75b4e69 | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 367 | |
Simon Glass | 52cb504 | 2022-10-18 07:46:31 -0600 | [diff] [blame] | 368 | if (IS_ENABLED(CONFIG_VIDEO)) |
Heinrich Schuchardt | ae165ef | 2021-03-16 12:56:57 +0100 | [diff] [blame] | 369 | ret = query_vidconsole(&rows, &cols); |
Simon Glass | ab31251 | 2025-05-10 14:54:38 +0200 | [diff] [blame] | 370 | if (ret) { |
| 371 | if (no_ansi) |
| 372 | ret = 0; |
| 373 | else |
| 374 | ret = query_console_serial(&rows, &cols); |
| 375 | } |
Heinrich Schuchardt | ae165ef | 2021-03-16 12:56:57 +0100 | [diff] [blame] | 376 | if (ret) |
Heinrich Schuchardt | 75b4e69 | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 377 | return; |
Heinrich Schuchardt | 75b4e69 | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 378 | |
Heinrich Schuchardt | 761908e | 2022-06-14 08:02:03 +0200 | [diff] [blame] | 379 | log_debug("Console size %dx%d\n", rows, cols); |
| 380 | |
Heinrich Schuchardt | 75b4e69 | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 381 | /* 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 Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 401 | /** |
| 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 414 | static 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 Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 421 | 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 427 | if (columns) |
Emmanuel Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 428 | *columns = efi_cout_modes[mode_number].columns; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 429 | if (rows) |
Emmanuel Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 430 | *rows = efi_cout_modes[mode_number].rows; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 431 | |
| 432 | return EFI_EXIT(EFI_SUCCESS); |
| 433 | } |
| 434 | |
Rob Clark | 87ef001 | 2017-10-10 08:23:01 -0400 | [diff] [blame] | 435 | static 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 Schuchardt | 6e8eff2 | 2018-09-08 19:57:24 +0200 | [diff] [blame] | 445 | { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/ |
| 446 | { 37, 47 }, /* 7: light gray, map to white */ |
Rob Clark | 87ef001 | 2017-10-10 08:23:01 -0400 | [diff] [blame] | 447 | }; |
| 448 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 449 | /** |
| 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 460 | static efi_status_t EFIAPI efi_cout_set_attribute( |
| 461 | struct efi_simple_text_output_protocol *this, |
| 462 | unsigned long attribute) |
| 463 | { |
Rob Clark | 87ef001 | 2017-10-10 08:23:01 -0400 | [diff] [blame] | 464 | 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 468 | EFI_ENTRY("%p, %lx", this, attribute); |
| 469 | |
Heinrich Schuchardt | 1e12962 | 2019-06-14 07:16:57 +0200 | [diff] [blame] | 470 | efi_con_mode.attribute = attribute; |
Rob Clark | 87ef001 | 2017-10-10 08:23:01 -0400 | [diff] [blame] | 471 | 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Heinrich Schuchardt | 7995cbe | 2019-12-22 07:15:55 +0000 | [diff] [blame] | 479 | /** |
Jan Kiszka | d4d0fec | 2023-01-18 22:24:59 +0100 | [diff] [blame] | 480 | * efi_clear_screen() - clear screen |
Heinrich Schuchardt | a13136f | 2022-10-15 11:13:59 +0200 | [diff] [blame] | 481 | */ |
| 482 | static void efi_clear_screen(void) |
| 483 | { |
Jan Kiszka | d4d0fec | 2023-01-18 22:24:59 +0100 | [diff] [blame] | 484 | 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 Schuchardt | a13136f | 2022-10-15 11:13:59 +0200 | [diff] [blame] | 495 | /* |
| 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 Schuchardt | 7995cbe | 2019-12-22 07:15:55 +0000 | [diff] [blame] | 506 | * |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 507 | * 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 Schuchardt | 7995cbe | 2019-12-22 07:15:55 +0000 | [diff] [blame] | 510 | * |
| 511 | * @this: pointer to the protocol instance |
| 512 | * Return: status code |
| 513 | */ |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 514 | static efi_status_t EFIAPI efi_cout_clear_screen( |
| 515 | struct efi_simple_text_output_protocol *this) |
| 516 | { |
| 517 | EFI_ENTRY("%p", this); |
| 518 | |
Jan Kiszka | fb181e2 | 2023-01-18 22:25:00 +0100 | [diff] [blame] | 519 | /* 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 Schuchardt | a13136f | 2022-10-15 11:13:59 +0200 | [diff] [blame] | 525 | efi_clear_screen(); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 526 | |
| 527 | return EFI_EXIT(EFI_SUCCESS); |
| 528 | } |
| 529 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 530 | /** |
| 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 Schuchardt | 757cc4a | 2019-06-14 07:20:51 +0200 | [diff] [blame] | 541 | static 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 Schuchardt | 6a6afa7 | 2019-09-04 22:46:13 +0200 | [diff] [blame] | 549 | |
| 550 | if (!efi_cout_modes[mode_number].present) |
| 551 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 552 | |
Heinrich Schuchardt | 757cc4a | 2019-06-14 07:20:51 +0200 | [diff] [blame] | 553 | efi_con_mode.mode = mode_number; |
Heinrich Schuchardt | a13136f | 2022-10-15 11:13:59 +0200 | [diff] [blame] | 554 | efi_clear_screen(); |
Heinrich Schuchardt | 757cc4a | 2019-06-14 07:20:51 +0200 | [diff] [blame] | 555 | |
| 556 | return EFI_EXIT(EFI_SUCCESS); |
| 557 | } |
| 558 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 559 | /** |
| 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 Schuchardt | 09866c2 | 2018-07-05 19:58:07 +0200 | [diff] [blame] | 570 | static 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 Schuchardt | 09866c2 | 2018-07-05 19:58:07 +0200 | [diff] [blame] | 576 | /* Set default colors */ |
Heinrich Schuchardt | 1e12962 | 2019-06-14 07:16:57 +0200 | [diff] [blame] | 577 | efi_con_mode.attribute = 0x07; |
Heinrich Schuchardt | 09866c2 | 2018-07-05 19:58:07 +0200 | [diff] [blame] | 578 | printf(ESC "[0;37;40m"); |
Heinrich Schuchardt | 30c9d67 | 2022-04-30 09:05:04 +0200 | [diff] [blame] | 579 | /* Clear screen */ |
Heinrich Schuchardt | a13136f | 2022-10-15 11:13:59 +0200 | [diff] [blame] | 580 | efi_clear_screen(); |
Heinrich Schuchardt | 09866c2 | 2018-07-05 19:58:07 +0200 | [diff] [blame] | 581 | |
| 582 | return EFI_EXIT(EFI_SUCCESS); |
| 583 | } |
| 584 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 585 | /** |
| 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 597 | static 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 Schuchardt | dcac1d9 | 2018-09-14 18:49:26 +0200 | [diff] [blame] | 601 | 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 605 | EFI_ENTRY("%p, %ld, %ld", this, column, row); |
| 606 | |
Heinrich Schuchardt | dcac1d9 | 2018-09-14 18:49:26 +0200 | [diff] [blame] | 607 | /* 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 622 | efi_con_mode.cursor_column = column; |
| 623 | efi_con_mode.cursor_row = row; |
Heinrich Schuchardt | dcac1d9 | 2018-09-14 18:49:26 +0200 | [diff] [blame] | 624 | out: |
| 625 | return EFI_EXIT(ret); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 626 | } |
| 627 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 628 | /** |
| 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 639 | static 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 Schuchardt | 53f26b9 | 2019-06-02 22:54:28 +0200 | [diff] [blame] | 646 | efi_con_mode.cursor_visible = !!enable; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 647 | |
| 648 | return EFI_EXIT(EFI_SUCCESS); |
| 649 | } |
| 650 | |
Heinrich Schuchardt | 580c13b | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 651 | struct efi_simple_text_output_protocol efi_con_out = { |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 652 | .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 Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 664 | /** |
| 665 | * struct efi_cin_notify_function - registered console input notify function |
| 666 | * |
| 667 | * @link: link to list |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 668 | * @key: key to notify |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 669 | * @function: function to call |
| 670 | */ |
| 671 | struct 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 Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 678 | static bool key_available; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 679 | static struct efi_key_data next_key; |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 680 | static LIST_HEAD(cin_notify_functions); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 681 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 682 | /** |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 683 | * set_shift_mask() - set shift mask |
| 684 | * |
| 685 | * @mod: Xterm shift mask |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 686 | * @key_state: receives the state of the shift, alt, control, and logo keys |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 687 | */ |
Heinrich Schuchardt | f2e7d00 | 2023-02-10 08:51:41 +0100 | [diff] [blame] | 688 | static void set_shift_mask(int mod, struct efi_key_state *key_state) |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 689 | { |
| 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 Schuchardt | fea93d6 | 2019-06-16 22:33:20 +0200 | [diff] [blame] | 699 | if (!mod || (mod & 8)) |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 700 | key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED; |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
| 704 | /** |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 705 | * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 706 | * |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 707 | * This gets called when we have already parsed CSI. |
| 708 | * |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 709 | * @key_state: receives the state of the shift, alt, control, and logo keys |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 710 | * Return: the unmodified code |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 711 | */ |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 712 | static int analyze_modifiers(struct efi_key_state *key_state) |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 713 | { |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 714 | int c, mod = 0, ret = 0; |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 715 | |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 716 | c = getchar(); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 717 | |
| 718 | if (c != ';') { |
| 719 | ret = c; |
| 720 | if (c == '~') |
| 721 | goto out; |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 722 | c = getchar(); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 723 | } |
| 724 | for (;;) { |
| 725 | switch (c) { |
| 726 | case '0'...'9': |
| 727 | mod *= 10; |
| 728 | mod += c - '0'; |
| 729 | /* fall through */ |
| 730 | case ';': |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 731 | c = getchar(); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 732 | break; |
| 733 | default: |
| 734 | goto out; |
| 735 | } |
| 736 | } |
| 737 | out: |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 738 | set_shift_mask(mod, key_state); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 739 | if (!ret) |
| 740 | ret = c; |
| 741 | return ret; |
| 742 | } |
| 743 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 744 | /** |
| 745 | * efi_cin_read_key() - read a key from the console input |
| 746 | * |
| 747 | * @key: - key received |
| 748 | * Return: - status code |
| 749 | */ |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 750 | static efi_status_t efi_cin_read_key(struct efi_key_data *key) |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 751 | { |
| 752 | struct efi_input_key pressed_key = { |
| 753 | .scan_code = 0, |
| 754 | .unicode_char = 0, |
| 755 | }; |
Heinrich Schuchardt | fc5f1a1 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 756 | s32 ch; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 757 | |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 758 | if (console_read_unicode(&ch)) |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 759 | return EFI_NOT_READY; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 760 | |
| 761 | key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID; |
| 762 | key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID; |
| 763 | |
Heinrich Schuchardt | fc5f1a1 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 764 | /* We do not support multi-word codes */ |
| 765 | if (ch >= 0x10000) |
| 766 | ch = '?'; |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 767 | |
| 768 | switch (ch) { |
| 769 | case 0x1b: |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 770 | /* |
Heinrich Schuchardt | e5c9317 | 2020-12-27 14:47:50 +0100 | [diff] [blame] | 771 | * 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 Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 782 | * Xterm Control Sequences |
| 783 | * https://www.xfree86.org/4.8.0/ctlseqs.html |
| 784 | */ |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 785 | ch = getchar(); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 786 | switch (ch) { |
| 787 | case cESC: /* ESC */ |
| 788 | pressed_key.scan_code = 23; |
| 789 | break; |
Heinrich Schuchardt | 46e41d0 | 2019-06-16 21:41:13 +0200 | [diff] [blame] | 790 | case 'O': /* F1 - F4, End */ |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 791 | ch = getchar(); |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 792 | /* consider modifiers */ |
Heinrich Schuchardt | 46e41d0 | 2019-06-16 21:41:13 +0200 | [diff] [blame] | 793 | if (ch == 'F') { /* End */ |
| 794 | pressed_key.scan_code = 6; |
| 795 | break; |
| 796 | } else if (ch < 'P') { |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 797 | set_shift_mask(ch - '0', &key->key_state); |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 798 | ch = getchar(); |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 799 | } |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 800 | pressed_key.scan_code = ch - 'P' + 11; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 801 | break; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 802 | case '[': |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 803 | ch = getchar(); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 804 | 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 Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 814 | case '1': |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 815 | ch = analyze_modifiers(&key->key_state); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 816 | switch (ch) { |
| 817 | case '1'...'5': /* F1 - F5 */ |
| 818 | pressed_key.scan_code = ch - '1' + 11; |
| 819 | break; |
Heinrich Schuchardt | 46e41d0 | 2019-06-16 21:41:13 +0200 | [diff] [blame] | 820 | case '6'...'9': /* F5 - F8 */ |
| 821 | pressed_key.scan_code = ch - '6' + 15; |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 822 | break; |
| 823 | case 'A'...'D': /* up, down right, left */ |
| 824 | pressed_key.scan_code = ch - 'A' + 1; |
| 825 | break; |
Heinrich Schuchardt | 46e41d0 | 2019-06-16 21:41:13 +0200 | [diff] [blame] | 826 | case 'F': /* End */ |
| 827 | pressed_key.scan_code = 6; |
| 828 | break; |
| 829 | case 'H': /* Home */ |
| 830 | pressed_key.scan_code = 5; |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 831 | break; |
Heinrich Schuchardt | 46e41d0 | 2019-06-16 21:41:13 +0200 | [diff] [blame] | 832 | case '~': /* Home */ |
| 833 | pressed_key.scan_code = 5; |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 834 | break; |
| 835 | } |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 836 | break; |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 837 | case '2': |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 838 | ch = analyze_modifiers(&key->key_state); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 839 | 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 850 | break; |
| 851 | case '3': /* DEL */ |
| 852 | pressed_key.scan_code = 8; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 853 | analyze_modifiers(&key->key_state); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 854 | break; |
| 855 | case '5': /* PG UP */ |
| 856 | pressed_key.scan_code = 9; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 857 | analyze_modifiers(&key->key_state); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 858 | break; |
| 859 | case '6': /* PG DOWN */ |
| 860 | pressed_key.scan_code = 10; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 861 | analyze_modifiers(&key->key_state); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 862 | break; |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 863 | } /* [ */ |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 864 | break; |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 865 | default: |
| 866 | /* ALT key */ |
| 867 | set_shift_mask(3, &key->key_state); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 868 | } |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 869 | break; |
| 870 | case 0x7f: |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 871 | /* Backspace */ |
| 872 | ch = 0x08; |
| 873 | } |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 874 | if (pressed_key.scan_code) { |
| 875 | key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID; |
| 876 | } else { |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 877 | pressed_key.unicode_char = ch; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 878 | |
| 879 | /* |
| 880 | * Assume left control key for control characters typically |
| 881 | * entered using the control key. |
| 882 | */ |
| 883 | if (ch >= 0x01 && ch <= 0x1f) { |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 884 | key->key_state.key_shift_state |= |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 885 | 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 Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 896 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 897 | return EFI_SUCCESS; |
| 898 | } |
| 899 | |
| 900 | /** |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 901 | * efi_cin_notify() - notify registered functions |
| 902 | */ |
| 903 | static 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 Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 929 | * efi_cin_check() - check if keyboard input is available |
| 930 | */ |
| 931 | static void efi_cin_check(void) |
| 932 | { |
| 933 | efi_status_t ret; |
| 934 | |
| 935 | if (key_available) { |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 936 | efi_signal_event(efi_con_in.wait_for_key); |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 937 | 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 Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 945 | /* Notify registered functions */ |
| 946 | efi_cin_notify(); |
| 947 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 948 | /* Queue the wait for key event */ |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 949 | if (key_available) |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 950 | efi_signal_event(efi_con_in.wait_for_key); |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 951 | } |
| 952 | } |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | /** |
| 956 | * efi_cin_empty_buffer() - empty input buffer |
| 957 | */ |
| 958 | static void efi_cin_empty_buffer(void) |
| 959 | { |
| 960 | while (tstc()) |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 961 | getchar(); |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 962 | 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 | */ |
| 979 | static 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(); |
| 994 | out: |
| 995 | return EFI_EXIT(ret); |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /** |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 999 | * 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 | */ |
| 1011 | static 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 Schuchardt | b6697c2 | 2022-09-01 23:30:09 +0200 | [diff] [blame] | 1032 | memset(key_data, 0, sizeof(struct efi_key_data)); |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1033 | ret = EFI_NOT_READY; |
| 1034 | goto out; |
| 1035 | } |
Heinrich Schuchardt | 00b00a1 | 2019-04-06 20:59:24 +0200 | [diff] [blame] | 1036 | /* |
| 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 Schuchardt | 068dc9c | 2022-09-02 00:49:12 +0200 | [diff] [blame] | 1039 | * CTRL+\ - CTRL+_ have to be signaled as \ - _. |
Heinrich Schuchardt | 00b00a1 | 2019-04-06 20:59:24 +0200 | [diff] [blame] | 1040 | */ |
| 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 Schuchardt | 068dc9c | 2022-09-02 00:49:12 +0200 | [diff] [blame] | 1052 | break; |
| 1053 | case 0x1c ... 0x1f: |
| 1054 | next_key.key.unicode_char += 0x40; |
Heinrich Schuchardt | 00b00a1 | 2019-04-06 20:59:24 +0200 | [diff] [blame] | 1055 | } |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1056 | *key_data = next_key; |
| 1057 | key_available = false; |
| 1058 | efi_con_in.wait_for_key->is_signaled = false; |
Heinrich Schuchardt | 00b00a1 | 2019-04-06 20:59:24 +0200 | [diff] [blame] | 1059 | |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1060 | out: |
| 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 Schuchardt | a41ccf9 | 2019-05-18 17:07:52 +0200 | [diff] [blame] | 1068 | * @key_toggle_state: pointer to key toggle state |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1069 | * 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 | */ |
| 1077 | static efi_status_t EFIAPI efi_cin_set_state( |
| 1078 | struct efi_simple_text_input_ex_protocol *this, |
Heinrich Schuchardt | a41ccf9 | 2019-05-18 17:07:52 +0200 | [diff] [blame] | 1079 | u8 *key_toggle_state) |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1080 | { |
Heinrich Schuchardt | a41ccf9 | 2019-05-18 17:07:52 +0200 | [diff] [blame] | 1081 | EFI_ENTRY("%p, %p", this, key_toggle_state); |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1082 | /* |
| 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 | */ |
| 1107 | static 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 Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 1114 | efi_status_t ret = EFI_SUCCESS; |
| 1115 | struct efi_cin_notify_function *notify_function; |
| 1116 | |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1117 | EFI_ENTRY("%p, %p, %p, %p", |
| 1118 | this, key_data, key_notify_function, notify_handle); |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 1119 | |
| 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(¬ify_function->link, &cin_notify_functions); |
| 1140 | *notify_handle = notify_function; |
| 1141 | out: |
| 1142 | return EFI_EXIT(ret); |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1143 | } |
| 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 | */ |
| 1158 | static efi_status_t EFIAPI efi_cin_unregister_key_notify( |
| 1159 | struct efi_simple_text_input_ex_protocol *this, |
| 1160 | void *notification_handle) |
| 1161 | { |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 1162 | efi_status_t ret = EFI_INVALID_PARAMETER; |
| 1163 | struct efi_cin_notify_function *item, *notify_function = |
| 1164 | notification_handle; |
| 1165 | |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1166 | EFI_ENTRY("%p, %p", this, notification_handle); |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 1167 | |
| 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(¬ify_function->link); |
| 1183 | free(notify_function); |
| 1184 | out: |
| 1185 | return EFI_EXIT(ret); |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1186 | } |
| 1187 | |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1188 | /** |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1189 | * 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 | */ |
| 1201 | static 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 Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1215 | efi_cin_empty_buffer(); |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1216 | out: |
| 1217 | return EFI_EXIT(ret); |
| 1218 | } |
| 1219 | |
| 1220 | /** |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1221 | * efi_cin_read_key_stroke() - read key stroke |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1222 | * |
| 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 | */ |
| 1233 | static 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 Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1257 | *key = next_key.key; |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1258 | key_available = false; |
| 1259 | efi_con_in.wait_for_key->is_signaled = false; |
| 1260 | out: |
| 1261 | return EFI_EXIT(ret); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 1262 | } |
| 1263 | |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1264 | static 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 Schuchardt | 3dabffd | 2018-09-08 10:20:10 +0200 | [diff] [blame] | 1273 | struct efi_simple_text_input_protocol efi_con_in = { |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 1274 | .reset = efi_cin_reset, |
| 1275 | .read_key_stroke = efi_cin_read_key_stroke, |
| 1276 | .wait_for_key = NULL, |
| 1277 | }; |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1278 | |
| 1279 | static struct efi_event *console_timer_event; |
| 1280 | |
Heinrich Schuchardt | d8b878a | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 1281 | /* |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1282 | * efi_console_timer_notify() - notify the console timer event |
Heinrich Schuchardt | d8b878a | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 1283 | * |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1284 | * @event: console timer event |
| 1285 | * @context: not used |
Heinrich Schuchardt | d8b878a | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 1286 | */ |
xypron.glpk@gmx.de | 0ce42dc | 2017-07-20 05:26:07 +0200 | [diff] [blame] | 1287 | static void EFIAPI efi_console_timer_notify(struct efi_event *event, |
| 1288 | void *context) |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1289 | { |
| 1290 | EFI_ENTRY("%p, %p", event, context); |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1291 | efi_cin_check(); |
| 1292 | EFI_EXIT(EFI_SUCCESS); |
| 1293 | } |
Heinrich Schuchardt | d8b878a | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 1294 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1295 | /** |
| 1296 | * efi_key_notify() - notify the wait for key event |
| 1297 | * |
| 1298 | * @event: wait for key event |
| 1299 | * @context: not used |
| 1300 | */ |
| 1301 | static 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.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1305 | EFI_EXIT(EFI_SUCCESS); |
| 1306 | } |
| 1307 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1308 | /** |
| 1309 | * efi_console_register() - install the console protocols |
| 1310 | * |
| 1311 | * This function is called from do_bootefi_exec(). |
Heinrich Schuchardt | a92e9ab | 2018-10-02 06:08:26 +0200 | [diff] [blame] | 1312 | * |
| 1313 | * Return: status code |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1314 | */ |
Heinrich Schuchardt | a92e9ab | 2018-10-02 06:08:26 +0200 | [diff] [blame] | 1315 | efi_status_t efi_console_register(void) |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1316 | { |
| 1317 | efi_status_t r; |
Heinrich Schuchardt | 81244ea | 2022-02-04 20:47:09 +0100 | [diff] [blame] | 1318 | struct efi_device_path *dp; |
Rob Clark | 49f7b4b | 2017-07-24 10:39:01 -0400 | [diff] [blame] | 1319 | |
Heinrich Schuchardt | 81244ea | 2022-02-04 20:47:09 +0100 | [diff] [blame] | 1320 | /* Install protocols on root node */ |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 1321 | 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 Graf | 36bab91 | 2018-09-04 14:59:11 +0200 | [diff] [blame] | 1329 | |
Heinrich Schuchardt | 81244ea | 2022-02-04 20:47:09 +0100 | [diff] [blame] | 1330 | /* 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 Graf | 36bab91 | 2018-09-04 14:59:11 +0200 | [diff] [blame] | 1335 | |
Heinrich Schuchardt | 81244ea | 2022-02-04 20:47:09 +0100 | [diff] [blame] | 1336 | /* Hook UART up to the device list */ |
| 1337 | efi_add_handle(&uart_obj); |
Alexander Graf | 36bab91 | 2018-09-04 14:59:11 +0200 | [diff] [blame] | 1338 | |
Heinrich Schuchardt | 81244ea | 2022-02-04 20:47:09 +0100 | [diff] [blame] | 1339 | /* 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 Clark | 49f7b4b | 2017-07-24 10:39:01 -0400 | [diff] [blame] | 1344 | |
Heinrich Schuchardt | 580c13b | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 1345 | /* Create console events */ |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1346 | r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify, |
| 1347 | NULL, NULL, &efi_con_in.wait_for_key); |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1348 | if (r != EFI_SUCCESS) { |
| 1349 | printf("ERROR: Failed to register WaitForKey event\n"); |
| 1350 | return r; |
| 1351 | } |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1352 | efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key; |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1353 | r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK, |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1354 | efi_console_timer_notify, NULL, NULL, |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1355 | &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 Schuchardt | 580c13b | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 1365 | out_of_memory: |
Heinrich Schuchardt | 6e8eff2 | 2018-09-08 19:57:24 +0200 | [diff] [blame] | 1366 | printf("ERROR: Out of memory\n"); |
Heinrich Schuchardt | 580c13b | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 1367 | return r; |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1368 | } |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1369 | |
| 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 | */ |
| 1381 | efi_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 Schuchardt | 49dbcaf | 2022-10-15 12:22:37 +0200 | [diff] [blame] | 1394 | efi_cin_empty_buffer(); |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1395 | |
| 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 | } |