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>
23 DECLARE_GLOBAL_DATA_PTR;
27 * Run a command using the selected parser.
29 * @param cmd Command to run
30 * @param flag Execution flags (CMD_FLAG_...)
31 * @return 0 on success, or != 0 on error.
33 int run_command(const char *cmd, int flag)
35 #if !CONFIG_IS_ENABLED(HUSH_PARSER)
37 * cli_run_command can return 0 or 1 for success, so clean up
40 if (cli_simple_run_command(cmd, flag) == -1)
45 int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
47 if (flag & CMD_FLAG_ENV)
48 hush_flags |= FLAG_CONT_ON_NEWLINE;
49 return parse_string_outer(cmd, hush_flags);
54 * Run a command using the selected parser, and check if it is repeatable.
56 * @param cmd Command to run
57 * @param flag Execution flags (CMD_FLAG_...)
58 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
60 int run_command_repeatable(const char *cmd, int flag)
62 #ifndef CONFIG_HUSH_PARSER
63 return cli_simple_run_command(cmd, flag);
66 * parse_string_outer() returns 1 for failure, so clean up
69 if (parse_string_outer(cmd,
70 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
77 __weak int board_run_command(const char *cmdline)
79 printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
83 #endif /* CONFIG_CMDLINE */
85 int run_command_list(const char *cmd, int len, int flag)
88 char *buff = (char *)cmd; /* cast away const */
93 #ifdef CONFIG_HUSH_PARSER
94 /* hush will never change our string */
97 /* the built-in parser will change our string if it sees \n */
98 need_buff = strchr(cmd, '\n') != NULL;
102 buff = malloc(len + 1);
105 memcpy(buff, cmd, len);
108 #ifdef CONFIG_HUSH_PARSER
109 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
112 * This function will overwrite any \n it sees with a \0, which
113 * is why it can't work with a const char *. Here we are making
114 * using of internal knowledge of this function, to avoid always
115 * doing a malloc() which is actually required only in a case that
118 #ifdef CONFIG_CMDLINE
119 rcode = cli_simple_run_command_list(buff, flag);
121 rcode = board_run_command(buff);
130 /****************************************************************************/
132 #if defined(CONFIG_CMD_RUN)
133 int do_run(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
138 return CMD_RET_USAGE;
140 for (i = 1; i < argc; ++i) {
143 arg = env_get(argv[i]);
145 printf("## Error: \"%s\" not defined\n", argv[i]);
149 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
156 #if CONFIG_IS_ENABLED(OF_CONTROL)
157 bool cli_process_fdt(const char **cmdp)
159 /* Allow the fdt to override the boot command */
160 char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
164 * If the bootsecure option was chosen, use secure_boot_cmd().
165 * Always use 'env' in this case, since bootsecure requres that the
166 * bootcmd was specified in the FDT too.
168 return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
172 * Runs the given boot command securely. Specifically:
173 * - Doesn't run the command with the shell (run_command or parse_string_outer),
174 * since that's a lot of code surface that an attacker might exploit.
175 * Because of this, we don't do any argument parsing--the secure boot command
176 * has to be a full-fledged u-boot command.
177 * - Doesn't check for keypresses before booting, since that could be a
178 * security hole; also disables Ctrl-C.
179 * - Doesn't allow the command to return.
181 * Upon any failures, this function will drop into an infinite loop after
182 * printing the error message to console.
184 void cli_secure_boot_cmd(const char *cmd)
186 #ifdef CONFIG_CMDLINE
187 struct cmd_tbl *cmdtp;
192 printf("## Error: Secure boot command not specified\n");
196 /* Disable Ctrl-C just in case some command is used that checks it. */
199 /* Find the command directly. */
200 #ifdef CONFIG_CMDLINE
201 cmdtp = find_cmd(cmd);
203 printf("## Error: \"%s\" not defined\n", cmd);
207 /* Run the command, forcing no flags and faking argc and argv. */
208 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
211 rc = board_run_command(cmd);
214 /* Shouldn't ever return from boot command. */
215 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
219 * Not a whole lot to do here. Rebooting won't help much, since we'll
220 * just end up right back here. Just loop.
224 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
228 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
229 #ifdef CONFIG_HUSH_PARSER
231 /* This point is never reached */
233 #elif defined(CONFIG_CMDLINE)
236 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
237 #endif /*CONFIG_HUSH_PARSER*/
242 #ifdef CONFIG_HUSH_PARSER
246 #if defined(CONFIG_HUSH_INIT_VAR)