3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Add to readline cmdline-editing by
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
9 * SPDX-License-Identifier: GPL-2.0+
19 DECLARE_GLOBAL_DATA_PTR;
23 * Run a command using the selected parser.
25 * @param cmd Command to run
26 * @param flag Execution flags (CMD_FLAG_...)
27 * @return 0 on success, or != 0 on error.
29 int run_command(const char *cmd, int flag)
31 #ifndef CONFIG_SYS_HUSH_PARSER
33 * cli_run_command can return 0 or 1 for success, so clean up
36 if (cli_simple_run_command(cmd, flag) == -1)
41 int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
43 if (flag & CMD_FLAG_ENV)
44 hush_flags |= FLAG_CONT_ON_NEWLINE;
45 return parse_string_outer(cmd, hush_flags);
50 * Run a command using the selected parser, and check if it is repeatable.
52 * @param cmd Command to run
53 * @param flag Execution flags (CMD_FLAG_...)
54 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
56 int run_command_repeatable(const char *cmd, int flag)
58 #ifndef CONFIG_SYS_HUSH_PARSER
59 return cli_simple_run_command(cmd, flag);
62 * parse_string_outer() returns 1 for failure, so clean up
65 if (parse_string_outer(cmd,
66 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
72 #endif /* CONFIG_CMDLINE */
74 int run_command_list(const char *cmd, int len, int flag)
77 char *buff = (char *)cmd; /* cast away const */
82 #ifdef CONFIG_SYS_HUSH_PARSER
83 /* hush will never change our string */
86 /* the built-in parser will change our string if it sees \n */
87 need_buff = strchr(cmd, '\n') != NULL;
91 buff = malloc(len + 1);
94 memcpy(buff, cmd, len);
97 #ifdef CONFIG_SYS_HUSH_PARSER
98 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
101 * This function will overwrite any \n it sees with a \0, which
102 * is why it can't work with a const char *. Here we are making
103 * using of internal knowledge of this function, to avoid always
104 * doing a malloc() which is actually required only in a case that
107 #ifdef CONFIG_CMDLINE
108 rcode = cli_simple_run_command_list(buff, flag);
110 rcode = board_run_command(buff);
119 /****************************************************************************/
121 #if defined(CONFIG_CMD_RUN)
122 int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
127 return CMD_RET_USAGE;
129 for (i = 1; i < argc; ++i) {
132 arg = getenv(argv[i]);
134 printf("## Error: \"%s\" not defined\n", argv[i]);
138 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
145 #if CONFIG_IS_ENABLED(OF_CONTROL)
146 bool cli_process_fdt(const char **cmdp)
148 /* Allow the fdt to override the boot command */
149 char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
153 * If the bootsecure option was chosen, use secure_boot_cmd().
154 * Always use 'env' in this case, since bootsecure requres that the
155 * bootcmd was specified in the FDT too.
157 return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
161 * Runs the given boot command securely. Specifically:
162 * - Doesn't run the command with the shell (run_command or parse_string_outer),
163 * since that's a lot of code surface that an attacker might exploit.
164 * Because of this, we don't do any argument parsing--the secure boot command
165 * has to be a full-fledged u-boot command.
166 * - Doesn't check for keypresses before booting, since that could be a
167 * security hole; also disables Ctrl-C.
168 * - Doesn't allow the command to return.
170 * Upon any failures, this function will drop into an infinite loop after
171 * printing the error message to console.
173 void cli_secure_boot_cmd(const char *cmd)
175 #ifdef CONFIG_CMDLINE
181 printf("## Error: Secure boot command not specified\n");
185 /* Disable Ctrl-C just in case some command is used that checks it. */
188 /* Find the command directly. */
189 #ifdef CONFIG_CMDLINE
190 cmdtp = find_cmd(cmd);
192 printf("## Error: \"%s\" not defined\n", cmd);
196 /* Run the command, forcing no flags and faking argc and argv. */
197 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
200 rc = board_run_command(cmd);
203 /* Shouldn't ever return from boot command. */
204 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
208 * Not a whole lot to do here. Rebooting won't help much, since we'll
209 * just end up right back here. Just loop.
213 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
217 #ifdef CONFIG_SYS_HUSH_PARSER
219 /* This point is never reached */
221 #elif defined(CONFIG_CMDLINE)
224 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
225 #endif /*CONFIG_SYS_HUSH_PARSER*/
230 #ifdef CONFIG_SYS_HUSH_PARSER
234 #if defined(CONFIG_HUSH_INIT_VAR)