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))
75 __weak int board_run_command(const char *cmdline)
77 printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
81 #endif /* CONFIG_CMDLINE */
83 int run_command_list(const char *cmd, int len, int flag)
86 char *buff = (char *)cmd; /* cast away const */
91 #ifdef CONFIG_HUSH_PARSER
92 /* hush will never change our string */
95 /* the built-in parser will change our string if it sees \n */
96 need_buff = strchr(cmd, '\n') != NULL;
100 buff = malloc(len + 1);
103 memcpy(buff, cmd, len);
106 #ifdef CONFIG_HUSH_PARSER
107 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
110 * This function will overwrite any \n it sees with a \0, which
111 * is why it can't work with a const char *. Here we are making
112 * using of internal knowledge of this function, to avoid always
113 * doing a malloc() which is actually required only in a case that
116 #ifdef CONFIG_CMDLINE
117 rcode = cli_simple_run_command_list(buff, flag);
119 rcode = board_run_command(buff);
128 /****************************************************************************/
130 #if defined(CONFIG_CMD_RUN)
131 int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
136 return CMD_RET_USAGE;
138 for (i = 1; i < argc; ++i) {
141 arg = env_get(argv[i]);
143 printf("## Error: \"%s\" not defined\n", argv[i]);
147 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
154 #if CONFIG_IS_ENABLED(OF_CONTROL)
155 bool cli_process_fdt(const char **cmdp)
157 /* Allow the fdt to override the boot command */
158 char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
162 * If the bootsecure option was chosen, use secure_boot_cmd().
163 * Always use 'env' in this case, since bootsecure requres that the
164 * bootcmd was specified in the FDT too.
166 return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
170 * Runs the given boot command securely. Specifically:
171 * - Doesn't run the command with the shell (run_command or parse_string_outer),
172 * since that's a lot of code surface that an attacker might exploit.
173 * Because of this, we don't do any argument parsing--the secure boot command
174 * has to be a full-fledged u-boot command.
175 * - Doesn't check for keypresses before booting, since that could be a
176 * security hole; also disables Ctrl-C.
177 * - Doesn't allow the command to return.
179 * Upon any failures, this function will drop into an infinite loop after
180 * printing the error message to console.
182 void cli_secure_boot_cmd(const char *cmd)
184 #ifdef CONFIG_CMDLINE
190 printf("## Error: Secure boot command not specified\n");
194 /* Disable Ctrl-C just in case some command is used that checks it. */
197 /* Find the command directly. */
198 #ifdef CONFIG_CMDLINE
199 cmdtp = find_cmd(cmd);
201 printf("## Error: \"%s\" not defined\n", cmd);
205 /* Run the command, forcing no flags and faking argc and argv. */
206 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
209 rc = board_run_command(cmd);
212 /* Shouldn't ever return from boot command. */
213 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
217 * Not a whole lot to do here. Rebooting won't help much, since we'll
218 * just end up right back here. Just loop.
222 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
226 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
227 #ifdef CONFIG_HUSH_PARSER
229 /* This point is never reached */
231 #elif defined(CONFIG_CMDLINE)
234 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
235 #endif /*CONFIG_HUSH_PARSER*/
240 #ifdef CONFIG_HUSH_PARSER
244 #if defined(CONFIG_HUSH_INIT_VAR)