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