start.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. int sandbox_early_getopt_check(void)
  11. {
  12. struct sandbox_state *state = state_get_current();
  13. struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
  14. size_t num_options = __u_boot_sandbox_option_count();
  15. size_t i;
  16. int max_arg_len, max_noarg_len;
  17. /* parse_err will be a string of the faulting option */
  18. if (!state->parse_err)
  19. return 0;
  20. if (strcmp(state->parse_err, "help")) {
  21. printf("u-boot: error: failed while parsing option: %s\n"
  22. "\ttry running with --help for more information.\n",
  23. state->parse_err);
  24. os_exit(1);
  25. }
  26. printf(
  27. "u-boot, a command line test interface to U-Boot\n\n"
  28. "Usage: u-boot [options]\n"
  29. "Options:\n");
  30. max_arg_len = 0;
  31. for (i = 0; i < num_options; ++i)
  32. max_arg_len = max(strlen(sb_opt[i]->flag), max_arg_len);
  33. max_noarg_len = max_arg_len + 7;
  34. for (i = 0; i < num_options; ++i) {
  35. struct sandbox_cmdline_option *opt = sb_opt[i];
  36. /* first output the short flag if it has one */
  37. if (opt->flag_short >= 0x100)
  38. printf(" ");
  39. else
  40. printf(" -%c, ", opt->flag_short);
  41. /* then the long flag */
  42. if (opt->has_arg)
  43. printf("--%-*s", max_noarg_len, opt->flag);
  44. else
  45. printf("--%-*s <arg> ", max_arg_len, opt->flag);
  46. /* finally the help text */
  47. printf(" %s\n", opt->help);
  48. }
  49. os_exit(0);
  50. }
  51. static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
  52. {
  53. /* just flag to sandbox_early_getopt_check to show usage */
  54. return 1;
  55. }
  56. SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
  57. int sandbox_main_loop_init(void)
  58. {
  59. struct sandbox_state *state = state_get_current();
  60. /* Execute command if required */
  61. if (state->cmd) {
  62. run_command_list(state->cmd, -1, 0);
  63. os_exit(state->exit_type);
  64. }
  65. return 0;
  66. }
  67. static int sandbox_cmdline_cb_command(struct sandbox_state *state,
  68. const char *arg)
  69. {
  70. state->cmd = arg;
  71. return 0;
  72. }
  73. SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
  74. static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
  75. {
  76. state->fdt_fname = arg;
  77. return 0;
  78. }
  79. SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
  80. int main(int argc, char *argv[])
  81. {
  82. struct sandbox_state *state;
  83. int err;
  84. err = state_init();
  85. if (err)
  86. return err;
  87. state = state_get_current();
  88. if (os_parse_args(state, argc, argv))
  89. return 1;
  90. /*
  91. * Do pre- and post-relocation init, then start up U-Boot. This will
  92. * never return.
  93. */
  94. board_init_f(0);
  95. /* NOTREACHED - board_init_f() does not return */
  96. return 0;
  97. }