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