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>
20 DECLARE_GLOBAL_DATA_PTR;
24 * Run a command using the selected parser.
26 * @param cmd Command to run
27 * @param flag Execution flags (CMD_FLAG_...)
28 * @return 0 on success, or != 0 on error.
30 int run_command(const char *cmd, int flag)
32 #if !CONFIG_IS_ENABLED(HUSH_PARSER)
34 * cli_run_command can return 0 or 1 for success, so clean up
37 if (cli_simple_run_command(cmd, flag) == -1)
42 int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
44 if (flag & CMD_FLAG_ENV)
45 hush_flags |= FLAG_CONT_ON_NEWLINE;
46 return parse_string_outer(cmd, hush_flags);
51 * Run a command using the selected parser, and check if it is repeatable.
53 * @param cmd Command to run
54 * @param flag Execution flags (CMD_FLAG_...)
55 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
57 int run_command_repeatable(const char *cmd, int flag)
59 #ifndef CONFIG_HUSH_PARSER
60 return cli_simple_run_command(cmd, flag);
63 * parse_string_outer() returns 1 for failure, so clean up
66 if (parse_string_outer(cmd,
67 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
73 #endif /* CONFIG_CMDLINE */
75 int run_command_list(const char *cmd, int len, int flag)
78 char *buff = (char *)cmd; /* cast away const */
83 #ifdef CONFIG_HUSH_PARSER
84 /* hush will never change our string */
87 /* the built-in parser will change our string if it sees \n */
88 need_buff = strchr(cmd, '\n') != NULL;
92 buff = malloc(len + 1);
95 memcpy(buff, cmd, len);
98 #ifdef CONFIG_HUSH_PARSER
99 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
102 * This function will overwrite any \n it sees with a \0, which
103 * is why it can't work with a const char *. Here we are making
104 * using of internal knowledge of this function, to avoid always
105 * doing a malloc() which is actually required only in a case that
108 #ifdef CONFIG_CMDLINE
109 rcode = cli_simple_run_command_list(buff, flag);
111 rcode = board_run_command(buff);
120 /****************************************************************************/
122 #if defined(CONFIG_CMD_RUN)
123 int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
128 return CMD_RET_USAGE;
130 for (i = 1; i < argc; ++i) {
133 arg = env_get(argv[i]);
135 printf("## Error: \"%s\" not defined\n", argv[i]);
139 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
146 #if CONFIG_IS_ENABLED(OF_CONTROL)
147 bool cli_process_fdt(const char **cmdp)
149 /* Allow the fdt to override the boot command */
150 char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
154 * If the bootsecure option was chosen, use secure_boot_cmd().
155 * Always use 'env' in this case, since bootsecure requres that the
156 * bootcmd was specified in the FDT too.
158 return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
162 * Runs the given boot command securely. Specifically:
163 * - Doesn't run the command with the shell (run_command or parse_string_outer),
164 * since that's a lot of code surface that an attacker might exploit.
165 * Because of this, we don't do any argument parsing--the secure boot command
166 * has to be a full-fledged u-boot command.
167 * - Doesn't check for keypresses before booting, since that could be a
168 * security hole; also disables Ctrl-C.
169 * - Doesn't allow the command to return.
171 * Upon any failures, this function will drop into an infinite loop after
172 * printing the error message to console.
174 void cli_secure_boot_cmd(const char *cmd)
176 #ifdef CONFIG_CMDLINE
182 printf("## Error: Secure boot command not specified\n");
186 /* Disable Ctrl-C just in case some command is used that checks it. */
189 /* Find the command directly. */
190 #ifdef CONFIG_CMDLINE
191 cmdtp = find_cmd(cmd);
193 printf("## Error: \"%s\" not defined\n", cmd);
197 /* Run the command, forcing no flags and faking argc and argv. */
198 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
201 rc = board_run_command(cmd);
204 /* Shouldn't ever return from boot command. */
205 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
209 * Not a whole lot to do here. Rebooting won't help much, since we'll
210 * just end up right back here. Just loop.
214 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
218 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
219 #ifdef CONFIG_HUSH_PARSER
221 /* This point is never reached */
223 #elif defined(CONFIG_CMDLINE)
226 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
227 #endif /*CONFIG_HUSH_PARSER*/
232 #ifdef CONFIG_HUSH_PARSER
236 #if defined(CONFIG_HUSH_INIT_VAR)