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