blob: 4b887a267fb6d82d3cd827fe1558cf0b087a5c54 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk5dfa25f2002-10-19 19:42:10 +00002/*
Detlev Zundel5e6c06b2009-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 Glassa73bda42015-11-08 23:47:45 -070013#include <console.h>
Simon Glassbf4229c2019-08-01 09:46:40 -060014#include <env.h>
Jason Hobbscafa1aa2011-08-23 11:06:54 +000015#include <linux/ctype.h>
wdenk5dfa25f2002-10-19 19:42:10 +000016
wdenk5dfa25f2002-10-19 19:42:10 +000017/*
18 * Use puts() instead of printf() to avoid printf buffer overflow
19 * for long help messages
20 */
Detlev Zundel5e6c06b2009-03-25 17:27:52 +010021
Masahiro Yamada682195e2014-10-23 17:27:30 +090022int _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t *cmdtp, int flag,
23 int argc, char * const argv[])
wdenk5dfa25f2002-10-19 19:42:10 +000024{
25 int i;
26 int rcode = 0;
27
Masahiro Yamada682195e2014-10-23 17:27:30 +090028 if (argc == 1) { /* show list of commands */
Anatolij Gustschin9ac7cc22012-11-08 12:40:17 +010029 cmd_tbl_t *cmd_array[cmd_items];
wdenk3086a972003-06-28 23:11:04 +000030 int i, j, swaps;
wdenk57b2d802003-06-27 21:31:46 +000031
wdenk3086a972003-06-28 23:11:04 +000032 /* Make array of commands from .uboot_cmd section */
Detlev Zundel5e6c06b2009-03-25 17:27:52 +010033 cmdtp = cmd_start;
wdenk3086a972003-06-28 23:11:04 +000034 for (i = 0; i < cmd_items; i++) {
35 cmd_array[i] = cmdtp++;
wdenk57b2d802003-06-27 21:31:46 +000036 }
wdenk57b2d802003-06-27 21:31:46 +000037
wdenk3086a972003-06-28 23:11:04 +000038 /* Sort command list (trivial bubble sort) */
39 for (i = cmd_items - 1; i > 0; --i) {
40 swaps = 0;
41 for (j = 0; j < i; ++j) {
Masahiro Yamada682195e2014-10-23 17:27:30 +090042 if (strcmp(cmd_array[j]->name,
43 cmd_array[j + 1]->name) > 0) {
wdenk3086a972003-06-28 23:11:04 +000044 cmd_tbl_t *tmp;
45 tmp = cmd_array[j];
46 cmd_array[j] = cmd_array[j + 1];
47 cmd_array[j + 1] = tmp;
48 ++swaps;
49 }
wdenk57b2d802003-06-27 21:31:46 +000050 }
wdenk3086a972003-06-28 23:11:04 +000051 if (!swaps)
52 break;
wdenk57b2d802003-06-27 21:31:46 +000053 }
54
55 /* print short help (usage) */
wdenk3086a972003-06-28 23:11:04 +000056 for (i = 0; i < cmd_items; i++) {
57 const char *usage = cmd_array[i]->usage;
58
wdenk5dfa25f2002-10-19 19:42:10 +000059 /* allow user abort */
Masahiro Yamada682195e2014-10-23 17:27:30 +090060 if (ctrlc())
wdenk5dfa25f2002-10-19 19:42:10 +000061 return 1;
wdenk3086a972003-06-28 23:11:04 +000062 if (usage == NULL)
wdenk5dfa25f2002-10-19 19:42:10 +000063 continue;
Peter Tyserdfb72b82009-01-27 18:03:12 -060064 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
65 cmd_array[i]->name, usage);
wdenk5dfa25f2002-10-19 19:42:10 +000066 }
wdenk5dfa25f2002-10-19 19:42:10 +000067 return 0;
68 }
wdenk5dfa25f2002-10-19 19:42:10 +000069 /*
70 * command help (long version)
71 */
wdenk57b2d802003-06-27 21:31:46 +000072 for (i = 1; i < argc; ++i) {
Masahiro Yamada682195e2014-10-23 17:27:30 +090073 cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items);
74 if (cmdtp != NULL) {
Wolfgang Denk9dd775b2009-05-24 19:17:29 +020075 rcode |= cmd_usage(cmdtp);
wdenk7a428cc2003-06-15 22:40:42 +000076 } else {
Masahiro Yamada682195e2014-10-23 17:27:30 +090077 printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n",
78 argv[i]);
wdenk5dfa25f2002-10-19 19:42:10 +000079 rcode = 1;
80 }
81 }
82 return rcode;
83}
84
Masahiro Yamada682195e2014-10-23 17:27:30 +090085/* find command table entry for a command */
86cmd_tbl_t *find_cmd_tbl(const char *cmd, cmd_tbl_t *table, int table_len)
wdenk5dfa25f2002-10-19 19:42:10 +000087{
Simon Glass4ef90b72016-03-19 02:18:38 -060088#ifdef CONFIG_CMDLINE
wdenk5dfa25f2002-10-19 19:42:10 +000089 cmd_tbl_t *cmdtp;
Masahiro Yamada682195e2014-10-23 17:27:30 +090090 cmd_tbl_t *cmdtp_temp = table; /* Init value */
wdenk3086a972003-06-28 23:11:04 +000091 const char *p;
92 int len;
93 int n_found = 0;
wdenk57b2d802003-06-27 21:31:46 +000094
Thomas Weber97ea46f2010-11-24 13:07:51 +010095 if (!cmd)
96 return NULL;
wdenk3086a972003-06-28 23:11:04 +000097 /*
98 * Some commands allow length modifiers (like "cp.b");
99 * compare command name only until first dot.
100 */
101 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
102
Masahiro Yamada682195e2014-10-23 17:27:30 +0900103 for (cmdtp = table; cmdtp != table + table_len; cmdtp++) {
104 if (strncmp(cmd, cmdtp->name, len) == 0) {
105 if (len == strlen(cmdtp->name))
wdenk3086a972003-06-28 23:11:04 +0000106 return cmdtp; /* full match */
107
108 cmdtp_temp = cmdtp; /* abbreviated command ? */
109 n_found++;
110 }
wdenk5dfa25f2002-10-19 19:42:10 +0000111 }
wdenk3086a972003-06-28 23:11:04 +0000112 if (n_found == 1) { /* exactly one match */
wdenk57b2d802003-06-27 21:31:46 +0000113 return cmdtp_temp;
wdenk3086a972003-06-28 23:11:04 +0000114 }
Simon Glass4ef90b72016-03-19 02:18:38 -0600115#endif /* CONFIG_CMDLINE */
wdenk5dfa25f2002-10-19 19:42:10 +0000116
wdenk3086a972003-06-28 23:11:04 +0000117 return NULL; /* not found or ambiguous command */
wdenk57b2d802003-06-27 21:31:46 +0000118}
wdenk3902d702004-04-15 18:22:41 +0000119
Masahiro Yamada682195e2014-10-23 17:27:30 +0900120cmd_tbl_t *find_cmd(const char *cmd)
Kumar Galaac2dad62008-09-23 10:05:02 -0500121{
Marek Vasut69c5e342012-10-12 10:27:04 +0000122 cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd);
123 const int len = ll_entry_count(cmd_tbl_t, cmd);
124 return find_cmd_tbl(cmd, start, len);
Kumar Galaac2dad62008-09-23 10:05:02 -0500125}
126
Mike Frysinger33e84d12011-04-23 23:43:24 +0000127int cmd_usage(const cmd_tbl_t *cmdtp)
Peter Tyserddb3af92009-01-27 18:03:10 -0600128{
Wolfgang Denk9dd775b2009-05-24 19:17:29 +0200129 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
130
131#ifdef CONFIG_SYS_LONGHELP
132 printf("Usage:\n%s ", cmdtp->name);
133
134 if (!cmdtp->help) {
135 puts ("- No additional help available.\n");
136 return 1;
137 }
138
Masahiro Yamada682195e2014-10-23 17:27:30 +0900139 puts(cmdtp->help);
140 putc('\n');
Wolfgang Denk9dd775b2009-05-24 19:17:29 +0200141#endif /* CONFIG_SYS_LONGHELP */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200142 return 1;
Peter Tyserddb3af92009-01-27 18:03:10 -0600143}
144
wdenk3902d702004-04-15 18:22:41 +0000145#ifdef CONFIG_AUTO_COMPLETE
Boris Brezillon619fc682018-12-05 09:26:50 +0100146static char env_complete_buf[512];
wdenk3902d702004-04-15 18:22:41 +0000147
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200148int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
wdenk3902d702004-04-15 18:22:41 +0000149{
wdenk3902d702004-04-15 18:22:41 +0000150 int space;
151
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000152 space = last_char == '\0' || isblank(last_char);
wdenk3902d702004-04-15 18:22:41 +0000153
154 if (space && argc == 1)
Boris Brezillon619fc682018-12-05 09:26:50 +0100155 return env_complete("", maxv, cmdv, sizeof(env_complete_buf),
156 env_complete_buf, false);
wdenk3902d702004-04-15 18:22:41 +0000157
158 if (!space && argc == 2)
Boris Brezillon619fc682018-12-05 09:26:50 +0100159 return env_complete(argv[1], maxv, cmdv,
160 sizeof(env_complete_buf),
161 env_complete_buf, false);
Mike Frysinger8d882322010-12-17 16:51:59 -0500162
wdenk3902d702004-04-15 18:22:41 +0000163 return 0;
164}
165
Boris Brezillon619fc682018-12-05 09:26:50 +0100166static int dollar_complete(int argc, char * const argv[], char last_char,
167 int maxv, char *cmdv[])
168{
169 /* Make sure the last argument starts with a $. */
170 if (argc < 1 || argv[argc - 1][0] != '$' ||
171 last_char == '\0' || isblank(last_char))
172 return 0;
173
174 return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf),
175 env_complete_buf, true);
176}
177
wdenk3902d702004-04-15 18:22:41 +0000178/*************************************************************************************/
179
Boris Brezillonae73c332018-12-03 22:54:19 +0100180int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc,
181 char * const argv[], char last_char,
182 int maxv, char *cmdv[])
wdenk3902d702004-04-15 18:22:41 +0000183{
Simon Glass4ef90b72016-03-19 02:18:38 -0600184#ifdef CONFIG_CMDLINE
Marek Vasut69c5e342012-10-12 10:27:04 +0000185 const cmd_tbl_t *cmdend = cmdtp + count;
wdenk3902d702004-04-15 18:22:41 +0000186 const char *p;
187 int len, clen;
188 int n_found = 0;
189 const char *cmd;
190
191 /* sanity? */
192 if (maxv < 2)
193 return -2;
194
195 cmdv[0] = NULL;
196
197 if (argc == 0) {
198 /* output full list of commands */
Marek Vasut69c5e342012-10-12 10:27:04 +0000199 for (; cmdtp != cmdend; cmdtp++) {
wdenk3902d702004-04-15 18:22:41 +0000200 if (n_found >= maxv - 2) {
Andrew Gabbasova7c03b02013-12-27 10:05:14 -0600201 cmdv[n_found++] = "...";
wdenk3902d702004-04-15 18:22:41 +0000202 break;
203 }
Andrew Gabbasova7c03b02013-12-27 10:05:14 -0600204 cmdv[n_found++] = cmdtp->name;
wdenk3902d702004-04-15 18:22:41 +0000205 }
206 cmdv[n_found] = NULL;
207 return n_found;
208 }
209
210 /* more than one arg or one but the start of the next */
Masahiro Yamada682195e2014-10-23 17:27:30 +0900211 if (argc > 1 || last_char == '\0' || isblank(last_char)) {
Boris Brezillonae73c332018-12-03 22:54:19 +0100212 cmdtp = find_cmd_tbl(argv[0], cmdtp, count);
wdenk3902d702004-04-15 18:22:41 +0000213 if (cmdtp == NULL || cmdtp->complete == NULL) {
214 cmdv[0] = NULL;
215 return 0;
216 }
217 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
218 }
219
220 cmd = argv[0];
221 /*
222 * Some commands allow length modifiers (like "cp.b");
223 * compare command name only until first dot.
224 */
225 p = strchr(cmd, '.');
226 if (p == NULL)
227 len = strlen(cmd);
228 else
229 len = p - cmd;
230
231 /* return the partial matches */
Marek Vasut69c5e342012-10-12 10:27:04 +0000232 for (; cmdtp != cmdend; cmdtp++) {
wdenk3902d702004-04-15 18:22:41 +0000233
234 clen = strlen(cmdtp->name);
235 if (clen < len)
236 continue;
237
238 if (memcmp(cmd, cmdtp->name, len) != 0)
239 continue;
240
241 /* too many! */
242 if (n_found >= maxv - 2) {
243 cmdv[n_found++] = "...";
244 break;
245 }
246
247 cmdv[n_found++] = cmdtp->name;
248 }
249
250 cmdv[n_found] = NULL;
251 return n_found;
Simon Glass4ef90b72016-03-19 02:18:38 -0600252#else
253 return 0;
254#endif
wdenk3902d702004-04-15 18:22:41 +0000255}
256
Boris Brezillonae73c332018-12-03 22:54:19 +0100257static int complete_cmdv(int argc, char * const argv[], char last_char,
258 int maxv, char *cmdv[])
259{
260#ifdef CONFIG_CMDLINE
261 return complete_subcmdv(ll_entry_start(cmd_tbl_t, cmd),
262 ll_entry_count(cmd_tbl_t, cmd), argc, argv,
263 last_char, maxv, cmdv);
264#else
265 return 0;
266#endif
267}
268
wdenk3902d702004-04-15 18:22:41 +0000269static int make_argv(char *s, int argvsz, char *argv[])
270{
271 int argc = 0;
272
273 /* split into argv */
274 while (argc < argvsz - 1) {
275
276 /* skip any white space */
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000277 while (isblank(*s))
wdenk3902d702004-04-15 18:22:41 +0000278 ++s;
279
Wolfgang Denka1be4762008-05-20 16:00:29 +0200280 if (*s == '\0') /* end of s, no more args */
wdenk3902d702004-04-15 18:22:41 +0000281 break;
282
283 argv[argc++] = s; /* begin of argument string */
284
285 /* find end of string */
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000286 while (*s && !isblank(*s))
wdenk3902d702004-04-15 18:22:41 +0000287 ++s;
288
289 if (*s == '\0') /* end of s, no more args */
290 break;
291
292 *s++ = '\0'; /* terminate current arg */
293 }
294 argv[argc] = NULL;
295
296 return argc;
297}
298
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200299static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
wdenk3902d702004-04-15 18:22:41 +0000300{
301 int ll = leader != NULL ? strlen(leader) : 0;
302 int sl = sep != NULL ? strlen(sep) : 0;
303 int len, i;
304
305 if (banner) {
306 puts("\n");
307 puts(banner);
308 }
309
310 i = linemax; /* force leader and newline */
311 while (*argv != NULL) {
312 len = strlen(*argv) + sl;
313 if (i + len >= linemax) {
314 puts("\n");
315 if (leader)
316 puts(leader);
317 i = ll - sl;
318 } else if (sep)
319 puts(sep);
320 puts(*argv++);
321 i += len;
322 }
323 printf("\n");
324}
325
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200326static int find_common_prefix(char * const argv[])
wdenk3902d702004-04-15 18:22:41 +0000327{
328 int i, len;
329 char *anchor, *s, *t;
330
331 if (*argv == NULL)
332 return 0;
333
334 /* begin with max */
335 anchor = *argv++;
336 len = strlen(anchor);
337 while ((t = *argv++) != NULL) {
338 s = anchor;
339 for (i = 0; i < len; i++, t++, s++) {
340 if (*t != *s)
341 break;
342 }
343 len = s - anchor;
344 }
345 return len;
346}
347
Heinrich Schuchardt831030d2017-11-19 23:07:50 +0100348static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */
wdenk3902d702004-04-15 18:22:41 +0000349
350int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
351{
352 int n = *np, col = *colp;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200353 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenk3902d702004-04-15 18:22:41 +0000354 char *cmdv[20];
355 char *s, *t;
356 const char *sep;
357 int i, j, k, len, seplen, argc;
358 int cnt;
359 char last_char;
Marek Vasutfe6cdf32019-06-27 00:17:27 +0200360#ifdef CONFIG_CMDLINE_PS_SUPPORT
361 const char *ps_prompt = env_get("PS1");
362#else
363 const char *ps_prompt = CONFIG_SYS_PROMPT;
364#endif
wdenk3902d702004-04-15 18:22:41 +0000365
Marek Vasutfe6cdf32019-06-27 00:17:27 +0200366 if (strcmp(prompt, ps_prompt) != 0)
wdenk3902d702004-04-15 18:22:41 +0000367 return 0; /* not in normal console */
368
369 cnt = strlen(buf);
370 if (cnt >= 1)
371 last_char = buf[cnt - 1];
372 else
373 last_char = '\0';
374
375 /* copy to secondary buffer which will be affected */
376 strcpy(tmp_buf, buf);
377
378 /* separate into argv */
379 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
380
Boris Brezillon619fc682018-12-05 09:26:50 +0100381 /* first try a $ completion */
382 i = dollar_complete(argc, argv, last_char,
383 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
384 if (!i) {
385 /* do the completion and return the possible completions */
386 i = complete_cmdv(argc, argv, last_char,
387 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
388 }
wdenk3902d702004-04-15 18:22:41 +0000389
390 /* no match; bell and out */
391 if (i == 0) {
392 if (argc > 1) /* allow tab for non command */
393 return 0;
394 putc('\a');
395 return 1;
396 }
397
398 s = NULL;
399 len = 0;
400 sep = NULL;
401 seplen = 0;
402 if (i == 1) { /* one match; perfect */
Boris Brezillon8506ec62018-12-03 22:54:18 +0100403 if (last_char != '\0' && !isblank(last_char))
404 k = strlen(argv[argc - 1]);
405 else
406 k = 0;
407
wdenk3902d702004-04-15 18:22:41 +0000408 s = cmdv[0] + k;
409 len = strlen(s);
410 sep = " ";
411 seplen = 1;
Masahiro Yamada682195e2014-10-23 17:27:30 +0900412 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
Boris Brezillon8506ec62018-12-03 22:54:18 +0100413 if (last_char != '\0' && !isblank(last_char))
414 k = strlen(argv[argc - 1]);
415 else
416 k = 0;
417
wdenk3902d702004-04-15 18:22:41 +0000418 j -= k;
419 if (j > 0) {
420 s = cmdv[0] + k;
421 len = j;
422 }
423 }
424
425 if (s != NULL) {
426 k = len + seplen;
427 /* make sure it fits */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200428 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
wdenk3902d702004-04-15 18:22:41 +0000429 putc('\a');
430 return 1;
431 }
432
433 t = buf + cnt;
434 for (i = 0; i < len; i++)
435 *t++ = *s++;
436 if (sep != NULL)
437 for (i = 0; i < seplen; i++)
438 *t++ = sep[i];
439 *t = '\0';
440 n += k;
441 col += k;
442 puts(t - k);
443 if (sep == NULL)
444 putc('\a');
445 *np = n;
446 *colp = col;
447 } else {
448 print_argv(NULL, " ", " ", 78, cmdv);
449
450 puts(prompt);
451 puts(buf);
452 }
453 return 1;
454}
455
456#endif
Jean-Christophe PLAGNIOL-VILLARD82290f22008-09-10 22:48:05 +0200457
458#ifdef CMD_DATA_SIZE
459int cmd_get_data_size(char* arg, int default_size)
460{
461 /* Check for a size specification .b, .w or .l.
462 */
463 int len = strlen(arg);
464 if (len > 2 && arg[len-2] == '.') {
Masahiro Yamada682195e2014-10-23 17:27:30 +0900465 switch (arg[len-1]) {
Jean-Christophe PLAGNIOL-VILLARD82290f22008-09-10 22:48:05 +0200466 case 'b':
467 return 1;
468 case 'w':
469 return 2;
470 case 'l':
471 return 4;
York Sun6c480012014-02-26 17:03:19 -0800472#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
473 case 'q':
474 return 8;
475#endif
Jean-Christophe PLAGNIOL-VILLARD82290f22008-09-10 22:48:05 +0200476 case 's':
477 return -2;
478 default:
479 return -1;
480 }
481 }
482 return default_size;
483}
484#endif
Heiko Schocher092cf162010-09-17 13:10:33 +0200485
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200486#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher092cf162010-09-17 13:10:33 +0200487DECLARE_GLOBAL_DATA_PTR;
488
489void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
490{
491 int i;
492
493 if (gd->reloc_off == 0)
494 return;
495
496 for (i = 0; i < size; i++) {
497 ulong addr;
498
Masahiro Yamada682195e2014-10-23 17:27:30 +0900499 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
Kun-Hua Huang89299e22015-08-24 14:52:35 +0800500#ifdef DEBUG_COMMANDS
Heiko Schocher092cf162010-09-17 13:10:33 +0200501 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
Masahiro Yamada682195e2014-10-23 17:27:30 +0900502 cmdtp->name, (ulong)(cmdtp->cmd), addr);
Heiko Schocher092cf162010-09-17 13:10:33 +0200503#endif
504 cmdtp->cmd =
505 (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
506 addr = (ulong)(cmdtp->name) + gd->reloc_off;
507 cmdtp->name = (char *)addr;
508 if (cmdtp->usage) {
509 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
510 cmdtp->usage = (char *)addr;
511 }
512#ifdef CONFIG_SYS_LONGHELP
513 if (cmdtp->help) {
514 addr = (ulong)(cmdtp->help) + gd->reloc_off;
515 cmdtp->help = (char *)addr;
516 }
517#endif
Daniel Schwierzecke5166222011-10-13 06:07:39 +0000518#ifdef CONFIG_AUTO_COMPLETE
519 if (cmdtp->complete) {
520 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
Daniel Schwierzeck76deb6c2011-10-18 11:12:22 +0000521 cmdtp->complete =
522 (int (*)(int, char * const [], char, int, char * []))addr;
Daniel Schwierzecke5166222011-10-13 06:07:39 +0000523 }
524#endif
Heiko Schocher092cf162010-09-17 13:10:33 +0200525 cmdtp++;
526 }
527}
528#endif
Simon Glass2176d602012-02-14 19:59:23 +0000529
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100530int cmd_always_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
531 char * const argv[], int *repeatable)
532{
533 *repeatable = 1;
534
535 return cmdtp->cmd(cmdtp, flag, argc, argv);
536}
537
538int cmd_never_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
539 char * const argv[], int *repeatable)
540{
541 *repeatable = 0;
542
543 return cmdtp->cmd(cmdtp, flag, argc, argv);
544}
545
546int cmd_discard_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
547 char * const argv[])
548{
549 int repeatable;
550
551 return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable);
552}
553
Simon Glass2176d602012-02-14 19:59:23 +0000554/**
555 * Call a command function. This should be the only route in U-Boot to call
556 * a command, so that we can track whether we are waiting for input or
557 * executing a command.
558 *
559 * @param cmdtp Pointer to the command to execute
560 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
561 * @param argc Number of arguments (arg 0 must be the command text)
562 * @param argv Arguments
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100563 * @param repeatable Can the command be repeated
Simon Glass2176d602012-02-14 19:59:23 +0000564 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
565 */
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100566static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
567 int *repeatable)
Simon Glass2176d602012-02-14 19:59:23 +0000568{
569 int result;
570
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100571 result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable);
Simon Glass2176d602012-02-14 19:59:23 +0000572 if (result)
Peng Fand6c09ba2015-07-28 22:45:37 +0800573 debug("Command failed, result=%d\n", result);
Simon Glass2176d602012-02-14 19:59:23 +0000574 return result;
575}
Simon Glass1727b942012-02-14 19:59:25 +0000576
577enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
Richard Genoud94ff0672012-12-03 06:28:28 +0000578 int *repeatable, ulong *ticks)
Simon Glass1727b942012-02-14 19:59:25 +0000579{
580 enum command_ret_t rc = CMD_RET_SUCCESS;
581 cmd_tbl_t *cmdtp;
582
Christoph Muellner388b1452019-04-05 13:03:46 +0200583#if defined(CONFIG_SYS_XTRACE)
584 char *xtrace;
585
586 xtrace = env_get("xtrace");
587 if (xtrace) {
588 puts("+");
589 for (int i = 0; i < argc; i++) {
590 puts(" ");
591 puts(argv[i]);
592 }
593 puts("\n");
594 }
595#endif
596
Simon Glass1727b942012-02-14 19:59:25 +0000597 /* Look up command in command table */
598 cmdtp = find_cmd(argv[0]);
599 if (cmdtp == NULL) {
600 printf("Unknown command '%s' - try 'help'\n", argv[0]);
601 return 1;
602 }
603
604 /* found - check max args */
605 if (argc > cmdtp->maxargs)
606 rc = CMD_RET_USAGE;
607
608#if defined(CONFIG_CMD_BOOTD)
609 /* avoid "bootd" recursion */
610 else if (cmdtp->cmd == do_bootd) {
611 if (flag & CMD_FLAG_BOOTD) {
612 puts("'bootd' recursion detected\n");
613 rc = CMD_RET_FAILURE;
614 } else {
615 flag |= CMD_FLAG_BOOTD;
616 }
617 }
618#endif
619
620 /* If OK so far, then do the command */
621 if (!rc) {
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100622 int newrep;
623
Richard Genoud94ff0672012-12-03 06:28:28 +0000624 if (ticks)
625 *ticks = get_timer(0);
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100626 rc = cmd_call(cmdtp, flag, argc, argv, &newrep);
Richard Genoud94ff0672012-12-03 06:28:28 +0000627 if (ticks)
628 *ticks = get_timer(*ticks);
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100629 *repeatable &= newrep;
Simon Glass1727b942012-02-14 19:59:25 +0000630 }
631 if (rc == CMD_RET_USAGE)
632 rc = cmd_usage(cmdtp);
633 return rc;
634}
Simon Glassd7385532014-02-26 15:59:15 -0700635
636int cmd_process_error(cmd_tbl_t *cmdtp, int err)
637{
Michal Simek2e1b90b2018-06-21 14:49:26 +0200638 if (err == CMD_RET_USAGE)
639 return CMD_RET_USAGE;
640
Simon Glassd7385532014-02-26 15:59:15 -0700641 if (err) {
642 printf("Command '%s' failed: Error %d\n", cmdtp->name, err);
Michal Simek44bdb572018-06-04 13:29:49 +0200643 return CMD_RET_FAILURE;
Simon Glassd7385532014-02-26 15:59:15 -0700644 }
645
Michal Simek44bdb572018-06-04 13:29:49 +0200646 return CMD_RET_SUCCESS;
Simon Glassd7385532014-02-26 15:59:15 -0700647}