start.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. state->jumped = 1;
  93. /*
  94. * TODO(sjg@chromium.org): Note this causes problems for gdb which
  95. * wants to read debug data from the image.
  96. *
  97. * os_unlink(arg);
  98. */
  99. return 0;
  100. }
  101. SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
  102. static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
  103. const char *arg)
  104. {
  105. int err;
  106. /* For now assume we always want to write it */
  107. state->write_ram_buf = true;
  108. state->ram_buf_fname = arg;
  109. if (os_read_ram_buf(arg)) {
  110. printf("Failed to read RAM buffer\n");
  111. return err;
  112. }
  113. return 0;
  114. }
  115. SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
  116. "Read/write ram_buf memory contents from file");
  117. static int sandbox_cmdline_cb_state(struct sandbox_state *state,
  118. const char *arg)
  119. {
  120. state->state_fname = arg;
  121. return 0;
  122. }
  123. SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
  124. static int sandbox_cmdline_cb_read(struct sandbox_state *state,
  125. const char *arg)
  126. {
  127. state->read_state = true;
  128. return 0;
  129. }
  130. SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
  131. static int sandbox_cmdline_cb_write(struct sandbox_state *state,
  132. const char *arg)
  133. {
  134. state->write_state = true;
  135. return 0;
  136. }
  137. SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
  138. static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
  139. const char *arg)
  140. {
  141. state->ignore_missing_state_on_read = true;
  142. return 0;
  143. }
  144. SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
  145. "Ignore missing state on read");
  146. static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
  147. const char *arg)
  148. {
  149. state->show_lcd = true;
  150. return 0;
  151. }
  152. SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
  153. "Show the sandbox LCD display");
  154. int main(int argc, char *argv[])
  155. {
  156. struct sandbox_state *state;
  157. int ret;
  158. ret = state_init();
  159. if (ret)
  160. goto err;
  161. state = state_get_current();
  162. if (os_parse_args(state, argc, argv))
  163. return 1;
  164. ret = sandbox_read_state(state, state->state_fname);
  165. if (ret)
  166. goto err;
  167. /* Do pre- and post-relocation init */
  168. board_init_f(0);
  169. board_init_r(gd->new_gd, 0);
  170. /* NOTREACHED - board_init_r() does not return */
  171. return 0;
  172. err:
  173. printf("Error %d\n", ret);
  174. return 1;
  175. }