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