blob: 2542580974742154998888180994cb60de9bfeb0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass5ba8ef62011-10-03 19:26:45 +00002/*
Simon Glass20bf89a2012-02-15 15:51:15 -08003 * Copyright (c) 2011-2012 The Chromium OS Authors.
Simon Glass5ba8ef62011-10-03 19:26:45 +00004 */
5
6#include <common.h>
Simon Glassadaaa482019-11-14 12:57:43 -07007#include <command.h>
Heinrich Schuchardt1c678442020-10-27 20:29:25 +01008#include <dm/root.h>
Heinrich Schuchardt43eb8722020-12-02 16:22:11 +01009#include <efi_loader.h>
Simon Glass07a3b232015-05-04 11:31:08 -060010#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Patrick Delaunay053156d2020-11-27 11:20:55 +010012#include <log.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070013#include <os.h>
Rabin Vincent7e0194a2014-10-29 23:21:38 +010014#include <cli.h>
Simon Glass11c92b12020-02-03 07:35:47 -070015#include <sort.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080016#include <asm/getopt.h>
Simon Glassc395fdf2014-07-10 22:23:27 -060017#include <asm/io.h>
Simon Glass30f92052020-02-03 07:36:05 -070018#include <asm/malloc.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080019#include <asm/sections.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080020#include <asm/state.h>
Simon Glass11c92b12020-02-03 07:35:47 -070021#include <linux/ctype.h>
Simon Glass5ba8ef62011-10-03 19:26:45 +000022
Simon Glass2fd34e62013-11-10 10:26:59 -070023DECLARE_GLOBAL_DATA_PTR;
24
Heinrich Schuchardt1c678442020-10-27 20:29:25 +010025static char **os_argv;
26
Simon Glass11c92b12020-02-03 07:35:47 -070027/* Compare two options so that they can be sorted into alphabetical order */
28static int h_compare_opt(const void *p1, const void *p2)
29{
30 const struct sandbox_cmdline_option *opt1 = p1;
31 const struct sandbox_cmdline_option *opt2 = p2;
32 const char *str1, *str2;
33 char flag1[2], flag2[2];
34
35 opt1 = *(struct sandbox_cmdline_option **)p1;
36 opt2 = *(struct sandbox_cmdline_option **)p2;
37 flag1[1] = '\0';
38 flag2[1] = '\0';
39
40 *flag1 = opt1->flag_short < 0x100 ? opt1->flag_short : '\0';
41 *flag2 = opt2->flag_short < 0x100 ? opt2->flag_short : '\0';
42
43 str1 = *flag1 ? flag1 : opt1->flag;
44 str2 = *flag2 ? flag2 : opt2->flag;
45
46 /*
47 * Force lower-case flags to come before upper-case ones. We only
48 * support upper-case for short flags.
49 */
50 if (isalpha(*str1) && isalpha(*str2) &&
51 tolower(*str1) == tolower(*str2))
52 return isupper(*str1) - isupper(*str2);
53
54 return strcasecmp(str1, str2);
55}
56
Simon Glass8a3e0352012-02-15 15:51:16 -080057int sandbox_early_getopt_check(void)
58{
59 struct sandbox_state *state = state_get_current();
Simon Glass64367c82013-12-03 16:43:23 -070060 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass8a3e0352012-02-15 15:51:16 -080061 size_t num_options = __u_boot_sandbox_option_count();
62 size_t i;
63 int max_arg_len, max_noarg_len;
Simon Glass11c92b12020-02-03 07:35:47 -070064 struct sandbox_cmdline_option **sorted_opt;
65 int size;
Simon Glass8a3e0352012-02-15 15:51:16 -080066
67 /* parse_err will be a string of the faulting option */
68 if (!state->parse_err)
69 return 0;
70
71 if (strcmp(state->parse_err, "help")) {
72 printf("u-boot: error: failed while parsing option: %s\n"
73 "\ttry running with --help for more information.\n",
74 state->parse_err);
75 os_exit(1);
76 }
77
78 printf(
79 "u-boot, a command line test interface to U-Boot\n\n"
80 "Usage: u-boot [options]\n"
81 "Options:\n");
82
83 max_arg_len = 0;
84 for (i = 0; i < num_options; ++i)
Masahiro Yamadadb204642014-11-07 03:03:31 +090085 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
Simon Glass8a3e0352012-02-15 15:51:16 -080086 max_noarg_len = max_arg_len + 7;
87
Simon Glass11c92b12020-02-03 07:35:47 -070088 /* Sort the options */
89 size = sizeof(*sorted_opt) * num_options;
90 sorted_opt = malloc(size);
91 if (!sorted_opt) {
92 printf("No memory to sort options\n");
93 os_exit(1);
94 }
95 memcpy(sorted_opt, sb_opt, size);
96 qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt);
97
Simon Glass8a3e0352012-02-15 15:51:16 -080098 for (i = 0; i < num_options; ++i) {
Simon Glass11c92b12020-02-03 07:35:47 -070099 struct sandbox_cmdline_option *opt = sorted_opt[i];
Simon Glass8a3e0352012-02-15 15:51:16 -0800100
101 /* first output the short flag if it has one */
102 if (opt->flag_short >= 0x100)
103 printf(" ");
104 else
105 printf(" -%c, ", opt->flag_short);
106
107 /* then the long flag */
108 if (opt->has_arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800109 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass5f679f02013-11-10 10:26:58 -0700110 else
111 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass8a3e0352012-02-15 15:51:16 -0800112
113 /* finally the help text */
114 printf(" %s\n", opt->help);
115 }
116
117 os_exit(0);
118}
119
Simon Glassb419dbb2017-03-28 10:27:28 -0600120int misc_init_f(void)
121{
122 return sandbox_early_getopt_check();
123}
124
Simon Glass64367c82013-12-03 16:43:23 -0700125static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800126{
127 /* just flag to sandbox_early_getopt_check to show usage */
128 return 1;
129}
Simon Glass64367c82013-12-03 16:43:23 -0700130SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass8a3e0352012-02-15 15:51:16 -0800131
Simon Glass5cf16632016-07-04 11:57:50 -0600132#ifndef CONFIG_SPL_BUILD
Simon Glass0fc3c222012-02-26 17:38:50 -0500133int sandbox_main_loop_init(void)
134{
Simon Glass8a3e0352012-02-15 15:51:16 -0800135 struct sandbox_state *state = state_get_current();
136
137 /* Execute command if required */
Sjoerd Simons335f4702015-04-30 22:16:09 +0200138 if (state->cmd || state->run_distro_boot) {
139 int retval = 0;
Joe Hershberger744d7882015-02-06 15:37:31 -0600140
Rabin Vincent7e0194a2014-10-29 23:21:38 +0100141 cli_init();
142
Simon Glasse0f12802016-03-13 19:07:30 -0600143#ifdef CONFIG_CMDLINE
Sjoerd Simons335f4702015-04-30 22:16:09 +0200144 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 Glasse0f12802016-03-13 19:07:30 -0600150#endif
Simon Glassf498e432013-11-10 10:27:02 -0700151 if (!state->interactive)
Joe Hershberger744d7882015-02-06 15:37:31 -0600152 os_exit(retval);
Simon Glass8a3e0352012-02-15 15:51:16 -0800153 }
154
Sjoerd Simons335f4702015-04-30 22:16:09 +0200155 return 0;
156}
Simon Glass5cf16632016-07-04 11:57:50 -0600157#endif
Sjoerd Simons335f4702015-04-30 22:16:09 +0200158
159static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
160 const char *arg)
161{
162 state->run_distro_boot = true;
Simon Glass0fc3c222012-02-26 17:38:50 -0500163 return 0;
164}
Sjoerd Simons335f4702015-04-30 22:16:09 +0200165SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
Simon Glass0fc3c222012-02-26 17:38:50 -0500166
Simon Glass64367c82013-12-03 16:43:23 -0700167static int sandbox_cmdline_cb_command(struct sandbox_state *state,
168 const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800169{
170 state->cmd = arg;
171 return 0;
172}
Simon Glass64367c82013-12-03 16:43:23 -0700173SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass8a3e0352012-02-15 15:51:16 -0800174
Simon Glass64367c82013-12-03 16:43:23 -0700175static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glass15393432013-04-20 08:42:41 +0000176{
177 state->fdt_fname = arg;
178 return 0;
179}
Simon Glass64367c82013-12-03 16:43:23 -0700180SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glass15393432013-04-20 08:42:41 +0000181
Simon Glass8e170782015-01-19 20:21:34 -0700182static 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 Glass30f92052020-02-03 07:36:05 -0700190 fname = malloc(len);
Simon Glass8e170782015-01-19 20:21:34 -0700191 if (!fname)
192 return -ENOMEM;
193 snprintf(fname, len, fmt, state->argv[0]);
194 state->fdt_fname = fname;
195
196 return 0;
197}
198SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
199 "Use the default u-boot.dtb control FDT in U-Boot directory");
200
Simon Glass3c3968f2019-09-25 08:56:07 -0600201static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
202 const char *arg)
203{
204 const char *fmt = "/arch/sandbox/dts/test.dtb";
205 char *p;
206 char *fname;
207 int len;
208
209 len = strlen(state->argv[0]) + strlen(fmt) + 1;
Simon Glass30f92052020-02-03 07:36:05 -0700210 fname = malloc(len);
Simon Glass3c3968f2019-09-25 08:56:07 -0600211 if (!fname)
212 return -ENOMEM;
213 strcpy(fname, state->argv[0]);
214 p = strrchr(fname, '/');
215 if (!p)
216 p = fname + strlen(fname);
217 len -= p - fname;
Heinrich Schuchardt9f4821f2020-12-25 16:04:26 +0100218 snprintf(p, len, fmt);
Simon Glass3c3968f2019-09-25 08:56:07 -0600219 state->fdt_fname = fname;
220
221 return 0;
222}
223SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0,
224 "Use the test.dtb control FDT in U-Boot directory");
225
Simon Glassf498e432013-11-10 10:27:02 -0700226static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
227 const char *arg)
228{
229 state->interactive = true;
230 return 0;
231}
232
233SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
234
Simon Glasse9906532014-02-27 13:26:16 -0700235static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
236 const char *arg)
237{
Simon Glass47acfc62014-02-27 13:26:23 -0700238 /* Remember to delete this U-Boot image later */
239 state->jumped_fname = arg;
Simon Glasse9906532014-02-27 13:26:16 -0700240
241 return 0;
242}
243SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
244
Simon Glass9dd10bf2013-11-10 10:27:03 -0700245static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
246 const char *arg)
247{
248 int err;
249
250 /* For now assume we always want to write it */
251 state->write_ram_buf = true;
252 state->ram_buf_fname = arg;
253
Simon Glassc043e032014-11-11 12:47:08 -0700254 err = os_read_ram_buf(arg);
255 if (err) {
Simon Glass332894d2018-10-01 11:55:12 -0600256 printf("Failed to read RAM buffer '%s': %d\n", arg, err);
Simon Glass9dd10bf2013-11-10 10:27:03 -0700257 return err;
258 }
Simon Glass24a284a2018-11-23 21:29:29 -0700259 state->ram_buf_read = true;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700260
261 return 0;
262}
263SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
264 "Read/write ram_buf memory contents from file");
265
Simon Glass47acfc62014-02-27 13:26:23 -0700266static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
267 const char *arg)
268{
269 state->ram_buf_rm = true;
270
271 return 0;
272}
273SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
274
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700275static int sandbox_cmdline_cb_state(struct sandbox_state *state,
276 const char *arg)
277{
278 state->state_fname = arg;
279 return 0;
280}
281SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
282
283static int sandbox_cmdline_cb_read(struct sandbox_state *state,
284 const char *arg)
285{
286 state->read_state = true;
287 return 0;
288}
289SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
290
291static int sandbox_cmdline_cb_write(struct sandbox_state *state,
292 const char *arg)
293{
294 state->write_state = true;
295 return 0;
296}
297SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
298
299static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
300 const char *arg)
301{
302 state->ignore_missing_state_on_read = true;
303 return 0;
304}
305SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
306 "Ignore missing state on read");
307
Simon Glassb9ddbf42014-02-27 13:26:19 -0700308static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
309 const char *arg)
310{
311 state->show_lcd = true;
312 return 0;
313}
314SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
315 "Show the sandbox LCD display");
316
Simon Glassf91de0b2020-02-03 07:36:13 -0700317static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state,
318 const char *arg)
319{
320 state->double_lcd = true;
321
322 return 0;
323}
324SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0,
325 "Double the LCD display size in each direction");
326
Simon Glass678ef472014-02-27 13:26:22 -0700327static const char *term_args[STATE_TERM_COUNT] = {
328 "raw-with-sigs",
329 "raw",
330 "cooked",
331};
332
333static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
334 const char *arg)
335{
336 int i;
337
338 for (i = 0; i < STATE_TERM_COUNT; i++) {
339 if (!strcmp(arg, term_args[i])) {
340 state->term_raw = i;
341 return 0;
342 }
343 }
344
345 printf("Unknown terminal setting '%s' (", arg);
346 for (i = 0; i < STATE_TERM_COUNT; i++)
347 printf("%s%s", i ? ", " : "", term_args[i]);
348 puts(")\n");
349
350 return 1;
351}
352SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
353 "Set terminal to raw/cooked mode");
354
Simon Glassfe6d12a2015-11-08 23:47:50 -0700355static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
356 const char *arg)
357{
358 state->show_test_output = true;
359 return 0;
360}
361SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
362
Simon Glass4e9a64d2018-10-01 11:55:11 -0600363static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
364 const char *arg)
365{
366 state->default_log_level = simple_strtol(arg, NULL, 10);
367
368 return 0;
369}
370SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
371 "Set log level (0=panic, 7=debug)");
372
Simon Glassa4e289b2020-10-25 20:38:28 -0600373static int sandbox_cmdline_cb_unittests(struct sandbox_state *state,
374 const char *arg)
375{
376 state->run_unittests = true;
377
378 return 0;
379}
380SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests");
381
Simon Glasseff96582020-10-25 20:38:33 -0600382static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state,
383 const char *arg)
384{
385 state->select_unittests = arg;
386
387 return 0;
388}
389SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run");
390
Simon Glassb94eed52017-03-28 10:27:16 -0600391static void setup_ram_buf(struct sandbox_state *state)
392{
Simon Glass24a284a2018-11-23 21:29:29 -0700393 /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
Simon Glasscd3705e2019-04-08 13:20:43 -0600394 if (!state->ram_buf_read)
Simon Glass24a284a2018-11-23 21:29:29 -0700395 memset(state->ram_buf, '\0', state->ram_size);
Simon Glass24a284a2018-11-23 21:29:29 -0700396
Simon Glassb94eed52017-03-28 10:27:16 -0600397 gd->arch.ram_buf = state->ram_buf;
398 gd->ram_size = state->ram_size;
399}
400
Simon Glass46508c92018-11-15 18:44:03 -0700401void state_show(struct sandbox_state *state)
402{
403 char **p;
404
405 printf("Arguments:\n");
406 for (p = state->argv; *p; p++)
407 printf("%s ", *p);
408 printf("\n");
409}
410
Heinrich Schuchardt43eb8722020-12-02 16:22:11 +0100411void __efi_runtime EFIAPI efi_reset_system(
412 enum efi_reset_type reset_type,
413 efi_status_t reset_status,
414 unsigned long data_size, void *reset_data)
415{
416 os_fd_restore();
417 os_relaunch(os_argv);
418}
419
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100420void sandbox_reset(void)
421{
422 /* Do this here while it still has an effect */
423 os_fd_restore();
424 if (state_uninit())
425 os_exit(2);
426
427 if (dm_uninit())
428 os_exit(2);
429
430 /* Restart U-Boot */
431 os_relaunch(os_argv);
432}
433
Simon Glass5ba8ef62011-10-03 19:26:45 +0000434int main(int argc, char *argv[])
435{
Simon Glass8a3e0352012-02-15 15:51:16 -0800436 struct sandbox_state *state;
Simon Glassc395fdf2014-07-10 22:23:27 -0600437 gd_t data;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700438 int ret;
Simon Glass20bf89a2012-02-15 15:51:15 -0800439
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100440 /*
441 * Copy argv[] so that we can pass the arguments in the original
442 * sequence when resetting the sandbox.
443 */
444 os_argv = calloc(argc + 1, sizeof(char *));
445 if (!os_argv)
446 os_exit(1);
447 memcpy(os_argv, argv, sizeof(char *) * (argc + 1));
448
Simon Glass752707a2019-04-08 13:20:41 -0600449 memset(&data, '\0', sizeof(data));
450 gd = &data;
451 gd->arch.text_base = os_find_text_base();
452
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700453 ret = state_init();
454 if (ret)
455 goto err;
Simon Glass20bf89a2012-02-15 15:51:15 -0800456
Simon Glass8a3e0352012-02-15 15:51:16 -0800457 state = state_get_current();
458 if (os_parse_args(state, argc, argv))
459 return 1;
460
Patrick Delaunay6daf9052020-11-20 09:48:33 +0100461 /* Remove old memory file if required */
462 if (state->ram_buf_rm && state->ram_buf_fname) {
463 os_unlink(state->ram_buf_fname);
464 state->write_ram_buf = false;
465 state->ram_buf_fname = NULL;
466 }
467
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700468 ret = sandbox_read_state(state, state->state_fname);
469 if (ret)
470 goto err;
471
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +0100472 ret = os_setup_signal_handlers();
473 if (ret)
474 goto err;
475
Andy Yan25428c42017-07-24 17:49:59 +0800476#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glass0cc6f5c2014-07-10 22:23:31 -0600477 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
478#endif
Simon Glass4e9a64d2018-10-01 11:55:11 -0600479#if CONFIG_IS_ENABLED(LOG)
480 gd->default_log_level = state->default_log_level;
481#endif
Simon Glassb94eed52017-03-28 10:27:16 -0600482 setup_ram_buf(state);
Simon Glassc395fdf2014-07-10 22:23:27 -0600483
Simon Glass752707a2019-04-08 13:20:41 -0600484 /*
485 * Set up the relocation offset here, since sandbox symbols are always
486 * relocated by the OS before sandbox is entered.
487 */
488 gd->reloc_off = (ulong)gd->arch.text_base;
489
Patrick Delaunay053156d2020-11-27 11:20:55 +0100490 /* sandbox test: log functions called before log_init in board_init_f */
491 log_info("sandbox: starting...\n");
492 log_debug("debug: %s\n", __func__);
493
Simon Glass2fd34e62013-11-10 10:26:59 -0700494 /* Do pre- and post-relocation init */
Simon Glass5ba8ef62011-10-03 19:26:45 +0000495 board_init_f(0);
Allen Martin074b4552013-01-22 13:11:21 +0000496
Simon Glass2fd34e62013-11-10 10:26:59 -0700497 board_init_r(gd->new_gd, 0);
498
499 /* NOTREACHED - board_init_r() does not return */
Allen Martin074b4552013-01-22 13:11:21 +0000500 return 0;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700501
502err:
503 printf("Error %d\n", ret);
504 return 1;
Simon Glass5ba8ef62011-10-03 19:26:45 +0000505}