start.c 5.8 KB

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