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