blob: a03e5aa0b3038c5a421c65561cfb3c33235067b2 [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>
Simon Glass38068822015-05-04 11:31:08 -06009#include <errno.h>
Simon Glass691d7192020-05-10 11:40:02 -060010#include <init.h>
Simon Glass5c2859c2013-11-10 10:27:03 -070011#include <os.h>
Rabin Vincent7dbcb762014-10-29 23:21:38 +010012#include <cli.h>
Simon Glass4209be32020-02-03 07:35:47 -070013#include <sort.h>
Simon Glass70db4212012-02-15 15:51:16 -080014#include <asm/getopt.h>
Simon Glass4d94dfa2014-07-10 22:23:27 -060015#include <asm/io.h>
Simon Glass3ff6fe52020-02-03 07:36:05 -070016#include <asm/malloc.h>
Simon Glass70db4212012-02-15 15:51:16 -080017#include <asm/sections.h>
Simon Glass6fb62072012-02-15 15:51:15 -080018#include <asm/state.h>
Simon Glass4209be32020-02-03 07:35:47 -070019#include <linux/ctype.h>
Simon Glassbace3d02011-10-03 19:26:45 +000020
Simon Glass808434c2013-11-10 10:26:59 -070021DECLARE_GLOBAL_DATA_PTR;
22
Heinrich Schuchardt329dccc2020-10-27 20:29:25 +010023static char **os_argv;
24
Simon Glass4209be32020-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 Glass70db4212012-02-15 15:51:16 -080055int sandbox_early_getopt_check(void)
56{
57 struct sandbox_state *state = state_get_current();
Simon Glass7b3efc62013-12-03 16:43:23 -070058 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass70db4212012-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 Glass4209be32020-02-03 07:35:47 -070062 struct sandbox_cmdline_option **sorted_opt;
63 int size;
Simon Glass70db4212012-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 Yamadab4141192014-11-07 03:03:31 +090083 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
Simon Glass70db4212012-02-15 15:51:16 -080084 max_noarg_len = max_arg_len + 7;
85
Simon Glass4209be32020-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 Glass70db4212012-02-15 15:51:16 -080096 for (i = 0; i < num_options; ++i) {
Simon Glass4209be32020-02-03 07:35:47 -070097 struct sandbox_cmdline_option *opt = sorted_opt[i];
Simon Glass70db4212012-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 Glass70db4212012-02-15 15:51:16 -0800107 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass6ebcab82013-11-10 10:26:58 -0700108 else
109 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass70db4212012-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 Glass68969772017-03-28 10:27:28 -0600118int misc_init_f(void)
119{
120 return sandbox_early_getopt_check();
121}
122
Simon Glass7b3efc62013-12-03 16:43:23 -0700123static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass70db4212012-02-15 15:51:16 -0800124{
125 /* just flag to sandbox_early_getopt_check to show usage */
126 return 1;
127}
Simon Glass7b3efc62013-12-03 16:43:23 -0700128SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass70db4212012-02-15 15:51:16 -0800129
Simon Glassd0d07462016-07-04 11:57:50 -0600130#ifndef CONFIG_SPL_BUILD
Simon Glassab4e07e2012-02-26 17:38:50 -0500131int sandbox_main_loop_init(void)
132{
Simon Glass70db4212012-02-15 15:51:16 -0800133 struct sandbox_state *state = state_get_current();
134
135 /* Execute command if required */
Sjoerd Simonsebaa8322015-04-30 22:16:09 +0200136 if (state->cmd || state->run_distro_boot) {
137 int retval = 0;
Joe Hershberger88539e42015-02-06 15:37:31 -0600138
Rabin Vincent7dbcb762014-10-29 23:21:38 +0100139 cli_init();
140
Simon Glass2b6793d2016-03-13 19:07:30 -0600141#ifdef CONFIG_CMDLINE
Sjoerd Simonsebaa8322015-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 Glass2b6793d2016-03-13 19:07:30 -0600148#endif
Simon Glassc5a62d42013-11-10 10:27:02 -0700149 if (!state->interactive)
Joe Hershberger88539e42015-02-06 15:37:31 -0600150 os_exit(retval);
Simon Glass70db4212012-02-15 15:51:16 -0800151 }
152
Simon Glassab4e07e2012-02-26 17:38:50 -0500153 return 0;
154}
Simon Glassd0d07462016-07-04 11:57:50 -0600155#endif
Simon Glassab4e07e2012-02-26 17:38:50 -0500156
Sjoerd Simonsebaa8322015-04-30 22:16:09 +0200157static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
158 const char *arg)
159{
160 state->run_distro_boot = true;
161 return 0;
162}
163SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
164
Simon Glass7b3efc62013-12-03 16:43:23 -0700165static int sandbox_cmdline_cb_command(struct sandbox_state *state,
166 const char *arg)
Simon Glass70db4212012-02-15 15:51:16 -0800167{
168 state->cmd = arg;
169 return 0;
170}
Simon Glass7b3efc62013-12-03 16:43:23 -0700171SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass70db4212012-02-15 15:51:16 -0800172
Simon Glass7b3efc62013-12-03 16:43:23 -0700173static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glassf828bf22013-04-20 08:42:41 +0000174{
175 state->fdt_fname = arg;
176 return 0;
177}
Simon Glass7b3efc62013-12-03 16:43:23 -0700178SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glassf828bf22013-04-20 08:42:41 +0000179
Simon Glass1f32ae92015-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 Glass3ff6fe52020-02-03 07:36:05 -0700188 fname = malloc(len);
Simon Glass1f32ae92015-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 Glass189882c2019-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 Glass3ff6fe52020-02-03 07:36:05 -0700208 fname = malloc(len);
Simon Glass189882c2019-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 Glassc5a62d42013-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 Glassbda77732014-02-27 13:26:16 -0700233static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
234 const char *arg)
235{
Simon Glassab839dc2014-02-27 13:26:23 -0700236 /* Remember to delete this U-Boot image later */
237 state->jumped_fname = arg;
Simon Glassbda77732014-02-27 13:26:16 -0700238
239 return 0;
240}
241SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
242
Simon Glass5c2859c2013-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 Glassf80a8bb2014-11-11 12:47:08 -0700252 err = os_read_ram_buf(arg);
253 if (err) {
Simon Glass1c5a81d2018-10-01 11:55:12 -0600254 printf("Failed to read RAM buffer '%s': %d\n", arg, err);
Simon Glass5c2859c2013-11-10 10:27:03 -0700255 return err;
256 }
Simon Glassa65d1a02018-11-23 21:29:29 -0700257 state->ram_buf_read = true;
Simon Glass5c2859c2013-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 Glassab839dc2014-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 Glass1209e272013-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 Glass7d95f2a2014-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 Glass6be88c72020-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 Glassffb87902014-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 Glass9ce8b402015-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 Glass2b1dc292018-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 Glassb25ff5c2020-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 Glass22b29cc2020-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 Glassbb967242017-03-28 10:27:16 -0600389static void setup_ram_buf(struct sandbox_state *state)
390{
Simon Glassa65d1a02018-11-23 21:29:29 -0700391 /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
Simon Glass5dbe7942019-04-08 13:20:43 -0600392 if (!state->ram_buf_read)
Simon Glassa65d1a02018-11-23 21:29:29 -0700393 memset(state->ram_buf, '\0', state->ram_size);
Simon Glassa65d1a02018-11-23 21:29:29 -0700394
Simon Glassbb967242017-03-28 10:27:16 -0600395 gd->arch.ram_buf = state->ram_buf;
396 gd->ram_size = state->ram_size;
397}
398
Simon Glassd66ddaf2018-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 Schuchardt329dccc2020-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 Glassbace3d02011-10-03 19:26:45 +0000423int main(int argc, char *argv[])
424{
Simon Glass70db4212012-02-15 15:51:16 -0800425 struct sandbox_state *state;
Simon Glass4d94dfa2014-07-10 22:23:27 -0600426 gd_t data;
Simon Glass1209e272013-11-10 10:27:04 -0700427 int ret;
Simon Glass6fb62072012-02-15 15:51:15 -0800428
Heinrich Schuchardt329dccc2020-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 Glass001d1882019-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 Glass1209e272013-11-10 10:27:04 -0700442 ret = state_init();
443 if (ret)
444 goto err;
Simon Glass6fb62072012-02-15 15:51:15 -0800445
Simon Glass70db4212012-02-15 15:51:16 -0800446 state = state_get_current();
447 if (os_parse_args(state, argc, argv))
448 return 1;
449
Simon Glass1209e272013-11-10 10:27:04 -0700450 ret = sandbox_read_state(state, state->state_fname);
451 if (ret)
452 goto err;
453
Andy Yan1fc50d72017-07-24 17:49:59 +0800454#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glass29afe9e2014-07-10 22:23:31 -0600455 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
456#endif
Simon Glass2b1dc292018-10-01 11:55:11 -0600457#if CONFIG_IS_ENABLED(LOG)
458 gd->default_log_level = state->default_log_level;
459#endif
Simon Glassbb967242017-03-28 10:27:16 -0600460 setup_ram_buf(state);
Simon Glass4d94dfa2014-07-10 22:23:27 -0600461
Simon Glass001d1882019-04-08 13:20:41 -0600462 /*
463 * Set up the relocation offset here, since sandbox symbols are always
464 * relocated by the OS before sandbox is entered.
465 */
466 gd->reloc_off = (ulong)gd->arch.text_base;
467
Simon Glass808434c2013-11-10 10:26:59 -0700468 /* Do pre- and post-relocation init */
Simon Glassbace3d02011-10-03 19:26:45 +0000469 board_init_f(0);
Allen Martin8ec21bb2013-01-22 13:11:21 +0000470
Simon Glass808434c2013-11-10 10:26:59 -0700471 board_init_r(gd->new_gd, 0);
472
473 /* NOTREACHED - board_init_r() does not return */
Allen Martin8ec21bb2013-01-22 13:11:21 +0000474 return 0;
Simon Glass1209e272013-11-10 10:27:04 -0700475
476err:
477 printf("Error %d\n", ret);
478 return 1;
Simon Glassbace3d02011-10-03 19:26:45 +0000479}