start.c 5.6 KB

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