1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011-2012 The Chromium OS Authors.
13 #include <asm/getopt.h>
15 #include <asm/malloc.h>
16 #include <asm/sections.h>
17 #include <asm/state.h>
18 #include <linux/ctype.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 /* Compare two options so that they can be sorted into alphabetical order */
23 static int h_compare_opt(const void *p1, const void *p2)
25 const struct sandbox_cmdline_option *opt1 = p1;
26 const struct sandbox_cmdline_option *opt2 = p2;
27 const char *str1, *str2;
28 char flag1[2], flag2[2];
30 opt1 = *(struct sandbox_cmdline_option **)p1;
31 opt2 = *(struct sandbox_cmdline_option **)p2;
35 *flag1 = opt1->flag_short < 0x100 ? opt1->flag_short : '\0';
36 *flag2 = opt2->flag_short < 0x100 ? opt2->flag_short : '\0';
38 str1 = *flag1 ? flag1 : opt1->flag;
39 str2 = *flag2 ? flag2 : opt2->flag;
42 * Force lower-case flags to come before upper-case ones. We only
43 * support upper-case for short flags.
45 if (isalpha(*str1) && isalpha(*str2) &&
46 tolower(*str1) == tolower(*str2))
47 return isupper(*str1) - isupper(*str2);
49 return strcasecmp(str1, str2);
52 int sandbox_early_getopt_check(void)
54 struct sandbox_state *state = state_get_current();
55 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
56 size_t num_options = __u_boot_sandbox_option_count();
58 int max_arg_len, max_noarg_len;
59 struct sandbox_cmdline_option **sorted_opt;
62 /* parse_err will be a string of the faulting option */
63 if (!state->parse_err)
66 if (strcmp(state->parse_err, "help")) {
67 printf("u-boot: error: failed while parsing option: %s\n"
68 "\ttry running with --help for more information.\n",
74 "u-boot, a command line test interface to U-Boot\n\n"
75 "Usage: u-boot [options]\n"
79 for (i = 0; i < num_options; ++i)
80 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
81 max_noarg_len = max_arg_len + 7;
83 /* Sort the options */
84 size = sizeof(*sorted_opt) * num_options;
85 sorted_opt = malloc(size);
87 printf("No memory to sort options\n");
90 memcpy(sorted_opt, sb_opt, size);
91 qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt);
93 for (i = 0; i < num_options; ++i) {
94 struct sandbox_cmdline_option *opt = sorted_opt[i];
96 /* first output the short flag if it has one */
97 if (opt->flag_short >= 0x100)
100 printf(" -%c, ", opt->flag_short);
102 /* then the long flag */
104 printf("--%-*s <arg> ", max_arg_len, opt->flag);
106 printf("--%-*s", max_noarg_len, opt->flag);
108 /* finally the help text */
109 printf(" %s\n", opt->help);
115 int misc_init_f(void)
117 return sandbox_early_getopt_check();
120 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
122 /* just flag to sandbox_early_getopt_check to show usage */
125 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
127 #ifndef CONFIG_SPL_BUILD
128 int sandbox_main_loop_init(void)
130 struct sandbox_state *state = state_get_current();
132 /* Execute command if required */
133 if (state->cmd || state->run_distro_boot) {
138 #ifdef CONFIG_CMDLINE
140 retval = run_command_list(state->cmd, -1, 0);
142 if (state->run_distro_boot)
143 retval = cli_simple_run_command("run distro_bootcmd",
146 if (!state->interactive)
154 static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
157 state->run_distro_boot = true;
160 SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
162 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
168 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
170 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
172 state->fdt_fname = arg;
175 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
177 static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
180 const char *fmt = "%s.dtb";
184 len = strlen(state->argv[0]) + strlen(fmt) + 1;
188 snprintf(fname, len, fmt, state->argv[0]);
189 state->fdt_fname = fname;
193 SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
194 "Use the default u-boot.dtb control FDT in U-Boot directory");
196 static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
199 const char *fmt = "/arch/sandbox/dts/test.dtb";
204 len = strlen(state->argv[0]) + strlen(fmt) + 1;
208 strcpy(fname, state->argv[0]);
209 p = strrchr(fname, '/');
211 p = fname + strlen(fname);
213 snprintf(p, len, fmt, p);
214 state->fdt_fname = fname;
218 SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0,
219 "Use the test.dtb control FDT in U-Boot directory");
221 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
224 state->interactive = true;
228 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
230 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
233 /* Remember to delete this U-Boot image later */
234 state->jumped_fname = arg;
238 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
240 static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
245 /* For now assume we always want to write it */
246 state->write_ram_buf = true;
247 state->ram_buf_fname = arg;
249 err = os_read_ram_buf(arg);
251 printf("Failed to read RAM buffer '%s': %d\n", arg, err);
254 state->ram_buf_read = true;
258 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
259 "Read/write ram_buf memory contents from file");
261 static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
264 state->ram_buf_rm = true;
268 SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
270 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
273 state->state_fname = arg;
276 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
278 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
281 state->read_state = true;
284 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
286 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
289 state->write_state = true;
292 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
294 static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
297 state->ignore_missing_state_on_read = true;
300 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
301 "Ignore missing state on read");
303 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
306 state->show_lcd = true;
309 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
310 "Show the sandbox LCD display");
312 static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state,
315 state->double_lcd = true;
319 SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0,
320 "Double the LCD display size in each direction");
322 static const char *term_args[STATE_TERM_COUNT] = {
328 static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
333 for (i = 0; i < STATE_TERM_COUNT; i++) {
334 if (!strcmp(arg, term_args[i])) {
340 printf("Unknown terminal setting '%s' (", arg);
341 for (i = 0; i < STATE_TERM_COUNT; i++)
342 printf("%s%s", i ? ", " : "", term_args[i]);
347 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
348 "Set terminal to raw/cooked mode");
350 static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
353 state->show_test_output = true;
356 SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
358 static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
361 state->default_log_level = simple_strtol(arg, NULL, 10);
365 SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
366 "Set log level (0=panic, 7=debug)");
368 static int sandbox_cmdline_cb_show_of_platdata(struct sandbox_state *state,
371 state->show_of_platdata = true;
375 SANDBOX_CMDLINE_OPT(show_of_platdata, 0, "Show of-platdata in SPL");
377 static void setup_ram_buf(struct sandbox_state *state)
379 /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
380 if (!state->ram_buf_read)
381 memset(state->ram_buf, '\0', state->ram_size);
383 gd->arch.ram_buf = state->ram_buf;
384 gd->ram_size = state->ram_size;
387 void state_show(struct sandbox_state *state)
391 printf("Arguments:\n");
392 for (p = state->argv; *p; p++)
397 int main(int argc, char *argv[])
399 struct sandbox_state *state;
403 memset(&data, '\0', sizeof(data));
405 gd->arch.text_base = os_find_text_base();
411 state = state_get_current();
412 if (os_parse_args(state, argc, argv))
415 ret = sandbox_read_state(state, state->state_fname);
419 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
420 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
422 #if CONFIG_IS_ENABLED(LOG)
423 gd->default_log_level = state->default_log_level;
425 setup_ram_buf(state);
428 * Set up the relocation offset here, since sandbox symbols are always
429 * relocated by the OS before sandbox is entered.
431 gd->reloc_off = (ulong)gd->arch.text_base;
433 /* Do pre- and post-relocation init */
436 board_init_r(gd->new_gd, 0);
438 /* NOTREACHED - board_init_r() does not return */
442 printf("Error %d\n", ret);