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 *cmd, int flags, int argc,
42 char *const argv[], int *repeatable);
43 /* Implementation function */
44 int (*cmd)(struct cmd_tbl *cmd, int flags, int argc,
46 char *usage; /* Usage message (short) */
47 #ifdef CONFIG_SYS_LONGHELP
48 char *help; /* Help message (long) */
50 #ifdef CONFIG_AUTO_COMPLETE
51 /* do auto completion on the arguments */
52 int (*complete)(int argc, char *const argv[],
53 char last_char, int maxv, char *cmdv[]);
57 #if defined(CONFIG_CMD_RUN)
58 extern int do_run(struct cmd_tbl *cmdtp, int flag, int argc,
62 /* common/command.c */
63 int _do_help(struct cmd_tbl *cmd_start, int cmd_items, struct cmd_tbl *cmdtp,
64 int flag, int argc, char *const argv[]);
65 struct cmd_tbl *find_cmd(const char *cmd);
66 struct cmd_tbl *find_cmd_tbl(const char *cmd, struct cmd_tbl *table,
68 int complete_subcmdv(struct cmd_tbl *cmdtp, int count, int argc,
69 char *const argv[], char last_char, int maxv,
72 extern int cmd_usage(const struct cmd_tbl *cmdtp);
74 /* Dummy ->cmd and ->cmd_rep wrappers. */
75 int cmd_always_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
76 char *const argv[], int *repeatable);
77 int cmd_never_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
78 char *const argv[], int *repeatable);
79 int cmd_discard_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
82 static inline bool cmd_is_repeatable(struct cmd_tbl *cmdtp)
84 return cmdtp->cmd_rep == cmd_always_repeatable;
87 #ifdef CONFIG_AUTO_COMPLETE
88 extern int var_complete(int argc, char *const argv[], char last_char, int maxv,
90 extern int cmd_auto_complete(const char *const prompt, char *buf, int *np,
95 * cmd_process_error() - report and process a possible error
97 * @cmdtp: Command which caused the error
98 * @err: Error code (0 if none, -ve for error, like -EIO)
99 * @return 0 (CMD_RET_SUCCESS) if there is not error,
100 * 1 (CMD_RET_FAILURE) if an error is found
101 * -1 (CMD_RET_USAGE) if 'usage' error is found
103 int cmd_process_error(struct cmd_tbl *cmdtp, int err);
108 * All commands use a common argument format:
110 * void function(struct cmd_tbl *cmdtp, int flag, int argc,
111 * char *const argv[]);
114 #if defined(CONFIG_CMD_MEMORY) || \
115 defined(CONFIG_CMD_I2C) || \
116 defined(CONFIG_CMD_ITEST) || \
117 defined(CONFIG_CMD_PCI) || \
118 defined(CONFIG_CMD_SETEXPR)
119 #define CMD_DATA_SIZE
120 #define CMD_DATA_SIZE_ERR (-1)
121 #define CMD_DATA_SIZE_STR (-2)
124 * cmd_get_data_size() - Get the data-size specifier from a command
126 * This reads a '.x' size specifier appended to a command. For example 'md.b'
127 * is the 'md' command with a '.b' specifier, meaning that the command should
130 * Valid characters are:
138 * @arg: Pointers to the command to check. If a valid specifier is present it
139 * will be the last character of the string, following a '.'
140 * @default_size: Default size to return if there is no specifier
141 * @return data size in bytes (1, 2, 4, 8) or CMD_DATA_SIZE_ERR for an invalid
142 * character, or CMD_DATA_SIZE_STR for a string
144 int cmd_get_data_size(char *arg, int default_size);
147 #ifdef CONFIG_CMD_BOOTD
148 extern int do_bootd(struct cmd_tbl *cmdtp, int flag, int argc,
151 #ifdef CONFIG_CMD_BOOTM
152 extern int do_bootm(struct cmd_tbl *cmdtp, int flag, int argc,
154 extern int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd);
156 static inline int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd)
162 extern int do_bootz(struct cmd_tbl *cmdtp, int flag, int argc,
165 extern int do_booti(struct cmd_tbl *cmdtp, int flag, int argc,
168 extern int common_diskboot(struct cmd_tbl *cmdtp, const char *intf, int argc,
171 extern int do_reset(struct cmd_tbl *cmdtp, int flag, int argc,
173 extern int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc,
176 extern unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
179 #if defined(CONFIG_CMD_NVEDIT_EFI)
180 extern int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
182 extern int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
187 * Error codes that commands return to cmd_process(). We use the standard 0
188 * and 1 for success and failure, but add one more case - failure with a
189 * request to call cmd_usage(). But the cmd_process() function handles
190 * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
191 * This is just a convenience for commands to avoid them having to call
192 * cmd_usage() all over the place.
195 CMD_RET_SUCCESS, /* 0 = Success */
196 CMD_RET_FAILURE, /* 1 = Failure */
197 CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
201 * Process a command with arguments. We look up the command and execute it
202 * if valid. Otherwise we print a usage message.
204 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
205 * @param argc Number of arguments (arg 0 must be the command text)
206 * @param argv Arguments
207 * @param repeatable This function sets this to 0 if the command is not
208 * repeatable. If the command is repeatable, the value
210 * @param ticks If ticks is not null, this function set it to the
211 * number of ticks the command took to complete.
212 * @return 0 if the command succeeded, 1 if it failed
214 int cmd_process(int flag, int argc, char *const argv[], int *repeatable,
215 unsigned long *ticks);
217 void fixup_cmdtable(struct cmd_tbl *cmdtp, int size);
220 * board_run_command() - Fallback function to execute a command
222 * When no command line features are enabled in U-Boot, this function is
223 * called to execute a command. Typically the function can look at the
224 * command and perform a few very specific tasks, such as booting the
225 * system in a particular way.
227 * This function is only used when CONFIG_CMDLINE is not enabled.
229 * In normal situations this function should not return, since U-Boot will
232 * @cmdline: Command line string to execute
233 * @return 0 if OK, 1 for error
235 int board_run_command(const char *cmdline);
237 int run_command(const char *cmd, int flag);
238 int run_command_repeatable(const char *cmd, int flag);
241 * Run a list of commands separated by ; or even \0
243 * Note that if 'len' is not -1, then the command does not need to be nul
244 * terminated, Memory will be allocated for the command in that case.
246 * @param cmd List of commands to run, each separated bu semicolon
247 * @param len Length of commands excluding terminator if known (-1 if not)
248 * @param flag Execution flags (CMD_FLAG_...)
249 * @return 0 on success, or != 0 on error.
251 int run_command_list(const char *cmd, int len, int flag);
252 #endif /* __ASSEMBLY__ */
257 #define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
258 #define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
259 #define CMD_FLAG_ENV 0x0004 /* command is from the environment */
261 #ifdef CONFIG_AUTO_COMPLETE
262 # define _CMD_COMPLETE(x) x,
264 # define _CMD_COMPLETE(x)
266 #ifdef CONFIG_SYS_LONGHELP
267 # define _CMD_HELP(x) x,
269 # define _CMD_HELP(x)
272 #ifdef CONFIG_NEEDS_MANUAL_RELOC
273 #define U_BOOT_SUBCMDS_RELOC(_cmdname) \
274 static void _cmdname##_subcmds_reloc(void) \
276 static int relocated; \
281 fixup_cmdtable(_cmdname##_subcmds, \
282 ARRAY_SIZE(_cmdname##_subcmds)); \
286 #define U_BOOT_SUBCMDS_RELOC(_cmdname) \
287 static void _cmdname##_subcmds_reloc(void) { }
290 #define U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
291 static int do_##_cmdname(struct cmd_tbl *cmdtp, int flag, \
292 int argc, char *const argv[], \
295 struct cmd_tbl *subcmd; \
297 _cmdname##_subcmds_reloc(); \
299 /* We need at least the cmd and subcmd names. */ \
300 if (argc < 2 || argc > CONFIG_SYS_MAXARGS) \
301 return CMD_RET_USAGE; \
303 subcmd = find_cmd_tbl(argv[1], _cmdname##_subcmds, \
304 ARRAY_SIZE(_cmdname##_subcmds)); \
305 if (!subcmd || argc - 1 > subcmd->maxargs) \
306 return CMD_RET_USAGE; \
308 if (flag == CMD_FLAG_REPEAT && \
309 !cmd_is_repeatable(subcmd)) \
310 return CMD_RET_SUCCESS; \
312 return subcmd->cmd_rep(subcmd, flag, argc - 1, \
313 argv + 1, repeatable); \
316 #ifdef CONFIG_AUTO_COMPLETE
317 #define U_BOOT_SUBCMDS_COMPLETE(_cmdname) \
318 static int complete_##_cmdname(int argc, char *const argv[], \
319 char last_char, int maxv, \
322 return complete_subcmdv(_cmdname##_subcmds, \
323 ARRAY_SIZE(_cmdname##_subcmds), \
324 argc - 1, argv + 1, last_char, \
328 #define U_BOOT_SUBCMDS_COMPLETE(_cmdname)
331 #define U_BOOT_SUBCMDS(_cmdname, ...) \
332 static struct cmd_tbl _cmdname##_subcmds[] = { __VA_ARGS__ }; \
333 U_BOOT_SUBCMDS_RELOC(_cmdname) \
334 U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
335 U_BOOT_SUBCMDS_COMPLETE(_cmdname)
337 #ifdef CONFIG_CMDLINE
338 #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
339 _usage, _help, _comp) \
340 { #_name, _maxargs, _cmd_rep, cmd_discard_repeatable, \
341 _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
343 #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
344 _usage, _help, _comp) \
345 { #_name, _maxargs, \
346 _rep ? cmd_always_repeatable : cmd_never_repeatable, \
347 _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
349 #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
350 ll_entry_declare(struct cmd_tbl, _name, cmd) = \
351 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
352 _usage, _help, _comp);
354 #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
356 ll_entry_declare(struct cmd_tbl, _name, cmd) = \
357 U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
358 _usage, _help, _comp)
361 #define U_BOOT_SUBCMD_START(name) static struct cmd_tbl name[] = {};
362 #define U_BOOT_SUBCMD_END
364 #define _CMD_REMOVE(_name, _cmd) \
365 int __remove_ ## _name(void) \
368 _cmd(NULL, 0, 0, NULL); \
372 #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
373 _usage, _help, _comp) \
374 { #_name, _maxargs, 0 ? _cmd_rep : NULL, NULL, _usage, \
375 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
377 #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
379 { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \
380 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
382 #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
384 _CMD_REMOVE(sub_ ## _name, _cmd)
386 #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
388 _CMD_REMOVE(sub_ ## _name, _cmd_rep)
390 #endif /* CONFIG_CMDLINE */
392 #define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
393 U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
395 #define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
396 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
399 #define U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
401 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
404 #define U_BOOT_SUBCMD_MKENT(_name, _maxargs, _rep, _do_cmd) \
405 U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
408 #define U_BOOT_CMD_WITH_SUBCMDS(_name, _usage, _help, ...) \
409 U_BOOT_SUBCMDS(_name, __VA_ARGS__) \
410 U_BOOT_CMDREP_COMPLETE(_name, CONFIG_SYS_MAXARGS, do_##_name, \
411 _usage, _help, complete_##_name)
413 #endif /* __COMMAND_H */