1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * (C) Copyright 2000-2009 4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 5 */ 6 7 /* 8 * Definitions for Command Processor 9 */ 10 #ifndef __COMMAND_H 11 #define __COMMAND_H 12 13 #include <linker_lists.h> 14 15 #ifndef NULL 16 #define NULL 0 17 #endif 18 19 /* Default to a width of 8 characters for help message command width */ 20 #ifndef CONFIG_SYS_HELP_CMD_WIDTH 21 #define CONFIG_SYS_HELP_CMD_WIDTH 8 22 #endif 23 24 #ifndef __ASSEMBLY__ 25 /* 26 * Monitor Command Table 27 */ 28 29 struct cmd_tbl_s { 30 char *name; /* Command Name */ 31 int maxargs; /* maximum number of arguments */ 32 /* 33 * Same as ->cmd() except the command 34 * tells us if it can be repeated. 35 * Replaces the old ->repeatable field 36 * which was not able to make 37 * repeatable property different for 38 * the main command and sub-commands. 39 */ 40 int (*cmd_rep)(struct cmd_tbl_s *cmd, int flags, int argc, 41 char * const argv[], int *repeatable); 42 /* Implementation function */ 43 int (*cmd)(struct cmd_tbl_s *, int, int, char * const []); 44 char *usage; /* Usage message (short) */ 45 #ifdef CONFIG_SYS_LONGHELP 46 char *help; /* Help message (long) */ 47 #endif 48 #ifdef CONFIG_AUTO_COMPLETE 49 /* do auto completion on the arguments */ 50 int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]); 51 #endif 52 }; 53 54 typedef struct cmd_tbl_s cmd_tbl_t; 55 56 57 #if defined(CONFIG_CMD_RUN) 58 extern int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); 59 #endif 60 61 /* common/command.c */ 62 int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int 63 flag, int argc, char * const argv[]); 64 cmd_tbl_t *find_cmd(const char *cmd); 65 cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len); 66 int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc, 67 char * const argv[], char last_char, int maxv, 68 char *cmdv[]); 69 70 extern int cmd_usage(const cmd_tbl_t *cmdtp); 71 72 /* Dummy ->cmd and ->cmd_rep wrappers. */ 73 int cmd_always_repeatable(cmd_tbl_t *cmdtp, int flag, int argc, 74 char * const argv[], int *repeatable); 75 int cmd_never_repeatable(cmd_tbl_t *cmdtp, int flag, int argc, 76 char * const argv[], int *repeatable); 77 int cmd_discard_repeatable(cmd_tbl_t *cmdtp, int flag, int argc, 78 char * const argv[]); 79 80 static inline bool cmd_is_repeatable(cmd_tbl_t *cmdtp) 81 { 82 return cmdtp->cmd_rep == cmd_always_repeatable; 83 } 84 85 #ifdef CONFIG_AUTO_COMPLETE 86 extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]); 87 extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp); 88 #endif 89 90 /** 91 * cmd_process_error() - report and process a possible error 92 * 93 * @cmdtp: Command which caused the error 94 * @err: Error code (0 if none, -ve for error, like -EIO) 95 * @return 0 (CMD_RET_SUCCESS) if there is not error, 96 * 1 (CMD_RET_FAILURE) if an error is found 97 * -1 (CMD_RET_USAGE) if 'usage' error is found 98 */ 99 int cmd_process_error(cmd_tbl_t *cmdtp, int err); 100 101 /* 102 * Monitor Command 103 * 104 * All commands use a common argument format: 105 * 106 * void function (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); 107 */ 108 109 #if defined(CONFIG_CMD_MEMORY) || \ 110 defined(CONFIG_CMD_I2C) || \ 111 defined(CONFIG_CMD_ITEST) || \ 112 defined(CONFIG_CMD_PCI) 113 #define CMD_DATA_SIZE 114 extern int cmd_get_data_size(char* arg, int default_size); 115 #endif 116 117 #ifdef CONFIG_CMD_BOOTD 118 extern int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); 119 #endif 120 #ifdef CONFIG_CMD_BOOTM 121 extern int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); 122 extern int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd); 123 #else 124 static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd) 125 { 126 return 0; 127 } 128 #endif 129 130 extern int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); 131 132 extern int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); 133 134 extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc, 135 char *const argv[]); 136 137 extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); 138 extern int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); 139 140 extern unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc, 141 char * const argv[]); 142 /* 143 * Error codes that commands return to cmd_process(). We use the standard 0 144 * and 1 for success and failure, but add one more case - failure with a 145 * request to call cmd_usage(). But the cmd_process() function handles 146 * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1. 147 * This is just a convenience for commands to avoid them having to call 148 * cmd_usage() all over the place. 149 */ 150 enum command_ret_t { 151 CMD_RET_SUCCESS, /* 0 = Success */ 152 CMD_RET_FAILURE, /* 1 = Failure */ 153 CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */ 154 }; 155 156 /** 157 * Process a command with arguments. We look up the command and execute it 158 * if valid. Otherwise we print a usage message. 159 * 160 * @param flag Some flags normally 0 (see CMD_FLAG_.. above) 161 * @param argc Number of arguments (arg 0 must be the command text) 162 * @param argv Arguments 163 * @param repeatable This function sets this to 0 if the command is not 164 * repeatable. If the command is repeatable, the value 165 * is left unchanged. 166 * @param ticks If ticks is not null, this function set it to the 167 * number of ticks the command took to complete. 168 * @return 0 if the command succeeded, 1 if it failed 169 */ 170 int cmd_process(int flag, int argc, char * const argv[], 171 int *repeatable, unsigned long *ticks); 172 173 void fixup_cmdtable(cmd_tbl_t *cmdtp, int size); 174 175 /** 176 * board_run_command() - Fallback function to execute a command 177 * 178 * When no command line features are enabled in U-Boot, this function is 179 * called to execute a command. Typically the function can look at the 180 * command and perform a few very specific tasks, such as booting the 181 * system in a particular way. 182 * 183 * This function is only used when CONFIG_CMDLINE is not enabled. 184 * 185 * In normal situations this function should not return, since U-Boot will 186 * simply hang. 187 * 188 * @cmdline: Command line string to execute 189 * @return 0 if OK, 1 for error 190 */ 191 int board_run_command(const char *cmdline); 192 #endif /* __ASSEMBLY__ */ 193 194 /* 195 * Command Flags: 196 */ 197 #define CMD_FLAG_REPEAT 0x0001 /* repeat last command */ 198 #define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */ 199 #define CMD_FLAG_ENV 0x0004 /* command is from the environment */ 200 201 #ifdef CONFIG_AUTO_COMPLETE 202 # define _CMD_COMPLETE(x) x, 203 #else 204 # define _CMD_COMPLETE(x) 205 #endif 206 #ifdef CONFIG_SYS_LONGHELP 207 # define _CMD_HELP(x) x, 208 #else 209 # define _CMD_HELP(x) 210 #endif 211 212 #ifdef CONFIG_CMDLINE 213 #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \ 214 _usage, _help, _comp) \ 215 { #_name, _maxargs, _cmd_rep, cmd_discard_repeatable, \ 216 _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) } 217 218 #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \ 219 _usage, _help, _comp) \ 220 { #_name, _maxargs, \ 221 _rep ? cmd_always_repeatable : cmd_never_repeatable, \ 222 _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) } 223 224 #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \ 225 ll_entry_declare(cmd_tbl_t, _name, cmd) = \ 226 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \ 227 _usage, _help, _comp); 228 229 #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \ 230 _help, _comp) \ 231 ll_entry_declare(cmd_tbl_t, _name, cmd) = \ 232 U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \ 233 _usage, _help, _comp) 234 235 #else 236 #define U_BOOT_SUBCMD_START(name) static cmd_tbl_t name[] = {}; 237 #define U_BOOT_SUBCMD_END 238 239 #define _CMD_REMOVE(_name, _cmd) \ 240 int __remove_ ## _name(void) \ 241 { \ 242 if (0) \ 243 _cmd(NULL, 0, 0, NULL); \ 244 return 0; \ 245 } 246 247 #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \ 248 _usage, _help, _comp) \ 249 { #_name, _maxargs, 0 ? _cmd_rep : NULL, NULL, _usage, \ 250 _CMD_HELP(_help) _CMD_COMPLETE(_comp) } 251 252 #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \ 253 _help, _comp) \ 254 { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \ 255 _CMD_HELP(_help) _CMD_COMPLETE(_comp) } 256 257 #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \ 258 _comp) \ 259 _CMD_REMOVE(sub_ ## _name, _cmd) 260 261 #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \ 262 _help, _comp) \ 263 _CMD_REMOVE(sub_ ## _name, _cmd_rep) 264 265 #endif /* CONFIG_CMDLINE */ 266 267 #define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \ 268 U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL) 269 270 #define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \ 271 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \ 272 _usage, _help, NULL) 273 274 #endif /* __COMMAND_H */ 275