Merge branch '2021-05-24-add-lto-support'
[platform/kernel/u-boot.git] / cmd / terminal.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2007 OpenMoko, Inc.
4  * Written by Harald Welte <laforge@openmoko.org>
5  */
6
7 /*
8  * Boot support
9  */
10 #include <common.h>
11 #include <command.h>
12 #include <stdio_dev.h>
13 #include <serial.h>
14
15 int do_terminal(struct cmd_tbl *cmd, int flag, int argc, char *const argv[])
16 {
17         int last_tilde = 0;
18         struct stdio_dev *dev = NULL;
19
20         if (argc < 1)
21                 return -1;
22
23         /* Scan for selected output/input device */
24         dev = stdio_get_by_name(argv[1]);
25         if (!dev)
26                 return -1;
27
28         if (IS_ENABLED(CONFIG_SERIAL))
29                 serial_reinit_all();
30
31         printf("Entering terminal mode for port %s\n", dev->name);
32         puts("Use '~.' to leave the terminal and get back to u-boot\n");
33
34         while (1) {
35                 int c;
36
37                 /* read from console and display on serial port */
38                 if (stdio_devices[0]->tstc(stdio_devices[0])) {
39                         c = stdio_devices[0]->getc(stdio_devices[0]);
40                         if (last_tilde == 1) {
41                                 if (c == '.') {
42                                         putc(c);
43                                         putc('\n');
44                                         break;
45                                 } else {
46                                         last_tilde = 0;
47                                         /* write the delayed tilde */
48                                         dev->putc(dev, '~');
49                                         /* fall-through to print current
50                                          * character */
51                                 }
52                         }
53                         if (c == '~') {
54                                 last_tilde = 1;
55                                 puts("[u-boot]");
56                                 putc(c);
57                         }
58                         dev->putc(dev, c);
59                 }
60
61                 /* read from serial port and display on console */
62                 if (dev->tstc(dev)) {
63                         c = dev->getc(dev);
64                         putc(c);
65                 }
66         }
67         return 0;
68 }
69
70
71 /***************************************************/
72
73 U_BOOT_CMD(
74         terminal,       3,      1,      do_terminal,
75         "start terminal emulator",
76         ""
77 );