blob: 4158ca11b0efdf962a45ce394a0be959fb4a01cc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
wdenk78f66222002-08-27 10:27:51 +00002/*
Detlev Zundel5e6c06b2009-03-25 17:27:52 +01003 * (C) Copyright 2000-2009
wdenk78f66222002-08-27 10:27:51 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk78f66222002-08-27 10:27:51 +00005 */
6
7/*
8 * Definitions for Command Processor
9 */
10#ifndef __COMMAND_H
11#define __COMMAND_H
12
Simon Glass5e6201b2019-08-01 09:46:51 -060013#include <env.h>
Marek Vasut69c5e342012-10-12 10:27:04 +000014#include <linker_lists.h>
Stefan Roese37628252008-08-06 14:05:38 +020015
Evgeny Bachinind95e1d12023-03-20 11:23:11 +030016#include <linux/compiler_attributes.h>
17
wdenk78f66222002-08-27 10:27:51 +000018#ifndef NULL
19#define NULL 0
20#endif
21
Peter Tyserdfb72b82009-01-27 18:03:12 -060022/* Default to a width of 8 characters for help message command width */
Tom Rini364d0022023-01-10 11:19:45 -050023#ifndef CFG_SYS_HELP_CMD_WIDTH
24#define CFG_SYS_HELP_CMD_WIDTH 10
Peter Tyserdfb72b82009-01-27 18:03:12 -060025#endif
26
wdenk78f66222002-08-27 10:27:51 +000027#ifndef __ASSEMBLY__
Simon Glassb3c2aa52023-09-27 08:22:37 -060028
29/* For ARRAY_SIZE() */
30#include <linux/kernel.h>
31
wdenk78f66222002-08-27 10:27:51 +000032/*
33 * Monitor Command Table
34 */
35
Simon Glassed38aef2020-05-10 11:40:03 -060036struct cmd_tbl {
wdenk78f66222002-08-27 10:27:51 +000037 char *name; /* Command Name */
wdenk78f66222002-08-27 10:27:51 +000038 int maxargs; /* maximum number of arguments */
Boris Brezillonc71dfd12018-12-03 22:54:20 +010039 /*
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.
46 */
Simon Glassed38aef2020-05-10 11:40:03 -060047 int (*cmd_rep)(struct cmd_tbl *cmd, int flags, int argc,
48 char *const argv[], int *repeatable);
wdenk78f66222002-08-27 10:27:51 +000049 /* Implementation function */
Simon Glassed38aef2020-05-10 11:40:03 -060050 int (*cmd)(struct cmd_tbl *cmd, int flags, int argc,
51 char *const argv[]);
wdenk78f66222002-08-27 10:27:51 +000052 char *usage; /* Usage message (short) */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020053#ifdef CONFIG_SYS_LONGHELP
Simon Glass9b724572021-09-19 15:49:32 -060054 const char *help; /* Help message (long) */
wdenk78f66222002-08-27 10:27:51 +000055#endif
wdenk3902d702004-04-15 18:22:41 +000056#ifdef CONFIG_AUTO_COMPLETE
57 /* do auto completion on the arguments */
Simon Glassed38aef2020-05-10 11:40:03 -060058 int (*complete)(int argc, char *const argv[],
59 char last_char, int maxv, char *cmdv[]);
wdenk3902d702004-04-15 18:22:41 +000060#endif
wdenk78f66222002-08-27 10:27:51 +000061};
62
Simon Glass793a98e2023-11-18 14:05:20 -070063/**
64 * cmd_arg_get() - Get a particular argument
65 *
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
70 */
71static inline const char *cmd_arg_get(int argc, char *const argv[], int argnum)
72{
73 return argc > argnum ? argv[argnum] : NULL;
74}
75
76static inline const char *cmd_arg0(int argc, char *const argv[])
77{
78 return cmd_arg_get(argc, argv, 0);
79}
80
81static inline const char *cmd_arg1(int argc, char *const argv[])
82{
83 return cmd_arg_get(argc, argv, 1);
84}
85
86static inline const char *cmd_arg2(int argc, char *const argv[])
87{
88 return cmd_arg_get(argc, argv, 2);
89}
90
91static inline const char *cmd_arg3(int argc, char *const argv[])
92{
93 return cmd_arg_get(argc, argv, 3);
94}
95
Igor Grinbergde832562011-11-07 01:13:59 +000096#if defined(CONFIG_CMD_RUN)
Kory Maincentb9c73492021-02-02 16:42:27 +010097int do_run(struct cmd_tbl *cmdtp, int flag, int argc,
98 char *const argv[]);
Igor Grinbergde832562011-11-07 01:13:59 +000099#endif
wdenk78f66222002-08-27 10:27:51 +0000100
101/* common/command.c */
Simon Glassed38aef2020-05-10 11:40:03 -0600102int _do_help(struct cmd_tbl *cmd_start, int cmd_items, struct cmd_tbl *cmdtp,
103 int flag, int argc, char *const argv[]);
104struct cmd_tbl *find_cmd(const char *cmd);
105struct cmd_tbl *find_cmd_tbl(const char *cmd, struct cmd_tbl *table,
106 int table_len);
107int complete_subcmdv(struct cmd_tbl *cmdtp, int count, int argc,
108 char *const argv[], char last_char, int maxv,
Boris Brezillonae73c332018-12-03 22:54:19 +0100109 char *cmdv[]);
wdenk78f66222002-08-27 10:27:51 +0000110
Kory Maincentb9c73492021-02-02 16:42:27 +0100111int cmd_usage(const struct cmd_tbl *cmdtp);
Peter Tyserddb3af92009-01-27 18:03:10 -0600112
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100113/* Dummy ->cmd and ->cmd_rep wrappers. */
Simon Glassed38aef2020-05-10 11:40:03 -0600114int cmd_always_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
115 char *const argv[], int *repeatable);
116int cmd_never_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
117 char *const argv[], int *repeatable);
118int cmd_discard_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
119 char *const argv[]);
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100120
Simon Glassed38aef2020-05-10 11:40:03 -0600121static inline bool cmd_is_repeatable(struct cmd_tbl *cmdtp)
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100122{
123 return cmdtp->cmd_rep == cmd_always_repeatable;
124}
125
wdenk3902d702004-04-15 18:22:41 +0000126#ifdef CONFIG_AUTO_COMPLETE
Kory Maincentb9c73492021-02-02 16:42:27 +0100127int var_complete(int argc, char *const argv[], char last_char, int maxv,
128 char *cmdv[]);
129int cmd_auto_complete(const char *const prompt, char *buf, int *np,
130 int *colp);
Simon Glassc0734c32023-10-01 19:13:08 -0600131#else
132static inline int cmd_auto_complete(const char *const prompt, char *buf,
133 int *np, int *colp)
134{
135 return 0;
136}
wdenk3902d702004-04-15 18:22:41 +0000137#endif
138
Simon Glassd7385532014-02-26 15:59:15 -0700139/**
140 * cmd_process_error() - report and process a possible error
141 *
142 * @cmdtp: Command which caused the error
143 * @err: Error code (0 if none, -ve for error, like -EIO)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100144 * Return: 0 (CMD_RET_SUCCESS) if there is not error,
Michal Simek2e1b90b2018-06-21 14:49:26 +0200145 * 1 (CMD_RET_FAILURE) if an error is found
146 * -1 (CMD_RET_USAGE) if 'usage' error is found
Simon Glassd7385532014-02-26 15:59:15 -0700147 */
Simon Glassed38aef2020-05-10 11:40:03 -0600148int cmd_process_error(struct cmd_tbl *cmdtp, int err);
Simon Glassd7385532014-02-26 15:59:15 -0700149
wdenk78f66222002-08-27 10:27:51 +0000150/*
151 * Monitor Command
152 *
153 * All commands use a common argument format:
154 *
Simon Glassed38aef2020-05-10 11:40:03 -0600155 * void function(struct cmd_tbl *cmdtp, int flag, int argc,
156 * char *const argv[]);
wdenk78f66222002-08-27 10:27:51 +0000157 */
158
Simon Glass0ae023d2017-08-04 16:34:38 -0600159#if defined(CONFIG_CMD_MEMORY) || \
160 defined(CONFIG_CMD_I2C) || \
161 defined(CONFIG_CMD_ITEST) || \
Bartosz Golaszewski7dd23862019-05-20 10:22:14 +0200162 defined(CONFIG_CMD_PCI) || \
163 defined(CONFIG_CMD_SETEXPR)
Jean-Christophe PLAGNIOL-VILLARD82290f22008-09-10 22:48:05 +0200164#define CMD_DATA_SIZE
Simon Glass3d3c3972020-11-01 14:15:36 -0700165#define CMD_DATA_SIZE_ERR (-1)
166#define CMD_DATA_SIZE_STR (-2)
167
168/**
169 * cmd_get_data_size() - Get the data-size specifier from a command
170 *
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
173 * use bytes.
174 *
175 * Valid characters are:
176 *
177 * b - byte
178 * w - word (16 bits)
179 * l - long (32 bits)
180 * q - quad (64 bits)
181 * s - string
182 *
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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100186 * Return: data size in bytes (1, 2, 4, 8) or CMD_DATA_SIZE_ERR for an invalid
Simon Glass3d3c3972020-11-01 14:15:36 -0700187 * character, or CMD_DATA_SIZE_STR for a string
188 */
Rasmus Villemoes209329d2024-01-03 11:47:05 +0100189int cmd_get_data_size(const char *arg, int default_size);
Jean-Christophe PLAGNIOL-VILLARD82290f22008-09-10 22:48:05 +0200190#endif
191
Mike Frysinger2e50fa32010-10-20 03:36:26 -0400192#ifdef CONFIG_CMD_BOOTD
Kory Maincentb9c73492021-02-02 16:42:27 +0100193int do_bootd(struct cmd_tbl *cmdtp, int flag, int argc,
194 char *const argv[]);
Mike Frysinger2e50fa32010-10-20 03:36:26 -0400195#endif
Kory Maincentb9c73492021-02-02 16:42:27 +0100196int do_bootm(struct cmd_tbl *cmdtp, int flag, int argc,
197 char *const argv[]);
John Keeping9b60a172022-07-28 11:19:15 +0100198#ifdef CONFIG_CMD_BOOTM
Kory Maincentb9c73492021-02-02 16:42:27 +0100199int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd);
Mike Frysinger194c2e82011-06-05 13:43:02 +0000200#else
Simon Glassed38aef2020-05-10 11:40:03 -0600201static inline int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd)
Mike Frysinger194c2e82011-06-05 13:43:02 +0000202{
203 return 0;
204}
205#endif
Rob Herringa20cbf92012-09-21 04:02:30 +0000206
Kory Maincentb9c73492021-02-02 16:42:27 +0100207int do_bootz(struct cmd_tbl *cmdtp, int flag, int argc,
208 char *const argv[]);
Rob Herringc88e1bf2012-12-02 21:00:23 -0600209
Kory Maincentb9c73492021-02-02 16:42:27 +0100210int do_booti(struct cmd_tbl *cmdtp, int flag, int argc,
211 char *const argv[]);
Stephen Warren9c3c9312015-07-21 17:49:41 -0600212
Kory Maincentb2187d02021-02-02 16:42:29 +0100213int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc,
214 char *const argv[], int *repeatable);
215
Kory Maincentb9c73492021-02-02 16:42:27 +0100216int common_diskboot(struct cmd_tbl *cmdtp, const char *intf, int argc,
Simon Glassed38aef2020-05-10 11:40:03 -0600217 char *const argv[]);
Mike Frysinger2a5ba452010-10-20 03:35:39 -0400218
Kory Maincentb9c73492021-02-02 16:42:27 +0100219int do_reset(struct cmd_tbl *cmdtp, int flag, int argc,
220 char *const argv[]);
221int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc,
222 char *const argv[]);
223
224unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
225 char *const argv[]);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900226
227#if defined(CONFIG_CMD_NVEDIT_EFI)
Kory Maincentb9c73492021-02-02 16:42:27 +0100228int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
229 char *const argv[]);
230int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
231 char *const argv[]);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900232#endif
233
Simon Glass00e44192020-11-01 14:15:40 -0700234/**
235 * setexpr_regex_sub() - Replace a regex pattern with a string
236 *
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
245 * first
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100246 * Return: 0 if OK, 1 on error
Simon Glass00e44192020-11-01 14:15:40 -0700247 */
248int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
249 const char *r, const char *s, bool global);
250
Simon Glass1727b942012-02-14 19:59:25 +0000251/*
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.
258 */
259enum command_ret_t {
260 CMD_RET_SUCCESS, /* 0 = Success */
261 CMD_RET_FAILURE, /* 1 = Failure */
262 CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
263};
264
265/**
266 * Process a command with arguments. We look up the command and execute it
267 * if valid. Otherwise we print a usage message.
268 *
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
274 * is left unchanged.
Richard Genoud94ff0672012-12-03 06:28:28 +0000275 * @param ticks If ticks is not null, this function set it to the
276 * number of ticks the command took to complete.
Heinrich Schuchardt91f8ff62022-08-01 15:17:49 +0200277 * Return: 0 if command succeeded, else non-zero (CMD_RET_...)
Simon Glass1727b942012-02-14 19:59:25 +0000278 */
Heinrich Schuchardt91f8ff62022-08-01 15:17:49 +0200279enum command_ret_t cmd_process(int flag, int argc, char *const argv[],
280 int *repeatable, unsigned long *ticks);
Simon Glass2176d602012-02-14 19:59:23 +0000281
Simon Glassed38aef2020-05-10 11:40:03 -0600282void fixup_cmdtable(struct cmd_tbl *cmdtp, int size);
Simon Glass4ef90b72016-03-19 02:18:38 -0600283
284/**
285 * board_run_command() - Fallback function to execute a command
286 *
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.
291 *
292 * This function is only used when CONFIG_CMDLINE is not enabled.
293 *
294 * In normal situations this function should not return, since U-Boot will
295 * simply hang.
296 *
297 * @cmdline: Command line string to execute
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100298 * Return: 0 if OK, 1 for error
Simon Glass4ef90b72016-03-19 02:18:38 -0600299 */
300int board_run_command(const char *cmdline);
Simon Glassadaaa482019-11-14 12:57:43 -0700301
302int run_command(const char *cmd, int flag);
303int run_command_repeatable(const char *cmd, int flag);
304
305/**
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600306 * run_commandf() - Run a command created by a format string
307 *
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600308 * @fmt: printf() format string
309 * @...: Arguments to use (flag is always 0)
Evgeny Bachinind95e1d12023-03-20 11:23:11 +0300310 *
311 * The command cannot be larger than (CONFIG_SYS_CBSIZE - 1) characters.
312 *
313 * Return:
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
316 * run_command().
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600317 */
Evgeny Bachinind95e1d12023-03-20 11:23:11 +0300318int run_commandf(const char *fmt, ...) __printf(1, 2);
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600319
320/**
Simon Glassadaaa482019-11-14 12:57:43 -0700321 * Run a list of commands separated by ; or even \0
322 *
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.
325 *
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_...)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100329 * Return: 0 on success, or != 0 on error.
Simon Glassadaaa482019-11-14 12:57:43 -0700330 */
331int run_command_list(const char *cmd, int len, int flag);
Simon Glassdaee3ba2023-01-06 08:52:28 -0600332
333/**
334 * cmd_source_script() - Execute a script
335 *
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).
338 *
339 * @addr: Address of script
340 * @fit_uname: FIT subimage name
341 * Return: result code (enum command_ret_t)
342 */
343int cmd_source_script(ulong addr, const char *fit_uname, const char *confname);
wdenk78f66222002-08-27 10:27:51 +0000344#endif /* __ASSEMBLY__ */
345
346/*
347 * Command Flags:
348 */
349#define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
350#define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
Simon Glass94cb9c22014-10-07 13:59:43 -0600351#define CMD_FLAG_ENV 0x0004 /* command is from the environment */
wdenk78f66222002-08-27 10:27:51 +0000352
Mike Frysingerb5bc05e2010-10-20 03:52:39 -0400353#ifdef CONFIG_AUTO_COMPLETE
354# define _CMD_COMPLETE(x) x,
355#else
356# define _CMD_COMPLETE(x)
357#endif
358#ifdef CONFIG_SYS_LONGHELP
359# define _CMD_HELP(x) x,
360#else
361# define _CMD_HELP(x)
362#endif
wdenk57b2d802003-06-27 21:31:46 +0000363
Tom Rini3d681672023-10-07 15:13:07 -0400364#define U_BOOT_LONGHELP(_cmdname, text) \
365 static __maybe_unused const char _cmdname##_help_text[] = text
366
Boris Brezillon20052d42018-12-03 22:54:21 +0100367#define U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
Simon Glassed38aef2020-05-10 11:40:03 -0600368 static int do_##_cmdname(struct cmd_tbl *cmdtp, int flag, \
369 int argc, char *const argv[], \
370 int *repeatable) \
Boris Brezillon20052d42018-12-03 22:54:21 +0100371 { \
Simon Glassed38aef2020-05-10 11:40:03 -0600372 struct cmd_tbl *subcmd; \
Boris Brezillon20052d42018-12-03 22:54:21 +0100373 \
Boris Brezillon20052d42018-12-03 22:54:21 +0100374 /* We need at least the cmd and subcmd names. */ \
375 if (argc < 2 || argc > CONFIG_SYS_MAXARGS) \
376 return CMD_RET_USAGE; \
377 \
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; \
382 \
383 if (flag == CMD_FLAG_REPEAT && \
384 !cmd_is_repeatable(subcmd)) \
385 return CMD_RET_SUCCESS; \
386 \
387 return subcmd->cmd_rep(subcmd, flag, argc - 1, \
388 argv + 1, repeatable); \
389 }
390
391#ifdef CONFIG_AUTO_COMPLETE
392#define U_BOOT_SUBCMDS_COMPLETE(_cmdname) \
Simon Glassed38aef2020-05-10 11:40:03 -0600393 static int complete_##_cmdname(int argc, char *const argv[], \
Boris Brezillon20052d42018-12-03 22:54:21 +0100394 char last_char, int maxv, \
395 char *cmdv[]) \
396 { \
397 return complete_subcmdv(_cmdname##_subcmds, \
398 ARRAY_SIZE(_cmdname##_subcmds), \
399 argc - 1, argv + 1, last_char, \
400 maxv, cmdv); \
401 }
402#else
403#define U_BOOT_SUBCMDS_COMPLETE(_cmdname)
404#endif
405
406#define U_BOOT_SUBCMDS(_cmdname, ...) \
Simon Glassed38aef2020-05-10 11:40:03 -0600407 static struct cmd_tbl _cmdname##_subcmds[] = { __VA_ARGS__ }; \
Boris Brezillon20052d42018-12-03 22:54:21 +0100408 U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
409 U_BOOT_SUBCMDS_COMPLETE(_cmdname)
410
Simon Glass21097382023-02-22 09:34:25 -0700411#if CONFIG_IS_ENABLED(CMDLINE)
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100412#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) }
416
Marek Vasut69c5e342012-10-12 10:27:04 +0000417#define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
418 _usage, _help, _comp) \
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100419 { #_name, _maxargs, \
420 _rep ? cmd_always_repeatable : cmd_never_repeatable, \
421 _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
wdenk57b2d802003-06-27 21:31:46 +0000422
Marek Vasut69c5e342012-10-12 10:27:04 +0000423#define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
Simon Glassed38aef2020-05-10 11:40:03 -0600424 ll_entry_declare(struct cmd_tbl, _name, cmd) = \
Marek Vasut69c5e342012-10-12 10:27:04 +0000425 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
Simon Glass761d6022023-11-18 14:04:52 -0700426 _usage, _help, _comp)
wdenk57b2d802003-06-27 21:31:46 +0000427
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100428#define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
429 _help, _comp) \
Simon Glassed38aef2020-05-10 11:40:03 -0600430 ll_entry_declare(struct cmd_tbl, _name, cmd) = \
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100431 U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
432 _usage, _help, _comp)
433
Simon Glass9a48bc32016-03-13 19:07:33 -0600434#else
Simon Glassed38aef2020-05-10 11:40:03 -0600435#define U_BOOT_SUBCMD_START(name) static struct cmd_tbl name[] = {};
Simon Glass9a48bc32016-03-13 19:07:33 -0600436#define U_BOOT_SUBCMD_END
437
438#define _CMD_REMOVE(_name, _cmd) \
439 int __remove_ ## _name(void) \
440 { \
441 if (0) \
442 _cmd(NULL, 0, 0, NULL); \
443 return 0; \
444 }
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100445
Simon Glassa4a55e52021-03-15 18:11:21 +1300446#define _CMD_REMOVE_REP(_name, _cmd) \
447 int __remove_ ## _name(void) \
448 { \
449 if (0) \
450 _cmd(NULL, 0, 0, NULL, NULL); \
451 return 0; \
452 }
453
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100454#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) }
458
Simon Glass9a48bc32016-03-13 19:07:33 -0600459#define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
460 _help, _comp) \
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100461 { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \
Simon Glass9a48bc32016-03-13 19:07:33 -0600462 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
463
464#define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
465 _comp) \
466 _CMD_REMOVE(sub_ ## _name, _cmd)
467
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100468#define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
469 _help, _comp) \
Simon Glassa4a55e52021-03-15 18:11:21 +1300470 _CMD_REMOVE_REP(sub_ ## _name, _cmd_rep)
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100471
Simon Glass9a48bc32016-03-13 19:07:33 -0600472#endif /* CONFIG_CMDLINE */
473
Marek Vasut69c5e342012-10-12 10:27:04 +0000474#define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
475 U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
wdenk57b2d802003-06-27 21:31:46 +0000476
Simon Glass9a48bc32016-03-13 19:07:33 -0600477#define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
478 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
479 _usage, _help, NULL)
480
Boris Brezillon20052d42018-12-03 22:54:21 +0100481#define U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
482 _comp) \
483 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
484 "", "", _comp)
485
486#define U_BOOT_SUBCMD_MKENT(_name, _maxargs, _rep, _do_cmd) \
487 U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
488 NULL)
489
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)
494
wdenk78f66222002-08-27 10:27:51 +0000495#endif /* __COMMAND_H */