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