blob: a50b5d53948c0a43fe65fb5b39d0804516b33c56 [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>
Simon Glass401d1c42020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glass7de8bd02021-08-07 07:24:01 -060022#include <dm/ofnode.h>
Simon Glassaffb2152014-04-10 20:01:35 -060023
benlong.zhouf0969fb2023-10-26 16:42:01 +080024#ifdef CONFIG_AMLOGIC_MODIFY
25#include <asm/amlogic/arch/timer.h>
26#endif
27
Simon Glassf8bb6962016-03-19 02:18:38 -060028#ifdef CONFIG_CMDLINE
Simon Glass30354972014-04-10 20:01:29 -060029/*
30 * Run a command using the selected parser.
31 *
32 * @param cmd Command to run
33 * @param flag Execution flags (CMD_FLAG_...)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010034 * Return: 0 on success, or != 0 on error.
Simon Glass30354972014-04-10 20:01:29 -060035 */
36int run_command(const char *cmd, int flag)
37{
benlong.zhouf0969fb2023-10-26 16:42:01 +080038#ifdef CONFIG_AMLOGIC_MODIFY
39#ifdef CONFIG_AMLOGIC_TIME_PROFILE
40 unsigned int tick = get_time();
41 int ret;
42#endif
43#endif
Andrew F. Davis2dd468d2019-01-17 13:43:04 -060044#if !CONFIG_IS_ENABLED(HUSH_PARSER)
Simon Glass30354972014-04-10 20:01:29 -060045 /*
46 * cli_run_command can return 0 or 1 for success, so clean up
47 * its result.
48 */
49 if (cli_simple_run_command(cmd, flag) == -1)
50 return 1;
51
52 return 0;
53#else
Simon Glass87b63982014-10-07 13:59:43 -060054 int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
55
56 if (flag & CMD_FLAG_ENV)
57 hush_flags |= FLAG_CONT_ON_NEWLINE;
benlong.zhouf0969fb2023-10-26 16:42:01 +080058#ifdef CONFIG_AMLOGIC_MODIFY
59#ifdef CONFIG_AMLOGIC_TIME_PROFILE
60 ret = parse_string_outer(cmd, hush_flags);
61 tick = get_time() - tick;
62 if (tick > 1000 && gd->time_print_flag) {
63 printf("\n ---long cmd, time:%5d, cmd:%s\n", tick, cmd);
64 }
65 return ret;
66#else
Simon Glass87b63982014-10-07 13:59:43 -060067 return parse_string_outer(cmd, hush_flags);
Simon Glass30354972014-04-10 20:01:29 -060068#endif
benlong.zhouf0969fb2023-10-26 16:42:01 +080069#else
70 return parse_string_outer(cmd, hush_flags);
71#endif
72#endif
Simon Glass30354972014-04-10 20:01:29 -060073}
74
Thomas Betker1d43bfd2014-06-05 20:07:57 +020075/*
76 * Run a command using the selected parser, and check if it is repeatable.
77 *
78 * @param cmd Command to run
79 * @param flag Execution flags (CMD_FLAG_...)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010080 * Return: 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
Thomas Betker1d43bfd2014-06-05 20:07:57 +020081 */
82int run_command_repeatable(const char *cmd, int flag)
83{
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +090084#ifndef CONFIG_HUSH_PARSER
Thomas Betker1d43bfd2014-06-05 20:07:57 +020085 return cli_simple_run_command(cmd, flag);
86#else
87 /*
88 * parse_string_outer() returns 1 for failure, so clean up
89 * its result.
90 */
91 if (parse_string_outer(cmd,
92 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
93 return -1;
94
95 return 0;
96#endif
97}
Sean Anderson19464f42020-01-10 12:32:19 -050098#else
99__weak int board_run_command(const char *cmdline)
100{
101 printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
102
103 return 1;
104}
Simon Glassf8bb6962016-03-19 02:18:38 -0600105#endif /* CONFIG_CMDLINE */
Thomas Betker1d43bfd2014-06-05 20:07:57 +0200106
Simon Glass30354972014-04-10 20:01:29 -0600107int run_command_list(const char *cmd, int len, int flag)
108{
109 int need_buff = 1;
110 char *buff = (char *)cmd; /* cast away const */
111 int rcode = 0;
112
113 if (len == -1) {
114 len = strlen(cmd);
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900115#ifdef CONFIG_HUSH_PARSER
Simon Glass30354972014-04-10 20:01:29 -0600116 /* hush will never change our string */
117 need_buff = 0;
118#else
119 /* the built-in parser will change our string if it sees \n */
120 need_buff = strchr(cmd, '\n') != NULL;
121#endif
122 }
123 if (need_buff) {
124 buff = malloc(len + 1);
125 if (!buff)
126 return 1;
127 memcpy(buff, cmd, len);
128 buff[len] = '\0';
129 }
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900130#ifdef CONFIG_HUSH_PARSER
Simon Glass30354972014-04-10 20:01:29 -0600131 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
132#else
133 /*
134 * This function will overwrite any \n it sees with a \0, which
135 * is why it can't work with a const char *. Here we are making
136 * using of internal knowledge of this function, to avoid always
137 * doing a malloc() which is actually required only in a case that
138 * is pretty rare.
139 */
Simon Glassf8bb6962016-03-19 02:18:38 -0600140#ifdef CONFIG_CMDLINE
Simon Glass30354972014-04-10 20:01:29 -0600141 rcode = cli_simple_run_command_list(buff, flag);
Simon Glassf8bb6962016-03-19 02:18:38 -0600142#else
143 rcode = board_run_command(buff);
144#endif
Peng Fan09a78862015-12-22 17:14:13 +0800145#endif
Simon Glass30354972014-04-10 20:01:29 -0600146 if (need_buff)
147 free(buff);
Simon Glass30354972014-04-10 20:01:29 -0600148
149 return rcode;
150}
151
Simon Glass74724482022-07-13 06:06:59 -0600152int run_commandf(const char *fmt, ...)
153{
154 va_list args;
155 char cmd[128];
156 int i, ret;
157
158 va_start(args, fmt);
159 i = vsnprintf(cmd, sizeof(cmd), fmt, args);
160 va_end(args);
161
162 ret = run_command(cmd, 0);
163
164 return ret;
165}
166
Simon Glass30354972014-04-10 20:01:29 -0600167/****************************************************************************/
168
169#if defined(CONFIG_CMD_RUN)
Simon Glass09140112020-05-10 11:40:03 -0600170int do_run(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glass30354972014-04-10 20:01:29 -0600171{
172 int i;
173
174 if (argc < 2)
175 return CMD_RET_USAGE;
176
177 for (i = 1; i < argc; ++i) {
178 char *arg;
179
Simon Glass00caae62017-08-03 12:22:12 -0600180 arg = env_get(argv[i]);
Simon Glass30354972014-04-10 20:01:29 -0600181 if (arg == NULL) {
182 printf("## Error: \"%s\" not defined\n", argv[i]);
183 return 1;
184 }
185
Simon Glass87b63982014-10-07 13:59:43 -0600186 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
Simon Glass30354972014-04-10 20:01:29 -0600187 return 1;
188 }
189 return 0;
190}
191#endif
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600192
Masahiro Yamada0f925822015-08-12 07:31:55 +0900193#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassaffb2152014-04-10 20:01:35 -0600194bool cli_process_fdt(const char **cmdp)
195{
196 /* Allow the fdt to override the boot command */
Simon Glass7de8bd02021-08-07 07:24:01 -0600197 const char *env = ofnode_conf_read_str("bootcmd");
Simon Glassaffb2152014-04-10 20:01:35 -0600198 if (env)
199 *cmdp = env;
200 /*
201 * If the bootsecure option was chosen, use secure_boot_cmd().
202 * Always use 'env' in this case, since bootsecure requres that the
203 * bootcmd was specified in the FDT too.
204 */
Simon Glass7de8bd02021-08-07 07:24:01 -0600205 return ofnode_conf_read_int("bootsecure", 0);
Simon Glassaffb2152014-04-10 20:01:35 -0600206}
207
208/*
209 * Runs the given boot command securely. Specifically:
210 * - Doesn't run the command with the shell (run_command or parse_string_outer),
211 * since that's a lot of code surface that an attacker might exploit.
212 * Because of this, we don't do any argument parsing--the secure boot command
213 * has to be a full-fledged u-boot command.
214 * - Doesn't check for keypresses before booting, since that could be a
215 * security hole; also disables Ctrl-C.
216 * - Doesn't allow the command to return.
217 *
218 * Upon any failures, this function will drop into an infinite loop after
219 * printing the error message to console.
220 */
221void cli_secure_boot_cmd(const char *cmd)
222{
Simon Glassf8bb6962016-03-19 02:18:38 -0600223#ifdef CONFIG_CMDLINE
Simon Glass09140112020-05-10 11:40:03 -0600224 struct cmd_tbl *cmdtp;
Simon Glassf8bb6962016-03-19 02:18:38 -0600225#endif
Simon Glassaffb2152014-04-10 20:01:35 -0600226 int rc;
227
228 if (!cmd) {
229 printf("## Error: Secure boot command not specified\n");
230 goto err;
231 }
232
233 /* Disable Ctrl-C just in case some command is used that checks it. */
234 disable_ctrlc(1);
235
236 /* Find the command directly. */
Simon Glassf8bb6962016-03-19 02:18:38 -0600237#ifdef CONFIG_CMDLINE
Simon Glassaffb2152014-04-10 20:01:35 -0600238 cmdtp = find_cmd(cmd);
239 if (!cmdtp) {
240 printf("## Error: \"%s\" not defined\n", cmd);
241 goto err;
242 }
243
244 /* Run the command, forcing no flags and faking argc and argv. */
245 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
246
Simon Glassf8bb6962016-03-19 02:18:38 -0600247#else
248 rc = board_run_command(cmd);
249#endif
250
Simon Glassaffb2152014-04-10 20:01:35 -0600251 /* Shouldn't ever return from boot command. */
252 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
253
254err:
255 /*
256 * Not a whole lot to do here. Rebooting won't help much, since we'll
257 * just end up right back here. Just loop.
258 */
259 hang();
260}
Masahiro Yamada0f925822015-08-12 07:31:55 +0900261#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Simon Glassaffb2152014-04-10 20:01:35 -0600262
xia.jin83aee562024-08-07 02:35:05 +0000263#ifdef CONFIG_ARMV8_MULTIENTRY
264void cli_release_lock(int cpu)
265{
266 release_cmd_locker(cpu);
267}
268#endif
269
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600270void cli_loop(void)
271{
Heiko Schocherb07cc482019-04-12 12:37:03 +0200272 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900273#ifdef CONFIG_HUSH_PARSER
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600274 parse_file_outer();
275 /* This point is never reached */
276 for (;;);
Stefan Roese30eae262016-04-04 16:32:15 +0200277#elif defined(CONFIG_CMDLINE)
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600278 cli_simple_loop();
Simon Glassf8bb6962016-03-19 02:18:38 -0600279#else
280 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900281#endif /*CONFIG_HUSH_PARSER*/
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600282}
283
284void cli_init(void)
285{
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900286#ifdef CONFIG_HUSH_PARSER
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600287 u_boot_hush_start();
288#endif
289
290#if defined(CONFIG_HUSH_INIT_VAR)
291 hush_init_var();
292#endif
293}