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