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