blob: be74f6ac92f77e20ccb1c80a81011df4a7e573dd [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
wdenk78f66222002-08-27 10:27:51 +00002/*
Detlev Zundel2dce5512009-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 Vasut6c7c9462012-10-12 10:27:04 +000013#include <linker_lists.h>
Stefan Roesef2302d42008-08-06 14:05:38 +020014
wdenk78f66222002-08-27 10:27:51 +000015#ifndef NULL
16#define NULL 0
17#endif
18
Peter Tyser2fb26042009-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
Heinrich Schuchardt9eebaba2019-02-28 06:17:56 +010021#define CONFIG_SYS_HELP_CMD_WIDTH 10
Peter Tyser2fb26042009-01-27 18:03:12 -060022#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 Brezillon80a48dd2018-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 Denk54841ab2010-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-VILLARD6d0f6bc2008-10-16 15:01:15 +020045#ifdef CONFIG_SYS_LONGHELP
wdenk78f66222002-08-27 10:27:51 +000046 char *help; /* Help message (long) */
47#endif
wdenk04a85b32004-04-15 18:22:41 +000048#ifdef CONFIG_AUTO_COMPLETE
49 /* do auto completion on the arguments */
Wolfgang Denk54841ab2010-06-28 22:00:46 +020050 int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
wdenk04a85b32004-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 Grinbergd09b1782011-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 Zundel2dce5512009-03-25 17:27:52 +010062int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
Wolfgang Denk54841ab2010-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 Galab799cb42008-09-23 10:05:02 -050065cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);
Boris Brezillon6fb61442018-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 Frysingere84ffdd2011-04-23 23:43:24 +000070extern int cmd_usage(const cmd_tbl_t *cmdtp);
Peter Tyser62c3ae72009-01-27 18:03:10 -060071
Boris Brezillon80a48dd2018-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
wdenk04a85b32004-04-15 18:22:41 +000085#ifdef CONFIG_AUTO_COMPLETE
Mike Frysinger722b0612010-10-20 03:52:39 -040086extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
wdenk04a85b32004-04-15 18:22:41 +000087extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp);
88#endif
89
Simon Glass16ff9902014-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 Schuchardt8a3d7342018-10-12 11:23:04 +020095 * @return 0 (CMD_RET_SUCCESS) if there is not error,
Michal Simek27eb7bc2018-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 Glass16ff9902014-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 Denk54841ab2010-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 Glass6f62d7c2017-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-VILLARD8a40fb12008-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 Frysinger7842fb72010-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 Frysinger67d668b2011-06-05 13:43:02 +0000120#ifdef CONFIG_CMD_BOOTM
Mike Frysinger36ebb782010-10-20 03:35:39 -0400121extern int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Mike Frysinger67d668b2011-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 Herring7405a132012-09-21 04:02:30 +0000129
Rob Herringda620222012-12-02 21:00:23 -0600130extern int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
131
Stephen Warren8b5c7382015-07-21 17:49:41 -0600132extern int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
133
Rob Herring7405a132012-09-21 04:02:30 +0000134extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
135 char *const argv[]);
136
Mike Frysinger882b7d72010-10-20 03:41:17 -0400137extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Michael van Slingerland4e42e292016-01-13 19:31:17 +0100138extern int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Mike Frysinger36ebb782010-10-20 03:35:39 -0400139
Nitin Jain51916862018-02-16 12:56:17 +0530140extern unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
141 char * const argv[]);
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900142
143#if defined(CONFIG_CMD_NVEDIT_EFI)
144extern int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc,
145 char * const argv[]);
146extern int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc,
147 char * const argv[]);
148#endif
149
Simon Glass9d12d5d2012-02-14 19:59:25 +0000150/*
151 * Error codes that commands return to cmd_process(). We use the standard 0
152 * and 1 for success and failure, but add one more case - failure with a
153 * request to call cmd_usage(). But the cmd_process() function handles
154 * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
155 * This is just a convenience for commands to avoid them having to call
156 * cmd_usage() all over the place.
157 */
158enum command_ret_t {
159 CMD_RET_SUCCESS, /* 0 = Success */
160 CMD_RET_FAILURE, /* 1 = Failure */
161 CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
162};
163
164/**
165 * Process a command with arguments. We look up the command and execute it
166 * if valid. Otherwise we print a usage message.
167 *
168 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
169 * @param argc Number of arguments (arg 0 must be the command text)
170 * @param argv Arguments
171 * @param repeatable This function sets this to 0 if the command is not
172 * repeatable. If the command is repeatable, the value
173 * is left unchanged.
Richard Genoud34765e82012-12-03 06:28:28 +0000174 * @param ticks If ticks is not null, this function set it to the
175 * number of ticks the command took to complete.
Simon Glass9d12d5d2012-02-14 19:59:25 +0000176 * @return 0 if the command succeeded, 1 if it failed
177 */
178int cmd_process(int flag, int argc, char * const argv[],
Richard Genoud34765e82012-12-03 06:28:28 +0000179 int *repeatable, unsigned long *ticks);
Simon Glassbdf8e342012-02-14 19:59:23 +0000180
Tom Rinicbb2df22015-12-07 08:23:29 -0500181void fixup_cmdtable(cmd_tbl_t *cmdtp, int size);
Simon Glassf8bb6962016-03-19 02:18:38 -0600182
183/**
184 * board_run_command() - Fallback function to execute a command
185 *
186 * When no command line features are enabled in U-Boot, this function is
187 * called to execute a command. Typically the function can look at the
188 * command and perform a few very specific tasks, such as booting the
189 * system in a particular way.
190 *
191 * This function is only used when CONFIG_CMDLINE is not enabled.
192 *
193 * In normal situations this function should not return, since U-Boot will
194 * simply hang.
195 *
196 * @cmdline: Command line string to execute
197 * @return 0 if OK, 1 for error
198 */
199int board_run_command(const char *cmdline);
wdenk78f66222002-08-27 10:27:51 +0000200#endif /* __ASSEMBLY__ */
201
202/*
203 * Command Flags:
204 */
205#define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
206#define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
Simon Glass87b63982014-10-07 13:59:43 -0600207#define CMD_FLAG_ENV 0x0004 /* command is from the environment */
wdenk78f66222002-08-27 10:27:51 +0000208
Mike Frysinger722b0612010-10-20 03:52:39 -0400209#ifdef CONFIG_AUTO_COMPLETE
210# define _CMD_COMPLETE(x) x,
211#else
212# define _CMD_COMPLETE(x)
213#endif
214#ifdef CONFIG_SYS_LONGHELP
215# define _CMD_HELP(x) x,
216#else
217# define _CMD_HELP(x)
218#endif
wdenk8bde7f72003-06-27 21:31:46 +0000219
Boris Brezillonc0cf06e2018-12-03 22:54:21 +0100220#ifdef CONFIG_NEEDS_MANUAL_RELOC
221#define U_BOOT_SUBCMDS_RELOC(_cmdname) \
222 static void _cmdname##_subcmds_reloc(void) \
223 { \
224 static int relocated; \
225 \
226 if (relocated) \
227 return; \
228 \
229 fixup_cmdtable(_cmdname##_subcmds, \
230 ARRAY_SIZE(_cmdname##_subcmds)); \
231 relocated = 1; \
232 }
233#else
234#define U_BOOT_SUBCMDS_RELOC(_cmdname) \
235 static void _cmdname##_subcmds_reloc(void) { }
236#endif
237
238#define U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
239 static int do_##_cmdname(cmd_tbl_t *cmdtp, int flag, int argc, \
240 char * const argv[], int *repeatable) \
241 { \
242 cmd_tbl_t *subcmd; \
243 \
244 _cmdname##_subcmds_reloc(); \
245 \
246 /* We need at least the cmd and subcmd names. */ \
247 if (argc < 2 || argc > CONFIG_SYS_MAXARGS) \
248 return CMD_RET_USAGE; \
249 \
250 subcmd = find_cmd_tbl(argv[1], _cmdname##_subcmds, \
251 ARRAY_SIZE(_cmdname##_subcmds)); \
252 if (!subcmd || argc - 1 > subcmd->maxargs) \
253 return CMD_RET_USAGE; \
254 \
255 if (flag == CMD_FLAG_REPEAT && \
256 !cmd_is_repeatable(subcmd)) \
257 return CMD_RET_SUCCESS; \
258 \
259 return subcmd->cmd_rep(subcmd, flag, argc - 1, \
260 argv + 1, repeatable); \
261 }
262
263#ifdef CONFIG_AUTO_COMPLETE
264#define U_BOOT_SUBCMDS_COMPLETE(_cmdname) \
265 static int complete_##_cmdname(int argc, char * const argv[], \
266 char last_char, int maxv, \
267 char *cmdv[]) \
268 { \
269 return complete_subcmdv(_cmdname##_subcmds, \
270 ARRAY_SIZE(_cmdname##_subcmds), \
271 argc - 1, argv + 1, last_char, \
272 maxv, cmdv); \
273 }
274#else
275#define U_BOOT_SUBCMDS_COMPLETE(_cmdname)
276#endif
277
278#define U_BOOT_SUBCMDS(_cmdname, ...) \
279 static cmd_tbl_t _cmdname##_subcmds[] = { __VA_ARGS__ }; \
280 U_BOOT_SUBCMDS_RELOC(_cmdname) \
281 U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
282 U_BOOT_SUBCMDS_COMPLETE(_cmdname)
283
Simon Glassfb241122016-03-13 19:07:33 -0600284#ifdef CONFIG_CMDLINE
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100285#define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
286 _usage, _help, _comp) \
287 { #_name, _maxargs, _cmd_rep, cmd_discard_repeatable, \
288 _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
289
Marek Vasut6c7c9462012-10-12 10:27:04 +0000290#define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
291 _usage, _help, _comp) \
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100292 { #_name, _maxargs, \
293 _rep ? cmd_always_repeatable : cmd_never_repeatable, \
294 _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
wdenk8bde7f72003-06-27 21:31:46 +0000295
Marek Vasut6c7c9462012-10-12 10:27:04 +0000296#define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000297 ll_entry_declare(cmd_tbl_t, _name, cmd) = \
Marek Vasut6c7c9462012-10-12 10:27:04 +0000298 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
299 _usage, _help, _comp);
wdenk8bde7f72003-06-27 21:31:46 +0000300
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100301#define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
302 _help, _comp) \
303 ll_entry_declare(cmd_tbl_t, _name, cmd) = \
304 U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
305 _usage, _help, _comp)
306
Simon Glassfb241122016-03-13 19:07:33 -0600307#else
308#define U_BOOT_SUBCMD_START(name) static cmd_tbl_t name[] = {};
309#define U_BOOT_SUBCMD_END
310
311#define _CMD_REMOVE(_name, _cmd) \
312 int __remove_ ## _name(void) \
313 { \
314 if (0) \
315 _cmd(NULL, 0, 0, NULL); \
316 return 0; \
317 }
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100318
319#define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
320 _usage, _help, _comp) \
321 { #_name, _maxargs, 0 ? _cmd_rep : NULL, NULL, _usage, \
322 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
323
Simon Glassfb241122016-03-13 19:07:33 -0600324#define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
325 _help, _comp) \
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100326 { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \
Simon Glassfb241122016-03-13 19:07:33 -0600327 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
328
329#define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
330 _comp) \
331 _CMD_REMOVE(sub_ ## _name, _cmd)
332
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100333#define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
334 _help, _comp) \
335 _CMD_REMOVE(sub_ ## _name, _cmd_rep)
336
Simon Glassfb241122016-03-13 19:07:33 -0600337#endif /* CONFIG_CMDLINE */
338
Marek Vasut6c7c9462012-10-12 10:27:04 +0000339#define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
340 U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
wdenk8bde7f72003-06-27 21:31:46 +0000341
Simon Glassfb241122016-03-13 19:07:33 -0600342#define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
343 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
344 _usage, _help, NULL)
345
Boris Brezillonc0cf06e2018-12-03 22:54:21 +0100346#define U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
347 _comp) \
348 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
349 "", "", _comp)
350
351#define U_BOOT_SUBCMD_MKENT(_name, _maxargs, _rep, _do_cmd) \
352 U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
353 NULL)
354
355#define U_BOOT_CMD_WITH_SUBCMDS(_name, _usage, _help, ...) \
356 U_BOOT_SUBCMDS(_name, __VA_ARGS__) \
357 U_BOOT_CMDREP_COMPLETE(_name, CONFIG_SYS_MAXARGS, do_##_name, \
358 _usage, _help, complete_##_name)
359
wdenk78f66222002-08-27 10:27:51 +0000360#endif /* __COMMAND_H */