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 <asm/getopt.h> 8 #include <asm/sections.h> 9 #include <asm/state.h> 10 11 #include <os.h> 12 13 DECLARE_GLOBAL_DATA_PTR; 14 15 int sandbox_early_getopt_check(void) 16 { 17 struct sandbox_state *state = state_get_current(); 18 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start; 19 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) { 45 struct sandbox_cmdline_option *opt = sb_opt[i]; 46 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) 55 printf("--%-*s <arg> ", max_arg_len, opt->flag); 56 else 57 printf("--%-*s", max_noarg_len, opt->flag); 58 59 /* finally the help text */ 60 printf(" %s\n", opt->help); 61 } 62 63 os_exit(0); 64 } 65 66 static int sandbox_cmdline_cb_help(struct sandbox_state *state, const char *arg) 67 { 68 /* just flag to sandbox_early_getopt_check to show usage */ 69 return 1; 70 } 71 SANDBOX_CMDLINE_OPT_SHORT(help, 'h', 0, "Display help"); 72 73 int sandbox_main_loop_init(void) 74 { 75 struct sandbox_state *state = state_get_current(); 76 77 /* Execute command if required */ 78 if (state->cmd) { 79 run_command_list(state->cmd, -1, 0); 80 if (!state->interactive) 81 os_exit(state->exit_type); 82 } 83 84 return 0; 85 } 86 87 static int sandbox_cmdline_cb_command(struct sandbox_state *state, 88 const char *arg) 89 { 90 state->cmd = arg; 91 return 0; 92 } 93 SANDBOX_CMDLINE_OPT_SHORT(command, 'c', 1, "Execute U-Boot command"); 94 95 static int sandbox_cmdline_cb_fdt(struct sandbox_state *state, const char *arg) 96 { 97 state->fdt_fname = arg; 98 return 0; 99 } 100 SANDBOX_CMDLINE_OPT_SHORT(fdt, 'd', 1, "Specify U-Boot's control FDT"); 101 102 static int sandbox_cmdline_cb_interactive(struct sandbox_state *state, 103 const char *arg) 104 { 105 state->interactive = true; 106 return 0; 107 } 108 109 SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode"); 110 111 int main(int argc, char *argv[]) 112 { 113 struct sandbox_state *state; 114 int err; 115 116 err = state_init(); 117 if (err) 118 return err; 119 120 state = state_get_current(); 121 if (os_parse_args(state, argc, argv)) 122 return 1; 123 124 /* Do pre- and post-relocation init */ 125 board_init_f(0); 126 127 board_init_r(gd->new_gd, 0); 128 129 /* NOTREACHED - board_init_r() does not return */ 130 return 0; 131 } 132