blob: 6635ab2bcf85186fd57292c7176896d09443dbed [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass30354972014-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 Glass30354972014-04-10 20:01:29 -06009 */
10
11#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -060012#include <bootstage.h>
Simon Glass30354972014-04-10 20:01:29 -060013#include <cli.h>
14#include <cli_hush.h>
Simon Glass288b29e2019-11-14 12:57:43 -070015#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070016#include <console.h>
Simon Glass7b51b572019-08-01 09:46:52 -060017#include <env.h>
Simon Glassaffb2152014-04-10 20:01:35 -060018#include <fdtdec.h>
Simon Glassdb41d652019-12-28 10:45:07 -070019#include <hang.h>
Simon Glass30354972014-04-10 20:01:29 -060020#include <malloc.h>
21
Simon Glassaffb2152014-04-10 20:01:35 -060022DECLARE_GLOBAL_DATA_PTR;
23
Simon Glassf8bb6962016-03-19 02:18:38 -060024#ifdef CONFIG_CMDLINE
Simon Glass30354972014-04-10 20:01:29 -060025/*
26 * Run a command using the selected parser.
27 *
28 * @param cmd Command to run
29 * @param flag Execution flags (CMD_FLAG_...)
30 * @return 0 on success, or != 0 on error.
31 */
32int run_command(const char *cmd, int flag)
33{
Andrew F. Davis2dd468d2019-01-17 13:43:04 -060034#if !CONFIG_IS_ENABLED(HUSH_PARSER)
Simon Glass30354972014-04-10 20:01:29 -060035 /*
36 * cli_run_command can return 0 or 1 for success, so clean up
37 * its result.
38 */
39 if (cli_simple_run_command(cmd, flag) == -1)
40 return 1;
41
42 return 0;
43#else
Simon Glass87b63982014-10-07 13:59:43 -060044 int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
45
46 if (flag & CMD_FLAG_ENV)
47 hush_flags |= FLAG_CONT_ON_NEWLINE;
48 return parse_string_outer(cmd, hush_flags);
Simon Glass30354972014-04-10 20:01:29 -060049#endif
50}
51
Thomas Betker1d43bfd2014-06-05 20:07:57 +020052/*
53 * Run a command using the selected parser, and check if it is repeatable.
54 *
55 * @param cmd Command to run
56 * @param flag Execution flags (CMD_FLAG_...)
57 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
58 */
59int run_command_repeatable(const char *cmd, int flag)
60{
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +090061#ifndef CONFIG_HUSH_PARSER
Thomas Betker1d43bfd2014-06-05 20:07:57 +020062 return cli_simple_run_command(cmd, flag);
63#else
64 /*
65 * parse_string_outer() returns 1 for failure, so clean up
66 * its result.
67 */
68 if (parse_string_outer(cmd,
69 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
70 return -1;
71
72 return 0;
73#endif
74}
Sean Anderson19464f42020-01-10 12:32:19 -050075#else
76__weak int board_run_command(const char *cmdline)
77{
78 printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
79
80 return 1;
81}
Simon Glassf8bb6962016-03-19 02:18:38 -060082#endif /* CONFIG_CMDLINE */
Thomas Betker1d43bfd2014-06-05 20:07:57 +020083
Simon Glass30354972014-04-10 20:01:29 -060084int run_command_list(const char *cmd, int len, int flag)
85{
86 int need_buff = 1;
87 char *buff = (char *)cmd; /* cast away const */
88 int rcode = 0;
89
90 if (len == -1) {
91 len = strlen(cmd);
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +090092#ifdef CONFIG_HUSH_PARSER
Simon Glass30354972014-04-10 20:01:29 -060093 /* hush will never change our string */
94 need_buff = 0;
95#else
96 /* the built-in parser will change our string if it sees \n */
97 need_buff = strchr(cmd, '\n') != NULL;
98#endif
99 }
100 if (need_buff) {
101 buff = malloc(len + 1);
102 if (!buff)
103 return 1;
104 memcpy(buff, cmd, len);
105 buff[len] = '\0';
106 }
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900107#ifdef CONFIG_HUSH_PARSER
Simon Glass30354972014-04-10 20:01:29 -0600108 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
109#else
110 /*
111 * This function will overwrite any \n it sees with a \0, which
112 * is why it can't work with a const char *. Here we are making
113 * using of internal knowledge of this function, to avoid always
114 * doing a malloc() which is actually required only in a case that
115 * is pretty rare.
116 */
Simon Glassf8bb6962016-03-19 02:18:38 -0600117#ifdef CONFIG_CMDLINE
Simon Glass30354972014-04-10 20:01:29 -0600118 rcode = cli_simple_run_command_list(buff, flag);
Simon Glassf8bb6962016-03-19 02:18:38 -0600119#else
120 rcode = board_run_command(buff);
121#endif
Peng Fan09a78862015-12-22 17:14:13 +0800122#endif
Simon Glass30354972014-04-10 20:01:29 -0600123 if (need_buff)
124 free(buff);
Simon Glass30354972014-04-10 20:01:29 -0600125
126 return rcode;
127}
128
129/****************************************************************************/
130
131#if defined(CONFIG_CMD_RUN)
Simon Glass09140112020-05-10 11:40:03 -0600132int do_run(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glass30354972014-04-10 20:01:29 -0600133{
134 int i;
135
136 if (argc < 2)
137 return CMD_RET_USAGE;
138
139 for (i = 1; i < argc; ++i) {
140 char *arg;
141
Simon Glass00caae62017-08-03 12:22:12 -0600142 arg = env_get(argv[i]);
Simon Glass30354972014-04-10 20:01:29 -0600143 if (arg == NULL) {
144 printf("## Error: \"%s\" not defined\n", argv[i]);
145 return 1;
146 }
147
Simon Glass87b63982014-10-07 13:59:43 -0600148 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
Simon Glass30354972014-04-10 20:01:29 -0600149 return 1;
150 }
151 return 0;
152}
153#endif
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600154
Masahiro Yamada0f925822015-08-12 07:31:55 +0900155#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassaffb2152014-04-10 20:01:35 -0600156bool cli_process_fdt(const char **cmdp)
157{
158 /* Allow the fdt to override the boot command */
159 char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
160 if (env)
161 *cmdp = env;
162 /*
163 * If the bootsecure option was chosen, use secure_boot_cmd().
164 * Always use 'env' in this case, since bootsecure requres that the
165 * bootcmd was specified in the FDT too.
166 */
167 return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
168}
169
170/*
171 * Runs the given boot command securely. Specifically:
172 * - Doesn't run the command with the shell (run_command or parse_string_outer),
173 * since that's a lot of code surface that an attacker might exploit.
174 * Because of this, we don't do any argument parsing--the secure boot command
175 * has to be a full-fledged u-boot command.
176 * - Doesn't check for keypresses before booting, since that could be a
177 * security hole; also disables Ctrl-C.
178 * - Doesn't allow the command to return.
179 *
180 * Upon any failures, this function will drop into an infinite loop after
181 * printing the error message to console.
182 */
183void cli_secure_boot_cmd(const char *cmd)
184{
Simon Glassf8bb6962016-03-19 02:18:38 -0600185#ifdef CONFIG_CMDLINE
Simon Glass09140112020-05-10 11:40:03 -0600186 struct cmd_tbl *cmdtp;
Simon Glassf8bb6962016-03-19 02:18:38 -0600187#endif
Simon Glassaffb2152014-04-10 20:01:35 -0600188 int rc;
189
190 if (!cmd) {
191 printf("## Error: Secure boot command not specified\n");
192 goto err;
193 }
194
195 /* Disable Ctrl-C just in case some command is used that checks it. */
196 disable_ctrlc(1);
197
198 /* Find the command directly. */
Simon Glassf8bb6962016-03-19 02:18:38 -0600199#ifdef CONFIG_CMDLINE
Simon Glassaffb2152014-04-10 20:01:35 -0600200 cmdtp = find_cmd(cmd);
201 if (!cmdtp) {
202 printf("## Error: \"%s\" not defined\n", cmd);
203 goto err;
204 }
205
206 /* Run the command, forcing no flags and faking argc and argv. */
207 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
208
Simon Glassf8bb6962016-03-19 02:18:38 -0600209#else
210 rc = board_run_command(cmd);
211#endif
212
Simon Glassaffb2152014-04-10 20:01:35 -0600213 /* Shouldn't ever return from boot command. */
214 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
215
216err:
217 /*
218 * Not a whole lot to do here. Rebooting won't help much, since we'll
219 * just end up right back here. Just loop.
220 */
221 hang();
222}
Masahiro Yamada0f925822015-08-12 07:31:55 +0900223#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Simon Glassaffb2152014-04-10 20:01:35 -0600224
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600225void cli_loop(void)
226{
Heiko Schocherb07cc482019-04-12 12:37:03 +0200227 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900228#ifdef CONFIG_HUSH_PARSER
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600229 parse_file_outer();
230 /* This point is never reached */
231 for (;;);
Stefan Roese30eae262016-04-04 16:32:15 +0200232#elif defined(CONFIG_CMDLINE)
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600233 cli_simple_loop();
Simon Glassf8bb6962016-03-19 02:18:38 -0600234#else
235 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900236#endif /*CONFIG_HUSH_PARSER*/
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600237}
238
239void cli_init(void)
240{
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900241#ifdef CONFIG_HUSH_PARSER
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600242 u_boot_hush_start();
243#endif
244
245#if defined(CONFIG_HUSH_INIT_VAR)
246 hush_init_var();
247#endif
248}