blob: 38bba17585ca5a56970ad61317d12408833cd657 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd6d592f2014-04-10 20:01:29 -06002/*
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>
Simon Glassd6d592f2014-04-10 20:01:29 -06009 */
10
11#include <common.h>
12#include <cli.h>
13#include <cli_hush.h>
Simon Glassadaaa482019-11-14 12:57:43 -070014#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070015#include <console.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060016#include <env.h>
Simon Glass5b47e302014-04-10 20:01:35 -060017#include <fdtdec.h>
Simon Glassf11478f2019-12-28 10:45:07 -070018#include <hang.h>
Simon Glassd6d592f2014-04-10 20:01:29 -060019#include <malloc.h>
20
Simon Glass5b47e302014-04-10 20:01:35 -060021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass4ef90b72016-03-19 02:18:38 -060023#ifdef CONFIG_CMDLINE
Simon Glassd6d592f2014-04-10 20:01:29 -060024/*
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 */
31int run_command(const char *cmd, int flag)
32{
Andrew F. Davis9b0fbdc2019-01-17 13:43:04 -060033#if !CONFIG_IS_ENABLED(HUSH_PARSER)
Simon Glassd6d592f2014-04-10 20:01:29 -060034 /*
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
Simon Glass94cb9c22014-10-07 13:59:43 -060043 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);
Simon Glassd6d592f2014-04-10 20:01:29 -060048#endif
49}
50
Thomas Betkerbe6346b2014-06-05 20:07:57 +020051/*
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 */
58int run_command_repeatable(const char *cmd, int flag)
59{
Masahiro Yamada17a48e42016-06-21 02:11:19 +090060#ifndef CONFIG_HUSH_PARSER
Thomas Betkerbe6346b2014-06-05 20:07:57 +020061 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}
Sean Anderson41f0a662020-01-10 12:32:19 -050074#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}
Simon Glass4ef90b72016-03-19 02:18:38 -060081#endif /* CONFIG_CMDLINE */
Thomas Betkerbe6346b2014-06-05 20:07:57 +020082
Simon Glassd6d592f2014-04-10 20:01:29 -060083int 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);
Masahiro Yamada17a48e42016-06-21 02:11:19 +090091#ifdef CONFIG_HUSH_PARSER
Simon Glassd6d592f2014-04-10 20:01:29 -060092 /* 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 }
Masahiro Yamada17a48e42016-06-21 02:11:19 +0900106#ifdef CONFIG_HUSH_PARSER
Simon Glassd6d592f2014-04-10 20:01:29 -0600107 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 */
Simon Glass4ef90b72016-03-19 02:18:38 -0600116#ifdef CONFIG_CMDLINE
Simon Glassd6d592f2014-04-10 20:01:29 -0600117 rcode = cli_simple_run_command_list(buff, flag);
Simon Glass4ef90b72016-03-19 02:18:38 -0600118#else
119 rcode = board_run_command(buff);
120#endif
Peng Fan63225512015-12-22 17:14:13 +0800121#endif
Simon Glassd6d592f2014-04-10 20:01:29 -0600122 if (need_buff)
123 free(buff);
Simon Glassd6d592f2014-04-10 20:01:29 -0600124
125 return rcode;
126}
127
128/****************************************************************************/
129
130#if defined(CONFIG_CMD_RUN)
131int 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
Simon Glass64b723f2017-08-03 12:22:12 -0600141 arg = env_get(argv[i]);
Simon Glassd6d592f2014-04-10 20:01:29 -0600142 if (arg == NULL) {
143 printf("## Error: \"%s\" not defined\n", argv[i]);
144 return 1;
145 }
146
Simon Glass94cb9c22014-10-07 13:59:43 -0600147 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
Simon Glassd6d592f2014-04-10 20:01:29 -0600148 return 1;
149 }
150 return 0;
151}
152#endif
Simon Glass33f79132014-04-10 20:01:34 -0600153
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900154#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass5b47e302014-04-10 20:01:35 -0600155bool 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 */
182void cli_secure_boot_cmd(const char *cmd)
183{
Simon Glass4ef90b72016-03-19 02:18:38 -0600184#ifdef CONFIG_CMDLINE
Simon Glass5b47e302014-04-10 20:01:35 -0600185 cmd_tbl_t *cmdtp;
Simon Glass4ef90b72016-03-19 02:18:38 -0600186#endif
Simon Glass5b47e302014-04-10 20:01:35 -0600187 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. */
Simon Glass4ef90b72016-03-19 02:18:38 -0600198#ifdef CONFIG_CMDLINE
Simon Glass5b47e302014-04-10 20:01:35 -0600199 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
Simon Glass4ef90b72016-03-19 02:18:38 -0600208#else
209 rc = board_run_command(cmd);
210#endif
211
Simon Glass5b47e302014-04-10 20:01:35 -0600212 /* Shouldn't ever return from boot command. */
213 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
214
215err:
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}
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900222#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Simon Glass5b47e302014-04-10 20:01:35 -0600223
Simon Glass33f79132014-04-10 20:01:34 -0600224void cli_loop(void)
225{
Heiko Schocher7df72c32019-04-12 12:37:03 +0200226 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
Masahiro Yamada17a48e42016-06-21 02:11:19 +0900227#ifdef CONFIG_HUSH_PARSER
Simon Glass33f79132014-04-10 20:01:34 -0600228 parse_file_outer();
229 /* This point is never reached */
230 for (;;);
Stefan Roese824300a2016-04-04 16:32:15 +0200231#elif defined(CONFIG_CMDLINE)
Simon Glass33f79132014-04-10 20:01:34 -0600232 cli_simple_loop();
Simon Glass4ef90b72016-03-19 02:18:38 -0600233#else
234 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
Masahiro Yamada17a48e42016-06-21 02:11:19 +0900235#endif /*CONFIG_HUSH_PARSER*/
Simon Glass33f79132014-04-10 20:01:34 -0600236}
237
238void cli_init(void)
239{
Masahiro Yamada17a48e42016-06-21 02:11:19 +0900240#ifdef CONFIG_HUSH_PARSER
Simon Glass33f79132014-04-10 20:01:34 -0600241 u_boot_hush_start();
242#endif
243
244#if defined(CONFIG_HUSH_INIT_VAR)
245 hush_init_var();
246#endif
247}