blob: 3916a7b10a7dd6c28ca26f35d5445d2669ae5df9 [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
Evgeny Bachinin26f923c2023-03-20 11:23:11 +030011#define pr_fmt(fmt) "cli: %s: " fmt, __func__
12
Simon Glass30354972014-04-10 20:01:29 -060013#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -060014#include <bootstage.h>
Simon Glass30354972014-04-10 20:01:29 -060015#include <cli.h>
16#include <cli_hush.h>
Simon Glass288b29e2019-11-14 12:57:43 -070017#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070018#include <console.h>
Simon Glass7b51b572019-08-01 09:46:52 -060019#include <env.h>
Simon Glassaffb2152014-04-10 20:01:35 -060020#include <fdtdec.h>
Simon Glassdb41d652019-12-28 10:45:07 -070021#include <hang.h>
Simon Glass30354972014-04-10 20:01:29 -060022#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060023#include <asm/global_data.h>
Simon Glass7de8bd02021-08-07 07:24:01 -060024#include <dm/ofnode.h>
Evgeny Bachinin26f923c2023-03-20 11:23:11 +030025#include <linux/errno.h>
Simon Glassaffb2152014-04-10 20:01:35 -060026
Simon Glassf8bb6962016-03-19 02:18:38 -060027#ifdef CONFIG_CMDLINE
Simon Glass30354972014-04-10 20:01:29 -060028/*
29 * Run a command using the selected parser.
30 *
31 * @param cmd Command to run
32 * @param flag Execution flags (CMD_FLAG_...)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010033 * Return: 0 on success, or != 0 on error.
Simon Glass30354972014-04-10 20:01:29 -060034 */
35int run_command(const char *cmd, int flag)
36{
Simon Glassb84d23d2023-02-05 15:40:06 -070037#if !IS_ENABLED(CONFIG_HUSH_PARSER)
Simon Glass30354972014-04-10 20:01:29 -060038 /*
39 * cli_run_command can return 0 or 1 for success, so clean up
40 * its result.
41 */
42 if (cli_simple_run_command(cmd, flag) == -1)
43 return 1;
44
45 return 0;
46#else
Simon Glass87b63982014-10-07 13:59:43 -060047 int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
48
49 if (flag & CMD_FLAG_ENV)
50 hush_flags |= FLAG_CONT_ON_NEWLINE;
51 return parse_string_outer(cmd, hush_flags);
Simon Glass30354972014-04-10 20:01:29 -060052#endif
53}
54
Thomas Betker1d43bfd2014-06-05 20:07:57 +020055/*
56 * Run a command using the selected parser, and check if it is repeatable.
57 *
58 * @param cmd Command to run
59 * @param flag Execution flags (CMD_FLAG_...)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010060 * Return: 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
Thomas Betker1d43bfd2014-06-05 20:07:57 +020061 */
62int run_command_repeatable(const char *cmd, int flag)
63{
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +090064#ifndef CONFIG_HUSH_PARSER
Thomas Betker1d43bfd2014-06-05 20:07:57 +020065 return cli_simple_run_command(cmd, flag);
66#else
67 /*
68 * parse_string_outer() returns 1 for failure, so clean up
69 * its result.
70 */
71 if (parse_string_outer(cmd,
72 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
73 return -1;
74
75 return 0;
76#endif
77}
Sean Anderson19464f42020-01-10 12:32:19 -050078#else
79__weak int board_run_command(const char *cmdline)
80{
81 printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
82
83 return 1;
84}
Simon Glassf8bb6962016-03-19 02:18:38 -060085#endif /* CONFIG_CMDLINE */
Thomas Betker1d43bfd2014-06-05 20:07:57 +020086
Simon Glass30354972014-04-10 20:01:29 -060087int run_command_list(const char *cmd, int len, int flag)
88{
89 int need_buff = 1;
90 char *buff = (char *)cmd; /* cast away const */
91 int rcode = 0;
92
93 if (len == -1) {
94 len = strlen(cmd);
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +090095#ifdef CONFIG_HUSH_PARSER
Simon Glass30354972014-04-10 20:01:29 -060096 /* hush will never change our string */
97 need_buff = 0;
98#else
99 /* the built-in parser will change our string if it sees \n */
100 need_buff = strchr(cmd, '\n') != NULL;
101#endif
102 }
103 if (need_buff) {
104 buff = malloc(len + 1);
105 if (!buff)
106 return 1;
107 memcpy(buff, cmd, len);
108 buff[len] = '\0';
109 }
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900110#ifdef CONFIG_HUSH_PARSER
Simon Glass30354972014-04-10 20:01:29 -0600111 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
112#else
113 /*
114 * This function will overwrite any \n it sees with a \0, which
115 * is why it can't work with a const char *. Here we are making
116 * using of internal knowledge of this function, to avoid always
117 * doing a malloc() which is actually required only in a case that
118 * is pretty rare.
119 */
Simon Glassf8bb6962016-03-19 02:18:38 -0600120#ifdef CONFIG_CMDLINE
Simon Glass30354972014-04-10 20:01:29 -0600121 rcode = cli_simple_run_command_list(buff, flag);
Simon Glassf8bb6962016-03-19 02:18:38 -0600122#else
123 rcode = board_run_command(buff);
124#endif
Peng Fan09a78862015-12-22 17:14:13 +0800125#endif
Simon Glass30354972014-04-10 20:01:29 -0600126 if (need_buff)
127 free(buff);
Simon Glass30354972014-04-10 20:01:29 -0600128
129 return rcode;
130}
131
Simon Glass74724482022-07-13 06:06:59 -0600132int run_commandf(const char *fmt, ...)
133{
134 va_list args;
Evgeny Bachinin26f923c2023-03-20 11:23:11 +0300135 int nbytes;
Simon Glass74724482022-07-13 06:06:59 -0600136
137 va_start(args, fmt);
Evgeny Bachinin26f923c2023-03-20 11:23:11 +0300138 /*
139 * Limit the console_buffer space being used to CONFIG_SYS_CBSIZE,
140 * because its last byte is used to fit the replacement of \0 by \n\0
141 * in underlying hush parser
142 */
143 nbytes = vsnprintf(console_buffer, CONFIG_SYS_CBSIZE, fmt, args);
Simon Glass74724482022-07-13 06:06:59 -0600144 va_end(args);
145
Evgeny Bachinin26f923c2023-03-20 11:23:11 +0300146 if (nbytes < 0) {
147 pr_debug("I/O internal error occurred.\n");
148 return -EIO;
149 } else if (nbytes >= CONFIG_SYS_CBSIZE) {
150 pr_debug("'fmt' size:%d exceeds the limit(%d)\n",
151 nbytes, CONFIG_SYS_CBSIZE);
152 return -ENOSPC;
153 }
154 return run_command(console_buffer, 0);
Simon Glass74724482022-07-13 06:06:59 -0600155}
156
Simon Glass30354972014-04-10 20:01:29 -0600157/****************************************************************************/
158
159#if defined(CONFIG_CMD_RUN)
Simon Glass09140112020-05-10 11:40:03 -0600160int do_run(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glass30354972014-04-10 20:01:29 -0600161{
Marek Vasut721307e2022-12-20 07:25:59 +0100162 int i, ret;
Simon Glass30354972014-04-10 20:01:29 -0600163
164 if (argc < 2)
165 return CMD_RET_USAGE;
166
167 for (i = 1; i < argc; ++i) {
168 char *arg;
169
Simon Glass00caae62017-08-03 12:22:12 -0600170 arg = env_get(argv[i]);
Simon Glass30354972014-04-10 20:01:29 -0600171 if (arg == NULL) {
172 printf("## Error: \"%s\" not defined\n", argv[i]);
173 return 1;
174 }
175
Marek Vasut721307e2022-12-20 07:25:59 +0100176 ret = run_command(arg, flag | CMD_FLAG_ENV);
177 if (ret)
178 return ret;
Simon Glass30354972014-04-10 20:01:29 -0600179 }
180 return 0;
181}
182#endif
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600183
Masahiro Yamada0f925822015-08-12 07:31:55 +0900184#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassaffb2152014-04-10 20:01:35 -0600185bool cli_process_fdt(const char **cmdp)
186{
187 /* Allow the fdt to override the boot command */
Simon Glass7de8bd02021-08-07 07:24:01 -0600188 const char *env = ofnode_conf_read_str("bootcmd");
Simon Glassaffb2152014-04-10 20:01:35 -0600189 if (env)
190 *cmdp = env;
191 /*
192 * If the bootsecure option was chosen, use secure_boot_cmd().
193 * Always use 'env' in this case, since bootsecure requres that the
194 * bootcmd was specified in the FDT too.
195 */
Simon Glass7de8bd02021-08-07 07:24:01 -0600196 return ofnode_conf_read_int("bootsecure", 0);
Simon Glassaffb2152014-04-10 20:01:35 -0600197}
198
199/*
200 * Runs the given boot command securely. Specifically:
201 * - Doesn't run the command with the shell (run_command or parse_string_outer),
202 * since that's a lot of code surface that an attacker might exploit.
203 * Because of this, we don't do any argument parsing--the secure boot command
204 * has to be a full-fledged u-boot command.
205 * - Doesn't check for keypresses before booting, since that could be a
206 * security hole; also disables Ctrl-C.
207 * - Doesn't allow the command to return.
208 *
209 * Upon any failures, this function will drop into an infinite loop after
210 * printing the error message to console.
211 */
212void cli_secure_boot_cmd(const char *cmd)
213{
Simon Glassf8bb6962016-03-19 02:18:38 -0600214#ifdef CONFIG_CMDLINE
Simon Glass09140112020-05-10 11:40:03 -0600215 struct cmd_tbl *cmdtp;
Simon Glassf8bb6962016-03-19 02:18:38 -0600216#endif
Simon Glassaffb2152014-04-10 20:01:35 -0600217 int rc;
218
219 if (!cmd) {
220 printf("## Error: Secure boot command not specified\n");
221 goto err;
222 }
223
224 /* Disable Ctrl-C just in case some command is used that checks it. */
225 disable_ctrlc(1);
226
227 /* Find the command directly. */
Simon Glassf8bb6962016-03-19 02:18:38 -0600228#ifdef CONFIG_CMDLINE
Simon Glassaffb2152014-04-10 20:01:35 -0600229 cmdtp = find_cmd(cmd);
230 if (!cmdtp) {
231 printf("## Error: \"%s\" not defined\n", cmd);
232 goto err;
233 }
234
235 /* Run the command, forcing no flags and faking argc and argv. */
236 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
237
Simon Glassf8bb6962016-03-19 02:18:38 -0600238#else
239 rc = board_run_command(cmd);
240#endif
241
Simon Glassaffb2152014-04-10 20:01:35 -0600242 /* Shouldn't ever return from boot command. */
243 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
244
245err:
246 /*
247 * Not a whole lot to do here. Rebooting won't help much, since we'll
248 * just end up right back here. Just loop.
249 */
250 hang();
251}
Masahiro Yamada0f925822015-08-12 07:31:55 +0900252#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Simon Glassaffb2152014-04-10 20:01:35 -0600253
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600254void cli_loop(void)
255{
Heiko Schocherb07cc482019-04-12 12:37:03 +0200256 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900257#ifdef CONFIG_HUSH_PARSER
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600258 parse_file_outer();
259 /* This point is never reached */
260 for (;;);
Stefan Roese30eae262016-04-04 16:32:15 +0200261#elif defined(CONFIG_CMDLINE)
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600262 cli_simple_loop();
Simon Glassf8bb6962016-03-19 02:18:38 -0600263#else
264 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900265#endif /*CONFIG_HUSH_PARSER*/
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600266}
267
268void cli_init(void)
269{
Masahiro Yamadaf1f9d4f2016-06-21 02:11:19 +0900270#ifdef CONFIG_HUSH_PARSER
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600271 u_boot_hush_start();
272#endif
273
274#if defined(CONFIG_HUSH_INIT_VAR)
275 hush_init_var();
276#endif
277}