start.c 6.3 KB

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