Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application console interface |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 9 | #include <charset.h> |
Rob Clark | a18c5a8 | 2017-09-13 18:05:43 -0400 | [diff] [blame] | 10 | #include <dm/device.h> |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 11 | #include <efi_loader.h> |
Rob Clark | a18c5a8 | 2017-09-13 18:05:43 -0400 | [diff] [blame] | 12 | #include <stdio_dev.h> |
| 13 | #include <video_console.h> |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 14 | |
Emmanuel Vadot | 5be8b0a | 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 | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 45 | const efi_guid_t efi_guid_text_output_protocol = |
| 46 | EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID; |
| 47 | const efi_guid_t efi_guid_text_input_protocol = |
| 48 | EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 49 | |
| 50 | #define cESC '\x1b' |
| 51 | #define ESC "\x1b" |
| 52 | |
Emmanuel Vadot | 5be8b0a | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 53 | /* Default to mode 0 */ |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 54 | static struct simple_text_output_mode efi_con_mode = { |
Emmanuel Vadot | 5be8b0a | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 55 | .max_mode = 1, |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 56 | .mode = 0, |
| 57 | .attribute = 0, |
| 58 | .cursor_column = 0, |
| 59 | .cursor_row = 0, |
| 60 | .cursor_visible = 1, |
| 61 | }; |
| 62 | |
Heinrich Schuchardt | 6221729 | 2018-05-16 18:17:38 +0200 | [diff] [blame] | 63 | /* |
| 64 | * Receive and parse a reply from the terminal. |
| 65 | * |
| 66 | * @n: array of return values |
| 67 | * @num: number of return values expected |
| 68 | * @end_char: character indicating end of terminal message |
| 69 | * @return: non-zero indicates error |
| 70 | */ |
| 71 | static int term_read_reply(int *n, int num, char end_char) |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 72 | { |
| 73 | char c; |
| 74 | int i = 0; |
| 75 | |
| 76 | c = getc(); |
| 77 | if (c != cESC) |
| 78 | return -1; |
| 79 | c = getc(); |
| 80 | if (c != '[') |
| 81 | return -1; |
| 82 | |
| 83 | n[0] = 0; |
| 84 | while (1) { |
| 85 | c = getc(); |
| 86 | if (c == ';') { |
| 87 | i++; |
Heinrich Schuchardt | 6221729 | 2018-05-16 18:17:38 +0200 | [diff] [blame] | 88 | if (i >= num) |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 89 | return -1; |
| 90 | n[i] = 0; |
| 91 | continue; |
| 92 | } else if (c == end_char) { |
| 93 | break; |
| 94 | } else if (c > '9' || c < '0') { |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | /* Read one more decimal position */ |
| 99 | n[i] *= 10; |
| 100 | n[i] += c - '0'; |
| 101 | } |
Heinrich Schuchardt | 6221729 | 2018-05-16 18:17:38 +0200 | [diff] [blame] | 102 | if (i != num - 1) |
| 103 | return -1; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static efi_status_t EFIAPI efi_cout_reset( |
| 109 | struct efi_simple_text_output_protocol *this, |
| 110 | char extended_verification) |
| 111 | { |
| 112 | EFI_ENTRY("%p, %d", this, extended_verification); |
| 113 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 114 | } |
| 115 | |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 116 | static efi_status_t EFIAPI efi_cout_output_string( |
| 117 | struct efi_simple_text_output_protocol *this, |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 118 | const efi_string_t string) |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 119 | { |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 120 | struct simple_text_output_mode *con = &efi_con_mode; |
| 121 | struct cout_mode *mode = &efi_cout_modes[con->mode]; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 122 | |
| 123 | EFI_ENTRY("%p, %p", this, string); |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 124 | |
| 125 | unsigned int n16 = utf16_strlen(string); |
| 126 | char buf[MAX_UTF8_PER_UTF16 * n16 + 1]; |
Heinrich Schuchardt | 7ca7c3c | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 127 | u16 *p; |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 128 | |
| 129 | *utf16_to_utf8((u8 *)buf, string, n16) = '\0'; |
| 130 | |
| 131 | fputs(stdout, buf); |
| 132 | |
Heinrich Schuchardt | 7ca7c3c | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 133 | /* |
| 134 | * Update the cursor position. |
| 135 | * |
| 136 | * The UEFI spec provides advance rules for U+0000, U+0008, U+000A, |
| 137 | * and U000D. All other characters, including control characters |
| 138 | * U+0007 (bel) and U+0009 (tab), have to increase the column by one. |
| 139 | */ |
| 140 | for (p = string; *p; ++p) { |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 141 | switch (*p) { |
Heinrich Schuchardt | 7ca7c3c | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 142 | case '\b': /* U+0008, backspace */ |
| 143 | con->cursor_column = max(0, con->cursor_column - 1); |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 144 | break; |
Heinrich Schuchardt | 7ca7c3c | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 145 | case '\n': /* U+000A, newline */ |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 146 | con->cursor_column = 0; |
| 147 | con->cursor_row++; |
| 148 | break; |
Heinrich Schuchardt | 7ca7c3c | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 149 | case '\r': /* U+000D, carriage-return */ |
| 150 | con->cursor_column = 0; |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 151 | break; |
Heinrich Schuchardt | 7ca7c3c | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 152 | case 0xd800 ... 0xdbff: |
| 153 | /* |
| 154 | * Ignore high surrogates, we do not want to count a |
| 155 | * Unicode character twice. |
| 156 | */ |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 157 | break; |
| 158 | default: |
| 159 | con->cursor_column++; |
| 160 | break; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 161 | } |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 162 | if (con->cursor_column >= mode->columns) { |
| 163 | con->cursor_column = 0; |
| 164 | con->cursor_row++; |
| 165 | } |
| 166 | con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1); |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | return EFI_EXIT(EFI_SUCCESS); |
| 170 | } |
| 171 | |
| 172 | static efi_status_t EFIAPI efi_cout_test_string( |
| 173 | struct efi_simple_text_output_protocol *this, |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 174 | const efi_string_t string) |
Alexander Graf | c1311ad | 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 | 5be8b0a | 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 | |
Rob Clark | 71cc25c | 2017-09-13 18:05:42 -0400 | [diff] [blame] | 188 | static int query_console_serial(int *rows, int *cols) |
| 189 | { |
| 190 | /* Ask the terminal about its size */ |
| 191 | int n[3]; |
| 192 | u64 timeout; |
| 193 | |
| 194 | /* Empty input buffer */ |
| 195 | while (tstc()) |
| 196 | getc(); |
| 197 | |
| 198 | printf(ESC"[18t"); |
| 199 | |
| 200 | /* Check if we have a terminal that understands */ |
| 201 | timeout = timer_get_us() + 1000000; |
| 202 | while (!tstc()) |
| 203 | if (timer_get_us() > timeout) |
| 204 | return -1; |
| 205 | |
| 206 | /* Read {depth,rows,cols} */ |
| 207 | if (term_read_reply(n, 3, 't')) |
| 208 | return -1; |
| 209 | |
| 210 | *cols = n[2]; |
| 211 | *rows = n[1]; |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Heinrich Schuchardt | a4aa7be | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 216 | /* |
| 217 | * Update the mode table. |
| 218 | * |
| 219 | * By default the only mode available is 80x25. If the console has at least 50 |
| 220 | * lines, enable mode 80x50. If we can query the console size and it is neither |
| 221 | * 80x25 nor 80x50, set it as an additional mode. |
| 222 | */ |
| 223 | static void query_console_size(void) |
| 224 | { |
| 225 | const char *stdout_name = env_get("stdout"); |
Alexander Graf | 80483b2 | 2018-06-03 15:51:17 +0200 | [diff] [blame] | 226 | int rows = 25, cols = 80; |
Heinrich Schuchardt | a4aa7be | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 227 | |
| 228 | if (stdout_name && !strcmp(stdout_name, "vidconsole") && |
| 229 | IS_ENABLED(CONFIG_DM_VIDEO)) { |
| 230 | struct stdio_dev *stdout_dev = |
| 231 | stdio_get_by_name("vidconsole"); |
| 232 | struct udevice *dev = stdout_dev->priv; |
| 233 | struct vidconsole_priv *priv = |
| 234 | dev_get_uclass_priv(dev); |
| 235 | rows = priv->rows; |
| 236 | cols = priv->cols; |
| 237 | } else if (query_console_serial(&rows, &cols)) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | /* Test if we can have Mode 1 */ |
| 242 | if (cols >= 80 && rows >= 50) { |
| 243 | efi_cout_modes[1].present = 1; |
| 244 | efi_con_mode.max_mode = 2; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Install our mode as mode 2 if it is different |
| 249 | * than mode 0 or 1 and set it as the currently selected mode |
| 250 | */ |
| 251 | if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) && |
| 252 | !cout_mode_matches(&efi_cout_modes[1], rows, cols)) { |
| 253 | efi_cout_modes[EFI_COUT_MODE_2].columns = cols; |
| 254 | efi_cout_modes[EFI_COUT_MODE_2].rows = rows; |
| 255 | efi_cout_modes[EFI_COUT_MODE_2].present = 1; |
| 256 | efi_con_mode.max_mode = EFI_MAX_COUT_MODE; |
| 257 | efi_con_mode.mode = EFI_COUT_MODE_2; |
| 258 | } |
| 259 | } |
| 260 | |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 261 | static efi_status_t EFIAPI efi_cout_query_mode( |
| 262 | struct efi_simple_text_output_protocol *this, |
| 263 | unsigned long mode_number, unsigned long *columns, |
| 264 | unsigned long *rows) |
| 265 | { |
| 266 | EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows); |
| 267 | |
Emmanuel Vadot | 5be8b0a | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 268 | if (mode_number >= efi_con_mode.max_mode) |
| 269 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 270 | |
| 271 | if (efi_cout_modes[mode_number].present != 1) |
| 272 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 273 | |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 274 | if (columns) |
Emmanuel Vadot | 5be8b0a | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 275 | *columns = efi_cout_modes[mode_number].columns; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 276 | if (rows) |
Emmanuel Vadot | 5be8b0a | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 277 | *rows = efi_cout_modes[mode_number].rows; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 278 | |
| 279 | return EFI_EXIT(EFI_SUCCESS); |
| 280 | } |
| 281 | |
| 282 | static efi_status_t EFIAPI efi_cout_set_mode( |
| 283 | struct efi_simple_text_output_protocol *this, |
| 284 | unsigned long mode_number) |
| 285 | { |
| 286 | EFI_ENTRY("%p, %ld", this, mode_number); |
| 287 | |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 288 | |
Emmanuel Vadot | 5be8b0a | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 289 | if (mode_number > efi_con_mode.max_mode) |
| 290 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 291 | |
| 292 | efi_con_mode.mode = mode_number; |
| 293 | efi_con_mode.cursor_column = 0; |
| 294 | efi_con_mode.cursor_row = 0; |
| 295 | |
| 296 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Rob Clark | 2d5dc2a | 2017-10-10 08:23:01 -0400 | [diff] [blame] | 299 | static const struct { |
| 300 | unsigned int fg; |
| 301 | unsigned int bg; |
| 302 | } color[] = { |
| 303 | { 30, 40 }, /* 0: black */ |
| 304 | { 34, 44 }, /* 1: blue */ |
| 305 | { 32, 42 }, /* 2: green */ |
| 306 | { 36, 46 }, /* 3: cyan */ |
| 307 | { 31, 41 }, /* 4: red */ |
| 308 | { 35, 45 }, /* 5: magenta */ |
| 309 | { 33, 43 }, /* 6: brown, map to yellow as edk2 does*/ |
| 310 | { 37, 47 }, /* 7: light grey, map to white */ |
| 311 | }; |
| 312 | |
| 313 | /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */ |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 314 | static efi_status_t EFIAPI efi_cout_set_attribute( |
| 315 | struct efi_simple_text_output_protocol *this, |
| 316 | unsigned long attribute) |
| 317 | { |
Rob Clark | 2d5dc2a | 2017-10-10 08:23:01 -0400 | [diff] [blame] | 318 | unsigned int bold = EFI_ATTR_BOLD(attribute); |
| 319 | unsigned int fg = EFI_ATTR_FG(attribute); |
| 320 | unsigned int bg = EFI_ATTR_BG(attribute); |
| 321 | |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 322 | EFI_ENTRY("%p, %lx", this, attribute); |
| 323 | |
Rob Clark | 2d5dc2a | 2017-10-10 08:23:01 -0400 | [diff] [blame] | 324 | if (attribute) |
| 325 | printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg); |
| 326 | else |
| 327 | printf(ESC"[0;37;40m"); |
| 328 | |
| 329 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static efi_status_t EFIAPI efi_cout_clear_screen( |
| 333 | struct efi_simple_text_output_protocol *this) |
| 334 | { |
| 335 | EFI_ENTRY("%p", this); |
| 336 | |
| 337 | printf(ESC"[2J"); |
| 338 | |
| 339 | return EFI_EXIT(EFI_SUCCESS); |
| 340 | } |
| 341 | |
| 342 | static efi_status_t EFIAPI efi_cout_set_cursor_position( |
| 343 | struct efi_simple_text_output_protocol *this, |
| 344 | unsigned long column, unsigned long row) |
| 345 | { |
| 346 | EFI_ENTRY("%p, %ld, %ld", this, column, row); |
| 347 | |
| 348 | printf(ESC"[%d;%df", (int)row, (int)column); |
| 349 | efi_con_mode.cursor_column = column; |
| 350 | efi_con_mode.cursor_row = row; |
| 351 | |
| 352 | return EFI_EXIT(EFI_SUCCESS); |
| 353 | } |
| 354 | |
| 355 | static efi_status_t EFIAPI efi_cout_enable_cursor( |
| 356 | struct efi_simple_text_output_protocol *this, |
| 357 | bool enable) |
| 358 | { |
| 359 | EFI_ENTRY("%p, %d", this, enable); |
| 360 | |
| 361 | printf(ESC"[?25%c", enable ? 'h' : 'l'); |
| 362 | |
| 363 | return EFI_EXIT(EFI_SUCCESS); |
| 364 | } |
| 365 | |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 366 | struct efi_simple_text_output_protocol efi_con_out = { |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 367 | .reset = efi_cout_reset, |
| 368 | .output_string = efi_cout_output_string, |
| 369 | .test_string = efi_cout_test_string, |
| 370 | .query_mode = efi_cout_query_mode, |
| 371 | .set_mode = efi_cout_set_mode, |
| 372 | .set_attribute = efi_cout_set_attribute, |
| 373 | .clear_screen = efi_cout_clear_screen, |
| 374 | .set_cursor_position = efi_cout_set_cursor_position, |
| 375 | .enable_cursor = efi_cout_enable_cursor, |
| 376 | .mode = (void*)&efi_con_mode, |
| 377 | }; |
| 378 | |
| 379 | static efi_status_t EFIAPI efi_cin_reset( |
| 380 | struct efi_simple_input_interface *this, |
| 381 | bool extended_verification) |
| 382 | { |
| 383 | EFI_ENTRY("%p, %d", this, extended_verification); |
Heinrich Schuchardt | 4f18789 | 2018-07-05 08:17:59 +0200 | [diff] [blame^] | 384 | |
| 385 | /* Empty input buffer */ |
| 386 | while (tstc()) |
| 387 | getc(); |
| 388 | |
| 389 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 390 | } |
| 391 | |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 392 | /* |
| 393 | * Analyze modifiers (shift, alt, ctrl) for function keys. |
| 394 | * This gets called when we have already parsed CSI. |
| 395 | * |
| 396 | * @modifiers: bitmask (shift, alt, ctrl) |
| 397 | * @return: the unmodified code |
| 398 | */ |
| 399 | static char skip_modifiers(int *modifiers) |
| 400 | { |
| 401 | char c, mod = 0, ret = 0; |
| 402 | |
| 403 | c = getc(); |
| 404 | |
| 405 | if (c != ';') { |
| 406 | ret = c; |
| 407 | if (c == '~') |
| 408 | goto out; |
| 409 | c = getc(); |
| 410 | } |
| 411 | for (;;) { |
| 412 | switch (c) { |
| 413 | case '0'...'9': |
| 414 | mod *= 10; |
| 415 | mod += c - '0'; |
| 416 | /* fall through */ |
| 417 | case ';': |
| 418 | c = getc(); |
| 419 | break; |
| 420 | default: |
| 421 | goto out; |
| 422 | } |
| 423 | } |
| 424 | out: |
| 425 | if (mod) |
| 426 | --mod; |
| 427 | if (modifiers) |
| 428 | *modifiers = mod; |
| 429 | if (!ret) |
| 430 | ret = c; |
| 431 | return ret; |
| 432 | } |
| 433 | |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 434 | static efi_status_t EFIAPI efi_cin_read_key_stroke( |
| 435 | struct efi_simple_input_interface *this, |
| 436 | struct efi_input_key *key) |
| 437 | { |
| 438 | struct efi_input_key pressed_key = { |
| 439 | .scan_code = 0, |
| 440 | .unicode_char = 0, |
| 441 | }; |
| 442 | char ch; |
| 443 | |
| 444 | EFI_ENTRY("%p, %p", this, key); |
| 445 | |
| 446 | /* We don't do interrupts, so check for timers cooperatively */ |
| 447 | efi_timer_check(); |
| 448 | |
| 449 | if (!tstc()) { |
| 450 | /* No key pressed */ |
| 451 | return EFI_EXIT(EFI_NOT_READY); |
| 452 | } |
| 453 | |
| 454 | ch = getc(); |
| 455 | if (ch == cESC) { |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 456 | /* |
| 457 | * Xterm Control Sequences |
| 458 | * https://www.xfree86.org/4.8.0/ctlseqs.html |
| 459 | */ |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 460 | ch = getc(); |
| 461 | switch (ch) { |
| 462 | case cESC: /* ESC */ |
| 463 | pressed_key.scan_code = 23; |
| 464 | break; |
| 465 | case 'O': /* F1 - F4 */ |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 466 | ch = getc(); |
| 467 | /* skip modifiers */ |
| 468 | if (ch <= '9') |
| 469 | ch = getc(); |
| 470 | pressed_key.scan_code = ch - 'P' + 11; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 471 | break; |
| 472 | case 'a'...'z': |
| 473 | ch = ch - 'a'; |
| 474 | break; |
| 475 | case '[': |
| 476 | ch = getc(); |
| 477 | switch (ch) { |
| 478 | case 'A'...'D': /* up, down right, left */ |
| 479 | pressed_key.scan_code = ch - 'A' + 1; |
| 480 | break; |
| 481 | case 'F': /* End */ |
| 482 | pressed_key.scan_code = 6; |
| 483 | break; |
| 484 | case 'H': /* Home */ |
| 485 | pressed_key.scan_code = 5; |
| 486 | break; |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 487 | case '1': |
| 488 | ch = skip_modifiers(NULL); |
| 489 | switch (ch) { |
| 490 | case '1'...'5': /* F1 - F5 */ |
| 491 | pressed_key.scan_code = ch - '1' + 11; |
| 492 | break; |
| 493 | case '7'...'9': /* F6 - F8 */ |
| 494 | pressed_key.scan_code = ch - '7' + 16; |
| 495 | break; |
| 496 | case 'A'...'D': /* up, down right, left */ |
| 497 | pressed_key.scan_code = ch - 'A' + 1; |
| 498 | break; |
| 499 | case 'F': |
| 500 | pressed_key.scan_code = 6; /* End */ |
| 501 | break; |
| 502 | case 'H': |
| 503 | pressed_key.scan_code = 5; /* Home */ |
| 504 | break; |
| 505 | } |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 506 | break; |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 507 | case '2': |
| 508 | ch = skip_modifiers(NULL); |
| 509 | switch (ch) { |
| 510 | case '0'...'1': /* F9 - F10 */ |
| 511 | pressed_key.scan_code = ch - '0' + 19; |
| 512 | break; |
| 513 | case '3'...'4': /* F11 - F12 */ |
| 514 | pressed_key.scan_code = ch - '3' + 21; |
| 515 | break; |
| 516 | case '~': /* INS */ |
| 517 | pressed_key.scan_code = 7; |
| 518 | break; |
| 519 | } |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 520 | break; |
| 521 | case '3': /* DEL */ |
| 522 | pressed_key.scan_code = 8; |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 523 | skip_modifiers(NULL); |
| 524 | break; |
| 525 | case '5': /* PG UP */ |
| 526 | pressed_key.scan_code = 9; |
| 527 | skip_modifiers(NULL); |
| 528 | break; |
| 529 | case '6': /* PG DOWN */ |
| 530 | pressed_key.scan_code = 10; |
| 531 | skip_modifiers(NULL); |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 532 | break; |
| 533 | } |
| 534 | break; |
| 535 | } |
| 536 | } else if (ch == 0x7f) { |
| 537 | /* Backspace */ |
| 538 | ch = 0x08; |
| 539 | } |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 540 | if (!pressed_key.scan_code) |
| 541 | pressed_key.unicode_char = ch; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 542 | *key = pressed_key; |
| 543 | |
| 544 | return EFI_EXIT(EFI_SUCCESS); |
| 545 | } |
| 546 | |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 547 | struct efi_simple_input_interface efi_con_in = { |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 548 | .reset = efi_cin_reset, |
| 549 | .read_key_stroke = efi_cin_read_key_stroke, |
| 550 | .wait_for_key = NULL, |
| 551 | }; |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 552 | |
| 553 | static struct efi_event *console_timer_event; |
| 554 | |
xypron.glpk@gmx.de | ff92593 | 2017-07-20 05:26:07 +0200 | [diff] [blame] | 555 | static void EFIAPI efi_key_notify(struct efi_event *event, void *context) |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 556 | { |
| 557 | } |
| 558 | |
Heinrich Schuchardt | 9bc9664 | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 559 | /* |
| 560 | * Notification function of the console timer event. |
| 561 | * |
| 562 | * event: console timer event |
| 563 | * context: not used |
| 564 | */ |
xypron.glpk@gmx.de | ff92593 | 2017-07-20 05:26:07 +0200 | [diff] [blame] | 565 | static void EFIAPI efi_console_timer_notify(struct efi_event *event, |
| 566 | void *context) |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 567 | { |
| 568 | EFI_ENTRY("%p, %p", event, context); |
Heinrich Schuchardt | 9bc9664 | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 569 | |
| 570 | /* Check if input is available */ |
Heinrich Schuchardt | ca62a4f | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 571 | if (tstc()) { |
Heinrich Schuchardt | 9bc9664 | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 572 | /* Queue the wait for key event */ |
Heinrich Schuchardt | e190e89 | 2017-10-04 15:03:24 +0200 | [diff] [blame] | 573 | efi_con_in.wait_for_key->is_signaled = true; |
Heinrich Schuchardt | 9bc9664 | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 574 | efi_signal_event(efi_con_in.wait_for_key, true); |
| 575 | } |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 576 | EFI_EXIT(EFI_SUCCESS); |
| 577 | } |
| 578 | |
| 579 | /* This gets called from do_bootefi_exec(). */ |
| 580 | int efi_console_register(void) |
| 581 | { |
| 582 | efi_status_t r; |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 583 | struct efi_object *efi_console_output_obj; |
| 584 | struct efi_object *efi_console_input_obj; |
Rob Clark | a17e62c | 2017-07-24 10:39:01 -0400 | [diff] [blame] | 585 | |
Heinrich Schuchardt | a4aa7be | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 586 | /* Set up mode information */ |
| 587 | query_console_size(); |
| 588 | |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 589 | /* Create handles */ |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 590 | r = efi_create_handle((efi_handle_t *)&efi_console_output_obj); |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 591 | if (r != EFI_SUCCESS) |
| 592 | goto out_of_memory; |
| 593 | r = efi_add_protocol(efi_console_output_obj->handle, |
| 594 | &efi_guid_text_output_protocol, &efi_con_out); |
| 595 | if (r != EFI_SUCCESS) |
| 596 | goto out_of_memory; |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 597 | r = efi_create_handle((efi_handle_t *)&efi_console_input_obj); |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 598 | if (r != EFI_SUCCESS) |
| 599 | goto out_of_memory; |
| 600 | r = efi_add_protocol(efi_console_input_obj->handle, |
| 601 | &efi_guid_text_input_protocol, &efi_con_in); |
| 602 | if (r != EFI_SUCCESS) |
| 603 | goto out_of_memory; |
Rob Clark | a17e62c | 2017-07-24 10:39:01 -0400 | [diff] [blame] | 604 | |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 605 | /* Create console events */ |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 606 | r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify, |
| 607 | NULL, NULL, &efi_con_in.wait_for_key); |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 608 | if (r != EFI_SUCCESS) { |
| 609 | printf("ERROR: Failed to register WaitForKey event\n"); |
| 610 | return r; |
| 611 | } |
| 612 | r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK, |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 613 | efi_console_timer_notify, NULL, NULL, |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 614 | &console_timer_event); |
| 615 | if (r != EFI_SUCCESS) { |
| 616 | printf("ERROR: Failed to register console event\n"); |
| 617 | return r; |
| 618 | } |
| 619 | /* 5000 ns cycle is sufficient for 2 MBaud */ |
| 620 | r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50); |
| 621 | if (r != EFI_SUCCESS) |
| 622 | printf("ERROR: Failed to set console timer\n"); |
| 623 | return r; |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 624 | out_of_memory: |
| 625 | printf("ERROR: Out of meemory\n"); |
| 626 | return r; |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 627 | } |