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 <asm/global_data.h>
18 #include <linux/ctype.h>
20 DECLARE_GLOBAL_DATA_PTR;
23 * Use puts() instead of printf() to avoid printf buffer overflow
24 * for long help messages
27 int _do_help(struct cmd_tbl *cmd_start, int cmd_items, struct cmd_tbl *cmdtp,
28 int flag, int argc, char *const argv[])
33 if (argc == 1) { /* show list of commands */
34 struct cmd_tbl *cmd_array[cmd_items];
37 /* Make array of commands from .uboot_cmd section */
39 for (i = 0; i < cmd_items; i++) {
40 cmd_array[i] = cmdtp++;
43 /* Sort command list (trivial bubble sort) */
44 for (i = cmd_items - 1; i > 0; --i) {
46 for (j = 0; j < i; ++j) {
47 if (strcmp(cmd_array[j]->name,
48 cmd_array[j + 1]->name) > 0) {
51 cmd_array[j] = cmd_array[j + 1];
52 cmd_array[j + 1] = tmp;
60 /* print short help (usage) */
61 for (i = 0; i < cmd_items; i++) {
62 const char *usage = cmd_array[i]->usage;
64 /* allow user abort */
69 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
70 cmd_array[i]->name, usage);
75 * command help (long version)
77 for (i = 1; i < argc; ++i) {
78 cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items);
80 rcode |= cmd_usage(cmdtp);
82 printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n",
90 /* find command table entry for a command */
91 struct cmd_tbl *find_cmd_tbl(const char *cmd, struct cmd_tbl *table,
95 struct cmd_tbl *cmdtp;
96 struct cmd_tbl *cmdtp_temp = table; /* Init value */
104 * Some commands allow length modifiers (like "cp.b");
105 * compare command name only until first dot.
107 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
109 for (cmdtp = table; cmdtp != table + table_len; cmdtp++) {
110 if (strncmp(cmd, cmdtp->name, len) == 0) {
111 if (len == strlen(cmdtp->name))
112 return cmdtp; /* full match */
114 cmdtp_temp = cmdtp; /* abbreviated command ? */
118 if (n_found == 1) { /* exactly one match */
121 #endif /* CONFIG_CMDLINE */
123 return NULL; /* not found or ambiguous command */
126 struct cmd_tbl *find_cmd(const char *cmd)
128 struct cmd_tbl *start = ll_entry_start(struct cmd_tbl, cmd);
129 const int len = ll_entry_count(struct cmd_tbl, cmd);
130 return find_cmd_tbl(cmd, start, len);
133 int cmd_usage(const struct cmd_tbl *cmdtp)
135 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
137 #ifdef CONFIG_SYS_LONGHELP
138 printf("Usage:\n%s ", cmdtp->name);
141 puts ("- No additional help available.\n");
147 #endif /* CONFIG_SYS_LONGHELP */
151 #ifdef CONFIG_AUTO_COMPLETE
152 static char env_complete_buf[512];
154 int var_complete(int argc, char *const argv[], char last_char, int maxv,
159 space = last_char == '\0' || isblank(last_char);
161 if (space && argc == 1)
162 return env_complete("", maxv, cmdv, sizeof(env_complete_buf),
163 env_complete_buf, false);
165 if (!space && argc == 2)
166 return env_complete(argv[1], maxv, cmdv,
167 sizeof(env_complete_buf),
168 env_complete_buf, false);
173 static int dollar_complete(int argc, char *const argv[], char last_char,
174 int maxv, char *cmdv[])
176 /* Make sure the last argument starts with a $. */
177 if (argc < 1 || argv[argc - 1][0] != '$' ||
178 last_char == '\0' || isblank(last_char))
181 return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf),
182 env_complete_buf, true);
185 /*************************************************************************************/
187 int complete_subcmdv(struct cmd_tbl *cmdtp, int count, int argc,
188 char *const argv[], char last_char,
189 int maxv, char *cmdv[])
191 #ifdef CONFIG_CMDLINE
192 const struct cmd_tbl *cmdend = cmdtp + count;
205 /* output full list of commands */
206 for (; cmdtp != cmdend; cmdtp++) {
207 if (n_found >= maxv - 2) {
208 cmdv[n_found++] = "...";
211 cmdv[n_found++] = cmdtp->name;
213 cmdv[n_found] = NULL;
217 /* more than one arg or one but the start of the next */
218 if (argc > 1 || last_char == '\0' || isblank(last_char)) {
219 cmdtp = find_cmd_tbl(argv[0], cmdtp, count);
220 if (cmdtp == NULL || cmdtp->complete == NULL) {
224 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
229 * Some commands allow length modifiers (like "cp.b");
230 * compare command name only until first dot.
232 p = strchr(cmd, '.');
238 /* return the partial matches */
239 for (; cmdtp != cmdend; cmdtp++) {
241 clen = strlen(cmdtp->name);
245 if (memcmp(cmd, cmdtp->name, len) != 0)
249 if (n_found >= maxv - 2) {
250 cmdv[n_found++] = "...";
254 cmdv[n_found++] = cmdtp->name;
257 cmdv[n_found] = NULL;
264 static int complete_cmdv(int argc, char *const argv[], char last_char,
265 int maxv, char *cmdv[])
267 #ifdef CONFIG_CMDLINE
268 return complete_subcmdv(ll_entry_start(struct cmd_tbl, cmd),
269 ll_entry_count(struct cmd_tbl, cmd), argc, argv,
270 last_char, maxv, cmdv);
276 static int make_argv(char *s, int argvsz, char *argv[])
280 /* split into argv */
281 while (argc < argvsz - 1) {
283 /* skip any white space */
287 if (*s == '\0') /* end of s, no more args */
290 argv[argc++] = s; /* begin of argument string */
292 /* find end of string */
293 while (*s && !isblank(*s))
296 if (*s == '\0') /* end of s, no more args */
299 *s++ = '\0'; /* terminate current arg */
306 static void print_argv(const char *banner, const char *leader, const char *sep,
307 int linemax, char *const argv[])
309 int ll = leader != NULL ? strlen(leader) : 0;
310 int sl = sep != NULL ? strlen(sep) : 0;
318 i = linemax; /* force leader and newline */
319 while (*argv != NULL) {
320 len = strlen(*argv) + sl;
321 if (i + len >= linemax) {
334 static int find_common_prefix(char *const argv[])
337 char *anchor, *s, *t;
344 len = strlen(anchor);
345 while ((t = *argv++) != NULL) {
347 for (i = 0; i < len; i++, t++, s++) {
356 static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */
358 int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
360 int n = *np, col = *colp;
361 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
365 int i, j, k, len, seplen, argc;
368 #ifdef CONFIG_CMDLINE_PS_SUPPORT
369 const char *ps_prompt = env_get("PS1");
371 const char *ps_prompt = CONFIG_SYS_PROMPT;
374 if (strcmp(prompt, ps_prompt) != 0)
375 return 0; /* not in normal console */
379 last_char = buf[cnt - 1];
383 /* copy to secondary buffer which will be affected */
384 strcpy(tmp_buf, buf);
386 /* separate into argv */
387 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
389 /* first try a $ completion */
390 i = dollar_complete(argc, argv, last_char,
391 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
393 /* do the completion and return the possible completions */
394 i = complete_cmdv(argc, argv, last_char,
395 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
398 /* no match; bell and out */
400 if (argc > 1) /* allow tab for non command */
410 if (i == 1) { /* one match; perfect */
411 if (last_char != '\0' && !isblank(last_char))
412 k = strlen(argv[argc - 1]);
420 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
421 if (last_char != '\0' && !isblank(last_char))
422 k = strlen(argv[argc - 1]);
435 /* make sure it fits */
436 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
442 for (i = 0; i < len; i++)
445 for (i = 0; i < seplen; i++)
456 print_argv(NULL, " ", " ", 78, cmdv);
467 int cmd_get_data_size(char* arg, int default_size)
469 /* Check for a size specification .b, .w or .l.
471 int len = strlen(arg);
472 if (len > 2 && arg[len-2] == '.') {
473 switch (arg[len-1]) {
481 return CMD_DATA_SIZE_STR;
483 if (MEM_SUPPORT_64BIT_DATA)
487 return CMD_DATA_SIZE_ERR;
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;
539 int cmd_always_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
540 char *const argv[], int *repeatable)
544 return cmdtp->cmd(cmdtp, flag, argc, argv);
547 int cmd_never_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
548 char *const argv[], int *repeatable)
552 return cmdtp->cmd(cmdtp, flag, argc, argv);
555 int cmd_discard_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
560 return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable);
564 * Call a command function. This should be the only route in U-Boot to call
565 * a command, so that we can track whether we are waiting for input or
566 * executing a command.
568 * @param cmdtp Pointer to the command to execute
569 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
570 * @param argc Number of arguments (arg 0 must be the command text)
571 * @param argv Arguments
572 * @param repeatable Can the command be repeated
573 * Return: 0 if command succeeded, else non-zero (CMD_RET_...)
575 static int cmd_call(struct cmd_tbl *cmdtp, int flag, int argc,
576 char *const argv[], int *repeatable)
580 result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable);
582 debug("Command failed, result=%d\n", result);
586 enum command_ret_t cmd_process(int flag, int argc, char *const argv[],
587 int *repeatable, ulong *ticks)
589 enum command_ret_t rc = CMD_RET_SUCCESS;
590 struct cmd_tbl *cmdtp;
592 #if defined(CONFIG_SYS_XTRACE)
595 xtrace = env_get("xtrace");
598 for (int i = 0; i < argc; i++) {
606 /* Look up command in command table */
607 cmdtp = find_cmd(argv[0]);
609 printf("Unknown command '%s' - try 'help'\n", argv[0]);
613 /* found - check max args */
614 if (argc > cmdtp->maxargs)
617 #if defined(CONFIG_CMD_BOOTD)
618 /* avoid "bootd" recursion */
619 else if (cmdtp->cmd == do_bootd) {
620 if (flag & CMD_FLAG_BOOTD) {
621 puts("'bootd' recursion detected\n");
622 rc = CMD_RET_FAILURE;
624 flag |= CMD_FLAG_BOOTD;
629 /* If OK so far, then do the command */
634 *ticks = get_timer(0);
635 rc = cmd_call(cmdtp, flag, argc, argv, &newrep);
637 *ticks = get_timer(*ticks);
638 *repeatable &= newrep;
640 if (rc == CMD_RET_USAGE)
641 rc = cmd_usage(cmdtp);
645 int cmd_process_error(struct cmd_tbl *cmdtp, int err)
647 if (err == CMD_RET_USAGE)
648 return CMD_RET_USAGE;
651 printf("Command '%s' failed: Error %d\n", cmdtp->name, err);
652 return CMD_RET_FAILURE;
655 return CMD_RET_SUCCESS;