blob: 42353d80a847bc51eade86f414e72238a8a4916f [file] [log] [blame]
Simon Glassbace3d02011-10-03 19:26:45 +00001/*
Simon Glass6fb62072012-02-15 15:51:15 -08002 * Copyright (c) 2011-2012 The Chromium OS Authors.
Wolfgang Denk1a459662013-07-08 09:37:19 +02003 * SPDX-License-Identifier: GPL-2.0+
Simon Glassbace3d02011-10-03 19:26:45 +00004 */
5
6#include <common.h>
Simon Glass5c2859c2013-11-10 10:27:03 -07007#include <os.h>
Rabin Vincent7dbcb762014-10-29 23:21:38 +01008#include <cli.h>
Simon Glass70db4212012-02-15 15:51:16 -08009#include <asm/getopt.h>
Simon Glass4d94dfa2014-07-10 22:23:27 -060010#include <asm/io.h>
Simon Glass70db4212012-02-15 15:51:16 -080011#include <asm/sections.h>
Simon Glass6fb62072012-02-15 15:51:15 -080012#include <asm/state.h>
Simon Glassbace3d02011-10-03 19:26:45 +000013
Simon Glass808434c2013-11-10 10:26:59 -070014DECLARE_GLOBAL_DATA_PTR;
15
Simon Glass70db4212012-02-15 15:51:16 -080016int sandbox_early_getopt_check(void)
17{
18 struct sandbox_state *state = state_get_current();
Simon Glass7b3efc62013-12-03 16:43:23 -070019 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass70db4212012-02-15 15:51:16 -080020 size_t num_options = __u_boot_sandbox_option_count();
21 size_t i;
22 int max_arg_len, max_noarg_len;
23
24 /* parse_err will be a string of the faulting option */
25 if (!state->parse_err)
26 return 0;
27
28 if (strcmp(state->parse_err, "help")) {
29 printf("u-boot: error: failed while parsing option: %s\n"
30 "\ttry running with --help for more information.\n",
31 state->parse_err);
32 os_exit(1);
33 }
34
35 printf(
36 "u-boot, a command line test interface to U-Boot\n\n"
37 "Usage: u-boot [options]\n"
38 "Options:\n");
39
40 max_arg_len = 0;
41 for (i = 0; i < num_options; ++i)
Masahiro Yamadab4141192014-11-07 03:03:31 +090042 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
Simon Glass70db4212012-02-15 15:51:16 -080043 max_noarg_len = max_arg_len + 7;
44
45 for (i = 0; i < num_options; ++i) {
Simon Glass7b3efc62013-12-03 16:43:23 -070046 struct sandbox_cmdline_option *opt = sb_opt[i];
Simon Glass70db4212012-02-15 15:51:16 -080047
48 /* first output the short flag if it has one */
49 if (opt->flag_short >= 0x100)
50 printf(" ");
51 else
52 printf(" -%c, ", opt->flag_short);
53
54 /* then the long flag */
55 if (opt->has_arg)
Simon Glass70db4212012-02-15 15:51:16 -080056 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass6ebcab82013-11-10 10:26:58 -070057 else
58 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass70db4212012-02-15 15:51:16 -080059
60 /* finally the help text */
61 printf(" %s\n", opt->help);
62 }
63
64 os_exit(0);
65}
66
Simon Glass7b3efc62013-12-03 16:43:23 -070067static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass70db4212012-02-15 15:51:16 -080068{
69 /* just flag to sandbox_early_getopt_check to show usage */
70 return 1;
71}
Simon Glass7b3efc62013-12-03 16:43:23 -070072SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass70db4212012-02-15 15:51:16 -080073
Simon Glassab4e07e2012-02-26 17:38:50 -050074int sandbox_main_loop_init(void)
75{
Simon Glass70db4212012-02-15 15:51:16 -080076 struct sandbox_state *state = state_get_current();
77
78 /* Execute command if required */
79 if (state->cmd) {
Rabin Vincent7dbcb762014-10-29 23:21:38 +010080 cli_init();
81
Simon Glass39042d82013-04-20 08:42:48 +000082 run_command_list(state->cmd, -1, 0);
Simon Glassc5a62d42013-11-10 10:27:02 -070083 if (!state->interactive)
84 os_exit(state->exit_type);
Simon Glass70db4212012-02-15 15:51:16 -080085 }
86
Simon Glassab4e07e2012-02-26 17:38:50 -050087 return 0;
88}
89
Simon Glass7b3efc62013-12-03 16:43:23 -070090static int sandbox_cmdline_cb_command(struct sandbox_state *state,
91 const char *arg)
Simon Glass70db4212012-02-15 15:51:16 -080092{
93 state->cmd = arg;
94 return 0;
95}
Simon Glass7b3efc62013-12-03 16:43:23 -070096SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass70db4212012-02-15 15:51:16 -080097
Simon Glass7b3efc62013-12-03 16:43:23 -070098static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glassf828bf22013-04-20 08:42:41 +000099{
100 state->fdt_fname = arg;
101 return 0;
102}
Simon Glass7b3efc62013-12-03 16:43:23 -0700103SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glassf828bf22013-04-20 08:42:41 +0000104
Simon Glassc5a62d42013-11-10 10:27:02 -0700105static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
106 const char *arg)
107{
108 state->interactive = true;
109 return 0;
110}
111
112SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
113
Simon Glassbda77732014-02-27 13:26:16 -0700114static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
115 const char *arg)
116{
Simon Glassab839dc2014-02-27 13:26:23 -0700117 /* Remember to delete this U-Boot image later */
118 state->jumped_fname = arg;
Simon Glassbda77732014-02-27 13:26:16 -0700119
120 return 0;
121}
122SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
123
Simon Glass5c2859c2013-11-10 10:27:03 -0700124static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
125 const char *arg)
126{
127 int err;
128
129 /* For now assume we always want to write it */
130 state->write_ram_buf = true;
131 state->ram_buf_fname = arg;
132
Simon Glassf80a8bb2014-11-11 12:47:08 -0700133 err = os_read_ram_buf(arg);
134 if (err) {
Simon Glass5c2859c2013-11-10 10:27:03 -0700135 printf("Failed to read RAM buffer\n");
136 return err;
137 }
138
139 return 0;
140}
141SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
142 "Read/write ram_buf memory contents from file");
143
Simon Glassab839dc2014-02-27 13:26:23 -0700144static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
145 const char *arg)
146{
147 state->ram_buf_rm = true;
148
149 return 0;
150}
151SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
152
Simon Glass1209e272013-11-10 10:27:04 -0700153static int sandbox_cmdline_cb_state(struct sandbox_state *state,
154 const char *arg)
155{
156 state->state_fname = arg;
157 return 0;
158}
159SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
160
161static int sandbox_cmdline_cb_read(struct sandbox_state *state,
162 const char *arg)
163{
164 state->read_state = true;
165 return 0;
166}
167SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
168
169static int sandbox_cmdline_cb_write(struct sandbox_state *state,
170 const char *arg)
171{
172 state->write_state = true;
173 return 0;
174}
175SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
176
177static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
178 const char *arg)
179{
180 state->ignore_missing_state_on_read = true;
181 return 0;
182}
183SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
184 "Ignore missing state on read");
185
Simon Glass7d95f2a2014-02-27 13:26:19 -0700186static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
187 const char *arg)
188{
189 state->show_lcd = true;
190 return 0;
191}
192SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
193 "Show the sandbox LCD display");
194
Simon Glassffb87902014-02-27 13:26:22 -0700195static const char *term_args[STATE_TERM_COUNT] = {
196 "raw-with-sigs",
197 "raw",
198 "cooked",
199};
200
201static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
202 const char *arg)
203{
204 int i;
205
206 for (i = 0; i < STATE_TERM_COUNT; i++) {
207 if (!strcmp(arg, term_args[i])) {
208 state->term_raw = i;
209 return 0;
210 }
211 }
212
213 printf("Unknown terminal setting '%s' (", arg);
214 for (i = 0; i < STATE_TERM_COUNT; i++)
215 printf("%s%s", i ? ", " : "", term_args[i]);
216 puts(")\n");
217
218 return 1;
219}
220SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
221 "Set terminal to raw/cooked mode");
222
Simon Glassbace3d02011-10-03 19:26:45 +0000223int main(int argc, char *argv[])
224{
Simon Glass70db4212012-02-15 15:51:16 -0800225 struct sandbox_state *state;
Simon Glass4d94dfa2014-07-10 22:23:27 -0600226 gd_t data;
Simon Glass1209e272013-11-10 10:27:04 -0700227 int ret;
Simon Glass6fb62072012-02-15 15:51:15 -0800228
Simon Glass1209e272013-11-10 10:27:04 -0700229 ret = state_init();
230 if (ret)
231 goto err;
Simon Glass6fb62072012-02-15 15:51:15 -0800232
Simon Glass70db4212012-02-15 15:51:16 -0800233 state = state_get_current();
234 if (os_parse_args(state, argc, argv))
235 return 1;
236
Simon Glass1209e272013-11-10 10:27:04 -0700237 ret = sandbox_read_state(state, state->state_fname);
238 if (ret)
239 goto err;
240
Simon Glassab839dc2014-02-27 13:26:23 -0700241 /* Remove old memory file if required */
242 if (state->ram_buf_rm && state->ram_buf_fname)
243 os_unlink(state->ram_buf_fname);
244
Simon Glass4d94dfa2014-07-10 22:23:27 -0600245 memset(&data, '\0', sizeof(data));
246 gd = &data;
Simon Glass29afe9e2014-07-10 22:23:31 -0600247#ifdef CONFIG_SYS_MALLOC_F_LEN
248 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
249#endif
Simon Glass4d94dfa2014-07-10 22:23:27 -0600250
Simon Glass808434c2013-11-10 10:26:59 -0700251 /* Do pre- and post-relocation init */
Simon Glassbace3d02011-10-03 19:26:45 +0000252 board_init_f(0);
Allen Martin8ec21bb2013-01-22 13:11:21 +0000253
Simon Glass808434c2013-11-10 10:26:59 -0700254 board_init_r(gd->new_gd, 0);
255
256 /* NOTREACHED - board_init_r() does not return */
Allen Martin8ec21bb2013-01-22 13:11:21 +0000257 return 0;
Simon Glass1209e272013-11-10 10:27:04 -0700258
259err:
260 printf("Error %d\n", ret);
261 return 1;
Simon Glassbace3d02011-10-03 19:26:45 +0000262}