blob: 4f49f15bfdfba3602c4bea27c6b12b9931781b2d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk5dfa25f2002-10-19 19:42:10 +00002/*
Detlev Zundel2dce5512009-03-25 17:27:52 +01003 * (C) Copyright 2000-2009
wdenk5dfa25f2002-10-19 19:42:10 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk5dfa25f2002-10-19 19:42:10 +00005 */
6
7/*
8 * Command Processor Table
9 */
10
11#include <common.h>
12#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070013#include <console.h>
Simon Glassaf95f202019-08-01 09:46:40 -060014#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Jason Hobbs4d91a6e2011-08-23 11:06:54 +000016#include <linux/ctype.h>
wdenk5dfa25f2002-10-19 19:42:10 +000017
wdenk5dfa25f2002-10-19 19:42:10 +000018/*
19 * Use puts() instead of printf() to avoid printf buffer overflow
20 * for long help messages
21 */
Detlev Zundel2dce5512009-03-25 17:27:52 +010022
Simon Glass09140112020-05-10 11:40:03 -060023int _do_help(struct cmd_tbl *cmd_start, int cmd_items, struct cmd_tbl *cmdtp,
24 int flag, int argc, char *const argv[])
wdenk5dfa25f2002-10-19 19:42:10 +000025{
26 int i;
27 int rcode = 0;
28
Masahiro Yamada616e2162014-10-23 17:27:30 +090029 if (argc == 1) { /* show list of commands */
Simon Glass09140112020-05-10 11:40:03 -060030 struct cmd_tbl *cmd_array[cmd_items];
wdenk9d2b18a2003-06-28 23:11:04 +000031 int i, j, swaps;
wdenk8bde7f72003-06-27 21:31:46 +000032
wdenk9d2b18a2003-06-28 23:11:04 +000033 /* Make array of commands from .uboot_cmd section */
Detlev Zundel2dce5512009-03-25 17:27:52 +010034 cmdtp = cmd_start;
wdenk9d2b18a2003-06-28 23:11:04 +000035 for (i = 0; i < cmd_items; i++) {
36 cmd_array[i] = cmdtp++;
wdenk8bde7f72003-06-27 21:31:46 +000037 }
wdenk8bde7f72003-06-27 21:31:46 +000038
wdenk9d2b18a2003-06-28 23:11:04 +000039 /* Sort command list (trivial bubble sort) */
40 for (i = cmd_items - 1; i > 0; --i) {
41 swaps = 0;
42 for (j = 0; j < i; ++j) {
Masahiro Yamada616e2162014-10-23 17:27:30 +090043 if (strcmp(cmd_array[j]->name,
44 cmd_array[j + 1]->name) > 0) {
Simon Glass09140112020-05-10 11:40:03 -060045 struct cmd_tbl *tmp;
wdenk9d2b18a2003-06-28 23:11:04 +000046 tmp = cmd_array[j];
47 cmd_array[j] = cmd_array[j + 1];
48 cmd_array[j + 1] = tmp;
49 ++swaps;
50 }
wdenk8bde7f72003-06-27 21:31:46 +000051 }
wdenk9d2b18a2003-06-28 23:11:04 +000052 if (!swaps)
53 break;
wdenk8bde7f72003-06-27 21:31:46 +000054 }
55
56 /* print short help (usage) */
wdenk9d2b18a2003-06-28 23:11:04 +000057 for (i = 0; i < cmd_items; i++) {
58 const char *usage = cmd_array[i]->usage;
59
wdenk5dfa25f2002-10-19 19:42:10 +000060 /* allow user abort */
Masahiro Yamada616e2162014-10-23 17:27:30 +090061 if (ctrlc())
wdenk5dfa25f2002-10-19 19:42:10 +000062 return 1;
wdenk9d2b18a2003-06-28 23:11:04 +000063 if (usage == NULL)
wdenk5dfa25f2002-10-19 19:42:10 +000064 continue;
Peter Tyser2fb26042009-01-27 18:03:12 -060065 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
66 cmd_array[i]->name, usage);
wdenk5dfa25f2002-10-19 19:42:10 +000067 }
wdenk5dfa25f2002-10-19 19:42:10 +000068 return 0;
69 }
wdenk5dfa25f2002-10-19 19:42:10 +000070 /*
71 * command help (long version)
72 */
wdenk8bde7f72003-06-27 21:31:46 +000073 for (i = 1; i < argc; ++i) {
Masahiro Yamada616e2162014-10-23 17:27:30 +090074 cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items);
75 if (cmdtp != NULL) {
Wolfgang Denk94796d82009-05-24 19:17:29 +020076 rcode |= cmd_usage(cmdtp);
wdenk71f95112003-06-15 22:40:42 +000077 } else {
Masahiro Yamada616e2162014-10-23 17:27:30 +090078 printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n",
79 argv[i]);
wdenk5dfa25f2002-10-19 19:42:10 +000080 rcode = 1;
81 }
82 }
83 return rcode;
84}
85
Masahiro Yamada616e2162014-10-23 17:27:30 +090086/* find command table entry for a command */
Simon Glass09140112020-05-10 11:40:03 -060087struct cmd_tbl *find_cmd_tbl(const char *cmd, struct cmd_tbl *table,
88 int table_len)
wdenk5dfa25f2002-10-19 19:42:10 +000089{
Simon Glassf8bb6962016-03-19 02:18:38 -060090#ifdef CONFIG_CMDLINE
Simon Glass09140112020-05-10 11:40:03 -060091 struct cmd_tbl *cmdtp;
92 struct cmd_tbl *cmdtp_temp = table; /* Init value */
wdenk9d2b18a2003-06-28 23:11:04 +000093 const char *p;
94 int len;
95 int n_found = 0;
wdenk8bde7f72003-06-27 21:31:46 +000096
Thomas Weber7013c062010-11-24 13:07:51 +010097 if (!cmd)
98 return NULL;
wdenk9d2b18a2003-06-28 23:11:04 +000099 /*
100 * Some commands allow length modifiers (like "cp.b");
101 * compare command name only until first dot.
102 */
103 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
104
Masahiro Yamada616e2162014-10-23 17:27:30 +0900105 for (cmdtp = table; cmdtp != table + table_len; cmdtp++) {
106 if (strncmp(cmd, cmdtp->name, len) == 0) {
107 if (len == strlen(cmdtp->name))
wdenk9d2b18a2003-06-28 23:11:04 +0000108 return cmdtp; /* full match */
109
110 cmdtp_temp = cmdtp; /* abbreviated command ? */
111 n_found++;
112 }
wdenk5dfa25f2002-10-19 19:42:10 +0000113 }
wdenk9d2b18a2003-06-28 23:11:04 +0000114 if (n_found == 1) { /* exactly one match */
wdenk8bde7f72003-06-27 21:31:46 +0000115 return cmdtp_temp;
wdenk9d2b18a2003-06-28 23:11:04 +0000116 }
Simon Glassf8bb6962016-03-19 02:18:38 -0600117#endif /* CONFIG_CMDLINE */
wdenk8bde7f72003-06-27 21:31:46 +0000118
wdenk9d2b18a2003-06-28 23:11:04 +0000119 return NULL; /* not found or ambiguous command */
wdenk5dfa25f2002-10-19 19:42:10 +0000120}
wdenk04a85b32004-04-15 18:22:41 +0000121
Simon Glass09140112020-05-10 11:40:03 -0600122struct cmd_tbl *find_cmd(const char *cmd)
Kumar Galab799cb42008-09-23 10:05:02 -0500123{
Simon Glass09140112020-05-10 11:40:03 -0600124 struct cmd_tbl *start = ll_entry_start(struct cmd_tbl, cmd);
125 const int len = ll_entry_count(struct cmd_tbl, cmd);
Marek Vasut6c7c9462012-10-12 10:27:04 +0000126 return find_cmd_tbl(cmd, start, len);
Kumar Galab799cb42008-09-23 10:05:02 -0500127}
128
Simon Glass09140112020-05-10 11:40:03 -0600129int cmd_usage(const struct cmd_tbl *cmdtp)
Peter Tyser62c3ae72009-01-27 18:03:10 -0600130{
Wolfgang Denk94796d82009-05-24 19:17:29 +0200131 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
132
133#ifdef CONFIG_SYS_LONGHELP
134 printf("Usage:\n%s ", cmdtp->name);
135
136 if (!cmdtp->help) {
137 puts ("- No additional help available.\n");
138 return 1;
139 }
140
Masahiro Yamada616e2162014-10-23 17:27:30 +0900141 puts(cmdtp->help);
142 putc('\n');
Wolfgang Denk94796d82009-05-24 19:17:29 +0200143#endif /* CONFIG_SYS_LONGHELP */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200144 return 1;
Peter Tyser62c3ae72009-01-27 18:03:10 -0600145}
146
wdenk04a85b32004-04-15 18:22:41 +0000147#ifdef CONFIG_AUTO_COMPLETE
Boris Brezillon03dcf172018-12-05 09:26:50 +0100148static char env_complete_buf[512];
wdenk04a85b32004-04-15 18:22:41 +0000149
Simon Glass09140112020-05-10 11:40:03 -0600150int var_complete(int argc, char *const argv[], char last_char, int maxv,
151 char *cmdv[])
wdenk04a85b32004-04-15 18:22:41 +0000152{
wdenk04a85b32004-04-15 18:22:41 +0000153 int space;
154
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000155 space = last_char == '\0' || isblank(last_char);
wdenk04a85b32004-04-15 18:22:41 +0000156
157 if (space && argc == 1)
Boris Brezillon03dcf172018-12-05 09:26:50 +0100158 return env_complete("", maxv, cmdv, sizeof(env_complete_buf),
159 env_complete_buf, false);
wdenk04a85b32004-04-15 18:22:41 +0000160
161 if (!space && argc == 2)
Boris Brezillon03dcf172018-12-05 09:26:50 +0100162 return env_complete(argv[1], maxv, cmdv,
163 sizeof(env_complete_buf),
164 env_complete_buf, false);
Mike Frysinger560d4242010-12-17 16:51:59 -0500165
wdenk04a85b32004-04-15 18:22:41 +0000166 return 0;
167}
168
Simon Glass09140112020-05-10 11:40:03 -0600169static int dollar_complete(int argc, char *const argv[], char last_char,
Boris Brezillon03dcf172018-12-05 09:26:50 +0100170 int maxv, char *cmdv[])
171{
172 /* Make sure the last argument starts with a $. */
173 if (argc < 1 || argv[argc - 1][0] != '$' ||
174 last_char == '\0' || isblank(last_char))
175 return 0;
176
177 return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf),
178 env_complete_buf, true);
179}
180
wdenk04a85b32004-04-15 18:22:41 +0000181/*************************************************************************************/
182
Simon Glass09140112020-05-10 11:40:03 -0600183int complete_subcmdv(struct cmd_tbl *cmdtp, int count, int argc,
184 char *const argv[], char last_char,
Boris Brezillon6fb61442018-12-03 22:54:19 +0100185 int maxv, char *cmdv[])
wdenk04a85b32004-04-15 18:22:41 +0000186{
Simon Glassf8bb6962016-03-19 02:18:38 -0600187#ifdef CONFIG_CMDLINE
Simon Glass09140112020-05-10 11:40:03 -0600188 const struct cmd_tbl *cmdend = cmdtp + count;
wdenk04a85b32004-04-15 18:22:41 +0000189 const char *p;
190 int len, clen;
191 int n_found = 0;
192 const char *cmd;
193
194 /* sanity? */
195 if (maxv < 2)
196 return -2;
197
198 cmdv[0] = NULL;
199
200 if (argc == 0) {
201 /* output full list of commands */
Marek Vasut6c7c9462012-10-12 10:27:04 +0000202 for (; cmdtp != cmdend; cmdtp++) {
wdenk04a85b32004-04-15 18:22:41 +0000203 if (n_found >= maxv - 2) {
Andrew Gabbasov9b438942013-12-27 10:05:14 -0600204 cmdv[n_found++] = "...";
wdenk04a85b32004-04-15 18:22:41 +0000205 break;
206 }
Andrew Gabbasov9b438942013-12-27 10:05:14 -0600207 cmdv[n_found++] = cmdtp->name;
wdenk04a85b32004-04-15 18:22:41 +0000208 }
209 cmdv[n_found] = NULL;
210 return n_found;
211 }
212
213 /* more than one arg or one but the start of the next */
Masahiro Yamada616e2162014-10-23 17:27:30 +0900214 if (argc > 1 || last_char == '\0' || isblank(last_char)) {
Boris Brezillon6fb61442018-12-03 22:54:19 +0100215 cmdtp = find_cmd_tbl(argv[0], cmdtp, count);
wdenk04a85b32004-04-15 18:22:41 +0000216 if (cmdtp == NULL || cmdtp->complete == NULL) {
217 cmdv[0] = NULL;
218 return 0;
219 }
220 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
221 }
222
223 cmd = argv[0];
224 /*
225 * Some commands allow length modifiers (like "cp.b");
226 * compare command name only until first dot.
227 */
228 p = strchr(cmd, '.');
229 if (p == NULL)
230 len = strlen(cmd);
231 else
232 len = p - cmd;
233
234 /* return the partial matches */
Marek Vasut6c7c9462012-10-12 10:27:04 +0000235 for (; cmdtp != cmdend; cmdtp++) {
wdenk04a85b32004-04-15 18:22:41 +0000236
237 clen = strlen(cmdtp->name);
238 if (clen < len)
239 continue;
240
241 if (memcmp(cmd, cmdtp->name, len) != 0)
242 continue;
243
244 /* too many! */
245 if (n_found >= maxv - 2) {
246 cmdv[n_found++] = "...";
247 break;
248 }
249
250 cmdv[n_found++] = cmdtp->name;
251 }
252
253 cmdv[n_found] = NULL;
254 return n_found;
Simon Glassf8bb6962016-03-19 02:18:38 -0600255#else
256 return 0;
257#endif
wdenk04a85b32004-04-15 18:22:41 +0000258}
259
Simon Glass09140112020-05-10 11:40:03 -0600260static int complete_cmdv(int argc, char *const argv[], char last_char,
Boris Brezillon6fb61442018-12-03 22:54:19 +0100261 int maxv, char *cmdv[])
262{
263#ifdef CONFIG_CMDLINE
Simon Glass09140112020-05-10 11:40:03 -0600264 return complete_subcmdv(ll_entry_start(struct cmd_tbl, cmd),
265 ll_entry_count(struct cmd_tbl, cmd), argc, argv,
Boris Brezillon6fb61442018-12-03 22:54:19 +0100266 last_char, maxv, cmdv);
267#else
268 return 0;
269#endif
270}
271
wdenk04a85b32004-04-15 18:22:41 +0000272static int make_argv(char *s, int argvsz, char *argv[])
273{
274 int argc = 0;
275
276 /* split into argv */
277 while (argc < argvsz - 1) {
278
279 /* skip any white space */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000280 while (isblank(*s))
wdenk04a85b32004-04-15 18:22:41 +0000281 ++s;
282
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200283 if (*s == '\0') /* end of s, no more args */
wdenk04a85b32004-04-15 18:22:41 +0000284 break;
285
286 argv[argc++] = s; /* begin of argument string */
287
288 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000289 while (*s && !isblank(*s))
wdenk04a85b32004-04-15 18:22:41 +0000290 ++s;
291
292 if (*s == '\0') /* end of s, no more args */
293 break;
294
295 *s++ = '\0'; /* terminate current arg */
296 }
297 argv[argc] = NULL;
298
299 return argc;
300}
301
Simon Glass09140112020-05-10 11:40:03 -0600302static void print_argv(const char *banner, const char *leader, const char *sep,
303 int linemax, char *const argv[])
wdenk04a85b32004-04-15 18:22:41 +0000304{
305 int ll = leader != NULL ? strlen(leader) : 0;
306 int sl = sep != NULL ? strlen(sep) : 0;
307 int len, i;
308
309 if (banner) {
310 puts("\n");
311 puts(banner);
312 }
313
314 i = linemax; /* force leader and newline */
315 while (*argv != NULL) {
316 len = strlen(*argv) + sl;
317 if (i + len >= linemax) {
318 puts("\n");
319 if (leader)
320 puts(leader);
321 i = ll - sl;
322 } else if (sep)
323 puts(sep);
324 puts(*argv++);
325 i += len;
326 }
327 printf("\n");
328}
329
Simon Glass09140112020-05-10 11:40:03 -0600330static int find_common_prefix(char *const argv[])
wdenk04a85b32004-04-15 18:22:41 +0000331{
332 int i, len;
333 char *anchor, *s, *t;
334
335 if (*argv == NULL)
336 return 0;
337
338 /* begin with max */
339 anchor = *argv++;
340 len = strlen(anchor);
341 while ((t = *argv++) != NULL) {
342 s = anchor;
343 for (i = 0; i < len; i++, t++, s++) {
344 if (*t != *s)
345 break;
346 }
347 len = s - anchor;
348 }
349 return len;
350}
351
Heinrich Schuchardt2614a202017-11-19 23:07:50 +0100352static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */
wdenk04a85b32004-04-15 18:22:41 +0000353
354int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
355{
356 int n = *np, col = *colp;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200357 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenk04a85b32004-04-15 18:22:41 +0000358 char *cmdv[20];
359 char *s, *t;
360 const char *sep;
361 int i, j, k, len, seplen, argc;
362 int cnt;
363 char last_char;
Marek Vasut4225f832019-06-27 00:17:27 +0200364#ifdef CONFIG_CMDLINE_PS_SUPPORT
365 const char *ps_prompt = env_get("PS1");
366#else
367 const char *ps_prompt = CONFIG_SYS_PROMPT;
368#endif
wdenk04a85b32004-04-15 18:22:41 +0000369
Marek Vasut4225f832019-06-27 00:17:27 +0200370 if (strcmp(prompt, ps_prompt) != 0)
wdenk04a85b32004-04-15 18:22:41 +0000371 return 0; /* not in normal console */
372
373 cnt = strlen(buf);
374 if (cnt >= 1)
375 last_char = buf[cnt - 1];
376 else
377 last_char = '\0';
378
379 /* copy to secondary buffer which will be affected */
380 strcpy(tmp_buf, buf);
381
382 /* separate into argv */
383 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
384
Boris Brezillon03dcf172018-12-05 09:26:50 +0100385 /* first try a $ completion */
386 i = dollar_complete(argc, argv, last_char,
387 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
388 if (!i) {
389 /* do the completion and return the possible completions */
390 i = complete_cmdv(argc, argv, last_char,
391 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
392 }
wdenk04a85b32004-04-15 18:22:41 +0000393
394 /* no match; bell and out */
395 if (i == 0) {
396 if (argc > 1) /* allow tab for non command */
397 return 0;
398 putc('\a');
399 return 1;
400 }
401
402 s = NULL;
403 len = 0;
404 sep = NULL;
405 seplen = 0;
406 if (i == 1) { /* one match; perfect */
Boris Brezilloncbe07eb2018-12-03 22:54:18 +0100407 if (last_char != '\0' && !isblank(last_char))
408 k = strlen(argv[argc - 1]);
409 else
410 k = 0;
411
wdenk04a85b32004-04-15 18:22:41 +0000412 s = cmdv[0] + k;
413 len = strlen(s);
414 sep = " ";
415 seplen = 1;
Masahiro Yamada616e2162014-10-23 17:27:30 +0900416 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
Boris Brezilloncbe07eb2018-12-03 22:54:18 +0100417 if (last_char != '\0' && !isblank(last_char))
418 k = strlen(argv[argc - 1]);
419 else
420 k = 0;
421
wdenk04a85b32004-04-15 18:22:41 +0000422 j -= k;
423 if (j > 0) {
424 s = cmdv[0] + k;
425 len = j;
426 }
427 }
428
429 if (s != NULL) {
430 k = len + seplen;
431 /* make sure it fits */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200432 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
wdenk04a85b32004-04-15 18:22:41 +0000433 putc('\a');
434 return 1;
435 }
436
437 t = buf + cnt;
438 for (i = 0; i < len; i++)
439 *t++ = *s++;
440 if (sep != NULL)
441 for (i = 0; i < seplen; i++)
442 *t++ = sep[i];
443 *t = '\0';
444 n += k;
445 col += k;
446 puts(t - k);
447 if (sep == NULL)
448 putc('\a');
449 *np = n;
450 *colp = col;
451 } else {
452 print_argv(NULL, " ", " ", 78, cmdv);
453
454 puts(prompt);
455 puts(buf);
456 }
457 return 1;
458}
459
460#endif
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200461
462#ifdef CMD_DATA_SIZE
463int cmd_get_data_size(char* arg, int default_size)
464{
465 /* Check for a size specification .b, .w or .l.
466 */
467 int len = strlen(arg);
468 if (len > 2 && arg[len-2] == '.') {
Masahiro Yamada616e2162014-10-23 17:27:30 +0900469 switch (arg[len-1]) {
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200470 case 'b':
471 return 1;
472 case 'w':
473 return 2;
474 case 'l':
475 return 4;
Simon Glass4d979bf2019-12-28 10:45:10 -0700476#ifdef MEM_SUPPORT_64BIT_DATA
York Sun4d1fd7f2014-02-26 17:03:19 -0800477 case 'q':
478 return 8;
479#endif
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200480 case 's':
481 return -2;
482 default:
483 return -1;
484 }
485 }
486 return default_size;
487}
488#endif
Heiko Schocher620f1f62010-09-17 13:10:33 +0200489
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200490#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher620f1f62010-09-17 13:10:33 +0200491DECLARE_GLOBAL_DATA_PTR;
492
Simon Glass09140112020-05-10 11:40:03 -0600493void fixup_cmdtable(struct cmd_tbl *cmdtp, int size)
Heiko Schocher620f1f62010-09-17 13:10:33 +0200494{
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 Simek6f94daf2019-10-21 12:54:20 +0200503 addr = (ulong)(cmdtp->cmd_rep) + gd->reloc_off;
504 cmdtp->cmd_rep =
Simon Glass09140112020-05-10 11:40:03 -0600505 (int (*)(struct cmd_tbl *, int, int,
Michal Simek6f94daf2019-10-21 12:54:20 +0200506 char * const [], int *))addr;
507
Masahiro Yamada616e2162014-10-23 17:27:30 +0900508 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
Kun-Hua Huang2e88bb22015-08-24 14:52:35 +0800509#ifdef DEBUG_COMMANDS
Heiko Schocher620f1f62010-09-17 13:10:33 +0200510 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
Masahiro Yamada616e2162014-10-23 17:27:30 +0900511 cmdtp->name, (ulong)(cmdtp->cmd), addr);
Heiko Schocher620f1f62010-09-17 13:10:33 +0200512#endif
Simon Glass09140112020-05-10 11:40:03 -0600513 cmdtp->cmd = (int (*)(struct cmd_tbl *, int, int,
514 char *const []))addr;
Heiko Schocher620f1f62010-09-17 13:10:33 +0200515 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 Schwierzeckfa28bd22011-10-13 06:07:39 +0000527#ifdef CONFIG_AUTO_COMPLETE
528 if (cmdtp->complete) {
529 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
Daniel Schwierzeck3668d8f2011-10-18 11:12:22 +0000530 cmdtp->complete =
531 (int (*)(int, char * const [], char, int, char * []))addr;
Daniel Schwierzeckfa28bd22011-10-13 06:07:39 +0000532 }
533#endif
Heiko Schocher620f1f62010-09-17 13:10:33 +0200534 cmdtp++;
535 }
536}
537#endif
Simon Glassbdf8e342012-02-14 19:59:23 +0000538
Simon Glass09140112020-05-10 11:40:03 -0600539int cmd_always_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
540 char *const argv[], int *repeatable)
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100541{
542 *repeatable = 1;
543
544 return cmdtp->cmd(cmdtp, flag, argc, argv);
545}
546
Simon Glass09140112020-05-10 11:40:03 -0600547int cmd_never_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
548 char *const argv[], int *repeatable)
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100549{
550 *repeatable = 0;
551
552 return cmdtp->cmd(cmdtp, flag, argc, argv);
553}
554
Simon Glass09140112020-05-10 11:40:03 -0600555int cmd_discard_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
556 char *const argv[])
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100557{
558 int repeatable;
559
560 return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable);
561}
562
Simon Glassbdf8e342012-02-14 19:59:23 +0000563/**
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 Brezillon80a48dd2018-12-03 22:54:20 +0100572 * @param repeatable Can the command be repeated
Simon Glassbdf8e342012-02-14 19:59:23 +0000573 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
574 */
Simon Glass09140112020-05-10 11:40:03 -0600575static int cmd_call(struct cmd_tbl *cmdtp, int flag, int argc,
576 char *const argv[], int *repeatable)
Simon Glassbdf8e342012-02-14 19:59:23 +0000577{
578 int result;
579
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100580 result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable);
Simon Glassbdf8e342012-02-14 19:59:23 +0000581 if (result)
Peng Fan58b6ad62015-07-28 22:45:37 +0800582 debug("Command failed, result=%d\n", result);
Simon Glassbdf8e342012-02-14 19:59:23 +0000583 return result;
584}
Simon Glass9d12d5d2012-02-14 19:59:25 +0000585
Simon Glass09140112020-05-10 11:40:03 -0600586enum command_ret_t cmd_process(int flag, int argc, char *const argv[],
Richard Genoud34765e82012-12-03 06:28:28 +0000587 int *repeatable, ulong *ticks)
Simon Glass9d12d5d2012-02-14 19:59:25 +0000588{
589 enum command_ret_t rc = CMD_RET_SUCCESS;
Simon Glass09140112020-05-10 11:40:03 -0600590 struct cmd_tbl *cmdtp;
Simon Glass9d12d5d2012-02-14 19:59:25 +0000591
Christoph Muellner7ae31fc2019-04-05 13:03:46 +0200592#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 Glass9d12d5d2012-02-14 19:59:25 +0000606 /* 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 Brezillon80a48dd2018-12-03 22:54:20 +0100631 int newrep;
632
Richard Genoud34765e82012-12-03 06:28:28 +0000633 if (ticks)
634 *ticks = get_timer(0);
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100635 rc = cmd_call(cmdtp, flag, argc, argv, &newrep);
Richard Genoud34765e82012-12-03 06:28:28 +0000636 if (ticks)
637 *ticks = get_timer(*ticks);
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100638 *repeatable &= newrep;
Simon Glass9d12d5d2012-02-14 19:59:25 +0000639 }
640 if (rc == CMD_RET_USAGE)
641 rc = cmd_usage(cmdtp);
642 return rc;
643}
Simon Glass16ff9902014-02-26 15:59:15 -0700644
Simon Glass09140112020-05-10 11:40:03 -0600645int cmd_process_error(struct cmd_tbl *cmdtp, int err)
Simon Glass16ff9902014-02-26 15:59:15 -0700646{
Michal Simek27eb7bc2018-06-21 14:49:26 +0200647 if (err == CMD_RET_USAGE)
648 return CMD_RET_USAGE;
649
Simon Glass16ff9902014-02-26 15:59:15 -0700650 if (err) {
651 printf("Command '%s' failed: Error %d\n", cmdtp->name, err);
Michal Simek37233242018-06-04 13:29:49 +0200652 return CMD_RET_FAILURE;
Simon Glass16ff9902014-02-26 15:59:15 -0700653 }
654
Michal Simek37233242018-06-04 13:29:49 +0200655 return CMD_RET_SUCCESS;
Simon Glass16ff9902014-02-26 15:59:15 -0700656}