xref: /openbmc/u-boot/arch/sandbox/cpu/start.c (revision 60570df1)
1 /*
2  * Copyright (c) 2011-2012 The Chromium OS Authors.
3  * SPDX-License-Identifier:	GPL-2.0+
4  */
5 
6 #include <common.h>
7 #include <os.h>
8 #include <cli.h>
9 #include <malloc.h>
10 #include <asm/getopt.h>
11 #include <asm/io.h>
12 #include <asm/sections.h>
13 #include <asm/state.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 int sandbox_early_getopt_check(void)
18 {
19 	struct sandbox_state *state = state_get_current();
20 	struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
21 	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)
43 		max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
44 	max_noarg_len = max_arg_len + 7;
45 
46 	for (i = 0; i < num_options; ++i) {
47 		struct sandbox_cmdline_option *opt = sb_opt[i];
48 
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)
57 			printf("--%-*s <arg> ", max_arg_len, opt->flag);
58 		else
59 			printf("--%-*s", max_noarg_len, opt->flag);
60 
61 		/* finally the help text */
62 		printf("  %s\n", opt->help);
63 	}
64 
65 	os_exit(0);
66 }
67 
68 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
69 {
70 	/* just flag to sandbox_early_getopt_check to show usage */
71 	return 1;
72 }
73 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
74 
75 int sandbox_main_loop_init(void)
76 {
77 	struct sandbox_state *state = state_get_current();
78 
79 	/* Execute command if required */
80 	if (state->cmd || state->run_distro_boot) {
81 		int retval = 0;
82 
83 		cli_init();
84 
85 		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 
92 		if (!state->interactive)
93 			os_exit(retval);
94 	}
95 
96 	return 0;
97 }
98 
99 static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
100 				      const char *arg)
101 {
102 	state->run_distro_boot = true;
103 	return 0;
104 }
105 SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
106 
107 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
108 				      const char *arg)
109 {
110 	state->cmd = arg;
111 	return 0;
112 }
113 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
114 
115 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
116 {
117 	state->fdt_fname = arg;
118 	return 0;
119 }
120 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
121 
122 static 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 }
138 SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
139 		"Use the default u-boot.dtb control FDT in U-Boot directory");
140 
141 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
142 					  const char *arg)
143 {
144 	state->interactive = true;
145 	return 0;
146 }
147 
148 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
149 
150 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
151 				   const char *arg)
152 {
153 	/* Remember to delete this U-Boot image later */
154 	state->jumped_fname = arg;
155 
156 	return 0;
157 }
158 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
159 
160 static 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 
169 	err = os_read_ram_buf(arg);
170 	if (err) {
171 		printf("Failed to read RAM buffer\n");
172 		return err;
173 	}
174 
175 	return 0;
176 }
177 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
178 			  "Read/write ram_buf memory contents from file");
179 
180 static 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 }
187 SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
188 
189 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
190 				    const char *arg)
191 {
192 	state->state_fname = arg;
193 	return 0;
194 }
195 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
196 
197 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
198 				   const char *arg)
199 {
200 	state->read_state = true;
201 	return 0;
202 }
203 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
204 
205 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
206 				    const char *arg)
207 {
208 	state->write_state = true;
209 	return 0;
210 }
211 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
212 
213 static 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 }
219 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
220 			  "Ignore missing state on read");
221 
222 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
223 				       const char *arg)
224 {
225 	state->show_lcd = true;
226 	return 0;
227 }
228 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
229 			  "Show the sandbox LCD display");
230 
231 static const char *term_args[STATE_TERM_COUNT] = {
232 	"raw-with-sigs",
233 	"raw",
234 	"cooked",
235 };
236 
237 static 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 }
256 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
257 			  "Set terminal to raw/cooked mode");
258 
259 int main(int argc, char *argv[])
260 {
261 	struct sandbox_state *state;
262 	gd_t data;
263 	int ret;
264 
265 	ret = state_init();
266 	if (ret)
267 		goto err;
268 
269 	state = state_get_current();
270 	if (os_parse_args(state, argc, argv))
271 		return 1;
272 
273 	ret = sandbox_read_state(state, state->state_fname);
274 	if (ret)
275 		goto err;
276 
277 	/* Remove old memory file if required */
278 	if (state->ram_buf_rm && state->ram_buf_fname)
279 		os_unlink(state->ram_buf_fname);
280 
281 	memset(&data, '\0', sizeof(data));
282 	gd = &data;
283 #ifdef CONFIG_SYS_MALLOC_F_LEN
284 	gd->malloc_base = CONFIG_MALLOC_F_ADDR;
285 #endif
286 
287 	/* Do pre- and post-relocation init */
288 	board_init_f(0);
289 
290 	board_init_r(gd->new_gd, 0);
291 
292 	/* NOTREACHED - board_init_r() does not return */
293 	return 0;
294 
295 err:
296 	printf("Error %d\n", ret);
297 	return 1;
298 }
299