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>
23 #include <linux/ctype.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 static char **os_argv;
29 /* Compare two options so that they can be sorted into alphabetical order */
30 static int h_compare_opt(const void *p1, const void *p2)
32 const struct sandbox_cmdline_option *opt1 = p1;
33 const struct sandbox_cmdline_option *opt2 = p2;
34 const char *str1, *str2;
35 char flag1[2], flag2[2];
37 opt1 = *(struct sandbox_cmdline_option **)p1;
38 opt2 = *(struct sandbox_cmdline_option **)p2;
42 *flag1 = opt1->flag_short < 0x100 ? opt1->flag_short : '\0';
43 *flag2 = opt2->flag_short < 0x100 ? opt2->flag_short : '\0';
45 str1 = *flag1 ? flag1 : opt1->flag;
46 str2 = *flag2 ? flag2 : opt2->flag;
49 * Force lower-case flags to come before upper-case ones. We only
50 * support upper-case for short flags.
52 if (isalpha(*str1) && isalpha(*str2) &&
53 tolower(*str1) == tolower(*str2))
54 return isupper(*str1) - isupper(*str2);
56 return strcasecmp(str1, str2);
59 int sandbox_early_getopt_check(void)
61 struct sandbox_state *state = state_get_current();
62 struct sandbox_cmdline_option **sb_opt =
63 __u_boot_sandbox_option_start();
64 size_t num_options = __u_boot_sandbox_option_count();
66 int max_arg_len, max_noarg_len;
67 struct sandbox_cmdline_option **sorted_opt;
70 /* parse_err will be a string of the faulting option */
71 if (!state->parse_err)
74 if (strcmp(state->parse_err, "help")) {
75 printf("u-boot: error: failed while parsing option: %s\n"
76 "\ttry running with --help for more information.\n",
82 "u-boot, a command line test interface to U-Boot\n\n"
83 "Usage: u-boot [options]\n"
87 for (i = 0; i < num_options; ++i)
88 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
89 max_noarg_len = max_arg_len + 7;
91 /* Sort the options */
92 size = sizeof(*sorted_opt) * num_options;
93 sorted_opt = os_malloc(size);
95 printf("No memory to sort options\n");
98 memcpy(sorted_opt, sb_opt, size);
99 qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt);
101 for (i = 0; i < num_options; ++i) {
102 struct sandbox_cmdline_option *opt = sorted_opt[i];
104 /* first output the short flag if it has one */
105 if (opt->flag_short >= 0x100)
108 printf(" -%c, ", opt->flag_short);
110 /* then the long flag */
112 printf("--%-*s <arg> ", max_arg_len, opt->flag);
114 printf("--%-*s", max_noarg_len, opt->flag);
116 /* finally the help text */
117 printf(" %s\n", opt->help);
123 static int sandbox_misc_init_f(void *ctx, struct event *event)
125 return sandbox_early_getopt_check();
127 EVENT_SPY(EVT_MISC_INIT_F, sandbox_misc_init_f);
129 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
131 /* just flag to sandbox_early_getopt_check to show usage */
134 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
136 #ifndef CONFIG_SPL_BUILD
137 int sandbox_main_loop_init(void)
139 struct sandbox_state *state = state_get_current();
141 /* Execute command if required */
142 if (state->cmd || state->run_distro_boot) {
147 #ifdef CONFIG_CMDLINE
149 retval = run_command_list(state->cmd, -1, 0);
151 if (state->run_distro_boot)
152 retval = cli_simple_run_command("run distro_bootcmd",
155 if (!state->interactive)
163 static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
166 state->run_distro_boot = true;
169 SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
171 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
177 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
179 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
181 state->fdt_fname = arg;
184 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
186 static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
189 const char *fmt = "%s.dtb";
193 len = strlen(state->argv[0]) + strlen(fmt) + 1;
194 fname = os_malloc(len);
197 snprintf(fname, len, fmt, state->argv[0]);
198 state->fdt_fname = fname;
202 SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
203 "Use the default u-boot.dtb control FDT in U-Boot directory");
205 static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
212 len = state_get_rel_filename("arch/sandbox/dts/test.dtb", buf,
217 fname = os_malloc(len);
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_program(struct sandbox_state *state,
251 * Record the program name to use when jumping to future phases. This
252 * is the original executable which holds all the phases. We need to
253 * use this instead of argv[0] since each phase is started by
254 * extracting a particular binary from the full program, then running
255 * it. Therefore in that binary, argv[0] contains only the
256 * current-phase executable.
258 * For example, sandbox TPL may be started using image file:
262 * but then TPL needs to run VPL, which it does by extracting the VPL
263 * image from the image.bin file.
267 * When VPL runs it needs access to the original image.bin so it can
268 * extract the next phase (SPL). This works if we use '-f image.bin'
269 * when starting the original image.bin file.
271 state->prog_fname = arg;
275 SANDBOX_CMDLINE_OPT_SHORT(program, 'p', 1, "U-Boot program name");
277 static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
282 /* For now assume we always want to write it */
283 state->write_ram_buf = true;
284 state->ram_buf_fname = arg;
286 err = os_read_ram_buf(arg);
288 printf("Failed to read RAM buffer '%s': %d\n", arg, err);
291 state->ram_buf_read = true;
295 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
296 "Read/write ram_buf memory contents from file");
298 static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
301 state->ram_buf_rm = true;
305 SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
307 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
310 state->state_fname = arg;
313 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
315 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
318 state->read_state = true;
321 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
323 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
326 state->write_state = true;
329 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
331 static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
334 state->ignore_missing_state_on_read = true;
337 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
338 "Ignore missing state on read");
340 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
343 state->show_lcd = true;
346 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
347 "Show the sandbox LCD display");
349 static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state,
352 state->double_lcd = true;
356 SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0,
357 "Double the LCD display size in each direction");
359 static const char *term_args[STATE_TERM_COUNT] = {
365 static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
370 for (i = 0; i < STATE_TERM_COUNT; i++) {
371 if (!strcmp(arg, term_args[i])) {
377 printf("Unknown terminal setting '%s' (", arg);
378 for (i = 0; i < STATE_TERM_COUNT; i++)
379 printf("%s%s", i ? ", " : "", term_args[i]);
384 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
385 "Set terminal to raw/cooked mode");
387 static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
390 state->show_test_output = true;
393 SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
395 static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
398 state->default_log_level = simple_strtol(arg, NULL, 10);
402 SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
403 "Set log level (0=panic, 7=debug)");
405 static int sandbox_cmdline_cb_unittests(struct sandbox_state *state,
408 state->run_unittests = true;
412 SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests");
414 static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state,
417 state->select_unittests = arg;
421 SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run");
423 static int sandbox_cmdline_cb_signals(struct sandbox_state *state,
426 state->handle_signals = true;
430 SANDBOX_CMDLINE_OPT_SHORT(signals, 'S', 0,
431 "Handle signals (such as SIGSEGV) in sandbox");
433 static int sandbox_cmdline_cb_autoboot_keyed(struct sandbox_state *state,
436 state->autoboot_keyed = true;
440 SANDBOX_CMDLINE_OPT(autoboot_keyed, 0, "Allow keyed autoboot");
442 static void setup_ram_buf(struct sandbox_state *state)
444 /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
445 if (!state->ram_buf_read)
446 memset(state->ram_buf, '\0', state->ram_size);
448 gd->arch.ram_buf = state->ram_buf;
449 gd->ram_size = state->ram_size;
452 void state_show(struct sandbox_state *state)
456 printf("Arguments:\n");
457 for (p = state->argv; *p; p++)
462 void __efi_runtime EFIAPI efi_reset_system(
463 enum efi_reset_type reset_type,
464 efi_status_t reset_status,
465 unsigned long data_size, void *reset_data)
467 if (reset_type == EFI_RESET_SHUTDOWN)
473 void sandbox_reset(void)
475 /* Do this here while it still has an effect */
481 os_relaunch(os_argv);
484 int sandbox_main(int argc, char *argv[])
486 struct sandbox_state *state;
492 text_base = os_find_text_base();
495 * This must be the first invocation of os_malloc() to have
496 * state->ram_buf in the low 4 GiB.
503 * Copy argv[] so that we can pass the arguments in the original
504 * sequence when resetting the sandbox.
506 size = sizeof(char *) * (argc + 1);
507 os_argv = os_malloc(size);
510 memcpy(os_argv, argv, size);
512 memset(&data, '\0', sizeof(data));
514 gd->arch.text_base = text_base;
516 state = state_get_current();
517 if (os_parse_args(state, argc, argv))
520 /* Remove old memory file if required */
521 if (state->ram_buf_rm && state->ram_buf_fname) {
522 os_unlink(state->ram_buf_fname);
523 state->write_ram_buf = false;
524 state->ram_buf_fname = NULL;
527 ret = sandbox_read_state(state, state->state_fname);
531 if (state->handle_signals) {
532 ret = os_setup_signal_handlers();
537 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
538 gd->malloc_base = CFG_MALLOC_F_ADDR;
540 #if CONFIG_IS_ENABLED(LOG)
541 gd->default_log_level = state->default_log_level;
543 setup_ram_buf(state);
546 * Set up the relocation offset here, since sandbox symbols are always
547 * relocated by the OS before sandbox is entered.
549 gd->reloc_off = (ulong)gd->arch.text_base;
551 /* sandbox test: log functions called before log_init in board_init_f */
552 log_debug("debug: %s\n", __func__);
554 /* Do pre- and post-relocation init */
557 board_init_r(gd->new_gd, 0);
559 /* NOTREACHED - board_init_r() does not return */
563 printf("Error %d\n", ret);