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