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 int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
41 if (flag & CMD_FLAG_ENV)
42 hush_flags |= FLAG_CONT_ON_NEWLINE;
43 return parse_string_outer(cmd, hush_flags);
48 * Run a command using the selected parser, and check if it is repeatable.
50 * @param cmd Command to run
51 * @param flag Execution flags (CMD_FLAG_...)
52 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
54 int run_command_repeatable(const char *cmd, int flag)
56 #ifndef CONFIG_SYS_HUSH_PARSER
57 return cli_simple_run_command(cmd, flag);
60 * parse_string_outer() returns 1 for failure, so clean up
63 if (parse_string_outer(cmd,
64 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
71 int run_command_list(const char *cmd, int len, int flag)
74 char *buff = (char *)cmd; /* cast away const */
79 #ifdef CONFIG_SYS_HUSH_PARSER
80 /* hush will never change our string */
83 /* the built-in parser will change our string if it sees \n */
84 need_buff = strchr(cmd, '\n') != NULL;
88 buff = malloc(len + 1);
91 memcpy(buff, cmd, len);
94 #ifdef CONFIG_SYS_HUSH_PARSER
95 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
98 * This function will overwrite any \n it sees with a \0, which
99 * is why it can't work with a const char *. Here we are making
100 * using of internal knowledge of this function, to avoid always
101 * doing a malloc() which is actually required only in a case that
104 rcode = cli_simple_run_command_list(buff, flag);
112 /****************************************************************************/
114 #if defined(CONFIG_CMD_RUN)
115 int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
120 return CMD_RET_USAGE;
122 for (i = 1; i < argc; ++i) {
125 arg = getenv(argv[i]);
127 printf("## Error: \"%s\" not defined\n", argv[i]);
131 if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
138 #ifdef CONFIG_OF_CONTROL
139 bool cli_process_fdt(const char **cmdp)
141 /* Allow the fdt to override the boot command */
142 char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
146 * If the bootsecure option was chosen, use secure_boot_cmd().
147 * Always use 'env' in this case, since bootsecure requres that the
148 * bootcmd was specified in the FDT too.
150 return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
154 * Runs the given boot command securely. Specifically:
155 * - Doesn't run the command with the shell (run_command or parse_string_outer),
156 * since that's a lot of code surface that an attacker might exploit.
157 * Because of this, we don't do any argument parsing--the secure boot command
158 * has to be a full-fledged u-boot command.
159 * - Doesn't check for keypresses before booting, since that could be a
160 * security hole; also disables Ctrl-C.
161 * - Doesn't allow the command to return.
163 * Upon any failures, this function will drop into an infinite loop after
164 * printing the error message to console.
166 void cli_secure_boot_cmd(const char *cmd)
172 printf("## Error: Secure boot command not specified\n");
176 /* Disable Ctrl-C just in case some command is used that checks it. */
179 /* Find the command directly. */
180 cmdtp = find_cmd(cmd);
182 printf("## Error: \"%s\" not defined\n", cmd);
186 /* Run the command, forcing no flags and faking argc and argv. */
187 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
189 /* Shouldn't ever return from boot command. */
190 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
194 * Not a whole lot to do here. Rebooting won't help much, since we'll
195 * just end up right back here. Just loop.
199 #endif /* CONFIG_OF_CONTROL */
203 #ifdef CONFIG_SYS_HUSH_PARSER
205 /* This point is never reached */
209 #endif /*CONFIG_SYS_HUSH_PARSER*/
214 #ifdef CONFIG_SYS_HUSH_PARSER
218 #if defined(CONFIG_HUSH_INIT_VAR)