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