1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Add to readline cmdline-editing by
8 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
12 #include <bootstage.h>
21 #include <asm/global_data.h>
22 #include <dm/ofnode.h>
26 * Run a command using the selected parser.
28 * @param cmd Command to run
29 * @param flag Execution flags (CMD_FLAG_...)
30 * Return: 0 on success, or != 0 on error.
32 int run_command(const char *cmd, int flag)
34 #if !CONFIG_IS_ENABLED(HUSH_PARSER)
36 * cli_run_command can return 0 or 1 for success, so clean up
39 if (cli_simple_run_command(cmd, flag) == -1)
44 int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
46 if (flag & CMD_FLAG_ENV)
47 hush_flags |= FLAG_CONT_ON_NEWLINE;
48 return parse_string_outer(cmd, hush_flags);
53 * Run a command using the selected parser, and check if it is repeatable.
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.
59 int run_command_repeatable(const char *cmd, int flag)
61 #ifndef CONFIG_HUSH_PARSER
62 return cli_simple_run_command(cmd, flag);
65 * parse_string_outer() returns 1 for failure, so clean up
68 if (parse_string_outer(cmd,
69 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
76 __weak int board_run_command(const char *cmdline)
78 printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
82 #endif /* CONFIG_CMDLINE */
84 int run_command_list(const char *cmd, int len, int flag)
87 char *buff = (char *)cmd; /* cast away const */
92 #ifdef CONFIG_HUSH_PARSER
93 /* hush will never change our string */
96 /* the built-in parser will change our string if it sees \n */
97 need_buff = strchr(cmd, '\n') != NULL;
101 buff = malloc(len + 1);
104 memcpy(buff, cmd, len);
107 #ifdef CONFIG_HUSH_PARSER
108 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
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
117 #ifdef CONFIG_CMDLINE
118 rcode = cli_simple_run_command_list(buff, flag);
120 rcode = board_run_command(buff);
129 int run_commandf(const char *fmt, ...)
136 i = vsnprintf(cmd, sizeof(cmd), fmt, args);
139 ret = run_command(cmd, 0);
144 /****************************************************************************/
146 #if defined(CONFIG_CMD_RUN)
147 int do_run(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
152 return CMD_RET_USAGE;
154 for (i = 1; i < argc; ++i) {
157 arg = env_get(argv[i]);
159 printf("## Error: \"%s\" not defined\n", argv[i]);
163 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
170 #if CONFIG_IS_ENABLED(OF_CONTROL)
171 bool cli_process_fdt(const char **cmdp)
173 /* Allow the fdt to override the boot command */
174 const char *env = ofnode_conf_read_str("bootcmd");
178 * If the bootsecure option was chosen, use secure_boot_cmd().
179 * Always use 'env' in this case, since bootsecure requres that the
180 * bootcmd was specified in the FDT too.
182 return ofnode_conf_read_int("bootsecure", 0);
186 * Runs the given boot command securely. Specifically:
187 * - Doesn't run the command with the shell (run_command or parse_string_outer),
188 * since that's a lot of code surface that an attacker might exploit.
189 * Because of this, we don't do any argument parsing--the secure boot command
190 * has to be a full-fledged u-boot command.
191 * - Doesn't check for keypresses before booting, since that could be a
192 * security hole; also disables Ctrl-C.
193 * - Doesn't allow the command to return.
195 * Upon any failures, this function will drop into an infinite loop after
196 * printing the error message to console.
198 void cli_secure_boot_cmd(const char *cmd)
200 #ifdef CONFIG_CMDLINE
201 struct cmd_tbl *cmdtp;
206 printf("## Error: Secure boot command not specified\n");
210 /* Disable Ctrl-C just in case some command is used that checks it. */
213 /* Find the command directly. */
214 #ifdef CONFIG_CMDLINE
215 cmdtp = find_cmd(cmd);
217 printf("## Error: \"%s\" not defined\n", cmd);
221 /* Run the command, forcing no flags and faking argc and argv. */
222 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
225 rc = board_run_command(cmd);
228 /* Shouldn't ever return from boot command. */
229 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
233 * Not a whole lot to do here. Rebooting won't help much, since we'll
234 * just end up right back here. Just loop.
238 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
242 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
243 #ifdef CONFIG_HUSH_PARSER
245 /* This point is never reached */
247 #elif defined(CONFIG_CMDLINE)
250 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
251 #endif /*CONFIG_HUSH_PARSER*/
256 #ifdef CONFIG_HUSH_PARSER
260 #if defined(CONFIG_HUSH_INIT_VAR)