blob: d1063774ce567dff15b6823b6ae10fdf72ba773d [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
wdenk78f66222002-08-27 10:27:51 +000016#ifndef NULL
17#define NULL 0
18#endif
19
Peter Tyserdfb72b82009-01-27 18:03:12 -060020/* Default to a width of 8 characters for help message command width */
21#ifndef CONFIG_SYS_HELP_CMD_WIDTH
Heinrich Schuchardt8c55cc02019-02-28 06:17:56 +010022#define CONFIG_SYS_HELP_CMD_WIDTH 10
Peter Tyserdfb72b82009-01-27 18:03:12 -060023#endif
24
wdenk78f66222002-08-27 10:27:51 +000025#ifndef __ASSEMBLY__
26/*
27 * Monitor Command Table
28 */
29
30struct cmd_tbl_s {
31 char *name; /* Command Name */
wdenk78f66222002-08-27 10:27:51 +000032 int maxargs; /* maximum number of arguments */
Boris Brezillonc71dfd12018-12-03 22:54:20 +010033 /*
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.
40 */
41 int (*cmd_rep)(struct cmd_tbl_s *cmd, int flags, int argc,
42 char * const argv[], int *repeatable);
wdenk78f66222002-08-27 10:27:51 +000043 /* Implementation function */
Wolfgang Denk6262d0212010-06-28 22:00:46 +020044 int (*cmd)(struct cmd_tbl_s *, int, int, char * const []);
wdenk78f66222002-08-27 10:27:51 +000045 char *usage; /* Usage message (short) */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020046#ifdef CONFIG_SYS_LONGHELP
wdenk78f66222002-08-27 10:27:51 +000047 char *help; /* Help message (long) */
48#endif
wdenk3902d702004-04-15 18:22:41 +000049#ifdef CONFIG_AUTO_COMPLETE
50 /* do auto completion on the arguments */
Wolfgang Denk6262d0212010-06-28 22:00:46 +020051 int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
wdenk3902d702004-04-15 18:22:41 +000052#endif
wdenk78f66222002-08-27 10:27:51 +000053};
54
55typedef struct cmd_tbl_s cmd_tbl_t;
56
wdenk78f66222002-08-27 10:27:51 +000057
Igor Grinbergde832562011-11-07 01:13:59 +000058#if defined(CONFIG_CMD_RUN)
59extern int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
60#endif
wdenk78f66222002-08-27 10:27:51 +000061
62/* common/command.c */
Detlev Zundel5e6c06b2009-03-25 17:27:52 +010063int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
Wolfgang Denk6262d0212010-06-28 22:00:46 +020064 flag, int argc, char * const argv[]);
wdenk78f66222002-08-27 10:27:51 +000065cmd_tbl_t *find_cmd(const char *cmd);
Kumar Galaac2dad62008-09-23 10:05:02 -050066cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);
Boris Brezillonae73c332018-12-03 22:54:19 +010067int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc,
68 char * const argv[], char last_char, int maxv,
69 char *cmdv[]);
wdenk78f66222002-08-27 10:27:51 +000070
Mike Frysinger33e84d12011-04-23 23:43:24 +000071extern int cmd_usage(const cmd_tbl_t *cmdtp);
Peter Tyserddb3af92009-01-27 18:03:10 -060072
Boris Brezillonc71dfd12018-12-03 22:54:20 +010073/* Dummy ->cmd and ->cmd_rep wrappers. */
74int cmd_always_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
75 char * const argv[], int *repeatable);
76int cmd_never_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
77 char * const argv[], int *repeatable);
78int cmd_discard_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
79 char * const argv[]);
80
81static inline bool cmd_is_repeatable(cmd_tbl_t *cmdtp)
82{
83 return cmdtp->cmd_rep == cmd_always_repeatable;
84}
85
wdenk3902d702004-04-15 18:22:41 +000086#ifdef CONFIG_AUTO_COMPLETE
Mike Frysingerb5bc05e2010-10-20 03:52:39 -040087extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
wdenk3902d702004-04-15 18:22:41 +000088extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp);
89#endif
90
Simon Glassd7385532014-02-26 15:59:15 -070091/**
92 * cmd_process_error() - report and process a possible error
93 *
94 * @cmdtp: Command which caused the error
95 * @err: Error code (0 if none, -ve for error, like -EIO)
Heinrich Schuchardt1b128c32018-10-12 11:23:04 +020096 * @return 0 (CMD_RET_SUCCESS) if there is not error,
Michal Simek2e1b90b2018-06-21 14:49:26 +020097 * 1 (CMD_RET_FAILURE) if an error is found
98 * -1 (CMD_RET_USAGE) if 'usage' error is found
Simon Glassd7385532014-02-26 15:59:15 -070099 */
100int cmd_process_error(cmd_tbl_t *cmdtp, int err);
101
wdenk78f66222002-08-27 10:27:51 +0000102/*
103 * Monitor Command
104 *
105 * All commands use a common argument format:
106 *
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200107 * void function (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
wdenk78f66222002-08-27 10:27:51 +0000108 */
109
Simon Glass0ae023d2017-08-04 16:34:38 -0600110#if defined(CONFIG_CMD_MEMORY) || \
111 defined(CONFIG_CMD_I2C) || \
112 defined(CONFIG_CMD_ITEST) || \
Bartosz Golaszewski7dd23862019-05-20 10:22:14 +0200113 defined(CONFIG_CMD_PCI) || \
114 defined(CONFIG_CMD_SETEXPR)
Jean-Christophe PLAGNIOL-VILLARD82290f22008-09-10 22:48:05 +0200115#define CMD_DATA_SIZE
116extern int cmd_get_data_size(char* arg, int default_size);
117#endif
118
Mike Frysinger2e50fa32010-10-20 03:36:26 -0400119#ifdef CONFIG_CMD_BOOTD
120extern int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
121#endif
Mike Frysinger194c2e82011-06-05 13:43:02 +0000122#ifdef CONFIG_CMD_BOOTM
Mike Frysinger2a5ba452010-10-20 03:35:39 -0400123extern int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Mike Frysinger194c2e82011-06-05 13:43:02 +0000124extern int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd);
125#else
126static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
127{
128 return 0;
129}
130#endif
Rob Herringa20cbf92012-09-21 04:02:30 +0000131
Rob Herringc88e1bf2012-12-02 21:00:23 -0600132extern int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
133
Stephen Warren9c3c9312015-07-21 17:49:41 -0600134extern int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
135
Rob Herringa20cbf92012-09-21 04:02:30 +0000136extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
137 char *const argv[]);
138
Mike Frysinger6d1f6982010-10-20 03:41:17 -0400139extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Michael van Slingerland2451dec2016-01-13 19:31:17 +0100140extern int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Mike Frysinger2a5ba452010-10-20 03:35:39 -0400141
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530142extern unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
143 char * const argv[]);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900144
145#if defined(CONFIG_CMD_NVEDIT_EFI)
146extern int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc,
147 char * const argv[]);
148extern int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc,
149 char * const argv[]);
150#endif
151
Simon Glass1727b942012-02-14 19:59:25 +0000152/*
153 * Error codes that commands return to cmd_process(). We use the standard 0
154 * and 1 for success and failure, but add one more case - failure with a
155 * request to call cmd_usage(). But the cmd_process() function handles
156 * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
157 * This is just a convenience for commands to avoid them having to call
158 * cmd_usage() all over the place.
159 */
160enum command_ret_t {
161 CMD_RET_SUCCESS, /* 0 = Success */
162 CMD_RET_FAILURE, /* 1 = Failure */
163 CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
164};
165
166/**
167 * Process a command with arguments. We look up the command and execute it
168 * if valid. Otherwise we print a usage message.
169 *
170 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
171 * @param argc Number of arguments (arg 0 must be the command text)
172 * @param argv Arguments
173 * @param repeatable This function sets this to 0 if the command is not
174 * repeatable. If the command is repeatable, the value
175 * is left unchanged.
Richard Genoud94ff0672012-12-03 06:28:28 +0000176 * @param ticks If ticks is not null, this function set it to the
177 * number of ticks the command took to complete.
Simon Glass1727b942012-02-14 19:59:25 +0000178 * @return 0 if the command succeeded, 1 if it failed
179 */
180int cmd_process(int flag, int argc, char * const argv[],
Richard Genoud94ff0672012-12-03 06:28:28 +0000181 int *repeatable, unsigned long *ticks);
Simon Glass2176d602012-02-14 19:59:23 +0000182
Tom Rini23415e52015-12-07 08:23:29 -0500183void fixup_cmdtable(cmd_tbl_t *cmdtp, int size);
Simon Glass4ef90b72016-03-19 02:18:38 -0600184
185/**
186 * board_run_command() - Fallback function to execute a command
187 *
188 * When no command line features are enabled in U-Boot, this function is
189 * called to execute a command. Typically the function can look at the
190 * command and perform a few very specific tasks, such as booting the
191 * system in a particular way.
192 *
193 * This function is only used when CONFIG_CMDLINE is not enabled.
194 *
195 * In normal situations this function should not return, since U-Boot will
196 * simply hang.
197 *
198 * @cmdline: Command line string to execute
199 * @return 0 if OK, 1 for error
200 */
201int board_run_command(const char *cmdline);
Simon Glassadaaa482019-11-14 12:57:43 -0700202
203int run_command(const char *cmd, int flag);
204int run_command_repeatable(const char *cmd, int flag);
205
206/**
207 * Run a list of commands separated by ; or even \0
208 *
209 * Note that if 'len' is not -1, then the command does not need to be nul
210 * terminated, Memory will be allocated for the command in that case.
211 *
212 * @param cmd List of commands to run, each separated bu semicolon
213 * @param len Length of commands excluding terminator if known (-1 if not)
214 * @param flag Execution flags (CMD_FLAG_...)
215 * @return 0 on success, or != 0 on error.
216 */
217int run_command_list(const char *cmd, int len, int flag);
wdenk78f66222002-08-27 10:27:51 +0000218#endif /* __ASSEMBLY__ */
219
220/*
221 * Command Flags:
222 */
223#define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
224#define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
Simon Glass94cb9c22014-10-07 13:59:43 -0600225#define CMD_FLAG_ENV 0x0004 /* command is from the environment */
wdenk78f66222002-08-27 10:27:51 +0000226
Mike Frysingerb5bc05e2010-10-20 03:52:39 -0400227#ifdef CONFIG_AUTO_COMPLETE
228# define _CMD_COMPLETE(x) x,
229#else
230# define _CMD_COMPLETE(x)
231#endif
232#ifdef CONFIG_SYS_LONGHELP
233# define _CMD_HELP(x) x,
234#else
235# define _CMD_HELP(x)
236#endif
wdenk57b2d802003-06-27 21:31:46 +0000237
Boris Brezillon20052d42018-12-03 22:54:21 +0100238#ifdef CONFIG_NEEDS_MANUAL_RELOC
239#define U_BOOT_SUBCMDS_RELOC(_cmdname) \
240 static void _cmdname##_subcmds_reloc(void) \
241 { \
242 static int relocated; \
243 \
244 if (relocated) \
245 return; \
246 \
247 fixup_cmdtable(_cmdname##_subcmds, \
248 ARRAY_SIZE(_cmdname##_subcmds)); \
249 relocated = 1; \
250 }
251#else
252#define U_BOOT_SUBCMDS_RELOC(_cmdname) \
253 static void _cmdname##_subcmds_reloc(void) { }
254#endif
255
256#define U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
257 static int do_##_cmdname(cmd_tbl_t *cmdtp, int flag, int argc, \
258 char * const argv[], int *repeatable) \
259 { \
260 cmd_tbl_t *subcmd; \
261 \
262 _cmdname##_subcmds_reloc(); \
263 \
264 /* We need at least the cmd and subcmd names. */ \
265 if (argc < 2 || argc > CONFIG_SYS_MAXARGS) \
266 return CMD_RET_USAGE; \
267 \
268 subcmd = find_cmd_tbl(argv[1], _cmdname##_subcmds, \
269 ARRAY_SIZE(_cmdname##_subcmds)); \
270 if (!subcmd || argc - 1 > subcmd->maxargs) \
271 return CMD_RET_USAGE; \
272 \
273 if (flag == CMD_FLAG_REPEAT && \
274 !cmd_is_repeatable(subcmd)) \
275 return CMD_RET_SUCCESS; \
276 \
277 return subcmd->cmd_rep(subcmd, flag, argc - 1, \
278 argv + 1, repeatable); \
279 }
280
281#ifdef CONFIG_AUTO_COMPLETE
282#define U_BOOT_SUBCMDS_COMPLETE(_cmdname) \
283 static int complete_##_cmdname(int argc, char * const argv[], \
284 char last_char, int maxv, \
285 char *cmdv[]) \
286 { \
287 return complete_subcmdv(_cmdname##_subcmds, \
288 ARRAY_SIZE(_cmdname##_subcmds), \
289 argc - 1, argv + 1, last_char, \
290 maxv, cmdv); \
291 }
292#else
293#define U_BOOT_SUBCMDS_COMPLETE(_cmdname)
294#endif
295
296#define U_BOOT_SUBCMDS(_cmdname, ...) \
297 static cmd_tbl_t _cmdname##_subcmds[] = { __VA_ARGS__ }; \
298 U_BOOT_SUBCMDS_RELOC(_cmdname) \
299 U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
300 U_BOOT_SUBCMDS_COMPLETE(_cmdname)
301
Simon Glass9a48bc32016-03-13 19:07:33 -0600302#ifdef CONFIG_CMDLINE
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100303#define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
304 _usage, _help, _comp) \
305 { #_name, _maxargs, _cmd_rep, cmd_discard_repeatable, \
306 _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
307
Marek Vasut69c5e342012-10-12 10:27:04 +0000308#define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
309 _usage, _help, _comp) \
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100310 { #_name, _maxargs, \
311 _rep ? cmd_always_repeatable : cmd_never_repeatable, \
312 _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
wdenk57b2d802003-06-27 21:31:46 +0000313
Marek Vasut69c5e342012-10-12 10:27:04 +0000314#define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000315 ll_entry_declare(cmd_tbl_t, _name, cmd) = \
Marek Vasut69c5e342012-10-12 10:27:04 +0000316 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
317 _usage, _help, _comp);
wdenk57b2d802003-06-27 21:31:46 +0000318
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100319#define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
320 _help, _comp) \
321 ll_entry_declare(cmd_tbl_t, _name, cmd) = \
322 U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
323 _usage, _help, _comp)
324
Simon Glass9a48bc32016-03-13 19:07:33 -0600325#else
326#define U_BOOT_SUBCMD_START(name) static cmd_tbl_t name[] = {};
327#define U_BOOT_SUBCMD_END
328
329#define _CMD_REMOVE(_name, _cmd) \
330 int __remove_ ## _name(void) \
331 { \
332 if (0) \
333 _cmd(NULL, 0, 0, NULL); \
334 return 0; \
335 }
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100336
337#define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
338 _usage, _help, _comp) \
339 { #_name, _maxargs, 0 ? _cmd_rep : NULL, NULL, _usage, \
340 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
341
Simon Glass9a48bc32016-03-13 19:07:33 -0600342#define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
343 _help, _comp) \
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100344 { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \
Simon Glass9a48bc32016-03-13 19:07:33 -0600345 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
346
347#define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
348 _comp) \
349 _CMD_REMOVE(sub_ ## _name, _cmd)
350
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100351#define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
352 _help, _comp) \
353 _CMD_REMOVE(sub_ ## _name, _cmd_rep)
354
Simon Glass9a48bc32016-03-13 19:07:33 -0600355#endif /* CONFIG_CMDLINE */
356
Marek Vasut69c5e342012-10-12 10:27:04 +0000357#define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
358 U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
wdenk57b2d802003-06-27 21:31:46 +0000359
Simon Glass9a48bc32016-03-13 19:07:33 -0600360#define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
361 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
362 _usage, _help, NULL)
363
Boris Brezillon20052d42018-12-03 22:54:21 +0100364#define U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
365 _comp) \
366 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
367 "", "", _comp)
368
369#define U_BOOT_SUBCMD_MKENT(_name, _maxargs, _rep, _do_cmd) \
370 U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
371 NULL)
372
373#define U_BOOT_CMD_WITH_SUBCMDS(_name, _usage, _help, ...) \
374 U_BOOT_SUBCMDS(_name, __VA_ARGS__) \
375 U_BOOT_CMDREP_COMPLETE(_name, CONFIG_SYS_MAXARGS, do_##_name, \
376 _usage, _help, complete_##_name)
377
wdenk78f66222002-08-27 10:27:51 +0000378#endif /* __COMMAND_H */