blob: 969618ef87502dfb1f34c0f677c18e66c4f6072f [file] [log] [blame]
Simon Glass5ba8ef62011-10-03 19:26:45 +00001/*
Simon Glass20bf89a2012-02-15 15:51:15 -08002 * Copyright (c) 2011-2012 The Chromium OS Authors.
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02003 * SPDX-License-Identifier: GPL-2.0+
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 Glass64367c82013-12-03 16:43:23 -070069static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080070{
71 /* just flag to sandbox_early_getopt_check to show usage */
72 return 1;
73}
Simon Glass64367c82013-12-03 16:43:23 -070074SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass8a3e0352012-02-15 15:51:16 -080075
Simon Glass0fc3c222012-02-26 17:38:50 -050076int sandbox_main_loop_init(void)
77{
Simon Glass8a3e0352012-02-15 15:51:16 -080078 struct sandbox_state *state = state_get_current();
79
80 /* Execute command if required */
Sjoerd Simons335f4702015-04-30 22:16:09 +020081 if (state->cmd || state->run_distro_boot) {
82 int retval = 0;
Joe Hershberger744d7882015-02-06 15:37:31 -060083
Rabin Vincent7e0194a2014-10-29 23:21:38 +010084 cli_init();
85
Simon Glasse0f12802016-03-13 19:07:30 -060086#ifdef CONFIG_CMDLINE
Sjoerd Simons335f4702015-04-30 22:16:09 +020087 if (state->cmd)
88 retval = run_command_list(state->cmd, -1, 0);
89
90 if (state->run_distro_boot)
91 retval = cli_simple_run_command("run distro_bootcmd",
92 0);
Simon Glasse0f12802016-03-13 19:07:30 -060093#endif
Simon Glassf498e432013-11-10 10:27:02 -070094 if (!state->interactive)
Joe Hershberger744d7882015-02-06 15:37:31 -060095 os_exit(retval);
Simon Glass8a3e0352012-02-15 15:51:16 -080096 }
97
Sjoerd Simons335f4702015-04-30 22:16:09 +020098 return 0;
99}
100
101static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
102 const char *arg)
103{
104 state->run_distro_boot = true;
Simon Glass0fc3c222012-02-26 17:38:50 -0500105 return 0;
106}
Sjoerd Simons335f4702015-04-30 22:16:09 +0200107SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
Simon Glass0fc3c222012-02-26 17:38:50 -0500108
Simon Glass64367c82013-12-03 16:43:23 -0700109static int sandbox_cmdline_cb_command(struct sandbox_state *state,
110 const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800111{
112 state->cmd = arg;
113 return 0;
114}
Simon Glass64367c82013-12-03 16:43:23 -0700115SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass8a3e0352012-02-15 15:51:16 -0800116
Simon Glass64367c82013-12-03 16:43:23 -0700117static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glass15393432013-04-20 08:42:41 +0000118{
119 state->fdt_fname = arg;
120 return 0;
121}
Simon Glass64367c82013-12-03 16:43:23 -0700122SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glass15393432013-04-20 08:42:41 +0000123
Simon Glass8e170782015-01-19 20:21:34 -0700124static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
125 const char *arg)
126{
127 const char *fmt = "%s.dtb";
128 char *fname;
129 int len;
130
131 len = strlen(state->argv[0]) + strlen(fmt) + 1;
132 fname = os_malloc(len);
133 if (!fname)
134 return -ENOMEM;
135 snprintf(fname, len, fmt, state->argv[0]);
136 state->fdt_fname = fname;
137
138 return 0;
139}
140SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
141 "Use the default u-boot.dtb control FDT in U-Boot directory");
142
Simon Glassf498e432013-11-10 10:27:02 -0700143static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
144 const char *arg)
145{
146 state->interactive = true;
147 return 0;
148}
149
150SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
151
Simon Glasse9906532014-02-27 13:26:16 -0700152static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
153 const char *arg)
154{
Simon Glass47acfc62014-02-27 13:26:23 -0700155 /* Remember to delete this U-Boot image later */
156 state->jumped_fname = arg;
Simon Glasse9906532014-02-27 13:26:16 -0700157
158 return 0;
159}
160SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
161
Simon Glass9dd10bf2013-11-10 10:27:03 -0700162static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
163 const char *arg)
164{
165 int err;
166
167 /* For now assume we always want to write it */
168 state->write_ram_buf = true;
169 state->ram_buf_fname = arg;
170
Simon Glassc043e032014-11-11 12:47:08 -0700171 err = os_read_ram_buf(arg);
172 if (err) {
Simon Glass9dd10bf2013-11-10 10:27:03 -0700173 printf("Failed to read RAM buffer\n");
174 return err;
175 }
176
177 return 0;
178}
179SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
180 "Read/write ram_buf memory contents from file");
181
Simon Glass47acfc62014-02-27 13:26:23 -0700182static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
183 const char *arg)
184{
185 state->ram_buf_rm = true;
186
187 return 0;
188}
189SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
190
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700191static int sandbox_cmdline_cb_state(struct sandbox_state *state,
192 const char *arg)
193{
194 state->state_fname = arg;
195 return 0;
196}
197SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
198
199static int sandbox_cmdline_cb_read(struct sandbox_state *state,
200 const char *arg)
201{
202 state->read_state = true;
203 return 0;
204}
205SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
206
207static int sandbox_cmdline_cb_write(struct sandbox_state *state,
208 const char *arg)
209{
210 state->write_state = true;
211 return 0;
212}
213SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
214
215static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
216 const char *arg)
217{
218 state->ignore_missing_state_on_read = true;
219 return 0;
220}
221SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
222 "Ignore missing state on read");
223
Simon Glassb9ddbf42014-02-27 13:26:19 -0700224static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
225 const char *arg)
226{
227 state->show_lcd = true;
228 return 0;
229}
230SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
231 "Show the sandbox LCD display");
232
Simon Glass678ef472014-02-27 13:26:22 -0700233static const char *term_args[STATE_TERM_COUNT] = {
234 "raw-with-sigs",
235 "raw",
236 "cooked",
237};
238
239static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
240 const char *arg)
241{
242 int i;
243
244 for (i = 0; i < STATE_TERM_COUNT; i++) {
245 if (!strcmp(arg, term_args[i])) {
246 state->term_raw = i;
247 return 0;
248 }
249 }
250
251 printf("Unknown terminal setting '%s' (", arg);
252 for (i = 0; i < STATE_TERM_COUNT; i++)
253 printf("%s%s", i ? ", " : "", term_args[i]);
254 puts(")\n");
255
256 return 1;
257}
258SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
259 "Set terminal to raw/cooked mode");
260
Simon Glassfe6d12a2015-11-08 23:47:50 -0700261static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
262 const char *arg)
263{
264 state->show_test_output = true;
265 return 0;
266}
267SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
268
Simon Glasse0f12802016-03-13 19:07:30 -0600269int board_run_command(const char *cmdline)
270{
271 printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
272
273 return 1;
274}
275
Simon Glass5ba8ef62011-10-03 19:26:45 +0000276int main(int argc, char *argv[])
277{
Simon Glass8a3e0352012-02-15 15:51:16 -0800278 struct sandbox_state *state;
Simon Glassc395fdf2014-07-10 22:23:27 -0600279 gd_t data;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700280 int ret;
Simon Glass20bf89a2012-02-15 15:51:15 -0800281
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700282 ret = state_init();
283 if (ret)
284 goto err;
Simon Glass20bf89a2012-02-15 15:51:15 -0800285
Simon Glass8a3e0352012-02-15 15:51:16 -0800286 state = state_get_current();
287 if (os_parse_args(state, argc, argv))
288 return 1;
289
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700290 ret = sandbox_read_state(state, state->state_fname);
291 if (ret)
292 goto err;
293
Simon Glass47acfc62014-02-27 13:26:23 -0700294 /* Remove old memory file if required */
295 if (state->ram_buf_rm && state->ram_buf_fname)
296 os_unlink(state->ram_buf_fname);
297
Simon Glassc395fdf2014-07-10 22:23:27 -0600298 memset(&data, '\0', sizeof(data));
299 gd = &data;
Simon Glass0cc6f5c2014-07-10 22:23:31 -0600300#ifdef CONFIG_SYS_MALLOC_F_LEN
301 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
302#endif
Simon Glassc395fdf2014-07-10 22:23:27 -0600303
Simon Glass2fd34e62013-11-10 10:26:59 -0700304 /* Do pre- and post-relocation init */
Simon Glass5ba8ef62011-10-03 19:26:45 +0000305 board_init_f(0);
Allen Martin074b4552013-01-22 13:11:21 +0000306
Simon Glass2fd34e62013-11-10 10:26:59 -0700307 board_init_r(gd->new_gd, 0);
308
309 /* NOTREACHED - board_init_r() does not return */
Allen Martin074b4552013-01-22 13:11:21 +0000310 return 0;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700311
312err:
313 printf("Error %d\n", ret);
314 return 1;
Simon Glass5ba8ef62011-10-03 19:26:45 +0000315}