blob: 3324d9d6e433a9b91efee84eea73e4f5f2d49cc6 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk501090a2006-07-21 11:33:45 +02005 * Add to readline cmdline-editing by
6 * (C) Copyright 2005
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
8 *
wdenkc6097192002-11-03 00:24:07 +00009 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
wdenka6c7ad22002-12-03 21:28:10 +000028/* #define DEBUG */
29
wdenkc6097192002-11-03 00:24:07 +000030#include <common.h>
31#include <watchdog.h>
32#include <command.h>
Andreas Bießmann09c2e902011-07-18 20:24:04 +020033#include <version.h>
Wolfgang Denkd9631ec2005-09-30 15:18:23 +020034#ifdef CONFIG_MODEM_SUPPORT
35#include <malloc.h> /* for free() prototype */
36#endif
wdenkc6097192002-11-03 00:24:07 +000037
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020038#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +000039#include <hush.h>
40#endif
41
wdenkbdccc4f2003-08-05 17:43:17 +000042#include <post.h>
43
James Yang597f6c22008-05-05 10:22:53 -050044#if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
Wolfgang Denkd87080b2006-03-31 18:32:53 +020045DECLARE_GLOBAL_DATA_PTR;
46#endif
47
Heiko Schocherfad63402007-07-13 09:54:17 +020048/*
49 * Board-specific Platform code can reimplement show_boot_progress () if needed
50 */
51void inline __show_boot_progress (int val) {}
Emil Medve5e2c08c2009-05-12 13:48:32 -050052void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
Heiko Schocherfad63402007-07-13 09:54:17 +020053
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020054#if defined(CONFIG_UPDATE_TFTP)
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +000055int update_tftp (ulong addr);
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020056#endif /* CONFIG_UPDATE_TFTP */
wdenk8bde7f72003-06-27 21:31:46 +000057
wdenkc6097192002-11-03 00:24:07 +000058#define MAX_DELAY_STOP_STR 32
59
wdenkc6097192002-11-03 00:24:07 +000060#undef DEBUG_PARSER
61
John Schmoller6475b9f2010-03-12 09:49:23 -060062char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
wdenkc6097192002-11-03 00:24:07 +000063
Stefan Roese3ca91222006-07-27 16:11:19 +020064static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
Mike Frysinger82359ae2010-12-22 09:40:45 -050065static const char erase_seq[] = "\b \b"; /* erase sequence */
66static const char tab_seq[] = " "; /* used to expand TABs */
wdenkc6097192002-11-03 00:24:07 +000067
68#ifdef CONFIG_BOOT_RETRY_TIME
69static uint64_t endtime = 0; /* must be set, default is instant timeout */
70static int retry_time = -1; /* -1 so can call readline before main_loop */
71#endif
72
73#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
74
75#ifndef CONFIG_BOOT_RETRY_MIN
76#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
77#endif
78
79#ifdef CONFIG_MODEM_SUPPORT
80int do_mdm_init = 0;
81extern void mdm_init(void); /* defined in board.c */
82#endif
83
84/***************************************************************************
85 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
86 * returns: 0 - no key string, allow autoboot
87 * 1 - got key string, abort
88 */
89#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
90# if defined(CONFIG_AUTOBOOT_KEYED)
Jason Hobbs3c766dc2011-06-29 06:25:14 +000091static inline int abortboot(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +000092{
93 int abort = 0;
94 uint64_t etime = endtick(bootdelay);
Wolfgang Denk19973b62006-10-28 00:38:39 +020095 struct {
wdenkc6097192002-11-03 00:24:07 +000096 char* str;
97 u_int len;
98 int retry;
99 }
Wolfgang Denk19973b62006-10-28 00:38:39 +0200100 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +0000101 { str: getenv ("bootdelaykey"), retry: 1 },
102 { str: getenv ("bootdelaykey2"), retry: 1 },
103 { str: getenv ("bootstopkey"), retry: 0 },
104 { str: getenv ("bootstopkey2"), retry: 0 },
105 };
106
107 char presskey [MAX_DELAY_STOP_STR];
108 u_int presskey_len = 0;
109 u_int presskey_max = 0;
110 u_int i;
111
112# ifdef CONFIG_AUTOBOOT_PROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200113 printf(CONFIG_AUTOBOOT_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000114# endif
115
116# ifdef CONFIG_AUTOBOOT_DELAY_STR
117 if (delaykey[0].str == NULL)
118 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
119# endif
120# ifdef CONFIG_AUTOBOOT_DELAY_STR2
121 if (delaykey[1].str == NULL)
122 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
123# endif
124# ifdef CONFIG_AUTOBOOT_STOP_STR
125 if (delaykey[2].str == NULL)
126 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
127# endif
128# ifdef CONFIG_AUTOBOOT_STOP_STR2
129 if (delaykey[3].str == NULL)
130 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
131# endif
132
133 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
134 delaykey[i].len = delaykey[i].str == NULL ?
135 0 : strlen (delaykey[i].str);
136 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
137 MAX_DELAY_STOP_STR : delaykey[i].len;
138
139 presskey_max = presskey_max > delaykey[i].len ?
140 presskey_max : delaykey[i].len;
141
142# if DEBUG_BOOTKEYS
143 printf("%s key:<%s>\n",
144 delaykey[i].retry ? "delay" : "stop",
145 delaykey[i].str ? delaykey[i].str : "NULL");
146# endif
147 }
148
149 /* In order to keep up with incoming data, check timeout only
150 * when catch up.
151 */
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100152 do {
153 if (tstc()) {
154 if (presskey_len < presskey_max) {
155 presskey [presskey_len ++] = getc();
156 }
157 else {
158 for (i = 0; i < presskey_max - 1; i ++)
159 presskey [i] = presskey [i + 1];
160
161 presskey [i] = getc();
162 }
163 }
164
wdenkc6097192002-11-03 00:24:07 +0000165 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
166 if (delaykey[i].len > 0 &&
167 presskey_len >= delaykey[i].len &&
168 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk8bde7f72003-06-27 21:31:46 +0000169 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000170 delaykey[i].len) == 0) {
171# if DEBUG_BOOTKEYS
172 printf("got %skey\n",
173 delaykey[i].retry ? "delay" : "stop");
174# endif
175
176# ifdef CONFIG_BOOT_RETRY_TIME
177 /* don't retry auto boot */
178 if (! delaykey[i].retry)
179 retry_time = -1;
180# endif
181 abort = 1;
182 }
183 }
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100184 } while (!abort && get_ticks() <= etime);
wdenkc6097192002-11-03 00:24:07 +0000185
wdenkc6097192002-11-03 00:24:07 +0000186# if DEBUG_BOOTKEYS
187 if (!abort)
Ladislav Michlada4d402007-04-25 16:01:26 +0200188 puts("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000189# endif
190
dzu8cb81432003-10-24 13:14:45 +0000191#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200192 if (abort)
193 gd->flags &= ~GD_FLG_SILENT;
dzu8cb81432003-10-24 13:14:45 +0000194#endif
195
wdenkc6097192002-11-03 00:24:07 +0000196 return abort;
197}
198
199# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
200
wdenkc7de8292002-11-19 11:04:11 +0000201#ifdef CONFIG_MENUKEY
202static int menukey = 0;
203#endif
204
Jason Hobbs3c766dc2011-06-29 06:25:14 +0000205static inline int abortboot(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000206{
207 int abort = 0;
208
wdenkc7de8292002-11-19 11:04:11 +0000209#ifdef CONFIG_MENUPROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200210 printf(CONFIG_MENUPROMPT);
wdenkc7de8292002-11-19 11:04:11 +0000211#else
wdenkc6097192002-11-03 00:24:07 +0000212 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenkc7de8292002-11-19 11:04:11 +0000213#endif
wdenkc6097192002-11-03 00:24:07 +0000214
215#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk8bde7f72003-06-27 21:31:46 +0000216 /*
217 * Check if key already pressed
218 * Don't check if bootdelay < 0
219 */
wdenkc6097192002-11-03 00:24:07 +0000220 if (bootdelay >= 0) {
221 if (tstc()) { /* we got a key press */
222 (void) getc(); /* consume input */
wdenk4b9206e2004-03-23 22:14:11 +0000223 puts ("\b\b\b 0");
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200224 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000225 }
wdenk8bde7f72003-06-27 21:31:46 +0000226 }
wdenkc6097192002-11-03 00:24:07 +0000227#endif
228
wdenkf72da342003-10-10 10:05:42 +0000229 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000230 int i;
231
232 --bootdelay;
233 /* delay 100 * 10ms */
234 for (i=0; !abort && i<100; ++i) {
235 if (tstc()) { /* we got a key press */
236 abort = 1; /* don't auto boot */
237 bootdelay = 0; /* no more delay */
wdenkc7de8292002-11-19 11:04:11 +0000238# ifdef CONFIG_MENUKEY
239 menukey = getc();
240# else
wdenkc6097192002-11-03 00:24:07 +0000241 (void) getc(); /* consume input */
wdenkc7de8292002-11-19 11:04:11 +0000242# endif
wdenkc6097192002-11-03 00:24:07 +0000243 break;
244 }
Ladislav Michlada4d402007-04-25 16:01:26 +0200245 udelay(10000);
wdenkc6097192002-11-03 00:24:07 +0000246 }
247
Ladislav Michlada4d402007-04-25 16:01:26 +0200248 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000249 }
250
Ladislav Michlada4d402007-04-25 16:01:26 +0200251 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000252
wdenkf72da342003-10-10 10:05:42 +0000253#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200254 if (abort)
255 gd->flags &= ~GD_FLG_SILENT;
wdenkf72da342003-10-10 10:05:42 +0000256#endif
257
wdenkc6097192002-11-03 00:24:07 +0000258 return abort;
259}
260# endif /* CONFIG_AUTOBOOT_KEYED */
261#endif /* CONFIG_BOOTDELAY >= 0 */
262
263/****************************************************************************/
264
265void main_loop (void)
266{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200267#ifndef CONFIG_SYS_HUSH_PARSER
268 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
wdenkc6097192002-11-03 00:24:07 +0000269 int len;
270 int rc = 1;
271 int flag;
272#endif
273
274#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
275 char *s;
276 int bootdelay;
277#endif
278#ifdef CONFIG_PREBOOT
279 char *p;
280#endif
wdenkbdccc4f2003-08-05 17:43:17 +0000281#ifdef CONFIG_BOOTCOUNT_LIMIT
282 unsigned long bootcount = 0;
283 unsigned long bootlimit = 0;
284 char *bcs;
285 char bcs_set[16];
286#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000287
wdenkbdccc4f2003-08-05 17:43:17 +0000288#ifdef CONFIG_BOOTCOUNT_LIMIT
289 bootcount = bootcount_load();
290 bootcount++;
291 bootcount_store (bootcount);
292 sprintf (bcs_set, "%lu", bootcount);
293 setenv ("bootcount", bcs_set);
294 bcs = getenv ("bootlimit");
295 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
296#endif /* CONFIG_BOOTCOUNT_LIMIT */
297
wdenkc6097192002-11-03 00:24:07 +0000298#ifdef CONFIG_MODEM_SUPPORT
299 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
300 if (do_mdm_init) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200301 char *str = strdup(getenv("mdm_cmd"));
wdenkc6097192002-11-03 00:24:07 +0000302 setenv ("preboot", str); /* set or delete definition */
303 if (str != NULL)
304 free (str);
305 mdm_init(); /* wait for modem connection */
306 }
307#endif /* CONFIG_MODEM_SUPPORT */
308
stroese05875972003-04-04 15:44:49 +0000309#ifdef CONFIG_VERSION_VARIABLE
310 {
stroese155cb012003-07-11 08:00:33 +0000311 setenv ("ver", version_string); /* set version variable */
stroese05875972003-04-04 15:44:49 +0000312 }
313#endif /* CONFIG_VERSION_VARIABLE */
314
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200315#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000316 u_boot_hush_start ();
317#endif
318
Heiko Schocher81473f62008-10-15 09:40:28 +0200319#if defined(CONFIG_HUSH_INIT_VAR)
320 hush_init_var ();
321#endif
322
wdenkc6097192002-11-03 00:24:07 +0000323#ifdef CONFIG_PREBOOT
324 if ((p = getenv ("preboot")) != NULL) {
325# ifdef CONFIG_AUTOBOOT_KEYED
326 int prev = disable_ctrlc(1); /* disable Control C checking */
327# endif
328
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200329# ifndef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000330 run_command (p, 0);
331# else
332 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
333 FLAG_EXIT_FROM_LOOP);
334# endif
335
336# ifdef CONFIG_AUTOBOOT_KEYED
337 disable_ctrlc(prev); /* restore Control C checking */
338# endif
339 }
340#endif /* CONFIG_PREBOOT */
341
Wolfgang Denk143cd212010-03-11 23:56:03 +0100342#if defined(CONFIG_UPDATE_TFTP)
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000343 update_tftp (0UL);
Wolfgang Denk143cd212010-03-11 23:56:03 +0100344#endif /* CONFIG_UPDATE_TFTP */
345
wdenkc6097192002-11-03 00:24:07 +0000346#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
347 s = getenv ("bootdelay");
348 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
349
wdenka6c7ad22002-12-03 21:28:10 +0000350 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000351
352# ifdef CONFIG_BOOT_RETRY_TIME
wdenk6dd652f2003-06-19 23:40:20 +0000353 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000354# endif /* CONFIG_BOOT_RETRY_TIME */
355
Yuri Tikhonovb428f6a2008-02-04 14:11:03 +0100356#ifdef CONFIG_POST
357 if (gd->flags & GD_FLG_POSTFAIL) {
358 s = getenv("failbootcmd");
359 }
360 else
361#endif /* CONFIG_POST */
wdenkbdccc4f2003-08-05 17:43:17 +0000362#ifdef CONFIG_BOOTCOUNT_LIMIT
363 if (bootlimit && (bootcount > bootlimit)) {
364 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
365 (unsigned)bootlimit);
366 s = getenv ("altbootcmd");
367 }
368 else
369#endif /* CONFIG_BOOTCOUNT_LIMIT */
370 s = getenv ("bootcmd");
wdenka6c7ad22002-12-03 21:28:10 +0000371
372 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
373
wdenkc6097192002-11-03 00:24:07 +0000374 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
375# ifdef CONFIG_AUTOBOOT_KEYED
376 int prev = disable_ctrlc(1); /* disable Control C checking */
377# endif
378
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200379# ifndef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000380 run_command (s, 0);
381# else
382 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
383 FLAG_EXIT_FROM_LOOP);
384# endif
385
386# ifdef CONFIG_AUTOBOOT_KEYED
387 disable_ctrlc(prev); /* restore Control C checking */
388# endif
389 }
wdenkc7de8292002-11-19 11:04:11 +0000390
391# ifdef CONFIG_MENUKEY
wdenka6c7ad22002-12-03 21:28:10 +0000392 if (menukey == CONFIG_MENUKEY) {
Jason Hobbs370d1e32011-06-23 08:27:30 +0000393 s = getenv("menucmd");
394 if (s) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200395# ifndef CONFIG_SYS_HUSH_PARSER
Jason Hobbs370d1e32011-06-23 08:27:30 +0000396 run_command(s, 0);
wdenkc7de8292002-11-19 11:04:11 +0000397# else
Jason Hobbs370d1e32011-06-23 08:27:30 +0000398 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
399 FLAG_EXIT_FROM_LOOP);
wdenkc7de8292002-11-19 11:04:11 +0000400# endif
Jason Hobbs370d1e32011-06-23 08:27:30 +0000401 }
wdenkc7de8292002-11-19 11:04:11 +0000402 }
403#endif /* CONFIG_MENUKEY */
Wolfgang Denk953b7e62010-06-13 18:28:54 +0200404#endif /* CONFIG_BOOTDELAY */
wdenkc7de8292002-11-19 11:04:11 +0000405
wdenkc6097192002-11-03 00:24:07 +0000406 /*
407 * Main Loop for Monitor Command Processing
408 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200409#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000410 parse_file_outer();
411 /* This point is never reached */
412 for (;;);
413#else
414 for (;;) {
415#ifdef CONFIG_BOOT_RETRY_TIME
416 if (rc >= 0) {
417 /* Saw enough of a valid command to
418 * restart the timeout.
419 */
420 reset_cmd_timeout();
421 }
422#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200423 len = readline (CONFIG_SYS_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000424
425 flag = 0; /* assume no special flags for now */
426 if (len > 0)
427 strcpy (lastcommand, console_buffer);
428 else if (len == 0)
429 flag |= CMD_FLAG_REPEAT;
430#ifdef CONFIG_BOOT_RETRY_TIME
431 else if (len == -2) {
432 /* -2 means timed out, retry autoboot
433 */
wdenk4b9206e2004-03-23 22:14:11 +0000434 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000435# ifdef CONFIG_RESET_TO_RETRY
436 /* Reinit board to run initialization code again */
437 do_reset (NULL, 0, 0, NULL);
438# else
439 return; /* retry autoboot */
440# endif
441 }
442#endif
443
444 if (len == -1)
wdenk4b9206e2004-03-23 22:14:11 +0000445 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000446 else
447 rc = run_command (lastcommand, flag);
448
449 if (rc <= 0) {
450 /* invalid command or not repeatable, forget it */
451 lastcommand[0] = 0;
452 }
453 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200454#endif /*CONFIG_SYS_HUSH_PARSER*/
wdenkc6097192002-11-03 00:24:07 +0000455}
456
wdenk6dd652f2003-06-19 23:40:20 +0000457#ifdef CONFIG_BOOT_RETRY_TIME
458/***************************************************************************
Wolfgang Denk19973b62006-10-28 00:38:39 +0200459 * initialize command line timeout
wdenk6dd652f2003-06-19 23:40:20 +0000460 */
461void init_cmd_timeout(void)
462{
463 char *s = getenv ("bootretry");
464
465 if (s != NULL)
wdenkb028f712003-12-07 21:39:28 +0000466 retry_time = (int)simple_strtol(s, NULL, 10);
wdenk6dd652f2003-06-19 23:40:20 +0000467 else
468 retry_time = CONFIG_BOOT_RETRY_TIME;
469
470 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
471 retry_time = CONFIG_BOOT_RETRY_MIN;
472}
473
wdenkc6097192002-11-03 00:24:07 +0000474/***************************************************************************
475 * reset command line timeout to retry_time seconds
476 */
wdenkc6097192002-11-03 00:24:07 +0000477void reset_cmd_timeout(void)
478{
479 endtime = endtick(retry_time);
480}
481#endif
482
Wolfgang Denk501090a2006-07-21 11:33:45 +0200483#ifdef CONFIG_CMDLINE_EDITING
484
485/*
486 * cmdline-editing related codes from vivi.
487 * Author: Janghoon Lyu <nandy@mizi.com>
488 */
489
Wolfgang Denk501090a2006-07-21 11:33:45 +0200490#define putnstr(str,n) do { \
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700491 printf ("%.*s", (int)n, str); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200492 } while (0)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200493
494#define CTL_CH(c) ((c) - 'a' + 1)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200495#define CTL_BACKSPACE ('\b')
496#define DEL ((char)255)
497#define DEL7 ((char)127)
498#define CREAD_HIST_CHAR ('!')
499
500#define getcmd_putch(ch) putc(ch)
501#define getcmd_getch() getc()
502#define getcmd_cbeep() getcmd_putch('\a')
503
504#define HIST_MAX 20
Peter Tyser8804ae32010-09-29 13:30:56 -0500505#define HIST_SIZE CONFIG_SYS_CBSIZE
Wolfgang Denk501090a2006-07-21 11:33:45 +0200506
507static int hist_max = 0;
508static int hist_add_idx = 0;
509static int hist_cur = -1;
510unsigned hist_num = 0;
511
512char* hist_list[HIST_MAX];
John Schmoller6475b9f2010-03-12 09:49:23 -0600513char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200514
515#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
516
517static void hist_init(void)
518{
519 int i;
520
521 hist_max = 0;
522 hist_add_idx = 0;
523 hist_cur = -1;
524 hist_num = 0;
525
526 for (i = 0; i < HIST_MAX; i++) {
527 hist_list[i] = hist_lines[i];
528 hist_list[i][0] = '\0';
529 }
530}
531
532static void cread_add_to_hist(char *line)
533{
534 strcpy(hist_list[hist_add_idx], line);
535
536 if (++hist_add_idx >= HIST_MAX)
537 hist_add_idx = 0;
538
539 if (hist_add_idx > hist_max)
540 hist_max = hist_add_idx;
541
542 hist_num++;
543}
544
545static char* hist_prev(void)
546{
547 char *ret;
548 int old_cur;
549
550 if (hist_cur < 0)
551 return NULL;
552
553 old_cur = hist_cur;
554 if (--hist_cur < 0)
555 hist_cur = hist_max;
556
557 if (hist_cur == hist_add_idx) {
558 hist_cur = old_cur;
559 ret = NULL;
560 } else
561 ret = hist_list[hist_cur];
562
563 return (ret);
564}
565
566static char* hist_next(void)
567{
568 char *ret;
569
570 if (hist_cur < 0)
571 return NULL;
572
573 if (hist_cur == hist_add_idx)
574 return NULL;
575
576 if (++hist_cur > hist_max)
577 hist_cur = 0;
578
579 if (hist_cur == hist_add_idx) {
580 ret = "";
581 } else
582 ret = hist_list[hist_cur];
583
584 return (ret);
585}
586
Stefan Roese3ca91222006-07-27 16:11:19 +0200587#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk501090a2006-07-21 11:33:45 +0200588static void cread_print_hist_list(void)
589{
590 int i;
591 unsigned long n;
592
593 n = hist_num - hist_max;
594
595 i = hist_add_idx + 1;
596 while (1) {
597 if (i > hist_max)
598 i = 0;
599 if (i == hist_add_idx)
600 break;
601 printf("%s\n", hist_list[i]);
602 n++;
603 i++;
604 }
605}
Stefan Roese3ca91222006-07-27 16:11:19 +0200606#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200607
608#define BEGINNING_OF_LINE() { \
609 while (num) { \
610 getcmd_putch(CTL_BACKSPACE); \
611 num--; \
612 } \
613}
614
615#define ERASE_TO_EOL() { \
616 if (num < eol_num) { \
Mike Frysinger8faba482010-07-23 05:28:15 -0400617 printf("%*s", (int)(eol_num - num), ""); \
618 do { \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200619 getcmd_putch(CTL_BACKSPACE); \
Mike Frysinger8faba482010-07-23 05:28:15 -0400620 } while (--eol_num > num); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200621 } \
622}
623
624#define REFRESH_TO_EOL() { \
625 if (num < eol_num) { \
626 wlen = eol_num - num; \
627 putnstr(buf + num, wlen); \
628 num = eol_num; \
629 } \
630}
631
632static void cread_add_char(char ichar, int insert, unsigned long *num,
633 unsigned long *eol_num, char *buf, unsigned long len)
634{
635 unsigned long wlen;
636
637 /* room ??? */
638 if (insert || *num == *eol_num) {
639 if (*eol_num > len - 1) {
640 getcmd_cbeep();
641 return;
642 }
643 (*eol_num)++;
644 }
645
646 if (insert) {
647 wlen = *eol_num - *num;
648 if (wlen > 1) {
649 memmove(&buf[*num+1], &buf[*num], wlen-1);
650 }
651
652 buf[*num] = ichar;
653 putnstr(buf + *num, wlen);
654 (*num)++;
655 while (--wlen) {
656 getcmd_putch(CTL_BACKSPACE);
657 }
658 } else {
659 /* echo the character */
660 wlen = 1;
661 buf[*num] = ichar;
662 putnstr(buf + *num, wlen);
663 (*num)++;
664 }
665}
666
667static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
668 unsigned long *eol_num, char *buf, unsigned long len)
669{
670 while (strsize--) {
671 cread_add_char(*str, insert, num, eol_num, buf, len);
672 str++;
673 }
674}
675
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100676static int cread_line(const char *const prompt, char *buf, unsigned int *len)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200677{
678 unsigned long num = 0;
679 unsigned long eol_num = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200680 unsigned long wlen;
681 char ichar;
682 int insert = 1;
683 int esc_len = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200684 char esc_save[8];
Peter Tyserecc55002009-10-25 15:12:54 -0500685 int init_len = strlen(buf);
686
687 if (init_len)
688 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
Wolfgang Denk501090a2006-07-21 11:33:45 +0200689
690 while (1) {
Andreas Engel00ac50e2008-01-09 17:10:56 +0100691#ifdef CONFIG_BOOT_RETRY_TIME
692 while (!tstc()) { /* while no incoming data */
693 if (retry_time >= 0 && get_ticks() > endtime)
694 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +0200695 WATCHDOG_RESET();
Andreas Engel00ac50e2008-01-09 17:10:56 +0100696 }
697#endif
698
Wolfgang Denk501090a2006-07-21 11:33:45 +0200699 ichar = getcmd_getch();
700
701 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200702 putc('\n');
Wolfgang Denk501090a2006-07-21 11:33:45 +0200703 break;
704 }
705
706 /*
707 * handle standard linux xterm esc sequences for arrow key, etc.
708 */
709 if (esc_len != 0) {
710 if (esc_len == 1) {
711 if (ichar == '[') {
712 esc_save[esc_len] = ichar;
713 esc_len = 2;
714 } else {
715 cread_add_str(esc_save, esc_len, insert,
716 &num, &eol_num, buf, *len);
717 esc_len = 0;
718 }
719 continue;
720 }
721
722 switch (ichar) {
723
724 case 'D': /* <- key */
725 ichar = CTL_CH('b');
726 esc_len = 0;
727 break;
728 case 'C': /* -> key */
729 ichar = CTL_CH('f');
730 esc_len = 0;
731 break; /* pass off to ^F handler */
732 case 'H': /* Home key */
733 ichar = CTL_CH('a');
734 esc_len = 0;
735 break; /* pass off to ^A handler */
736 case 'A': /* up arrow */
737 ichar = CTL_CH('p');
738 esc_len = 0;
739 break; /* pass off to ^P handler */
740 case 'B': /* down arrow */
741 ichar = CTL_CH('n');
742 esc_len = 0;
743 break; /* pass off to ^N handler */
744 default:
745 esc_save[esc_len++] = ichar;
746 cread_add_str(esc_save, esc_len, insert,
747 &num, &eol_num, buf, *len);
748 esc_len = 0;
749 continue;
750 }
751 }
752
753 switch (ichar) {
754 case 0x1b:
755 if (esc_len == 0) {
756 esc_save[esc_len] = ichar;
757 esc_len = 1;
758 } else {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200759 puts("impossible condition #876\n");
Wolfgang Denk501090a2006-07-21 11:33:45 +0200760 esc_len = 0;
761 }
762 break;
763
764 case CTL_CH('a'):
765 BEGINNING_OF_LINE();
766 break;
767 case CTL_CH('c'): /* ^C - break */
768 *buf = '\0'; /* discard input */
769 return (-1);
770 case CTL_CH('f'):
771 if (num < eol_num) {
772 getcmd_putch(buf[num]);
773 num++;
774 }
775 break;
776 case CTL_CH('b'):
777 if (num) {
778 getcmd_putch(CTL_BACKSPACE);
779 num--;
780 }
781 break;
782 case CTL_CH('d'):
783 if (num < eol_num) {
784 wlen = eol_num - num - 1;
785 if (wlen) {
786 memmove(&buf[num], &buf[num+1], wlen);
787 putnstr(buf + num, wlen);
788 }
789
790 getcmd_putch(' ');
791 do {
792 getcmd_putch(CTL_BACKSPACE);
793 } while (wlen--);
794 eol_num--;
795 }
796 break;
797 case CTL_CH('k'):
798 ERASE_TO_EOL();
799 break;
800 case CTL_CH('e'):
801 REFRESH_TO_EOL();
802 break;
803 case CTL_CH('o'):
804 insert = !insert;
805 break;
806 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100807 case CTL_CH('u'):
Wolfgang Denk501090a2006-07-21 11:33:45 +0200808 BEGINNING_OF_LINE();
809 ERASE_TO_EOL();
810 break;
811 case DEL:
812 case DEL7:
813 case 8:
814 if (num) {
815 wlen = eol_num - num;
816 num--;
817 memmove(&buf[num], &buf[num+1], wlen);
818 getcmd_putch(CTL_BACKSPACE);
819 putnstr(buf + num, wlen);
820 getcmd_putch(' ');
821 do {
822 getcmd_putch(CTL_BACKSPACE);
823 } while (wlen--);
824 eol_num--;
825 }
826 break;
827 case CTL_CH('p'):
828 case CTL_CH('n'):
829 {
830 char * hline;
831
832 esc_len = 0;
833
834 if (ichar == CTL_CH('p'))
835 hline = hist_prev();
836 else
837 hline = hist_next();
838
839 if (!hline) {
840 getcmd_cbeep();
841 continue;
842 }
843
844 /* nuke the current line */
845 /* first, go home */
846 BEGINNING_OF_LINE();
847
848 /* erase to end of line */
849 ERASE_TO_EOL();
850
851 /* copy new line into place and display */
852 strcpy(buf, hline);
853 eol_num = strlen(buf);
854 REFRESH_TO_EOL();
855 continue;
856 }
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100857#ifdef CONFIG_AUTO_COMPLETE
858 case '\t': {
859 int num2, col;
860
861 /* do not autocomplete when in the middle */
862 if (num < eol_num) {
863 getcmd_cbeep();
864 break;
865 }
866
867 buf[num] = '\0';
868 col = strlen(prompt) + eol_num;
869 num2 = num;
870 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
871 col = num2 - num;
872 num += col;
873 eol_num += col;
874 }
875 break;
876 }
877#endif
Wolfgang Denk501090a2006-07-21 11:33:45 +0200878 default:
879 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
880 break;
881 }
882 }
883 *len = eol_num;
884 buf[eol_num] = '\0'; /* lose the newline */
885
886 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
887 cread_add_to_hist(buf);
888 hist_cur = hist_add_idx;
889
Peter Tyserf9239432009-10-25 15:12:53 -0500890 return 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200891}
892
893#endif /* CONFIG_CMDLINE_EDITING */
894
wdenkc6097192002-11-03 00:24:07 +0000895/****************************************************************************/
896
897/*
898 * Prompt for input and read a line.
899 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
900 * time out when time goes past endtime (timebase time in ticks).
901 * Return: number of read characters
902 * -1 if break
903 * -2 if timed out
904 */
905int readline (const char *const prompt)
906{
Peter Tyserecc55002009-10-25 15:12:54 -0500907 /*
908 * If console_buffer isn't 0-length the user will be prompted to modify
909 * it instead of entering it from scratch as desired.
910 */
911 console_buffer[0] = '\0';
912
James Yang6636b622008-01-09 11:17:49 -0600913 return readline_into_buffer(prompt, console_buffer);
914}
915
916
917int readline_into_buffer (const char *const prompt, char * buffer)
918{
919 char *p = buffer;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200920#ifdef CONFIG_CMDLINE_EDITING
Peter Tyser8804ae32010-09-29 13:30:56 -0500921 unsigned int len = CONFIG_SYS_CBSIZE;
Stefan Roesed8f961b2006-08-07 15:08:44 +0200922 int rc;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200923 static int initted = 0;
924
James Yang597f6c22008-05-05 10:22:53 -0500925 /*
926 * History uses a global array which is not
927 * writable until after relocation to RAM.
928 * Revert to non-history version if still
929 * running from flash.
930 */
931 if (gd->flags & GD_FLG_RELOC) {
932 if (!initted) {
933 hist_init();
934 initted = 1;
935 }
936
Peter Tysere491a712009-10-25 15:12:52 -0500937 if (prompt)
938 puts (prompt);
James Yang597f6c22008-05-05 10:22:53 -0500939
940 rc = cread_line(prompt, p, &len);
941 return rc < 0 ? rc : len;
942
943 } else {
944#endif /* CONFIG_CMDLINE_EDITING */
Kumar Gala0ec59522008-01-10 02:22:05 -0600945 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +0000946 int n = 0; /* buffer index */
947 int plen = 0; /* prompt length */
948 int col; /* output column cnt */
949 char c;
950
951 /* print prompt */
952 if (prompt) {
953 plen = strlen (prompt);
954 puts (prompt);
955 }
956 col = plen;
957
958 for (;;) {
959#ifdef CONFIG_BOOT_RETRY_TIME
960 while (!tstc()) { /* while no incoming data */
961 if (retry_time >= 0 && get_ticks() > endtime)
962 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +0200963 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000964 }
965#endif
966 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
967
968#ifdef CONFIG_SHOW_ACTIVITY
969 while (!tstc()) {
970 extern void show_activity(int arg);
971 show_activity(0);
Jens Scharsig30dc1652010-04-09 19:02:38 +0200972 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000973 }
974#endif
975 c = getc();
976
977 /*
978 * Special character handling
979 */
980 switch (c) {
981 case '\r': /* Enter */
982 case '\n':
983 *p = '\0';
984 puts ("\r\n");
James Yang6636b622008-01-09 11:17:49 -0600985 return (p - p_buf);
wdenkc6097192002-11-03 00:24:07 +0000986
wdenk27aa8182004-03-23 22:37:33 +0000987 case '\0': /* nul */
988 continue;
989
wdenkc6097192002-11-03 00:24:07 +0000990 case 0x03: /* ^C - break */
James Yang6636b622008-01-09 11:17:49 -0600991 p_buf[0] = '\0'; /* discard input */
wdenkc6097192002-11-03 00:24:07 +0000992 return (-1);
993
994 case 0x15: /* ^U - erase line */
995 while (col > plen) {
996 puts (erase_seq);
997 --col;
998 }
James Yang6636b622008-01-09 11:17:49 -0600999 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001000 n = 0;
1001 continue;
1002
Wolfgang Denk1636d1c2007-06-22 23:59:00 +02001003 case 0x17: /* ^W - erase word */
James Yang6636b622008-01-09 11:17:49 -06001004 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001005 while ((n > 0) && (*p != ' ')) {
James Yang6636b622008-01-09 11:17:49 -06001006 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001007 }
1008 continue;
1009
1010 case 0x08: /* ^H - backspace */
1011 case 0x7F: /* DEL - backspace */
James Yang6636b622008-01-09 11:17:49 -06001012 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001013 continue;
1014
1015 default:
1016 /*
1017 * Must be a normal character then
1018 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001019 if (n < CONFIG_SYS_CBSIZE-2) {
wdenkc6097192002-11-03 00:24:07 +00001020 if (c == '\t') { /* expand TABs */
wdenk04a85b32004-04-15 18:22:41 +00001021#ifdef CONFIG_AUTO_COMPLETE
1022 /* if auto completion triggered just continue */
1023 *p = '\0';
1024 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang6636b622008-01-09 11:17:49 -06001025 p = p_buf + n; /* reset */
wdenk04a85b32004-04-15 18:22:41 +00001026 continue;
1027 }
1028#endif
wdenkc6097192002-11-03 00:24:07 +00001029 puts (tab_seq+(col&07));
1030 col += 8 - (col&07);
1031 } else {
1032 ++col; /* echo input */
1033 putc (c);
1034 }
1035 *p++ = c;
1036 ++n;
1037 } else { /* Buffer full */
1038 putc ('\a');
1039 }
1040 }
1041 }
James Yang597f6c22008-05-05 10:22:53 -05001042#ifdef CONFIG_CMDLINE_EDITING
1043 }
1044#endif
wdenkc6097192002-11-03 00:24:07 +00001045}
1046
1047/****************************************************************************/
1048
1049static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1050{
1051 char *s;
1052
1053 if (*np == 0) {
1054 return (p);
1055 }
1056
1057 if (*(--p) == '\t') { /* will retype the whole line */
1058 while (*colp > plen) {
1059 puts (erase_seq);
1060 (*colp)--;
1061 }
1062 for (s=buffer; s<p; ++s) {
1063 if (*s == '\t') {
1064 puts (tab_seq+((*colp) & 07));
1065 *colp += 8 - ((*colp) & 07);
1066 } else {
1067 ++(*colp);
1068 putc (*s);
1069 }
1070 }
1071 } else {
1072 puts (erase_seq);
1073 (*colp)--;
1074 }
1075 (*np)--;
1076 return (p);
1077}
1078
1079/****************************************************************************/
1080
1081int parse_line (char *line, char *argv[])
1082{
1083 int nargs = 0;
1084
1085#ifdef DEBUG_PARSER
1086 printf ("parse_line: \"%s\"\n", line);
1087#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001088 while (nargs < CONFIG_SYS_MAXARGS) {
wdenkc6097192002-11-03 00:24:07 +00001089
1090 /* skip any white space */
1091 while ((*line == ' ') || (*line == '\t')) {
1092 ++line;
1093 }
1094
1095 if (*line == '\0') { /* end of line, no more args */
1096 argv[nargs] = NULL;
1097#ifdef DEBUG_PARSER
1098 printf ("parse_line: nargs=%d\n", nargs);
1099#endif
1100 return (nargs);
1101 }
1102
1103 argv[nargs++] = line; /* begin of argument string */
1104
1105 /* find end of string */
1106 while (*line && (*line != ' ') && (*line != '\t')) {
1107 ++line;
1108 }
1109
1110 if (*line == '\0') { /* end of line, no more args */
1111 argv[nargs] = NULL;
1112#ifdef DEBUG_PARSER
1113 printf ("parse_line: nargs=%d\n", nargs);
1114#endif
1115 return (nargs);
1116 }
1117
1118 *line++ = '\0'; /* terminate current arg */
1119 }
1120
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001121 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
wdenkc6097192002-11-03 00:24:07 +00001122
1123#ifdef DEBUG_PARSER
1124 printf ("parse_line: nargs=%d\n", nargs);
1125#endif
1126 return (nargs);
1127}
1128
1129/****************************************************************************/
1130
1131static void process_macros (const char *input, char *output)
1132{
1133 char c, prev;
1134 const char *varname_start = NULL;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001135 int inputcnt = strlen (input);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001136 int outputcnt = CONFIG_SYS_CBSIZE;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001137 int state = 0; /* 0 = waiting for '$' */
1138
1139 /* 1 = waiting for '(' or '{' */
1140 /* 2 = waiting for ')' or '}' */
1141 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001142#ifdef DEBUG_PARSER
1143 char *output_start = output;
1144
Wolfgang Denk19973b62006-10-28 00:38:39 +02001145 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1146 input);
wdenkc6097192002-11-03 00:24:07 +00001147#endif
1148
Wolfgang Denk19973b62006-10-28 00:38:39 +02001149 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001150
1151 while (inputcnt && outputcnt) {
wdenk8bde7f72003-06-27 21:31:46 +00001152 c = *input++;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001153 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001154
Wolfgang Denk19973b62006-10-28 00:38:39 +02001155 if (state != 3) {
1156 /* remove one level of escape characters */
1157 if ((c == '\\') && (prev != '\\')) {
1158 if (inputcnt-- == 0)
1159 break;
1160 prev = c;
1161 c = *input++;
1162 }
wdenka25f8622003-01-02 23:57:29 +00001163 }
wdenkc6097192002-11-03 00:24:07 +00001164
Wolfgang Denk19973b62006-10-28 00:38:39 +02001165 switch (state) {
1166 case 0: /* Waiting for (unescaped) $ */
1167 if ((c == '\'') && (prev != '\\')) {
1168 state = 3;
1169 break;
1170 }
1171 if ((c == '$') && (prev != '\\')) {
1172 state++;
1173 } else {
wdenkc6097192002-11-03 00:24:07 +00001174 *(output++) = c;
1175 outputcnt--;
1176 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001177 break;
1178 case 1: /* Waiting for ( */
1179 if (c == '(' || c == '{') {
1180 state++;
1181 varname_start = input;
1182 } else {
1183 state = 0;
1184 *(output++) = '$';
1185 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001186
Wolfgang Denk19973b62006-10-28 00:38:39 +02001187 if (outputcnt) {
1188 *(output++) = c;
wdenkc6097192002-11-03 00:24:07 +00001189 outputcnt--;
1190 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001191 }
1192 break;
1193 case 2: /* Waiting for ) */
1194 if (c == ')' || c == '}') {
1195 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001196 char envname[CONFIG_SYS_CBSIZE], *envval;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001197 int envcnt = input - varname_start - 1; /* Varname # of chars */
1198
1199 /* Get the varname */
1200 for (i = 0; i < envcnt; i++) {
1201 envname[i] = varname_start[i];
1202 }
1203 envname[i] = 0;
1204
1205 /* Get its value */
1206 envval = getenv (envname);
1207
1208 /* Copy into the line if it exists */
1209 if (envval != NULL)
1210 while ((*envval) && outputcnt) {
1211 *(output++) = *(envval++);
1212 outputcnt--;
1213 }
1214 /* Look for another '$' */
1215 state = 0;
1216 }
1217 break;
1218 case 3: /* Waiting for ' */
1219 if ((c == '\'') && (prev != '\\')) {
1220 state = 0;
1221 } else {
1222 *(output++) = c;
1223 outputcnt--;
1224 }
1225 break;
wdenkc6097192002-11-03 00:24:07 +00001226 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001227 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001228 }
1229
1230 if (outputcnt)
1231 *output = 0;
Bartlomiej Sieka9160b962007-05-27 17:04:18 +02001232 else
1233 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001234
1235#ifdef DEBUG_PARSER
1236 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
Wolfgang Denk19973b62006-10-28 00:38:39 +02001237 strlen (output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001238#endif
1239}
1240
1241/****************************************************************************
1242 * returns:
1243 * 1 - command executed, repeatable
1244 * 0 - command executed but not repeatable, interrupted commands are
1245 * always considered not repeatable
1246 * -1 - not executed (unrecognized, bootd recursion or too many args)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001247 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
wdenkc6097192002-11-03 00:24:07 +00001248 * considered unrecognized)
1249 *
1250 * WARNING:
1251 *
1252 * We must create a temporary copy of the command since the command we get
1253 * may be the result from getenv(), which returns a pointer directly to
1254 * the environment data, which may change magicly when the command we run
1255 * creates or modifies environment variables (like "bootp" does).
1256 */
1257
1258int run_command (const char *cmd, int flag)
1259{
1260 cmd_tbl_t *cmdtp;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001261 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
wdenkc6097192002-11-03 00:24:07 +00001262 char *token; /* start of token in cmdbuf */
1263 char *sep; /* end of token (separator) in cmdbuf */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001264 char finaltoken[CONFIG_SYS_CBSIZE];
wdenkc6097192002-11-03 00:24:07 +00001265 char *str = cmdbuf;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001266 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenkf07771c2003-05-28 08:06:31 +00001267 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001268 int repeatable = 1;
wdenkf07771c2003-05-28 08:06:31 +00001269 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001270
1271#ifdef DEBUG_PARSER
1272 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1273 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1274 puts ("\"\n");
1275#endif
1276
1277 clear_ctrlc(); /* forget any previous Control C */
1278
1279 if (!cmd || !*cmd) {
1280 return -1; /* empty command */
1281 }
1282
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001283 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
wdenkc6097192002-11-03 00:24:07 +00001284 puts ("## Command too long!\n");
1285 return -1;
1286 }
1287
1288 strcpy (cmdbuf, cmd);
1289
1290 /* Process separators and check for invalid
1291 * repeatable commands
1292 */
1293
1294#ifdef DEBUG_PARSER
1295 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1296#endif
1297 while (*str) {
1298
1299 /*
1300 * Find separator, or string end
1301 * Allow simple escape of ';' by writing "\;"
1302 */
wdenka25f8622003-01-02 23:57:29 +00001303 for (inquotes = 0, sep = str; *sep; sep++) {
1304 if ((*sep=='\'') &&
1305 (*(sep-1) != '\\'))
1306 inquotes=!inquotes;
1307
1308 if (!inquotes &&
1309 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001310 ( sep != str) && /* past string start */
1311 (*(sep-1) != '\\')) /* and NOT escaped */
1312 break;
1313 }
1314
1315 /*
1316 * Limit the token to data between separators
1317 */
1318 token = str;
1319 if (*sep) {
1320 str = sep + 1; /* start of command for next pass */
1321 *sep = '\0';
1322 }
1323 else
1324 str = sep; /* no more commands for next pass */
1325#ifdef DEBUG_PARSER
1326 printf ("token: \"%s\"\n", token);
1327#endif
1328
1329 /* find macros in this token and replace them */
1330 process_macros (token, finaltoken);
1331
1332 /* Extract arguments */
Wolfgang Denk1264b402006-03-12 02:20:55 +01001333 if ((argc = parse_line (finaltoken, argv)) == 0) {
1334 rc = -1; /* no command at all */
1335 continue;
1336 }
wdenkc6097192002-11-03 00:24:07 +00001337
1338 /* Look up command in command table */
1339 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1340 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
wdenkf07771c2003-05-28 08:06:31 +00001341 rc = -1; /* give up after bad command */
1342 continue;
wdenkc6097192002-11-03 00:24:07 +00001343 }
1344
1345 /* found - check max args */
1346 if (argc > cmdtp->maxargs) {
Peter Tyser62c3ae72009-01-27 18:03:10 -06001347 cmd_usage(cmdtp);
wdenkf07771c2003-05-28 08:06:31 +00001348 rc = -1;
1349 continue;
wdenkc6097192002-11-03 00:24:07 +00001350 }
1351
Jon Loeligerc3517f92007-07-08 18:10:08 -05001352#if defined(CONFIG_CMD_BOOTD)
wdenkc6097192002-11-03 00:24:07 +00001353 /* avoid "bootd" recursion */
1354 if (cmdtp->cmd == do_bootd) {
1355#ifdef DEBUG_PARSER
1356 printf ("[%s]\n", finaltoken);
1357#endif
1358 if (flag & CMD_FLAG_BOOTD) {
wdenk4b9206e2004-03-23 22:14:11 +00001359 puts ("'bootd' recursion detected\n");
wdenkf07771c2003-05-28 08:06:31 +00001360 rc = -1;
1361 continue;
Wolfgang Denk1264b402006-03-12 02:20:55 +01001362 } else {
wdenkc6097192002-11-03 00:24:07 +00001363 flag |= CMD_FLAG_BOOTD;
Wolfgang Denk1264b402006-03-12 02:20:55 +01001364 }
wdenkc6097192002-11-03 00:24:07 +00001365 }
Jon Loeliger90253172007-07-10 11:02:44 -05001366#endif
wdenkc6097192002-11-03 00:24:07 +00001367
1368 /* OK - call function to do the command */
1369 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
wdenkf07771c2003-05-28 08:06:31 +00001370 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001371 }
1372
1373 repeatable &= cmdtp->repeatable;
1374
1375 /* Did the user stop this? */
1376 if (had_ctrlc ())
Detlev Zundel5afb2022007-05-23 18:47:48 +02001377 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001378 }
1379
wdenkf07771c2003-05-28 08:06:31 +00001380 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001381}
1382
1383/****************************************************************************/
1384
Jon Loeligerc3517f92007-07-08 18:10:08 -05001385#if defined(CONFIG_CMD_RUN)
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001386int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +00001387{
1388 int i;
wdenkc6097192002-11-03 00:24:07 +00001389
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001390 if (argc < 2)
1391 return cmd_usage(cmdtp);
wdenkc6097192002-11-03 00:24:07 +00001392
1393 for (i=1; i<argc; ++i) {
wdenk3e386912003-04-05 00:53:31 +00001394 char *arg;
1395
1396 if ((arg = getenv (argv[i])) == NULL) {
1397 printf ("## Error: \"%s\" not defined\n", argv[i]);
1398 return 1;
1399 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001400#ifndef CONFIG_SYS_HUSH_PARSER
wdenk3e386912003-04-05 00:53:31 +00001401 if (run_command (arg, flag) == -1)
1402 return 1;
wdenkc6097192002-11-03 00:24:07 +00001403#else
wdenk3e386912003-04-05 00:53:31 +00001404 if (parse_string_outer(arg,
wdenk7aa78612003-05-03 15:50:43 +00001405 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
wdenk3e386912003-04-05 00:53:31 +00001406 return 1;
wdenkc6097192002-11-03 00:24:07 +00001407#endif
1408 }
wdenk3e386912003-04-05 00:53:31 +00001409 return 0;
wdenkc6097192002-11-03 00:24:07 +00001410}
Jon Loeliger90253172007-07-10 11:02:44 -05001411#endif