start.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011-2012 The Chromium OS Authors.
  4. */
  5. #include <common.h>
  6. #include <errno.h>
  7. #include <os.h>
  8. #include <cli.h>
  9. #include <malloc.h>
  10. #include <asm/getopt.h>
  11. #include <asm/io.h>
  12. #include <asm/sections.h>
  13. #include <asm/state.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. int sandbox_early_getopt_check(void)
  16. {
  17. struct sandbox_state *state = state_get_current();
  18. struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
  19. size_t num_options = __u_boot_sandbox_option_count();
  20. size_t i;
  21. int max_arg_len, max_noarg_len;
  22. /* parse_err will be a string of the faulting option */
  23. if (!state->parse_err)
  24. return 0;
  25. if (strcmp(state->parse_err, "help")) {
  26. printf("u-boot: error: failed while parsing option: %s\n"
  27. "\ttry running with --help for more information.\n",
  28. state->parse_err);
  29. os_exit(1);
  30. }
  31. printf(
  32. "u-boot, a command line test interface to U-Boot\n\n"
  33. "Usage: u-boot [options]\n"
  34. "Options:\n");
  35. max_arg_len = 0;
  36. for (i = 0; i < num_options; ++i)
  37. max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
  38. max_noarg_len = max_arg_len + 7;
  39. for (i = 0; i < num_options; ++i) {
  40. struct sandbox_cmdline_option *opt = sb_opt[i];
  41. /* first output the short flag if it has one */
  42. if (opt->flag_short >= 0x100)
  43. printf(" ");
  44. else
  45. printf(" -%c, ", opt->flag_short);
  46. /* then the long flag */
  47. if (opt->has_arg)
  48. printf("--%-*s <arg> ", max_arg_len, opt->flag);
  49. else
  50. printf("--%-*s", max_noarg_len, opt->flag);
  51. /* finally the help text */
  52. printf(" %s\n", opt->help);
  53. }
  54. os_exit(0);
  55. }
  56. int misc_init_f(void)
  57. {
  58. return sandbox_early_getopt_check();
  59. }
  60. static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
  61. {
  62. /* just flag to sandbox_early_getopt_check to show usage */
  63. return 1;
  64. }
  65. SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
  66. #ifndef CONFIG_SPL_BUILD
  67. int sandbox_main_loop_init(void)
  68. {
  69. struct sandbox_state *state = state_get_current();
  70. /* Execute command if required */
  71. if (state->cmd || state->run_distro_boot) {
  72. int retval = 0;
  73. cli_init();
  74. #ifdef CONFIG_CMDLINE
  75. if (state->cmd)
  76. retval = run_command_list(state->cmd, -1, 0);
  77. if (state->run_distro_boot)
  78. retval = cli_simple_run_command("run distro_bootcmd",
  79. 0);
  80. #endif
  81. if (!state->interactive)
  82. os_exit(retval);
  83. }
  84. return 0;
  85. }
  86. #endif
  87. static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
  88. const char *arg)
  89. {
  90. state->run_distro_boot = true;
  91. return 0;
  92. }
  93. SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
  94. static int sandbox_cmdline_cb_command(struct sandbox_state *state,
  95. const char *arg)
  96. {
  97. state->cmd = arg;
  98. return 0;
  99. }
  100. SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
  101. static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
  102. {
  103. state->fdt_fname = arg;
  104. return 0;
  105. }
  106. SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
  107. static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
  108. const char *arg)
  109. {
  110. const char *fmt = "%s.dtb";
  111. char *fname;
  112. int len;
  113. len = strlen(state->argv[0]) + strlen(fmt) + 1;
  114. fname = os_malloc(len);
  115. if (!fname)
  116. return -ENOMEM;
  117. snprintf(fname, len, fmt, state->argv[0]);
  118. state->fdt_fname = fname;
  119. return 0;
  120. }
  121. SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
  122. "Use the default u-boot.dtb control FDT in U-Boot directory");
  123. static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
  124. const char *arg)
  125. {
  126. state->interactive = true;
  127. return 0;
  128. }
  129. SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
  130. static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
  131. const char *arg)
  132. {
  133. /* Remember to delete this U-Boot image later */
  134. state->jumped_fname = arg;
  135. return 0;
  136. }
  137. SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
  138. static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
  139. const char *arg)
  140. {
  141. int err;
  142. /* For now assume we always want to write it */
  143. state->write_ram_buf = true;
  144. state->ram_buf_fname = arg;
  145. err = os_read_ram_buf(arg);
  146. if (err) {
  147. printf("Failed to read RAM buffer '%s': %d\n", arg, err);
  148. return err;
  149. }
  150. return 0;
  151. }
  152. SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
  153. "Read/write ram_buf memory contents from file");
  154. static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
  155. const char *arg)
  156. {
  157. state->ram_buf_rm = true;
  158. return 0;
  159. }
  160. SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
  161. static int sandbox_cmdline_cb_state(struct sandbox_state *state,
  162. const char *arg)
  163. {
  164. state->state_fname = arg;
  165. return 0;
  166. }
  167. SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
  168. static int sandbox_cmdline_cb_read(struct sandbox_state *state,
  169. const char *arg)
  170. {
  171. state->read_state = true;
  172. return 0;
  173. }
  174. SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
  175. static int sandbox_cmdline_cb_write(struct sandbox_state *state,
  176. const char *arg)
  177. {
  178. state->write_state = true;
  179. return 0;
  180. }
  181. SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
  182. static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
  183. const char *arg)
  184. {
  185. state->ignore_missing_state_on_read = true;
  186. return 0;
  187. }
  188. SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
  189. "Ignore missing state on read");
  190. static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
  191. const char *arg)
  192. {
  193. state->show_lcd = true;
  194. return 0;
  195. }
  196. SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
  197. "Show the sandbox LCD display");
  198. static const char *term_args[STATE_TERM_COUNT] = {
  199. "raw-with-sigs",
  200. "raw",
  201. "cooked",
  202. };
  203. static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
  204. const char *arg)
  205. {
  206. int i;
  207. for (i = 0; i < STATE_TERM_COUNT; i++) {
  208. if (!strcmp(arg, term_args[i])) {
  209. state->term_raw = i;
  210. return 0;
  211. }
  212. }
  213. printf("Unknown terminal setting '%s' (", arg);
  214. for (i = 0; i < STATE_TERM_COUNT; i++)
  215. printf("%s%s", i ? ", " : "", term_args[i]);
  216. puts(")\n");
  217. return 1;
  218. }
  219. SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
  220. "Set terminal to raw/cooked mode");
  221. static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
  222. const char *arg)
  223. {
  224. state->show_test_output = true;
  225. return 0;
  226. }
  227. SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
  228. static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
  229. const char *arg)
  230. {
  231. state->default_log_level = simple_strtol(arg, NULL, 10);
  232. return 0;
  233. }
  234. SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
  235. "Set log level (0=panic, 7=debug)");
  236. static int sandbox_cmdline_cb_show_of_platdata(struct sandbox_state *state,
  237. const char *arg)
  238. {
  239. state->show_of_platdata = true;
  240. return 0;
  241. }
  242. SANDBOX_CMDLINE_OPT(show_of_platdata, 0, "Show of-platdata in SPL");
  243. int board_run_command(const char *cmdline)
  244. {
  245. printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
  246. return 1;
  247. }
  248. static void setup_ram_buf(struct sandbox_state *state)
  249. {
  250. gd->arch.ram_buf = state->ram_buf;
  251. gd->ram_size = state->ram_size;
  252. }
  253. int main(int argc, char *argv[])
  254. {
  255. struct sandbox_state *state;
  256. gd_t data;
  257. int ret;
  258. ret = state_init();
  259. if (ret)
  260. goto err;
  261. state = state_get_current();
  262. if (os_parse_args(state, argc, argv))
  263. return 1;
  264. ret = sandbox_read_state(state, state->state_fname);
  265. if (ret)
  266. goto err;
  267. memset(&data, '\0', sizeof(data));
  268. gd = &data;
  269. #if CONFIG_VAL(SYS_MALLOC_F_LEN)
  270. gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  271. #endif
  272. #if CONFIG_IS_ENABLED(LOG)
  273. gd->default_log_level = state->default_log_level;
  274. #endif
  275. setup_ram_buf(state);
  276. /* Do pre- and post-relocation init */
  277. board_init_f(0);
  278. board_init_r(gd->new_gd, 0);
  279. /* NOTREACHED - board_init_r() does not return */
  280. return 0;
  281. err:
  282. printf("Error %d\n", ret);
  283. return 1;
  284. }