blob: ceca992510762d1b2cea8df922f8d5310cfb1ce0 [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>
Jason Hobbs4d91a6e2011-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 Zundel2dce5512009-03-25 17:27:52 +010021
Masahiro Yamada616e2162014-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 Yamada616e2162014-10-23 17:27:30 +090028 if (argc == 1) { /* show list of commands */
Anatolij Gustschin22b6fcb2012-11-08 12:40:17 +010029 cmd_tbl_t *cmd_array[cmd_items];
wdenk9d2b18a2003-06-28 23:11:04 +000030 int i, j, swaps;
wdenk8bde7f72003-06-27 21:31:46 +000031
wdenk9d2b18a2003-06-28 23:11:04 +000032 /* Make array of commands from .uboot_cmd section */
Detlev Zundel2dce5512009-03-25 17:27:52 +010033 cmdtp = cmd_start;
wdenk9d2b18a2003-06-28 23:11:04 +000034 for (i = 0; i < cmd_items; i++) {
35 cmd_array[i] = cmdtp++;
wdenk8bde7f72003-06-27 21:31:46 +000036 }
wdenk8bde7f72003-06-27 21:31:46 +000037
wdenk9d2b18a2003-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 Yamada616e2162014-10-23 17:27:30 +090042 if (strcmp(cmd_array[j]->name,
43 cmd_array[j + 1]->name) > 0) {
wdenk9d2b18a2003-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 }
wdenk8bde7f72003-06-27 21:31:46 +000050 }
wdenk9d2b18a2003-06-28 23:11:04 +000051 if (!swaps)
52 break;
wdenk8bde7f72003-06-27 21:31:46 +000053 }
54
55 /* print short help (usage) */
wdenk9d2b18a2003-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 Yamada616e2162014-10-23 17:27:30 +090060 if (ctrlc())
wdenk5dfa25f2002-10-19 19:42:10 +000061 return 1;
wdenk9d2b18a2003-06-28 23:11:04 +000062 if (usage == NULL)
wdenk5dfa25f2002-10-19 19:42:10 +000063 continue;
Peter Tyser2fb26042009-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 */
wdenk8bde7f72003-06-27 21:31:46 +000072 for (i = 1; i < argc; ++i) {
Masahiro Yamada616e2162014-10-23 17:27:30 +090073 cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items);
74 if (cmdtp != NULL) {
Wolfgang Denk94796d82009-05-24 19:17:29 +020075 rcode |= cmd_usage(cmdtp);
wdenk71f95112003-06-15 22:40:42 +000076 } else {
Masahiro Yamada616e2162014-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 Yamada616e2162014-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 Glassf8bb6962016-03-19 02:18:38 -060088#ifdef CONFIG_CMDLINE
wdenk5dfa25f2002-10-19 19:42:10 +000089 cmd_tbl_t *cmdtp;
Masahiro Yamada616e2162014-10-23 17:27:30 +090090 cmd_tbl_t *cmdtp_temp = table; /* Init value */
wdenk9d2b18a2003-06-28 23:11:04 +000091 const char *p;
92 int len;
93 int n_found = 0;
wdenk8bde7f72003-06-27 21:31:46 +000094
Thomas Weber7013c062010-11-24 13:07:51 +010095 if (!cmd)
96 return NULL;
wdenk9d2b18a2003-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 Yamada616e2162014-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))
wdenk9d2b18a2003-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 }
wdenk9d2b18a2003-06-28 23:11:04 +0000112 if (n_found == 1) { /* exactly one match */
wdenk8bde7f72003-06-27 21:31:46 +0000113 return cmdtp_temp;
wdenk9d2b18a2003-06-28 23:11:04 +0000114 }
Simon Glassf8bb6962016-03-19 02:18:38 -0600115#endif /* CONFIG_CMDLINE */
wdenk8bde7f72003-06-27 21:31:46 +0000116
wdenk9d2b18a2003-06-28 23:11:04 +0000117 return NULL; /* not found or ambiguous command */
wdenk5dfa25f2002-10-19 19:42:10 +0000118}
wdenk04a85b32004-04-15 18:22:41 +0000119
Masahiro Yamada616e2162014-10-23 17:27:30 +0900120cmd_tbl_t *find_cmd(const char *cmd)
Kumar Galab799cb42008-09-23 10:05:02 -0500121{
Marek Vasut6c7c9462012-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 Galab799cb42008-09-23 10:05:02 -0500125}
126
Mike Frysingere84ffdd2011-04-23 23:43:24 +0000127int cmd_usage(const cmd_tbl_t *cmdtp)
Peter Tyser62c3ae72009-01-27 18:03:10 -0600128{
Wolfgang Denk94796d82009-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 Yamada616e2162014-10-23 17:27:30 +0900139 puts(cmdtp->help);
140 putc('\n');
Wolfgang Denk94796d82009-05-24 19:17:29 +0200141#endif /* CONFIG_SYS_LONGHELP */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200142 return 1;
Peter Tyser62c3ae72009-01-27 18:03:10 -0600143}
144
wdenk04a85b32004-04-15 18:22:41 +0000145#ifdef CONFIG_AUTO_COMPLETE
Boris Brezillon03dcf172018-12-05 09:26:50 +0100146static char env_complete_buf[512];
wdenk04a85b32004-04-15 18:22:41 +0000147
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200148int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
wdenk04a85b32004-04-15 18:22:41 +0000149{
wdenk04a85b32004-04-15 18:22:41 +0000150 int space;
151
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000152 space = last_char == '\0' || isblank(last_char);
wdenk04a85b32004-04-15 18:22:41 +0000153
154 if (space && argc == 1)
Boris Brezillon03dcf172018-12-05 09:26:50 +0100155 return env_complete("", maxv, cmdv, sizeof(env_complete_buf),
156 env_complete_buf, false);
wdenk04a85b32004-04-15 18:22:41 +0000157
158 if (!space && argc == 2)
Boris Brezillon03dcf172018-12-05 09:26:50 +0100159 return env_complete(argv[1], maxv, cmdv,
160 sizeof(env_complete_buf),
161 env_complete_buf, false);
Mike Frysinger560d4242010-12-17 16:51:59 -0500162
wdenk04a85b32004-04-15 18:22:41 +0000163 return 0;
164}
165
Boris Brezillon03dcf172018-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
wdenk04a85b32004-04-15 18:22:41 +0000178/*************************************************************************************/
179
Boris Brezillon6fb61442018-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[])
wdenk04a85b32004-04-15 18:22:41 +0000183{
Simon Glassf8bb6962016-03-19 02:18:38 -0600184#ifdef CONFIG_CMDLINE
Marek Vasut6c7c9462012-10-12 10:27:04 +0000185 const cmd_tbl_t *cmdend = cmdtp + count;
wdenk04a85b32004-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 Vasut6c7c9462012-10-12 10:27:04 +0000199 for (; cmdtp != cmdend; cmdtp++) {
wdenk04a85b32004-04-15 18:22:41 +0000200 if (n_found >= maxv - 2) {
Andrew Gabbasov9b438942013-12-27 10:05:14 -0600201 cmdv[n_found++] = "...";
wdenk04a85b32004-04-15 18:22:41 +0000202 break;
203 }
Andrew Gabbasov9b438942013-12-27 10:05:14 -0600204 cmdv[n_found++] = cmdtp->name;
wdenk04a85b32004-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 Yamada616e2162014-10-23 17:27:30 +0900211 if (argc > 1 || last_char == '\0' || isblank(last_char)) {
Boris Brezillon6fb61442018-12-03 22:54:19 +0100212 cmdtp = find_cmd_tbl(argv[0], cmdtp, count);
wdenk04a85b32004-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 Vasut6c7c9462012-10-12 10:27:04 +0000232 for (; cmdtp != cmdend; cmdtp++) {
wdenk04a85b32004-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 Glassf8bb6962016-03-19 02:18:38 -0600252#else
253 return 0;
254#endif
wdenk04a85b32004-04-15 18:22:41 +0000255}
256
Boris Brezillon6fb61442018-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
wdenk04a85b32004-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 Hobbs4d91a6e2011-08-23 11:06:54 +0000277 while (isblank(*s))
wdenk04a85b32004-04-15 18:22:41 +0000278 ++s;
279
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200280 if (*s == '\0') /* end of s, no more args */
wdenk04a85b32004-04-15 18:22:41 +0000281 break;
282
283 argv[argc++] = s; /* begin of argument string */
284
285 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000286 while (*s && !isblank(*s))
wdenk04a85b32004-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 Denk54841ab2010-06-28 22:00:46 +0200299static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
wdenk04a85b32004-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 Denk54841ab2010-06-28 22:00:46 +0200326static int find_common_prefix(char * const argv[])
wdenk04a85b32004-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 Schuchardt2614a202017-11-19 23:07:50 +0100348static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */
wdenk04a85b32004-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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200353 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenk04a85b32004-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 Vasut4225f832019-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
wdenk04a85b32004-04-15 18:22:41 +0000365
Marek Vasut4225f832019-06-27 00:17:27 +0200366 if (strcmp(prompt, ps_prompt) != 0)
wdenk04a85b32004-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 Brezillon03dcf172018-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 }
wdenk04a85b32004-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 Brezilloncbe07eb2018-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
wdenk04a85b32004-04-15 18:22:41 +0000408 s = cmdv[0] + k;
409 len = strlen(s);
410 sep = " ";
411 seplen = 1;
Masahiro Yamada616e2162014-10-23 17:27:30 +0900412 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
Boris Brezilloncbe07eb2018-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
wdenk04a85b32004-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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200428 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
wdenk04a85b32004-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-VILLARD8a40fb12008-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 Yamada616e2162014-10-23 17:27:30 +0900465 switch (arg[len-1]) {
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200466 case 'b':
467 return 1;
468 case 'w':
469 return 2;
470 case 'l':
471 return 4;
York Sun4d1fd7f2014-02-26 17:03:19 -0800472#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
473 case 'q':
474 return 8;
475#endif
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-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 Schocher620f1f62010-09-17 13:10:33 +0200485
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200486#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher620f1f62010-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
Michal Simek6f94daf2019-10-21 12:54:20 +0200499 addr = (ulong)(cmdtp->cmd_rep) + gd->reloc_off;
500 cmdtp->cmd_rep =
501 (int (*)(struct cmd_tbl_s *, int, int,
502 char * const [], int *))addr;
503
Masahiro Yamada616e2162014-10-23 17:27:30 +0900504 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
Kun-Hua Huang2e88bb22015-08-24 14:52:35 +0800505#ifdef DEBUG_COMMANDS
Heiko Schocher620f1f62010-09-17 13:10:33 +0200506 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
Masahiro Yamada616e2162014-10-23 17:27:30 +0900507 cmdtp->name, (ulong)(cmdtp->cmd), addr);
Heiko Schocher620f1f62010-09-17 13:10:33 +0200508#endif
509 cmdtp->cmd =
510 (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
511 addr = (ulong)(cmdtp->name) + gd->reloc_off;
512 cmdtp->name = (char *)addr;
513 if (cmdtp->usage) {
514 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
515 cmdtp->usage = (char *)addr;
516 }
517#ifdef CONFIG_SYS_LONGHELP
518 if (cmdtp->help) {
519 addr = (ulong)(cmdtp->help) + gd->reloc_off;
520 cmdtp->help = (char *)addr;
521 }
522#endif
Daniel Schwierzeckfa28bd22011-10-13 06:07:39 +0000523#ifdef CONFIG_AUTO_COMPLETE
524 if (cmdtp->complete) {
525 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
Daniel Schwierzeck3668d8f2011-10-18 11:12:22 +0000526 cmdtp->complete =
527 (int (*)(int, char * const [], char, int, char * []))addr;
Daniel Schwierzeckfa28bd22011-10-13 06:07:39 +0000528 }
529#endif
Heiko Schocher620f1f62010-09-17 13:10:33 +0200530 cmdtp++;
531 }
532}
533#endif
Simon Glassbdf8e342012-02-14 19:59:23 +0000534
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100535int cmd_always_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
536 char * const argv[], int *repeatable)
537{
538 *repeatable = 1;
539
540 return cmdtp->cmd(cmdtp, flag, argc, argv);
541}
542
543int cmd_never_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
544 char * const argv[], int *repeatable)
545{
546 *repeatable = 0;
547
548 return cmdtp->cmd(cmdtp, flag, argc, argv);
549}
550
551int cmd_discard_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
552 char * const argv[])
553{
554 int repeatable;
555
556 return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable);
557}
558
Simon Glassbdf8e342012-02-14 19:59:23 +0000559/**
560 * Call a command function. This should be the only route in U-Boot to call
561 * a command, so that we can track whether we are waiting for input or
562 * executing a command.
563 *
564 * @param cmdtp Pointer to the command to execute
565 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
566 * @param argc Number of arguments (arg 0 must be the command text)
567 * @param argv Arguments
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100568 * @param repeatable Can the command be repeated
Simon Glassbdf8e342012-02-14 19:59:23 +0000569 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
570 */
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100571static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
572 int *repeatable)
Simon Glassbdf8e342012-02-14 19:59:23 +0000573{
574 int result;
575
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100576 result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable);
Simon Glassbdf8e342012-02-14 19:59:23 +0000577 if (result)
Peng Fan58b6ad62015-07-28 22:45:37 +0800578 debug("Command failed, result=%d\n", result);
Simon Glassbdf8e342012-02-14 19:59:23 +0000579 return result;
580}
Simon Glass9d12d5d2012-02-14 19:59:25 +0000581
582enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
Richard Genoud34765e82012-12-03 06:28:28 +0000583 int *repeatable, ulong *ticks)
Simon Glass9d12d5d2012-02-14 19:59:25 +0000584{
585 enum command_ret_t rc = CMD_RET_SUCCESS;
586 cmd_tbl_t *cmdtp;
587
Christoph Muellner7ae31fc2019-04-05 13:03:46 +0200588#if defined(CONFIG_SYS_XTRACE)
589 char *xtrace;
590
591 xtrace = env_get("xtrace");
592 if (xtrace) {
593 puts("+");
594 for (int i = 0; i < argc; i++) {
595 puts(" ");
596 puts(argv[i]);
597 }
598 puts("\n");
599 }
600#endif
601
Simon Glass9d12d5d2012-02-14 19:59:25 +0000602 /* Look up command in command table */
603 cmdtp = find_cmd(argv[0]);
604 if (cmdtp == NULL) {
605 printf("Unknown command '%s' - try 'help'\n", argv[0]);
606 return 1;
607 }
608
609 /* found - check max args */
610 if (argc > cmdtp->maxargs)
611 rc = CMD_RET_USAGE;
612
613#if defined(CONFIG_CMD_BOOTD)
614 /* avoid "bootd" recursion */
615 else if (cmdtp->cmd == do_bootd) {
616 if (flag & CMD_FLAG_BOOTD) {
617 puts("'bootd' recursion detected\n");
618 rc = CMD_RET_FAILURE;
619 } else {
620 flag |= CMD_FLAG_BOOTD;
621 }
622 }
623#endif
624
625 /* If OK so far, then do the command */
626 if (!rc) {
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100627 int newrep;
628
Richard Genoud34765e82012-12-03 06:28:28 +0000629 if (ticks)
630 *ticks = get_timer(0);
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100631 rc = cmd_call(cmdtp, flag, argc, argv, &newrep);
Richard Genoud34765e82012-12-03 06:28:28 +0000632 if (ticks)
633 *ticks = get_timer(*ticks);
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100634 *repeatable &= newrep;
Simon Glass9d12d5d2012-02-14 19:59:25 +0000635 }
636 if (rc == CMD_RET_USAGE)
637 rc = cmd_usage(cmdtp);
638 return rc;
639}
Simon Glass16ff9902014-02-26 15:59:15 -0700640
641int cmd_process_error(cmd_tbl_t *cmdtp, int err)
642{
Michal Simek27eb7bc2018-06-21 14:49:26 +0200643 if (err == CMD_RET_USAGE)
644 return CMD_RET_USAGE;
645
Simon Glass16ff9902014-02-26 15:59:15 -0700646 if (err) {
647 printf("Command '%s' failed: Error %d\n", cmdtp->name, err);
Michal Simek37233242018-06-04 13:29:49 +0200648 return CMD_RET_FAILURE;
Simon Glass16ff9902014-02-26 15:59:15 -0700649 }
650
Michal Simek37233242018-06-04 13:29:49 +0200651 return CMD_RET_SUCCESS;
Simon Glass16ff9902014-02-26 15:59:15 -0700652}