blob: 19f0534a76ea53cf146c042b79076d83a181b3b9 [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>
Jason Hobbs4d91a6e2011-08-23 11:06:54 +000014#include <linux/ctype.h>
wdenk5dfa25f2002-10-19 19:42:10 +000015
wdenk5dfa25f2002-10-19 19:42:10 +000016/*
17 * Use puts() instead of printf() to avoid printf buffer overflow
18 * for long help messages
19 */
Detlev Zundel2dce5512009-03-25 17:27:52 +010020
Masahiro Yamada616e2162014-10-23 17:27:30 +090021int _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t *cmdtp, int flag,
22 int argc, char * const argv[])
wdenk5dfa25f2002-10-19 19:42:10 +000023{
24 int i;
25 int rcode = 0;
26
Masahiro Yamada616e2162014-10-23 17:27:30 +090027 if (argc == 1) { /* show list of commands */
Anatolij Gustschin22b6fcb2012-11-08 12:40:17 +010028 cmd_tbl_t *cmd_array[cmd_items];
wdenk9d2b18a2003-06-28 23:11:04 +000029 int i, j, swaps;
wdenk8bde7f72003-06-27 21:31:46 +000030
wdenk9d2b18a2003-06-28 23:11:04 +000031 /* Make array of commands from .uboot_cmd section */
Detlev Zundel2dce5512009-03-25 17:27:52 +010032 cmdtp = cmd_start;
wdenk9d2b18a2003-06-28 23:11:04 +000033 for (i = 0; i < cmd_items; i++) {
34 cmd_array[i] = cmdtp++;
wdenk8bde7f72003-06-27 21:31:46 +000035 }
wdenk8bde7f72003-06-27 21:31:46 +000036
wdenk9d2b18a2003-06-28 23:11:04 +000037 /* Sort command list (trivial bubble sort) */
38 for (i = cmd_items - 1; i > 0; --i) {
39 swaps = 0;
40 for (j = 0; j < i; ++j) {
Masahiro Yamada616e2162014-10-23 17:27:30 +090041 if (strcmp(cmd_array[j]->name,
42 cmd_array[j + 1]->name) > 0) {
wdenk9d2b18a2003-06-28 23:11:04 +000043 cmd_tbl_t *tmp;
44 tmp = cmd_array[j];
45 cmd_array[j] = cmd_array[j + 1];
46 cmd_array[j + 1] = tmp;
47 ++swaps;
48 }
wdenk8bde7f72003-06-27 21:31:46 +000049 }
wdenk9d2b18a2003-06-28 23:11:04 +000050 if (!swaps)
51 break;
wdenk8bde7f72003-06-27 21:31:46 +000052 }
53
54 /* print short help (usage) */
wdenk9d2b18a2003-06-28 23:11:04 +000055 for (i = 0; i < cmd_items; i++) {
56 const char *usage = cmd_array[i]->usage;
57
wdenk5dfa25f2002-10-19 19:42:10 +000058 /* allow user abort */
Masahiro Yamada616e2162014-10-23 17:27:30 +090059 if (ctrlc())
wdenk5dfa25f2002-10-19 19:42:10 +000060 return 1;
wdenk9d2b18a2003-06-28 23:11:04 +000061 if (usage == NULL)
wdenk5dfa25f2002-10-19 19:42:10 +000062 continue;
Peter Tyser2fb26042009-01-27 18:03:12 -060063 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
64 cmd_array[i]->name, usage);
wdenk5dfa25f2002-10-19 19:42:10 +000065 }
wdenk5dfa25f2002-10-19 19:42:10 +000066 return 0;
67 }
wdenk5dfa25f2002-10-19 19:42:10 +000068 /*
69 * command help (long version)
70 */
wdenk8bde7f72003-06-27 21:31:46 +000071 for (i = 1; i < argc; ++i) {
Masahiro Yamada616e2162014-10-23 17:27:30 +090072 cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items);
73 if (cmdtp != NULL) {
Wolfgang Denk94796d82009-05-24 19:17:29 +020074 rcode |= cmd_usage(cmdtp);
wdenk71f95112003-06-15 22:40:42 +000075 } else {
Masahiro Yamada616e2162014-10-23 17:27:30 +090076 printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n",
77 argv[i]);
wdenk5dfa25f2002-10-19 19:42:10 +000078 rcode = 1;
79 }
80 }
81 return rcode;
82}
83
Masahiro Yamada616e2162014-10-23 17:27:30 +090084/* find command table entry for a command */
85cmd_tbl_t *find_cmd_tbl(const char *cmd, cmd_tbl_t *table, int table_len)
wdenk5dfa25f2002-10-19 19:42:10 +000086{
Simon Glassf8bb6962016-03-19 02:18:38 -060087#ifdef CONFIG_CMDLINE
wdenk5dfa25f2002-10-19 19:42:10 +000088 cmd_tbl_t *cmdtp;
Masahiro Yamada616e2162014-10-23 17:27:30 +090089 cmd_tbl_t *cmdtp_temp = table; /* Init value */
wdenk9d2b18a2003-06-28 23:11:04 +000090 const char *p;
91 int len;
92 int n_found = 0;
wdenk8bde7f72003-06-27 21:31:46 +000093
Thomas Weber7013c062010-11-24 13:07:51 +010094 if (!cmd)
95 return NULL;
wdenk9d2b18a2003-06-28 23:11:04 +000096 /*
97 * Some commands allow length modifiers (like "cp.b");
98 * compare command name only until first dot.
99 */
100 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
101
Masahiro Yamada616e2162014-10-23 17:27:30 +0900102 for (cmdtp = table; cmdtp != table + table_len; cmdtp++) {
103 if (strncmp(cmd, cmdtp->name, len) == 0) {
104 if (len == strlen(cmdtp->name))
wdenk9d2b18a2003-06-28 23:11:04 +0000105 return cmdtp; /* full match */
106
107 cmdtp_temp = cmdtp; /* abbreviated command ? */
108 n_found++;
109 }
wdenk5dfa25f2002-10-19 19:42:10 +0000110 }
wdenk9d2b18a2003-06-28 23:11:04 +0000111 if (n_found == 1) { /* exactly one match */
wdenk8bde7f72003-06-27 21:31:46 +0000112 return cmdtp_temp;
wdenk9d2b18a2003-06-28 23:11:04 +0000113 }
Simon Glassf8bb6962016-03-19 02:18:38 -0600114#endif /* CONFIG_CMDLINE */
wdenk8bde7f72003-06-27 21:31:46 +0000115
wdenk9d2b18a2003-06-28 23:11:04 +0000116 return NULL; /* not found or ambiguous command */
wdenk5dfa25f2002-10-19 19:42:10 +0000117}
wdenk04a85b32004-04-15 18:22:41 +0000118
Masahiro Yamada616e2162014-10-23 17:27:30 +0900119cmd_tbl_t *find_cmd(const char *cmd)
Kumar Galab799cb42008-09-23 10:05:02 -0500120{
Marek Vasut6c7c9462012-10-12 10:27:04 +0000121 cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd);
122 const int len = ll_entry_count(cmd_tbl_t, cmd);
123 return find_cmd_tbl(cmd, start, len);
Kumar Galab799cb42008-09-23 10:05:02 -0500124}
125
Mike Frysingere84ffdd2011-04-23 23:43:24 +0000126int cmd_usage(const cmd_tbl_t *cmdtp)
Peter Tyser62c3ae72009-01-27 18:03:10 -0600127{
Wolfgang Denk94796d82009-05-24 19:17:29 +0200128 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
129
130#ifdef CONFIG_SYS_LONGHELP
131 printf("Usage:\n%s ", cmdtp->name);
132
133 if (!cmdtp->help) {
134 puts ("- No additional help available.\n");
135 return 1;
136 }
137
Masahiro Yamada616e2162014-10-23 17:27:30 +0900138 puts(cmdtp->help);
139 putc('\n');
Wolfgang Denk94796d82009-05-24 19:17:29 +0200140#endif /* CONFIG_SYS_LONGHELP */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200141 return 1;
Peter Tyser62c3ae72009-01-27 18:03:10 -0600142}
143
wdenk04a85b32004-04-15 18:22:41 +0000144#ifdef CONFIG_AUTO_COMPLETE
145
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200146int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
wdenk04a85b32004-04-15 18:22:41 +0000147{
148 static char tmp_buf[512];
149 int space;
150
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000151 space = last_char == '\0' || isblank(last_char);
wdenk04a85b32004-04-15 18:22:41 +0000152
153 if (space && argc == 1)
154 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
155
156 if (!space && argc == 2)
157 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
Mike Frysinger560d4242010-12-17 16:51:59 -0500158
wdenk04a85b32004-04-15 18:22:41 +0000159 return 0;
160}
161
wdenk04a85b32004-04-15 18:22:41 +0000162/*************************************************************************************/
163
Boris Brezillon6fb61442018-12-03 22:54:19 +0100164int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc,
165 char * const argv[], char last_char,
166 int maxv, char *cmdv[])
wdenk04a85b32004-04-15 18:22:41 +0000167{
Simon Glassf8bb6962016-03-19 02:18:38 -0600168#ifdef CONFIG_CMDLINE
Marek Vasut6c7c9462012-10-12 10:27:04 +0000169 const cmd_tbl_t *cmdend = cmdtp + count;
wdenk04a85b32004-04-15 18:22:41 +0000170 const char *p;
171 int len, clen;
172 int n_found = 0;
173 const char *cmd;
174
175 /* sanity? */
176 if (maxv < 2)
177 return -2;
178
179 cmdv[0] = NULL;
180
181 if (argc == 0) {
182 /* output full list of commands */
Marek Vasut6c7c9462012-10-12 10:27:04 +0000183 for (; cmdtp != cmdend; cmdtp++) {
wdenk04a85b32004-04-15 18:22:41 +0000184 if (n_found >= maxv - 2) {
Andrew Gabbasov9b438942013-12-27 10:05:14 -0600185 cmdv[n_found++] = "...";
wdenk04a85b32004-04-15 18:22:41 +0000186 break;
187 }
Andrew Gabbasov9b438942013-12-27 10:05:14 -0600188 cmdv[n_found++] = cmdtp->name;
wdenk04a85b32004-04-15 18:22:41 +0000189 }
190 cmdv[n_found] = NULL;
191 return n_found;
192 }
193
194 /* more than one arg or one but the start of the next */
Masahiro Yamada616e2162014-10-23 17:27:30 +0900195 if (argc > 1 || last_char == '\0' || isblank(last_char)) {
Boris Brezillon6fb61442018-12-03 22:54:19 +0100196 cmdtp = find_cmd_tbl(argv[0], cmdtp, count);
wdenk04a85b32004-04-15 18:22:41 +0000197 if (cmdtp == NULL || cmdtp->complete == NULL) {
198 cmdv[0] = NULL;
199 return 0;
200 }
201 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
202 }
203
204 cmd = argv[0];
205 /*
206 * Some commands allow length modifiers (like "cp.b");
207 * compare command name only until first dot.
208 */
209 p = strchr(cmd, '.');
210 if (p == NULL)
211 len = strlen(cmd);
212 else
213 len = p - cmd;
214
215 /* return the partial matches */
Marek Vasut6c7c9462012-10-12 10:27:04 +0000216 for (; cmdtp != cmdend; cmdtp++) {
wdenk04a85b32004-04-15 18:22:41 +0000217
218 clen = strlen(cmdtp->name);
219 if (clen < len)
220 continue;
221
222 if (memcmp(cmd, cmdtp->name, len) != 0)
223 continue;
224
225 /* too many! */
226 if (n_found >= maxv - 2) {
227 cmdv[n_found++] = "...";
228 break;
229 }
230
231 cmdv[n_found++] = cmdtp->name;
232 }
233
234 cmdv[n_found] = NULL;
235 return n_found;
Simon Glassf8bb6962016-03-19 02:18:38 -0600236#else
237 return 0;
238#endif
wdenk04a85b32004-04-15 18:22:41 +0000239}
240
Boris Brezillon6fb61442018-12-03 22:54:19 +0100241static int complete_cmdv(int argc, char * const argv[], char last_char,
242 int maxv, char *cmdv[])
243{
244#ifdef CONFIG_CMDLINE
245 return complete_subcmdv(ll_entry_start(cmd_tbl_t, cmd),
246 ll_entry_count(cmd_tbl_t, cmd), argc, argv,
247 last_char, maxv, cmdv);
248#else
249 return 0;
250#endif
251}
252
wdenk04a85b32004-04-15 18:22:41 +0000253static int make_argv(char *s, int argvsz, char *argv[])
254{
255 int argc = 0;
256
257 /* split into argv */
258 while (argc < argvsz - 1) {
259
260 /* skip any white space */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000261 while (isblank(*s))
wdenk04a85b32004-04-15 18:22:41 +0000262 ++s;
263
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200264 if (*s == '\0') /* end of s, no more args */
wdenk04a85b32004-04-15 18:22:41 +0000265 break;
266
267 argv[argc++] = s; /* begin of argument string */
268
269 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000270 while (*s && !isblank(*s))
wdenk04a85b32004-04-15 18:22:41 +0000271 ++s;
272
273 if (*s == '\0') /* end of s, no more args */
274 break;
275
276 *s++ = '\0'; /* terminate current arg */
277 }
278 argv[argc] = NULL;
279
280 return argc;
281}
282
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200283static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
wdenk04a85b32004-04-15 18:22:41 +0000284{
285 int ll = leader != NULL ? strlen(leader) : 0;
286 int sl = sep != NULL ? strlen(sep) : 0;
287 int len, i;
288
289 if (banner) {
290 puts("\n");
291 puts(banner);
292 }
293
294 i = linemax; /* force leader and newline */
295 while (*argv != NULL) {
296 len = strlen(*argv) + sl;
297 if (i + len >= linemax) {
298 puts("\n");
299 if (leader)
300 puts(leader);
301 i = ll - sl;
302 } else if (sep)
303 puts(sep);
304 puts(*argv++);
305 i += len;
306 }
307 printf("\n");
308}
309
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200310static int find_common_prefix(char * const argv[])
wdenk04a85b32004-04-15 18:22:41 +0000311{
312 int i, len;
313 char *anchor, *s, *t;
314
315 if (*argv == NULL)
316 return 0;
317
318 /* begin with max */
319 anchor = *argv++;
320 len = strlen(anchor);
321 while ((t = *argv++) != NULL) {
322 s = anchor;
323 for (i = 0; i < len; i++, t++, s++) {
324 if (*t != *s)
325 break;
326 }
327 len = s - anchor;
328 }
329 return len;
330}
331
Heinrich Schuchardt2614a202017-11-19 23:07:50 +0100332static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */
wdenk04a85b32004-04-15 18:22:41 +0000333
334int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
335{
336 int n = *np, col = *colp;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200337 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenk04a85b32004-04-15 18:22:41 +0000338 char *cmdv[20];
339 char *s, *t;
340 const char *sep;
341 int i, j, k, len, seplen, argc;
342 int cnt;
343 char last_char;
344
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200345 if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
wdenk04a85b32004-04-15 18:22:41 +0000346 return 0; /* not in normal console */
347
348 cnt = strlen(buf);
349 if (cnt >= 1)
350 last_char = buf[cnt - 1];
351 else
352 last_char = '\0';
353
354 /* copy to secondary buffer which will be affected */
355 strcpy(tmp_buf, buf);
356
357 /* separate into argv */
358 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
359
360 /* do the completion and return the possible completions */
Masahiro Yamada616e2162014-10-23 17:27:30 +0900361 i = complete_cmdv(argc, argv, last_char,
362 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
wdenk04a85b32004-04-15 18:22:41 +0000363
364 /* no match; bell and out */
365 if (i == 0) {
366 if (argc > 1) /* allow tab for non command */
367 return 0;
368 putc('\a');
369 return 1;
370 }
371
372 s = NULL;
373 len = 0;
374 sep = NULL;
375 seplen = 0;
376 if (i == 1) { /* one match; perfect */
Boris Brezilloncbe07eb2018-12-03 22:54:18 +0100377 if (last_char != '\0' && !isblank(last_char))
378 k = strlen(argv[argc - 1]);
379 else
380 k = 0;
381
wdenk04a85b32004-04-15 18:22:41 +0000382 s = cmdv[0] + k;
383 len = strlen(s);
384 sep = " ";
385 seplen = 1;
Masahiro Yamada616e2162014-10-23 17:27:30 +0900386 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
Boris Brezilloncbe07eb2018-12-03 22:54:18 +0100387 if (last_char != '\0' && !isblank(last_char))
388 k = strlen(argv[argc - 1]);
389 else
390 k = 0;
391
wdenk04a85b32004-04-15 18:22:41 +0000392 j -= k;
393 if (j > 0) {
394 s = cmdv[0] + k;
395 len = j;
396 }
397 }
398
399 if (s != NULL) {
400 k = len + seplen;
401 /* make sure it fits */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200402 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
wdenk04a85b32004-04-15 18:22:41 +0000403 putc('\a');
404 return 1;
405 }
406
407 t = buf + cnt;
408 for (i = 0; i < len; i++)
409 *t++ = *s++;
410 if (sep != NULL)
411 for (i = 0; i < seplen; i++)
412 *t++ = sep[i];
413 *t = '\0';
414 n += k;
415 col += k;
416 puts(t - k);
417 if (sep == NULL)
418 putc('\a');
419 *np = n;
420 *colp = col;
421 } else {
422 print_argv(NULL, " ", " ", 78, cmdv);
423
424 puts(prompt);
425 puts(buf);
426 }
427 return 1;
428}
429
430#endif
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200431
432#ifdef CMD_DATA_SIZE
433int cmd_get_data_size(char* arg, int default_size)
434{
435 /* Check for a size specification .b, .w or .l.
436 */
437 int len = strlen(arg);
438 if (len > 2 && arg[len-2] == '.') {
Masahiro Yamada616e2162014-10-23 17:27:30 +0900439 switch (arg[len-1]) {
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200440 case 'b':
441 return 1;
442 case 'w':
443 return 2;
444 case 'l':
445 return 4;
York Sun4d1fd7f2014-02-26 17:03:19 -0800446#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
447 case 'q':
448 return 8;
449#endif
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200450 case 's':
451 return -2;
452 default:
453 return -1;
454 }
455 }
456 return default_size;
457}
458#endif
Heiko Schocher620f1f62010-09-17 13:10:33 +0200459
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200460#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher620f1f62010-09-17 13:10:33 +0200461DECLARE_GLOBAL_DATA_PTR;
462
463void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
464{
465 int i;
466
467 if (gd->reloc_off == 0)
468 return;
469
470 for (i = 0; i < size; i++) {
471 ulong addr;
472
Masahiro Yamada616e2162014-10-23 17:27:30 +0900473 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
Kun-Hua Huang2e88bb22015-08-24 14:52:35 +0800474#ifdef DEBUG_COMMANDS
Heiko Schocher620f1f62010-09-17 13:10:33 +0200475 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
Masahiro Yamada616e2162014-10-23 17:27:30 +0900476 cmdtp->name, (ulong)(cmdtp->cmd), addr);
Heiko Schocher620f1f62010-09-17 13:10:33 +0200477#endif
478 cmdtp->cmd =
479 (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
480 addr = (ulong)(cmdtp->name) + gd->reloc_off;
481 cmdtp->name = (char *)addr;
482 if (cmdtp->usage) {
483 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
484 cmdtp->usage = (char *)addr;
485 }
486#ifdef CONFIG_SYS_LONGHELP
487 if (cmdtp->help) {
488 addr = (ulong)(cmdtp->help) + gd->reloc_off;
489 cmdtp->help = (char *)addr;
490 }
491#endif
Daniel Schwierzeckfa28bd22011-10-13 06:07:39 +0000492#ifdef CONFIG_AUTO_COMPLETE
493 if (cmdtp->complete) {
494 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
Daniel Schwierzeck3668d8f2011-10-18 11:12:22 +0000495 cmdtp->complete =
496 (int (*)(int, char * const [], char, int, char * []))addr;
Daniel Schwierzeckfa28bd22011-10-13 06:07:39 +0000497 }
498#endif
Heiko Schocher620f1f62010-09-17 13:10:33 +0200499 cmdtp++;
500 }
501}
502#endif
Simon Glassbdf8e342012-02-14 19:59:23 +0000503
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100504int cmd_always_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
505 char * const argv[], int *repeatable)
506{
507 *repeatable = 1;
508
509 return cmdtp->cmd(cmdtp, flag, argc, argv);
510}
511
512int cmd_never_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
513 char * const argv[], int *repeatable)
514{
515 *repeatable = 0;
516
517 return cmdtp->cmd(cmdtp, flag, argc, argv);
518}
519
520int cmd_discard_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
521 char * const argv[])
522{
523 int repeatable;
524
525 return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable);
526}
527
Simon Glassbdf8e342012-02-14 19:59:23 +0000528/**
529 * Call a command function. This should be the only route in U-Boot to call
530 * a command, so that we can track whether we are waiting for input or
531 * executing a command.
532 *
533 * @param cmdtp Pointer to the command to execute
534 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
535 * @param argc Number of arguments (arg 0 must be the command text)
536 * @param argv Arguments
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100537 * @param repeatable Can the command be repeated
Simon Glassbdf8e342012-02-14 19:59:23 +0000538 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
539 */
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100540static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
541 int *repeatable)
Simon Glassbdf8e342012-02-14 19:59:23 +0000542{
543 int result;
544
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100545 result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable);
Simon Glassbdf8e342012-02-14 19:59:23 +0000546 if (result)
Peng Fan58b6ad62015-07-28 22:45:37 +0800547 debug("Command failed, result=%d\n", result);
Simon Glassbdf8e342012-02-14 19:59:23 +0000548 return result;
549}
Simon Glass9d12d5d2012-02-14 19:59:25 +0000550
551enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
Richard Genoud34765e82012-12-03 06:28:28 +0000552 int *repeatable, ulong *ticks)
Simon Glass9d12d5d2012-02-14 19:59:25 +0000553{
554 enum command_ret_t rc = CMD_RET_SUCCESS;
555 cmd_tbl_t *cmdtp;
556
557 /* Look up command in command table */
558 cmdtp = find_cmd(argv[0]);
559 if (cmdtp == NULL) {
560 printf("Unknown command '%s' - try 'help'\n", argv[0]);
561 return 1;
562 }
563
564 /* found - check max args */
565 if (argc > cmdtp->maxargs)
566 rc = CMD_RET_USAGE;
567
568#if defined(CONFIG_CMD_BOOTD)
569 /* avoid "bootd" recursion */
570 else if (cmdtp->cmd == do_bootd) {
571 if (flag & CMD_FLAG_BOOTD) {
572 puts("'bootd' recursion detected\n");
573 rc = CMD_RET_FAILURE;
574 } else {
575 flag |= CMD_FLAG_BOOTD;
576 }
577 }
578#endif
579
580 /* If OK so far, then do the command */
581 if (!rc) {
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100582 int newrep;
583
Richard Genoud34765e82012-12-03 06:28:28 +0000584 if (ticks)
585 *ticks = get_timer(0);
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100586 rc = cmd_call(cmdtp, flag, argc, argv, &newrep);
Richard Genoud34765e82012-12-03 06:28:28 +0000587 if (ticks)
588 *ticks = get_timer(*ticks);
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100589 *repeatable &= newrep;
Simon Glass9d12d5d2012-02-14 19:59:25 +0000590 }
591 if (rc == CMD_RET_USAGE)
592 rc = cmd_usage(cmdtp);
593 return rc;
594}
Simon Glass16ff9902014-02-26 15:59:15 -0700595
596int cmd_process_error(cmd_tbl_t *cmdtp, int err)
597{
Michal Simek27eb7bc2018-06-21 14:49:26 +0200598 if (err == CMD_RET_USAGE)
599 return CMD_RET_USAGE;
600
Simon Glass16ff9902014-02-26 15:59:15 -0700601 if (err) {
602 printf("Command '%s' failed: Error %d\n", cmdtp->name, err);
Michal Simek37233242018-06-04 13:29:49 +0200603 return CMD_RET_FAILURE;
Simon Glass16ff9902014-02-26 15:59:15 -0700604 }
605
Michal Simek37233242018-06-04 13:29:49 +0200606 return CMD_RET_SUCCESS;
Simon Glass16ff9902014-02-26 15:59:15 -0700607}