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