start.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (c) 2011-2012 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  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. static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
  57. {
  58. /* just flag to sandbox_early_getopt_check to show usage */
  59. return 1;
  60. }
  61. SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
  62. #ifndef CONFIG_SPL_BUILD
  63. int sandbox_main_loop_init(void)
  64. {
  65. struct sandbox_state *state = state_get_current();
  66. /* Execute command if required */
  67. if (state->cmd || state->run_distro_boot) {
  68. int retval = 0;
  69. cli_init();
  70. #ifdef CONFIG_CMDLINE
  71. if (state->cmd)
  72. retval = run_command_list(state->cmd, -1, 0);
  73. if (state->run_distro_boot)
  74. retval = cli_simple_run_command("run distro_bootcmd",
  75. 0);
  76. #endif
  77. if (!state->interactive)
  78. os_exit(retval);
  79. }
  80. return 0;
  81. }
  82. #endif
  83. static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
  84. const char *arg)
  85. {
  86. state->run_distro_boot = true;
  87. return 0;
  88. }
  89. SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
  90. static int sandbox_cmdline_cb_command(struct sandbox_state *state,
  91. const char *arg)
  92. {
  93. state->cmd = arg;
  94. return 0;
  95. }
  96. SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
  97. static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
  98. {
  99. state->fdt_fname = arg;
  100. return 0;
  101. }
  102. SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
  103. static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
  104. const char *arg)
  105. {
  106. const char *fmt = "%s.dtb";
  107. char *fname;
  108. int len;
  109. len = strlen(state->argv[0]) + strlen(fmt) + 1;
  110. fname = os_malloc(len);
  111. if (!fname)
  112. return -ENOMEM;
  113. snprintf(fname, len, fmt, state->argv[0]);
  114. state->fdt_fname = fname;
  115. return 0;
  116. }
  117. SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
  118. "Use the default u-boot.dtb control FDT in U-Boot directory");
  119. static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
  120. const char *arg)
  121. {
  122. state->interactive = true;
  123. return 0;
  124. }
  125. SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
  126. static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
  127. const char *arg)
  128. {
  129. /* Remember to delete this U-Boot image later */
  130. state->jumped_fname = arg;
  131. return 0;
  132. }
  133. SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
  134. static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
  135. const char *arg)
  136. {
  137. int err;
  138. /* For now assume we always want to write it */
  139. state->write_ram_buf = true;
  140. state->ram_buf_fname = arg;
  141. err = os_read_ram_buf(arg);
  142. if (err) {
  143. printf("Failed to read RAM buffer\n");
  144. return err;
  145. }
  146. return 0;
  147. }
  148. SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
  149. "Read/write ram_buf memory contents from file");
  150. static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
  151. const char *arg)
  152. {
  153. state->ram_buf_rm = true;
  154. return 0;
  155. }
  156. SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
  157. static int sandbox_cmdline_cb_state(struct sandbox_state *state,
  158. const char *arg)
  159. {
  160. state->state_fname = arg;
  161. return 0;
  162. }
  163. SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
  164. static int sandbox_cmdline_cb_read(struct sandbox_state *state,
  165. const char *arg)
  166. {
  167. state->read_state = true;
  168. return 0;
  169. }
  170. SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
  171. static int sandbox_cmdline_cb_write(struct sandbox_state *state,
  172. const char *arg)
  173. {
  174. state->write_state = true;
  175. return 0;
  176. }
  177. SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
  178. static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
  179. const char *arg)
  180. {
  181. state->ignore_missing_state_on_read = true;
  182. return 0;
  183. }
  184. SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
  185. "Ignore missing state on read");
  186. static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
  187. const char *arg)
  188. {
  189. state->show_lcd = true;
  190. return 0;
  191. }
  192. SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
  193. "Show the sandbox LCD display");
  194. static const char *term_args[STATE_TERM_COUNT] = {
  195. "raw-with-sigs",
  196. "raw",
  197. "cooked",
  198. };
  199. static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
  200. const char *arg)
  201. {
  202. int i;
  203. for (i = 0; i < STATE_TERM_COUNT; i++) {
  204. if (!strcmp(arg, term_args[i])) {
  205. state->term_raw = i;
  206. return 0;
  207. }
  208. }
  209. printf("Unknown terminal setting '%s' (", arg);
  210. for (i = 0; i < STATE_TERM_COUNT; i++)
  211. printf("%s%s", i ? ", " : "", term_args[i]);
  212. puts(")\n");
  213. return 1;
  214. }
  215. SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
  216. "Set terminal to raw/cooked mode");
  217. static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
  218. const char *arg)
  219. {
  220. state->show_test_output = true;
  221. return 0;
  222. }
  223. SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
  224. int board_run_command(const char *cmdline)
  225. {
  226. printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
  227. return 1;
  228. }
  229. int main(int argc, char *argv[])
  230. {
  231. struct sandbox_state *state;
  232. gd_t data;
  233. int ret;
  234. ret = state_init();
  235. if (ret)
  236. goto err;
  237. state = state_get_current();
  238. if (os_parse_args(state, argc, argv))
  239. return 1;
  240. ret = sandbox_read_state(state, state->state_fname);
  241. if (ret)
  242. goto err;
  243. /* Remove old memory file if required */
  244. if (state->ram_buf_rm && state->ram_buf_fname)
  245. os_unlink(state->ram_buf_fname);
  246. memset(&data, '\0', sizeof(data));
  247. gd = &data;
  248. #ifdef CONFIG_SYS_MALLOC_F_LEN
  249. gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  250. #endif
  251. /* Do pre- and post-relocation init */
  252. board_init_f(0);
  253. board_init_r(gd->new_gd, 0);
  254. /* NOTREACHED - board_init_r() does not return */
  255. return 0;
  256. err:
  257. printf("Error %d\n", ret);
  258. return 1;
  259. }