]>
Commit | Line | Data |
---|---|---|
c609719b WD |
1 | /* |
2 | * (C) Copyright 2000 | |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
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 | ||
a6c7ad2f WD |
24 | /* #define DEBUG */ |
25 | ||
c609719b WD |
26 | #include <common.h> |
27 | #include <watchdog.h> | |
28 | #include <command.h> | |
c609719b | 29 | #include <malloc.h> |
c609719b WD |
30 | |
31 | #ifdef CFG_HUSH_PARSER | |
32 | #include <hush.h> | |
33 | #endif | |
34 | ||
bdccc4fe WD |
35 | #include <post.h> |
36 | ||
8bde7f77 WD |
37 | #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY) |
38 | extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */ | |
39 | #endif | |
40 | ||
41 | extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); | |
42 | ||
43 | ||
c609719b WD |
44 | #define MAX_DELAY_STOP_STR 32 |
45 | ||
46 | static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen); | |
47 | static int parse_line (char *, char *[]); | |
48 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) | |
49 | static int abortboot(int); | |
50 | #endif | |
51 | ||
52 | #undef DEBUG_PARSER | |
53 | ||
54 | char console_buffer[CFG_CBSIZE]; /* console I/O buffer */ | |
55 | ||
56 | static char erase_seq[] = "\b \b"; /* erase sequence */ | |
57 | static char tab_seq[] = " "; /* used to expand TABs */ | |
58 | ||
59 | #ifdef CONFIG_BOOT_RETRY_TIME | |
60 | static uint64_t endtime = 0; /* must be set, default is instant timeout */ | |
61 | static int retry_time = -1; /* -1 so can call readline before main_loop */ | |
62 | #endif | |
63 | ||
64 | #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk()) | |
65 | ||
66 | #ifndef CONFIG_BOOT_RETRY_MIN | |
67 | #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME | |
68 | #endif | |
69 | ||
70 | #ifdef CONFIG_MODEM_SUPPORT | |
71 | int do_mdm_init = 0; | |
72 | extern void mdm_init(void); /* defined in board.c */ | |
73 | #endif | |
74 | ||
75 | /*************************************************************************** | |
76 | * Watch for 'delay' seconds for autoboot stop or autoboot delay string. | |
77 | * returns: 0 - no key string, allow autoboot | |
78 | * 1 - got key string, abort | |
79 | */ | |
80 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) | |
81 | # if defined(CONFIG_AUTOBOOT_KEYED) | |
82 | static __inline__ int abortboot(int bootdelay) | |
83 | { | |
84 | int abort = 0; | |
85 | uint64_t etime = endtick(bootdelay); | |
86 | struct | |
87 | { | |
88 | char* str; | |
89 | u_int len; | |
90 | int retry; | |
91 | } | |
92 | delaykey [] = | |
93 | { | |
94 | { str: getenv ("bootdelaykey"), retry: 1 }, | |
95 | { str: getenv ("bootdelaykey2"), retry: 1 }, | |
96 | { str: getenv ("bootstopkey"), retry: 0 }, | |
97 | { str: getenv ("bootstopkey2"), retry: 0 }, | |
98 | }; | |
99 | ||
100 | char presskey [MAX_DELAY_STOP_STR]; | |
101 | u_int presskey_len = 0; | |
102 | u_int presskey_max = 0; | |
103 | u_int i; | |
104 | ||
8cb8143e | 105 | #ifdef CONFIG_SILENT_CONSOLE |
106 | { | |
107 | DECLARE_GLOBAL_DATA_PTR; | |
108 | ||
109 | if (gd->flags & GD_FLG_SILENT) { | |
110 | /* Restore serial console */ | |
111 | console_assign (stdout, "serial"); | |
112 | console_assign (stderr, "serial"); | |
113 | } | |
114 | } | |
115 | #endif | |
116 | ||
c609719b WD |
117 | # ifdef CONFIG_AUTOBOOT_PROMPT |
118 | printf (CONFIG_AUTOBOOT_PROMPT, bootdelay); | |
119 | # endif | |
120 | ||
121 | # ifdef CONFIG_AUTOBOOT_DELAY_STR | |
122 | if (delaykey[0].str == NULL) | |
123 | delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR; | |
124 | # endif | |
125 | # ifdef CONFIG_AUTOBOOT_DELAY_STR2 | |
126 | if (delaykey[1].str == NULL) | |
127 | delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2; | |
128 | # endif | |
129 | # ifdef CONFIG_AUTOBOOT_STOP_STR | |
130 | if (delaykey[2].str == NULL) | |
131 | delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR; | |
132 | # endif | |
133 | # ifdef CONFIG_AUTOBOOT_STOP_STR2 | |
134 | if (delaykey[3].str == NULL) | |
135 | delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2; | |
136 | # endif | |
137 | ||
138 | for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) { | |
139 | delaykey[i].len = delaykey[i].str == NULL ? | |
140 | 0 : strlen (delaykey[i].str); | |
141 | delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ? | |
142 | MAX_DELAY_STOP_STR : delaykey[i].len; | |
143 | ||
144 | presskey_max = presskey_max > delaykey[i].len ? | |
145 | presskey_max : delaykey[i].len; | |
146 | ||
147 | # if DEBUG_BOOTKEYS | |
148 | printf("%s key:<%s>\n", | |
149 | delaykey[i].retry ? "delay" : "stop", | |
150 | delaykey[i].str ? delaykey[i].str : "NULL"); | |
151 | # endif | |
152 | } | |
153 | ||
154 | /* In order to keep up with incoming data, check timeout only | |
155 | * when catch up. | |
156 | */ | |
157 | while (!abort && get_ticks() <= etime) { | |
158 | for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) { | |
159 | if (delaykey[i].len > 0 && | |
160 | presskey_len >= delaykey[i].len && | |
161 | memcmp (presskey + presskey_len - delaykey[i].len, | |
8bde7f77 | 162 | delaykey[i].str, |
c609719b WD |
163 | delaykey[i].len) == 0) { |
164 | # if DEBUG_BOOTKEYS | |
165 | printf("got %skey\n", | |
166 | delaykey[i].retry ? "delay" : "stop"); | |
167 | # endif | |
168 | ||
169 | # ifdef CONFIG_BOOT_RETRY_TIME | |
170 | /* don't retry auto boot */ | |
171 | if (! delaykey[i].retry) | |
172 | retry_time = -1; | |
173 | # endif | |
174 | abort = 1; | |
175 | } | |
176 | } | |
177 | ||
178 | if (tstc()) { | |
179 | if (presskey_len < presskey_max) { | |
180 | presskey [presskey_len ++] = getc(); | |
181 | } | |
182 | else { | |
183 | for (i = 0; i < presskey_max - 1; i ++) | |
184 | presskey [i] = presskey [i + 1]; | |
185 | ||
186 | presskey [i] = getc(); | |
187 | } | |
188 | } | |
189 | } | |
190 | # if DEBUG_BOOTKEYS | |
191 | if (!abort) | |
192 | printf("key timeout\n"); | |
193 | # endif | |
194 | ||
8cb8143e | 195 | #ifdef CONFIG_SILENT_CONSOLE |
196 | { | |
197 | DECLARE_GLOBAL_DATA_PTR; | |
198 | ||
199 | if (abort) { | |
200 | /* permanently enable normal console output */ | |
201 | gd->flags &= ~(GD_FLG_SILENT); | |
202 | } else if (gd->flags & GD_FLG_SILENT) { | |
203 | /* Restore silent console */ | |
204 | console_assign (stdout, "nulldev"); | |
205 | console_assign (stderr, "nulldev"); | |
206 | } | |
207 | } | |
208 | #endif | |
209 | ||
c609719b WD |
210 | return abort; |
211 | } | |
212 | ||
213 | # else /* !defined(CONFIG_AUTOBOOT_KEYED) */ | |
214 | ||
c7de829c WD |
215 | #ifdef CONFIG_MENUKEY |
216 | static int menukey = 0; | |
217 | #endif | |
218 | ||
c609719b WD |
219 | static __inline__ int abortboot(int bootdelay) |
220 | { | |
221 | int abort = 0; | |
222 | ||
f72da340 WD |
223 | #ifdef CONFIG_SILENT_CONSOLE |
224 | { | |
225 | DECLARE_GLOBAL_DATA_PTR; | |
226 | ||
227 | if (gd->flags & GD_FLG_SILENT) { | |
228 | /* Restore serial console */ | |
229 | console_assign (stdout, "serial"); | |
230 | console_assign (stderr, "serial"); | |
231 | } | |
232 | } | |
233 | #endif | |
234 | ||
c7de829c WD |
235 | #ifdef CONFIG_MENUPROMPT |
236 | printf(CONFIG_MENUPROMPT, bootdelay); | |
237 | #else | |
c609719b | 238 | printf("Hit any key to stop autoboot: %2d ", bootdelay); |
c7de829c | 239 | #endif |
c609719b WD |
240 | |
241 | #if defined CONFIG_ZERO_BOOTDELAY_CHECK | |
8bde7f77 WD |
242 | /* |
243 | * Check if key already pressed | |
244 | * Don't check if bootdelay < 0 | |
245 | */ | |
c609719b WD |
246 | if (bootdelay >= 0) { |
247 | if (tstc()) { /* we got a key press */ | |
248 | (void) getc(); /* consume input */ | |
f72da340 WD |
249 | printf ("\b\b\b 0"); |
250 | abort = 1; /* don't auto boot */ | |
c609719b | 251 | } |
8bde7f77 | 252 | } |
c609719b WD |
253 | #endif |
254 | ||
f72da340 | 255 | while ((bootdelay > 0) && (!abort)) { |
c609719b WD |
256 | int i; |
257 | ||
258 | --bootdelay; | |
259 | /* delay 100 * 10ms */ | |
260 | for (i=0; !abort && i<100; ++i) { | |
261 | if (tstc()) { /* we got a key press */ | |
262 | abort = 1; /* don't auto boot */ | |
263 | bootdelay = 0; /* no more delay */ | |
c7de829c WD |
264 | # ifdef CONFIG_MENUKEY |
265 | menukey = getc(); | |
266 | # else | |
c609719b | 267 | (void) getc(); /* consume input */ |
c7de829c | 268 | # endif |
c609719b WD |
269 | break; |
270 | } | |
271 | udelay (10000); | |
272 | } | |
273 | ||
274 | printf ("\b\b\b%2d ", bootdelay); | |
275 | } | |
276 | ||
277 | putc ('\n'); | |
278 | ||
f72da340 WD |
279 | #ifdef CONFIG_SILENT_CONSOLE |
280 | { | |
281 | DECLARE_GLOBAL_DATA_PTR; | |
282 | ||
283 | if (abort) { | |
284 | /* permanently enable normal console output */ | |
285 | gd->flags &= ~(GD_FLG_SILENT); | |
286 | } else if (gd->flags & GD_FLG_SILENT) { | |
287 | /* Restore silent console */ | |
288 | console_assign (stdout, "nulldev"); | |
289 | console_assign (stderr, "nulldev"); | |
290 | } | |
291 | } | |
292 | #endif | |
293 | ||
c609719b WD |
294 | return abort; |
295 | } | |
296 | # endif /* CONFIG_AUTOBOOT_KEYED */ | |
297 | #endif /* CONFIG_BOOTDELAY >= 0 */ | |
298 | ||
299 | /****************************************************************************/ | |
300 | ||
301 | void main_loop (void) | |
302 | { | |
303 | #ifndef CFG_HUSH_PARSER | |
304 | static char lastcommand[CFG_CBSIZE] = { 0, }; | |
305 | int len; | |
306 | int rc = 1; | |
307 | int flag; | |
308 | #endif | |
309 | ||
310 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) | |
311 | char *s; | |
312 | int bootdelay; | |
313 | #endif | |
314 | #ifdef CONFIG_PREBOOT | |
315 | char *p; | |
316 | #endif | |
bdccc4fe WD |
317 | #ifdef CONFIG_BOOTCOUNT_LIMIT |
318 | unsigned long bootcount = 0; | |
319 | unsigned long bootlimit = 0; | |
320 | char *bcs; | |
321 | char bcs_set[16]; | |
322 | #endif /* CONFIG_BOOTCOUNT_LIMIT */ | |
c609719b WD |
323 | |
324 | #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO) | |
325 | ulong bmp = 0; /* default bitmap */ | |
326 | extern int trab_vfd (ulong bitmap); | |
327 | ||
328 | #ifdef CONFIG_MODEM_SUPPORT | |
329 | if (do_mdm_init) | |
330 | bmp = 1; /* alternate bitmap */ | |
331 | #endif | |
332 | trab_vfd (bmp); | |
333 | #endif /* CONFIG_VFD && VFD_TEST_LOGO */ | |
334 | ||
bdccc4fe WD |
335 | #ifdef CONFIG_BOOTCOUNT_LIMIT |
336 | bootcount = bootcount_load(); | |
337 | bootcount++; | |
338 | bootcount_store (bootcount); | |
339 | sprintf (bcs_set, "%lu", bootcount); | |
340 | setenv ("bootcount", bcs_set); | |
341 | bcs = getenv ("bootlimit"); | |
342 | bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0; | |
343 | #endif /* CONFIG_BOOTCOUNT_LIMIT */ | |
344 | ||
c609719b WD |
345 | #ifdef CONFIG_MODEM_SUPPORT |
346 | debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init); | |
347 | if (do_mdm_init) { | |
348 | uchar *str = strdup(getenv("mdm_cmd")); | |
349 | setenv ("preboot", str); /* set or delete definition */ | |
350 | if (str != NULL) | |
351 | free (str); | |
352 | mdm_init(); /* wait for modem connection */ | |
353 | } | |
354 | #endif /* CONFIG_MODEM_SUPPORT */ | |
355 | ||
0587597c SR |
356 | #ifdef CONFIG_VERSION_VARIABLE |
357 | { | |
358 | extern char version_string[]; | |
0587597c | 359 | |
155cb010 | 360 | setenv ("ver", version_string); /* set version variable */ |
0587597c SR |
361 | } |
362 | #endif /* CONFIG_VERSION_VARIABLE */ | |
363 | ||
c609719b WD |
364 | #ifdef CFG_HUSH_PARSER |
365 | u_boot_hush_start (); | |
366 | #endif | |
367 | ||
368 | #ifdef CONFIG_PREBOOT | |
369 | if ((p = getenv ("preboot")) != NULL) { | |
370 | # ifdef CONFIG_AUTOBOOT_KEYED | |
371 | int prev = disable_ctrlc(1); /* disable Control C checking */ | |
372 | # endif | |
373 | ||
374 | # ifndef CFG_HUSH_PARSER | |
375 | run_command (p, 0); | |
376 | # else | |
377 | parse_string_outer(p, FLAG_PARSE_SEMICOLON | | |
378 | FLAG_EXIT_FROM_LOOP); | |
379 | # endif | |
380 | ||
381 | # ifdef CONFIG_AUTOBOOT_KEYED | |
382 | disable_ctrlc(prev); /* restore Control C checking */ | |
383 | # endif | |
384 | } | |
385 | #endif /* CONFIG_PREBOOT */ | |
386 | ||
387 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) | |
388 | s = getenv ("bootdelay"); | |
389 | bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY; | |
390 | ||
a6c7ad2f | 391 | debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay); |
c609719b WD |
392 | |
393 | # ifdef CONFIG_BOOT_RETRY_TIME | |
6dd652fa | 394 | init_cmd_timeout (); |
c609719b WD |
395 | # endif /* CONFIG_BOOT_RETRY_TIME */ |
396 | ||
bdccc4fe WD |
397 | #ifdef CONFIG_BOOTCOUNT_LIMIT |
398 | if (bootlimit && (bootcount > bootlimit)) { | |
399 | printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n", | |
400 | (unsigned)bootlimit); | |
401 | s = getenv ("altbootcmd"); | |
402 | } | |
403 | else | |
404 | #endif /* CONFIG_BOOTCOUNT_LIMIT */ | |
405 | s = getenv ("bootcmd"); | |
a6c7ad2f WD |
406 | |
407 | debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>"); | |
408 | ||
c609719b WD |
409 | if (bootdelay >= 0 && s && !abortboot (bootdelay)) { |
410 | # ifdef CONFIG_AUTOBOOT_KEYED | |
411 | int prev = disable_ctrlc(1); /* disable Control C checking */ | |
412 | # endif | |
413 | ||
414 | # ifndef CFG_HUSH_PARSER | |
415 | run_command (s, 0); | |
416 | # else | |
417 | parse_string_outer(s, FLAG_PARSE_SEMICOLON | | |
418 | FLAG_EXIT_FROM_LOOP); | |
419 | # endif | |
420 | ||
421 | # ifdef CONFIG_AUTOBOOT_KEYED | |
422 | disable_ctrlc(prev); /* restore Control C checking */ | |
423 | # endif | |
424 | } | |
c7de829c WD |
425 | |
426 | # ifdef CONFIG_MENUKEY | |
a6c7ad2f | 427 | if (menukey == CONFIG_MENUKEY) { |
c7de829c | 428 | s = getenv("menucmd"); |
a6c7ad2f | 429 | if (s) { |
c7de829c WD |
430 | # ifndef CFG_HUSH_PARSER |
431 | run_command (s, bd, 0); | |
432 | # else | |
433 | parse_string_outer(s, FLAG_PARSE_SEMICOLON | | |
434 | FLAG_EXIT_FROM_LOOP); | |
435 | # endif | |
436 | } | |
437 | } | |
438 | #endif /* CONFIG_MENUKEY */ | |
c609719b WD |
439 | #endif /* CONFIG_BOOTDELAY */ |
440 | ||
c7de829c WD |
441 | #ifdef CONFIG_AMIGAONEG3SE |
442 | { | |
443 | extern void video_banner(void); | |
444 | video_banner(); | |
445 | } | |
446 | #endif | |
447 | ||
c609719b WD |
448 | /* |
449 | * Main Loop for Monitor Command Processing | |
450 | */ | |
451 | #ifdef CFG_HUSH_PARSER | |
452 | parse_file_outer(); | |
453 | /* This point is never reached */ | |
454 | for (;;); | |
455 | #else | |
456 | for (;;) { | |
457 | #ifdef CONFIG_BOOT_RETRY_TIME | |
458 | if (rc >= 0) { | |
459 | /* Saw enough of a valid command to | |
460 | * restart the timeout. | |
461 | */ | |
462 | reset_cmd_timeout(); | |
463 | } | |
464 | #endif | |
465 | len = readline (CFG_PROMPT); | |
466 | ||
467 | flag = 0; /* assume no special flags for now */ | |
468 | if (len > 0) | |
469 | strcpy (lastcommand, console_buffer); | |
470 | else if (len == 0) | |
471 | flag |= CMD_FLAG_REPEAT; | |
472 | #ifdef CONFIG_BOOT_RETRY_TIME | |
473 | else if (len == -2) { | |
474 | /* -2 means timed out, retry autoboot | |
475 | */ | |
476 | printf("\nTimed out waiting for command\n"); | |
477 | # ifdef CONFIG_RESET_TO_RETRY | |
478 | /* Reinit board to run initialization code again */ | |
479 | do_reset (NULL, 0, 0, NULL); | |
480 | # else | |
481 | return; /* retry autoboot */ | |
482 | # endif | |
483 | } | |
484 | #endif | |
485 | ||
486 | if (len == -1) | |
487 | printf ("<INTERRUPT>\n"); | |
488 | else | |
489 | rc = run_command (lastcommand, flag); | |
490 | ||
491 | if (rc <= 0) { | |
492 | /* invalid command or not repeatable, forget it */ | |
493 | lastcommand[0] = 0; | |
494 | } | |
495 | } | |
496 | #endif /*CFG_HUSH_PARSER*/ | |
497 | } | |
498 | ||
6dd652fa WD |
499 | #ifdef CONFIG_BOOT_RETRY_TIME |
500 | /*************************************************************************** | |
501 | * initialise command line timeout | |
502 | */ | |
503 | void init_cmd_timeout(void) | |
504 | { | |
505 | char *s = getenv ("bootretry"); | |
506 | ||
507 | if (s != NULL) | |
b028f715 | 508 | retry_time = (int)simple_strtol(s, NULL, 10); |
6dd652fa WD |
509 | else |
510 | retry_time = CONFIG_BOOT_RETRY_TIME; | |
511 | ||
512 | if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN) | |
513 | retry_time = CONFIG_BOOT_RETRY_MIN; | |
514 | } | |
515 | ||
c609719b WD |
516 | /*************************************************************************** |
517 | * reset command line timeout to retry_time seconds | |
518 | */ | |
c609719b WD |
519 | void reset_cmd_timeout(void) |
520 | { | |
521 | endtime = endtick(retry_time); | |
522 | } | |
523 | #endif | |
524 | ||
525 | /****************************************************************************/ | |
526 | ||
527 | /* | |
528 | * Prompt for input and read a line. | |
529 | * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0, | |
530 | * time out when time goes past endtime (timebase time in ticks). | |
531 | * Return: number of read characters | |
532 | * -1 if break | |
533 | * -2 if timed out | |
534 | */ | |
535 | int readline (const char *const prompt) | |
536 | { | |
537 | char *p = console_buffer; | |
538 | int n = 0; /* buffer index */ | |
539 | int plen = 0; /* prompt length */ | |
540 | int col; /* output column cnt */ | |
541 | char c; | |
542 | ||
543 | /* print prompt */ | |
544 | if (prompt) { | |
545 | plen = strlen (prompt); | |
546 | puts (prompt); | |
547 | } | |
548 | col = plen; | |
549 | ||
550 | for (;;) { | |
551 | #ifdef CONFIG_BOOT_RETRY_TIME | |
552 | while (!tstc()) { /* while no incoming data */ | |
553 | if (retry_time >= 0 && get_ticks() > endtime) | |
554 | return (-2); /* timed out */ | |
555 | } | |
556 | #endif | |
557 | WATCHDOG_RESET(); /* Trigger watchdog, if needed */ | |
558 | ||
559 | #ifdef CONFIG_SHOW_ACTIVITY | |
560 | while (!tstc()) { | |
561 | extern void show_activity(int arg); | |
562 | show_activity(0); | |
563 | } | |
564 | #endif | |
565 | c = getc(); | |
566 | ||
567 | /* | |
568 | * Special character handling | |
569 | */ | |
570 | switch (c) { | |
571 | case '\r': /* Enter */ | |
572 | case '\n': | |
573 | *p = '\0'; | |
574 | puts ("\r\n"); | |
575 | return (p - console_buffer); | |
576 | ||
577 | case 0x03: /* ^C - break */ | |
578 | console_buffer[0] = '\0'; /* discard input */ | |
579 | return (-1); | |
580 | ||
581 | case 0x15: /* ^U - erase line */ | |
582 | while (col > plen) { | |
583 | puts (erase_seq); | |
584 | --col; | |
585 | } | |
586 | p = console_buffer; | |
587 | n = 0; | |
588 | continue; | |
589 | ||
590 | case 0x17: /* ^W - erase word */ | |
591 | p=delete_char(console_buffer, p, &col, &n, plen); | |
592 | while ((n > 0) && (*p != ' ')) { | |
593 | p=delete_char(console_buffer, p, &col, &n, plen); | |
594 | } | |
595 | continue; | |
596 | ||
597 | case 0x08: /* ^H - backspace */ | |
598 | case 0x7F: /* DEL - backspace */ | |
599 | p=delete_char(console_buffer, p, &col, &n, plen); | |
600 | continue; | |
601 | ||
602 | default: | |
603 | /* | |
604 | * Must be a normal character then | |
605 | */ | |
606 | if (n < CFG_CBSIZE-2) { | |
607 | if (c == '\t') { /* expand TABs */ | |
608 | puts (tab_seq+(col&07)); | |
609 | col += 8 - (col&07); | |
610 | } else { | |
611 | ++col; /* echo input */ | |
612 | putc (c); | |
613 | } | |
614 | *p++ = c; | |
615 | ++n; | |
616 | } else { /* Buffer full */ | |
617 | putc ('\a'); | |
618 | } | |
619 | } | |
620 | } | |
621 | } | |
622 | ||
623 | /****************************************************************************/ | |
624 | ||
625 | static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen) | |
626 | { | |
627 | char *s; | |
628 | ||
629 | if (*np == 0) { | |
630 | return (p); | |
631 | } | |
632 | ||
633 | if (*(--p) == '\t') { /* will retype the whole line */ | |
634 | while (*colp > plen) { | |
635 | puts (erase_seq); | |
636 | (*colp)--; | |
637 | } | |
638 | for (s=buffer; s<p; ++s) { | |
639 | if (*s == '\t') { | |
640 | puts (tab_seq+((*colp) & 07)); | |
641 | *colp += 8 - ((*colp) & 07); | |
642 | } else { | |
643 | ++(*colp); | |
644 | putc (*s); | |
645 | } | |
646 | } | |
647 | } else { | |
648 | puts (erase_seq); | |
649 | (*colp)--; | |
650 | } | |
651 | (*np)--; | |
652 | return (p); | |
653 | } | |
654 | ||
655 | /****************************************************************************/ | |
656 | ||
657 | int parse_line (char *line, char *argv[]) | |
658 | { | |
659 | int nargs = 0; | |
660 | ||
661 | #ifdef DEBUG_PARSER | |
662 | printf ("parse_line: \"%s\"\n", line); | |
663 | #endif | |
664 | while (nargs < CFG_MAXARGS) { | |
665 | ||
666 | /* skip any white space */ | |
667 | while ((*line == ' ') || (*line == '\t')) { | |
668 | ++line; | |
669 | } | |
670 | ||
671 | if (*line == '\0') { /* end of line, no more args */ | |
672 | argv[nargs] = NULL; | |
673 | #ifdef DEBUG_PARSER | |
674 | printf ("parse_line: nargs=%d\n", nargs); | |
675 | #endif | |
676 | return (nargs); | |
677 | } | |
678 | ||
679 | argv[nargs++] = line; /* begin of argument string */ | |
680 | ||
681 | /* find end of string */ | |
682 | while (*line && (*line != ' ') && (*line != '\t')) { | |
683 | ++line; | |
684 | } | |
685 | ||
686 | if (*line == '\0') { /* end of line, no more args */ | |
687 | argv[nargs] = NULL; | |
688 | #ifdef DEBUG_PARSER | |
689 | printf ("parse_line: nargs=%d\n", nargs); | |
690 | #endif | |
691 | return (nargs); | |
692 | } | |
693 | ||
694 | *line++ = '\0'; /* terminate current arg */ | |
695 | } | |
696 | ||
697 | printf ("** Too many args (max. %d) **\n", CFG_MAXARGS); | |
698 | ||
699 | #ifdef DEBUG_PARSER | |
700 | printf ("parse_line: nargs=%d\n", nargs); | |
701 | #endif | |
702 | return (nargs); | |
703 | } | |
704 | ||
705 | /****************************************************************************/ | |
706 | ||
707 | static void process_macros (const char *input, char *output) | |
708 | { | |
709 | char c, prev; | |
710 | const char *varname_start = NULL; | |
711 | int inputcnt = strlen (input); | |
712 | int outputcnt = CFG_CBSIZE; | |
713 | int state = 0; /* 0 = waiting for '$' */ | |
714 | /* 1 = waiting for '(' */ | |
715 | /* 2 = waiting for ')' */ | |
8bde7f77 | 716 | /* 3 = waiting for ''' */ |
c609719b WD |
717 | #ifdef DEBUG_PARSER |
718 | char *output_start = output; | |
719 | ||
720 | printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input); | |
721 | #endif | |
722 | ||
723 | prev = '\0'; /* previous character */ | |
724 | ||
725 | while (inputcnt && outputcnt) { | |
726 | c = *input++; | |
727 | inputcnt--; | |
728 | ||
a25f862b | 729 | if (state!=3) { |
c609719b WD |
730 | /* remove one level of escape characters */ |
731 | if ((c == '\\') && (prev != '\\')) { | |
732 | if (inputcnt-- == 0) | |
733 | break; | |
734 | prev = c; | |
8bde7f77 | 735 | c = *input++; |
c609719b | 736 | } |
a25f862b | 737 | } |
c609719b WD |
738 | |
739 | switch (state) { | |
740 | case 0: /* Waiting for (unescaped) $ */ | |
a25f862b WD |
741 | if ((c == '\'') && (prev != '\\')) { |
742 | state = 3; | |
a25f862b WD |
743 | break; |
744 | } | |
c609719b WD |
745 | if ((c == '$') && (prev != '\\')) { |
746 | state++; | |
747 | } else { | |
748 | *(output++) = c; | |
749 | outputcnt--; | |
750 | } | |
751 | break; | |
752 | case 1: /* Waiting for ( */ | |
753 | if (c == '(') { | |
754 | state++; | |
755 | varname_start = input; | |
756 | } else { | |
757 | state = 0; | |
758 | *(output++) = '$'; | |
759 | outputcnt--; | |
760 | ||
761 | if (outputcnt) { | |
762 | *(output++) = c; | |
763 | outputcnt--; | |
764 | } | |
765 | } | |
766 | break; | |
767 | case 2: /* Waiting for ) */ | |
768 | if (c == ')') { | |
769 | int i; | |
770 | char envname[CFG_CBSIZE], *envval; | |
771 | int envcnt = input-varname_start-1; /* Varname # of chars */ | |
772 | ||
773 | /* Get the varname */ | |
774 | for (i = 0; i < envcnt; i++) { | |
775 | envname[i] = varname_start[i]; | |
776 | } | |
777 | envname[i] = 0; | |
778 | ||
779 | /* Get its value */ | |
780 | envval = getenv (envname); | |
781 | ||
782 | /* Copy into the line if it exists */ | |
783 | if (envval != NULL) | |
784 | while ((*envval) && outputcnt) { | |
785 | *(output++) = *(envval++); | |
786 | outputcnt--; | |
787 | } | |
788 | /* Look for another '$' */ | |
789 | state = 0; | |
790 | } | |
791 | break; | |
a25f862b WD |
792 | case 3: /* Waiting for ' */ |
793 | if ((c == '\'') && (prev != '\\')) { | |
794 | state = 0; | |
a25f862b WD |
795 | } else { |
796 | *(output++) = c; | |
797 | outputcnt--; | |
798 | } | |
799 | break; | |
c609719b | 800 | } |
c609719b WD |
801 | prev = c; |
802 | } | |
803 | ||
804 | if (outputcnt) | |
805 | *output = 0; | |
806 | ||
807 | #ifdef DEBUG_PARSER | |
808 | printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n", | |
809 | strlen(output_start), output_start); | |
810 | #endif | |
811 | } | |
812 | ||
813 | /**************************************************************************** | |
814 | * returns: | |
815 | * 1 - command executed, repeatable | |
816 | * 0 - command executed but not repeatable, interrupted commands are | |
817 | * always considered not repeatable | |
818 | * -1 - not executed (unrecognized, bootd recursion or too many args) | |
819 | * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is | |
820 | * considered unrecognized) | |
821 | * | |
822 | * WARNING: | |
823 | * | |
824 | * We must create a temporary copy of the command since the command we get | |
825 | * may be the result from getenv(), which returns a pointer directly to | |
826 | * the environment data, which may change magicly when the command we run | |
827 | * creates or modifies environment variables (like "bootp" does). | |
828 | */ | |
829 | ||
830 | int run_command (const char *cmd, int flag) | |
831 | { | |
832 | cmd_tbl_t *cmdtp; | |
833 | char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */ | |
834 | char *token; /* start of token in cmdbuf */ | |
835 | char *sep; /* end of token (separator) in cmdbuf */ | |
836 | char finaltoken[CFG_CBSIZE]; | |
837 | char *str = cmdbuf; | |
838 | char *argv[CFG_MAXARGS + 1]; /* NULL terminated */ | |
f07771cc | 839 | int argc, inquotes; |
c609719b | 840 | int repeatable = 1; |
f07771cc | 841 | int rc = 0; |
c609719b WD |
842 | |
843 | #ifdef DEBUG_PARSER | |
844 | printf ("[RUN_COMMAND] cmd[%p]=\"", cmd); | |
845 | puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */ | |
846 | puts ("\"\n"); | |
847 | #endif | |
848 | ||
849 | clear_ctrlc(); /* forget any previous Control C */ | |
850 | ||
851 | if (!cmd || !*cmd) { | |
852 | return -1; /* empty command */ | |
853 | } | |
854 | ||
855 | if (strlen(cmd) >= CFG_CBSIZE) { | |
856 | puts ("## Command too long!\n"); | |
857 | return -1; | |
858 | } | |
859 | ||
860 | strcpy (cmdbuf, cmd); | |
861 | ||
862 | /* Process separators and check for invalid | |
863 | * repeatable commands | |
864 | */ | |
865 | ||
866 | #ifdef DEBUG_PARSER | |
867 | printf ("[PROCESS_SEPARATORS] %s\n", cmd); | |
868 | #endif | |
869 | while (*str) { | |
870 | ||
871 | /* | |
872 | * Find separator, or string end | |
873 | * Allow simple escape of ';' by writing "\;" | |
874 | */ | |
a25f862b WD |
875 | for (inquotes = 0, sep = str; *sep; sep++) { |
876 | if ((*sep=='\'') && | |
877 | (*(sep-1) != '\\')) | |
878 | inquotes=!inquotes; | |
879 | ||
880 | if (!inquotes && | |
881 | (*sep == ';') && /* separator */ | |
c609719b WD |
882 | ( sep != str) && /* past string start */ |
883 | (*(sep-1) != '\\')) /* and NOT escaped */ | |
884 | break; | |
885 | } | |
886 | ||
887 | /* | |
888 | * Limit the token to data between separators | |
889 | */ | |
890 | token = str; | |
891 | if (*sep) { | |
892 | str = sep + 1; /* start of command for next pass */ | |
893 | *sep = '\0'; | |
894 | } | |
895 | else | |
896 | str = sep; /* no more commands for next pass */ | |
897 | #ifdef DEBUG_PARSER | |
898 | printf ("token: \"%s\"\n", token); | |
899 | #endif | |
900 | ||
901 | /* find macros in this token and replace them */ | |
902 | process_macros (token, finaltoken); | |
903 | ||
904 | /* Extract arguments */ | |
905 | argc = parse_line (finaltoken, argv); | |
906 | ||
907 | /* Look up command in command table */ | |
908 | if ((cmdtp = find_cmd(argv[0])) == NULL) { | |
909 | printf ("Unknown command '%s' - try 'help'\n", argv[0]); | |
f07771cc WD |
910 | rc = -1; /* give up after bad command */ |
911 | continue; | |
c609719b WD |
912 | } |
913 | ||
914 | /* found - check max args */ | |
915 | if (argc > cmdtp->maxargs) { | |
916 | printf ("Usage:\n%s\n", cmdtp->usage); | |
f07771cc WD |
917 | rc = -1; |
918 | continue; | |
c609719b WD |
919 | } |
920 | ||
921 | #if (CONFIG_COMMANDS & CFG_CMD_BOOTD) | |
922 | /* avoid "bootd" recursion */ | |
923 | if (cmdtp->cmd == do_bootd) { | |
924 | #ifdef DEBUG_PARSER | |
925 | printf ("[%s]\n", finaltoken); | |
926 | #endif | |
927 | if (flag & CMD_FLAG_BOOTD) { | |
928 | printf ("'bootd' recursion detected\n"); | |
f07771cc WD |
929 | rc = -1; |
930 | continue; | |
c609719b WD |
931 | } |
932 | else | |
933 | flag |= CMD_FLAG_BOOTD; | |
934 | } | |
935 | #endif /* CFG_CMD_BOOTD */ | |
936 | ||
937 | /* OK - call function to do the command */ | |
938 | if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) { | |
f07771cc | 939 | rc = -1; |
c609719b WD |
940 | } |
941 | ||
942 | repeatable &= cmdtp->repeatable; | |
943 | ||
944 | /* Did the user stop this? */ | |
945 | if (had_ctrlc ()) | |
946 | return 0; /* if stopped then not repeatable */ | |
947 | } | |
948 | ||
f07771cc | 949 | return rc ? rc : repeatable; |
c609719b WD |
950 | } |
951 | ||
952 | /****************************************************************************/ | |
953 | ||
954 | #if (CONFIG_COMMANDS & CFG_CMD_RUN) | |
955 | int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) | |
956 | { | |
957 | int i; | |
c609719b WD |
958 | |
959 | if (argc < 2) { | |
960 | printf ("Usage:\n%s\n", cmdtp->usage); | |
961 | return 1; | |
962 | } | |
963 | ||
964 | for (i=1; i<argc; ++i) { | |
3e38691e WD |
965 | char *arg; |
966 | ||
967 | if ((arg = getenv (argv[i])) == NULL) { | |
968 | printf ("## Error: \"%s\" not defined\n", argv[i]); | |
969 | return 1; | |
970 | } | |
c609719b | 971 | #ifndef CFG_HUSH_PARSER |
3e38691e WD |
972 | if (run_command (arg, flag) == -1) |
973 | return 1; | |
c609719b | 974 | #else |
3e38691e | 975 | if (parse_string_outer(arg, |
7aa78614 | 976 | FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0) |
3e38691e | 977 | return 1; |
c609719b WD |
978 | #endif |
979 | } | |
3e38691e | 980 | return 0; |
c609719b | 981 | } |
3e38691e | 982 | #endif /* CFG_CMD_RUN */ |