start.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2011-2012 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  4. */
  5. #include <common.h>
  6. #include <asm/getopt.h>
  7. #include <asm/sections.h>
  8. #include <asm/state.h>
  9. #include <os.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. os_exit(state->exit_type);
  65. }
  66. return 0;
  67. }
  68. static int sandbox_cmdline_cb_command(struct sandbox_state *state,
  69. const char *arg)
  70. {
  71. state->cmd = arg;
  72. return 0;
  73. }
  74. SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
  75. static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
  76. {
  77. state->fdt_fname = arg;
  78. return 0;
  79. }
  80. SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
  81. int main(int argc, char *argv[])
  82. {
  83. struct sandbox_state *state;
  84. int err;
  85. err = state_init();
  86. if (err)
  87. return err;
  88. state = state_get_current();
  89. if (os_parse_args(state, argc, argv))
  90. return 1;
  91. /* Do pre- and post-relocation init */
  92. board_init_f(0);
  93. board_init_r(gd->new_gd, 0);
  94. /* NOTREACHED - board_init_r() does not return */
  95. return 0;
  96. }