blob: 895c4d82b0dd654bd70cbc4b91c62ba3d5c5d952 [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 Glass8a3e0352012-02-15 15:51:16 -08007#include <asm/getopt.h>
8#include <asm/sections.h>
Simon Glass20bf89a2012-02-15 15:51:15 -08009#include <asm/state.h>
Simon Glass5ba8ef62011-10-03 19:26:45 +000010
Simon Glass8a3e0352012-02-15 15:51:16 -080011#include <os.h>
12
Simon Glass2fd34e62013-11-10 10:26:59 -070013DECLARE_GLOBAL_DATA_PTR;
14
Simon Glass8a3e0352012-02-15 15:51:16 -080015int sandbox_early_getopt_check(void)
16{
17 struct sandbox_state *state = state_get_current();
Simon Glass64367c82013-12-03 16:43:23 -070018 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass8a3e0352012-02-15 15:51:16 -080019 size_t num_options = __u_boot_sandbox_option_count();
20 size_t i;
21 int max_arg_len, max_noarg_len;
22
23 /* parse_err will be a string of the faulting option */
24 if (!state->parse_err)
25 return 0;
26
27 if (strcmp(state->parse_err, "help")) {
28 printf("u-boot: error: failed while parsing option: %s\n"
29 "\ttry running with --help for more information.\n",
30 state->parse_err);
31 os_exit(1);
32 }
33
34 printf(
35 "u-boot, a command line test interface to U-Boot\n\n"
36 "Usage: u-boot [options]\n"
37 "Options:\n");
38
39 max_arg_len = 0;
40 for (i = 0; i < num_options; ++i)
41 max_arg_len = max(strlen(sb_opt[i]->flag), max_arg_len);
42 max_noarg_len = max_arg_len + 7;
43
44 for (i = 0; i < num_options; ++i) {
Simon Glass64367c82013-12-03 16:43:23 -070045 struct sandbox_cmdline_option *opt = sb_opt[i];
Simon Glass8a3e0352012-02-15 15:51:16 -080046
47 /* first output the short flag if it has one */
48 if (opt->flag_short >= 0x100)
49 printf(" ");
50 else
51 printf(" -%c, ", opt->flag_short);
52
53 /* then the long flag */
54 if (opt->has_arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080055 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass5f679f02013-11-10 10:26:58 -070056 else
57 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass8a3e0352012-02-15 15:51:16 -080058
59 /* finally the help text */
60 printf(" %s\n", opt->help);
61 }
62
63 os_exit(0);
64}
65
Simon Glass64367c82013-12-03 16:43:23 -070066static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080067{
68 /* just flag to sandbox_early_getopt_check to show usage */
69 return 1;
70}
Simon Glass64367c82013-12-03 16:43:23 -070071SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass8a3e0352012-02-15 15:51:16 -080072
Simon Glass0fc3c222012-02-26 17:38:50 -050073int sandbox_main_loop_init(void)
74{
Simon Glass8a3e0352012-02-15 15:51:16 -080075 struct sandbox_state *state = state_get_current();
76
77 /* Execute command if required */
78 if (state->cmd) {
Simon Glassbde3f222013-04-20 08:42:48 +000079 run_command_list(state->cmd, -1, 0);
Simon Glass8a3e0352012-02-15 15:51:16 -080080 os_exit(state->exit_type);
81 }
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 Glass5ba8ef62011-10-03 19:26:45 +0000101int main(int argc, char *argv[])
102{
Simon Glass8a3e0352012-02-15 15:51:16 -0800103 struct sandbox_state *state;
Simon Glass20bf89a2012-02-15 15:51:15 -0800104 int err;
105
106 err = state_init();
107 if (err)
108 return err;
109
Simon Glass8a3e0352012-02-15 15:51:16 -0800110 state = state_get_current();
111 if (os_parse_args(state, argc, argv))
112 return 1;
113
Simon Glass2fd34e62013-11-10 10:26:59 -0700114 /* Do pre- and post-relocation init */
Simon Glass5ba8ef62011-10-03 19:26:45 +0000115 board_init_f(0);
Allen Martin074b4552013-01-22 13:11:21 +0000116
Simon Glass2fd34e62013-11-10 10:26:59 -0700117 board_init_r(gd->new_gd, 0);
118
119 /* NOTREACHED - board_init_r() does not return */
Allen Martin074b4552013-01-22 13:11:21 +0000120 return 0;
Simon Glass5ba8ef62011-10-03 19:26:45 +0000121}