blob: 18d7e198a88235bd06fc82b6466d6bb865286071 [file] [log] [blame]
Simon Glass30354972014-04-10 20:01:29 -06001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * Add to readline cmdline-editing by
6 * (C) Copyright 2005
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
8 *
9 * SPDX-License-Identifier: GPL-2.0+
10 */
11
12#include <common.h>
13#include <cli.h>
14#include <cli_hush.h>
Simon Glass24b852a2015-11-08 23:47:45 -070015#include <console.h>
Simon Glassaffb2152014-04-10 20:01:35 -060016#include <fdtdec.h>
Simon Glass30354972014-04-10 20:01:29 -060017#include <malloc.h>
18
Simon Glassaffb2152014-04-10 20:01:35 -060019DECLARE_GLOBAL_DATA_PTR;
20
Simon Glassf8bb6962016-03-19 02:18:38 -060021#ifdef CONFIG_CMDLINE
Simon Glass30354972014-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{
31#ifndef CONFIG_SYS_HUSH_PARSER
32 /*
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 Glass87b63982014-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 Glass30354972014-04-10 20:01:29 -060046#endif
47}
48
Thomas Betker1d43bfd2014-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{
58#ifndef CONFIG_SYS_HUSH_PARSER
59 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 Glassf8bb6962016-03-19 02:18:38 -060072#endif /* CONFIG_CMDLINE */
Thomas Betker1d43bfd2014-06-05 20:07:57 +020073
Simon Glass30354972014-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);
82#ifdef CONFIG_SYS_HUSH_PARSER
83 /* 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 }
97#ifdef CONFIG_SYS_HUSH_PARSER
98 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 Glassf8bb6962016-03-19 02:18:38 -0600107#ifdef CONFIG_CMDLINE
Simon Glass30354972014-04-10 20:01:29 -0600108 rcode = cli_simple_run_command_list(buff, flag);
Simon Glassf8bb6962016-03-19 02:18:38 -0600109#else
110 rcode = board_run_command(buff);
111#endif
Peng Fan09a78862015-12-22 17:14:13 +0800112#endif
Simon Glass30354972014-04-10 20:01:29 -0600113 if (need_buff)
114 free(buff);
Simon Glass30354972014-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
132 arg = getenv(argv[i]);
133 if (arg == NULL) {
134 printf("## Error: \"%s\" not defined\n", argv[i]);
135 return 1;
136 }
137
Simon Glass87b63982014-10-07 13:59:43 -0600138 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
Simon Glass30354972014-04-10 20:01:29 -0600139 return 1;
140 }
141 return 0;
142}
143#endif
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600144
Masahiro Yamada0f925822015-08-12 07:31:55 +0900145#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassaffb2152014-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 Glassf8bb6962016-03-19 02:18:38 -0600175#ifdef CONFIG_CMDLINE
Simon Glassaffb2152014-04-10 20:01:35 -0600176 cmd_tbl_t *cmdtp;
Simon Glassf8bb6962016-03-19 02:18:38 -0600177#endif
Simon Glassaffb2152014-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 Glassf8bb6962016-03-19 02:18:38 -0600189#ifdef CONFIG_CMDLINE
Simon Glassaffb2152014-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 Glassf8bb6962016-03-19 02:18:38 -0600199#else
200 rc = board_run_command(cmd);
201#endif
202
Simon Glassaffb2152014-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 Yamada0f925822015-08-12 07:31:55 +0900213#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Simon Glassaffb2152014-04-10 20:01:35 -0600214
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600215void cli_loop(void)
216{
217#ifdef CONFIG_SYS_HUSH_PARSER
218 parse_file_outer();
219 /* This point is never reached */
220 for (;;);
Stefan Roese30eae262016-04-04 16:32:15 +0200221#elif defined(CONFIG_CMDLINE)
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600222 cli_simple_loop();
Simon Glassf8bb6962016-03-19 02:18:38 -0600223#else
224 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600225#endif /*CONFIG_SYS_HUSH_PARSER*/
226}
227
228void cli_init(void)
229{
230#ifdef CONFIG_SYS_HUSH_PARSER
231 u_boot_hush_start();
232#endif
233
234#if defined(CONFIG_HUSH_INIT_VAR)
235 hush_init_var();
236#endif
237}