start.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. if (os_read_ram_buf(arg)) {
  108. printf("Failed to read RAM buffer\n");
  109. return err;
  110. }
  111. return 0;
  112. }
  113. SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
  114. "Read/write ram_buf memory contents from file");
  115. static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
  116. const char *arg)
  117. {
  118. state->ram_buf_rm = true;
  119. return 0;
  120. }
  121. SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
  122. static int sandbox_cmdline_cb_state(struct sandbox_state *state,
  123. const char *arg)
  124. {
  125. state->state_fname = arg;
  126. return 0;
  127. }
  128. SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
  129. static int sandbox_cmdline_cb_read(struct sandbox_state *state,
  130. const char *arg)
  131. {
  132. state->read_state = true;
  133. return 0;
  134. }
  135. SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
  136. static int sandbox_cmdline_cb_write(struct sandbox_state *state,
  137. const char *arg)
  138. {
  139. state->write_state = true;
  140. return 0;
  141. }
  142. SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
  143. static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
  144. const char *arg)
  145. {
  146. state->ignore_missing_state_on_read = true;
  147. return 0;
  148. }
  149. SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
  150. "Ignore missing state on read");
  151. static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
  152. const char *arg)
  153. {
  154. state->show_lcd = true;
  155. return 0;
  156. }
  157. SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
  158. "Show the sandbox LCD display");
  159. static const char *term_args[STATE_TERM_COUNT] = {
  160. "raw-with-sigs",
  161. "raw",
  162. "cooked",
  163. };
  164. static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
  165. const char *arg)
  166. {
  167. int i;
  168. for (i = 0; i < STATE_TERM_COUNT; i++) {
  169. if (!strcmp(arg, term_args[i])) {
  170. state->term_raw = i;
  171. return 0;
  172. }
  173. }
  174. printf("Unknown terminal setting '%s' (", arg);
  175. for (i = 0; i < STATE_TERM_COUNT; i++)
  176. printf("%s%s", i ? ", " : "", term_args[i]);
  177. puts(")\n");
  178. return 1;
  179. }
  180. SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
  181. "Set terminal to raw/cooked mode");
  182. int main(int argc, char *argv[])
  183. {
  184. struct sandbox_state *state;
  185. gd_t data;
  186. int ret;
  187. ret = state_init();
  188. if (ret)
  189. goto err;
  190. state = state_get_current();
  191. if (os_parse_args(state, argc, argv))
  192. return 1;
  193. ret = sandbox_read_state(state, state->state_fname);
  194. if (ret)
  195. goto err;
  196. /* Remove old memory file if required */
  197. if (state->ram_buf_rm && state->ram_buf_fname)
  198. os_unlink(state->ram_buf_fname);
  199. memset(&data, '\0', sizeof(data));
  200. gd = &data;
  201. #ifdef CONFIG_SYS_MALLOC_F_LEN
  202. gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  203. #endif
  204. /* Do pre- and post-relocation init */
  205. board_init_f(0);
  206. board_init_r(gd->new_gd, 0);
  207. /* NOTREACHED - board_init_r() does not return */
  208. return 0;
  209. err:
  210. printf("Error %d\n", ret);
  211. return 1;
  212. }