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