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
15 #include <linux/ctype.h>
18 * Use puts() instead of printf() to avoid printf buffer overflow
19 * for long help messages
22 int _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t *cmdtp, int flag,
23 int argc, char * const argv[])
28 if (argc == 1) { /* show list of commands */
29 cmd_tbl_t *cmd_array[cmd_items];
32 /* Make array of commands from .uboot_cmd section */
34 for (i = 0; i < cmd_items; i++) {
35 cmd_array[i] = cmdtp++;
38 /* Sort command list (trivial bubble sort) */
39 for (i = cmd_items - 1; i > 0; --i) {
41 for (j = 0; j < i; ++j) {
42 if (strcmp(cmd_array[j]->name,
43 cmd_array[j + 1]->name) > 0) {
46 cmd_array[j] = cmd_array[j + 1];
47 cmd_array[j + 1] = tmp;
55 /* print short help (usage) */
56 for (i = 0; i < cmd_items; i++) {
57 const char *usage = cmd_array[i]->usage;
59 /* allow user abort */
64 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
65 cmd_array[i]->name, usage);
70 * command help (long version)
72 for (i = 1; i < argc; ++i) {
73 cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items);
75 rcode |= cmd_usage(cmdtp);
77 printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n",
85 /* find command table entry for a command */
86 cmd_tbl_t *find_cmd_tbl(const char *cmd, cmd_tbl_t *table, int table_len)
90 cmd_tbl_t *cmdtp_temp = table; /* Init value */
98 * Some commands allow length modifiers (like "cp.b");
99 * compare command name only until first dot.
101 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
103 for (cmdtp = table; cmdtp != table + table_len; cmdtp++) {
104 if (strncmp(cmd, cmdtp->name, len) == 0) {
105 if (len == strlen(cmdtp->name))
106 return cmdtp; /* full match */
108 cmdtp_temp = cmdtp; /* abbreviated command ? */
112 if (n_found == 1) { /* exactly one match */
115 #endif /* CONFIG_CMDLINE */
117 return NULL; /* not found or ambiguous command */
120 cmd_tbl_t *find_cmd(const char *cmd)
122 cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd);
123 const int len = ll_entry_count(cmd_tbl_t, cmd);
124 return find_cmd_tbl(cmd, start, len);
127 int cmd_usage(const cmd_tbl_t *cmdtp)
129 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
131 #ifdef CONFIG_SYS_LONGHELP
132 printf("Usage:\n%s ", cmdtp->name);
135 puts ("- No additional help available.\n");
141 #endif /* CONFIG_SYS_LONGHELP */
145 #ifdef CONFIG_AUTO_COMPLETE
146 static char env_complete_buf[512];
148 int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
152 space = last_char == '\0' || isblank(last_char);
154 if (space && argc == 1)
155 return env_complete("", maxv, cmdv, sizeof(env_complete_buf),
156 env_complete_buf, false);
158 if (!space && argc == 2)
159 return env_complete(argv[1], maxv, cmdv,
160 sizeof(env_complete_buf),
161 env_complete_buf, false);
166 static int dollar_complete(int argc, char * const argv[], char last_char,
167 int maxv, char *cmdv[])
169 /* Make sure the last argument starts with a $. */
170 if (argc < 1 || argv[argc - 1][0] != '$' ||
171 last_char == '\0' || isblank(last_char))
174 return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf),
175 env_complete_buf, true);
178 /*************************************************************************************/
180 int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc,
181 char * const argv[], char last_char,
182 int maxv, char *cmdv[])
184 #ifdef CONFIG_CMDLINE
185 const cmd_tbl_t *cmdend = cmdtp + count;
198 /* output full list of commands */
199 for (; cmdtp != cmdend; cmdtp++) {
200 if (n_found >= maxv - 2) {
201 cmdv[n_found++] = "...";
204 cmdv[n_found++] = cmdtp->name;
206 cmdv[n_found] = NULL;
210 /* more than one arg or one but the start of the next */
211 if (argc > 1 || last_char == '\0' || isblank(last_char)) {
212 cmdtp = find_cmd_tbl(argv[0], cmdtp, count);
213 if (cmdtp == NULL || cmdtp->complete == NULL) {
217 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
222 * Some commands allow length modifiers (like "cp.b");
223 * compare command name only until first dot.
225 p = strchr(cmd, '.');
231 /* return the partial matches */
232 for (; cmdtp != cmdend; cmdtp++) {
234 clen = strlen(cmdtp->name);
238 if (memcmp(cmd, cmdtp->name, len) != 0)
242 if (n_found >= maxv - 2) {
243 cmdv[n_found++] = "...";
247 cmdv[n_found++] = cmdtp->name;
250 cmdv[n_found] = NULL;
257 static int complete_cmdv(int argc, char * const argv[], char last_char,
258 int maxv, char *cmdv[])
260 #ifdef CONFIG_CMDLINE
261 return complete_subcmdv(ll_entry_start(cmd_tbl_t, cmd),
262 ll_entry_count(cmd_tbl_t, cmd), argc, argv,
263 last_char, maxv, cmdv);
269 static int make_argv(char *s, int argvsz, char *argv[])
273 /* split into argv */
274 while (argc < argvsz - 1) {
276 /* skip any white space */
280 if (*s == '\0') /* end of s, no more args */
283 argv[argc++] = s; /* begin of argument string */
285 /* find end of string */
286 while (*s && !isblank(*s))
289 if (*s == '\0') /* end of s, no more args */
292 *s++ = '\0'; /* terminate current arg */
299 static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
301 int ll = leader != NULL ? strlen(leader) : 0;
302 int sl = sep != NULL ? strlen(sep) : 0;
310 i = linemax; /* force leader and newline */
311 while (*argv != NULL) {
312 len = strlen(*argv) + sl;
313 if (i + len >= linemax) {
326 static int find_common_prefix(char * const argv[])
329 char *anchor, *s, *t;
336 len = strlen(anchor);
337 while ((t = *argv++) != NULL) {
339 for (i = 0; i < len; i++, t++, s++) {
348 static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */
350 int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
352 int n = *np, col = *colp;
353 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
357 int i, j, k, len, seplen, argc;
360 #ifdef CONFIG_CMDLINE_PS_SUPPORT
361 const char *ps_prompt = env_get("PS1");
363 const char *ps_prompt = CONFIG_SYS_PROMPT;
366 if (strcmp(prompt, ps_prompt) != 0)
367 return 0; /* not in normal console */
371 last_char = buf[cnt - 1];
375 /* copy to secondary buffer which will be affected */
376 strcpy(tmp_buf, buf);
378 /* separate into argv */
379 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
381 /* first try a $ completion */
382 i = dollar_complete(argc, argv, last_char,
383 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
385 /* do the completion and return the possible completions */
386 i = complete_cmdv(argc, argv, last_char,
387 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
390 /* no match; bell and out */
392 if (argc > 1) /* allow tab for non command */
402 if (i == 1) { /* one match; perfect */
403 if (last_char != '\0' && !isblank(last_char))
404 k = strlen(argv[argc - 1]);
412 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
413 if (last_char != '\0' && !isblank(last_char))
414 k = strlen(argv[argc - 1]);
427 /* make sure it fits */
428 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
434 for (i = 0; i < len; i++)
437 for (i = 0; i < seplen; i++)
448 print_argv(NULL, " ", " ", 78, cmdv);
459 int cmd_get_data_size(char* arg, int default_size)
461 /* Check for a size specification .b, .w or .l.
463 int len = strlen(arg);
464 if (len > 2 && arg[len-2] == '.') {
465 switch (arg[len-1]) {
472 #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
486 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
487 DECLARE_GLOBAL_DATA_PTR;
489 void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
493 if (gd->reloc_off == 0)
496 for (i = 0; i < size; i++) {
499 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
500 #ifdef DEBUG_COMMANDS
501 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
502 cmdtp->name, (ulong)(cmdtp->cmd), addr);
505 (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
506 addr = (ulong)(cmdtp->name) + gd->reloc_off;
507 cmdtp->name = (char *)addr;
509 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
510 cmdtp->usage = (char *)addr;
512 #ifdef CONFIG_SYS_LONGHELP
514 addr = (ulong)(cmdtp->help) + gd->reloc_off;
515 cmdtp->help = (char *)addr;
518 #ifdef CONFIG_AUTO_COMPLETE
519 if (cmdtp->complete) {
520 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
522 (int (*)(int, char * const [], char, int, char * []))addr;
530 int cmd_always_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
531 char * const argv[], int *repeatable)
535 return cmdtp->cmd(cmdtp, flag, argc, argv);
538 int cmd_never_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
539 char * const argv[], int *repeatable)
543 return cmdtp->cmd(cmdtp, flag, argc, argv);
546 int cmd_discard_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
551 return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable);
555 * Call a command function. This should be the only route in U-Boot to call
556 * a command, so that we can track whether we are waiting for input or
557 * executing a command.
559 * @param cmdtp Pointer to the command to execute
560 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
561 * @param argc Number of arguments (arg 0 must be the command text)
562 * @param argv Arguments
563 * @param repeatable Can the command be repeated
564 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
566 static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
571 result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable);
573 debug("Command failed, result=%d\n", result);
577 enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
578 int *repeatable, ulong *ticks)
580 enum command_ret_t rc = CMD_RET_SUCCESS;
583 #if defined(CONFIG_SYS_XTRACE)
586 xtrace = env_get("xtrace");
589 for (int i = 0; i < argc; i++) {
597 /* Look up command in command table */
598 cmdtp = find_cmd(argv[0]);
600 printf("Unknown command '%s' - try 'help'\n", argv[0]);
604 /* found - check max args */
605 if (argc > cmdtp->maxargs)
608 #if defined(CONFIG_CMD_BOOTD)
609 /* avoid "bootd" recursion */
610 else if (cmdtp->cmd == do_bootd) {
611 if (flag & CMD_FLAG_BOOTD) {
612 puts("'bootd' recursion detected\n");
613 rc = CMD_RET_FAILURE;
615 flag |= CMD_FLAG_BOOTD;
620 /* If OK so far, then do the command */
625 *ticks = get_timer(0);
626 rc = cmd_call(cmdtp, flag, argc, argv, &newrep);
628 *ticks = get_timer(*ticks);
629 *repeatable &= newrep;
631 if (rc == CMD_RET_USAGE)
632 rc = cmd_usage(cmdtp);
636 int cmd_process_error(cmd_tbl_t *cmdtp, int err)
638 if (err == CMD_RET_USAGE)
639 return CMD_RET_USAGE;
642 printf("Command '%s' failed: Error %d\n", cmdtp->name, err);
643 return CMD_RET_FAILURE;
646 return CMD_RET_SUCCESS;