]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/cli.c
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
);
45 * Run a command using the selected parser, and check if it is repeatable.
47 * @param cmd Command to run
48 * @param flag Execution flags (CMD_FLAG_...)
49 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
51 int run_command_repeatable(const char *cmd
, int flag
)
53 #ifndef CONFIG_SYS_HUSH_PARSER
54 return cli_simple_run_command(cmd
, flag
);
57 * parse_string_outer() returns 1 for failure, so clean up
60 if (parse_string_outer(cmd
,
61 FLAG_PARSE_SEMICOLON
| FLAG_EXIT_FROM_LOOP
))
68 int run_command_list(const char *cmd
, int len
, int flag
)
71 char *buff
= (char *)cmd
; /* cast away const */
76 #ifdef CONFIG_SYS_HUSH_PARSER
77 /* hush will never change our string */
80 /* the built-in parser will change our string if it sees \n */
81 need_buff
= strchr(cmd
, '\n') != NULL
;
85 buff
= malloc(len
+ 1);
88 memcpy(buff
, cmd
, len
);
91 #ifdef CONFIG_SYS_HUSH_PARSER
92 rcode
= parse_string_outer(buff
, FLAG_PARSE_SEMICOLON
);
95 * This function will overwrite any \n it sees with a \0, which
96 * is why it can't work with a const char *. Here we are making
97 * using of internal knowledge of this function, to avoid always
98 * doing a malloc() which is actually required only in a case that
101 rcode
= cli_simple_run_command_list(buff
, flag
);
109 /****************************************************************************/
111 #if defined(CONFIG_CMD_RUN)
112 int do_run(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
117 return CMD_RET_USAGE
;
119 for (i
= 1; i
< argc
; ++i
) {
122 arg
= getenv(argv
[i
]);
124 printf("## Error: \"%s\" not defined\n", argv
[i
]);
128 if (run_command(arg
, flag
) != 0)
135 #ifdef CONFIG_OF_CONTROL
136 bool cli_process_fdt(const char **cmdp
)
138 /* Allow the fdt to override the boot command */
139 char *env
= fdtdec_get_config_string(gd
->fdt_blob
, "bootcmd");
143 * If the bootsecure option was chosen, use secure_boot_cmd().
144 * Always use 'env' in this case, since bootsecure requres that the
145 * bootcmd was specified in the FDT too.
147 return fdtdec_get_config_int(gd
->fdt_blob
, "bootsecure", 0) != 0;
151 * Runs the given boot command securely. Specifically:
152 * - Doesn't run the command with the shell (run_command or parse_string_outer),
153 * since that's a lot of code surface that an attacker might exploit.
154 * Because of this, we don't do any argument parsing--the secure boot command
155 * has to be a full-fledged u-boot command.
156 * - Doesn't check for keypresses before booting, since that could be a
157 * security hole; also disables Ctrl-C.
158 * - Doesn't allow the command to return.
160 * Upon any failures, this function will drop into an infinite loop after
161 * printing the error message to console.
163 void cli_secure_boot_cmd(const char *cmd
)
169 printf("## Error: Secure boot command not specified\n");
173 /* Disable Ctrl-C just in case some command is used that checks it. */
176 /* Find the command directly. */
177 cmdtp
= find_cmd(cmd
);
179 printf("## Error: \"%s\" not defined\n", cmd
);
183 /* Run the command, forcing no flags and faking argc and argv. */
184 rc
= (cmdtp
->cmd
)(cmdtp
, 0, 1, (char **)&cmd
);
186 /* Shouldn't ever return from boot command. */
187 printf("## Error: \"%s\" returned (code %d)\n", cmd
, rc
);
191 * Not a whole lot to do here. Rebooting won't help much, since we'll
192 * just end up right back here. Just loop.
196 #endif /* CONFIG_OF_CONTROL */
200 #ifdef CONFIG_SYS_HUSH_PARSER
202 /* This point is never reached */
206 #endif /*CONFIG_SYS_HUSH_PARSER*/
211 #ifdef CONFIG_SYS_HUSH_PARSER
215 #if defined(CONFIG_HUSH_INIT_VAR)