blob: f6c98545e0d59d83deaff84008db802ddbafb093 [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>
Simon Glass07a3b232015-05-04 11:31:08 -06009#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060010#include <init.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070011#include <os.h>
Rabin Vincent7e0194a2014-10-29 23:21:38 +010012#include <cli.h>
Simon Glass11c92b12020-02-03 07:35:47 -070013#include <sort.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080014#include <asm/getopt.h>
Simon Glassc395fdf2014-07-10 22:23:27 -060015#include <asm/io.h>
Simon Glass30f92052020-02-03 07:36:05 -070016#include <asm/malloc.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080017#include <asm/sections.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080018#include <asm/state.h>
Simon Glass11c92b12020-02-03 07:35:47 -070019#include <linux/ctype.h>
Simon Glass5ba8ef62011-10-03 19:26:45 +000020
Simon Glass2fd34e62013-11-10 10:26:59 -070021DECLARE_GLOBAL_DATA_PTR;
22
Heinrich Schuchardt1c678442020-10-27 20:29:25 +010023static char **os_argv;
24
Simon Glass11c92b12020-02-03 07:35:47 -070025/* Compare two options so that they can be sorted into alphabetical order */
26static int h_compare_opt(const void *p1, const void *p2)
27{
28 const struct sandbox_cmdline_option *opt1 = p1;
29 const struct sandbox_cmdline_option *opt2 = p2;
30 const char *str1, *str2;
31 char flag1[2], flag2[2];
32
33 opt1 = *(struct sandbox_cmdline_option **)p1;
34 opt2 = *(struct sandbox_cmdline_option **)p2;
35 flag1[1] = '\0';
36 flag2[1] = '\0';
37
38 *flag1 = opt1->flag_short < 0x100 ? opt1->flag_short : '\0';
39 *flag2 = opt2->flag_short < 0x100 ? opt2->flag_short : '\0';
40
41 str1 = *flag1 ? flag1 : opt1->flag;
42 str2 = *flag2 ? flag2 : opt2->flag;
43
44 /*
45 * Force lower-case flags to come before upper-case ones. We only
46 * support upper-case for short flags.
47 */
48 if (isalpha(*str1) && isalpha(*str2) &&
49 tolower(*str1) == tolower(*str2))
50 return isupper(*str1) - isupper(*str2);
51
52 return strcasecmp(str1, str2);
53}
54
Simon Glass8a3e0352012-02-15 15:51:16 -080055int sandbox_early_getopt_check(void)
56{
57 struct sandbox_state *state = state_get_current();
Simon Glass64367c82013-12-03 16:43:23 -070058 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass8a3e0352012-02-15 15:51:16 -080059 size_t num_options = __u_boot_sandbox_option_count();
60 size_t i;
61 int max_arg_len, max_noarg_len;
Simon Glass11c92b12020-02-03 07:35:47 -070062 struct sandbox_cmdline_option **sorted_opt;
63 int size;
Simon Glass8a3e0352012-02-15 15:51:16 -080064
65 /* parse_err will be a string of the faulting option */
66 if (!state->parse_err)
67 return 0;
68
69 if (strcmp(state->parse_err, "help")) {
70 printf("u-boot: error: failed while parsing option: %s\n"
71 "\ttry running with --help for more information.\n",
72 state->parse_err);
73 os_exit(1);
74 }
75
76 printf(
77 "u-boot, a command line test interface to U-Boot\n\n"
78 "Usage: u-boot [options]\n"
79 "Options:\n");
80
81 max_arg_len = 0;
82 for (i = 0; i < num_options; ++i)
Masahiro Yamadadb204642014-11-07 03:03:31 +090083 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
Simon Glass8a3e0352012-02-15 15:51:16 -080084 max_noarg_len = max_arg_len + 7;
85
Simon Glass11c92b12020-02-03 07:35:47 -070086 /* Sort the options */
87 size = sizeof(*sorted_opt) * num_options;
88 sorted_opt = malloc(size);
89 if (!sorted_opt) {
90 printf("No memory to sort options\n");
91 os_exit(1);
92 }
93 memcpy(sorted_opt, sb_opt, size);
94 qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt);
95
Simon Glass8a3e0352012-02-15 15:51:16 -080096 for (i = 0; i < num_options; ++i) {
Simon Glass11c92b12020-02-03 07:35:47 -070097 struct sandbox_cmdline_option *opt = sorted_opt[i];
Simon Glass8a3e0352012-02-15 15:51:16 -080098
99 /* first output the short flag if it has one */
100 if (opt->flag_short >= 0x100)
101 printf(" ");
102 else
103 printf(" -%c, ", opt->flag_short);
104
105 /* then the long flag */
106 if (opt->has_arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800107 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass5f679f02013-11-10 10:26:58 -0700108 else
109 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass8a3e0352012-02-15 15:51:16 -0800110
111 /* finally the help text */
112 printf(" %s\n", opt->help);
113 }
114
115 os_exit(0);
116}
117
Simon Glassb419dbb2017-03-28 10:27:28 -0600118int misc_init_f(void)
119{
120 return sandbox_early_getopt_check();
121}
122
Simon Glass64367c82013-12-03 16:43:23 -0700123static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800124{
125 /* just flag to sandbox_early_getopt_check to show usage */
126 return 1;
127}
Simon Glass64367c82013-12-03 16:43:23 -0700128SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass8a3e0352012-02-15 15:51:16 -0800129
Simon Glass5cf16632016-07-04 11:57:50 -0600130#ifndef CONFIG_SPL_BUILD
Simon Glass0fc3c222012-02-26 17:38:50 -0500131int sandbox_main_loop_init(void)
132{
Simon Glass8a3e0352012-02-15 15:51:16 -0800133 struct sandbox_state *state = state_get_current();
134
135 /* Execute command if required */
Sjoerd Simons335f4702015-04-30 22:16:09 +0200136 if (state->cmd || state->run_distro_boot) {
137 int retval = 0;
Joe Hershberger744d7882015-02-06 15:37:31 -0600138
Rabin Vincent7e0194a2014-10-29 23:21:38 +0100139 cli_init();
140
Simon Glasse0f12802016-03-13 19:07:30 -0600141#ifdef CONFIG_CMDLINE
Sjoerd Simons335f4702015-04-30 22:16:09 +0200142 if (state->cmd)
143 retval = run_command_list(state->cmd, -1, 0);
144
145 if (state->run_distro_boot)
146 retval = cli_simple_run_command("run distro_bootcmd",
147 0);
Simon Glasse0f12802016-03-13 19:07:30 -0600148#endif
Simon Glassf498e432013-11-10 10:27:02 -0700149 if (!state->interactive)
Joe Hershberger744d7882015-02-06 15:37:31 -0600150 os_exit(retval);
Simon Glass8a3e0352012-02-15 15:51:16 -0800151 }
152
Sjoerd Simons335f4702015-04-30 22:16:09 +0200153 return 0;
154}
Simon Glass5cf16632016-07-04 11:57:50 -0600155#endif
Sjoerd Simons335f4702015-04-30 22:16:09 +0200156
157static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
158 const char *arg)
159{
160 state->run_distro_boot = true;
Simon Glass0fc3c222012-02-26 17:38:50 -0500161 return 0;
162}
Sjoerd Simons335f4702015-04-30 22:16:09 +0200163SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
Simon Glass0fc3c222012-02-26 17:38:50 -0500164
Simon Glass64367c82013-12-03 16:43:23 -0700165static int sandbox_cmdline_cb_command(struct sandbox_state *state,
166 const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800167{
168 state->cmd = arg;
169 return 0;
170}
Simon Glass64367c82013-12-03 16:43:23 -0700171SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass8a3e0352012-02-15 15:51:16 -0800172
Simon Glass64367c82013-12-03 16:43:23 -0700173static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glass15393432013-04-20 08:42:41 +0000174{
175 state->fdt_fname = arg;
176 return 0;
177}
Simon Glass64367c82013-12-03 16:43:23 -0700178SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glass15393432013-04-20 08:42:41 +0000179
Simon Glass8e170782015-01-19 20:21:34 -0700180static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
181 const char *arg)
182{
183 const char *fmt = "%s.dtb";
184 char *fname;
185 int len;
186
187 len = strlen(state->argv[0]) + strlen(fmt) + 1;
Simon Glass30f92052020-02-03 07:36:05 -0700188 fname = malloc(len);
Simon Glass8e170782015-01-19 20:21:34 -0700189 if (!fname)
190 return -ENOMEM;
191 snprintf(fname, len, fmt, state->argv[0]);
192 state->fdt_fname = fname;
193
194 return 0;
195}
196SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
197 "Use the default u-boot.dtb control FDT in U-Boot directory");
198
Simon Glass3c3968f2019-09-25 08:56:07 -0600199static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
200 const char *arg)
201{
202 const char *fmt = "/arch/sandbox/dts/test.dtb";
203 char *p;
204 char *fname;
205 int len;
206
207 len = strlen(state->argv[0]) + strlen(fmt) + 1;
Simon Glass30f92052020-02-03 07:36:05 -0700208 fname = malloc(len);
Simon Glass3c3968f2019-09-25 08:56:07 -0600209 if (!fname)
210 return -ENOMEM;
211 strcpy(fname, state->argv[0]);
212 p = strrchr(fname, '/');
213 if (!p)
214 p = fname + strlen(fname);
215 len -= p - fname;
216 snprintf(p, len, fmt, p);
217 state->fdt_fname = fname;
218
219 return 0;
220}
221SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0,
222 "Use the test.dtb control FDT in U-Boot directory");
223
Simon Glassf498e432013-11-10 10:27:02 -0700224static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
225 const char *arg)
226{
227 state->interactive = true;
228 return 0;
229}
230
231SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
232
Simon Glasse9906532014-02-27 13:26:16 -0700233static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
234 const char *arg)
235{
Simon Glass47acfc62014-02-27 13:26:23 -0700236 /* Remember to delete this U-Boot image later */
237 state->jumped_fname = arg;
Simon Glasse9906532014-02-27 13:26:16 -0700238
239 return 0;
240}
241SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
242
Simon Glass9dd10bf2013-11-10 10:27:03 -0700243static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
244 const char *arg)
245{
246 int err;
247
248 /* For now assume we always want to write it */
249 state->write_ram_buf = true;
250 state->ram_buf_fname = arg;
251
Simon Glassc043e032014-11-11 12:47:08 -0700252 err = os_read_ram_buf(arg);
253 if (err) {
Simon Glass332894d2018-10-01 11:55:12 -0600254 printf("Failed to read RAM buffer '%s': %d\n", arg, err);
Simon Glass9dd10bf2013-11-10 10:27:03 -0700255 return err;
256 }
Simon Glass24a284a2018-11-23 21:29:29 -0700257 state->ram_buf_read = true;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700258
259 return 0;
260}
261SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
262 "Read/write ram_buf memory contents from file");
263
Simon Glass47acfc62014-02-27 13:26:23 -0700264static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
265 const char *arg)
266{
267 state->ram_buf_rm = true;
268
269 return 0;
270}
271SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
272
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700273static int sandbox_cmdline_cb_state(struct sandbox_state *state,
274 const char *arg)
275{
276 state->state_fname = arg;
277 return 0;
278}
279SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
280
281static int sandbox_cmdline_cb_read(struct sandbox_state *state,
282 const char *arg)
283{
284 state->read_state = true;
285 return 0;
286}
287SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
288
289static int sandbox_cmdline_cb_write(struct sandbox_state *state,
290 const char *arg)
291{
292 state->write_state = true;
293 return 0;
294}
295SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
296
297static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
298 const char *arg)
299{
300 state->ignore_missing_state_on_read = true;
301 return 0;
302}
303SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
304 "Ignore missing state on read");
305
Simon Glassb9ddbf42014-02-27 13:26:19 -0700306static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
307 const char *arg)
308{
309 state->show_lcd = true;
310 return 0;
311}
312SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
313 "Show the sandbox LCD display");
314
Simon Glassf91de0b2020-02-03 07:36:13 -0700315static int sandbox_cmdline_cb_double_lcd(struct sandbox_state *state,
316 const char *arg)
317{
318 state->double_lcd = true;
319
320 return 0;
321}
322SANDBOX_CMDLINE_OPT_SHORT(double_lcd, 'K', 0,
323 "Double the LCD display size in each direction");
324
Simon Glass678ef472014-02-27 13:26:22 -0700325static const char *term_args[STATE_TERM_COUNT] = {
326 "raw-with-sigs",
327 "raw",
328 "cooked",
329};
330
331static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
332 const char *arg)
333{
334 int i;
335
336 for (i = 0; i < STATE_TERM_COUNT; i++) {
337 if (!strcmp(arg, term_args[i])) {
338 state->term_raw = i;
339 return 0;
340 }
341 }
342
343 printf("Unknown terminal setting '%s' (", arg);
344 for (i = 0; i < STATE_TERM_COUNT; i++)
345 printf("%s%s", i ? ", " : "", term_args[i]);
346 puts(")\n");
347
348 return 1;
349}
350SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
351 "Set terminal to raw/cooked mode");
352
Simon Glassfe6d12a2015-11-08 23:47:50 -0700353static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
354 const char *arg)
355{
356 state->show_test_output = true;
357 return 0;
358}
359SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
360
Simon Glass4e9a64d2018-10-01 11:55:11 -0600361static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
362 const char *arg)
363{
364 state->default_log_level = simple_strtol(arg, NULL, 10);
365
366 return 0;
367}
368SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
369 "Set log level (0=panic, 7=debug)");
370
Simon Glassa4e289b2020-10-25 20:38:28 -0600371static int sandbox_cmdline_cb_unittests(struct sandbox_state *state,
372 const char *arg)
373{
374 state->run_unittests = true;
375
376 return 0;
377}
378SANDBOX_CMDLINE_OPT_SHORT(unittests, 'u', 0, "Run unit tests");
379
Simon Glasseff96582020-10-25 20:38:33 -0600380static int sandbox_cmdline_cb_select_unittests(struct sandbox_state *state,
381 const char *arg)
382{
383 state->select_unittests = arg;
384
385 return 0;
386}
387SANDBOX_CMDLINE_OPT_SHORT(select_unittests, 'k', 1, "Select unit tests to run");
388
Simon Glassb94eed52017-03-28 10:27:16 -0600389static void setup_ram_buf(struct sandbox_state *state)
390{
Simon Glass24a284a2018-11-23 21:29:29 -0700391 /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
Simon Glasscd3705e2019-04-08 13:20:43 -0600392 if (!state->ram_buf_read)
Simon Glass24a284a2018-11-23 21:29:29 -0700393 memset(state->ram_buf, '\0', state->ram_size);
Simon Glass24a284a2018-11-23 21:29:29 -0700394
Simon Glassb94eed52017-03-28 10:27:16 -0600395 gd->arch.ram_buf = state->ram_buf;
396 gd->ram_size = state->ram_size;
397}
398
Simon Glass46508c92018-11-15 18:44:03 -0700399void state_show(struct sandbox_state *state)
400{
401 char **p;
402
403 printf("Arguments:\n");
404 for (p = state->argv; *p; p++)
405 printf("%s ", *p);
406 printf("\n");
407}
408
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100409void sandbox_reset(void)
410{
411 /* Do this here while it still has an effect */
412 os_fd_restore();
413 if (state_uninit())
414 os_exit(2);
415
416 if (dm_uninit())
417 os_exit(2);
418
419 /* Restart U-Boot */
420 os_relaunch(os_argv);
421}
422
Simon Glass5ba8ef62011-10-03 19:26:45 +0000423int main(int argc, char *argv[])
424{
Simon Glass8a3e0352012-02-15 15:51:16 -0800425 struct sandbox_state *state;
Simon Glassc395fdf2014-07-10 22:23:27 -0600426 gd_t data;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700427 int ret;
Simon Glass20bf89a2012-02-15 15:51:15 -0800428
Heinrich Schuchardt1c678442020-10-27 20:29:25 +0100429 /*
430 * Copy argv[] so that we can pass the arguments in the original
431 * sequence when resetting the sandbox.
432 */
433 os_argv = calloc(argc + 1, sizeof(char *));
434 if (!os_argv)
435 os_exit(1);
436 memcpy(os_argv, argv, sizeof(char *) * (argc + 1));
437
Simon Glass752707a2019-04-08 13:20:41 -0600438 memset(&data, '\0', sizeof(data));
439 gd = &data;
440 gd->arch.text_base = os_find_text_base();
441
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700442 ret = state_init();
443 if (ret)
444 goto err;
Simon Glass20bf89a2012-02-15 15:51:15 -0800445
Simon Glass8a3e0352012-02-15 15:51:16 -0800446 state = state_get_current();
447 if (os_parse_args(state, argc, argv))
448 return 1;
449
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700450 ret = sandbox_read_state(state, state->state_fname);
451 if (ret)
452 goto err;
453
Heinrich Schuchardt28eb5092020-11-12 00:29:56 +0100454 ret = os_setup_signal_handlers();
455 if (ret)
456 goto err;
457
Andy Yan25428c42017-07-24 17:49:59 +0800458#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glass0cc6f5c2014-07-10 22:23:31 -0600459 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
460#endif
Simon Glass4e9a64d2018-10-01 11:55:11 -0600461#if CONFIG_IS_ENABLED(LOG)
462 gd->default_log_level = state->default_log_level;
463#endif
Simon Glassb94eed52017-03-28 10:27:16 -0600464 setup_ram_buf(state);
Simon Glassc395fdf2014-07-10 22:23:27 -0600465
Simon Glass752707a2019-04-08 13:20:41 -0600466 /*
467 * Set up the relocation offset here, since sandbox symbols are always
468 * relocated by the OS before sandbox is entered.
469 */
470 gd->reloc_off = (ulong)gd->arch.text_base;
471
Simon Glass2fd34e62013-11-10 10:26:59 -0700472 /* Do pre- and post-relocation init */
Simon Glass5ba8ef62011-10-03 19:26:45 +0000473 board_init_f(0);
Allen Martin074b4552013-01-22 13:11:21 +0000474
Simon Glass2fd34e62013-11-10 10:26:59 -0700475 board_init_r(gd->new_gd, 0);
476
477 /* NOTREACHED - board_init_r() does not return */
Allen Martin074b4552013-01-22 13:11:21 +0000478 return 0;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700479
480err:
481 printf("Error %d\n", ret);
482 return 1;
Simon Glass5ba8ef62011-10-03 19:26:45 +0000483}