]> Git Repo - J-u-boot.git/blame - include/command.h
cli: Drop some #ifdefs in cli_readline
[J-u-boot.git] / include / command.h
CommitLineData
83d290c5 1/* SPDX-License-Identifier: GPL-2.0+ */
78f6622a 2/*
2dce551e 3 * (C) Copyright 2000-2009
78f6622a 4 * Wolfgang Denk, DENX Software Engineering, [email protected].
78f6622a
WD
5 */
6
7/*
8 * Definitions for Command Processor
9 */
10#ifndef __COMMAND_H
11#define __COMMAND_H
12
9fb625ce 13#include <env.h>
6c7c946c 14#include <linker_lists.h>
f2302d44 15
26f923c7
EB
16#include <linux/compiler_attributes.h>
17
78f6622a
WD
18#ifndef NULL
19#define NULL 0
20#endif
21
2fb2604d 22/* Default to a width of 8 characters for help message command width */
6e7df1d1
TR
23#ifndef CFG_SYS_HELP_CMD_WIDTH
24#define CFG_SYS_HELP_CMD_WIDTH 10
2fb2604d
PT
25#endif
26
78f6622a 27#ifndef __ASSEMBLY__
be595146
SG
28
29/* For ARRAY_SIZE() */
30#include <linux/kernel.h>
31
78f6622a
WD
32/*
33 * Monitor Command Table
34 */
35
09140113 36struct cmd_tbl {
78f6622a 37 char *name; /* Command Name */
78f6622a 38 int maxargs; /* maximum number of arguments */
80a48dd4
BB
39 /*
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 */
09140113
SG
47 int (*cmd_rep)(struct cmd_tbl *cmd, int flags, int argc,
48 char *const argv[], int *repeatable);
78f6622a 49 /* Implementation function */
09140113
SG
50 int (*cmd)(struct cmd_tbl *cmd, int flags, int argc,
51 char *const argv[]);
78f6622a 52 char *usage; /* Usage message (short) */
6d0f6bcf 53#ifdef CONFIG_SYS_LONGHELP
6b034487 54 const char *help; /* Help message (long) */
78f6622a 55#endif
04a85b3b
WD
56#ifdef CONFIG_AUTO_COMPLETE
57 /* do auto completion on the arguments */
09140113
SG
58 int (*complete)(int argc, char *const argv[],
59 char last_char, int maxv, char *cmdv[]);
04a85b3b 60#endif
78f6622a
WD
61};
62
d09b1787 63#if defined(CONFIG_CMD_RUN)
d3a3d44f
KM
64int do_run(struct cmd_tbl *cmdtp, int flag, int argc,
65 char *const argv[]);
d09b1787 66#endif
78f6622a
WD
67
68/* common/command.c */
09140113
SG
69int _do_help(struct cmd_tbl *cmd_start, int cmd_items, struct cmd_tbl *cmdtp,
70 int flag, int argc, char *const argv[]);
71struct cmd_tbl *find_cmd(const char *cmd);
72struct cmd_tbl *find_cmd_tbl(const char *cmd, struct cmd_tbl *table,
73 int table_len);
74int complete_subcmdv(struct cmd_tbl *cmdtp, int count, int argc,
75 char *const argv[], char last_char, int maxv,
6fb61445 76 char *cmdv[]);
78f6622a 77
d3a3d44f 78int cmd_usage(const struct cmd_tbl *cmdtp);
62c3ae7c 79
80a48dd4 80/* Dummy ->cmd and ->cmd_rep wrappers. */
09140113
SG
81int cmd_always_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
82 char *const argv[], int *repeatable);
83int cmd_never_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
84 char *const argv[], int *repeatable);
85int cmd_discard_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
86 char *const argv[]);
87
88static inline bool cmd_is_repeatable(struct cmd_tbl *cmdtp)
80a48dd4
BB
89{
90 return cmdtp->cmd_rep == cmd_always_repeatable;
91}
92
04a85b3b 93#ifdef CONFIG_AUTO_COMPLETE
d3a3d44f
KM
94int var_complete(int argc, char *const argv[], char last_char, int maxv,
95 char *cmdv[]);
96int cmd_auto_complete(const char *const prompt, char *buf, int *np,
97 int *colp);
04a85b3b
WD
98#endif
99
16ff9902
SG
100/**
101 * cmd_process_error() - report and process a possible error
102 *
103 * @cmdtp: Command which caused the error
104 * @err: Error code (0 if none, -ve for error, like -EIO)
185f812c 105 * Return: 0 (CMD_RET_SUCCESS) if there is not error,
27eb7bce
MS
106 * 1 (CMD_RET_FAILURE) if an error is found
107 * -1 (CMD_RET_USAGE) if 'usage' error is found
16ff9902 108 */
09140113 109int cmd_process_error(struct cmd_tbl *cmdtp, int err);
16ff9902 110
78f6622a
WD
111/*
112 * Monitor Command
113 *
114 * All commands use a common argument format:
115 *
09140113
SG
116 * void function(struct cmd_tbl *cmdtp, int flag, int argc,
117 * char *const argv[]);
78f6622a
WD
118 */
119
6f62d7c4
SG
120#if defined(CONFIG_CMD_MEMORY) || \
121 defined(CONFIG_CMD_I2C) || \
122 defined(CONFIG_CMD_ITEST) || \
75846445
BG
123 defined(CONFIG_CMD_PCI) || \
124 defined(CONFIG_CMD_SETEXPR)
8a40fb14 125#define CMD_DATA_SIZE
7526deec
SG
126#define CMD_DATA_SIZE_ERR (-1)
127#define CMD_DATA_SIZE_STR (-2)
128
129/**
130 * cmd_get_data_size() - Get the data-size specifier from a command
131 *
132 * This reads a '.x' size specifier appended to a command. For example 'md.b'
133 * is the 'md' command with a '.b' specifier, meaning that the command should
134 * use bytes.
135 *
136 * Valid characters are:
137 *
138 * b - byte
139 * w - word (16 bits)
140 * l - long (32 bits)
141 * q - quad (64 bits)
142 * s - string
143 *
144 * @arg: Pointers to the command to check. If a valid specifier is present it
145 * will be the last character of the string, following a '.'
146 * @default_size: Default size to return if there is no specifier
185f812c 147 * Return: data size in bytes (1, 2, 4, 8) or CMD_DATA_SIZE_ERR for an invalid
7526deec
SG
148 * character, or CMD_DATA_SIZE_STR for a string
149 */
150int cmd_get_data_size(char *arg, int default_size);
8a40fb14
JCPV
151#endif
152
7842fb7c 153#ifdef CONFIG_CMD_BOOTD
d3a3d44f
KM
154int do_bootd(struct cmd_tbl *cmdtp, int flag, int argc,
155 char *const argv[]);
7842fb7c 156#endif
d3a3d44f
KM
157int do_bootm(struct cmd_tbl *cmdtp, int flag, int argc,
158 char *const argv[]);
be43a35b 159#ifdef CONFIG_CMD_BOOTM
d3a3d44f 160int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd);
67d668bf 161#else
09140113 162static inline int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd)
67d668bf
MF
163{
164 return 0;
165}
166#endif
7405a133 167
d3a3d44f
KM
168int do_bootz(struct cmd_tbl *cmdtp, int flag, int argc,
169 char *const argv[]);
da620222 170
d3a3d44f
KM
171int do_booti(struct cmd_tbl *cmdtp, int flag, int argc,
172 char *const argv[]);
8b5c738b 173
18c25821
KM
174int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc,
175 char *const argv[], int *repeatable);
176
d3a3d44f 177int common_diskboot(struct cmd_tbl *cmdtp, const char *intf, int argc,
09140113 178 char *const argv[]);
36ebb787 179
d3a3d44f
KM
180int do_reset(struct cmd_tbl *cmdtp, int flag, int argc,
181 char *const argv[]);
182int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc,
183 char *const argv[]);
184
185unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
186 char *const argv[]);
49d81fdf
AT
187
188#if defined(CONFIG_CMD_NVEDIT_EFI)
d3a3d44f
KM
189int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
190 char *const argv[]);
191int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
192 char *const argv[]);
49d81fdf
AT
193#endif
194
d422c77a
SG
195/**
196 * setexpr_regex_sub() - Replace a regex pattern with a string
197 *
198 * @data: Buffer containing the string to update
199 * @data_size: Size of buffer (must be large enough for the new string)
200 * @nbuf: Back-reference buffer
201 * @nbuf_size: Size of back-reference buffer (must be larger enough for @s plus
202 * all back-reference expansions)
203 * @r: Regular expression to find
204 * @s: String to replace with
205 * @global: true to replace all matches in @data, false to replace just the
206 * first
185f812c 207 * Return: 0 if OK, 1 on error
d422c77a
SG
208 */
209int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
210 const char *r, const char *s, bool global);
211
9d12d5d4
SG
212/*
213 * Error codes that commands return to cmd_process(). We use the standard 0
214 * and 1 for success and failure, but add one more case - failure with a
215 * request to call cmd_usage(). But the cmd_process() function handles
216 * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
217 * This is just a convenience for commands to avoid them having to call
218 * cmd_usage() all over the place.
219 */
220enum command_ret_t {
221 CMD_RET_SUCCESS, /* 0 = Success */
222 CMD_RET_FAILURE, /* 1 = Failure */
223 CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
224};
225
226/**
227 * Process a command with arguments. We look up the command and execute it
228 * if valid. Otherwise we print a usage message.
229 *
230 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
231 * @param argc Number of arguments (arg 0 must be the command text)
232 * @param argv Arguments
233 * @param repeatable This function sets this to 0 if the command is not
234 * repeatable. If the command is repeatable, the value
235 * is left unchanged.
34765e88
RG
236 * @param ticks If ticks is not null, this function set it to the
237 * number of ticks the command took to complete.
8b8accb8 238 * Return: 0 if command succeeded, else non-zero (CMD_RET_...)
9d12d5d4 239 */
8b8accb8
HS
240enum command_ret_t cmd_process(int flag, int argc, char *const argv[],
241 int *repeatable, unsigned long *ticks);
bdf8e34b 242
09140113 243void fixup_cmdtable(struct cmd_tbl *cmdtp, int size);
f8bb6964
SG
244
245/**
246 * board_run_command() - Fallback function to execute a command
247 *
248 * When no command line features are enabled in U-Boot, this function is
249 * called to execute a command. Typically the function can look at the
250 * command and perform a few very specific tasks, such as booting the
251 * system in a particular way.
252 *
253 * This function is only used when CONFIG_CMDLINE is not enabled.
254 *
255 * In normal situations this function should not return, since U-Boot will
256 * simply hang.
257 *
258 * @cmdline: Command line string to execute
185f812c 259 * Return: 0 if OK, 1 for error
f8bb6964
SG
260 */
261int board_run_command(const char *cmdline);
288b29e4
SG
262
263int run_command(const char *cmd, int flag);
264int run_command_repeatable(const char *cmd, int flag);
265
74724484
SG
266/**
267 * run_commandf() - Run a command created by a format string
268 *
74724484
SG
269 * @fmt: printf() format string
270 * @...: Arguments to use (flag is always 0)
26f923c7
EB
271 *
272 * The command cannot be larger than (CONFIG_SYS_CBSIZE - 1) characters.
273 *
274 * Return:
275 * Returns 0 on success, -EIO if internal output error occurred, -ENOSPC in
276 * case of 'fmt' string truncation, or != 0 on error, specific for
277 * run_command().
74724484 278 */
26f923c7 279int run_commandf(const char *fmt, ...) __printf(1, 2);
74724484 280
288b29e4
SG
281/**
282 * Run a list of commands separated by ; or even \0
283 *
284 * Note that if 'len' is not -1, then the command does not need to be nul
285 * terminated, Memory will be allocated for the command in that case.
286 *
287 * @param cmd List of commands to run, each separated bu semicolon
288 * @param len Length of commands excluding terminator if known (-1 if not)
289 * @param flag Execution flags (CMD_FLAG_...)
185f812c 290 * Return: 0 on success, or != 0 on error.
288b29e4
SG
291 */
292int run_command_list(const char *cmd, int len, int flag);
30f3333d
SG
293
294/**
295 * cmd_source_script() - Execute a script
296 *
297 * Executes a U-Boot script at a particular address in memory. The script should
298 * have a header (FIT or legacy) with the script type (IH_TYPE_SCRIPT).
299 *
300 * @addr: Address of script
301 * @fit_uname: FIT subimage name
302 * Return: result code (enum command_ret_t)
303 */
304int cmd_source_script(ulong addr, const char *fit_uname, const char *confname);
78f6622a
WD
305#endif /* __ASSEMBLY__ */
306
307/*
308 * Command Flags:
309 */
310#define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
311#define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
87b6398b 312#define CMD_FLAG_ENV 0x0004 /* command is from the environment */
78f6622a 313
722b061b
MF
314#ifdef CONFIG_AUTO_COMPLETE
315# define _CMD_COMPLETE(x) x,
316#else
317# define _CMD_COMPLETE(x)
318#endif
319#ifdef CONFIG_SYS_LONGHELP
320# define _CMD_HELP(x) x,
321#else
322# define _CMD_HELP(x)
323#endif
8bde7f77 324
c0cf06e5 325#define U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
09140113
SG
326 static int do_##_cmdname(struct cmd_tbl *cmdtp, int flag, \
327 int argc, char *const argv[], \
328 int *repeatable) \
c0cf06e5 329 { \
09140113 330 struct cmd_tbl *subcmd; \
c0cf06e5 331 \
c0cf06e5
BB
332 /* We need at least the cmd and subcmd names. */ \
333 if (argc < 2 || argc > CONFIG_SYS_MAXARGS) \
334 return CMD_RET_USAGE; \
335 \
336 subcmd = find_cmd_tbl(argv[1], _cmdname##_subcmds, \
337 ARRAY_SIZE(_cmdname##_subcmds)); \
338 if (!subcmd || argc - 1 > subcmd->maxargs) \
339 return CMD_RET_USAGE; \
340 \
341 if (flag == CMD_FLAG_REPEAT && \
342 !cmd_is_repeatable(subcmd)) \
343 return CMD_RET_SUCCESS; \
344 \
345 return subcmd->cmd_rep(subcmd, flag, argc - 1, \
346 argv + 1, repeatable); \
347 }
348
349#ifdef CONFIG_AUTO_COMPLETE
350#define U_BOOT_SUBCMDS_COMPLETE(_cmdname) \
09140113 351 static int complete_##_cmdname(int argc, char *const argv[], \
c0cf06e5
BB
352 char last_char, int maxv, \
353 char *cmdv[]) \
354 { \
355 return complete_subcmdv(_cmdname##_subcmds, \
356 ARRAY_SIZE(_cmdname##_subcmds), \
357 argc - 1, argv + 1, last_char, \
358 maxv, cmdv); \
359 }
360#else
361#define U_BOOT_SUBCMDS_COMPLETE(_cmdname)
362#endif
363
364#define U_BOOT_SUBCMDS(_cmdname, ...) \
09140113 365 static struct cmd_tbl _cmdname##_subcmds[] = { __VA_ARGS__ }; \
c0cf06e5
BB
366 U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
367 U_BOOT_SUBCMDS_COMPLETE(_cmdname)
368
d99e6f78 369#if CONFIG_IS_ENABLED(CMDLINE)
80a48dd4
BB
370#define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
371 _usage, _help, _comp) \
372 { #_name, _maxargs, _cmd_rep, cmd_discard_repeatable, \
373 _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
374
6c7c946c
MV
375#define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
376 _usage, _help, _comp) \
80a48dd4
BB
377 { #_name, _maxargs, \
378 _rep ? cmd_always_repeatable : cmd_never_repeatable, \
379 _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
8bde7f77 380
6c7c946c 381#define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
09140113 382 ll_entry_declare(struct cmd_tbl, _name, cmd) = \
6c7c946c
MV
383 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
384 _usage, _help, _comp);
8bde7f77 385
80a48dd4
BB
386#define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
387 _help, _comp) \
09140113 388 ll_entry_declare(struct cmd_tbl, _name, cmd) = \
80a48dd4
BB
389 U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
390 _usage, _help, _comp)
391
fb24112c 392#else
09140113 393#define U_BOOT_SUBCMD_START(name) static struct cmd_tbl name[] = {};
fb24112c
SG
394#define U_BOOT_SUBCMD_END
395
396#define _CMD_REMOVE(_name, _cmd) \
397 int __remove_ ## _name(void) \
398 { \
399 if (0) \
400 _cmd(NULL, 0, 0, NULL); \
401 return 0; \
402 }
80a48dd4 403
45cd2e55
SG
404#define _CMD_REMOVE_REP(_name, _cmd) \
405 int __remove_ ## _name(void) \
406 { \
407 if (0) \
408 _cmd(NULL, 0, 0, NULL, NULL); \
409 return 0; \
410 }
411
80a48dd4
BB
412#define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
413 _usage, _help, _comp) \
414 { #_name, _maxargs, 0 ? _cmd_rep : NULL, NULL, _usage, \
415 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
416
fb24112c
SG
417#define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
418 _help, _comp) \
80a48dd4 419 { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \
fb24112c
SG
420 _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
421
422#define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
423 _comp) \
424 _CMD_REMOVE(sub_ ## _name, _cmd)
425
80a48dd4
BB
426#define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
427 _help, _comp) \
45cd2e55 428 _CMD_REMOVE_REP(sub_ ## _name, _cmd_rep)
80a48dd4 429
fb24112c
SG
430#endif /* CONFIG_CMDLINE */
431
6c7c946c
MV
432#define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
433 U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
8bde7f77 434
fb24112c
SG
435#define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
436 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
437 _usage, _help, NULL)
438
c0cf06e5
BB
439#define U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
440 _comp) \
441 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
442 "", "", _comp)
443
444#define U_BOOT_SUBCMD_MKENT(_name, _maxargs, _rep, _do_cmd) \
445 U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
446 NULL)
447
448#define U_BOOT_CMD_WITH_SUBCMDS(_name, _usage, _help, ...) \
449 U_BOOT_SUBCMDS(_name, __VA_ARGS__) \
450 U_BOOT_CMDREP_COMPLETE(_name, CONFIG_SYS_MAXARGS, do_##_name, \
451 _usage, _help, complete_##_name)
452
78f6622a 453#endif /* __COMMAND_H */
This page took 0.754124 seconds and 4 git commands to generate.