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