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