blob: 951ac63f3a16c67a2ce704b06fa1d0424a8e073a [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
13int sandbox_early_getopt_check(void)
14{
15 struct sandbox_state *state = state_get_current();
Simon Glass64367c82013-12-03 16:43:23 -070016 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
Simon Glass8a3e0352012-02-15 15:51:16 -080017 size_t num_options = __u_boot_sandbox_option_count();
18 size_t i;
19 int max_arg_len, max_noarg_len;
20
21 /* parse_err will be a string of the faulting option */
22 if (!state->parse_err)
23 return 0;
24
25 if (strcmp(state->parse_err, "help")) {
26 printf("u-boot: error: failed while parsing option: %s\n"
27 "\ttry running with --help for more information.\n",
28 state->parse_err);
29 os_exit(1);
30 }
31
32 printf(
33 "u-boot, a command line test interface to U-Boot\n\n"
34 "Usage: u-boot [options]\n"
35 "Options:\n");
36
37 max_arg_len = 0;
38 for (i = 0; i < num_options; ++i)
39 max_arg_len = max(strlen(sb_opt[i]->flag), max_arg_len);
40 max_noarg_len = max_arg_len + 7;
41
42 for (i = 0; i < num_options; ++i) {
Simon Glass64367c82013-12-03 16:43:23 -070043 struct sandbox_cmdline_option *opt = sb_opt[i];
Simon Glass8a3e0352012-02-15 15:51:16 -080044
45 /* first output the short flag if it has one */
46 if (opt->flag_short >= 0x100)
47 printf(" ");
48 else
49 printf(" -%c, ", opt->flag_short);
50
51 /* then the long flag */
52 if (opt->has_arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080053 printf("--%-*s <arg> ", max_arg_len, opt->flag);
Simon Glass5f679f02013-11-10 10:26:58 -070054 else
55 printf("--%-*s", max_noarg_len, opt->flag);
Simon Glass8a3e0352012-02-15 15:51:16 -080056
57 /* finally the help text */
58 printf(" %s\n", opt->help);
59 }
60
61 os_exit(0);
62}
63
Simon Glass64367c82013-12-03 16:43:23 -070064static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080065{
66 /* just flag to sandbox_early_getopt_check to show usage */
67 return 1;
68}
Simon Glass64367c82013-12-03 16:43:23 -070069SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
Simon Glass8a3e0352012-02-15 15:51:16 -080070
Simon Glass0fc3c222012-02-26 17:38:50 -050071int sandbox_main_loop_init(void)
72{
Simon Glass8a3e0352012-02-15 15:51:16 -080073 struct sandbox_state *state = state_get_current();
74
75 /* Execute command if required */
76 if (state->cmd) {
Simon Glassbde3f222013-04-20 08:42:48 +000077 run_command_list(state->cmd, -1, 0);
Simon Glass8a3e0352012-02-15 15:51:16 -080078 os_exit(state->exit_type);
79 }
80
Simon Glass0fc3c222012-02-26 17:38:50 -050081 return 0;
82}
83
Simon Glass64367c82013-12-03 16:43:23 -070084static int sandbox_cmdline_cb_command(struct sandbox_state *state,
85 const char *arg)
Simon Glass8a3e0352012-02-15 15:51:16 -080086{
87 state->cmd = arg;
88 return 0;
89}
Simon Glass64367c82013-12-03 16:43:23 -070090SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
Simon Glass8a3e0352012-02-15 15:51:16 -080091
Simon Glass64367c82013-12-03 16:43:23 -070092static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
Simon Glass15393432013-04-20 08:42:41 +000093{
94 state->fdt_fname = arg;
95 return 0;
96}
Simon Glass64367c82013-12-03 16:43:23 -070097SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
Simon Glass15393432013-04-20 08:42:41 +000098
Simon Glass5ba8ef62011-10-03 19:26:45 +000099int main(int argc, char *argv[])
100{
Simon Glass8a3e0352012-02-15 15:51:16 -0800101 struct sandbox_state *state;
Simon Glass20bf89a2012-02-15 15:51:15 -0800102 int err;
103
104 err = state_init();
105 if (err)
106 return err;
107
Simon Glass8a3e0352012-02-15 15:51:16 -0800108 state = state_get_current();
109 if (os_parse_args(state, argc, argv))
110 return 1;
111
Simon Glass5ba8ef62011-10-03 19:26:45 +0000112 /*
113 * Do pre- and post-relocation init, then start up U-Boot. This will
114 * never return.
115 */
116 board_init_f(0);
Allen Martin074b4552013-01-22 13:11:21 +0000117
118 /* NOTREACHED - board_init_f() does not return */
119 return 0;
Simon Glass5ba8ef62011-10-03 19:26:45 +0000120}