Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 2 | /* |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2011-2012 The Chromium OS Authors. |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 7 | #include <command.h> |
Simon Glass | 07a3b23 | 2015-05-04 11:31:08 -0600 | [diff] [blame] | 8 | #include <errno.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 9 | #include <init.h> |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 10 | #include <os.h> |
Rabin Vincent | 7e0194a | 2014-10-29 23:21:38 +0100 | [diff] [blame] | 11 | #include <cli.h> |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 12 | #include <sort.h> |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 13 | #include <asm/getopt.h> |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 14 | #include <asm/io.h> |
Simon Glass | 30f9205 | 2020-02-03 07:36:05 -0700 | [diff] [blame] | 15 | #include <asm/malloc.h> |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 16 | #include <asm/sections.h> |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 17 | #include <asm/state.h> |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 18 | #include <linux/ctype.h> |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 19 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 22 | /* Compare two options so that they can be sorted into alphabetical order */ |
| 23 | static int h_compare_opt(const void *p1, const void *p2) |
| 24 | { |
| 25 | const struct sandbox_cmdline_option *opt1 = p1; |
| 26 | const struct sandbox_cmdline_option *opt2 = p2; |
| 27 | const char *str1, *str2; |
| 28 | char flag1[2], flag2[2]; |
| 29 | |
| 30 | opt1 = *(struct sandbox_cmdline_option **)p1; |
| 31 | opt2 = *(struct sandbox_cmdline_option **)p2; |
| 32 | flag1[1] = '\0'; |
| 33 | flag2[1] = '\0'; |
| 34 | |
| 35 | *flag1 = opt1->flag_short < 0x100 ? opt1->flag_short : '\0'; |
| 36 | *flag2 = opt2->flag_short < 0x100 ? opt2->flag_short : '\0'; |
| 37 | |
| 38 | str1 = *flag1 ? flag1 : opt1->flag; |
| 39 | str2 = *flag2 ? flag2 : opt2->flag; |
| 40 | |
| 41 | /* |
| 42 | * Force lower-case flags to come before upper-case ones. We only |
| 43 | * support upper-case for short flags. |
| 44 | */ |
| 45 | if (isalpha(*str1) && isalpha(*str2) && |
| 46 | tolower(*str1) == tolower(*str2)) |
| 47 | return isupper(*str1) - isupper(*str2); |
| 48 | |
| 49 | return strcasecmp(str1, str2); |
| 50 | } |
| 51 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 52 | int sandbox_early_getopt_check(void) |
| 53 | { |
| 54 | struct sandbox_state *state = state_get_current(); |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 55 | struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start; |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 56 | size_t num_options = __u_boot_sandbox_option_count(); |
| 57 | size_t i; |
| 58 | int max_arg_len, max_noarg_len; |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 59 | struct sandbox_cmdline_option **sorted_opt; |
| 60 | int size; |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 61 | |
| 62 | /* parse_err will be a string of the faulting option */ |
| 63 | if (!state->parse_err) |
| 64 | return 0; |
| 65 | |
| 66 | if (strcmp(state->parse_err, "help")) { |
| 67 | printf("u-boot: error: failed while parsing option: %s\n" |
| 68 | "\ttry running with --help for more information.\n", |
| 69 | state->parse_err); |
| 70 | os_exit(1); |
| 71 | } |
| 72 | |
| 73 | printf( |
| 74 | "u-boot, a command line test interface to U-Boot\n\n" |
| 75 | "Usage: u-boot [options]\n" |
| 76 | "Options:\n"); |
| 77 | |
| 78 | max_arg_len = 0; |
| 79 | for (i = 0; i < num_options; ++i) |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 80 | 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] | 81 | max_noarg_len = max_arg_len + 7; |
| 82 | |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 83 | /* Sort the options */ |
| 84 | size = sizeof(*sorted_opt) * num_options; |
| 85 | sorted_opt = malloc(size); |
| 86 | if (!sorted_opt) { |
| 87 | printf("No memory to sort options\n"); |
| 88 | os_exit(1); |
| 89 | } |
| 90 | memcpy(sorted_opt, sb_opt, size); |
| 91 | qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt); |
| 92 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 93 | for (i = 0; i < num_options; ++i) { |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 94 | struct sandbox_cmdline_option *opt = sorted_opt[i]; |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 95 | |
| 96 | /* first output the short flag if it has one */ |
| 97 | if (opt->flag_short >= 0x100) |
| 98 | printf(" "); |
| 99 | else |
| 100 | printf(" -%c, ", opt->flag_short); |
| 101 | |
| 102 | /* then the long flag */ |
| 103 | if (opt->has_arg) |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 104 | printf("--%-*s <arg> ", max_arg_len, opt->flag); |
Simon Glass | 5f679f0 | 2013-11-10 10:26:58 -0700 | [diff] [blame] | 105 | else |
| 106 | printf("--%-*s", max_noarg_len, opt->flag); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 107 | |
| 108 | /* finally the help text */ |
| 109 | printf(" %s\n", opt->help); |
| 110 | } |
| 111 | |
| 112 | os_exit(0); |
| 113 | } |
| 114 | |
Simon Glass | b419dbb | 2017-03-28 10:27:28 -0600 | [diff] [blame] | 115 | int misc_init_f(void) |
| 116 | { |
| 117 | return sandbox_early_getopt_check(); |
| 118 | } |
| 119 | |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 120 | 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] | 121 | { |
| 122 | /* just flag to sandbox_early_getopt_check to show usage */ |
| 123 | return 1; |
| 124 | } |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 125 | SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help"); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 126 | |
Simon Glass | 5cf1663 | 2016-07-04 11:57:50 -0600 | [diff] [blame] | 127 | #ifndef CONFIG_SPL_BUILD |
Simon Glass | 0fc3c22 | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 128 | int sandbox_main_loop_init(void) |
| 129 | { |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 130 | struct sandbox_state *state = state_get_current(); |
| 131 | |
| 132 | /* Execute command if required */ |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 133 | if (state->cmd || state->run_distro_boot) { |
| 134 | int retval = 0; |
Joe Hershberger | 744d788 | 2015-02-06 15:37:31 -0600 | [diff] [blame] | 135 | |
Rabin Vincent | 7e0194a | 2014-10-29 23:21:38 +0100 | [diff] [blame] | 136 | cli_init(); |
| 137 | |
Simon Glass | e0f1280 | 2016-03-13 19:07:30 -0600 | [diff] [blame] | 138 | #ifdef CONFIG_CMDLINE |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 139 | if (state->cmd) |
| 140 | retval = run_command_list(state->cmd, -1, 0); |
| 141 | |
| 142 | if (state->run_distro_boot) |
| 143 | retval = cli_simple_run_command("run distro_bootcmd", |
| 144 | 0); |
Simon Glass | e0f1280 | 2016-03-13 19:07:30 -0600 | [diff] [blame] | 145 | #endif |
Simon Glass | f498e43 | 2013-11-10 10:27:02 -0700 | [diff] [blame] | 146 | if (!state->interactive) |
Joe Hershberger | 744d788 | 2015-02-06 15:37:31 -0600 | [diff] [blame] | 147 | os_exit(retval); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 150 | return 0; |
| 151 | } |
Simon Glass | 5cf1663 | 2016-07-04 11:57:50 -0600 | [diff] [blame] | 152 | #endif |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 153 | |
| 154 | static int sandbox_cmdline_cb_boot(struct sandbox_state *state, |
| 155 | const char *arg) |
| 156 | { |
| 157 | state->run_distro_boot = true; |
Simon Glass | 0fc3c22 | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 158 | return 0; |
| 159 | } |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 160 | SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands"); |
Simon Glass | 0fc3c22 | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 161 | |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 162 | static int sandbox_cmdline_cb_command(struct sandbox_state *state, |
| 163 | const char *arg) |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 164 | { |
| 165 | state->cmd = arg; |
| 166 | return 0; |
| 167 | } |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 168 | SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command"); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 169 | |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 170 | 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] | 171 | { |
| 172 | state->fdt_fname = arg; |
| 173 | return 0; |
| 174 | } |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 175 | 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] | 176 | |
Simon Glass | 8e17078 | 2015-01-19 20:21:34 -0700 | [diff] [blame] | 177 | static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state, |
| 178 | const char *arg) |
| 179 | { |
| 180 | const char *fmt = "%s.dtb"; |
| 181 | char *fname; |
| 182 | int len; |
| 183 | |
| 184 | len = strlen(state->argv[0]) + strlen(fmt) + 1; |
Simon Glass | 30f9205 | 2020-02-03 07:36:05 -0700 | [diff] [blame] | 185 | fname = malloc(len); |
Simon Glass | 8e17078 | 2015-01-19 20:21:34 -0700 | [diff] [blame] | 186 | if (!fname) |
| 187 | return -ENOMEM; |
| 188 | snprintf(fname, len, fmt, state->argv[0]); |
| 189 | state->fdt_fname = fname; |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0, |
| 194 | "Use the default u-boot.dtb control FDT in U-Boot directory"); |
| 195 | |
Simon Glass | 3c3968f | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 196 | static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state, |
| 197 | const char *arg) |
| 198 | { |
| 199 | const char *fmt = "/arch/sandbox/dts/test.dtb"; |
| 200 | char *p; |
| 201 | char *fname; |
| 202 | int len; |
| 203 | |
| 204 | len = strlen(state->argv[0]) + strlen(fmt) + 1; |
Simon Glass | 30f9205 | 2020-02-03 07:36:05 -0700 | [diff] [blame] | 205 | fname = malloc(len); |
Simon Glass | 3c3968f | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 206 | if (!fname) |
| 207 | return -ENOMEM; |
| 208 | strcpy(fname, state->argv[0]); |
| 209 | p = strrchr(fname, '/'); |
| 210 | if (!p) |
| 211 | p = fname + strlen(fname); |
| 212 | len -= p - fname; |
| 213 | snprintf(p, len, fmt, p); |
| 214 | state->fdt_fname = fname; |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0, |
| 219 | "Use the test.dtb control FDT in U-Boot directory"); |
| 220 | |
Simon Glass | f498e43 | 2013-11-10 10:27:02 -0700 | [diff] [blame] | 221 | static int sandbox_cmdline_cb_interactive(struct sandbox_state *state, |
| 222 | const char *arg) |
| 223 | { |
| 224 | state->interactive = true; |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode"); |
| 229 | |
Simon Glass | e990653 | 2014-02-27 13:26:16 -0700 | [diff] [blame] | 230 | static int sandbox_cmdline_cb_jump(struct sandbox_state *state, |
| 231 | const char *arg) |
| 232 | { |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 233 | /* Remember to delete this U-Boot image later */ |
| 234 | state->jumped_fname = arg; |
Simon Glass | e990653 | 2014-02-27 13:26:16 -0700 | [diff] [blame] | 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot"); |
| 239 | |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 240 | static int sandbox_cmdline_cb_memory(struct sandbox_state *state, |
| 241 | const char *arg) |
| 242 | { |
| 243 | int err; |
| 244 | |
| 245 | /* For now assume we always want to write it */ |
| 246 | state->write_ram_buf = true; |
| 247 | state->ram_buf_fname = arg; |
| 248 | |
Simon Glass | c043e03 | 2014-11-11 12:47:08 -0700 | [diff] [blame] | 249 | err = os_read_ram_buf(arg); |
| 250 | if (err) { |
Simon Glass | 332894d | 2018-10-01 11:55:12 -0600 | [diff] [blame] | 251 | printf("Failed to read RAM buffer '%s': %d\n", arg, err); |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 252 | return err; |
| 253 | } |
Simon Glass | 24a284a | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 254 | state->ram_buf_read = true; |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1, |
| 259 | "Read/write ram_buf memory contents from file"); |
| 260 | |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 261 | static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state, |
| 262 | const char *arg) |
| 263 | { |
| 264 | state->ram_buf_rm = true; |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading"); |
| 269 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 270 | static int sandbox_cmdline_cb_state(struct sandbox_state *state, |
| 271 | const char *arg) |
| 272 | { |
| 273 | state->state_fname = arg; |
| 274 | return 0; |
| 275 | } |
| 276 | SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT"); |
| 277 | |
| 278 | static int sandbox_cmdline_cb_read(struct sandbox_state *state, |
| 279 | const char *arg) |
| 280 | { |
| 281 | state->read_state = true; |
| 282 | return 0; |
| 283 | } |
| 284 | SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup"); |
| 285 | |
| 286 | static int sandbox_cmdline_cb_write(struct sandbox_state *state, |
| 287 | const char *arg) |
| 288 | { |
| 289 | state->write_state = true; |
| 290 | return 0; |
| 291 | } |
| 292 | SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit"); |
| 293 | |
| 294 | static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state, |
| 295 | const char *arg) |
| 296 | { |
| 297 | state->ignore_missing_state_on_read = true; |
| 298 | return 0; |
| 299 | } |
| 300 | SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0, |
| 301 | "Ignore missing state on read"); |
| 302 | |
Simon Glass | b9ddbf4 | 2014-02-27 13:26:19 -0700 | [diff] [blame] | 303 | static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state, |
| 304 | const char *arg) |
| 305 | { |
| 306 | state->show_lcd = true; |
| 307 | return 0; |
| 308 | } |
| 309 | SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0, |
| 310 | "Show the sandbox LCD display"); |
| 311 | |
Simon Glass | f91de0b | 2020-02-03 07:36:13 -0700 | [diff] [blame] | 312 | static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state, |
| 313 | const char *arg) |
| 314 | { |
| 315 | state->double_lcd = true; |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0, |
| 320 | "Double the LCD display size in each direction"); |
| 321 | |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 322 | static const char *term_args[STATE_TERM_COUNT] = { |
| 323 | "raw-with-sigs", |
| 324 | "raw", |
| 325 | "cooked", |
| 326 | }; |
| 327 | |
| 328 | static int sandbox_cmdline_cb_terminal(struct sandbox_state *state, |
| 329 | const char *arg) |
| 330 | { |
| 331 | int i; |
| 332 | |
| 333 | for (i = 0; i < STATE_TERM_COUNT; i++) { |
| 334 | if (!strcmp(arg, term_args[i])) { |
| 335 | state->term_raw = i; |
| 336 | return 0; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | printf("Unknown terminal setting '%s' (", arg); |
| 341 | for (i = 0; i < STATE_TERM_COUNT; i++) |
| 342 | printf("%s%s", i ? ", " : "", term_args[i]); |
| 343 | puts(")\n"); |
| 344 | |
| 345 | return 1; |
| 346 | } |
| 347 | SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1, |
| 348 | "Set terminal to raw/cooked mode"); |
| 349 | |
Simon Glass | fe6d12a | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 350 | static int sandbox_cmdline_cb_verbose(struct sandbox_state *state, |
| 351 | const char *arg) |
| 352 | { |
| 353 | state->show_test_output = true; |
| 354 | return 0; |
| 355 | } |
| 356 | SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output"); |
| 357 | |
Simon Glass | 4e9a64d | 2018-10-01 11:55:11 -0600 | [diff] [blame] | 358 | static int sandbox_cmdline_cb_log_level(struct sandbox_state *state, |
| 359 | const char *arg) |
| 360 | { |
| 361 | state->default_log_level = simple_strtol(arg, NULL, 10); |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1, |
| 366 | "Set log level (0=panic, 7=debug)"); |
| 367 | |
Simon Glass | b5dfea8 | 2018-11-15 18:44:01 -0700 | [diff] [blame] | 368 | static int sandbox_cmdline_cb_show_of_platdata(struct sandbox_state *state, |
| 369 | const char *arg) |
| 370 | { |
| 371 | state->show_of_platdata = true; |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | SANDBOX_CMDLINE_OPT(show_of_platdata, 0, "Show of-platdata in SPL"); |
| 376 | |
Simon Glass | b94eed5 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 377 | static void setup_ram_buf(struct sandbox_state *state) |
| 378 | { |
Simon Glass | 24a284a | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 379 | /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */ |
Simon Glass | cd3705e | 2019-04-08 13:20:43 -0600 | [diff] [blame] | 380 | if (!state->ram_buf_read) |
Simon Glass | 24a284a | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 381 | memset(state->ram_buf, '\0', state->ram_size); |
Simon Glass | 24a284a | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 382 | |
Simon Glass | b94eed5 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 383 | gd->arch.ram_buf = state->ram_buf; |
| 384 | gd->ram_size = state->ram_size; |
| 385 | } |
| 386 | |
Simon Glass | 46508c9 | 2018-11-15 18:44:03 -0700 | [diff] [blame] | 387 | void state_show(struct sandbox_state *state) |
| 388 | { |
| 389 | char **p; |
| 390 | |
| 391 | printf("Arguments:\n"); |
| 392 | for (p = state->argv; *p; p++) |
| 393 | printf("%s ", *p); |
| 394 | printf("\n"); |
| 395 | } |
| 396 | |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 397 | int main(int argc, char *argv[]) |
| 398 | { |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 399 | struct sandbox_state *state; |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 400 | gd_t data; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 401 | int ret; |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 402 | |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 403 | memset(&data, '\0', sizeof(data)); |
| 404 | gd = &data; |
| 405 | gd->arch.text_base = os_find_text_base(); |
| 406 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 407 | ret = state_init(); |
| 408 | if (ret) |
| 409 | goto err; |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 410 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 411 | state = state_get_current(); |
| 412 | if (os_parse_args(state, argc, argv)) |
| 413 | return 1; |
| 414 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 415 | ret = sandbox_read_state(state, state->state_fname); |
| 416 | if (ret) |
| 417 | goto err; |
| 418 | |
Andy Yan | 25428c4 | 2017-07-24 17:49:59 +0800 | [diff] [blame] | 419 | #if CONFIG_VAL(SYS_MALLOC_F_LEN) |
Simon Glass | 0cc6f5c | 2014-07-10 22:23:31 -0600 | [diff] [blame] | 420 | gd->malloc_base = CONFIG_MALLOC_F_ADDR; |
| 421 | #endif |
Simon Glass | 4e9a64d | 2018-10-01 11:55:11 -0600 | [diff] [blame] | 422 | #if CONFIG_IS_ENABLED(LOG) |
| 423 | gd->default_log_level = state->default_log_level; |
| 424 | #endif |
Simon Glass | b94eed5 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 425 | setup_ram_buf(state); |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 426 | |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 427 | /* |
| 428 | * Set up the relocation offset here, since sandbox symbols are always |
| 429 | * relocated by the OS before sandbox is entered. |
| 430 | */ |
| 431 | gd->reloc_off = (ulong)gd->arch.text_base; |
| 432 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 433 | /* Do pre- and post-relocation init */ |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 434 | board_init_f(0); |
Allen Martin | 074b455 | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 435 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 436 | board_init_r(gd->new_gd, 0); |
| 437 | |
| 438 | /* NOTREACHED - board_init_r() does not return */ |
Allen Martin | 074b455 | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 439 | return 0; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 440 | |
| 441 | err: |
| 442 | printf("Error %d\n", ret); |
| 443 | return 1; |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 444 | } |