start.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2011-2012 The Chromium OS Authors.
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. #include <common.h>
  22. #include <asm/getopt.h>
  23. #include <asm/sections.h>
  24. #include <asm/state.h>
  25. #include <os.h>
  26. int sandbox_early_getopt_check(void)
  27. {
  28. struct sandbox_state *state = state_get_current();
  29. struct sb_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
  30. size_t num_options = __u_boot_sandbox_option_count();
  31. size_t i;
  32. int max_arg_len, max_noarg_len;
  33. /* parse_err will be a string of the faulting option */
  34. if (!state->parse_err)
  35. return 0;
  36. if (strcmp(state->parse_err, "help")) {
  37. printf("u-boot: error: failed while parsing option: %s\n"
  38. "\ttry running with --help for more information.\n",
  39. state->parse_err);
  40. os_exit(1);
  41. }
  42. printf(
  43. "u-boot, a command line test interface to U-Boot\n\n"
  44. "Usage: u-boot [options]\n"
  45. "Options:\n");
  46. max_arg_len = 0;
  47. for (i = 0; i < num_options; ++i)
  48. max_arg_len = max(strlen(sb_opt[i]->flag), max_arg_len);
  49. max_noarg_len = max_arg_len + 7;
  50. for (i = 0; i < num_options; ++i) {
  51. struct sb_cmdline_option *opt = sb_opt[i];
  52. /* first output the short flag if it has one */
  53. if (opt->flag_short >= 0x100)
  54. printf(" ");
  55. else
  56. printf(" -%c, ", opt->flag_short);
  57. /* then the long flag */
  58. if (opt->has_arg)
  59. printf("--%-*s", max_noarg_len, opt->flag);
  60. else
  61. printf("--%-*s <arg> ", max_arg_len, opt->flag);
  62. /* finally the help text */
  63. printf(" %s\n", opt->help);
  64. }
  65. os_exit(0);
  66. }
  67. static int sb_cmdline_cb_help(struct sandbox_state *state, const char *arg)
  68. {
  69. /* just flag to sandbox_early_getopt_check to show usage */
  70. return 1;
  71. }
  72. SB_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
  73. int sandbox_main_loop_init(void)
  74. {
  75. struct sandbox_state *state = state_get_current();
  76. /* Execute command if required */
  77. if (state->cmd) {
  78. run_command(state->cmd, 0);
  79. os_exit(state->exit_type);
  80. }
  81. return 0;
  82. }
  83. static int sb_cmdline_cb_command(struct sandbox_state *state, const char *arg)
  84. {
  85. state->cmd = arg;
  86. return 0;
  87. }
  88. SB_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
  89. int main(int argc, char *argv[])
  90. {
  91. struct sandbox_state *state;
  92. int err;
  93. err = state_init();
  94. if (err)
  95. return err;
  96. state = state_get_current();
  97. if (os_parse_args(state, argc, argv))
  98. return 1;
  99. /*
  100. * Do pre- and post-relocation init, then start up U-Boot. This will
  101. * never return.
  102. */
  103. board_init_f(0);
  104. }