start.c 7.0 KB

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