Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 2 | /* |
Detlev Zundel | 5e6c06b | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 3 | * (C) Copyright 2000-2009 |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Command Processor Table |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 13 | #include <console.h> |
Simon Glass | bf4229c | 2019-08-01 09:46:40 -0600 | [diff] [blame] | 14 | #include <env.h> |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 15 | #include <linux/ctype.h> |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 16 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 17 | /* |
| 18 | * Use puts() instead of printf() to avoid printf buffer overflow |
| 19 | * for long help messages |
| 20 | */ |
Detlev Zundel | 5e6c06b | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 21 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 22 | int _do_help(struct cmd_tbl *cmd_start, int cmd_items, struct cmd_tbl *cmdtp, |
| 23 | int flag, int argc, char *const argv[]) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 24 | { |
| 25 | int i; |
| 26 | int rcode = 0; |
| 27 | |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 28 | if (argc == 1) { /* show list of commands */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 29 | struct cmd_tbl *cmd_array[cmd_items]; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 30 | int i, j, swaps; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 31 | |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 32 | /* Make array of commands from .uboot_cmd section */ |
Detlev Zundel | 5e6c06b | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 33 | cmdtp = cmd_start; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 34 | for (i = 0; i < cmd_items; i++) { |
| 35 | cmd_array[i] = cmdtp++; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 36 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 37 | |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 38 | /* Sort command list (trivial bubble sort) */ |
| 39 | for (i = cmd_items - 1; i > 0; --i) { |
| 40 | swaps = 0; |
| 41 | for (j = 0; j < i; ++j) { |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 42 | if (strcmp(cmd_array[j]->name, |
| 43 | cmd_array[j + 1]->name) > 0) { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 44 | struct cmd_tbl *tmp; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 45 | tmp = cmd_array[j]; |
| 46 | cmd_array[j] = cmd_array[j + 1]; |
| 47 | cmd_array[j + 1] = tmp; |
| 48 | ++swaps; |
| 49 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 50 | } |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 51 | if (!swaps) |
| 52 | break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* print short help (usage) */ |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 56 | for (i = 0; i < cmd_items; i++) { |
| 57 | const char *usage = cmd_array[i]->usage; |
| 58 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 59 | /* allow user abort */ |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 60 | if (ctrlc()) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 61 | return 1; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 62 | if (usage == NULL) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 63 | continue; |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 64 | printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH, |
| 65 | cmd_array[i]->name, usage); |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 66 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 67 | return 0; |
| 68 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 69 | /* |
| 70 | * command help (long version) |
| 71 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 72 | for (i = 1; i < argc; ++i) { |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 73 | cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items); |
| 74 | if (cmdtp != NULL) { |
Wolfgang Denk | 9dd775b | 2009-05-24 19:17:29 +0200 | [diff] [blame] | 75 | rcode |= cmd_usage(cmdtp); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 76 | } else { |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 77 | printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n", |
| 78 | argv[i]); |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 79 | rcode = 1; |
| 80 | } |
| 81 | } |
| 82 | return rcode; |
| 83 | } |
| 84 | |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 85 | /* find command table entry for a command */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 86 | struct cmd_tbl *find_cmd_tbl(const char *cmd, struct cmd_tbl *table, |
| 87 | int table_len) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 88 | { |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 89 | #ifdef CONFIG_CMDLINE |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 90 | struct cmd_tbl *cmdtp; |
| 91 | struct cmd_tbl *cmdtp_temp = table; /* Init value */ |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 92 | const char *p; |
| 93 | int len; |
| 94 | int n_found = 0; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 95 | |
Thomas Weber | 97ea46f | 2010-11-24 13:07:51 +0100 | [diff] [blame] | 96 | if (!cmd) |
| 97 | return NULL; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 98 | /* |
| 99 | * Some commands allow length modifiers (like "cp.b"); |
| 100 | * compare command name only until first dot. |
| 101 | */ |
| 102 | len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd); |
| 103 | |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 104 | for (cmdtp = table; cmdtp != table + table_len; cmdtp++) { |
| 105 | if (strncmp(cmd, cmdtp->name, len) == 0) { |
| 106 | if (len == strlen(cmdtp->name)) |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 107 | return cmdtp; /* full match */ |
| 108 | |
| 109 | cmdtp_temp = cmdtp; /* abbreviated command ? */ |
| 110 | n_found++; |
| 111 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 112 | } |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 113 | if (n_found == 1) { /* exactly one match */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 114 | return cmdtp_temp; |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 115 | } |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 116 | #endif /* CONFIG_CMDLINE */ |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 117 | |
wdenk | 3086a97 | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 118 | return NULL; /* not found or ambiguous command */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 119 | } |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 120 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 121 | struct cmd_tbl *find_cmd(const char *cmd) |
Kumar Gala | ac2dad6 | 2008-09-23 10:05:02 -0500 | [diff] [blame] | 122 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 123 | struct cmd_tbl *start = ll_entry_start(struct cmd_tbl, cmd); |
| 124 | const int len = ll_entry_count(struct cmd_tbl, cmd); |
Marek Vasut | 69c5e34 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 125 | return find_cmd_tbl(cmd, start, len); |
Kumar Gala | ac2dad6 | 2008-09-23 10:05:02 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 128 | int cmd_usage(const struct cmd_tbl *cmdtp) |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 129 | { |
Wolfgang Denk | 9dd775b | 2009-05-24 19:17:29 +0200 | [diff] [blame] | 130 | printf("%s - %s\n\n", cmdtp->name, cmdtp->usage); |
| 131 | |
| 132 | #ifdef CONFIG_SYS_LONGHELP |
| 133 | printf("Usage:\n%s ", cmdtp->name); |
| 134 | |
| 135 | if (!cmdtp->help) { |
| 136 | puts ("- No additional help available.\n"); |
| 137 | return 1; |
| 138 | } |
| 139 | |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 140 | puts(cmdtp->help); |
| 141 | putc('\n'); |
Wolfgang Denk | 9dd775b | 2009-05-24 19:17:29 +0200 | [diff] [blame] | 142 | #endif /* CONFIG_SYS_LONGHELP */ |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 143 | return 1; |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 144 | } |
| 145 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 146 | #ifdef CONFIG_AUTO_COMPLETE |
Boris Brezillon | 619fc68 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 147 | static char env_complete_buf[512]; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 148 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 149 | int var_complete(int argc, char *const argv[], char last_char, int maxv, |
| 150 | char *cmdv[]) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 151 | { |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 152 | int space; |
| 153 | |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 154 | space = last_char == '\0' || isblank(last_char); |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 155 | |
| 156 | if (space && argc == 1) |
Boris Brezillon | 619fc68 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 157 | return env_complete("", maxv, cmdv, sizeof(env_complete_buf), |
| 158 | env_complete_buf, false); |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 159 | |
| 160 | if (!space && argc == 2) |
Boris Brezillon | 619fc68 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 161 | return env_complete(argv[1], maxv, cmdv, |
| 162 | sizeof(env_complete_buf), |
| 163 | env_complete_buf, false); |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 164 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 168 | static int dollar_complete(int argc, char *const argv[], char last_char, |
Boris Brezillon | 619fc68 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 169 | int maxv, char *cmdv[]) |
| 170 | { |
| 171 | /* Make sure the last argument starts with a $. */ |
| 172 | if (argc < 1 || argv[argc - 1][0] != '$' || |
| 173 | last_char == '\0' || isblank(last_char)) |
| 174 | return 0; |
| 175 | |
| 176 | return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf), |
| 177 | env_complete_buf, true); |
| 178 | } |
| 179 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 180 | /*************************************************************************************/ |
| 181 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 182 | int complete_subcmdv(struct cmd_tbl *cmdtp, int count, int argc, |
| 183 | char *const argv[], char last_char, |
Boris Brezillon | ae73c33 | 2018-12-03 22:54:19 +0100 | [diff] [blame] | 184 | int maxv, char *cmdv[]) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 185 | { |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 186 | #ifdef CONFIG_CMDLINE |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 187 | const struct cmd_tbl *cmdend = cmdtp + count; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 188 | const char *p; |
| 189 | int len, clen; |
| 190 | int n_found = 0; |
| 191 | const char *cmd; |
| 192 | |
| 193 | /* sanity? */ |
| 194 | if (maxv < 2) |
| 195 | return -2; |
| 196 | |
| 197 | cmdv[0] = NULL; |
| 198 | |
| 199 | if (argc == 0) { |
| 200 | /* output full list of commands */ |
Marek Vasut | 69c5e34 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 201 | for (; cmdtp != cmdend; cmdtp++) { |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 202 | if (n_found >= maxv - 2) { |
Andrew Gabbasov | a7c03b0 | 2013-12-27 10:05:14 -0600 | [diff] [blame] | 203 | cmdv[n_found++] = "..."; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 204 | break; |
| 205 | } |
Andrew Gabbasov | a7c03b0 | 2013-12-27 10:05:14 -0600 | [diff] [blame] | 206 | cmdv[n_found++] = cmdtp->name; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 207 | } |
| 208 | cmdv[n_found] = NULL; |
| 209 | return n_found; |
| 210 | } |
| 211 | |
| 212 | /* more than one arg or one but the start of the next */ |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 213 | if (argc > 1 || last_char == '\0' || isblank(last_char)) { |
Boris Brezillon | ae73c33 | 2018-12-03 22:54:19 +0100 | [diff] [blame] | 214 | cmdtp = find_cmd_tbl(argv[0], cmdtp, count); |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 215 | if (cmdtp == NULL || cmdtp->complete == NULL) { |
| 216 | cmdv[0] = NULL; |
| 217 | return 0; |
| 218 | } |
| 219 | return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv); |
| 220 | } |
| 221 | |
| 222 | cmd = argv[0]; |
| 223 | /* |
| 224 | * Some commands allow length modifiers (like "cp.b"); |
| 225 | * compare command name only until first dot. |
| 226 | */ |
| 227 | p = strchr(cmd, '.'); |
| 228 | if (p == NULL) |
| 229 | len = strlen(cmd); |
| 230 | else |
| 231 | len = p - cmd; |
| 232 | |
| 233 | /* return the partial matches */ |
Marek Vasut | 69c5e34 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 234 | for (; cmdtp != cmdend; cmdtp++) { |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 235 | |
| 236 | clen = strlen(cmdtp->name); |
| 237 | if (clen < len) |
| 238 | continue; |
| 239 | |
| 240 | if (memcmp(cmd, cmdtp->name, len) != 0) |
| 241 | continue; |
| 242 | |
| 243 | /* too many! */ |
| 244 | if (n_found >= maxv - 2) { |
| 245 | cmdv[n_found++] = "..."; |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | cmdv[n_found++] = cmdtp->name; |
| 250 | } |
| 251 | |
| 252 | cmdv[n_found] = NULL; |
| 253 | return n_found; |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 254 | #else |
| 255 | return 0; |
| 256 | #endif |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 259 | static int complete_cmdv(int argc, char *const argv[], char last_char, |
Boris Brezillon | ae73c33 | 2018-12-03 22:54:19 +0100 | [diff] [blame] | 260 | int maxv, char *cmdv[]) |
| 261 | { |
| 262 | #ifdef CONFIG_CMDLINE |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 263 | return complete_subcmdv(ll_entry_start(struct cmd_tbl, cmd), |
| 264 | ll_entry_count(struct cmd_tbl, cmd), argc, argv, |
Boris Brezillon | ae73c33 | 2018-12-03 22:54:19 +0100 | [diff] [blame] | 265 | last_char, maxv, cmdv); |
| 266 | #else |
| 267 | return 0; |
| 268 | #endif |
| 269 | } |
| 270 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 271 | static int make_argv(char *s, int argvsz, char *argv[]) |
| 272 | { |
| 273 | int argc = 0; |
| 274 | |
| 275 | /* split into argv */ |
| 276 | while (argc < argvsz - 1) { |
| 277 | |
| 278 | /* skip any white space */ |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 279 | while (isblank(*s)) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 280 | ++s; |
| 281 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 282 | if (*s == '\0') /* end of s, no more args */ |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 283 | break; |
| 284 | |
| 285 | argv[argc++] = s; /* begin of argument string */ |
| 286 | |
| 287 | /* find end of string */ |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 288 | while (*s && !isblank(*s)) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 289 | ++s; |
| 290 | |
| 291 | if (*s == '\0') /* end of s, no more args */ |
| 292 | break; |
| 293 | |
| 294 | *s++ = '\0'; /* terminate current arg */ |
| 295 | } |
| 296 | argv[argc] = NULL; |
| 297 | |
| 298 | return argc; |
| 299 | } |
| 300 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 301 | static void print_argv(const char *banner, const char *leader, const char *sep, |
| 302 | int linemax, char *const argv[]) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 303 | { |
| 304 | int ll = leader != NULL ? strlen(leader) : 0; |
| 305 | int sl = sep != NULL ? strlen(sep) : 0; |
| 306 | int len, i; |
| 307 | |
| 308 | if (banner) { |
| 309 | puts("\n"); |
| 310 | puts(banner); |
| 311 | } |
| 312 | |
| 313 | i = linemax; /* force leader and newline */ |
| 314 | while (*argv != NULL) { |
| 315 | len = strlen(*argv) + sl; |
| 316 | if (i + len >= linemax) { |
| 317 | puts("\n"); |
| 318 | if (leader) |
| 319 | puts(leader); |
| 320 | i = ll - sl; |
| 321 | } else if (sep) |
| 322 | puts(sep); |
| 323 | puts(*argv++); |
| 324 | i += len; |
| 325 | } |
| 326 | printf("\n"); |
| 327 | } |
| 328 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 329 | static int find_common_prefix(char *const argv[]) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 330 | { |
| 331 | int i, len; |
| 332 | char *anchor, *s, *t; |
| 333 | |
| 334 | if (*argv == NULL) |
| 335 | return 0; |
| 336 | |
| 337 | /* begin with max */ |
| 338 | anchor = *argv++; |
| 339 | len = strlen(anchor); |
| 340 | while ((t = *argv++) != NULL) { |
| 341 | s = anchor; |
| 342 | for (i = 0; i < len; i++, t++, s++) { |
| 343 | if (*t != *s) |
| 344 | break; |
| 345 | } |
| 346 | len = s - anchor; |
| 347 | } |
| 348 | return len; |
| 349 | } |
| 350 | |
Heinrich Schuchardt | 831030d | 2017-11-19 23:07:50 +0100 | [diff] [blame] | 351 | static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */ |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 352 | |
| 353 | int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp) |
| 354 | { |
| 355 | int n = *np, col = *colp; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 356 | char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 357 | char *cmdv[20]; |
| 358 | char *s, *t; |
| 359 | const char *sep; |
| 360 | int i, j, k, len, seplen, argc; |
| 361 | int cnt; |
| 362 | char last_char; |
Marek Vasut | fe6cdf3 | 2019-06-27 00:17:27 +0200 | [diff] [blame] | 363 | #ifdef CONFIG_CMDLINE_PS_SUPPORT |
| 364 | const char *ps_prompt = env_get("PS1"); |
| 365 | #else |
| 366 | const char *ps_prompt = CONFIG_SYS_PROMPT; |
| 367 | #endif |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 368 | |
Marek Vasut | fe6cdf3 | 2019-06-27 00:17:27 +0200 | [diff] [blame] | 369 | if (strcmp(prompt, ps_prompt) != 0) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 370 | return 0; /* not in normal console */ |
| 371 | |
| 372 | cnt = strlen(buf); |
| 373 | if (cnt >= 1) |
| 374 | last_char = buf[cnt - 1]; |
| 375 | else |
| 376 | last_char = '\0'; |
| 377 | |
| 378 | /* copy to secondary buffer which will be affected */ |
| 379 | strcpy(tmp_buf, buf); |
| 380 | |
| 381 | /* separate into argv */ |
| 382 | argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv); |
| 383 | |
Boris Brezillon | 619fc68 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 384 | /* first try a $ completion */ |
| 385 | i = dollar_complete(argc, argv, last_char, |
| 386 | sizeof(cmdv) / sizeof(cmdv[0]), cmdv); |
| 387 | if (!i) { |
| 388 | /* do the completion and return the possible completions */ |
| 389 | i = complete_cmdv(argc, argv, last_char, |
| 390 | sizeof(cmdv) / sizeof(cmdv[0]), cmdv); |
| 391 | } |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 392 | |
| 393 | /* no match; bell and out */ |
| 394 | if (i == 0) { |
| 395 | if (argc > 1) /* allow tab for non command */ |
| 396 | return 0; |
| 397 | putc('\a'); |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | s = NULL; |
| 402 | len = 0; |
| 403 | sep = NULL; |
| 404 | seplen = 0; |
| 405 | if (i == 1) { /* one match; perfect */ |
Boris Brezillon | 8506ec6 | 2018-12-03 22:54:18 +0100 | [diff] [blame] | 406 | if (last_char != '\0' && !isblank(last_char)) |
| 407 | k = strlen(argv[argc - 1]); |
| 408 | else |
| 409 | k = 0; |
| 410 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 411 | s = cmdv[0] + k; |
| 412 | len = strlen(s); |
| 413 | sep = " "; |
| 414 | seplen = 1; |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 415 | } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */ |
Boris Brezillon | 8506ec6 | 2018-12-03 22:54:18 +0100 | [diff] [blame] | 416 | if (last_char != '\0' && !isblank(last_char)) |
| 417 | k = strlen(argv[argc - 1]); |
| 418 | else |
| 419 | k = 0; |
| 420 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 421 | j -= k; |
| 422 | if (j > 0) { |
| 423 | s = cmdv[0] + k; |
| 424 | len = j; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | if (s != NULL) { |
| 429 | k = len + seplen; |
| 430 | /* make sure it fits */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 431 | if (n + k >= CONFIG_SYS_CBSIZE - 2) { |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 432 | putc('\a'); |
| 433 | return 1; |
| 434 | } |
| 435 | |
| 436 | t = buf + cnt; |
| 437 | for (i = 0; i < len; i++) |
| 438 | *t++ = *s++; |
| 439 | if (sep != NULL) |
| 440 | for (i = 0; i < seplen; i++) |
| 441 | *t++ = sep[i]; |
| 442 | *t = '\0'; |
| 443 | n += k; |
| 444 | col += k; |
| 445 | puts(t - k); |
| 446 | if (sep == NULL) |
| 447 | putc('\a'); |
| 448 | *np = n; |
| 449 | *colp = col; |
| 450 | } else { |
| 451 | print_argv(NULL, " ", " ", 78, cmdv); |
| 452 | |
| 453 | puts(prompt); |
| 454 | puts(buf); |
| 455 | } |
| 456 | return 1; |
| 457 | } |
| 458 | |
| 459 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 82290f2 | 2008-09-10 22:48:05 +0200 | [diff] [blame] | 460 | |
| 461 | #ifdef CMD_DATA_SIZE |
| 462 | int cmd_get_data_size(char* arg, int default_size) |
| 463 | { |
| 464 | /* Check for a size specification .b, .w or .l. |
| 465 | */ |
| 466 | int len = strlen(arg); |
| 467 | if (len > 2 && arg[len-2] == '.') { |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 468 | switch (arg[len-1]) { |
Jean-Christophe PLAGNIOL-VILLARD | 82290f2 | 2008-09-10 22:48:05 +0200 | [diff] [blame] | 469 | case 'b': |
| 470 | return 1; |
| 471 | case 'w': |
| 472 | return 2; |
| 473 | case 'l': |
| 474 | return 4; |
Simon Glass | 8927bf2 | 2019-12-28 10:45:10 -0700 | [diff] [blame] | 475 | #ifdef MEM_SUPPORT_64BIT_DATA |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 476 | case 'q': |
| 477 | return 8; |
| 478 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 82290f2 | 2008-09-10 22:48:05 +0200 | [diff] [blame] | 479 | case 's': |
| 480 | return -2; |
| 481 | default: |
| 482 | return -1; |
| 483 | } |
| 484 | } |
| 485 | return default_size; |
| 486 | } |
| 487 | #endif |
Heiko Schocher | 092cf16 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 488 | |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 489 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
Heiko Schocher | 092cf16 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 490 | DECLARE_GLOBAL_DATA_PTR; |
| 491 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 492 | void fixup_cmdtable(struct cmd_tbl *cmdtp, int size) |
Heiko Schocher | 092cf16 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 493 | { |
| 494 | int i; |
| 495 | |
| 496 | if (gd->reloc_off == 0) |
| 497 | return; |
| 498 | |
| 499 | for (i = 0; i < size; i++) { |
| 500 | ulong addr; |
| 501 | |
Michal Simek | 8d76d2f | 2019-10-21 12:54:20 +0200 | [diff] [blame] | 502 | addr = (ulong)(cmdtp->cmd_rep) + gd->reloc_off; |
| 503 | cmdtp->cmd_rep = |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 504 | (int (*)(struct cmd_tbl *, int, int, |
Michal Simek | 8d76d2f | 2019-10-21 12:54:20 +0200 | [diff] [blame] | 505 | char * const [], int *))addr; |
| 506 | |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 507 | addr = (ulong)(cmdtp->cmd) + gd->reloc_off; |
Kun-Hua Huang | 89299e2 | 2015-08-24 14:52:35 +0800 | [diff] [blame] | 508 | #ifdef DEBUG_COMMANDS |
Heiko Schocher | 092cf16 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 509 | printf("Command \"%s\": 0x%08lx => 0x%08lx\n", |
Masahiro Yamada | 682195e | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 510 | cmdtp->name, (ulong)(cmdtp->cmd), addr); |
Heiko Schocher | 092cf16 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 511 | #endif |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 512 | cmdtp->cmd = (int (*)(struct cmd_tbl *, int, int, |
| 513 | char *const []))addr; |
Heiko Schocher | 092cf16 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 514 | addr = (ulong)(cmdtp->name) + gd->reloc_off; |
| 515 | cmdtp->name = (char *)addr; |
| 516 | if (cmdtp->usage) { |
| 517 | addr = (ulong)(cmdtp->usage) + gd->reloc_off; |
| 518 | cmdtp->usage = (char *)addr; |
| 519 | } |
| 520 | #ifdef CONFIG_SYS_LONGHELP |
| 521 | if (cmdtp->help) { |
| 522 | addr = (ulong)(cmdtp->help) + gd->reloc_off; |
| 523 | cmdtp->help = (char *)addr; |
| 524 | } |
| 525 | #endif |
Daniel Schwierzeck | e516622 | 2011-10-13 06:07:39 +0000 | [diff] [blame] | 526 | #ifdef CONFIG_AUTO_COMPLETE |
| 527 | if (cmdtp->complete) { |
| 528 | addr = (ulong)(cmdtp->complete) + gd->reloc_off; |
Daniel Schwierzeck | 76deb6c | 2011-10-18 11:12:22 +0000 | [diff] [blame] | 529 | cmdtp->complete = |
| 530 | (int (*)(int, char * const [], char, int, char * []))addr; |
Daniel Schwierzeck | e516622 | 2011-10-13 06:07:39 +0000 | [diff] [blame] | 531 | } |
| 532 | #endif |
Heiko Schocher | 092cf16 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 533 | cmdtp++; |
| 534 | } |
| 535 | } |
| 536 | #endif |
Simon Glass | 2176d60 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 537 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 538 | int cmd_always_repeatable(struct cmd_tbl *cmdtp, int flag, int argc, |
| 539 | char *const argv[], int *repeatable) |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 540 | { |
| 541 | *repeatable = 1; |
| 542 | |
| 543 | return cmdtp->cmd(cmdtp, flag, argc, argv); |
| 544 | } |
| 545 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 546 | int cmd_never_repeatable(struct cmd_tbl *cmdtp, int flag, int argc, |
| 547 | char *const argv[], int *repeatable) |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 548 | { |
| 549 | *repeatable = 0; |
| 550 | |
| 551 | return cmdtp->cmd(cmdtp, flag, argc, argv); |
| 552 | } |
| 553 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 554 | int cmd_discard_repeatable(struct cmd_tbl *cmdtp, int flag, int argc, |
| 555 | char *const argv[]) |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 556 | { |
| 557 | int repeatable; |
| 558 | |
| 559 | return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable); |
| 560 | } |
| 561 | |
Simon Glass | 2176d60 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 562 | /** |
| 563 | * Call a command function. This should be the only route in U-Boot to call |
| 564 | * a command, so that we can track whether we are waiting for input or |
| 565 | * executing a command. |
| 566 | * |
| 567 | * @param cmdtp Pointer to the command to execute |
| 568 | * @param flag Some flags normally 0 (see CMD_FLAG_.. above) |
| 569 | * @param argc Number of arguments (arg 0 must be the command text) |
| 570 | * @param argv Arguments |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 571 | * @param repeatable Can the command be repeated |
Simon Glass | 2176d60 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 572 | * @return 0 if command succeeded, else non-zero (CMD_RET_...) |
| 573 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 574 | static int cmd_call(struct cmd_tbl *cmdtp, int flag, int argc, |
| 575 | char *const argv[], int *repeatable) |
Simon Glass | 2176d60 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 576 | { |
| 577 | int result; |
| 578 | |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 579 | result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable); |
Simon Glass | 2176d60 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 580 | if (result) |
Peng Fan | d6c09ba | 2015-07-28 22:45:37 +0800 | [diff] [blame] | 581 | debug("Command failed, result=%d\n", result); |
Simon Glass | 2176d60 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 582 | return result; |
| 583 | } |
Simon Glass | 1727b94 | 2012-02-14 19:59:25 +0000 | [diff] [blame] | 584 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 585 | enum command_ret_t cmd_process(int flag, int argc, char *const argv[], |
Richard Genoud | 94ff067 | 2012-12-03 06:28:28 +0000 | [diff] [blame] | 586 | int *repeatable, ulong *ticks) |
Simon Glass | 1727b94 | 2012-02-14 19:59:25 +0000 | [diff] [blame] | 587 | { |
| 588 | enum command_ret_t rc = CMD_RET_SUCCESS; |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 589 | struct cmd_tbl *cmdtp; |
Simon Glass | 1727b94 | 2012-02-14 19:59:25 +0000 | [diff] [blame] | 590 | |
Christoph Muellner | 388b145 | 2019-04-05 13:03:46 +0200 | [diff] [blame] | 591 | #if defined(CONFIG_SYS_XTRACE) |
| 592 | char *xtrace; |
| 593 | |
| 594 | xtrace = env_get("xtrace"); |
| 595 | if (xtrace) { |
| 596 | puts("+"); |
| 597 | for (int i = 0; i < argc; i++) { |
| 598 | puts(" "); |
| 599 | puts(argv[i]); |
| 600 | } |
| 601 | puts("\n"); |
| 602 | } |
| 603 | #endif |
| 604 | |
Simon Glass | 1727b94 | 2012-02-14 19:59:25 +0000 | [diff] [blame] | 605 | /* Look up command in command table */ |
| 606 | cmdtp = find_cmd(argv[0]); |
| 607 | if (cmdtp == NULL) { |
| 608 | printf("Unknown command '%s' - try 'help'\n", argv[0]); |
| 609 | return 1; |
| 610 | } |
| 611 | |
| 612 | /* found - check max args */ |
| 613 | if (argc > cmdtp->maxargs) |
| 614 | rc = CMD_RET_USAGE; |
| 615 | |
| 616 | #if defined(CONFIG_CMD_BOOTD) |
| 617 | /* avoid "bootd" recursion */ |
| 618 | else if (cmdtp->cmd == do_bootd) { |
| 619 | if (flag & CMD_FLAG_BOOTD) { |
| 620 | puts("'bootd' recursion detected\n"); |
| 621 | rc = CMD_RET_FAILURE; |
| 622 | } else { |
| 623 | flag |= CMD_FLAG_BOOTD; |
| 624 | } |
| 625 | } |
| 626 | #endif |
| 627 | |
| 628 | /* If OK so far, then do the command */ |
| 629 | if (!rc) { |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 630 | int newrep; |
| 631 | |
Richard Genoud | 94ff067 | 2012-12-03 06:28:28 +0000 | [diff] [blame] | 632 | if (ticks) |
| 633 | *ticks = get_timer(0); |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 634 | rc = cmd_call(cmdtp, flag, argc, argv, &newrep); |
Richard Genoud | 94ff067 | 2012-12-03 06:28:28 +0000 | [diff] [blame] | 635 | if (ticks) |
| 636 | *ticks = get_timer(*ticks); |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 637 | *repeatable &= newrep; |
Simon Glass | 1727b94 | 2012-02-14 19:59:25 +0000 | [diff] [blame] | 638 | } |
| 639 | if (rc == CMD_RET_USAGE) |
| 640 | rc = cmd_usage(cmdtp); |
| 641 | return rc; |
| 642 | } |
Simon Glass | d738553 | 2014-02-26 15:59:15 -0700 | [diff] [blame] | 643 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 644 | int cmd_process_error(struct cmd_tbl *cmdtp, int err) |
Simon Glass | d738553 | 2014-02-26 15:59:15 -0700 | [diff] [blame] | 645 | { |
Michal Simek | 2e1b90b | 2018-06-21 14:49:26 +0200 | [diff] [blame] | 646 | if (err == CMD_RET_USAGE) |
| 647 | return CMD_RET_USAGE; |
| 648 | |
Simon Glass | d738553 | 2014-02-26 15:59:15 -0700 | [diff] [blame] | 649 | if (err) { |
| 650 | printf("Command '%s' failed: Error %d\n", cmdtp->name, err); |
Michal Simek | 44bdb57 | 2018-06-04 13:29:49 +0200 | [diff] [blame] | 651 | return CMD_RET_FAILURE; |
Simon Glass | d738553 | 2014-02-26 15:59:15 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Michal Simek | 44bdb57 | 2018-06-04 13:29:49 +0200 | [diff] [blame] | 654 | return CMD_RET_SUCCESS; |
Simon Glass | d738553 | 2014-02-26 15:59:15 -0700 | [diff] [blame] | 655 | } |