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