blob: 49b910666b9fff195363b3bb6f2f9db675ca5015 [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 Glassa73bda42015-11-08 23:47:45 -070014#include <console.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060015#include <env.h>
Simon Glass5b47e302014-04-10 20:01:35 -060016#include <fdtdec.h>
Simon Glassd6d592f2014-04-10 20:01:29 -060017#include <malloc.h>
18
Simon Glass5b47e302014-04-10 20:01:35 -060019DECLARE_GLOBAL_DATA_PTR;
20
Simon Glass4ef90b72016-03-19 02:18:38 -060021#ifdef CONFIG_CMDLINE
Simon Glassd6d592f2014-04-10 20:01:29 -060022/*
23 * Run a command using the selected parser.
24 *
25 * @param cmd Command to run
26 * @param flag Execution flags (CMD_FLAG_...)
27 * @return 0 on success, or != 0 on error.
28 */
29int run_command(const char *cmd, int flag)
30{
Andrew F. Davis9b0fbdc2019-01-17 13:43:04 -060031#if !CONFIG_IS_ENABLED(HUSH_PARSER)
Simon Glassd6d592f2014-04-10 20:01:29 -060032 /*
33 * cli_run_command can return 0 or 1 for success, so clean up
34 * its result.
35 */
36 if (cli_simple_run_command(cmd, flag) == -1)
37 return 1;
38
39 return 0;
40#else
Simon Glass94cb9c22014-10-07 13:59:43 -060041 int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
42
43 if (flag & CMD_FLAG_ENV)
44 hush_flags |= FLAG_CONT_ON_NEWLINE;
45 return parse_string_outer(cmd, hush_flags);
Simon Glassd6d592f2014-04-10 20:01:29 -060046#endif
47}
48
Thomas Betkerbe6346b2014-06-05 20:07:57 +020049/*
50 * Run a command using the selected parser, and check if it is repeatable.
51 *
52 * @param cmd Command to run
53 * @param flag Execution flags (CMD_FLAG_...)
54 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
55 */
56int run_command_repeatable(const char *cmd, int flag)
57{
Masahiro Yamada17a48e42016-06-21 02:11:19 +090058#ifndef CONFIG_HUSH_PARSER
Thomas Betkerbe6346b2014-06-05 20:07:57 +020059 return cli_simple_run_command(cmd, flag);
60#else
61 /*
62 * parse_string_outer() returns 1 for failure, so clean up
63 * its result.
64 */
65 if (parse_string_outer(cmd,
66 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
67 return -1;
68
69 return 0;
70#endif
71}
Simon Glass4ef90b72016-03-19 02:18:38 -060072#endif /* CONFIG_CMDLINE */
Thomas Betkerbe6346b2014-06-05 20:07:57 +020073
Simon Glassd6d592f2014-04-10 20:01:29 -060074int run_command_list(const char *cmd, int len, int flag)
75{
76 int need_buff = 1;
77 char *buff = (char *)cmd; /* cast away const */
78 int rcode = 0;
79
80 if (len == -1) {
81 len = strlen(cmd);
Masahiro Yamada17a48e42016-06-21 02:11:19 +090082#ifdef CONFIG_HUSH_PARSER
Simon Glassd6d592f2014-04-10 20:01:29 -060083 /* hush will never change our string */
84 need_buff = 0;
85#else
86 /* the built-in parser will change our string if it sees \n */
87 need_buff = strchr(cmd, '\n') != NULL;
88#endif
89 }
90 if (need_buff) {
91 buff = malloc(len + 1);
92 if (!buff)
93 return 1;
94 memcpy(buff, cmd, len);
95 buff[len] = '\0';
96 }
Masahiro Yamada17a48e42016-06-21 02:11:19 +090097#ifdef CONFIG_HUSH_PARSER
Simon Glassd6d592f2014-04-10 20:01:29 -060098 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
99#else
100 /*
101 * This function will overwrite any \n it sees with a \0, which
102 * is why it can't work with a const char *. Here we are making
103 * using of internal knowledge of this function, to avoid always
104 * doing a malloc() which is actually required only in a case that
105 * is pretty rare.
106 */
Simon Glass4ef90b72016-03-19 02:18:38 -0600107#ifdef CONFIG_CMDLINE
Simon Glassd6d592f2014-04-10 20:01:29 -0600108 rcode = cli_simple_run_command_list(buff, flag);
Simon Glass4ef90b72016-03-19 02:18:38 -0600109#else
110 rcode = board_run_command(buff);
111#endif
Peng Fan63225512015-12-22 17:14:13 +0800112#endif
Simon Glassd6d592f2014-04-10 20:01:29 -0600113 if (need_buff)
114 free(buff);
Simon Glassd6d592f2014-04-10 20:01:29 -0600115
116 return rcode;
117}
118
119/****************************************************************************/
120
121#if defined(CONFIG_CMD_RUN)
122int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
123{
124 int i;
125
126 if (argc < 2)
127 return CMD_RET_USAGE;
128
129 for (i = 1; i < argc; ++i) {
130 char *arg;
131
Simon Glass64b723f2017-08-03 12:22:12 -0600132 arg = env_get(argv[i]);
Simon Glassd6d592f2014-04-10 20:01:29 -0600133 if (arg == NULL) {
134 printf("## Error: \"%s\" not defined\n", argv[i]);
135 return 1;
136 }
137
Simon Glass94cb9c22014-10-07 13:59:43 -0600138 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
Simon Glassd6d592f2014-04-10 20:01:29 -0600139 return 1;
140 }
141 return 0;
142}
143#endif
Simon Glass33f79132014-04-10 20:01:34 -0600144
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900145#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass5b47e302014-04-10 20:01:35 -0600146bool cli_process_fdt(const char **cmdp)
147{
148 /* Allow the fdt to override the boot command */
149 char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
150 if (env)
151 *cmdp = env;
152 /*
153 * If the bootsecure option was chosen, use secure_boot_cmd().
154 * Always use 'env' in this case, since bootsecure requres that the
155 * bootcmd was specified in the FDT too.
156 */
157 return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
158}
159
160/*
161 * Runs the given boot command securely. Specifically:
162 * - Doesn't run the command with the shell (run_command or parse_string_outer),
163 * since that's a lot of code surface that an attacker might exploit.
164 * Because of this, we don't do any argument parsing--the secure boot command
165 * has to be a full-fledged u-boot command.
166 * - Doesn't check for keypresses before booting, since that could be a
167 * security hole; also disables Ctrl-C.
168 * - Doesn't allow the command to return.
169 *
170 * Upon any failures, this function will drop into an infinite loop after
171 * printing the error message to console.
172 */
173void cli_secure_boot_cmd(const char *cmd)
174{
Simon Glass4ef90b72016-03-19 02:18:38 -0600175#ifdef CONFIG_CMDLINE
Simon Glass5b47e302014-04-10 20:01:35 -0600176 cmd_tbl_t *cmdtp;
Simon Glass4ef90b72016-03-19 02:18:38 -0600177#endif
Simon Glass5b47e302014-04-10 20:01:35 -0600178 int rc;
179
180 if (!cmd) {
181 printf("## Error: Secure boot command not specified\n");
182 goto err;
183 }
184
185 /* Disable Ctrl-C just in case some command is used that checks it. */
186 disable_ctrlc(1);
187
188 /* Find the command directly. */
Simon Glass4ef90b72016-03-19 02:18:38 -0600189#ifdef CONFIG_CMDLINE
Simon Glass5b47e302014-04-10 20:01:35 -0600190 cmdtp = find_cmd(cmd);
191 if (!cmdtp) {
192 printf("## Error: \"%s\" not defined\n", cmd);
193 goto err;
194 }
195
196 /* Run the command, forcing no flags and faking argc and argv. */
197 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
198
Simon Glass4ef90b72016-03-19 02:18:38 -0600199#else
200 rc = board_run_command(cmd);
201#endif
202
Simon Glass5b47e302014-04-10 20:01:35 -0600203 /* Shouldn't ever return from boot command. */
204 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
205
206err:
207 /*
208 * Not a whole lot to do here. Rebooting won't help much, since we'll
209 * just end up right back here. Just loop.
210 */
211 hang();
212}
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900213#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Simon Glass5b47e302014-04-10 20:01:35 -0600214
Simon Glass33f79132014-04-10 20:01:34 -0600215void cli_loop(void)
216{
Heiko Schocher7df72c32019-04-12 12:37:03 +0200217 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
Masahiro Yamada17a48e42016-06-21 02:11:19 +0900218#ifdef CONFIG_HUSH_PARSER
Simon Glass33f79132014-04-10 20:01:34 -0600219 parse_file_outer();
220 /* This point is never reached */
221 for (;;);
Stefan Roese824300a2016-04-04 16:32:15 +0200222#elif defined(CONFIG_CMDLINE)
Simon Glass33f79132014-04-10 20:01:34 -0600223 cli_simple_loop();
Simon Glass4ef90b72016-03-19 02:18:38 -0600224#else
225 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
Masahiro Yamada17a48e42016-06-21 02:11:19 +0900226#endif /*CONFIG_HUSH_PARSER*/
Simon Glass33f79132014-04-10 20:01:34 -0600227}
228
229void cli_init(void)
230{
Masahiro Yamada17a48e42016-06-21 02:11:19 +0900231#ifdef CONFIG_HUSH_PARSER
Simon Glass33f79132014-04-10 20:01:34 -0600232 u_boot_hush_start();
233#endif
234
235#if defined(CONFIG_HUSH_INIT_VAR)
236 hush_init_var();
237#endif
238}