2 * Copyright (c) 2011-2012 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
8 #include <asm/getopt.h>
10 #include <asm/sections.h>
11 #include <asm/state.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 int sandbox_early_getopt_check(void)
17 struct sandbox_state *state = state_get_current();
18 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
19 size_t num_options = __u_boot_sandbox_option_count();
21 int max_arg_len, max_noarg_len;
23 /* parse_err will be a string of the faulting option */
24 if (!state->parse_err)
27 if (strcmp(state->parse_err, "help")) {
28 printf("u-boot: error: failed while parsing option: %s\n"
29 "\ttry running with --help for more information.\n",
35 "u-boot, a command line test interface to U-Boot\n\n"
36 "Usage: u-boot [options]\n"
40 for (i = 0; i < num_options; ++i)
41 max_arg_len = max(strlen(sb_opt[i]->flag), max_arg_len);
42 max_noarg_len = max_arg_len + 7;
44 for (i = 0; i < num_options; ++i) {
45 struct sandbox_cmdline_option *opt = sb_opt[i];
47 /* first output the short flag if it has one */
48 if (opt->flag_short >= 0x100)
51 printf(" -%c, ", opt->flag_short);
53 /* then the long flag */
55 printf("--%-*s <arg> ", max_arg_len, opt->flag);
57 printf("--%-*s", max_noarg_len, opt->flag);
59 /* finally the help text */
60 printf(" %s\n", opt->help);
66 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
68 /* just flag to sandbox_early_getopt_check to show usage */
71 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
73 int sandbox_main_loop_init(void)
75 struct sandbox_state *state = state_get_current();
77 /* Execute command if required */
79 run_command_list(state->cmd, -1, 0);
80 if (!state->interactive)
81 os_exit(state->exit_type);
87 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
93 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
95 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
97 state->fdt_fname = arg;
100 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
102 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
105 state->interactive = true;
109 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
111 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
114 /* Remember to delete this U-Boot image later */
115 state->jumped_fname = arg;
119 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
121 static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
126 /* For now assume we always want to write it */
127 state->write_ram_buf = true;
128 state->ram_buf_fname = arg;
130 if (os_read_ram_buf(arg)) {
131 printf("Failed to read RAM buffer\n");
137 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
138 "Read/write ram_buf memory contents from file");
140 static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
143 state->ram_buf_rm = true;
147 SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
149 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
152 state->state_fname = arg;
155 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
157 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
160 state->read_state = true;
163 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
165 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
168 state->write_state = true;
171 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
173 static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
176 state->ignore_missing_state_on_read = true;
179 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
180 "Ignore missing state on read");
182 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
185 state->show_lcd = true;
188 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
189 "Show the sandbox LCD display");
191 static const char *term_args[STATE_TERM_COUNT] = {
197 static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
202 for (i = 0; i < STATE_TERM_COUNT; i++) {
203 if (!strcmp(arg, term_args[i])) {
209 printf("Unknown terminal setting '%s' (", arg);
210 for (i = 0; i < STATE_TERM_COUNT; i++)
211 printf("%s%s", i ? ", " : "", term_args[i]);
216 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
217 "Set terminal to raw/cooked mode");
219 int main(int argc, char *argv[])
221 struct sandbox_state *state;
229 state = state_get_current();
230 if (os_parse_args(state, argc, argv))
233 ret = sandbox_read_state(state, state->state_fname);
237 /* Remove old memory file if required */
238 if (state->ram_buf_rm && state->ram_buf_fname)
239 os_unlink(state->ram_buf_fname);
241 memset(&data, '\0', sizeof(data));
243 #ifdef CONFIG_SYS_MALLOC_F_LEN
244 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
247 /* Do pre- and post-relocation init */
250 board_init_r(gd->new_gd, 0);
252 /* NOTREACHED - board_init_r() does not return */
256 printf("Error %d\n", ret);