start.c 6.6 KB

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