blob: 50c84292c166ef1fa5487997ce04298ae4fe834e [file] [log] [blame]
wdenk5dfa25f2002-10-19 19:42:10 +00001/*
Detlev Zundel5e6c06b2009-03-25 17:27:52 +01002 * (C) Copyright 2000-2009
wdenk5dfa25f2002-10-19 19:42:10 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * Command Processor Table
26 */
27
28#include <common.h>
29#include <command.h>
Jason Hobbscafa1aa2011-08-23 11:06:54 +000030#include <linux/ctype.h>
wdenk5dfa25f2002-10-19 19:42:10 +000031
wdenk5dfa25f2002-10-19 19:42:10 +000032/*
33 * Use puts() instead of printf() to avoid printf buffer overflow
34 * for long help messages
35 */
Detlev Zundel5e6c06b2009-03-25 17:27:52 +010036
37int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
Wolfgang Denk6262d0212010-06-28 22:00:46 +020038 flag, int argc, char * const argv[])
wdenk5dfa25f2002-10-19 19:42:10 +000039{
40 int i;
41 int rcode = 0;
42
wdenk57b2d802003-06-27 21:31:46 +000043 if (argc == 1) { /*show list of commands */
Anatolij Gustschin9ac7cc22012-11-08 12:40:17 +010044 cmd_tbl_t *cmd_array[cmd_items];
wdenk3086a972003-06-28 23:11:04 +000045 int i, j, swaps;
wdenk57b2d802003-06-27 21:31:46 +000046
wdenk3086a972003-06-28 23:11:04 +000047 /* Make array of commands from .uboot_cmd section */
Detlev Zundel5e6c06b2009-03-25 17:27:52 +010048 cmdtp = cmd_start;
wdenk3086a972003-06-28 23:11:04 +000049 for (i = 0; i < cmd_items; i++) {
50 cmd_array[i] = cmdtp++;
wdenk57b2d802003-06-27 21:31:46 +000051 }
wdenk57b2d802003-06-27 21:31:46 +000052
wdenk3086a972003-06-28 23:11:04 +000053 /* Sort command list (trivial bubble sort) */
54 for (i = cmd_items - 1; i > 0; --i) {
55 swaps = 0;
56 for (j = 0; j < i; ++j) {
57 if (strcmp (cmd_array[j]->name,
58 cmd_array[j + 1]->name) > 0) {
59 cmd_tbl_t *tmp;
60 tmp = cmd_array[j];
61 cmd_array[j] = cmd_array[j + 1];
62 cmd_array[j + 1] = tmp;
63 ++swaps;
64 }
wdenk57b2d802003-06-27 21:31:46 +000065 }
wdenk3086a972003-06-28 23:11:04 +000066 if (!swaps)
67 break;
wdenk57b2d802003-06-27 21:31:46 +000068 }
69
70 /* print short help (usage) */
wdenk3086a972003-06-28 23:11:04 +000071 for (i = 0; i < cmd_items; i++) {
72 const char *usage = cmd_array[i]->usage;
73
wdenk5dfa25f2002-10-19 19:42:10 +000074 /* allow user abort */
wdenk57b2d802003-06-27 21:31:46 +000075 if (ctrlc ())
wdenk5dfa25f2002-10-19 19:42:10 +000076 return 1;
wdenk3086a972003-06-28 23:11:04 +000077 if (usage == NULL)
wdenk5dfa25f2002-10-19 19:42:10 +000078 continue;
Peter Tyserdfb72b82009-01-27 18:03:12 -060079 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
80 cmd_array[i]->name, usage);
wdenk5dfa25f2002-10-19 19:42:10 +000081 }
wdenk5dfa25f2002-10-19 19:42:10 +000082 return 0;
83 }
wdenk5dfa25f2002-10-19 19:42:10 +000084 /*
85 * command help (long version)
86 */
wdenk57b2d802003-06-27 21:31:46 +000087 for (i = 1; i < argc; ++i) {
Detlev Zundel5e6c06b2009-03-25 17:27:52 +010088 if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
Wolfgang Denk9dd775b2009-05-24 19:17:29 +020089 rcode |= cmd_usage(cmdtp);
wdenk7a428cc2003-06-15 22:40:42 +000090 } else {
wdenk5dfa25f2002-10-19 19:42:10 +000091 printf ("Unknown command '%s' - try 'help'"
92 " without arguments for list of all"
wdenk57b2d802003-06-27 21:31:46 +000093 " known commands\n\n", argv[i]
94 );
wdenk5dfa25f2002-10-19 19:42:10 +000095 rcode = 1;
96 }
97 }
98 return rcode;
99}
100
101/***************************************************************************
102 * find command table entry for a command
103 */
Kumar Galaac2dad62008-09-23 10:05:02 -0500104cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
wdenk5dfa25f2002-10-19 19:42:10 +0000105{
106 cmd_tbl_t *cmdtp;
Kumar Galaac2dad62008-09-23 10:05:02 -0500107 cmd_tbl_t *cmdtp_temp = table; /*Init value */
wdenk3086a972003-06-28 23:11:04 +0000108 const char *p;
109 int len;
110 int n_found = 0;
wdenk57b2d802003-06-27 21:31:46 +0000111
Thomas Weber97ea46f2010-11-24 13:07:51 +0100112 if (!cmd)
113 return NULL;
wdenk3086a972003-06-28 23:11:04 +0000114 /*
115 * Some commands allow length modifiers (like "cp.b");
116 * compare command name only until first dot.
117 */
118 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
119
Kumar Galaac2dad62008-09-23 10:05:02 -0500120 for (cmdtp = table;
121 cmdtp != table + table_len;
wdenk3086a972003-06-28 23:11:04 +0000122 cmdtp++) {
123 if (strncmp (cmd, cmdtp->name, len) == 0) {
124 if (len == strlen (cmdtp->name))
125 return cmdtp; /* full match */
126
127 cmdtp_temp = cmdtp; /* abbreviated command ? */
128 n_found++;
129 }
wdenk5dfa25f2002-10-19 19:42:10 +0000130 }
wdenk3086a972003-06-28 23:11:04 +0000131 if (n_found == 1) { /* exactly one match */
wdenk57b2d802003-06-27 21:31:46 +0000132 return cmdtp_temp;
wdenk3086a972003-06-28 23:11:04 +0000133 }
wdenk5dfa25f2002-10-19 19:42:10 +0000134
wdenk3086a972003-06-28 23:11:04 +0000135 return NULL; /* not found or ambiguous command */
wdenk57b2d802003-06-27 21:31:46 +0000136}
wdenk3902d702004-04-15 18:22:41 +0000137
Kumar Galaac2dad62008-09-23 10:05:02 -0500138cmd_tbl_t *find_cmd (const char *cmd)
139{
Marek Vasut69c5e342012-10-12 10:27:04 +0000140 cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd);
141 const int len = ll_entry_count(cmd_tbl_t, cmd);
142 return find_cmd_tbl(cmd, start, len);
Kumar Galaac2dad62008-09-23 10:05:02 -0500143}
144
Mike Frysinger33e84d12011-04-23 23:43:24 +0000145int cmd_usage(const cmd_tbl_t *cmdtp)
Peter Tyserddb3af92009-01-27 18:03:10 -0600146{
Wolfgang Denk9dd775b2009-05-24 19:17:29 +0200147 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
148
149#ifdef CONFIG_SYS_LONGHELP
150 printf("Usage:\n%s ", cmdtp->name);
151
152 if (!cmdtp->help) {
153 puts ("- No additional help available.\n");
154 return 1;
155 }
156
157 puts (cmdtp->help);
158 putc ('\n');
159#endif /* CONFIG_SYS_LONGHELP */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200160 return 1;
Peter Tyserddb3af92009-01-27 18:03:10 -0600161}
162
wdenk3902d702004-04-15 18:22:41 +0000163#ifdef CONFIG_AUTO_COMPLETE
164
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200165int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
wdenk3902d702004-04-15 18:22:41 +0000166{
167 static char tmp_buf[512];
168 int space;
169
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000170 space = last_char == '\0' || isblank(last_char);
wdenk3902d702004-04-15 18:22:41 +0000171
172 if (space && argc == 1)
173 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
174
175 if (!space && argc == 2)
176 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
Mike Frysinger8d882322010-12-17 16:51:59 -0500177
wdenk3902d702004-04-15 18:22:41 +0000178 return 0;
179}
180
wdenk3902d702004-04-15 18:22:41 +0000181/*************************************************************************************/
182
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200183static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
wdenk3902d702004-04-15 18:22:41 +0000184{
Marek Vasut69c5e342012-10-12 10:27:04 +0000185 cmd_tbl_t *cmdtp = ll_entry_start(cmd_tbl_t, cmd);
186 const int count = ll_entry_count(cmd_tbl_t, cmd);
187 const cmd_tbl_t *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) {
Marek Vasut69c5e342012-10-12 10:27:04 +0000203 cmdv[n_found] = "...";
wdenk3902d702004-04-15 18:22:41 +0000204 break;
205 }
Marek Vasut69c5e342012-10-12 10:27:04 +0000206 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 */
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000213 if (argc > 1 || (last_char == '\0' || isblank(last_char))) {
wdenk3902d702004-04-15 18:22:41 +0000214 cmdtp = find_cmd(argv[0]);
215 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;
254}
255
256static int make_argv(char *s, int argvsz, char *argv[])
257{
258 int argc = 0;
259
260 /* split into argv */
261 while (argc < argvsz - 1) {
262
263 /* skip any white space */
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000264 while (isblank(*s))
wdenk3902d702004-04-15 18:22:41 +0000265 ++s;
266
Wolfgang Denka1be4762008-05-20 16:00:29 +0200267 if (*s == '\0') /* end of s, no more args */
wdenk3902d702004-04-15 18:22:41 +0000268 break;
269
270 argv[argc++] = s; /* begin of argument string */
271
272 /* find end of string */
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000273 while (*s && !isblank(*s))
wdenk3902d702004-04-15 18:22:41 +0000274 ++s;
275
276 if (*s == '\0') /* end of s, no more args */
277 break;
278
279 *s++ = '\0'; /* terminate current arg */
280 }
281 argv[argc] = NULL;
282
283 return argc;
284}
285
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200286static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
wdenk3902d702004-04-15 18:22:41 +0000287{
288 int ll = leader != NULL ? strlen(leader) : 0;
289 int sl = sep != NULL ? strlen(sep) : 0;
290 int len, i;
291
292 if (banner) {
293 puts("\n");
294 puts(banner);
295 }
296
297 i = linemax; /* force leader and newline */
298 while (*argv != NULL) {
299 len = strlen(*argv) + sl;
300 if (i + len >= linemax) {
301 puts("\n");
302 if (leader)
303 puts(leader);
304 i = ll - sl;
305 } else if (sep)
306 puts(sep);
307 puts(*argv++);
308 i += len;
309 }
310 printf("\n");
311}
312
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200313static int find_common_prefix(char * const argv[])
wdenk3902d702004-04-15 18:22:41 +0000314{
315 int i, len;
316 char *anchor, *s, *t;
317
318 if (*argv == NULL)
319 return 0;
320
321 /* begin with max */
322 anchor = *argv++;
323 len = strlen(anchor);
324 while ((t = *argv++) != NULL) {
325 s = anchor;
326 for (i = 0; i < len; i++, t++, s++) {
327 if (*t != *s)
328 break;
329 }
330 len = s - anchor;
331 }
332 return len;
333}
334
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200335static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
wdenk3902d702004-04-15 18:22:41 +0000336
337int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
338{
339 int n = *np, col = *colp;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200340 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenk3902d702004-04-15 18:22:41 +0000341 char *cmdv[20];
342 char *s, *t;
343 const char *sep;
344 int i, j, k, len, seplen, argc;
345 int cnt;
346 char last_char;
347
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200348 if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
wdenk3902d702004-04-15 18:22:41 +0000349 return 0; /* not in normal console */
350
351 cnt = strlen(buf);
352 if (cnt >= 1)
353 last_char = buf[cnt - 1];
354 else
355 last_char = '\0';
356
357 /* copy to secondary buffer which will be affected */
358 strcpy(tmp_buf, buf);
359
360 /* separate into argv */
361 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
362
363 /* do the completion and return the possible completions */
364 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
365
366 /* no match; bell and out */
367 if (i == 0) {
368 if (argc > 1) /* allow tab for non command */
369 return 0;
370 putc('\a');
371 return 1;
372 }
373
374 s = NULL;
375 len = 0;
376 sep = NULL;
377 seplen = 0;
378 if (i == 1) { /* one match; perfect */
379 k = strlen(argv[argc - 1]);
380 s = cmdv[0] + k;
381 len = strlen(s);
382 sep = " ";
383 seplen = 1;
384 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
385 k = strlen(argv[argc - 1]);
386 j -= k;
387 if (j > 0) {
388 s = cmdv[0] + k;
389 len = j;
390 }
391 }
392
393 if (s != NULL) {
394 k = len + seplen;
395 /* make sure it fits */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200396 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
wdenk3902d702004-04-15 18:22:41 +0000397 putc('\a');
398 return 1;
399 }
400
401 t = buf + cnt;
402 for (i = 0; i < len; i++)
403 *t++ = *s++;
404 if (sep != NULL)
405 for (i = 0; i < seplen; i++)
406 *t++ = sep[i];
407 *t = '\0';
408 n += k;
409 col += k;
410 puts(t - k);
411 if (sep == NULL)
412 putc('\a');
413 *np = n;
414 *colp = col;
415 } else {
416 print_argv(NULL, " ", " ", 78, cmdv);
417
418 puts(prompt);
419 puts(buf);
420 }
421 return 1;
422}
423
424#endif
Jean-Christophe PLAGNIOL-VILLARD82290f22008-09-10 22:48:05 +0200425
426#ifdef CMD_DATA_SIZE
427int cmd_get_data_size(char* arg, int default_size)
428{
429 /* Check for a size specification .b, .w or .l.
430 */
431 int len = strlen(arg);
432 if (len > 2 && arg[len-2] == '.') {
433 switch(arg[len-1]) {
434 case 'b':
435 return 1;
436 case 'w':
437 return 2;
438 case 'l':
439 return 4;
440 case 's':
441 return -2;
442 default:
443 return -1;
444 }
445 }
446 return default_size;
447}
448#endif
Heiko Schocher092cf162010-09-17 13:10:33 +0200449
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200450#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher092cf162010-09-17 13:10:33 +0200451DECLARE_GLOBAL_DATA_PTR;
452
453void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
454{
455 int i;
456
457 if (gd->reloc_off == 0)
458 return;
459
460 for (i = 0; i < size; i++) {
461 ulong addr;
462
463 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
464#if DEBUG_COMMANDS
465 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
466 cmdtp->name, (ulong) (cmdtp->cmd), addr);
467#endif
468 cmdtp->cmd =
469 (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
470 addr = (ulong)(cmdtp->name) + gd->reloc_off;
471 cmdtp->name = (char *)addr;
472 if (cmdtp->usage) {
473 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
474 cmdtp->usage = (char *)addr;
475 }
476#ifdef CONFIG_SYS_LONGHELP
477 if (cmdtp->help) {
478 addr = (ulong)(cmdtp->help) + gd->reloc_off;
479 cmdtp->help = (char *)addr;
480 }
481#endif
Daniel Schwierzecke5166222011-10-13 06:07:39 +0000482#ifdef CONFIG_AUTO_COMPLETE
483 if (cmdtp->complete) {
484 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
Daniel Schwierzeck76deb6c2011-10-18 11:12:22 +0000485 cmdtp->complete =
486 (int (*)(int, char * const [], char, int, char * []))addr;
Daniel Schwierzecke5166222011-10-13 06:07:39 +0000487 }
488#endif
Heiko Schocher092cf162010-09-17 13:10:33 +0200489 cmdtp++;
490 }
491}
492#endif
Simon Glass2176d602012-02-14 19:59:23 +0000493
494/**
495 * Call a command function. This should be the only route in U-Boot to call
496 * a command, so that we can track whether we are waiting for input or
497 * executing a command.
498 *
499 * @param cmdtp Pointer to the command to execute
500 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
501 * @param argc Number of arguments (arg 0 must be the command text)
502 * @param argv Arguments
503 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
504 */
Simon Glass1727b942012-02-14 19:59:25 +0000505static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Simon Glass2176d602012-02-14 19:59:23 +0000506{
507 int result;
508
509 result = (cmdtp->cmd)(cmdtp, flag, argc, argv);
510 if (result)
511 debug("Command failed, result=%d", result);
512 return result;
513}
Simon Glass1727b942012-02-14 19:59:25 +0000514
515enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
516 int *repeatable)
517{
518 enum command_ret_t rc = CMD_RET_SUCCESS;
519 cmd_tbl_t *cmdtp;
520
521 /* Look up command in command table */
522 cmdtp = find_cmd(argv[0]);
523 if (cmdtp == NULL) {
524 printf("Unknown command '%s' - try 'help'\n", argv[0]);
525 return 1;
526 }
527
528 /* found - check max args */
529 if (argc > cmdtp->maxargs)
530 rc = CMD_RET_USAGE;
531
532#if defined(CONFIG_CMD_BOOTD)
533 /* avoid "bootd" recursion */
534 else if (cmdtp->cmd == do_bootd) {
535 if (flag & CMD_FLAG_BOOTD) {
536 puts("'bootd' recursion detected\n");
537 rc = CMD_RET_FAILURE;
538 } else {
539 flag |= CMD_FLAG_BOOTD;
540 }
541 }
542#endif
543
544 /* If OK so far, then do the command */
545 if (!rc) {
546 rc = cmd_call(cmdtp, flag, argc, argv);
547 *repeatable &= cmdtp->repeatable;
548 }
549 if (rc == CMD_RET_USAGE)
550 rc = cmd_usage(cmdtp);
551 return rc;
552}