]>
Commit | Line | Data |
---|---|---|
5dfa25f2 | 1 | /* |
2dce551e | 2 | * (C) Copyright 2000-2009 |
5dfa25f2 WD |
3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
4 | * | |
5 | * See file CREDITS for list of people who contributed to this | |
6 | * project. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License as | |
10 | * published by the Free Software Foundation; either version 2 of | |
11 | * the License, or (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
21 | * MA 02111-1307 USA | |
22 | */ | |
23 | ||
24 | /* | |
25 | * Command Processor Table | |
26 | */ | |
27 | ||
28 | #include <common.h> | |
29 | #include <command.h> | |
4d91a6ec | 30 | #include <linux/ctype.h> |
5dfa25f2 | 31 | |
5dfa25f2 WD |
32 | /* |
33 | * Use puts() instead of printf() to avoid printf buffer overflow | |
34 | * for long help messages | |
35 | */ | |
2dce551e DZ |
36 | |
37 | int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int | |
54841ab5 | 38 | flag, int argc, char * const argv[]) |
5dfa25f2 WD |
39 | { |
40 | int i; | |
41 | int rcode = 0; | |
42 | ||
199adb60 KP |
43 | if (cmd_items > CONFIG_SYS_MAXARGS) { |
44 | printf("%s: cmd_items %d exceeds hardcoded limit %d." | |
45 | " Recompile with higher CONFIG_SYS_MAXARGS?\n", | |
46 | __func__, cmd_items, CONFIG_SYS_MAXARGS); | |
47 | return -1; | |
48 | } | |
49 | ||
8bde7f77 | 50 | if (argc == 1) { /*show list of commands */ |
199adb60 | 51 | cmd_tbl_t *cmd_array[CONFIG_SYS_MAXARGS]; |
9d2b18a0 WD |
52 | int i, j, swaps; |
53 | ||
54 | /* Make array of commands from .uboot_cmd section */ | |
2dce551e | 55 | cmdtp = cmd_start; |
9d2b18a0 WD |
56 | for (i = 0; i < cmd_items; i++) { |
57 | cmd_array[i] = cmdtp++; | |
8bde7f77 | 58 | } |
8bde7f77 | 59 | |
9d2b18a0 WD |
60 | /* Sort command list (trivial bubble sort) */ |
61 | for (i = cmd_items - 1; i > 0; --i) { | |
62 | swaps = 0; | |
63 | for (j = 0; j < i; ++j) { | |
64 | if (strcmp (cmd_array[j]->name, | |
65 | cmd_array[j + 1]->name) > 0) { | |
66 | cmd_tbl_t *tmp; | |
67 | tmp = cmd_array[j]; | |
68 | cmd_array[j] = cmd_array[j + 1]; | |
69 | cmd_array[j + 1] = tmp; | |
70 | ++swaps; | |
71 | } | |
8bde7f77 | 72 | } |
9d2b18a0 WD |
73 | if (!swaps) |
74 | break; | |
8bde7f77 | 75 | } |
5dfa25f2 | 76 | |
8bde7f77 | 77 | /* print short help (usage) */ |
9d2b18a0 WD |
78 | for (i = 0; i < cmd_items; i++) { |
79 | const char *usage = cmd_array[i]->usage; | |
80 | ||
5dfa25f2 | 81 | /* allow user abort */ |
8bde7f77 | 82 | if (ctrlc ()) |
5dfa25f2 | 83 | return 1; |
9d2b18a0 | 84 | if (usage == NULL) |
5dfa25f2 | 85 | continue; |
2fb2604d PT |
86 | printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH, |
87 | cmd_array[i]->name, usage); | |
5dfa25f2 | 88 | } |
5dfa25f2 WD |
89 | return 0; |
90 | } | |
5dfa25f2 WD |
91 | /* |
92 | * command help (long version) | |
93 | */ | |
8bde7f77 | 94 | for (i = 1; i < argc; ++i) { |
2dce551e | 95 | if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) { |
94796d85 | 96 | rcode |= cmd_usage(cmdtp); |
71f95118 | 97 | } else { |
5dfa25f2 WD |
98 | printf ("Unknown command '%s' - try 'help'" |
99 | " without arguments for list of all" | |
8bde7f77 WD |
100 | " known commands\n\n", argv[i] |
101 | ); | |
5dfa25f2 WD |
102 | rcode = 1; |
103 | } | |
104 | } | |
105 | return rcode; | |
106 | } | |
107 | ||
108 | /*************************************************************************** | |
109 | * find command table entry for a command | |
110 | */ | |
b799cb4c | 111 | cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len) |
5dfa25f2 WD |
112 | { |
113 | cmd_tbl_t *cmdtp; | |
b799cb4c | 114 | cmd_tbl_t *cmdtp_temp = table; /*Init value */ |
9d2b18a0 WD |
115 | const char *p; |
116 | int len; | |
117 | int n_found = 0; | |
118 | ||
7013c061 TW |
119 | if (!cmd) |
120 | return NULL; | |
9d2b18a0 WD |
121 | /* |
122 | * Some commands allow length modifiers (like "cp.b"); | |
123 | * compare command name only until first dot. | |
124 | */ | |
125 | len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd); | |
126 | ||
b799cb4c KG |
127 | for (cmdtp = table; |
128 | cmdtp != table + table_len; | |
9d2b18a0 WD |
129 | cmdtp++) { |
130 | if (strncmp (cmd, cmdtp->name, len) == 0) { | |
131 | if (len == strlen (cmdtp->name)) | |
132 | return cmdtp; /* full match */ | |
133 | ||
134 | cmdtp_temp = cmdtp; /* abbreviated command ? */ | |
135 | n_found++; | |
136 | } | |
5dfa25f2 | 137 | } |
9d2b18a0 | 138 | if (n_found == 1) { /* exactly one match */ |
8bde7f77 | 139 | return cmdtp_temp; |
9d2b18a0 | 140 | } |
5dfa25f2 | 141 | |
9d2b18a0 | 142 | return NULL; /* not found or ambiguous command */ |
8bde7f77 | 143 | } |
04a85b3b | 144 | |
b799cb4c KG |
145 | cmd_tbl_t *find_cmd (const char *cmd) |
146 | { | |
6c7c946c MV |
147 | cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd); |
148 | const int len = ll_entry_count(cmd_tbl_t, cmd); | |
149 | return find_cmd_tbl(cmd, start, len); | |
b799cb4c KG |
150 | } |
151 | ||
e84ffddb | 152 | int cmd_usage(const cmd_tbl_t *cmdtp) |
62c3ae7c | 153 | { |
94796d85 WD |
154 | printf("%s - %s\n\n", cmdtp->name, cmdtp->usage); |
155 | ||
156 | #ifdef CONFIG_SYS_LONGHELP | |
157 | printf("Usage:\n%s ", cmdtp->name); | |
158 | ||
159 | if (!cmdtp->help) { | |
160 | puts ("- No additional help available.\n"); | |
161 | return 1; | |
162 | } | |
163 | ||
164 | puts (cmdtp->help); | |
165 | putc ('\n'); | |
166 | #endif /* CONFIG_SYS_LONGHELP */ | |
47e26b1b | 167 | return 1; |
62c3ae7c PT |
168 | } |
169 | ||
04a85b3b WD |
170 | #ifdef CONFIG_AUTO_COMPLETE |
171 | ||
54841ab5 | 172 | int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) |
04a85b3b WD |
173 | { |
174 | static char tmp_buf[512]; | |
175 | int space; | |
176 | ||
4d91a6ec | 177 | space = last_char == '\0' || isblank(last_char); |
04a85b3b WD |
178 | |
179 | if (space && argc == 1) | |
180 | return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf); | |
181 | ||
182 | if (!space && argc == 2) | |
183 | return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf); | |
560d424b | 184 | |
04a85b3b WD |
185 | return 0; |
186 | } | |
187 | ||
04a85b3b WD |
188 | /*************************************************************************************/ |
189 | ||
54841ab5 | 190 | static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) |
04a85b3b | 191 | { |
6c7c946c MV |
192 | cmd_tbl_t *cmdtp = ll_entry_start(cmd_tbl_t, cmd); |
193 | const int count = ll_entry_count(cmd_tbl_t, cmd); | |
194 | const cmd_tbl_t *cmdend = cmdtp + count; | |
04a85b3b WD |
195 | const char *p; |
196 | int len, clen; | |
197 | int n_found = 0; | |
198 | const char *cmd; | |
199 | ||
200 | /* sanity? */ | |
201 | if (maxv < 2) | |
202 | return -2; | |
203 | ||
204 | cmdv[0] = NULL; | |
205 | ||
206 | if (argc == 0) { | |
207 | /* output full list of commands */ | |
6c7c946c | 208 | for (; cmdtp != cmdend; cmdtp++) { |
04a85b3b | 209 | if (n_found >= maxv - 2) { |
6c7c946c | 210 | cmdv[n_found] = "..."; |
04a85b3b WD |
211 | break; |
212 | } | |
6c7c946c | 213 | cmdv[n_found] = cmdtp->name; |
04a85b3b WD |
214 | } |
215 | cmdv[n_found] = NULL; | |
216 | return n_found; | |
217 | } | |
218 | ||
219 | /* more than one arg or one but the start of the next */ | |
4d91a6ec | 220 | if (argc > 1 || (last_char == '\0' || isblank(last_char))) { |
04a85b3b WD |
221 | cmdtp = find_cmd(argv[0]); |
222 | if (cmdtp == NULL || cmdtp->complete == NULL) { | |
223 | cmdv[0] = NULL; | |
224 | return 0; | |
225 | } | |
226 | return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv); | |
227 | } | |
228 | ||
229 | cmd = argv[0]; | |
230 | /* | |
231 | * Some commands allow length modifiers (like "cp.b"); | |
232 | * compare command name only until first dot. | |
233 | */ | |
234 | p = strchr(cmd, '.'); | |
235 | if (p == NULL) | |
236 | len = strlen(cmd); | |
237 | else | |
238 | len = p - cmd; | |
239 | ||
240 | /* return the partial matches */ | |
6c7c946c | 241 | for (; cmdtp != cmdend; cmdtp++) { |
04a85b3b WD |
242 | |
243 | clen = strlen(cmdtp->name); | |
244 | if (clen < len) | |
245 | continue; | |
246 | ||
247 | if (memcmp(cmd, cmdtp->name, len) != 0) | |
248 | continue; | |
249 | ||
250 | /* too many! */ | |
251 | if (n_found >= maxv - 2) { | |
252 | cmdv[n_found++] = "..."; | |
253 | break; | |
254 | } | |
255 | ||
256 | cmdv[n_found++] = cmdtp->name; | |
257 | } | |
258 | ||
259 | cmdv[n_found] = NULL; | |
260 | return n_found; | |
261 | } | |
262 | ||
263 | static int make_argv(char *s, int argvsz, char *argv[]) | |
264 | { | |
265 | int argc = 0; | |
266 | ||
267 | /* split into argv */ | |
268 | while (argc < argvsz - 1) { | |
269 | ||
270 | /* skip any white space */ | |
4d91a6ec | 271 | while (isblank(*s)) |
04a85b3b WD |
272 | ++s; |
273 | ||
53677ef1 | 274 | if (*s == '\0') /* end of s, no more args */ |
04a85b3b WD |
275 | break; |
276 | ||
277 | argv[argc++] = s; /* begin of argument string */ | |
278 | ||
279 | /* find end of string */ | |
4d91a6ec | 280 | while (*s && !isblank(*s)) |
04a85b3b WD |
281 | ++s; |
282 | ||
283 | if (*s == '\0') /* end of s, no more args */ | |
284 | break; | |
285 | ||
286 | *s++ = '\0'; /* terminate current arg */ | |
287 | } | |
288 | argv[argc] = NULL; | |
289 | ||
290 | return argc; | |
291 | } | |
292 | ||
54841ab5 | 293 | static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[]) |
04a85b3b WD |
294 | { |
295 | int ll = leader != NULL ? strlen(leader) : 0; | |
296 | int sl = sep != NULL ? strlen(sep) : 0; | |
297 | int len, i; | |
298 | ||
299 | if (banner) { | |
300 | puts("\n"); | |
301 | puts(banner); | |
302 | } | |
303 | ||
304 | i = linemax; /* force leader and newline */ | |
305 | while (*argv != NULL) { | |
306 | len = strlen(*argv) + sl; | |
307 | if (i + len >= linemax) { | |
308 | puts("\n"); | |
309 | if (leader) | |
310 | puts(leader); | |
311 | i = ll - sl; | |
312 | } else if (sep) | |
313 | puts(sep); | |
314 | puts(*argv++); | |
315 | i += len; | |
316 | } | |
317 | printf("\n"); | |
318 | } | |
319 | ||
54841ab5 | 320 | static int find_common_prefix(char * const argv[]) |
04a85b3b WD |
321 | { |
322 | int i, len; | |
323 | char *anchor, *s, *t; | |
324 | ||
325 | if (*argv == NULL) | |
326 | return 0; | |
327 | ||
328 | /* begin with max */ | |
329 | anchor = *argv++; | |
330 | len = strlen(anchor); | |
331 | while ((t = *argv++) != NULL) { | |
332 | s = anchor; | |
333 | for (i = 0; i < len; i++, t++, s++) { | |
334 | if (*t != *s) | |
335 | break; | |
336 | } | |
337 | len = s - anchor; | |
338 | } | |
339 | return len; | |
340 | } | |
341 | ||
6d0f6bcf | 342 | static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */ |
04a85b3b WD |
343 | |
344 | int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp) | |
345 | { | |
346 | int n = *np, col = *colp; | |
6d0f6bcf | 347 | char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ |
04a85b3b WD |
348 | char *cmdv[20]; |
349 | char *s, *t; | |
350 | const char *sep; | |
351 | int i, j, k, len, seplen, argc; | |
352 | int cnt; | |
353 | char last_char; | |
354 | ||
6d0f6bcf | 355 | if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0) |
04a85b3b WD |
356 | return 0; /* not in normal console */ |
357 | ||
358 | cnt = strlen(buf); | |
359 | if (cnt >= 1) | |
360 | last_char = buf[cnt - 1]; | |
361 | else | |
362 | last_char = '\0'; | |
363 | ||
364 | /* copy to secondary buffer which will be affected */ | |
365 | strcpy(tmp_buf, buf); | |
366 | ||
367 | /* separate into argv */ | |
368 | argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv); | |
369 | ||
370 | /* do the completion and return the possible completions */ | |
371 | i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv); | |
372 | ||
373 | /* no match; bell and out */ | |
374 | if (i == 0) { | |
375 | if (argc > 1) /* allow tab for non command */ | |
376 | return 0; | |
377 | putc('\a'); | |
378 | return 1; | |
379 | } | |
380 | ||
381 | s = NULL; | |
382 | len = 0; | |
383 | sep = NULL; | |
384 | seplen = 0; | |
385 | if (i == 1) { /* one match; perfect */ | |
386 | k = strlen(argv[argc - 1]); | |
387 | s = cmdv[0] + k; | |
388 | len = strlen(s); | |
389 | sep = " "; | |
390 | seplen = 1; | |
391 | } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */ | |
392 | k = strlen(argv[argc - 1]); | |
393 | j -= k; | |
394 | if (j > 0) { | |
395 | s = cmdv[0] + k; | |
396 | len = j; | |
397 | } | |
398 | } | |
399 | ||
400 | if (s != NULL) { | |
401 | k = len + seplen; | |
402 | /* make sure it fits */ | |
6d0f6bcf | 403 | if (n + k >= CONFIG_SYS_CBSIZE - 2) { |
04a85b3b WD |
404 | putc('\a'); |
405 | return 1; | |
406 | } | |
407 | ||
408 | t = buf + cnt; | |
409 | for (i = 0; i < len; i++) | |
410 | *t++ = *s++; | |
411 | if (sep != NULL) | |
412 | for (i = 0; i < seplen; i++) | |
413 | *t++ = sep[i]; | |
414 | *t = '\0'; | |
415 | n += k; | |
416 | col += k; | |
417 | puts(t - k); | |
418 | if (sep == NULL) | |
419 | putc('\a'); | |
420 | *np = n; | |
421 | *colp = col; | |
422 | } else { | |
423 | print_argv(NULL, " ", " ", 78, cmdv); | |
424 | ||
425 | puts(prompt); | |
426 | puts(buf); | |
427 | } | |
428 | return 1; | |
429 | } | |
430 | ||
431 | #endif | |
8a40fb14 JCPV |
432 | |
433 | #ifdef CMD_DATA_SIZE | |
434 | int cmd_get_data_size(char* arg, int default_size) | |
435 | { | |
436 | /* Check for a size specification .b, .w or .l. | |
437 | */ | |
438 | int len = strlen(arg); | |
439 | if (len > 2 && arg[len-2] == '.') { | |
440 | switch(arg[len-1]) { | |
441 | case 'b': | |
442 | return 1; | |
443 | case 'w': | |
444 | return 2; | |
445 | case 'l': | |
446 | return 4; | |
447 | case 's': | |
448 | return -2; | |
449 | default: | |
450 | return -1; | |
451 | } | |
452 | } | |
453 | return default_size; | |
454 | } | |
455 | #endif | |
620f1f6a | 456 | |
2e5167cc | 457 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
620f1f6a HS |
458 | DECLARE_GLOBAL_DATA_PTR; |
459 | ||
460 | void fixup_cmdtable(cmd_tbl_t *cmdtp, int size) | |
461 | { | |
462 | int i; | |
463 | ||
464 | if (gd->reloc_off == 0) | |
465 | return; | |
466 | ||
467 | for (i = 0; i < size; i++) { | |
468 | ulong addr; | |
469 | ||
470 | addr = (ulong) (cmdtp->cmd) + gd->reloc_off; | |
471 | #if DEBUG_COMMANDS | |
472 | printf("Command \"%s\": 0x%08lx => 0x%08lx\n", | |
473 | cmdtp->name, (ulong) (cmdtp->cmd), addr); | |
474 | #endif | |
475 | cmdtp->cmd = | |
476 | (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr; | |
477 | addr = (ulong)(cmdtp->name) + gd->reloc_off; | |
478 | cmdtp->name = (char *)addr; | |
479 | if (cmdtp->usage) { | |
480 | addr = (ulong)(cmdtp->usage) + gd->reloc_off; | |
481 | cmdtp->usage = (char *)addr; | |
482 | } | |
483 | #ifdef CONFIG_SYS_LONGHELP | |
484 | if (cmdtp->help) { | |
485 | addr = (ulong)(cmdtp->help) + gd->reloc_off; | |
486 | cmdtp->help = (char *)addr; | |
487 | } | |
fa28bd2e DS |
488 | #endif |
489 | #ifdef CONFIG_AUTO_COMPLETE | |
490 | if (cmdtp->complete) { | |
491 | addr = (ulong)(cmdtp->complete) + gd->reloc_off; | |
3668d8fa DS |
492 | cmdtp->complete = |
493 | (int (*)(int, char * const [], char, int, char * []))addr; | |
fa28bd2e | 494 | } |
620f1f6a HS |
495 | #endif |
496 | cmdtp++; | |
497 | } | |
498 | } | |
499 | #endif | |
bdf8e34b SG |
500 | |
501 | /** | |
502 | * Call a command function. This should be the only route in U-Boot to call | |
503 | * a command, so that we can track whether we are waiting for input or | |
504 | * executing a command. | |
505 | * | |
506 | * @param cmdtp Pointer to the command to execute | |
507 | * @param flag Some flags normally 0 (see CMD_FLAG_.. above) | |
508 | * @param argc Number of arguments (arg 0 must be the command text) | |
509 | * @param argv Arguments | |
510 | * @return 0 if command succeeded, else non-zero (CMD_RET_...) | |
511 | */ | |
9d12d5d4 | 512 | static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
bdf8e34b SG |
513 | { |
514 | int result; | |
515 | ||
516 | result = (cmdtp->cmd)(cmdtp, flag, argc, argv); | |
517 | if (result) | |
518 | debug("Command failed, result=%d", result); | |
519 | return result; | |
520 | } | |
9d12d5d4 SG |
521 | |
522 | enum command_ret_t cmd_process(int flag, int argc, char * const argv[], | |
523 | int *repeatable) | |
524 | { | |
525 | enum command_ret_t rc = CMD_RET_SUCCESS; | |
526 | cmd_tbl_t *cmdtp; | |
527 | ||
528 | /* Look up command in command table */ | |
529 | cmdtp = find_cmd(argv[0]); | |
530 | if (cmdtp == NULL) { | |
531 | printf("Unknown command '%s' - try 'help'\n", argv[0]); | |
532 | return 1; | |
533 | } | |
534 | ||
535 | /* found - check max args */ | |
536 | if (argc > cmdtp->maxargs) | |
537 | rc = CMD_RET_USAGE; | |
538 | ||
539 | #if defined(CONFIG_CMD_BOOTD) | |
540 | /* avoid "bootd" recursion */ | |
541 | else if (cmdtp->cmd == do_bootd) { | |
542 | if (flag & CMD_FLAG_BOOTD) { | |
543 | puts("'bootd' recursion detected\n"); | |
544 | rc = CMD_RET_FAILURE; | |
545 | } else { | |
546 | flag |= CMD_FLAG_BOOTD; | |
547 | } | |
548 | } | |
549 | #endif | |
550 | ||
551 | /* If OK so far, then do the command */ | |
552 | if (!rc) { | |
553 | rc = cmd_call(cmdtp, flag, argc, argv); | |
554 | *repeatable &= cmdtp->repeatable; | |
555 | } | |
556 | if (rc == CMD_RET_USAGE) | |
557 | rc = cmd_usage(cmdtp); | |
558 | return rc; | |
559 | } |