1 // SPDX-License-Identifier: GPL-2.0+
6 * Add to readline cmdline-editing by
11 #define pr_fmt(fmt) "cli: %s: " fmt, __func__
14 #include <bootstage.h>
23 #include <asm/global_data.h>
24 #include <dm/ofnode.h>
25 #include <linux/errno.h>
29 * Run a command using the selected parser.
31 * @param cmd Command to run
32 * @param flag Execution flags (CMD_FLAG_...)
33 * Return: 0 on success, or != 0 on error.
35 int run_command(const char *cmd, int flag)
37 #if !IS_ENABLED(CONFIG_HUSH_PARSER)
39 * cli_run_command can return 0 or 1 for success, so clean up
42 if (cli_simple_run_command(cmd, flag) == -1)
47 int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
49 if (flag & CMD_FLAG_ENV)
50 hush_flags |= FLAG_CONT_ON_NEWLINE;
51 return parse_string_outer(cmd, hush_flags);
56 * Run a command using the selected parser, and check if it is repeatable.
58 * @param cmd Command to run
59 * @param flag Execution flags (CMD_FLAG_...)
60 * Return: 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
62 int run_command_repeatable(const char *cmd, int flag)
64 #ifndef CONFIG_HUSH_PARSER
65 return cli_simple_run_command(cmd, flag);
68 * parse_string_outer() returns 1 for failure, so clean up
71 if (parse_string_outer(cmd,
72 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
79 __weak int board_run_command(const char *cmdline)
81 printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
85 #endif /* CONFIG_CMDLINE */
87 int run_command_list(const char *cmd, int len, int flag)
90 char *buff = (char *)cmd; /* cast away const */
95 #ifdef CONFIG_HUSH_PARSER
96 /* hush will never change our string */
99 /* the built-in parser will change our string if it sees \n */
100 need_buff = strchr(cmd, '\n') != NULL;
104 buff = malloc(len + 1);
107 memcpy(buff, cmd, len);
110 #ifdef CONFIG_HUSH_PARSER
111 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
114 * This function will overwrite any \n it sees with a \0, which
115 * is why it can't work with a const char *. Here we are making
116 * using of internal knowledge of this function, to avoid always
117 * doing a malloc() which is actually required only in a case that
120 #ifdef CONFIG_CMDLINE
121 rcode = cli_simple_run_command_list(buff, flag);
123 rcode = board_run_command(buff);
132 int run_commandf(const char *fmt, ...)
139 * Limit the console_buffer space being used to CONFIG_SYS_CBSIZE,
140 * because its last byte is used to fit the replacement of \0 by \n\0
141 * in underlying hush parser
143 nbytes = vsnprintf(console_buffer, CONFIG_SYS_CBSIZE, fmt, args);
147 pr_debug("I/O internal error occurred.\n");
149 } else if (nbytes >= CONFIG_SYS_CBSIZE) {
150 pr_debug("'fmt' size:%d exceeds the limit(%d)\n",
151 nbytes, CONFIG_SYS_CBSIZE);
154 return run_command(console_buffer, 0);
157 /****************************************************************************/
159 #if defined(CONFIG_CMD_RUN)
160 int do_run(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
165 return CMD_RET_USAGE;
167 for (i = 1; i < argc; ++i) {
170 arg = env_get(argv[i]);
172 printf("## Error: \"%s\" not defined\n", argv[i]);
176 ret = run_command(arg, flag | CMD_FLAG_ENV);
184 #if CONFIG_IS_ENABLED(OF_CONTROL)
185 bool cli_process_fdt(const char **cmdp)
187 /* Allow the fdt to override the boot command */
188 const char *env = ofnode_conf_read_str("bootcmd");
192 * If the bootsecure option was chosen, use secure_boot_cmd().
193 * Always use 'env' in this case, since bootsecure requres that the
194 * bootcmd was specified in the FDT too.
196 return ofnode_conf_read_int("bootsecure", 0);
200 * Runs the given boot command securely. Specifically:
201 * - Doesn't run the command with the shell (run_command or parse_string_outer),
202 * since that's a lot of code surface that an attacker might exploit.
203 * Because of this, we don't do any argument parsing--the secure boot command
204 * has to be a full-fledged u-boot command.
205 * - Doesn't check for keypresses before booting, since that could be a
206 * security hole; also disables Ctrl-C.
207 * - Doesn't allow the command to return.
209 * Upon any failures, this function will drop into an infinite loop after
210 * printing the error message to console.
212 void cli_secure_boot_cmd(const char *cmd)
214 #ifdef CONFIG_CMDLINE
215 struct cmd_tbl *cmdtp;
220 printf("## Error: Secure boot command not specified\n");
224 /* Disable Ctrl-C just in case some command is used that checks it. */
227 /* Find the command directly. */
228 #ifdef CONFIG_CMDLINE
229 cmdtp = find_cmd(cmd);
231 printf("## Error: \"%s\" not defined\n", cmd);
235 /* Run the command, forcing no flags and faking argc and argv. */
236 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
239 rc = board_run_command(cmd);
242 /* Shouldn't ever return from boot command. */
243 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
247 * Not a whole lot to do here. Rebooting won't help much, since we'll
248 * just end up right back here. Just loop.
252 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
256 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
257 #ifdef CONFIG_HUSH_PARSER
259 /* This point is never reached */
261 #elif defined(CONFIG_CMDLINE)
264 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
265 #endif /*CONFIG_HUSH_PARSER*/
270 #ifdef CONFIG_HUSH_PARSER
274 #if defined(CONFIG_HUSH_INIT_VAR)