blob: 461b17447c0dc23d9f1cdf4839dee0b281977eea [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
Marek Vasut69c5e342012-10-12 10:27:04 +000013#include <linker_lists.h>
Stefan Roese37628252008-08-06 14:05:38 +020014
wdenk78f66222002-08-27 10:27:51 +000015#ifndef NULL
16#define NULL 0
17#endif
18
Peter Tyserdfb72b82009-01-27 18:03:12 -060019/* 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
wdenk78f66222002-08-27 10:27:51 +000024#ifndef __ASSEMBLY__
25/*
26 * Monitor Command Table
27 */
28
29struct cmd_tbl_s {
30 char *name; /* Command Name */
wdenk78f66222002-08-27 10:27:51 +000031 int maxargs; /* maximum number of arguments */
Boris Brezillonc71dfd12018-12-03 22:54:20 +010032 /*
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);
wdenk78f66222002-08-27 10:27:51 +000042 /* Implementation function */
Wolfgang Denk6262d0212010-06-28 22:00:46 +020043 int (*cmd)(struct cmd_tbl_s *, int, int, char * const []);
wdenk78f66222002-08-27 10:27:51 +000044 char *usage; /* Usage message (short) */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020045#ifdef CONFIG_SYS_LONGHELP
wdenk78f66222002-08-27 10:27:51 +000046 char *help; /* Help message (long) */
47#endif
wdenk3902d702004-04-15 18:22:41 +000048#ifdef CONFIG_AUTO_COMPLETE
49 /* do auto completion on the arguments */
Wolfgang Denk6262d0212010-06-28 22:00:46 +020050 int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
wdenk3902d702004-04-15 18:22:41 +000051#endif
wdenk78f66222002-08-27 10:27:51 +000052};
53
54typedef struct cmd_tbl_s cmd_tbl_t;
55
wdenk78f66222002-08-27 10:27:51 +000056
Igor Grinbergde832562011-11-07 01:13:59 +000057#if defined(CONFIG_CMD_RUN)
58extern int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
59#endif
wdenk78f66222002-08-27 10:27:51 +000060
61/* common/command.c */
Detlev Zundel5e6c06b2009-03-25 17:27:52 +010062int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
Wolfgang Denk6262d0212010-06-28 22:00:46 +020063 flag, int argc, char * const argv[]);
wdenk78f66222002-08-27 10:27:51 +000064cmd_tbl_t *find_cmd(const char *cmd);
Kumar Galaac2dad62008-09-23 10:05:02 -050065cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);
Boris Brezillonae73c332018-12-03 22:54:19 +010066int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc,
67 char * const argv[], char last_char, int maxv,
68 char *cmdv[]);
wdenk78f66222002-08-27 10:27:51 +000069
Mike Frysinger33e84d12011-04-23 23:43:24 +000070extern int cmd_usage(const cmd_tbl_t *cmdtp);
Peter Tyserddb3af92009-01-27 18:03:10 -060071
Boris Brezillonc71dfd12018-12-03 22:54:20 +010072/* Dummy ->cmd and ->cmd_rep wrappers. */
73int cmd_always_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
74 char * const argv[], int *repeatable);
75int cmd_never_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
76 char * const argv[], int *repeatable);
77int cmd_discard_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
78 char * const argv[]);
79
80static inline bool cmd_is_repeatable(cmd_tbl_t *cmdtp)
81{
82 return cmdtp->cmd_rep == cmd_always_repeatable;
83}
84
wdenk3902d702004-04-15 18:22:41 +000085#ifdef CONFIG_AUTO_COMPLETE
Mike Frysingerb5bc05e2010-10-20 03:52:39 -040086extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
wdenk3902d702004-04-15 18:22:41 +000087extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp);
88#endif
89
Simon Glassd7385532014-02-26 15:59:15 -070090/**
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)
Heinrich Schuchardt1b128c32018-10-12 11:23:04 +020095 * @return 0 (CMD_RET_SUCCESS) if there is not error,
Michal Simek2e1b90b2018-06-21 14:49:26 +020096 * 1 (CMD_RET_FAILURE) if an error is found
97 * -1 (CMD_RET_USAGE) if 'usage' error is found
Simon Glassd7385532014-02-26 15:59:15 -070098 */
99int cmd_process_error(cmd_tbl_t *cmdtp, int err);
100
wdenk78f66222002-08-27 10:27:51 +0000101/*
102 * Monitor Command
103 *
104 * All commands use a common argument format:
105 *
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200106 * void function (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
wdenk78f66222002-08-27 10:27:51 +0000107 */
108
Simon Glass0ae023d2017-08-04 16:34:38 -0600109#if defined(CONFIG_CMD_MEMORY) || \
110 defined(CONFIG_CMD_I2C) || \
111 defined(CONFIG_CMD_ITEST) || \
112 defined(CONFIG_CMD_PCI)
Jean-Christophe PLAGNIOL-VILLARD82290f22008-09-10 22:48:05 +0200113#define CMD_DATA_SIZE
114extern int cmd_get_data_size(char* arg, int default_size);
115#endif
116
Mike Frysinger2e50fa32010-10-20 03:36:26 -0400117#ifdef CONFIG_CMD_BOOTD
118extern int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
119#endif
Mike Frysinger194c2e82011-06-05 13:43:02 +0000120#ifdef CONFIG_CMD_BOOTM
Mike Frysinger2a5ba452010-10-20 03:35:39 -0400121extern int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Mike Frysinger194c2e82011-06-05 13:43:02 +0000122extern int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd);
123#else
124static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
125{
126 return 0;
127}
128#endif
Rob Herringa20cbf92012-09-21 04:02:30 +0000129
Rob Herringc88e1bf2012-12-02 21:00:23 -0600130extern int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
131
Stephen Warren9c3c9312015-07-21 17:49:41 -0600132extern int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
133
Rob Herringa20cbf92012-09-21 04:02:30 +0000134extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
135 char *const argv[]);
136
Mike Frysinger6d1f6982010-10-20 03:41:17 -0400137extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Michael van Slingerland2451dec2016-01-13 19:31:17 +0100138extern int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Mike Frysinger2a5ba452010-10-20 03:35:39 -0400139
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530140extern unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
141 char * const argv[]);
Simon Glass1727b942012-02-14 19:59:25 +0000142/*
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 */
150enum 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.
Richard Genoud94ff0672012-12-03 06:28:28 +0000166 * @param ticks If ticks is not null, this function set it to the
167 * number of ticks the command took to complete.
Simon Glass1727b942012-02-14 19:59:25 +0000168 * @return 0 if the command succeeded, 1 if it failed
169 */
170int cmd_process(int flag, int argc, char * const argv[],
Richard Genoud94ff0672012-12-03 06:28:28 +0000171 int *repeatable, unsigned long *ticks);
Simon Glass2176d602012-02-14 19:59:23 +0000172
Tom Rini23415e52015-12-07 08:23:29 -0500173void fixup_cmdtable(cmd_tbl_t *cmdtp, int size);
Simon Glass4ef90b72016-03-19 02:18:38 -0600174
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 */
191int board_run_command(const char *cmdline);
wdenk78f66222002-08-27 10:27:51 +0000192#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 */
Simon Glass94cb9c22014-10-07 13:59:43 -0600199#define CMD_FLAG_ENV 0x0004 /* command is from the environment */
wdenk78f66222002-08-27 10:27:51 +0000200
Mike Frysingerb5bc05e2010-10-20 03:52:39 -0400201#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
wdenk57b2d802003-06-27 21:31:46 +0000211
Boris Brezillon20052d42018-12-03 22:54:21 +0100212#ifdef CONFIG_NEEDS_MANUAL_RELOC
213#define U_BOOT_SUBCMDS_RELOC(_cmdname) \
214 static void _cmdname##_subcmds_reloc(void) \
215 { \
216 static int relocated; \
217 \
218 if (relocated) \
219 return; \
220 \
221 fixup_cmdtable(_cmdname##_subcmds, \
222 ARRAY_SIZE(_cmdname##_subcmds)); \
223 relocated = 1; \
224 }
225#else
226#define U_BOOT_SUBCMDS_RELOC(_cmdname) \
227 static void _cmdname##_subcmds_reloc(void) { }
228#endif
229
230#define U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
231 static int do_##_cmdname(cmd_tbl_t *cmdtp, int flag, int argc, \
232 char * const argv[], int *repeatable) \
233 { \
234 cmd_tbl_t *subcmd; \
235 \
236 _cmdname##_subcmds_reloc(); \
237 \
238 /* We need at least the cmd and subcmd names. */ \
239 if (argc < 2 || argc > CONFIG_SYS_MAXARGS) \
240 return CMD_RET_USAGE; \
241 \
242 subcmd = find_cmd_tbl(argv[1], _cmdname##_subcmds, \
243 ARRAY_SIZE(_cmdname##_subcmds)); \
244 if (!subcmd || argc - 1 > subcmd->maxargs) \
245 return CMD_RET_USAGE; \
246 \
247 if (flag == CMD_FLAG_REPEAT && \
248 !cmd_is_repeatable(subcmd)) \
249 return CMD_RET_SUCCESS; \
250 \
251 return subcmd->cmd_rep(subcmd, flag, argc - 1, \
252 argv + 1, repeatable); \
253 }
254
255#ifdef CONFIG_AUTO_COMPLETE
256#define U_BOOT_SUBCMDS_COMPLETE(_cmdname) \
257 static int complete_##_cmdname(int argc, char * const argv[], \
258 char last_char, int maxv, \
259 char *cmdv[]) \
260 { \
261 return complete_subcmdv(_cmdname##_subcmds, \
262 ARRAY_SIZE(_cmdname##_subcmds), \
263 argc - 1, argv + 1, last_char, \
264 maxv, cmdv); \
265 }
266#else
267#define U_BOOT_SUBCMDS_COMPLETE(_cmdname)
268#endif
269
270#define U_BOOT_SUBCMDS(_cmdname, ...) \
271 static cmd_tbl_t _cmdname##_subcmds[] = { __VA_ARGS__ }; \
272 U_BOOT_SUBCMDS_RELOC(_cmdname) \
273 U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
274 U_BOOT_SUBCMDS_COMPLETE(_cmdname)
275
Simon Glass9a48bc32016-03-13 19:07:33 -0600276#ifdef CONFIG_CMDLINE
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100277#define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
278 _usage, _help, _comp) \
279 { #_name, _maxargs, _cmd_rep, cmd_discard_repeatable, \
280 _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
281
Marek Vasut69c5e342012-10-12 10:27:04 +0000282#define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
283 _usage, _help, _comp) \
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100284 { #_name, _maxargs, \
285 _rep ? cmd_always_repeatable : cmd_never_repeatable, \
286 _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
wdenk57b2d802003-06-27 21:31:46 +0000287
Marek Vasut69c5e342012-10-12 10:27:04 +0000288#define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000289 ll_entry_declare(cmd_tbl_t, _name, cmd) = \
Marek Vasut69c5e342012-10-12 10:27:04 +0000290 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
291 _usage, _help, _comp);
wdenk57b2d802003-06-27 21:31:46 +0000292
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100293#define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
294 _help, _comp) \
295 ll_entry_declare(cmd_tbl_t, _name, cmd) = \
296 U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
297 _usage, _help, _comp)
298
Simon Glass9a48bc32016-03-13 19:07:33 -0600299#else
300#define U_BOOT_SUBCMD_START(name) static cmd_tbl_t name[] = {};
301#define U_BOOT_SUBCMD_END
302
303#define _CMD_REMOVE(_name, _cmd) \
304 int __remove_ ## _name(void) \
305 { \
306 if (0) \
307 _cmd(NULL, 0, 0, NULL); \
308 return 0; \
309 }
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100310
311#define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
312 _usage, _help, _comp) \
313 { #_name, _maxargs, 0 ? _cmd_rep : NULL, NULL, _usage, \
314 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
315
Simon Glass9a48bc32016-03-13 19:07:33 -0600316#define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
317 _help, _comp) \
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100318 { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \
Simon Glass9a48bc32016-03-13 19:07:33 -0600319 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
320
321#define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
322 _comp) \
323 _CMD_REMOVE(sub_ ## _name, _cmd)
324
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100325#define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
326 _help, _comp) \
327 _CMD_REMOVE(sub_ ## _name, _cmd_rep)
328
Simon Glass9a48bc32016-03-13 19:07:33 -0600329#endif /* CONFIG_CMDLINE */
330
Marek Vasut69c5e342012-10-12 10:27:04 +0000331#define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
332 U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
wdenk57b2d802003-06-27 21:31:46 +0000333
Simon Glass9a48bc32016-03-13 19:07:33 -0600334#define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
335 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
336 _usage, _help, NULL)
337
Boris Brezillon20052d42018-12-03 22:54:21 +0100338#define U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
339 _comp) \
340 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
341 "", "", _comp)
342
343#define U_BOOT_SUBCMD_MKENT(_name, _maxargs, _rep, _do_cmd) \
344 U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
345 NULL)
346
347#define U_BOOT_CMD_WITH_SUBCMDS(_name, _usage, _help, ...) \
348 U_BOOT_SUBCMDS(_name, __VA_ARGS__) \
349 U_BOOT_CMDREP_COMPLETE(_name, CONFIG_SYS_MAXARGS, do_##_name, \
350 _usage, _help, complete_##_name)
351
wdenk78f66222002-08-27 10:27:51 +0000352#endif /* __COMMAND_H */