2 * Copyright (c) 2011-2012 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/getopt.h>
13 #include <asm/sections.h>
14 #include <asm/state.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 int sandbox_early_getopt_check(void)
20 struct sandbox_state *state = state_get_current();
21 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
22 size_t num_options = __u_boot_sandbox_option_count();
24 int max_arg_len, max_noarg_len;
26 /* parse_err will be a string of the faulting option */
27 if (!state->parse_err)
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",
38 "u-boot, a command line test interface to U-Boot\n\n"
39 "Usage: u-boot [options]\n"
43 for (i = 0; i < num_options; ++i)
44 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
45 max_noarg_len = max_arg_len + 7;
47 for (i = 0; i < num_options; ++i) {
48 struct sandbox_cmdline_option *opt = sb_opt[i];
50 /* first output the short flag if it has one */
51 if (opt->flag_short >= 0x100)
54 printf(" -%c, ", opt->flag_short);
56 /* then the long flag */
58 printf("--%-*s <arg> ", max_arg_len, opt->flag);
60 printf("--%-*s", max_noarg_len, opt->flag);
62 /* finally the help text */
63 printf(" %s\n", opt->help);
69 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
71 /* just flag to sandbox_early_getopt_check to show usage */
74 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
76 #ifndef CONFIG_SPL_BUILD
77 int sandbox_main_loop_init(void)
79 struct sandbox_state *state = state_get_current();
81 /* Execute command if required */
82 if (state->cmd || state->run_distro_boot) {
89 retval = run_command_list(state->cmd, -1, 0);
91 if (state->run_distro_boot)
92 retval = cli_simple_run_command("run distro_bootcmd",
95 if (!state->interactive)
103 static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
106 state->run_distro_boot = true;
109 SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
111 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
117 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
119 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
121 state->fdt_fname = arg;
124 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
126 static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
129 const char *fmt = "%s.dtb";
133 len = strlen(state->argv[0]) + strlen(fmt) + 1;
134 fname = os_malloc(len);
137 snprintf(fname, len, fmt, state->argv[0]);
138 state->fdt_fname = fname;
142 SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
143 "Use the default u-boot.dtb control FDT in U-Boot directory");
145 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
148 state->interactive = true;
152 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
154 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
157 /* Remember to delete this U-Boot image later */
158 state->jumped_fname = arg;
162 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
164 static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
169 /* For now assume we always want to write it */
170 state->write_ram_buf = true;
171 state->ram_buf_fname = arg;
173 err = os_read_ram_buf(arg);
175 printf("Failed to read RAM buffer\n");
181 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
182 "Read/write ram_buf memory contents from file");
184 static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
187 state->ram_buf_rm = true;
191 SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
193 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
196 state->state_fname = arg;
199 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
201 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
204 state->read_state = true;
207 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
209 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
212 state->write_state = true;
215 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
217 static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
220 state->ignore_missing_state_on_read = true;
223 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
224 "Ignore missing state on read");
226 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
229 state->show_lcd = true;
232 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
233 "Show the sandbox LCD display");
235 static const char *term_args[STATE_TERM_COUNT] = {
241 static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
246 for (i = 0; i < STATE_TERM_COUNT; i++) {
247 if (!strcmp(arg, term_args[i])) {
253 printf("Unknown terminal setting '%s' (", arg);
254 for (i = 0; i < STATE_TERM_COUNT; i++)
255 printf("%s%s", i ? ", " : "", term_args[i]);
260 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
261 "Set terminal to raw/cooked mode");
263 static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
266 state->show_test_output = true;
269 SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
271 int board_run_command(const char *cmdline)
273 printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
278 int main(int argc, char *argv[])
280 struct sandbox_state *state;
288 state = state_get_current();
289 if (os_parse_args(state, argc, argv))
292 ret = sandbox_read_state(state, state->state_fname);
296 /* Remove old memory file if required */
297 if (state->ram_buf_rm && state->ram_buf_fname)
298 os_unlink(state->ram_buf_fname);
300 memset(&data, '\0', sizeof(data));
302 #ifdef CONFIG_SYS_MALLOC_F_LEN
303 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
306 /* Do pre- and post-relocation init */
309 board_init_r(gd->new_gd, 0);
311 /* NOTREACHED - board_init_r() does not return */
315 printf("Error %d\n", ret);