]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - include/command.h
4158ca11b0efdf962a45ce394a0be959fb4a01cc
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>
16 #include <linux/compiler_attributes.h>
22 /* Default to a width of 8 characters for help message command width */
23 #ifndef CFG_SYS_HELP_CMD_WIDTH
24 #define CFG_SYS_HELP_CMD_WIDTH 10
29 /* For ARRAY_SIZE() */
30 #include <linux/kernel.h>
33 * Monitor Command Table
37 char *name
; /* Command Name */
38 int maxargs
; /* maximum number of arguments */
40 * Same as ->cmd() except the command
41 * tells us if it can be repeated.
42 * Replaces the old ->repeatable field
43 * which was not able to make
44 * repeatable property different for
45 * the main command and sub-commands.
47 int (*cmd_rep
)(struct cmd_tbl
*cmd
, int flags
, int argc
,
48 char *const argv
[], int *repeatable
);
49 /* Implementation function */
50 int (*cmd
)(struct cmd_tbl
*cmd
, int flags
, int argc
,
52 char *usage
; /* Usage message (short) */
53 #ifdef CONFIG_SYS_LONGHELP
54 const char *help
; /* Help message (long) */
56 #ifdef CONFIG_AUTO_COMPLETE
57 /* do auto completion on the arguments */
58 int (*complete
)(int argc
, char *const argv
[],
59 char last_char
, int maxv
, char *cmdv
[]);
64 * cmd_arg_get() - Get a particular argument
66 * @argc: Number of arguments
67 * @argv: Argument vector of length @argc
68 * @argnum: Argument to get (0=first)
69 * Return: Pointer to argument @argnum if it exists, else NULL
71 static inline const char *cmd_arg_get(int argc
, char *const argv
[], int argnum
)
73 return argc
> argnum
? argv
[argnum
] : NULL
;
76 static inline const char *cmd_arg0(int argc
, char *const argv
[])
78 return cmd_arg_get(argc
, argv
, 0);
81 static inline const char *cmd_arg1(int argc
, char *const argv
[])
83 return cmd_arg_get(argc
, argv
, 1);
86 static inline const char *cmd_arg2(int argc
, char *const argv
[])
88 return cmd_arg_get(argc
, argv
, 2);
91 static inline const char *cmd_arg3(int argc
, char *const argv
[])
93 return cmd_arg_get(argc
, argv
, 3);
96 #if defined(CONFIG_CMD_RUN)
97 int do_run(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
101 /* common/command.c */
102 int _do_help(struct cmd_tbl
*cmd_start
, int cmd_items
, struct cmd_tbl
*cmdtp
,
103 int flag
, int argc
, char *const argv
[]);
104 struct cmd_tbl
*find_cmd(const char *cmd
);
105 struct cmd_tbl
*find_cmd_tbl(const char *cmd
, struct cmd_tbl
*table
,
107 int complete_subcmdv(struct cmd_tbl
*cmdtp
, int count
, int argc
,
108 char *const argv
[], char last_char
, int maxv
,
111 int cmd_usage(const struct cmd_tbl
*cmdtp
);
113 /* Dummy ->cmd and ->cmd_rep wrappers. */
114 int cmd_always_repeatable(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
115 char *const argv
[], int *repeatable
);
116 int cmd_never_repeatable(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
117 char *const argv
[], int *repeatable
);
118 int cmd_discard_repeatable(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
121 static inline bool cmd_is_repeatable(struct cmd_tbl
*cmdtp
)
123 return cmdtp
->cmd_rep
== cmd_always_repeatable
;
126 #ifdef CONFIG_AUTO_COMPLETE
127 int var_complete(int argc
, char *const argv
[], char last_char
, int maxv
,
129 int cmd_auto_complete(const char *const prompt
, char *buf
, int *np
,
132 static inline int cmd_auto_complete(const char *const prompt
, char *buf
,
140 * cmd_process_error() - report and process a possible error
142 * @cmdtp: Command which caused the error
143 * @err: Error code (0 if none, -ve for error, like -EIO)
144 * Return: 0 (CMD_RET_SUCCESS) if there is not error,
145 * 1 (CMD_RET_FAILURE) if an error is found
146 * -1 (CMD_RET_USAGE) if 'usage' error is found
148 int cmd_process_error(struct cmd_tbl
*cmdtp
, int err
);
153 * All commands use a common argument format:
155 * void function(struct cmd_tbl *cmdtp, int flag, int argc,
156 * char *const argv[]);
159 #if defined(CONFIG_CMD_MEMORY) || \
160 defined(CONFIG_CMD_I2C) || \
161 defined(CONFIG_CMD_ITEST) || \
162 defined(CONFIG_CMD_PCI) || \
163 defined(CONFIG_CMD_SETEXPR)
164 #define CMD_DATA_SIZE
165 #define CMD_DATA_SIZE_ERR (-1)
166 #define CMD_DATA_SIZE_STR (-2)
169 * cmd_get_data_size() - Get the data-size specifier from a command
171 * This reads a '.x' size specifier appended to a command. For example 'md.b'
172 * is the 'md' command with a '.b' specifier, meaning that the command should
175 * Valid characters are:
183 * @arg: Pointers to the command to check. If a valid specifier is present it
184 * will be the last character of the string, following a '.'
185 * @default_size: Default size to return if there is no specifier
186 * Return: data size in bytes (1, 2, 4, 8) or CMD_DATA_SIZE_ERR for an invalid
187 * character, or CMD_DATA_SIZE_STR for a string
189 int cmd_get_data_size(const char *arg
, int default_size
);
192 #ifdef CONFIG_CMD_BOOTD
193 int do_bootd(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
196 int do_bootm(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
198 #ifdef CONFIG_CMD_BOOTM
199 int bootm_maybe_autostart(struct cmd_tbl
*cmdtp
, const char *cmd
);
201 static inline int bootm_maybe_autostart(struct cmd_tbl
*cmdtp
, const char *cmd
)
207 int do_bootz(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
210 int do_booti(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
213 int do_zboot_parent(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
214 char *const argv
[], int *repeatable
);
216 int common_diskboot(struct cmd_tbl
*cmdtp
, const char *intf
, int argc
,
219 int do_reset(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
221 int do_poweroff(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
224 unsigned long do_go_exec(ulong (*entry
)(int, char * const []), int argc
,
227 #if defined(CONFIG_CMD_NVEDIT_EFI)
228 int do_env_print_efi(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
230 int do_env_set_efi(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
235 * setexpr_regex_sub() - Replace a regex pattern with a string
237 * @data: Buffer containing the string to update
238 * @data_size: Size of buffer (must be large enough for the new string)
239 * @nbuf: Back-reference buffer
240 * @nbuf_size: Size of back-reference buffer (must be larger enough for @s plus
241 * all back-reference expansions)
242 * @r: Regular expression to find
243 * @s: String to replace with
244 * @global: true to replace all matches in @data, false to replace just the
246 * Return: 0 if OK, 1 on error
248 int setexpr_regex_sub(char *data
, uint data_size
, char *nbuf
, uint nbuf_size
,
249 const char *r
, const char *s
, bool global
);
252 * Error codes that commands return to cmd_process(). We use the standard 0
253 * and 1 for success and failure, but add one more case - failure with a
254 * request to call cmd_usage(). But the cmd_process() function handles
255 * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
256 * This is just a convenience for commands to avoid them having to call
257 * cmd_usage() all over the place.
260 CMD_RET_SUCCESS
, /* 0 = Success */
261 CMD_RET_FAILURE
, /* 1 = Failure */
262 CMD_RET_USAGE
= -1, /* Failure, please report 'usage' error */
266 * Process a command with arguments. We look up the command and execute it
267 * if valid. Otherwise we print a usage message.
269 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
270 * @param argc Number of arguments (arg 0 must be the command text)
271 * @param argv Arguments
272 * @param repeatable This function sets this to 0 if the command is not
273 * repeatable. If the command is repeatable, the value
275 * @param ticks If ticks is not null, this function set it to the
276 * number of ticks the command took to complete.
277 * Return: 0 if command succeeded, else non-zero (CMD_RET_...)
279 enum command_ret_t
cmd_process(int flag
, int argc
, char *const argv
[],
280 int *repeatable
, unsigned long *ticks
);
282 void fixup_cmdtable(struct cmd_tbl
*cmdtp
, int size
);
285 * board_run_command() - Fallback function to execute a command
287 * When no command line features are enabled in U-Boot, this function is
288 * called to execute a command. Typically the function can look at the
289 * command and perform a few very specific tasks, such as booting the
290 * system in a particular way.
292 * This function is only used when CONFIG_CMDLINE is not enabled.
294 * In normal situations this function should not return, since U-Boot will
297 * @cmdline: Command line string to execute
298 * Return: 0 if OK, 1 for error
300 int board_run_command(const char *cmdline
);
302 int run_command(const char *cmd
, int flag
);
303 int run_command_repeatable(const char *cmd
, int flag
);
306 * run_commandf() - Run a command created by a format string
308 * @fmt: printf() format string
309 * @...: Arguments to use (flag is always 0)
311 * The command cannot be larger than (CONFIG_SYS_CBSIZE - 1) characters.
314 * Returns 0 on success, -EIO if internal output error occurred, -ENOSPC in
315 * case of 'fmt' string truncation, or != 0 on error, specific for
318 int run_commandf(const char *fmt
, ...) __printf(1, 2);
321 * Run a list of commands separated by ; or even \0
323 * Note that if 'len' is not -1, then the command does not need to be nul
324 * terminated, Memory will be allocated for the command in that case.
326 * @param cmd List of commands to run, each separated bu semicolon
327 * @param len Length of commands excluding terminator if known (-1 if not)
328 * @param flag Execution flags (CMD_FLAG_...)
329 * Return: 0 on success, or != 0 on error.
331 int run_command_list(const char *cmd
, int len
, int flag
);
334 * cmd_source_script() - Execute a script
336 * Executes a U-Boot script at a particular address in memory. The script should
337 * have a header (FIT or legacy) with the script type (IH_TYPE_SCRIPT).
339 * @addr: Address of script
340 * @fit_uname: FIT subimage name
341 * Return: result code (enum command_ret_t)
343 int cmd_source_script(ulong addr
, const char *fit_uname
, const char *confname
);
344 #endif /* __ASSEMBLY__ */
349 #define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
350 #define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
351 #define CMD_FLAG_ENV 0x0004 /* command is from the environment */
353 #ifdef CONFIG_AUTO_COMPLETE
354 # define _CMD_COMPLETE(x) x,
356 # define _CMD_COMPLETE(x)
358 #ifdef CONFIG_SYS_LONGHELP
359 # define _CMD_HELP(x) x,
361 # define _CMD_HELP(x)
364 #define U_BOOT_LONGHELP(_cmdname, text) \
365 static __maybe_unused const char _cmdname##_help_text[] = text
367 #define U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
368 static int do_##_cmdname(struct cmd_tbl *cmdtp, int flag, \
369 int argc, char *const argv[], \
372 struct cmd_tbl *subcmd; \
374 /* We need at least the cmd and subcmd names. */ \
375 if (argc < 2 || argc > CONFIG_SYS_MAXARGS) \
376 return CMD_RET_USAGE; \
378 subcmd = find_cmd_tbl(argv[1], _cmdname##_subcmds, \
379 ARRAY_SIZE(_cmdname##_subcmds)); \
380 if (!subcmd || argc - 1 > subcmd->maxargs) \
381 return CMD_RET_USAGE; \
383 if (flag == CMD_FLAG_REPEAT && \
384 !cmd_is_repeatable(subcmd)) \
385 return CMD_RET_SUCCESS; \
387 return subcmd->cmd_rep(subcmd, flag, argc - 1, \
388 argv + 1, repeatable); \
391 #ifdef CONFIG_AUTO_COMPLETE
392 #define U_BOOT_SUBCMDS_COMPLETE(_cmdname) \
393 static int complete_##_cmdname(int argc, char *const argv[], \
394 char last_char, int maxv, \
397 return complete_subcmdv(_cmdname##_subcmds, \
398 ARRAY_SIZE(_cmdname##_subcmds), \
399 argc - 1, argv + 1, last_char, \
403 #define U_BOOT_SUBCMDS_COMPLETE(_cmdname)
406 #define U_BOOT_SUBCMDS(_cmdname, ...) \
407 static struct cmd_tbl _cmdname##_subcmds[] = { __VA_ARGS__ }; \
408 U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
409 U_BOOT_SUBCMDS_COMPLETE(_cmdname)
411 #if CONFIG_IS_ENABLED(CMDLINE)
412 #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
413 _usage, _help, _comp) \
414 { #_name, _maxargs, _cmd_rep, cmd_discard_repeatable, \
415 _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
417 #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
418 _usage, _help, _comp) \
419 { #_name, _maxargs, \
420 _rep ? cmd_always_repeatable : cmd_never_repeatable, \
421 _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
423 #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
424 ll_entry_declare(struct cmd_tbl, _name, cmd) = \
425 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
426 _usage, _help, _comp)
428 #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
430 ll_entry_declare(struct cmd_tbl, _name, cmd) = \
431 U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
432 _usage, _help, _comp)
435 #define U_BOOT_SUBCMD_START(name) static struct cmd_tbl name[] = {};
436 #define U_BOOT_SUBCMD_END
438 #define _CMD_REMOVE(_name, _cmd) \
439 int __remove_ ## _name(void) \
442 _cmd(NULL, 0, 0, NULL); \
446 #define _CMD_REMOVE_REP(_name, _cmd) \
447 int __remove_ ## _name(void) \
450 _cmd(NULL, 0, 0, NULL, NULL); \
454 #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
455 _usage, _help, _comp) \
456 { #_name, _maxargs, 0 ? _cmd_rep : NULL, NULL, _usage, \
457 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
459 #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
461 { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \
462 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
464 #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
466 _CMD_REMOVE(sub_ ## _name, _cmd)
468 #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
470 _CMD_REMOVE_REP(sub_ ## _name, _cmd_rep)
472 #endif /* CONFIG_CMDLINE */
474 #define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
475 U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
477 #define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
478 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
481 #define U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
483 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
486 #define U_BOOT_SUBCMD_MKENT(_name, _maxargs, _rep, _do_cmd) \
487 U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
490 #define U_BOOT_CMD_WITH_SUBCMDS(_name, _usage, _help, ...) \
491 U_BOOT_SUBCMDS(_name, __VA_ARGS__) \
492 U_BOOT_CMDREP_COMPLETE(_name, CONFIG_SYS_MAXARGS, do_##_name, \
493 _usage, _help, complete_##_name)
495 #endif /* __COMMAND_H */