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