blob: 846e16e2ada27c42029ec13bea358eb37f52b637 [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 Glass30f33332023-01-06 08:52:28 -060016#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
Simon Glass30f33332023-01-06 08:52:28 -060018#include <mapmem.h>
Simon Glass401d1c42020-10-30 21:38:53 -060019#include <asm/global_data.h>
Jason Hobbs4d91a6e2011-08-23 11:06:54 +000020#include <linux/ctype.h>
wdenk5dfa25f2002-10-19 19:42:10 +000021
Heinrich Schuchardt7cf5b402020-11-26 20:46:37 +010022DECLARE_GLOBAL_DATA_PTR;
23
wdenk5dfa25f2002-10-19 19:42:10 +000024/*
25 * Use puts() instead of printf() to avoid printf buffer overflow
26 * for long help messages
27 */
Detlev Zundel2dce5512009-03-25 17:27:52 +010028
Simon Glass09140112020-05-10 11:40:03 -060029int _do_help(struct cmd_tbl *cmd_start, int cmd_items, struct cmd_tbl *cmdtp,
30 int flag, int argc, char *const argv[])
wdenk5dfa25f2002-10-19 19:42:10 +000031{
32 int i;
33 int rcode = 0;
34
Masahiro Yamada616e2162014-10-23 17:27:30 +090035 if (argc == 1) { /* show list of commands */
Simon Glass09140112020-05-10 11:40:03 -060036 struct cmd_tbl *cmd_array[cmd_items];
wdenk9d2b18a2003-06-28 23:11:04 +000037 int i, j, swaps;
wdenk8bde7f72003-06-27 21:31:46 +000038
wdenk9d2b18a2003-06-28 23:11:04 +000039 /* Make array of commands from .uboot_cmd section */
Detlev Zundel2dce5512009-03-25 17:27:52 +010040 cmdtp = cmd_start;
wdenk9d2b18a2003-06-28 23:11:04 +000041 for (i = 0; i < cmd_items; i++) {
42 cmd_array[i] = cmdtp++;
wdenk8bde7f72003-06-27 21:31:46 +000043 }
wdenk8bde7f72003-06-27 21:31:46 +000044
wdenk9d2b18a2003-06-28 23:11:04 +000045 /* Sort command list (trivial bubble sort) */
46 for (i = cmd_items - 1; i > 0; --i) {
47 swaps = 0;
48 for (j = 0; j < i; ++j) {
Masahiro Yamada616e2162014-10-23 17:27:30 +090049 if (strcmp(cmd_array[j]->name,
50 cmd_array[j + 1]->name) > 0) {
Simon Glass09140112020-05-10 11:40:03 -060051 struct cmd_tbl *tmp;
wdenk9d2b18a2003-06-28 23:11:04 +000052 tmp = cmd_array[j];
53 cmd_array[j] = cmd_array[j + 1];
54 cmd_array[j + 1] = tmp;
55 ++swaps;
56 }
wdenk8bde7f72003-06-27 21:31:46 +000057 }
wdenk9d2b18a2003-06-28 23:11:04 +000058 if (!swaps)
59 break;
wdenk8bde7f72003-06-27 21:31:46 +000060 }
61
62 /* print short help (usage) */
wdenk9d2b18a2003-06-28 23:11:04 +000063 for (i = 0; i < cmd_items; i++) {
64 const char *usage = cmd_array[i]->usage;
65
wdenk5dfa25f2002-10-19 19:42:10 +000066 /* allow user abort */
Masahiro Yamada616e2162014-10-23 17:27:30 +090067 if (ctrlc())
wdenk5dfa25f2002-10-19 19:42:10 +000068 return 1;
wdenk9d2b18a2003-06-28 23:11:04 +000069 if (usage == NULL)
wdenk5dfa25f2002-10-19 19:42:10 +000070 continue;
Tom Rini6e7df1d2023-01-10 11:19:45 -050071 printf("%-*s- %s\n", CFG_SYS_HELP_CMD_WIDTH,
Peter Tyser2fb26042009-01-27 18:03:12 -060072 cmd_array[i]->name, usage);
wdenk5dfa25f2002-10-19 19:42:10 +000073 }
wdenk5dfa25f2002-10-19 19:42:10 +000074 return 0;
75 }
wdenk5dfa25f2002-10-19 19:42:10 +000076 /*
77 * command help (long version)
78 */
wdenk8bde7f72003-06-27 21:31:46 +000079 for (i = 1; i < argc; ++i) {
Masahiro Yamada616e2162014-10-23 17:27:30 +090080 cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items);
81 if (cmdtp != NULL) {
Wolfgang Denk94796d82009-05-24 19:17:29 +020082 rcode |= cmd_usage(cmdtp);
wdenk71f95112003-06-15 22:40:42 +000083 } else {
Masahiro Yamada616e2162014-10-23 17:27:30 +090084 printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n",
85 argv[i]);
wdenk5dfa25f2002-10-19 19:42:10 +000086 rcode = 1;
87 }
88 }
89 return rcode;
90}
91
Masahiro Yamada616e2162014-10-23 17:27:30 +090092/* find command table entry for a command */
Simon Glass09140112020-05-10 11:40:03 -060093struct cmd_tbl *find_cmd_tbl(const char *cmd, struct cmd_tbl *table,
94 int table_len)
wdenk5dfa25f2002-10-19 19:42:10 +000095{
Simon Glassf8bb6962016-03-19 02:18:38 -060096#ifdef CONFIG_CMDLINE
Simon Glass09140112020-05-10 11:40:03 -060097 struct cmd_tbl *cmdtp;
98 struct cmd_tbl *cmdtp_temp = table; /* Init value */
wdenk9d2b18a2003-06-28 23:11:04 +000099 const char *p;
100 int len;
101 int n_found = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000102
Thomas Weber7013c062010-11-24 13:07:51 +0100103 if (!cmd)
104 return NULL;
wdenk9d2b18a2003-06-28 23:11:04 +0000105 /*
106 * Some commands allow length modifiers (like "cp.b");
107 * compare command name only until first dot.
108 */
109 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
110
Masahiro Yamada616e2162014-10-23 17:27:30 +0900111 for (cmdtp = table; cmdtp != table + table_len; cmdtp++) {
112 if (strncmp(cmd, cmdtp->name, len) == 0) {
113 if (len == strlen(cmdtp->name))
wdenk9d2b18a2003-06-28 23:11:04 +0000114 return cmdtp; /* full match */
115
116 cmdtp_temp = cmdtp; /* abbreviated command ? */
117 n_found++;
118 }
wdenk5dfa25f2002-10-19 19:42:10 +0000119 }
wdenk9d2b18a2003-06-28 23:11:04 +0000120 if (n_found == 1) { /* exactly one match */
wdenk8bde7f72003-06-27 21:31:46 +0000121 return cmdtp_temp;
wdenk9d2b18a2003-06-28 23:11:04 +0000122 }
Simon Glassf8bb6962016-03-19 02:18:38 -0600123#endif /* CONFIG_CMDLINE */
wdenk8bde7f72003-06-27 21:31:46 +0000124
wdenk9d2b18a2003-06-28 23:11:04 +0000125 return NULL; /* not found or ambiguous command */
wdenk5dfa25f2002-10-19 19:42:10 +0000126}
wdenk04a85b32004-04-15 18:22:41 +0000127
Simon Glass09140112020-05-10 11:40:03 -0600128struct cmd_tbl *find_cmd(const char *cmd)
Kumar Galab799cb42008-09-23 10:05:02 -0500129{
Simon Glass09140112020-05-10 11:40:03 -0600130 struct cmd_tbl *start = ll_entry_start(struct cmd_tbl, cmd);
131 const int len = ll_entry_count(struct cmd_tbl, cmd);
Marek Vasut6c7c9462012-10-12 10:27:04 +0000132 return find_cmd_tbl(cmd, start, len);
Kumar Galab799cb42008-09-23 10:05:02 -0500133}
134
Simon Glass09140112020-05-10 11:40:03 -0600135int cmd_usage(const struct cmd_tbl *cmdtp)
Peter Tyser62c3ae72009-01-27 18:03:10 -0600136{
Wolfgang Denk94796d82009-05-24 19:17:29 +0200137 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
138
139#ifdef CONFIG_SYS_LONGHELP
140 printf("Usage:\n%s ", cmdtp->name);
141
142 if (!cmdtp->help) {
143 puts ("- No additional help available.\n");
144 return 1;
145 }
146
Masahiro Yamada616e2162014-10-23 17:27:30 +0900147 puts(cmdtp->help);
148 putc('\n');
Wolfgang Denk94796d82009-05-24 19:17:29 +0200149#endif /* CONFIG_SYS_LONGHELP */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200150 return 1;
Peter Tyser62c3ae72009-01-27 18:03:10 -0600151}
152
wdenk04a85b32004-04-15 18:22:41 +0000153#ifdef CONFIG_AUTO_COMPLETE
Boris Brezillon03dcf172018-12-05 09:26:50 +0100154static char env_complete_buf[512];
wdenk04a85b32004-04-15 18:22:41 +0000155
Simon Glass09140112020-05-10 11:40:03 -0600156int var_complete(int argc, char *const argv[], char last_char, int maxv,
157 char *cmdv[])
wdenk04a85b32004-04-15 18:22:41 +0000158{
wdenk04a85b32004-04-15 18:22:41 +0000159 int space;
160
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000161 space = last_char == '\0' || isblank(last_char);
wdenk04a85b32004-04-15 18:22:41 +0000162
163 if (space && argc == 1)
Boris Brezillon03dcf172018-12-05 09:26:50 +0100164 return env_complete("", maxv, cmdv, sizeof(env_complete_buf),
165 env_complete_buf, false);
wdenk04a85b32004-04-15 18:22:41 +0000166
167 if (!space && argc == 2)
Boris Brezillon03dcf172018-12-05 09:26:50 +0100168 return env_complete(argv[1], maxv, cmdv,
169 sizeof(env_complete_buf),
170 env_complete_buf, false);
Mike Frysinger560d4242010-12-17 16:51:59 -0500171
wdenk04a85b32004-04-15 18:22:41 +0000172 return 0;
173}
174
Simon Glass09140112020-05-10 11:40:03 -0600175static int dollar_complete(int argc, char *const argv[], char last_char,
Boris Brezillon03dcf172018-12-05 09:26:50 +0100176 int maxv, char *cmdv[])
177{
178 /* Make sure the last argument starts with a $. */
179 if (argc < 1 || argv[argc - 1][0] != '$' ||
180 last_char == '\0' || isblank(last_char))
181 return 0;
182
183 return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf),
184 env_complete_buf, true);
185}
186
wdenk04a85b32004-04-15 18:22:41 +0000187/*************************************************************************************/
188
Simon Glass09140112020-05-10 11:40:03 -0600189int complete_subcmdv(struct cmd_tbl *cmdtp, int count, int argc,
190 char *const argv[], char last_char,
Boris Brezillon6fb61442018-12-03 22:54:19 +0100191 int maxv, char *cmdv[])
wdenk04a85b32004-04-15 18:22:41 +0000192{
Simon Glassf8bb6962016-03-19 02:18:38 -0600193#ifdef CONFIG_CMDLINE
Simon Glass09140112020-05-10 11:40:03 -0600194 const struct cmd_tbl *cmdend = cmdtp + count;
wdenk04a85b32004-04-15 18:22:41 +0000195 const char *p;
196 int len, clen;
197 int n_found = 0;
198 const char *cmd;
199
200 /* sanity? */
201 if (maxv < 2)
202 return -2;
203
204 cmdv[0] = NULL;
205
206 if (argc == 0) {
207 /* output full list of commands */
Marek Vasut6c7c9462012-10-12 10:27:04 +0000208 for (; cmdtp != cmdend; cmdtp++) {
wdenk04a85b32004-04-15 18:22:41 +0000209 if (n_found >= maxv - 2) {
Andrew Gabbasov9b438942013-12-27 10:05:14 -0600210 cmdv[n_found++] = "...";
wdenk04a85b32004-04-15 18:22:41 +0000211 break;
212 }
Andrew Gabbasov9b438942013-12-27 10:05:14 -0600213 cmdv[n_found++] = cmdtp->name;
wdenk04a85b32004-04-15 18:22:41 +0000214 }
215 cmdv[n_found] = NULL;
216 return n_found;
217 }
218
219 /* more than one arg or one but the start of the next */
Masahiro Yamada616e2162014-10-23 17:27:30 +0900220 if (argc > 1 || last_char == '\0' || isblank(last_char)) {
Boris Brezillon6fb61442018-12-03 22:54:19 +0100221 cmdtp = find_cmd_tbl(argv[0], cmdtp, count);
wdenk04a85b32004-04-15 18:22:41 +0000222 if (cmdtp == NULL || cmdtp->complete == NULL) {
223 cmdv[0] = NULL;
224 return 0;
225 }
226 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
227 }
228
229 cmd = argv[0];
230 /*
231 * Some commands allow length modifiers (like "cp.b");
232 * compare command name only until first dot.
233 */
234 p = strchr(cmd, '.');
235 if (p == NULL)
236 len = strlen(cmd);
237 else
238 len = p - cmd;
239
240 /* return the partial matches */
Marek Vasut6c7c9462012-10-12 10:27:04 +0000241 for (; cmdtp != cmdend; cmdtp++) {
wdenk04a85b32004-04-15 18:22:41 +0000242
243 clen = strlen(cmdtp->name);
244 if (clen < len)
245 continue;
246
247 if (memcmp(cmd, cmdtp->name, len) != 0)
248 continue;
249
250 /* too many! */
251 if (n_found >= maxv - 2) {
252 cmdv[n_found++] = "...";
253 break;
254 }
255
256 cmdv[n_found++] = cmdtp->name;
257 }
258
259 cmdv[n_found] = NULL;
260 return n_found;
Simon Glassf8bb6962016-03-19 02:18:38 -0600261#else
262 return 0;
263#endif
wdenk04a85b32004-04-15 18:22:41 +0000264}
265
Simon Glass09140112020-05-10 11:40:03 -0600266static int complete_cmdv(int argc, char *const argv[], char last_char,
Boris Brezillon6fb61442018-12-03 22:54:19 +0100267 int maxv, char *cmdv[])
268{
269#ifdef CONFIG_CMDLINE
Simon Glass09140112020-05-10 11:40:03 -0600270 return complete_subcmdv(ll_entry_start(struct cmd_tbl, cmd),
271 ll_entry_count(struct cmd_tbl, cmd), argc, argv,
Boris Brezillon6fb61442018-12-03 22:54:19 +0100272 last_char, maxv, cmdv);
273#else
274 return 0;
275#endif
276}
277
wdenk04a85b32004-04-15 18:22:41 +0000278static int make_argv(char *s, int argvsz, char *argv[])
279{
280 int argc = 0;
281
282 /* split into argv */
283 while (argc < argvsz - 1) {
284
285 /* skip any white space */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000286 while (isblank(*s))
wdenk04a85b32004-04-15 18:22:41 +0000287 ++s;
288
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200289 if (*s == '\0') /* end of s, no more args */
wdenk04a85b32004-04-15 18:22:41 +0000290 break;
291
292 argv[argc++] = s; /* begin of argument string */
293
294 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000295 while (*s && !isblank(*s))
wdenk04a85b32004-04-15 18:22:41 +0000296 ++s;
297
298 if (*s == '\0') /* end of s, no more args */
299 break;
300
301 *s++ = '\0'; /* terminate current arg */
302 }
303 argv[argc] = NULL;
304
305 return argc;
306}
307
Simon Glass09140112020-05-10 11:40:03 -0600308static void print_argv(const char *banner, const char *leader, const char *sep,
309 int linemax, char *const argv[])
wdenk04a85b32004-04-15 18:22:41 +0000310{
311 int ll = leader != NULL ? strlen(leader) : 0;
312 int sl = sep != NULL ? strlen(sep) : 0;
313 int len, i;
314
315 if (banner) {
316 puts("\n");
317 puts(banner);
318 }
319
320 i = linemax; /* force leader and newline */
321 while (*argv != NULL) {
322 len = strlen(*argv) + sl;
323 if (i + len >= linemax) {
324 puts("\n");
325 if (leader)
326 puts(leader);
327 i = ll - sl;
328 } else if (sep)
329 puts(sep);
330 puts(*argv++);
331 i += len;
332 }
333 printf("\n");
334}
335
Simon Glass09140112020-05-10 11:40:03 -0600336static int find_common_prefix(char *const argv[])
wdenk04a85b32004-04-15 18:22:41 +0000337{
338 int i, len;
339 char *anchor, *s, *t;
340
341 if (*argv == NULL)
342 return 0;
343
344 /* begin with max */
345 anchor = *argv++;
346 len = strlen(anchor);
347 while ((t = *argv++) != NULL) {
348 s = anchor;
349 for (i = 0; i < len; i++, t++, s++) {
350 if (*t != *s)
351 break;
352 }
353 len = s - anchor;
354 }
355 return len;
356}
357
Heinrich Schuchardt2614a202017-11-19 23:07:50 +0100358static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */
wdenk04a85b32004-04-15 18:22:41 +0000359
360int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
361{
362 int n = *np, col = *colp;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200363 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenk04a85b32004-04-15 18:22:41 +0000364 char *cmdv[20];
365 char *s, *t;
366 const char *sep;
367 int i, j, k, len, seplen, argc;
368 int cnt;
369 char last_char;
Marek Vasut4225f832019-06-27 00:17:27 +0200370#ifdef CONFIG_CMDLINE_PS_SUPPORT
371 const char *ps_prompt = env_get("PS1");
372#else
373 const char *ps_prompt = CONFIG_SYS_PROMPT;
374#endif
wdenk04a85b32004-04-15 18:22:41 +0000375
Marek Vasut4225f832019-06-27 00:17:27 +0200376 if (strcmp(prompt, ps_prompt) != 0)
wdenk04a85b32004-04-15 18:22:41 +0000377 return 0; /* not in normal console */
378
379 cnt = strlen(buf);
380 if (cnt >= 1)
381 last_char = buf[cnt - 1];
382 else
383 last_char = '\0';
384
385 /* copy to secondary buffer which will be affected */
386 strcpy(tmp_buf, buf);
387
388 /* separate into argv */
389 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
390
Boris Brezillon03dcf172018-12-05 09:26:50 +0100391 /* first try a $ completion */
392 i = dollar_complete(argc, argv, last_char,
393 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
394 if (!i) {
395 /* do the completion and return the possible completions */
396 i = complete_cmdv(argc, argv, last_char,
397 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
398 }
wdenk04a85b32004-04-15 18:22:41 +0000399
400 /* no match; bell and out */
401 if (i == 0) {
402 if (argc > 1) /* allow tab for non command */
403 return 0;
404 putc('\a');
405 return 1;
406 }
407
408 s = NULL;
409 len = 0;
410 sep = NULL;
411 seplen = 0;
412 if (i == 1) { /* one match; perfect */
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 s = cmdv[0] + k;
419 len = strlen(s);
420 sep = " ";
421 seplen = 1;
Masahiro Yamada616e2162014-10-23 17:27:30 +0900422 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
Boris Brezilloncbe07eb2018-12-03 22:54:18 +0100423 if (last_char != '\0' && !isblank(last_char))
424 k = strlen(argv[argc - 1]);
425 else
426 k = 0;
427
wdenk04a85b32004-04-15 18:22:41 +0000428 j -= k;
429 if (j > 0) {
430 s = cmdv[0] + k;
431 len = j;
432 }
433 }
434
435 if (s != NULL) {
436 k = len + seplen;
437 /* make sure it fits */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200438 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
wdenk04a85b32004-04-15 18:22:41 +0000439 putc('\a');
440 return 1;
441 }
442
443 t = buf + cnt;
444 for (i = 0; i < len; i++)
445 *t++ = *s++;
446 if (sep != NULL)
447 for (i = 0; i < seplen; i++)
448 *t++ = sep[i];
449 *t = '\0';
450 n += k;
451 col += k;
452 puts(t - k);
453 if (sep == NULL)
454 putc('\a');
455 *np = n;
456 *colp = col;
457 } else {
458 print_argv(NULL, " ", " ", 78, cmdv);
459
460 puts(prompt);
461 puts(buf);
462 }
463 return 1;
464}
465
466#endif
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200467
468#ifdef CMD_DATA_SIZE
469int cmd_get_data_size(char* arg, int default_size)
470{
471 /* Check for a size specification .b, .w or .l.
472 */
473 int len = strlen(arg);
474 if (len > 2 && arg[len-2] == '.') {
Masahiro Yamada616e2162014-10-23 17:27:30 +0900475 switch (arg[len-1]) {
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200476 case 'b':
477 return 1;
478 case 'w':
479 return 2;
480 case 'l':
481 return 4;
482 case 's':
Simon Glass7526dee2020-11-01 14:15:36 -0700483 return CMD_DATA_SIZE_STR;
Simon Glassa33a8242020-06-02 19:26:48 -0600484 case 'q':
485 if (MEM_SUPPORT_64BIT_DATA)
486 return 8;
487 /* no break */
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200488 default:
Simon Glass7526dee2020-11-01 14:15:36 -0700489 return CMD_DATA_SIZE_ERR;
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200490 }
491 }
492 return default_size;
493}
494#endif
Heiko Schocher620f1f62010-09-17 13:10:33 +0200495
Simon Glass09140112020-05-10 11:40:03 -0600496void fixup_cmdtable(struct cmd_tbl *cmdtp, int size)
Heiko Schocher620f1f62010-09-17 13:10:33 +0200497{
498 int i;
499
500 if (gd->reloc_off == 0)
501 return;
502
503 for (i = 0; i < size; i++) {
504 ulong addr;
505
Michal Simek6f94daf2019-10-21 12:54:20 +0200506 addr = (ulong)(cmdtp->cmd_rep) + gd->reloc_off;
507 cmdtp->cmd_rep =
Simon Glass09140112020-05-10 11:40:03 -0600508 (int (*)(struct cmd_tbl *, int, int,
Michal Simek6f94daf2019-10-21 12:54:20 +0200509 char * const [], int *))addr;
510
Masahiro Yamada616e2162014-10-23 17:27:30 +0900511 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
Kun-Hua Huang2e88bb22015-08-24 14:52:35 +0800512#ifdef DEBUG_COMMANDS
Heiko Schocher620f1f62010-09-17 13:10:33 +0200513 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
Masahiro Yamada616e2162014-10-23 17:27:30 +0900514 cmdtp->name, (ulong)(cmdtp->cmd), addr);
Heiko Schocher620f1f62010-09-17 13:10:33 +0200515#endif
Simon Glass09140112020-05-10 11:40:03 -0600516 cmdtp->cmd = (int (*)(struct cmd_tbl *, int, int,
517 char *const []))addr;
Heiko Schocher620f1f62010-09-17 13:10:33 +0200518 addr = (ulong)(cmdtp->name) + gd->reloc_off;
519 cmdtp->name = (char *)addr;
520 if (cmdtp->usage) {
521 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
522 cmdtp->usage = (char *)addr;
523 }
524#ifdef CONFIG_SYS_LONGHELP
525 if (cmdtp->help) {
526 addr = (ulong)(cmdtp->help) + gd->reloc_off;
527 cmdtp->help = (char *)addr;
528 }
529#endif
Daniel Schwierzeckfa28bd22011-10-13 06:07:39 +0000530#ifdef CONFIG_AUTO_COMPLETE
531 if (cmdtp->complete) {
532 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
Daniel Schwierzeck3668d8f2011-10-18 11:12:22 +0000533 cmdtp->complete =
534 (int (*)(int, char * const [], char, int, char * []))addr;
Daniel Schwierzeckfa28bd22011-10-13 06:07:39 +0000535 }
536#endif
Heiko Schocher620f1f62010-09-17 13:10:33 +0200537 cmdtp++;
538 }
539}
Simon Glassbdf8e342012-02-14 19:59:23 +0000540
Simon Glass09140112020-05-10 11:40:03 -0600541int cmd_always_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
542 char *const argv[], int *repeatable)
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100543{
544 *repeatable = 1;
545
546 return cmdtp->cmd(cmdtp, flag, argc, argv);
547}
548
Simon Glass09140112020-05-10 11:40:03 -0600549int cmd_never_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
550 char *const argv[], int *repeatable)
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100551{
552 *repeatable = 0;
553
554 return cmdtp->cmd(cmdtp, flag, argc, argv);
555}
556
Simon Glass09140112020-05-10 11:40:03 -0600557int cmd_discard_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
558 char *const argv[])
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100559{
560 int repeatable;
561
562 return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable);
563}
564
Simon Glassbdf8e342012-02-14 19:59:23 +0000565/**
566 * Call a command function. This should be the only route in U-Boot to call
567 * a command, so that we can track whether we are waiting for input or
568 * executing a command.
569 *
570 * @param cmdtp Pointer to the command to execute
571 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
572 * @param argc Number of arguments (arg 0 must be the command text)
573 * @param argv Arguments
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100574 * @param repeatable Can the command be repeated
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100575 * Return: 0 if command succeeded, else non-zero (CMD_RET_...)
Simon Glassbdf8e342012-02-14 19:59:23 +0000576 */
Simon Glass09140112020-05-10 11:40:03 -0600577static int cmd_call(struct cmd_tbl *cmdtp, int flag, int argc,
578 char *const argv[], int *repeatable)
Simon Glassbdf8e342012-02-14 19:59:23 +0000579{
580 int result;
581
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100582 result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable);
Simon Glassbdf8e342012-02-14 19:59:23 +0000583 if (result)
Peng Fan58b6ad62015-07-28 22:45:37 +0800584 debug("Command failed, result=%d\n", result);
Simon Glassbdf8e342012-02-14 19:59:23 +0000585 return result;
586}
Simon Glass9d12d5d2012-02-14 19:59:25 +0000587
Simon Glass09140112020-05-10 11:40:03 -0600588enum command_ret_t cmd_process(int flag, int argc, char *const argv[],
Richard Genoud34765e82012-12-03 06:28:28 +0000589 int *repeatable, ulong *ticks)
Simon Glass9d12d5d2012-02-14 19:59:25 +0000590{
591 enum command_ret_t rc = CMD_RET_SUCCESS;
Simon Glass09140112020-05-10 11:40:03 -0600592 struct cmd_tbl *cmdtp;
Simon Glass9d12d5d2012-02-14 19:59:25 +0000593
Christoph Muellner7ae31fc2019-04-05 13:03:46 +0200594#if defined(CONFIG_SYS_XTRACE)
595 char *xtrace;
596
597 xtrace = env_get("xtrace");
598 if (xtrace) {
599 puts("+");
600 for (int i = 0; i < argc; i++) {
601 puts(" ");
602 puts(argv[i]);
603 }
604 puts("\n");
605 }
606#endif
607
Simon Glass9d12d5d2012-02-14 19:59:25 +0000608 /* Look up command in command table */
609 cmdtp = find_cmd(argv[0]);
610 if (cmdtp == NULL) {
611 printf("Unknown command '%s' - try 'help'\n", argv[0]);
612 return 1;
613 }
614
615 /* found - check max args */
616 if (argc > cmdtp->maxargs)
617 rc = CMD_RET_USAGE;
618
619#if defined(CONFIG_CMD_BOOTD)
620 /* avoid "bootd" recursion */
621 else if (cmdtp->cmd == do_bootd) {
622 if (flag & CMD_FLAG_BOOTD) {
623 puts("'bootd' recursion detected\n");
624 rc = CMD_RET_FAILURE;
625 } else {
626 flag |= CMD_FLAG_BOOTD;
627 }
628 }
629#endif
630
631 /* If OK so far, then do the command */
632 if (!rc) {
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100633 int newrep;
634
Richard Genoud34765e82012-12-03 06:28:28 +0000635 if (ticks)
636 *ticks = get_timer(0);
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100637 rc = cmd_call(cmdtp, flag, argc, argv, &newrep);
Richard Genoud34765e82012-12-03 06:28:28 +0000638 if (ticks)
639 *ticks = get_timer(*ticks);
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100640 *repeatable &= newrep;
Simon Glass9d12d5d2012-02-14 19:59:25 +0000641 }
642 if (rc == CMD_RET_USAGE)
643 rc = cmd_usage(cmdtp);
644 return rc;
645}
Simon Glass16ff9902014-02-26 15:59:15 -0700646
Simon Glass09140112020-05-10 11:40:03 -0600647int cmd_process_error(struct cmd_tbl *cmdtp, int err)
Simon Glass16ff9902014-02-26 15:59:15 -0700648{
Michal Simek27eb7bc2018-06-21 14:49:26 +0200649 if (err == CMD_RET_USAGE)
650 return CMD_RET_USAGE;
651
Simon Glass16ff9902014-02-26 15:59:15 -0700652 if (err) {
653 printf("Command '%s' failed: Error %d\n", cmdtp->name, err);
Michal Simek37233242018-06-04 13:29:49 +0200654 return CMD_RET_FAILURE;
Simon Glass16ff9902014-02-26 15:59:15 -0700655 }
656
Michal Simek37233242018-06-04 13:29:49 +0200657 return CMD_RET_SUCCESS;
Simon Glass16ff9902014-02-26 15:59:15 -0700658}
Simon Glass30f33332023-01-06 08:52:28 -0600659
660int cmd_source_script(ulong addr, const char *fit_uname, const char *confname)
661{
662 char *data;
663 void *buf;
664 uint len;
665 int ret;
666
667 buf = map_sysmem(addr, 0);
668 ret = image_locate_script(buf, 0, fit_uname, confname, &data, &len);
669 unmap_sysmem(buf);
670 if (ret)
671 return CMD_RET_FAILURE;
672
673 debug("** Script length: %d\n", len);
674 return run_command_list(data, len, 0);
675}