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+
18 DECLARE_GLOBAL_DATA_PTR;
21 * Run a command using the selected parser.
23 * @param cmd Command to run
24 * @param flag Execution flags (CMD_FLAG_...)
25 * @return 0 on success, or != 0 on error.
27 int run_command(const char *cmd, int flag)
29 #ifndef CONFIG_SYS_HUSH_PARSER
31 * cli_run_command can return 0 or 1 for success, so clean up
34 if (cli_simple_run_command(cmd, flag) == -1)
39 return parse_string_outer(cmd,
40 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
44 int run_command_list(const char *cmd, int len, int flag)
47 char *buff = (char *)cmd; /* cast away const */
52 #ifdef CONFIG_SYS_HUSH_PARSER
53 /* hush will never change our string */
56 /* the built-in parser will change our string if it sees \n */
57 need_buff = strchr(cmd, '\n') != NULL;
61 buff = malloc(len + 1);
64 memcpy(buff, cmd, len);
67 #ifdef CONFIG_SYS_HUSH_PARSER
68 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
71 * This function will overwrite any \n it sees with a \0, which
72 * is why it can't work with a const char *. Here we are making
73 * using of internal knowledge of this function, to avoid always
74 * doing a malloc() which is actually required only in a case that
77 rcode = cli_simple_run_command_list(buff, flag);
85 /****************************************************************************/
87 #if defined(CONFIG_CMD_RUN)
88 int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
95 for (i = 1; i < argc; ++i) {
98 arg = getenv(argv[i]);
100 printf("## Error: \"%s\" not defined\n", argv[i]);
104 if (run_command(arg, flag) != 0)
111 #ifdef CONFIG_OF_CONTROL
112 bool cli_process_fdt(const char **cmdp)
114 /* Allow the fdt to override the boot command */
115 char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
119 * If the bootsecure option was chosen, use secure_boot_cmd().
120 * Always use 'env' in this case, since bootsecure requres that the
121 * bootcmd was specified in the FDT too.
123 return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
127 * Runs the given boot command securely. Specifically:
128 * - Doesn't run the command with the shell (run_command or parse_string_outer),
129 * since that's a lot of code surface that an attacker might exploit.
130 * Because of this, we don't do any argument parsing--the secure boot command
131 * has to be a full-fledged u-boot command.
132 * - Doesn't check for keypresses before booting, since that could be a
133 * security hole; also disables Ctrl-C.
134 * - Doesn't allow the command to return.
136 * Upon any failures, this function will drop into an infinite loop after
137 * printing the error message to console.
139 void cli_secure_boot_cmd(const char *cmd)
145 printf("## Error: Secure boot command not specified\n");
149 /* Disable Ctrl-C just in case some command is used that checks it. */
152 /* Find the command directly. */
153 cmdtp = find_cmd(cmd);
155 printf("## Error: \"%s\" not defined\n", cmd);
159 /* Run the command, forcing no flags and faking argc and argv. */
160 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
162 /* Shouldn't ever return from boot command. */
163 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
167 * Not a whole lot to do here. Rebooting won't help much, since we'll
168 * just end up right back here. Just loop.
172 #endif /* CONFIG_OF_CONTROL */
176 #ifdef CONFIG_SYS_HUSH_PARSER
178 /* This point is never reached */
182 #endif /*CONFIG_SYS_HUSH_PARSER*/
187 #ifdef CONFIG_SYS_HUSH_PARSER
191 #if defined(CONFIG_HUSH_INIT_VAR)