xref: /openbmc/u-boot/arch/sandbox/cpu/start.c (revision 5296cb1d)
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) {
81 		int retval;
82 
83 		cli_init();
84 
85 		retval = run_command_list(state->cmd, -1, 0);
86 		if (!state->interactive)
87 			os_exit(retval);
88 	}
89 
90 	return 0;
91 }
92 
93 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
94 				      const char *arg)
95 {
96 	state->cmd = arg;
97 	return 0;
98 }
99 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
100 
101 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
102 {
103 	state->fdt_fname = arg;
104 	return 0;
105 }
106 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
107 
108 static 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 }
124 SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
125 		"Use the default u-boot.dtb control FDT in U-Boot directory");
126 
127 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
128 					  const char *arg)
129 {
130 	state->interactive = true;
131 	return 0;
132 }
133 
134 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
135 
136 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
137 				   const char *arg)
138 {
139 	/* Remember to delete this U-Boot image later */
140 	state->jumped_fname = arg;
141 
142 	return 0;
143 }
144 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
145 
146 static 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 
155 	err = os_read_ram_buf(arg);
156 	if (err) {
157 		printf("Failed to read RAM buffer\n");
158 		return err;
159 	}
160 
161 	return 0;
162 }
163 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
164 			  "Read/write ram_buf memory contents from file");
165 
166 static 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 }
173 SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
174 
175 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
176 				    const char *arg)
177 {
178 	state->state_fname = arg;
179 	return 0;
180 }
181 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
182 
183 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
184 				   const char *arg)
185 {
186 	state->read_state = true;
187 	return 0;
188 }
189 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
190 
191 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
192 				    const char *arg)
193 {
194 	state->write_state = true;
195 	return 0;
196 }
197 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
198 
199 static 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 }
205 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
206 			  "Ignore missing state on read");
207 
208 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
209 				       const char *arg)
210 {
211 	state->show_lcd = true;
212 	return 0;
213 }
214 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
215 			  "Show the sandbox LCD display");
216 
217 static const char *term_args[STATE_TERM_COUNT] = {
218 	"raw-with-sigs",
219 	"raw",
220 	"cooked",
221 };
222 
223 static 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 }
242 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
243 			  "Set terminal to raw/cooked mode");
244 
245 int main(int argc, char *argv[])
246 {
247 	struct sandbox_state *state;
248 	gd_t data;
249 	int ret;
250 
251 	ret = state_init();
252 	if (ret)
253 		goto err;
254 
255 	state = state_get_current();
256 	if (os_parse_args(state, argc, argv))
257 		return 1;
258 
259 	ret = sandbox_read_state(state, state->state_fname);
260 	if (ret)
261 		goto err;
262 
263 	/* Remove old memory file if required */
264 	if (state->ram_buf_rm && state->ram_buf_fname)
265 		os_unlink(state->ram_buf_fname);
266 
267 	memset(&data, '\0', sizeof(data));
268 	gd = &data;
269 #ifdef CONFIG_SYS_MALLOC_F_LEN
270 	gd->malloc_base = CONFIG_MALLOC_F_ADDR;
271 #endif
272 
273 	/* Do pre- and post-relocation init */
274 	board_init_f(0);
275 
276 	board_init_r(gd->new_gd, 0);
277 
278 	/* NOTREACHED - board_init_r() does not return */
279 	return 0;
280 
281 err:
282 	printf("Error %d\n", ret);
283 	return 1;
284 }
285