Simon Glass | bace3d0 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 1 | /* |
Simon Glass | 6fb6207 | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 2 | * Copyright (c) 2011-2012 The Chromium OS Authors. |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | bace3d0 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 7 | #include <asm/getopt.h> |
| 8 | #include <asm/sections.h> |
Simon Glass | 6fb6207 | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 9 | #include <asm/state.h> |
Simon Glass | bace3d0 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 10 | |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 11 | #include <os.h> |
| 12 | |
| 13 | int sandbox_early_getopt_check(void) |
| 14 | { |
| 15 | struct sandbox_state *state = state_get_current(); |
| 16 | struct sb_cmdline_option **sb_opt = __u_boot_sandbox_option_start; |
| 17 | size_t num_options = __u_boot_sandbox_option_count(); |
| 18 | size_t i; |
| 19 | int max_arg_len, max_noarg_len; |
| 20 | |
| 21 | /* parse_err will be a string of the faulting option */ |
| 22 | if (!state->parse_err) |
| 23 | return 0; |
| 24 | |
| 25 | if (strcmp(state->parse_err, "help")) { |
| 26 | printf("u-boot: error: failed while parsing option: %s\n" |
| 27 | "\ttry running with --help for more information.\n", |
| 28 | state->parse_err); |
| 29 | os_exit(1); |
| 30 | } |
| 31 | |
| 32 | printf( |
| 33 | "u-boot, a command line test interface to U-Boot\n\n" |
| 34 | "Usage: u-boot [options]\n" |
| 35 | "Options:\n"); |
| 36 | |
| 37 | max_arg_len = 0; |
| 38 | for (i = 0; i < num_options; ++i) |
| 39 | max_arg_len = max(strlen(sb_opt[i]->flag), max_arg_len); |
| 40 | max_noarg_len = max_arg_len + 7; |
| 41 | |
| 42 | for (i = 0; i < num_options; ++i) { |
| 43 | struct sb_cmdline_option *opt = sb_opt[i]; |
| 44 | |
| 45 | /* first output the short flag if it has one */ |
| 46 | if (opt->flag_short >= 0x100) |
| 47 | printf(" "); |
| 48 | else |
| 49 | printf(" -%c, ", opt->flag_short); |
| 50 | |
| 51 | /* then the long flag */ |
| 52 | if (opt->has_arg) |
| 53 | printf("--%-*s", max_noarg_len, opt->flag); |
| 54 | else |
| 55 | printf("--%-*s <arg> ", max_arg_len, opt->flag); |
| 56 | |
| 57 | /* finally the help text */ |
| 58 | printf(" %s\n", opt->help); |
| 59 | } |
| 60 | |
| 61 | os_exit(0); |
| 62 | } |
| 63 | |
| 64 | static int sb_cmdline_cb_help(struct sandbox_state *state, const char *arg) |
| 65 | { |
| 66 | /* just flag to sandbox_early_getopt_check to show usage */ |
| 67 | return 1; |
| 68 | } |
| 69 | SB_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help"); |
| 70 | |
Simon Glass | ab4e07e | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 71 | int sandbox_main_loop_init(void) |
| 72 | { |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 73 | struct sandbox_state *state = state_get_current(); |
| 74 | |
| 75 | /* Execute command if required */ |
| 76 | if (state->cmd) { |
Simon Glass | 39042d8 | 2013-04-20 08:42:48 +0000 | [diff] [blame] | 77 | run_command_list(state->cmd, -1, 0); |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 78 | os_exit(state->exit_type); |
| 79 | } |
| 80 | |
Simon Glass | ab4e07e | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 84 | static int sb_cmdline_cb_command(struct sandbox_state *state, const char *arg) |
| 85 | { |
| 86 | state->cmd = arg; |
| 87 | return 0; |
| 88 | } |
| 89 | SB_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command"); |
| 90 | |
Simon Glass | f828bf2 | 2013-04-20 08:42:41 +0000 | [diff] [blame] | 91 | static int sb_cmdline_cb_fdt(struct sandbox_state *state, const char *arg) |
| 92 | { |
| 93 | state->fdt_fname = arg; |
| 94 | return 0; |
| 95 | } |
| 96 | SB_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT"); |
| 97 | |
Simon Glass | bace3d0 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 98 | int main(int argc, char *argv[]) |
| 99 | { |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 100 | struct sandbox_state *state; |
Simon Glass | 6fb6207 | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 101 | int err; |
| 102 | |
| 103 | err = state_init(); |
| 104 | if (err) |
| 105 | return err; |
| 106 | |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 107 | state = state_get_current(); |
| 108 | if (os_parse_args(state, argc, argv)) |
| 109 | return 1; |
| 110 | |
Simon Glass | bace3d0 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 111 | /* |
| 112 | * Do pre- and post-relocation init, then start up U-Boot. This will |
| 113 | * never return. |
| 114 | */ |
| 115 | board_init_f(0); |
Allen Martin | 8ec21bb | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 116 | |
| 117 | /* NOTREACHED - board_init_f() does not return */ |
| 118 | return 0; |
Simon Glass | bace3d0 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 119 | } |