2 * Copyright (c) 2011-2012 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/getopt.h>
12 #include <asm/sections.h>
13 #include <asm/state.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 int sandbox_early_getopt_check(void)
19 struct sandbox_state *state = state_get_current();
20 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
21 size_t num_options = __u_boot_sandbox_option_count();
23 int max_arg_len, max_noarg_len;
25 /* parse_err will be a string of the faulting option */
26 if (!state->parse_err)
29 if (strcmp(state->parse_err, "help")) {
30 printf("u-boot: error: failed while parsing option: %s\n"
31 "\ttry running with --help for more information.\n",
37 "u-boot, a command line test interface to U-Boot\n\n"
38 "Usage: u-boot [options]\n"
42 for (i = 0; i < num_options; ++i)
43 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
44 max_noarg_len = max_arg_len + 7;
46 for (i = 0; i < num_options; ++i) {
47 struct sandbox_cmdline_option *opt = sb_opt[i];
49 /* first output the short flag if it has one */
50 if (opt->flag_short >= 0x100)
53 printf(" -%c, ", opt->flag_short);
55 /* then the long flag */
57 printf("--%-*s <arg> ", max_arg_len, opt->flag);
59 printf("--%-*s", max_noarg_len, opt->flag);
61 /* finally the help text */
62 printf(" %s\n", opt->help);
68 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
70 /* just flag to sandbox_early_getopt_check to show usage */
73 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
75 int sandbox_main_loop_init(void)
77 struct sandbox_state *state = state_get_current();
79 /* Execute command if required */
83 run_command_list(state->cmd, -1, 0);
84 if (!state->interactive)
85 os_exit(state->exit_type);
91 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
97 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
99 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
101 state->fdt_fname = arg;
104 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
106 static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
109 const char *fmt = "%s.dtb";
113 len = strlen(state->argv[0]) + strlen(fmt) + 1;
114 fname = os_malloc(len);
117 snprintf(fname, len, fmt, state->argv[0]);
118 state->fdt_fname = fname;
122 SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
123 "Use the default u-boot.dtb control FDT in U-Boot directory");
125 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
128 state->interactive = true;
132 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
134 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
137 /* Remember to delete this U-Boot image later */
138 state->jumped_fname = arg;
142 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
144 static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
149 /* For now assume we always want to write it */
150 state->write_ram_buf = true;
151 state->ram_buf_fname = arg;
153 err = os_read_ram_buf(arg);
155 printf("Failed to read RAM buffer\n");
161 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
162 "Read/write ram_buf memory contents from file");
164 static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
167 state->ram_buf_rm = true;
171 SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
173 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
176 state->state_fname = arg;
179 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
181 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
184 state->read_state = true;
187 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
189 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
192 state->write_state = true;
195 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
197 static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
200 state->ignore_missing_state_on_read = true;
203 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
204 "Ignore missing state on read");
206 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
209 state->show_lcd = true;
212 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
213 "Show the sandbox LCD display");
215 static const char *term_args[STATE_TERM_COUNT] = {
221 static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
226 for (i = 0; i < STATE_TERM_COUNT; i++) {
227 if (!strcmp(arg, term_args[i])) {
233 printf("Unknown terminal setting '%s' (", arg);
234 for (i = 0; i < STATE_TERM_COUNT; i++)
235 printf("%s%s", i ? ", " : "", term_args[i]);
240 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
241 "Set terminal to raw/cooked mode");
243 int main(int argc, char *argv[])
245 struct sandbox_state *state;
253 state = state_get_current();
254 if (os_parse_args(state, argc, argv))
257 ret = sandbox_read_state(state, state->state_fname);
261 /* Remove old memory file if required */
262 if (state->ram_buf_rm && state->ram_buf_fname)
263 os_unlink(state->ram_buf_fname);
265 memset(&data, '\0', sizeof(data));
267 #ifdef CONFIG_SYS_MALLOC_F_LEN
268 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
271 /* Do pre- and post-relocation init */
274 board_init_r(gd->new_gd, 0);
276 /* NOTREACHED - board_init_r() does not return */
280 printf("Error %d\n", ret);