Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 2 | /* |
Detlev Zundel | 5e6c06b | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 3 | * (C) Copyright 2000-2009 |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Definitions for Command Processor |
| 9 | */ |
| 10 | #ifndef __COMMAND_H |
| 11 | #define __COMMAND_H |
| 12 | |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 13 | #include <env.h> |
Marek Vasut | 69c5e34 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 14 | #include <linker_lists.h> |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 15 | |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 16 | #ifndef NULL |
| 17 | #define NULL 0 |
| 18 | #endif |
| 19 | |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 20 | /* Default to a width of 8 characters for help message command width */ |
| 21 | #ifndef CONFIG_SYS_HELP_CMD_WIDTH |
Heinrich Schuchardt | 8c55cc0 | 2019-02-28 06:17:56 +0100 | [diff] [blame] | 22 | #define CONFIG_SYS_HELP_CMD_WIDTH 10 |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 23 | #endif |
| 24 | |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 25 | #ifndef __ASSEMBLY__ |
| 26 | /* |
| 27 | * Monitor Command Table |
| 28 | */ |
| 29 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 30 | struct cmd_tbl { |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 31 | char *name; /* Command Name */ |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 32 | int maxargs; /* maximum number of arguments */ |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 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 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 41 | int (*cmd_rep)(struct cmd_tbl *cmd, int flags, int argc, |
| 42 | char *const argv[], int *repeatable); |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 43 | /* Implementation function */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 44 | int (*cmd)(struct cmd_tbl *cmd, int flags, int argc, |
| 45 | char *const argv[]); |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 46 | char *usage; /* Usage message (short) */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 47 | #ifdef CONFIG_SYS_LONGHELP |
Simon Glass | 9b72457 | 2021-09-19 15:49:32 -0600 | [diff] [blame] | 48 | const char *help; /* Help message (long) */ |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 49 | #endif |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 50 | #ifdef CONFIG_AUTO_COMPLETE |
| 51 | /* do auto completion on the arguments */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 52 | int (*complete)(int argc, char *const argv[], |
| 53 | char last_char, int maxv, char *cmdv[]); |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 54 | #endif |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 57 | #if defined(CONFIG_CMD_RUN) |
Kory Maincent | b9c7349 | 2021-02-02 16:42:27 +0100 | [diff] [blame] | 58 | int do_run(struct cmd_tbl *cmdtp, int flag, int argc, |
| 59 | char *const argv[]); |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 60 | #endif |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 61 | |
| 62 | /* common/command.c */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 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, |
Boris Brezillon | ae73c33 | 2018-12-03 22:54:19 +0100 | [diff] [blame] | 70 | char *cmdv[]); |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 71 | |
Kory Maincent | b9c7349 | 2021-02-02 16:42:27 +0100 | [diff] [blame] | 72 | int cmd_usage(const struct cmd_tbl *cmdtp); |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 73 | |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 74 | /* Dummy ->cmd and ->cmd_rep wrappers. */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 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[]); |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 81 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 82 | static inline bool cmd_is_repeatable(struct cmd_tbl *cmdtp) |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 83 | { |
| 84 | return cmdtp->cmd_rep == cmd_always_repeatable; |
| 85 | } |
| 86 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 87 | #ifdef CONFIG_AUTO_COMPLETE |
Kory Maincent | b9c7349 | 2021-02-02 16:42:27 +0100 | [diff] [blame] | 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); |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
Simon Glass | d738553 | 2014-02-26 15:59:15 -0700 | [diff] [blame] | 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) |
Heinrich Schuchardt | 1b128c3 | 2018-10-12 11:23:04 +0200 | [diff] [blame] | 99 | * @return 0 (CMD_RET_SUCCESS) if there is not error, |
Michal Simek | 2e1b90b | 2018-06-21 14:49:26 +0200 | [diff] [blame] | 100 | * 1 (CMD_RET_FAILURE) if an error is found |
| 101 | * -1 (CMD_RET_USAGE) if 'usage' error is found |
Simon Glass | d738553 | 2014-02-26 15:59:15 -0700 | [diff] [blame] | 102 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 103 | int cmd_process_error(struct cmd_tbl *cmdtp, int err); |
Simon Glass | d738553 | 2014-02-26 15:59:15 -0700 | [diff] [blame] | 104 | |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 105 | /* |
| 106 | * Monitor Command |
| 107 | * |
| 108 | * All commands use a common argument format: |
| 109 | * |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 110 | * void function(struct cmd_tbl *cmdtp, int flag, int argc, |
| 111 | * char *const argv[]); |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 112 | */ |
| 113 | |
Simon Glass | 0ae023d | 2017-08-04 16:34:38 -0600 | [diff] [blame] | 114 | #if defined(CONFIG_CMD_MEMORY) || \ |
| 115 | defined(CONFIG_CMD_I2C) || \ |
| 116 | defined(CONFIG_CMD_ITEST) || \ |
Bartosz Golaszewski | 7dd2386 | 2019-05-20 10:22:14 +0200 | [diff] [blame] | 117 | defined(CONFIG_CMD_PCI) || \ |
| 118 | defined(CONFIG_CMD_SETEXPR) |
Jean-Christophe PLAGNIOL-VILLARD | 82290f2 | 2008-09-10 22:48:05 +0200 | [diff] [blame] | 119 | #define CMD_DATA_SIZE |
Simon Glass | 3d3c397 | 2020-11-01 14:15:36 -0700 | [diff] [blame] | 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); |
Jean-Christophe PLAGNIOL-VILLARD | 82290f2 | 2008-09-10 22:48:05 +0200 | [diff] [blame] | 145 | #endif |
| 146 | |
Mike Frysinger | 2e50fa3 | 2010-10-20 03:36:26 -0400 | [diff] [blame] | 147 | #ifdef CONFIG_CMD_BOOTD |
Kory Maincent | b9c7349 | 2021-02-02 16:42:27 +0100 | [diff] [blame] | 148 | int do_bootd(struct cmd_tbl *cmdtp, int flag, int argc, |
| 149 | char *const argv[]); |
Mike Frysinger | 2e50fa3 | 2010-10-20 03:36:26 -0400 | [diff] [blame] | 150 | #endif |
Mike Frysinger | 194c2e8 | 2011-06-05 13:43:02 +0000 | [diff] [blame] | 151 | #ifdef CONFIG_CMD_BOOTM |
Kory Maincent | b9c7349 | 2021-02-02 16:42:27 +0100 | [diff] [blame] | 152 | int do_bootm(struct cmd_tbl *cmdtp, int flag, int argc, |
| 153 | char *const argv[]); |
| 154 | int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd); |
Mike Frysinger | 194c2e8 | 2011-06-05 13:43:02 +0000 | [diff] [blame] | 155 | #else |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 156 | static inline int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd) |
Mike Frysinger | 194c2e8 | 2011-06-05 13:43:02 +0000 | [diff] [blame] | 157 | { |
| 158 | return 0; |
| 159 | } |
| 160 | #endif |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 161 | |
Kory Maincent | b9c7349 | 2021-02-02 16:42:27 +0100 | [diff] [blame] | 162 | int do_bootz(struct cmd_tbl *cmdtp, int flag, int argc, |
| 163 | char *const argv[]); |
Rob Herring | c88e1bf | 2012-12-02 21:00:23 -0600 | [diff] [blame] | 164 | |
Kory Maincent | b9c7349 | 2021-02-02 16:42:27 +0100 | [diff] [blame] | 165 | int do_booti(struct cmd_tbl *cmdtp, int flag, int argc, |
| 166 | char *const argv[]); |
Stephen Warren | 9c3c931 | 2015-07-21 17:49:41 -0600 | [diff] [blame] | 167 | |
Kory Maincent | b2187d0 | 2021-02-02 16:42:29 +0100 | [diff] [blame] | 168 | int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc, |
| 169 | char *const argv[], int *repeatable); |
| 170 | |
Kory Maincent | b9c7349 | 2021-02-02 16:42:27 +0100 | [diff] [blame] | 171 | int common_diskboot(struct cmd_tbl *cmdtp, const char *intf, int argc, |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 172 | char *const argv[]); |
Mike Frysinger | 2a5ba45 | 2010-10-20 03:35:39 -0400 | [diff] [blame] | 173 | |
Kory Maincent | b9c7349 | 2021-02-02 16:42:27 +0100 | [diff] [blame] | 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[]); |
AKASHI Takahiro | 117e68a | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 181 | |
| 182 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
Kory Maincent | b9c7349 | 2021-02-02 16:42:27 +0100 | [diff] [blame] | 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[]); |
AKASHI Takahiro | 117e68a | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 187 | #endif |
| 188 | |
Simon Glass | 00e4419 | 2020-11-01 14:15:40 -0700 | [diff] [blame] | 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 | |
Simon Glass | 1727b94 | 2012-02-14 19:59:25 +0000 | [diff] [blame] | 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. |
Richard Genoud | 94ff067 | 2012-12-03 06:28:28 +0000 | [diff] [blame] | 230 | * @param ticks If ticks is not null, this function set it to the |
| 231 | * number of ticks the command took to complete. |
Simon Glass | 1727b94 | 2012-02-14 19:59:25 +0000 | [diff] [blame] | 232 | * @return 0 if the command succeeded, 1 if it failed |
| 233 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 234 | int cmd_process(int flag, int argc, char *const argv[], int *repeatable, |
| 235 | unsigned long *ticks); |
Simon Glass | 2176d60 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 236 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 237 | void fixup_cmdtable(struct cmd_tbl *cmdtp, int size); |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 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); |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 256 | |
| 257 | int run_command(const char *cmd, int flag); |
| 258 | int run_command_repeatable(const char *cmd, int flag); |
| 259 | |
| 260 | /** |
| 261 | * Run a list of commands separated by ; or even \0 |
| 262 | * |
| 263 | * Note that if 'len' is not -1, then the command does not need to be nul |
| 264 | * terminated, Memory will be allocated for the command in that case. |
| 265 | * |
| 266 | * @param cmd List of commands to run, each separated bu semicolon |
| 267 | * @param len Length of commands excluding terminator if known (-1 if not) |
| 268 | * @param flag Execution flags (CMD_FLAG_...) |
| 269 | * @return 0 on success, or != 0 on error. |
| 270 | */ |
| 271 | int run_command_list(const char *cmd, int len, int flag); |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 272 | #endif /* __ASSEMBLY__ */ |
| 273 | |
| 274 | /* |
| 275 | * Command Flags: |
| 276 | */ |
| 277 | #define CMD_FLAG_REPEAT 0x0001 /* repeat last command */ |
| 278 | #define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */ |
Simon Glass | 94cb9c2 | 2014-10-07 13:59:43 -0600 | [diff] [blame] | 279 | #define CMD_FLAG_ENV 0x0004 /* command is from the environment */ |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 280 | |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 281 | #ifdef CONFIG_AUTO_COMPLETE |
| 282 | # define _CMD_COMPLETE(x) x, |
| 283 | #else |
| 284 | # define _CMD_COMPLETE(x) |
| 285 | #endif |
| 286 | #ifdef CONFIG_SYS_LONGHELP |
| 287 | # define _CMD_HELP(x) x, |
| 288 | #else |
| 289 | # define _CMD_HELP(x) |
| 290 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 291 | |
Boris Brezillon | 20052d4 | 2018-12-03 22:54:21 +0100 | [diff] [blame] | 292 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
| 293 | #define U_BOOT_SUBCMDS_RELOC(_cmdname) \ |
| 294 | static void _cmdname##_subcmds_reloc(void) \ |
| 295 | { \ |
| 296 | static int relocated; \ |
| 297 | \ |
| 298 | if (relocated) \ |
| 299 | return; \ |
| 300 | \ |
| 301 | fixup_cmdtable(_cmdname##_subcmds, \ |
| 302 | ARRAY_SIZE(_cmdname##_subcmds)); \ |
| 303 | relocated = 1; \ |
| 304 | } |
| 305 | #else |
| 306 | #define U_BOOT_SUBCMDS_RELOC(_cmdname) \ |
| 307 | static void _cmdname##_subcmds_reloc(void) { } |
| 308 | #endif |
| 309 | |
| 310 | #define U_BOOT_SUBCMDS_DO_CMD(_cmdname) \ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 311 | static int do_##_cmdname(struct cmd_tbl *cmdtp, int flag, \ |
| 312 | int argc, char *const argv[], \ |
| 313 | int *repeatable) \ |
Boris Brezillon | 20052d4 | 2018-12-03 22:54:21 +0100 | [diff] [blame] | 314 | { \ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 315 | struct cmd_tbl *subcmd; \ |
Boris Brezillon | 20052d4 | 2018-12-03 22:54:21 +0100 | [diff] [blame] | 316 | \ |
| 317 | _cmdname##_subcmds_reloc(); \ |
| 318 | \ |
| 319 | /* We need at least the cmd and subcmd names. */ \ |
| 320 | if (argc < 2 || argc > CONFIG_SYS_MAXARGS) \ |
| 321 | return CMD_RET_USAGE; \ |
| 322 | \ |
| 323 | subcmd = find_cmd_tbl(argv[1], _cmdname##_subcmds, \ |
| 324 | ARRAY_SIZE(_cmdname##_subcmds)); \ |
| 325 | if (!subcmd || argc - 1 > subcmd->maxargs) \ |
| 326 | return CMD_RET_USAGE; \ |
| 327 | \ |
| 328 | if (flag == CMD_FLAG_REPEAT && \ |
| 329 | !cmd_is_repeatable(subcmd)) \ |
| 330 | return CMD_RET_SUCCESS; \ |
| 331 | \ |
| 332 | return subcmd->cmd_rep(subcmd, flag, argc - 1, \ |
| 333 | argv + 1, repeatable); \ |
| 334 | } |
| 335 | |
| 336 | #ifdef CONFIG_AUTO_COMPLETE |
| 337 | #define U_BOOT_SUBCMDS_COMPLETE(_cmdname) \ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 338 | static int complete_##_cmdname(int argc, char *const argv[], \ |
Boris Brezillon | 20052d4 | 2018-12-03 22:54:21 +0100 | [diff] [blame] | 339 | char last_char, int maxv, \ |
| 340 | char *cmdv[]) \ |
| 341 | { \ |
| 342 | return complete_subcmdv(_cmdname##_subcmds, \ |
| 343 | ARRAY_SIZE(_cmdname##_subcmds), \ |
| 344 | argc - 1, argv + 1, last_char, \ |
| 345 | maxv, cmdv); \ |
| 346 | } |
| 347 | #else |
| 348 | #define U_BOOT_SUBCMDS_COMPLETE(_cmdname) |
| 349 | #endif |
| 350 | |
| 351 | #define U_BOOT_SUBCMDS(_cmdname, ...) \ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 352 | static struct cmd_tbl _cmdname##_subcmds[] = { __VA_ARGS__ }; \ |
Boris Brezillon | 20052d4 | 2018-12-03 22:54:21 +0100 | [diff] [blame] | 353 | U_BOOT_SUBCMDS_RELOC(_cmdname) \ |
| 354 | U_BOOT_SUBCMDS_DO_CMD(_cmdname) \ |
| 355 | U_BOOT_SUBCMDS_COMPLETE(_cmdname) |
| 356 | |
Simon Glass | 9a48bc3 | 2016-03-13 19:07:33 -0600 | [diff] [blame] | 357 | #ifdef CONFIG_CMDLINE |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 358 | #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \ |
| 359 | _usage, _help, _comp) \ |
| 360 | { #_name, _maxargs, _cmd_rep, cmd_discard_repeatable, \ |
| 361 | _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) } |
| 362 | |
Marek Vasut | 69c5e34 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 363 | #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \ |
| 364 | _usage, _help, _comp) \ |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 365 | { #_name, _maxargs, \ |
| 366 | _rep ? cmd_always_repeatable : cmd_never_repeatable, \ |
| 367 | _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 368 | |
Marek Vasut | 69c5e34 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 369 | #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 370 | ll_entry_declare(struct cmd_tbl, _name, cmd) = \ |
Marek Vasut | 69c5e34 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 371 | U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \ |
| 372 | _usage, _help, _comp); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 373 | |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 374 | #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \ |
| 375 | _help, _comp) \ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 376 | ll_entry_declare(struct cmd_tbl, _name, cmd) = \ |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 377 | U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \ |
| 378 | _usage, _help, _comp) |
| 379 | |
Simon Glass | 9a48bc3 | 2016-03-13 19:07:33 -0600 | [diff] [blame] | 380 | #else |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 381 | #define U_BOOT_SUBCMD_START(name) static struct cmd_tbl name[] = {}; |
Simon Glass | 9a48bc3 | 2016-03-13 19:07:33 -0600 | [diff] [blame] | 382 | #define U_BOOT_SUBCMD_END |
| 383 | |
| 384 | #define _CMD_REMOVE(_name, _cmd) \ |
| 385 | int __remove_ ## _name(void) \ |
| 386 | { \ |
| 387 | if (0) \ |
| 388 | _cmd(NULL, 0, 0, NULL); \ |
| 389 | return 0; \ |
| 390 | } |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 391 | |
Simon Glass | a4a55e5 | 2021-03-15 18:11:21 +1300 | [diff] [blame] | 392 | #define _CMD_REMOVE_REP(_name, _cmd) \ |
| 393 | int __remove_ ## _name(void) \ |
| 394 | { \ |
| 395 | if (0) \ |
| 396 | _cmd(NULL, 0, 0, NULL, NULL); \ |
| 397 | return 0; \ |
| 398 | } |
| 399 | |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 400 | #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \ |
| 401 | _usage, _help, _comp) \ |
| 402 | { #_name, _maxargs, 0 ? _cmd_rep : NULL, NULL, _usage, \ |
| 403 | _CMD_HELP(_help) _CMD_COMPLETE(_comp) } |
| 404 | |
Simon Glass | 9a48bc3 | 2016-03-13 19:07:33 -0600 | [diff] [blame] | 405 | #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \ |
| 406 | _help, _comp) \ |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 407 | { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \ |
Simon Glass | 9a48bc3 | 2016-03-13 19:07:33 -0600 | [diff] [blame] | 408 | _CMD_HELP(_help) _CMD_COMPLETE(_comp) } |
| 409 | |
| 410 | #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \ |
| 411 | _comp) \ |
| 412 | _CMD_REMOVE(sub_ ## _name, _cmd) |
| 413 | |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 414 | #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \ |
| 415 | _help, _comp) \ |
Simon Glass | a4a55e5 | 2021-03-15 18:11:21 +1300 | [diff] [blame] | 416 | _CMD_REMOVE_REP(sub_ ## _name, _cmd_rep) |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 417 | |
Simon Glass | 9a48bc3 | 2016-03-13 19:07:33 -0600 | [diff] [blame] | 418 | #endif /* CONFIG_CMDLINE */ |
| 419 | |
Marek Vasut | 69c5e34 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 420 | #define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \ |
| 421 | U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 422 | |
Simon Glass | 9a48bc3 | 2016-03-13 19:07:33 -0600 | [diff] [blame] | 423 | #define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \ |
| 424 | U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \ |
| 425 | _usage, _help, NULL) |
| 426 | |
Boris Brezillon | 20052d4 | 2018-12-03 22:54:21 +0100 | [diff] [blame] | 427 | #define U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \ |
| 428 | _comp) \ |
| 429 | U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \ |
| 430 | "", "", _comp) |
| 431 | |
| 432 | #define U_BOOT_SUBCMD_MKENT(_name, _maxargs, _rep, _do_cmd) \ |
| 433 | U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \ |
| 434 | NULL) |
| 435 | |
| 436 | #define U_BOOT_CMD_WITH_SUBCMDS(_name, _usage, _help, ...) \ |
| 437 | U_BOOT_SUBCMDS(_name, __VA_ARGS__) \ |
| 438 | U_BOOT_CMDREP_COMPLETE(_name, CONFIG_SYS_MAXARGS, do_##_name, \ |
| 439 | _usage, _help, complete_##_name) |
| 440 | |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 441 | #endif /* __COMMAND_H */ |