blob: b1566a8143571ad63776cbb79b15a15666b3af80 [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 Glass38068822015-05-04 11:31:08 -06007#include <errno.h>
Simon Glass5c2859c2013-11-10 10:27:03 -07008#include <os.h>
Rabin Vincent7dbcb762014-10-29 23:21:38 +01009#include <cli.h>
Simon Glass1f32ae92015-01-19 20:21:34 -070010#include <malloc.h>
Simon Glass70db4212012-02-15 15:51:16 -080011#include <asm/getopt.h>
Simon Glass4d94dfa2014-07-10 22:23:27 -060012#include <asm/io.h>
Simon Glass70db4212012-02-15 15:51:16 -080013#include <asm/sections.h>
Simon Glass6fb62072012-02-15 15:51:15 -080014#include <asm/state.h>
Simon Glassbace3d02011-10-03 19:26:45 +000015
Simon Glass808434c2013-11-10 10:26:59 -070016DECLARE_GLOBAL_DATA_PTR;
17
Simon Glass70db4212012-02-15 15:51:16 -080018int sandbox_early_getopt_check(void)
19{
20 struct sandbox_state *state = state_get_current();
Simon Glass7b3efc62013-12-03 16:43:23 -070021 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass70db4212012-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 Yamadab4141192014-11-07 03:03:31 +090044 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
Simon Glass70db4212012-02-15 15:51:16 -080045 max_noarg_len = max_arg_len + 7;
46
47 for (i = 0; i < num_options; ++i) {
Simon Glass7b3efc62013-12-03 16:43:23 -070048 struct sandbox_cmdline_option *opt = sb_opt[i];
Simon Glass70db4212012-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 Glass70db4212012-02-15 15:51:16 -080058 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass6ebcab82013-11-10 10:26:58 -070059 else
60 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass70db4212012-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 Glass68969772017-03-28 10:27:28 -060069int misc_init_f(void)
70{
71 return sandbox_early_getopt_check();
72}
73
Simon Glass7b3efc62013-12-03 16:43:23 -070074static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass70db4212012-02-15 15:51:16 -080075{
76 /* just flag to sandbox_early_getopt_check to show usage */
77 return 1;
78}
Simon Glass7b3efc62013-12-03 16:43:23 -070079SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass70db4212012-02-15 15:51:16 -080080
Simon Glassd0d07462016-07-04 11:57:50 -060081#ifndef CONFIG_SPL_BUILD
Simon Glassab4e07e2012-02-26 17:38:50 -050082int sandbox_main_loop_init(void)
83{
Simon Glass70db4212012-02-15 15:51:16 -080084 struct sandbox_state *state = state_get_current();
85
86 /* Execute command if required */
Sjoerd Simonsebaa8322015-04-30 22:16:09 +020087 if (state->cmd || state->run_distro_boot) {
88 int retval = 0;
Joe Hershberger88539e42015-02-06 15:37:31 -060089
Rabin Vincent7dbcb762014-10-29 23:21:38 +010090 cli_init();
91
Simon Glass2b6793d2016-03-13 19:07:30 -060092#ifdef CONFIG_CMDLINE
Sjoerd Simonsebaa8322015-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 Glass2b6793d2016-03-13 19:07:30 -060099#endif
Simon Glassc5a62d42013-11-10 10:27:02 -0700100 if (!state->interactive)
Joe Hershberger88539e42015-02-06 15:37:31 -0600101 os_exit(retval);
Simon Glass70db4212012-02-15 15:51:16 -0800102 }
103
Simon Glassab4e07e2012-02-26 17:38:50 -0500104 return 0;
105}
Simon Glassd0d07462016-07-04 11:57:50 -0600106#endif
Simon Glassab4e07e2012-02-26 17:38:50 -0500107
Sjoerd Simonsebaa8322015-04-30 22:16:09 +0200108static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
109 const char *arg)
110{
111 state->run_distro_boot = true;
112 return 0;
113}
114SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
115
Simon Glass7b3efc62013-12-03 16:43:23 -0700116static int sandbox_cmdline_cb_command(struct sandbox_state *state,
117 const char *arg)
Simon Glass70db4212012-02-15 15:51:16 -0800118{
119 state->cmd = arg;
120 return 0;
121}
Simon Glass7b3efc62013-12-03 16:43:23 -0700122SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass70db4212012-02-15 15:51:16 -0800123
Simon Glass7b3efc62013-12-03 16:43:23 -0700124static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glassf828bf22013-04-20 08:42:41 +0000125{
126 state->fdt_fname = arg;
127 return 0;
128}
Simon Glass7b3efc62013-12-03 16:43:23 -0700129SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glassf828bf22013-04-20 08:42:41 +0000130
Simon Glass1f32ae92015-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 Glassc5a62d42013-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 Glassbda77732014-02-27 13:26:16 -0700159static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
160 const char *arg)
161{
Simon Glassab839dc2014-02-27 13:26:23 -0700162 /* Remember to delete this U-Boot image later */
163 state->jumped_fname = arg;
Simon Glassbda77732014-02-27 13:26:16 -0700164
165 return 0;
166}
167SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
168
Simon Glass5c2859c2013-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 Glassf80a8bb2014-11-11 12:47:08 -0700178 err = os_read_ram_buf(arg);
179 if (err) {
Simon Glass1c5a81d2018-10-01 11:55:12 -0600180 printf("Failed to read RAM buffer '%s': %d\n", arg, err);
Simon Glass5c2859c2013-11-10 10:27:03 -0700181 return err;
182 }
183
184 return 0;
185}
186SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
187 "Read/write ram_buf memory contents from file");
188
Simon Glassab839dc2014-02-27 13:26:23 -0700189static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
190 const char *arg)
191{
192 state->ram_buf_rm = true;
193
194 return 0;
195}
196SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
197
Simon Glass1209e272013-11-10 10:27:04 -0700198static int sandbox_cmdline_cb_state(struct sandbox_state *state,
199 const char *arg)
200{
201 state->state_fname = arg;
202 return 0;
203}
204SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
205
206static int sandbox_cmdline_cb_read(struct sandbox_state *state,
207 const char *arg)
208{
209 state->read_state = true;
210 return 0;
211}
212SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
213
214static int sandbox_cmdline_cb_write(struct sandbox_state *state,
215 const char *arg)
216{
217 state->write_state = true;
218 return 0;
219}
220SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
221
222static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
223 const char *arg)
224{
225 state->ignore_missing_state_on_read = true;
226 return 0;
227}
228SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
229 "Ignore missing state on read");
230
Simon Glass7d95f2a2014-02-27 13:26:19 -0700231static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
232 const char *arg)
233{
234 state->show_lcd = true;
235 return 0;
236}
237SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
238 "Show the sandbox LCD display");
239
Simon Glassffb87902014-02-27 13:26:22 -0700240static const char *term_args[STATE_TERM_COUNT] = {
241 "raw-with-sigs",
242 "raw",
243 "cooked",
244};
245
246static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
247 const char *arg)
248{
249 int i;
250
251 for (i = 0; i < STATE_TERM_COUNT; i++) {
252 if (!strcmp(arg, term_args[i])) {
253 state->term_raw = i;
254 return 0;
255 }
256 }
257
258 printf("Unknown terminal setting '%s' (", arg);
259 for (i = 0; i < STATE_TERM_COUNT; i++)
260 printf("%s%s", i ? ", " : "", term_args[i]);
261 puts(")\n");
262
263 return 1;
264}
265SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
266 "Set terminal to raw/cooked mode");
267
Simon Glass9ce8b402015-11-08 23:47:50 -0700268static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
269 const char *arg)
270{
271 state->show_test_output = true;
272 return 0;
273}
274SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
275
Simon Glass2b1dc292018-10-01 11:55:11 -0600276static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
277 const char *arg)
278{
279 state->default_log_level = simple_strtol(arg, NULL, 10);
280
281 return 0;
282}
283SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
284 "Set log level (0=panic, 7=debug)");
285
Simon Glass1ca910b2018-11-15 18:44:01 -0700286static int sandbox_cmdline_cb_show_of_platdata(struct sandbox_state *state,
287 const char *arg)
288{
289 state->show_of_platdata = true;
290
291 return 0;
292}
293SANDBOX_CMDLINE_OPT(show_of_platdata, 0, "Show of-platdata in SPL");
294
Simon Glass2b6793d2016-03-13 19:07:30 -0600295int board_run_command(const char *cmdline)
296{
297 printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
298
299 return 1;
300}
301
Simon Glassbb967242017-03-28 10:27:16 -0600302static void setup_ram_buf(struct sandbox_state *state)
303{
304 gd->arch.ram_buf = state->ram_buf;
305 gd->ram_size = state->ram_size;
306}
307
Simon Glassd66ddaf2018-11-15 18:44:03 -0700308void state_show(struct sandbox_state *state)
309{
310 char **p;
311
312 printf("Arguments:\n");
313 for (p = state->argv; *p; p++)
314 printf("%s ", *p);
315 printf("\n");
316}
317
Simon Glassbace3d02011-10-03 19:26:45 +0000318int main(int argc, char *argv[])
319{
Simon Glass70db4212012-02-15 15:51:16 -0800320 struct sandbox_state *state;
Simon Glass4d94dfa2014-07-10 22:23:27 -0600321 gd_t data;
Simon Glass1209e272013-11-10 10:27:04 -0700322 int ret;
Simon Glass6fb62072012-02-15 15:51:15 -0800323
Simon Glass1209e272013-11-10 10:27:04 -0700324 ret = state_init();
325 if (ret)
326 goto err;
Simon Glass6fb62072012-02-15 15:51:15 -0800327
Simon Glass70db4212012-02-15 15:51:16 -0800328 state = state_get_current();
329 if (os_parse_args(state, argc, argv))
330 return 1;
331
Simon Glass1209e272013-11-10 10:27:04 -0700332 ret = sandbox_read_state(state, state->state_fname);
333 if (ret)
334 goto err;
335
Simon Glass4d94dfa2014-07-10 22:23:27 -0600336 memset(&data, '\0', sizeof(data));
337 gd = &data;
Andy Yan1fc50d72017-07-24 17:49:59 +0800338#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glass29afe9e2014-07-10 22:23:31 -0600339 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
340#endif
Simon Glass2b1dc292018-10-01 11:55:11 -0600341#if CONFIG_IS_ENABLED(LOG)
342 gd->default_log_level = state->default_log_level;
343#endif
Simon Glassbb967242017-03-28 10:27:16 -0600344 setup_ram_buf(state);
Simon Glass4d94dfa2014-07-10 22:23:27 -0600345
Simon Glass808434c2013-11-10 10:26:59 -0700346 /* Do pre- and post-relocation init */
Simon Glassbace3d02011-10-03 19:26:45 +0000347 board_init_f(0);
Allen Martin8ec21bb2013-01-22 13:11:21 +0000348
Simon Glass808434c2013-11-10 10:26:59 -0700349 board_init_r(gd->new_gd, 0);
350
351 /* NOTREACHED - board_init_r() does not return */
Allen Martin8ec21bb2013-01-22 13:11:21 +0000352 return 0;
Simon Glass1209e272013-11-10 10:27:04 -0700353
354err:
355 printf("Error %d\n", ret);
356 return 1;
Simon Glassbace3d02011-10-03 19:26:45 +0000357}