blob: 91e888fd3ce0a3ef54c1996d153538e76e78707c [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>
Jason Hobbs4d91a6e2011-08-23 11:06:54 +000043#include <linux/ctype.h>
Heiko Schocher317d6c52012-01-16 21:13:35 +000044#include <menu.h>
wdenkbdccc4f2003-08-05 17:43:17 +000045
James Yang597f6c22008-05-05 10:22:53 -050046#if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
Wolfgang Denkd87080b2006-03-31 18:32:53 +020047DECLARE_GLOBAL_DATA_PTR;
48#endif
49
Heiko Schocherfad63402007-07-13 09:54:17 +020050/*
51 * Board-specific Platform code can reimplement show_boot_progress () if needed
52 */
53void inline __show_boot_progress (int val) {}
Emil Medve5e2c08c2009-05-12 13:48:32 -050054void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
Heiko Schocherfad63402007-07-13 09:54:17 +020055
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020056#if defined(CONFIG_UPDATE_TFTP)
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +000057int update_tftp (ulong addr);
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020058#endif /* CONFIG_UPDATE_TFTP */
wdenk8bde7f72003-06-27 21:31:46 +000059
wdenkc6097192002-11-03 00:24:07 +000060#define MAX_DELAY_STOP_STR 32
61
wdenkc6097192002-11-03 00:24:07 +000062#undef DEBUG_PARSER
63
John Schmoller6475b9f2010-03-12 09:49:23 -060064char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
wdenkc6097192002-11-03 00:24:07 +000065
Stefan Roese3ca91222006-07-27 16:11:19 +020066static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
Mike Frysinger82359ae2010-12-22 09:40:45 -050067static const char erase_seq[] = "\b \b"; /* erase sequence */
68static const char tab_seq[] = " "; /* used to expand TABs */
wdenkc6097192002-11-03 00:24:07 +000069
70#ifdef CONFIG_BOOT_RETRY_TIME
71static uint64_t endtime = 0; /* must be set, default is instant timeout */
72static int retry_time = -1; /* -1 so can call readline before main_loop */
73#endif
74
75#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
76
77#ifndef CONFIG_BOOT_RETRY_MIN
78#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
79#endif
80
81#ifdef CONFIG_MODEM_SUPPORT
82int do_mdm_init = 0;
83extern void mdm_init(void); /* defined in board.c */
84#endif
85
86/***************************************************************************
87 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
Jason Hobbsc8a20792011-08-31 05:37:24 +000088 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
wdenkc6097192002-11-03 00:24:07 +000089 */
90#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
91# if defined(CONFIG_AUTOBOOT_KEYED)
Jason Hobbsb41bc5a2011-08-23 11:06:50 +000092#ifndef CONFIG_MENU
93static inline
94#endif
95int abortboot(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +000096{
97 int abort = 0;
98 uint64_t etime = endtick(bootdelay);
Wolfgang Denk19973b62006-10-28 00:38:39 +020099 struct {
wdenkc6097192002-11-03 00:24:07 +0000100 char* str;
101 u_int len;
102 int retry;
103 }
Wolfgang Denk19973b62006-10-28 00:38:39 +0200104 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +0000105 { str: getenv ("bootdelaykey"), retry: 1 },
106 { str: getenv ("bootdelaykey2"), retry: 1 },
107 { str: getenv ("bootstopkey"), retry: 0 },
108 { str: getenv ("bootstopkey2"), retry: 0 },
109 };
110
111 char presskey [MAX_DELAY_STOP_STR];
112 u_int presskey_len = 0;
113 u_int presskey_max = 0;
114 u_int i;
115
116# ifdef CONFIG_AUTOBOOT_PROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200117 printf(CONFIG_AUTOBOOT_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000118# endif
119
120# ifdef CONFIG_AUTOBOOT_DELAY_STR
121 if (delaykey[0].str == NULL)
122 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
123# endif
124# ifdef CONFIG_AUTOBOOT_DELAY_STR2
125 if (delaykey[1].str == NULL)
126 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
127# endif
128# ifdef CONFIG_AUTOBOOT_STOP_STR
129 if (delaykey[2].str == NULL)
130 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
131# endif
132# ifdef CONFIG_AUTOBOOT_STOP_STR2
133 if (delaykey[3].str == NULL)
134 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
135# endif
136
137 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
138 delaykey[i].len = delaykey[i].str == NULL ?
139 0 : strlen (delaykey[i].str);
140 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
141 MAX_DELAY_STOP_STR : delaykey[i].len;
142
143 presskey_max = presskey_max > delaykey[i].len ?
144 presskey_max : delaykey[i].len;
145
146# if DEBUG_BOOTKEYS
147 printf("%s key:<%s>\n",
148 delaykey[i].retry ? "delay" : "stop",
149 delaykey[i].str ? delaykey[i].str : "NULL");
150# endif
151 }
152
153 /* In order to keep up with incoming data, check timeout only
154 * when catch up.
155 */
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100156 do {
157 if (tstc()) {
158 if (presskey_len < presskey_max) {
159 presskey [presskey_len ++] = getc();
160 }
161 else {
162 for (i = 0; i < presskey_max - 1; i ++)
163 presskey [i] = presskey [i + 1];
164
165 presskey [i] = getc();
166 }
167 }
168
wdenkc6097192002-11-03 00:24:07 +0000169 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
170 if (delaykey[i].len > 0 &&
171 presskey_len >= delaykey[i].len &&
172 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk8bde7f72003-06-27 21:31:46 +0000173 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000174 delaykey[i].len) == 0) {
175# if DEBUG_BOOTKEYS
176 printf("got %skey\n",
177 delaykey[i].retry ? "delay" : "stop");
178# endif
179
180# ifdef CONFIG_BOOT_RETRY_TIME
181 /* don't retry auto boot */
182 if (! delaykey[i].retry)
183 retry_time = -1;
184# endif
185 abort = 1;
186 }
187 }
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100188 } while (!abort && get_ticks() <= etime);
wdenkc6097192002-11-03 00:24:07 +0000189
wdenkc6097192002-11-03 00:24:07 +0000190# if DEBUG_BOOTKEYS
191 if (!abort)
Ladislav Michlada4d402007-04-25 16:01:26 +0200192 puts("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000193# endif
194
dzu8cb81432003-10-24 13:14:45 +0000195#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200196 if (abort)
197 gd->flags &= ~GD_FLG_SILENT;
dzu8cb81432003-10-24 13:14:45 +0000198#endif
199
wdenkc6097192002-11-03 00:24:07 +0000200 return abort;
201}
202
203# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
204
wdenkc7de8292002-11-19 11:04:11 +0000205#ifdef CONFIG_MENUKEY
206static int menukey = 0;
207#endif
208
Jason Hobbsb41bc5a2011-08-23 11:06:50 +0000209#ifndef CONFIG_MENU
210static inline
211#endif
212int abortboot(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000213{
214 int abort = 0;
215
wdenkc7de8292002-11-19 11:04:11 +0000216#ifdef CONFIG_MENUPROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200217 printf(CONFIG_MENUPROMPT);
wdenkc7de8292002-11-19 11:04:11 +0000218#else
wdenkc6097192002-11-03 00:24:07 +0000219 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenkc7de8292002-11-19 11:04:11 +0000220#endif
wdenkc6097192002-11-03 00:24:07 +0000221
222#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk8bde7f72003-06-27 21:31:46 +0000223 /*
224 * Check if key already pressed
225 * Don't check if bootdelay < 0
226 */
wdenkc6097192002-11-03 00:24:07 +0000227 if (bootdelay >= 0) {
228 if (tstc()) { /* we got a key press */
229 (void) getc(); /* consume input */
wdenk4b9206e2004-03-23 22:14:11 +0000230 puts ("\b\b\b 0");
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200231 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000232 }
wdenk8bde7f72003-06-27 21:31:46 +0000233 }
wdenkc6097192002-11-03 00:24:07 +0000234#endif
235
wdenkf72da342003-10-10 10:05:42 +0000236 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000237 int i;
238
239 --bootdelay;
240 /* delay 100 * 10ms */
241 for (i=0; !abort && i<100; ++i) {
242 if (tstc()) { /* we got a key press */
243 abort = 1; /* don't auto boot */
244 bootdelay = 0; /* no more delay */
wdenkc7de8292002-11-19 11:04:11 +0000245# ifdef CONFIG_MENUKEY
246 menukey = getc();
247# else
wdenkc6097192002-11-03 00:24:07 +0000248 (void) getc(); /* consume input */
wdenkc7de8292002-11-19 11:04:11 +0000249# endif
wdenkc6097192002-11-03 00:24:07 +0000250 break;
251 }
Ladislav Michlada4d402007-04-25 16:01:26 +0200252 udelay(10000);
wdenkc6097192002-11-03 00:24:07 +0000253 }
254
Ladislav Michlada4d402007-04-25 16:01:26 +0200255 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000256 }
257
Ladislav Michlada4d402007-04-25 16:01:26 +0200258 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000259
wdenkf72da342003-10-10 10:05:42 +0000260#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200261 if (abort)
262 gd->flags &= ~GD_FLG_SILENT;
wdenkf72da342003-10-10 10:05:42 +0000263#endif
264
wdenkc6097192002-11-03 00:24:07 +0000265 return abort;
266}
267# endif /* CONFIG_AUTOBOOT_KEYED */
268#endif /* CONFIG_BOOTDELAY >= 0 */
269
Jason Hobbsc8a20792011-08-31 05:37:24 +0000270/*
271 * Return 0 on success, or != 0 on error.
272 */
Jason Hobbs06283a62011-08-31 10:37:30 -0500273#ifndef CONFIG_CMD_PXE
Jason Hobbsc8a20792011-08-31 05:37:24 +0000274static inline
Jason Hobbs06283a62011-08-31 10:37:30 -0500275#endif
Jason Hobbsc8a20792011-08-31 05:37:24 +0000276int run_command2(const char *cmd, int flag)
277{
278#ifndef CONFIG_SYS_HUSH_PARSER
279 /*
280 * run_command can return 0 or 1 for success, so clean up its result.
281 */
282 if (run_command(cmd, flag) == -1)
283 return 1;
284
285 return 0;
286#else
287 return parse_string_outer(cmd,
288 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
289#endif
290}
291
wdenkc6097192002-11-03 00:24:07 +0000292/****************************************************************************/
293
294void main_loop (void)
295{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200296#ifndef CONFIG_SYS_HUSH_PARSER
297 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
wdenkc6097192002-11-03 00:24:07 +0000298 int len;
299 int rc = 1;
300 int flag;
301#endif
302
303#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
304 char *s;
305 int bootdelay;
306#endif
307#ifdef CONFIG_PREBOOT
308 char *p;
309#endif
wdenkbdccc4f2003-08-05 17:43:17 +0000310#ifdef CONFIG_BOOTCOUNT_LIMIT
311 unsigned long bootcount = 0;
312 unsigned long bootlimit = 0;
313 char *bcs;
314 char bcs_set[16];
315#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000316
wdenkbdccc4f2003-08-05 17:43:17 +0000317#ifdef CONFIG_BOOTCOUNT_LIMIT
318 bootcount = bootcount_load();
319 bootcount++;
320 bootcount_store (bootcount);
321 sprintf (bcs_set, "%lu", bootcount);
322 setenv ("bootcount", bcs_set);
323 bcs = getenv ("bootlimit");
324 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
325#endif /* CONFIG_BOOTCOUNT_LIMIT */
326
wdenkc6097192002-11-03 00:24:07 +0000327#ifdef CONFIG_MODEM_SUPPORT
328 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
329 if (do_mdm_init) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200330 char *str = strdup(getenv("mdm_cmd"));
wdenkc6097192002-11-03 00:24:07 +0000331 setenv ("preboot", str); /* set or delete definition */
332 if (str != NULL)
333 free (str);
334 mdm_init(); /* wait for modem connection */
335 }
336#endif /* CONFIG_MODEM_SUPPORT */
337
stroese05875972003-04-04 15:44:49 +0000338#ifdef CONFIG_VERSION_VARIABLE
339 {
stroese155cb012003-07-11 08:00:33 +0000340 setenv ("ver", version_string); /* set version variable */
stroese05875972003-04-04 15:44:49 +0000341 }
342#endif /* CONFIG_VERSION_VARIABLE */
343
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200344#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000345 u_boot_hush_start ();
346#endif
347
Heiko Schocher81473f62008-10-15 09:40:28 +0200348#if defined(CONFIG_HUSH_INIT_VAR)
349 hush_init_var ();
350#endif
351
wdenkc6097192002-11-03 00:24:07 +0000352#ifdef CONFIG_PREBOOT
353 if ((p = getenv ("preboot")) != NULL) {
354# ifdef CONFIG_AUTOBOOT_KEYED
355 int prev = disable_ctrlc(1); /* disable Control C checking */
356# endif
357
Jason Hobbsc8a20792011-08-31 05:37:24 +0000358 run_command2(p, 0);
wdenkc6097192002-11-03 00:24:07 +0000359
360# ifdef CONFIG_AUTOBOOT_KEYED
361 disable_ctrlc(prev); /* restore Control C checking */
362# endif
363 }
364#endif /* CONFIG_PREBOOT */
365
Wolfgang Denk143cd212010-03-11 23:56:03 +0100366#if defined(CONFIG_UPDATE_TFTP)
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000367 update_tftp (0UL);
Wolfgang Denk143cd212010-03-11 23:56:03 +0100368#endif /* CONFIG_UPDATE_TFTP */
369
wdenkc6097192002-11-03 00:24:07 +0000370#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
371 s = getenv ("bootdelay");
372 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
373
wdenka6c7ad22002-12-03 21:28:10 +0000374 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000375
Heiko Schocher317d6c52012-01-16 21:13:35 +0000376#if defined(CONFIG_MENU_SHOW)
377 bootdelay = menu_show(bootdelay);
378#endif
wdenkc6097192002-11-03 00:24:07 +0000379# ifdef CONFIG_BOOT_RETRY_TIME
wdenk6dd652f2003-06-19 23:40:20 +0000380 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000381# endif /* CONFIG_BOOT_RETRY_TIME */
382
Yuri Tikhonovb428f6a2008-02-04 14:11:03 +0100383#ifdef CONFIG_POST
384 if (gd->flags & GD_FLG_POSTFAIL) {
385 s = getenv("failbootcmd");
386 }
387 else
388#endif /* CONFIG_POST */
wdenkbdccc4f2003-08-05 17:43:17 +0000389#ifdef CONFIG_BOOTCOUNT_LIMIT
390 if (bootlimit && (bootcount > bootlimit)) {
391 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
392 (unsigned)bootlimit);
393 s = getenv ("altbootcmd");
394 }
395 else
396#endif /* CONFIG_BOOTCOUNT_LIMIT */
397 s = getenv ("bootcmd");
wdenka6c7ad22002-12-03 21:28:10 +0000398
399 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
400
wdenkc6097192002-11-03 00:24:07 +0000401 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
402# ifdef CONFIG_AUTOBOOT_KEYED
403 int prev = disable_ctrlc(1); /* disable Control C checking */
404# endif
405
Jason Hobbsc8a20792011-08-31 05:37:24 +0000406 run_command2(s, 0);
wdenkc6097192002-11-03 00:24:07 +0000407
408# ifdef CONFIG_AUTOBOOT_KEYED
409 disable_ctrlc(prev); /* restore Control C checking */
410# endif
411 }
wdenkc7de8292002-11-19 11:04:11 +0000412
413# ifdef CONFIG_MENUKEY
wdenka6c7ad22002-12-03 21:28:10 +0000414 if (menukey == CONFIG_MENUKEY) {
Jason Hobbs370d1e32011-06-23 08:27:30 +0000415 s = getenv("menucmd");
Jason Hobbsc8a20792011-08-31 05:37:24 +0000416 if (s)
417 run_command2(s, 0);
wdenkc7de8292002-11-19 11:04:11 +0000418 }
419#endif /* CONFIG_MENUKEY */
Wolfgang Denk953b7e62010-06-13 18:28:54 +0200420#endif /* CONFIG_BOOTDELAY */
wdenkc7de8292002-11-19 11:04:11 +0000421
wdenkc6097192002-11-03 00:24:07 +0000422 /*
423 * Main Loop for Monitor Command Processing
424 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200425#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000426 parse_file_outer();
427 /* This point is never reached */
428 for (;;);
429#else
430 for (;;) {
431#ifdef CONFIG_BOOT_RETRY_TIME
432 if (rc >= 0) {
433 /* Saw enough of a valid command to
434 * restart the timeout.
435 */
436 reset_cmd_timeout();
437 }
438#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200439 len = readline (CONFIG_SYS_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000440
441 flag = 0; /* assume no special flags for now */
442 if (len > 0)
443 strcpy (lastcommand, console_buffer);
444 else if (len == 0)
445 flag |= CMD_FLAG_REPEAT;
446#ifdef CONFIG_BOOT_RETRY_TIME
447 else if (len == -2) {
448 /* -2 means timed out, retry autoboot
449 */
wdenk4b9206e2004-03-23 22:14:11 +0000450 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000451# ifdef CONFIG_RESET_TO_RETRY
452 /* Reinit board to run initialization code again */
453 do_reset (NULL, 0, 0, NULL);
454# else
455 return; /* retry autoboot */
456# endif
457 }
458#endif
459
460 if (len == -1)
wdenk4b9206e2004-03-23 22:14:11 +0000461 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000462 else
463 rc = run_command (lastcommand, flag);
464
465 if (rc <= 0) {
466 /* invalid command or not repeatable, forget it */
467 lastcommand[0] = 0;
468 }
469 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200470#endif /*CONFIG_SYS_HUSH_PARSER*/
wdenkc6097192002-11-03 00:24:07 +0000471}
472
wdenk6dd652f2003-06-19 23:40:20 +0000473#ifdef CONFIG_BOOT_RETRY_TIME
474/***************************************************************************
Wolfgang Denk19973b62006-10-28 00:38:39 +0200475 * initialize command line timeout
wdenk6dd652f2003-06-19 23:40:20 +0000476 */
477void init_cmd_timeout(void)
478{
479 char *s = getenv ("bootretry");
480
481 if (s != NULL)
wdenkb028f712003-12-07 21:39:28 +0000482 retry_time = (int)simple_strtol(s, NULL, 10);
wdenk6dd652f2003-06-19 23:40:20 +0000483 else
484 retry_time = CONFIG_BOOT_RETRY_TIME;
485
486 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
487 retry_time = CONFIG_BOOT_RETRY_MIN;
488}
489
wdenkc6097192002-11-03 00:24:07 +0000490/***************************************************************************
491 * reset command line timeout to retry_time seconds
492 */
wdenkc6097192002-11-03 00:24:07 +0000493void reset_cmd_timeout(void)
494{
495 endtime = endtick(retry_time);
496}
497#endif
498
Wolfgang Denk501090a2006-07-21 11:33:45 +0200499#ifdef CONFIG_CMDLINE_EDITING
500
501/*
502 * cmdline-editing related codes from vivi.
503 * Author: Janghoon Lyu <nandy@mizi.com>
504 */
505
Wolfgang Denk501090a2006-07-21 11:33:45 +0200506#define putnstr(str,n) do { \
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700507 printf ("%.*s", (int)n, str); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200508 } while (0)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200509
510#define CTL_CH(c) ((c) - 'a' + 1)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200511#define CTL_BACKSPACE ('\b')
512#define DEL ((char)255)
513#define DEL7 ((char)127)
514#define CREAD_HIST_CHAR ('!')
515
516#define getcmd_putch(ch) putc(ch)
517#define getcmd_getch() getc()
518#define getcmd_cbeep() getcmd_putch('\a')
519
520#define HIST_MAX 20
Peter Tyser8804ae32010-09-29 13:30:56 -0500521#define HIST_SIZE CONFIG_SYS_CBSIZE
Wolfgang Denk501090a2006-07-21 11:33:45 +0200522
523static int hist_max = 0;
524static int hist_add_idx = 0;
525static int hist_cur = -1;
526unsigned hist_num = 0;
527
528char* hist_list[HIST_MAX];
John Schmoller6475b9f2010-03-12 09:49:23 -0600529char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200530
531#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
532
533static void hist_init(void)
534{
535 int i;
536
537 hist_max = 0;
538 hist_add_idx = 0;
539 hist_cur = -1;
540 hist_num = 0;
541
542 for (i = 0; i < HIST_MAX; i++) {
543 hist_list[i] = hist_lines[i];
544 hist_list[i][0] = '\0';
545 }
546}
547
548static void cread_add_to_hist(char *line)
549{
550 strcpy(hist_list[hist_add_idx], line);
551
552 if (++hist_add_idx >= HIST_MAX)
553 hist_add_idx = 0;
554
555 if (hist_add_idx > hist_max)
556 hist_max = hist_add_idx;
557
558 hist_num++;
559}
560
561static char* hist_prev(void)
562{
563 char *ret;
564 int old_cur;
565
566 if (hist_cur < 0)
567 return NULL;
568
569 old_cur = hist_cur;
570 if (--hist_cur < 0)
571 hist_cur = hist_max;
572
573 if (hist_cur == hist_add_idx) {
574 hist_cur = old_cur;
575 ret = NULL;
576 } else
577 ret = hist_list[hist_cur];
578
579 return (ret);
580}
581
582static char* hist_next(void)
583{
584 char *ret;
585
586 if (hist_cur < 0)
587 return NULL;
588
589 if (hist_cur == hist_add_idx)
590 return NULL;
591
592 if (++hist_cur > hist_max)
593 hist_cur = 0;
594
595 if (hist_cur == hist_add_idx) {
596 ret = "";
597 } else
598 ret = hist_list[hist_cur];
599
600 return (ret);
601}
602
Stefan Roese3ca91222006-07-27 16:11:19 +0200603#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk501090a2006-07-21 11:33:45 +0200604static void cread_print_hist_list(void)
605{
606 int i;
607 unsigned long n;
608
609 n = hist_num - hist_max;
610
611 i = hist_add_idx + 1;
612 while (1) {
613 if (i > hist_max)
614 i = 0;
615 if (i == hist_add_idx)
616 break;
617 printf("%s\n", hist_list[i]);
618 n++;
619 i++;
620 }
621}
Stefan Roese3ca91222006-07-27 16:11:19 +0200622#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200623
624#define BEGINNING_OF_LINE() { \
625 while (num) { \
626 getcmd_putch(CTL_BACKSPACE); \
627 num--; \
628 } \
629}
630
631#define ERASE_TO_EOL() { \
632 if (num < eol_num) { \
Mike Frysinger8faba482010-07-23 05:28:15 -0400633 printf("%*s", (int)(eol_num - num), ""); \
634 do { \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200635 getcmd_putch(CTL_BACKSPACE); \
Mike Frysinger8faba482010-07-23 05:28:15 -0400636 } while (--eol_num > num); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200637 } \
638}
639
640#define REFRESH_TO_EOL() { \
641 if (num < eol_num) { \
642 wlen = eol_num - num; \
643 putnstr(buf + num, wlen); \
644 num = eol_num; \
645 } \
646}
647
648static void cread_add_char(char ichar, int insert, unsigned long *num,
649 unsigned long *eol_num, char *buf, unsigned long len)
650{
651 unsigned long wlen;
652
653 /* room ??? */
654 if (insert || *num == *eol_num) {
655 if (*eol_num > len - 1) {
656 getcmd_cbeep();
657 return;
658 }
659 (*eol_num)++;
660 }
661
662 if (insert) {
663 wlen = *eol_num - *num;
664 if (wlen > 1) {
665 memmove(&buf[*num+1], &buf[*num], wlen-1);
666 }
667
668 buf[*num] = ichar;
669 putnstr(buf + *num, wlen);
670 (*num)++;
671 while (--wlen) {
672 getcmd_putch(CTL_BACKSPACE);
673 }
674 } else {
675 /* echo the character */
676 wlen = 1;
677 buf[*num] = ichar;
678 putnstr(buf + *num, wlen);
679 (*num)++;
680 }
681}
682
683static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
684 unsigned long *eol_num, char *buf, unsigned long len)
685{
686 while (strsize--) {
687 cread_add_char(*str, insert, num, eol_num, buf, len);
688 str++;
689 }
690}
691
Heiko Schocher9c348312012-01-16 21:13:05 +0000692static int cread_line(const char *const prompt, char *buf, unsigned int *len,
693 int timeout)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200694{
695 unsigned long num = 0;
696 unsigned long eol_num = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200697 unsigned long wlen;
698 char ichar;
699 int insert = 1;
700 int esc_len = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200701 char esc_save[8];
Peter Tyserecc55002009-10-25 15:12:54 -0500702 int init_len = strlen(buf);
Heiko Schocher9c348312012-01-16 21:13:05 +0000703 int first = 1;
Peter Tyserecc55002009-10-25 15:12:54 -0500704
705 if (init_len)
706 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
Wolfgang Denk501090a2006-07-21 11:33:45 +0200707
708 while (1) {
Andreas Engel00ac50e2008-01-09 17:10:56 +0100709#ifdef CONFIG_BOOT_RETRY_TIME
710 while (!tstc()) { /* while no incoming data */
711 if (retry_time >= 0 && get_ticks() > endtime)
712 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +0200713 WATCHDOG_RESET();
Andreas Engel00ac50e2008-01-09 17:10:56 +0100714 }
715#endif
Heiko Schocher9c348312012-01-16 21:13:05 +0000716 if (first && timeout) {
717 uint64_t etime = endtick(timeout);
718
719 while (!tstc()) { /* while no incoming data */
720 if (get_ticks() >= etime)
721 return -2; /* timed out */
722 WATCHDOG_RESET();
723 }
724 first = 0;
725 }
Andreas Engel00ac50e2008-01-09 17:10:56 +0100726
Wolfgang Denk501090a2006-07-21 11:33:45 +0200727 ichar = getcmd_getch();
728
729 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200730 putc('\n');
Wolfgang Denk501090a2006-07-21 11:33:45 +0200731 break;
732 }
733
734 /*
735 * handle standard linux xterm esc sequences for arrow key, etc.
736 */
737 if (esc_len != 0) {
738 if (esc_len == 1) {
739 if (ichar == '[') {
740 esc_save[esc_len] = ichar;
741 esc_len = 2;
742 } else {
743 cread_add_str(esc_save, esc_len, insert,
744 &num, &eol_num, buf, *len);
745 esc_len = 0;
746 }
747 continue;
748 }
749
750 switch (ichar) {
751
752 case 'D': /* <- key */
753 ichar = CTL_CH('b');
754 esc_len = 0;
755 break;
756 case 'C': /* -> key */
757 ichar = CTL_CH('f');
758 esc_len = 0;
759 break; /* pass off to ^F handler */
760 case 'H': /* Home key */
761 ichar = CTL_CH('a');
762 esc_len = 0;
763 break; /* pass off to ^A handler */
764 case 'A': /* up arrow */
765 ichar = CTL_CH('p');
766 esc_len = 0;
767 break; /* pass off to ^P handler */
768 case 'B': /* down arrow */
769 ichar = CTL_CH('n');
770 esc_len = 0;
771 break; /* pass off to ^N handler */
772 default:
773 esc_save[esc_len++] = ichar;
774 cread_add_str(esc_save, esc_len, insert,
775 &num, &eol_num, buf, *len);
776 esc_len = 0;
777 continue;
778 }
779 }
780
781 switch (ichar) {
782 case 0x1b:
783 if (esc_len == 0) {
784 esc_save[esc_len] = ichar;
785 esc_len = 1;
786 } else {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200787 puts("impossible condition #876\n");
Wolfgang Denk501090a2006-07-21 11:33:45 +0200788 esc_len = 0;
789 }
790 break;
791
792 case CTL_CH('a'):
793 BEGINNING_OF_LINE();
794 break;
795 case CTL_CH('c'): /* ^C - break */
796 *buf = '\0'; /* discard input */
797 return (-1);
798 case CTL_CH('f'):
799 if (num < eol_num) {
800 getcmd_putch(buf[num]);
801 num++;
802 }
803 break;
804 case CTL_CH('b'):
805 if (num) {
806 getcmd_putch(CTL_BACKSPACE);
807 num--;
808 }
809 break;
810 case CTL_CH('d'):
811 if (num < eol_num) {
812 wlen = eol_num - num - 1;
813 if (wlen) {
814 memmove(&buf[num], &buf[num+1], wlen);
815 putnstr(buf + num, wlen);
816 }
817
818 getcmd_putch(' ');
819 do {
820 getcmd_putch(CTL_BACKSPACE);
821 } while (wlen--);
822 eol_num--;
823 }
824 break;
825 case CTL_CH('k'):
826 ERASE_TO_EOL();
827 break;
828 case CTL_CH('e'):
829 REFRESH_TO_EOL();
830 break;
831 case CTL_CH('o'):
832 insert = !insert;
833 break;
834 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100835 case CTL_CH('u'):
Wolfgang Denk501090a2006-07-21 11:33:45 +0200836 BEGINNING_OF_LINE();
837 ERASE_TO_EOL();
838 break;
839 case DEL:
840 case DEL7:
841 case 8:
842 if (num) {
843 wlen = eol_num - num;
844 num--;
845 memmove(&buf[num], &buf[num+1], wlen);
846 getcmd_putch(CTL_BACKSPACE);
847 putnstr(buf + num, wlen);
848 getcmd_putch(' ');
849 do {
850 getcmd_putch(CTL_BACKSPACE);
851 } while (wlen--);
852 eol_num--;
853 }
854 break;
855 case CTL_CH('p'):
856 case CTL_CH('n'):
857 {
858 char * hline;
859
860 esc_len = 0;
861
862 if (ichar == CTL_CH('p'))
863 hline = hist_prev();
864 else
865 hline = hist_next();
866
867 if (!hline) {
868 getcmd_cbeep();
869 continue;
870 }
871
872 /* nuke the current line */
873 /* first, go home */
874 BEGINNING_OF_LINE();
875
876 /* erase to end of line */
877 ERASE_TO_EOL();
878
879 /* copy new line into place and display */
880 strcpy(buf, hline);
881 eol_num = strlen(buf);
882 REFRESH_TO_EOL();
883 continue;
884 }
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100885#ifdef CONFIG_AUTO_COMPLETE
886 case '\t': {
887 int num2, col;
888
889 /* do not autocomplete when in the middle */
890 if (num < eol_num) {
891 getcmd_cbeep();
892 break;
893 }
894
895 buf[num] = '\0';
896 col = strlen(prompt) + eol_num;
897 num2 = num;
898 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
899 col = num2 - num;
900 num += col;
901 eol_num += col;
902 }
903 break;
904 }
905#endif
Wolfgang Denk501090a2006-07-21 11:33:45 +0200906 default:
907 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
908 break;
909 }
910 }
911 *len = eol_num;
912 buf[eol_num] = '\0'; /* lose the newline */
913
914 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
915 cread_add_to_hist(buf);
916 hist_cur = hist_add_idx;
917
Peter Tyserf9239432009-10-25 15:12:53 -0500918 return 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200919}
920
921#endif /* CONFIG_CMDLINE_EDITING */
922
wdenkc6097192002-11-03 00:24:07 +0000923/****************************************************************************/
924
925/*
926 * Prompt for input and read a line.
927 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
928 * time out when time goes past endtime (timebase time in ticks).
929 * Return: number of read characters
930 * -1 if break
931 * -2 if timed out
932 */
933int readline (const char *const prompt)
934{
Peter Tyserecc55002009-10-25 15:12:54 -0500935 /*
936 * If console_buffer isn't 0-length the user will be prompted to modify
937 * it instead of entering it from scratch as desired.
938 */
939 console_buffer[0] = '\0';
940
Heiko Schocher9c348312012-01-16 21:13:05 +0000941 return readline_into_buffer(prompt, console_buffer, 0);
James Yang6636b622008-01-09 11:17:49 -0600942}
943
944
Heiko Schocher9c348312012-01-16 21:13:05 +0000945int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
James Yang6636b622008-01-09 11:17:49 -0600946{
947 char *p = buffer;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200948#ifdef CONFIG_CMDLINE_EDITING
Peter Tyser8804ae32010-09-29 13:30:56 -0500949 unsigned int len = CONFIG_SYS_CBSIZE;
Stefan Roesed8f961b2006-08-07 15:08:44 +0200950 int rc;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200951 static int initted = 0;
952
James Yang597f6c22008-05-05 10:22:53 -0500953 /*
954 * History uses a global array which is not
955 * writable until after relocation to RAM.
956 * Revert to non-history version if still
957 * running from flash.
958 */
959 if (gd->flags & GD_FLG_RELOC) {
960 if (!initted) {
961 hist_init();
962 initted = 1;
963 }
964
Peter Tysere491a712009-10-25 15:12:52 -0500965 if (prompt)
966 puts (prompt);
James Yang597f6c22008-05-05 10:22:53 -0500967
Heiko Schocher9c348312012-01-16 21:13:05 +0000968 rc = cread_line(prompt, p, &len, timeout);
James Yang597f6c22008-05-05 10:22:53 -0500969 return rc < 0 ? rc : len;
970
971 } else {
972#endif /* CONFIG_CMDLINE_EDITING */
Kumar Gala0ec59522008-01-10 02:22:05 -0600973 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +0000974 int n = 0; /* buffer index */
975 int plen = 0; /* prompt length */
976 int col; /* output column cnt */
977 char c;
978
979 /* print prompt */
980 if (prompt) {
981 plen = strlen (prompt);
982 puts (prompt);
983 }
984 col = plen;
985
986 for (;;) {
987#ifdef CONFIG_BOOT_RETRY_TIME
988 while (!tstc()) { /* while no incoming data */
989 if (retry_time >= 0 && get_ticks() > endtime)
990 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +0200991 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000992 }
993#endif
994 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
995
996#ifdef CONFIG_SHOW_ACTIVITY
997 while (!tstc()) {
998 extern void show_activity(int arg);
999 show_activity(0);
Jens Scharsig30dc1652010-04-09 19:02:38 +02001000 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001001 }
1002#endif
1003 c = getc();
1004
1005 /*
1006 * Special character handling
1007 */
1008 switch (c) {
1009 case '\r': /* Enter */
1010 case '\n':
1011 *p = '\0';
1012 puts ("\r\n");
James Yang6636b622008-01-09 11:17:49 -06001013 return (p - p_buf);
wdenkc6097192002-11-03 00:24:07 +00001014
wdenk27aa8182004-03-23 22:37:33 +00001015 case '\0': /* nul */
1016 continue;
1017
wdenkc6097192002-11-03 00:24:07 +00001018 case 0x03: /* ^C - break */
James Yang6636b622008-01-09 11:17:49 -06001019 p_buf[0] = '\0'; /* discard input */
wdenkc6097192002-11-03 00:24:07 +00001020 return (-1);
1021
1022 case 0x15: /* ^U - erase line */
1023 while (col > plen) {
1024 puts (erase_seq);
1025 --col;
1026 }
James Yang6636b622008-01-09 11:17:49 -06001027 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001028 n = 0;
1029 continue;
1030
Wolfgang Denk1636d1c2007-06-22 23:59:00 +02001031 case 0x17: /* ^W - erase word */
James Yang6636b622008-01-09 11:17:49 -06001032 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001033 while ((n > 0) && (*p != ' ')) {
James Yang6636b622008-01-09 11:17:49 -06001034 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001035 }
1036 continue;
1037
1038 case 0x08: /* ^H - backspace */
1039 case 0x7F: /* DEL - backspace */
James Yang6636b622008-01-09 11:17:49 -06001040 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001041 continue;
1042
1043 default:
1044 /*
1045 * Must be a normal character then
1046 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001047 if (n < CONFIG_SYS_CBSIZE-2) {
wdenkc6097192002-11-03 00:24:07 +00001048 if (c == '\t') { /* expand TABs */
wdenk04a85b32004-04-15 18:22:41 +00001049#ifdef CONFIG_AUTO_COMPLETE
1050 /* if auto completion triggered just continue */
1051 *p = '\0';
1052 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang6636b622008-01-09 11:17:49 -06001053 p = p_buf + n; /* reset */
wdenk04a85b32004-04-15 18:22:41 +00001054 continue;
1055 }
1056#endif
wdenkc6097192002-11-03 00:24:07 +00001057 puts (tab_seq+(col&07));
1058 col += 8 - (col&07);
1059 } else {
1060 ++col; /* echo input */
1061 putc (c);
1062 }
1063 *p++ = c;
1064 ++n;
1065 } else { /* Buffer full */
1066 putc ('\a');
1067 }
1068 }
1069 }
James Yang597f6c22008-05-05 10:22:53 -05001070#ifdef CONFIG_CMDLINE_EDITING
1071 }
1072#endif
wdenkc6097192002-11-03 00:24:07 +00001073}
1074
1075/****************************************************************************/
1076
1077static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1078{
1079 char *s;
1080
1081 if (*np == 0) {
1082 return (p);
1083 }
1084
1085 if (*(--p) == '\t') { /* will retype the whole line */
1086 while (*colp > plen) {
1087 puts (erase_seq);
1088 (*colp)--;
1089 }
1090 for (s=buffer; s<p; ++s) {
1091 if (*s == '\t') {
1092 puts (tab_seq+((*colp) & 07));
1093 *colp += 8 - ((*colp) & 07);
1094 } else {
1095 ++(*colp);
1096 putc (*s);
1097 }
1098 }
1099 } else {
1100 puts (erase_seq);
1101 (*colp)--;
1102 }
1103 (*np)--;
1104 return (p);
1105}
1106
1107/****************************************************************************/
1108
1109int parse_line (char *line, char *argv[])
1110{
1111 int nargs = 0;
1112
1113#ifdef DEBUG_PARSER
1114 printf ("parse_line: \"%s\"\n", line);
1115#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001116 while (nargs < CONFIG_SYS_MAXARGS) {
wdenkc6097192002-11-03 00:24:07 +00001117
1118 /* skip any white space */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001119 while (isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001120 ++line;
wdenkc6097192002-11-03 00:24:07 +00001121
1122 if (*line == '\0') { /* end of line, no more args */
1123 argv[nargs] = NULL;
1124#ifdef DEBUG_PARSER
1125 printf ("parse_line: nargs=%d\n", nargs);
1126#endif
1127 return (nargs);
1128 }
1129
1130 argv[nargs++] = line; /* begin of argument string */
1131
1132 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001133 while (*line && !isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001134 ++line;
wdenkc6097192002-11-03 00:24:07 +00001135
1136 if (*line == '\0') { /* end of line, no more args */
1137 argv[nargs] = NULL;
1138#ifdef DEBUG_PARSER
1139 printf ("parse_line: nargs=%d\n", nargs);
1140#endif
1141 return (nargs);
1142 }
1143
1144 *line++ = '\0'; /* terminate current arg */
1145 }
1146
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001147 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
wdenkc6097192002-11-03 00:24:07 +00001148
1149#ifdef DEBUG_PARSER
1150 printf ("parse_line: nargs=%d\n", nargs);
1151#endif
1152 return (nargs);
1153}
1154
1155/****************************************************************************/
1156
1157static void process_macros (const char *input, char *output)
1158{
1159 char c, prev;
1160 const char *varname_start = NULL;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001161 int inputcnt = strlen (input);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001162 int outputcnt = CONFIG_SYS_CBSIZE;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001163 int state = 0; /* 0 = waiting for '$' */
1164
1165 /* 1 = waiting for '(' or '{' */
1166 /* 2 = waiting for ')' or '}' */
1167 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001168#ifdef DEBUG_PARSER
1169 char *output_start = output;
1170
Wolfgang Denk19973b62006-10-28 00:38:39 +02001171 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1172 input);
wdenkc6097192002-11-03 00:24:07 +00001173#endif
1174
Wolfgang Denk19973b62006-10-28 00:38:39 +02001175 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001176
1177 while (inputcnt && outputcnt) {
wdenk8bde7f72003-06-27 21:31:46 +00001178 c = *input++;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001179 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001180
Wolfgang Denk19973b62006-10-28 00:38:39 +02001181 if (state != 3) {
1182 /* remove one level of escape characters */
1183 if ((c == '\\') && (prev != '\\')) {
1184 if (inputcnt-- == 0)
1185 break;
1186 prev = c;
1187 c = *input++;
1188 }
wdenka25f8622003-01-02 23:57:29 +00001189 }
wdenkc6097192002-11-03 00:24:07 +00001190
Wolfgang Denk19973b62006-10-28 00:38:39 +02001191 switch (state) {
1192 case 0: /* Waiting for (unescaped) $ */
1193 if ((c == '\'') && (prev != '\\')) {
1194 state = 3;
1195 break;
1196 }
1197 if ((c == '$') && (prev != '\\')) {
1198 state++;
1199 } else {
wdenkc6097192002-11-03 00:24:07 +00001200 *(output++) = c;
1201 outputcnt--;
1202 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001203 break;
1204 case 1: /* Waiting for ( */
1205 if (c == '(' || c == '{') {
1206 state++;
1207 varname_start = input;
1208 } else {
1209 state = 0;
1210 *(output++) = '$';
1211 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001212
Wolfgang Denk19973b62006-10-28 00:38:39 +02001213 if (outputcnt) {
1214 *(output++) = c;
wdenkc6097192002-11-03 00:24:07 +00001215 outputcnt--;
1216 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001217 }
1218 break;
1219 case 2: /* Waiting for ) */
1220 if (c == ')' || c == '}') {
1221 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001222 char envname[CONFIG_SYS_CBSIZE], *envval;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001223 int envcnt = input - varname_start - 1; /* Varname # of chars */
1224
1225 /* Get the varname */
1226 for (i = 0; i < envcnt; i++) {
1227 envname[i] = varname_start[i];
1228 }
1229 envname[i] = 0;
1230
1231 /* Get its value */
1232 envval = getenv (envname);
1233
1234 /* Copy into the line if it exists */
1235 if (envval != NULL)
1236 while ((*envval) && outputcnt) {
1237 *(output++) = *(envval++);
1238 outputcnt--;
1239 }
1240 /* Look for another '$' */
1241 state = 0;
1242 }
1243 break;
1244 case 3: /* Waiting for ' */
1245 if ((c == '\'') && (prev != '\\')) {
1246 state = 0;
1247 } else {
1248 *(output++) = c;
1249 outputcnt--;
1250 }
1251 break;
wdenkc6097192002-11-03 00:24:07 +00001252 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001253 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001254 }
1255
1256 if (outputcnt)
1257 *output = 0;
Bartlomiej Sieka9160b962007-05-27 17:04:18 +02001258 else
1259 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001260
1261#ifdef DEBUG_PARSER
1262 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
Wolfgang Denk19973b62006-10-28 00:38:39 +02001263 strlen (output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001264#endif
1265}
1266
1267/****************************************************************************
1268 * returns:
1269 * 1 - command executed, repeatable
1270 * 0 - command executed but not repeatable, interrupted commands are
1271 * always considered not repeatable
1272 * -1 - not executed (unrecognized, bootd recursion or too many args)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001273 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
wdenkc6097192002-11-03 00:24:07 +00001274 * considered unrecognized)
1275 *
1276 * WARNING:
1277 *
1278 * We must create a temporary copy of the command since the command we get
1279 * may be the result from getenv(), which returns a pointer directly to
1280 * the environment data, which may change magicly when the command we run
1281 * creates or modifies environment variables (like "bootp" does).
1282 */
1283
1284int run_command (const char *cmd, int flag)
1285{
1286 cmd_tbl_t *cmdtp;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001287 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
wdenkc6097192002-11-03 00:24:07 +00001288 char *token; /* start of token in cmdbuf */
1289 char *sep; /* end of token (separator) in cmdbuf */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001290 char finaltoken[CONFIG_SYS_CBSIZE];
wdenkc6097192002-11-03 00:24:07 +00001291 char *str = cmdbuf;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001292 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenkf07771c2003-05-28 08:06:31 +00001293 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001294 int repeatable = 1;
wdenkf07771c2003-05-28 08:06:31 +00001295 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001296
1297#ifdef DEBUG_PARSER
1298 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1299 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1300 puts ("\"\n");
1301#endif
1302
1303 clear_ctrlc(); /* forget any previous Control C */
1304
1305 if (!cmd || !*cmd) {
1306 return -1; /* empty command */
1307 }
1308
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001309 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
wdenkc6097192002-11-03 00:24:07 +00001310 puts ("## Command too long!\n");
1311 return -1;
1312 }
1313
1314 strcpy (cmdbuf, cmd);
1315
1316 /* Process separators and check for invalid
1317 * repeatable commands
1318 */
1319
1320#ifdef DEBUG_PARSER
1321 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1322#endif
1323 while (*str) {
1324
1325 /*
1326 * Find separator, or string end
1327 * Allow simple escape of ';' by writing "\;"
1328 */
wdenka25f8622003-01-02 23:57:29 +00001329 for (inquotes = 0, sep = str; *sep; sep++) {
1330 if ((*sep=='\'') &&
1331 (*(sep-1) != '\\'))
1332 inquotes=!inquotes;
1333
1334 if (!inquotes &&
1335 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001336 ( sep != str) && /* past string start */
1337 (*(sep-1) != '\\')) /* and NOT escaped */
1338 break;
1339 }
1340
1341 /*
1342 * Limit the token to data between separators
1343 */
1344 token = str;
1345 if (*sep) {
1346 str = sep + 1; /* start of command for next pass */
1347 *sep = '\0';
1348 }
1349 else
1350 str = sep; /* no more commands for next pass */
1351#ifdef DEBUG_PARSER
1352 printf ("token: \"%s\"\n", token);
1353#endif
1354
1355 /* find macros in this token and replace them */
1356 process_macros (token, finaltoken);
1357
1358 /* Extract arguments */
Wolfgang Denk1264b402006-03-12 02:20:55 +01001359 if ((argc = parse_line (finaltoken, argv)) == 0) {
1360 rc = -1; /* no command at all */
1361 continue;
1362 }
wdenkc6097192002-11-03 00:24:07 +00001363
1364 /* Look up command in command table */
1365 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1366 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
wdenkf07771c2003-05-28 08:06:31 +00001367 rc = -1; /* give up after bad command */
1368 continue;
wdenkc6097192002-11-03 00:24:07 +00001369 }
1370
1371 /* found - check max args */
1372 if (argc > cmdtp->maxargs) {
Peter Tyser62c3ae72009-01-27 18:03:10 -06001373 cmd_usage(cmdtp);
wdenkf07771c2003-05-28 08:06:31 +00001374 rc = -1;
1375 continue;
wdenkc6097192002-11-03 00:24:07 +00001376 }
1377
Jon Loeligerc3517f92007-07-08 18:10:08 -05001378#if defined(CONFIG_CMD_BOOTD)
wdenkc6097192002-11-03 00:24:07 +00001379 /* avoid "bootd" recursion */
1380 if (cmdtp->cmd == do_bootd) {
1381#ifdef DEBUG_PARSER
1382 printf ("[%s]\n", finaltoken);
1383#endif
1384 if (flag & CMD_FLAG_BOOTD) {
wdenk4b9206e2004-03-23 22:14:11 +00001385 puts ("'bootd' recursion detected\n");
wdenkf07771c2003-05-28 08:06:31 +00001386 rc = -1;
1387 continue;
Wolfgang Denk1264b402006-03-12 02:20:55 +01001388 } else {
wdenkc6097192002-11-03 00:24:07 +00001389 flag |= CMD_FLAG_BOOTD;
Wolfgang Denk1264b402006-03-12 02:20:55 +01001390 }
wdenkc6097192002-11-03 00:24:07 +00001391 }
Jon Loeliger90253172007-07-10 11:02:44 -05001392#endif
wdenkc6097192002-11-03 00:24:07 +00001393
1394 /* OK - call function to do the command */
1395 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
wdenkf07771c2003-05-28 08:06:31 +00001396 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001397 }
1398
1399 repeatable &= cmdtp->repeatable;
1400
1401 /* Did the user stop this? */
1402 if (had_ctrlc ())
Detlev Zundel5afb2022007-05-23 18:47:48 +02001403 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001404 }
1405
wdenkf07771c2003-05-28 08:06:31 +00001406 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001407}
1408
1409/****************************************************************************/
1410
Jon Loeligerc3517f92007-07-08 18:10:08 -05001411#if defined(CONFIG_CMD_RUN)
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001412int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +00001413{
1414 int i;
wdenkc6097192002-11-03 00:24:07 +00001415
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001416 if (argc < 2)
1417 return cmd_usage(cmdtp);
wdenkc6097192002-11-03 00:24:07 +00001418
1419 for (i=1; i<argc; ++i) {
wdenk3e386912003-04-05 00:53:31 +00001420 char *arg;
1421
1422 if ((arg = getenv (argv[i])) == NULL) {
1423 printf ("## Error: \"%s\" not defined\n", argv[i]);
1424 return 1;
1425 }
Jason Hobbsc8a20792011-08-31 05:37:24 +00001426
1427 if (run_command2(arg, flag) != 0)
wdenk3e386912003-04-05 00:53:31 +00001428 return 1;
wdenkc6097192002-11-03 00:24:07 +00001429 }
wdenk3e386912003-04-05 00:53:31 +00001430 return 0;
wdenkc6097192002-11-03 00:24:07 +00001431}
Jon Loeliger90253172007-07-10 11:02:44 -05001432#endif