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 | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 13 | #include <time.h> |
Rob Clark | 3863b71 | 2017-09-13 18:05:43 -0400 | [diff] [blame] | 14 | #include <dm/device.h> |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 15 | #include <efi_loader.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 16 | #include <env.h> |
Heinrich Schuchardt | 761908e | 2022-06-14 08:02:03 +0200 | [diff] [blame] | 17 | #include <log.h> |
Rob Clark | 3863b71 | 2017-09-13 18:05:43 -0400 | [diff] [blame] | 18 | #include <stdio_dev.h> |
| 19 | #include <video_console.h> |
Heinrich Schuchardt | e5c9317 | 2020-12-27 14:47:50 +0100 | [diff] [blame] | 20 | #include <linux/delay.h> |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 21 | |
Emmanuel Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 22 | #define EFI_COUT_MODE_2 2 |
| 23 | #define EFI_MAX_COUT_MODE 3 |
| 24 | |
| 25 | struct cout_mode { |
| 26 | unsigned long columns; |
| 27 | unsigned long rows; |
| 28 | int present; |
| 29 | }; |
| 30 | |
Heinrich Schuchardt | 81244ea | 2022-02-04 20:47:09 +0100 | [diff] [blame] | 31 | __maybe_unused static struct efi_object uart_obj; |
| 32 | |
Emmanuel Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 33 | static struct cout_mode efi_cout_modes[] = { |
| 34 | /* EFI Mode 0 is 80x25 and always present */ |
| 35 | { |
| 36 | .columns = 80, |
| 37 | .rows = 25, |
| 38 | .present = 1, |
| 39 | }, |
| 40 | /* EFI Mode 1 is always 80x50 */ |
| 41 | { |
| 42 | .columns = 80, |
| 43 | .rows = 50, |
| 44 | .present = 0, |
| 45 | }, |
| 46 | /* Value are unknown until we query the console */ |
| 47 | { |
| 48 | .columns = 0, |
| 49 | .rows = 0, |
| 50 | .present = 0, |
| 51 | }, |
| 52 | }; |
| 53 | |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 54 | const efi_guid_t efi_guid_text_input_ex_protocol = |
| 55 | EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID; |
Heinrich Schuchardt | 580c13b | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 56 | const efi_guid_t efi_guid_text_input_protocol = |
| 57 | EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 58 | const efi_guid_t efi_guid_text_output_protocol = |
| 59 | EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 60 | |
| 61 | #define cESC '\x1b' |
| 62 | #define ESC "\x1b" |
| 63 | |
Heinrich Schuchardt | 761908e | 2022-06-14 08:02:03 +0200 | [diff] [blame] | 64 | /* |
| 65 | * efi_con_mode - mode information of the Simple Text Output Protocol |
| 66 | * |
| 67 | * Use safe settings before efi_setup_console_size() is called. |
| 68 | * By default enable only the 80x25 mode which must always exist. |
| 69 | */ |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 70 | static struct simple_text_output_mode efi_con_mode = { |
Emmanuel Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 71 | .max_mode = 1, |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 72 | .mode = 0, |
| 73 | .attribute = 0, |
| 74 | .cursor_column = 0, |
| 75 | .cursor_row = 0, |
| 76 | .cursor_visible = 1, |
| 77 | }; |
| 78 | |
Heinrich Schuchardt | ef6c69e | 2023-03-03 22:04:26 +0100 | [diff] [blame] | 79 | /** |
| 80 | * term_get_char() - read a character from the console |
| 81 | * |
| 82 | * Wait for up to 100 ms to read a character from the console. |
| 83 | * |
| 84 | * @c: pointer to the buffer to receive the character |
| 85 | * Return: 0 on success, 1 otherwise |
| 86 | */ |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 87 | static int term_get_char(s32 *c) |
| 88 | { |
| 89 | u64 timeout; |
| 90 | |
| 91 | /* Wait up to 100 ms for a character */ |
| 92 | timeout = timer_get_us() + 100000; |
| 93 | |
| 94 | while (!tstc()) |
| 95 | if (timer_get_us() > timeout) |
| 96 | return 1; |
| 97 | |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 98 | *c = getchar(); |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 102 | /** |
Heinrich Schuchardt | 77135c2 | 2018-05-16 18:17:38 +0200 | [diff] [blame] | 103 | * Receive and parse a reply from the terminal. |
| 104 | * |
| 105 | * @n: array of return values |
| 106 | * @num: number of return values expected |
| 107 | * @end_char: character indicating end of terminal message |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 108 | * Return: non-zero indicates error |
Heinrich Schuchardt | 77135c2 | 2018-05-16 18:17:38 +0200 | [diff] [blame] | 109 | */ |
| 110 | static int term_read_reply(int *n, int num, char end_char) |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 111 | { |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 112 | s32 c; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 113 | int i = 0; |
| 114 | |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 115 | if (term_get_char(&c) || c != cESC) |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 116 | return -1; |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 117 | |
| 118 | if (term_get_char(&c) || c != '[') |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 119 | return -1; |
| 120 | |
| 121 | n[0] = 0; |
| 122 | while (1) { |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 123 | if (!term_get_char(&c)) { |
| 124 | if (c == ';') { |
| 125 | i++; |
| 126 | if (i >= num) |
| 127 | return -1; |
| 128 | n[i] = 0; |
| 129 | continue; |
| 130 | } else if (c == end_char) { |
| 131 | break; |
| 132 | } else if (c > '9' || c < '0') { |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 133 | return -1; |
Matthias Brugger | 29c6f5f | 2019-03-05 12:50:18 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* Read one more decimal position */ |
| 137 | n[i] *= 10; |
| 138 | n[i] += c - '0'; |
| 139 | } else { |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 140 | return -1; |
| 141 | } |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 142 | } |
Heinrich Schuchardt | 77135c2 | 2018-05-16 18:17:38 +0200 | [diff] [blame] | 143 | if (i != num - 1) |
| 144 | return -1; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 149 | /** |
| 150 | * efi_cout_output_string() - write Unicode string to console |
| 151 | * |
| 152 | * This function implements the OutputString service of the simple text output |
| 153 | * protocol. See the Unified Extensible Firmware Interface (UEFI) specification |
| 154 | * for details. |
| 155 | * |
| 156 | * @this: simple text output protocol |
| 157 | * @string: u16 string |
| 158 | * Return: status code |
| 159 | */ |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 160 | static efi_status_t EFIAPI efi_cout_output_string( |
| 161 | struct efi_simple_text_output_protocol *this, |
Heinrich Schuchardt | 8449428 | 2021-01-05 07:50:09 +0100 | [diff] [blame] | 162 | const u16 *string) |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 163 | { |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 164 | struct simple_text_output_mode *con = &efi_con_mode; |
| 165 | struct cout_mode *mode = &efi_cout_modes[con->mode]; |
Heinrich Schuchardt | a419d1d | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 166 | char *buf, *pos; |
Heinrich Schuchardt | 8449428 | 2021-01-05 07:50:09 +0100 | [diff] [blame] | 167 | const u16 *p; |
Heinrich Schuchardt | a419d1d | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 168 | efi_status_t ret = EFI_SUCCESS; |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 169 | |
Heinrich Schuchardt | a419d1d | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 170 | EFI_ENTRY("%p, %p", this, string); |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 171 | |
Heinrich Schuchardt | e60b298 | 2019-05-18 18:11:54 +0200 | [diff] [blame] | 172 | if (!this || !string) { |
| 173 | ret = EFI_INVALID_PARAMETER; |
| 174 | goto out; |
| 175 | } |
| 176 | |
Heinrich Schuchardt | a419d1d | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 177 | buf = malloc(utf16_utf8_strlen(string) + 1); |
| 178 | if (!buf) { |
| 179 | ret = EFI_OUT_OF_RESOURCES; |
| 180 | goto out; |
| 181 | } |
| 182 | pos = buf; |
| 183 | utf16_utf8_strcpy(&pos, string); |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 184 | fputs(stdout, buf); |
Heinrich Schuchardt | a419d1d | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 185 | free(buf); |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 186 | |
Heinrich Schuchardt | 8aaeed9 | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 187 | /* |
| 188 | * Update the cursor position. |
| 189 | * |
| 190 | * 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] | 191 | * and U000D. All other control characters are ignored. Any non-control |
| 192 | * character increase the column by one. |
Heinrich Schuchardt | 8aaeed9 | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 193 | */ |
| 194 | for (p = string; *p; ++p) { |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 195 | switch (*p) { |
Heinrich Schuchardt | 8aaeed9 | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 196 | case '\b': /* U+0008, backspace */ |
Heinrich Schuchardt | da88da8 | 2019-09-04 21:13:45 +0200 | [diff] [blame] | 197 | if (con->cursor_column) |
| 198 | con->cursor_column--; |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 199 | break; |
Heinrich Schuchardt | 8aaeed9 | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 200 | case '\n': /* U+000A, newline */ |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 201 | con->cursor_column = 0; |
| 202 | con->cursor_row++; |
| 203 | break; |
Heinrich Schuchardt | 8aaeed9 | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 204 | case '\r': /* U+000D, carriage-return */ |
| 205 | con->cursor_column = 0; |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 206 | break; |
Heinrich Schuchardt | 8aaeed9 | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 207 | case 0xd800 ... 0xdbff: |
| 208 | /* |
| 209 | * Ignore high surrogates, we do not want to count a |
| 210 | * Unicode character twice. |
| 211 | */ |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 212 | break; |
| 213 | default: |
Heinrich Schuchardt | da88da8 | 2019-09-04 21:13:45 +0200 | [diff] [blame] | 214 | /* Exclude control codes */ |
| 215 | if (*p > 0x1f) |
| 216 | con->cursor_column++; |
Rob Clark | 1ef185d | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 217 | break; |
| 218 | } |
| 219 | if (con->cursor_column >= mode->columns) { |
| 220 | con->cursor_column = 0; |
| 221 | con->cursor_row++; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 222 | } |
Heinrich Schuchardt | da88da8 | 2019-09-04 21:13:45 +0200 | [diff] [blame] | 223 | /* |
| 224 | * When we exceed the row count the terminal will scroll up one |
| 225 | * line. We have to adjust the cursor position. |
| 226 | */ |
| 227 | if (con->cursor_row >= mode->rows && con->cursor_row) |
| 228 | con->cursor_row--; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Heinrich Schuchardt | a419d1d | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 231 | out: |
| 232 | return EFI_EXIT(ret); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 235 | /** |
| 236 | * efi_cout_test_string() - test writing Unicode string to console |
| 237 | * |
| 238 | * This function implements the TestString service of the simple text output |
| 239 | * protocol. See the Unified Extensible Firmware Interface (UEFI) specification |
| 240 | * for details. |
| 241 | * |
| 242 | * As in OutputString we simply convert UTF-16 to UTF-8 there are no unsupported |
| 243 | * code points and we can always return EFI_SUCCESS. |
| 244 | * |
| 245 | * @this: simple text output protocol |
| 246 | * @string: u16 string |
| 247 | * Return: status code |
| 248 | */ |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 249 | static efi_status_t EFIAPI efi_cout_test_string( |
| 250 | struct efi_simple_text_output_protocol *this, |
Heinrich Schuchardt | 8449428 | 2021-01-05 07:50:09 +0100 | [diff] [blame] | 251 | const u16 *string) |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 252 | { |
| 253 | EFI_ENTRY("%p, %p", this, string); |
| 254 | return EFI_EXIT(EFI_SUCCESS); |
| 255 | } |
| 256 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 257 | /** |
| 258 | * cout_mode_matches() - check if mode has given terminal size |
| 259 | * |
| 260 | * @mode: text mode |
| 261 | * @rows: number of rows |
| 262 | * @cols: number of columns |
| 263 | * Return: true if number of rows and columns matches the mode and |
| 264 | * the mode is present |
| 265 | */ |
Emmanuel Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 266 | static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols) |
| 267 | { |
| 268 | if (!mode->present) |
| 269 | return false; |
| 270 | |
| 271 | return (mode->rows == rows) && (mode->columns == cols); |
| 272 | } |
| 273 | |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 274 | /** |
Heinrich Schuchardt | ae165ef | 2021-03-16 12:56:57 +0100 | [diff] [blame] | 275 | * query_console_serial() - query serial console size |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 276 | * |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 277 | * When using a serial console or the net console we can only devise the |
| 278 | * terminal size by querying the terminal using ECMA-48 control sequences. |
| 279 | * |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 280 | * @rows: pointer to return number of rows |
| 281 | * @cols: pointer to return number of columns |
| 282 | * Returns: 0 on success |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 283 | */ |
Rob Clark | 1e94892 | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 284 | static int query_console_serial(int *rows, int *cols) |
| 285 | { |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 286 | int ret = 0; |
| 287 | int n[2]; |
Rob Clark | 1e94892 | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 288 | |
| 289 | /* Empty input buffer */ |
| 290 | while (tstc()) |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 291 | getchar(); |
Rob Clark | 1e94892 | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 292 | |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 293 | /* |
| 294 | * Not all terminals understand CSI [18t for querying the console size. |
| 295 | * We should adhere to escape sequences documented in the console_codes |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 296 | * man page and the ECMA-48 standard. |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 297 | * |
| 298 | * So here we follow a different approach. We position the cursor to the |
| 299 | * bottom right and query its position. Before leaving the function we |
| 300 | * restore the original cursor position. |
| 301 | */ |
| 302 | printf(ESC "7" /* Save cursor position */ |
| 303 | ESC "[r" /* Set scrolling region to full window */ |
| 304 | ESC "[999;999H" /* Move to bottom right corner */ |
| 305 | ESC "[6n"); /* Query cursor position */ |
Rob Clark | 1e94892 | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 306 | |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 307 | /* Read {rows,cols} */ |
| 308 | if (term_read_reply(n, 2, 'R')) { |
| 309 | ret = 1; |
| 310 | goto out; |
| 311 | } |
Rob Clark | 1e94892 | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 312 | |
Heinrich Schuchardt | 5965d2e | 2018-09-15 23:52:07 +0200 | [diff] [blame] | 313 | *cols = n[1]; |
| 314 | *rows = n[0]; |
| 315 | out: |
| 316 | printf(ESC "8"); /* Restore cursor position */ |
| 317 | return ret; |
Rob Clark | 1e94892 | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 318 | } |
| 319 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 320 | /** |
Heinrich Schuchardt | ae165ef | 2021-03-16 12:56:57 +0100 | [diff] [blame] | 321 | * query_vidconsole() - query video console size |
| 322 | * |
| 323 | * |
| 324 | * @rows: pointer to return number of rows |
| 325 | * @cols: pointer to return number of columns |
| 326 | * Returns: 0 on success |
| 327 | */ |
| 328 | static int __maybe_unused query_vidconsole(int *rows, int *cols) |
| 329 | { |
| 330 | const char *stdout_name = env_get("stdout"); |
| 331 | struct stdio_dev *stdout_dev; |
| 332 | struct udevice *dev; |
| 333 | struct vidconsole_priv *priv; |
| 334 | |
| 335 | if (!stdout_name || strncmp(stdout_name, "vidconsole", 10)) |
| 336 | return -ENODEV; |
| 337 | stdout_dev = stdio_get_by_name("vidconsole"); |
| 338 | if (!stdout_dev) |
| 339 | return -ENODEV; |
| 340 | dev = stdout_dev->priv; |
| 341 | if (!dev) |
| 342 | return -ENODEV; |
| 343 | priv = dev_get_uclass_priv(dev); |
| 344 | if (!priv) |
| 345 | return -ENODEV; |
| 346 | *rows = priv->rows; |
| 347 | *cols = priv->cols; |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | /** |
Heinrich Schuchardt | 761908e | 2022-06-14 08:02:03 +0200 | [diff] [blame] | 352 | * efi_setup_console_size() - update the mode table. |
Heinrich Schuchardt | 75b4e69 | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 353 | * |
| 354 | * By default the only mode available is 80x25. If the console has at least 50 |
| 355 | * lines, enable mode 80x50. If we can query the console size and it is neither |
| 356 | * 80x25 nor 80x50, set it as an additional mode. |
| 357 | */ |
Heinrich Schuchardt | 761908e | 2022-06-14 08:02:03 +0200 | [diff] [blame] | 358 | void efi_setup_console_size(void) |
Heinrich Schuchardt | 75b4e69 | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 359 | { |
Alexander Graf | 2879532 | 2018-06-03 15:51:17 +0200 | [diff] [blame] | 360 | int rows = 25, cols = 80; |
Heinrich Schuchardt | ae165ef | 2021-03-16 12:56:57 +0100 | [diff] [blame] | 361 | int ret = -ENODEV; |
Heinrich Schuchardt | 75b4e69 | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 362 | |
Simon Glass | 52cb504 | 2022-10-18 07:46:31 -0600 | [diff] [blame] | 363 | if (IS_ENABLED(CONFIG_VIDEO)) |
Heinrich Schuchardt | ae165ef | 2021-03-16 12:56:57 +0100 | [diff] [blame] | 364 | ret = query_vidconsole(&rows, &cols); |
| 365 | if (ret) |
| 366 | ret = query_console_serial(&rows, &cols); |
| 367 | if (ret) |
Heinrich Schuchardt | 75b4e69 | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 368 | return; |
Heinrich Schuchardt | 75b4e69 | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 369 | |
Heinrich Schuchardt | 761908e | 2022-06-14 08:02:03 +0200 | [diff] [blame] | 370 | log_debug("Console size %dx%d\n", rows, cols); |
| 371 | |
Heinrich Schuchardt | 75b4e69 | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 372 | /* Test if we can have Mode 1 */ |
| 373 | if (cols >= 80 && rows >= 50) { |
| 374 | efi_cout_modes[1].present = 1; |
| 375 | efi_con_mode.max_mode = 2; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Install our mode as mode 2 if it is different |
| 380 | * than mode 0 or 1 and set it as the currently selected mode |
| 381 | */ |
| 382 | if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) && |
| 383 | !cout_mode_matches(&efi_cout_modes[1], rows, cols)) { |
| 384 | efi_cout_modes[EFI_COUT_MODE_2].columns = cols; |
| 385 | efi_cout_modes[EFI_COUT_MODE_2].rows = rows; |
| 386 | efi_cout_modes[EFI_COUT_MODE_2].present = 1; |
| 387 | efi_con_mode.max_mode = EFI_MAX_COUT_MODE; |
| 388 | efi_con_mode.mode = EFI_COUT_MODE_2; |
| 389 | } |
| 390 | } |
| 391 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 392 | /** |
| 393 | * efi_cout_query_mode() - get terminal size for a text mode |
| 394 | * |
| 395 | * This function implements the QueryMode service of the simple text output |
| 396 | * protocol. See the Unified Extensible Firmware Interface (UEFI) specification |
| 397 | * for details. |
| 398 | * |
| 399 | * @this: simple text output protocol |
| 400 | * @mode_number: mode number to retrieve information on |
| 401 | * @columns: number of columns |
| 402 | * @rows: number of rows |
| 403 | * Return: status code |
| 404 | */ |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 405 | static efi_status_t EFIAPI efi_cout_query_mode( |
| 406 | struct efi_simple_text_output_protocol *this, |
| 407 | unsigned long mode_number, unsigned long *columns, |
| 408 | unsigned long *rows) |
| 409 | { |
| 410 | EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows); |
| 411 | |
Emmanuel Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 412 | if (mode_number >= efi_con_mode.max_mode) |
| 413 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 414 | |
| 415 | if (efi_cout_modes[mode_number].present != 1) |
| 416 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 417 | |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 418 | if (columns) |
Emmanuel Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 419 | *columns = efi_cout_modes[mode_number].columns; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 420 | if (rows) |
Emmanuel Vadot | a074d5d | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 421 | *rows = efi_cout_modes[mode_number].rows; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 422 | |
| 423 | return EFI_EXIT(EFI_SUCCESS); |
| 424 | } |
| 425 | |
Rob Clark | 87ef001 | 2017-10-10 08:23:01 -0400 | [diff] [blame] | 426 | static const struct { |
| 427 | unsigned int fg; |
| 428 | unsigned int bg; |
| 429 | } color[] = { |
| 430 | { 30, 40 }, /* 0: black */ |
| 431 | { 34, 44 }, /* 1: blue */ |
| 432 | { 32, 42 }, /* 2: green */ |
| 433 | { 36, 46 }, /* 3: cyan */ |
| 434 | { 31, 41 }, /* 4: red */ |
| 435 | { 35, 45 }, /* 5: magenta */ |
Heinrich Schuchardt | 6e8eff2 | 2018-09-08 19:57:24 +0200 | [diff] [blame] | 436 | { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/ |
| 437 | { 37, 47 }, /* 7: light gray, map to white */ |
Rob Clark | 87ef001 | 2017-10-10 08:23:01 -0400 | [diff] [blame] | 438 | }; |
| 439 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 440 | /** |
| 441 | * efi_cout_set_attribute() - set fore- and background color |
| 442 | * |
| 443 | * This function implements the SetAttribute service of the simple text output |
| 444 | * protocol. See the Unified Extensible Firmware Interface (UEFI) specification |
| 445 | * for details. |
| 446 | * |
| 447 | * @this: simple text output protocol |
| 448 | * @attribute: foreground color - bits 0-3, background color - bits 4-6 |
| 449 | * Return: status code |
| 450 | */ |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 451 | static efi_status_t EFIAPI efi_cout_set_attribute( |
| 452 | struct efi_simple_text_output_protocol *this, |
| 453 | unsigned long attribute) |
| 454 | { |
Rob Clark | 87ef001 | 2017-10-10 08:23:01 -0400 | [diff] [blame] | 455 | unsigned int bold = EFI_ATTR_BOLD(attribute); |
| 456 | unsigned int fg = EFI_ATTR_FG(attribute); |
| 457 | unsigned int bg = EFI_ATTR_BG(attribute); |
| 458 | |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 459 | EFI_ENTRY("%p, %lx", this, attribute); |
| 460 | |
Heinrich Schuchardt | 1e12962 | 2019-06-14 07:16:57 +0200 | [diff] [blame] | 461 | efi_con_mode.attribute = attribute; |
Rob Clark | 87ef001 | 2017-10-10 08:23:01 -0400 | [diff] [blame] | 462 | if (attribute) |
| 463 | printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg); |
| 464 | else |
| 465 | printf(ESC"[0;37;40m"); |
| 466 | |
| 467 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 468 | } |
| 469 | |
Heinrich Schuchardt | 7995cbe | 2019-12-22 07:15:55 +0000 | [diff] [blame] | 470 | /** |
Jan Kiszka | d4d0fec | 2023-01-18 22:24:59 +0100 | [diff] [blame] | 471 | * efi_clear_screen() - clear screen |
Heinrich Schuchardt | a13136f | 2022-10-15 11:13:59 +0200 | [diff] [blame] | 472 | */ |
| 473 | static void efi_clear_screen(void) |
| 474 | { |
Jan Kiszka | d4d0fec | 2023-01-18 22:24:59 +0100 | [diff] [blame] | 475 | if (CONFIG_IS_ENABLED(EFI_SCROLL_ON_CLEAR_SCREEN)) { |
| 476 | unsigned int row, screen_rows, screen_columns; |
| 477 | |
| 478 | /* Avoid overwriting previous outputs on streaming consoles */ |
| 479 | screen_rows = efi_cout_modes[efi_con_mode.mode].rows; |
| 480 | screen_columns = efi_cout_modes[efi_con_mode.mode].columns; |
| 481 | printf(ESC "[%u;%uH", screen_rows, screen_columns); |
| 482 | for (row = 1; row < screen_rows; row++) |
| 483 | printf("\n"); |
| 484 | } |
| 485 | |
Heinrich Schuchardt | a13136f | 2022-10-15 11:13:59 +0200 | [diff] [blame] | 486 | /* |
| 487 | * The Linux console wants both a clear and a home command. The video |
| 488 | * uclass does not support <ESC>[H without coordinates, yet. |
| 489 | */ |
| 490 | printf(ESC "[2J" ESC "[1;1H"); |
| 491 | efi_con_mode.cursor_column = 0; |
| 492 | efi_con_mode.cursor_row = 0; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * efi_cout_clear_screen() - clear screen |
Heinrich Schuchardt | 7995cbe | 2019-12-22 07:15:55 +0000 | [diff] [blame] | 497 | * |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 498 | * This function implements the ClearScreen service of the simple text output |
| 499 | * protocol. See the Unified Extensible Firmware Interface (UEFI) specification |
| 500 | * for details. |
Heinrich Schuchardt | 7995cbe | 2019-12-22 07:15:55 +0000 | [diff] [blame] | 501 | * |
| 502 | * @this: pointer to the protocol instance |
| 503 | * Return: status code |
| 504 | */ |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 505 | static efi_status_t EFIAPI efi_cout_clear_screen( |
| 506 | struct efi_simple_text_output_protocol *this) |
| 507 | { |
| 508 | EFI_ENTRY("%p", this); |
| 509 | |
Jan Kiszka | fb181e2 | 2023-01-18 22:25:00 +0100 | [diff] [blame] | 510 | /* Set default colors if not done yet */ |
| 511 | if (efi_con_mode.attribute == 0) { |
| 512 | efi_con_mode.attribute = 0x07; |
| 513 | printf(ESC "[0;37;40m"); |
| 514 | } |
| 515 | |
Heinrich Schuchardt | a13136f | 2022-10-15 11:13:59 +0200 | [diff] [blame] | 516 | efi_clear_screen(); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 517 | |
| 518 | return EFI_EXIT(EFI_SUCCESS); |
| 519 | } |
| 520 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 521 | /** |
| 522 | * efi_cout_clear_set_mode() - set text model |
| 523 | * |
| 524 | * This function implements the SetMode service of the simple text output |
| 525 | * protocol. See the Unified Extensible Firmware Interface (UEFI) specification |
| 526 | * for details. |
| 527 | * |
| 528 | * @this: pointer to the protocol instance |
| 529 | * @mode_number: number of the text mode to set |
| 530 | * Return: status code |
| 531 | */ |
Heinrich Schuchardt | 757cc4a | 2019-06-14 07:20:51 +0200 | [diff] [blame] | 532 | static efi_status_t EFIAPI efi_cout_set_mode( |
| 533 | struct efi_simple_text_output_protocol *this, |
| 534 | unsigned long mode_number) |
| 535 | { |
| 536 | EFI_ENTRY("%p, %ld", this, mode_number); |
| 537 | |
| 538 | if (mode_number >= efi_con_mode.max_mode) |
| 539 | return EFI_EXIT(EFI_UNSUPPORTED); |
Heinrich Schuchardt | 6a6afa7 | 2019-09-04 22:46:13 +0200 | [diff] [blame] | 540 | |
| 541 | if (!efi_cout_modes[mode_number].present) |
| 542 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 543 | |
Heinrich Schuchardt | 757cc4a | 2019-06-14 07:20:51 +0200 | [diff] [blame] | 544 | efi_con_mode.mode = mode_number; |
Heinrich Schuchardt | a13136f | 2022-10-15 11:13:59 +0200 | [diff] [blame] | 545 | efi_clear_screen(); |
Heinrich Schuchardt | 757cc4a | 2019-06-14 07:20:51 +0200 | [diff] [blame] | 546 | |
| 547 | return EFI_EXIT(EFI_SUCCESS); |
| 548 | } |
| 549 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 550 | /** |
| 551 | * efi_cout_reset() - reset the terminal |
| 552 | * |
| 553 | * This function implements the Reset service of the simple text output |
| 554 | * protocol. See the Unified Extensible Firmware Interface (UEFI) specification |
| 555 | * for details. |
| 556 | * |
| 557 | * @this: pointer to the protocol instance |
| 558 | * @extended_verification: if set an extended verification may be executed |
| 559 | * Return: status code |
| 560 | */ |
Heinrich Schuchardt | 09866c2 | 2018-07-05 19:58:07 +0200 | [diff] [blame] | 561 | static efi_status_t EFIAPI efi_cout_reset( |
| 562 | struct efi_simple_text_output_protocol *this, |
| 563 | char extended_verification) |
| 564 | { |
| 565 | EFI_ENTRY("%p, %d", this, extended_verification); |
| 566 | |
Heinrich Schuchardt | 09866c2 | 2018-07-05 19:58:07 +0200 | [diff] [blame] | 567 | /* Set default colors */ |
Heinrich Schuchardt | 1e12962 | 2019-06-14 07:16:57 +0200 | [diff] [blame] | 568 | efi_con_mode.attribute = 0x07; |
Heinrich Schuchardt | 09866c2 | 2018-07-05 19:58:07 +0200 | [diff] [blame] | 569 | printf(ESC "[0;37;40m"); |
Heinrich Schuchardt | 30c9d67 | 2022-04-30 09:05:04 +0200 | [diff] [blame] | 570 | /* Clear screen */ |
Heinrich Schuchardt | a13136f | 2022-10-15 11:13:59 +0200 | [diff] [blame] | 571 | efi_clear_screen(); |
Heinrich Schuchardt | 09866c2 | 2018-07-05 19:58:07 +0200 | [diff] [blame] | 572 | |
| 573 | return EFI_EXIT(EFI_SUCCESS); |
| 574 | } |
| 575 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 576 | /** |
| 577 | * efi_cout_set_cursor_position() - reset the terminal |
| 578 | * |
| 579 | * This function implements the SetCursorPosition service of the simple text |
| 580 | * output protocol. See the Unified Extensible Firmware Interface (UEFI) |
| 581 | * specification for details. |
| 582 | * |
| 583 | * @this: pointer to the protocol instance |
| 584 | * @column: column to move to |
| 585 | * @row: row to move to |
| 586 | * Return: status code |
| 587 | */ |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 588 | static efi_status_t EFIAPI efi_cout_set_cursor_position( |
| 589 | struct efi_simple_text_output_protocol *this, |
| 590 | unsigned long column, unsigned long row) |
| 591 | { |
Heinrich Schuchardt | dcac1d9 | 2018-09-14 18:49:26 +0200 | [diff] [blame] | 592 | efi_status_t ret = EFI_SUCCESS; |
| 593 | struct simple_text_output_mode *con = &efi_con_mode; |
| 594 | struct cout_mode *mode = &efi_cout_modes[con->mode]; |
| 595 | |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 596 | EFI_ENTRY("%p, %ld, %ld", this, column, row); |
| 597 | |
Heinrich Schuchardt | dcac1d9 | 2018-09-14 18:49:26 +0200 | [diff] [blame] | 598 | /* Check parameters */ |
| 599 | if (!this) { |
| 600 | ret = EFI_INVALID_PARAMETER; |
| 601 | goto out; |
| 602 | } |
| 603 | if (row >= mode->rows || column >= mode->columns) { |
| 604 | ret = EFI_UNSUPPORTED; |
| 605 | goto out; |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | * Set cursor position by sending CSI H. |
| 610 | * EFI origin is [0, 0], terminal origin is [1, 1]. |
| 611 | */ |
| 612 | printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 613 | efi_con_mode.cursor_column = column; |
| 614 | efi_con_mode.cursor_row = row; |
Heinrich Schuchardt | dcac1d9 | 2018-09-14 18:49:26 +0200 | [diff] [blame] | 615 | out: |
| 616 | return EFI_EXIT(ret); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 617 | } |
| 618 | |
Heinrich Schuchardt | 8bc44ed | 2020-06-04 18:40:44 +0200 | [diff] [blame] | 619 | /** |
| 620 | * efi_cout_enable_cursor() - enable the cursor |
| 621 | * |
| 622 | * This function implements the EnableCursor service of the simple text output |
| 623 | * protocol. See the Unified Extensible Firmware Interface (UEFI) specification |
| 624 | * for details. |
| 625 | * |
| 626 | * @this: pointer to the protocol instance |
| 627 | * @enable: if true enable, if false disable the cursor |
| 628 | * Return: status code |
| 629 | */ |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 630 | static efi_status_t EFIAPI efi_cout_enable_cursor( |
| 631 | struct efi_simple_text_output_protocol *this, |
| 632 | bool enable) |
| 633 | { |
| 634 | EFI_ENTRY("%p, %d", this, enable); |
| 635 | |
| 636 | printf(ESC"[?25%c", enable ? 'h' : 'l'); |
Heinrich Schuchardt | 53f26b9 | 2019-06-02 22:54:28 +0200 | [diff] [blame] | 637 | efi_con_mode.cursor_visible = !!enable; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 638 | |
| 639 | return EFI_EXIT(EFI_SUCCESS); |
| 640 | } |
| 641 | |
Heinrich Schuchardt | 580c13b | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 642 | struct efi_simple_text_output_protocol efi_con_out = { |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 643 | .reset = efi_cout_reset, |
| 644 | .output_string = efi_cout_output_string, |
| 645 | .test_string = efi_cout_test_string, |
| 646 | .query_mode = efi_cout_query_mode, |
| 647 | .set_mode = efi_cout_set_mode, |
| 648 | .set_attribute = efi_cout_set_attribute, |
| 649 | .clear_screen = efi_cout_clear_screen, |
| 650 | .set_cursor_position = efi_cout_set_cursor_position, |
| 651 | .enable_cursor = efi_cout_enable_cursor, |
| 652 | .mode = (void*)&efi_con_mode, |
| 653 | }; |
| 654 | |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 655 | /** |
| 656 | * struct efi_cin_notify_function - registered console input notify function |
| 657 | * |
| 658 | * @link: link to list |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 659 | * @key: key to notify |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 660 | * @function: function to call |
| 661 | */ |
| 662 | struct efi_cin_notify_function { |
| 663 | struct list_head link; |
| 664 | struct efi_key_data key; |
| 665 | efi_status_t (EFIAPI *function) |
| 666 | (struct efi_key_data *key_data); |
| 667 | }; |
| 668 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 669 | static bool key_available; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 670 | static struct efi_key_data next_key; |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 671 | static LIST_HEAD(cin_notify_functions); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 672 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 673 | /** |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 674 | * set_shift_mask() - set shift mask |
| 675 | * |
| 676 | * @mod: Xterm shift mask |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 677 | * @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] | 678 | */ |
Heinrich Schuchardt | f2e7d00 | 2023-02-10 08:51:41 +0100 | [diff] [blame] | 679 | 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] | 680 | { |
| 681 | key_state->key_shift_state = EFI_SHIFT_STATE_VALID; |
| 682 | if (mod) { |
| 683 | --mod; |
| 684 | if (mod & 1) |
| 685 | key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED; |
| 686 | if (mod & 2) |
| 687 | key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED; |
| 688 | if (mod & 4) |
| 689 | key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED; |
Heinrich Schuchardt | fea93d6 | 2019-06-16 22:33:20 +0200 | [diff] [blame] | 690 | if (!mod || (mod & 8)) |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 691 | key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED; |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
| 695 | /** |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 696 | * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 697 | * |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 698 | * This gets called when we have already parsed CSI. |
| 699 | * |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 700 | * @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] | 701 | * Return: the unmodified code |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 702 | */ |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 703 | static int analyze_modifiers(struct efi_key_state *key_state) |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 704 | { |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 705 | int c, mod = 0, ret = 0; |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 706 | |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 707 | c = getchar(); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 708 | |
| 709 | if (c != ';') { |
| 710 | ret = c; |
| 711 | if (c == '~') |
| 712 | goto out; |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 713 | c = getchar(); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 714 | } |
| 715 | for (;;) { |
| 716 | switch (c) { |
| 717 | case '0'...'9': |
| 718 | mod *= 10; |
| 719 | mod += c - '0'; |
| 720 | /* fall through */ |
| 721 | case ';': |
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 | break; |
| 724 | default: |
| 725 | goto out; |
| 726 | } |
| 727 | } |
| 728 | out: |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 729 | set_shift_mask(mod, key_state); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 730 | if (!ret) |
| 731 | ret = c; |
| 732 | return ret; |
| 733 | } |
| 734 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 735 | /** |
| 736 | * efi_cin_read_key() - read a key from the console input |
| 737 | * |
| 738 | * @key: - key received |
| 739 | * Return: - status code |
| 740 | */ |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 741 | 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] | 742 | { |
| 743 | struct efi_input_key pressed_key = { |
| 744 | .scan_code = 0, |
| 745 | .unicode_char = 0, |
| 746 | }; |
Heinrich Schuchardt | fc5f1a1 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 747 | s32 ch; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 748 | |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 749 | if (console_read_unicode(&ch)) |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 750 | return EFI_NOT_READY; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 751 | |
| 752 | key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID; |
| 753 | key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID; |
| 754 | |
Heinrich Schuchardt | fc5f1a1 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 755 | /* We do not support multi-word codes */ |
| 756 | if (ch >= 0x10000) |
| 757 | ch = '?'; |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 758 | |
| 759 | switch (ch) { |
| 760 | case 0x1b: |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 761 | /* |
Heinrich Schuchardt | e5c9317 | 2020-12-27 14:47:50 +0100 | [diff] [blame] | 762 | * If a second key is received within 10 ms, assume that we are |
| 763 | * dealing with an escape sequence. Otherwise consider this the |
| 764 | * escape key being hit. 10 ms is long enough to work fine at |
| 765 | * 1200 baud and above. |
| 766 | */ |
| 767 | udelay(10000); |
| 768 | if (!tstc()) { |
| 769 | pressed_key.scan_code = 23; |
| 770 | break; |
| 771 | } |
| 772 | /* |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 773 | * Xterm Control Sequences |
| 774 | * https://www.xfree86.org/4.8.0/ctlseqs.html |
| 775 | */ |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 776 | ch = getchar(); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 777 | switch (ch) { |
| 778 | case cESC: /* ESC */ |
| 779 | pressed_key.scan_code = 23; |
| 780 | break; |
Heinrich Schuchardt | 46e41d0 | 2019-06-16 21:41:13 +0200 | [diff] [blame] | 781 | case 'O': /* F1 - F4, End */ |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 782 | ch = getchar(); |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 783 | /* consider modifiers */ |
Heinrich Schuchardt | 46e41d0 | 2019-06-16 21:41:13 +0200 | [diff] [blame] | 784 | if (ch == 'F') { /* End */ |
| 785 | pressed_key.scan_code = 6; |
| 786 | break; |
| 787 | } else if (ch < 'P') { |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 788 | set_shift_mask(ch - '0', &key->key_state); |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 789 | ch = getchar(); |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 790 | } |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 791 | pressed_key.scan_code = ch - 'P' + 11; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 792 | break; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 793 | case '[': |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 794 | ch = getchar(); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 795 | switch (ch) { |
| 796 | case 'A'...'D': /* up, down right, left */ |
| 797 | pressed_key.scan_code = ch - 'A' + 1; |
| 798 | break; |
| 799 | case 'F': /* End */ |
| 800 | pressed_key.scan_code = 6; |
| 801 | break; |
| 802 | case 'H': /* Home */ |
| 803 | pressed_key.scan_code = 5; |
| 804 | break; |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 805 | case '1': |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 806 | ch = analyze_modifiers(&key->key_state); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 807 | switch (ch) { |
| 808 | case '1'...'5': /* F1 - F5 */ |
| 809 | pressed_key.scan_code = ch - '1' + 11; |
| 810 | break; |
Heinrich Schuchardt | 46e41d0 | 2019-06-16 21:41:13 +0200 | [diff] [blame] | 811 | case '6'...'9': /* F5 - F8 */ |
| 812 | pressed_key.scan_code = ch - '6' + 15; |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 813 | break; |
| 814 | case 'A'...'D': /* up, down right, left */ |
| 815 | pressed_key.scan_code = ch - 'A' + 1; |
| 816 | break; |
Heinrich Schuchardt | 46e41d0 | 2019-06-16 21:41:13 +0200 | [diff] [blame] | 817 | case 'F': /* End */ |
| 818 | pressed_key.scan_code = 6; |
| 819 | break; |
| 820 | case 'H': /* Home */ |
| 821 | pressed_key.scan_code = 5; |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 822 | break; |
Heinrich Schuchardt | 46e41d0 | 2019-06-16 21:41:13 +0200 | [diff] [blame] | 823 | case '~': /* Home */ |
| 824 | pressed_key.scan_code = 5; |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 825 | break; |
| 826 | } |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 827 | break; |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 828 | case '2': |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 829 | ch = analyze_modifiers(&key->key_state); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 830 | switch (ch) { |
| 831 | case '0'...'1': /* F9 - F10 */ |
| 832 | pressed_key.scan_code = ch - '0' + 19; |
| 833 | break; |
| 834 | case '3'...'4': /* F11 - F12 */ |
| 835 | pressed_key.scan_code = ch - '3' + 21; |
| 836 | break; |
| 837 | case '~': /* INS */ |
| 838 | pressed_key.scan_code = 7; |
| 839 | break; |
| 840 | } |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 841 | break; |
| 842 | case '3': /* DEL */ |
| 843 | pressed_key.scan_code = 8; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 844 | analyze_modifiers(&key->key_state); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 845 | break; |
| 846 | case '5': /* PG UP */ |
| 847 | pressed_key.scan_code = 9; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 848 | analyze_modifiers(&key->key_state); |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 849 | break; |
| 850 | case '6': /* PG DOWN */ |
| 851 | pressed_key.scan_code = 10; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 852 | analyze_modifiers(&key->key_state); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 853 | break; |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 854 | } /* [ */ |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 855 | break; |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 856 | default: |
| 857 | /* ALT key */ |
| 858 | set_shift_mask(3, &key->key_state); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 859 | } |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 860 | break; |
| 861 | case 0x7f: |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 862 | /* Backspace */ |
| 863 | ch = 0x08; |
| 864 | } |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 865 | if (pressed_key.scan_code) { |
| 866 | key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID; |
| 867 | } else { |
Heinrich Schuchardt | aa503da | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 868 | pressed_key.unicode_char = ch; |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 869 | |
| 870 | /* |
| 871 | * Assume left control key for control characters typically |
| 872 | * entered using the control key. |
| 873 | */ |
| 874 | if (ch >= 0x01 && ch <= 0x1f) { |
Heinrich Schuchardt | 587ae79 | 2018-09-11 22:38:09 +0200 | [diff] [blame] | 875 | key->key_state.key_shift_state |= |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 876 | EFI_SHIFT_STATE_VALID; |
| 877 | switch (ch) { |
| 878 | case 0x01 ... 0x07: |
| 879 | case 0x0b ... 0x0c: |
| 880 | case 0x0e ... 0x1f: |
| 881 | key->key_state.key_shift_state |= |
| 882 | EFI_LEFT_CONTROL_PRESSED; |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | key->key = pressed_key; |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 887 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 888 | return EFI_SUCCESS; |
| 889 | } |
| 890 | |
| 891 | /** |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 892 | * efi_cin_notify() - notify registered functions |
| 893 | */ |
| 894 | static void efi_cin_notify(void) |
| 895 | { |
| 896 | struct efi_cin_notify_function *item; |
| 897 | |
| 898 | list_for_each_entry(item, &cin_notify_functions, link) { |
| 899 | bool match = true; |
| 900 | |
| 901 | /* We do not support toggle states */ |
| 902 | if (item->key.key.unicode_char || item->key.key.scan_code) { |
| 903 | if (item->key.key.unicode_char != |
| 904 | next_key.key.unicode_char || |
| 905 | item->key.key.scan_code != next_key.key.scan_code) |
| 906 | match = false; |
| 907 | } |
| 908 | if (item->key.key_state.key_shift_state && |
| 909 | item->key.key_state.key_shift_state != |
| 910 | next_key.key_state.key_shift_state) |
| 911 | match = false; |
| 912 | |
| 913 | if (match) |
| 914 | /* We don't bother about the return code */ |
| 915 | EFI_CALL(item->function(&next_key)); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | /** |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 920 | * efi_cin_check() - check if keyboard input is available |
| 921 | */ |
| 922 | static void efi_cin_check(void) |
| 923 | { |
| 924 | efi_status_t ret; |
| 925 | |
| 926 | if (key_available) { |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 927 | efi_signal_event(efi_con_in.wait_for_key); |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 928 | return; |
| 929 | } |
| 930 | |
| 931 | if (tstc()) { |
| 932 | ret = efi_cin_read_key(&next_key); |
| 933 | if (ret == EFI_SUCCESS) { |
| 934 | key_available = true; |
| 935 | |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 936 | /* Notify registered functions */ |
| 937 | efi_cin_notify(); |
| 938 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 939 | /* Queue the wait for key event */ |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 940 | if (key_available) |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 941 | efi_signal_event(efi_con_in.wait_for_key); |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 942 | } |
| 943 | } |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /** |
| 947 | * efi_cin_empty_buffer() - empty input buffer |
| 948 | */ |
| 949 | static void efi_cin_empty_buffer(void) |
| 950 | { |
| 951 | while (tstc()) |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 952 | getchar(); |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 953 | key_available = false; |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * efi_cin_reset_ex() - reset console input |
| 958 | * |
| 959 | * @this: - the extended simple text input protocol |
| 960 | * @extended_verification: - extended verification |
| 961 | * |
| 962 | * This function implements the reset service of the |
| 963 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. |
| 964 | * |
| 965 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 966 | * details. |
| 967 | * |
| 968 | * Return: old value of the task priority level |
| 969 | */ |
| 970 | static efi_status_t EFIAPI efi_cin_reset_ex( |
| 971 | struct efi_simple_text_input_ex_protocol *this, |
| 972 | bool extended_verification) |
| 973 | { |
| 974 | efi_status_t ret = EFI_SUCCESS; |
| 975 | |
| 976 | EFI_ENTRY("%p, %d", this, extended_verification); |
| 977 | |
| 978 | /* Check parameters */ |
| 979 | if (!this) { |
| 980 | ret = EFI_INVALID_PARAMETER; |
| 981 | goto out; |
| 982 | } |
| 983 | |
| 984 | efi_cin_empty_buffer(); |
| 985 | out: |
| 986 | return EFI_EXIT(ret); |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /** |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 990 | * efi_cin_read_key_stroke_ex() - read key stroke |
| 991 | * |
| 992 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
| 993 | * @key_data: key read from console |
| 994 | * Return: status code |
| 995 | * |
| 996 | * This function implements the ReadKeyStrokeEx service of the |
| 997 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. |
| 998 | * |
| 999 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1000 | * details. |
| 1001 | */ |
| 1002 | static efi_status_t EFIAPI efi_cin_read_key_stroke_ex( |
| 1003 | struct efi_simple_text_input_ex_protocol *this, |
| 1004 | struct efi_key_data *key_data) |
| 1005 | { |
| 1006 | efi_status_t ret = EFI_SUCCESS; |
| 1007 | |
| 1008 | EFI_ENTRY("%p, %p", this, key_data); |
| 1009 | |
| 1010 | /* Check parameters */ |
| 1011 | if (!this || !key_data) { |
| 1012 | ret = EFI_INVALID_PARAMETER; |
| 1013 | goto out; |
| 1014 | } |
| 1015 | |
| 1016 | /* We don't do interrupts, so check for timers cooperatively */ |
| 1017 | efi_timer_check(); |
| 1018 | |
| 1019 | /* Enable console input after ExitBootServices */ |
| 1020 | efi_cin_check(); |
| 1021 | |
| 1022 | if (!key_available) { |
Heinrich Schuchardt | b6697c2 | 2022-09-01 23:30:09 +0200 | [diff] [blame] | 1023 | memset(key_data, 0, sizeof(struct efi_key_data)); |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1024 | ret = EFI_NOT_READY; |
| 1025 | goto out; |
| 1026 | } |
Heinrich Schuchardt | 00b00a1 | 2019-04-06 20:59:24 +0200 | [diff] [blame] | 1027 | /* |
| 1028 | * CTRL+A - CTRL+Z have to be signaled as a - z. |
| 1029 | * 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] | 1030 | * CTRL+\ - CTRL+_ have to be signaled as \ - _. |
Heinrich Schuchardt | 00b00a1 | 2019-04-06 20:59:24 +0200 | [diff] [blame] | 1031 | */ |
| 1032 | switch (next_key.key.unicode_char) { |
| 1033 | case 0x01 ... 0x07: |
| 1034 | case 0x0b ... 0x0c: |
| 1035 | case 0x0e ... 0x1a: |
| 1036 | if (!(next_key.key_state.key_toggle_state & |
| 1037 | EFI_CAPS_LOCK_ACTIVE) ^ |
| 1038 | !(next_key.key_state.key_shift_state & |
| 1039 | (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED))) |
| 1040 | next_key.key.unicode_char += 0x40; |
| 1041 | else |
| 1042 | next_key.key.unicode_char += 0x60; |
Heinrich Schuchardt | 068dc9c | 2022-09-02 00:49:12 +0200 | [diff] [blame] | 1043 | break; |
| 1044 | case 0x1c ... 0x1f: |
| 1045 | next_key.key.unicode_char += 0x40; |
Heinrich Schuchardt | 00b00a1 | 2019-04-06 20:59:24 +0200 | [diff] [blame] | 1046 | } |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1047 | *key_data = next_key; |
| 1048 | key_available = false; |
| 1049 | efi_con_in.wait_for_key->is_signaled = false; |
Heinrich Schuchardt | 00b00a1 | 2019-04-06 20:59:24 +0200 | [diff] [blame] | 1050 | |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1051 | out: |
| 1052 | return EFI_EXIT(ret); |
| 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * efi_cin_set_state() - set toggle key state |
| 1057 | * |
| 1058 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
Heinrich Schuchardt | a41ccf9 | 2019-05-18 17:07:52 +0200 | [diff] [blame] | 1059 | * @key_toggle_state: pointer to key toggle state |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1060 | * Return: status code |
| 1061 | * |
| 1062 | * This function implements the SetState service of the |
| 1063 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. |
| 1064 | * |
| 1065 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1066 | * details. |
| 1067 | */ |
| 1068 | static efi_status_t EFIAPI efi_cin_set_state( |
| 1069 | struct efi_simple_text_input_ex_protocol *this, |
Heinrich Schuchardt | a41ccf9 | 2019-05-18 17:07:52 +0200 | [diff] [blame] | 1070 | u8 *key_toggle_state) |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1071 | { |
Heinrich Schuchardt | a41ccf9 | 2019-05-18 17:07:52 +0200 | [diff] [blame] | 1072 | EFI_ENTRY("%p, %p", this, key_toggle_state); |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1073 | /* |
| 1074 | * U-Boot supports multiple console input sources like serial and |
| 1075 | * net console for which a key toggle state cannot be set at all. |
| 1076 | * |
| 1077 | * According to the UEFI specification it is allowable to not implement |
| 1078 | * this service. |
| 1079 | */ |
| 1080 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 1081 | } |
| 1082 | |
| 1083 | /** |
| 1084 | * efi_cin_register_key_notify() - register key notification function |
| 1085 | * |
| 1086 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
| 1087 | * @key_data: key to be notified |
| 1088 | * @key_notify_function: function to be called if the key is pressed |
| 1089 | * @notify_handle: handle for unregistering the notification |
| 1090 | * Return: status code |
| 1091 | * |
| 1092 | * This function implements the SetState service of the |
| 1093 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. |
| 1094 | * |
| 1095 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1096 | * details. |
| 1097 | */ |
| 1098 | static efi_status_t EFIAPI efi_cin_register_key_notify( |
| 1099 | struct efi_simple_text_input_ex_protocol *this, |
| 1100 | struct efi_key_data *key_data, |
| 1101 | efi_status_t (EFIAPI *key_notify_function)( |
| 1102 | struct efi_key_data *key_data), |
| 1103 | void **notify_handle) |
| 1104 | { |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 1105 | efi_status_t ret = EFI_SUCCESS; |
| 1106 | struct efi_cin_notify_function *notify_function; |
| 1107 | |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1108 | EFI_ENTRY("%p, %p, %p, %p", |
| 1109 | this, key_data, key_notify_function, notify_handle); |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 1110 | |
| 1111 | /* Check parameters */ |
| 1112 | if (!this || !key_data || !key_notify_function || !notify_handle) { |
| 1113 | ret = EFI_INVALID_PARAMETER; |
| 1114 | goto out; |
| 1115 | } |
| 1116 | |
| 1117 | EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n", |
| 1118 | key_data->key.unicode_char, |
| 1119 | key_data->key.scan_code, |
| 1120 | key_data->key_state.key_shift_state, |
| 1121 | key_data->key_state.key_toggle_state); |
| 1122 | |
| 1123 | notify_function = calloc(1, sizeof(struct efi_cin_notify_function)); |
| 1124 | if (!notify_function) { |
| 1125 | ret = EFI_OUT_OF_RESOURCES; |
| 1126 | goto out; |
| 1127 | } |
| 1128 | notify_function->key = *key_data; |
| 1129 | notify_function->function = key_notify_function; |
| 1130 | list_add_tail(¬ify_function->link, &cin_notify_functions); |
| 1131 | *notify_handle = notify_function; |
| 1132 | out: |
| 1133 | return EFI_EXIT(ret); |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | /** |
| 1137 | * efi_cin_unregister_key_notify() - unregister key notification function |
| 1138 | * |
| 1139 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
| 1140 | * @notification_handle: handle received when registering |
| 1141 | * Return: status code |
| 1142 | * |
| 1143 | * This function implements the SetState service of the |
| 1144 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. |
| 1145 | * |
| 1146 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1147 | * details. |
| 1148 | */ |
| 1149 | static efi_status_t EFIAPI efi_cin_unregister_key_notify( |
| 1150 | struct efi_simple_text_input_ex_protocol *this, |
| 1151 | void *notification_handle) |
| 1152 | { |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 1153 | efi_status_t ret = EFI_INVALID_PARAMETER; |
| 1154 | struct efi_cin_notify_function *item, *notify_function = |
| 1155 | notification_handle; |
| 1156 | |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1157 | EFI_ENTRY("%p, %p", this, notification_handle); |
Heinrich Schuchardt | 695691e | 2018-09-11 22:38:12 +0200 | [diff] [blame] | 1158 | |
| 1159 | /* Check parameters */ |
| 1160 | if (!this || !notification_handle) |
| 1161 | goto out; |
| 1162 | |
| 1163 | list_for_each_entry(item, &cin_notify_functions, link) { |
| 1164 | if (item == notify_function) { |
| 1165 | ret = EFI_SUCCESS; |
| 1166 | break; |
| 1167 | } |
| 1168 | } |
| 1169 | if (ret != EFI_SUCCESS) |
| 1170 | goto out; |
| 1171 | |
| 1172 | /* Remove the notify function */ |
| 1173 | list_del(¬ify_function->link); |
| 1174 | free(notify_function); |
| 1175 | out: |
| 1176 | return EFI_EXIT(ret); |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | |
| 1180 | /** |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1181 | * efi_cin_reset() - drain the input buffer |
| 1182 | * |
| 1183 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
| 1184 | * @extended_verification: allow for exhaustive verification |
| 1185 | * Return: status code |
| 1186 | * |
| 1187 | * This function implements the Reset service of the |
| 1188 | * EFI_SIMPLE_TEXT_INPUT_PROTOCOL. |
| 1189 | * |
| 1190 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1191 | * details. |
| 1192 | */ |
| 1193 | static efi_status_t EFIAPI efi_cin_reset |
| 1194 | (struct efi_simple_text_input_protocol *this, |
| 1195 | bool extended_verification) |
| 1196 | { |
| 1197 | efi_status_t ret = EFI_SUCCESS; |
| 1198 | |
| 1199 | EFI_ENTRY("%p, %d", this, extended_verification); |
| 1200 | |
| 1201 | /* Check parameters */ |
| 1202 | if (!this) { |
| 1203 | ret = EFI_INVALID_PARAMETER; |
| 1204 | goto out; |
| 1205 | } |
| 1206 | |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1207 | efi_cin_empty_buffer(); |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1208 | out: |
| 1209 | return EFI_EXIT(ret); |
| 1210 | } |
| 1211 | |
| 1212 | /** |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1213 | * efi_cin_read_key_stroke() - read key stroke |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1214 | * |
| 1215 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
| 1216 | * @key: key read from console |
| 1217 | * Return: status code |
| 1218 | * |
| 1219 | * This function implements the ReadKeyStroke service of the |
| 1220 | * EFI_SIMPLE_TEXT_INPUT_PROTOCOL. |
| 1221 | * |
| 1222 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1223 | * details. |
| 1224 | */ |
| 1225 | static efi_status_t EFIAPI efi_cin_read_key_stroke |
| 1226 | (struct efi_simple_text_input_protocol *this, |
| 1227 | struct efi_input_key *key) |
| 1228 | { |
| 1229 | efi_status_t ret = EFI_SUCCESS; |
| 1230 | |
| 1231 | EFI_ENTRY("%p, %p", this, key); |
| 1232 | |
| 1233 | /* Check parameters */ |
| 1234 | if (!this || !key) { |
| 1235 | ret = EFI_INVALID_PARAMETER; |
| 1236 | goto out; |
| 1237 | } |
| 1238 | |
| 1239 | /* We don't do interrupts, so check for timers cooperatively */ |
| 1240 | efi_timer_check(); |
| 1241 | |
| 1242 | /* Enable console input after ExitBootServices */ |
| 1243 | efi_cin_check(); |
| 1244 | |
| 1245 | if (!key_available) { |
| 1246 | ret = EFI_NOT_READY; |
| 1247 | goto out; |
| 1248 | } |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1249 | *key = next_key.key; |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1250 | key_available = false; |
| 1251 | efi_con_in.wait_for_key->is_signaled = false; |
| 1252 | out: |
| 1253 | return EFI_EXIT(ret); |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 1254 | } |
| 1255 | |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1256 | static struct efi_simple_text_input_ex_protocol efi_con_in_ex = { |
| 1257 | .reset = efi_cin_reset_ex, |
| 1258 | .read_key_stroke_ex = efi_cin_read_key_stroke_ex, |
| 1259 | .wait_for_key_ex = NULL, |
| 1260 | .set_state = efi_cin_set_state, |
| 1261 | .register_key_notify = efi_cin_register_key_notify, |
| 1262 | .unregister_key_notify = efi_cin_unregister_key_notify, |
| 1263 | }; |
| 1264 | |
Heinrich Schuchardt | 3dabffd | 2018-09-08 10:20:10 +0200 | [diff] [blame] | 1265 | struct efi_simple_text_input_protocol efi_con_in = { |
Alexander Graf | 4aacacc | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 1266 | .reset = efi_cin_reset, |
| 1267 | .read_key_stroke = efi_cin_read_key_stroke, |
| 1268 | .wait_for_key = NULL, |
| 1269 | }; |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1270 | |
| 1271 | static struct efi_event *console_timer_event; |
| 1272 | |
Heinrich Schuchardt | d8b878a | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 1273 | /* |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1274 | * efi_console_timer_notify() - notify the console timer event |
Heinrich Schuchardt | d8b878a | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 1275 | * |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1276 | * @event: console timer event |
| 1277 | * @context: not used |
Heinrich Schuchardt | d8b878a | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 1278 | */ |
xypron.glpk@gmx.de | 0ce42dc | 2017-07-20 05:26:07 +0200 | [diff] [blame] | 1279 | static void EFIAPI efi_console_timer_notify(struct efi_event *event, |
| 1280 | void *context) |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1281 | { |
| 1282 | EFI_ENTRY("%p, %p", event, context); |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1283 | efi_cin_check(); |
| 1284 | EFI_EXIT(EFI_SUCCESS); |
| 1285 | } |
Heinrich Schuchardt | d8b878a | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 1286 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1287 | /** |
| 1288 | * efi_key_notify() - notify the wait for key event |
| 1289 | * |
| 1290 | * @event: wait for key event |
| 1291 | * @context: not used |
| 1292 | */ |
| 1293 | static void EFIAPI efi_key_notify(struct efi_event *event, void *context) |
| 1294 | { |
| 1295 | EFI_ENTRY("%p, %p", event, context); |
| 1296 | efi_cin_check(); |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1297 | EFI_EXIT(EFI_SUCCESS); |
| 1298 | } |
| 1299 | |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1300 | /** |
| 1301 | * efi_console_register() - install the console protocols |
| 1302 | * |
| 1303 | * This function is called from do_bootefi_exec(). |
Heinrich Schuchardt | a92e9ab | 2018-10-02 06:08:26 +0200 | [diff] [blame] | 1304 | * |
| 1305 | * Return: status code |
Heinrich Schuchardt | 49574bc | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 1306 | */ |
Heinrich Schuchardt | a92e9ab | 2018-10-02 06:08:26 +0200 | [diff] [blame] | 1307 | efi_status_t efi_console_register(void) |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1308 | { |
| 1309 | efi_status_t r; |
Heinrich Schuchardt | 81244ea | 2022-02-04 20:47:09 +0100 | [diff] [blame] | 1310 | struct efi_device_path *dp; |
Rob Clark | 49f7b4b | 2017-07-24 10:39:01 -0400 | [diff] [blame] | 1311 | |
Heinrich Schuchardt | 81244ea | 2022-02-04 20:47:09 +0100 | [diff] [blame] | 1312 | /* Install protocols on root node */ |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 1313 | r = efi_install_multiple_protocol_interfaces(&efi_root, |
| 1314 | &efi_guid_text_output_protocol, |
| 1315 | &efi_con_out, |
| 1316 | &efi_guid_text_input_protocol, |
| 1317 | &efi_con_in, |
| 1318 | &efi_guid_text_input_ex_protocol, |
| 1319 | &efi_con_in_ex, |
| 1320 | NULL); |
Alexander Graf | 36bab91 | 2018-09-04 14:59:11 +0200 | [diff] [blame] | 1321 | |
Heinrich Schuchardt | 81244ea | 2022-02-04 20:47:09 +0100 | [diff] [blame] | 1322 | /* Create console node and install device path protocols */ |
| 1323 | if (CONFIG_IS_ENABLED(DM_SERIAL)) { |
| 1324 | dp = efi_dp_from_uart(); |
| 1325 | if (!dp) |
| 1326 | goto out_of_memory; |
Alexander Graf | 36bab91 | 2018-09-04 14:59:11 +0200 | [diff] [blame] | 1327 | |
Heinrich Schuchardt | 81244ea | 2022-02-04 20:47:09 +0100 | [diff] [blame] | 1328 | /* Hook UART up to the device list */ |
| 1329 | efi_add_handle(&uart_obj); |
Alexander Graf | 36bab91 | 2018-09-04 14:59:11 +0200 | [diff] [blame] | 1330 | |
Heinrich Schuchardt | 81244ea | 2022-02-04 20:47:09 +0100 | [diff] [blame] | 1331 | /* Install device path */ |
| 1332 | r = efi_add_protocol(&uart_obj, &efi_guid_device_path, dp); |
| 1333 | if (r != EFI_SUCCESS) |
| 1334 | goto out_of_memory; |
| 1335 | } |
Rob Clark | 49f7b4b | 2017-07-24 10:39:01 -0400 | [diff] [blame] | 1336 | |
Heinrich Schuchardt | 580c13b | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 1337 | /* Create console events */ |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1338 | r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify, |
| 1339 | NULL, NULL, &efi_con_in.wait_for_key); |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1340 | if (r != EFI_SUCCESS) { |
| 1341 | printf("ERROR: Failed to register WaitForKey event\n"); |
| 1342 | return r; |
| 1343 | } |
Heinrich Schuchardt | 6c4cd26 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 1344 | 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] | 1345 | r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK, |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1346 | efi_console_timer_notify, NULL, NULL, |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1347 | &console_timer_event); |
| 1348 | if (r != EFI_SUCCESS) { |
| 1349 | printf("ERROR: Failed to register console event\n"); |
| 1350 | return r; |
| 1351 | } |
| 1352 | /* 5000 ns cycle is sufficient for 2 MBaud */ |
| 1353 | r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50); |
| 1354 | if (r != EFI_SUCCESS) |
| 1355 | printf("ERROR: Failed to set console timer\n"); |
| 1356 | return r; |
Heinrich Schuchardt | 580c13b | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 1357 | out_of_memory: |
Heinrich Schuchardt | 6e8eff2 | 2018-09-08 19:57:24 +0200 | [diff] [blame] | 1358 | printf("ERROR: Out of memory\n"); |
Heinrich Schuchardt | 580c13b | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 1359 | return r; |
xypron.glpk@gmx.de | 54c7a8e | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 1360 | } |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1361 | |
| 1362 | /** |
| 1363 | * efi_console_get_u16_string() - get user input string |
| 1364 | * |
| 1365 | * @cin: protocol interface to EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
| 1366 | * @buf: buffer to store user input string in UTF16 |
| 1367 | * @count: number of u16 string including NULL terminator that buf has |
| 1368 | * @filter_func: callback to filter user input |
| 1369 | * @row: row number to locate user input form |
| 1370 | * @col: column number to locate user input form |
| 1371 | * Return: status code |
| 1372 | */ |
| 1373 | efi_status_t efi_console_get_u16_string(struct efi_simple_text_input_protocol *cin, |
| 1374 | u16 *buf, efi_uintn_t count, |
| 1375 | efi_console_filter_func filter_func, |
| 1376 | int row, int col) |
| 1377 | { |
| 1378 | efi_status_t ret; |
| 1379 | efi_uintn_t len = 0; |
| 1380 | struct efi_input_key key; |
| 1381 | |
| 1382 | printf(ANSI_CURSOR_POSITION |
| 1383 | ANSI_CLEAR_LINE_TO_END |
| 1384 | ANSI_CURSOR_SHOW, row, col); |
| 1385 | |
Heinrich Schuchardt | 49dbcaf | 2022-10-15 12:22:37 +0200 | [diff] [blame] | 1386 | efi_cin_empty_buffer(); |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1387 | |
| 1388 | for (;;) { |
| 1389 | do { |
| 1390 | ret = EFI_CALL(cin->read_key_stroke(cin, &key)); |
| 1391 | mdelay(10); |
| 1392 | } while (ret == EFI_NOT_READY); |
| 1393 | |
| 1394 | if (key.unicode_char == u'\b') { |
| 1395 | if (len > 0) |
| 1396 | buf[--len] = u'\0'; |
| 1397 | |
| 1398 | printf(ANSI_CURSOR_POSITION |
| 1399 | "%ls" |
| 1400 | ANSI_CLEAR_LINE_TO_END, row, col, buf); |
| 1401 | continue; |
| 1402 | } else if (key.unicode_char == u'\r') { |
| 1403 | buf[len] = u'\0'; |
| 1404 | return EFI_SUCCESS; |
| 1405 | } else if (key.unicode_char == 0x3 || key.scan_code == 23) { |
| 1406 | return EFI_ABORTED; |
| 1407 | } else if (key.unicode_char < 0x20) { |
| 1408 | /* ignore control codes other than Ctrl+C, '\r' and '\b' */ |
| 1409 | continue; |
| 1410 | } else if (key.scan_code != 0) { |
| 1411 | /* only accept single ESC press for cancel */ |
| 1412 | continue; |
| 1413 | } |
| 1414 | |
| 1415 | if (filter_func) { |
| 1416 | if (filter_func(&key) != EFI_SUCCESS) |
| 1417 | continue; |
| 1418 | } |
| 1419 | |
| 1420 | if (len >= (count - 1)) |
| 1421 | continue; |
| 1422 | |
| 1423 | buf[len] = key.unicode_char; |
| 1424 | len++; |
| 1425 | printf(ANSI_CURSOR_POSITION "%ls", row, col, buf); |
| 1426 | } |
| 1427 | } |