1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * (C) Copyright 2000
3*4882a593Smuzhiyun * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Add to readline cmdline-editing by
6*4882a593Smuzhiyun * (C) Copyright 2005
7*4882a593Smuzhiyun * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <common.h>
13*4882a593Smuzhiyun #include <cli.h>
14*4882a593Smuzhiyun #include <cli_hush.h>
15*4882a593Smuzhiyun #include <console.h>
16*4882a593Smuzhiyun #include <fdtdec.h>
17*4882a593Smuzhiyun #include <malloc.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
20*4882a593Smuzhiyun
board_run_command(const char * cmdline)21*4882a593Smuzhiyun __weak int board_run_command(const char *cmdline)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun return cli_simple_run_command_list((char *)cmdline, 0);
24*4882a593Smuzhiyun }
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun * Run a command using the selected parser.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * @param cmd Command to run
30*4882a593Smuzhiyun * @param flag Execution flags (CMD_FLAG_...)
31*4882a593Smuzhiyun * @return 0 on success, or != 0 on error.
32*4882a593Smuzhiyun */
run_command(const char * cmd,int flag)33*4882a593Smuzhiyun int run_command(const char *cmd, int flag)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun #ifndef CONFIG_HUSH_PARSER
36*4882a593Smuzhiyun /*
37*4882a593Smuzhiyun * cli_run_command can return 0 or 1 for success, so clean up
38*4882a593Smuzhiyun * its result.
39*4882a593Smuzhiyun */
40*4882a593Smuzhiyun if (cli_simple_run_command(cmd, flag) == -1)
41*4882a593Smuzhiyun return 1;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun return 0;
44*4882a593Smuzhiyun #else
45*4882a593Smuzhiyun int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun if (flag & CMD_FLAG_ENV)
48*4882a593Smuzhiyun hush_flags |= FLAG_CONT_ON_NEWLINE;
49*4882a593Smuzhiyun return parse_string_outer(cmd, hush_flags);
50*4882a593Smuzhiyun #endif
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun * Run a command using the selected parser, and check if it is repeatable.
55*4882a593Smuzhiyun *
56*4882a593Smuzhiyun * @param cmd Command to run
57*4882a593Smuzhiyun * @param flag Execution flags (CMD_FLAG_...)
58*4882a593Smuzhiyun * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
59*4882a593Smuzhiyun */
run_command_repeatable(const char * cmd,int flag)60*4882a593Smuzhiyun int run_command_repeatable(const char *cmd, int flag)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun #ifndef CONFIG_HUSH_PARSER
63*4882a593Smuzhiyun return cli_simple_run_command(cmd, flag);
64*4882a593Smuzhiyun #else
65*4882a593Smuzhiyun /*
66*4882a593Smuzhiyun * parse_string_outer() returns 1 for failure, so clean up
67*4882a593Smuzhiyun * its result.
68*4882a593Smuzhiyun */
69*4882a593Smuzhiyun if (parse_string_outer(cmd,
70*4882a593Smuzhiyun FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
71*4882a593Smuzhiyun return -1;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun return 0;
74*4882a593Smuzhiyun #endif
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
run_command_list(const char * cmd,int len,int flag)77*4882a593Smuzhiyun int run_command_list(const char *cmd, int len, int flag)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun int need_buff = 1;
80*4882a593Smuzhiyun char *buff = (char *)cmd; /* cast away const */
81*4882a593Smuzhiyun int rcode = 0;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun if (len == -1) {
84*4882a593Smuzhiyun len = strlen(cmd);
85*4882a593Smuzhiyun #ifdef CONFIG_HUSH_PARSER
86*4882a593Smuzhiyun /* hush will never change our string */
87*4882a593Smuzhiyun need_buff = 0;
88*4882a593Smuzhiyun #else
89*4882a593Smuzhiyun /* the built-in parser will change our string if it sees \n */
90*4882a593Smuzhiyun need_buff = strchr(cmd, '\n') != NULL;
91*4882a593Smuzhiyun #endif
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun if (need_buff) {
94*4882a593Smuzhiyun buff = malloc(len + 1);
95*4882a593Smuzhiyun if (!buff)
96*4882a593Smuzhiyun return 1;
97*4882a593Smuzhiyun memcpy(buff, cmd, len);
98*4882a593Smuzhiyun buff[len] = '\0';
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun #ifdef CONFIG_HUSH_PARSER
101*4882a593Smuzhiyun rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
102*4882a593Smuzhiyun #else
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * This function will overwrite any \n it sees with a \0, which
105*4882a593Smuzhiyun * is why it can't work with a const char *. Here we are making
106*4882a593Smuzhiyun * using of internal knowledge of this function, to avoid always
107*4882a593Smuzhiyun * doing a malloc() which is actually required only in a case that
108*4882a593Smuzhiyun * is pretty rare.
109*4882a593Smuzhiyun */
110*4882a593Smuzhiyun #ifdef CONFIG_CMDLINE
111*4882a593Smuzhiyun rcode = cli_simple_run_command_list(buff, flag);
112*4882a593Smuzhiyun #else
113*4882a593Smuzhiyun rcode = board_run_command(buff);
114*4882a593Smuzhiyun #endif
115*4882a593Smuzhiyun #endif
116*4882a593Smuzhiyun if (need_buff)
117*4882a593Smuzhiyun free(buff);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun return rcode;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /****************************************************************************/
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun #if defined(CONFIG_CMD_RUN)
do_run(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])125*4882a593Smuzhiyun int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun int i;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun if (argc < 2)
130*4882a593Smuzhiyun return CMD_RET_USAGE;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun for (i = 1; i < argc; ++i) {
133*4882a593Smuzhiyun char *arg;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun arg = env_get(argv[i]);
136*4882a593Smuzhiyun if (arg == NULL) {
137*4882a593Smuzhiyun printf("## Error: \"%s\" not defined\n", argv[i]);
138*4882a593Smuzhiyun return 1;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
142*4882a593Smuzhiyun return 1;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun return 0;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun #endif
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(OF_CONTROL)
cli_process_fdt(const char ** cmdp)149*4882a593Smuzhiyun bool cli_process_fdt(const char **cmdp)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun /* Allow the fdt to override the boot command */
152*4882a593Smuzhiyun char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
153*4882a593Smuzhiyun if (env)
154*4882a593Smuzhiyun *cmdp = env;
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun * If the bootsecure option was chosen, use secure_boot_cmd().
157*4882a593Smuzhiyun * Always use 'env' in this case, since bootsecure requres that the
158*4882a593Smuzhiyun * bootcmd was specified in the FDT too.
159*4882a593Smuzhiyun */
160*4882a593Smuzhiyun return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun * Runs the given boot command securely. Specifically:
165*4882a593Smuzhiyun * - Doesn't run the command with the shell (run_command or parse_string_outer),
166*4882a593Smuzhiyun * since that's a lot of code surface that an attacker might exploit.
167*4882a593Smuzhiyun * Because of this, we don't do any argument parsing--the secure boot command
168*4882a593Smuzhiyun * has to be a full-fledged u-boot command.
169*4882a593Smuzhiyun * - Doesn't check for keypresses before booting, since that could be a
170*4882a593Smuzhiyun * security hole; also disables Ctrl-C.
171*4882a593Smuzhiyun * - Doesn't allow the command to return.
172*4882a593Smuzhiyun *
173*4882a593Smuzhiyun * Upon any failures, this function will drop into an infinite loop after
174*4882a593Smuzhiyun * printing the error message to console.
175*4882a593Smuzhiyun */
cli_secure_boot_cmd(const char * cmd)176*4882a593Smuzhiyun void cli_secure_boot_cmd(const char *cmd)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun #ifdef CONFIG_CMDLINE
179*4882a593Smuzhiyun cmd_tbl_t *cmdtp;
180*4882a593Smuzhiyun #endif
181*4882a593Smuzhiyun int rc;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun if (!cmd) {
184*4882a593Smuzhiyun printf("## Error: Secure boot command not specified\n");
185*4882a593Smuzhiyun goto err;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /* Disable Ctrl-C just in case some command is used that checks it. */
189*4882a593Smuzhiyun disable_ctrlc(1);
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /* Find the command directly. */
192*4882a593Smuzhiyun #ifdef CONFIG_CMDLINE
193*4882a593Smuzhiyun cmdtp = find_cmd(cmd);
194*4882a593Smuzhiyun if (!cmdtp) {
195*4882a593Smuzhiyun printf("## Error: \"%s\" not defined\n", cmd);
196*4882a593Smuzhiyun goto err;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun /* Run the command, forcing no flags and faking argc and argv. */
200*4882a593Smuzhiyun rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun #else
203*4882a593Smuzhiyun rc = board_run_command(cmd);
204*4882a593Smuzhiyun #endif
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* Shouldn't ever return from boot command. */
207*4882a593Smuzhiyun printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun err:
210*4882a593Smuzhiyun /*
211*4882a593Smuzhiyun * Not a whole lot to do here. Rebooting won't help much, since we'll
212*4882a593Smuzhiyun * just end up right back here. Just loop.
213*4882a593Smuzhiyun */
214*4882a593Smuzhiyun hang();
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun #ifndef CONFIG_CONSOLE_DISABLE_CLI
cli_loop(void)219*4882a593Smuzhiyun void cli_loop(void)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun #ifdef CONFIG_HUSH_PARSER
222*4882a593Smuzhiyun parse_file_outer();
223*4882a593Smuzhiyun /* This point is never reached */
224*4882a593Smuzhiyun for (;;);
225*4882a593Smuzhiyun #elif defined(CONFIG_CMDLINE)
226*4882a593Smuzhiyun cli_simple_loop();
227*4882a593Smuzhiyun #else
228*4882a593Smuzhiyun printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
229*4882a593Smuzhiyun #endif /*CONFIG_HUSH_PARSER*/
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun #else
cli_loop(void)232*4882a593Smuzhiyun void cli_loop(void) { }
233*4882a593Smuzhiyun #endif
234*4882a593Smuzhiyun
cli_init(void)235*4882a593Smuzhiyun void cli_init(void)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun #ifdef CONFIG_HUSH_PARSER
238*4882a593Smuzhiyun u_boot_hush_start();
239*4882a593Smuzhiyun #endif
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun #if defined(CONFIG_HUSH_INIT_VAR)
242*4882a593Smuzhiyun hush_init_var();
243*4882a593Smuzhiyun #endif
244*4882a593Smuzhiyun }
245