1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 */
6
7 /*
8 * Command Processor Table
9 */
10
11 #include <common.h>
12 #include <command.h>
13 #include <console.h>
14 #include <env.h>
15 #include <linux/ctype.h>
16
17 /*
18 * Use puts() instead of printf() to avoid printf buffer overflow
19 * for long help messages
20 */
21
_do_help(cmd_tbl_t * cmd_start,int cmd_items,cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])22 int _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t *cmdtp, int flag,
23 int argc, char * const argv[])
24 {
25 int i;
26 int rcode = 0;
27
28 if (argc == 1) { /* show list of commands */
29 cmd_tbl_t *cmd_array[cmd_items];
30 int i, j, swaps;
31
32 /* Make array of commands from .uboot_cmd section */
33 cmdtp = cmd_start;
34 for (i = 0; i < cmd_items; i++) {
35 cmd_array[i] = cmdtp++;
36 }
37
38 /* Sort command list (trivial bubble sort) */
39 for (i = cmd_items - 1; i > 0; --i) {
40 swaps = 0;
41 for (j = 0; j < i; ++j) {
42 if (strcmp(cmd_array[j]->name,
43 cmd_array[j + 1]->name) > 0) {
44 cmd_tbl_t *tmp;
45 tmp = cmd_array[j];
46 cmd_array[j] = cmd_array[j + 1];
47 cmd_array[j + 1] = tmp;
48 ++swaps;
49 }
50 }
51 if (!swaps)
52 break;
53 }
54
55 /* print short help (usage) */
56 for (i = 0; i < cmd_items; i++) {
57 const char *usage = cmd_array[i]->usage;
58
59 /* allow user abort */
60 if (ctrlc())
61 return 1;
62 if (usage == NULL)
63 continue;
64 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
65 cmd_array[i]->name, usage);
66 }
67 return 0;
68 }
69 /*
70 * command help (long version)
71 */
72 for (i = 1; i < argc; ++i) {
73 cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items);
74 if (cmdtp != NULL) {
75 rcode |= cmd_usage(cmdtp);
76 } else {
77 printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n",
78 argv[i]);
79 rcode = 1;
80 }
81 }
82 return rcode;
83 }
84
85 /* find command table entry for a command */
find_cmd_tbl(const char * cmd,cmd_tbl_t * table,int table_len)86 cmd_tbl_t *find_cmd_tbl(const char *cmd, cmd_tbl_t *table, int table_len)
87 {
88 #ifdef CONFIG_CMDLINE
89 cmd_tbl_t *cmdtp;
90 cmd_tbl_t *cmdtp_temp = table; /* Init value */
91 const char *p;
92 int len;
93 int n_found = 0;
94
95 if (!cmd)
96 return NULL;
97 /*
98 * Some commands allow length modifiers (like "cp.b");
99 * compare command name only until first dot.
100 */
101 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
102
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 */
107
108 cmdtp_temp = cmdtp; /* abbreviated command ? */
109 n_found++;
110 }
111 }
112 if (n_found == 1) { /* exactly one match */
113 return cmdtp_temp;
114 }
115 #endif /* CONFIG_CMDLINE */
116
117 return NULL; /* not found or ambiguous command */
118 }
119
find_cmd(const char * cmd)120 cmd_tbl_t *find_cmd(const char *cmd)
121 {
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);
125 }
126
cmd_usage(const cmd_tbl_t * cmdtp)127 int cmd_usage(const cmd_tbl_t *cmdtp)
128 {
129 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
130
131 #ifdef CONFIG_SYS_LONGHELP
132 printf("Usage:\n%s ", cmdtp->name);
133
134 if (!cmdtp->help) {
135 puts ("- No additional help available.\n");
136 return 1;
137 }
138
139 puts(cmdtp->help);
140 putc('\n');
141 #endif /* CONFIG_SYS_LONGHELP */
142 return 1;
143 }
144
145 #ifdef CONFIG_AUTO_COMPLETE
146 static char env_complete_buf[512];
147
var_complete(int argc,char * const argv[],char last_char,int maxv,char * cmdv[])148 int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
149 {
150 int space;
151
152 space = last_char == '\0' || isblank(last_char);
153
154 if (space && argc == 1)
155 return env_complete("", maxv, cmdv, sizeof(env_complete_buf),
156 env_complete_buf, false);
157
158 if (!space && argc == 2)
159 return env_complete(argv[1], maxv, cmdv,
160 sizeof(env_complete_buf),
161 env_complete_buf, false);
162
163 return 0;
164 }
165
dollar_complete(int argc,char * const argv[],char last_char,int maxv,char * cmdv[])166 static int dollar_complete(int argc, char * const argv[], char last_char,
167 int maxv, char *cmdv[])
168 {
169 /* Make sure the last argument starts with a $. */
170 if (argc < 1 || argv[argc - 1][0] != '$' ||
171 last_char == '\0' || isblank(last_char))
172 return 0;
173
174 return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf),
175 env_complete_buf, true);
176 }
177
178 /*************************************************************************************/
179
complete_subcmdv(cmd_tbl_t * cmdtp,int count,int argc,char * const argv[],char last_char,int maxv,char * cmdv[])180 int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc,
181 char * const argv[], char last_char,
182 int maxv, char *cmdv[])
183 {
184 #ifdef CONFIG_CMDLINE
185 const cmd_tbl_t *cmdend = cmdtp + count;
186 const char *p;
187 int len, clen;
188 int n_found = 0;
189 const char *cmd;
190
191 /* sanity? */
192 if (maxv < 2)
193 return -2;
194
195 cmdv[0] = NULL;
196
197 if (argc == 0) {
198 /* output full list of commands */
199 for (; cmdtp != cmdend; cmdtp++) {
200 if (n_found >= maxv - 2) {
201 cmdv[n_found++] = "...";
202 break;
203 }
204 cmdv[n_found++] = cmdtp->name;
205 }
206 cmdv[n_found] = NULL;
207 return n_found;
208 }
209
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) {
214 cmdv[0] = NULL;
215 return 0;
216 }
217 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
218 }
219
220 cmd = argv[0];
221 /*
222 * Some commands allow length modifiers (like "cp.b");
223 * compare command name only until first dot.
224 */
225 p = strchr(cmd, '.');
226 if (p == NULL)
227 len = strlen(cmd);
228 else
229 len = p - cmd;
230
231 /* return the partial matches */
232 for (; cmdtp != cmdend; cmdtp++) {
233
234 clen = strlen(cmdtp->name);
235 if (clen < len)
236 continue;
237
238 if (memcmp(cmd, cmdtp->name, len) != 0)
239 continue;
240
241 /* too many! */
242 if (n_found >= maxv - 2) {
243 cmdv[n_found++] = "...";
244 break;
245 }
246
247 cmdv[n_found++] = cmdtp->name;
248 }
249
250 cmdv[n_found] = NULL;
251 return n_found;
252 #else
253 return 0;
254 #endif
255 }
256
complete_cmdv(int argc,char * const argv[],char last_char,int maxv,char * cmdv[])257 static int complete_cmdv(int argc, char * const argv[], char last_char,
258 int maxv, char *cmdv[])
259 {
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);
264 #else
265 return 0;
266 #endif
267 }
268
make_argv(char * s,int argvsz,char * argv[])269 static int make_argv(char *s, int argvsz, char *argv[])
270 {
271 int argc = 0;
272
273 /* split into argv */
274 while (argc < argvsz - 1) {
275
276 /* skip any white space */
277 while (isblank(*s))
278 ++s;
279
280 if (*s == '\0') /* end of s, no more args */
281 break;
282
283 argv[argc++] = s; /* begin of argument string */
284
285 /* find end of string */
286 while (*s && !isblank(*s))
287 ++s;
288
289 if (*s == '\0') /* end of s, no more args */
290 break;
291
292 *s++ = '\0'; /* terminate current arg */
293 }
294 argv[argc] = NULL;
295
296 return argc;
297 }
298
print_argv(const char * banner,const char * leader,const char * sep,int linemax,char * const argv[])299 static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
300 {
301 int ll = leader != NULL ? strlen(leader) : 0;
302 int sl = sep != NULL ? strlen(sep) : 0;
303 int len, i;
304
305 if (banner) {
306 puts("\n");
307 puts(banner);
308 }
309
310 i = linemax; /* force leader and newline */
311 while (*argv != NULL) {
312 len = strlen(*argv) + sl;
313 if (i + len >= linemax) {
314 puts("\n");
315 if (leader)
316 puts(leader);
317 i = ll - sl;
318 } else if (sep)
319 puts(sep);
320 puts(*argv++);
321 i += len;
322 }
323 printf("\n");
324 }
325
find_common_prefix(char * const argv[])326 static int find_common_prefix(char * const argv[])
327 {
328 int i, len;
329 char *anchor, *s, *t;
330
331 if (*argv == NULL)
332 return 0;
333
334 /* begin with max */
335 anchor = *argv++;
336 len = strlen(anchor);
337 while ((t = *argv++) != NULL) {
338 s = anchor;
339 for (i = 0; i < len; i++, t++, s++) {
340 if (*t != *s)
341 break;
342 }
343 len = s - anchor;
344 }
345 return len;
346 }
347
348 static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */
349
cmd_auto_complete(const char * const prompt,char * buf,int * np,int * colp)350 int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
351 {
352 int n = *np, col = *colp;
353 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
354 char *cmdv[20];
355 char *s, *t;
356 const char *sep;
357 int i, j, k, len, seplen, argc;
358 int cnt;
359 char last_char;
360 #ifdef CONFIG_CMDLINE_PS_SUPPORT
361 const char *ps_prompt = env_get("PS1");
362 #else
363 const char *ps_prompt = CONFIG_SYS_PROMPT;
364 #endif
365
366 if (strcmp(prompt, ps_prompt) != 0)
367 return 0; /* not in normal console */
368
369 cnt = strlen(buf);
370 if (cnt >= 1)
371 last_char = buf[cnt - 1];
372 else
373 last_char = '\0';
374
375 /* copy to secondary buffer which will be affected */
376 strcpy(tmp_buf, buf);
377
378 /* separate into argv */
379 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
380
381 /* first try a $ completion */
382 i = dollar_complete(argc, argv, last_char,
383 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
384 if (!i) {
385 /* do the completion and return the possible completions */
386 i = complete_cmdv(argc, argv, last_char,
387 sizeof(cmdv) / sizeof(cmdv[0]), cmdv);
388 }
389
390 /* no match; bell and out */
391 if (i == 0) {
392 if (argc > 1) /* allow tab for non command */
393 return 0;
394 putc('\a');
395 return 1;
396 }
397
398 s = NULL;
399 len = 0;
400 sep = NULL;
401 seplen = 0;
402 if (i == 1) { /* one match; perfect */
403 if (last_char != '\0' && !isblank(last_char))
404 k = strlen(argv[argc - 1]);
405 else
406 k = 0;
407
408 s = cmdv[0] + k;
409 len = strlen(s);
410 sep = " ";
411 seplen = 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]);
415 else
416 k = 0;
417
418 j -= k;
419 if (j > 0) {
420 s = cmdv[0] + k;
421 len = j;
422 }
423 }
424
425 if (s != NULL) {
426 k = len + seplen;
427 /* make sure it fits */
428 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
429 putc('\a');
430 return 1;
431 }
432
433 t = buf + cnt;
434 for (i = 0; i < len; i++)
435 *t++ = *s++;
436 if (sep != NULL)
437 for (i = 0; i < seplen; i++)
438 *t++ = sep[i];
439 *t = '\0';
440 n += k;
441 col += k;
442 puts(t - k);
443 if (sep == NULL)
444 putc('\a');
445 *np = n;
446 *colp = col;
447 } else {
448 print_argv(NULL, " ", " ", 78, cmdv);
449
450 puts(prompt);
451 puts(buf);
452 }
453 return 1;
454 }
455
456 #endif
457
458 #ifdef CMD_DATA_SIZE
cmd_get_data_size(char * arg,int default_size)459 int cmd_get_data_size(char* arg, int default_size)
460 {
461 /* Check for a size specification .b, .w or .l.
462 */
463 int len = strlen(arg);
464 if (len > 2 && arg[len-2] == '.') {
465 switch (arg[len-1]) {
466 case 'b':
467 return 1;
468 case 'w':
469 return 2;
470 case 'l':
471 return 4;
472 #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
473 case 'q':
474 return 8;
475 #endif
476 case 's':
477 return -2;
478 default:
479 return -1;
480 }
481 }
482 return default_size;
483 }
484 #endif
485
486 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
487 DECLARE_GLOBAL_DATA_PTR;
488
fixup_cmdtable(cmd_tbl_t * cmdtp,int size)489 void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
490 {
491 int i;
492
493 if (gd->reloc_off == 0)
494 return;
495
496 for (i = 0; i < size; i++) {
497 ulong addr;
498
499 addr = (ulong)(cmdtp->cmd_rep) + gd->reloc_off;
500 cmdtp->cmd_rep =
501 (int (*)(struct cmd_tbl_s *, int, int,
502 char * const [], int *))addr;
503
504 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
505 #ifdef DEBUG_COMMANDS
506 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
507 cmdtp->name, (ulong)(cmdtp->cmd), addr);
508 #endif
509 cmdtp->cmd =
510 (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
511 addr = (ulong)(cmdtp->name) + gd->reloc_off;
512 cmdtp->name = (char *)addr;
513 if (cmdtp->usage) {
514 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
515 cmdtp->usage = (char *)addr;
516 }
517 #ifdef CONFIG_SYS_LONGHELP
518 if (cmdtp->help) {
519 addr = (ulong)(cmdtp->help) + gd->reloc_off;
520 cmdtp->help = (char *)addr;
521 }
522 #endif
523 #ifdef CONFIG_AUTO_COMPLETE
524 if (cmdtp->complete) {
525 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
526 cmdtp->complete =
527 (int (*)(int, char * const [], char, int, char * []))addr;
528 }
529 #endif
530 cmdtp++;
531 }
532 }
533 #endif
534
cmd_always_repeatable(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[],int * repeatable)535 int cmd_always_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
536 char * const argv[], int *repeatable)
537 {
538 *repeatable = 1;
539
540 return cmdtp->cmd(cmdtp, flag, argc, argv);
541 }
542
cmd_never_repeatable(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[],int * repeatable)543 int cmd_never_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
544 char * const argv[], int *repeatable)
545 {
546 *repeatable = 0;
547
548 return cmdtp->cmd(cmdtp, flag, argc, argv);
549 }
550
cmd_discard_repeatable(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])551 int cmd_discard_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
552 char * const argv[])
553 {
554 int repeatable;
555
556 return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable);
557 }
558
559 /**
560 * Call a command function. This should be the only route in U-Boot to call
561 * a command, so that we can track whether we are waiting for input or
562 * executing a command.
563 *
564 * @param cmdtp Pointer to the command to execute
565 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
566 * @param argc Number of arguments (arg 0 must be the command text)
567 * @param argv Arguments
568 * @param repeatable Can the command be repeated
569 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
570 */
cmd_call(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[],int * repeatable)571 static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
572 int *repeatable)
573 {
574 int result;
575
576 result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable);
577 if (result)
578 debug("Command failed, result=%d\n", result);
579 return result;
580 }
581
cmd_process(int flag,int argc,char * const argv[],int * repeatable,ulong * ticks)582 enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
583 int *repeatable, ulong *ticks)
584 {
585 enum command_ret_t rc = CMD_RET_SUCCESS;
586 cmd_tbl_t *cmdtp;
587
588 #if defined(CONFIG_SYS_XTRACE)
589 char *xtrace;
590
591 xtrace = env_get("xtrace");
592 if (xtrace) {
593 puts("+");
594 for (int i = 0; i < argc; i++) {
595 puts(" ");
596 puts(argv[i]);
597 }
598 puts("\n");
599 }
600 #endif
601
602 /* Look up command in command table */
603 cmdtp = find_cmd(argv[0]);
604 if (cmdtp == NULL) {
605 printf("Unknown command '%s' - try 'help'\n", argv[0]);
606 return 1;
607 }
608
609 /* found - check max args */
610 if (argc > cmdtp->maxargs)
611 rc = CMD_RET_USAGE;
612
613 #if defined(CONFIG_CMD_BOOTD)
614 /* avoid "bootd" recursion */
615 else if (cmdtp->cmd == do_bootd) {
616 if (flag & CMD_FLAG_BOOTD) {
617 puts("'bootd' recursion detected\n");
618 rc = CMD_RET_FAILURE;
619 } else {
620 flag |= CMD_FLAG_BOOTD;
621 }
622 }
623 #endif
624
625 /* If OK so far, then do the command */
626 if (!rc) {
627 int newrep;
628
629 if (ticks)
630 *ticks = get_timer(0);
631 rc = cmd_call(cmdtp, flag, argc, argv, &newrep);
632 if (ticks)
633 *ticks = get_timer(*ticks);
634 *repeatable &= newrep;
635 }
636 if (rc == CMD_RET_USAGE)
637 rc = cmd_usage(cmdtp);
638 return rc;
639 }
640
cmd_process_error(cmd_tbl_t * cmdtp,int err)641 int cmd_process_error(cmd_tbl_t *cmdtp, int err)
642 {
643 if (err == CMD_RET_USAGE)
644 return CMD_RET_USAGE;
645
646 if (err) {
647 printf("Command '%s' failed: Error %d\n", cmdtp->name, err);
648 return CMD_RET_FAILURE;
649 }
650
651 return CMD_RET_SUCCESS;
652 }
653