]>
Commit | Line | Data |
---|---|---|
f739fcd8 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
c1311ad4 AG |
2 | /* |
3 | * EFI application console interface | |
4 | * | |
5 | * Copyright (c) 2016 Alexander Graf | |
c1311ad4 AG |
6 | */ |
7 | ||
8 | #include <common.h> | |
78178bb0 | 9 | #include <charset.h> |
a18c5a83 | 10 | #include <dm/device.h> |
c1311ad4 | 11 | #include <efi_loader.h> |
a18c5a83 RC |
12 | #include <stdio_dev.h> |
13 | #include <video_console.h> | |
c1311ad4 | 14 | |
5be8b0a3 EV |
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 | ||
110c6280 HS |
45 | const efi_guid_t efi_guid_text_input_ex_protocol = |
46 | EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID; | |
ebb4dd5b HS |
47 | const efi_guid_t efi_guid_text_input_protocol = |
48 | EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID; | |
110c6280 HS |
49 | const efi_guid_t efi_guid_text_output_protocol = |
50 | EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID; | |
c1311ad4 AG |
51 | |
52 | #define cESC '\x1b' | |
53 | #define ESC "\x1b" | |
54 | ||
5be8b0a3 | 55 | /* Default to mode 0 */ |
c1311ad4 | 56 | static struct simple_text_output_mode efi_con_mode = { |
5be8b0a3 | 57 | .max_mode = 1, |
c1311ad4 AG |
58 | .mode = 0, |
59 | .attribute = 0, | |
60 | .cursor_column = 0, | |
61 | .cursor_row = 0, | |
62 | .cursor_visible = 1, | |
63 | }; | |
64 | ||
62217295 HS |
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) | |
c1311ad4 AG |
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++; | |
62217295 | 90 | if (i >= num) |
c1311ad4 AG |
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 | } | |
62217295 HS |
104 | if (i != num - 1) |
105 | return -1; | |
c1311ad4 AG |
106 | |
107 | return 0; | |
108 | } | |
109 | ||
c1311ad4 AG |
110 | static efi_status_t EFIAPI efi_cout_output_string( |
111 | struct efi_simple_text_output_protocol *this, | |
3a45bc7f | 112 | const efi_string_t string) |
c1311ad4 | 113 | { |
3a45bc7f RC |
114 | struct simple_text_output_mode *con = &efi_con_mode; |
115 | struct cout_mode *mode = &efi_cout_modes[con->mode]; | |
ba7bd5c2 | 116 | char *buf, *pos; |
7ca7c3c0 | 117 | u16 *p; |
ba7bd5c2 | 118 | efi_status_t ret = EFI_SUCCESS; |
3a45bc7f | 119 | |
ba7bd5c2 | 120 | EFI_ENTRY("%p, %p", this, string); |
3a45bc7f | 121 | |
ba7bd5c2 HS |
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); | |
3a45bc7f | 129 | fputs(stdout, buf); |
ba7bd5c2 | 130 | free(buf); |
3a45bc7f | 131 | |
7ca7c3c0 HS |
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 | |
14d103bb | 137 | * U+0007 (BEL) and U+0009 (TAB), have to increase the column by one. |
7ca7c3c0 HS |
138 | */ |
139 | for (p = string; *p; ++p) { | |
3a45bc7f | 140 | switch (*p) { |
7ca7c3c0 HS |
141 | case '\b': /* U+0008, backspace */ |
142 | con->cursor_column = max(0, con->cursor_column - 1); | |
3a45bc7f | 143 | break; |
7ca7c3c0 | 144 | case '\n': /* U+000A, newline */ |
3a45bc7f RC |
145 | con->cursor_column = 0; |
146 | con->cursor_row++; | |
147 | break; | |
7ca7c3c0 HS |
148 | case '\r': /* U+000D, carriage-return */ |
149 | con->cursor_column = 0; | |
3a45bc7f | 150 | break; |
7ca7c3c0 HS |
151 | case 0xd800 ... 0xdbff: |
152 | /* | |
153 | * Ignore high surrogates, we do not want to count a | |
154 | * Unicode character twice. | |
155 | */ | |
3a45bc7f RC |
156 | break; |
157 | default: | |
158 | con->cursor_column++; | |
159 | break; | |
160 | } | |
161 | if (con->cursor_column >= mode->columns) { | |
162 | con->cursor_column = 0; | |
163 | con->cursor_row++; | |
c1311ad4 | 164 | } |
3a45bc7f | 165 | con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1); |
c1311ad4 AG |
166 | } |
167 | ||
ba7bd5c2 HS |
168 | out: |
169 | return EFI_EXIT(ret); | |
c1311ad4 AG |
170 | } |
171 | ||
172 | static efi_status_t EFIAPI efi_cout_test_string( | |
173 | struct efi_simple_text_output_protocol *this, | |
3a45bc7f | 174 | const efi_string_t string) |
c1311ad4 AG |
175 | { |
176 | EFI_ENTRY("%p, %p", this, string); | |
177 | return EFI_EXIT(EFI_SUCCESS); | |
178 | } | |
179 | ||
5be8b0a3 EV |
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 | ||
71cc25c3 RC |
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 | ||
a4aa7bef HS |
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"); | |
80483b2a | 226 | int rows = 25, cols = 80; |
a4aa7bef HS |
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 | ||
c1311ad4 AG |
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 | ||
5be8b0a3 EV |
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 | ||
c1311ad4 | 274 | if (columns) |
5be8b0a3 | 275 | *columns = efi_cout_modes[mode_number].columns; |
c1311ad4 | 276 | if (rows) |
5be8b0a3 | 277 | *rows = efi_cout_modes[mode_number].rows; |
c1311ad4 AG |
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 | ||
c1311ad4 | 288 | |
5be8b0a3 EV |
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); | |
c1311ad4 AG |
297 | } |
298 | ||
2d5dc2a5 RC |
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 */ | |
14d103bb HS |
309 | { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/ |
310 | { 37, 47 }, /* 7: light gray, map to white */ | |
2d5dc2a5 RC |
311 | }; |
312 | ||
313 | /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */ | |
c1311ad4 AG |
314 | static efi_status_t EFIAPI efi_cout_set_attribute( |
315 | struct efi_simple_text_output_protocol *this, | |
316 | unsigned long attribute) | |
317 | { | |
2d5dc2a5 RC |
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 | ||
c1311ad4 AG |
322 | EFI_ENTRY("%p, %lx", this, attribute); |
323 | ||
2d5dc2a5 RC |
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); | |
c1311ad4 AG |
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"); | |
e67ff94d HS |
338 | efi_con_mode.cursor_column = 0; |
339 | efi_con_mode.cursor_row = 0; | |
c1311ad4 AG |
340 | |
341 | return EFI_EXIT(EFI_SUCCESS); | |
342 | } | |
343 | ||
9d12daff HS |
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 | ||
c1311ad4 AG |
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 | { | |
aaace4b0 HS |
362 | efi_status_t ret = EFI_SUCCESS; |
363 | struct simple_text_output_mode *con = &efi_con_mode; | |
364 | struct cout_mode *mode = &efi_cout_modes[con->mode]; | |
365 | ||
c1311ad4 AG |
366 | EFI_ENTRY("%p, %ld, %ld", this, column, row); |
367 | ||
aaace4b0 HS |
368 | /* Check parameters */ |
369 | if (!this) { | |
370 | ret = EFI_INVALID_PARAMETER; | |
371 | goto out; | |
372 | } | |
373 | if (row >= mode->rows || column >= mode->columns) { | |
374 | ret = EFI_UNSUPPORTED; | |
375 | goto out; | |
376 | } | |
377 | ||
378 | /* | |
379 | * Set cursor position by sending CSI H. | |
380 | * EFI origin is [0, 0], terminal origin is [1, 1]. | |
381 | */ | |
382 | printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1); | |
c1311ad4 AG |
383 | efi_con_mode.cursor_column = column; |
384 | efi_con_mode.cursor_row = row; | |
aaace4b0 HS |
385 | out: |
386 | return EFI_EXIT(ret); | |
c1311ad4 AG |
387 | } |
388 | ||
389 | static efi_status_t EFIAPI efi_cout_enable_cursor( | |
390 | struct efi_simple_text_output_protocol *this, | |
391 | bool enable) | |
392 | { | |
393 | EFI_ENTRY("%p, %d", this, enable); | |
394 | ||
395 | printf(ESC"[?25%c", enable ? 'h' : 'l'); | |
396 | ||
397 | return EFI_EXIT(EFI_SUCCESS); | |
398 | } | |
399 | ||
ebb4dd5b | 400 | struct efi_simple_text_output_protocol efi_con_out = { |
c1311ad4 AG |
401 | .reset = efi_cout_reset, |
402 | .output_string = efi_cout_output_string, | |
403 | .test_string = efi_cout_test_string, | |
404 | .query_mode = efi_cout_query_mode, | |
405 | .set_mode = efi_cout_set_mode, | |
406 | .set_attribute = efi_cout_set_attribute, | |
407 | .clear_screen = efi_cout_clear_screen, | |
408 | .set_cursor_position = efi_cout_set_cursor_position, | |
409 | .enable_cursor = efi_cout_enable_cursor, | |
410 | .mode = (void*)&efi_con_mode, | |
411 | }; | |
412 | ||
4fdcf066 HS |
413 | /** |
414 | * struct efi_cin_notify_function - registered console input notify function | |
415 | * | |
416 | * @link: link to list | |
417 | * @data: key to notify | |
418 | * @function: function to call | |
419 | */ | |
420 | struct efi_cin_notify_function { | |
421 | struct list_head link; | |
422 | struct efi_key_data key; | |
423 | efi_status_t (EFIAPI *function) | |
424 | (struct efi_key_data *key_data); | |
425 | }; | |
426 | ||
0dfd13a4 | 427 | static bool key_available; |
110c6280 | 428 | static struct efi_key_data next_key; |
4fdcf066 | 429 | static LIST_HEAD(cin_notify_functions); |
4f187897 | 430 | |
55fbdf99 HS |
431 | /** |
432 | * set_shift_mask() - set shift mask | |
433 | * | |
434 | * @mod: Xterm shift mask | |
435 | */ | |
436 | void set_shift_mask(int mod, struct efi_key_state *key_state) | |
437 | { | |
438 | key_state->key_shift_state = EFI_SHIFT_STATE_VALID; | |
439 | if (mod) { | |
440 | --mod; | |
441 | if (mod & 1) | |
442 | key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED; | |
443 | if (mod & 2) | |
444 | key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED; | |
445 | if (mod & 4) | |
446 | key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED; | |
447 | if (mod & 8) | |
448 | key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED; | |
449 | } else { | |
450 | key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED; | |
451 | } | |
452 | } | |
453 | ||
0dfd13a4 | 454 | /** |
110c6280 | 455 | * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys |
0dfd13a4 | 456 | * |
0fb4169e HS |
457 | * This gets called when we have already parsed CSI. |
458 | * | |
459 | * @modifiers: bitmask (shift, alt, ctrl) | |
460 | * @return: the unmodified code | |
461 | */ | |
110c6280 | 462 | static int analyze_modifiers(struct efi_key_state *key_state) |
0fb4169e | 463 | { |
110c6280 | 464 | int c, mod = 0, ret = 0; |
0fb4169e HS |
465 | |
466 | c = getc(); | |
467 | ||
468 | if (c != ';') { | |
469 | ret = c; | |
470 | if (c == '~') | |
471 | goto out; | |
472 | c = getc(); | |
473 | } | |
474 | for (;;) { | |
475 | switch (c) { | |
476 | case '0'...'9': | |
477 | mod *= 10; | |
478 | mod += c - '0'; | |
479 | /* fall through */ | |
480 | case ';': | |
481 | c = getc(); | |
482 | break; | |
483 | default: | |
484 | goto out; | |
485 | } | |
486 | } | |
487 | out: | |
55fbdf99 | 488 | set_shift_mask(mod, key_state); |
0fb4169e HS |
489 | if (!ret) |
490 | ret = c; | |
491 | return ret; | |
492 | } | |
493 | ||
0dfd13a4 HS |
494 | /** |
495 | * efi_cin_read_key() - read a key from the console input | |
496 | * | |
497 | * @key: - key received | |
498 | * Return: - status code | |
499 | */ | |
110c6280 | 500 | static efi_status_t efi_cin_read_key(struct efi_key_data *key) |
c1311ad4 AG |
501 | { |
502 | struct efi_input_key pressed_key = { | |
503 | .scan_code = 0, | |
504 | .unicode_char = 0, | |
505 | }; | |
35cbb796 | 506 | s32 ch; |
c1311ad4 | 507 | |
55fbdf99 | 508 | if (console_read_unicode(&ch)) |
0dfd13a4 | 509 | return EFI_NOT_READY; |
110c6280 HS |
510 | |
511 | key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID; | |
512 | key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID; | |
513 | ||
35cbb796 HS |
514 | /* We do not support multi-word codes */ |
515 | if (ch >= 0x10000) | |
516 | ch = '?'; | |
55fbdf99 HS |
517 | |
518 | switch (ch) { | |
519 | case 0x1b: | |
0fb4169e HS |
520 | /* |
521 | * Xterm Control Sequences | |
522 | * https://www.xfree86.org/4.8.0/ctlseqs.html | |
523 | */ | |
c1311ad4 AG |
524 | ch = getc(); |
525 | switch (ch) { | |
526 | case cESC: /* ESC */ | |
527 | pressed_key.scan_code = 23; | |
528 | break; | |
529 | case 'O': /* F1 - F4 */ | |
0fb4169e | 530 | ch = getc(); |
55fbdf99 HS |
531 | /* consider modifiers */ |
532 | if (ch < 'P') { | |
533 | set_shift_mask(ch - '0', &key->key_state); | |
0fb4169e | 534 | ch = getc(); |
55fbdf99 | 535 | } |
0fb4169e | 536 | pressed_key.scan_code = ch - 'P' + 11; |
c1311ad4 | 537 | break; |
c1311ad4 AG |
538 | case '[': |
539 | ch = getc(); | |
540 | switch (ch) { | |
541 | case 'A'...'D': /* up, down right, left */ | |
542 | pressed_key.scan_code = ch - 'A' + 1; | |
543 | break; | |
544 | case 'F': /* End */ | |
545 | pressed_key.scan_code = 6; | |
546 | break; | |
547 | case 'H': /* Home */ | |
548 | pressed_key.scan_code = 5; | |
549 | break; | |
0fb4169e | 550 | case '1': |
110c6280 | 551 | ch = analyze_modifiers(&key->key_state); |
0fb4169e HS |
552 | switch (ch) { |
553 | case '1'...'5': /* F1 - F5 */ | |
554 | pressed_key.scan_code = ch - '1' + 11; | |
555 | break; | |
556 | case '7'...'9': /* F6 - F8 */ | |
557 | pressed_key.scan_code = ch - '7' + 16; | |
558 | break; | |
559 | case 'A'...'D': /* up, down right, left */ | |
560 | pressed_key.scan_code = ch - 'A' + 1; | |
561 | break; | |
562 | case 'F': | |
563 | pressed_key.scan_code = 6; /* End */ | |
564 | break; | |
565 | case 'H': | |
566 | pressed_key.scan_code = 5; /* Home */ | |
567 | break; | |
568 | } | |
c1311ad4 | 569 | break; |
0fb4169e | 570 | case '2': |
110c6280 | 571 | ch = analyze_modifiers(&key->key_state); |
0fb4169e HS |
572 | switch (ch) { |
573 | case '0'...'1': /* F9 - F10 */ | |
574 | pressed_key.scan_code = ch - '0' + 19; | |
575 | break; | |
576 | case '3'...'4': /* F11 - F12 */ | |
577 | pressed_key.scan_code = ch - '3' + 21; | |
578 | break; | |
579 | case '~': /* INS */ | |
580 | pressed_key.scan_code = 7; | |
581 | break; | |
582 | } | |
c1311ad4 AG |
583 | break; |
584 | case '3': /* DEL */ | |
585 | pressed_key.scan_code = 8; | |
110c6280 | 586 | analyze_modifiers(&key->key_state); |
0fb4169e HS |
587 | break; |
588 | case '5': /* PG UP */ | |
589 | pressed_key.scan_code = 9; | |
110c6280 | 590 | analyze_modifiers(&key->key_state); |
0fb4169e HS |
591 | break; |
592 | case '6': /* PG DOWN */ | |
593 | pressed_key.scan_code = 10; | |
110c6280 | 594 | analyze_modifiers(&key->key_state); |
c1311ad4 | 595 | break; |
55fbdf99 | 596 | } /* [ */ |
c1311ad4 | 597 | break; |
55fbdf99 HS |
598 | default: |
599 | /* ALT key */ | |
600 | set_shift_mask(3, &key->key_state); | |
c1311ad4 | 601 | } |
55fbdf99 HS |
602 | break; |
603 | case 0x7f: | |
c1311ad4 AG |
604 | /* Backspace */ |
605 | ch = 0x08; | |
606 | } | |
110c6280 HS |
607 | if (pressed_key.scan_code) { |
608 | key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID; | |
609 | } else { | |
0fb4169e | 610 | pressed_key.unicode_char = ch; |
110c6280 HS |
611 | |
612 | /* | |
613 | * Assume left control key for control characters typically | |
614 | * entered using the control key. | |
615 | */ | |
616 | if (ch >= 0x01 && ch <= 0x1f) { | |
55fbdf99 | 617 | key->key_state.key_shift_state |= |
110c6280 HS |
618 | EFI_SHIFT_STATE_VALID; |
619 | switch (ch) { | |
620 | case 0x01 ... 0x07: | |
621 | case 0x0b ... 0x0c: | |
622 | case 0x0e ... 0x1f: | |
623 | key->key_state.key_shift_state |= | |
624 | EFI_LEFT_CONTROL_PRESSED; | |
625 | } | |
626 | } | |
627 | } | |
628 | key->key = pressed_key; | |
c1311ad4 | 629 | |
0dfd13a4 HS |
630 | return EFI_SUCCESS; |
631 | } | |
632 | ||
4fdcf066 HS |
633 | /** |
634 | * efi_cin_notify() - notify registered functions | |
635 | */ | |
636 | static void efi_cin_notify(void) | |
637 | { | |
638 | struct efi_cin_notify_function *item; | |
639 | ||
640 | list_for_each_entry(item, &cin_notify_functions, link) { | |
641 | bool match = true; | |
642 | ||
643 | /* We do not support toggle states */ | |
644 | if (item->key.key.unicode_char || item->key.key.scan_code) { | |
645 | if (item->key.key.unicode_char != | |
646 | next_key.key.unicode_char || | |
647 | item->key.key.scan_code != next_key.key.scan_code) | |
648 | match = false; | |
649 | } | |
650 | if (item->key.key_state.key_shift_state && | |
651 | item->key.key_state.key_shift_state != | |
652 | next_key.key_state.key_shift_state) | |
653 | match = false; | |
654 | ||
655 | if (match) | |
656 | /* We don't bother about the return code */ | |
657 | EFI_CALL(item->function(&next_key)); | |
658 | } | |
659 | } | |
660 | ||
0dfd13a4 HS |
661 | /** |
662 | * efi_cin_check() - check if keyboard input is available | |
663 | */ | |
664 | static void efi_cin_check(void) | |
665 | { | |
666 | efi_status_t ret; | |
667 | ||
668 | if (key_available) { | |
669 | efi_signal_event(efi_con_in.wait_for_key, true); | |
670 | return; | |
671 | } | |
672 | ||
673 | if (tstc()) { | |
674 | ret = efi_cin_read_key(&next_key); | |
675 | if (ret == EFI_SUCCESS) { | |
676 | key_available = true; | |
677 | ||
4fdcf066 HS |
678 | /* Notify registered functions */ |
679 | efi_cin_notify(); | |
680 | ||
0dfd13a4 | 681 | /* Queue the wait for key event */ |
4fdcf066 HS |
682 | if (key_available) |
683 | efi_signal_event(efi_con_in.wait_for_key, true); | |
0dfd13a4 HS |
684 | } |
685 | } | |
686 | } | |
687 | ||
110c6280 HS |
688 | /** |
689 | * efi_cin_empty_buffer() - empty input buffer | |
690 | */ | |
691 | static void efi_cin_empty_buffer(void) | |
692 | { | |
693 | while (tstc()) | |
694 | getc(); | |
695 | key_available = false; | |
696 | } | |
697 | ||
698 | /** | |
699 | * efi_cin_reset_ex() - reset console input | |
700 | * | |
701 | * @this: - the extended simple text input protocol | |
702 | * @extended_verification: - extended verification | |
703 | * | |
704 | * This function implements the reset service of the | |
705 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. | |
706 | * | |
707 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
708 | * details. | |
709 | * | |
710 | * Return: old value of the task priority level | |
711 | */ | |
712 | static efi_status_t EFIAPI efi_cin_reset_ex( | |
713 | struct efi_simple_text_input_ex_protocol *this, | |
714 | bool extended_verification) | |
715 | { | |
716 | efi_status_t ret = EFI_SUCCESS; | |
717 | ||
718 | EFI_ENTRY("%p, %d", this, extended_verification); | |
719 | ||
720 | /* Check parameters */ | |
721 | if (!this) { | |
722 | ret = EFI_INVALID_PARAMETER; | |
723 | goto out; | |
724 | } | |
725 | ||
726 | efi_cin_empty_buffer(); | |
727 | out: | |
728 | return EFI_EXIT(ret); | |
729 | } | |
730 | ||
731 | /** | |
732 | * efi_cin_read_key_stroke_ex() - read key stroke | |
733 | * | |
734 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL | |
735 | * @key_data: key read from console | |
736 | * Return: status code | |
737 | * | |
738 | * This function implements the ReadKeyStrokeEx service of the | |
739 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. | |
740 | * | |
741 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
742 | * details. | |
743 | */ | |
744 | static efi_status_t EFIAPI efi_cin_read_key_stroke_ex( | |
745 | struct efi_simple_text_input_ex_protocol *this, | |
746 | struct efi_key_data *key_data) | |
747 | { | |
748 | efi_status_t ret = EFI_SUCCESS; | |
749 | ||
750 | EFI_ENTRY("%p, %p", this, key_data); | |
751 | ||
752 | /* Check parameters */ | |
753 | if (!this || !key_data) { | |
754 | ret = EFI_INVALID_PARAMETER; | |
755 | goto out; | |
756 | } | |
757 | ||
758 | /* We don't do interrupts, so check for timers cooperatively */ | |
759 | efi_timer_check(); | |
760 | ||
761 | /* Enable console input after ExitBootServices */ | |
762 | efi_cin_check(); | |
763 | ||
764 | if (!key_available) { | |
765 | ret = EFI_NOT_READY; | |
766 | goto out; | |
767 | } | |
768 | *key_data = next_key; | |
769 | key_available = false; | |
770 | efi_con_in.wait_for_key->is_signaled = false; | |
771 | out: | |
772 | return EFI_EXIT(ret); | |
773 | } | |
774 | ||
775 | /** | |
776 | * efi_cin_set_state() - set toggle key state | |
777 | * | |
778 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL | |
779 | * @key_toggle_state: key toggle state | |
780 | * Return: status code | |
781 | * | |
782 | * This function implements the SetState service of the | |
783 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. | |
784 | * | |
785 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
786 | * details. | |
787 | */ | |
788 | static efi_status_t EFIAPI efi_cin_set_state( | |
789 | struct efi_simple_text_input_ex_protocol *this, | |
790 | u8 key_toggle_state) | |
791 | { | |
792 | EFI_ENTRY("%p, %u", this, key_toggle_state); | |
793 | /* | |
794 | * U-Boot supports multiple console input sources like serial and | |
795 | * net console for which a key toggle state cannot be set at all. | |
796 | * | |
797 | * According to the UEFI specification it is allowable to not implement | |
798 | * this service. | |
799 | */ | |
800 | return EFI_EXIT(EFI_UNSUPPORTED); | |
801 | } | |
802 | ||
803 | /** | |
804 | * efi_cin_register_key_notify() - register key notification function | |
805 | * | |
806 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL | |
807 | * @key_data: key to be notified | |
808 | * @key_notify_function: function to be called if the key is pressed | |
809 | * @notify_handle: handle for unregistering the notification | |
810 | * Return: status code | |
811 | * | |
812 | * This function implements the SetState service of the | |
813 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. | |
814 | * | |
815 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
816 | * details. | |
817 | */ | |
818 | static efi_status_t EFIAPI efi_cin_register_key_notify( | |
819 | struct efi_simple_text_input_ex_protocol *this, | |
820 | struct efi_key_data *key_data, | |
821 | efi_status_t (EFIAPI *key_notify_function)( | |
822 | struct efi_key_data *key_data), | |
823 | void **notify_handle) | |
824 | { | |
4fdcf066 HS |
825 | efi_status_t ret = EFI_SUCCESS; |
826 | struct efi_cin_notify_function *notify_function; | |
827 | ||
110c6280 HS |
828 | EFI_ENTRY("%p, %p, %p, %p", |
829 | this, key_data, key_notify_function, notify_handle); | |
4fdcf066 HS |
830 | |
831 | /* Check parameters */ | |
832 | if (!this || !key_data || !key_notify_function || !notify_handle) { | |
833 | ret = EFI_INVALID_PARAMETER; | |
834 | goto out; | |
835 | } | |
836 | ||
837 | EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n", | |
838 | key_data->key.unicode_char, | |
839 | key_data->key.scan_code, | |
840 | key_data->key_state.key_shift_state, | |
841 | key_data->key_state.key_toggle_state); | |
842 | ||
843 | notify_function = calloc(1, sizeof(struct efi_cin_notify_function)); | |
844 | if (!notify_function) { | |
845 | ret = EFI_OUT_OF_RESOURCES; | |
846 | goto out; | |
847 | } | |
848 | notify_function->key = *key_data; | |
849 | notify_function->function = key_notify_function; | |
850 | list_add_tail(¬ify_function->link, &cin_notify_functions); | |
851 | *notify_handle = notify_function; | |
852 | out: | |
853 | return EFI_EXIT(ret); | |
110c6280 HS |
854 | } |
855 | ||
856 | /** | |
857 | * efi_cin_unregister_key_notify() - unregister key notification function | |
858 | * | |
859 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL | |
860 | * @notification_handle: handle received when registering | |
861 | * Return: status code | |
862 | * | |
863 | * This function implements the SetState service of the | |
864 | * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL. | |
865 | * | |
866 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
867 | * details. | |
868 | */ | |
869 | static efi_status_t EFIAPI efi_cin_unregister_key_notify( | |
870 | struct efi_simple_text_input_ex_protocol *this, | |
871 | void *notification_handle) | |
872 | { | |
4fdcf066 HS |
873 | efi_status_t ret = EFI_INVALID_PARAMETER; |
874 | struct efi_cin_notify_function *item, *notify_function = | |
875 | notification_handle; | |
876 | ||
110c6280 | 877 | EFI_ENTRY("%p, %p", this, notification_handle); |
4fdcf066 HS |
878 | |
879 | /* Check parameters */ | |
880 | if (!this || !notification_handle) | |
881 | goto out; | |
882 | ||
883 | list_for_each_entry(item, &cin_notify_functions, link) { | |
884 | if (item == notify_function) { | |
885 | ret = EFI_SUCCESS; | |
886 | break; | |
887 | } | |
888 | } | |
889 | if (ret != EFI_SUCCESS) | |
890 | goto out; | |
891 | ||
892 | /* Remove the notify function */ | |
893 | list_del(¬ify_function->link); | |
894 | free(notify_function); | |
895 | out: | |
896 | return EFI_EXIT(ret); | |
110c6280 HS |
897 | } |
898 | ||
899 | ||
0dfd13a4 HS |
900 | /** |
901 | * efi_cin_reset() - drain the input buffer | |
902 | * | |
903 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL | |
904 | * @extended_verification: allow for exhaustive verification | |
905 | * Return: status code | |
906 | * | |
907 | * This function implements the Reset service of the | |
908 | * EFI_SIMPLE_TEXT_INPUT_PROTOCOL. | |
909 | * | |
910 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
911 | * details. | |
912 | */ | |
913 | static efi_status_t EFIAPI efi_cin_reset | |
914 | (struct efi_simple_text_input_protocol *this, | |
915 | bool extended_verification) | |
916 | { | |
917 | efi_status_t ret = EFI_SUCCESS; | |
918 | ||
919 | EFI_ENTRY("%p, %d", this, extended_verification); | |
920 | ||
921 | /* Check parameters */ | |
922 | if (!this) { | |
923 | ret = EFI_INVALID_PARAMETER; | |
924 | goto out; | |
925 | } | |
926 | ||
110c6280 | 927 | efi_cin_empty_buffer(); |
0dfd13a4 HS |
928 | out: |
929 | return EFI_EXIT(ret); | |
930 | } | |
931 | ||
932 | /** | |
110c6280 | 933 | * efi_cin_read_key_stroke() - read key stroke |
0dfd13a4 HS |
934 | * |
935 | * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL | |
936 | * @key: key read from console | |
937 | * Return: status code | |
938 | * | |
939 | * This function implements the ReadKeyStroke service of the | |
940 | * EFI_SIMPLE_TEXT_INPUT_PROTOCOL. | |
941 | * | |
942 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
943 | * details. | |
944 | */ | |
945 | static efi_status_t EFIAPI efi_cin_read_key_stroke | |
946 | (struct efi_simple_text_input_protocol *this, | |
947 | struct efi_input_key *key) | |
948 | { | |
949 | efi_status_t ret = EFI_SUCCESS; | |
950 | ||
951 | EFI_ENTRY("%p, %p", this, key); | |
952 | ||
953 | /* Check parameters */ | |
954 | if (!this || !key) { | |
955 | ret = EFI_INVALID_PARAMETER; | |
956 | goto out; | |
957 | } | |
958 | ||
959 | /* We don't do interrupts, so check for timers cooperatively */ | |
960 | efi_timer_check(); | |
961 | ||
962 | /* Enable console input after ExitBootServices */ | |
963 | efi_cin_check(); | |
964 | ||
965 | if (!key_available) { | |
966 | ret = EFI_NOT_READY; | |
967 | goto out; | |
968 | } | |
110c6280 | 969 | *key = next_key.key; |
0dfd13a4 HS |
970 | key_available = false; |
971 | efi_con_in.wait_for_key->is_signaled = false; | |
972 | out: | |
973 | return EFI_EXIT(ret); | |
c1311ad4 AG |
974 | } |
975 | ||
110c6280 HS |
976 | static struct efi_simple_text_input_ex_protocol efi_con_in_ex = { |
977 | .reset = efi_cin_reset_ex, | |
978 | .read_key_stroke_ex = efi_cin_read_key_stroke_ex, | |
979 | .wait_for_key_ex = NULL, | |
980 | .set_state = efi_cin_set_state, | |
981 | .register_key_notify = efi_cin_register_key_notify, | |
982 | .unregister_key_notify = efi_cin_unregister_key_notify, | |
983 | }; | |
984 | ||
3e603ec7 | 985 | struct efi_simple_text_input_protocol efi_con_in = { |
c1311ad4 AG |
986 | .reset = efi_cin_reset, |
987 | .read_key_stroke = efi_cin_read_key_stroke, | |
988 | .wait_for_key = NULL, | |
989 | }; | |
91be9a77 HS |
990 | |
991 | static struct efi_event *console_timer_event; | |
992 | ||
9bc9664d | 993 | /* |
0dfd13a4 | 994 | * efi_console_timer_notify() - notify the console timer event |
9bc9664d | 995 | * |
0dfd13a4 HS |
996 | * @event: console timer event |
997 | * @context: not used | |
9bc9664d | 998 | */ |
ff925938 HS |
999 | static void EFIAPI efi_console_timer_notify(struct efi_event *event, |
1000 | void *context) | |
91be9a77 HS |
1001 | { |
1002 | EFI_ENTRY("%p, %p", event, context); | |
0dfd13a4 HS |
1003 | efi_cin_check(); |
1004 | EFI_EXIT(EFI_SUCCESS); | |
1005 | } | |
9bc9664d | 1006 | |
0dfd13a4 HS |
1007 | /** |
1008 | * efi_key_notify() - notify the wait for key event | |
1009 | * | |
1010 | * @event: wait for key event | |
1011 | * @context: not used | |
1012 | */ | |
1013 | static void EFIAPI efi_key_notify(struct efi_event *event, void *context) | |
1014 | { | |
1015 | EFI_ENTRY("%p, %p", event, context); | |
1016 | efi_cin_check(); | |
91be9a77 HS |
1017 | EFI_EXIT(EFI_SUCCESS); |
1018 | } | |
1019 | ||
0dfd13a4 HS |
1020 | /** |
1021 | * efi_console_register() - install the console protocols | |
1022 | * | |
1023 | * This function is called from do_bootefi_exec(). | |
1024 | */ | |
91be9a77 HS |
1025 | int efi_console_register(void) |
1026 | { | |
1027 | efi_status_t r; | |
ebb4dd5b HS |
1028 | struct efi_object *efi_console_output_obj; |
1029 | struct efi_object *efi_console_input_obj; | |
a17e62cc | 1030 | |
a4aa7bef HS |
1031 | /* Set up mode information */ |
1032 | query_console_size(); | |
1033 | ||
ebb4dd5b | 1034 | /* Create handles */ |
2074f700 | 1035 | r = efi_create_handle((efi_handle_t *)&efi_console_output_obj); |
ebb4dd5b HS |
1036 | if (r != EFI_SUCCESS) |
1037 | goto out_of_memory; | |
40e3e757 | 1038 | |
ebb4dd5b HS |
1039 | r = efi_add_protocol(efi_console_output_obj->handle, |
1040 | &efi_guid_text_output_protocol, &efi_con_out); | |
1041 | if (r != EFI_SUCCESS) | |
1042 | goto out_of_memory; | |
40e3e757 AG |
1043 | systab.con_out_handle = efi_console_output_obj->handle; |
1044 | systab.stderr_handle = efi_console_output_obj->handle; | |
1045 | ||
2074f700 | 1046 | r = efi_create_handle((efi_handle_t *)&efi_console_input_obj); |
ebb4dd5b HS |
1047 | if (r != EFI_SUCCESS) |
1048 | goto out_of_memory; | |
40e3e757 | 1049 | |
ebb4dd5b HS |
1050 | r = efi_add_protocol(efi_console_input_obj->handle, |
1051 | &efi_guid_text_input_protocol, &efi_con_in); | |
1052 | if (r != EFI_SUCCESS) | |
1053 | goto out_of_memory; | |
40e3e757 | 1054 | systab.con_in_handle = efi_console_input_obj->handle; |
110c6280 HS |
1055 | r = efi_add_protocol(efi_console_input_obj->handle, |
1056 | &efi_guid_text_input_ex_protocol, &efi_con_in_ex); | |
1057 | if (r != EFI_SUCCESS) | |
1058 | goto out_of_memory; | |
a17e62cc | 1059 | |
ebb4dd5b | 1060 | /* Create console events */ |
b095f3c8 HS |
1061 | r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify, |
1062 | NULL, NULL, &efi_con_in.wait_for_key); | |
91be9a77 HS |
1063 | if (r != EFI_SUCCESS) { |
1064 | printf("ERROR: Failed to register WaitForKey event\n"); | |
1065 | return r; | |
1066 | } | |
110c6280 | 1067 | efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key; |
91be9a77 | 1068 | r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK, |
b095f3c8 | 1069 | efi_console_timer_notify, NULL, NULL, |
91be9a77 HS |
1070 | &console_timer_event); |
1071 | if (r != EFI_SUCCESS) { | |
1072 | printf("ERROR: Failed to register console event\n"); | |
1073 | return r; | |
1074 | } | |
1075 | /* 5000 ns cycle is sufficient for 2 MBaud */ | |
1076 | r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50); | |
1077 | if (r != EFI_SUCCESS) | |
1078 | printf("ERROR: Failed to set console timer\n"); | |
1079 | return r; | |
ebb4dd5b | 1080 | out_of_memory: |
14d103bb | 1081 | printf("ERROR: Out of memory\n"); |
ebb4dd5b | 1082 | return r; |
91be9a77 | 1083 | } |