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
19 #include <asm/global_data.h>
20 #include <linux/ctype.h>
22 DECLARE_GLOBAL_DATA_PTR
;
25 * Use puts() instead of printf() to avoid printf buffer overflow
26 * for long help messages
29 int _do_help(struct cmd_tbl
*cmd_start
, int cmd_items
, struct cmd_tbl
*cmdtp
,
30 int flag
, int argc
, char *const argv
[])
35 if (argc
== 1) { /* show list of commands */
36 struct cmd_tbl
*cmd_array
[cmd_items
];
39 /* Make array of commands from .uboot_cmd section */
41 for (i
= 0; i
< cmd_items
; i
++) {
42 cmd_array
[i
] = cmdtp
++;
45 /* Sort command list (trivial bubble sort) */
46 for (i
= cmd_items
- 1; i
> 0; --i
) {
48 for (j
= 0; j
< i
; ++j
) {
49 if (strcmp(cmd_array
[j
]->name
,
50 cmd_array
[j
+ 1]->name
) > 0) {
53 cmd_array
[j
] = cmd_array
[j
+ 1];
54 cmd_array
[j
+ 1] = tmp
;
62 /* print short help (usage) */
63 for (i
= 0; i
< cmd_items
; i
++) {
64 const char *usage
= cmd_array
[i
]->usage
;
66 /* allow user abort */
71 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH
,
72 cmd_array
[i
]->name
, usage
);
77 * command help (long version)
79 for (i
= 1; i
< argc
; ++i
) {
80 cmdtp
= find_cmd_tbl(argv
[i
], cmd_start
, cmd_items
);
82 rcode
|= cmd_usage(cmdtp
);
84 printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n",
92 /* find command table entry for a command */
93 struct cmd_tbl
*find_cmd_tbl(const char *cmd
, struct cmd_tbl
*table
,
97 struct cmd_tbl
*cmdtp
;
98 struct cmd_tbl
*cmdtp_temp
= table
; /* Init value */
106 * Some commands allow length modifiers (like "cp.b");
107 * compare command name only until first dot.
109 len
= ((p
= strchr(cmd
, '.')) == NULL
) ? strlen (cmd
) : (p
- cmd
);
111 for (cmdtp
= table
; cmdtp
!= table
+ table_len
; cmdtp
++) {
112 if (strncmp(cmd
, cmdtp
->name
, len
) == 0) {
113 if (len
== strlen(cmdtp
->name
))
114 return cmdtp
; /* full match */
116 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
120 if (n_found
== 1) { /* exactly one match */
123 #endif /* CONFIG_CMDLINE */
125 return NULL
; /* not found or ambiguous command */
128 struct cmd_tbl
*find_cmd(const char *cmd
)
130 struct cmd_tbl
*start
= ll_entry_start(struct cmd_tbl
, cmd
);
131 const int len
= ll_entry_count(struct cmd_tbl
, cmd
);
132 return find_cmd_tbl(cmd
, start
, len
);
135 int cmd_usage(const struct cmd_tbl
*cmdtp
)
137 printf("%s - %s\n\n", cmdtp
->name
, cmdtp
->usage
);
139 #ifdef CONFIG_SYS_LONGHELP
140 printf("Usage:\n%s ", cmdtp
->name
);
143 puts ("- No additional help available.\n");
149 #endif /* CONFIG_SYS_LONGHELP */
153 #ifdef CONFIG_AUTO_COMPLETE
154 static char env_complete_buf
[512];
156 int var_complete(int argc
, char *const argv
[], char last_char
, int maxv
,
161 space
= last_char
== '\0' || isblank(last_char
);
163 if (space
&& argc
== 1)
164 return env_complete("", maxv
, cmdv
, sizeof(env_complete_buf
),
165 env_complete_buf
, false);
167 if (!space
&& argc
== 2)
168 return env_complete(argv
[1], maxv
, cmdv
,
169 sizeof(env_complete_buf
),
170 env_complete_buf
, false);
175 static int dollar_complete(int argc
, char *const argv
[], char last_char
,
176 int maxv
, char *cmdv
[])
178 /* Make sure the last argument starts with a $. */
179 if (argc
< 1 || argv
[argc
- 1][0] != '$' ||
180 last_char
== '\0' || isblank(last_char
))
183 return env_complete(argv
[argc
- 1], maxv
, cmdv
, sizeof(env_complete_buf
),
184 env_complete_buf
, true);
187 /*************************************************************************************/
189 int complete_subcmdv(struct cmd_tbl
*cmdtp
, int count
, int argc
,
190 char *const argv
[], char last_char
,
191 int maxv
, char *cmdv
[])
193 #ifdef CONFIG_CMDLINE
194 const struct cmd_tbl
*cmdend
= cmdtp
+ count
;
207 /* output full list of commands */
208 for (; cmdtp
!= cmdend
; cmdtp
++) {
209 if (n_found
>= maxv
- 2) {
210 cmdv
[n_found
++] = "...";
213 cmdv
[n_found
++] = cmdtp
->name
;
215 cmdv
[n_found
] = NULL
;
219 /* more than one arg or one but the start of the next */
220 if (argc
> 1 || last_char
== '\0' || isblank(last_char
)) {
221 cmdtp
= find_cmd_tbl(argv
[0], cmdtp
, count
);
222 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
226 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
231 * Some commands allow length modifiers (like "cp.b");
232 * compare command name only until first dot.
234 p
= strchr(cmd
, '.');
240 /* return the partial matches */
241 for (; cmdtp
!= cmdend
; cmdtp
++) {
243 clen
= strlen(cmdtp
->name
);
247 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
251 if (n_found
>= maxv
- 2) {
252 cmdv
[n_found
++] = "...";
256 cmdv
[n_found
++] = cmdtp
->name
;
259 cmdv
[n_found
] = NULL
;
266 static int complete_cmdv(int argc
, char *const argv
[], char last_char
,
267 int maxv
, char *cmdv
[])
269 #ifdef CONFIG_CMDLINE
270 return complete_subcmdv(ll_entry_start(struct cmd_tbl
, cmd
),
271 ll_entry_count(struct cmd_tbl
, cmd
), argc
, argv
,
272 last_char
, maxv
, cmdv
);
278 static int make_argv(char *s
, int argvsz
, char *argv
[])
282 /* split into argv */
283 while (argc
< argvsz
- 1) {
285 /* skip any white space */
289 if (*s
== '\0') /* end of s, no more args */
292 argv
[argc
++] = s
; /* begin of argument string */
294 /* find end of string */
295 while (*s
&& !isblank(*s
))
298 if (*s
== '\0') /* end of s, no more args */
301 *s
++ = '\0'; /* terminate current arg */
308 static void print_argv(const char *banner
, const char *leader
, const char *sep
,
309 int linemax
, char *const argv
[])
311 int ll
= leader
!= NULL
? strlen(leader
) : 0;
312 int sl
= sep
!= NULL
? strlen(sep
) : 0;
320 i
= linemax
; /* force leader and newline */
321 while (*argv
!= NULL
) {
322 len
= strlen(*argv
) + sl
;
323 if (i
+ len
>= linemax
) {
336 static int find_common_prefix(char *const argv
[])
339 char *anchor
, *s
, *t
;
346 len
= strlen(anchor
);
347 while ((t
= *argv
++) != NULL
) {
349 for (i
= 0; i
< len
; i
++, t
++, s
++) {
358 static char tmp_buf
[CONFIG_SYS_CBSIZE
+ 1]; /* copy of console I/O buffer */
360 int cmd_auto_complete(const char *const prompt
, char *buf
, int *np
, int *colp
)
362 int n
= *np
, col
= *colp
;
363 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
367 int i
, j
, k
, len
, seplen
, argc
;
370 #ifdef CONFIG_CMDLINE_PS_SUPPORT
371 const char *ps_prompt
= env_get("PS1");
373 const char *ps_prompt
= CONFIG_SYS_PROMPT
;
376 if (strcmp(prompt
, ps_prompt
) != 0)
377 return 0; /* not in normal console */
381 last_char
= buf
[cnt
- 1];
385 /* copy to secondary buffer which will be affected */
386 strcpy(tmp_buf
, buf
);
388 /* separate into argv */
389 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
391 /* first try a $ completion */
392 i
= dollar_complete(argc
, argv
, last_char
,
393 sizeof(cmdv
) / sizeof(cmdv
[0]), cmdv
);
395 /* do the completion and return the possible completions */
396 i
= complete_cmdv(argc
, argv
, last_char
,
397 sizeof(cmdv
) / sizeof(cmdv
[0]), cmdv
);
400 /* no match; bell and out */
402 if (argc
> 1) /* allow tab for non command */
412 if (i
== 1) { /* one match; perfect */
413 if (last_char
!= '\0' && !isblank(last_char
))
414 k
= strlen(argv
[argc
- 1]);
422 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
423 if (last_char
!= '\0' && !isblank(last_char
))
424 k
= strlen(argv
[argc
- 1]);
437 /* make sure it fits */
438 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
444 for (i
= 0; i
< len
; i
++)
447 for (i
= 0; i
< seplen
; i
++)
458 print_argv(NULL
, " ", " ", 78, cmdv
);
469 int cmd_get_data_size(char* arg
, int default_size
)
471 /* Check for a size specification .b, .w or .l.
473 int len
= strlen(arg
);
474 if (len
> 2 && arg
[len
-2] == '.') {
475 switch (arg
[len
-1]) {
483 return CMD_DATA_SIZE_STR
;
485 if (MEM_SUPPORT_64BIT_DATA
)
489 return CMD_DATA_SIZE_ERR
;
496 void fixup_cmdtable(struct cmd_tbl
*cmdtp
, int size
)
500 if (gd
->reloc_off
== 0)
503 for (i
= 0; i
< size
; i
++) {
506 addr
= (ulong
)(cmdtp
->cmd_rep
) + gd
->reloc_off
;
508 (int (*)(struct cmd_tbl
*, int, int,
509 char * const [], int *))addr
;
511 addr
= (ulong
)(cmdtp
->cmd
) + gd
->reloc_off
;
512 #ifdef DEBUG_COMMANDS
513 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
514 cmdtp
->name
, (ulong
)(cmdtp
->cmd
), addr
);
516 cmdtp
->cmd
= (int (*)(struct cmd_tbl
*, int, int,
517 char *const []))addr
;
518 addr
= (ulong
)(cmdtp
->name
) + gd
->reloc_off
;
519 cmdtp
->name
= (char *)addr
;
521 addr
= (ulong
)(cmdtp
->usage
) + gd
->reloc_off
;
522 cmdtp
->usage
= (char *)addr
;
524 #ifdef CONFIG_SYS_LONGHELP
526 addr
= (ulong
)(cmdtp
->help
) + gd
->reloc_off
;
527 cmdtp
->help
= (char *)addr
;
530 #ifdef CONFIG_AUTO_COMPLETE
531 if (cmdtp
->complete
) {
532 addr
= (ulong
)(cmdtp
->complete
) + gd
->reloc_off
;
534 (int (*)(int, char * const [], char, int, char * []))addr
;
541 int cmd_always_repeatable(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
542 char *const argv
[], int *repeatable
)
546 return cmdtp
->cmd(cmdtp
, flag
, argc
, argv
);
549 int cmd_never_repeatable(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
550 char *const argv
[], int *repeatable
)
554 return cmdtp
->cmd(cmdtp
, flag
, argc
, argv
);
557 int cmd_discard_repeatable(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
562 return cmdtp
->cmd_rep(cmdtp
, flag
, argc
, argv
, &repeatable
);
566 * Call a command function. This should be the only route in U-Boot to call
567 * a command, so that we can track whether we are waiting for input or
568 * executing a command.
570 * @param cmdtp Pointer to the command to execute
571 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
572 * @param argc Number of arguments (arg 0 must be the command text)
573 * @param argv Arguments
574 * @param repeatable Can the command be repeated
575 * Return: 0 if command succeeded, else non-zero (CMD_RET_...)
577 static int cmd_call(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
578 char *const argv
[], int *repeatable
)
582 result
= cmdtp
->cmd_rep(cmdtp
, flag
, argc
, argv
, repeatable
);
584 debug("Command failed, result=%d\n", result
);
588 enum command_ret_t
cmd_process(int flag
, int argc
, char *const argv
[],
589 int *repeatable
, ulong
*ticks
)
591 enum command_ret_t rc
= CMD_RET_SUCCESS
;
592 struct cmd_tbl
*cmdtp
;
594 #if defined(CONFIG_SYS_XTRACE)
597 xtrace
= env_get("xtrace");
600 for (int i
= 0; i
< argc
; i
++) {
608 /* Look up command in command table */
609 cmdtp
= find_cmd(argv
[0]);
611 printf("Unknown command '%s' - try 'help'\n", argv
[0]);
615 /* found - check max args */
616 if (argc
> cmdtp
->maxargs
)
619 #if defined(CONFIG_CMD_BOOTD)
620 /* avoid "bootd" recursion */
621 else if (cmdtp
->cmd
== do_bootd
) {
622 if (flag
& CMD_FLAG_BOOTD
) {
623 puts("'bootd' recursion detected\n");
624 rc
= CMD_RET_FAILURE
;
626 flag
|= CMD_FLAG_BOOTD
;
631 /* If OK so far, then do the command */
636 *ticks
= get_timer(0);
637 rc
= cmd_call(cmdtp
, flag
, argc
, argv
, &newrep
);
639 *ticks
= get_timer(*ticks
);
640 *repeatable
&= newrep
;
642 if (rc
== CMD_RET_USAGE
)
643 rc
= cmd_usage(cmdtp
);
647 int cmd_process_error(struct cmd_tbl
*cmdtp
, int err
)
649 if (err
== CMD_RET_USAGE
)
650 return CMD_RET_USAGE
;
653 printf("Command '%s' failed: Error %d\n", cmdtp
->name
, err
);
654 return CMD_RET_FAILURE
;
657 return CMD_RET_SUCCESS
;
660 int cmd_source_script(ulong addr
, const char *fit_uname
, const char *confname
)
667 buf
= map_sysmem(addr
, 0);
668 ret
= image_locate_script(buf
, 0, fit_uname
, confname
, &data
, &len
);
671 return CMD_RET_FAILURE
;
673 debug("** Script length: %d\n", len
);
674 return run_command_list(data
, len
, 0);