start.c 3.3 KB
Newer Older
S
Simon Glass 已提交
1
/*
2
 * Copyright (c) 2011-2012 The Chromium OS Authors.
3
 * SPDX-License-Identifier:	GPL-2.0+
S
Simon Glass 已提交
4 5 6
 */

#include <common.h>
7
#include <os.h>
S
Simon Glass 已提交
8 9
#include <asm/getopt.h>
#include <asm/sections.h>
10
#include <asm/state.h>
S
Simon Glass 已提交
11

12 13
DECLARE_GLOBAL_DATA_PTR;

S
Simon Glass 已提交
14 15 16
int sandbox_early_getopt_check(void)
{
	struct sandbox_state *state = state_get_current();
17
	struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
S
Simon Glass 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	size_t num_options = __u_boot_sandbox_option_count();
	size_t i;
	int max_arg_len, max_noarg_len;

	/* parse_err will be a string of the faulting option */
	if (!state->parse_err)
		return 0;

	if (strcmp(state->parse_err, "help")) {
		printf("u-boot: error: failed while parsing option: %s\n"
			"\ttry running with --help for more information.\n",
			state->parse_err);
		os_exit(1);
	}

	printf(
		"u-boot, a command line test interface to U-Boot\n\n"
		"Usage: u-boot [options]\n"
		"Options:\n");

	max_arg_len = 0;
	for (i = 0; i < num_options; ++i)
		max_arg_len = max(strlen(sb_opt[i]->flag), max_arg_len);
	max_noarg_len = max_arg_len + 7;

	for (i = 0; i < num_options; ++i) {
44
		struct sandbox_cmdline_option *opt = sb_opt[i];
S
Simon Glass 已提交
45 46 47 48 49 50 51 52 53 54

		/* first output the short flag if it has one */
		if (opt->flag_short >= 0x100)
			printf("      ");
		else
			printf("  -%c, ", opt->flag_short);

		/* then the long flag */
		if (opt->has_arg)
			printf("--%-*s <arg> ", max_arg_len, opt->flag);
55 56
		else
			printf("--%-*s", max_noarg_len, opt->flag);
S
Simon Glass 已提交
57 58 59 60 61 62 63 64

		/* finally the help text */
		printf("  %s\n", opt->help);
	}

	os_exit(0);
}

65
static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg)
S
Simon Glass 已提交
66 67 68 69
{
	/* just flag to sandbox_early_getopt_check to show usage */
	return 1;
}
70
SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help");
S
Simon Glass 已提交
71

72 73
int sandbox_main_loop_init(void)
{
S
Simon Glass 已提交
74 75 76 77
	struct sandbox_state *state = state_get_current();

	/* Execute command if required */
	if (state->cmd) {
78
		run_command_list(state->cmd, -1, 0);
79 80
		if (!state->interactive)
			os_exit(state->exit_type);
S
Simon Glass 已提交
81 82
	}

83 84 85
	return 0;
}

86 87
static int sandbox_cmdline_cb_command(struct sandbox_state *state,
				      const char *arg)
S
Simon Glass 已提交
88 89 90 91
{
	state->cmd = arg;
	return 0;
}
92
SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command");
S
Simon Glass 已提交
93

94
static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg)
95 96 97 98
{
	state->fdt_fname = arg;
	return 0;
}
99
SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT");
100

101 102 103 104 105 106 107 108 109
static int sandbox_cmdline_cb_interactive(struct sandbox_state *state,
					  const char *arg)
{
	state->interactive = true;
	return 0;
}

SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode");

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static int sandbox_cmdline_cb_memory(struct sandbox_state *state,
				     const char *arg)
{
	int err;

	/* For now assume we always want to write it */
	state->write_ram_buf = true;
	state->ram_buf_fname = arg;

	if (os_read_ram_buf(arg)) {
		printf("Failed to read RAM buffer\n");
		return err;
	}

	return 0;
}
SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1,
			  "Read/write ram_buf memory contents from file");

S
Simon Glass 已提交
129 130
int main(int argc, char *argv[])
{
S
Simon Glass 已提交
131
	struct sandbox_state *state;
132 133 134 135 136 137
	int err;

	err = state_init();
	if (err)
		return err;

S
Simon Glass 已提交
138 139 140 141
	state = state_get_current();
	if (os_parse_args(state, argc, argv))
		return 1;

142
	/* Do pre- and post-relocation init */
S
Simon Glass 已提交
143
	board_init_f(0);
A
Allen Martin 已提交
144

145 146 147
	board_init_r(gd->new_gd, 0);

	/* NOTREACHED - board_init_r() does not return */
A
Allen Martin 已提交
148
	return 0;
S
Simon Glass 已提交
149
}