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