1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011-2012 The Chromium OS Authors.
12 #include <asm/getopt.h>
14 #include <asm/sections.h>
15 #include <asm/state.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 int sandbox_early_getopt_check(void)
21 struct sandbox_state *state = state_get_current();
22 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
23 size_t num_options = __u_boot_sandbox_option_count();
25 int max_arg_len, max_noarg_len;
27 /* parse_err will be a string of the faulting option */
28 if (!state->parse_err)
31 if (strcmp(state->parse_err, "help")) {
32 printf("u-boot: error: failed while parsing option: %s\n"
33 "\ttry running with --help for more information.\n",
39 "u-boot, a command line test interface to U-Boot\n\n"
40 "Usage: u-boot [options]\n"
44 for (i = 0; i < num_options; ++i)
45 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
46 max_noarg_len = max_arg_len + 7;
48 for (i = 0; i < num_options; ++i) {
49 struct sandbox_cmdline_option *opt = sb_opt[i];
51 /* first output the short flag if it has one */
52 if (opt->flag_short >= 0x100)
55 printf(" -%c, ", opt->flag_short);
57 /* then the long flag */
59 printf("--%-*s <arg> ", max_arg_len, opt->flag);
61 printf("--%-*s", max_noarg_len, opt->flag);
63 /* finally the help text */
64 printf(" %s\n", opt->help);
72 return sandbox_early_getopt_check();
75 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
77 /* just flag to sandbox_early_getopt_check to show usage */
80 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
82 #ifndef CONFIG_SPL_BUILD
83 int sandbox_main_loop_init(void)
85 struct sandbox_state *state = state_get_current();
87 /* Execute command if required */
88 if (state->cmd || state->run_distro_boot) {
95 retval = run_command_list(state->cmd, -1, 0);
97 if (state->run_distro_boot)
98 retval = cli_simple_run_command("run distro_bootcmd",
101 if (!state->interactive)
109 static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
112 state->run_distro_boot = true;
115 SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
117 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
123 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
125 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
127 state->fdt_fname = arg;
130 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
132 static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
135 const char *fmt = "%s.dtb";
139 len = strlen(state->argv[0]) + strlen(fmt) + 1;
140 fname = os_malloc(len);
143 snprintf(fname, len, fmt, state->argv[0]);
144 state->fdt_fname = fname;
148 SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
149 "Use the default u-boot.dtb control FDT in U-Boot directory");
151 static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
154 const char *fmt = "/arch/sandbox/dts/test.dtb";
159 len = strlen(state->argv[0]) + strlen(fmt) + 1;
160 fname = os_malloc(len);
163 strcpy(fname, state->argv[0]);
164 p = strrchr(fname, '/');
166 p = fname + strlen(fname);
168 snprintf(p, len, fmt, p);
169 state->fdt_fname = fname;
173 SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0,
174 "Use the test.dtb control FDT in U-Boot directory");
176 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
179 state->interactive = true;
183 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
185 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
188 /* Remember to delete this U-Boot image later */
189 state->jumped_fname = arg;
193 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
195 static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
200 /* For now assume we always want to write it */
201 state->write_ram_buf = true;
202 state->ram_buf_fname = arg;
204 err = os_read_ram_buf(arg);
206 printf("Failed to read RAM buffer '%s': %d\n", arg, err);
209 state->ram_buf_read = true;
213 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
214 "Read/write ram_buf memory contents from file");
216 static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
219 state->ram_buf_rm = true;
223 SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
225 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
228 state->state_fname = arg;
231 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
233 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
236 state->read_state = true;
239 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
241 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
244 state->write_state = true;
247 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
249 static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
252 state->ignore_missing_state_on_read = true;
255 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
256 "Ignore missing state on read");
258 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
261 state->show_lcd = true;
264 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
265 "Show the sandbox LCD display");
267 static const char *term_args[STATE_TERM_COUNT] = {
273 static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
278 for (i = 0; i < STATE_TERM_COUNT; i++) {
279 if (!strcmp(arg, term_args[i])) {
285 printf("Unknown terminal setting '%s' (", arg);
286 for (i = 0; i < STATE_TERM_COUNT; i++)
287 printf("%s%s", i ? ", " : "", term_args[i]);
292 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
293 "Set terminal to raw/cooked mode");
295 static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
298 state->show_test_output = true;
301 SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
303 static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
306 state->default_log_level = simple_strtol(arg, NULL, 10);
310 SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
311 "Set log level (0=panic, 7=debug)");
313 static int sandbox_cmdline_cb_show_of_platdata(struct sandbox_state *state,
316 state->show_of_platdata = true;
320 SANDBOX_CMDLINE_OPT(show_of_platdata, 0, "Show of-platdata in SPL");
322 static void setup_ram_buf(struct sandbox_state *state)
324 /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
325 if (!state->ram_buf_read)
326 memset(state->ram_buf, '\0', state->ram_size);
328 gd->arch.ram_buf = state->ram_buf;
329 gd->ram_size = state->ram_size;
332 void state_show(struct sandbox_state *state)
336 printf("Arguments:\n");
337 for (p = state->argv; *p; p++)
342 int main(int argc, char *argv[])
344 struct sandbox_state *state;
348 memset(&data, '\0', sizeof(data));
350 gd->arch.text_base = os_find_text_base();
356 state = state_get_current();
357 if (os_parse_args(state, argc, argv))
360 ret = sandbox_read_state(state, state->state_fname);
364 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
365 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
367 #if CONFIG_IS_ENABLED(LOG)
368 gd->default_log_level = state->default_log_level;
370 setup_ram_buf(state);
373 * Set up the relocation offset here, since sandbox symbols are always
374 * relocated by the OS before sandbox is entered.
376 gd->reloc_off = (ulong)gd->arch.text_base;
378 /* Do pre- and post-relocation init */
381 board_init_r(gd->new_gd, 0);
383 /* NOTREACHED - board_init_r() does not return */
387 printf("Error %d\n", ret);