blob: 82828f0c1d45f5dcbb7fa7e28c6656e468189a3b [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 Glass07a3b232015-05-04 11:31:08 -06007#include <errno.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -07008#include <os.h>
Rabin Vincent7e0194a2014-10-29 23:21:38 +01009#include <cli.h>
Simon Glass8e170782015-01-19 20:21:34 -070010#include <malloc.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080011#include <asm/getopt.h>
Simon Glassc395fdf2014-07-10 22:23:27 -060012#include <asm/io.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080013#include <asm/sections.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080014#include <asm/state.h>
Simon Glass5ba8ef62011-10-03 19:26:45 +000015
Simon Glass2fd34e62013-11-10 10:26:59 -070016DECLARE_GLOBAL_DATA_PTR;
17
Simon Glass8a3e0352012-02-15 15:51:16 -080018int sandbox_early_getopt_check(void)
19{
20 struct sandbox_state *state = state_get_current();
Simon Glass64367c82013-12-03 16:43:23 -070021 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass8a3e0352012-02-15 15:51:16 -080022 size_t num_options = __u_boot_sandbox_option_count();
23 size_t i;
24 int max_arg_len, max_noarg_len;
25
26 /* parse_err will be a string of the faulting option */
27 if (!state->parse_err)
28 return 0;
29
30 if (strcmp(state->parse_err, "help")) {
31 printf("u-boot: error: failed while parsing option: %s\n"
32 "\ttry running with --help for more information.\n",
33 state->parse_err);
34 os_exit(1);
35 }
36
37 printf(
38 "u-boot, a command line test interface to U-Boot\n\n"
39 "Usage: u-boot [options]\n"
40 "Options:\n");
41
42 max_arg_len = 0;
43 for (i = 0; i < num_options; ++i)
Masahiro Yamadadb204642014-11-07 03:03:31 +090044 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
Simon Glass8a3e0352012-02-15 15:51:16 -080045 max_noarg_len = max_arg_len + 7;
46
47 for (i = 0; i < num_options; ++i) {
Simon Glass64367c82013-12-03 16:43:23 -070048 struct sandbox_cmdline_option *opt = sb_opt[i];
Simon Glass8a3e0352012-02-15 15:51:16 -080049
50 /* first output the short flag if it has one */
51 if (opt->flag_short >= 0x100)
52 printf(" ");
53 else
54 printf(" -%c, ", opt->flag_short);
55
56 /* then the long flag */
57 if (opt->has_arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080058 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass5f679f02013-11-10 10:26:58 -070059 else
60 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass8a3e0352012-02-15 15:51:16 -080061
62 /* finally the help text */
63 printf(" %s\n", opt->help);
64 }
65
66 os_exit(0);
67}
68
Simon Glassb419dbb2017-03-28 10:27:28 -060069int misc_init_f(void)
70{
71 return sandbox_early_getopt_check();
72}
73
Simon Glass64367c82013-12-03 16:43:23 -070074static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080075{
76 /* just flag to sandbox_early_getopt_check to show usage */
77 return 1;
78}
Simon Glass64367c82013-12-03 16:43:23 -070079SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass8a3e0352012-02-15 15:51:16 -080080
Simon Glass5cf16632016-07-04 11:57:50 -060081#ifndef CONFIG_SPL_BUILD
Simon Glass0fc3c222012-02-26 17:38:50 -050082int sandbox_main_loop_init(void)
83{
Simon Glass8a3e0352012-02-15 15:51:16 -080084 struct sandbox_state *state = state_get_current();
85
86 /* Execute command if required */
Sjoerd Simons335f4702015-04-30 22:16:09 +020087 if (state->cmd || state->run_distro_boot) {
88 int retval = 0;
Joe Hershberger744d7882015-02-06 15:37:31 -060089
Rabin Vincent7e0194a2014-10-29 23:21:38 +010090 cli_init();
91
Simon Glasse0f12802016-03-13 19:07:30 -060092#ifdef CONFIG_CMDLINE
Sjoerd Simons335f4702015-04-30 22:16:09 +020093 if (state->cmd)
94 retval = run_command_list(state->cmd, -1, 0);
95
96 if (state->run_distro_boot)
97 retval = cli_simple_run_command("run distro_bootcmd",
98 0);
Simon Glasse0f12802016-03-13 19:07:30 -060099#endif
Simon Glassf498e432013-11-10 10:27:02 -0700100 if (!state->interactive)
Joe Hershberger744d7882015-02-06 15:37:31 -0600101 os_exit(retval);
Simon Glass8a3e0352012-02-15 15:51:16 -0800102 }
103
Sjoerd Simons335f4702015-04-30 22:16:09 +0200104 return 0;
105}
Simon Glass5cf16632016-07-04 11:57:50 -0600106#endif
Sjoerd Simons335f4702015-04-30 22:16:09 +0200107
108static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
109 const char *arg)
110{
111 state->run_distro_boot = true;
Simon Glass0fc3c222012-02-26 17:38:50 -0500112 return 0;
113}
Sjoerd Simons335f4702015-04-30 22:16:09 +0200114SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
Simon Glass0fc3c222012-02-26 17:38:50 -0500115
Simon Glass64367c82013-12-03 16:43:23 -0700116static int sandbox_cmdline_cb_command(struct sandbox_state *state,
117 const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800118{
119 state->cmd = arg;
120 return 0;
121}
Simon Glass64367c82013-12-03 16:43:23 -0700122SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass8a3e0352012-02-15 15:51:16 -0800123
Simon Glass64367c82013-12-03 16:43:23 -0700124static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glass15393432013-04-20 08:42:41 +0000125{
126 state->fdt_fname = arg;
127 return 0;
128}
Simon Glass64367c82013-12-03 16:43:23 -0700129SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glass15393432013-04-20 08:42:41 +0000130
Simon Glass8e170782015-01-19 20:21:34 -0700131static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
132 const char *arg)
133{
134 const char *fmt = "%s.dtb";
135 char *fname;
136 int len;
137
138 len = strlen(state->argv[0]) + strlen(fmt) + 1;
139 fname = os_malloc(len);
140 if (!fname)
141 return -ENOMEM;
142 snprintf(fname, len, fmt, state->argv[0]);
143 state->fdt_fname = fname;
144
145 return 0;
146}
147SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
148 "Use the default u-boot.dtb control FDT in U-Boot directory");
149
Simon Glassf498e432013-11-10 10:27:02 -0700150static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
151 const char *arg)
152{
153 state->interactive = true;
154 return 0;
155}
156
157SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
158
Simon Glasse9906532014-02-27 13:26:16 -0700159static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
160 const char *arg)
161{
Simon Glass47acfc62014-02-27 13:26:23 -0700162 /* Remember to delete this U-Boot image later */
163 state->jumped_fname = arg;
Simon Glasse9906532014-02-27 13:26:16 -0700164
165 return 0;
166}
167SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
168
Simon Glass9dd10bf2013-11-10 10:27:03 -0700169static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
170 const char *arg)
171{
172 int err;
173
174 /* For now assume we always want to write it */
175 state->write_ram_buf = true;
176 state->ram_buf_fname = arg;
177
Simon Glassc043e032014-11-11 12:47:08 -0700178 err = os_read_ram_buf(arg);
179 if (err) {
Simon Glass332894d2018-10-01 11:55:12 -0600180 printf("Failed to read RAM buffer '%s': %d\n", arg, err);
Simon Glass9dd10bf2013-11-10 10:27:03 -0700181 return err;
182 }
Simon Glass24a284a2018-11-23 21:29:29 -0700183 state->ram_buf_read = true;
Simon Glass9dd10bf2013-11-10 10:27:03 -0700184
185 return 0;
186}
187SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
188 "Read/write ram_buf memory contents from file");
189
Simon Glass47acfc62014-02-27 13:26:23 -0700190static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
191 const char *arg)
192{
193 state->ram_buf_rm = true;
194
195 return 0;
196}
197SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
198
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700199static int sandbox_cmdline_cb_state(struct sandbox_state *state,
200 const char *arg)
201{
202 state->state_fname = arg;
203 return 0;
204}
205SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
206
207static int sandbox_cmdline_cb_read(struct sandbox_state *state,
208 const char *arg)
209{
210 state->read_state = true;
211 return 0;
212}
213SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
214
215static int sandbox_cmdline_cb_write(struct sandbox_state *state,
216 const char *arg)
217{
218 state->write_state = true;
219 return 0;
220}
221SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
222
223static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
224 const char *arg)
225{
226 state->ignore_missing_state_on_read = true;
227 return 0;
228}
229SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
230 "Ignore missing state on read");
231
Simon Glassb9ddbf42014-02-27 13:26:19 -0700232static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
233 const char *arg)
234{
235 state->show_lcd = true;
236 return 0;
237}
238SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
239 "Show the sandbox LCD display");
240
Simon Glass678ef472014-02-27 13:26:22 -0700241static const char *term_args[STATE_TERM_COUNT] = {
242 "raw-with-sigs",
243 "raw",
244 "cooked",
245};
246
247static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
248 const char *arg)
249{
250 int i;
251
252 for (i = 0; i < STATE_TERM_COUNT; i++) {
253 if (!strcmp(arg, term_args[i])) {
254 state->term_raw = i;
255 return 0;
256 }
257 }
258
259 printf("Unknown terminal setting '%s' (", arg);
260 for (i = 0; i < STATE_TERM_COUNT; i++)
261 printf("%s%s", i ? ", " : "", term_args[i]);
262 puts(")\n");
263
264 return 1;
265}
266SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
267 "Set terminal to raw/cooked mode");
268
Simon Glassfe6d12a2015-11-08 23:47:50 -0700269static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
270 const char *arg)
271{
272 state->show_test_output = true;
273 return 0;
274}
275SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
276
Simon Glass4e9a64d2018-10-01 11:55:11 -0600277static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
278 const char *arg)
279{
280 state->default_log_level = simple_strtol(arg, NULL, 10);
281
282 return 0;
283}
284SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
285 "Set log level (0=panic, 7=debug)");
286
Simon Glassb5dfea82018-11-15 18:44:01 -0700287static int sandbox_cmdline_cb_show_of_platdata(struct sandbox_state *state,
288 const char *arg)
289{
290 state->show_of_platdata = true;
291
292 return 0;
293}
294SANDBOX_CMDLINE_OPT(show_of_platdata, 0, "Show of-platdata in SPL");
295
Simon Glasse0f12802016-03-13 19:07:30 -0600296int board_run_command(const char *cmdline)
297{
298 printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
299
300 return 1;
301}
302
Simon Glassb94eed52017-03-28 10:27:16 -0600303static void setup_ram_buf(struct sandbox_state *state)
304{
Simon Glass24a284a2018-11-23 21:29:29 -0700305 /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
Simon Glasscd3705e2019-04-08 13:20:43 -0600306 if (!state->ram_buf_read)
Simon Glass24a284a2018-11-23 21:29:29 -0700307 memset(state->ram_buf, '\0', state->ram_size);
Simon Glass24a284a2018-11-23 21:29:29 -0700308
Simon Glassb94eed52017-03-28 10:27:16 -0600309 gd->arch.ram_buf = state->ram_buf;
310 gd->ram_size = state->ram_size;
311}
312
Simon Glass46508c92018-11-15 18:44:03 -0700313void state_show(struct sandbox_state *state)
314{
315 char **p;
316
317 printf("Arguments:\n");
318 for (p = state->argv; *p; p++)
319 printf("%s ", *p);
320 printf("\n");
321}
322
Simon Glass5ba8ef62011-10-03 19:26:45 +0000323int main(int argc, char *argv[])
324{
Simon Glass8a3e0352012-02-15 15:51:16 -0800325 struct sandbox_state *state;
Simon Glassc395fdf2014-07-10 22:23:27 -0600326 gd_t data;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700327 int ret;
Simon Glass20bf89a2012-02-15 15:51:15 -0800328
Simon Glass752707a2019-04-08 13:20:41 -0600329 memset(&data, '\0', sizeof(data));
330 gd = &data;
331 gd->arch.text_base = os_find_text_base();
332
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700333 ret = state_init();
334 if (ret)
335 goto err;
Simon Glass20bf89a2012-02-15 15:51:15 -0800336
Simon Glass8a3e0352012-02-15 15:51:16 -0800337 state = state_get_current();
338 if (os_parse_args(state, argc, argv))
339 return 1;
340
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700341 ret = sandbox_read_state(state, state->state_fname);
342 if (ret)
343 goto err;
344
Andy Yan25428c42017-07-24 17:49:59 +0800345#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glass0cc6f5c2014-07-10 22:23:31 -0600346 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
347#endif
Simon Glass4e9a64d2018-10-01 11:55:11 -0600348#if CONFIG_IS_ENABLED(LOG)
349 gd->default_log_level = state->default_log_level;
350#endif
Simon Glassb94eed52017-03-28 10:27:16 -0600351 setup_ram_buf(state);
Simon Glassc395fdf2014-07-10 22:23:27 -0600352
Simon Glass752707a2019-04-08 13:20:41 -0600353 /*
354 * Set up the relocation offset here, since sandbox symbols are always
355 * relocated by the OS before sandbox is entered.
356 */
357 gd->reloc_off = (ulong)gd->arch.text_base;
358
Simon Glass2fd34e62013-11-10 10:26:59 -0700359 /* Do pre- and post-relocation init */
Simon Glass5ba8ef62011-10-03 19:26:45 +0000360 board_init_f(0);
Allen Martin074b4552013-01-22 13:11:21 +0000361
Simon Glass2fd34e62013-11-10 10:26:59 -0700362 board_init_r(gd->new_gd, 0);
363
364 /* NOTREACHED - board_init_r() does not return */
Allen Martin074b4552013-01-22 13:11:21 +0000365 return 0;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700366
367err:
368 printf("Error %d\n", ret);
369 return 1;
Simon Glass5ba8ef62011-10-03 19:26:45 +0000370}