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