36dfc0a408e960cc2dde7723f14cfc3c4a2a1e58
[karo-tx-uboot.git] / arch / sandbox / cpu / start.c
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 <asm/getopt.h>
9 #include <asm/sections.h>
10 #include <asm/state.h>
11
12 DECLARE_GLOBAL_DATA_PTR;
13
14 int sandbox_early_getopt_check(void)
15 {
16         struct sandbox_state *state = state_get_current();
17         struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
18         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) {
44                 struct sandbox_cmdline_option *opt = sb_opt[i];
45
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)
54                         printf("--%-*s <arg> ", max_arg_len, opt->flag);
55                 else
56                         printf("--%-*s", max_noarg_len, opt->flag);
57
58                 /* finally the help text */
59                 printf("  %s\n", opt->help);
60         }
61
62         os_exit(0);
63 }
64
65 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
66 {
67         /* just flag to sandbox_early_getopt_check to show usage */
68         return 1;
69 }
70 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
71
72 int sandbox_main_loop_init(void)
73 {
74         struct sandbox_state *state = state_get_current();
75
76         /* Execute command if required */
77         if (state->cmd) {
78                 run_command_list(state->cmd, -1, 0);
79                 if (!state->interactive)
80                         os_exit(state->exit_type);
81         }
82
83         return 0;
84 }
85
86 static int sandbox_cmdline_cb_command(struct sandbox_state *state,
87                                       const char *arg)
88 {
89         state->cmd = arg;
90         return 0;
91 }
92 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
93
94 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
95 {
96         state->fdt_fname = arg;
97         return 0;
98 }
99 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
100
101 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
102                                           const char *arg)
103 {
104         state->interactive = true;
105         return 0;
106 }
107
108 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");
109
110 static int sandbox_cmdline_cb_jump(struct sandbox_state *state,
111                                    const char *arg)
112 {
113         state->jumped = 1;
114
115         /*
116          * TODO(sjg@chromium.org): Note this causes problems for gdb which
117          * wants to read debug data from the image.
118          *
119          * os_unlink(arg);
120          */
121
122         return 0;
123 }
124 SANDBOX_CMDLINE_OPT_SHORT(jump, 'j', 1, "Jumped from previous U-Boot");
125
126 static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
127                                      const char *arg)
128 {
129         int err;
130
131         /* For now assume we always want to write it */
132         state->write_ram_buf = true;
133         state->ram_buf_fname = arg;
134
135         if (os_read_ram_buf(arg)) {
136                 printf("Failed to read RAM buffer\n");
137                 return err;
138         }
139
140         return 0;
141 }
142 SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
143                           "Read/write ram_buf memory contents from file");
144
145 static int sandbox_cmdline_cb_state(struct sandbox_state *state,
146                                     const char *arg)
147 {
148         state->state_fname = arg;
149         return 0;
150 }
151 SANDBOX_CMDLINE_OPT_SHORT(state, 's', 1, "Specify the sandbox state FDT");
152
153 static int sandbox_cmdline_cb_read(struct sandbox_state *state,
154                                    const char *arg)
155 {
156         state->read_state = true;
157         return 0;
158 }
159 SANDBOX_CMDLINE_OPT_SHORT(read, 'r', 0, "Read the state FDT on startup");
160
161 static int sandbox_cmdline_cb_write(struct sandbox_state *state,
162                                     const char *arg)
163 {
164         state->write_state = true;
165         return 0;
166 }
167 SANDBOX_CMDLINE_OPT_SHORT(write, 'w', 0, "Write state FDT on exit");
168
169 static int sandbox_cmdline_cb_ignore_missing(struct sandbox_state *state,
170                                              const char *arg)
171 {
172         state->ignore_missing_state_on_read = true;
173         return 0;
174 }
175 SANDBOX_CMDLINE_OPT_SHORT(ignore_missing, 'n', 0,
176                           "Ignore missing state on read");
177
178 static int sandbox_cmdline_cb_show_lcd(struct sandbox_state *state,
179                                        const char *arg)
180 {
181         state->show_lcd = true;
182         return 0;
183 }
184 SANDBOX_CMDLINE_OPT_SHORT(show_lcd, 'l', 0,
185                           "Show the sandbox LCD display");
186
187 static const char *term_args[STATE_TERM_COUNT] = {
188         "raw-with-sigs",
189         "raw",
190         "cooked",
191 };
192
193 static int sandbox_cmdline_cb_terminal(struct sandbox_state *state,
194                                        const char *arg)
195 {
196         int i;
197
198         for (i = 0; i < STATE_TERM_COUNT; i++) {
199                 if (!strcmp(arg, term_args[i])) {
200                         state->term_raw = i;
201                         return 0;
202                 }
203         }
204
205         printf("Unknown terminal setting '%s' (", arg);
206         for (i = 0; i < STATE_TERM_COUNT; i++)
207                 printf("%s%s", i ? ", " : "", term_args[i]);
208         puts(")\n");
209
210         return 1;
211 }
212 SANDBOX_CMDLINE_OPT_SHORT(terminal, 't', 1,
213                           "Set terminal to raw/cooked mode");
214
215 int main(int argc, char *argv[])
216 {
217         struct sandbox_state *state;
218         int ret;
219
220         ret = state_init();
221         if (ret)
222                 goto err;
223
224         state = state_get_current();
225         if (os_parse_args(state, argc, argv))
226                 return 1;
227
228         ret = sandbox_read_state(state, state->state_fname);
229         if (ret)
230                 goto err;
231
232         /* Do pre- and post-relocation init */
233         board_init_f(0);
234
235         board_init_r(gd->new_gd, 0);
236
237         /* NOTREACHED - board_init_r() does not return */
238         return 0;
239
240 err:
241         printf("Error %d\n", ret);
242         return 1;
243 }