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> |
Sean Anderson | 44c925d | 2023-10-14 16:47:56 -0400 | [diff] [blame^] | 16 | #include <spl.h> |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 17 | #include <asm/getopt.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 19 | #include <asm/io.h> |
Simon Glass | 30f9205 | 2020-02-03 07:36:05 -0700 | [diff] [blame] | 20 | #include <asm/malloc.h> |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 21 | #include <asm/sections.h> |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 22 | #include <asm/state.h> |
Simon Glass | fe143ee | 2022-03-04 08:42:58 -0700 | [diff] [blame] | 23 | #include <dm/root.h> |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 24 | #include <linux/ctype.h> |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 25 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 28 | static char **os_argv; |
| 29 | |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 30 | /* Compare two options so that they can be sorted into alphabetical order */ |
| 31 | static int h_compare_opt(const void *p1, const void *p2) |
| 32 | { |
| 33 | const struct sandbox_cmdline_option *opt1 = p1; |
| 34 | const struct sandbox_cmdline_option *opt2 = p2; |
| 35 | const char *str1, *str2; |
| 36 | char flag1[2], flag2[2]; |
| 37 | |
| 38 | opt1 = *(struct sandbox_cmdline_option **)p1; |
| 39 | opt2 = *(struct sandbox_cmdline_option **)p2; |
| 40 | flag1[1] = '\0'; |
| 41 | flag2[1] = '\0'; |
| 42 | |
| 43 | *flag1 = opt1->flag_short < 0x100 ? opt1->flag_short : '\0'; |
| 44 | *flag2 = opt2->flag_short < 0x100 ? opt2->flag_short : '\0'; |
| 45 | |
| 46 | str1 = *flag1 ? flag1 : opt1->flag; |
| 47 | str2 = *flag2 ? flag2 : opt2->flag; |
| 48 | |
| 49 | /* |
| 50 | * Force lower-case flags to come before upper-case ones. We only |
| 51 | * support upper-case for short flags. |
| 52 | */ |
| 53 | if (isalpha(*str1) && isalpha(*str2) && |
| 54 | tolower(*str1) == tolower(*str2)) |
| 55 | return isupper(*str1) - isupper(*str2); |
| 56 | |
| 57 | return strcasecmp(str1, str2); |
| 58 | } |
| 59 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 60 | int sandbox_early_getopt_check(void) |
| 61 | { |
| 62 | struct sandbox_state *state = state_get_current(); |
Marek BehĂșn | 184c4af | 2021-05-20 13:24:06 +0200 | [diff] [blame] | 63 | struct sandbox_cmdline_option **sb_opt = |
| 64 | __u_boot_sandbox_option_start(); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 65 | size_t num_options = __u_boot_sandbox_option_count(); |
| 66 | size_t i; |
| 67 | int max_arg_len, max_noarg_len; |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 68 | struct sandbox_cmdline_option **sorted_opt; |
| 69 | int size; |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 70 | |
| 71 | /* parse_err will be a string of the faulting option */ |
| 72 | if (!state->parse_err) |
| 73 | return 0; |
| 74 | |
| 75 | if (strcmp(state->parse_err, "help")) { |
| 76 | printf("u-boot: error: failed while parsing option: %s\n" |
| 77 | "\ttry running with --help for more information.\n", |
| 78 | state->parse_err); |
| 79 | os_exit(1); |
| 80 | } |
| 81 | |
| 82 | printf( |
| 83 | "u-boot, a command line test interface to U-Boot\n\n" |
| 84 | "Usage: u-boot [options]\n" |
| 85 | "Options:\n"); |
| 86 | |
| 87 | max_arg_len = 0; |
| 88 | for (i = 0; i < num_options; ++i) |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 89 | 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] | 90 | max_noarg_len = max_arg_len + 7; |
| 91 | |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 92 | /* Sort the options */ |
| 93 | size = sizeof(*sorted_opt) * num_options; |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 94 | sorted_opt = os_malloc(size); |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 95 | if (!sorted_opt) { |
| 96 | printf("No memory to sort options\n"); |
| 97 | os_exit(1); |
| 98 | } |
| 99 | memcpy(sorted_opt, sb_opt, size); |
| 100 | qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt); |
| 101 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 102 | for (i = 0; i < num_options; ++i) { |
Simon Glass | 11c92b1 | 2020-02-03 07:35:47 -0700 | [diff] [blame] | 103 | struct sandbox_cmdline_option *opt = sorted_opt[i]; |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 104 | |
| 105 | /* first output the short flag if it has one */ |
| 106 | if (opt->flag_short >= 0x100) |
| 107 | printf(" "); |
| 108 | else |
| 109 | printf(" -%c, ", opt->flag_short); |
| 110 | |
| 111 | /* then the long flag */ |
| 112 | if (opt->has_arg) |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 113 | printf("--%-*s <arg> ", max_arg_len, opt->flag); |
Simon Glass | 5f679f0 | 2013-11-10 10:26:58 -0700 | [diff] [blame] | 114 | else |
| 115 | printf("--%-*s", max_noarg_len, opt->flag); |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 116 | |
| 117 | /* finally the help text */ |
| 118 | printf(" %s\n", opt->help); |
| 119 | } |
| 120 | |
| 121 | os_exit(0); |
| 122 | } |
Simon Glass | b8357c1 | 2023-08-21 21:16:56 -0600 | [diff] [blame] | 123 | EVENT_SPY_SIMPLE(EVT_MISC_INIT_F, sandbox_early_getopt_check); |
Simon Glass | b419dbb | 2017-03-28 10:27:28 -0600 | [diff] [blame] | 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 | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 190 | fname = os_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 | { |
Simon Glass | d74c461 | 2022-09-06 20:27:08 -0600 | [diff] [blame] | 204 | char buf[256]; |
Simon Glass | 3c3968f | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 205 | char *fname; |
Sean Anderson | 44c925d | 2023-10-14 16:47:56 -0400 | [diff] [blame^] | 206 | char *relname; |
Simon Glass | 3c3968f | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 207 | int len; |
| 208 | |
Sean Anderson | 44c925d | 2023-10-14 16:47:56 -0400 | [diff] [blame^] | 209 | if (spl_phase() <= PHASE_SPL) |
| 210 | relname = "../arch/sandbox/dts/test.dtb"; |
| 211 | else |
| 212 | relname = "arch/sandbox/dts/test.dtb"; |
| 213 | len = state_get_rel_filename(relname, buf, sizeof(buf)); |
Simon Glass | d74c461 | 2022-09-06 20:27:08 -0600 | [diff] [blame] | 214 | if (len < 0) |
| 215 | return len; |
| 216 | |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 217 | fname = os_malloc(len); |
Simon Glass | 3c3968f | 2019-09-25 08:56:07 -0600 | [diff] [blame] | 218 | if (!fname) |
| 219 | return -ENOMEM; |
Simon Glass | d74c461 | 2022-09-06 20:27:08 -0600 | [diff] [blame] | 220 | strcpy(fname, buf); |
Simon Glass | 3c3968f | 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 | f498e43 | 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 | e990653 | 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 | 47acfc6 | 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 | e990653 | 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 | f0b534f | 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 | 9dd10bf | 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 | { |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 280 | /* For now assume we always want to write it */ |
| 281 | state->write_ram_buf = true; |
| 282 | state->ram_buf_fname = arg; |
Simon Glass | 24a284a | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 283 | state->ram_buf_read = true; |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1, |
| 288 | "Read/write ram_buf memory contents from file"); |
| 289 | |
Simon Glass | 47acfc6 | 2014-02-27 13:26:23 -0700 | [diff] [blame] | 290 | static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state, |
| 291 | const char *arg) |
| 292 | { |
| 293 | state->ram_buf_rm = true; |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading"); |
| 298 | |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 299 | static int sandbox_cmdline_cb_state(struct sandbox_state *state, |
| 300 | const char *arg) |
| 301 | { |
| 302 | state->state_fname = arg; |
| 303 | return 0; |
| 304 | } |
| 305 | SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT"); |
| 306 | |
| 307 | static int sandbox_cmdline_cb_read(struct sandbox_state *state, |
| 308 | const char *arg) |
| 309 | { |
| 310 | state->read_state = true; |
| 311 | return 0; |
| 312 | } |
| 313 | SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup"); |
| 314 | |
| 315 | static int sandbox_cmdline_cb_write(struct sandbox_state *state, |
| 316 | const char *arg) |
| 317 | { |
| 318 | state->write_state = true; |
| 319 | return 0; |
| 320 | } |
| 321 | SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit"); |
| 322 | |
| 323 | static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state, |
| 324 | const char *arg) |
| 325 | { |
| 326 | state->ignore_missing_state_on_read = true; |
| 327 | return 0; |
| 328 | } |
| 329 | SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0, |
| 330 | "Ignore missing state on read"); |
| 331 | |
Simon Glass | b9ddbf4 | 2014-02-27 13:26:19 -0700 | [diff] [blame] | 332 | static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state, |
| 333 | const char *arg) |
| 334 | { |
| 335 | state->show_lcd = true; |
| 336 | return 0; |
| 337 | } |
| 338 | SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0, |
| 339 | "Show the sandbox LCD display"); |
| 340 | |
Simon Glass | f91de0b | 2020-02-03 07:36:13 -0700 | [diff] [blame] | 341 | static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state, |
| 342 | const char *arg) |
| 343 | { |
| 344 | state->double_lcd = true; |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0, |
| 349 | "Double the LCD display size in each direction"); |
| 350 | |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 351 | static const char *term_args[STATE_TERM_COUNT] = { |
| 352 | "raw-with-sigs", |
| 353 | "raw", |
| 354 | "cooked", |
| 355 | }; |
| 356 | |
| 357 | static int sandbox_cmdline_cb_terminal(struct sandbox_state *state, |
| 358 | const char *arg) |
| 359 | { |
| 360 | int i; |
| 361 | |
| 362 | for (i = 0; i < STATE_TERM_COUNT; i++) { |
| 363 | if (!strcmp(arg, term_args[i])) { |
| 364 | state->term_raw = i; |
| 365 | return 0; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | printf("Unknown terminal setting '%s' (", arg); |
| 370 | for (i = 0; i < STATE_TERM_COUNT; i++) |
| 371 | printf("%s%s", i ? ", " : "", term_args[i]); |
| 372 | puts(")\n"); |
| 373 | |
| 374 | return 1; |
| 375 | } |
| 376 | SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1, |
| 377 | "Set terminal to raw/cooked mode"); |
| 378 | |
Simon Glass | fe6d12a | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 379 | static int sandbox_cmdline_cb_verbose(struct sandbox_state *state, |
| 380 | const char *arg) |
| 381 | { |
| 382 | state->show_test_output = true; |
| 383 | return 0; |
| 384 | } |
| 385 | SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output"); |
| 386 | |
Simon Glass | 4e9a64d | 2018-10-01 11:55:11 -0600 | [diff] [blame] | 387 | static int sandbox_cmdline_cb_log_level(struct sandbox_state *state, |
| 388 | const char *arg) |
| 389 | { |
| 390 | state->default_log_level = simple_strtol(arg, NULL, 10); |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1, |
| 395 | "Set log level (0=panic, 7=debug)"); |
| 396 | |
Simon Glass | a4e289b | 2020-10-25 20:38:28 -0600 | [diff] [blame] | 397 | static int sandbox_cmdline_cb_unittests(struct sandbox_state *state, |
| 398 | const char *arg) |
| 399 | { |
| 400 | state->run_unittests = true; |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests"); |
| 405 | |
Simon Glass | eff9658 | 2020-10-25 20:38:33 -0600 | [diff] [blame] | 406 | static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state, |
| 407 | const char *arg) |
| 408 | { |
| 409 | state->select_unittests = arg; |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run"); |
| 414 | |
Simon Glass | b78cc9b | 2021-03-22 18:21:01 +1300 | [diff] [blame] | 415 | static int sandbox_cmdline_cb_signals(struct sandbox_state *state, |
| 416 | const char *arg) |
| 417 | { |
| 418 | state->handle_signals = true; |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | SANDBOX_CMDLINE_OPT_SHORT(signals, 'S', 0, |
| 423 | "Handle signals (such as SIGSEGV) in sandbox"); |
| 424 | |
Simon Glass | d8c6017 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 425 | static int sandbox_cmdline_cb_autoboot_keyed(struct sandbox_state *state, |
| 426 | const char *arg) |
| 427 | { |
| 428 | state->autoboot_keyed = true; |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | SANDBOX_CMDLINE_OPT(autoboot_keyed, 0, "Allow keyed autoboot"); |
| 433 | |
Simon Glass | b94eed5 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 434 | static void setup_ram_buf(struct sandbox_state *state) |
| 435 | { |
Simon Glass | 24a284a | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 436 | /* 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] | 437 | if (!state->ram_buf_read) |
Simon Glass | 24a284a | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 438 | memset(state->ram_buf, '\0', state->ram_size); |
Simon Glass | 24a284a | 2018-11-23 21:29:29 -0700 | [diff] [blame] | 439 | |
Simon Glass | b94eed5 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 440 | gd->arch.ram_buf = state->ram_buf; |
| 441 | gd->ram_size = state->ram_size; |
| 442 | } |
| 443 | |
Simon Glass | 46508c9 | 2018-11-15 18:44:03 -0700 | [diff] [blame] | 444 | void state_show(struct sandbox_state *state) |
| 445 | { |
| 446 | char **p; |
| 447 | |
| 448 | printf("Arguments:\n"); |
| 449 | for (p = state->argv; *p; p++) |
| 450 | printf("%s ", *p); |
| 451 | printf("\n"); |
| 452 | } |
| 453 | |
Heinrich Schuchardt | 43eb872 | 2020-12-02 16:22:11 +0100 | [diff] [blame] | 454 | void __efi_runtime EFIAPI efi_reset_system( |
| 455 | enum efi_reset_type reset_type, |
| 456 | efi_status_t reset_status, |
| 457 | unsigned long data_size, void *reset_data) |
| 458 | { |
Heinrich Schuchardt | 7fb7254 | 2021-11-20 14:49:18 +0100 | [diff] [blame] | 459 | if (reset_type == EFI_RESET_SHUTDOWN) |
| 460 | sandbox_exit(); |
| 461 | else |
| 462 | sandbox_reset(); |
Heinrich Schuchardt | 43eb872 | 2020-12-02 16:22:11 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 465 | void sandbox_reset(void) |
| 466 | { |
| 467 | /* Do this here while it still has an effect */ |
| 468 | os_fd_restore(); |
| 469 | if (state_uninit()) |
| 470 | os_exit(2); |
| 471 | |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 472 | /* Restart U-Boot */ |
| 473 | os_relaunch(os_argv); |
| 474 | } |
| 475 | |
Andrew Scull | ca5d137 | 2022-05-30 10:00:10 +0000 | [diff] [blame] | 476 | int sandbox_main(int argc, char *argv[]) |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 477 | { |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 478 | struct sandbox_state *state; |
Heinrich Schuchardt | b8b9237 | 2021-05-15 19:29:13 +0200 | [diff] [blame] | 479 | void * text_base; |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 480 | gd_t data; |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 481 | int size; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 482 | int ret; |
Simon Glass | 20bf89a | 2012-02-15 15:51:15 -0800 | [diff] [blame] | 483 | |
Heinrich Schuchardt | b8b9237 | 2021-05-15 19:29:13 +0200 | [diff] [blame] | 484 | text_base = os_find_text_base(); |
| 485 | |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 486 | /* |
Heinrich Schuchardt | e6d24be | 2021-05-11 21:03:16 +0200 | [diff] [blame] | 487 | * This must be the first invocation of os_malloc() to have |
| 488 | * state->ram_buf in the low 4 GiB. |
| 489 | */ |
| 490 | ret = state_init(); |
| 491 | if (ret) |
| 492 | goto err; |
| 493 | |
| 494 | /* |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 495 | * Copy argv[] so that we can pass the arguments in the original |
| 496 | * sequence when resetting the sandbox. |
| 497 | */ |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 498 | size = sizeof(char *) * (argc + 1); |
| 499 | os_argv = os_malloc(size); |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 500 | if (!os_argv) |
| 501 | os_exit(1); |
Simon Glass | edd094e | 2021-02-06 09:57:33 -0700 | [diff] [blame] | 502 | memcpy(os_argv, argv, size); |
Heinrich Schuchardt | 1c67844 | 2020-10-27 20:29:25 +0100 | [diff] [blame] | 503 | |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 504 | memset(&data, '\0', sizeof(data)); |
| 505 | gd = &data; |
Heinrich Schuchardt | b8b9237 | 2021-05-15 19:29:13 +0200 | [diff] [blame] | 506 | gd->arch.text_base = text_base; |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 507 | |
Simon Glass | 8a3e035 | 2012-02-15 15:51:16 -0800 | [diff] [blame] | 508 | state = state_get_current(); |
| 509 | if (os_parse_args(state, argc, argv)) |
| 510 | return 1; |
| 511 | |
Simon Glass | 22c793a | 2023-09-26 08:14:47 -0600 | [diff] [blame] | 512 | if (state->ram_buf_fname) { |
| 513 | ret = os_read_ram_buf(state->ram_buf_fname); |
| 514 | if (ret) { |
| 515 | printf("Failed to read RAM buffer '%s': %d\n", |
| 516 | state->ram_buf_fname, ret); |
| 517 | } else { |
| 518 | state->ram_buf_read = true; |
| 519 | log_debug("Read RAM buffer from '%s'\n", state->ram_buf_fname); |
| 520 | } |
| 521 | } |
| 522 | |
Patrick Delaunay | 6daf905 | 2020-11-20 09:48:33 +0100 | [diff] [blame] | 523 | /* Remove old memory file if required */ |
| 524 | if (state->ram_buf_rm && state->ram_buf_fname) { |
| 525 | os_unlink(state->ram_buf_fname); |
| 526 | state->write_ram_buf = false; |
| 527 | state->ram_buf_fname = NULL; |
| 528 | } |
| 529 | |
Simon Glass | 27191f1 | 2023-09-26 08:14:49 -0600 | [diff] [blame] | 530 | if (state->read_state && state->state_fname) { |
| 531 | ret = sandbox_read_state(state, state->state_fname); |
| 532 | if (ret) |
| 533 | goto err; |
| 534 | } |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 535 | |
Simon Glass | b78cc9b | 2021-03-22 18:21:01 +1300 | [diff] [blame] | 536 | if (state->handle_signals) { |
| 537 | ret = os_setup_signal_handlers(); |
| 538 | if (ret) |
| 539 | goto err; |
| 540 | } |
Heinrich Schuchardt | 28eb509 | 2020-11-12 00:29:56 +0100 | [diff] [blame] | 541 | |
Simon Glass | adad2d0 | 2023-09-26 08:14:27 -0600 | [diff] [blame] | 542 | #if CONFIG_IS_ENABLED(SYS_MALLOC_F) |
Tom Rini | fb52b94 | 2022-12-04 10:04:49 -0500 | [diff] [blame] | 543 | gd->malloc_base = CFG_MALLOC_F_ADDR; |
Simon Glass | 0cc6f5c | 2014-07-10 22:23:31 -0600 | [diff] [blame] | 544 | #endif |
Simon Glass | 4e9a64d | 2018-10-01 11:55:11 -0600 | [diff] [blame] | 545 | #if CONFIG_IS_ENABLED(LOG) |
| 546 | gd->default_log_level = state->default_log_level; |
| 547 | #endif |
Simon Glass | b94eed5 | 2017-03-28 10:27:16 -0600 | [diff] [blame] | 548 | setup_ram_buf(state); |
Simon Glass | c395fdf | 2014-07-10 22:23:27 -0600 | [diff] [blame] | 549 | |
Simon Glass | 752707a | 2019-04-08 13:20:41 -0600 | [diff] [blame] | 550 | /* |
| 551 | * Set up the relocation offset here, since sandbox symbols are always |
| 552 | * relocated by the OS before sandbox is entered. |
| 553 | */ |
| 554 | gd->reloc_off = (ulong)gd->arch.text_base; |
| 555 | |
Patrick Delaunay | 053156d | 2020-11-27 11:20:55 +0100 | [diff] [blame] | 556 | /* sandbox test: log functions called before log_init in board_init_f */ |
Patrick Delaunay | 053156d | 2020-11-27 11:20:55 +0100 | [diff] [blame] | 557 | log_debug("debug: %s\n", __func__); |
| 558 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 559 | /* Do pre- and post-relocation init */ |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 560 | board_init_f(0); |
Allen Martin | 074b455 | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 561 | |
Simon Glass | 2fd34e6 | 2013-11-10 10:26:59 -0700 | [diff] [blame] | 562 | board_init_r(gd->new_gd, 0); |
| 563 | |
| 564 | /* NOTREACHED - board_init_r() does not return */ |
Allen Martin | 074b455 | 2013-01-22 13:11:21 +0000 | [diff] [blame] | 565 | return 0; |
Simon Glass | d7c8d8d | 2013-11-10 10:27:04 -0700 | [diff] [blame] | 566 | |
| 567 | err: |
| 568 | printf("Error %d\n", ret); |
| 569 | return 1; |
Simon Glass | 5ba8ef6 | 2011-10-03 19:26:45 +0000 | [diff] [blame] | 570 | } |