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>
21 DECLARE_GLOBAL_DATA_PTR;
25 * Run a command using the selected parser.
27 * @param cmd Command to run
28 * @param flag Execution flags (CMD_FLAG_...)
29 * @return 0 on success, or != 0 on error.
31 int run_command(const char *cmd, int flag)
33 #if !CONFIG_IS_ENABLED(HUSH_PARSER)
35 * cli_run_command can return 0 or 1 for success, so clean up
38 if (cli_simple_run_command(cmd, flag) == -1)
43 int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
45 if (flag & CMD_FLAG_ENV)
46 hush_flags |= FLAG_CONT_ON_NEWLINE;
47 return parse_string_outer(cmd, hush_flags);
52 * Run a command using the selected parser, and check if it is repeatable.
54 * @param cmd Command to run
55 * @param flag Execution flags (CMD_FLAG_...)
56 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
58 int run_command_repeatable(const char *cmd, int flag)
60 #ifndef CONFIG_HUSH_PARSER
61 return cli_simple_run_command(cmd, flag);
64 * parse_string_outer() returns 1 for failure, so clean up
67 if (parse_string_outer(cmd,
68 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
74 #endif /* CONFIG_CMDLINE */
76 int run_command_list(const char *cmd, int len, int flag)
79 char *buff = (char *)cmd; /* cast away const */
84 #ifdef CONFIG_HUSH_PARSER
85 /* hush will never change our string */
88 /* the built-in parser will change our string if it sees \n */
89 need_buff = strchr(cmd, '\n') != NULL;
93 buff = malloc(len + 1);
96 memcpy(buff, cmd, len);
99 #ifdef CONFIG_HUSH_PARSER
100 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
103 * This function will overwrite any \n it sees with a \0, which
104 * is why it can't work with a const char *. Here we are making
105 * using of internal knowledge of this function, to avoid always
106 * doing a malloc() which is actually required only in a case that
109 #ifdef CONFIG_CMDLINE
110 rcode = cli_simple_run_command_list(buff, flag);
112 rcode = board_run_command(buff);
121 /****************************************************************************/
123 #if defined(CONFIG_CMD_RUN)
124 int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
129 return CMD_RET_USAGE;
131 for (i = 1; i < argc; ++i) {
134 arg = env_get(argv[i]);
136 printf("## Error: \"%s\" not defined\n", argv[i]);
140 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
147 #if CONFIG_IS_ENABLED(OF_CONTROL)
148 bool cli_process_fdt(const char **cmdp)
150 /* Allow the fdt to override the boot command */
151 char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
155 * If the bootsecure option was chosen, use secure_boot_cmd().
156 * Always use 'env' in this case, since bootsecure requres that the
157 * bootcmd was specified in the FDT too.
159 return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
163 * Runs the given boot command securely. Specifically:
164 * - Doesn't run the command with the shell (run_command or parse_string_outer),
165 * since that's a lot of code surface that an attacker might exploit.
166 * Because of this, we don't do any argument parsing--the secure boot command
167 * has to be a full-fledged u-boot command.
168 * - Doesn't check for keypresses before booting, since that could be a
169 * security hole; also disables Ctrl-C.
170 * - Doesn't allow the command to return.
172 * Upon any failures, this function will drop into an infinite loop after
173 * printing the error message to console.
175 void cli_secure_boot_cmd(const char *cmd)
177 #ifdef CONFIG_CMDLINE
183 printf("## Error: Secure boot command not specified\n");
187 /* Disable Ctrl-C just in case some command is used that checks it. */
190 /* Find the command directly. */
191 #ifdef CONFIG_CMDLINE
192 cmdtp = find_cmd(cmd);
194 printf("## Error: \"%s\" not defined\n", cmd);
198 /* Run the command, forcing no flags and faking argc and argv. */
199 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
202 rc = board_run_command(cmd);
205 /* Shouldn't ever return from boot command. */
206 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
210 * Not a whole lot to do here. Rebooting won't help much, since we'll
211 * just end up right back here. Just loop.
215 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
219 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
220 #ifdef CONFIG_HUSH_PARSER
222 /* This point is never reached */
224 #elif defined(CONFIG_CMDLINE)
227 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
228 #endif /*CONFIG_HUSH_PARSER*/
233 #ifdef CONFIG_HUSH_PARSER
237 #if defined(CONFIG_HUSH_INIT_VAR)