1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * Command Processor Table
17 #include <linux/ctype.h>
20 * Use puts() instead of printf() to avoid printf buffer overflow
21 * for long help messages
24 int _do_help(struct cmd_tbl *cmd_start, int cmd_items, struct cmd_tbl *cmdtp,
25 int flag, int argc, char *const argv[])
30 if (argc == 1) { /* show list of commands */
31 struct cmd_tbl *cmd_array[cmd_items];
34 /* Make array of commands from .uboot_cmd section */
36 for (i = 0; i < cmd_items; i++) {
37 cmd_array[i] = cmdtp++;
40 /* Sort command list (trivial bubble sort) */
41 for (i = cmd_items - 1; i > 0; --i) {
43 for (j = 0; j < i; ++j) {
44 if (strcmp(cmd_array[j]->name,
45 cmd_array[j + 1]->name) > 0) {
48 cmd_array[j] = cmd_array[j + 1];
49 cmd_array[j + 1] = tmp;
57 /* print short help (usage) */
58 for (i = 0; i < cmd_items; i++) {
59 const char *usage = cmd_array[i]->usage;
61 /* allow user abort */
66 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
67 cmd_array[i]->name, usage);
72 * command help (long version)
74 for (i = 1; i < argc; ++i) {
75 cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items);
77 rcode |= cmd_usage(cmdtp);
79 printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n",
87 /* find command table entry for a command */
88 struct cmd_tbl *find_cmd_tbl(const char *cmd, struct cmd_tbl *table,
92 struct cmd_tbl *cmdtp;
93 struct cmd_tbl *cmdtp_temp = table; /* Init value */
101 * Some commands allow length modifiers (like "cp.b");
102 * compare command name only until first dot.
104 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
106 for (cmdtp = table; cmdtp != table + table_len; cmdtp++) {
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 */
118 #endif /* CONFIG_CMDLINE */
120 return NULL; /* not found or ambiguous command */
123 struct cmd_tbl *find_cmd(const char *cmd)
125 struct cmd_tbl *start = ll_entry_start(struct cmd_tbl, cmd);
126 const int len = ll_entry_count(struct cmd_tbl, cmd);
127 return find_cmd_tbl(cmd, start, len);
130 int cmd_usage(const struct cmd_tbl *cmdtp)
132 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
134 #ifdef CONFIG_SYS_LONGHELP
135 printf("Usage:\n%s ", cmdtp->name);
138 puts ("- No additional help available.\n");
144 #endif /* CONFIG_SYS_LONGHELP */
148 #ifdef CONFIG_AUTO_COMPLETE
149 static char env_complete_buf[512];
151 int var_complete(int argc, char *const argv[], char last_char, int maxv,
156 space = last_char == '\0' || isblank(last_char);
158 if (space && argc == 1)
159 return env_complete("", maxv, cmdv, sizeof(env_complete_buf),
160 env_complete_buf, false);
162 if (!space && argc == 2)
163 return env_complete(argv[1], maxv, cmdv,
164 sizeof(env_complete_buf),
165 env_complete_buf, false);
170 static int dollar_complete(int argc, char *const argv[], char last_char,
171 int maxv, char *cmdv[])
173 /* Make sure the last argument starts with a $. */
174 if (argc < 1 || argv[argc - 1][0] != '$' ||
175 last_char == '\0' || isblank(last_char))
178 return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf),
179 env_complete_buf, true);
182 /*************************************************************************************/
184 int complete_subcmdv(struct cmd_tbl *cmdtp, int count, int argc,
185 char *const argv[], char last_char,
186 int maxv, char *cmdv[])
188 #ifdef CONFIG_CMDLINE
189 const struct cmd_tbl *cmdend = cmdtp + count;
202 /* output full list of commands */
203 for (; cmdtp != cmdend; cmdtp++) {
204 if (n_found >= maxv - 2) {
205 cmdv[n_found++] = "...";
208 cmdv[n_found++] = cmdtp->name;
210 cmdv[n_found] = NULL;
214 /* more than one arg or one but the start of the next */
215 if (argc > 1 || last_char == '\0' || isblank(last_char)) {
216 cmdtp = find_cmd_tbl(argv[0], cmdtp, count);
217 if (cmdtp == NULL || cmdtp->complete == NULL) {
221 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
226 * Some commands allow length modifiers (like "cp.b");
227 * compare command name only until first dot.
229 p = strchr(cmd, '.');
235 /* return the partial matches */
236 for (; cmdtp != cmdend; cmdtp++) {
238 clen = strlen(cmdtp->name);
242 if (memcmp(cmd, cmdtp->name, len) != 0)
246 if (n_found >= maxv - 2) {
247 cmdv[n_found++] = "...";
251 cmdv[n_found++] = cmdtp->name;
254 cmdv[n_found] = NULL;
261 static int complete_cmdv(int argc, char *const argv[], char last_char,
262 int maxv, char *cmdv[])
264 #ifdef CONFIG_CMDLINE
265 return complete_subcmdv(ll_entry_start(struct cmd_tbl, cmd),
266 ll_entry_count(struct cmd_tbl, cmd), argc, argv,
267 last_char, maxv, cmdv);
273 static int make_argv(char *s, int argvsz, char *argv[])
277 /* split into argv */
278 while (argc < argvsz - 1) {
280 /* skip any white space */
284 if (*s == '\0') /* end of s, no more args */
287 argv[argc++] = s; /* begin of argument string */
289 /* find end of string */
290 while (*s && !isblank(*s))
293 if (*s == '\0') /* end of s, no more args */
296 *s++ = '\0'; /* terminate current arg */
303 static void print_argv(const char *banner, const char *leader, const char *sep,
304 int linemax, char *const argv[])
306 int ll = leader != NULL ? strlen(leader) : 0;
307 int sl = sep != NULL ? strlen(sep) : 0;
315 i = linemax; /* force leader and newline */
316 while (*argv != NULL) {
317 len = strlen(*argv) + sl;
318 if (i + len >= linemax) {
331 static int find_common_prefix(char *const argv[])
334 char *anchor, *s, *t;
341 len = strlen(anchor);
342 while ((t = *argv++) != NULL) {
344 for (i = 0; i < len; i++, t++, s++) {
353 static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */
355 int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
357 int n = *np, col = *colp;
358 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
362 int i, j, k, len, seplen, argc;
365 #ifdef CONFIG_CMDLINE_PS_SUPPORT
366 const char *ps_prompt = env_get("PS1");
368 const char *ps_prompt = CONFIG_SYS_PROMPT;
371 if (strcmp(prompt, ps_prompt) != 0)
372 return 0; /* not in normal console */
376 last_char = buf[cnt - 1];
380 /* copy to secondary buffer which will be affected */
381 strcpy(tmp_buf, buf);
383 /* separate into argv */
384 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
386 /* first try a $ completion */
387 i = dollar_complete(argc, argv, last_char,
388 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
390 /* do the completion and return the possible completions */
391 i = complete_cmdv(argc, argv, last_char,
392 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
395 /* no match; bell and out */
397 if (argc > 1) /* allow tab for non command */
407 if (i == 1) { /* one match; perfect */
408 if (last_char != '\0' && !isblank(last_char))
409 k = strlen(argv[argc - 1]);
417 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
418 if (last_char != '\0' && !isblank(last_char))
419 k = strlen(argv[argc - 1]);
432 /* make sure it fits */
433 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
439 for (i = 0; i < len; i++)
442 for (i = 0; i < seplen; i++)
453 print_argv(NULL, " ", " ", 78, cmdv);
464 int cmd_get_data_size(char* arg, int default_size)
466 /* Check for a size specification .b, .w or .l.
468 int len = strlen(arg);
469 if (len > 2 && arg[len-2] == '.') {
470 switch (arg[len-1]) {
480 if (MEM_SUPPORT_64BIT_DATA)
491 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
492 DECLARE_GLOBAL_DATA_PTR;
494 void fixup_cmdtable(struct cmd_tbl *cmdtp, int size)
498 if (gd->reloc_off == 0)
501 for (i = 0; i < size; i++) {
504 addr = (ulong)(cmdtp->cmd_rep) + gd->reloc_off;
506 (int (*)(struct cmd_tbl *, int, int,
507 char * const [], int *))addr;
509 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
510 #ifdef DEBUG_COMMANDS
511 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
512 cmdtp->name, (ulong)(cmdtp->cmd), addr);
514 cmdtp->cmd = (int (*)(struct cmd_tbl *, int, int,
515 char *const []))addr;
516 addr = (ulong)(cmdtp->name) + gd->reloc_off;
517 cmdtp->name = (char *)addr;
519 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
520 cmdtp->usage = (char *)addr;
522 #ifdef CONFIG_SYS_LONGHELP
524 addr = (ulong)(cmdtp->help) + gd->reloc_off;
525 cmdtp->help = (char *)addr;
528 #ifdef CONFIG_AUTO_COMPLETE
529 if (cmdtp->complete) {
530 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
532 (int (*)(int, char * const [], char, int, char * []))addr;
540 int cmd_always_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
541 char *const argv[], int *repeatable)
545 return cmdtp->cmd(cmdtp, flag, argc, argv);
548 int cmd_never_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
549 char *const argv[], int *repeatable)
553 return cmdtp->cmd(cmdtp, flag, argc, argv);
556 int cmd_discard_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
561 return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable);
565 * Call a command function. This should be the only route in U-Boot to call
566 * a command, so that we can track whether we are waiting for input or
567 * executing a command.
569 * @param cmdtp Pointer to the command to execute
570 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
571 * @param argc Number of arguments (arg 0 must be the command text)
572 * @param argv Arguments
573 * @param repeatable Can the command be repeated
574 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
576 static int cmd_call(struct cmd_tbl *cmdtp, int flag, int argc,
577 char *const argv[], int *repeatable)
581 result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable);
583 debug("Command failed, result=%d\n", result);
587 enum command_ret_t cmd_process(int flag, int argc, char *const argv[],
588 int *repeatable, ulong *ticks)
590 enum command_ret_t rc = CMD_RET_SUCCESS;
591 struct cmd_tbl *cmdtp;
593 #if defined(CONFIG_SYS_XTRACE)
596 xtrace = env_get("xtrace");
599 for (int i = 0; i < argc; i++) {
607 /* Look up command in command table */
608 cmdtp = find_cmd(argv[0]);
610 printf("Unknown command '%s' - try 'help'\n", argv[0]);
614 /* found - check max args */
615 if (argc > cmdtp->maxargs)
618 #if defined(CONFIG_CMD_BOOTD)
619 /* avoid "bootd" recursion */
620 else if (cmdtp->cmd == do_bootd) {
621 if (flag & CMD_FLAG_BOOTD) {
622 puts("'bootd' recursion detected\n");
623 rc = CMD_RET_FAILURE;
625 flag |= CMD_FLAG_BOOTD;
630 /* If OK so far, then do the command */
635 *ticks = get_timer(0);
636 rc = cmd_call(cmdtp, flag, argc, argv, &newrep);
638 *ticks = get_timer(*ticks);
639 *repeatable &= newrep;
641 if (rc == CMD_RET_USAGE)
642 rc = cmd_usage(cmdtp);
646 int cmd_process_error(struct cmd_tbl *cmdtp, int err)
648 if (err == CMD_RET_USAGE)
649 return CMD_RET_USAGE;
652 printf("Command '%s' failed: Error %d\n", cmdtp->name, err);
653 return CMD_RET_FAILURE;
656 return CMD_RET_SUCCESS;