2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 * Command Processor Table
14 #include <linux/ctype.h>
17 * Use puts() instead of printf() to avoid printf buffer overflow
18 * for long help messages
21 int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
22 flag, int argc, char * const argv[])
27 if (argc == 1) { /*show list of commands */
28 cmd_tbl_t *cmd_array[cmd_items];
31 /* Make array of commands from .uboot_cmd section */
33 for (i = 0; i < cmd_items; i++) {
34 cmd_array[i] = cmdtp++;
37 /* Sort command list (trivial bubble sort) */
38 for (i = cmd_items - 1; i > 0; --i) {
40 for (j = 0; j < i; ++j) {
41 if (strcmp (cmd_array[j]->name,
42 cmd_array[j + 1]->name) > 0) {
45 cmd_array[j] = cmd_array[j + 1];
46 cmd_array[j + 1] = tmp;
54 /* print short help (usage) */
55 for (i = 0; i < cmd_items; i++) {
56 const char *usage = cmd_array[i]->usage;
58 /* allow user abort */
63 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
64 cmd_array[i]->name, usage);
69 * command help (long version)
71 for (i = 1; i < argc; ++i) {
72 if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
73 rcode |= cmd_usage(cmdtp);
75 printf ("Unknown command '%s' - try 'help'"
76 " without arguments for list of all"
77 " known commands\n\n", argv[i]
85 /***************************************************************************
86 * find command table entry for a command
88 cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
91 cmd_tbl_t *cmdtp_temp = table; /*Init value */
99 * Some commands allow length modifiers (like "cp.b");
100 * compare command name only until first dot.
102 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
105 cmdtp != table + table_len;
107 if (strncmp (cmd, cmdtp->name, len) == 0) {
108 if (len == strlen (cmdtp->name))
109 return cmdtp; /* full match */
111 cmdtp_temp = cmdtp; /* abbreviated command ? */
115 if (n_found == 1) { /* exactly one match */
119 return NULL; /* not found or ambiguous command */
122 cmd_tbl_t *find_cmd (const char *cmd)
124 cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd);
125 const int len = ll_entry_count(cmd_tbl_t, cmd);
126 return find_cmd_tbl(cmd, start, len);
129 int cmd_usage(const cmd_tbl_t *cmdtp)
131 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
133 #ifdef CONFIG_SYS_LONGHELP
134 printf("Usage:\n%s ", cmdtp->name);
137 puts ("- No additional help available.\n");
143 #endif /* CONFIG_SYS_LONGHELP */
147 #ifdef CONFIG_AUTO_COMPLETE
149 int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
151 static char tmp_buf[512];
154 space = last_char == '\0' || isblank(last_char);
156 if (space && argc == 1)
157 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
159 if (!space && argc == 2)
160 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
165 /*************************************************************************************/
167 static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
169 cmd_tbl_t *cmdtp = ll_entry_start(cmd_tbl_t, cmd);
170 const int count = ll_entry_count(cmd_tbl_t, cmd);
171 const cmd_tbl_t *cmdend = cmdtp + count;
184 /* output full list of commands */
185 for (; cmdtp != cmdend; cmdtp++) {
186 if (n_found >= maxv - 2) {
187 cmdv[n_found++] = "...";
190 cmdv[n_found++] = cmdtp->name;
192 cmdv[n_found] = NULL;
196 /* more than one arg or one but the start of the next */
197 if (argc > 1 || (last_char == '\0' || isblank(last_char))) {
198 cmdtp = find_cmd(argv[0]);
199 if (cmdtp == NULL || cmdtp->complete == NULL) {
203 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
208 * Some commands allow length modifiers (like "cp.b");
209 * compare command name only until first dot.
211 p = strchr(cmd, '.');
217 /* return the partial matches */
218 for (; cmdtp != cmdend; cmdtp++) {
220 clen = strlen(cmdtp->name);
224 if (memcmp(cmd, cmdtp->name, len) != 0)
228 if (n_found >= maxv - 2) {
229 cmdv[n_found++] = "...";
233 cmdv[n_found++] = cmdtp->name;
236 cmdv[n_found] = NULL;
240 static int make_argv(char *s, int argvsz, char *argv[])
244 /* split into argv */
245 while (argc < argvsz - 1) {
247 /* skip any white space */
251 if (*s == '\0') /* end of s, no more args */
254 argv[argc++] = s; /* begin of argument string */
256 /* find end of string */
257 while (*s && !isblank(*s))
260 if (*s == '\0') /* end of s, no more args */
263 *s++ = '\0'; /* terminate current arg */
270 static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
272 int ll = leader != NULL ? strlen(leader) : 0;
273 int sl = sep != NULL ? strlen(sep) : 0;
281 i = linemax; /* force leader and newline */
282 while (*argv != NULL) {
283 len = strlen(*argv) + sl;
284 if (i + len >= linemax) {
297 static int find_common_prefix(char * const argv[])
300 char *anchor, *s, *t;
307 len = strlen(anchor);
308 while ((t = *argv++) != NULL) {
310 for (i = 0; i < len; i++, t++, s++) {
319 static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
321 int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
323 int n = *np, col = *colp;
324 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
328 int i, j, k, len, seplen, argc;
332 if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
333 return 0; /* not in normal console */
337 last_char = buf[cnt - 1];
341 /* copy to secondary buffer which will be affected */
342 strcpy(tmp_buf, buf);
344 /* separate into argv */
345 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
347 /* do the completion and return the possible completions */
348 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
350 /* no match; bell and out */
352 if (argc > 1) /* allow tab for non command */
362 if (i == 1) { /* one match; perfect */
363 k = strlen(argv[argc - 1]);
368 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
369 k = strlen(argv[argc - 1]);
379 /* make sure it fits */
380 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
386 for (i = 0; i < len; i++)
389 for (i = 0; i < seplen; i++)
400 print_argv(NULL, " ", " ", 78, cmdv);
411 int cmd_get_data_size(char* arg, int default_size)
413 /* Check for a size specification .b, .w or .l.
415 int len = strlen(arg);
416 if (len > 2 && arg[len-2] == '.') {
424 #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
438 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
439 DECLARE_GLOBAL_DATA_PTR;
441 void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
445 if (gd->reloc_off == 0)
448 for (i = 0; i < size; i++) {
451 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
453 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
454 cmdtp->name, (ulong) (cmdtp->cmd), addr);
457 (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
458 addr = (ulong)(cmdtp->name) + gd->reloc_off;
459 cmdtp->name = (char *)addr;
461 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
462 cmdtp->usage = (char *)addr;
464 #ifdef CONFIG_SYS_LONGHELP
466 addr = (ulong)(cmdtp->help) + gd->reloc_off;
467 cmdtp->help = (char *)addr;
470 #ifdef CONFIG_AUTO_COMPLETE
471 if (cmdtp->complete) {
472 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
474 (int (*)(int, char * const [], char, int, char * []))addr;
483 * Call a command function. This should be the only route in U-Boot to call
484 * a command, so that we can track whether we are waiting for input or
485 * executing a command.
487 * @param cmdtp Pointer to the command to execute
488 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
489 * @param argc Number of arguments (arg 0 must be the command text)
490 * @param argv Arguments
491 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
493 static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
497 result = (cmdtp->cmd)(cmdtp, flag, argc, argv);
499 debug("Command failed, result=%d", result);
503 enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
504 int *repeatable, ulong *ticks)
506 enum command_ret_t rc = CMD_RET_SUCCESS;
509 /* Look up command in command table */
510 cmdtp = find_cmd(argv[0]);
512 printf("Unknown command '%s' - try 'help'\n", argv[0]);
516 /* found - check max args */
517 if (argc > cmdtp->maxargs)
520 #if defined(CONFIG_CMD_BOOTD)
521 /* avoid "bootd" recursion */
522 else if (cmdtp->cmd == do_bootd) {
523 if (flag & CMD_FLAG_BOOTD) {
524 puts("'bootd' recursion detected\n");
525 rc = CMD_RET_FAILURE;
527 flag |= CMD_FLAG_BOOTD;
532 /* If OK so far, then do the command */
535 *ticks = get_timer(0);
536 rc = cmd_call(cmdtp, flag, argc, argv);
538 *ticks = get_timer(*ticks);
539 *repeatable &= cmdtp->repeatable;
541 if (rc == CMD_RET_USAGE)
542 rc = cmd_usage(cmdtp);
546 int cmd_process_error(cmd_tbl_t *cmdtp, int err)
549 printf("Command '%s' failed: Error %d\n", cmdtp->name, err);