2 * Copyright (c) 2011-2012 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/getopt.h>
11 #include <asm/sections.h>
12 #include <asm/state.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 int sandbox_early_getopt_check(void)
18 struct sandbox_state *state = state_get_current();
19 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
20 size_t num_options = __u_boot_sandbox_option_count();
22 int max_arg_len, max_noarg_len;
24 /* parse_err will be a string of the faulting option */
25 if (!state->parse_err)
28 if (strcmp(state->parse_err, "help")) {
29 printf("u-boot: error: failed while parsing option: %s\n"
30 "\ttry running with --help for more information.\n",
36 "u-boot, a command line test interface to U-Boot\n\n"
37 "Usage: u-boot [options]\n"
41 for (i = 0; i < num_options; ++i)
42 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
43 max_noarg_len = max_arg_len + 7;
45 for (i = 0; i < num_options; ++i) {
46 struct sandbox_cmdline_option *opt = sb_opt[i];
48 /* first output the short flag if it has one */
49 if (opt->flag_short >= 0x100)
52 printf(" -%c, ", opt->flag_short);
54 /* then the long flag */
56 printf("--%-*s <arg> ", max_arg_len, opt->flag);
58 printf("--%-*s", max_noarg_len, opt->flag);
60 /* finally the help text */
61 printf(" %s\n", opt->help);
67 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
69 /* just flag to sandbox_early_getopt_check to show usage */
72 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
74 int sandbox_main_loop_init(void)
76 struct sandbox_state *state = state_get_current();
78 /* Execute command if required */
82 run_command_list(state->cmd, -1, 0);
83 if (!state->interactive)
84 os_exit(state->exit_type);
90 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
96 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
98 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
100 state->fdt_fname = arg;
103 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
105 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
108 state->interactive = true;
112 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
114 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
117 /* Remember to delete this U-Boot image later */
118 state->jumped_fname = arg;
122 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
124 static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
129 /* For now assume we always want to write it */
130 state->write_ram_buf = true;
131 state->ram_buf_fname = arg;
133 err = os_read_ram_buf(arg);
135 printf("Failed to read RAM buffer\n");
141 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
142 "Read/write ram_buf memory contents from file");
144 static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
147 state->ram_buf_rm = true;
151 SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
153 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
156 state->state_fname = arg;
159 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
161 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
164 state->read_state = true;
167 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
169 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
172 state->write_state = true;
175 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
177 static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
180 state->ignore_missing_state_on_read = true;
183 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
184 "Ignore missing state on read");
186 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
189 state->show_lcd = true;
192 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
193 "Show the sandbox LCD display");
195 static const char *term_args[STATE_TERM_COUNT] = {
201 static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
206 for (i = 0; i < STATE_TERM_COUNT; i++) {
207 if (!strcmp(arg, term_args[i])) {
213 printf("Unknown terminal setting '%s' (", arg);
214 for (i = 0; i < STATE_TERM_COUNT; i++)
215 printf("%s%s", i ? ", " : "", term_args[i]);
220 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
221 "Set terminal to raw/cooked mode");
223 int main(int argc, char *argv[])
225 struct sandbox_state *state;
233 state = state_get_current();
234 if (os_parse_args(state, argc, argv))
237 ret = sandbox_read_state(state, state->state_fname);
241 /* Remove old memory file if required */
242 if (state->ram_buf_rm && state->ram_buf_fname)
243 os_unlink(state->ram_buf_fname);
245 memset(&data, '\0', sizeof(data));
247 #ifdef CONFIG_SYS_MALLOC_F_LEN
248 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
251 /* Do pre- and post-relocation init */
254 board_init_r(gd->new_gd, 0);
256 /* NOTREACHED - board_init_r() does not return */
260 printf("Error %d\n", ret);