blob: b23d08b5a1bb33ace30f67029b3336f0c6ed8564 [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 Glass9dd10bf2013-11-10 10:27:03 -07007#include <os.h>
Rabin Vincent7e0194a2014-10-29 23:21:38 +01008#include <cli.h>
Simon Glass8e170782015-01-19 20:21:34 -07009#include <malloc.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080010#include <asm/getopt.h>
Simon Glassc395fdf2014-07-10 22:23:27 -060011#include <asm/io.h>
Simon Glass8a3e0352012-02-15 15:51:16 -080012#include <asm/sections.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080013#include <asm/state.h>
Simon Glass5ba8ef62011-10-03 19:26:45 +000014
Simon Glass2fd34e62013-11-10 10:26:59 -070015DECLARE_GLOBAL_DATA_PTR;
16
Simon Glass8a3e0352012-02-15 15:51:16 -080017int sandbox_early_getopt_check(void)
18{
19 struct sandbox_state *state = state_get_current();
Simon Glass64367c82013-12-03 16:43:23 -070020 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass8a3e0352012-02-15 15:51:16 -080021 size_t num_options = __u_boot_sandbox_option_count();
22 size_t i;
23 int max_arg_len, max_noarg_len;
24
25 /* parse_err will be a string of the faulting option */
26 if (!state->parse_err)
27 return 0;
28
29 if (strcmp(state->parse_err, "help")) {
30 printf("u-boot: error: failed while parsing option: %s\n"
31 "\ttry running with --help for more information.\n",
32 state->parse_err);
33 os_exit(1);
34 }
35
36 printf(
37 "u-boot, a command line test interface to U-Boot\n\n"
38 "Usage: u-boot [options]\n"
39 "Options:\n");
40
41 max_arg_len = 0;
42 for (i = 0; i < num_options; ++i)
Masahiro Yamadadb204642014-11-07 03:03:31 +090043 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
Simon Glass8a3e0352012-02-15 15:51:16 -080044 max_noarg_len = max_arg_len + 7;
45
46 for (i = 0; i < num_options; ++i) {
Simon Glass64367c82013-12-03 16:43:23 -070047 struct sandbox_cmdline_option *opt = sb_opt[i];
Simon Glass8a3e0352012-02-15 15:51:16 -080048
49 /* first output the short flag if it has one */
50 if (opt->flag_short >= 0x100)
51 printf(" ");
52 else
53 printf(" -%c, ", opt->flag_short);
54
55 /* then the long flag */
56 if (opt->has_arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080057 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass5f679f02013-11-10 10:26:58 -070058 else
59 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass8a3e0352012-02-15 15:51:16 -080060
61 /* finally the help text */
62 printf(" %s\n", opt->help);
63 }
64
65 os_exit(0);
66}
67
Simon Glass64367c82013-12-03 16:43:23 -070068static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080069{
70 /* just flag to sandbox_early_getopt_check to show usage */
71 return 1;
72}
Simon Glass64367c82013-12-03 16:43:23 -070073SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass8a3e0352012-02-15 15:51:16 -080074
Simon Glass0fc3c222012-02-26 17:38:50 -050075int sandbox_main_loop_init(void)
76{
Simon Glass8a3e0352012-02-15 15:51:16 -080077 struct sandbox_state *state = state_get_current();
78
79 /* Execute command if required */
Sjoerd Simons335f4702015-04-30 22:16:09 +020080 if (state->cmd || state->run_distro_boot) {
81 int retval = 0;
Joe Hershberger744d7882015-02-06 15:37:31 -060082
Rabin Vincent7e0194a2014-10-29 23:21:38 +010083 cli_init();
84
Sjoerd Simons335f4702015-04-30 22:16:09 +020085 if (state->cmd)
86 retval = run_command_list(state->cmd, -1, 0);
87
88 if (state->run_distro_boot)
89 retval = cli_simple_run_command("run distro_bootcmd",
90 0);
91
Simon Glassf498e432013-11-10 10:27:02 -070092 if (!state->interactive)
Joe Hershberger744d7882015-02-06 15:37:31 -060093 os_exit(retval);
Simon Glass8a3e0352012-02-15 15:51:16 -080094 }
95
Sjoerd Simons335f4702015-04-30 22:16:09 +020096 return 0;
97}
98
99static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
100 const char *arg)
101{
102 state->run_distro_boot = true;
Simon Glass0fc3c222012-02-26 17:38:50 -0500103 return 0;
104}
Sjoerd Simons335f4702015-04-30 22:16:09 +0200105SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
Simon Glass0fc3c222012-02-26 17:38:50 -0500106
Simon Glass64367c82013-12-03 16:43:23 -0700107static int sandbox_cmdline_cb_command(struct sandbox_state *state,
108 const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -0800109{
110 state->cmd = arg;
111 return 0;
112}
Simon Glass64367c82013-12-03 16:43:23 -0700113SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass8a3e0352012-02-15 15:51:16 -0800114
Simon Glass64367c82013-12-03 16:43:23 -0700115static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glass15393432013-04-20 08:42:41 +0000116{
117 state->fdt_fname = arg;
118 return 0;
119}
Simon Glass64367c82013-12-03 16:43:23 -0700120SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glass15393432013-04-20 08:42:41 +0000121
Simon Glass8e170782015-01-19 20:21:34 -0700122static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
123 const char *arg)
124{
125 const char *fmt = "%s.dtb";
126 char *fname;
127 int len;
128
129 len = strlen(state->argv[0]) + strlen(fmt) + 1;
130 fname = os_malloc(len);
131 if (!fname)
132 return -ENOMEM;
133 snprintf(fname, len, fmt, state->argv[0]);
134 state->fdt_fname = fname;
135
136 return 0;
137}
138SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
139 "Use the default u-boot.dtb control FDT in U-Boot directory");
140
Simon Glassf498e432013-11-10 10:27:02 -0700141static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
142 const char *arg)
143{
144 state->interactive = true;
145 return 0;
146}
147
148SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
149
Simon Glasse9906532014-02-27 13:26:16 -0700150static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
151 const char *arg)
152{
Simon Glass47acfc62014-02-27 13:26:23 -0700153 /* Remember to delete this U-Boot image later */
154 state->jumped_fname = arg;
Simon Glasse9906532014-02-27 13:26:16 -0700155
156 return 0;
157}
158SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
159
Simon Glass9dd10bf2013-11-10 10:27:03 -0700160static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
161 const char *arg)
162{
163 int err;
164
165 /* For now assume we always want to write it */
166 state->write_ram_buf = true;
167 state->ram_buf_fname = arg;
168
Simon Glassc043e032014-11-11 12:47:08 -0700169 err = os_read_ram_buf(arg);
170 if (err) {
Simon Glass9dd10bf2013-11-10 10:27:03 -0700171 printf("Failed to read RAM buffer\n");
172 return err;
173 }
174
175 return 0;
176}
177SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
178 "Read/write ram_buf memory contents from file");
179
Simon Glass47acfc62014-02-27 13:26:23 -0700180static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
181 const char *arg)
182{
183 state->ram_buf_rm = true;
184
185 return 0;
186}
187SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
188
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700189static int sandbox_cmdline_cb_state(struct sandbox_state *state,
190 const char *arg)
191{
192 state->state_fname = arg;
193 return 0;
194}
195SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
196
197static int sandbox_cmdline_cb_read(struct sandbox_state *state,
198 const char *arg)
199{
200 state->read_state = true;
201 return 0;
202}
203SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
204
205static int sandbox_cmdline_cb_write(struct sandbox_state *state,
206 const char *arg)
207{
208 state->write_state = true;
209 return 0;
210}
211SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
212
213static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
214 const char *arg)
215{
216 state->ignore_missing_state_on_read = true;
217 return 0;
218}
219SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
220 "Ignore missing state on read");
221
Simon Glassb9ddbf42014-02-27 13:26:19 -0700222static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
223 const char *arg)
224{
225 state->show_lcd = true;
226 return 0;
227}
228SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
229 "Show the sandbox LCD display");
230
Simon Glass678ef472014-02-27 13:26:22 -0700231static const char *term_args[STATE_TERM_COUNT] = {
232 "raw-with-sigs",
233 "raw",
234 "cooked",
235};
236
237static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
238 const char *arg)
239{
240 int i;
241
242 for (i = 0; i < STATE_TERM_COUNT; i++) {
243 if (!strcmp(arg, term_args[i])) {
244 state->term_raw = i;
245 return 0;
246 }
247 }
248
249 printf("Unknown terminal setting '%s' (", arg);
250 for (i = 0; i < STATE_TERM_COUNT; i++)
251 printf("%s%s", i ? ", " : "", term_args[i]);
252 puts(")\n");
253
254 return 1;
255}
256SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
257 "Set terminal to raw/cooked mode");
258
Simon Glass5ba8ef62011-10-03 19:26:45 +0000259int main(int argc, char *argv[])
260{
Simon Glass8a3e0352012-02-15 15:51:16 -0800261 struct sandbox_state *state;
Simon Glassc395fdf2014-07-10 22:23:27 -0600262 gd_t data;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700263 int ret;
Simon Glass20bf89a2012-02-15 15:51:15 -0800264
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700265 ret = state_init();
266 if (ret)
267 goto err;
Simon Glass20bf89a2012-02-15 15:51:15 -0800268
Simon Glass8a3e0352012-02-15 15:51:16 -0800269 state = state_get_current();
270 if (os_parse_args(state, argc, argv))
271 return 1;
272
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700273 ret = sandbox_read_state(state, state->state_fname);
274 if (ret)
275 goto err;
276
Simon Glass47acfc62014-02-27 13:26:23 -0700277 /* Remove old memory file if required */
278 if (state->ram_buf_rm && state->ram_buf_fname)
279 os_unlink(state->ram_buf_fname);
280
Simon Glassc395fdf2014-07-10 22:23:27 -0600281 memset(&data, '\0', sizeof(data));
282 gd = &data;
Simon Glass0cc6f5c2014-07-10 22:23:31 -0600283#ifdef CONFIG_SYS_MALLOC_F_LEN
284 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
285#endif
Simon Glassc395fdf2014-07-10 22:23:27 -0600286
Simon Glass2fd34e62013-11-10 10:26:59 -0700287 /* Do pre- and post-relocation init */
Simon Glass5ba8ef62011-10-03 19:26:45 +0000288 board_init_f(0);
Allen Martin074b4552013-01-22 13:11:21 +0000289
Simon Glass2fd34e62013-11-10 10:26:59 -0700290 board_init_r(gd->new_gd, 0);
291
292 /* NOTREACHED - board_init_r() does not return */
Allen Martin074b4552013-01-22 13:11:21 +0000293 return 0;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700294
295err:
296 printf("Error %d\n", ret);
297 return 1;
Simon Glass5ba8ef62011-10-03 19:26:45 +0000298}