start.c 6.6 KB

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