]>
Commit | Line | Data |
---|---|---|
bace3d00 | 1 | /* |
6fb62078 | 2 | * Copyright (c) 2011-2012 The Chromium OS Authors. |
1a459660 | 3 | * SPDX-License-Identifier: GPL-2.0+ |
bace3d00 SG |
4 | */ |
5 | ||
6 | #include <common.h> | |
38068820 | 7 | #include <errno.h> |
5c2859cd | 8 | #include <os.h> |
7dbcb76e | 9 | #include <cli.h> |
1f32ae95 | 10 | #include <malloc.h> |
70db4212 | 11 | #include <asm/getopt.h> |
4d94dfa0 | 12 | #include <asm/io.h> |
70db4212 | 13 | #include <asm/sections.h> |
6fb62078 | 14 | #include <asm/state.h> |
bace3d00 | 15 | |
808434cd SG |
16 | DECLARE_GLOBAL_DATA_PTR; |
17 | ||
70db4212 SG |
18 | int sandbox_early_getopt_check(void) |
19 | { | |
20 | struct sandbox_state *state = state_get_current(); | |
7b3efc66 | 21 | struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start; |
70db4212 SG |
22 | size_t num_options = __u_boot_sandbox_option_count(); |
23 | size_t i; | |
24 | int max_arg_len, max_noarg_len; | |
25 | ||
26 | /* parse_err will be a string of the faulting option */ | |
27 | if (!state->parse_err) | |
28 | return 0; | |
29 | ||
30 | if (strcmp(state->parse_err, "help")) { | |
31 | printf("u-boot: error: failed while parsing option: %s\n" | |
32 | "\ttry running with --help for more information.\n", | |
33 | state->parse_err); | |
34 | os_exit(1); | |
35 | } | |
36 | ||
37 | printf( | |
38 | "u-boot, a command line test interface to U-Boot\n\n" | |
39 | "Usage: u-boot [options]\n" | |
40 | "Options:\n"); | |
41 | ||
42 | max_arg_len = 0; | |
43 | for (i = 0; i < num_options; ++i) | |
b4141195 | 44 | max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len); |
70db4212 SG |
45 | max_noarg_len = max_arg_len + 7; |
46 | ||
47 | for (i = 0; i < num_options; ++i) { | |
7b3efc66 | 48 | struct sandbox_cmdline_option *opt = sb_opt[i]; |
70db4212 SG |
49 | |
50 | /* first output the short flag if it has one */ | |
51 | if (opt->flag_short >= 0x100) | |
52 | printf(" "); | |
53 | else | |
54 | printf(" -%c, ", opt->flag_short); | |
55 | ||
56 | /* then the long flag */ | |
57 | if (opt->has_arg) | |
70db4212 | 58 | printf("--%-*s <arg> ", max_arg_len, opt->flag); |
6ebcab8d SG |
59 | else |
60 | printf("--%-*s", max_noarg_len, opt->flag); | |
70db4212 SG |
61 | |
62 | /* finally the help text */ | |
63 | printf(" %s\n", opt->help); | |
64 | } | |
65 | ||
66 | os_exit(0); | |
67 | } | |
68 | ||
7b3efc66 | 69 | static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg) |
70db4212 SG |
70 | { |
71 | /* just flag to sandbox_early_getopt_check to show usage */ | |
72 | return 1; | |
73 | } | |
7b3efc66 | 74 | SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help"); |
70db4212 | 75 | |
ab4e07eb SG |
76 | int sandbox_main_loop_init(void) |
77 | { | |
70db4212 SG |
78 | struct sandbox_state *state = state_get_current(); |
79 | ||
80 | /* Execute command if required */ | |
ebaa832e SS |
81 | if (state->cmd || state->run_distro_boot) { |
82 | int retval = 0; | |
88539e44 | 83 | |
7dbcb76e RV |
84 | cli_init(); |
85 | ||
ebaa832e SS |
86 | if (state->cmd) |
87 | retval = run_command_list(state->cmd, -1, 0); | |
88 | ||
89 | if (state->run_distro_boot) | |
90 | retval = cli_simple_run_command("run distro_bootcmd", | |
91 | 0); | |
92 | ||
c5a62d4a | 93 | if (!state->interactive) |
88539e44 | 94 | os_exit(retval); |
70db4212 SG |
95 | } |
96 | ||
ab4e07eb SG |
97 | return 0; |
98 | } | |
99 | ||
ebaa832e SS |
100 | static int sandbox_cmdline_cb_boot(struct sandbox_state *state, |
101 | const char *arg) | |
102 | { | |
103 | state->run_distro_boot = true; | |
104 | return 0; | |
105 | } | |
106 | SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands"); | |
107 | ||
7b3efc66 SG |
108 | static int sandbox_cmdline_cb_command(struct sandbox_state *state, |
109 | const char *arg) | |
70db4212 SG |
110 | { |
111 | state->cmd = arg; | |
112 | return 0; | |
113 | } | |
7b3efc66 | 114 | SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command"); |
70db4212 | 115 | |
7b3efc66 | 116 | static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg) |
f828bf25 SG |
117 | { |
118 | state->fdt_fname = arg; | |
119 | return 0; | |
120 | } | |
7b3efc66 | 121 | SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT"); |
f828bf25 | 122 | |
1f32ae95 SG |
123 | static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state, |
124 | const char *arg) | |
125 | { | |
126 | const char *fmt = "%s.dtb"; | |
127 | char *fname; | |
128 | int len; | |
129 | ||
130 | len = strlen(state->argv[0]) + strlen(fmt) + 1; | |
131 | fname = os_malloc(len); | |
132 | if (!fname) | |
133 | return -ENOMEM; | |
134 | snprintf(fname, len, fmt, state->argv[0]); | |
135 | state->fdt_fname = fname; | |
136 | ||
137 | return 0; | |
138 | } | |
139 | SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0, | |
140 | "Use the default u-boot.dtb control FDT in U-Boot directory"); | |
141 | ||
c5a62d4a SG |
142 | static int sandbox_cmdline_cb_interactive(struct sandbox_state *state, |
143 | const char *arg) | |
144 | { | |
145 | state->interactive = true; | |
146 | return 0; | |
147 | } | |
148 | ||
149 | SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode"); | |
150 | ||
bda7773f SG |
151 | static int sandbox_cmdline_cb_jump(struct sandbox_state *state, |
152 | const char *arg) | |
153 | { | |
ab839dc3 SG |
154 | /* Remember to delete this U-Boot image later */ |
155 | state->jumped_fname = arg; | |
bda7773f SG |
156 | |
157 | return 0; | |
158 | } | |
159 | SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot"); | |
160 | ||
5c2859cd SG |
161 | static int sandbox_cmdline_cb_memory(struct sandbox_state *state, |
162 | const char *arg) | |
163 | { | |
164 | int err; | |
165 | ||
166 | /* For now assume we always want to write it */ | |
167 | state->write_ram_buf = true; | |
168 | state->ram_buf_fname = arg; | |
169 | ||
f80a8bbe SG |
170 | err = os_read_ram_buf(arg); |
171 | if (err) { | |
5c2859cd SG |
172 | printf("Failed to read RAM buffer\n"); |
173 | return err; | |
174 | } | |
175 | ||
176 | return 0; | |
177 | } | |
178 | SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1, | |
179 | "Read/write ram_buf memory contents from file"); | |
180 | ||
ab839dc3 SG |
181 | static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state, |
182 | const char *arg) | |
183 | { | |
184 | state->ram_buf_rm = true; | |
185 | ||
186 | return 0; | |
187 | } | |
188 | SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading"); | |
189 | ||
1209e272 SG |
190 | static int sandbox_cmdline_cb_state(struct sandbox_state *state, |
191 | const char *arg) | |
192 | { | |
193 | state->state_fname = arg; | |
194 | return 0; | |
195 | } | |
196 | SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT"); | |
197 | ||
198 | static int sandbox_cmdline_cb_read(struct sandbox_state *state, | |
199 | const char *arg) | |
200 | { | |
201 | state->read_state = true; | |
202 | return 0; | |
203 | } | |
204 | SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup"); | |
205 | ||
206 | static int sandbox_cmdline_cb_write(struct sandbox_state *state, | |
207 | const char *arg) | |
208 | { | |
209 | state->write_state = true; | |
210 | return 0; | |
211 | } | |
212 | SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit"); | |
213 | ||
214 | static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state, | |
215 | const char *arg) | |
216 | { | |
217 | state->ignore_missing_state_on_read = true; | |
218 | return 0; | |
219 | } | |
220 | SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0, | |
221 | "Ignore missing state on read"); | |
222 | ||
7d95f2a3 SG |
223 | static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state, |
224 | const char *arg) | |
225 | { | |
226 | state->show_lcd = true; | |
227 | return 0; | |
228 | } | |
229 | SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0, | |
230 | "Show the sandbox LCD display"); | |
231 | ||
ffb87905 SG |
232 | static const char *term_args[STATE_TERM_COUNT] = { |
233 | "raw-with-sigs", | |
234 | "raw", | |
235 | "cooked", | |
236 | }; | |
237 | ||
238 | static int sandbox_cmdline_cb_terminal(struct sandbox_state *state, | |
239 | const char *arg) | |
240 | { | |
241 | int i; | |
242 | ||
243 | for (i = 0; i < STATE_TERM_COUNT; i++) { | |
244 | if (!strcmp(arg, term_args[i])) { | |
245 | state->term_raw = i; | |
246 | return 0; | |
247 | } | |
248 | } | |
249 | ||
250 | printf("Unknown terminal setting '%s' (", arg); | |
251 | for (i = 0; i < STATE_TERM_COUNT; i++) | |
252 | printf("%s%s", i ? ", " : "", term_args[i]); | |
253 | puts(")\n"); | |
254 | ||
255 | return 1; | |
256 | } | |
257 | SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1, | |
258 | "Set terminal to raw/cooked mode"); | |
259 | ||
9ce8b402 SG |
260 | static int sandbox_cmdline_cb_verbose(struct sandbox_state *state, |
261 | const char *arg) | |
262 | { | |
263 | state->show_test_output = true; | |
264 | return 0; | |
265 | } | |
266 | SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output"); | |
267 | ||
bace3d00 SG |
268 | int main(int argc, char *argv[]) |
269 | { | |
70db4212 | 270 | struct sandbox_state *state; |
4d94dfa0 | 271 | gd_t data; |
1209e272 | 272 | int ret; |
6fb62078 | 273 | |
1209e272 SG |
274 | ret = state_init(); |
275 | if (ret) | |
276 | goto err; | |
6fb62078 | 277 | |
70db4212 SG |
278 | state = state_get_current(); |
279 | if (os_parse_args(state, argc, argv)) | |
280 | return 1; | |
281 | ||
1209e272 SG |
282 | ret = sandbox_read_state(state, state->state_fname); |
283 | if (ret) | |
284 | goto err; | |
285 | ||
ab839dc3 SG |
286 | /* Remove old memory file if required */ |
287 | if (state->ram_buf_rm && state->ram_buf_fname) | |
288 | os_unlink(state->ram_buf_fname); | |
289 | ||
4d94dfa0 SG |
290 | memset(&data, '\0', sizeof(data)); |
291 | gd = &data; | |
29afe9e6 SG |
292 | #ifdef CONFIG_SYS_MALLOC_F_LEN |
293 | gd->malloc_base = CONFIG_MALLOC_F_ADDR; | |
294 | #endif | |
4d94dfa0 | 295 | |
808434cd | 296 | /* Do pre- and post-relocation init */ |
bace3d00 | 297 | board_init_f(0); |
8ec21bbe | 298 | |
808434cd SG |
299 | board_init_r(gd->new_gd, 0); |
300 | ||
301 | /* NOTREACHED - board_init_r() does not return */ | |
8ec21bbe | 302 | return 0; |
1209e272 SG |
303 | |
304 | err: | |
305 | printf("Error %d\n", ret); | |
306 | return 1; | |
bace3d00 | 307 | } |