Tom Rini | 83d290c | 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 | 2dce551 | 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 | a33a824 | 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 | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 14 | #include <console.h> |
Simon Glass | af95f20 | 2019-08-01 09:46:40 -0600 | [diff] [blame] | 15 | #include <env.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Jason Hobbs | 4d91a6e | 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 | |
Heinrich Schuchardt | 7cf5b40 | 2020-11-26 20:46:37 +0100 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 21 | /* |
| 22 | * Use puts() instead of printf() to avoid printf buffer overflow |
| 23 | * for long help messages |
| 24 | */ |
Detlev Zundel | 2dce551 | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 25 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 26 | int _do_help(struct cmd_tbl *cmd_start, int cmd_items, struct cmd_tbl *cmdtp, |
| 27 | int flag, int argc, char *const argv[]) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 28 | { |
| 29 | int i; |
| 30 | int rcode = 0; |
| 31 | |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 32 | if (argc == 1) { /* show list of commands */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 33 | struct cmd_tbl *cmd_array[cmd_items]; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 34 | int i, j, swaps; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 35 | |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 36 | /* Make array of commands from .uboot_cmd section */ |
Detlev Zundel | 2dce551 | 2009-03-25 17:27:52 +0100 | [diff] [blame] | 37 | cmdtp = cmd_start; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 38 | for (i = 0; i < cmd_items; i++) { |
| 39 | cmd_array[i] = cmdtp++; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 40 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 41 | |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 42 | /* Sort command list (trivial bubble sort) */ |
| 43 | for (i = cmd_items - 1; i > 0; --i) { |
| 44 | swaps = 0; |
| 45 | for (j = 0; j < i; ++j) { |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 46 | if (strcmp(cmd_array[j]->name, |
| 47 | cmd_array[j + 1]->name) > 0) { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 48 | struct cmd_tbl *tmp; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 49 | tmp = cmd_array[j]; |
| 50 | cmd_array[j] = cmd_array[j + 1]; |
| 51 | cmd_array[j + 1] = tmp; |
| 52 | ++swaps; |
| 53 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 54 | } |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 55 | if (!swaps) |
| 56 | break; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* print short help (usage) */ |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 60 | for (i = 0; i < cmd_items; i++) { |
| 61 | const char *usage = cmd_array[i]->usage; |
| 62 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 63 | /* allow user abort */ |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 64 | if (ctrlc()) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 65 | return 1; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 66 | if (usage == NULL) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 67 | continue; |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 68 | printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH, |
| 69 | cmd_array[i]->name, usage); |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 70 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 71 | return 0; |
| 72 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 73 | /* |
| 74 | * command help (long version) |
| 75 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 76 | for (i = 1; i < argc; ++i) { |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 77 | cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items); |
| 78 | if (cmdtp != NULL) { |
Wolfgang Denk | 94796d8 | 2009-05-24 19:17:29 +0200 | [diff] [blame] | 79 | rcode |= cmd_usage(cmdtp); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 80 | } else { |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 81 | printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n", |
| 82 | argv[i]); |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 83 | rcode = 1; |
| 84 | } |
| 85 | } |
| 86 | return rcode; |
| 87 | } |
| 88 | |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 89 | /* find command table entry for a command */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 90 | struct cmd_tbl *find_cmd_tbl(const char *cmd, struct cmd_tbl *table, |
| 91 | int table_len) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 92 | { |
Simon Glass | f8bb696 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 93 | #ifdef CONFIG_CMDLINE |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 94 | struct cmd_tbl *cmdtp; |
| 95 | struct cmd_tbl *cmdtp_temp = table; /* Init value */ |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 96 | const char *p; |
| 97 | int len; |
| 98 | int n_found = 0; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 99 | |
Thomas Weber | 7013c06 | 2010-11-24 13:07:51 +0100 | [diff] [blame] | 100 | if (!cmd) |
| 101 | return NULL; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 102 | /* |
| 103 | * Some commands allow length modifiers (like "cp.b"); |
| 104 | * compare command name only until first dot. |
| 105 | */ |
| 106 | len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd); |
| 107 | |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 108 | for (cmdtp = table; cmdtp != table + table_len; cmdtp++) { |
| 109 | if (strncmp(cmd, cmdtp->name, len) == 0) { |
| 110 | if (len == strlen(cmdtp->name)) |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 111 | return cmdtp; /* full match */ |
| 112 | |
| 113 | cmdtp_temp = cmdtp; /* abbreviated command ? */ |
| 114 | n_found++; |
| 115 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 116 | } |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 117 | if (n_found == 1) { /* exactly one match */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 118 | return cmdtp_temp; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 119 | } |
Simon Glass | f8bb696 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 120 | #endif /* CONFIG_CMDLINE */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 121 | |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 122 | return NULL; /* not found or ambiguous command */ |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 123 | } |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 124 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 125 | struct cmd_tbl *find_cmd(const char *cmd) |
Kumar Gala | b799cb4 | 2008-09-23 10:05:02 -0500 | [diff] [blame] | 126 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 127 | struct cmd_tbl *start = ll_entry_start(struct cmd_tbl, cmd); |
| 128 | const int len = ll_entry_count(struct cmd_tbl, cmd); |
Marek Vasut | 6c7c946 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 129 | return find_cmd_tbl(cmd, start, len); |
Kumar Gala | b799cb4 | 2008-09-23 10:05:02 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 132 | int cmd_usage(const struct cmd_tbl *cmdtp) |
Peter Tyser | 62c3ae7 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 133 | { |
Wolfgang Denk | 94796d8 | 2009-05-24 19:17:29 +0200 | [diff] [blame] | 134 | printf("%s - %s\n\n", cmdtp->name, cmdtp->usage); |
| 135 | |
| 136 | #ifdef CONFIG_SYS_LONGHELP |
| 137 | printf("Usage:\n%s ", cmdtp->name); |
| 138 | |
| 139 | if (!cmdtp->help) { |
| 140 | puts ("- No additional help available.\n"); |
| 141 | return 1; |
| 142 | } |
| 143 | |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 144 | puts(cmdtp->help); |
| 145 | putc('\n'); |
Wolfgang Denk | 94796d8 | 2009-05-24 19:17:29 +0200 | [diff] [blame] | 146 | #endif /* CONFIG_SYS_LONGHELP */ |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 147 | return 1; |
Peter Tyser | 62c3ae7 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 148 | } |
| 149 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 150 | #ifdef CONFIG_AUTO_COMPLETE |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 151 | static char env_complete_buf[512]; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 152 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 153 | int var_complete(int argc, char *const argv[], char last_char, int maxv, |
| 154 | char *cmdv[]) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 155 | { |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 156 | int space; |
| 157 | |
Jason Hobbs | 4d91a6e | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 158 | space = last_char == '\0' || isblank(last_char); |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 159 | |
| 160 | if (space && argc == 1) |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 161 | return env_complete("", maxv, cmdv, sizeof(env_complete_buf), |
| 162 | env_complete_buf, false); |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 163 | |
| 164 | if (!space && argc == 2) |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 165 | return env_complete(argv[1], maxv, cmdv, |
| 166 | sizeof(env_complete_buf), |
| 167 | env_complete_buf, false); |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 168 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 172 | static int dollar_complete(int argc, char *const argv[], char last_char, |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 173 | int maxv, char *cmdv[]) |
| 174 | { |
| 175 | /* Make sure the last argument starts with a $. */ |
| 176 | if (argc < 1 || argv[argc - 1][0] != '$' || |
| 177 | last_char == '\0' || isblank(last_char)) |
| 178 | return 0; |
| 179 | |
| 180 | return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf), |
| 181 | env_complete_buf, true); |
| 182 | } |
| 183 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 184 | /*************************************************************************************/ |
| 185 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 186 | int complete_subcmdv(struct cmd_tbl *cmdtp, int count, int argc, |
| 187 | char *const argv[], char last_char, |
Boris Brezillon | 6fb6144 | 2018-12-03 22:54:19 +0100 | [diff] [blame] | 188 | int maxv, char *cmdv[]) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 189 | { |
Simon Glass | f8bb696 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 190 | #ifdef CONFIG_CMDLINE |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 191 | const struct cmd_tbl *cmdend = cmdtp + count; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 192 | const char *p; |
| 193 | int len, clen; |
| 194 | int n_found = 0; |
| 195 | const char *cmd; |
| 196 | |
| 197 | /* sanity? */ |
| 198 | if (maxv < 2) |
| 199 | return -2; |
| 200 | |
| 201 | cmdv[0] = NULL; |
| 202 | |
| 203 | if (argc == 0) { |
| 204 | /* output full list of commands */ |
Marek Vasut | 6c7c946 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 205 | for (; cmdtp != cmdend; cmdtp++) { |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 206 | if (n_found >= maxv - 2) { |
Andrew Gabbasov | 9b43894 | 2013-12-27 10:05:14 -0600 | [diff] [blame] | 207 | cmdv[n_found++] = "..."; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 208 | break; |
| 209 | } |
Andrew Gabbasov | 9b43894 | 2013-12-27 10:05:14 -0600 | [diff] [blame] | 210 | cmdv[n_found++] = cmdtp->name; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 211 | } |
| 212 | cmdv[n_found] = NULL; |
| 213 | return n_found; |
| 214 | } |
| 215 | |
| 216 | /* more than one arg or one but the start of the next */ |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 217 | if (argc > 1 || last_char == '\0' || isblank(last_char)) { |
Boris Brezillon | 6fb6144 | 2018-12-03 22:54:19 +0100 | [diff] [blame] | 218 | cmdtp = find_cmd_tbl(argv[0], cmdtp, count); |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 219 | if (cmdtp == NULL || cmdtp->complete == NULL) { |
| 220 | cmdv[0] = NULL; |
| 221 | return 0; |
| 222 | } |
| 223 | return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv); |
| 224 | } |
| 225 | |
| 226 | cmd = argv[0]; |
| 227 | /* |
| 228 | * Some commands allow length modifiers (like "cp.b"); |
| 229 | * compare command name only until first dot. |
| 230 | */ |
| 231 | p = strchr(cmd, '.'); |
| 232 | if (p == NULL) |
| 233 | len = strlen(cmd); |
| 234 | else |
| 235 | len = p - cmd; |
| 236 | |
| 237 | /* return the partial matches */ |
Marek Vasut | 6c7c946 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 238 | for (; cmdtp != cmdend; cmdtp++) { |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 239 | |
| 240 | clen = strlen(cmdtp->name); |
| 241 | if (clen < len) |
| 242 | continue; |
| 243 | |
| 244 | if (memcmp(cmd, cmdtp->name, len) != 0) |
| 245 | continue; |
| 246 | |
| 247 | /* too many! */ |
| 248 | if (n_found >= maxv - 2) { |
| 249 | cmdv[n_found++] = "..."; |
| 250 | break; |
| 251 | } |
| 252 | |
| 253 | cmdv[n_found++] = cmdtp->name; |
| 254 | } |
| 255 | |
| 256 | cmdv[n_found] = NULL; |
| 257 | return n_found; |
Simon Glass | f8bb696 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 258 | #else |
| 259 | return 0; |
| 260 | #endif |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 263 | static int complete_cmdv(int argc, char *const argv[], char last_char, |
Boris Brezillon | 6fb6144 | 2018-12-03 22:54:19 +0100 | [diff] [blame] | 264 | int maxv, char *cmdv[]) |
| 265 | { |
| 266 | #ifdef CONFIG_CMDLINE |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 267 | return complete_subcmdv(ll_entry_start(struct cmd_tbl, cmd), |
| 268 | ll_entry_count(struct cmd_tbl, cmd), argc, argv, |
Boris Brezillon | 6fb6144 | 2018-12-03 22:54:19 +0100 | [diff] [blame] | 269 | last_char, maxv, cmdv); |
| 270 | #else |
| 271 | return 0; |
| 272 | #endif |
| 273 | } |
| 274 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 275 | static int make_argv(char *s, int argvsz, char *argv[]) |
| 276 | { |
| 277 | int argc = 0; |
| 278 | |
| 279 | /* split into argv */ |
| 280 | while (argc < argvsz - 1) { |
| 281 | |
| 282 | /* skip any white space */ |
Jason Hobbs | 4d91a6e | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 283 | while (isblank(*s)) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 284 | ++s; |
| 285 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 286 | if (*s == '\0') /* end of s, no more args */ |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 287 | break; |
| 288 | |
| 289 | argv[argc++] = s; /* begin of argument string */ |
| 290 | |
| 291 | /* find end of string */ |
Jason Hobbs | 4d91a6e | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 292 | while (*s && !isblank(*s)) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 293 | ++s; |
| 294 | |
| 295 | if (*s == '\0') /* end of s, no more args */ |
| 296 | break; |
| 297 | |
| 298 | *s++ = '\0'; /* terminate current arg */ |
| 299 | } |
| 300 | argv[argc] = NULL; |
| 301 | |
| 302 | return argc; |
| 303 | } |
| 304 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 305 | static void print_argv(const char *banner, const char *leader, const char *sep, |
| 306 | int linemax, char *const argv[]) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 307 | { |
| 308 | int ll = leader != NULL ? strlen(leader) : 0; |
| 309 | int sl = sep != NULL ? strlen(sep) : 0; |
| 310 | int len, i; |
| 311 | |
| 312 | if (banner) { |
| 313 | puts("\n"); |
| 314 | puts(banner); |
| 315 | } |
| 316 | |
| 317 | i = linemax; /* force leader and newline */ |
| 318 | while (*argv != NULL) { |
| 319 | len = strlen(*argv) + sl; |
| 320 | if (i + len >= linemax) { |
| 321 | puts("\n"); |
| 322 | if (leader) |
| 323 | puts(leader); |
| 324 | i = ll - sl; |
| 325 | } else if (sep) |
| 326 | puts(sep); |
| 327 | puts(*argv++); |
| 328 | i += len; |
| 329 | } |
| 330 | printf("\n"); |
| 331 | } |
| 332 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 333 | static int find_common_prefix(char *const argv[]) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 334 | { |
| 335 | int i, len; |
| 336 | char *anchor, *s, *t; |
| 337 | |
| 338 | if (*argv == NULL) |
| 339 | return 0; |
| 340 | |
| 341 | /* begin with max */ |
| 342 | anchor = *argv++; |
| 343 | len = strlen(anchor); |
| 344 | while ((t = *argv++) != NULL) { |
| 345 | s = anchor; |
| 346 | for (i = 0; i < len; i++, t++, s++) { |
| 347 | if (*t != *s) |
| 348 | break; |
| 349 | } |
| 350 | len = s - anchor; |
| 351 | } |
| 352 | return len; |
| 353 | } |
| 354 | |
Heinrich Schuchardt | 2614a20 | 2017-11-19 23:07:50 +0100 | [diff] [blame] | 355 | static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */ |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 356 | |
| 357 | int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp) |
| 358 | { |
| 359 | int n = *np, col = *colp; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 360 | char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 361 | char *cmdv[20]; |
| 362 | char *s, *t; |
| 363 | const char *sep; |
| 364 | int i, j, k, len, seplen, argc; |
| 365 | int cnt; |
| 366 | char last_char; |
Marek Vasut | 4225f83 | 2019-06-27 00:17:27 +0200 | [diff] [blame] | 367 | #ifdef CONFIG_CMDLINE_PS_SUPPORT |
| 368 | const char *ps_prompt = env_get("PS1"); |
| 369 | #else |
| 370 | const char *ps_prompt = CONFIG_SYS_PROMPT; |
| 371 | #endif |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 372 | |
Marek Vasut | 4225f83 | 2019-06-27 00:17:27 +0200 | [diff] [blame] | 373 | if (strcmp(prompt, ps_prompt) != 0) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 374 | return 0; /* not in normal console */ |
| 375 | |
| 376 | cnt = strlen(buf); |
| 377 | if (cnt >= 1) |
| 378 | last_char = buf[cnt - 1]; |
| 379 | else |
| 380 | last_char = '\0'; |
| 381 | |
| 382 | /* copy to secondary buffer which will be affected */ |
| 383 | strcpy(tmp_buf, buf); |
| 384 | |
| 385 | /* separate into argv */ |
| 386 | argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv); |
| 387 | |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 388 | /* first try a $ completion */ |
| 389 | i = dollar_complete(argc, argv, last_char, |
| 390 | sizeof(cmdv) / sizeof(cmdv[0]), cmdv); |
| 391 | if (!i) { |
| 392 | /* do the completion and return the possible completions */ |
| 393 | i = complete_cmdv(argc, argv, last_char, |
| 394 | sizeof(cmdv) / sizeof(cmdv[0]), cmdv); |
| 395 | } |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 396 | |
| 397 | /* no match; bell and out */ |
| 398 | if (i == 0) { |
| 399 | if (argc > 1) /* allow tab for non command */ |
| 400 | return 0; |
| 401 | putc('\a'); |
| 402 | return 1; |
| 403 | } |
| 404 | |
| 405 | s = NULL; |
| 406 | len = 0; |
| 407 | sep = NULL; |
| 408 | seplen = 0; |
| 409 | if (i == 1) { /* one match; perfect */ |
Boris Brezillon | cbe07eb | 2018-12-03 22:54:18 +0100 | [diff] [blame] | 410 | if (last_char != '\0' && !isblank(last_char)) |
| 411 | k = strlen(argv[argc - 1]); |
| 412 | else |
| 413 | k = 0; |
| 414 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 415 | s = cmdv[0] + k; |
| 416 | len = strlen(s); |
| 417 | sep = " "; |
| 418 | seplen = 1; |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 419 | } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */ |
Boris Brezillon | cbe07eb | 2018-12-03 22:54:18 +0100 | [diff] [blame] | 420 | if (last_char != '\0' && !isblank(last_char)) |
| 421 | k = strlen(argv[argc - 1]); |
| 422 | else |
| 423 | k = 0; |
| 424 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 425 | j -= k; |
| 426 | if (j > 0) { |
| 427 | s = cmdv[0] + k; |
| 428 | len = j; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (s != NULL) { |
| 433 | k = len + seplen; |
| 434 | /* make sure it fits */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 435 | if (n + k >= CONFIG_SYS_CBSIZE - 2) { |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 436 | putc('\a'); |
| 437 | return 1; |
| 438 | } |
| 439 | |
| 440 | t = buf + cnt; |
| 441 | for (i = 0; i < len; i++) |
| 442 | *t++ = *s++; |
| 443 | if (sep != NULL) |
| 444 | for (i = 0; i < seplen; i++) |
| 445 | *t++ = sep[i]; |
| 446 | *t = '\0'; |
| 447 | n += k; |
| 448 | col += k; |
| 449 | puts(t - k); |
| 450 | if (sep == NULL) |
| 451 | putc('\a'); |
| 452 | *np = n; |
| 453 | *colp = col; |
| 454 | } else { |
| 455 | print_argv(NULL, " ", " ", 78, cmdv); |
| 456 | |
| 457 | puts(prompt); |
| 458 | puts(buf); |
| 459 | } |
| 460 | return 1; |
| 461 | } |
| 462 | |
| 463 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 8a40fb1 | 2008-09-10 22:48:05 +0200 | [diff] [blame] | 464 | |
| 465 | #ifdef CMD_DATA_SIZE |
| 466 | int cmd_get_data_size(char* arg, int default_size) |
| 467 | { |
| 468 | /* Check for a size specification .b, .w or .l. |
| 469 | */ |
| 470 | int len = strlen(arg); |
| 471 | if (len > 2 && arg[len-2] == '.') { |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 472 | switch (arg[len-1]) { |
Jean-Christophe PLAGNIOL-VILLARD | 8a40fb1 | 2008-09-10 22:48:05 +0200 | [diff] [blame] | 473 | case 'b': |
| 474 | return 1; |
| 475 | case 'w': |
| 476 | return 2; |
| 477 | case 'l': |
| 478 | return 4; |
| 479 | case 's': |
Simon Glass | 7526dee | 2020-11-01 14:15:36 -0700 | [diff] [blame] | 480 | return CMD_DATA_SIZE_STR; |
Simon Glass | a33a824 | 2020-06-02 19:26:48 -0600 | [diff] [blame] | 481 | case 'q': |
| 482 | if (MEM_SUPPORT_64BIT_DATA) |
| 483 | return 8; |
| 484 | /* no break */ |
Jean-Christophe PLAGNIOL-VILLARD | 8a40fb1 | 2008-09-10 22:48:05 +0200 | [diff] [blame] | 485 | default: |
Simon Glass | 7526dee | 2020-11-01 14:15:36 -0700 | [diff] [blame] | 486 | return CMD_DATA_SIZE_ERR; |
Jean-Christophe PLAGNIOL-VILLARD | 8a40fb1 | 2008-09-10 22:48:05 +0200 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | return default_size; |
| 490 | } |
| 491 | #endif |
Heiko Schocher | 620f1f6 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 492 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 493 | void fixup_cmdtable(struct cmd_tbl *cmdtp, int size) |
Heiko Schocher | 620f1f6 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 494 | { |
| 495 | int i; |
| 496 | |
| 497 | if (gd->reloc_off == 0) |
| 498 | return; |
| 499 | |
| 500 | for (i = 0; i < size; i++) { |
| 501 | ulong addr; |
| 502 | |
Michal Simek | 6f94daf | 2019-10-21 12:54:20 +0200 | [diff] [blame] | 503 | addr = (ulong)(cmdtp->cmd_rep) + gd->reloc_off; |
| 504 | cmdtp->cmd_rep = |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 505 | (int (*)(struct cmd_tbl *, int, int, |
Michal Simek | 6f94daf | 2019-10-21 12:54:20 +0200 | [diff] [blame] | 506 | char * const [], int *))addr; |
| 507 | |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 508 | addr = (ulong)(cmdtp->cmd) + gd->reloc_off; |
Kun-Hua Huang | 2e88bb2 | 2015-08-24 14:52:35 +0800 | [diff] [blame] | 509 | #ifdef DEBUG_COMMANDS |
Heiko Schocher | 620f1f6 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 510 | printf("Command \"%s\": 0x%08lx => 0x%08lx\n", |
Masahiro Yamada | 616e216 | 2014-10-23 17:27:30 +0900 | [diff] [blame] | 511 | cmdtp->name, (ulong)(cmdtp->cmd), addr); |
Heiko Schocher | 620f1f6 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 512 | #endif |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 513 | cmdtp->cmd = (int (*)(struct cmd_tbl *, int, int, |
| 514 | char *const []))addr; |
Heiko Schocher | 620f1f6 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 515 | addr = (ulong)(cmdtp->name) + gd->reloc_off; |
| 516 | cmdtp->name = (char *)addr; |
| 517 | if (cmdtp->usage) { |
| 518 | addr = (ulong)(cmdtp->usage) + gd->reloc_off; |
| 519 | cmdtp->usage = (char *)addr; |
| 520 | } |
| 521 | #ifdef CONFIG_SYS_LONGHELP |
| 522 | if (cmdtp->help) { |
| 523 | addr = (ulong)(cmdtp->help) + gd->reloc_off; |
| 524 | cmdtp->help = (char *)addr; |
| 525 | } |
| 526 | #endif |
Daniel Schwierzeck | fa28bd2 | 2011-10-13 06:07:39 +0000 | [diff] [blame] | 527 | #ifdef CONFIG_AUTO_COMPLETE |
| 528 | if (cmdtp->complete) { |
| 529 | addr = (ulong)(cmdtp->complete) + gd->reloc_off; |
Daniel Schwierzeck | 3668d8f | 2011-10-18 11:12:22 +0000 | [diff] [blame] | 530 | cmdtp->complete = |
| 531 | (int (*)(int, char * const [], char, int, char * []))addr; |
Daniel Schwierzeck | fa28bd2 | 2011-10-13 06:07:39 +0000 | [diff] [blame] | 532 | } |
| 533 | #endif |
Heiko Schocher | 620f1f6 | 2010-09-17 13:10:33 +0200 | [diff] [blame] | 534 | cmdtp++; |
| 535 | } |
| 536 | } |
Simon Glass | bdf8e34 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 537 | |
Simon Glass | 0914011 | 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 | 80a48dd | 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 | 0914011 | 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 | 80a48dd | 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 | 0914011 | 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 | 80a48dd | 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 | bdf8e34 | 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 | 80a48dd | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 571 | * @param repeatable Can the command be repeated |
Simon Glass | bdf8e34 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 572 | * @return 0 if command succeeded, else non-zero (CMD_RET_...) |
| 573 | */ |
Simon Glass | 0914011 | 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 | bdf8e34 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 576 | { |
| 577 | int result; |
| 578 | |
Boris Brezillon | 80a48dd | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 579 | result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable); |
Simon Glass | bdf8e34 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 580 | if (result) |
Peng Fan | 58b6ad6 | 2015-07-28 22:45:37 +0800 | [diff] [blame] | 581 | debug("Command failed, result=%d\n", result); |
Simon Glass | bdf8e34 | 2012-02-14 19:59:23 +0000 | [diff] [blame] | 582 | return result; |
| 583 | } |
Simon Glass | 9d12d5d | 2012-02-14 19:59:25 +0000 | [diff] [blame] | 584 | |
Simon Glass | 0914011 | 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 | 34765e8 | 2012-12-03 06:28:28 +0000 | [diff] [blame] | 586 | int *repeatable, ulong *ticks) |
Simon Glass | 9d12d5d | 2012-02-14 19:59:25 +0000 | [diff] [blame] | 587 | { |
| 588 | enum command_ret_t rc = CMD_RET_SUCCESS; |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 589 | struct cmd_tbl *cmdtp; |
Simon Glass | 9d12d5d | 2012-02-14 19:59:25 +0000 | [diff] [blame] | 590 | |
Christoph Muellner | 7ae31fc | 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 | 9d12d5d | 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 | 80a48dd | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 630 | int newrep; |
| 631 | |
Richard Genoud | 34765e8 | 2012-12-03 06:28:28 +0000 | [diff] [blame] | 632 | if (ticks) |
| 633 | *ticks = get_timer(0); |
Boris Brezillon | 80a48dd | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 634 | rc = cmd_call(cmdtp, flag, argc, argv, &newrep); |
Richard Genoud | 34765e8 | 2012-12-03 06:28:28 +0000 | [diff] [blame] | 635 | if (ticks) |
| 636 | *ticks = get_timer(*ticks); |
Boris Brezillon | 80a48dd | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 637 | *repeatable &= newrep; |
Simon Glass | 9d12d5d | 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 | 16ff990 | 2014-02-26 15:59:15 -0700 | [diff] [blame] | 643 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 644 | int cmd_process_error(struct cmd_tbl *cmdtp, int err) |
Simon Glass | 16ff990 | 2014-02-26 15:59:15 -0700 | [diff] [blame] | 645 | { |
Michal Simek | 27eb7bc | 2018-06-21 14:49:26 +0200 | [diff] [blame] | 646 | if (err == CMD_RET_USAGE) |
| 647 | return CMD_RET_USAGE; |
| 648 | |
Simon Glass | 16ff990 | 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 | 3723324 | 2018-06-04 13:29:49 +0200 | [diff] [blame] | 651 | return CMD_RET_FAILURE; |
Simon Glass | 16ff990 | 2014-02-26 15:59:15 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Michal Simek | 3723324 | 2018-06-04 13:29:49 +0200 | [diff] [blame] | 654 | return CMD_RET_SUCCESS; |
Simon Glass | 16ff990 | 2014-02-26 15:59:15 -0700 | [diff] [blame] | 655 | } |