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 static inline bool use_hush_old(void)
31 return IS_ENABLED(CONFIG_HUSH_SELECTABLE) ?
32 gd->flags & GD_FLG_HUSH_OLD_PARSER :
33 IS_ENABLED(CONFIG_HUSH_OLD_PARSER);
37 * Run a command using the selected parser.
39 * @param cmd Command to run
40 * @param flag Execution flags (CMD_FLAG_...)
41 * Return: 0 on success, or != 0 on error.
43 int run_command(const char *cmd, int flag)
45 #if !IS_ENABLED(CONFIG_HUSH_PARSER)
47 * cli_run_command can return 0 or 1 for success, so clean up
50 if (cli_simple_run_command(cmd, flag) == -1)
56 int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
58 if (flag & CMD_FLAG_ENV)
59 hush_flags |= FLAG_CONT_ON_NEWLINE;
60 return parse_string_outer(cmd, hush_flags);
63 * Possible values for flags are the following:
64 * FLAG_EXIT_FROM_LOOP: This flags ensures we exit from loop in
65 * parse_and_run_stream() after first iteration while normal
66 * behavior, * i.e. when called from cli_loop(), is to loop
68 * FLAG_PARSE_SEMICOLON: modern Hush parses ';' and does not stop
69 * first time it sees one. So, I think we do not need this flag.
70 * FLAG_REPARSING: For the moment, I do not understand the goal
72 * FLAG_CONT_ON_NEWLINE: This flag seems to be used to continue
73 * parsing even when reading '\n' when coming from
74 * run_command(). In this case, modern Hush reads until it finds
75 * '\0'. So, I think we do not need this flag.
77 return parse_string_outer_modern(cmd, FLAG_EXIT_FROM_LOOP);
82 * Run a command using the selected parser, and check if it is repeatable.
84 * @param cmd Command to run
85 * @param flag Execution flags (CMD_FLAG_...)
86 * Return: 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
88 int run_command_repeatable(const char *cmd, int flag)
90 #ifndef CONFIG_HUSH_PARSER
91 return cli_simple_run_command(cmd, flag);
96 ret = parse_string_outer(cmd,
98 | FLAG_EXIT_FROM_LOOP);
100 ret = parse_string_outer_modern(cmd,
102 | FLAG_EXIT_FROM_LOOP);
106 * parse_string_outer() returns 1 for failure, so clean up
116 __weak int board_run_command(const char *cmdline)
118 printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
122 #endif /* CONFIG_CMDLINE */
124 int run_command_list(const char *cmd, int len, int flag)
127 char *buff = (char *)cmd; /* cast away const */
132 #ifdef CONFIG_HUSH_PARSER
133 /* hush will never change our string */
136 /* the built-in parser will change our string if it sees \n */
137 need_buff = strchr(cmd, '\n') != NULL;
141 buff = malloc(len + 1);
144 memcpy(buff, cmd, len);
147 #ifdef CONFIG_HUSH_PARSER
148 if (use_hush_old()) {
149 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
151 rcode = parse_string_outer_modern(buff, FLAG_PARSE_SEMICOLON);
155 * This function will overwrite any \n it sees with a \0, which
156 * is why it can't work with a const char *. Here we are making
157 * using of internal knowledge of this function, to avoid always
158 * doing a malloc() which is actually required only in a case that
161 #ifdef CONFIG_CMDLINE
162 rcode = cli_simple_run_command_list(buff, flag);
164 rcode = board_run_command(buff);
173 int run_commandf(const char *fmt, ...)
180 * Limit the console_buffer space being used to CONFIG_SYS_CBSIZE,
181 * because its last byte is used to fit the replacement of \0 by \n\0
182 * in underlying hush parser
184 nbytes = vsnprintf(console_buffer, CONFIG_SYS_CBSIZE, fmt, args);
188 pr_debug("I/O internal error occurred.\n");
190 } else if (nbytes >= CONFIG_SYS_CBSIZE) {
191 pr_debug("'fmt' size:%d exceeds the limit(%d)\n",
192 nbytes, CONFIG_SYS_CBSIZE);
195 return run_command(console_buffer, 0);
198 /****************************************************************************/
200 #if defined(CONFIG_CMD_RUN)
201 int do_run(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
206 return CMD_RET_USAGE;
208 for (i = 1; i < argc; ++i) {
211 arg = env_get(argv[i]);
213 printf("## Error: \"%s\" not defined\n", argv[i]);
217 ret = run_command(arg, flag | CMD_FLAG_ENV);
225 #if CONFIG_IS_ENABLED(OF_CONTROL)
226 bool cli_process_fdt(const char **cmdp)
228 /* Allow the fdt to override the boot command */
229 const char *env = ofnode_conf_read_str("bootcmd");
233 * If the bootsecure option was chosen, use secure_boot_cmd().
234 * Always use 'env' in this case, since bootsecure requres that the
235 * bootcmd was specified in the FDT too.
237 return ofnode_conf_read_int("bootsecure", 0);
241 * Runs the given boot command securely. Specifically:
242 * - Doesn't run the command with the shell (run_command or parse_string_outer),
243 * since that's a lot of code surface that an attacker might exploit.
244 * Because of this, we don't do any argument parsing--the secure boot command
245 * has to be a full-fledged u-boot command.
246 * - Doesn't check for keypresses before booting, since that could be a
247 * security hole; also disables Ctrl-C.
248 * - Doesn't allow the command to return.
250 * Upon any failures, this function will drop into an infinite loop after
251 * printing the error message to console.
253 void cli_secure_boot_cmd(const char *cmd)
255 #ifdef CONFIG_CMDLINE
256 struct cmd_tbl *cmdtp;
261 printf("## Error: Secure boot command not specified\n");
265 /* Disable Ctrl-C just in case some command is used that checks it. */
268 /* Find the command directly. */
269 #ifdef CONFIG_CMDLINE
270 cmdtp = find_cmd(cmd);
272 printf("## Error: \"%s\" not defined\n", cmd);
276 /* Run the command, forcing no flags and faking argc and argv. */
277 rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
280 rc = board_run_command(cmd);
283 /* Shouldn't ever return from boot command. */
284 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
288 * Not a whole lot to do here. Rebooting won't help much, since we'll
289 * just end up right back here. Just loop.
293 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
297 bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
298 #if CONFIG_IS_ENABLED(HUSH_PARSER)
299 if (gd->flags & GD_FLG_HUSH_MODERN_PARSER)
300 parse_and_run_file();
301 else if (gd->flags & GD_FLG_HUSH_OLD_PARSER)
305 /* This point is never reached */
307 #elif defined(CONFIG_CMDLINE)
310 printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
311 #endif /*CONFIG_HUSH_PARSER*/
316 #ifdef CONFIG_HUSH_PARSER
317 /* This if block is used to initialize hush parser gd flag. */
318 if (!(gd->flags & GD_FLG_HUSH_OLD_PARSER)
319 && !(gd->flags & GD_FLG_HUSH_MODERN_PARSER)) {
320 if (CONFIG_IS_ENABLED(HUSH_OLD_PARSER))
321 gd->flags |= GD_FLG_HUSH_OLD_PARSER;
322 else if (CONFIG_IS_ENABLED(HUSH_MODERN_PARSER))
323 gd->flags |= GD_FLG_HUSH_MODERN_PARSER;
326 if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
328 } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
329 u_boot_hush_start_modern();
331 printf("No valid hush parser to use, cli will not initialized!\n");
336 #if defined(CONFIG_HUSH_INIT_VAR)
340 if (CONFIG_IS_ENABLED(VIDEO_ANSI))
341 printf(ANSI_CURSOR_SHOW "\n");