start.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_memory(struct sandbox_state *state,
  90. const char *arg)
  91. {
  92. int err;
  93. /* For now assume we always want to write it */
  94. state->write_ram_buf = true;
  95. state->ram_buf_fname = arg;
  96. if (os_read_ram_buf(arg)) {
  97. printf("Failed to read RAM buffer\n");
  98. return err;
  99. }
  100. return 0;
  101. }
  102. SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
  103. "Read/write ram_buf memory contents from file");
  104. int main(int argc, char *argv[])
  105. {
  106. struct sandbox_state *state;
  107. int err;
  108. err = state_init();
  109. if (err)
  110. return err;
  111. state = state_get_current();
  112. if (os_parse_args(state, argc, argv))
  113. return 1;
  114. /* Do pre- and post-relocation init */
  115. board_init_f(0);
  116. board_init_r(gd->new_gd, 0);
  117. /* NOTREACHED - board_init_r() does not return */
  118. return 0;
  119. }