blob: d75908aae2ca5830d383854d8389e8b68f5a565e [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
Simon Glassed38aef2020-05-10 11:40:03 -060022int _do_help(struct cmd_tbl *cmd_start, int cmd_items, struct cmd_tbl *cmdtp,
23 int flag, 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 */
Simon Glassed38aef2020-05-10 11:40:03 -060029 struct cmd_tbl *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) {
Simon Glassed38aef2020-05-10 11:40:03 -060044 struct cmd_tbl *tmp;
wdenk3086a972003-06-28 23:11:04 +000045 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 */
Simon Glassed38aef2020-05-10 11:40:03 -060086struct cmd_tbl *find_cmd_tbl(const char *cmd, struct cmd_tbl *table,
87 int table_len)
wdenk5dfa25f2002-10-19 19:42:10 +000088{
Simon Glass4ef90b72016-03-19 02:18:38 -060089#ifdef CONFIG_CMDLINE
Simon Glassed38aef2020-05-10 11:40:03 -060090 struct cmd_tbl *cmdtp;
91 struct cmd_tbl *cmdtp_temp = table; /* Init value */
wdenk3086a972003-06-28 23:11:04 +000092 const char *p;
93 int len;
94 int n_found = 0;
wdenk57b2d802003-06-27 21:31:46 +000095
Thomas Weber97ea46f2010-11-24 13:07:51 +010096 if (!cmd)
97 return NULL;
wdenk3086a972003-06-28 23:11:04 +000098 /*
99 * Some commands allow length modifiers (like "cp.b");
100 * compare command name only until first dot.
101 */
102 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
103
Masahiro Yamada682195e2014-10-23 17:27:30 +0900104 for (cmdtp = table; cmdtp != table + table_len; cmdtp++) {
105 if (strncmp(cmd, cmdtp->name, len) == 0) {
106 if (len == strlen(cmdtp->name))
wdenk3086a972003-06-28 23:11:04 +0000107 return cmdtp; /* full match */
108
109 cmdtp_temp = cmdtp; /* abbreviated command ? */
110 n_found++;
111 }
wdenk5dfa25f2002-10-19 19:42:10 +0000112 }
wdenk3086a972003-06-28 23:11:04 +0000113 if (n_found == 1) { /* exactly one match */
wdenk57b2d802003-06-27 21:31:46 +0000114 return cmdtp_temp;
wdenk3086a972003-06-28 23:11:04 +0000115 }
Simon Glass4ef90b72016-03-19 02:18:38 -0600116#endif /* CONFIG_CMDLINE */
wdenk5dfa25f2002-10-19 19:42:10 +0000117
wdenk3086a972003-06-28 23:11:04 +0000118 return NULL; /* not found or ambiguous command */
wdenk57b2d802003-06-27 21:31:46 +0000119}
wdenk3902d702004-04-15 18:22:41 +0000120
Simon Glassed38aef2020-05-10 11:40:03 -0600121struct cmd_tbl *find_cmd(const char *cmd)
Kumar Galaac2dad62008-09-23 10:05:02 -0500122{
Simon Glassed38aef2020-05-10 11:40:03 -0600123 struct cmd_tbl *start = ll_entry_start(struct cmd_tbl, cmd);
124 const int len = ll_entry_count(struct cmd_tbl, cmd);
Marek Vasut69c5e342012-10-12 10:27:04 +0000125 return find_cmd_tbl(cmd, start, len);
Kumar Galaac2dad62008-09-23 10:05:02 -0500126}
127
Simon Glassed38aef2020-05-10 11:40:03 -0600128int cmd_usage(const struct cmd_tbl *cmdtp)
Peter Tyserddb3af92009-01-27 18:03:10 -0600129{
Wolfgang Denk9dd775b2009-05-24 19:17:29 +0200130 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
131
132#ifdef CONFIG_SYS_LONGHELP
133 printf("Usage:\n%s ", cmdtp->name);
134
135 if (!cmdtp->help) {
136 puts ("- No additional help available.\n");
137 return 1;
138 }
139
Masahiro Yamada682195e2014-10-23 17:27:30 +0900140 puts(cmdtp->help);
141 putc('\n');
Wolfgang Denk9dd775b2009-05-24 19:17:29 +0200142#endif /* CONFIG_SYS_LONGHELP */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200143 return 1;
Peter Tyserddb3af92009-01-27 18:03:10 -0600144}
145
wdenk3902d702004-04-15 18:22:41 +0000146#ifdef CONFIG_AUTO_COMPLETE
Boris Brezillon619fc682018-12-05 09:26:50 +0100147static char env_complete_buf[512];
wdenk3902d702004-04-15 18:22:41 +0000148
Simon Glassed38aef2020-05-10 11:40:03 -0600149int var_complete(int argc, char *const argv[], char last_char, int maxv,
150 char *cmdv[])
wdenk3902d702004-04-15 18:22:41 +0000151{
wdenk3902d702004-04-15 18:22:41 +0000152 int space;
153
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000154 space = last_char == '\0' || isblank(last_char);
wdenk3902d702004-04-15 18:22:41 +0000155
156 if (space && argc == 1)
Boris Brezillon619fc682018-12-05 09:26:50 +0100157 return env_complete("", maxv, cmdv, sizeof(env_complete_buf),
158 env_complete_buf, false);
wdenk3902d702004-04-15 18:22:41 +0000159
160 if (!space && argc == 2)
Boris Brezillon619fc682018-12-05 09:26:50 +0100161 return env_complete(argv[1], maxv, cmdv,
162 sizeof(env_complete_buf),
163 env_complete_buf, false);
Mike Frysinger8d882322010-12-17 16:51:59 -0500164
wdenk3902d702004-04-15 18:22:41 +0000165 return 0;
166}
167
Simon Glassed38aef2020-05-10 11:40:03 -0600168static int dollar_complete(int argc, char *const argv[], char last_char,
Boris Brezillon619fc682018-12-05 09:26:50 +0100169 int maxv, char *cmdv[])
170{
171 /* Make sure the last argument starts with a $. */
172 if (argc < 1 || argv[argc - 1][0] != '$' ||
173 last_char == '\0' || isblank(last_char))
174 return 0;
175
176 return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf),
177 env_complete_buf, true);
178}
179
wdenk3902d702004-04-15 18:22:41 +0000180/*************************************************************************************/
181
Simon Glassed38aef2020-05-10 11:40:03 -0600182int complete_subcmdv(struct cmd_tbl *cmdtp, int count, int argc,
183 char *const argv[], char last_char,
Boris Brezillonae73c332018-12-03 22:54:19 +0100184 int maxv, char *cmdv[])
wdenk3902d702004-04-15 18:22:41 +0000185{
Simon Glass4ef90b72016-03-19 02:18:38 -0600186#ifdef CONFIG_CMDLINE
Simon Glassed38aef2020-05-10 11:40:03 -0600187 const struct cmd_tbl *cmdend = cmdtp + count;
wdenk3902d702004-04-15 18:22:41 +0000188 const char *p;
189 int len, clen;
190 int n_found = 0;
191 const char *cmd;
192
193 /* sanity? */
194 if (maxv < 2)
195 return -2;
196
197 cmdv[0] = NULL;
198
199 if (argc == 0) {
200 /* output full list of commands */
Marek Vasut69c5e342012-10-12 10:27:04 +0000201 for (; cmdtp != cmdend; cmdtp++) {
wdenk3902d702004-04-15 18:22:41 +0000202 if (n_found >= maxv - 2) {
Andrew Gabbasova7c03b02013-12-27 10:05:14 -0600203 cmdv[n_found++] = "...";
wdenk3902d702004-04-15 18:22:41 +0000204 break;
205 }
Andrew Gabbasova7c03b02013-12-27 10:05:14 -0600206 cmdv[n_found++] = cmdtp->name;
wdenk3902d702004-04-15 18:22:41 +0000207 }
208 cmdv[n_found] = NULL;
209 return n_found;
210 }
211
212 /* more than one arg or one but the start of the next */
Masahiro Yamada682195e2014-10-23 17:27:30 +0900213 if (argc > 1 || last_char == '\0' || isblank(last_char)) {
Boris Brezillonae73c332018-12-03 22:54:19 +0100214 cmdtp = find_cmd_tbl(argv[0], cmdtp, count);
wdenk3902d702004-04-15 18:22:41 +0000215 if (cmdtp == NULL || cmdtp->complete == NULL) {
216 cmdv[0] = NULL;
217 return 0;
218 }
219 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
220 }
221
222 cmd = argv[0];
223 /*
224 * Some commands allow length modifiers (like "cp.b");
225 * compare command name only until first dot.
226 */
227 p = strchr(cmd, '.');
228 if (p == NULL)
229 len = strlen(cmd);
230 else
231 len = p - cmd;
232
233 /* return the partial matches */
Marek Vasut69c5e342012-10-12 10:27:04 +0000234 for (; cmdtp != cmdend; cmdtp++) {
wdenk3902d702004-04-15 18:22:41 +0000235
236 clen = strlen(cmdtp->name);
237 if (clen < len)
238 continue;
239
240 if (memcmp(cmd, cmdtp->name, len) != 0)
241 continue;
242
243 /* too many! */
244 if (n_found >= maxv - 2) {
245 cmdv[n_found++] = "...";
246 break;
247 }
248
249 cmdv[n_found++] = cmdtp->name;
250 }
251
252 cmdv[n_found] = NULL;
253 return n_found;
Simon Glass4ef90b72016-03-19 02:18:38 -0600254#else
255 return 0;
256#endif
wdenk3902d702004-04-15 18:22:41 +0000257}
258
Simon Glassed38aef2020-05-10 11:40:03 -0600259static int complete_cmdv(int argc, char *const argv[], char last_char,
Boris Brezillonae73c332018-12-03 22:54:19 +0100260 int maxv, char *cmdv[])
261{
262#ifdef CONFIG_CMDLINE
Simon Glassed38aef2020-05-10 11:40:03 -0600263 return complete_subcmdv(ll_entry_start(struct cmd_tbl, cmd),
264 ll_entry_count(struct cmd_tbl, cmd), argc, argv,
Boris Brezillonae73c332018-12-03 22:54:19 +0100265 last_char, maxv, cmdv);
266#else
267 return 0;
268#endif
269}
270
wdenk3902d702004-04-15 18:22:41 +0000271static int make_argv(char *s, int argvsz, char *argv[])
272{
273 int argc = 0;
274
275 /* split into argv */
276 while (argc < argvsz - 1) {
277
278 /* skip any white space */
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000279 while (isblank(*s))
wdenk3902d702004-04-15 18:22:41 +0000280 ++s;
281
Wolfgang Denka1be4762008-05-20 16:00:29 +0200282 if (*s == '\0') /* end of s, no more args */
wdenk3902d702004-04-15 18:22:41 +0000283 break;
284
285 argv[argc++] = s; /* begin of argument string */
286
287 /* find end of string */
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000288 while (*s && !isblank(*s))
wdenk3902d702004-04-15 18:22:41 +0000289 ++s;
290
291 if (*s == '\0') /* end of s, no more args */
292 break;
293
294 *s++ = '\0'; /* terminate current arg */
295 }
296 argv[argc] = NULL;
297
298 return argc;
299}
300
Simon Glassed38aef2020-05-10 11:40:03 -0600301static void print_argv(const char *banner, const char *leader, const char *sep,
302 int linemax, char *const argv[])
wdenk3902d702004-04-15 18:22:41 +0000303{
304 int ll = leader != NULL ? strlen(leader) : 0;
305 int sl = sep != NULL ? strlen(sep) : 0;
306 int len, i;
307
308 if (banner) {
309 puts("\n");
310 puts(banner);
311 }
312
313 i = linemax; /* force leader and newline */
314 while (*argv != NULL) {
315 len = strlen(*argv) + sl;
316 if (i + len >= linemax) {
317 puts("\n");
318 if (leader)
319 puts(leader);
320 i = ll - sl;
321 } else if (sep)
322 puts(sep);
323 puts(*argv++);
324 i += len;
325 }
326 printf("\n");
327}
328
Simon Glassed38aef2020-05-10 11:40:03 -0600329static int find_common_prefix(char *const argv[])
wdenk3902d702004-04-15 18:22:41 +0000330{
331 int i, len;
332 char *anchor, *s, *t;
333
334 if (*argv == NULL)
335 return 0;
336
337 /* begin with max */
338 anchor = *argv++;
339 len = strlen(anchor);
340 while ((t = *argv++) != NULL) {
341 s = anchor;
342 for (i = 0; i < len; i++, t++, s++) {
343 if (*t != *s)
344 break;
345 }
346 len = s - anchor;
347 }
348 return len;
349}
350
Heinrich Schuchardt831030d2017-11-19 23:07:50 +0100351static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */
wdenk3902d702004-04-15 18:22:41 +0000352
353int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
354{
355 int n = *np, col = *colp;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200356 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenk3902d702004-04-15 18:22:41 +0000357 char *cmdv[20];
358 char *s, *t;
359 const char *sep;
360 int i, j, k, len, seplen, argc;
361 int cnt;
362 char last_char;
Marek Vasutfe6cdf32019-06-27 00:17:27 +0200363#ifdef CONFIG_CMDLINE_PS_SUPPORT
364 const char *ps_prompt = env_get("PS1");
365#else
366 const char *ps_prompt = CONFIG_SYS_PROMPT;
367#endif
wdenk3902d702004-04-15 18:22:41 +0000368
Marek Vasutfe6cdf32019-06-27 00:17:27 +0200369 if (strcmp(prompt, ps_prompt) != 0)
wdenk3902d702004-04-15 18:22:41 +0000370 return 0; /* not in normal console */
371
372 cnt = strlen(buf);
373 if (cnt >= 1)
374 last_char = buf[cnt - 1];
375 else
376 last_char = '\0';
377
378 /* copy to secondary buffer which will be affected */
379 strcpy(tmp_buf, buf);
380
381 /* separate into argv */
382 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
383
Boris Brezillon619fc682018-12-05 09:26:50 +0100384 /* first try a $ completion */
385 i = dollar_complete(argc, argv, last_char,
386 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
387 if (!i) {
388 /* do the completion and return the possible completions */
389 i = complete_cmdv(argc, argv, last_char,
390 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
391 }
wdenk3902d702004-04-15 18:22:41 +0000392
393 /* no match; bell and out */
394 if (i == 0) {
395 if (argc > 1) /* allow tab for non command */
396 return 0;
397 putc('\a');
398 return 1;
399 }
400
401 s = NULL;
402 len = 0;
403 sep = NULL;
404 seplen = 0;
405 if (i == 1) { /* one match; perfect */
Boris Brezillon8506ec62018-12-03 22:54:18 +0100406 if (last_char != '\0' && !isblank(last_char))
407 k = strlen(argv[argc - 1]);
408 else
409 k = 0;
410
wdenk3902d702004-04-15 18:22:41 +0000411 s = cmdv[0] + k;
412 len = strlen(s);
413 sep = " ";
414 seplen = 1;
Masahiro Yamada682195e2014-10-23 17:27:30 +0900415 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
Boris Brezillon8506ec62018-12-03 22:54:18 +0100416 if (last_char != '\0' && !isblank(last_char))
417 k = strlen(argv[argc - 1]);
418 else
419 k = 0;
420
wdenk3902d702004-04-15 18:22:41 +0000421 j -= k;
422 if (j > 0) {
423 s = cmdv[0] + k;
424 len = j;
425 }
426 }
427
428 if (s != NULL) {
429 k = len + seplen;
430 /* make sure it fits */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200431 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
wdenk3902d702004-04-15 18:22:41 +0000432 putc('\a');
433 return 1;
434 }
435
436 t = buf + cnt;
437 for (i = 0; i < len; i++)
438 *t++ = *s++;
439 if (sep != NULL)
440 for (i = 0; i < seplen; i++)
441 *t++ = sep[i];
442 *t = '\0';
443 n += k;
444 col += k;
445 puts(t - k);
446 if (sep == NULL)
447 putc('\a');
448 *np = n;
449 *colp = col;
450 } else {
451 print_argv(NULL, " ", " ", 78, cmdv);
452
453 puts(prompt);
454 puts(buf);
455 }
456 return 1;
457}
458
459#endif
Jean-Christophe PLAGNIOL-VILLARD82290f22008-09-10 22:48:05 +0200460
461#ifdef CMD_DATA_SIZE
462int cmd_get_data_size(char* arg, int default_size)
463{
464 /* Check for a size specification .b, .w or .l.
465 */
466 int len = strlen(arg);
467 if (len > 2 && arg[len-2] == '.') {
Masahiro Yamada682195e2014-10-23 17:27:30 +0900468 switch (arg[len-1]) {
Jean-Christophe PLAGNIOL-VILLARD82290f22008-09-10 22:48:05 +0200469 case 'b':
470 return 1;
471 case 'w':
472 return 2;
473 case 'l':
474 return 4;
Simon Glass8927bf22019-12-28 10:45:10 -0700475#ifdef MEM_SUPPORT_64BIT_DATA
York Sun6c480012014-02-26 17:03:19 -0800476 case 'q':
477 return 8;
478#endif
Jean-Christophe PLAGNIOL-VILLARD82290f22008-09-10 22:48:05 +0200479 case 's':
480 return -2;
481 default:
482 return -1;
483 }
484 }
485 return default_size;
486}
487#endif
Heiko Schocher092cf162010-09-17 13:10:33 +0200488
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200489#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher092cf162010-09-17 13:10:33 +0200490DECLARE_GLOBAL_DATA_PTR;
491
Simon Glassed38aef2020-05-10 11:40:03 -0600492void fixup_cmdtable(struct cmd_tbl *cmdtp, int size)
Heiko Schocher092cf162010-09-17 13:10:33 +0200493{
494 int i;
495
496 if (gd->reloc_off == 0)
497 return;
498
499 for (i = 0; i < size; i++) {
500 ulong addr;
501
Michal Simek8d76d2f2019-10-21 12:54:20 +0200502 addr = (ulong)(cmdtp->cmd_rep) + gd->reloc_off;
503 cmdtp->cmd_rep =
Simon Glassed38aef2020-05-10 11:40:03 -0600504 (int (*)(struct cmd_tbl *, int, int,
Michal Simek8d76d2f2019-10-21 12:54:20 +0200505 char * const [], int *))addr;
506
Masahiro Yamada682195e2014-10-23 17:27:30 +0900507 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
Kun-Hua Huang89299e22015-08-24 14:52:35 +0800508#ifdef DEBUG_COMMANDS
Heiko Schocher092cf162010-09-17 13:10:33 +0200509 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
Masahiro Yamada682195e2014-10-23 17:27:30 +0900510 cmdtp->name, (ulong)(cmdtp->cmd), addr);
Heiko Schocher092cf162010-09-17 13:10:33 +0200511#endif
Simon Glassed38aef2020-05-10 11:40:03 -0600512 cmdtp->cmd = (int (*)(struct cmd_tbl *, int, int,
513 char *const []))addr;
Heiko Schocher092cf162010-09-17 13:10:33 +0200514 addr = (ulong)(cmdtp->name) + gd->reloc_off;
515 cmdtp->name = (char *)addr;
516 if (cmdtp->usage) {
517 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
518 cmdtp->usage = (char *)addr;
519 }
520#ifdef CONFIG_SYS_LONGHELP
521 if (cmdtp->help) {
522 addr = (ulong)(cmdtp->help) + gd->reloc_off;
523 cmdtp->help = (char *)addr;
524 }
525#endif
Daniel Schwierzecke5166222011-10-13 06:07:39 +0000526#ifdef CONFIG_AUTO_COMPLETE
527 if (cmdtp->complete) {
528 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
Daniel Schwierzeck76deb6c2011-10-18 11:12:22 +0000529 cmdtp->complete =
530 (int (*)(int, char * const [], char, int, char * []))addr;
Daniel Schwierzecke5166222011-10-13 06:07:39 +0000531 }
532#endif
Heiko Schocher092cf162010-09-17 13:10:33 +0200533 cmdtp++;
534 }
535}
536#endif
Simon Glass2176d602012-02-14 19:59:23 +0000537
Simon Glassed38aef2020-05-10 11:40:03 -0600538int cmd_always_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
539 char *const argv[], int *repeatable)
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100540{
541 *repeatable = 1;
542
543 return cmdtp->cmd(cmdtp, flag, argc, argv);
544}
545
Simon Glassed38aef2020-05-10 11:40:03 -0600546int cmd_never_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
547 char *const argv[], int *repeatable)
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100548{
549 *repeatable = 0;
550
551 return cmdtp->cmd(cmdtp, flag, argc, argv);
552}
553
Simon Glassed38aef2020-05-10 11:40:03 -0600554int cmd_discard_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
555 char *const argv[])
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100556{
557 int repeatable;
558
559 return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable);
560}
561
Simon Glass2176d602012-02-14 19:59:23 +0000562/**
563 * Call a command function. This should be the only route in U-Boot to call
564 * a command, so that we can track whether we are waiting for input or
565 * executing a command.
566 *
567 * @param cmdtp Pointer to the command to execute
568 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
569 * @param argc Number of arguments (arg 0 must be the command text)
570 * @param argv Arguments
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100571 * @param repeatable Can the command be repeated
Simon Glass2176d602012-02-14 19:59:23 +0000572 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
573 */
Simon Glassed38aef2020-05-10 11:40:03 -0600574static int cmd_call(struct cmd_tbl *cmdtp, int flag, int argc,
575 char *const argv[], int *repeatable)
Simon Glass2176d602012-02-14 19:59:23 +0000576{
577 int result;
578
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100579 result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable);
Simon Glass2176d602012-02-14 19:59:23 +0000580 if (result)
Peng Fand6c09ba2015-07-28 22:45:37 +0800581 debug("Command failed, result=%d\n", result);
Simon Glass2176d602012-02-14 19:59:23 +0000582 return result;
583}
Simon Glass1727b942012-02-14 19:59:25 +0000584
Simon Glassed38aef2020-05-10 11:40:03 -0600585enum command_ret_t cmd_process(int flag, int argc, char *const argv[],
Richard Genoud94ff0672012-12-03 06:28:28 +0000586 int *repeatable, ulong *ticks)
Simon Glass1727b942012-02-14 19:59:25 +0000587{
588 enum command_ret_t rc = CMD_RET_SUCCESS;
Simon Glassed38aef2020-05-10 11:40:03 -0600589 struct cmd_tbl *cmdtp;
Simon Glass1727b942012-02-14 19:59:25 +0000590
Christoph Muellner388b1452019-04-05 13:03:46 +0200591#if defined(CONFIG_SYS_XTRACE)
592 char *xtrace;
593
594 xtrace = env_get("xtrace");
595 if (xtrace) {
596 puts("+");
597 for (int i = 0; i < argc; i++) {
598 puts(" ");
599 puts(argv[i]);
600 }
601 puts("\n");
602 }
603#endif
604
Simon Glass1727b942012-02-14 19:59:25 +0000605 /* Look up command in command table */
606 cmdtp = find_cmd(argv[0]);
607 if (cmdtp == NULL) {
608 printf("Unknown command '%s' - try 'help'\n", argv[0]);
609 return 1;
610 }
611
612 /* found - check max args */
613 if (argc > cmdtp->maxargs)
614 rc = CMD_RET_USAGE;
615
616#if defined(CONFIG_CMD_BOOTD)
617 /* avoid "bootd" recursion */
618 else if (cmdtp->cmd == do_bootd) {
619 if (flag & CMD_FLAG_BOOTD) {
620 puts("'bootd' recursion detected\n");
621 rc = CMD_RET_FAILURE;
622 } else {
623 flag |= CMD_FLAG_BOOTD;
624 }
625 }
626#endif
627
628 /* If OK so far, then do the command */
629 if (!rc) {
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100630 int newrep;
631
Richard Genoud94ff0672012-12-03 06:28:28 +0000632 if (ticks)
633 *ticks = get_timer(0);
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100634 rc = cmd_call(cmdtp, flag, argc, argv, &newrep);
Richard Genoud94ff0672012-12-03 06:28:28 +0000635 if (ticks)
636 *ticks = get_timer(*ticks);
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100637 *repeatable &= newrep;
Simon Glass1727b942012-02-14 19:59:25 +0000638 }
639 if (rc == CMD_RET_USAGE)
640 rc = cmd_usage(cmdtp);
641 return rc;
642}
Simon Glassd7385532014-02-26 15:59:15 -0700643
Simon Glassed38aef2020-05-10 11:40:03 -0600644int cmd_process_error(struct cmd_tbl *cmdtp, int err)
Simon Glassd7385532014-02-26 15:59:15 -0700645{
Michal Simek2e1b90b2018-06-21 14:49:26 +0200646 if (err == CMD_RET_USAGE)
647 return CMD_RET_USAGE;
648
Simon Glassd7385532014-02-26 15:59:15 -0700649 if (err) {
650 printf("Command '%s' failed: Error %d\n", cmdtp->name, err);
Michal Simek44bdb572018-06-04 13:29:49 +0200651 return CMD_RET_FAILURE;
Simon Glassd7385532014-02-26 15:59:15 -0700652 }
653
Michal Simek44bdb572018-06-04 13:29:49 +0200654 return CMD_RET_SUCCESS;
Simon Glassd7385532014-02-26 15:59:15 -0700655}