blob: 8322ed7a1fe135ddab0edfa386c61f7eb1e917f7 [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>
Simon Glass9dd10bf2013-11-10 10:27:03 -070012#include <os.h>
Rabin Vincent7e0194a2014-10-29 23:21:38 +010013#include <cli.h>
Simon Glass11c92b12020-02-03 07:35:47 -070014#include <sort.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080015#include <asm/getopt.h>
Simon Glassc395fdf2014-07-10 22:23:27 -060016#include <asm/io.h>
Simon Glass30f92052020-02-03 07:36:05 -070017#include <asm/malloc.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080018#include <asm/sections.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080019#include <asm/state.h>
Simon Glass11c92b12020-02-03 07:35:47 -070020#include <linux/ctype.h>
Simon Glass5ba8ef62011-10-03 19:26:45 +000021
Simon Glass2fd34e62013-11-10 10:26:59 -070022DECLARE_GLOBAL_DATA_PTR;
23
Heinrich Schuchardt1c678442020-10-27 20:29:25 +010024static char **os_argv;
25
Simon Glass11c92b12020-02-03 07:35:47 -070026/* Compare two options so that they can be sorted into alphabetical order */
27static int h_compare_opt(const void *p1, const void *p2)
28{
29 const struct sandbox_cmdline_option *opt1 = p1;
30 const struct sandbox_cmdline_option *opt2 = p2;
31 const char *str1, *str2;
32 char flag1[2], flag2[2];
33
34 opt1 = *(struct sandbox_cmdline_option **)p1;
35 opt2 = *(struct sandbox_cmdline_option **)p2;
36 flag1[1] = '\0';
37 flag2[1] = '\0';
38
39 *flag1 = opt1->flag_short < 0x100 ? opt1->flag_short : '\0';
40 *flag2 = opt2->flag_short < 0x100 ? opt2->flag_short : '\0';
41
42 str1 = *flag1 ? flag1 : opt1->flag;
43 str2 = *flag2 ? flag2 : opt2->flag;
44
45 /*
46 * Force lower-case flags to come before upper-case ones. We only
47 * support upper-case for short flags.
48 */
49 if (isalpha(*str1) && isalpha(*str2) &&
50 tolower(*str1) == tolower(*str2))
51 return isupper(*str1) - isupper(*str2);
52
53 return strcasecmp(str1, str2);
54}
55
Simon Glass8a3e0352012-02-15 15:51:16 -080056int sandbox_early_getopt_check(void)
57{
58 struct sandbox_state *state = state_get_current();
Simon Glass64367c82013-12-03 16:43:23 -070059 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass8a3e0352012-02-15 15:51:16 -080060 size_t num_options = __u_boot_sandbox_option_count();
61 size_t i;
62 int max_arg_len, max_noarg_len;
Simon Glass11c92b12020-02-03 07:35:47 -070063 struct sandbox_cmdline_option **sorted_opt;
64 int size;
Simon Glass8a3e0352012-02-15 15:51:16 -080065
66 /* parse_err will be a string of the faulting option */
67 if (!state->parse_err)
68 return 0;
69
70 if (strcmp(state->parse_err, "help")) {
71 printf("u-boot: error: failed while parsing option: %s\n"
72 "\ttry running with --help for more information.\n",
73 state->parse_err);
74 os_exit(1);
75 }
76
77 printf(
78 "u-boot, a command line test interface to U-Boot\n\n"
79 "Usage: u-boot [options]\n"
80 "Options:\n");
81
82 max_arg_len = 0;
83 for (i = 0; i < num_options; ++i)
Masahiro Yamadadb204642014-11-07 03:03:31 +090084 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
Simon Glass8a3e0352012-02-15 15:51:16 -080085 max_noarg_len = max_arg_len + 7;
86
Simon Glass11c92b12020-02-03 07:35:47 -070087 /* Sort the options */
88 size = sizeof(*sorted_opt) * num_options;
89 sorted_opt = malloc(size);
90 if (!sorted_opt) {
91 printf("No memory to sort options\n");
92 os_exit(1);
93 }
94 memcpy(sorted_opt, sb_opt, size);
95 qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt);
96
Simon Glass8a3e0352012-02-15 15:51:16 -080097 for (i = 0; i < num_options; ++i) {
Simon Glass11c92b12020-02-03 07:35:47 -070098 struct sandbox_cmdline_option *opt = sorted_opt[i];
Simon Glass8a3e0352012-02-15 15:51:16 -080099
100 /* first output the short flag if it has one */
101 if (opt->flag_short >= 0x100)
102 printf(" ");
103 else
104 printf(" -%c, ", opt->flag_short);
105
106 /* then the long flag */
107 if (opt->has_arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800108 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass5f679f02013-11-10 10:26:58 -0700109 else
110 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass8a3e0352012-02-15 15:51:16 -0800111
112 /* finally the help text */
113 printf(" %s\n", opt->help);
114 }
115
116 os_exit(0);
117}
118
Simon Glassb419dbb2017-03-28 10:27:28 -0600119int misc_init_f(void)
120{
121 return sandbox_early_getopt_check();
122}
123
Simon Glass64367c82013-12-03 16:43:23 -0700124static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800125{
126 /* just flag to sandbox_early_getopt_check to show usage */
127 return 1;
128}
Simon Glass64367c82013-12-03 16:43:23 -0700129SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass8a3e0352012-02-15 15:51:16 -0800130
Simon Glass5cf16632016-07-04 11:57:50 -0600131#ifndef CONFIG_SPL_BUILD
Simon Glass0fc3c222012-02-26 17:38:50 -0500132int sandbox_main_loop_init(void)
133{
Simon Glass8a3e0352012-02-15 15:51:16 -0800134 struct sandbox_state *state = state_get_current();
135
136 /* Execute command if required */
Sjoerd Simons335f4702015-04-30 22:16:09 +0200137 if (state->cmd || state->run_distro_boot) {
138 int retval = 0;
Joe Hershberger744d7882015-02-06 15:37:31 -0600139
Rabin Vincent7e0194a2014-10-29 23:21:38 +0100140 cli_init();
141
Simon Glasse0f12802016-03-13 19:07:30 -0600142#ifdef CONFIG_CMDLINE
Sjoerd Simons335f4702015-04-30 22:16:09 +0200143 if (state->cmd)
144 retval = run_command_list(state->cmd, -1, 0);
145
146 if (state->run_distro_boot)
147 retval = cli_simple_run_command("run distro_bootcmd",
148 0);
Simon Glasse0f12802016-03-13 19:07:30 -0600149#endif
Simon Glassf498e432013-11-10 10:27:02 -0700150 if (!state->interactive)
Joe Hershberger744d7882015-02-06 15:37:31 -0600151 os_exit(retval);
Simon Glass8a3e0352012-02-15 15:51:16 -0800152 }
153
Sjoerd Simons335f4702015-04-30 22:16:09 +0200154 return 0;
155}
Simon Glass5cf16632016-07-04 11:57:50 -0600156#endif
Sjoerd Simons335f4702015-04-30 22:16:09 +0200157
158static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
159 const char *arg)
160{
161 state->run_distro_boot = true;
Simon Glass0fc3c222012-02-26 17:38:50 -0500162 return 0;
163}
Sjoerd Simons335f4702015-04-30 22:16:09 +0200164SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
Simon Glass0fc3c222012-02-26 17:38:50 -0500165
Simon Glass64367c82013-12-03 16:43:23 -0700166static int sandbox_cmdline_cb_command(struct sandbox_state *state,
167 const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800168{
169 state->cmd = arg;
170 return 0;
171}
Simon Glass64367c82013-12-03 16:43:23 -0700172SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass8a3e0352012-02-15 15:51:16 -0800173
Simon Glass64367c82013-12-03 16:43:23 -0700174static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glass15393432013-04-20 08:42:41 +0000175{
176 state->fdt_fname = arg;
177 return 0;
178}
Simon Glass64367c82013-12-03 16:43:23 -0700179SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glass15393432013-04-20 08:42:41 +0000180
Simon Glass8e170782015-01-19 20:21:34 -0700181static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
182 const char *arg)
183{
184 const char *fmt = "%s.dtb";
185 char *fname;
186 int len;
187
188 len = strlen(state->argv[0]) + strlen(fmt) + 1;
Simon Glass30f92052020-02-03 07:36:05 -0700189 fname = malloc(len);
Simon Glass8e170782015-01-19 20:21:34 -0700190 if (!fname)
191 return -ENOMEM;
192 snprintf(fname, len, fmt, state->argv[0]);
193 state->fdt_fname = fname;
194
195 return 0;
196}
197SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
198 "Use the default u-boot.dtb control FDT in U-Boot directory");
199
Simon Glass3c3968f2019-09-25 08:56:07 -0600200static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
201 const char *arg)
202{
203 const char *fmt = "/arch/sandbox/dts/test.dtb";
204 char *p;
205 char *fname;
206 int len;
207
208 len = strlen(state->argv[0]) + strlen(fmt) + 1;
Simon Glass30f92052020-02-03 07:36:05 -0700209 fname = malloc(len);
Simon Glass3c3968f2019-09-25 08:56:07 -0600210 if (!fname)
211 return -ENOMEM;
212 strcpy(fname, state->argv[0]);
213 p = strrchr(fname, '/');
214 if (!p)
215 p = fname + strlen(fname);
216 len -= p - fname;
217 snprintf(p, len, fmt, p);
218 state->fdt_fname = fname;
219
220 return 0;
221}
222SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0,
223 "Use the test.dtb control FDT in U-Boot directory");
224
Simon Glassf498e432013-11-10 10:27:02 -0700225static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
226 const char *arg)
227{
228 state->interactive = true;
229 return 0;
230}
231
232SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
233
Simon Glasse9906532014-02-27 13:26:16 -0700234static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
235 const char *arg)
236{
Simon Glass47acfc62014-02-27 13:26:23 -0700237 /* Remember to delete this U-Boot image later */
238 state->jumped_fname = arg;
Simon Glasse9906532014-02-27 13:26:16 -0700239
240 return 0;
241}
242SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
243
Simon Glass9dd10bf2013-11-10 10:27:03 -0700244static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
245 const char *arg)
246{
247 int err;
248
249 /* For now assume we always want to write it */
250 state->write_ram_buf = true;
251 state->ram_buf_fname = arg;
252
Simon Glassc043e032014-11-11 12:47:08 -0700253 err = os_read_ram_buf(arg);
254 if (err) {
Simon Glass332894d2018-10-01 11:55:12 -0600255 printf("Failed to read RAM buffer '%s': %d\n", arg, err);
Simon Glass9dd10bf2013-11-10 10:27:03 -0700256 return err;
257 }
Simon Glass24a284a2018-11-23 21:29:29 -0700258 state->ram_buf_read = true;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700259
260 return 0;
261}
262SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
263 "Read/write ram_buf memory contents from file");
264
Simon Glass47acfc62014-02-27 13:26:23 -0700265static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
266 const char *arg)
267{
268 state->ram_buf_rm = true;
269
270 return 0;
271}
272SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
273
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700274static int sandbox_cmdline_cb_state(struct sandbox_state *state,
275 const char *arg)
276{
277 state->state_fname = arg;
278 return 0;
279}
280SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
281
282static int sandbox_cmdline_cb_read(struct sandbox_state *state,
283 const char *arg)
284{
285 state->read_state = true;
286 return 0;
287}
288SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
289
290static int sandbox_cmdline_cb_write(struct sandbox_state *state,
291 const char *arg)
292{
293 state->write_state = true;
294 return 0;
295}
296SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
297
298static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
299 const char *arg)
300{
301 state->ignore_missing_state_on_read = true;
302 return 0;
303}
304SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
305 "Ignore missing state on read");
306
Simon Glassb9ddbf42014-02-27 13:26:19 -0700307static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
308 const char *arg)
309{
310 state->show_lcd = true;
311 return 0;
312}
313SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
314 "Show the sandbox LCD display");
315
Simon Glassf91de0b2020-02-03 07:36:13 -0700316static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state,
317 const char *arg)
318{
319 state->double_lcd = true;
320
321 return 0;
322}
323SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0,
324 "Double the LCD display size in each direction");
325
Simon Glass678ef472014-02-27 13:26:22 -0700326static const char *term_args[STATE_TERM_COUNT] = {
327 "raw-with-sigs",
328 "raw",
329 "cooked",
330};
331
332static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
333 const char *arg)
334{
335 int i;
336
337 for (i = 0; i < STATE_TERM_COUNT; i++) {
338 if (!strcmp(arg, term_args[i])) {
339 state->term_raw = i;
340 return 0;
341 }
342 }
343
344 printf("Unknown terminal setting '%s' (", arg);
345 for (i = 0; i < STATE_TERM_COUNT; i++)
346 printf("%s%s", i ? ", " : "", term_args[i]);
347 puts(")\n");
348
349 return 1;
350}
351SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
352 "Set terminal to raw/cooked mode");
353
Simon Glassfe6d12a2015-11-08 23:47:50 -0700354static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
355 const char *arg)
356{
357 state->show_test_output = true;
358 return 0;
359}
360SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
361
Simon Glass4e9a64d2018-10-01 11:55:11 -0600362static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
363 const char *arg)
364{
365 state->default_log_level = simple_strtol(arg, NULL, 10);
366
367 return 0;
368}
369SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
370 "Set log level (0=panic, 7=debug)");
371
Simon Glassa4e289b2020-10-25 20:38:28 -0600372static int sandbox_cmdline_cb_unittests(struct sandbox_state *state,
373 const char *arg)
374{
375 state->run_unittests = true;
376
377 return 0;
378}
379SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests");
380
Simon Glasseff96582020-10-25 20:38:33 -0600381static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state,
382 const char *arg)
383{
384 state->select_unittests = arg;
385
386 return 0;
387}
388SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run");
389
Simon Glassb94eed52017-03-28 10:27:16 -0600390static void setup_ram_buf(struct sandbox_state *state)
391{
Simon Glass24a284a2018-11-23 21:29:29 -0700392 /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
Simon Glasscd3705e2019-04-08 13:20:43 -0600393 if (!state->ram_buf_read)
Simon Glass24a284a2018-11-23 21:29:29 -0700394 memset(state->ram_buf, '\0', state->ram_size);
Simon Glass24a284a2018-11-23 21:29:29 -0700395
Simon Glassb94eed52017-03-28 10:27:16 -0600396 gd->arch.ram_buf = state->ram_buf;
397 gd->ram_size = state->ram_size;
398}
399
Simon Glass46508c92018-11-15 18:44:03 -0700400void state_show(struct sandbox_state *state)
401{
402 char **p;
403
404 printf("Arguments:\n");
405 for (p = state->argv; *p; p++)
406 printf("%s ", *p);
407 printf("\n");
408}
409
Heinrich Schuchardt43eb8722020-12-02 16:22:11 +0100410void __efi_runtime EFIAPI efi_reset_system(
411 enum efi_reset_type reset_type,
412 efi_status_t reset_status,
413 unsigned long data_size, void *reset_data)
414{
415 os_fd_restore();
416 os_relaunch(os_argv);
417}
418
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100419void sandbox_reset(void)
420{
421 /* Do this here while it still has an effect */
422 os_fd_restore();
423 if (state_uninit())
424 os_exit(2);
425
426 if (dm_uninit())
427 os_exit(2);
428
429 /* Restart U-Boot */
430 os_relaunch(os_argv);
431}
432
Simon Glass5ba8ef62011-10-03 19:26:45 +0000433int main(int argc, char *argv[])
434{
Simon Glass8a3e0352012-02-15 15:51:16 -0800435 struct sandbox_state *state;
Simon Glassc395fdf2014-07-10 22:23:27 -0600436 gd_t data;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700437 int ret;
Simon Glass20bf89a2012-02-15 15:51:15 -0800438
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100439 /*
440 * Copy argv[] so that we can pass the arguments in the original
441 * sequence when resetting the sandbox.
442 */
443 os_argv = calloc(argc + 1, sizeof(char *));
444 if (!os_argv)
445 os_exit(1);
446 memcpy(os_argv, argv, sizeof(char *) * (argc + 1));
447
Simon Glass752707a2019-04-08 13:20:41 -0600448 memset(&data, '\0', sizeof(data));
449 gd = &data;
450 gd->arch.text_base = os_find_text_base();
451
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700452 ret = state_init();
453 if (ret)
454 goto err;
Simon Glass20bf89a2012-02-15 15:51:15 -0800455
Simon Glass8a3e0352012-02-15 15:51:16 -0800456 state = state_get_current();
457 if (os_parse_args(state, argc, argv))
458 return 1;
459
Patrick Delaunay6daf9052020-11-20 09:48:33 +0100460 /* Remove old memory file if required */
461 if (state->ram_buf_rm && state->ram_buf_fname) {
462 os_unlink(state->ram_buf_fname);
463 state->write_ram_buf = false;
464 state->ram_buf_fname = NULL;
465 }
466
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700467 ret = sandbox_read_state(state, state->state_fname);
468 if (ret)
469 goto err;
470
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +0100471 ret = os_setup_signal_handlers();
472 if (ret)
473 goto err;
474
Andy Yan25428c42017-07-24 17:49:59 +0800475#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glass0cc6f5c2014-07-10 22:23:31 -0600476 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
477#endif
Simon Glass4e9a64d2018-10-01 11:55:11 -0600478#if CONFIG_IS_ENABLED(LOG)
479 gd->default_log_level = state->default_log_level;
480#endif
Simon Glassb94eed52017-03-28 10:27:16 -0600481 setup_ram_buf(state);
Simon Glassc395fdf2014-07-10 22:23:27 -0600482
Simon Glass752707a2019-04-08 13:20:41 -0600483 /*
484 * Set up the relocation offset here, since sandbox symbols are always
485 * relocated by the OS before sandbox is entered.
486 */
487 gd->reloc_off = (ulong)gd->arch.text_base;
488
Simon Glass2fd34e62013-11-10 10:26:59 -0700489 /* Do pre- and post-relocation init */
Simon Glass5ba8ef62011-10-03 19:26:45 +0000490 board_init_f(0);
Allen Martin074b4552013-01-22 13:11:21 +0000491
Simon Glass2fd34e62013-11-10 10:26:59 -0700492 board_init_r(gd->new_gd, 0);
493
494 /* NOTREACHED - board_init_r() does not return */
Allen Martin074b4552013-01-22 13:11:21 +0000495 return 0;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700496
497err:
498 printf("Error %d\n", ret);
499 return 1;
Simon Glass5ba8ef62011-10-03 19:26:45 +0000500}