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