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