start.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011-2012 The Chromium OS Authors.
  4. */
  5. #include <common.h>
  6. #include <cli.h>
  7. #include <command.h>
  8. #include <efi_loader.h>
  9. #include <errno.h>
  10. #include <event.h>
  11. #include <init.h>
  12. #include <log.h>
  13. #include <os.h>
  14. #include <sort.h>
  15. #include <asm/getopt.h>
  16. #include <asm/global_data.h>
  17. #include <asm/io.h>
  18. #include <asm/malloc.h>
  19. #include <asm/sections.h>
  20. #include <asm/state.h>
  21. #include <dm/root.h>
  22. #include <linux/ctype.h>
  23. DECLARE_GLOBAL_DATA_PTR;
  24. static char **os_argv;
  25. /* Compare two options so that they can be sorted into alphabetical order */
  26. static int h_compare_opt(const void *p1, const void *p2)
  27. {
  28. const struct sandbox_cmdline_option *opt1 = p1;
  29. const struct sandbox_cmdline_option *opt2 = p2;
  30. const char *str1, *str2;
  31. char flag1[2], flag2[2];
  32. opt1 = *(struct sandbox_cmdline_option **)p1;
  33. opt2 = *(struct sandbox_cmdline_option **)p2;
  34. flag1[1] = '\0';
  35. flag2[1] = '\0';
  36. *flag1 = opt1->flag_short < 0x100 ? opt1->flag_short : '\0';
  37. *flag2 = opt2->flag_short < 0x100 ? opt2->flag_short : '\0';
  38. str1 = *flag1 ? flag1 : opt1->flag;
  39. str2 = *flag2 ? flag2 : opt2->flag;
  40. /*
  41. * Force lower-case flags to come before upper-case ones. We only
  42. * support upper-case for short flags.
  43. */
  44. if (isalpha(*str1) && isalpha(*str2) &&
  45. tolower(*str1) == tolower(*str2))
  46. return isupper(*str1) - isupper(*str2);
  47. return strcasecmp(str1, str2);
  48. }
  49. int sandbox_early_getopt_check(void)
  50. {
  51. struct sandbox_state *state = state_get_current();
  52. struct sandbox_cmdline_option **sb_opt =
  53. __u_boot_sandbox_option_start();
  54. size_t num_options = __u_boot_sandbox_option_count();
  55. size_t i;
  56. int max_arg_len, max_noarg_len;
  57. struct sandbox_cmdline_option **sorted_opt;
  58. int size;
  59. /* parse_err will be a string of the faulting option */
  60. if (!state->parse_err)
  61. return 0;
  62. if (strcmp(state->parse_err, "help")) {
  63. printf("u-boot: error: failed while parsing option: %s\n"
  64. "\ttry running with --help for more information.\n",
  65. state->parse_err);
  66. os_exit(1);
  67. }
  68. printf(
  69. "u-boot, a command line test interface to U-Boot\n\n"
  70. "Usage: u-boot [options]\n"
  71. "Options:\n");
  72. max_arg_len = 0;
  73. for (i = 0; i < num_options; ++i)
  74. max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
  75. max_noarg_len = max_arg_len + 7;
  76. /* Sort the options */
  77. size = sizeof(*sorted_opt) * num_options;
  78. sorted_opt = os_malloc(size);
  79. if (!sorted_opt) {
  80. printf("No memory to sort options\n");
  81. os_exit(1);
  82. }
  83. memcpy(sorted_opt, sb_opt, size);
  84. qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt);
  85. for (i = 0; i < num_options; ++i) {
  86. struct sandbox_cmdline_option *opt = sorted_opt[i];
  87. /* first output the short flag if it has one */
  88. if (opt->flag_short >= 0x100)
  89. printf(" ");
  90. else
  91. printf(" -%c, ", opt->flag_short);
  92. /* then the long flag */
  93. if (opt->has_arg)
  94. printf("--%-*s <arg> ", max_arg_len, opt->flag);
  95. else
  96. printf("--%-*s", max_noarg_len, opt->flag);
  97. /* finally the help text */
  98. printf(" %s\n", opt->help);
  99. }
  100. os_exit(0);
  101. }
  102. static int sandbox_misc_init_f(void *ctx, struct event *event)
  103. {
  104. return sandbox_early_getopt_check();
  105. }
  106. EVENT_SPY(EVT_MISC_INIT_F, sandbox_misc_init_f);
  107. static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
  108. {
  109. /* just flag to sandbox_early_getopt_check to show usage */
  110. return 1;
  111. }
  112. SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
  113. #ifndef CONFIG_SPL_BUILD
  114. int sandbox_main_loop_init(void)
  115. {
  116. struct sandbox_state *state = state_get_current();
  117. /* Execute command if required */
  118. if (state->cmd || state->run_distro_boot) {
  119. int retval = 0;
  120. cli_init();
  121. #ifdef CONFIG_CMDLINE
  122. if (state->cmd)
  123. retval = run_command_list(state->cmd, -1, 0);
  124. if (state->run_distro_boot)
  125. retval = cli_simple_run_command("run distro_bootcmd",
  126. 0);
  127. #endif
  128. if (!state->interactive)
  129. os_exit(retval);
  130. }
  131. return 0;
  132. }
  133. #endif
  134. static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
  135. const char *arg)
  136. {
  137. state->run_distro_boot = true;
  138. return 0;
  139. }
  140. SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
  141. static int sandbox_cmdline_cb_command(struct sandbox_state *state,
  142. const char *arg)
  143. {
  144. state->cmd = arg;
  145. return 0;
  146. }
  147. SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
  148. static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
  149. {
  150. state->fdt_fname = arg;
  151. return 0;
  152. }
  153. SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
  154. static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
  155. const char *arg)
  156. {
  157. const char *fmt = "%s.dtb";
  158. char *fname;
  159. int len;
  160. len = strlen(state->argv[0]) + strlen(fmt) + 1;
  161. fname = os_malloc(len);
  162. if (!fname)
  163. return -ENOMEM;
  164. snprintf(fname, len, fmt, state->argv[0]);
  165. state->fdt_fname = fname;
  166. return 0;
  167. }
  168. SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
  169. "Use the default u-boot.dtb control FDT in U-Boot directory");
  170. static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
  171. const char *arg)
  172. {
  173. char buf[256];
  174. char *fname;
  175. int len;
  176. len = state_get_rel_filename("arch/sandbox/dts/test.dtb", buf,
  177. sizeof(buf));
  178. if (len < 0)
  179. return len;
  180. fname = os_malloc(len);
  181. if (!fname)
  182. return -ENOMEM;
  183. strcpy(fname, buf);
  184. state->fdt_fname = fname;
  185. return 0;
  186. }
  187. SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0,
  188. "Use the test.dtb control FDT in U-Boot directory");
  189. static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
  190. const char *arg)
  191. {
  192. state->interactive = true;
  193. return 0;
  194. }
  195. SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
  196. static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
  197. const char *arg)
  198. {
  199. /* Remember to delete this U-Boot image later */
  200. state->jumped_fname = arg;
  201. return 0;
  202. }
  203. SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
  204. static int sandbox_cmdline_cb_program(struct sandbox_state *state,
  205. const char *arg)
  206. {
  207. /*
  208. * Record the program name to use when jumping to future phases. This
  209. * is the original executable which holds all the phases. We need to
  210. * use this instead of argv[0] since each phase is started by
  211. * extracting a particular binary from the full program, then running
  212. * it. Therefore in that binary, argv[0] contains only the
  213. * current-phase executable.
  214. *
  215. * For example, sandbox TPL may be started using image file:
  216. *
  217. * ./image.bin
  218. *
  219. * but then TPL needs to run VPL, which it does by extracting the VPL
  220. * image from the image.bin file.
  221. *
  222. * ./temp-vpl
  223. *
  224. * When VPL runs it needs access to the original image.bin so it can
  225. * extract the next phase (SPL). This works if we use '-f image.bin'
  226. * when starting the original image.bin file.
  227. */
  228. state->prog_fname = arg;
  229. return 0;
  230. }
  231. SANDBOX_CMDLINE_OPT_SHORT(program, 'p', 1, "U-Boot program name");
  232. static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
  233. const char *arg)
  234. {
  235. int err;
  236. /* For now assume we always want to write it */
  237. state->write_ram_buf = true;
  238. state->ram_buf_fname = arg;
  239. err = os_read_ram_buf(arg);
  240. if (err) {
  241. printf("Failed to read RAM buffer '%s': %d\n", arg, err);
  242. return err;
  243. }
  244. state->ram_buf_read = true;
  245. return 0;
  246. }
  247. SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
  248. "Read/write ram_buf memory contents from file");
  249. static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
  250. const char *arg)
  251. {
  252. state->ram_buf_rm = true;
  253. return 0;
  254. }
  255. SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
  256. static int sandbox_cmdline_cb_state(struct sandbox_state *state,
  257. const char *arg)
  258. {
  259. state->state_fname = arg;
  260. return 0;
  261. }
  262. SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
  263. static int sandbox_cmdline_cb_read(struct sandbox_state *state,
  264. const char *arg)
  265. {
  266. state->read_state = true;
  267. return 0;
  268. }
  269. SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
  270. static int sandbox_cmdline_cb_write(struct sandbox_state *state,
  271. const char *arg)
  272. {
  273. state->write_state = true;
  274. return 0;
  275. }
  276. SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
  277. static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
  278. const char *arg)
  279. {
  280. state->ignore_missing_state_on_read = true;
  281. return 0;
  282. }
  283. SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
  284. "Ignore missing state on read");
  285. static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
  286. const char *arg)
  287. {
  288. state->show_lcd = true;
  289. return 0;
  290. }
  291. SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
  292. "Show the sandbox LCD display");
  293. static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state,
  294. const char *arg)
  295. {
  296. state->double_lcd = true;
  297. return 0;
  298. }
  299. SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0,
  300. "Double the LCD display size in each direction");
  301. static const char *term_args[STATE_TERM_COUNT] = {
  302. "raw-with-sigs",
  303. "raw",
  304. "cooked",
  305. };
  306. static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
  307. const char *arg)
  308. {
  309. int i;
  310. for (i = 0; i < STATE_TERM_COUNT; i++) {
  311. if (!strcmp(arg, term_args[i])) {
  312. state->term_raw = i;
  313. return 0;
  314. }
  315. }
  316. printf("Unknown terminal setting '%s' (", arg);
  317. for (i = 0; i < STATE_TERM_COUNT; i++)
  318. printf("%s%s", i ? ", " : "", term_args[i]);
  319. puts(")\n");
  320. return 1;
  321. }
  322. SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
  323. "Set terminal to raw/cooked mode");
  324. static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
  325. const char *arg)
  326. {
  327. state->show_test_output = true;
  328. return 0;
  329. }
  330. SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
  331. static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
  332. const char *arg)
  333. {
  334. state->default_log_level = simple_strtol(arg, NULL, 10);
  335. return 0;
  336. }
  337. SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
  338. "Set log level (0=panic, 7=debug)");
  339. static int sandbox_cmdline_cb_unittests(struct sandbox_state *state,
  340. const char *arg)
  341. {
  342. state->run_unittests = true;
  343. return 0;
  344. }
  345. SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests");
  346. static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state,
  347. const char *arg)
  348. {
  349. state->select_unittests = arg;
  350. return 0;
  351. }
  352. SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run");
  353. static int sandbox_cmdline_cb_signals(struct sandbox_state *state,
  354. const char *arg)
  355. {
  356. state->handle_signals = true;
  357. return 0;
  358. }
  359. SANDBOX_CMDLINE_OPT_SHORT(signals, 'S', 0,
  360. "Handle signals (such as SIGSEGV) in sandbox");
  361. static int sandbox_cmdline_cb_autoboot_keyed(struct sandbox_state *state,
  362. const char *arg)
  363. {
  364. state->autoboot_keyed = true;
  365. return 0;
  366. }
  367. SANDBOX_CMDLINE_OPT(autoboot_keyed, 0, "Allow keyed autoboot");
  368. static void setup_ram_buf(struct sandbox_state *state)
  369. {
  370. /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
  371. if (!state->ram_buf_read)
  372. memset(state->ram_buf, '\0', state->ram_size);
  373. gd->arch.ram_buf = state->ram_buf;
  374. gd->ram_size = state->ram_size;
  375. }
  376. void state_show(struct sandbox_state *state)
  377. {
  378. char **p;
  379. printf("Arguments:\n");
  380. for (p = state->argv; *p; p++)
  381. printf("%s ", *p);
  382. printf("\n");
  383. }
  384. void __efi_runtime EFIAPI efi_reset_system(
  385. enum efi_reset_type reset_type,
  386. efi_status_t reset_status,
  387. unsigned long data_size, void *reset_data)
  388. {
  389. if (reset_type == EFI_RESET_SHUTDOWN)
  390. sandbox_exit();
  391. else
  392. sandbox_reset();
  393. }
  394. void sandbox_reset(void)
  395. {
  396. /* Do this here while it still has an effect */
  397. os_fd_restore();
  398. if (state_uninit())
  399. os_exit(2);
  400. /* Restart U-Boot */
  401. os_relaunch(os_argv);
  402. }
  403. int sandbox_main(int argc, char *argv[])
  404. {
  405. struct sandbox_state *state;
  406. void * text_base;
  407. gd_t data;
  408. int size;
  409. int ret;
  410. text_base = os_find_text_base();
  411. /*
  412. * This must be the first invocation of os_malloc() to have
  413. * state->ram_buf in the low 4 GiB.
  414. */
  415. ret = state_init();
  416. if (ret)
  417. goto err;
  418. /*
  419. * Copy argv[] so that we can pass the arguments in the original
  420. * sequence when resetting the sandbox.
  421. */
  422. size = sizeof(char *) * (argc + 1);
  423. os_argv = os_malloc(size);
  424. if (!os_argv)
  425. os_exit(1);
  426. memcpy(os_argv, argv, size);
  427. memset(&data, '\0', sizeof(data));
  428. gd = &data;
  429. gd->arch.text_base = text_base;
  430. state = state_get_current();
  431. if (os_parse_args(state, argc, argv))
  432. return 1;
  433. /* Remove old memory file if required */
  434. if (state->ram_buf_rm && state->ram_buf_fname) {
  435. os_unlink(state->ram_buf_fname);
  436. state->write_ram_buf = false;
  437. state->ram_buf_fname = NULL;
  438. }
  439. ret = sandbox_read_state(state, state->state_fname);
  440. if (ret)
  441. goto err;
  442. if (state->handle_signals) {
  443. ret = os_setup_signal_handlers();
  444. if (ret)
  445. goto err;
  446. }
  447. #if CONFIG_VAL(SYS_MALLOC_F_LEN)
  448. gd->malloc_base = CFG_MALLOC_F_ADDR;
  449. #endif
  450. #if CONFIG_IS_ENABLED(LOG)
  451. gd->default_log_level = state->default_log_level;
  452. #endif
  453. setup_ram_buf(state);
  454. /*
  455. * Set up the relocation offset here, since sandbox symbols are always
  456. * relocated by the OS before sandbox is entered.
  457. */
  458. gd->reloc_off = (ulong)gd->arch.text_base;
  459. /* sandbox test: log functions called before log_init in board_init_f */
  460. log_debug("debug: %s\n", __func__);
  461. /* Do pre- and post-relocation init */
  462. board_init_f(0);
  463. board_init_r(gd->new_gd, 0);
  464. /* NOTREACHED - board_init_r() does not return */
  465. return 0;
  466. err:
  467. printf("Error %d\n", ret);
  468. return 1;
  469. }