blob: f51df26bf9fbde273211f645ed7a8aead7045dae [file] [log] [blame]
wdenk5dfa25f2002-10-19 19:42:10 +00001/*
Detlev Zundel2dce5512009-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 Hobbs4d91a6e2011-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 Zundel2dce5512009-03-25 17:27:52 +010036
37int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
Wolfgang Denk54841ab2010-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
Kim Phillips199adb62012-10-29 13:34:32 +000043 if (cmd_items > CONFIG_SYS_MAXARGS) {
44 printf("%s: cmd_items %d exceeds hardcoded limit %d."
45 " Recompile with higher CONFIG_SYS_MAXARGS?\n",
46 __func__, cmd_items, CONFIG_SYS_MAXARGS);
47 return -1;
48 }
49
wdenk8bde7f72003-06-27 21:31:46 +000050 if (argc == 1) { /*show list of commands */
Kim Phillips199adb62012-10-29 13:34:32 +000051 cmd_tbl_t *cmd_array[CONFIG_SYS_MAXARGS];
wdenk9d2b18a2003-06-28 23:11:04 +000052 int i, j, swaps;
wdenk8bde7f72003-06-27 21:31:46 +000053
wdenk9d2b18a2003-06-28 23:11:04 +000054 /* Make array of commands from .uboot_cmd section */
Detlev Zundel2dce5512009-03-25 17:27:52 +010055 cmdtp = cmd_start;
wdenk9d2b18a2003-06-28 23:11:04 +000056 for (i = 0; i < cmd_items; i++) {
57 cmd_array[i] = cmdtp++;
wdenk8bde7f72003-06-27 21:31:46 +000058 }
wdenk8bde7f72003-06-27 21:31:46 +000059
wdenk9d2b18a2003-06-28 23:11:04 +000060 /* Sort command list (trivial bubble sort) */
61 for (i = cmd_items - 1; i > 0; --i) {
62 swaps = 0;
63 for (j = 0; j < i; ++j) {
64 if (strcmp (cmd_array[j]->name,
65 cmd_array[j + 1]->name) > 0) {
66 cmd_tbl_t *tmp;
67 tmp = cmd_array[j];
68 cmd_array[j] = cmd_array[j + 1];
69 cmd_array[j + 1] = tmp;
70 ++swaps;
71 }
wdenk8bde7f72003-06-27 21:31:46 +000072 }
wdenk9d2b18a2003-06-28 23:11:04 +000073 if (!swaps)
74 break;
wdenk8bde7f72003-06-27 21:31:46 +000075 }
76
77 /* print short help (usage) */
wdenk9d2b18a2003-06-28 23:11:04 +000078 for (i = 0; i < cmd_items; i++) {
79 const char *usage = cmd_array[i]->usage;
80
wdenk5dfa25f2002-10-19 19:42:10 +000081 /* allow user abort */
wdenk8bde7f72003-06-27 21:31:46 +000082 if (ctrlc ())
wdenk5dfa25f2002-10-19 19:42:10 +000083 return 1;
wdenk9d2b18a2003-06-28 23:11:04 +000084 if (usage == NULL)
wdenk5dfa25f2002-10-19 19:42:10 +000085 continue;
Peter Tyser2fb26042009-01-27 18:03:12 -060086 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
87 cmd_array[i]->name, usage);
wdenk5dfa25f2002-10-19 19:42:10 +000088 }
wdenk5dfa25f2002-10-19 19:42:10 +000089 return 0;
90 }
wdenk5dfa25f2002-10-19 19:42:10 +000091 /*
92 * command help (long version)
93 */
wdenk8bde7f72003-06-27 21:31:46 +000094 for (i = 1; i < argc; ++i) {
Detlev Zundel2dce5512009-03-25 17:27:52 +010095 if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
Wolfgang Denk94796d82009-05-24 19:17:29 +020096 rcode |= cmd_usage(cmdtp);
wdenk71f95112003-06-15 22:40:42 +000097 } else {
wdenk5dfa25f2002-10-19 19:42:10 +000098 printf ("Unknown command '%s' - try 'help'"
99 " without arguments for list of all"
wdenk8bde7f72003-06-27 21:31:46 +0000100 " known commands\n\n", argv[i]
101 );
wdenk5dfa25f2002-10-19 19:42:10 +0000102 rcode = 1;
103 }
104 }
105 return rcode;
106}
107
108/***************************************************************************
109 * find command table entry for a command
110 */
Kumar Galab799cb42008-09-23 10:05:02 -0500111cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
wdenk5dfa25f2002-10-19 19:42:10 +0000112{
113 cmd_tbl_t *cmdtp;
Kumar Galab799cb42008-09-23 10:05:02 -0500114 cmd_tbl_t *cmdtp_temp = table; /*Init value */
wdenk9d2b18a2003-06-28 23:11:04 +0000115 const char *p;
116 int len;
117 int n_found = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000118
Thomas Weber7013c062010-11-24 13:07:51 +0100119 if (!cmd)
120 return NULL;
wdenk9d2b18a2003-06-28 23:11:04 +0000121 /*
122 * Some commands allow length modifiers (like "cp.b");
123 * compare command name only until first dot.
124 */
125 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
126
Kumar Galab799cb42008-09-23 10:05:02 -0500127 for (cmdtp = table;
128 cmdtp != table + table_len;
wdenk9d2b18a2003-06-28 23:11:04 +0000129 cmdtp++) {
130 if (strncmp (cmd, cmdtp->name, len) == 0) {
131 if (len == strlen (cmdtp->name))
132 return cmdtp; /* full match */
133
134 cmdtp_temp = cmdtp; /* abbreviated command ? */
135 n_found++;
136 }
wdenk5dfa25f2002-10-19 19:42:10 +0000137 }
wdenk9d2b18a2003-06-28 23:11:04 +0000138 if (n_found == 1) { /* exactly one match */
wdenk8bde7f72003-06-27 21:31:46 +0000139 return cmdtp_temp;
wdenk9d2b18a2003-06-28 23:11:04 +0000140 }
wdenk8bde7f72003-06-27 21:31:46 +0000141
wdenk9d2b18a2003-06-28 23:11:04 +0000142 return NULL; /* not found or ambiguous command */
wdenk5dfa25f2002-10-19 19:42:10 +0000143}
wdenk04a85b32004-04-15 18:22:41 +0000144
Kumar Galab799cb42008-09-23 10:05:02 -0500145cmd_tbl_t *find_cmd (const char *cmd)
146{
Marek Vasut6c7c9462012-10-12 10:27:04 +0000147 cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd);
148 const int len = ll_entry_count(cmd_tbl_t, cmd);
149 return find_cmd_tbl(cmd, start, len);
Kumar Galab799cb42008-09-23 10:05:02 -0500150}
151
Mike Frysingere84ffdd2011-04-23 23:43:24 +0000152int cmd_usage(const cmd_tbl_t *cmdtp)
Peter Tyser62c3ae72009-01-27 18:03:10 -0600153{
Wolfgang Denk94796d82009-05-24 19:17:29 +0200154 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
155
156#ifdef CONFIG_SYS_LONGHELP
157 printf("Usage:\n%s ", cmdtp->name);
158
159 if (!cmdtp->help) {
160 puts ("- No additional help available.\n");
161 return 1;
162 }
163
164 puts (cmdtp->help);
165 putc ('\n');
166#endif /* CONFIG_SYS_LONGHELP */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200167 return 1;
Peter Tyser62c3ae72009-01-27 18:03:10 -0600168}
169
wdenk04a85b32004-04-15 18:22:41 +0000170#ifdef CONFIG_AUTO_COMPLETE
171
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200172int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
wdenk04a85b32004-04-15 18:22:41 +0000173{
174 static char tmp_buf[512];
175 int space;
176
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000177 space = last_char == '\0' || isblank(last_char);
wdenk04a85b32004-04-15 18:22:41 +0000178
179 if (space && argc == 1)
180 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
181
182 if (!space && argc == 2)
183 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
Mike Frysinger560d4242010-12-17 16:51:59 -0500184
wdenk04a85b32004-04-15 18:22:41 +0000185 return 0;
186}
187
wdenk04a85b32004-04-15 18:22:41 +0000188/*************************************************************************************/
189
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200190static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
wdenk04a85b32004-04-15 18:22:41 +0000191{
Marek Vasut6c7c9462012-10-12 10:27:04 +0000192 cmd_tbl_t *cmdtp = ll_entry_start(cmd_tbl_t, cmd);
193 const int count = ll_entry_count(cmd_tbl_t, cmd);
194 const cmd_tbl_t *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) {
Marek Vasut6c7c9462012-10-12 10:27:04 +0000210 cmdv[n_found] = "...";
wdenk04a85b32004-04-15 18:22:41 +0000211 break;
212 }
Marek Vasut6c7c9462012-10-12 10:27:04 +0000213 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 */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000220 if (argc > 1 || (last_char == '\0' || isblank(last_char))) {
wdenk04a85b32004-04-15 18:22:41 +0000221 cmdtp = find_cmd(argv[0]);
222 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;
261}
262
263static int make_argv(char *s, int argvsz, char *argv[])
264{
265 int argc = 0;
266
267 /* split into argv */
268 while (argc < argvsz - 1) {
269
270 /* skip any white space */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000271 while (isblank(*s))
wdenk04a85b32004-04-15 18:22:41 +0000272 ++s;
273
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200274 if (*s == '\0') /* end of s, no more args */
wdenk04a85b32004-04-15 18:22:41 +0000275 break;
276
277 argv[argc++] = s; /* begin of argument string */
278
279 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000280 while (*s && !isblank(*s))
wdenk04a85b32004-04-15 18:22:41 +0000281 ++s;
282
283 if (*s == '\0') /* end of s, no more args */
284 break;
285
286 *s++ = '\0'; /* terminate current arg */
287 }
288 argv[argc] = NULL;
289
290 return argc;
291}
292
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200293static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
wdenk04a85b32004-04-15 18:22:41 +0000294{
295 int ll = leader != NULL ? strlen(leader) : 0;
296 int sl = sep != NULL ? strlen(sep) : 0;
297 int len, i;
298
299 if (banner) {
300 puts("\n");
301 puts(banner);
302 }
303
304 i = linemax; /* force leader and newline */
305 while (*argv != NULL) {
306 len = strlen(*argv) + sl;
307 if (i + len >= linemax) {
308 puts("\n");
309 if (leader)
310 puts(leader);
311 i = ll - sl;
312 } else if (sep)
313 puts(sep);
314 puts(*argv++);
315 i += len;
316 }
317 printf("\n");
318}
319
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200320static int find_common_prefix(char * const argv[])
wdenk04a85b32004-04-15 18:22:41 +0000321{
322 int i, len;
323 char *anchor, *s, *t;
324
325 if (*argv == NULL)
326 return 0;
327
328 /* begin with max */
329 anchor = *argv++;
330 len = strlen(anchor);
331 while ((t = *argv++) != NULL) {
332 s = anchor;
333 for (i = 0; i < len; i++, t++, s++) {
334 if (*t != *s)
335 break;
336 }
337 len = s - anchor;
338 }
339 return len;
340}
341
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200342static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
wdenk04a85b32004-04-15 18:22:41 +0000343
344int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
345{
346 int n = *np, col = *colp;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200347 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenk04a85b32004-04-15 18:22:41 +0000348 char *cmdv[20];
349 char *s, *t;
350 const char *sep;
351 int i, j, k, len, seplen, argc;
352 int cnt;
353 char last_char;
354
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200355 if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
wdenk04a85b32004-04-15 18:22:41 +0000356 return 0; /* not in normal console */
357
358 cnt = strlen(buf);
359 if (cnt >= 1)
360 last_char = buf[cnt - 1];
361 else
362 last_char = '\0';
363
364 /* copy to secondary buffer which will be affected */
365 strcpy(tmp_buf, buf);
366
367 /* separate into argv */
368 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
369
370 /* do the completion and return the possible completions */
371 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
372
373 /* no match; bell and out */
374 if (i == 0) {
375 if (argc > 1) /* allow tab for non command */
376 return 0;
377 putc('\a');
378 return 1;
379 }
380
381 s = NULL;
382 len = 0;
383 sep = NULL;
384 seplen = 0;
385 if (i == 1) { /* one match; perfect */
386 k = strlen(argv[argc - 1]);
387 s = cmdv[0] + k;
388 len = strlen(s);
389 sep = " ";
390 seplen = 1;
391 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
392 k = strlen(argv[argc - 1]);
393 j -= k;
394 if (j > 0) {
395 s = cmdv[0] + k;
396 len = j;
397 }
398 }
399
400 if (s != NULL) {
401 k = len + seplen;
402 /* make sure it fits */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200403 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
wdenk04a85b32004-04-15 18:22:41 +0000404 putc('\a');
405 return 1;
406 }
407
408 t = buf + cnt;
409 for (i = 0; i < len; i++)
410 *t++ = *s++;
411 if (sep != NULL)
412 for (i = 0; i < seplen; i++)
413 *t++ = sep[i];
414 *t = '\0';
415 n += k;
416 col += k;
417 puts(t - k);
418 if (sep == NULL)
419 putc('\a');
420 *np = n;
421 *colp = col;
422 } else {
423 print_argv(NULL, " ", " ", 78, cmdv);
424
425 puts(prompt);
426 puts(buf);
427 }
428 return 1;
429}
430
431#endif
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200432
433#ifdef CMD_DATA_SIZE
434int cmd_get_data_size(char* arg, int default_size)
435{
436 /* Check for a size specification .b, .w or .l.
437 */
438 int len = strlen(arg);
439 if (len > 2 && arg[len-2] == '.') {
440 switch(arg[len-1]) {
441 case 'b':
442 return 1;
443 case 'w':
444 return 2;
445 case 'l':
446 return 4;
447 case 's':
448 return -2;
449 default:
450 return -1;
451 }
452 }
453 return default_size;
454}
455#endif
Heiko Schocher620f1f62010-09-17 13:10:33 +0200456
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200457#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher620f1f62010-09-17 13:10:33 +0200458DECLARE_GLOBAL_DATA_PTR;
459
460void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
461{
462 int i;
463
464 if (gd->reloc_off == 0)
465 return;
466
467 for (i = 0; i < size; i++) {
468 ulong addr;
469
470 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
471#if DEBUG_COMMANDS
472 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
473 cmdtp->name, (ulong) (cmdtp->cmd), addr);
474#endif
475 cmdtp->cmd =
476 (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
477 addr = (ulong)(cmdtp->name) + gd->reloc_off;
478 cmdtp->name = (char *)addr;
479 if (cmdtp->usage) {
480 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
481 cmdtp->usage = (char *)addr;
482 }
483#ifdef CONFIG_SYS_LONGHELP
484 if (cmdtp->help) {
485 addr = (ulong)(cmdtp->help) + gd->reloc_off;
486 cmdtp->help = (char *)addr;
487 }
488#endif
Daniel Schwierzeckfa28bd22011-10-13 06:07:39 +0000489#ifdef CONFIG_AUTO_COMPLETE
490 if (cmdtp->complete) {
491 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
Daniel Schwierzeck3668d8f2011-10-18 11:12:22 +0000492 cmdtp->complete =
493 (int (*)(int, char * const [], char, int, char * []))addr;
Daniel Schwierzeckfa28bd22011-10-13 06:07:39 +0000494 }
495#endif
Heiko Schocher620f1f62010-09-17 13:10:33 +0200496 cmdtp++;
497 }
498}
499#endif
Simon Glassbdf8e342012-02-14 19:59:23 +0000500
501/**
502 * Call a command function. This should be the only route in U-Boot to call
503 * a command, so that we can track whether we are waiting for input or
504 * executing a command.
505 *
506 * @param cmdtp Pointer to the command to execute
507 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
508 * @param argc Number of arguments (arg 0 must be the command text)
509 * @param argv Arguments
510 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
511 */
Simon Glass9d12d5d2012-02-14 19:59:25 +0000512static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Simon Glassbdf8e342012-02-14 19:59:23 +0000513{
514 int result;
515
516 result = (cmdtp->cmd)(cmdtp, flag, argc, argv);
517 if (result)
518 debug("Command failed, result=%d", result);
519 return result;
520}
Simon Glass9d12d5d2012-02-14 19:59:25 +0000521
522enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
523 int *repeatable)
524{
525 enum command_ret_t rc = CMD_RET_SUCCESS;
526 cmd_tbl_t *cmdtp;
527
528 /* Look up command in command table */
529 cmdtp = find_cmd(argv[0]);
530 if (cmdtp == NULL) {
531 printf("Unknown command '%s' - try 'help'\n", argv[0]);
532 return 1;
533 }
534
535 /* found - check max args */
536 if (argc > cmdtp->maxargs)
537 rc = CMD_RET_USAGE;
538
539#if defined(CONFIG_CMD_BOOTD)
540 /* avoid "bootd" recursion */
541 else if (cmdtp->cmd == do_bootd) {
542 if (flag & CMD_FLAG_BOOTD) {
543 puts("'bootd' recursion detected\n");
544 rc = CMD_RET_FAILURE;
545 } else {
546 flag |= CMD_FLAG_BOOTD;
547 }
548 }
549#endif
550
551 /* If OK so far, then do the command */
552 if (!rc) {
553 rc = cmd_call(cmdtp, flag, argc, argv);
554 *repeatable &= cmdtp->repeatable;
555 }
556 if (rc == CMD_RET_USAGE)
557 rc = cmd_usage(cmdtp);
558 return rc;
559}