]> Git Repo - u-boot.git/blob - lib/efi_loader/efi_console.c
efi_loader: define internal implementations of install/uninstallmultiple
[u-boot.git] / lib / efi_loader / efi_console.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI application console interface
4  *
5  *  Copyright (c) 2016 Alexander Graf
6  */
7
8 #define LOG_CATEGORY LOGC_EFI
9
10 #include <ansi.h>
11 #include <common.h>
12 #include <charset.h>
13 #include <malloc.h>
14 #include <time.h>
15 #include <dm/device.h>
16 #include <efi_loader.h>
17 #include <env.h>
18 #include <log.h>
19 #include <stdio_dev.h>
20 #include <video_console.h>
21 #include <linux/delay.h>
22
23 #define EFI_COUT_MODE_2 2
24 #define EFI_MAX_COUT_MODE 3
25
26 struct cout_mode {
27         unsigned long columns;
28         unsigned long rows;
29         int present;
30 };
31
32 __maybe_unused static struct efi_object uart_obj;
33
34 static struct cout_mode efi_cout_modes[] = {
35         /* EFI Mode 0 is 80x25 and always present */
36         {
37                 .columns = 80,
38                 .rows = 25,
39                 .present = 1,
40         },
41         /* EFI Mode 1 is always 80x50 */
42         {
43                 .columns = 80,
44                 .rows = 50,
45                 .present = 0,
46         },
47         /* Value are unknown until we query the console */
48         {
49                 .columns = 0,
50                 .rows = 0,
51                 .present = 0,
52         },
53 };
54
55 const efi_guid_t efi_guid_text_input_ex_protocol =
56                         EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
57 const efi_guid_t efi_guid_text_input_protocol =
58                         EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
59 const efi_guid_t efi_guid_text_output_protocol =
60                         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
61
62 #define cESC '\x1b'
63 #define ESC "\x1b"
64
65 /*
66  * efi_con_mode - mode information of the Simple Text Output Protocol
67  *
68  * Use safe settings before efi_setup_console_size() is called.
69  * By default enable only the 80x25 mode which must always exist.
70  */
71 static struct simple_text_output_mode efi_con_mode = {
72         .max_mode = 1,
73         .mode = 0,
74         .attribute = 0,
75         .cursor_column = 0,
76         .cursor_row = 0,
77         .cursor_visible = 1,
78 };
79
80 static int term_get_char(s32 *c)
81 {
82         u64 timeout;
83
84         /* Wait up to 100 ms for a character */
85         timeout = timer_get_us() + 100000;
86
87         while (!tstc())
88                 if (timer_get_us() > timeout)
89                         return 1;
90
91         *c = getchar();
92         return 0;
93 }
94
95 /**
96  * Receive and parse a reply from the terminal.
97  *
98  * @n:          array of return values
99  * @num:        number of return values expected
100  * @end_char:   character indicating end of terminal message
101  * Return:      non-zero indicates error
102  */
103 static int term_read_reply(int *n, int num, char end_char)
104 {
105         s32 c;
106         int i = 0;
107
108         if (term_get_char(&c) || c != cESC)
109                 return -1;
110
111         if (term_get_char(&c) || c != '[')
112                 return -1;
113
114         n[0] = 0;
115         while (1) {
116                 if (!term_get_char(&c)) {
117                         if (c == ';') {
118                                 i++;
119                                 if (i >= num)
120                                         return -1;
121                                 n[i] = 0;
122                                 continue;
123                         } else if (c == end_char) {
124                                 break;
125                         } else if (c > '9' || c < '0') {
126                                 return -1;
127                         }
128
129                         /* Read one more decimal position */
130                         n[i] *= 10;
131                         n[i] += c - '0';
132                 } else {
133                         return -1;
134                 }
135         }
136         if (i != num - 1)
137                 return -1;
138
139         return 0;
140 }
141
142 /**
143  * efi_cout_output_string() - write Unicode string to console
144  *
145  * This function implements the OutputString service of the simple text output
146  * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
147  * for details.
148  *
149  * @this:       simple text output protocol
150  * @string:     u16 string
151  * Return:      status code
152  */
153 static efi_status_t EFIAPI efi_cout_output_string(
154                         struct efi_simple_text_output_protocol *this,
155                         const u16 *string)
156 {
157         struct simple_text_output_mode *con = &efi_con_mode;
158         struct cout_mode *mode = &efi_cout_modes[con->mode];
159         char *buf, *pos;
160         const u16 *p;
161         efi_status_t ret = EFI_SUCCESS;
162
163         EFI_ENTRY("%p, %p", this, string);
164
165         if (!this || !string) {
166                 ret = EFI_INVALID_PARAMETER;
167                 goto out;
168         }
169
170         buf = malloc(utf16_utf8_strlen(string) + 1);
171         if (!buf) {
172                 ret = EFI_OUT_OF_RESOURCES;
173                 goto out;
174         }
175         pos = buf;
176         utf16_utf8_strcpy(&pos, string);
177         fputs(stdout, buf);
178         free(buf);
179
180         /*
181          * Update the cursor position.
182          *
183          * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
184          * and U000D. All other control characters are ignored. Any non-control
185          * character increase the column by one.
186          */
187         for (p = string; *p; ++p) {
188                 switch (*p) {
189                 case '\b':      /* U+0008, backspace */
190                         if (con->cursor_column)
191                                 con->cursor_column--;
192                         break;
193                 case '\n':      /* U+000A, newline */
194                         con->cursor_column = 0;
195                         con->cursor_row++;
196                         break;
197                 case '\r':      /* U+000D, carriage-return */
198                         con->cursor_column = 0;
199                         break;
200                 case 0xd800 ... 0xdbff:
201                         /*
202                          * Ignore high surrogates, we do not want to count a
203                          * Unicode character twice.
204                          */
205                         break;
206                 default:
207                         /* Exclude control codes */
208                         if (*p > 0x1f)
209                                 con->cursor_column++;
210                         break;
211                 }
212                 if (con->cursor_column >= mode->columns) {
213                         con->cursor_column = 0;
214                         con->cursor_row++;
215                 }
216                 /*
217                  * When we exceed the row count the terminal will scroll up one
218                  * line. We have to adjust the cursor position.
219                  */
220                 if (con->cursor_row >= mode->rows && con->cursor_row)
221                         con->cursor_row--;
222         }
223
224 out:
225         return EFI_EXIT(ret);
226 }
227
228 /**
229  * efi_cout_test_string() - test writing Unicode string to console
230  *
231  * This function implements the TestString service of the simple text output
232  * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
233  * for details.
234  *
235  * As in OutputString we simply convert UTF-16 to UTF-8 there are no unsupported
236  * code points and we can always return EFI_SUCCESS.
237  *
238  * @this:       simple text output protocol
239  * @string:     u16 string
240  * Return:      status code
241  */
242 static efi_status_t EFIAPI efi_cout_test_string(
243                         struct efi_simple_text_output_protocol *this,
244                         const u16 *string)
245 {
246         EFI_ENTRY("%p, %p", this, string);
247         return EFI_EXIT(EFI_SUCCESS);
248 }
249
250 /**
251  * cout_mode_matches() - check if mode has given terminal size
252  *
253  * @mode:       text mode
254  * @rows:       number of rows
255  * @cols:       number of columns
256  * Return:      true if number of rows and columns matches the mode and
257  *              the mode is present
258  */
259 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
260 {
261         if (!mode->present)
262                 return false;
263
264         return (mode->rows == rows) && (mode->columns == cols);
265 }
266
267 /**
268  * query_console_serial() - query serial console size
269  *
270  * When using a serial console or the net console we can only devise the
271  * terminal size by querying the terminal using ECMA-48 control sequences.
272  *
273  * @rows:       pointer to return number of rows
274  * @cols:       pointer to return number of columns
275  * Returns:     0 on success
276  */
277 static int query_console_serial(int *rows, int *cols)
278 {
279         int ret = 0;
280         int n[2];
281
282         /* Empty input buffer */
283         while (tstc())
284                 getchar();
285
286         /*
287          * Not all terminals understand CSI [18t for querying the console size.
288          * We should adhere to escape sequences documented in the console_codes
289          * man page and the ECMA-48 standard.
290          *
291          * So here we follow a different approach. We position the cursor to the
292          * bottom right and query its position. Before leaving the function we
293          * restore the original cursor position.
294          */
295         printf(ESC "7"          /* Save cursor position */
296                ESC "[r"         /* Set scrolling region to full window */
297                ESC "[999;999H"  /* Move to bottom right corner */
298                ESC "[6n");      /* Query cursor position */
299
300         /* Read {rows,cols} */
301         if (term_read_reply(n, 2, 'R')) {
302                 ret = 1;
303                 goto out;
304         }
305
306         *cols = n[1];
307         *rows = n[0];
308 out:
309         printf(ESC "8");        /* Restore cursor position */
310         return ret;
311 }
312
313 /**
314  * query_vidconsole() - query video console size
315  *
316  *
317  * @rows:       pointer to return number of rows
318  * @cols:       pointer to return number of columns
319  * Returns:     0 on success
320  */
321 static int __maybe_unused query_vidconsole(int *rows, int *cols)
322 {
323         const char *stdout_name = env_get("stdout");
324         struct stdio_dev *stdout_dev;
325         struct udevice *dev;
326         struct vidconsole_priv *priv;
327
328         if (!stdout_name || strncmp(stdout_name, "vidconsole", 10))
329                 return -ENODEV;
330         stdout_dev = stdio_get_by_name("vidconsole");
331         if (!stdout_dev)
332                 return -ENODEV;
333         dev = stdout_dev->priv;
334         if (!dev)
335                 return -ENODEV;
336         priv = dev_get_uclass_priv(dev);
337         if (!priv)
338                 return -ENODEV;
339         *rows = priv->rows;
340         *cols = priv->cols;
341         return 0;
342 }
343
344 /**
345  * efi_setup_console_size() - update the mode table.
346  *
347  * By default the only mode available is 80x25. If the console has at least 50
348  * lines, enable mode 80x50. If we can query the console size and it is neither
349  * 80x25 nor 80x50, set it as an additional mode.
350  */
351 void efi_setup_console_size(void)
352 {
353         int rows = 25, cols = 80;
354         int ret = -ENODEV;
355
356         if (IS_ENABLED(CONFIG_DM_VIDEO))
357                 ret = query_vidconsole(&rows, &cols);
358         if (ret)
359                 ret = query_console_serial(&rows, &cols);
360         if (ret)
361                 return;
362
363         log_debug("Console size %dx%d\n", rows, cols);
364
365         /* Test if we can have Mode 1 */
366         if (cols >= 80 && rows >= 50) {
367                 efi_cout_modes[1].present = 1;
368                 efi_con_mode.max_mode = 2;
369         }
370
371         /*
372          * Install our mode as mode 2 if it is different
373          * than mode 0 or 1 and set it as the currently selected mode
374          */
375         if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
376             !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
377                 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
378                 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
379                 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
380                 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
381                 efi_con_mode.mode = EFI_COUT_MODE_2;
382         }
383 }
384
385 /**
386  * efi_cout_query_mode() - get terminal size for a text mode
387  *
388  * This function implements the QueryMode service of the simple text output
389  * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
390  * for details.
391  *
392  * @this:               simple text output protocol
393  * @mode_number:        mode number to retrieve information on
394  * @columns:            number of columns
395  * @rows:               number of rows
396  * Return:              status code
397  */
398 static efi_status_t EFIAPI efi_cout_query_mode(
399                         struct efi_simple_text_output_protocol *this,
400                         unsigned long mode_number, unsigned long *columns,
401                         unsigned long *rows)
402 {
403         EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
404
405         if (mode_number >= efi_con_mode.max_mode)
406                 return EFI_EXIT(EFI_UNSUPPORTED);
407
408         if (efi_cout_modes[mode_number].present != 1)
409                 return EFI_EXIT(EFI_UNSUPPORTED);
410
411         if (columns)
412                 *columns = efi_cout_modes[mode_number].columns;
413         if (rows)
414                 *rows = efi_cout_modes[mode_number].rows;
415
416         return EFI_EXIT(EFI_SUCCESS);
417 }
418
419 static const struct {
420         unsigned int fg;
421         unsigned int bg;
422 } color[] = {
423         { 30, 40 },     /* 0: black */
424         { 34, 44 },     /* 1: blue */
425         { 32, 42 },     /* 2: green */
426         { 36, 46 },     /* 3: cyan */
427         { 31, 41 },     /* 4: red */
428         { 35, 45 },     /* 5: magenta */
429         { 33, 43 },     /* 6: brown, map to yellow as EDK2 does*/
430         { 37, 47 },     /* 7: light gray, map to white */
431 };
432
433 /**
434  * efi_cout_set_attribute() - set fore- and background color
435  *
436  * This function implements the SetAttribute service of the simple text output
437  * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
438  * for details.
439  *
440  * @this:       simple text output protocol
441  * @attribute:  foreground color - bits 0-3, background color - bits 4-6
442  * Return:      status code
443  */
444 static efi_status_t EFIAPI efi_cout_set_attribute(
445                         struct efi_simple_text_output_protocol *this,
446                         unsigned long attribute)
447 {
448         unsigned int bold = EFI_ATTR_BOLD(attribute);
449         unsigned int fg = EFI_ATTR_FG(attribute);
450         unsigned int bg = EFI_ATTR_BG(attribute);
451
452         EFI_ENTRY("%p, %lx", this, attribute);
453
454         efi_con_mode.attribute = attribute;
455         if (attribute)
456                 printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
457         else
458                 printf(ESC"[0;37;40m");
459
460         return EFI_EXIT(EFI_SUCCESS);
461 }
462
463 /**
464  * efi_cout_clear_screen() - clear screen
465  *
466  * This function implements the ClearScreen service of the simple text output
467  * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
468  * for details.
469  *
470  * @this:       pointer to the protocol instance
471  * Return:      status code
472  */
473 static efi_status_t EFIAPI efi_cout_clear_screen(
474                         struct efi_simple_text_output_protocol *this)
475 {
476         EFI_ENTRY("%p", this);
477
478         /*
479          * The Linux console wants both a clear and a home command. The video
480          * uclass does not support <ESC>[H without coordinates, yet.
481          */
482         printf(ESC "[2J" ESC "[1;1H");
483         efi_con_mode.cursor_column = 0;
484         efi_con_mode.cursor_row = 0;
485
486         return EFI_EXIT(EFI_SUCCESS);
487 }
488
489 /**
490  * efi_cout_clear_set_mode() - set text model
491  *
492  * This function implements the SetMode service of the simple text output
493  * protocol. See the Unified Extensible Firmware  Interface (UEFI) specification
494  * for details.
495  *
496  * @this:               pointer to the protocol instance
497  * @mode_number:        number of the text mode to set
498  * Return:              status code
499  */
500 static efi_status_t EFIAPI efi_cout_set_mode(
501                         struct efi_simple_text_output_protocol *this,
502                         unsigned long mode_number)
503 {
504         EFI_ENTRY("%p, %ld", this, mode_number);
505
506         if (mode_number >= efi_con_mode.max_mode)
507                 return EFI_EXIT(EFI_UNSUPPORTED);
508
509         if (!efi_cout_modes[mode_number].present)
510                 return EFI_EXIT(EFI_UNSUPPORTED);
511
512         efi_con_mode.mode = mode_number;
513         EFI_CALL(efi_cout_clear_screen(this));
514
515         return EFI_EXIT(EFI_SUCCESS);
516 }
517
518 /**
519  * efi_cout_reset() - reset the terminal
520  *
521  * This function implements the Reset service of the simple text output
522  * protocol. See the Unified Extensible Firmware  Interface (UEFI) specification
523  * for details.
524  *
525  * @this:                       pointer to the protocol instance
526  * @extended_verification:      if set an extended verification may be executed
527  * Return:                      status code
528  */
529 static efi_status_t EFIAPI efi_cout_reset(
530                         struct efi_simple_text_output_protocol *this,
531                         char extended_verification)
532 {
533         EFI_ENTRY("%p, %d", this, extended_verification);
534
535         /* Set default colors */
536         efi_con_mode.attribute = 0x07;
537         printf(ESC "[0;37;40m");
538         /* Clear screen */
539         EFI_CALL(efi_cout_clear_screen(this));
540
541         return EFI_EXIT(EFI_SUCCESS);
542 }
543
544 /**
545  * efi_cout_set_cursor_position() - reset the terminal
546  *
547  * This function implements the SetCursorPosition service of the simple text
548  * output protocol. See the Unified Extensible Firmware  Interface (UEFI)
549  * specification for details.
550  *
551  * @this:       pointer to the protocol instance
552  * @column:     column to move to
553  * @row:        row to move to
554  * Return:      status code
555  */
556 static efi_status_t EFIAPI efi_cout_set_cursor_position(
557                         struct efi_simple_text_output_protocol *this,
558                         unsigned long column, unsigned long row)
559 {
560         efi_status_t ret = EFI_SUCCESS;
561         struct simple_text_output_mode *con = &efi_con_mode;
562         struct cout_mode *mode = &efi_cout_modes[con->mode];
563
564         EFI_ENTRY("%p, %ld, %ld", this, column, row);
565
566         /* Check parameters */
567         if (!this) {
568                 ret = EFI_INVALID_PARAMETER;
569                 goto out;
570         }
571         if (row >= mode->rows || column >= mode->columns) {
572                 ret = EFI_UNSUPPORTED;
573                 goto out;
574         }
575
576         /*
577          * Set cursor position by sending CSI H.
578          * EFI origin is [0, 0], terminal origin is [1, 1].
579          */
580         printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
581         efi_con_mode.cursor_column = column;
582         efi_con_mode.cursor_row = row;
583 out:
584         return EFI_EXIT(ret);
585 }
586
587 /**
588  * efi_cout_enable_cursor() - enable the cursor
589  *
590  * This function implements the EnableCursor service of the simple text  output
591  * protocol. See the Unified Extensible Firmware  Interface (UEFI) specification
592  * for details.
593  *
594  * @this:       pointer to the protocol instance
595  * @enable:     if true enable, if false disable the cursor
596  * Return:      status code
597  */
598 static efi_status_t EFIAPI efi_cout_enable_cursor(
599                         struct efi_simple_text_output_protocol *this,
600                         bool enable)
601 {
602         EFI_ENTRY("%p, %d", this, enable);
603
604         printf(ESC"[?25%c", enable ? 'h' : 'l');
605         efi_con_mode.cursor_visible = !!enable;
606
607         return EFI_EXIT(EFI_SUCCESS);
608 }
609
610 struct efi_simple_text_output_protocol efi_con_out = {
611         .reset = efi_cout_reset,
612         .output_string = efi_cout_output_string,
613         .test_string = efi_cout_test_string,
614         .query_mode = efi_cout_query_mode,
615         .set_mode = efi_cout_set_mode,
616         .set_attribute = efi_cout_set_attribute,
617         .clear_screen = efi_cout_clear_screen,
618         .set_cursor_position = efi_cout_set_cursor_position,
619         .enable_cursor = efi_cout_enable_cursor,
620         .mode = (void*)&efi_con_mode,
621 };
622
623 /**
624  * struct efi_cin_notify_function - registered console input notify function
625  *
626  * @link:       link to list
627  * @key:        key to notify
628  * @function:   function to call
629  */
630 struct efi_cin_notify_function {
631         struct list_head link;
632         struct efi_key_data key;
633         efi_status_t (EFIAPI *function)
634                 (struct efi_key_data *key_data);
635 };
636
637 static bool key_available;
638 static struct efi_key_data next_key;
639 static LIST_HEAD(cin_notify_functions);
640
641 /**
642  * set_shift_mask() - set shift mask
643  *
644  * @mod:        Xterm shift mask
645  * @key_state:  receives the state of the shift, alt, control, and logo keys
646  */
647 void set_shift_mask(int mod, struct efi_key_state *key_state)
648 {
649         key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
650         if (mod) {
651                 --mod;
652                 if (mod & 1)
653                         key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
654                 if (mod & 2)
655                         key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
656                 if (mod & 4)
657                         key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
658                 if (!mod || (mod & 8))
659                         key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
660         }
661 }
662
663 /**
664  * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
665  *
666  * This gets called when we have already parsed CSI.
667  *
668  * @key_state:  receives the state of the shift, alt, control, and logo keys
669  * Return:      the unmodified code
670  */
671 static int analyze_modifiers(struct efi_key_state *key_state)
672 {
673         int c, mod = 0, ret = 0;
674
675         c = getchar();
676
677         if (c != ';') {
678                 ret = c;
679                 if (c == '~')
680                         goto out;
681                 c = getchar();
682         }
683         for (;;) {
684                 switch (c) {
685                 case '0'...'9':
686                         mod *= 10;
687                         mod += c - '0';
688                 /* fall through */
689                 case ';':
690                         c = getchar();
691                         break;
692                 default:
693                         goto out;
694                 }
695         }
696 out:
697         set_shift_mask(mod, key_state);
698         if (!ret)
699                 ret = c;
700         return ret;
701 }
702
703 /**
704  * efi_cin_read_key() - read a key from the console input
705  *
706  * @key:        - key received
707  * Return:      - status code
708  */
709 static efi_status_t efi_cin_read_key(struct efi_key_data *key)
710 {
711         struct efi_input_key pressed_key = {
712                 .scan_code = 0,
713                 .unicode_char = 0,
714         };
715         s32 ch;
716
717         if (console_read_unicode(&ch))
718                 return EFI_NOT_READY;
719
720         key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
721         key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
722
723         /* We do not support multi-word codes */
724         if (ch >= 0x10000)
725                 ch = '?';
726
727         switch (ch) {
728         case 0x1b:
729                 /*
730                  * If a second key is received within 10 ms, assume that we are
731                  * dealing with an escape sequence. Otherwise consider this the
732                  * escape key being hit. 10 ms is long enough to work fine at
733                  * 1200 baud and above.
734                  */
735                 udelay(10000);
736                 if (!tstc()) {
737                         pressed_key.scan_code = 23;
738                         break;
739                 }
740                 /*
741                  * Xterm Control Sequences
742                  * https://www.xfree86.org/4.8.0/ctlseqs.html
743                  */
744                 ch = getchar();
745                 switch (ch) {
746                 case cESC: /* ESC */
747                         pressed_key.scan_code = 23;
748                         break;
749                 case 'O': /* F1 - F4, End */
750                         ch = getchar();
751                         /* consider modifiers */
752                         if (ch == 'F') { /* End */
753                                 pressed_key.scan_code = 6;
754                                 break;
755                         } else if (ch < 'P') {
756                                 set_shift_mask(ch - '0', &key->key_state);
757                                 ch = getchar();
758                         }
759                         pressed_key.scan_code = ch - 'P' + 11;
760                         break;
761                 case '[':
762                         ch = getchar();
763                         switch (ch) {
764                         case 'A'...'D': /* up, down right, left */
765                                 pressed_key.scan_code = ch - 'A' + 1;
766                                 break;
767                         case 'F': /* End */
768                                 pressed_key.scan_code = 6;
769                                 break;
770                         case 'H': /* Home */
771                                 pressed_key.scan_code = 5;
772                                 break;
773                         case '1':
774                                 ch = analyze_modifiers(&key->key_state);
775                                 switch (ch) {
776                                 case '1'...'5': /* F1 - F5 */
777                                         pressed_key.scan_code = ch - '1' + 11;
778                                         break;
779                                 case '6'...'9': /* F5 - F8 */
780                                         pressed_key.scan_code = ch - '6' + 15;
781                                         break;
782                                 case 'A'...'D': /* up, down right, left */
783                                         pressed_key.scan_code = ch - 'A' + 1;
784                                         break;
785                                 case 'F': /* End */
786                                         pressed_key.scan_code = 6;
787                                         break;
788                                 case 'H': /* Home */
789                                         pressed_key.scan_code = 5;
790                                         break;
791                                 case '~': /* Home */
792                                         pressed_key.scan_code = 5;
793                                         break;
794                                 }
795                                 break;
796                         case '2':
797                                 ch = analyze_modifiers(&key->key_state);
798                                 switch (ch) {
799                                 case '0'...'1': /* F9 - F10 */
800                                         pressed_key.scan_code = ch - '0' + 19;
801                                         break;
802                                 case '3'...'4': /* F11 - F12 */
803                                         pressed_key.scan_code = ch - '3' + 21;
804                                         break;
805                                 case '~': /* INS */
806                                         pressed_key.scan_code = 7;
807                                         break;
808                                 }
809                                 break;
810                         case '3': /* DEL */
811                                 pressed_key.scan_code = 8;
812                                 analyze_modifiers(&key->key_state);
813                                 break;
814                         case '5': /* PG UP */
815                                 pressed_key.scan_code = 9;
816                                 analyze_modifiers(&key->key_state);
817                                 break;
818                         case '6': /* PG DOWN */
819                                 pressed_key.scan_code = 10;
820                                 analyze_modifiers(&key->key_state);
821                                 break;
822                         } /* [ */
823                         break;
824                 default:
825                         /* ALT key */
826                         set_shift_mask(3, &key->key_state);
827                 }
828                 break;
829         case 0x7f:
830                 /* Backspace */
831                 ch = 0x08;
832         }
833         if (pressed_key.scan_code) {
834                 key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
835         } else {
836                 pressed_key.unicode_char = ch;
837
838                 /*
839                  * Assume left control key for control characters typically
840                  * entered using the control key.
841                  */
842                 if (ch >= 0x01 && ch <= 0x1f) {
843                         key->key_state.key_shift_state |=
844                                         EFI_SHIFT_STATE_VALID;
845                         switch (ch) {
846                         case 0x01 ... 0x07:
847                         case 0x0b ... 0x0c:
848                         case 0x0e ... 0x1f:
849                                 key->key_state.key_shift_state |=
850                                                 EFI_LEFT_CONTROL_PRESSED;
851                         }
852                 }
853         }
854         key->key = pressed_key;
855
856         return EFI_SUCCESS;
857 }
858
859 /**
860  * efi_cin_notify() - notify registered functions
861  */
862 static void efi_cin_notify(void)
863 {
864         struct efi_cin_notify_function *item;
865
866         list_for_each_entry(item, &cin_notify_functions, link) {
867                 bool match = true;
868
869                 /* We do not support toggle states */
870                 if (item->key.key.unicode_char || item->key.key.scan_code) {
871                         if (item->key.key.unicode_char !=
872                             next_key.key.unicode_char ||
873                             item->key.key.scan_code != next_key.key.scan_code)
874                                 match = false;
875                 }
876                 if (item->key.key_state.key_shift_state &&
877                     item->key.key_state.key_shift_state !=
878                     next_key.key_state.key_shift_state)
879                         match = false;
880
881                 if (match)
882                         /* We don't bother about the return code */
883                         EFI_CALL(item->function(&next_key));
884         }
885 }
886
887 /**
888  * efi_cin_check() - check if keyboard input is available
889  */
890 static void efi_cin_check(void)
891 {
892         efi_status_t ret;
893
894         if (key_available) {
895                 efi_signal_event(efi_con_in.wait_for_key);
896                 return;
897         }
898
899         if (tstc()) {
900                 ret = efi_cin_read_key(&next_key);
901                 if (ret == EFI_SUCCESS) {
902                         key_available = true;
903
904                         /* Notify registered functions */
905                         efi_cin_notify();
906
907                         /* Queue the wait for key event */
908                         if (key_available)
909                                 efi_signal_event(efi_con_in.wait_for_key);
910                 }
911         }
912 }
913
914 /**
915  * efi_cin_empty_buffer() - empty input buffer
916  */
917 static void efi_cin_empty_buffer(void)
918 {
919         while (tstc())
920                 getchar();
921         key_available = false;
922 }
923
924 /**
925  * efi_cin_reset_ex() - reset console input
926  *
927  * @this:                       - the extended simple text input protocol
928  * @extended_verification:      - extended verification
929  *
930  * This function implements the reset service of the
931  * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
932  *
933  * See the Unified Extensible Firmware Interface (UEFI) specification for
934  * details.
935  *
936  * Return: old value of the task priority level
937  */
938 static efi_status_t EFIAPI efi_cin_reset_ex(
939                 struct efi_simple_text_input_ex_protocol *this,
940                 bool extended_verification)
941 {
942         efi_status_t ret = EFI_SUCCESS;
943
944         EFI_ENTRY("%p, %d", this, extended_verification);
945
946         /* Check parameters */
947         if (!this) {
948                 ret = EFI_INVALID_PARAMETER;
949                 goto out;
950         }
951
952         efi_cin_empty_buffer();
953 out:
954         return EFI_EXIT(ret);
955 }
956
957 /**
958  * efi_cin_read_key_stroke_ex() - read key stroke
959  *
960  * @this:       instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
961  * @key_data:   key read from console
962  * Return:      status code
963  *
964  * This function implements the ReadKeyStrokeEx service of the
965  * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
966  *
967  * See the Unified Extensible Firmware Interface (UEFI) specification for
968  * details.
969  */
970 static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
971                 struct efi_simple_text_input_ex_protocol *this,
972                 struct efi_key_data *key_data)
973 {
974         efi_status_t ret = EFI_SUCCESS;
975
976         EFI_ENTRY("%p, %p", this, key_data);
977
978         /* Check parameters */
979         if (!this || !key_data) {
980                 ret = EFI_INVALID_PARAMETER;
981                 goto out;
982         }
983
984         /* We don't do interrupts, so check for timers cooperatively */
985         efi_timer_check();
986
987         /* Enable console input after ExitBootServices */
988         efi_cin_check();
989
990         if (!key_available) {
991                 memset(key_data, 0, sizeof(struct efi_key_data));
992                 ret = EFI_NOT_READY;
993                 goto out;
994         }
995         /*
996          * CTRL+A - CTRL+Z have to be signaled as a - z.
997          * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
998          * CTRL+\ - CTRL+_ have to be signaled as \ - _.
999          */
1000         switch (next_key.key.unicode_char) {
1001         case 0x01 ... 0x07:
1002         case 0x0b ... 0x0c:
1003         case 0x0e ... 0x1a:
1004                 if (!(next_key.key_state.key_toggle_state &
1005                       EFI_CAPS_LOCK_ACTIVE) ^
1006                     !(next_key.key_state.key_shift_state &
1007                       (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
1008                         next_key.key.unicode_char += 0x40;
1009                 else
1010                         next_key.key.unicode_char += 0x60;
1011                 break;
1012         case 0x1c ... 0x1f:
1013                         next_key.key.unicode_char += 0x40;
1014         }
1015         *key_data = next_key;
1016         key_available = false;
1017         efi_con_in.wait_for_key->is_signaled = false;
1018
1019 out:
1020         return EFI_EXIT(ret);
1021 }
1022
1023 /**
1024  * efi_cin_set_state() - set toggle key state
1025  *
1026  * @this:               instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1027  * @key_toggle_state:   pointer to key toggle state
1028  * Return:              status code
1029  *
1030  * This function implements the SetState service of the
1031  * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1032  *
1033  * See the Unified Extensible Firmware Interface (UEFI) specification for
1034  * details.
1035  */
1036 static efi_status_t EFIAPI efi_cin_set_state(
1037                 struct efi_simple_text_input_ex_protocol *this,
1038                 u8 *key_toggle_state)
1039 {
1040         EFI_ENTRY("%p, %p", this, key_toggle_state);
1041         /*
1042          * U-Boot supports multiple console input sources like serial and
1043          * net console for which a key toggle state cannot be set at all.
1044          *
1045          * According to the UEFI specification it is allowable to not implement
1046          * this service.
1047          */
1048         return EFI_EXIT(EFI_UNSUPPORTED);
1049 }
1050
1051 /**
1052  * efi_cin_register_key_notify() - register key notification function
1053  *
1054  * @this:                       instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1055  * @key_data:                   key to be notified
1056  * @key_notify_function:        function to be called if the key is pressed
1057  * @notify_handle:              handle for unregistering the notification
1058  * Return:                      status code
1059  *
1060  * This function implements the SetState service of the
1061  * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1062  *
1063  * See the Unified Extensible Firmware Interface (UEFI) specification for
1064  * details.
1065  */
1066 static efi_status_t EFIAPI efi_cin_register_key_notify(
1067                 struct efi_simple_text_input_ex_protocol *this,
1068                 struct efi_key_data *key_data,
1069                 efi_status_t (EFIAPI *key_notify_function)(
1070                         struct efi_key_data *key_data),
1071                 void **notify_handle)
1072 {
1073         efi_status_t ret = EFI_SUCCESS;
1074         struct efi_cin_notify_function *notify_function;
1075
1076         EFI_ENTRY("%p, %p, %p, %p",
1077                   this, key_data, key_notify_function, notify_handle);
1078
1079         /* Check parameters */
1080         if (!this || !key_data || !key_notify_function || !notify_handle) {
1081                 ret = EFI_INVALID_PARAMETER;
1082                 goto out;
1083         }
1084
1085         EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
1086                   key_data->key.unicode_char,
1087                key_data->key.scan_code,
1088                key_data->key_state.key_shift_state,
1089                key_data->key_state.key_toggle_state);
1090
1091         notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
1092         if (!notify_function) {
1093                 ret = EFI_OUT_OF_RESOURCES;
1094                 goto out;
1095         }
1096         notify_function->key = *key_data;
1097         notify_function->function = key_notify_function;
1098         list_add_tail(&notify_function->link, &cin_notify_functions);
1099         *notify_handle = notify_function;
1100 out:
1101         return EFI_EXIT(ret);
1102 }
1103
1104 /**
1105  * efi_cin_unregister_key_notify() - unregister key notification function
1106  *
1107  * @this:                       instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1108  * @notification_handle:        handle received when registering
1109  * Return:                      status code
1110  *
1111  * This function implements the SetState service of the
1112  * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
1113  *
1114  * See the Unified Extensible Firmware Interface (UEFI) specification for
1115  * details.
1116  */
1117 static efi_status_t EFIAPI efi_cin_unregister_key_notify(
1118                 struct efi_simple_text_input_ex_protocol *this,
1119                 void *notification_handle)
1120 {
1121         efi_status_t ret = EFI_INVALID_PARAMETER;
1122         struct efi_cin_notify_function *item, *notify_function =
1123                         notification_handle;
1124
1125         EFI_ENTRY("%p, %p", this, notification_handle);
1126
1127         /* Check parameters */
1128         if (!this || !notification_handle)
1129                 goto out;
1130
1131         list_for_each_entry(item, &cin_notify_functions, link) {
1132                 if (item == notify_function) {
1133                         ret = EFI_SUCCESS;
1134                         break;
1135                 }
1136         }
1137         if (ret != EFI_SUCCESS)
1138                 goto out;
1139
1140         /* Remove the notify function */
1141         list_del(&notify_function->link);
1142         free(notify_function);
1143 out:
1144         return EFI_EXIT(ret);
1145 }
1146
1147
1148 /**
1149  * efi_cin_reset() - drain the input buffer
1150  *
1151  * @this:                       instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1152  * @extended_verification:      allow for exhaustive verification
1153  * Return:                      status code
1154  *
1155  * This function implements the Reset service of the
1156  * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1157  *
1158  * See the Unified Extensible Firmware Interface (UEFI) specification for
1159  * details.
1160  */
1161 static efi_status_t EFIAPI efi_cin_reset
1162                         (struct efi_simple_text_input_protocol *this,
1163                          bool extended_verification)
1164 {
1165         efi_status_t ret = EFI_SUCCESS;
1166
1167         EFI_ENTRY("%p, %d", this, extended_verification);
1168
1169         /* Check parameters */
1170         if (!this) {
1171                 ret = EFI_INVALID_PARAMETER;
1172                 goto out;
1173         }
1174
1175         efi_cin_empty_buffer();
1176 out:
1177         return EFI_EXIT(ret);
1178 }
1179
1180 /**
1181  * efi_cin_read_key_stroke() - read key stroke
1182  *
1183  * @this:       instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1184  * @key:        key read from console
1185  * Return:      status code
1186  *
1187  * This function implements the ReadKeyStroke service of the
1188  * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1189  *
1190  * See the Unified Extensible Firmware Interface (UEFI) specification for
1191  * details.
1192  */
1193 static efi_status_t EFIAPI efi_cin_read_key_stroke
1194                         (struct efi_simple_text_input_protocol *this,
1195                          struct efi_input_key *key)
1196 {
1197         efi_status_t ret = EFI_SUCCESS;
1198
1199         EFI_ENTRY("%p, %p", this, key);
1200
1201         /* Check parameters */
1202         if (!this || !key) {
1203                 ret = EFI_INVALID_PARAMETER;
1204                 goto out;
1205         }
1206
1207         /* We don't do interrupts, so check for timers cooperatively */
1208         efi_timer_check();
1209
1210         /* Enable console input after ExitBootServices */
1211         efi_cin_check();
1212
1213         if (!key_available) {
1214                 ret = EFI_NOT_READY;
1215                 goto out;
1216         }
1217         *key = next_key.key;
1218         key_available = false;
1219         efi_con_in.wait_for_key->is_signaled = false;
1220 out:
1221         return EFI_EXIT(ret);
1222 }
1223
1224 static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
1225         .reset = efi_cin_reset_ex,
1226         .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
1227         .wait_for_key_ex = NULL,
1228         .set_state = efi_cin_set_state,
1229         .register_key_notify = efi_cin_register_key_notify,
1230         .unregister_key_notify = efi_cin_unregister_key_notify,
1231 };
1232
1233 struct efi_simple_text_input_protocol efi_con_in = {
1234         .reset = efi_cin_reset,
1235         .read_key_stroke = efi_cin_read_key_stroke,
1236         .wait_for_key = NULL,
1237 };
1238
1239 static struct efi_event *console_timer_event;
1240
1241 /*
1242  * efi_console_timer_notify() - notify the console timer event
1243  *
1244  * @event:      console timer event
1245  * @context:    not used
1246  */
1247 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
1248                                             void *context)
1249 {
1250         EFI_ENTRY("%p, %p", event, context);
1251         efi_cin_check();
1252         EFI_EXIT(EFI_SUCCESS);
1253 }
1254
1255 /**
1256  * efi_key_notify() - notify the wait for key event
1257  *
1258  * @event:      wait for key event
1259  * @context:    not used
1260  */
1261 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
1262 {
1263         EFI_ENTRY("%p, %p", event, context);
1264         efi_cin_check();
1265         EFI_EXIT(EFI_SUCCESS);
1266 }
1267
1268 /**
1269  * efi_console_register() - install the console protocols
1270  *
1271  * This function is called from do_bootefi_exec().
1272  *
1273  * Return:      status code
1274  */
1275 efi_status_t efi_console_register(void)
1276 {
1277         efi_status_t r;
1278         struct efi_device_path *dp;
1279
1280         /* Install protocols on root node */
1281         r = efi_install_multiple_protocol_interfaces(&efi_root,
1282                                                      &efi_guid_text_output_protocol,
1283                                                      &efi_con_out,
1284                                                      &efi_guid_text_input_protocol,
1285                                                      &efi_con_in,
1286                                                      &efi_guid_text_input_ex_protocol,
1287                                                      &efi_con_in_ex,
1288                                                      NULL);
1289
1290         /* Create console node and install device path protocols */
1291         if (CONFIG_IS_ENABLED(DM_SERIAL)) {
1292                 dp = efi_dp_from_uart();
1293                 if (!dp)
1294                         goto out_of_memory;
1295
1296                 /* Hook UART up to the device list */
1297                 efi_add_handle(&uart_obj);
1298
1299                 /* Install device path */
1300                 r = efi_add_protocol(&uart_obj, &efi_guid_device_path, dp);
1301                 if (r != EFI_SUCCESS)
1302                         goto out_of_memory;
1303         }
1304
1305         /* Create console events */
1306         r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
1307                              NULL, NULL, &efi_con_in.wait_for_key);
1308         if (r != EFI_SUCCESS) {
1309                 printf("ERROR: Failed to register WaitForKey event\n");
1310                 return r;
1311         }
1312         efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
1313         r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
1314                              efi_console_timer_notify, NULL, NULL,
1315                              &console_timer_event);
1316         if (r != EFI_SUCCESS) {
1317                 printf("ERROR: Failed to register console event\n");
1318                 return r;
1319         }
1320         /* 5000 ns cycle is sufficient for 2 MBaud */
1321         r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
1322         if (r != EFI_SUCCESS)
1323                 printf("ERROR: Failed to set console timer\n");
1324         return r;
1325 out_of_memory:
1326         printf("ERROR: Out of memory\n");
1327         return r;
1328 }
1329
1330 /**
1331  * efi_console_get_u16_string() - get user input string
1332  *
1333  * @cin:                protocol interface to EFI_SIMPLE_TEXT_INPUT_PROTOCOL
1334  * @buf:                buffer to store user input string in UTF16
1335  * @count:              number of u16 string including NULL terminator that buf has
1336  * @filter_func:        callback to filter user input
1337  * @row:                row number to locate user input form
1338  * @col:                column number to locate user input form
1339  * Return:              status code
1340  */
1341 efi_status_t efi_console_get_u16_string(struct efi_simple_text_input_protocol *cin,
1342                                         u16 *buf, efi_uintn_t count,
1343                                         efi_console_filter_func filter_func,
1344                                         int row, int col)
1345 {
1346         efi_status_t ret;
1347         efi_uintn_t len = 0;
1348         struct efi_input_key key;
1349
1350         printf(ANSI_CURSOR_POSITION
1351                ANSI_CLEAR_LINE_TO_END
1352                ANSI_CURSOR_SHOW, row, col);
1353
1354         ret = EFI_CALL(cin->reset(cin, false));
1355         if (ret != EFI_SUCCESS)
1356                 return ret;
1357
1358         for (;;) {
1359                 do {
1360                         ret = EFI_CALL(cin->read_key_stroke(cin, &key));
1361                         mdelay(10);
1362                 } while (ret == EFI_NOT_READY);
1363
1364                 if (key.unicode_char == u'\b') {
1365                         if (len > 0)
1366                                 buf[--len] = u'\0';
1367
1368                         printf(ANSI_CURSOR_POSITION
1369                                "%ls"
1370                                ANSI_CLEAR_LINE_TO_END, row, col, buf);
1371                         continue;
1372                 } else if (key.unicode_char == u'\r') {
1373                         buf[len] = u'\0';
1374                         return EFI_SUCCESS;
1375                 } else if (key.unicode_char == 0x3 || key.scan_code == 23) {
1376                         return EFI_ABORTED;
1377                 } else if (key.unicode_char < 0x20) {
1378                         /* ignore control codes other than Ctrl+C, '\r' and '\b' */
1379                         continue;
1380                 } else if (key.scan_code != 0) {
1381                         /* only accept single ESC press for cancel */
1382                         continue;
1383                 }
1384
1385                 if (filter_func) {
1386                         if (filter_func(&key) != EFI_SUCCESS)
1387                                 continue;
1388                 }
1389
1390                 if (len >= (count - 1))
1391                         continue;
1392
1393                 buf[len] = key.unicode_char;
1394                 len++;
1395                 printf(ANSI_CURSOR_POSITION "%ls", row, col, buf);
1396         }
1397 }
This page took 0.110574 seconds and 4 git commands to generate.