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. |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 7 | #include <os.h> |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 8 | #include <asm/getopt.h> |
| 9 | #include <asm/sections.h> |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 10 | #include <asm/state.h> |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 11 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 14 | int sandbox_early_getopt_check(void) |
| 15 | { |
| 16 | struct sandbox_state *state = state_get_current(); |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 17 | struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start; |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 18 | size_t num_options = __u_boot_sandbox_option_count(); |
| 19 | size_t i; |
| 20 | int max_arg_len, max_noarg_len; |
| 21 | |
| 22 | /* parse_err will be a string of the faulting option */ |
| 23 | if (!state->parse_err) |
| 24 | return 0; |
| 25 | |
| 26 | if (strcmp(state->parse_err, "help")) { |
| 27 | printf("u-boot: error: failed while parsing option: %s\n" |
| 28 | "\ttry running with --help for more information.\n", |
| 29 | state->parse_err); |
| 30 | os_exit(1); |
| 31 | } |
| 32 | |
| 33 | printf( |
| 34 | "u-boot, a command line test interface to U-Boot\n\n" |
| 35 | "Usage: u-boot [options]\n" |
| 36 | "Options:\n"); |
| 37 | |
| 38 | max_arg_len = 0; |
| 39 | for (i = 0; i < num_options; ++i) |
| 40 | max_arg_len = max(strlen(sb_opt[i]->flag), max_arg_len); |
| 41 | max_noarg_len = max_arg_len + 7; |
| 42 | |
| 43 | for (i = 0; i < num_options; ++i) { |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 44 | struct sandbox_cmdline_option *opt = sb_opt[i]; |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 45 | |
| 46 | /* first output the short flag if it has one */ |
| 47 | if (opt->flag_short >= 0x100) |
| 48 | printf(" "); |
| 49 | else |
| 50 | printf(" -%c, ", opt->flag_short); |
| 51 | |
| 52 | /* then the long flag */ |
| 53 | if (opt->has_arg) |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 54 | printf("--%-*s <arg> ", max_arg_len, opt->flag); |
Simon Glass | 5f679f0 | 2013-11-10 10:26:58 -0700 | [diff] [blame] | 55 | else |
| 56 | printf("--%-*s", max_noarg_len, opt->flag); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 57 | |
| 58 | /* finally the help text */ |
| 59 | printf(" %s\n", opt->help); |
| 60 | } |
| 61 | |
| 62 | os_exit(0); |
| 63 | } |
| 64 | |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 65 | static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg) |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 66 | { |
| 67 | /* just flag to sandbox_early_getopt_check to show usage */ |
| 68 | return 1; |
| 69 | } |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 70 | SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help"); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 71 | |
Simon Glass | 0fc3c22 | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 72 | int sandbox_main_loop_init(void) |
| 73 | { |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 74 | struct sandbox_state *state = state_get_current(); |
| 75 | |
| 76 | /* Execute command if required */ |
| 77 | if (state->cmd) { |
Simon Glass | bde3f22 | 2013-04-20 08:42:48 +0000 | [diff] [blame] | 78 | run_command_list(state->cmd, -1, 0); |
Simon Glass | f498e43 | 2013-11-10 10:27:02 -0700 | [diff] [blame] | 79 | if (!state->interactive) |
| 80 | os_exit(state->exit_type); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Simon Glass | 0fc3c22 | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 86 | static int sandbox_cmdline_cb_command(struct sandbox_state *state, |
| 87 | const char *arg) |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 88 | { |
| 89 | state->cmd = arg; |
| 90 | return 0; |
| 91 | } |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 92 | SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command"); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 93 | |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 94 | static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg) |
Simon Glass | 1539343 | 2013-04-20 08:42:41 +0000 | [diff] [blame] | 95 | { |
| 96 | state->fdt_fname = arg; |
| 97 | return 0; |
| 98 | } |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 99 | SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT"); |
Simon Glass | 1539343 | 2013-04-20 08:42:41 +0000 | [diff] [blame] | 100 | |
Simon Glass | f498e43 | 2013-11-10 10:27:02 -0700 | [diff] [blame] | 101 | static int sandbox_cmdline_cb_interactive(struct sandbox_state *state, |
| 102 | const char *arg) |
| 103 | { |
| 104 | state->interactive = true; |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode"); |
| 109 | |
Simon Glass | e990653 | 2014-02-27 13:26:16 -0700 | [diff] [blame] | 110 | static int sandbox_cmdline_cb_jump(struct sandbox_state *state, |
| 111 | const char *arg) |
| 112 | { |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 113 | /* Remember to delete this U-Boot image later */ |
| 114 | state->jumped_fname = arg; |
Simon Glass | e990653 | 2014-02-27 13:26:16 -0700 | [diff] [blame] | 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot"); |
| 119 | |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 120 | static int sandbox_cmdline_cb_memory(struct sandbox_state *state, |
| 121 | const char *arg) |
| 122 | { |
| 123 | int err; |
| 124 | |
| 125 | /* For now assume we always want to write it */ |
| 126 | state->write_ram_buf = true; |
| 127 | state->ram_buf_fname = arg; |
| 128 | |
| 129 | if (os_read_ram_buf(arg)) { |
| 130 | printf("Failed to read RAM buffer\n"); |
| 131 | return err; |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1, |
| 137 | "Read/write ram_buf memory contents from file"); |
| 138 | |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 139 | static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state, |
| 140 | const char *arg) |
| 141 | { |
| 142 | state->ram_buf_rm = true; |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading"); |
| 147 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 148 | static int sandbox_cmdline_cb_state(struct sandbox_state *state, |
| 149 | const char *arg) |
| 150 | { |
| 151 | state->state_fname = arg; |
| 152 | return 0; |
| 153 | } |
| 154 | SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT"); |
| 155 | |
| 156 | static int sandbox_cmdline_cb_read(struct sandbox_state *state, |
| 157 | const char *arg) |
| 158 | { |
| 159 | state->read_state = true; |
| 160 | return 0; |
| 161 | } |
| 162 | SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup"); |
| 163 | |
| 164 | static int sandbox_cmdline_cb_write(struct sandbox_state *state, |
| 165 | const char *arg) |
| 166 | { |
| 167 | state->write_state = true; |
| 168 | return 0; |
| 169 | } |
| 170 | SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit"); |
| 171 | |
| 172 | static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state, |
| 173 | const char *arg) |
| 174 | { |
| 175 | state->ignore_missing_state_on_read = true; |
| 176 | return 0; |
| 177 | } |
| 178 | SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0, |
| 179 | "Ignore missing state on read"); |
| 180 | |
Simon Glass | b9ddbf4 | 2014-02-27 13:26:19 -0700 | [diff] [blame] | 181 | static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state, |
| 182 | const char *arg) |
| 183 | { |
| 184 | state->show_lcd = true; |
| 185 | return 0; |
| 186 | } |
| 187 | SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0, |
| 188 | "Show the sandbox LCD display"); |
| 189 | |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 190 | static const char *term_args[STATE_TERM_COUNT] = { |
| 191 | "raw-with-sigs", |
| 192 | "raw", |
| 193 | "cooked", |
| 194 | }; |
| 195 | |
| 196 | static int sandbox_cmdline_cb_terminal(struct sandbox_state *state, |
| 197 | const char *arg) |
| 198 | { |
| 199 | int i; |
| 200 | |
| 201 | for (i = 0; i < STATE_TERM_COUNT; i++) { |
| 202 | if (!strcmp(arg, term_args[i])) { |
| 203 | state->term_raw = i; |
| 204 | return 0; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | printf("Unknown terminal setting '%s' (", arg); |
| 209 | for (i = 0; i < STATE_TERM_COUNT; i++) |
| 210 | printf("%s%s", i ? ", " : "", term_args[i]); |
| 211 | puts(")\n"); |
| 212 | |
| 213 | return 1; |
| 214 | } |
| 215 | SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1, |
| 216 | "Set terminal to raw/cooked mode"); |
| 217 | |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 218 | int main(int argc, char *argv[]) |
| 219 | { |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 220 | struct sandbox_state *state; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 221 | int ret; |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 222 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 223 | ret = state_init(); |
| 224 | if (ret) |
| 225 | goto err; |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 226 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 227 | state = state_get_current(); |
| 228 | if (os_parse_args(state, argc, argv)) |
| 229 | return 1; |
| 230 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 231 | ret = sandbox_read_state(state, state->state_fname); |
| 232 | if (ret) |
| 233 | goto err; |
| 234 | |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 235 | /* Remove old memory file if required */ |
| 236 | if (state->ram_buf_rm && state->ram_buf_fname) |
| 237 | os_unlink(state->ram_buf_fname); |
| 238 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 239 | /* Do pre- and post-relocation init */ |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 240 | board_init_f(0); |
Allen Martin | 074b455 | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 241 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 242 | board_init_r(gd->new_gd, 0); |
| 243 | |
| 244 | /* NOTREACHED - board_init_r() does not return */ |
Allen Martin | 074b455 | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 245 | return 0; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 246 | |
| 247 | err: |
| 248 | printf("Error %d\n", ret); |
| 249 | return 1; |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 250 | } |