Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | bace3d0 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 2 | /* |
Simon Glass | 6fb6207 | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2011-2012 The Chromium OS Authors. |
Simon Glass | bace3d0 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | d41b703 | 2022-03-04 08:42:58 -0700 | [diff] [blame] | 7 | #include <cli.h> |
Simon Glass | 288b29e | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 8 | #include <command.h> |
Heinrich Schuchardt | 9c54729 | 2020-12-02 16:22:11 +0100 | [diff] [blame] | 9 | #include <efi_loader.h> |
Simon Glass | 3806882 | 2015-05-04 11:31:08 -0600 | [diff] [blame] | 10 | #include <errno.h> |
Simon Glass | 42fdceb | 2022-03-04 08:43:04 -0700 | [diff] [blame] | 11 | #include <event.h> |
Simon Glass | 691d719 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 12 | #include <init.h> |
Patrick Delaunay | a4918b2 | 2020-11-27 11:20:55 +0100 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 14 | #include <os.h> |
Simon Glass | 4209be3 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 15 | #include <sort.h> |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 16 | #include <asm/getopt.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Simon Glass | 4d94dfa | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 18 | #include <asm/io.h> |
Simon Glass | 3ff6fe5 | 2020-02-03 07:36:05 -0700 | [diff] [blame] | 19 | #include <asm/malloc.h> |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 20 | #include <asm/sections.h> |
Simon Glass | 6fb6207 | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 21 | #include <asm/state.h> |
Simon Glass | d41b703 | 2022-03-04 08:42:58 -0700 | [diff] [blame] | 22 | #include <dm/root.h> |
Simon Glass | 4209be3 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 23 | #include <linux/ctype.h> |
Simon Glass | bace3d0 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 24 | |
Simon Glass | 808434c | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
Heinrich Schuchardt | 329dccc | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 27 | static char **os_argv; |
| 28 | |
Simon Glass | 4209be3 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 29 | /* Compare two options so that they can be sorted into alphabetical order */ |
| 30 | static int h_compare_opt(const void *p1, const void *p2) |
| 31 | { |
| 32 | const struct sandbox_cmdline_option *opt1 = p1; |
| 33 | const struct sandbox_cmdline_option *opt2 = p2; |
| 34 | const char *str1, *str2; |
| 35 | char flag1[2], flag2[2]; |
| 36 | |
| 37 | opt1 = *(struct sandbox_cmdline_option **)p1; |
| 38 | opt2 = *(struct sandbox_cmdline_option **)p2; |
| 39 | flag1[1] = '\0'; |
| 40 | flag2[1] = '\0'; |
| 41 | |
| 42 | *flag1 = opt1->flag_short < 0x100 ? opt1->flag_short : '\0'; |
| 43 | *flag2 = opt2->flag_short < 0x100 ? opt2->flag_short : '\0'; |
| 44 | |
| 45 | str1 = *flag1 ? flag1 : opt1->flag; |
| 46 | str2 = *flag2 ? flag2 : opt2->flag; |
| 47 | |
| 48 | /* |
| 49 | * Force lower-case flags to come before upper-case ones. We only |
| 50 | * support upper-case for short flags. |
| 51 | */ |
| 52 | if (isalpha(*str1) && isalpha(*str2) && |
| 53 | tolower(*str1) == tolower(*str2)) |
| 54 | return isupper(*str1) - isupper(*str2); |
| 55 | |
| 56 | return strcasecmp(str1, str2); |
| 57 | } |
| 58 | |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 59 | int sandbox_early_getopt_check(void) |
| 60 | { |
| 61 | struct sandbox_state *state = state_get_current(); |
Marek BehĂșn | d1f81fd | 2021-05-20 13:24:06 +0200 | [diff] [blame] | 62 | struct sandbox_cmdline_option **sb_opt = |
| 63 | __u_boot_sandbox_option_start(); |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 64 | size_t num_options = __u_boot_sandbox_option_count(); |
| 65 | size_t i; |
| 66 | int max_arg_len, max_noarg_len; |
Simon Glass | 4209be3 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 67 | struct sandbox_cmdline_option **sorted_opt; |
| 68 | int size; |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 69 | |
| 70 | /* parse_err will be a string of the faulting option */ |
| 71 | if (!state->parse_err) |
| 72 | return 0; |
| 73 | |
| 74 | if (strcmp(state->parse_err, "help")) { |
| 75 | printf("u-boot: error: failed while parsing option: %s\n" |
| 76 | "\ttry running with --help for more information.\n", |
| 77 | state->parse_err); |
| 78 | os_exit(1); |
| 79 | } |
| 80 | |
| 81 | printf( |
| 82 | "u-boot, a command line test interface to U-Boot\n\n" |
| 83 | "Usage: u-boot [options]\n" |
| 84 | "Options:\n"); |
| 85 | |
| 86 | max_arg_len = 0; |
| 87 | for (i = 0; i < num_options; ++i) |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 88 | max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len); |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 89 | max_noarg_len = max_arg_len + 7; |
| 90 | |
Simon Glass | 4209be3 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 91 | /* Sort the options */ |
| 92 | size = sizeof(*sorted_opt) * num_options; |
Simon Glass | b308d9f | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 93 | sorted_opt = os_malloc(size); |
Simon Glass | 4209be3 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 94 | if (!sorted_opt) { |
| 95 | printf("No memory to sort options\n"); |
| 96 | os_exit(1); |
| 97 | } |
| 98 | memcpy(sorted_opt, sb_opt, size); |
| 99 | qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt); |
| 100 | |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 101 | for (i = 0; i < num_options; ++i) { |
Simon Glass | 4209be3 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 102 | struct sandbox_cmdline_option *opt = sorted_opt[i]; |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 103 | |
| 104 | /* first output the short flag if it has one */ |
| 105 | if (opt->flag_short >= 0x100) |
| 106 | printf(" "); |
| 107 | else |
| 108 | printf(" -%c, ", opt->flag_short); |
| 109 | |
| 110 | /* then the long flag */ |
| 111 | if (opt->has_arg) |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 112 | printf("--%-*s <arg> ", max_arg_len, opt->flag); |
Simon Glass | 6ebcab8 | 2013-11-10 10:26:58 -0700 | [diff] [blame] | 113 | else |
| 114 | printf("--%-*s", max_noarg_len, opt->flag); |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 115 | |
| 116 | /* finally the help text */ |
| 117 | printf(" %s\n", opt->help); |
| 118 | } |
| 119 | |
| 120 | os_exit(0); |
| 121 | } |
| 122 | |
Simon Glass | 42fdceb | 2022-03-04 08:43:04 -0700 | [diff] [blame] | 123 | static int sandbox_misc_init_f(void *ctx, struct event *event) |
Simon Glass | 6896977 | 2017-03-28 10:27:28 -0600 | [diff] [blame] | 124 | { |
| 125 | return sandbox_early_getopt_check(); |
| 126 | } |
Simon Glass | 42fdceb | 2022-03-04 08:43:04 -0700 | [diff] [blame] | 127 | EVENT_SPY(EVT_MISC_INIT_F, sandbox_misc_init_f); |
Simon Glass | 6896977 | 2017-03-28 10:27:28 -0600 | [diff] [blame] | 128 | |
Simon Glass | 7b3efc6 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 129 | static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg) |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 130 | { |
| 131 | /* just flag to sandbox_early_getopt_check to show usage */ |
| 132 | return 1; |
| 133 | } |
Simon Glass | 7b3efc6 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 134 | SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help"); |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 135 | |
Simon Glass | d0d0746 | 2016-07-04 11:57:50 -0600 | [diff] [blame] | 136 | #ifndef CONFIG_SPL_BUILD |
Simon Glass | ab4e07e | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 137 | int sandbox_main_loop_init(void) |
| 138 | { |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 139 | struct sandbox_state *state = state_get_current(); |
| 140 | |
| 141 | /* Execute command if required */ |
Sjoerd Simons | ebaa832 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 142 | if (state->cmd || state->run_distro_boot) { |
| 143 | int retval = 0; |
Joe Hershberger | 88539e4 | 2015-02-06 15:37:31 -0600 | [diff] [blame] | 144 | |
Rabin Vincent | 7dbcb76 | 2014-10-29 23:21:38 +0100 | [diff] [blame] | 145 | cli_init(); |
| 146 | |
Simon Glass | 2b6793d | 2016-03-13 19:07:30 -0600 | [diff] [blame] | 147 | #ifdef CONFIG_CMDLINE |
Sjoerd Simons | ebaa832 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 148 | if (state->cmd) |
| 149 | retval = run_command_list(state->cmd, -1, 0); |
| 150 | |
| 151 | if (state->run_distro_boot) |
| 152 | retval = cli_simple_run_command("run distro_bootcmd", |
| 153 | 0); |
Simon Glass | 2b6793d | 2016-03-13 19:07:30 -0600 | [diff] [blame] | 154 | #endif |
Simon Glass | c5a62d4 | 2013-11-10 10:27:02 -0700 | [diff] [blame] | 155 | if (!state->interactive) |
Joe Hershberger | 88539e4 | 2015-02-06 15:37:31 -0600 | [diff] [blame] | 156 | os_exit(retval); |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Simon Glass | ab4e07e | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 159 | return 0; |
| 160 | } |
Simon Glass | d0d0746 | 2016-07-04 11:57:50 -0600 | [diff] [blame] | 161 | #endif |
Simon Glass | ab4e07e | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 162 | |
Sjoerd Simons | ebaa832 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 163 | static int sandbox_cmdline_cb_boot(struct sandbox_state *state, |
| 164 | const char *arg) |
| 165 | { |
| 166 | state->run_distro_boot = true; |
| 167 | return 0; |
| 168 | } |
| 169 | SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands"); |
| 170 | |
Simon Glass | 7b3efc6 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 171 | static int sandbox_cmdline_cb_command(struct sandbox_state *state, |
| 172 | const char *arg) |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 173 | { |
| 174 | state->cmd = arg; |
| 175 | return 0; |
| 176 | } |
Simon Glass | 7b3efc6 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 177 | SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command"); |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 178 | |
Simon Glass | 7b3efc6 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 179 | static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg) |
Simon Glass | f828bf2 | 2013-04-20 08:42:41 +0000 | [diff] [blame] | 180 | { |
| 181 | state->fdt_fname = arg; |
| 182 | return 0; |
| 183 | } |
Simon Glass | 7b3efc6 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 184 | SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT"); |
Simon Glass | f828bf2 | 2013-04-20 08:42:41 +0000 | [diff] [blame] | 185 | |
Simon Glass | 1f32ae9 | 2015-01-19 20:21:34 -0700 | [diff] [blame] | 186 | static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state, |
| 187 | const char *arg) |
| 188 | { |
| 189 | const char *fmt = "%s.dtb"; |
| 190 | char *fname; |
| 191 | int len; |
| 192 | |
| 193 | len = strlen(state->argv[0]) + strlen(fmt) + 1; |
Simon Glass | b308d9f | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 194 | fname = os_malloc(len); |
Simon Glass | 1f32ae9 | 2015-01-19 20:21:34 -0700 | [diff] [blame] | 195 | if (!fname) |
| 196 | return -ENOMEM; |
| 197 | snprintf(fname, len, fmt, state->argv[0]); |
| 198 | state->fdt_fname = fname; |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0, |
| 203 | "Use the default u-boot.dtb control FDT in U-Boot directory"); |
| 204 | |
Simon Glass | 189882c | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 205 | static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state, |
| 206 | const char *arg) |
| 207 | { |
Simon Glass | 73c5cb9 | 2022-09-06 20:27:08 -0600 | [diff] [blame] | 208 | char buf[256]; |
Simon Glass | 189882c | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 209 | char *fname; |
| 210 | int len; |
| 211 | |
Simon Glass | 73c5cb9 | 2022-09-06 20:27:08 -0600 | [diff] [blame] | 212 | len = state_get_rel_filename("arch/sandbox/dts/test.dtb", buf, |
| 213 | sizeof(buf)); |
| 214 | if (len < 0) |
| 215 | return len; |
| 216 | |
Simon Glass | b308d9f | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 217 | fname = os_malloc(len); |
Simon Glass | 189882c | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 218 | if (!fname) |
| 219 | return -ENOMEM; |
Simon Glass | 73c5cb9 | 2022-09-06 20:27:08 -0600 | [diff] [blame] | 220 | strcpy(fname, buf); |
Simon Glass | 189882c | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 221 | state->fdt_fname = fname; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0, |
| 226 | "Use the test.dtb control FDT in U-Boot directory"); |
| 227 | |
Simon Glass | c5a62d4 | 2013-11-10 10:27:02 -0700 | [diff] [blame] | 228 | static int sandbox_cmdline_cb_interactive(struct sandbox_state *state, |
| 229 | const char *arg) |
| 230 | { |
| 231 | state->interactive = true; |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode"); |
| 236 | |
Simon Glass | bda7773 | 2014-02-27 13:26:16 -0700 | [diff] [blame] | 237 | static int sandbox_cmdline_cb_jump(struct sandbox_state *state, |
| 238 | const char *arg) |
| 239 | { |
Simon Glass | ab839dc | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 240 | /* Remember to delete this U-Boot image later */ |
| 241 | state->jumped_fname = arg; |
Simon Glass | bda7773 | 2014-02-27 13:26:16 -0700 | [diff] [blame] | 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot"); |
| 246 | |
Simon Glass | b2d93c6 | 2022-10-20 18:23:02 -0600 | [diff] [blame] | 247 | static int sandbox_cmdline_cb_program(struct sandbox_state *state, |
| 248 | const char *arg) |
| 249 | { |
| 250 | /* |
| 251 | * Record the program name to use when jumping to future phases. This |
| 252 | * is the original executable which holds all the phases. We need to |
| 253 | * use this instead of argv[0] since each phase is started by |
| 254 | * extracting a particular binary from the full program, then running |
| 255 | * it. Therefore in that binary, argv[0] contains only the |
| 256 | * current-phase executable. |
| 257 | * |
| 258 | * For example, sandbox TPL may be started using image file: |
| 259 | * |
| 260 | * ./image.bin |
| 261 | * |
| 262 | * but then TPL needs to run VPL, which it does by extracting the VPL |
| 263 | * image from the image.bin file. |
| 264 | * |
| 265 | * ./temp-vpl |
| 266 | * |
| 267 | * When VPL runs it needs access to the original image.bin so it can |
| 268 | * extract the next phase (SPL). This works if we use '-f image.bin' |
| 269 | * when starting the original image.bin file. |
| 270 | */ |
| 271 | state->prog_fname = arg; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | SANDBOX_CMDLINE_OPT_SHORT(program, 'p', 1, "U-Boot program name"); |
| 276 | |
Simon Glass | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 277 | static int sandbox_cmdline_cb_memory(struct sandbox_state *state, |
| 278 | const char *arg) |
| 279 | { |
| 280 | int err; |
| 281 | |
| 282 | /* For now assume we always want to write it */ |
| 283 | state->write_ram_buf = true; |
| 284 | state->ram_buf_fname = arg; |
| 285 | |
Simon Glass | f80a8bb | 2014-11-11 12:47:08 -0700 | [diff] [blame] | 286 | err = os_read_ram_buf(arg); |
| 287 | if (err) { |
Simon Glass | 1c5a81d | 2018-10-01 11:55:12 -0600 | [diff] [blame] | 288 | printf("Failed to read RAM buffer '%s': %d\n", arg, err); |
Simon Glass | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 289 | return err; |
| 290 | } |
Simon Glass | a65d1a0 | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 291 | state->ram_buf_read = true; |
Simon Glass | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1, |
| 296 | "Read/write ram_buf memory contents from file"); |
| 297 | |
Simon Glass | ab839dc | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 298 | static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state, |
| 299 | const char *arg) |
| 300 | { |
| 301 | state->ram_buf_rm = true; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading"); |
| 306 | |
Simon Glass | 1209e27 | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 307 | static int sandbox_cmdline_cb_state(struct sandbox_state *state, |
| 308 | const char *arg) |
| 309 | { |
| 310 | state->state_fname = arg; |
| 311 | return 0; |
| 312 | } |
| 313 | SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT"); |
| 314 | |
| 315 | static int sandbox_cmdline_cb_read(struct sandbox_state *state, |
| 316 | const char *arg) |
| 317 | { |
| 318 | state->read_state = true; |
| 319 | return 0; |
| 320 | } |
| 321 | SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup"); |
| 322 | |
| 323 | static int sandbox_cmdline_cb_write(struct sandbox_state *state, |
| 324 | const char *arg) |
| 325 | { |
| 326 | state->write_state = true; |
| 327 | return 0; |
| 328 | } |
| 329 | SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit"); |
| 330 | |
| 331 | static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state, |
| 332 | const char *arg) |
| 333 | { |
| 334 | state->ignore_missing_state_on_read = true; |
| 335 | return 0; |
| 336 | } |
| 337 | SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0, |
| 338 | "Ignore missing state on read"); |
| 339 | |
Simon Glass | 7d95f2a | 2014-02-27 13:26:19 -0700 | [diff] [blame] | 340 | static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state, |
| 341 | const char *arg) |
| 342 | { |
| 343 | state->show_lcd = true; |
| 344 | return 0; |
| 345 | } |
| 346 | SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0, |
| 347 | "Show the sandbox LCD display"); |
| 348 | |
Simon Glass | 6be88c7 | 2020-02-03 07:36:13 -0700 | [diff] [blame] | 349 | static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state, |
| 350 | const char *arg) |
| 351 | { |
| 352 | state->double_lcd = true; |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0, |
| 357 | "Double the LCD display size in each direction"); |
| 358 | |
Simon Glass | ffb8790 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 359 | static const char *term_args[STATE_TERM_COUNT] = { |
| 360 | "raw-with-sigs", |
| 361 | "raw", |
| 362 | "cooked", |
| 363 | }; |
| 364 | |
| 365 | static int sandbox_cmdline_cb_terminal(struct sandbox_state *state, |
| 366 | const char *arg) |
| 367 | { |
| 368 | int i; |
| 369 | |
| 370 | for (i = 0; i < STATE_TERM_COUNT; i++) { |
| 371 | if (!strcmp(arg, term_args[i])) { |
| 372 | state->term_raw = i; |
| 373 | return 0; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | printf("Unknown terminal setting '%s' (", arg); |
| 378 | for (i = 0; i < STATE_TERM_COUNT; i++) |
| 379 | printf("%s%s", i ? ", " : "", term_args[i]); |
| 380 | puts(")\n"); |
| 381 | |
| 382 | return 1; |
| 383 | } |
| 384 | SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1, |
| 385 | "Set terminal to raw/cooked mode"); |
| 386 | |
Simon Glass | 9ce8b40 | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 387 | static int sandbox_cmdline_cb_verbose(struct sandbox_state *state, |
| 388 | const char *arg) |
| 389 | { |
| 390 | state->show_test_output = true; |
| 391 | return 0; |
| 392 | } |
| 393 | SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output"); |
| 394 | |
Simon Glass | 2b1dc29 | 2018-10-01 11:55:11 -0600 | [diff] [blame] | 395 | static int sandbox_cmdline_cb_log_level(struct sandbox_state *state, |
| 396 | const char *arg) |
| 397 | { |
| 398 | state->default_log_level = simple_strtol(arg, NULL, 10); |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1, |
| 403 | "Set log level (0=panic, 7=debug)"); |
| 404 | |
Simon Glass | b25ff5c | 2020-10-25 20:38:28 -0600 | [diff] [blame] | 405 | static int sandbox_cmdline_cb_unittests(struct sandbox_state *state, |
| 406 | const char *arg) |
| 407 | { |
| 408 | state->run_unittests = true; |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests"); |
| 413 | |
Simon Glass | 22b29cc | 2020-10-25 20:38:33 -0600 | [diff] [blame] | 414 | static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state, |
| 415 | const char *arg) |
| 416 | { |
| 417 | state->select_unittests = arg; |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run"); |
| 422 | |
Simon Glass | 85f718f | 2021-03-22 18:21:01 +1300 | [diff] [blame] | 423 | static int sandbox_cmdline_cb_signals(struct sandbox_state *state, |
| 424 | const char *arg) |
| 425 | { |
| 426 | state->handle_signals = true; |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | SANDBOX_CMDLINE_OPT_SHORT(signals, 'S', 0, |
| 431 | "Handle signals (such as SIGSEGV) in sandbox"); |
| 432 | |
Simon Glass | cb89700 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 433 | static int sandbox_cmdline_cb_autoboot_keyed(struct sandbox_state *state, |
| 434 | const char *arg) |
| 435 | { |
| 436 | state->autoboot_keyed = true; |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | SANDBOX_CMDLINE_OPT(autoboot_keyed, 0, "Allow keyed autoboot"); |
| 441 | |
Simon Glass | bb96724 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 442 | static void setup_ram_buf(struct sandbox_state *state) |
| 443 | { |
Simon Glass | a65d1a0 | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 444 | /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */ |
Simon Glass | 5dbe794 | 2019-04-08 13:20:43 -0600 | [diff] [blame] | 445 | if (!state->ram_buf_read) |
Simon Glass | a65d1a0 | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 446 | memset(state->ram_buf, '\0', state->ram_size); |
Simon Glass | a65d1a0 | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 447 | |
Simon Glass | bb96724 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 448 | gd->arch.ram_buf = state->ram_buf; |
| 449 | gd->ram_size = state->ram_size; |
| 450 | } |
| 451 | |
Simon Glass | d66ddaf | 2018-11-15 18:44:03 -0700 | [diff] [blame] | 452 | void state_show(struct sandbox_state *state) |
| 453 | { |
| 454 | char **p; |
| 455 | |
| 456 | printf("Arguments:\n"); |
| 457 | for (p = state->argv; *p; p++) |
| 458 | printf("%s ", *p); |
| 459 | printf("\n"); |
| 460 | } |
| 461 | |
Heinrich Schuchardt | 9c54729 | 2020-12-02 16:22:11 +0100 | [diff] [blame] | 462 | void __efi_runtime EFIAPI efi_reset_system( |
| 463 | enum efi_reset_type reset_type, |
| 464 | efi_status_t reset_status, |
| 465 | unsigned long data_size, void *reset_data) |
| 466 | { |
Heinrich Schuchardt | 7a001e0 | 2021-11-20 14:49:18 +0100 | [diff] [blame] | 467 | if (reset_type == EFI_RESET_SHUTDOWN) |
| 468 | sandbox_exit(); |
| 469 | else |
| 470 | sandbox_reset(); |
Heinrich Schuchardt | 9c54729 | 2020-12-02 16:22:11 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Heinrich Schuchardt | 329dccc | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 473 | void sandbox_reset(void) |
| 474 | { |
| 475 | /* Do this here while it still has an effect */ |
| 476 | os_fd_restore(); |
| 477 | if (state_uninit()) |
| 478 | os_exit(2); |
| 479 | |
Heinrich Schuchardt | 329dccc | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 480 | /* Restart U-Boot */ |
| 481 | os_relaunch(os_argv); |
| 482 | } |
| 483 | |
Andrew Scull | 001c39a | 2022-05-30 10:00:10 +0000 | [diff] [blame] | 484 | int sandbox_main(int argc, char *argv[]) |
Simon Glass | bace3d0 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 485 | { |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 486 | struct sandbox_state *state; |
Heinrich Schuchardt | 205b9f5 | 2021-05-15 19:29:13 +0200 | [diff] [blame] | 487 | void * text_base; |
Simon Glass | 4d94dfa | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 488 | gd_t data; |
Simon Glass | b308d9f | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 489 | int size; |
Simon Glass | 1209e27 | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 490 | int ret; |
Simon Glass | 6fb6207 | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 491 | |
Heinrich Schuchardt | 205b9f5 | 2021-05-15 19:29:13 +0200 | [diff] [blame] | 492 | text_base = os_find_text_base(); |
| 493 | |
Heinrich Schuchardt | 329dccc | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 494 | /* |
Heinrich Schuchardt | 3beba4a | 2021-05-11 21:03:16 +0200 | [diff] [blame] | 495 | * This must be the first invocation of os_malloc() to have |
| 496 | * state->ram_buf in the low 4 GiB. |
| 497 | */ |
| 498 | ret = state_init(); |
| 499 | if (ret) |
| 500 | goto err; |
| 501 | |
| 502 | /* |
Heinrich Schuchardt | 329dccc | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 503 | * Copy argv[] so that we can pass the arguments in the original |
| 504 | * sequence when resetting the sandbox. |
| 505 | */ |
Simon Glass | b308d9f | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 506 | size = sizeof(char *) * (argc + 1); |
| 507 | os_argv = os_malloc(size); |
Heinrich Schuchardt | 329dccc | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 508 | if (!os_argv) |
| 509 | os_exit(1); |
Simon Glass | b308d9f | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 510 | memcpy(os_argv, argv, size); |
Heinrich Schuchardt | 329dccc | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 511 | |
Simon Glass | 001d188 | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 512 | memset(&data, '\0', sizeof(data)); |
| 513 | gd = &data; |
Heinrich Schuchardt | 205b9f5 | 2021-05-15 19:29:13 +0200 | [diff] [blame] | 514 | gd->arch.text_base = text_base; |
Simon Glass | 001d188 | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 515 | |
Simon Glass | 70db421 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 516 | state = state_get_current(); |
| 517 | if (os_parse_args(state, argc, argv)) |
| 518 | return 1; |
| 519 | |
Patrick Delaunay | 10bb90f | 2020-11-20 09:48:33 +0100 | [diff] [blame] | 520 | /* Remove old memory file if required */ |
| 521 | if (state->ram_buf_rm && state->ram_buf_fname) { |
| 522 | os_unlink(state->ram_buf_fname); |
| 523 | state->write_ram_buf = false; |
| 524 | state->ram_buf_fname = NULL; |
| 525 | } |
| 526 | |
Simon Glass | 1209e27 | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 527 | ret = sandbox_read_state(state, state->state_fname); |
| 528 | if (ret) |
| 529 | goto err; |
| 530 | |
Simon Glass | 85f718f | 2021-03-22 18:21:01 +1300 | [diff] [blame] | 531 | if (state->handle_signals) { |
| 532 | ret = os_setup_signal_handlers(); |
| 533 | if (ret) |
| 534 | goto err; |
| 535 | } |
Heinrich Schuchardt | b46f30a | 2020-11-12 00:29:56 +0100 | [diff] [blame] | 536 | |
Andy Yan | 1fc50d7 | 2017-07-24 17:49:59 +0800 | [diff] [blame] | 537 | #if CONFIG_VAL(SYS_MALLOC_F_LEN) |
Tom Rini | dd5b58c | 2022-12-04 10:04:49 -0500 | [diff] [blame] | 538 | gd->malloc_base = CFG_MALLOC_F_ADDR; |
Simon Glass | 29afe9e | 2014-07-10 22:23:31 -0600 | [diff] [blame] | 539 | #endif |
Simon Glass | 2b1dc29 | 2018-10-01 11:55:11 -0600 | [diff] [blame] | 540 | #if CONFIG_IS_ENABLED(LOG) |
| 541 | gd->default_log_level = state->default_log_level; |
| 542 | #endif |
Simon Glass | bb96724 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 543 | setup_ram_buf(state); |
Simon Glass | 4d94dfa | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 544 | |
Simon Glass | 001d188 | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 545 | /* |
| 546 | * Set up the relocation offset here, since sandbox symbols are always |
| 547 | * relocated by the OS before sandbox is entered. |
| 548 | */ |
| 549 | gd->reloc_off = (ulong)gd->arch.text_base; |
| 550 | |
Patrick Delaunay | a4918b2 | 2020-11-27 11:20:55 +0100 | [diff] [blame] | 551 | /* sandbox test: log functions called before log_init in board_init_f */ |
Patrick Delaunay | a4918b2 | 2020-11-27 11:20:55 +0100 | [diff] [blame] | 552 | log_debug("debug: %s\n", __func__); |
| 553 | |
Simon Glass | 808434c | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 554 | /* Do pre- and post-relocation init */ |
Simon Glass | bace3d0 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 555 | board_init_f(0); |
Allen Martin | 8ec21bb | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 556 | |
Simon Glass | 808434c | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 557 | board_init_r(gd->new_gd, 0); |
| 558 | |
| 559 | /* NOTREACHED - board_init_r() does not return */ |
Allen Martin | 8ec21bb | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 560 | return 0; |
Simon Glass | 1209e27 | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 561 | |
| 562 | err: |
| 563 | printf("Error %d\n", ret); |
| 564 | return 1; |
Simon Glass | bace3d0 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 565 | } |