blob: 1df21d49fa501d9744b5a589cc1904d1f0bc20c6 [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>
Simon Glass8a3e0352012-02-15 15:51:16 -08008#include <asm/getopt.h>
9#include <asm/sections.h>
Simon Glass20bf89a2012-02-15 15:51:15 -080010#include <asm/state.h>
Simon Glass5ba8ef62011-10-03 19:26:45 +000011
Simon Glass2fd34e62013-11-10 10:26:59 -070012DECLARE_GLOBAL_DATA_PTR;
13
Simon Glass8a3e0352012-02-15 15:51:16 -080014int sandbox_early_getopt_check(void)
15{
16 struct sandbox_state *state = state_get_current();
Simon Glass64367c82013-12-03 16:43:23 -070017 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass8a3e0352012-02-15 15:51:16 -080018 size_t num_options = __u_boot_sandbox_option_count();
19 size_t i;
20 int max_arg_len, max_noarg_len;
21
22 /* parse_err will be a string of the faulting option */
23 if (!state->parse_err)
24 return 0;
25
26 if (strcmp(state->parse_err, "help")) {
27 printf("u-boot: error: failed while parsing option: %s\n"
28 "\ttry running with --help for more information.\n",
29 state->parse_err);
30 os_exit(1);
31 }
32
33 printf(
34 "u-boot, a command line test interface to U-Boot\n\n"
35 "Usage: u-boot [options]\n"
36 "Options:\n");
37
38 max_arg_len = 0;
39 for (i = 0; i < num_options; ++i)
40 max_arg_len = max(strlen(sb_opt[i]->flag), max_arg_len);
41 max_noarg_len = max_arg_len + 7;
42
43 for (i = 0; i < num_options; ++i) {
Simon Glass64367c82013-12-03 16:43:23 -070044 struct sandbox_cmdline_option *opt = sb_opt[i];
Simon Glass8a3e0352012-02-15 15:51:16 -080045
46 /* first output the short flag if it has one */
47 if (opt->flag_short >= 0x100)
48 printf(" ");
49 else
50 printf(" -%c, ", opt->flag_short);
51
52 /* then the long flag */
53 if (opt->has_arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080054 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass5f679f02013-11-10 10:26:58 -070055 else
56 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass8a3e0352012-02-15 15:51:16 -080057
58 /* finally the help text */
59 printf(" %s\n", opt->help);
60 }
61
62 os_exit(0);
63}
64
Simon Glass64367c82013-12-03 16:43:23 -070065static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080066{
67 /* just flag to sandbox_early_getopt_check to show usage */
68 return 1;
69}
Simon Glass64367c82013-12-03 16:43:23 -070070SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass8a3e0352012-02-15 15:51:16 -080071
Simon Glass0fc3c222012-02-26 17:38:50 -050072int sandbox_main_loop_init(void)
73{
Simon Glass8a3e0352012-02-15 15:51:16 -080074 struct sandbox_state *state = state_get_current();
75
76 /* Execute command if required */
77 if (state->cmd) {
Simon Glassbde3f222013-04-20 08:42:48 +000078 run_command_list(state->cmd, -1, 0);
Simon Glassf498e432013-11-10 10:27:02 -070079 if (!state->interactive)
80 os_exit(state->exit_type);
Simon Glass8a3e0352012-02-15 15:51:16 -080081 }
82
Simon Glass0fc3c222012-02-26 17:38:50 -050083 return 0;
84}
85
Simon Glass64367c82013-12-03 16:43:23 -070086static int sandbox_cmdline_cb_command(struct sandbox_state *state,
87 const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080088{
89 state->cmd = arg;
90 return 0;
91}
Simon Glass64367c82013-12-03 16:43:23 -070092SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass8a3e0352012-02-15 15:51:16 -080093
Simon Glass64367c82013-12-03 16:43:23 -070094static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glass15393432013-04-20 08:42:41 +000095{
96 state->fdt_fname = arg;
97 return 0;
98}
Simon Glass64367c82013-12-03 16:43:23 -070099SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glass15393432013-04-20 08:42:41 +0000100
Simon Glassf498e432013-11-10 10:27:02 -0700101static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
102 const char *arg)
103{
104 state->interactive = true;
105 return 0;
106}
107
108SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
109
Simon Glass9dd10bf2013-11-10 10:27:03 -0700110static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
111 const char *arg)
112{
113 int err;
114
115 /* For now assume we always want to write it */
116 state->write_ram_buf = true;
117 state->ram_buf_fname = arg;
118
119 if (os_read_ram_buf(arg)) {
120 printf("Failed to read RAM buffer\n");
121 return err;
122 }
123
124 return 0;
125}
126SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
127 "Read/write ram_buf memory contents from file");
128
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700129static int sandbox_cmdline_cb_state(struct sandbox_state *state,
130 const char *arg)
131{
132 state->state_fname = arg;
133 return 0;
134}
135SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
136
137static int sandbox_cmdline_cb_read(struct sandbox_state *state,
138 const char *arg)
139{
140 state->read_state = true;
141 return 0;
142}
143SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
144
145static int sandbox_cmdline_cb_write(struct sandbox_state *state,
146 const char *arg)
147{
148 state->write_state = true;
149 return 0;
150}
151SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
152
153static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
154 const char *arg)
155{
156 state->ignore_missing_state_on_read = true;
157 return 0;
158}
159SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
160 "Ignore missing state on read");
161
Simon Glass5ba8ef62011-10-03 19:26:45 +0000162int main(int argc, char *argv[])
163{
Simon Glass8a3e0352012-02-15 15:51:16 -0800164 struct sandbox_state *state;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700165 int ret;
Simon Glass20bf89a2012-02-15 15:51:15 -0800166
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700167 ret = state_init();
168 if (ret)
169 goto err;
Simon Glass20bf89a2012-02-15 15:51:15 -0800170
Simon Glass8a3e0352012-02-15 15:51:16 -0800171 state = state_get_current();
172 if (os_parse_args(state, argc, argv))
173 return 1;
174
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700175 ret = sandbox_read_state(state, state->state_fname);
176 if (ret)
177 goto err;
178
Simon Glass2fd34e62013-11-10 10:26:59 -0700179 /* Do pre- and post-relocation init */
Simon Glass5ba8ef62011-10-03 19:26:45 +0000180 board_init_f(0);
Allen Martin074b4552013-01-22 13:11:21 +0000181
Simon Glass2fd34e62013-11-10 10:26:59 -0700182 board_init_r(gd->new_gd, 0);
183
184 /* NOTREACHED - board_init_r() does not return */
Allen Martin074b4552013-01-22 13:11:21 +0000185 return 0;
Simon Glassd7c8d8d2013-11-10 10:27:04 -0700186
187err:
188 printf("Error %d\n", ret);
189 return 1;
Simon Glass5ba8ef62011-10-03 19:26:45 +0000190}