1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011-2012 The Chromium OS Authors.
9 #include <efi_loader.h>
16 #include <asm/getopt.h>
17 #include <asm/global_data.h>
19 #include <asm/malloc.h>
20 #include <asm/sections.h>
21 #include <asm/state.h>
22 #include <linux/ctype.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 static char **os_argv;
28 /* Compare two options so that they can be sorted into alphabetical order */
29 static int h_compare_opt(const void *p1, const void *p2)
31 const struct sandbox_cmdline_option *opt1 = p1;
32 const struct sandbox_cmdline_option *opt2 = p2;
33 const char *str1, *str2;
34 char flag1[2], flag2[2];
36 opt1 = *(struct sandbox_cmdline_option **)p1;
37 opt2 = *(struct sandbox_cmdline_option **)p2;
41 *flag1 = opt1->flag_short < 0x100 ? opt1->flag_short : '\0';
42 *flag2 = opt2->flag_short < 0x100 ? opt2->flag_short : '\0';
44 str1 = *flag1 ? flag1 : opt1->flag;
45 str2 = *flag2 ? flag2 : opt2->flag;
48 * Force lower-case flags to come before upper-case ones. We only
49 * support upper-case for short flags.
51 if (isalpha(*str1) && isalpha(*str2) &&
52 tolower(*str1) == tolower(*str2))
53 return isupper(*str1) - isupper(*str2);
55 return strcasecmp(str1, str2);
58 int sandbox_early_getopt_check(void)
60 struct sandbox_state *state = state_get_current();
61 struct sandbox_cmdline_option **sb_opt =
62 __u_boot_sandbox_option_start();
63 size_t num_options = __u_boot_sandbox_option_count();
65 int max_arg_len, max_noarg_len;
66 struct sandbox_cmdline_option **sorted_opt;
69 /* parse_err will be a string of the faulting option */
70 if (!state->parse_err)
73 if (strcmp(state->parse_err, "help")) {
74 printf("u-boot: error: failed while parsing option: %s\n"
75 "\ttry running with --help for more information.\n",
81 "u-boot, a command line test interface to U-Boot\n\n"
82 "Usage: u-boot [options]\n"
86 for (i = 0; i < num_options; ++i)
87 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
88 max_noarg_len = max_arg_len + 7;
90 /* Sort the options */
91 size = sizeof(*sorted_opt) * num_options;
92 sorted_opt = os_malloc(size);
94 printf("No memory to sort options\n");
97 memcpy(sorted_opt, sb_opt, size);
98 qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt);
100 for (i = 0; i < num_options; ++i) {
101 struct sandbox_cmdline_option *opt = sorted_opt[i];
103 /* first output the short flag if it has one */
104 if (opt->flag_short >= 0x100)
107 printf(" -%c, ", opt->flag_short);
109 /* then the long flag */
111 printf("--%-*s <arg> ", max_arg_len, opt->flag);
113 printf("--%-*s", max_noarg_len, opt->flag);
115 /* finally the help text */
116 printf(" %s\n", opt->help);
122 int misc_init_f(void)
124 return sandbox_early_getopt_check();
127 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
129 /* just flag to sandbox_early_getopt_check to show usage */
132 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
134 #ifndef CONFIG_SPL_BUILD
135 int sandbox_main_loop_init(void)
137 struct sandbox_state *state = state_get_current();
139 /* Execute command if required */
140 if (state->cmd || state->run_distro_boot) {
145 #ifdef CONFIG_CMDLINE
147 retval = run_command_list(state->cmd, -1, 0);
149 if (state->run_distro_boot)
150 retval = cli_simple_run_command("run distro_bootcmd",
153 if (!state->interactive)
161 static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
164 state->run_distro_boot = true;
167 SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
169 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
175 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
177 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
179 state->fdt_fname = arg;
182 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
184 static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
187 const char *fmt = "%s.dtb";
191 len = strlen(state->argv[0]) + strlen(fmt) + 1;
192 fname = os_malloc(len);
195 snprintf(fname, len, fmt, state->argv[0]);
196 state->fdt_fname = fname;
200 SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
201 "Use the default u-boot.dtb control FDT in U-Boot directory");
203 static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
206 const char *fmt = "/arch/sandbox/dts/test.dtb";
211 len = strlen(state->argv[0]) + strlen(fmt) + 1;
212 fname = os_malloc(len);
215 strcpy(fname, state->argv[0]);
216 p = strrchr(fname, '/');
218 p = fname + strlen(fname);
220 snprintf(p, len, fmt);
221 state->fdt_fname = fname;
225 SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0,
226 "Use the test.dtb control FDT in U-Boot directory");
228 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
231 state->interactive = true;
235 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
237 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
240 /* Remember to delete this U-Boot image later */
241 state->jumped_fname = arg;
245 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
247 static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
252 /* For now assume we always want to write it */
253 state->write_ram_buf = true;
254 state->ram_buf_fname = arg;
256 err = os_read_ram_buf(arg);
258 printf("Failed to read RAM buffer '%s': %d\n", arg, err);
261 state->ram_buf_read = true;
265 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
266 "Read/write ram_buf memory contents from file");
268 static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
271 state->ram_buf_rm = true;
275 SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
277 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
280 state->state_fname = arg;
283 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
285 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
288 state->read_state = true;
291 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
293 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
296 state->write_state = true;
299 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
301 static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
304 state->ignore_missing_state_on_read = true;
307 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
308 "Ignore missing state on read");
310 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
313 state->show_lcd = true;
316 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
317 "Show the sandbox LCD display");
319 static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state,
322 state->double_lcd = true;
326 SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0,
327 "Double the LCD display size in each direction");
329 static const char *term_args[STATE_TERM_COUNT] = {
335 static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
340 for (i = 0; i < STATE_TERM_COUNT; i++) {
341 if (!strcmp(arg, term_args[i])) {
347 printf("Unknown terminal setting '%s' (", arg);
348 for (i = 0; i < STATE_TERM_COUNT; i++)
349 printf("%s%s", i ? ", " : "", term_args[i]);
354 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
355 "Set terminal to raw/cooked mode");
357 static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
360 state->show_test_output = true;
363 SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
365 static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
368 state->default_log_level = simple_strtol(arg, NULL, 10);
372 SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
373 "Set log level (0=panic, 7=debug)");
375 static int sandbox_cmdline_cb_unittests(struct sandbox_state *state,
378 state->run_unittests = true;
382 SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests");
384 static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state,
387 state->select_unittests = arg;
391 SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run");
393 static int sandbox_cmdline_cb_signals(struct sandbox_state *state,
396 state->handle_signals = true;
400 SANDBOX_CMDLINE_OPT_SHORT(signals, 'S', 0,
401 "Handle signals (such as SIGSEGV) in sandbox");
403 static int sandbox_cmdline_cb_autoboot_keyed(struct sandbox_state *state,
406 state->autoboot_keyed = true;
410 SANDBOX_CMDLINE_OPT(autoboot_keyed, 0, "Allow keyed autoboot");
412 static void setup_ram_buf(struct sandbox_state *state)
414 /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
415 if (!state->ram_buf_read)
416 memset(state->ram_buf, '\0', state->ram_size);
418 gd->arch.ram_buf = state->ram_buf;
419 gd->ram_size = state->ram_size;
422 void state_show(struct sandbox_state *state)
426 printf("Arguments:\n");
427 for (p = state->argv; *p; p++)
432 void __efi_runtime EFIAPI efi_reset_system(
433 enum efi_reset_type reset_type,
434 efi_status_t reset_status,
435 unsigned long data_size, void *reset_data)
438 os_relaunch(os_argv);
441 void sandbox_reset(void)
443 /* Do this here while it still has an effect */
449 os_relaunch(os_argv);
452 int main(int argc, char *argv[])
454 struct sandbox_state *state;
460 text_base = os_find_text_base();
463 * This must be the first invocation of os_malloc() to have
464 * state->ram_buf in the low 4 GiB.
471 * Copy argv[] so that we can pass the arguments in the original
472 * sequence when resetting the sandbox.
474 size = sizeof(char *) * (argc + 1);
475 os_argv = os_malloc(size);
478 memcpy(os_argv, argv, size);
480 memset(&data, '\0', sizeof(data));
482 gd->arch.text_base = text_base;
484 state = state_get_current();
485 if (os_parse_args(state, argc, argv))
488 /* Remove old memory file if required */
489 if (state->ram_buf_rm && state->ram_buf_fname) {
490 os_unlink(state->ram_buf_fname);
491 state->write_ram_buf = false;
492 state->ram_buf_fname = NULL;
495 ret = sandbox_read_state(state, state->state_fname);
499 if (state->handle_signals) {
500 ret = os_setup_signal_handlers();
505 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
506 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
508 #if CONFIG_IS_ENABLED(LOG)
509 gd->default_log_level = state->default_log_level;
511 setup_ram_buf(state);
514 * Set up the relocation offset here, since sandbox symbols are always
515 * relocated by the OS before sandbox is entered.
517 gd->reloc_off = (ulong)gd->arch.text_base;
519 /* sandbox test: log functions called before log_init in board_init_f */
520 log_debug("debug: %s\n", __func__);
522 /* Do pre- and post-relocation init */
525 board_init_r(gd->new_gd, 0);
527 /* NOTREACHED - board_init_r() does not return */
531 printf("Error %d\n", ret);