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 | fe143ee | 2022-03-04 08:42:58 -0700 | [diff] [blame] | 7 | #include <cli.h> |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 8 | #include <command.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 | f1c5191 | 2022-03-04 08:43:04 -0700 | [diff] [blame] | 11 | #include <event.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 12 | #include <init.h> |
Patrick Delaunay | 053156d | 2020-11-27 11:20:55 +0100 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 14 | #include <os.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 | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 18 | #include <asm/io.h> |
Simon Glass | 30f9205 | 2020-02-03 07:36:05 -0700 | [diff] [blame] | 19 | #include <asm/malloc.h> |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 20 | #include <asm/sections.h> |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 21 | #include <asm/state.h> |
Simon Glass | fe143ee | 2022-03-04 08:42:58 -0700 | [diff] [blame] | 22 | #include <dm/root.h> |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 23 | #include <linux/ctype.h> |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 24 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 27 | static char **os_argv; |
| 28 | |
Simon Glass | 11c92b1 | 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 | 8a3e035 | 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 | 184c4af | 2021-05-20 13:24:06 +0200 | [diff] [blame] | 62 | struct sandbox_cmdline_option **sb_opt = |
| 63 | __u_boot_sandbox_option_start(); |
Simon Glass | 8a3e035 | 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 | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 67 | struct sandbox_cmdline_option **sorted_opt; |
| 68 | int size; |
Simon Glass | 8a3e035 | 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 | db20464 | 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 | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 89 | max_noarg_len = max_arg_len + 7; |
| 90 | |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 91 | /* Sort the options */ |
| 92 | size = sizeof(*sorted_opt) * num_options; |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 93 | sorted_opt = os_malloc(size); |
Simon Glass | 11c92b1 | 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 | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 101 | for (i = 0; i < num_options; ++i) { |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 102 | struct sandbox_cmdline_option *opt = sorted_opt[i]; |
Simon Glass | 8a3e035 | 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 | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 112 | printf("--%-*s <arg> ", max_arg_len, opt->flag); |
Simon Glass | 5f679f0 | 2013-11-10 10:26:58 -0700 | [diff] [blame] | 113 | else |
| 114 | printf("--%-*s", max_noarg_len, opt->flag); |
Simon Glass | 8a3e035 | 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 | } |
Simon Glass | b8357c1 | 2023-08-21 21:16:56 -0600 | [diff] [blame] | 122 | EVENT_SPY_SIMPLE(EVT_MISC_INIT_F, sandbox_early_getopt_check); |
Simon Glass | b419dbb | 2017-03-28 10:27:28 -0600 | [diff] [blame] | 123 | |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 124 | 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] | 125 | { |
| 126 | /* just flag to sandbox_early_getopt_check to show usage */ |
| 127 | return 1; |
| 128 | } |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 129 | SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help"); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 130 | |
Simon Glass | 5cf1663 | 2016-07-04 11:57:50 -0600 | [diff] [blame] | 131 | #ifndef CONFIG_SPL_BUILD |
Simon Glass | 0fc3c22 | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 132 | int sandbox_main_loop_init(void) |
| 133 | { |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 134 | struct sandbox_state *state = state_get_current(); |
| 135 | |
| 136 | /* Execute command if required */ |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 137 | if (state->cmd || state->run_distro_boot) { |
| 138 | int retval = 0; |
Joe Hershberger | 744d788 | 2015-02-06 15:37:31 -0600 | [diff] [blame] | 139 | |
Rabin Vincent | 7e0194a | 2014-10-29 23:21:38 +0100 | [diff] [blame] | 140 | cli_init(); |
| 141 | |
Simon Glass | e0f1280 | 2016-03-13 19:07:30 -0600 | [diff] [blame] | 142 | #ifdef CONFIG_CMDLINE |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 143 | if (state->cmd) |
| 144 | retval = run_command_list(state->cmd, -1, 0); |
| 145 | |
| 146 | if (state->run_distro_boot) |
| 147 | retval = cli_simple_run_command("run distro_bootcmd", |
| 148 | 0); |
Simon Glass | e0f1280 | 2016-03-13 19:07:30 -0600 | [diff] [blame] | 149 | #endif |
Simon Glass | f498e43 | 2013-11-10 10:27:02 -0700 | [diff] [blame] | 150 | if (!state->interactive) |
Joe Hershberger | 744d788 | 2015-02-06 15:37:31 -0600 | [diff] [blame] | 151 | os_exit(retval); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 154 | return 0; |
| 155 | } |
Simon Glass | 5cf1663 | 2016-07-04 11:57:50 -0600 | [diff] [blame] | 156 | #endif |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 157 | |
| 158 | static int sandbox_cmdline_cb_boot(struct sandbox_state *state, |
| 159 | const char *arg) |
| 160 | { |
| 161 | state->run_distro_boot = true; |
Simon Glass | 0fc3c22 | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 162 | return 0; |
| 163 | } |
Sjoerd Simons | 335f470 | 2015-04-30 22:16:09 +0200 | [diff] [blame] | 164 | SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands"); |
Simon Glass | 0fc3c22 | 2012-02-26 17:38:50 -0500 | [diff] [blame] | 165 | |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 166 | static int sandbox_cmdline_cb_command(struct sandbox_state *state, |
| 167 | const char *arg) |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 168 | { |
| 169 | state->cmd = arg; |
| 170 | return 0; |
| 171 | } |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 172 | SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command"); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 173 | |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 174 | 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] | 175 | { |
| 176 | state->fdt_fname = arg; |
| 177 | return 0; |
| 178 | } |
Simon Glass | 64367c8 | 2013-12-03 16:43:23 -0700 | [diff] [blame] | 179 | 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] | 180 | |
Simon Glass | 8e17078 | 2015-01-19 20:21:34 -0700 | [diff] [blame] | 181 | static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state, |
| 182 | const char *arg) |
| 183 | { |
| 184 | const char *fmt = "%s.dtb"; |
| 185 | char *fname; |
| 186 | int len; |
| 187 | |
| 188 | len = strlen(state->argv[0]) + strlen(fmt) + 1; |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 189 | fname = os_malloc(len); |
Simon Glass | 8e17078 | 2015-01-19 20:21:34 -0700 | [diff] [blame] | 190 | if (!fname) |
| 191 | return -ENOMEM; |
| 192 | snprintf(fname, len, fmt, state->argv[0]); |
| 193 | state->fdt_fname = fname; |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0, |
| 198 | "Use the default u-boot.dtb control FDT in U-Boot directory"); |
| 199 | |
Simon Glass | 3c3968f | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 200 | static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state, |
| 201 | const char *arg) |
| 202 | { |
Simon Glass | d74c461 | 2022-09-06 20:27:08 -0600 | [diff] [blame] | 203 | char buf[256]; |
Simon Glass | 3c3968f | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 204 | char *fname; |
| 205 | int len; |
| 206 | |
Simon Glass | d74c461 | 2022-09-06 20:27:08 -0600 | [diff] [blame] | 207 | len = state_get_rel_filename("arch/sandbox/dts/test.dtb", buf, |
| 208 | sizeof(buf)); |
| 209 | if (len < 0) |
| 210 | return len; |
| 211 | |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 212 | fname = os_malloc(len); |
Simon Glass | 3c3968f | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 213 | if (!fname) |
| 214 | return -ENOMEM; |
Simon Glass | d74c461 | 2022-09-06 20:27:08 -0600 | [diff] [blame] | 215 | strcpy(fname, buf); |
Simon Glass | 3c3968f | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 216 | state->fdt_fname = fname; |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0, |
| 221 | "Use the test.dtb control FDT in U-Boot directory"); |
| 222 | |
Simon Glass | f498e43 | 2013-11-10 10:27:02 -0700 | [diff] [blame] | 223 | static int sandbox_cmdline_cb_interactive(struct sandbox_state *state, |
| 224 | const char *arg) |
| 225 | { |
| 226 | state->interactive = true; |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode"); |
| 231 | |
Simon Glass | e990653 | 2014-02-27 13:26:16 -0700 | [diff] [blame] | 232 | static int sandbox_cmdline_cb_jump(struct sandbox_state *state, |
| 233 | const char *arg) |
| 234 | { |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 235 | /* Remember to delete this U-Boot image later */ |
| 236 | state->jumped_fname = arg; |
Simon Glass | e990653 | 2014-02-27 13:26:16 -0700 | [diff] [blame] | 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot"); |
| 241 | |
Simon Glass | f0b534f | 2022-10-20 18:23:02 -0600 | [diff] [blame] | 242 | static int sandbox_cmdline_cb_program(struct sandbox_state *state, |
| 243 | const char *arg) |
| 244 | { |
| 245 | /* |
| 246 | * Record the program name to use when jumping to future phases. This |
| 247 | * is the original executable which holds all the phases. We need to |
| 248 | * use this instead of argv[0] since each phase is started by |
| 249 | * extracting a particular binary from the full program, then running |
| 250 | * it. Therefore in that binary, argv[0] contains only the |
| 251 | * current-phase executable. |
| 252 | * |
| 253 | * For example, sandbox TPL may be started using image file: |
| 254 | * |
| 255 | * ./image.bin |
| 256 | * |
| 257 | * but then TPL needs to run VPL, which it does by extracting the VPL |
| 258 | * image from the image.bin file. |
| 259 | * |
| 260 | * ./temp-vpl |
| 261 | * |
| 262 | * When VPL runs it needs access to the original image.bin so it can |
| 263 | * extract the next phase (SPL). This works if we use '-f image.bin' |
| 264 | * when starting the original image.bin file. |
| 265 | */ |
| 266 | state->prog_fname = arg; |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | SANDBOX_CMDLINE_OPT_SHORT(program, 'p', 1, "U-Boot program name"); |
| 271 | |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 272 | static int sandbox_cmdline_cb_memory(struct sandbox_state *state, |
| 273 | const char *arg) |
| 274 | { |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 275 | /* For now assume we always want to write it */ |
| 276 | state->write_ram_buf = true; |
| 277 | state->ram_buf_fname = arg; |
Simon Glass | 24a284a | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 278 | state->ram_buf_read = true; |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1, |
| 283 | "Read/write ram_buf memory contents from file"); |
| 284 | |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 285 | static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state, |
| 286 | const char *arg) |
| 287 | { |
| 288 | state->ram_buf_rm = true; |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading"); |
| 293 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 294 | static int sandbox_cmdline_cb_state(struct sandbox_state *state, |
| 295 | const char *arg) |
| 296 | { |
| 297 | state->state_fname = arg; |
| 298 | return 0; |
| 299 | } |
| 300 | SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT"); |
| 301 | |
| 302 | static int sandbox_cmdline_cb_read(struct sandbox_state *state, |
| 303 | const char *arg) |
| 304 | { |
| 305 | state->read_state = true; |
| 306 | return 0; |
| 307 | } |
| 308 | SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup"); |
| 309 | |
| 310 | static int sandbox_cmdline_cb_write(struct sandbox_state *state, |
| 311 | const char *arg) |
| 312 | { |
| 313 | state->write_state = true; |
| 314 | return 0; |
| 315 | } |
| 316 | SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit"); |
| 317 | |
| 318 | static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state, |
| 319 | const char *arg) |
| 320 | { |
| 321 | state->ignore_missing_state_on_read = true; |
| 322 | return 0; |
| 323 | } |
| 324 | SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0, |
| 325 | "Ignore missing state on read"); |
| 326 | |
Simon Glass | b9ddbf4 | 2014-02-27 13:26:19 -0700 | [diff] [blame] | 327 | static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state, |
| 328 | const char *arg) |
| 329 | { |
| 330 | state->show_lcd = true; |
| 331 | return 0; |
| 332 | } |
| 333 | SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0, |
| 334 | "Show the sandbox LCD display"); |
| 335 | |
Simon Glass | f91de0b | 2020-02-03 07:36:13 -0700 | [diff] [blame] | 336 | static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state, |
| 337 | const char *arg) |
| 338 | { |
| 339 | state->double_lcd = true; |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0, |
| 344 | "Double the LCD display size in each direction"); |
| 345 | |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 346 | static const char *term_args[STATE_TERM_COUNT] = { |
| 347 | "raw-with-sigs", |
| 348 | "raw", |
| 349 | "cooked", |
| 350 | }; |
| 351 | |
| 352 | static int sandbox_cmdline_cb_terminal(struct sandbox_state *state, |
| 353 | const char *arg) |
| 354 | { |
| 355 | int i; |
| 356 | |
| 357 | for (i = 0; i < STATE_TERM_COUNT; i++) { |
| 358 | if (!strcmp(arg, term_args[i])) { |
| 359 | state->term_raw = i; |
| 360 | return 0; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | printf("Unknown terminal setting '%s' (", arg); |
| 365 | for (i = 0; i < STATE_TERM_COUNT; i++) |
| 366 | printf("%s%s", i ? ", " : "", term_args[i]); |
| 367 | puts(")\n"); |
| 368 | |
| 369 | return 1; |
| 370 | } |
| 371 | SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1, |
| 372 | "Set terminal to raw/cooked mode"); |
| 373 | |
Simon Glass | fe6d12a | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 374 | static int sandbox_cmdline_cb_verbose(struct sandbox_state *state, |
| 375 | const char *arg) |
| 376 | { |
| 377 | state->show_test_output = true; |
| 378 | return 0; |
| 379 | } |
| 380 | SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output"); |
| 381 | |
Simon Glass | 4e9a64d | 2018-10-01 11:55:11 -0600 | [diff] [blame] | 382 | static int sandbox_cmdline_cb_log_level(struct sandbox_state *state, |
| 383 | const char *arg) |
| 384 | { |
| 385 | state->default_log_level = simple_strtol(arg, NULL, 10); |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1, |
| 390 | "Set log level (0=panic, 7=debug)"); |
| 391 | |
Simon Glass | a4e289b | 2020-10-25 20:38:28 -0600 | [diff] [blame] | 392 | static int sandbox_cmdline_cb_unittests(struct sandbox_state *state, |
| 393 | const char *arg) |
| 394 | { |
| 395 | state->run_unittests = true; |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests"); |
| 400 | |
Simon Glass | eff9658 | 2020-10-25 20:38:33 -0600 | [diff] [blame] | 401 | static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state, |
| 402 | const char *arg) |
| 403 | { |
| 404 | state->select_unittests = arg; |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run"); |
| 409 | |
Simon Glass | b78cc9b | 2021-03-22 18:21:01 +1300 | [diff] [blame] | 410 | static int sandbox_cmdline_cb_signals(struct sandbox_state *state, |
| 411 | const char *arg) |
| 412 | { |
| 413 | state->handle_signals = true; |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | SANDBOX_CMDLINE_OPT_SHORT(signals, 'S', 0, |
| 418 | "Handle signals (such as SIGSEGV) in sandbox"); |
| 419 | |
Simon Glass | d8c6017 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 420 | static int sandbox_cmdline_cb_autoboot_keyed(struct sandbox_state *state, |
| 421 | const char *arg) |
| 422 | { |
| 423 | state->autoboot_keyed = true; |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | SANDBOX_CMDLINE_OPT(autoboot_keyed, 0, "Allow keyed autoboot"); |
| 428 | |
Simon Glass | b94eed5 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 429 | static void setup_ram_buf(struct sandbox_state *state) |
| 430 | { |
Simon Glass | 24a284a | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 431 | /* 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] | 432 | if (!state->ram_buf_read) |
Simon Glass | 24a284a | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 433 | memset(state->ram_buf, '\0', state->ram_size); |
Simon Glass | 24a284a | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 434 | |
Simon Glass | b94eed5 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 435 | gd->arch.ram_buf = state->ram_buf; |
| 436 | gd->ram_size = state->ram_size; |
| 437 | } |
| 438 | |
Simon Glass | 46508c9 | 2018-11-15 18:44:03 -0700 | [diff] [blame] | 439 | void state_show(struct sandbox_state *state) |
| 440 | { |
| 441 | char **p; |
| 442 | |
| 443 | printf("Arguments:\n"); |
| 444 | for (p = state->argv; *p; p++) |
| 445 | printf("%s ", *p); |
| 446 | printf("\n"); |
| 447 | } |
| 448 | |
Heinrich Schuchardt | 43eb872 | 2020-12-02 16:22:11 +0100 | [diff] [blame] | 449 | void __efi_runtime EFIAPI efi_reset_system( |
| 450 | enum efi_reset_type reset_type, |
| 451 | efi_status_t reset_status, |
| 452 | unsigned long data_size, void *reset_data) |
| 453 | { |
Heinrich Schuchardt | 7fb7254 | 2021-11-20 14:49:18 +0100 | [diff] [blame] | 454 | if (reset_type == EFI_RESET_SHUTDOWN) |
| 455 | sandbox_exit(); |
| 456 | else |
| 457 | sandbox_reset(); |
Heinrich Schuchardt | 43eb872 | 2020-12-02 16:22:11 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 460 | void sandbox_reset(void) |
| 461 | { |
| 462 | /* Do this here while it still has an effect */ |
| 463 | os_fd_restore(); |
| 464 | if (state_uninit()) |
| 465 | os_exit(2); |
| 466 | |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 467 | /* Restart U-Boot */ |
| 468 | os_relaunch(os_argv); |
| 469 | } |
| 470 | |
Andrew Scull | ca5d137 | 2022-05-30 10:00:10 +0000 | [diff] [blame] | 471 | int sandbox_main(int argc, char *argv[]) |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 472 | { |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 473 | struct sandbox_state *state; |
Heinrich Schuchardt | b8b9237 | 2021-05-15 19:29:13 +0200 | [diff] [blame] | 474 | void * text_base; |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 475 | gd_t data; |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 476 | int size; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 477 | int ret; |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 478 | |
Heinrich Schuchardt | b8b9237 | 2021-05-15 19:29:13 +0200 | [diff] [blame] | 479 | text_base = os_find_text_base(); |
| 480 | |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 481 | /* |
Heinrich Schuchardt | e6d24be | 2021-05-11 21:03:16 +0200 | [diff] [blame] | 482 | * This must be the first invocation of os_malloc() to have |
| 483 | * state->ram_buf in the low 4 GiB. |
| 484 | */ |
| 485 | ret = state_init(); |
| 486 | if (ret) |
| 487 | goto err; |
| 488 | |
| 489 | /* |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 490 | * Copy argv[] so that we can pass the arguments in the original |
| 491 | * sequence when resetting the sandbox. |
| 492 | */ |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 493 | size = sizeof(char *) * (argc + 1); |
| 494 | os_argv = os_malloc(size); |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 495 | if (!os_argv) |
| 496 | os_exit(1); |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 497 | memcpy(os_argv, argv, size); |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 498 | |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 499 | memset(&data, '\0', sizeof(data)); |
| 500 | gd = &data; |
Heinrich Schuchardt | b8b9237 | 2021-05-15 19:29:13 +0200 | [diff] [blame] | 501 | gd->arch.text_base = text_base; |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 502 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 503 | state = state_get_current(); |
| 504 | if (os_parse_args(state, argc, argv)) |
| 505 | return 1; |
| 506 | |
Simon Glass | 22c793a | 2023-09-26 08:14:47 -0600 | [diff] [blame^] | 507 | if (state->ram_buf_fname) { |
| 508 | ret = os_read_ram_buf(state->ram_buf_fname); |
| 509 | if (ret) { |
| 510 | printf("Failed to read RAM buffer '%s': %d\n", |
| 511 | state->ram_buf_fname, ret); |
| 512 | } else { |
| 513 | state->ram_buf_read = true; |
| 514 | log_debug("Read RAM buffer from '%s'\n", state->ram_buf_fname); |
| 515 | } |
| 516 | } |
| 517 | |
Patrick Delaunay | 6daf905 | 2020-11-20 09:48:33 +0100 | [diff] [blame] | 518 | /* Remove old memory file if required */ |
| 519 | if (state->ram_buf_rm && state->ram_buf_fname) { |
| 520 | os_unlink(state->ram_buf_fname); |
| 521 | state->write_ram_buf = false; |
| 522 | state->ram_buf_fname = NULL; |
| 523 | } |
| 524 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 525 | ret = sandbox_read_state(state, state->state_fname); |
| 526 | if (ret) |
| 527 | goto err; |
| 528 | |
Simon Glass | b78cc9b | 2021-03-22 18:21:01 +1300 | [diff] [blame] | 529 | if (state->handle_signals) { |
| 530 | ret = os_setup_signal_handlers(); |
| 531 | if (ret) |
| 532 | goto err; |
| 533 | } |
Heinrich Schuchardt | 28eb509 | 2020-11-12 00:29:56 +0100 | [diff] [blame] | 534 | |
Simon Glass | adad2d0 | 2023-09-26 08:14:27 -0600 | [diff] [blame] | 535 | #if CONFIG_IS_ENABLED(SYS_MALLOC_F) |
Tom Rini | fb52b94 | 2022-12-04 10:04:49 -0500 | [diff] [blame] | 536 | gd->malloc_base = CFG_MALLOC_F_ADDR; |
Simon Glass | 0cc6f5c | 2014-07-10 22:23:31 -0600 | [diff] [blame] | 537 | #endif |
Simon Glass | 4e9a64d | 2018-10-01 11:55:11 -0600 | [diff] [blame] | 538 | #if CONFIG_IS_ENABLED(LOG) |
| 539 | gd->default_log_level = state->default_log_level; |
| 540 | #endif |
Simon Glass | b94eed5 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 541 | setup_ram_buf(state); |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 542 | |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 543 | /* |
| 544 | * Set up the relocation offset here, since sandbox symbols are always |
| 545 | * relocated by the OS before sandbox is entered. |
| 546 | */ |
| 547 | gd->reloc_off = (ulong)gd->arch.text_base; |
| 548 | |
Patrick Delaunay | 053156d | 2020-11-27 11:20:55 +0100 | [diff] [blame] | 549 | /* sandbox test: log functions called before log_init in board_init_f */ |
Patrick Delaunay | 053156d | 2020-11-27 11:20:55 +0100 | [diff] [blame] | 550 | log_debug("debug: %s\n", __func__); |
| 551 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 552 | /* Do pre- and post-relocation init */ |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 553 | board_init_f(0); |
Allen Martin | 074b455 | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 554 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 555 | board_init_r(gd->new_gd, 0); |
| 556 | |
| 557 | /* NOTREACHED - board_init_r() does not return */ |
Allen Martin | 074b455 | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 558 | return 0; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 559 | |
| 560 | err: |
| 561 | printf("Error %d\n", ret); |
| 562 | return 1; |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 563 | } |