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 | 110c628 | 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 | ebb4dd5 | 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 | 110c628 | 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 | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 51 | |
| 52 | #define cESC '\x1b' |
| 53 | #define ESC "\x1b" |
| 54 | |
Emmanuel Vadot | 5be8b0a | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 55 | /* Default to mode 0 */ |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 56 | static struct simple_text_output_mode efi_con_mode = { |
Emmanuel Vadot | 5be8b0a | 2016-11-08 06:03:29 +0100 | [diff] [blame] | 57 | .max_mode = 1, |
Alexander Graf | c1311ad | 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 | 6221729 | 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 | c1311ad | 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 | 6221729 | 2018-05-16 18:17:38 +0200 | [diff] [blame] | 90 | if (i >= num) |
Alexander Graf | c1311ad | 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 | 6221729 | 2018-05-16 18:17:38 +0200 | [diff] [blame] | 104 | if (i != num - 1) |
| 105 | return -1; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Alexander Graf | c1311ad | 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 | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 112 | const efi_string_t string) |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 113 | { |
Rob Clark | 3a45bc7 | 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 | ba7bd5c | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 116 | char *buf, *pos; |
| 117 | u16 *p; |
| 118 | efi_status_t ret = EFI_SUCCESS; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 119 | |
| 120 | EFI_ENTRY("%p, %p", this, string); |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 121 | |
Heinrich Schuchardt | ba7bd5c | 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 | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 129 | fputs(stdout, buf); |
Heinrich Schuchardt | ba7bd5c | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 130 | free(buf); |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 131 | |
Heinrich Schuchardt | 7ca7c3c | 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 | 14d103b | 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 | 7ca7c3c | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 138 | */ |
| 139 | for (p = string; *p; ++p) { |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 140 | switch (*p) { |
Heinrich Schuchardt | 7ca7c3c | 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 | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 143 | break; |
Heinrich Schuchardt | 7ca7c3c | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 144 | case '\n': /* U+000A, newline */ |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 145 | con->cursor_column = 0; |
| 146 | con->cursor_row++; |
| 147 | break; |
Heinrich Schuchardt | 7ca7c3c | 2018-04-29 16:24:25 +0200 | [diff] [blame] | 148 | case '\r': /* U+000D, carriage-return */ |
| 149 | con->cursor_column = 0; |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 150 | break; |
Heinrich Schuchardt | 7ca7c3c | 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 | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 156 | break; |
| 157 | default: |
| 158 | con->cursor_column++; |
| 159 | break; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 160 | } |
Rob Clark | 3a45bc7 | 2017-09-13 18:05:44 -0400 | [diff] [blame] | 161 | if (con->cursor_column >= mode->columns) { |
| 162 | con->cursor_column = 0; |
| 163 | con->cursor_row++; |
| 164 | } |
| 165 | con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1); |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Heinrich Schuchardt | ba7bd5c | 2018-08-31 21:31:32 +0200 | [diff] [blame] | 168 | out: |
| 169 | return EFI_EXIT(ret); |
Alexander Graf | c1311ad | 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 | 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 */ |
Heinrich Schuchardt | 14d103b | 2018-09-08 19:57:24 +0200 | [diff] [blame] | 309 | { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/ |
| 310 | { 37, 47 }, /* 7: light gray, map to white */ |
Rob Clark | 2d5dc2a | 2017-10-10 08:23:01 -0400 | [diff] [blame] | 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"); |
Heinrich Schuchardt | e67ff94 | 2018-07-05 08:18:00 +0200 | [diff] [blame] | 338 | efi_con_mode.cursor_column = 0; |
| 339 | efi_con_mode.cursor_row = 0; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 340 | |
| 341 | return EFI_EXIT(EFI_SUCCESS); |
| 342 | } |
| 343 | |
Heinrich Schuchardt | 9d12daf | 2018-07-05 19:58:07 +0200 | [diff] [blame] | 344 | static efi_status_t EFIAPI efi_cout_reset( |
| 345 | struct efi_simple_text_output_protocol *this, |
| 346 | char extended_verification) |
| 347 | { |
| 348 | EFI_ENTRY("%p, %d", this, extended_verification); |
| 349 | |
| 350 | /* Clear screen */ |
| 351 | EFI_CALL(efi_cout_clear_screen(this)); |
| 352 | /* Set default colors */ |
| 353 | printf(ESC "[0;37;40m"); |
| 354 | |
| 355 | return EFI_EXIT(EFI_SUCCESS); |
| 356 | } |
| 357 | |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 358 | static efi_status_t EFIAPI efi_cout_set_cursor_position( |
| 359 | struct efi_simple_text_output_protocol *this, |
| 360 | unsigned long column, unsigned long row) |
| 361 | { |
| 362 | EFI_ENTRY("%p, %ld, %ld", this, column, row); |
| 363 | |
| 364 | printf(ESC"[%d;%df", (int)row, (int)column); |
| 365 | efi_con_mode.cursor_column = column; |
| 366 | efi_con_mode.cursor_row = row; |
| 367 | |
| 368 | return EFI_EXIT(EFI_SUCCESS); |
| 369 | } |
| 370 | |
| 371 | static efi_status_t EFIAPI efi_cout_enable_cursor( |
| 372 | struct efi_simple_text_output_protocol *this, |
| 373 | bool enable) |
| 374 | { |
| 375 | EFI_ENTRY("%p, %d", this, enable); |
| 376 | |
| 377 | printf(ESC"[?25%c", enable ? 'h' : 'l'); |
| 378 | |
| 379 | return EFI_EXIT(EFI_SUCCESS); |
| 380 | } |
| 381 | |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 382 | struct efi_simple_text_output_protocol efi_con_out = { |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 383 | .reset = efi_cout_reset, |
| 384 | .output_string = efi_cout_output_string, |
| 385 | .test_string = efi_cout_test_string, |
| 386 | .query_mode = efi_cout_query_mode, |
| 387 | .set_mode = efi_cout_set_mode, |
| 388 | .set_attribute = efi_cout_set_attribute, |
| 389 | .clear_screen = efi_cout_clear_screen, |
| 390 | .set_cursor_position = efi_cout_set_cursor_position, |
| 391 | .enable_cursor = efi_cout_enable_cursor, |
| 392 | .mode = (void*)&efi_con_mode, |
| 393 | }; |
| 394 | |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 395 | static bool key_available; |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 396 | static struct efi_key_data next_key; |
Heinrich Schuchardt | 4f18789 | 2018-07-05 08:17:59 +0200 | [diff] [blame] | 397 | |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 398 | /** |
Heinrich Schuchardt | 55fbdf9 | 2018-09-11 22:38:09 +0200 | [diff] [blame^] | 399 | * set_shift_mask() - set shift mask |
| 400 | * |
| 401 | * @mod: Xterm shift mask |
| 402 | */ |
| 403 | void set_shift_mask(int mod, struct efi_key_state *key_state) |
| 404 | { |
| 405 | key_state->key_shift_state = EFI_SHIFT_STATE_VALID; |
| 406 | if (mod) { |
| 407 | --mod; |
| 408 | if (mod & 1) |
| 409 | key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED; |
| 410 | if (mod & 2) |
| 411 | key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED; |
| 412 | if (mod & 4) |
| 413 | key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED; |
| 414 | if (mod & 8) |
| 415 | key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED; |
| 416 | } else { |
| 417 | key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /** |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 422 | * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 423 | * |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 424 | * This gets called when we have already parsed CSI. |
| 425 | * |
| 426 | * @modifiers: bitmask (shift, alt, ctrl) |
| 427 | * @return: the unmodified code |
| 428 | */ |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 429 | static int analyze_modifiers(struct efi_key_state *key_state) |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 430 | { |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 431 | int c, mod = 0, ret = 0; |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 432 | |
| 433 | c = getc(); |
| 434 | |
| 435 | if (c != ';') { |
| 436 | ret = c; |
| 437 | if (c == '~') |
| 438 | goto out; |
| 439 | c = getc(); |
| 440 | } |
| 441 | for (;;) { |
| 442 | switch (c) { |
| 443 | case '0'...'9': |
| 444 | mod *= 10; |
| 445 | mod += c - '0'; |
| 446 | /* fall through */ |
| 447 | case ';': |
| 448 | c = getc(); |
| 449 | break; |
| 450 | default: |
| 451 | goto out; |
| 452 | } |
| 453 | } |
| 454 | out: |
Heinrich Schuchardt | 55fbdf9 | 2018-09-11 22:38:09 +0200 | [diff] [blame^] | 455 | set_shift_mask(mod, key_state); |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 456 | if (!ret) |
| 457 | ret = c; |
| 458 | return ret; |
| 459 | } |
| 460 | |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 461 | /** |
| 462 | * efi_cin_read_key() - read a key from the console input |
| 463 | * |
| 464 | * @key: - key received |
| 465 | * Return: - status code |
| 466 | */ |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 467 | static efi_status_t efi_cin_read_key(struct efi_key_data *key) |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 468 | { |
| 469 | struct efi_input_key pressed_key = { |
| 470 | .scan_code = 0, |
| 471 | .unicode_char = 0, |
| 472 | }; |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 473 | s32 ch; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 474 | |
Heinrich Schuchardt | 55fbdf9 | 2018-09-11 22:38:09 +0200 | [diff] [blame^] | 475 | if (console_read_unicode(&ch)) |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 476 | return EFI_NOT_READY; |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 477 | |
| 478 | key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID; |
| 479 | key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID; |
| 480 | |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 481 | /* We do not support multi-word codes */ |
| 482 | if (ch >= 0x10000) |
| 483 | ch = '?'; |
Heinrich Schuchardt | 55fbdf9 | 2018-09-11 22:38:09 +0200 | [diff] [blame^] | 484 | |
| 485 | switch (ch) { |
| 486 | case 0x1b: |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 487 | /* |
| 488 | * Xterm Control Sequences |
| 489 | * https://www.xfree86.org/4.8.0/ctlseqs.html |
| 490 | */ |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 491 | ch = getc(); |
| 492 | switch (ch) { |
| 493 | case cESC: /* ESC */ |
| 494 | pressed_key.scan_code = 23; |
| 495 | break; |
| 496 | case 'O': /* F1 - F4 */ |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 497 | ch = getc(); |
Heinrich Schuchardt | 55fbdf9 | 2018-09-11 22:38:09 +0200 | [diff] [blame^] | 498 | /* consider modifiers */ |
| 499 | if (ch < 'P') { |
| 500 | set_shift_mask(ch - '0', &key->key_state); |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 501 | ch = getc(); |
Heinrich Schuchardt | 55fbdf9 | 2018-09-11 22:38:09 +0200 | [diff] [blame^] | 502 | } |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 503 | pressed_key.scan_code = ch - 'P' + 11; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 504 | break; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 505 | case '[': |
| 506 | ch = getc(); |
| 507 | switch (ch) { |
| 508 | case 'A'...'D': /* up, down right, left */ |
| 509 | pressed_key.scan_code = ch - 'A' + 1; |
| 510 | break; |
| 511 | case 'F': /* End */ |
| 512 | pressed_key.scan_code = 6; |
| 513 | break; |
| 514 | case 'H': /* Home */ |
| 515 | pressed_key.scan_code = 5; |
| 516 | break; |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 517 | case '1': |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 518 | ch = analyze_modifiers(&key->key_state); |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 519 | switch (ch) { |
| 520 | case '1'...'5': /* F1 - F5 */ |
| 521 | pressed_key.scan_code = ch - '1' + 11; |
| 522 | break; |
| 523 | case '7'...'9': /* F6 - F8 */ |
| 524 | pressed_key.scan_code = ch - '7' + 16; |
| 525 | break; |
| 526 | case 'A'...'D': /* up, down right, left */ |
| 527 | pressed_key.scan_code = ch - 'A' + 1; |
| 528 | break; |
| 529 | case 'F': |
| 530 | pressed_key.scan_code = 6; /* End */ |
| 531 | break; |
| 532 | case 'H': |
| 533 | pressed_key.scan_code = 5; /* Home */ |
| 534 | break; |
| 535 | } |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 536 | break; |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 537 | case '2': |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 538 | ch = analyze_modifiers(&key->key_state); |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 539 | switch (ch) { |
| 540 | case '0'...'1': /* F9 - F10 */ |
| 541 | pressed_key.scan_code = ch - '0' + 19; |
| 542 | break; |
| 543 | case '3'...'4': /* F11 - F12 */ |
| 544 | pressed_key.scan_code = ch - '3' + 21; |
| 545 | break; |
| 546 | case '~': /* INS */ |
| 547 | pressed_key.scan_code = 7; |
| 548 | break; |
| 549 | } |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 550 | break; |
| 551 | case '3': /* DEL */ |
| 552 | pressed_key.scan_code = 8; |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 553 | analyze_modifiers(&key->key_state); |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 554 | break; |
| 555 | case '5': /* PG UP */ |
| 556 | pressed_key.scan_code = 9; |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 557 | analyze_modifiers(&key->key_state); |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 558 | break; |
| 559 | case '6': /* PG DOWN */ |
| 560 | pressed_key.scan_code = 10; |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 561 | analyze_modifiers(&key->key_state); |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 562 | break; |
Heinrich Schuchardt | 55fbdf9 | 2018-09-11 22:38:09 +0200 | [diff] [blame^] | 563 | } /* [ */ |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 564 | break; |
Heinrich Schuchardt | 55fbdf9 | 2018-09-11 22:38:09 +0200 | [diff] [blame^] | 565 | default: |
| 566 | /* ALT key */ |
| 567 | set_shift_mask(3, &key->key_state); |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 568 | } |
Heinrich Schuchardt | 55fbdf9 | 2018-09-11 22:38:09 +0200 | [diff] [blame^] | 569 | break; |
| 570 | case 0x7f: |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 571 | /* Backspace */ |
| 572 | ch = 0x08; |
| 573 | } |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 574 | if (pressed_key.scan_code) { |
| 575 | key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID; |
| 576 | } else { |
Heinrich Schuchardt | 0fb4169 | 2018-02-19 18:53:29 +0100 | [diff] [blame] | 577 | pressed_key.unicode_char = ch; |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 578 | |
| 579 | /* |
| 580 | * Assume left control key for control characters typically |
| 581 | * entered using the control key. |
| 582 | */ |
| 583 | if (ch >= 0x01 && ch <= 0x1f) { |
Heinrich Schuchardt | 55fbdf9 | 2018-09-11 22:38:09 +0200 | [diff] [blame^] | 584 | key->key_state.key_shift_state |= |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 585 | EFI_SHIFT_STATE_VALID; |
| 586 | switch (ch) { |
| 587 | case 0x01 ... 0x07: |
| 588 | case 0x0b ... 0x0c: |
| 589 | case 0x0e ... 0x1f: |
| 590 | key->key_state.key_shift_state |= |
| 591 | EFI_LEFT_CONTROL_PRESSED; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | key->key = pressed_key; |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 596 | |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 597 | return EFI_SUCCESS; |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * efi_cin_check() - check if keyboard input is available |
| 602 | */ |
| 603 | static void efi_cin_check(void) |
| 604 | { |
| 605 | efi_status_t ret; |
| 606 | |
| 607 | if (key_available) { |
| 608 | efi_signal_event(efi_con_in.wait_for_key, true); |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | if (tstc()) { |
| 613 | ret = efi_cin_read_key(&next_key); |
| 614 | if (ret == EFI_SUCCESS) { |
| 615 | key_available = true; |
| 616 | |
| 617 | /* Queue the wait for key event */ |
| 618 | efi_signal_event(efi_con_in.wait_for_key, true); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /** |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 624 | * efi_cin_empty_buffer() - empty input buffer |
| 625 | */ |
| 626 | static void efi_cin_empty_buffer(void) |
| 627 | { |
| 628 | while (tstc()) |
| 629 | getc(); |
| 630 | key_available = false; |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * efi_cin_reset_ex() - reset console input |
| 635 | * |
| 636 | * @this: - the extended simple text input protocol |
| 637 | * @extended_verification: - extended verification |
| 638 | * |
| 639 | * This function implements the reset service of the |
| 640 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. |
| 641 | * |
| 642 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 643 | * details. |
| 644 | * |
| 645 | * Return: old value of the task priority level |
| 646 | */ |
| 647 | static efi_status_t EFIAPI efi_cin_reset_ex( |
| 648 | struct efi_simple_text_input_ex_protocol *this, |
| 649 | bool extended_verification) |
| 650 | { |
| 651 | efi_status_t ret = EFI_SUCCESS; |
| 652 | |
| 653 | EFI_ENTRY("%p, %d", this, extended_verification); |
| 654 | |
| 655 | /* Check parameters */ |
| 656 | if (!this) { |
| 657 | ret = EFI_INVALID_PARAMETER; |
| 658 | goto out; |
| 659 | } |
| 660 | |
| 661 | efi_cin_empty_buffer(); |
| 662 | out: |
| 663 | return EFI_EXIT(ret); |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * efi_cin_read_key_stroke_ex() - read key stroke |
| 668 | * |
| 669 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
| 670 | * @key_data: key read from console |
| 671 | * Return: status code |
| 672 | * |
| 673 | * This function implements the ReadKeyStrokeEx service of the |
| 674 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. |
| 675 | * |
| 676 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 677 | * details. |
| 678 | */ |
| 679 | static efi_status_t EFIAPI efi_cin_read_key_stroke_ex( |
| 680 | struct efi_simple_text_input_ex_protocol *this, |
| 681 | struct efi_key_data *key_data) |
| 682 | { |
| 683 | efi_status_t ret = EFI_SUCCESS; |
| 684 | |
| 685 | EFI_ENTRY("%p, %p", this, key_data); |
| 686 | |
| 687 | /* Check parameters */ |
| 688 | if (!this || !key_data) { |
| 689 | ret = EFI_INVALID_PARAMETER; |
| 690 | goto out; |
| 691 | } |
| 692 | |
| 693 | /* We don't do interrupts, so check for timers cooperatively */ |
| 694 | efi_timer_check(); |
| 695 | |
| 696 | /* Enable console input after ExitBootServices */ |
| 697 | efi_cin_check(); |
| 698 | |
| 699 | if (!key_available) { |
| 700 | ret = EFI_NOT_READY; |
| 701 | goto out; |
| 702 | } |
| 703 | *key_data = next_key; |
| 704 | key_available = false; |
| 705 | efi_con_in.wait_for_key->is_signaled = false; |
| 706 | out: |
| 707 | return EFI_EXIT(ret); |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * efi_cin_set_state() - set toggle key state |
| 712 | * |
| 713 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
| 714 | * @key_toggle_state: key toggle state |
| 715 | * Return: status code |
| 716 | * |
| 717 | * This function implements the SetState service of the |
| 718 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. |
| 719 | * |
| 720 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 721 | * details. |
| 722 | */ |
| 723 | static efi_status_t EFIAPI efi_cin_set_state( |
| 724 | struct efi_simple_text_input_ex_protocol *this, |
| 725 | u8 key_toggle_state) |
| 726 | { |
| 727 | EFI_ENTRY("%p, %u", this, key_toggle_state); |
| 728 | /* |
| 729 | * U-Boot supports multiple console input sources like serial and |
| 730 | * net console for which a key toggle state cannot be set at all. |
| 731 | * |
| 732 | * According to the UEFI specification it is allowable to not implement |
| 733 | * this service. |
| 734 | */ |
| 735 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * efi_cin_register_key_notify() - register key notification function |
| 740 | * |
| 741 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
| 742 | * @key_data: key to be notified |
| 743 | * @key_notify_function: function to be called if the key is pressed |
| 744 | * @notify_handle: handle for unregistering the notification |
| 745 | * Return: status code |
| 746 | * |
| 747 | * This function implements the SetState service of the |
| 748 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. |
| 749 | * |
| 750 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 751 | * details. |
| 752 | */ |
| 753 | static efi_status_t EFIAPI efi_cin_register_key_notify( |
| 754 | struct efi_simple_text_input_ex_protocol *this, |
| 755 | struct efi_key_data *key_data, |
| 756 | efi_status_t (EFIAPI *key_notify_function)( |
| 757 | struct efi_key_data *key_data), |
| 758 | void **notify_handle) |
| 759 | { |
| 760 | EFI_ENTRY("%p, %p, %p, %p", |
| 761 | this, key_data, key_notify_function, notify_handle); |
| 762 | return EFI_EXIT(EFI_OUT_OF_RESOURCES); |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * efi_cin_unregister_key_notify() - unregister key notification function |
| 767 | * |
| 768 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
| 769 | * @notification_handle: handle received when registering |
| 770 | * Return: status code |
| 771 | * |
| 772 | * This function implements the SetState service of the |
| 773 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. |
| 774 | * |
| 775 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 776 | * details. |
| 777 | */ |
| 778 | static efi_status_t EFIAPI efi_cin_unregister_key_notify( |
| 779 | struct efi_simple_text_input_ex_protocol *this, |
| 780 | void *notification_handle) |
| 781 | { |
| 782 | EFI_ENTRY("%p, %p", this, notification_handle); |
| 783 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 784 | } |
| 785 | |
| 786 | |
| 787 | /** |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 788 | * efi_cin_reset() - drain the input buffer |
| 789 | * |
| 790 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
| 791 | * @extended_verification: allow for exhaustive verification |
| 792 | * Return: status code |
| 793 | * |
| 794 | * This function implements the Reset service of the |
| 795 | * EFI_SIMPLE_TEXT_INPUT_PROTOCOL. |
| 796 | * |
| 797 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 798 | * details. |
| 799 | */ |
| 800 | static efi_status_t EFIAPI efi_cin_reset |
| 801 | (struct efi_simple_text_input_protocol *this, |
| 802 | bool extended_verification) |
| 803 | { |
| 804 | efi_status_t ret = EFI_SUCCESS; |
| 805 | |
| 806 | EFI_ENTRY("%p, %d", this, extended_verification); |
| 807 | |
| 808 | /* Check parameters */ |
| 809 | if (!this) { |
| 810 | ret = EFI_INVALID_PARAMETER; |
| 811 | goto out; |
| 812 | } |
| 813 | |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 814 | efi_cin_empty_buffer(); |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 815 | out: |
| 816 | return EFI_EXIT(ret); |
| 817 | } |
| 818 | |
| 819 | /** |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 820 | * efi_cin_read_key_stroke() - read key stroke |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 821 | * |
| 822 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL |
| 823 | * @key: key read from console |
| 824 | * Return: status code |
| 825 | * |
| 826 | * This function implements the ReadKeyStroke service of the |
| 827 | * EFI_SIMPLE_TEXT_INPUT_PROTOCOL. |
| 828 | * |
| 829 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 830 | * details. |
| 831 | */ |
| 832 | static efi_status_t EFIAPI efi_cin_read_key_stroke |
| 833 | (struct efi_simple_text_input_protocol *this, |
| 834 | struct efi_input_key *key) |
| 835 | { |
| 836 | efi_status_t ret = EFI_SUCCESS; |
| 837 | |
| 838 | EFI_ENTRY("%p, %p", this, key); |
| 839 | |
| 840 | /* Check parameters */ |
| 841 | if (!this || !key) { |
| 842 | ret = EFI_INVALID_PARAMETER; |
| 843 | goto out; |
| 844 | } |
| 845 | |
| 846 | /* We don't do interrupts, so check for timers cooperatively */ |
| 847 | efi_timer_check(); |
| 848 | |
| 849 | /* Enable console input after ExitBootServices */ |
| 850 | efi_cin_check(); |
| 851 | |
| 852 | if (!key_available) { |
| 853 | ret = EFI_NOT_READY; |
| 854 | goto out; |
| 855 | } |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 856 | *key = next_key.key; |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 857 | key_available = false; |
| 858 | efi_con_in.wait_for_key->is_signaled = false; |
| 859 | out: |
| 860 | return EFI_EXIT(ret); |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 861 | } |
| 862 | |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 863 | static struct efi_simple_text_input_ex_protocol efi_con_in_ex = { |
| 864 | .reset = efi_cin_reset_ex, |
| 865 | .read_key_stroke_ex = efi_cin_read_key_stroke_ex, |
| 866 | .wait_for_key_ex = NULL, |
| 867 | .set_state = efi_cin_set_state, |
| 868 | .register_key_notify = efi_cin_register_key_notify, |
| 869 | .unregister_key_notify = efi_cin_unregister_key_notify, |
| 870 | }; |
| 871 | |
Heinrich Schuchardt | 3e603ec | 2018-09-08 10:20:10 +0200 | [diff] [blame] | 872 | struct efi_simple_text_input_protocol efi_con_in = { |
Alexander Graf | c1311ad | 2016-03-04 01:10:00 +0100 | [diff] [blame] | 873 | .reset = efi_cin_reset, |
| 874 | .read_key_stroke = efi_cin_read_key_stroke, |
| 875 | .wait_for_key = NULL, |
| 876 | }; |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 877 | |
| 878 | static struct efi_event *console_timer_event; |
| 879 | |
Heinrich Schuchardt | 9bc9664 | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 880 | /* |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 881 | * efi_console_timer_notify() - notify the console timer event |
Heinrich Schuchardt | 9bc9664 | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 882 | * |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 883 | * @event: console timer event |
| 884 | * @context: not used |
Heinrich Schuchardt | 9bc9664 | 2018-01-19 20:24:51 +0100 | [diff] [blame] | 885 | */ |
xypron.glpk@gmx.de | ff92593 | 2017-07-20 05:26:07 +0200 | [diff] [blame] | 886 | static void EFIAPI efi_console_timer_notify(struct efi_event *event, |
| 887 | void *context) |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 888 | { |
| 889 | EFI_ENTRY("%p, %p", event, context); |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 890 | efi_cin_check(); |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 891 | EFI_EXIT(EFI_SUCCESS); |
| 892 | } |
| 893 | |
Heinrich Schuchardt | 0dfd13a | 2018-09-11 22:38:05 +0200 | [diff] [blame] | 894 | /** |
| 895 | * efi_key_notify() - notify the wait for key event |
| 896 | * |
| 897 | * @event: wait for key event |
| 898 | * @context: not used |
| 899 | */ |
| 900 | static void EFIAPI efi_key_notify(struct efi_event *event, void *context) |
| 901 | { |
| 902 | EFI_ENTRY("%p, %p", event, context); |
| 903 | efi_cin_check(); |
| 904 | EFI_EXIT(EFI_SUCCESS); |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * efi_console_register() - install the console protocols |
| 909 | * |
| 910 | * This function is called from do_bootefi_exec(). |
| 911 | */ |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 912 | int efi_console_register(void) |
| 913 | { |
| 914 | efi_status_t r; |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 915 | struct efi_object *efi_console_output_obj; |
| 916 | struct efi_object *efi_console_input_obj; |
Rob Clark | a17e62c | 2017-07-24 10:39:01 -0400 | [diff] [blame] | 917 | |
Heinrich Schuchardt | a4aa7be | 2018-04-29 20:02:46 +0200 | [diff] [blame] | 918 | /* Set up mode information */ |
| 919 | query_console_size(); |
| 920 | |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 921 | /* Create handles */ |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 922 | r = efi_create_handle((efi_handle_t *)&efi_console_output_obj); |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 923 | if (r != EFI_SUCCESS) |
| 924 | goto out_of_memory; |
Alexander Graf | 40e3e75 | 2018-09-04 14:59:11 +0200 | [diff] [blame] | 925 | |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 926 | r = efi_add_protocol(efi_console_output_obj->handle, |
| 927 | &efi_guid_text_output_protocol, &efi_con_out); |
| 928 | if (r != EFI_SUCCESS) |
| 929 | goto out_of_memory; |
Alexander Graf | 40e3e75 | 2018-09-04 14:59:11 +0200 | [diff] [blame] | 930 | systab.con_out_handle = efi_console_output_obj->handle; |
| 931 | systab.stderr_handle = efi_console_output_obj->handle; |
| 932 | |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 933 | r = efi_create_handle((efi_handle_t *)&efi_console_input_obj); |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 934 | if (r != EFI_SUCCESS) |
| 935 | goto out_of_memory; |
Alexander Graf | 40e3e75 | 2018-09-04 14:59:11 +0200 | [diff] [blame] | 936 | |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 937 | r = efi_add_protocol(efi_console_input_obj->handle, |
| 938 | &efi_guid_text_input_protocol, &efi_con_in); |
| 939 | if (r != EFI_SUCCESS) |
| 940 | goto out_of_memory; |
Alexander Graf | 40e3e75 | 2018-09-04 14:59:11 +0200 | [diff] [blame] | 941 | systab.con_in_handle = efi_console_input_obj->handle; |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 942 | r = efi_add_protocol(efi_console_input_obj->handle, |
| 943 | &efi_guid_text_input_ex_protocol, &efi_con_in_ex); |
| 944 | if (r != EFI_SUCCESS) |
| 945 | goto out_of_memory; |
Rob Clark | a17e62c | 2017-07-24 10:39:01 -0400 | [diff] [blame] | 946 | |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 947 | /* Create console events */ |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 948 | r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify, |
| 949 | NULL, NULL, &efi_con_in.wait_for_key); |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 950 | if (r != EFI_SUCCESS) { |
| 951 | printf("ERROR: Failed to register WaitForKey event\n"); |
| 952 | return r; |
| 953 | } |
Heinrich Schuchardt | 110c628 | 2018-09-11 22:38:08 +0200 | [diff] [blame] | 954 | efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key; |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 955 | r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK, |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 956 | efi_console_timer_notify, NULL, NULL, |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 957 | &console_timer_event); |
| 958 | if (r != EFI_SUCCESS) { |
| 959 | printf("ERROR: Failed to register console event\n"); |
| 960 | return r; |
| 961 | } |
| 962 | /* 5000 ns cycle is sufficient for 2 MBaud */ |
| 963 | r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50); |
| 964 | if (r != EFI_SUCCESS) |
| 965 | printf("ERROR: Failed to set console timer\n"); |
| 966 | return r; |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 967 | out_of_memory: |
Heinrich Schuchardt | 14d103b | 2018-09-08 19:57:24 +0200 | [diff] [blame] | 968 | printf("ERROR: Out of memory\n"); |
Heinrich Schuchardt | ebb4dd5 | 2017-10-26 19:25:59 +0200 | [diff] [blame] | 969 | return r; |
xypron.glpk@gmx.de | 91be9a7 | 2017-07-18 20:17:22 +0200 | [diff] [blame] | 970 | } |