blob: 483a2640404620dfaacb365aaf1cde0321262987 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassbace3d02011-10-03 19:26:45 +00002/*
Simon Glass6fb62072012-02-15 15:51:15 -08003 * Copyright (c) 2011-2012 The Chromium OS Authors.
Simon Glassbace3d02011-10-03 19:26:45 +00004 */
5
6#include <common.h>
Simon Glass288b29e2019-11-14 12:57:43 -07007#include <command.h>
Heinrich Schuchardt329dccc2020-10-27 20:29:25 +01008#include <dm/root.h>
Heinrich Schuchardt9c547292020-12-02 16:22:11 +01009#include <efi_loader.h>
Simon Glass38068822015-05-04 11:31:08 -060010#include <errno.h>
Simon Glass691d7192020-05-10 11:40:02 -060011#include <init.h>
Patrick Delaunaya4918b22020-11-27 11:20:55 +010012#include <log.h>
Simon Glass5c2859c2013-11-10 10:27:03 -070013#include <os.h>
Rabin Vincent7dbcb762014-10-29 23:21:38 +010014#include <cli.h>
Simon Glass4209be32020-02-03 07:35:47 -070015#include <sort.h>
Simon Glass70db4212012-02-15 15:51:16 -080016#include <asm/getopt.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass4d94dfa2014-07-10 22:23:27 -060018#include <asm/io.h>
Simon Glass3ff6fe52020-02-03 07:36:05 -070019#include <asm/malloc.h>
Simon Glass70db4212012-02-15 15:51:16 -080020#include <asm/sections.h>
Simon Glass6fb62072012-02-15 15:51:15 -080021#include <asm/state.h>
Simon Glass4209be32020-02-03 07:35:47 -070022#include <linux/ctype.h>
Simon Glassbace3d02011-10-03 19:26:45 +000023
Simon Glass808434c2013-11-10 10:26:59 -070024DECLARE_GLOBAL_DATA_PTR;
25
Heinrich Schuchardt329dccc2020-10-27 20:29:25 +010026static char **os_argv;
27
Simon Glass4209be32020-02-03 07:35:47 -070028/* Compare two options so that they can be sorted into alphabetical order */
29static 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 Glass70db4212012-02-15 15:51:16 -080058int sandbox_early_getopt_check(void)
59{
60 struct sandbox_state *state = state_get_current();
Simon Glass7b3efc62013-12-03 16:43:23 -070061 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass70db4212012-02-15 15:51:16 -080062 size_t num_options = __u_boot_sandbox_option_count();
63 size_t i;
64 int max_arg_len, max_noarg_len;
Simon Glass4209be32020-02-03 07:35:47 -070065 struct sandbox_cmdline_option **sorted_opt;
66 int size;
Simon Glass70db4212012-02-15 15:51:16 -080067
68 /* parse_err will be a string of the faulting option */
69 if (!state->parse_err)
70 return 0;
71
72 if (strcmp(state->parse_err, "help")) {
73 printf("u-boot: error: failed while parsing option: %s\n"
74 "\ttry running with --help for more information.\n",
75 state->parse_err);
76 os_exit(1);
77 }
78
79 printf(
80 "u-boot, a command line test interface to U-Boot\n\n"
81 "Usage: u-boot [options]\n"
82 "Options:\n");
83
84 max_arg_len = 0;
85 for (i = 0; i < num_options; ++i)
Masahiro Yamadab4141192014-11-07 03:03:31 +090086 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
Simon Glass70db4212012-02-15 15:51:16 -080087 max_noarg_len = max_arg_len + 7;
88
Simon Glass4209be32020-02-03 07:35:47 -070089 /* Sort the options */
90 size = sizeof(*sorted_opt) * num_options;
91 sorted_opt = malloc(size);
92 if (!sorted_opt) {
93 printf("No memory to sort options\n");
94 os_exit(1);
95 }
96 memcpy(sorted_opt, sb_opt, size);
97 qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt);
98
Simon Glass70db4212012-02-15 15:51:16 -080099 for (i = 0; i < num_options; ++i) {
Simon Glass4209be32020-02-03 07:35:47 -0700100 struct sandbox_cmdline_option *opt = sorted_opt[i];
Simon Glass70db4212012-02-15 15:51:16 -0800101
102 /* first output the short flag if it has one */
103 if (opt->flag_short >= 0x100)
104 printf(" ");
105 else
106 printf(" -%c, ", opt->flag_short);
107
108 /* then the long flag */
109 if (opt->has_arg)
Simon Glass70db4212012-02-15 15:51:16 -0800110 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass6ebcab82013-11-10 10:26:58 -0700111 else
112 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass70db4212012-02-15 15:51:16 -0800113
114 /* finally the help text */
115 printf(" %s\n", opt->help);
116 }
117
118 os_exit(0);
119}
120
Simon Glass68969772017-03-28 10:27:28 -0600121int misc_init_f(void)
122{
123 return sandbox_early_getopt_check();
124}
125
Simon Glass7b3efc62013-12-03 16:43:23 -0700126static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass70db4212012-02-15 15:51:16 -0800127{
128 /* just flag to sandbox_early_getopt_check to show usage */
129 return 1;
130}
Simon Glass7b3efc62013-12-03 16:43:23 -0700131SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass70db4212012-02-15 15:51:16 -0800132
Simon Glassd0d07462016-07-04 11:57:50 -0600133#ifndef CONFIG_SPL_BUILD
Simon Glassab4e07e2012-02-26 17:38:50 -0500134int sandbox_main_loop_init(void)
135{
Simon Glass70db4212012-02-15 15:51:16 -0800136 struct sandbox_state *state = state_get_current();
137
138 /* Execute command if required */
Sjoerd Simonsebaa8322015-04-30 22:16:09 +0200139 if (state->cmd || state->run_distro_boot) {
140 int retval = 0;
Joe Hershberger88539e42015-02-06 15:37:31 -0600141
Rabin Vincent7dbcb762014-10-29 23:21:38 +0100142 cli_init();
143
Simon Glass2b6793d2016-03-13 19:07:30 -0600144#ifdef CONFIG_CMDLINE
Sjoerd Simonsebaa8322015-04-30 22:16:09 +0200145 if (state->cmd)
146 retval = run_command_list(state->cmd, -1, 0);
147
148 if (state->run_distro_boot)
149 retval = cli_simple_run_command("run distro_bootcmd",
150 0);
Simon Glass2b6793d2016-03-13 19:07:30 -0600151#endif
Simon Glassc5a62d42013-11-10 10:27:02 -0700152 if (!state->interactive)
Joe Hershberger88539e42015-02-06 15:37:31 -0600153 os_exit(retval);
Simon Glass70db4212012-02-15 15:51:16 -0800154 }
155
Simon Glassab4e07e2012-02-26 17:38:50 -0500156 return 0;
157}
Simon Glassd0d07462016-07-04 11:57:50 -0600158#endif
Simon Glassab4e07e2012-02-26 17:38:50 -0500159
Sjoerd Simonsebaa8322015-04-30 22:16:09 +0200160static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
161 const char *arg)
162{
163 state->run_distro_boot = true;
164 return 0;
165}
166SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
167
Simon Glass7b3efc62013-12-03 16:43:23 -0700168static int sandbox_cmdline_cb_command(struct sandbox_state *state,
169 const char *arg)
Simon Glass70db4212012-02-15 15:51:16 -0800170{
171 state->cmd = arg;
172 return 0;
173}
Simon Glass7b3efc62013-12-03 16:43:23 -0700174SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass70db4212012-02-15 15:51:16 -0800175
Simon Glass7b3efc62013-12-03 16:43:23 -0700176static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glassf828bf22013-04-20 08:42:41 +0000177{
178 state->fdt_fname = arg;
179 return 0;
180}
Simon Glass7b3efc62013-12-03 16:43:23 -0700181SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glassf828bf22013-04-20 08:42:41 +0000182
Simon Glass1f32ae92015-01-19 20:21:34 -0700183static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
184 const char *arg)
185{
186 const char *fmt = "%s.dtb";
187 char *fname;
188 int len;
189
190 len = strlen(state->argv[0]) + strlen(fmt) + 1;
Simon Glass3ff6fe52020-02-03 07:36:05 -0700191 fname = malloc(len);
Simon Glass1f32ae92015-01-19 20:21:34 -0700192 if (!fname)
193 return -ENOMEM;
194 snprintf(fname, len, fmt, state->argv[0]);
195 state->fdt_fname = fname;
196
197 return 0;
198}
199SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
200 "Use the default u-boot.dtb control FDT in U-Boot directory");
201
Simon Glass189882c2019-09-25 08:56:07 -0600202static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
203 const char *arg)
204{
205 const char *fmt = "/arch/sandbox/dts/test.dtb";
206 char *p;
207 char *fname;
208 int len;
209
210 len = strlen(state->argv[0]) + strlen(fmt) + 1;
Simon Glass3ff6fe52020-02-03 07:36:05 -0700211 fname = malloc(len);
Simon Glass189882c2019-09-25 08:56:07 -0600212 if (!fname)
213 return -ENOMEM;
214 strcpy(fname, state->argv[0]);
215 p = strrchr(fname, '/');
216 if (!p)
217 p = fname + strlen(fname);
218 len -= p - fname;
Heinrich Schuchardtb680c5532020-12-25 16:04:26 +0100219 snprintf(p, len, fmt);
Simon Glass189882c2019-09-25 08:56:07 -0600220 state->fdt_fname = fname;
221
222 return 0;
223}
224SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0,
225 "Use the test.dtb control FDT in U-Boot directory");
226
Simon Glassc5a62d42013-11-10 10:27:02 -0700227static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
228 const char *arg)
229{
230 state->interactive = true;
231 return 0;
232}
233
234SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
235
Simon Glassbda77732014-02-27 13:26:16 -0700236static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
237 const char *arg)
238{
Simon Glassab839dc2014-02-27 13:26:23 -0700239 /* Remember to delete this U-Boot image later */
240 state->jumped_fname = arg;
Simon Glassbda77732014-02-27 13:26:16 -0700241
242 return 0;
243}
244SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
245
Simon Glass5c2859c2013-11-10 10:27:03 -0700246static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
247 const char *arg)
248{
249 int err;
250
251 /* For now assume we always want to write it */
252 state->write_ram_buf = true;
253 state->ram_buf_fname = arg;
254
Simon Glassf80a8bb2014-11-11 12:47:08 -0700255 err = os_read_ram_buf(arg);
256 if (err) {
Simon Glass1c5a81d2018-10-01 11:55:12 -0600257 printf("Failed to read RAM buffer '%s': %d\n", arg, err);
Simon Glass5c2859c2013-11-10 10:27:03 -0700258 return err;
259 }
Simon Glassa65d1a02018-11-23 21:29:29 -0700260 state->ram_buf_read = true;
Simon Glass5c2859c2013-11-10 10:27:03 -0700261
262 return 0;
263}
264SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
265 "Read/write ram_buf memory contents from file");
266
Simon Glassab839dc2014-02-27 13:26:23 -0700267static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
268 const char *arg)
269{
270 state->ram_buf_rm = true;
271
272 return 0;
273}
274SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
275
Simon Glass1209e272013-11-10 10:27:04 -0700276static int sandbox_cmdline_cb_state(struct sandbox_state *state,
277 const char *arg)
278{
279 state->state_fname = arg;
280 return 0;
281}
282SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
283
284static int sandbox_cmdline_cb_read(struct sandbox_state *state,
285 const char *arg)
286{
287 state->read_state = true;
288 return 0;
289}
290SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
291
292static int sandbox_cmdline_cb_write(struct sandbox_state *state,
293 const char *arg)
294{
295 state->write_state = true;
296 return 0;
297}
298SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
299
300static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
301 const char *arg)
302{
303 state->ignore_missing_state_on_read = true;
304 return 0;
305}
306SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
307 "Ignore missing state on read");
308
Simon Glass7d95f2a2014-02-27 13:26:19 -0700309static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
310 const char *arg)
311{
312 state->show_lcd = true;
313 return 0;
314}
315SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
316 "Show the sandbox LCD display");
317
Simon Glass6be88c72020-02-03 07:36:13 -0700318static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state,
319 const char *arg)
320{
321 state->double_lcd = true;
322
323 return 0;
324}
325SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0,
326 "Double the LCD display size in each direction");
327
Simon Glassffb87902014-02-27 13:26:22 -0700328static const char *term_args[STATE_TERM_COUNT] = {
329 "raw-with-sigs",
330 "raw",
331 "cooked",
332};
333
334static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
335 const char *arg)
336{
337 int i;
338
339 for (i = 0; i < STATE_TERM_COUNT; i++) {
340 if (!strcmp(arg, term_args[i])) {
341 state->term_raw = i;
342 return 0;
343 }
344 }
345
346 printf("Unknown terminal setting '%s' (", arg);
347 for (i = 0; i < STATE_TERM_COUNT; i++)
348 printf("%s%s", i ? ", " : "", term_args[i]);
349 puts(")\n");
350
351 return 1;
352}
353SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
354 "Set terminal to raw/cooked mode");
355
Simon Glass9ce8b402015-11-08 23:47:50 -0700356static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
357 const char *arg)
358{
359 state->show_test_output = true;
360 return 0;
361}
362SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
363
Simon Glass2b1dc292018-10-01 11:55:11 -0600364static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
365 const char *arg)
366{
367 state->default_log_level = simple_strtol(arg, NULL, 10);
368
369 return 0;
370}
371SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
372 "Set log level (0=panic, 7=debug)");
373
Simon Glassb25ff5c2020-10-25 20:38:28 -0600374static int sandbox_cmdline_cb_unittests(struct sandbox_state *state,
375 const char *arg)
376{
377 state->run_unittests = true;
378
379 return 0;
380}
381SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests");
382
Simon Glass22b29cc2020-10-25 20:38:33 -0600383static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state,
384 const char *arg)
385{
386 state->select_unittests = arg;
387
388 return 0;
389}
390SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run");
391
Simon Glassbb967242017-03-28 10:27:16 -0600392static void setup_ram_buf(struct sandbox_state *state)
393{
Simon Glassa65d1a02018-11-23 21:29:29 -0700394 /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
Simon Glass5dbe7942019-04-08 13:20:43 -0600395 if (!state->ram_buf_read)
Simon Glassa65d1a02018-11-23 21:29:29 -0700396 memset(state->ram_buf, '\0', state->ram_size);
Simon Glassa65d1a02018-11-23 21:29:29 -0700397
Simon Glassbb967242017-03-28 10:27:16 -0600398 gd->arch.ram_buf = state->ram_buf;
399 gd->ram_size = state->ram_size;
400}
401
Simon Glassd66ddaf2018-11-15 18:44:03 -0700402void state_show(struct sandbox_state *state)
403{
404 char **p;
405
406 printf("Arguments:\n");
407 for (p = state->argv; *p; p++)
408 printf("%s ", *p);
409 printf("\n");
410}
411
Heinrich Schuchardt9c547292020-12-02 16:22:11 +0100412void __efi_runtime EFIAPI efi_reset_system(
413 enum efi_reset_type reset_type,
414 efi_status_t reset_status,
415 unsigned long data_size, void *reset_data)
416{
417 os_fd_restore();
418 os_relaunch(os_argv);
419}
420
Heinrich Schuchardt329dccc2020-10-27 20:29:25 +0100421void sandbox_reset(void)
422{
423 /* Do this here while it still has an effect */
424 os_fd_restore();
425 if (state_uninit())
426 os_exit(2);
427
428 if (dm_uninit())
429 os_exit(2);
430
431 /* Restart U-Boot */
432 os_relaunch(os_argv);
433}
434
Simon Glassbace3d02011-10-03 19:26:45 +0000435int main(int argc, char *argv[])
436{
Simon Glass70db4212012-02-15 15:51:16 -0800437 struct sandbox_state *state;
Simon Glass4d94dfa2014-07-10 22:23:27 -0600438 gd_t data;
Simon Glass1209e272013-11-10 10:27:04 -0700439 int ret;
Simon Glass6fb62072012-02-15 15:51:15 -0800440
Heinrich Schuchardt329dccc2020-10-27 20:29:25 +0100441 /*
442 * Copy argv[] so that we can pass the arguments in the original
443 * sequence when resetting the sandbox.
444 */
445 os_argv = calloc(argc + 1, sizeof(char *));
446 if (!os_argv)
447 os_exit(1);
448 memcpy(os_argv, argv, sizeof(char *) * (argc + 1));
449
Simon Glass001d1882019-04-08 13:20:41 -0600450 memset(&data, '\0', sizeof(data));
451 gd = &data;
452 gd->arch.text_base = os_find_text_base();
453
Simon Glass1209e272013-11-10 10:27:04 -0700454 ret = state_init();
455 if (ret)
456 goto err;
Simon Glass6fb62072012-02-15 15:51:15 -0800457
Simon Glass70db4212012-02-15 15:51:16 -0800458 state = state_get_current();
459 if (os_parse_args(state, argc, argv))
460 return 1;
461
Patrick Delaunay10bb90f2020-11-20 09:48:33 +0100462 /* Remove old memory file if required */
463 if (state->ram_buf_rm && state->ram_buf_fname) {
464 os_unlink(state->ram_buf_fname);
465 state->write_ram_buf = false;
466 state->ram_buf_fname = NULL;
467 }
468
Simon Glass1209e272013-11-10 10:27:04 -0700469 ret = sandbox_read_state(state, state->state_fname);
470 if (ret)
471 goto err;
472
Heinrich Schuchardtb46f30a2020-11-12 00:29:56 +0100473 ret = os_setup_signal_handlers();
474 if (ret)
475 goto err;
476
Andy Yan1fc50d72017-07-24 17:49:59 +0800477#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glass29afe9e2014-07-10 22:23:31 -0600478 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
479#endif
Simon Glass2b1dc292018-10-01 11:55:11 -0600480#if CONFIG_IS_ENABLED(LOG)
481 gd->default_log_level = state->default_log_level;
482#endif
Simon Glassbb967242017-03-28 10:27:16 -0600483 setup_ram_buf(state);
Simon Glass4d94dfa2014-07-10 22:23:27 -0600484
Simon Glass001d1882019-04-08 13:20:41 -0600485 /*
486 * Set up the relocation offset here, since sandbox symbols are always
487 * relocated by the OS before sandbox is entered.
488 */
489 gd->reloc_off = (ulong)gd->arch.text_base;
490
Patrick Delaunaya4918b22020-11-27 11:20:55 +0100491 /* sandbox test: log functions called before log_init in board_init_f */
492 log_info("sandbox: starting...\n");
493 log_debug("debug: %s\n", __func__);
494
Simon Glass808434c2013-11-10 10:26:59 -0700495 /* Do pre- and post-relocation init */
Simon Glassbace3d02011-10-03 19:26:45 +0000496 board_init_f(0);
Allen Martin8ec21bb2013-01-22 13:11:21 +0000497
Simon Glass808434c2013-11-10 10:26:59 -0700498 board_init_r(gd->new_gd, 0);
499
500 /* NOTREACHED - board_init_r() does not return */
Allen Martin8ec21bb2013-01-22 13:11:21 +0000501 return 0;
Simon Glass1209e272013-11-10 10:27:04 -0700502
503err:
504 printf("Error %d\n", ret);
505 return 1;
Simon Glassbace3d02011-10-03 19:26:45 +0000506}