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