2 * Copyright (c) 2011-2012 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
8 #include <asm/getopt.h>
9 #include <asm/sections.h>
10 #include <asm/state.h>
12 DECLARE_GLOBAL_DATA_PTR;
14 int sandbox_early_getopt_check(void)
16 struct sandbox_state *state = state_get_current();
17 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
18 size_t num_options = __u_boot_sandbox_option_count();
20 int max_arg_len, max_noarg_len;
22 /* parse_err will be a string of the faulting option */
23 if (!state->parse_err)
26 if (strcmp(state->parse_err, "help")) {
27 printf("u-boot: error: failed while parsing option: %s\n"
28 "\ttry running with --help for more information.\n",
34 "u-boot, a command line test interface to U-Boot\n\n"
35 "Usage: u-boot [options]\n"
39 for (i = 0; i < num_options; ++i)
40 max_arg_len = max(strlen(sb_opt[i]->flag), max_arg_len);
41 max_noarg_len = max_arg_len + 7;
43 for (i = 0; i < num_options; ++i) {
44 struct sandbox_cmdline_option *opt = sb_opt[i];
46 /* first output the short flag if it has one */
47 if (opt->flag_short >= 0x100)
50 printf(" -%c, ", opt->flag_short);
52 /* then the long flag */
54 printf("--%-*s <arg> ", max_arg_len, opt->flag);
56 printf("--%-*s", max_noarg_len, opt->flag);
58 /* finally the help text */
59 printf(" %s\n", opt->help);
65 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
67 /* just flag to sandbox_early_getopt_check to show usage */
70 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
72 int sandbox_main_loop_init(void)
74 struct sandbox_state *state = state_get_current();
76 /* Execute command if required */
78 run_command_list(state->cmd, -1, 0);
79 if (!state->interactive)
80 os_exit(state->exit_type);
86 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
92 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
94 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
96 state->fdt_fname = arg;
99 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
101 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
104 state->interactive = true;
108 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
110 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
116 * TODO(sjg@chromium.org): Note this causes problems for gdb which
117 * wants to read debug data from the image.
124 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
126 static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
131 /* For now assume we always want to write it */
132 state->write_ram_buf = true;
133 state->ram_buf_fname = arg;
135 if (os_read_ram_buf(arg)) {
136 printf("Failed to read RAM buffer\n");
142 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
143 "Read/write ram_buf memory contents from file");
145 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
148 state->state_fname = arg;
151 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
153 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
156 state->read_state = true;
159 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
161 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
164 state->write_state = true;
167 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
169 static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
172 state->ignore_missing_state_on_read = true;
175 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
176 "Ignore missing state on read");
178 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
181 state->show_lcd = true;
184 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
185 "Show the sandbox LCD display");
187 int main(int argc, char *argv[])
189 struct sandbox_state *state;
196 state = state_get_current();
197 if (os_parse_args(state, argc, argv))
200 ret = sandbox_read_state(state, state->state_fname);
204 /* Do pre- and post-relocation init */
207 board_init_r(gd->new_gd, 0);
209 /* NOTREACHED - board_init_r() does not return */
213 printf("Error %d\n", ret);