Merge branch 'master' of git://git.denx.de/u-boot
[oweals/u-boot.git] / common / cli.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * Add to readline cmdline-editing by
7  * (C) Copyright 2005
8  * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
9  */
10
11 #include <common.h>
12 #include <cli.h>
13 #include <cli_hush.h>
14 #include <command.h>
15 #include <console.h>
16 #include <env.h>
17 #include <fdtdec.h>
18 #include <hang.h>
19 #include <malloc.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 #ifdef CONFIG_CMDLINE
24 /*
25  * Run a command using the selected parser.
26  *
27  * @param cmd   Command to run
28  * @param flag  Execution flags (CMD_FLAG_...)
29  * @return 0 on success, or != 0 on error.
30  */
31 int run_command(const char *cmd, int flag)
32 {
33 #if !CONFIG_IS_ENABLED(HUSH_PARSER)
34         /*
35          * cli_run_command can return 0 or 1 for success, so clean up
36          * its result.
37          */
38         if (cli_simple_run_command(cmd, flag) == -1)
39                 return 1;
40
41         return 0;
42 #else
43         int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
44
45         if (flag & CMD_FLAG_ENV)
46                 hush_flags |= FLAG_CONT_ON_NEWLINE;
47         return parse_string_outer(cmd, hush_flags);
48 #endif
49 }
50
51 /*
52  * Run a command using the selected parser, and check if it is repeatable.
53  *
54  * @param cmd   Command to run
55  * @param flag  Execution flags (CMD_FLAG_...)
56  * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
57  */
58 int run_command_repeatable(const char *cmd, int flag)
59 {
60 #ifndef CONFIG_HUSH_PARSER
61         return cli_simple_run_command(cmd, flag);
62 #else
63         /*
64          * parse_string_outer() returns 1 for failure, so clean up
65          * its result.
66          */
67         if (parse_string_outer(cmd,
68                                FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
69                 return -1;
70
71         return 0;
72 #endif
73 }
74 #else
75 __weak int board_run_command(const char *cmdline)
76 {
77         printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
78
79         return 1;
80 }
81 #endif /* CONFIG_CMDLINE */
82
83 int run_command_list(const char *cmd, int len, int flag)
84 {
85         int need_buff = 1;
86         char *buff = (char *)cmd;       /* cast away const */
87         int rcode = 0;
88
89         if (len == -1) {
90                 len = strlen(cmd);
91 #ifdef CONFIG_HUSH_PARSER
92                 /* hush will never change our string */
93                 need_buff = 0;
94 #else
95                 /* the built-in parser will change our string if it sees \n */
96                 need_buff = strchr(cmd, '\n') != NULL;
97 #endif
98         }
99         if (need_buff) {
100                 buff = malloc(len + 1);
101                 if (!buff)
102                         return 1;
103                 memcpy(buff, cmd, len);
104                 buff[len] = '\0';
105         }
106 #ifdef CONFIG_HUSH_PARSER
107         rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
108 #else
109         /*
110          * This function will overwrite any \n it sees with a \0, which
111          * is why it can't work with a const char *. Here we are making
112          * using of internal knowledge of this function, to avoid always
113          * doing a malloc() which is actually required only in a case that
114          * is pretty rare.
115          */
116 #ifdef CONFIG_CMDLINE
117         rcode = cli_simple_run_command_list(buff, flag);
118 #else
119         rcode = board_run_command(buff);
120 #endif
121 #endif
122         if (need_buff)
123                 free(buff);
124
125         return rcode;
126 }
127
128 /****************************************************************************/
129
130 #if defined(CONFIG_CMD_RUN)
131 int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
132 {
133         int i;
134
135         if (argc < 2)
136                 return CMD_RET_USAGE;
137
138         for (i = 1; i < argc; ++i) {
139                 char *arg;
140
141                 arg = env_get(argv[i]);
142                 if (arg == NULL) {
143                         printf("## Error: \"%s\" not defined\n", argv[i]);
144                         return 1;
145                 }
146
147                 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
148                         return 1;
149         }
150         return 0;
151 }
152 #endif
153
154 #if CONFIG_IS_ENABLED(OF_CONTROL)
155 bool cli_process_fdt(const char **cmdp)
156 {
157         /* Allow the fdt to override the boot command */
158         char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
159         if (env)
160                 *cmdp = env;
161         /*
162          * If the bootsecure option was chosen, use secure_boot_cmd().
163          * Always use 'env' in this case, since bootsecure requres that the
164          * bootcmd was specified in the FDT too.
165          */
166         return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
167 }
168
169 /*
170  * Runs the given boot command securely.  Specifically:
171  * - Doesn't run the command with the shell (run_command or parse_string_outer),
172  *   since that's a lot of code surface that an attacker might exploit.
173  *   Because of this, we don't do any argument parsing--the secure boot command
174  *   has to be a full-fledged u-boot command.
175  * - Doesn't check for keypresses before booting, since that could be a
176  *   security hole; also disables Ctrl-C.
177  * - Doesn't allow the command to return.
178  *
179  * Upon any failures, this function will drop into an infinite loop after
180  * printing the error message to console.
181  */
182 void cli_secure_boot_cmd(const char *cmd)
183 {
184 #ifdef CONFIG_CMDLINE
185         cmd_tbl_t *cmdtp;
186 #endif
187         int rc;
188
189         if (!cmd) {
190                 printf("## Error: Secure boot command not specified\n");
191                 goto err;
192         }
193
194         /* Disable Ctrl-C just in case some command is used that checks it. */
195         disable_ctrlc(1);
196
197         /* Find the command directly. */
198 #ifdef CONFIG_CMDLINE
199         cmdtp = find_cmd(cmd);
200         if (!cmdtp) {
201                 printf("## Error: \"%s\" not defined\n", cmd);
202                 goto err;
203         }
204
205         /* Run the command, forcing no flags and faking argc and argv. */
206         rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
207
208 #else
209         rc = board_run_command(cmd);
210 #endif
211
212         /* Shouldn't ever return from boot command. */
213         printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
214
215 err:
216         /*
217          * Not a whole lot to do here.  Rebooting won't help much, since we'll
218          * just end up right back here.  Just loop.
219          */
220         hang();
221 }
222 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
223
224 void cli_loop(void)
225 {
226         bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
227 #ifdef CONFIG_HUSH_PARSER
228         parse_file_outer();
229         /* This point is never reached */
230         for (;;);
231 #elif defined(CONFIG_CMDLINE)
232         cli_simple_loop();
233 #else
234         printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
235 #endif /*CONFIG_HUSH_PARSER*/
236 }
237
238 void cli_init(void)
239 {
240 #ifdef CONFIG_HUSH_PARSER
241         u_boot_hush_start();
242 #endif
243
244 #if defined(CONFIG_HUSH_INIT_VAR)
245         hush_init_var();
246 #endif
247 }