start.c 5.7 KB

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