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