blob: 7ffe902b88dd13c06d67035a3eac91047a9aeee5 [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>
12#include <cli.h>
13#include <cli_hush.h>
Simon Glass288b29e2019-11-14 12:57:43 -070014#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070015#include <console.h>
Simon Glass7b51b572019-08-01 09:46:52 -060016#include <env.h>
Simon Glassaffb2152014-04-10 20:01:35 -060017#include <fdtdec.h>
Simon Glassdb41d652019-12-28 10:45:07 -070018#include <hang.h>
Simon Glass30354972014-04-10 20:01:29 -060019#include <malloc.h>
20
Simon Glassaffb2152014-04-10 20:01:35 -060021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glassf8bb6962016-03-19 02:18:38 -060023#ifdef CONFIG_CMDLINE
Simon Glass30354972014-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. Davis2dd468d2019-01-17 13:43:04 -060033#if !CONFIG_IS_ENABLED(HUSH_PARSER)
Simon Glass30354972014-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 Glass87b63982014-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 Glass30354972014-04-10 20:01:29 -060048#endif
49}
50
Thomas Betker1d43bfd2014-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 Yamadaf1f9d4f2016-06-21 02:11:19 +090060#ifndef CONFIG_HUSH_PARSER
Thomas Betker1d43bfd2014-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}
Simon Glassf8bb6962016-03-19 02:18:38 -060074#endif /* CONFIG_CMDLINE */
Thomas Betker1d43bfd2014-06-05 20:07:57 +020075
Simon Glass30354972014-04-10 20:01:29 -060076int run_command_list(const char *cmd, int len, int flag)
77{
78 int need_buff = 1;
79 char *buff = (char *)cmd; /* cast away const */
80 int rcode = 0;
81
82 if (len == -1) {
83 len = strlen(cmd);
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +090084#ifdef CONFIG_HUSH_PARSER
Simon Glass30354972014-04-10 20:01:29 -060085 /* hush will never change our string */
86 need_buff = 0;
87#else
88 /* the built-in parser will change our string if it sees \n */
89 need_buff = strchr(cmd, '\n') != NULL;
90#endif
91 }
92 if (need_buff) {
93 buff = malloc(len + 1);
94 if (!buff)
95 return 1;
96 memcpy(buff, cmd, len);
97 buff[len] = '\0';
98 }
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +090099#ifdef CONFIG_HUSH_PARSER
Simon Glass30354972014-04-10 20:01:29 -0600100 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
101#else
102 /*
103 * This function will overwrite any \n it sees with a \0, which
104 * is why it can't work with a const char *. Here we are making
105 * using of internal knowledge of this function, to avoid always
106 * doing a malloc() which is actually required only in a case that
107 * is pretty rare.
108 */
Simon Glassf8bb6962016-03-19 02:18:38 -0600109#ifdef CONFIG_CMDLINE
Simon Glass30354972014-04-10 20:01:29 -0600110 rcode = cli_simple_run_command_list(buff, flag);
Simon Glassf8bb6962016-03-19 02:18:38 -0600111#else
112 rcode = board_run_command(buff);
113#endif
Peng Fan09a78862015-12-22 17:14:13 +0800114#endif
Simon Glass30354972014-04-10 20:01:29 -0600115 if (need_buff)
116 free(buff);
Simon Glass30354972014-04-10 20:01:29 -0600117
118 return rcode;
119}
120
121/****************************************************************************/
122
123#if defined(CONFIG_CMD_RUN)
124int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
125{
126 int i;
127
128 if (argc < 2)
129 return CMD_RET_USAGE;
130
131 for (i = 1; i < argc; ++i) {
132 char *arg;
133
Simon Glass00caae62017-08-03 12:22:12 -0600134 arg = env_get(argv[i]);
Simon Glass30354972014-04-10 20:01:29 -0600135 if (arg == NULL) {
136 printf("## Error: \"%s\" not defined\n", argv[i]);
137 return 1;
138 }
139
Simon Glass87b63982014-10-07 13:59:43 -0600140 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
Simon Glass30354972014-04-10 20:01:29 -0600141 return 1;
142 }
143 return 0;
144}
145#endif
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600146
Masahiro Yamada0f925822015-08-12 07:31:55 +0900147#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassaffb2152014-04-10 20:01:35 -0600148bool cli_process_fdt(const char **cmdp)
149{
150 /* Allow the fdt to override the boot command */
151 char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
152 if (env)
153 *cmdp = env;
154 /*
155 * If the bootsecure option was chosen, use secure_boot_cmd().
156 * Always use 'env' in this case, since bootsecure requres that the
157 * bootcmd was specified in the FDT too.
158 */
159 return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
160}
161
162/*
163 * Runs the given boot command securely. Specifically:
164 * - Doesn't run the command with the shell (run_command or parse_string_outer),
165 * since that's a lot of code surface that an attacker might exploit.
166 * Because of this, we don't do any argument parsing--the secure boot command
167 * has to be a full-fledged u-boot command.
168 * - Doesn't check for keypresses before booting, since that could be a
169 * security hole; also disables Ctrl-C.
170 * - Doesn't allow the command to return.
171 *
172 * Upon any failures, this function will drop into an infinite loop after
173 * printing the error message to console.
174 */
175void cli_secure_boot_cmd(const char *cmd)
176{
Simon Glassf8bb6962016-03-19 02:18:38 -0600177#ifdef CONFIG_CMDLINE
Simon Glassaffb2152014-04-10 20:01:35 -0600178 cmd_tbl_t *cmdtp;
Simon Glassf8bb6962016-03-19 02:18:38 -0600179#endif
Simon Glassaffb2152014-04-10 20:01:35 -0600180 int rc;
181
182 if (!cmd) {
183 printf("## Error: Secure boot command not specified\n");
184 goto err;
185 }
186
187 /* Disable Ctrl-C just in case some command is used that checks it. */
188 disable_ctrlc(1);
189
190 /* Find the command directly. */
Simon Glassf8bb6962016-03-19 02:18:38 -0600191#ifdef CONFIG_CMDLINE
Simon Glassaffb2152014-04-10 20:01:35 -0600192 cmdtp = find_cmd(cmd);
193 if (!cmdtp) {
194 printf("## Error: \"%s\" not defined\n", cmd);
195 goto err;
196 }
197
198 /* Run the command, forcing no flags and faking argc and argv. */
199 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
200
Simon Glassf8bb6962016-03-19 02:18:38 -0600201#else
202 rc = board_run_command(cmd);
203#endif
204
Simon Glassaffb2152014-04-10 20:01:35 -0600205 /* Shouldn't ever return from boot command. */
206 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
207
208err:
209 /*
210 * Not a whole lot to do here. Rebooting won't help much, since we'll
211 * just end up right back here. Just loop.
212 */
213 hang();
214}
Masahiro Yamada0f925822015-08-12 07:31:55 +0900215#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Simon Glassaffb2152014-04-10 20:01:35 -0600216
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600217void cli_loop(void)
218{
Heiko Schocherb07cc482019-04-12 12:37:03 +0200219 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900220#ifdef CONFIG_HUSH_PARSER
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600221 parse_file_outer();
222 /* This point is never reached */
223 for (;;);
Stefan Roese30eae262016-04-04 16:32:15 +0200224#elif defined(CONFIG_CMDLINE)
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600225 cli_simple_loop();
Simon Glassf8bb6962016-03-19 02:18:38 -0600226#else
227 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900228#endif /*CONFIG_HUSH_PARSER*/
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600229}
230
231void cli_init(void)
232{
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900233#ifdef CONFIG_HUSH_PARSER
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600234 u_boot_hush_start();
235#endif
236
237#if defined(CONFIG_HUSH_INIT_VAR)
238 hush_init_var();
239#endif
240}