Merge tag 'u-boot-imx-20191009' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[oweals/u-boot.git] / arch / sandbox / cpu / start.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011-2012 The Chromium OS Authors.
4  */
5
6 #include <common.h>
7 #include <errno.h>
8 #include <os.h>
9 #include <cli.h>
10 #include <malloc.h>
11 #include <asm/getopt.h>
12 #include <asm/io.h>
13 #include <asm/sections.h>
14 #include <asm/state.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 int sandbox_early_getopt_check(void)
19 {
20         struct sandbox_state *state = state_get_current();
21         struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
22         size_t num_options = __u_boot_sandbox_option_count();
23         size_t i;
24         int max_arg_len, max_noarg_len;
25
26         /* parse_err will be a string of the faulting option */
27         if (!state->parse_err)
28                 return 0;
29
30         if (strcmp(state->parse_err, "help")) {
31                 printf("u-boot: error: failed while parsing option: %s\n"
32                         "\ttry running with --help for more information.\n",
33                         state->parse_err);
34                 os_exit(1);
35         }
36
37         printf(
38                 "u-boot, a command line test interface to U-Boot\n\n"
39                 "Usage: u-boot [options]\n"
40                 "Options:\n");
41
42         max_arg_len = 0;
43         for (i = 0; i < num_options; ++i)
44                 max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
45         max_noarg_len = max_arg_len + 7;
46
47         for (i = 0; i < num_options; ++i) {
48                 struct sandbox_cmdline_option *opt = sb_opt[i];
49
50                 /* first output the short flag if it has one */
51                 if (opt->flag_short >= 0x100)
52                         printf("      ");
53                 else
54                         printf("  -%c, ", opt->flag_short);
55
56                 /* then the long flag */
57                 if (opt->has_arg)
58                         printf("--%-*s <arg> ", max_arg_len, opt->flag);
59                 else
60                         printf("--%-*s", max_noarg_len, opt->flag);
61
62                 /* finally the help text */
63                 printf("  %s\n", opt->help);
64         }
65
66         os_exit(0);
67 }
68
69 int misc_init_f(void)
70 {
71         return sandbox_early_getopt_check();
72 }
73
74 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
75 {
76         /* just flag to sandbox_early_getopt_check to show usage */
77         return 1;
78 }
79 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
80
81 #ifndef CONFIG_SPL_BUILD
82 int sandbox_main_loop_init(void)
83 {
84         struct sandbox_state *state = state_get_current();
85
86         /* Execute command if required */
87         if (state->cmd || state->run_distro_boot) {
88                 int retval = 0;
89
90                 cli_init();
91
92 #ifdef CONFIG_CMDLINE
93                 if (state->cmd)
94                         retval = run_command_list(state->cmd, -1, 0);
95
96                 if (state->run_distro_boot)
97                         retval = cli_simple_run_command("run distro_bootcmd",
98                                                         0);
99 #endif
100                 if (!state->interactive)
101                         os_exit(retval);
102         }
103
104         return 0;
105 }
106 #endif
107
108 static int sandbox_cmdline_cb_boot(struct sandbox_state *state,
109                                       const char *arg)
110 {
111         state->run_distro_boot = true;
112         return 0;
113 }
114 SANDBOX_CMDLINE_OPT_SHORT(boot, 'b', 0, "Run distro boot commands");
115
116 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
117                                       const char *arg)
118 {
119         state->cmd = arg;
120         return 0;
121 }
122 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
123
124 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
125 {
126         state->fdt_fname = arg;
127         return 0;
128 }
129 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
130
131 static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state,
132                                           const char *arg)
133 {
134         const char *fmt = "%s.dtb";
135         char *fname;
136         int len;
137
138         len = strlen(state->argv[0]) + strlen(fmt) + 1;
139         fname = os_malloc(len);
140         if (!fname)
141                 return -ENOMEM;
142         snprintf(fname, len, fmt, state->argv[0]);
143         state->fdt_fname = fname;
144
145         return 0;
146 }
147 SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
148                 "Use the default u-boot.dtb control FDT in U-Boot directory");
149
150 static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
151                                        const char *arg)
152 {
153         const char *fmt = "/arch/sandbox/dts/test.dtb";
154         char *p;
155         char *fname;
156         int len;
157
158         len = strlen(state->argv[0]) + strlen(fmt) + 1;
159         fname = os_malloc(len);
160         if (!fname)
161                 return -ENOMEM;
162         strcpy(fname, state->argv[0]);
163         p = strrchr(fname, '/');
164         if (!p)
165                 p = fname + strlen(fname);
166         len -= p - fname;
167         snprintf(p, len, fmt, p);
168         state->fdt_fname = fname;
169
170         return 0;
171 }
172 SANDBOX_CMDLINE_OPT_SHORT(test_fdt, 'T', 0,
173                           "Use the test.dtb control FDT in U-Boot directory");
174
175 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
176                                           const char *arg)
177 {
178         state->interactive = true;
179         return 0;
180 }
181
182 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
183
184 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
185                                    const char *arg)
186 {
187         /* Remember to delete this U-Boot image later */
188         state->jumped_fname = arg;
189
190         return 0;
191 }
192 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
193
194 static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
195                                      const char *arg)
196 {
197         int err;
198
199         /* For now assume we always want to write it */
200         state->write_ram_buf = true;
201         state->ram_buf_fname = arg;
202
203         err = os_read_ram_buf(arg);
204         if (err) {
205                 printf("Failed to read RAM buffer '%s': %d\n", arg, err);
206                 return err;
207         }
208         state->ram_buf_read = true;
209
210         return 0;
211 }
212 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
213                           "Read/write ram_buf memory contents from file");
214
215 static int sandbox_cmdline_cb_rm_memory(struct sandbox_state *state,
216                                         const char *arg)
217 {
218         state->ram_buf_rm = true;
219
220         return 0;
221 }
222 SANDBOX_CMDLINE_OPT(rm_memory, 0, "Remove memory file after reading");
223
224 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
225                                     const char *arg)
226 {
227         state->state_fname = arg;
228         return 0;
229 }
230 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
231
232 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
233                                    const char *arg)
234 {
235         state->read_state = true;
236         return 0;
237 }
238 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
239
240 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
241                                     const char *arg)
242 {
243         state->write_state = true;
244         return 0;
245 }
246 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
247
248 static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
249                                              const char *arg)
250 {
251         state->ignore_missing_state_on_read = true;
252         return 0;
253 }
254 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
255                           "Ignore missing state on read");
256
257 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
258                                        const char *arg)
259 {
260         state->show_lcd = true;
261         return 0;
262 }
263 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
264                           "Show the sandbox LCD display");
265
266 static const char *term_args[STATE_TERM_COUNT] = {
267         "raw-with-sigs",
268         "raw",
269         "cooked",
270 };
271
272 static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
273                                        const char *arg)
274 {
275         int i;
276
277         for (i = 0; i < STATE_TERM_COUNT; i++) {
278                 if (!strcmp(arg, term_args[i])) {
279                         state->term_raw = i;
280                         return 0;
281                 }
282         }
283
284         printf("Unknown terminal setting '%s' (", arg);
285         for (i = 0; i < STATE_TERM_COUNT; i++)
286                 printf("%s%s", i ? ", " : "", term_args[i]);
287         puts(")\n");
288
289         return 1;
290 }
291 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
292                           "Set terminal to raw/cooked mode");
293
294 static int sandbox_cmdline_cb_verbose(struct sandbox_state *state,
295                                       const char *arg)
296 {
297         state->show_test_output = true;
298         return 0;
299 }
300 SANDBOX_CMDLINE_OPT_SHORT(verbose, 'v', 0, "Show test output");
301
302 static int sandbox_cmdline_cb_log_level(struct sandbox_state *state,
303                                         const char *arg)
304 {
305         state->default_log_level = simple_strtol(arg, NULL, 10);
306
307         return 0;
308 }
309 SANDBOX_CMDLINE_OPT_SHORT(log_level, 'L', 1,
310                           "Set log level (0=panic, 7=debug)");
311
312 static int sandbox_cmdline_cb_show_of_platdata(struct sandbox_state *state,
313                                                const char *arg)
314 {
315         state->show_of_platdata = true;
316
317         return 0;
318 }
319 SANDBOX_CMDLINE_OPT(show_of_platdata, 0, "Show of-platdata in SPL");
320
321 int board_run_command(const char *cmdline)
322 {
323         printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
324
325         return 1;
326 }
327
328 static void setup_ram_buf(struct sandbox_state *state)
329 {
330         /* Zero the RAM buffer if we didn't read it, to keep valgrind happy */
331         if (!state->ram_buf_read)
332                 memset(state->ram_buf, '\0', state->ram_size);
333
334         gd->arch.ram_buf = state->ram_buf;
335         gd->ram_size = state->ram_size;
336 }
337
338 void state_show(struct sandbox_state *state)
339 {
340         char **p;
341
342         printf("Arguments:\n");
343         for (p = state->argv; *p; p++)
344                 printf("%s ", *p);
345         printf("\n");
346 }
347
348 int main(int argc, char *argv[])
349 {
350         struct sandbox_state *state;
351         gd_t data;
352         int ret;
353
354         memset(&data, '\0', sizeof(data));
355         gd = &data;
356         gd->arch.text_base = os_find_text_base();
357
358         ret = state_init();
359         if (ret)
360                 goto err;
361
362         state = state_get_current();
363         if (os_parse_args(state, argc, argv))
364                 return 1;
365
366         ret = sandbox_read_state(state, state->state_fname);
367         if (ret)
368                 goto err;
369
370 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
371         gd->malloc_base = CONFIG_MALLOC_F_ADDR;
372 #endif
373 #if CONFIG_IS_ENABLED(LOG)
374         gd->default_log_level = state->default_log_level;
375 #endif
376         setup_ram_buf(state);
377
378         /*
379          * Set up the relocation offset here, since sandbox symbols are always
380          * relocated by the OS before sandbox is entered.
381          */
382         gd->reloc_off = (ulong)gd->arch.text_base;
383
384         /* Do pre- and post-relocation init */
385         board_init_f(0);
386
387         board_init_r(gd->new_gd, 0);
388
389         /* NOTREACHED - board_init_r() does not return */
390         return 0;
391
392 err:
393         printf("Error %d\n", ret);
394         return 1;
395 }