blob: 41c91c6d8c876802caf9d00a95d2dbeef6daa39a [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>
Simon Glassa33a8242020-06-02 19:26:48 -060012#include <compiler.h>
wdenk5dfa25f2002-10-19 19:42:10 +000013#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070014#include <console.h>
Simon Glassaf95f202019-08-01 09:46:40 -060015#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Jason Hobbs4d91a6e2011-08-23 11:06:54 +000018#include <linux/ctype.h>
wdenk5dfa25f2002-10-19 19:42:10 +000019
Heinrich Schuchardt7cf5b402020-11-26 20:46:37 +010020DECLARE_GLOBAL_DATA_PTR;
21
wdenk5dfa25f2002-10-19 19:42:10 +000022/*
23 * Use puts() instead of printf() to avoid printf buffer overflow
24 * for long help messages
25 */
Detlev Zundel2dce5512009-03-25 17:27:52 +010026
Simon Glass09140112020-05-10 11:40:03 -060027int _do_help(struct cmd_tbl *cmd_start, int cmd_items, struct cmd_tbl *cmdtp,
28 int flag, int argc, char *const argv[])
wdenk5dfa25f2002-10-19 19:42:10 +000029{
30 int i;
31 int rcode = 0;
32
Masahiro Yamada616e2162014-10-23 17:27:30 +090033 if (argc == 1) { /* show list of commands */
Simon Glass09140112020-05-10 11:40:03 -060034 struct cmd_tbl *cmd_array[cmd_items];
wdenk9d2b18a2003-06-28 23:11:04 +000035 int i, j, swaps;
wdenk8bde7f72003-06-27 21:31:46 +000036
wdenk9d2b18a2003-06-28 23:11:04 +000037 /* Make array of commands from .uboot_cmd section */
Detlev Zundel2dce5512009-03-25 17:27:52 +010038 cmdtp = cmd_start;
wdenk9d2b18a2003-06-28 23:11:04 +000039 for (i = 0; i < cmd_items; i++) {
40 cmd_array[i] = cmdtp++;
wdenk8bde7f72003-06-27 21:31:46 +000041 }
wdenk8bde7f72003-06-27 21:31:46 +000042
wdenk9d2b18a2003-06-28 23:11:04 +000043 /* Sort command list (trivial bubble sort) */
44 for (i = cmd_items - 1; i > 0; --i) {
45 swaps = 0;
46 for (j = 0; j < i; ++j) {
Masahiro Yamada616e2162014-10-23 17:27:30 +090047 if (strcmp(cmd_array[j]->name,
48 cmd_array[j + 1]->name) > 0) {
Simon Glass09140112020-05-10 11:40:03 -060049 struct cmd_tbl *tmp;
wdenk9d2b18a2003-06-28 23:11:04 +000050 tmp = cmd_array[j];
51 cmd_array[j] = cmd_array[j + 1];
52 cmd_array[j + 1] = tmp;
53 ++swaps;
54 }
wdenk8bde7f72003-06-27 21:31:46 +000055 }
wdenk9d2b18a2003-06-28 23:11:04 +000056 if (!swaps)
57 break;
wdenk8bde7f72003-06-27 21:31:46 +000058 }
59
60 /* print short help (usage) */
wdenk9d2b18a2003-06-28 23:11:04 +000061 for (i = 0; i < cmd_items; i++) {
62 const char *usage = cmd_array[i]->usage;
63
wdenk5dfa25f2002-10-19 19:42:10 +000064 /* allow user abort */
Masahiro Yamada616e2162014-10-23 17:27:30 +090065 if (ctrlc())
wdenk5dfa25f2002-10-19 19:42:10 +000066 return 1;
wdenk9d2b18a2003-06-28 23:11:04 +000067 if (usage == NULL)
wdenk5dfa25f2002-10-19 19:42:10 +000068 continue;
Peter Tyser2fb26042009-01-27 18:03:12 -060069 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
70 cmd_array[i]->name, usage);
wdenk5dfa25f2002-10-19 19:42:10 +000071 }
wdenk5dfa25f2002-10-19 19:42:10 +000072 return 0;
73 }
wdenk5dfa25f2002-10-19 19:42:10 +000074 /*
75 * command help (long version)
76 */
wdenk8bde7f72003-06-27 21:31:46 +000077 for (i = 1; i < argc; ++i) {
Masahiro Yamada616e2162014-10-23 17:27:30 +090078 cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items);
79 if (cmdtp != NULL) {
Wolfgang Denk94796d82009-05-24 19:17:29 +020080 rcode |= cmd_usage(cmdtp);
wdenk71f95112003-06-15 22:40:42 +000081 } else {
Masahiro Yamada616e2162014-10-23 17:27:30 +090082 printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n",
83 argv[i]);
wdenk5dfa25f2002-10-19 19:42:10 +000084 rcode = 1;
85 }
86 }
87 return rcode;
88}
89
Masahiro Yamada616e2162014-10-23 17:27:30 +090090/* find command table entry for a command */
Simon Glass09140112020-05-10 11:40:03 -060091struct cmd_tbl *find_cmd_tbl(const char *cmd, struct cmd_tbl *table,
92 int table_len)
wdenk5dfa25f2002-10-19 19:42:10 +000093{
Simon Glassf8bb6962016-03-19 02:18:38 -060094#ifdef CONFIG_CMDLINE
Simon Glass09140112020-05-10 11:40:03 -060095 struct cmd_tbl *cmdtp;
96 struct cmd_tbl *cmdtp_temp = table; /* Init value */
wdenk9d2b18a2003-06-28 23:11:04 +000097 const char *p;
98 int len;
99 int n_found = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000100
Thomas Weber7013c062010-11-24 13:07:51 +0100101 if (!cmd)
102 return NULL;
wdenk9d2b18a2003-06-28 23:11:04 +0000103 /*
104 * Some commands allow length modifiers (like "cp.b");
105 * compare command name only until first dot.
106 */
107 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
108
Masahiro Yamada616e2162014-10-23 17:27:30 +0900109 for (cmdtp = table; cmdtp != table + table_len; cmdtp++) {
110 if (strncmp(cmd, cmdtp->name, len) == 0) {
111 if (len == strlen(cmdtp->name))
wdenk9d2b18a2003-06-28 23:11:04 +0000112 return cmdtp; /* full match */
113
114 cmdtp_temp = cmdtp; /* abbreviated command ? */
115 n_found++;
116 }
wdenk5dfa25f2002-10-19 19:42:10 +0000117 }
wdenk9d2b18a2003-06-28 23:11:04 +0000118 if (n_found == 1) { /* exactly one match */
wdenk8bde7f72003-06-27 21:31:46 +0000119 return cmdtp_temp;
wdenk9d2b18a2003-06-28 23:11:04 +0000120 }
Simon Glassf8bb6962016-03-19 02:18:38 -0600121#endif /* CONFIG_CMDLINE */
wdenk8bde7f72003-06-27 21:31:46 +0000122
wdenk9d2b18a2003-06-28 23:11:04 +0000123 return NULL; /* not found or ambiguous command */
wdenk5dfa25f2002-10-19 19:42:10 +0000124}
wdenk04a85b32004-04-15 18:22:41 +0000125
Simon Glass09140112020-05-10 11:40:03 -0600126struct cmd_tbl *find_cmd(const char *cmd)
Kumar Galab799cb42008-09-23 10:05:02 -0500127{
Simon Glass09140112020-05-10 11:40:03 -0600128 struct cmd_tbl *start = ll_entry_start(struct cmd_tbl, cmd);
129 const int len = ll_entry_count(struct cmd_tbl, cmd);
Marek Vasut6c7c9462012-10-12 10:27:04 +0000130 return find_cmd_tbl(cmd, start, len);
Kumar Galab799cb42008-09-23 10:05:02 -0500131}
132
Simon Glass09140112020-05-10 11:40:03 -0600133int cmd_usage(const struct cmd_tbl *cmdtp)
Peter Tyser62c3ae72009-01-27 18:03:10 -0600134{
Wolfgang Denk94796d82009-05-24 19:17:29 +0200135 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
136
137#ifdef CONFIG_SYS_LONGHELP
138 printf("Usage:\n%s ", cmdtp->name);
139
140 if (!cmdtp->help) {
141 puts ("- No additional help available.\n");
142 return 1;
143 }
144
Masahiro Yamada616e2162014-10-23 17:27:30 +0900145 puts(cmdtp->help);
146 putc('\n');
Wolfgang Denk94796d82009-05-24 19:17:29 +0200147#endif /* CONFIG_SYS_LONGHELP */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200148 return 1;
Peter Tyser62c3ae72009-01-27 18:03:10 -0600149}
150
wdenk04a85b32004-04-15 18:22:41 +0000151#ifdef CONFIG_AUTO_COMPLETE
Boris Brezillon03dcf172018-12-05 09:26:50 +0100152static char env_complete_buf[512];
wdenk04a85b32004-04-15 18:22:41 +0000153
Simon Glass09140112020-05-10 11:40:03 -0600154int var_complete(int argc, char *const argv[], char last_char, int maxv,
155 char *cmdv[])
wdenk04a85b32004-04-15 18:22:41 +0000156{
wdenk04a85b32004-04-15 18:22:41 +0000157 int space;
158
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000159 space = last_char == '\0' || isblank(last_char);
wdenk04a85b32004-04-15 18:22:41 +0000160
161 if (space && argc == 1)
Boris Brezillon03dcf172018-12-05 09:26:50 +0100162 return env_complete("", maxv, cmdv, sizeof(env_complete_buf),
163 env_complete_buf, false);
wdenk04a85b32004-04-15 18:22:41 +0000164
165 if (!space && argc == 2)
Boris Brezillon03dcf172018-12-05 09:26:50 +0100166 return env_complete(argv[1], maxv, cmdv,
167 sizeof(env_complete_buf),
168 env_complete_buf, false);
Mike Frysinger560d4242010-12-17 16:51:59 -0500169
wdenk04a85b32004-04-15 18:22:41 +0000170 return 0;
171}
172
Simon Glass09140112020-05-10 11:40:03 -0600173static int dollar_complete(int argc, char *const argv[], char last_char,
Boris Brezillon03dcf172018-12-05 09:26:50 +0100174 int maxv, char *cmdv[])
175{
176 /* Make sure the last argument starts with a $. */
177 if (argc < 1 || argv[argc - 1][0] != '$' ||
178 last_char == '\0' || isblank(last_char))
179 return 0;
180
181 return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf),
182 env_complete_buf, true);
183}
184
wdenk04a85b32004-04-15 18:22:41 +0000185/*************************************************************************************/
186
Simon Glass09140112020-05-10 11:40:03 -0600187int complete_subcmdv(struct cmd_tbl *cmdtp, int count, int argc,
188 char *const argv[], char last_char,
Boris Brezillon6fb61442018-12-03 22:54:19 +0100189 int maxv, char *cmdv[])
wdenk04a85b32004-04-15 18:22:41 +0000190{
Simon Glassf8bb6962016-03-19 02:18:38 -0600191#ifdef CONFIG_CMDLINE
Simon Glass09140112020-05-10 11:40:03 -0600192 const struct cmd_tbl *cmdend = cmdtp + count;
wdenk04a85b32004-04-15 18:22:41 +0000193 const char *p;
194 int len, clen;
195 int n_found = 0;
196 const char *cmd;
197
198 /* sanity? */
199 if (maxv < 2)
200 return -2;
201
202 cmdv[0] = NULL;
203
204 if (argc == 0) {
205 /* output full list of commands */
Marek Vasut6c7c9462012-10-12 10:27:04 +0000206 for (; cmdtp != cmdend; cmdtp++) {
wdenk04a85b32004-04-15 18:22:41 +0000207 if (n_found >= maxv - 2) {
Andrew Gabbasov9b438942013-12-27 10:05:14 -0600208 cmdv[n_found++] = "...";
wdenk04a85b32004-04-15 18:22:41 +0000209 break;
210 }
Andrew Gabbasov9b438942013-12-27 10:05:14 -0600211 cmdv[n_found++] = cmdtp->name;
wdenk04a85b32004-04-15 18:22:41 +0000212 }
213 cmdv[n_found] = NULL;
214 return n_found;
215 }
216
217 /* more than one arg or one but the start of the next */
Masahiro Yamada616e2162014-10-23 17:27:30 +0900218 if (argc > 1 || last_char == '\0' || isblank(last_char)) {
Boris Brezillon6fb61442018-12-03 22:54:19 +0100219 cmdtp = find_cmd_tbl(argv[0], cmdtp, count);
wdenk04a85b32004-04-15 18:22:41 +0000220 if (cmdtp == NULL || cmdtp->complete == NULL) {
221 cmdv[0] = NULL;
222 return 0;
223 }
224 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
225 }
226
227 cmd = argv[0];
228 /*
229 * Some commands allow length modifiers (like "cp.b");
230 * compare command name only until first dot.
231 */
232 p = strchr(cmd, '.');
233 if (p == NULL)
234 len = strlen(cmd);
235 else
236 len = p - cmd;
237
238 /* return the partial matches */
Marek Vasut6c7c9462012-10-12 10:27:04 +0000239 for (; cmdtp != cmdend; cmdtp++) {
wdenk04a85b32004-04-15 18:22:41 +0000240
241 clen = strlen(cmdtp->name);
242 if (clen < len)
243 continue;
244
245 if (memcmp(cmd, cmdtp->name, len) != 0)
246 continue;
247
248 /* too many! */
249 if (n_found >= maxv - 2) {
250 cmdv[n_found++] = "...";
251 break;
252 }
253
254 cmdv[n_found++] = cmdtp->name;
255 }
256
257 cmdv[n_found] = NULL;
258 return n_found;
Simon Glassf8bb6962016-03-19 02:18:38 -0600259#else
260 return 0;
261#endif
wdenk04a85b32004-04-15 18:22:41 +0000262}
263
Simon Glass09140112020-05-10 11:40:03 -0600264static int complete_cmdv(int argc, char *const argv[], char last_char,
Boris Brezillon6fb61442018-12-03 22:54:19 +0100265 int maxv, char *cmdv[])
266{
267#ifdef CONFIG_CMDLINE
Simon Glass09140112020-05-10 11:40:03 -0600268 return complete_subcmdv(ll_entry_start(struct cmd_tbl, cmd),
269 ll_entry_count(struct cmd_tbl, cmd), argc, argv,
Boris Brezillon6fb61442018-12-03 22:54:19 +0100270 last_char, maxv, cmdv);
271#else
272 return 0;
273#endif
274}
275
wdenk04a85b32004-04-15 18:22:41 +0000276static int make_argv(char *s, int argvsz, char *argv[])
277{
278 int argc = 0;
279
280 /* split into argv */
281 while (argc < argvsz - 1) {
282
283 /* skip any white space */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000284 while (isblank(*s))
wdenk04a85b32004-04-15 18:22:41 +0000285 ++s;
286
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200287 if (*s == '\0') /* end of s, no more args */
wdenk04a85b32004-04-15 18:22:41 +0000288 break;
289
290 argv[argc++] = s; /* begin of argument string */
291
292 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000293 while (*s && !isblank(*s))
wdenk04a85b32004-04-15 18:22:41 +0000294 ++s;
295
296 if (*s == '\0') /* end of s, no more args */
297 break;
298
299 *s++ = '\0'; /* terminate current arg */
300 }
301 argv[argc] = NULL;
302
303 return argc;
304}
305
Simon Glass09140112020-05-10 11:40:03 -0600306static void print_argv(const char *banner, const char *leader, const char *sep,
307 int linemax, char *const argv[])
wdenk04a85b32004-04-15 18:22:41 +0000308{
309 int ll = leader != NULL ? strlen(leader) : 0;
310 int sl = sep != NULL ? strlen(sep) : 0;
311 int len, i;
312
313 if (banner) {
314 puts("\n");
315 puts(banner);
316 }
317
318 i = linemax; /* force leader and newline */
319 while (*argv != NULL) {
320 len = strlen(*argv) + sl;
321 if (i + len >= linemax) {
322 puts("\n");
323 if (leader)
324 puts(leader);
325 i = ll - sl;
326 } else if (sep)
327 puts(sep);
328 puts(*argv++);
329 i += len;
330 }
331 printf("\n");
332}
333
Simon Glass09140112020-05-10 11:40:03 -0600334static int find_common_prefix(char *const argv[])
wdenk04a85b32004-04-15 18:22:41 +0000335{
336 int i, len;
337 char *anchor, *s, *t;
338
339 if (*argv == NULL)
340 return 0;
341
342 /* begin with max */
343 anchor = *argv++;
344 len = strlen(anchor);
345 while ((t = *argv++) != NULL) {
346 s = anchor;
347 for (i = 0; i < len; i++, t++, s++) {
348 if (*t != *s)
349 break;
350 }
351 len = s - anchor;
352 }
353 return len;
354}
355
Heinrich Schuchardt2614a202017-11-19 23:07:50 +0100356static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */
wdenk04a85b32004-04-15 18:22:41 +0000357
358int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
359{
360 int n = *np, col = *colp;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200361 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenk04a85b32004-04-15 18:22:41 +0000362 char *cmdv[20];
363 char *s, *t;
364 const char *sep;
365 int i, j, k, len, seplen, argc;
366 int cnt;
367 char last_char;
Marek Vasut4225f832019-06-27 00:17:27 +0200368#ifdef CONFIG_CMDLINE_PS_SUPPORT
369 const char *ps_prompt = env_get("PS1");
370#else
371 const char *ps_prompt = CONFIG_SYS_PROMPT;
372#endif
wdenk04a85b32004-04-15 18:22:41 +0000373
Marek Vasut4225f832019-06-27 00:17:27 +0200374 if (strcmp(prompt, ps_prompt) != 0)
wdenk04a85b32004-04-15 18:22:41 +0000375 return 0; /* not in normal console */
376
377 cnt = strlen(buf);
378 if (cnt >= 1)
379 last_char = buf[cnt - 1];
380 else
381 last_char = '\0';
382
383 /* copy to secondary buffer which will be affected */
384 strcpy(tmp_buf, buf);
385
386 /* separate into argv */
387 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
388
Boris Brezillon03dcf172018-12-05 09:26:50 +0100389 /* first try a $ completion */
390 i = dollar_complete(argc, argv, last_char,
391 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
392 if (!i) {
393 /* do the completion and return the possible completions */
394 i = complete_cmdv(argc, argv, last_char,
395 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
396 }
wdenk04a85b32004-04-15 18:22:41 +0000397
398 /* no match; bell and out */
399 if (i == 0) {
400 if (argc > 1) /* allow tab for non command */
401 return 0;
402 putc('\a');
403 return 1;
404 }
405
406 s = NULL;
407 len = 0;
408 sep = NULL;
409 seplen = 0;
410 if (i == 1) { /* one match; perfect */
Boris Brezilloncbe07eb2018-12-03 22:54:18 +0100411 if (last_char != '\0' && !isblank(last_char))
412 k = strlen(argv[argc - 1]);
413 else
414 k = 0;
415
wdenk04a85b32004-04-15 18:22:41 +0000416 s = cmdv[0] + k;
417 len = strlen(s);
418 sep = " ";
419 seplen = 1;
Masahiro Yamada616e2162014-10-23 17:27:30 +0900420 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
Boris Brezilloncbe07eb2018-12-03 22:54:18 +0100421 if (last_char != '\0' && !isblank(last_char))
422 k = strlen(argv[argc - 1]);
423 else
424 k = 0;
425
wdenk04a85b32004-04-15 18:22:41 +0000426 j -= k;
427 if (j > 0) {
428 s = cmdv[0] + k;
429 len = j;
430 }
431 }
432
433 if (s != NULL) {
434 k = len + seplen;
435 /* make sure it fits */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200436 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
wdenk04a85b32004-04-15 18:22:41 +0000437 putc('\a');
438 return 1;
439 }
440
441 t = buf + cnt;
442 for (i = 0; i < len; i++)
443 *t++ = *s++;
444 if (sep != NULL)
445 for (i = 0; i < seplen; i++)
446 *t++ = sep[i];
447 *t = '\0';
448 n += k;
449 col += k;
450 puts(t - k);
451 if (sep == NULL)
452 putc('\a');
453 *np = n;
454 *colp = col;
455 } else {
456 print_argv(NULL, " ", " ", 78, cmdv);
457
458 puts(prompt);
459 puts(buf);
460 }
461 return 1;
462}
463
464#endif
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200465
466#ifdef CMD_DATA_SIZE
467int cmd_get_data_size(char* arg, int default_size)
468{
469 /* Check for a size specification .b, .w or .l.
470 */
471 int len = strlen(arg);
472 if (len > 2 && arg[len-2] == '.') {
Masahiro Yamada616e2162014-10-23 17:27:30 +0900473 switch (arg[len-1]) {
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200474 case 'b':
475 return 1;
476 case 'w':
477 return 2;
478 case 'l':
479 return 4;
480 case 's':
Simon Glass7526dee2020-11-01 14:15:36 -0700481 return CMD_DATA_SIZE_STR;
Simon Glassa33a8242020-06-02 19:26:48 -0600482 case 'q':
483 if (MEM_SUPPORT_64BIT_DATA)
484 return 8;
485 /* no break */
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200486 default:
Simon Glass7526dee2020-11-01 14:15:36 -0700487 return CMD_DATA_SIZE_ERR;
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200488 }
489 }
490 return default_size;
491}
492#endif
Heiko Schocher620f1f62010-09-17 13:10:33 +0200493
Simon Glass09140112020-05-10 11:40:03 -0600494void fixup_cmdtable(struct cmd_tbl *cmdtp, int size)
Heiko Schocher620f1f62010-09-17 13:10:33 +0200495{
496 int i;
497
498 if (gd->reloc_off == 0)
499 return;
500
501 for (i = 0; i < size; i++) {
502 ulong addr;
503
Michal Simek6f94daf2019-10-21 12:54:20 +0200504 addr = (ulong)(cmdtp->cmd_rep) + gd->reloc_off;
505 cmdtp->cmd_rep =
Simon Glass09140112020-05-10 11:40:03 -0600506 (int (*)(struct cmd_tbl *, int, int,
Michal Simek6f94daf2019-10-21 12:54:20 +0200507 char * const [], int *))addr;
508
Masahiro Yamada616e2162014-10-23 17:27:30 +0900509 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
Kun-Hua Huang2e88bb22015-08-24 14:52:35 +0800510#ifdef DEBUG_COMMANDS
Heiko Schocher620f1f62010-09-17 13:10:33 +0200511 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
Masahiro Yamada616e2162014-10-23 17:27:30 +0900512 cmdtp->name, (ulong)(cmdtp->cmd), addr);
Heiko Schocher620f1f62010-09-17 13:10:33 +0200513#endif
Simon Glass09140112020-05-10 11:40:03 -0600514 cmdtp->cmd = (int (*)(struct cmd_tbl *, int, int,
515 char *const []))addr;
Heiko Schocher620f1f62010-09-17 13:10:33 +0200516 addr = (ulong)(cmdtp->name) + gd->reloc_off;
517 cmdtp->name = (char *)addr;
518 if (cmdtp->usage) {
519 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
520 cmdtp->usage = (char *)addr;
521 }
522#ifdef CONFIG_SYS_LONGHELP
523 if (cmdtp->help) {
524 addr = (ulong)(cmdtp->help) + gd->reloc_off;
525 cmdtp->help = (char *)addr;
526 }
527#endif
Daniel Schwierzeckfa28bd22011-10-13 06:07:39 +0000528#ifdef CONFIG_AUTO_COMPLETE
529 if (cmdtp->complete) {
530 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
Daniel Schwierzeck3668d8f2011-10-18 11:12:22 +0000531 cmdtp->complete =
532 (int (*)(int, char * const [], char, int, char * []))addr;
Daniel Schwierzeckfa28bd22011-10-13 06:07:39 +0000533 }
534#endif
Heiko Schocher620f1f62010-09-17 13:10:33 +0200535 cmdtp++;
536 }
537}
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
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100573 * Return: 0 if command succeeded, else non-zero (CMD_RET_...)
Simon Glassbdf8e342012-02-14 19:59:23 +0000574 */
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}