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