blob: ec010402d77f47f40b2ca3950d4cb9a5e52355c4 [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 */
80 if (state->cmd) {
Joe Hershberger744d7882015-02-06 15:37:31 -060081 int retval;
82
Rabin Vincent7e0194a2014-10-29 23:21:38 +010083 cli_init();
84
Joe Hershberger744d7882015-02-06 15:37:31 -060085 retval = run_command_list(state->cmd, -1, 0);
Simon Glassf498e432013-11-10 10:27:02 -070086 if (!state->interactive)
Joe Hershberger744d7882015-02-06 15:37:31 -060087 os_exit(retval);
Simon Glass8a3e0352012-02-15 15:51:16 -080088 }
89
Simon Glass0fc3c222012-02-26 17:38:50 -050090 return 0;
91}
92
Simon Glass64367c82013-12-03 16:43:23 -070093static int sandbox_cmdline_cb_command(struct sandbox_state *state,
94 const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080095{
96 state->cmd = arg;
97 return 0;
98}
Simon Glass64367c82013-12-03 16:43:23 -070099SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass8a3e0352012-02-15 15:51:16 -0800100
Simon Glass64367c82013-12-03 16:43:23 -0700101static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glass15393432013-04-20 08:42:41 +0000102{
103 state->fdt_fname = arg;
104 return 0;
105}
Simon Glass64367c82013-12-03 16:43:23 -0700106SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glass15393432013-04-20 08:42:41 +0000107
Simon Glass8e170782015-01-19 20:21:34 -0700108static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
109 const char *arg)
110{
111 const char *fmt = "%s.dtb";
112 char *fname;
113 int len;
114
115 len = strlen(state->argv[0]) + strlen(fmt) + 1;
116 fname = os_malloc(len);
117 if (!fname)
118 return -ENOMEM;
119 snprintf(fname, len, fmt, state->argv[0]);
120 state->fdt_fname = fname;
121
122 return 0;
123}
124SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
125 "Use the default u-boot.dtb control FDT in U-Boot directory");
126
Simon Glassf498e432013-11-10 10:27:02 -0700127static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
128 const char *arg)
129{
130 state->interactive = true;
131 return 0;
132}
133
134SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
135
Simon Glasse9906532014-02-27 13:26:16 -0700136static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
137 const char *arg)
138{
Simon Glass47acfc62014-02-27 13:26:23 -0700139 /* Remember to delete this U-Boot image later */
140 state->jumped_fname = arg;
Simon Glasse9906532014-02-27 13:26:16 -0700141
142 return 0;
143}
144SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
145
Simon Glass9dd10bf2013-11-10 10:27:03 -0700146static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
147 const char *arg)
148{
149 int err;
150
151 /* For now assume we always want to write it */
152 state->write_ram_buf = true;
153 state->ram_buf_fname = arg;
154
Simon Glassc043e032014-11-11 12:47:08 -0700155 err = os_read_ram_buf(arg);
156 if (err) {
Simon Glass9dd10bf2013-11-10 10:27:03 -0700157 printf("Failed to read RAM buffer\n");
158 return err;
159 }
160
161 return 0;
162}
163SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
164 "Read/write ram_buf memory contents from file");
165
Simon Glass47acfc62014-02-27 13:26:23 -0700166static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
167 const char *arg)
168{
169 state->ram_buf_rm = true;
170
171 return 0;
172}
173SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
174
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700175static int sandbox_cmdline_cb_state(struct sandbox_state *state,
176 const char *arg)
177{
178 state->state_fname = arg;
179 return 0;
180}
181SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
182
183static int sandbox_cmdline_cb_read(struct sandbox_state *state,
184 const char *arg)
185{
186 state->read_state = true;
187 return 0;
188}
189SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
190
191static int sandbox_cmdline_cb_write(struct sandbox_state *state,
192 const char *arg)
193{
194 state->write_state = true;
195 return 0;
196}
197SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
198
199static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
200 const char *arg)
201{
202 state->ignore_missing_state_on_read = true;
203 return 0;
204}
205SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
206 "Ignore missing state on read");
207
Simon Glassb9ddbf42014-02-27 13:26:19 -0700208static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
209 const char *arg)
210{
211 state->show_lcd = true;
212 return 0;
213}
214SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
215 "Show the sandbox LCD display");
216
Simon Glass678ef472014-02-27 13:26:22 -0700217static const char *term_args[STATE_TERM_COUNT] = {
218 "raw-with-sigs",
219 "raw",
220 "cooked",
221};
222
223static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
224 const char *arg)
225{
226 int i;
227
228 for (i = 0; i < STATE_TERM_COUNT; i++) {
229 if (!strcmp(arg, term_args[i])) {
230 state->term_raw = i;
231 return 0;
232 }
233 }
234
235 printf("Unknown terminal setting '%s' (", arg);
236 for (i = 0; i < STATE_TERM_COUNT; i++)
237 printf("%s%s", i ? ", " : "", term_args[i]);
238 puts(")\n");
239
240 return 1;
241}
242SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
243 "Set terminal to raw/cooked mode");
244
Simon Glass5ba8ef62011-10-03 19:26:45 +0000245int main(int argc, char *argv[])
246{
Simon Glass8a3e0352012-02-15 15:51:16 -0800247 struct sandbox_state *state;
Simon Glassc395fdf2014-07-10 22:23:27 -0600248 gd_t data;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700249 int ret;
Simon Glass20bf89a2012-02-15 15:51:15 -0800250
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700251 ret = state_init();
252 if (ret)
253 goto err;
Simon Glass20bf89a2012-02-15 15:51:15 -0800254
Simon Glass8a3e0352012-02-15 15:51:16 -0800255 state = state_get_current();
256 if (os_parse_args(state, argc, argv))
257 return 1;
258
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700259 ret = sandbox_read_state(state, state->state_fname);
260 if (ret)
261 goto err;
262
Simon Glass47acfc62014-02-27 13:26:23 -0700263 /* Remove old memory file if required */
264 if (state->ram_buf_rm && state->ram_buf_fname)
265 os_unlink(state->ram_buf_fname);
266
Simon Glassc395fdf2014-07-10 22:23:27 -0600267 memset(&data, '\0', sizeof(data));
268 gd = &data;
Simon Glass0cc6f5c2014-07-10 22:23:31 -0600269#ifdef CONFIG_SYS_MALLOC_F_LEN
270 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
271#endif
Simon Glassc395fdf2014-07-10 22:23:27 -0600272
Simon Glass2fd34e62013-11-10 10:26:59 -0700273 /* Do pre- and post-relocation init */
Simon Glass5ba8ef62011-10-03 19:26:45 +0000274 board_init_f(0);
Allen Martin074b4552013-01-22 13:11:21 +0000275
Simon Glass2fd34e62013-11-10 10:26:59 -0700276 board_init_r(gd->new_gd, 0);
277
278 /* NOTREACHED - board_init_r() does not return */
Allen Martin074b4552013-01-22 13:11:21 +0000279 return 0;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700280
281err:
282 printf("Error %d\n", ret);
283 return 1;
Simon Glass5ba8ef62011-10-03 19:26:45 +0000284}