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