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