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 | 07a3b23 | 2015-05-04 11:31:08 -0600 | [diff] [blame] | 7 | #include <errno.h> |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 8 | #include <os.h> |
Rabin Vincent | 7e0194a | 2014-10-29 23:21:38 +0100 | [diff] [blame] | 9 | #include <cli.h> |
Simon Glass | 8e17078 | 2015-01-19 20:21:34 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 11 | #include <asm/getopt.h> |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 12 | #include <asm/io.h> |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 13 | #include <asm/sections.h> |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 14 | #include <asm/state.h> |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 15 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 18 | int sandbox_early_getopt_check(void) |
| 19 | { |
| 20 | struct sandbox_state *state = state_get_current(); |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 21 | struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start; |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 22 | size_t num_options = __u_boot_sandbox_option_count(); |
| 23 | size_t i; |
| 24 | int max_arg_len, max_noarg_len; |
| 25 | |
| 26 | /* parse_err will be a string of the faulting option */ |
| 27 | if (!state->parse_err) |
| 28 | return 0; |
| 29 | |
| 30 | if (strcmp(state->parse_err, "help")) { |
| 31 | printf("u-boot: error: failed while parsing option: %s\n" |
| 32 | "\ttry running with --help for more information.\n", |
| 33 | state->parse_err); |
| 34 | os_exit(1); |
| 35 | } |
| 36 | |
| 37 | printf( |
| 38 | "u-boot, a command line test interface to U-Boot\n\n" |
| 39 | "Usage: u-boot [options]\n" |
| 40 | "Options:\n"); |
| 41 | |
| 42 | max_arg_len = 0; |
| 43 | for (i = 0; i < num_options; ++i) |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 44 | max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 45 | max_noarg_len = max_arg_len + 7; |
| 46 | |
| 47 | for (i = 0; i < num_options; ++i) { |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 48 | struct sandbox_cmdline_option *opt = sb_opt[i]; |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 49 | |
| 50 | /* first output the short flag if it has one */ |
| 51 | if (opt->flag_short >= 0x100) |
| 52 | printf(" "); |
| 53 | else |
| 54 | printf(" -%c, ", opt->flag_short); |
| 55 | |
| 56 | /* then the long flag */ |
| 57 | if (opt->has_arg) |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 58 | printf("--%-*s <arg> ", max_arg_len, opt->flag); |
Simon Glass | 5f679f0 | 2013-11-10 10:26:58 -0700 | [diff] [blame] | 59 | else |
| 60 | printf("--%-*s", max_noarg_len, opt->flag); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 61 | |
| 62 | /* finally the help text */ |
| 63 | printf(" %s\n", opt->help); |
| 64 | } |
| 65 | |
| 66 | os_exit(0); |
| 67 | } |
| 68 | |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 69 | 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] | 70 | { |
| 71 | /* just flag to sandbox_early_getopt_check to show usage */ |
| 72 | return 1; |
| 73 | } |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 74 | SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help"); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 75 | |
Simon Glass | 5cf1663 | 2016-07-04 11:57:50 -0600 | [diff] [blame] | 76 | #ifndef CONFIG_SPL_BUILD |
Simon Glass | 0fc3c22 | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 77 | int sandbox_main_loop_init(void) |
| 78 | { |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 79 | struct sandbox_state *state = state_get_current(); |
| 80 | |
| 81 | /* Execute command if required */ |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 82 | if (state->cmd || state->run_distro_boot) { |
| 83 | int retval = 0; |
Joe Hershberger | 744d788 | 2015-02-06 15:37:31 -0600 | [diff] [blame] | 84 | |
Rabin Vincent | 7e0194a | 2014-10-29 23:21:38 +0100 | [diff] [blame] | 85 | cli_init(); |
| 86 | |
Simon Glass | e0f1280 | 2016-03-13 19:07:30 -0600 | [diff] [blame] | 87 | #ifdef CONFIG_CMDLINE |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 88 | if (state->cmd) |
| 89 | retval = run_command_list(state->cmd, -1, 0); |
| 90 | |
| 91 | if (state->run_distro_boot) |
| 92 | retval = cli_simple_run_command("run distro_bootcmd", |
| 93 | 0); |
Simon Glass | e0f1280 | 2016-03-13 19:07:30 -0600 | [diff] [blame] | 94 | #endif |
Simon Glass | f498e43 | 2013-11-10 10:27:02 -0700 | [diff] [blame] | 95 | if (!state->interactive) |
Joe Hershberger | 744d788 | 2015-02-06 15:37:31 -0600 | [diff] [blame] | 96 | os_exit(retval); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 99 | return 0; |
| 100 | } |
Simon Glass | 5cf1663 | 2016-07-04 11:57:50 -0600 | [diff] [blame] | 101 | #endif |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 102 | |
| 103 | static int sandbox_cmdline_cb_boot(struct sandbox_state *state, |
| 104 | const char *arg) |
| 105 | { |
| 106 | state->run_distro_boot = true; |
Simon Glass | 0fc3c22 | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 107 | return 0; |
| 108 | } |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 109 | SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands"); |
Simon Glass | 0fc3c22 | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 110 | |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 111 | static int sandbox_cmdline_cb_command(struct sandbox_state *state, |
| 112 | const char *arg) |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 113 | { |
| 114 | state->cmd = arg; |
| 115 | return 0; |
| 116 | } |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 117 | SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command"); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 118 | |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 119 | 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] | 120 | { |
| 121 | state->fdt_fname = arg; |
| 122 | return 0; |
| 123 | } |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 124 | 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] | 125 | |
Simon Glass | 8e17078 | 2015-01-19 20:21:34 -0700 | [diff] [blame] | 126 | static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state, |
| 127 | const char *arg) |
| 128 | { |
| 129 | const char *fmt = "%s.dtb"; |
| 130 | char *fname; |
| 131 | int len; |
| 132 | |
| 133 | len = strlen(state->argv[0]) + strlen(fmt) + 1; |
| 134 | fname = os_malloc(len); |
| 135 | if (!fname) |
| 136 | return -ENOMEM; |
| 137 | snprintf(fname, len, fmt, state->argv[0]); |
| 138 | state->fdt_fname = fname; |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0, |
| 143 | "Use the default u-boot.dtb control FDT in U-Boot directory"); |
| 144 | |
Simon Glass | f498e43 | 2013-11-10 10:27:02 -0700 | [diff] [blame] | 145 | static int sandbox_cmdline_cb_interactive(struct sandbox_state *state, |
| 146 | const char *arg) |
| 147 | { |
| 148 | state->interactive = true; |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode"); |
| 153 | |
Simon Glass | e990653 | 2014-02-27 13:26:16 -0700 | [diff] [blame] | 154 | static int sandbox_cmdline_cb_jump(struct sandbox_state *state, |
| 155 | const char *arg) |
| 156 | { |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 157 | /* Remember to delete this U-Boot image later */ |
| 158 | state->jumped_fname = arg; |
Simon Glass | e990653 | 2014-02-27 13:26:16 -0700 | [diff] [blame] | 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot"); |
| 163 | |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 164 | static int sandbox_cmdline_cb_memory(struct sandbox_state *state, |
| 165 | const char *arg) |
| 166 | { |
| 167 | int err; |
| 168 | |
| 169 | /* For now assume we always want to write it */ |
| 170 | state->write_ram_buf = true; |
| 171 | state->ram_buf_fname = arg; |
| 172 | |
Simon Glass | c043e03 | 2014-11-11 12:47:08 -0700 | [diff] [blame] | 173 | err = os_read_ram_buf(arg); |
| 174 | if (err) { |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 175 | printf("Failed to read RAM buffer\n"); |
| 176 | return err; |
| 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1, |
| 182 | "Read/write ram_buf memory contents from file"); |
| 183 | |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 184 | static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state, |
| 185 | const char *arg) |
| 186 | { |
| 187 | state->ram_buf_rm = true; |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading"); |
| 192 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 193 | static int sandbox_cmdline_cb_state(struct sandbox_state *state, |
| 194 | const char *arg) |
| 195 | { |
| 196 | state->state_fname = arg; |
| 197 | return 0; |
| 198 | } |
| 199 | SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT"); |
| 200 | |
| 201 | static int sandbox_cmdline_cb_read(struct sandbox_state *state, |
| 202 | const char *arg) |
| 203 | { |
| 204 | state->read_state = true; |
| 205 | return 0; |
| 206 | } |
| 207 | SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup"); |
| 208 | |
| 209 | static int sandbox_cmdline_cb_write(struct sandbox_state *state, |
| 210 | const char *arg) |
| 211 | { |
| 212 | state->write_state = true; |
| 213 | return 0; |
| 214 | } |
| 215 | SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit"); |
| 216 | |
| 217 | static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state, |
| 218 | const char *arg) |
| 219 | { |
| 220 | state->ignore_missing_state_on_read = true; |
| 221 | return 0; |
| 222 | } |
| 223 | SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0, |
| 224 | "Ignore missing state on read"); |
| 225 | |
Simon Glass | b9ddbf4 | 2014-02-27 13:26:19 -0700 | [diff] [blame] | 226 | static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state, |
| 227 | const char *arg) |
| 228 | { |
| 229 | state->show_lcd = true; |
| 230 | return 0; |
| 231 | } |
| 232 | SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0, |
| 233 | "Show the sandbox LCD display"); |
| 234 | |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 235 | static const char *term_args[STATE_TERM_COUNT] = { |
| 236 | "raw-with-sigs", |
| 237 | "raw", |
| 238 | "cooked", |
| 239 | }; |
| 240 | |
| 241 | static int sandbox_cmdline_cb_terminal(struct sandbox_state *state, |
| 242 | const char *arg) |
| 243 | { |
| 244 | int i; |
| 245 | |
| 246 | for (i = 0; i < STATE_TERM_COUNT; i++) { |
| 247 | if (!strcmp(arg, term_args[i])) { |
| 248 | state->term_raw = i; |
| 249 | return 0; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | printf("Unknown terminal setting '%s' (", arg); |
| 254 | for (i = 0; i < STATE_TERM_COUNT; i++) |
| 255 | printf("%s%s", i ? ", " : "", term_args[i]); |
| 256 | puts(")\n"); |
| 257 | |
| 258 | return 1; |
| 259 | } |
| 260 | SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1, |
| 261 | "Set terminal to raw/cooked mode"); |
| 262 | |
Simon Glass | fe6d12a | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 263 | static int sandbox_cmdline_cb_verbose(struct sandbox_state *state, |
| 264 | const char *arg) |
| 265 | { |
| 266 | state->show_test_output = true; |
| 267 | return 0; |
| 268 | } |
| 269 | SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output"); |
| 270 | |
Simon Glass | e0f1280 | 2016-03-13 19:07:30 -0600 | [diff] [blame] | 271 | int board_run_command(const char *cmdline) |
| 272 | { |
| 273 | printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n"); |
| 274 | |
| 275 | return 1; |
| 276 | } |
| 277 | |
Simon Glass | b94eed5 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 278 | static void setup_ram_buf(struct sandbox_state *state) |
| 279 | { |
| 280 | gd->arch.ram_buf = state->ram_buf; |
| 281 | gd->ram_size = state->ram_size; |
| 282 | } |
| 283 | |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 284 | int main(int argc, char *argv[]) |
| 285 | { |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 286 | struct sandbox_state *state; |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 287 | gd_t data; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 288 | int ret; |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 289 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 290 | ret = state_init(); |
| 291 | if (ret) |
| 292 | goto err; |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 293 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 294 | state = state_get_current(); |
| 295 | if (os_parse_args(state, argc, argv)) |
| 296 | return 1; |
| 297 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 298 | ret = sandbox_read_state(state, state->state_fname); |
| 299 | if (ret) |
| 300 | goto err; |
| 301 | |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 302 | /* Remove old memory file if required */ |
| 303 | if (state->ram_buf_rm && state->ram_buf_fname) |
| 304 | os_unlink(state->ram_buf_fname); |
| 305 | |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 306 | memset(&data, '\0', sizeof(data)); |
| 307 | gd = &data; |
Simon Glass | 0cc6f5c | 2014-07-10 22:23:31 -0600 | [diff] [blame] | 308 | #ifdef CONFIG_SYS_MALLOC_F_LEN |
| 309 | gd->malloc_base = CONFIG_MALLOC_F_ADDR; |
| 310 | #endif |
Simon Glass | b94eed5 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 311 | setup_ram_buf(state); |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 312 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 313 | /* Do pre- and post-relocation init */ |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 314 | board_init_f(0); |
Allen Martin | 074b455 | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 315 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 316 | board_init_r(gd->new_gd, 0); |
| 317 | |
| 318 | /* NOTREACHED - board_init_r() does not return */ |
Allen Martin | 074b455 | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 319 | return 0; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 320 | |
| 321 | err: |
| 322 | printf("Error %d\n", ret); |
| 323 | return 1; |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 324 | } |