Prepare v2023.10
[platform/kernel/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 #define pr_fmt(fmt) "cli: %s: " fmt, __func__
12
13 #include <common.h>
14 #include <bootstage.h>
15 #include <cli.h>
16 #include <cli_hush.h>
17 #include <command.h>
18 #include <console.h>
19 #include <env.h>
20 #include <fdtdec.h>
21 #include <hang.h>
22 #include <malloc.h>
23 #include <asm/global_data.h>
24 #include <dm/ofnode.h>
25 #include <linux/errno.h>
26
27 #ifdef CONFIG_CMDLINE
28 /*
29  * Run a command using the selected parser.
30  *
31  * @param cmd   Command to run
32  * @param flag  Execution flags (CMD_FLAG_...)
33  * Return: 0 on success, or != 0 on error.
34  */
35 int run_command(const char *cmd, int flag)
36 {
37 #if !IS_ENABLED(CONFIG_HUSH_PARSER)
38         /*
39          * cli_run_command can return 0 or 1 for success, so clean up
40          * its result.
41          */
42         if (cli_simple_run_command(cmd, flag) == -1)
43                 return 1;
44
45         return 0;
46 #else
47         int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
48
49         if (flag & CMD_FLAG_ENV)
50                 hush_flags |= FLAG_CONT_ON_NEWLINE;
51         return parse_string_outer(cmd, hush_flags);
52 #endif
53 }
54
55 /*
56  * Run a command using the selected parser, and check if it is repeatable.
57  *
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.
61  */
62 int run_command_repeatable(const char *cmd, int flag)
63 {
64 #ifndef CONFIG_HUSH_PARSER
65         return cli_simple_run_command(cmd, flag);
66 #else
67         /*
68          * parse_string_outer() returns 1 for failure, so clean up
69          * its result.
70          */
71         if (parse_string_outer(cmd,
72                                FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
73                 return -1;
74
75         return 0;
76 #endif
77 }
78 #else
79 __weak int board_run_command(const char *cmdline)
80 {
81         printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
82
83         return 1;
84 }
85 #endif /* CONFIG_CMDLINE */
86
87 int run_command_list(const char *cmd, int len, int flag)
88 {
89         int need_buff = 1;
90         char *buff = (char *)cmd;       /* cast away const */
91         int rcode = 0;
92
93         if (len == -1) {
94                 len = strlen(cmd);
95 #ifdef CONFIG_HUSH_PARSER
96                 /* hush will never change our string */
97                 need_buff = 0;
98 #else
99                 /* the built-in parser will change our string if it sees \n */
100                 need_buff = strchr(cmd, '\n') != NULL;
101 #endif
102         }
103         if (need_buff) {
104                 buff = malloc(len + 1);
105                 if (!buff)
106                         return 1;
107                 memcpy(buff, cmd, len);
108                 buff[len] = '\0';
109         }
110 #ifdef CONFIG_HUSH_PARSER
111         rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
112 #else
113         /*
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
118          * is pretty rare.
119          */
120 #ifdef CONFIG_CMDLINE
121         rcode = cli_simple_run_command_list(buff, flag);
122 #else
123         rcode = board_run_command(buff);
124 #endif
125 #endif
126         if (need_buff)
127                 free(buff);
128
129         return rcode;
130 }
131
132 int run_commandf(const char *fmt, ...)
133 {
134         va_list args;
135         int nbytes;
136
137         va_start(args, fmt);
138         /*
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
142          */
143         nbytes = vsnprintf(console_buffer, CONFIG_SYS_CBSIZE, fmt, args);
144         va_end(args);
145
146         if (nbytes < 0) {
147                 pr_debug("I/O internal error occurred.\n");
148                 return -EIO;
149         } else if (nbytes >= CONFIG_SYS_CBSIZE) {
150                 pr_debug("'fmt' size:%d exceeds the limit(%d)\n",
151                          nbytes, CONFIG_SYS_CBSIZE);
152                 return -ENOSPC;
153         }
154         return run_command(console_buffer, 0);
155 }
156
157 /****************************************************************************/
158
159 #if defined(CONFIG_CMD_RUN)
160 int do_run(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
161 {
162         int i, ret;
163
164         if (argc < 2)
165                 return CMD_RET_USAGE;
166
167         for (i = 1; i < argc; ++i) {
168                 char *arg;
169
170                 arg = env_get(argv[i]);
171                 if (arg == NULL) {
172                         printf("## Error: \"%s\" not defined\n", argv[i]);
173                         return 1;
174                 }
175
176                 ret = run_command(arg, flag | CMD_FLAG_ENV);
177                 if (ret)
178                         return ret;
179         }
180         return 0;
181 }
182 #endif
183
184 #if CONFIG_IS_ENABLED(OF_CONTROL)
185 bool cli_process_fdt(const char **cmdp)
186 {
187         /* Allow the fdt to override the boot command */
188         const char *env = ofnode_conf_read_str("bootcmd");
189         if (env)
190                 *cmdp = env;
191         /*
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.
195          */
196         return ofnode_conf_read_int("bootsecure", 0);
197 }
198
199 /*
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.
208  *
209  * Upon any failures, this function will drop into an infinite loop after
210  * printing the error message to console.
211  */
212 void cli_secure_boot_cmd(const char *cmd)
213 {
214 #ifdef CONFIG_CMDLINE
215         struct cmd_tbl *cmdtp;
216 #endif
217         int rc;
218
219         if (!cmd) {
220                 printf("## Error: Secure boot command not specified\n");
221                 goto err;
222         }
223
224         /* Disable Ctrl-C just in case some command is used that checks it. */
225         disable_ctrlc(1);
226
227         /* Find the command directly. */
228 #ifdef CONFIG_CMDLINE
229         cmdtp = find_cmd(cmd);
230         if (!cmdtp) {
231                 printf("## Error: \"%s\" not defined\n", cmd);
232                 goto err;
233         }
234
235         /* Run the command, forcing no flags and faking argc and argv. */
236         rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
237
238 #else
239         rc = board_run_command(cmd);
240 #endif
241
242         /* Shouldn't ever return from boot command. */
243         printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
244
245 err:
246         /*
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.
249          */
250         hang();
251 }
252 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
253
254 void cli_loop(void)
255 {
256         bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
257 #ifdef CONFIG_HUSH_PARSER
258         parse_file_outer();
259         /* This point is never reached */
260         for (;;);
261 #elif defined(CONFIG_CMDLINE)
262         cli_simple_loop();
263 #else
264         printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
265 #endif /*CONFIG_HUSH_PARSER*/
266 }
267
268 void cli_init(void)
269 {
270 #ifdef CONFIG_HUSH_PARSER
271         u_boot_hush_start();
272 #endif
273
274 #if defined(CONFIG_HUSH_INIT_VAR)
275         hush_init_var();
276 #endif
277 }