1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * Definitions for Command Processor
14 #include <linker_lists.h>
20 /* Default to a width of 8 characters for help message command width */
21 #ifndef CONFIG_SYS_HELP_CMD_WIDTH
22 #define CONFIG_SYS_HELP_CMD_WIDTH 10
27 * Monitor Command Table
31 char *name; /* Command Name */
32 int maxargs; /* maximum number of arguments */
34 * Same as ->cmd() except the command
35 * tells us if it can be repeated.
36 * Replaces the old ->repeatable field
37 * which was not able to make
38 * repeatable property different for
39 * the main command and sub-commands.
41 int (*cmd_rep)(struct cmd_tbl_s *cmd, int flags, int argc,
42 char * const argv[], int *repeatable);
43 /* Implementation function */
44 int (*cmd)(struct cmd_tbl_s *, int, int, char * const []);
45 char *usage; /* Usage message (short) */
46 #ifdef CONFIG_SYS_LONGHELP
47 char *help; /* Help message (long) */
49 #ifdef CONFIG_AUTO_COMPLETE
50 /* do auto completion on the arguments */
51 int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
55 typedef struct cmd_tbl_s cmd_tbl_t;
58 #if defined(CONFIG_CMD_RUN)
59 extern int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
62 /* common/command.c */
63 int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
64 flag, int argc, char * const argv[]);
65 cmd_tbl_t *find_cmd(const char *cmd);
66 cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);
67 int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc,
68 char * const argv[], char last_char, int maxv,
71 extern int cmd_usage(const cmd_tbl_t *cmdtp);
73 /* Dummy ->cmd and ->cmd_rep wrappers. */
74 int cmd_always_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
75 char * const argv[], int *repeatable);
76 int cmd_never_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
77 char * const argv[], int *repeatable);
78 int cmd_discard_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
81 static inline bool cmd_is_repeatable(cmd_tbl_t *cmdtp)
83 return cmdtp->cmd_rep == cmd_always_repeatable;
86 #ifdef CONFIG_AUTO_COMPLETE
87 extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
88 extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp);
92 * cmd_process_error() - report and process a possible error
94 * @cmdtp: Command which caused the error
95 * @err: Error code (0 if none, -ve for error, like -EIO)
96 * @return 0 (CMD_RET_SUCCESS) if there is not error,
97 * 1 (CMD_RET_FAILURE) if an error is found
98 * -1 (CMD_RET_USAGE) if 'usage' error is found
100 int cmd_process_error(cmd_tbl_t *cmdtp, int err);
105 * All commands use a common argument format:
107 * void function (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
110 #if defined(CONFIG_CMD_MEMORY) || \
111 defined(CONFIG_CMD_I2C) || \
112 defined(CONFIG_CMD_ITEST) || \
113 defined(CONFIG_CMD_PCI) || \
114 defined(CONFIG_CMD_SETEXPR)
115 #define CMD_DATA_SIZE
116 extern int cmd_get_data_size(char* arg, int default_size);
119 #ifdef CONFIG_CMD_BOOTD
120 extern int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
122 #ifdef CONFIG_CMD_BOOTM
123 extern int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
124 extern int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd);
126 static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
132 extern int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
134 extern int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
136 extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
139 extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
140 extern int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
142 extern unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
143 char * const argv[]);
145 #if defined(CONFIG_CMD_NVEDIT_EFI)
146 extern int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc,
147 char * const argv[]);
148 extern int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc,
149 char * const argv[]);
153 * Error codes that commands return to cmd_process(). We use the standard 0
154 * and 1 for success and failure, but add one more case - failure with a
155 * request to call cmd_usage(). But the cmd_process() function handles
156 * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
157 * This is just a convenience for commands to avoid them having to call
158 * cmd_usage() all over the place.
161 CMD_RET_SUCCESS, /* 0 = Success */
162 CMD_RET_FAILURE, /* 1 = Failure */
163 CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
167 * Process a command with arguments. We look up the command and execute it
168 * if valid. Otherwise we print a usage message.
170 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
171 * @param argc Number of arguments (arg 0 must be the command text)
172 * @param argv Arguments
173 * @param repeatable This function sets this to 0 if the command is not
174 * repeatable. If the command is repeatable, the value
176 * @param ticks If ticks is not null, this function set it to the
177 * number of ticks the command took to complete.
178 * @return 0 if the command succeeded, 1 if it failed
180 int cmd_process(int flag, int argc, char * const argv[],
181 int *repeatable, unsigned long *ticks);
183 void fixup_cmdtable(cmd_tbl_t *cmdtp, int size);
186 * board_run_command() - Fallback function to execute a command
188 * When no command line features are enabled in U-Boot, this function is
189 * called to execute a command. Typically the function can look at the
190 * command and perform a few very specific tasks, such as booting the
191 * system in a particular way.
193 * This function is only used when CONFIG_CMDLINE is not enabled.
195 * In normal situations this function should not return, since U-Boot will
198 * @cmdline: Command line string to execute
199 * @return 0 if OK, 1 for error
201 int board_run_command(const char *cmdline);
203 int run_command(const char *cmd, int flag);
204 int run_command_repeatable(const char *cmd, int flag);
207 * Run a list of commands separated by ; or even \0
209 * Note that if 'len' is not -1, then the command does not need to be nul
210 * terminated, Memory will be allocated for the command in that case.
212 * @param cmd List of commands to run, each separated bu semicolon
213 * @param len Length of commands excluding terminator if known (-1 if not)
214 * @param flag Execution flags (CMD_FLAG_...)
215 * @return 0 on success, or != 0 on error.
217 int run_command_list(const char *cmd, int len, int flag);
218 #endif /* __ASSEMBLY__ */
223 #define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
224 #define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
225 #define CMD_FLAG_ENV 0x0004 /* command is from the environment */
227 #ifdef CONFIG_AUTO_COMPLETE
228 # define _CMD_COMPLETE(x) x,
230 # define _CMD_COMPLETE(x)
232 #ifdef CONFIG_SYS_LONGHELP
233 # define _CMD_HELP(x) x,
235 # define _CMD_HELP(x)
238 #ifdef CONFIG_NEEDS_MANUAL_RELOC
239 #define U_BOOT_SUBCMDS_RELOC(_cmdname) \
240 static void _cmdname##_subcmds_reloc(void) \
242 static int relocated; \
247 fixup_cmdtable(_cmdname##_subcmds, \
248 ARRAY_SIZE(_cmdname##_subcmds)); \
252 #define U_BOOT_SUBCMDS_RELOC(_cmdname) \
253 static void _cmdname##_subcmds_reloc(void) { }
256 #define U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
257 static int do_##_cmdname(cmd_tbl_t *cmdtp, int flag, int argc, \
258 char * const argv[], int *repeatable) \
262 _cmdname##_subcmds_reloc(); \
264 /* We need at least the cmd and subcmd names. */ \
265 if (argc < 2 || argc > CONFIG_SYS_MAXARGS) \
266 return CMD_RET_USAGE; \
268 subcmd = find_cmd_tbl(argv[1], _cmdname##_subcmds, \
269 ARRAY_SIZE(_cmdname##_subcmds)); \
270 if (!subcmd || argc - 1 > subcmd->maxargs) \
271 return CMD_RET_USAGE; \
273 if (flag == CMD_FLAG_REPEAT && \
274 !cmd_is_repeatable(subcmd)) \
275 return CMD_RET_SUCCESS; \
277 return subcmd->cmd_rep(subcmd, flag, argc - 1, \
278 argv + 1, repeatable); \
281 #ifdef CONFIG_AUTO_COMPLETE
282 #define U_BOOT_SUBCMDS_COMPLETE(_cmdname) \
283 static int complete_##_cmdname(int argc, char * const argv[], \
284 char last_char, int maxv, \
287 return complete_subcmdv(_cmdname##_subcmds, \
288 ARRAY_SIZE(_cmdname##_subcmds), \
289 argc - 1, argv + 1, last_char, \
293 #define U_BOOT_SUBCMDS_COMPLETE(_cmdname)
296 #define U_BOOT_SUBCMDS(_cmdname, ...) \
297 static cmd_tbl_t _cmdname##_subcmds[] = { __VA_ARGS__ }; \
298 U_BOOT_SUBCMDS_RELOC(_cmdname) \
299 U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
300 U_BOOT_SUBCMDS_COMPLETE(_cmdname)
302 #ifdef CONFIG_CMDLINE
303 #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
304 _usage, _help, _comp) \
305 { #_name, _maxargs, _cmd_rep, cmd_discard_repeatable, \
306 _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
308 #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
309 _usage, _help, _comp) \
310 { #_name, _maxargs, \
311 _rep ? cmd_always_repeatable : cmd_never_repeatable, \
312 _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
314 #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
315 ll_entry_declare(cmd_tbl_t, _name, cmd) = \
316 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
317 _usage, _help, _comp);
319 #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
321 ll_entry_declare(cmd_tbl_t, _name, cmd) = \
322 U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
323 _usage, _help, _comp)
326 #define U_BOOT_SUBCMD_START(name) static cmd_tbl_t name[] = {};
327 #define U_BOOT_SUBCMD_END
329 #define _CMD_REMOVE(_name, _cmd) \
330 int __remove_ ## _name(void) \
333 _cmd(NULL, 0, 0, NULL); \
337 #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
338 _usage, _help, _comp) \
339 { #_name, _maxargs, 0 ? _cmd_rep : NULL, NULL, _usage, \
340 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
342 #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
344 { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \
345 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
347 #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
349 _CMD_REMOVE(sub_ ## _name, _cmd)
351 #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
353 _CMD_REMOVE(sub_ ## _name, _cmd_rep)
355 #endif /* CONFIG_CMDLINE */
357 #define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
358 U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
360 #define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
361 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
364 #define U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
366 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
369 #define U_BOOT_SUBCMD_MKENT(_name, _maxargs, _rep, _do_cmd) \
370 U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
373 #define U_BOOT_CMD_WITH_SUBCMDS(_name, _usage, _help, ...) \
374 U_BOOT_SUBCMDS(_name, __VA_ARGS__) \
375 U_BOOT_CMDREP_COMPLETE(_name, CONFIG_SYS_MAXARGS, do_##_name, \
376 _usage, _help, complete_##_name)
378 #endif /* __COMMAND_H */