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
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
147 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
149 static char tmp_buf
[512];
152 space
= last_char
== '\0' || isblank(last_char
);
154 if (space
&& argc
== 1)
155 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
157 if (!space
&& argc
== 2)
158 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
163 /*************************************************************************************/
165 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
167 #ifdef CONFIG_CMDLINE
168 cmd_tbl_t
*cmdtp
= ll_entry_start(cmd_tbl_t
, cmd
);
169 const int count
= ll_entry_count(cmd_tbl_t
, cmd
);
170 const cmd_tbl_t
*cmdend
= cmdtp
+ count
;
183 /* output full list of commands */
184 for (; cmdtp
!= cmdend
; cmdtp
++) {
185 if (n_found
>= maxv
- 2) {
186 cmdv
[n_found
++] = "...";
189 cmdv
[n_found
++] = cmdtp
->name
;
191 cmdv
[n_found
] = NULL
;
195 /* more than one arg or one but the start of the next */
196 if (argc
> 1 || last_char
== '\0' || isblank(last_char
)) {
197 cmdtp
= find_cmd(argv
[0]);
198 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
202 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
207 * Some commands allow length modifiers (like "cp.b");
208 * compare command name only until first dot.
210 p
= strchr(cmd
, '.');
216 /* return the partial matches */
217 for (; cmdtp
!= cmdend
; cmdtp
++) {
219 clen
= strlen(cmdtp
->name
);
223 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
227 if (n_found
>= maxv
- 2) {
228 cmdv
[n_found
++] = "...";
232 cmdv
[n_found
++] = cmdtp
->name
;
235 cmdv
[n_found
] = NULL
;
242 static int make_argv(char *s
, int argvsz
, char *argv
[])
246 /* split into argv */
247 while (argc
< argvsz
- 1) {
249 /* skip any white space */
253 if (*s
== '\0') /* end of s, no more args */
256 argv
[argc
++] = s
; /* begin of argument string */
258 /* find end of string */
259 while (*s
&& !isblank(*s
))
262 if (*s
== '\0') /* end of s, no more args */
265 *s
++ = '\0'; /* terminate current arg */
272 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
274 int ll
= leader
!= NULL
? strlen(leader
) : 0;
275 int sl
= sep
!= NULL
? strlen(sep
) : 0;
283 i
= linemax
; /* force leader and newline */
284 while (*argv
!= NULL
) {
285 len
= strlen(*argv
) + sl
;
286 if (i
+ len
>= linemax
) {
299 static int find_common_prefix(char * const argv
[])
302 char *anchor
, *s
, *t
;
309 len
= strlen(anchor
);
310 while ((t
= *argv
++) != NULL
) {
312 for (i
= 0; i
< len
; i
++, t
++, s
++) {
321 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
323 int cmd_auto_complete(const char *const prompt
, char *buf
, int *np
, int *colp
)
325 int n
= *np
, col
= *colp
;
326 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
330 int i
, j
, k
, len
, seplen
, argc
;
334 if (strcmp(prompt
, CONFIG_SYS_PROMPT
) != 0)
335 return 0; /* not in normal console */
339 last_char
= buf
[cnt
- 1];
343 /* copy to secondary buffer which will be affected */
344 strcpy(tmp_buf
, buf
);
346 /* separate into argv */
347 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
349 /* do the completion and return the possible completions */
350 i
= complete_cmdv(argc
, argv
, last_char
,
351 sizeof(cmdv
) / sizeof(cmdv
[0]), cmdv
);
353 /* no match; bell and out */
355 if (argc
> 1) /* allow tab for non command */
365 if (i
== 1) { /* one match; perfect */
366 k
= strlen(argv
[argc
- 1]);
371 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
372 k
= strlen(argv
[argc
- 1]);
382 /* make sure it fits */
383 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
389 for (i
= 0; i
< len
; i
++)
392 for (i
= 0; i
< seplen
; i
++)
403 print_argv(NULL
, " ", " ", 78, cmdv
);
414 int cmd_get_data_size(char* arg
, int default_size
)
416 /* Check for a size specification .b, .w or .l.
418 int len
= strlen(arg
);
419 if (len
> 2 && arg
[len
-2] == '.') {
420 switch (arg
[len
-1]) {
427 #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
441 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
442 DECLARE_GLOBAL_DATA_PTR
;
444 void fixup_cmdtable(cmd_tbl_t
*cmdtp
, int size
)
448 if (gd
->reloc_off
== 0)
451 for (i
= 0; i
< size
; i
++) {
454 addr
= (ulong
)(cmdtp
->cmd
) + gd
->reloc_off
;
455 #ifdef DEBUG_COMMANDS
456 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
457 cmdtp
->name
, (ulong
)(cmdtp
->cmd
), addr
);
460 (int (*)(struct cmd_tbl_s
*, int, int, char * const []))addr
;
461 addr
= (ulong
)(cmdtp
->name
) + gd
->reloc_off
;
462 cmdtp
->name
= (char *)addr
;
464 addr
= (ulong
)(cmdtp
->usage
) + gd
->reloc_off
;
465 cmdtp
->usage
= (char *)addr
;
467 #ifdef CONFIG_SYS_LONGHELP
469 addr
= (ulong
)(cmdtp
->help
) + gd
->reloc_off
;
470 cmdtp
->help
= (char *)addr
;
473 #ifdef CONFIG_AUTO_COMPLETE
474 if (cmdtp
->complete
) {
475 addr
= (ulong
)(cmdtp
->complete
) + gd
->reloc_off
;
477 (int (*)(int, char * const [], char, int, char * []))addr
;
486 * Call a command function. This should be the only route in U-Boot to call
487 * a command, so that we can track whether we are waiting for input or
488 * executing a command.
490 * @param cmdtp Pointer to the command to execute
491 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
492 * @param argc Number of arguments (arg 0 must be the command text)
493 * @param argv Arguments
494 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
496 static int cmd_call(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
500 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
502 debug("Command failed, result=%d\n", result
);
506 enum command_ret_t
cmd_process(int flag
, int argc
, char * const argv
[],
507 int *repeatable
, ulong
*ticks
)
509 enum command_ret_t rc
= CMD_RET_SUCCESS
;
512 /* Look up command in command table */
513 cmdtp
= find_cmd(argv
[0]);
515 printf("Unknown command '%s' - try 'help'\n", argv
[0]);
519 /* found - check max args */
520 if (argc
> cmdtp
->maxargs
)
523 #if defined(CONFIG_CMD_BOOTD)
524 /* avoid "bootd" recursion */
525 else if (cmdtp
->cmd
== do_bootd
) {
526 if (flag
& CMD_FLAG_BOOTD
) {
527 puts("'bootd' recursion detected\n");
528 rc
= CMD_RET_FAILURE
;
530 flag
|= CMD_FLAG_BOOTD
;
535 /* If OK so far, then do the command */
538 *ticks
= get_timer(0);
539 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
541 *ticks
= get_timer(*ticks
);
542 *repeatable
&= cmdtp
->repeatable
;
544 if (rc
== CMD_RET_USAGE
)
545 rc
= cmd_usage(cmdtp
);
549 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
552 printf("Command '%s' failed: Error %d\n", cmdtp
->name
, err
);