blob: 0a39c05d9413bdf761e45358d8554b6bf82e9858 [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
Justin L Wernerdc80e002012-08-09 14:20:30 -060046#if defined(CONFIG_BOOTP_VENDOREX) && defined(CONFIG_BOOTP_VENDOREX_PXE_SHARED)
47#include "../net/magic.h"
48#endif
49
James Yang597f6c22008-05-05 10:22:53 -050050#if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
Wolfgang Denkd87080b2006-03-31 18:32:53 +020051DECLARE_GLOBAL_DATA_PTR;
52#endif
53
Heiko Schocherfad63402007-07-13 09:54:17 +020054/*
55 * Board-specific Platform code can reimplement show_boot_progress () if needed
56 */
57void inline __show_boot_progress (int val) {}
Emil Medve5e2c08c2009-05-12 13:48:32 -050058void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
Heiko Schocherfad63402007-07-13 09:54:17 +020059
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020060#if defined(CONFIG_UPDATE_TFTP)
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +000061int update_tftp (ulong addr);
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020062#endif /* CONFIG_UPDATE_TFTP */
wdenk8bde7f72003-06-27 21:31:46 +000063
wdenkc6097192002-11-03 00:24:07 +000064#define MAX_DELAY_STOP_STR 32
65
wdenkc6097192002-11-03 00:24:07 +000066#undef DEBUG_PARSER
67
John Schmoller6475b9f2010-03-12 09:49:23 -060068char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
wdenkc6097192002-11-03 00:24:07 +000069
Stefan Roese3ca91222006-07-27 16:11:19 +020070static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
Mike Frysinger82359ae2010-12-22 09:40:45 -050071static const char erase_seq[] = "\b \b"; /* erase sequence */
72static const char tab_seq[] = " "; /* used to expand TABs */
wdenkc6097192002-11-03 00:24:07 +000073
74#ifdef CONFIG_BOOT_RETRY_TIME
75static uint64_t endtime = 0; /* must be set, default is instant timeout */
76static int retry_time = -1; /* -1 so can call readline before main_loop */
77#endif
78
79#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
80
81#ifndef CONFIG_BOOT_RETRY_MIN
82#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
83#endif
84
85#ifdef CONFIG_MODEM_SUPPORT
86int do_mdm_init = 0;
87extern void mdm_init(void); /* defined in board.c */
88#endif
89
90/***************************************************************************
91 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
Jason Hobbsc8a20792011-08-31 05:37:24 +000092 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
wdenkc6097192002-11-03 00:24:07 +000093 */
94#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
95# if defined(CONFIG_AUTOBOOT_KEYED)
Jason Hobbsb41bc5a2011-08-23 11:06:50 +000096#ifndef CONFIG_MENU
97static inline
98#endif
99int abortboot(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000100{
101 int abort = 0;
102 uint64_t etime = endtick(bootdelay);
Wolfgang Denk19973b62006-10-28 00:38:39 +0200103 struct {
wdenkc6097192002-11-03 00:24:07 +0000104 char* str;
105 u_int len;
106 int retry;
107 }
Wolfgang Denk19973b62006-10-28 00:38:39 +0200108 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +0000109 { str: getenv ("bootdelaykey"), retry: 1 },
110 { str: getenv ("bootdelaykey2"), retry: 1 },
111 { str: getenv ("bootstopkey"), retry: 0 },
112 { str: getenv ("bootstopkey2"), retry: 0 },
113 };
114
115 char presskey [MAX_DELAY_STOP_STR];
116 u_int presskey_len = 0;
117 u_int presskey_max = 0;
118 u_int i;
119
120# ifdef CONFIG_AUTOBOOT_PROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200121 printf(CONFIG_AUTOBOOT_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000122# endif
123
124# ifdef CONFIG_AUTOBOOT_DELAY_STR
125 if (delaykey[0].str == NULL)
126 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
127# endif
128# ifdef CONFIG_AUTOBOOT_DELAY_STR2
129 if (delaykey[1].str == NULL)
130 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
131# endif
132# ifdef CONFIG_AUTOBOOT_STOP_STR
133 if (delaykey[2].str == NULL)
134 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
135# endif
136# ifdef CONFIG_AUTOBOOT_STOP_STR2
137 if (delaykey[3].str == NULL)
138 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
139# endif
140
141 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
142 delaykey[i].len = delaykey[i].str == NULL ?
143 0 : strlen (delaykey[i].str);
144 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
145 MAX_DELAY_STOP_STR : delaykey[i].len;
146
147 presskey_max = presskey_max > delaykey[i].len ?
148 presskey_max : delaykey[i].len;
149
150# if DEBUG_BOOTKEYS
151 printf("%s key:<%s>\n",
152 delaykey[i].retry ? "delay" : "stop",
153 delaykey[i].str ? delaykey[i].str : "NULL");
154# endif
155 }
156
157 /* In order to keep up with incoming data, check timeout only
158 * when catch up.
159 */
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100160 do {
161 if (tstc()) {
162 if (presskey_len < presskey_max) {
163 presskey [presskey_len ++] = getc();
164 }
165 else {
166 for (i = 0; i < presskey_max - 1; i ++)
167 presskey [i] = presskey [i + 1];
168
169 presskey [i] = getc();
170 }
171 }
172
wdenkc6097192002-11-03 00:24:07 +0000173 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
174 if (delaykey[i].len > 0 &&
175 presskey_len >= delaykey[i].len &&
176 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk8bde7f72003-06-27 21:31:46 +0000177 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000178 delaykey[i].len) == 0) {
179# if DEBUG_BOOTKEYS
180 printf("got %skey\n",
181 delaykey[i].retry ? "delay" : "stop");
182# endif
183
184# ifdef CONFIG_BOOT_RETRY_TIME
185 /* don't retry auto boot */
186 if (! delaykey[i].retry)
187 retry_time = -1;
188# endif
189 abort = 1;
190 }
191 }
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100192 } while (!abort && get_ticks() <= etime);
wdenkc6097192002-11-03 00:24:07 +0000193
wdenkc6097192002-11-03 00:24:07 +0000194# if DEBUG_BOOTKEYS
195 if (!abort)
Ladislav Michlada4d402007-04-25 16:01:26 +0200196 puts("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000197# endif
198
dzu8cb81432003-10-24 13:14:45 +0000199#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200200 if (abort)
201 gd->flags &= ~GD_FLG_SILENT;
dzu8cb81432003-10-24 13:14:45 +0000202#endif
203
wdenkc6097192002-11-03 00:24:07 +0000204 return abort;
205}
206
207# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
208
wdenkc7de8292002-11-19 11:04:11 +0000209#ifdef CONFIG_MENUKEY
210static int menukey = 0;
211#endif
212
Jason Hobbsb41bc5a2011-08-23 11:06:50 +0000213#ifndef CONFIG_MENU
214static inline
215#endif
216int abortboot(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000217{
218 int abort = 0;
219
wdenkc7de8292002-11-19 11:04:11 +0000220#ifdef CONFIG_MENUPROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200221 printf(CONFIG_MENUPROMPT);
wdenkc7de8292002-11-19 11:04:11 +0000222#else
wdenkc6097192002-11-03 00:24:07 +0000223 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenkc7de8292002-11-19 11:04:11 +0000224#endif
wdenkc6097192002-11-03 00:24:07 +0000225
226#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk8bde7f72003-06-27 21:31:46 +0000227 /*
228 * Check if key already pressed
229 * Don't check if bootdelay < 0
230 */
wdenkc6097192002-11-03 00:24:07 +0000231 if (bootdelay >= 0) {
232 if (tstc()) { /* we got a key press */
233 (void) getc(); /* consume input */
wdenk4b9206e2004-03-23 22:14:11 +0000234 puts ("\b\b\b 0");
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200235 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000236 }
wdenk8bde7f72003-06-27 21:31:46 +0000237 }
wdenkc6097192002-11-03 00:24:07 +0000238#endif
239
wdenkf72da342003-10-10 10:05:42 +0000240 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000241 int i;
242
243 --bootdelay;
244 /* delay 100 * 10ms */
245 for (i=0; !abort && i<100; ++i) {
246 if (tstc()) { /* we got a key press */
247 abort = 1; /* don't auto boot */
248 bootdelay = 0; /* no more delay */
wdenkc7de8292002-11-19 11:04:11 +0000249# ifdef CONFIG_MENUKEY
250 menukey = getc();
251# else
wdenkc6097192002-11-03 00:24:07 +0000252 (void) getc(); /* consume input */
wdenkc7de8292002-11-19 11:04:11 +0000253# endif
wdenkc6097192002-11-03 00:24:07 +0000254 break;
255 }
Ladislav Michlada4d402007-04-25 16:01:26 +0200256 udelay(10000);
wdenkc6097192002-11-03 00:24:07 +0000257 }
258
Ladislav Michlada4d402007-04-25 16:01:26 +0200259 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000260 }
261
Ladislav Michlada4d402007-04-25 16:01:26 +0200262 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000263
wdenkf72da342003-10-10 10:05:42 +0000264#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200265 if (abort)
266 gd->flags &= ~GD_FLG_SILENT;
wdenkf72da342003-10-10 10:05:42 +0000267#endif
268
wdenkc6097192002-11-03 00:24:07 +0000269 return abort;
270}
271# endif /* CONFIG_AUTOBOOT_KEYED */
272#endif /* CONFIG_BOOTDELAY >= 0 */
273
274/****************************************************************************/
275
276void main_loop (void)
277{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200278#ifndef CONFIG_SYS_HUSH_PARSER
279 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
wdenkc6097192002-11-03 00:24:07 +0000280 int len;
281 int rc = 1;
282 int flag;
283#endif
284
285#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
286 char *s;
287 int bootdelay;
288#endif
289#ifdef CONFIG_PREBOOT
290 char *p;
291#endif
wdenkbdccc4f2003-08-05 17:43:17 +0000292#ifdef CONFIG_BOOTCOUNT_LIMIT
293 unsigned long bootcount = 0;
294 unsigned long bootlimit = 0;
295 char *bcs;
296 char bcs_set[16];
297#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000298
wdenkbdccc4f2003-08-05 17:43:17 +0000299#ifdef CONFIG_BOOTCOUNT_LIMIT
300 bootcount = bootcount_load();
301 bootcount++;
302 bootcount_store (bootcount);
303 sprintf (bcs_set, "%lu", bootcount);
304 setenv ("bootcount", bcs_set);
305 bcs = getenv ("bootlimit");
306 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
307#endif /* CONFIG_BOOTCOUNT_LIMIT */
308
wdenkc6097192002-11-03 00:24:07 +0000309#ifdef CONFIG_MODEM_SUPPORT
310 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
311 if (do_mdm_init) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200312 char *str = strdup(getenv("mdm_cmd"));
wdenkc6097192002-11-03 00:24:07 +0000313 setenv ("preboot", str); /* set or delete definition */
314 if (str != NULL)
315 free (str);
316 mdm_init(); /* wait for modem connection */
317 }
318#endif /* CONFIG_MODEM_SUPPORT */
319
stroese05875972003-04-04 15:44:49 +0000320#ifdef CONFIG_VERSION_VARIABLE
321 {
stroese155cb012003-07-11 08:00:33 +0000322 setenv ("ver", version_string); /* set version variable */
stroese05875972003-04-04 15:44:49 +0000323 }
324#endif /* CONFIG_VERSION_VARIABLE */
325
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200326#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000327 u_boot_hush_start ();
328#endif
329
Heiko Schocher81473f62008-10-15 09:40:28 +0200330#if defined(CONFIG_HUSH_INIT_VAR)
331 hush_init_var ();
332#endif
333
wdenkc6097192002-11-03 00:24:07 +0000334#ifdef CONFIG_PREBOOT
335 if ((p = getenv ("preboot")) != NULL) {
336# ifdef CONFIG_AUTOBOOT_KEYED
337 int prev = disable_ctrlc(1); /* disable Control C checking */
338# endif
339
Simon Glass009dde12012-02-14 19:59:20 +0000340 run_command(p, 0);
wdenkc6097192002-11-03 00:24:07 +0000341
342# ifdef CONFIG_AUTOBOOT_KEYED
343 disable_ctrlc(prev); /* restore Control C checking */
344# endif
345 }
346#endif /* CONFIG_PREBOOT */
347
Wolfgang Denk143cd212010-03-11 23:56:03 +0100348#if defined(CONFIG_UPDATE_TFTP)
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000349 update_tftp (0UL);
Wolfgang Denk143cd212010-03-11 23:56:03 +0100350#endif /* CONFIG_UPDATE_TFTP */
351
wdenkc6097192002-11-03 00:24:07 +0000352#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
353 s = getenv ("bootdelay");
354 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
355
wdenka6c7ad22002-12-03 21:28:10 +0000356 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000357
Heiko Schocher317d6c52012-01-16 21:13:35 +0000358#if defined(CONFIG_MENU_SHOW)
359 bootdelay = menu_show(bootdelay);
360#endif
wdenkc6097192002-11-03 00:24:07 +0000361# ifdef CONFIG_BOOT_RETRY_TIME
wdenk6dd652f2003-06-19 23:40:20 +0000362 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000363# endif /* CONFIG_BOOT_RETRY_TIME */
364
Yuri Tikhonovb428f6a2008-02-04 14:11:03 +0100365#ifdef CONFIG_POST
366 if (gd->flags & GD_FLG_POSTFAIL) {
367 s = getenv("failbootcmd");
368 }
369 else
370#endif /* CONFIG_POST */
wdenkbdccc4f2003-08-05 17:43:17 +0000371#ifdef CONFIG_BOOTCOUNT_LIMIT
372 if (bootlimit && (bootcount > bootlimit)) {
373 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
374 (unsigned)bootlimit);
375 s = getenv ("altbootcmd");
376 }
377 else
378#endif /* CONFIG_BOOTCOUNT_LIMIT */
379 s = getenv ("bootcmd");
wdenka6c7ad22002-12-03 21:28:10 +0000380
381 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
382
wdenkc6097192002-11-03 00:24:07 +0000383 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
384# ifdef CONFIG_AUTOBOOT_KEYED
385 int prev = disable_ctrlc(1); /* disable Control C checking */
386# endif
387
Simon Glass009dde12012-02-14 19:59:20 +0000388 run_command(s, 0);
wdenkc6097192002-11-03 00:24:07 +0000389
390# ifdef CONFIG_AUTOBOOT_KEYED
391 disable_ctrlc(prev); /* restore Control C checking */
392# endif
393 }
wdenkc7de8292002-11-19 11:04:11 +0000394
395# ifdef CONFIG_MENUKEY
wdenka6c7ad22002-12-03 21:28:10 +0000396 if (menukey == CONFIG_MENUKEY) {
Jason Hobbs370d1e32011-06-23 08:27:30 +0000397 s = getenv("menucmd");
Jason Hobbsc8a20792011-08-31 05:37:24 +0000398 if (s)
Simon Glass009dde12012-02-14 19:59:20 +0000399 run_command(s, 0);
wdenkc7de8292002-11-19 11:04:11 +0000400 }
401#endif /* CONFIG_MENUKEY */
Wolfgang Denk953b7e62010-06-13 18:28:54 +0200402#endif /* CONFIG_BOOTDELAY */
wdenkc7de8292002-11-19 11:04:11 +0000403
wdenkc6097192002-11-03 00:24:07 +0000404 /*
405 * Main Loop for Monitor Command Processing
406 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200407#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000408 parse_file_outer();
409 /* This point is never reached */
410 for (;;);
411#else
412 for (;;) {
413#ifdef CONFIG_BOOT_RETRY_TIME
414 if (rc >= 0) {
415 /* Saw enough of a valid command to
416 * restart the timeout.
417 */
418 reset_cmd_timeout();
419 }
420#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200421 len = readline (CONFIG_SYS_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000422
423 flag = 0; /* assume no special flags for now */
424 if (len > 0)
425 strcpy (lastcommand, console_buffer);
426 else if (len == 0)
427 flag |= CMD_FLAG_REPEAT;
428#ifdef CONFIG_BOOT_RETRY_TIME
429 else if (len == -2) {
430 /* -2 means timed out, retry autoboot
431 */
wdenk4b9206e2004-03-23 22:14:11 +0000432 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000433# ifdef CONFIG_RESET_TO_RETRY
434 /* Reinit board to run initialization code again */
435 do_reset (NULL, 0, 0, NULL);
436# else
437 return; /* retry autoboot */
438# endif
439 }
440#endif
441
442 if (len == -1)
wdenk4b9206e2004-03-23 22:14:11 +0000443 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000444 else
Simon Glass53071532012-02-14 19:59:21 +0000445 rc = run_command(lastcommand, flag);
wdenkc6097192002-11-03 00:24:07 +0000446
447 if (rc <= 0) {
448 /* invalid command or not repeatable, forget it */
449 lastcommand[0] = 0;
450 }
451 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200452#endif /*CONFIG_SYS_HUSH_PARSER*/
wdenkc6097192002-11-03 00:24:07 +0000453}
454
wdenk6dd652f2003-06-19 23:40:20 +0000455#ifdef CONFIG_BOOT_RETRY_TIME
456/***************************************************************************
Wolfgang Denk19973b62006-10-28 00:38:39 +0200457 * initialize command line timeout
wdenk6dd652f2003-06-19 23:40:20 +0000458 */
459void init_cmd_timeout(void)
460{
461 char *s = getenv ("bootretry");
462
463 if (s != NULL)
wdenkb028f712003-12-07 21:39:28 +0000464 retry_time = (int)simple_strtol(s, NULL, 10);
wdenk6dd652f2003-06-19 23:40:20 +0000465 else
466 retry_time = CONFIG_BOOT_RETRY_TIME;
467
468 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
469 retry_time = CONFIG_BOOT_RETRY_MIN;
470}
471
wdenkc6097192002-11-03 00:24:07 +0000472/***************************************************************************
473 * reset command line timeout to retry_time seconds
474 */
wdenkc6097192002-11-03 00:24:07 +0000475void reset_cmd_timeout(void)
476{
Justin L Wernerdc80e002012-08-09 14:20:30 -0600477#if defined(CONFIG_BOOTP_VENDOREX) && defined(CONFIG_BOOTP_VENDOREX_PXE_SHARED)
478 char *pxe_reboot_time = getenv(PXE_REBOOT_TIME);
479
480 if (pxe_reboot_time && *pxe_reboot_time) {
481 retry_time = atoi(pxe_reboot_time);
482 }
483#endif
wdenkc6097192002-11-03 00:24:07 +0000484 endtime = endtick(retry_time);
485}
486#endif
487
Wolfgang Denk501090a2006-07-21 11:33:45 +0200488#ifdef CONFIG_CMDLINE_EDITING
489
490/*
491 * cmdline-editing related codes from vivi.
492 * Author: Janghoon Lyu <nandy@mizi.com>
493 */
494
Wolfgang Denk501090a2006-07-21 11:33:45 +0200495#define putnstr(str,n) do { \
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700496 printf ("%.*s", (int)n, str); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200497 } while (0)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200498
499#define CTL_CH(c) ((c) - 'a' + 1)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200500#define CTL_BACKSPACE ('\b')
501#define DEL ((char)255)
502#define DEL7 ((char)127)
503#define CREAD_HIST_CHAR ('!')
504
505#define getcmd_putch(ch) putc(ch)
506#define getcmd_getch() getc()
507#define getcmd_cbeep() getcmd_putch('\a')
508
509#define HIST_MAX 20
Peter Tyser8804ae32010-09-29 13:30:56 -0500510#define HIST_SIZE CONFIG_SYS_CBSIZE
Wolfgang Denk501090a2006-07-21 11:33:45 +0200511
512static int hist_max = 0;
513static int hist_add_idx = 0;
514static int hist_cur = -1;
515unsigned hist_num = 0;
516
517char* hist_list[HIST_MAX];
John Schmoller6475b9f2010-03-12 09:49:23 -0600518char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200519
520#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
521
522static void hist_init(void)
523{
524 int i;
525
526 hist_max = 0;
527 hist_add_idx = 0;
528 hist_cur = -1;
529 hist_num = 0;
530
531 for (i = 0; i < HIST_MAX; i++) {
532 hist_list[i] = hist_lines[i];
533 hist_list[i][0] = '\0';
534 }
535}
536
537static void cread_add_to_hist(char *line)
538{
539 strcpy(hist_list[hist_add_idx], line);
540
541 if (++hist_add_idx >= HIST_MAX)
542 hist_add_idx = 0;
543
544 if (hist_add_idx > hist_max)
545 hist_max = hist_add_idx;
546
547 hist_num++;
548}
549
550static char* hist_prev(void)
551{
552 char *ret;
553 int old_cur;
554
555 if (hist_cur < 0)
556 return NULL;
557
558 old_cur = hist_cur;
559 if (--hist_cur < 0)
560 hist_cur = hist_max;
561
562 if (hist_cur == hist_add_idx) {
563 hist_cur = old_cur;
564 ret = NULL;
565 } else
566 ret = hist_list[hist_cur];
567
568 return (ret);
569}
570
571static char* hist_next(void)
572{
573 char *ret;
574
575 if (hist_cur < 0)
576 return NULL;
577
578 if (hist_cur == hist_add_idx)
579 return NULL;
580
581 if (++hist_cur > hist_max)
582 hist_cur = 0;
583
584 if (hist_cur == hist_add_idx) {
585 ret = "";
586 } else
587 ret = hist_list[hist_cur];
588
589 return (ret);
590}
591
Stefan Roese3ca91222006-07-27 16:11:19 +0200592#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk501090a2006-07-21 11:33:45 +0200593static void cread_print_hist_list(void)
594{
595 int i;
596 unsigned long n;
597
598 n = hist_num - hist_max;
599
600 i = hist_add_idx + 1;
601 while (1) {
602 if (i > hist_max)
603 i = 0;
604 if (i == hist_add_idx)
605 break;
606 printf("%s\n", hist_list[i]);
607 n++;
608 i++;
609 }
610}
Stefan Roese3ca91222006-07-27 16:11:19 +0200611#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200612
613#define BEGINNING_OF_LINE() { \
614 while (num) { \
615 getcmd_putch(CTL_BACKSPACE); \
616 num--; \
617 } \
618}
619
620#define ERASE_TO_EOL() { \
621 if (num < eol_num) { \
Mike Frysinger8faba482010-07-23 05:28:15 -0400622 printf("%*s", (int)(eol_num - num), ""); \
623 do { \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200624 getcmd_putch(CTL_BACKSPACE); \
Mike Frysinger8faba482010-07-23 05:28:15 -0400625 } while (--eol_num > num); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200626 } \
627}
628
629#define REFRESH_TO_EOL() { \
630 if (num < eol_num) { \
631 wlen = eol_num - num; \
632 putnstr(buf + num, wlen); \
633 num = eol_num; \
634 } \
635}
636
637static void cread_add_char(char ichar, int insert, unsigned long *num,
638 unsigned long *eol_num, char *buf, unsigned long len)
639{
640 unsigned long wlen;
641
642 /* room ??? */
643 if (insert || *num == *eol_num) {
644 if (*eol_num > len - 1) {
645 getcmd_cbeep();
646 return;
647 }
648 (*eol_num)++;
649 }
650
651 if (insert) {
652 wlen = *eol_num - *num;
653 if (wlen > 1) {
654 memmove(&buf[*num+1], &buf[*num], wlen-1);
655 }
656
657 buf[*num] = ichar;
658 putnstr(buf + *num, wlen);
659 (*num)++;
660 while (--wlen) {
661 getcmd_putch(CTL_BACKSPACE);
662 }
663 } else {
664 /* echo the character */
665 wlen = 1;
666 buf[*num] = ichar;
667 putnstr(buf + *num, wlen);
668 (*num)++;
669 }
670}
671
672static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
673 unsigned long *eol_num, char *buf, unsigned long len)
674{
675 while (strsize--) {
676 cread_add_char(*str, insert, num, eol_num, buf, len);
677 str++;
678 }
679}
680
Heiko Schocher9c348312012-01-16 21:13:05 +0000681static int cread_line(const char *const prompt, char *buf, unsigned int *len,
682 int timeout)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200683{
684 unsigned long num = 0;
685 unsigned long eol_num = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200686 unsigned long wlen;
687 char ichar;
688 int insert = 1;
689 int esc_len = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200690 char esc_save[8];
Peter Tyserecc55002009-10-25 15:12:54 -0500691 int init_len = strlen(buf);
Heiko Schocher9c348312012-01-16 21:13:05 +0000692 int first = 1;
Peter Tyserecc55002009-10-25 15:12:54 -0500693
694 if (init_len)
695 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
Wolfgang Denk501090a2006-07-21 11:33:45 +0200696
697 while (1) {
Andreas Engel00ac50e2008-01-09 17:10:56 +0100698#ifdef CONFIG_BOOT_RETRY_TIME
699 while (!tstc()) { /* while no incoming data */
700 if (retry_time >= 0 && get_ticks() > endtime)
701 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +0200702 WATCHDOG_RESET();
Andreas Engel00ac50e2008-01-09 17:10:56 +0100703 }
704#endif
Heiko Schocher9c348312012-01-16 21:13:05 +0000705 if (first && timeout) {
706 uint64_t etime = endtick(timeout);
707
708 while (!tstc()) { /* while no incoming data */
709 if (get_ticks() >= etime)
710 return -2; /* timed out */
711 WATCHDOG_RESET();
712 }
713 first = 0;
714 }
Andreas Engel00ac50e2008-01-09 17:10:56 +0100715
Wolfgang Denk501090a2006-07-21 11:33:45 +0200716 ichar = getcmd_getch();
717
718 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200719 putc('\n');
Wolfgang Denk501090a2006-07-21 11:33:45 +0200720 break;
721 }
722
723 /*
724 * handle standard linux xterm esc sequences for arrow key, etc.
725 */
726 if (esc_len != 0) {
727 if (esc_len == 1) {
728 if (ichar == '[') {
729 esc_save[esc_len] = ichar;
730 esc_len = 2;
731 } else {
732 cread_add_str(esc_save, esc_len, insert,
733 &num, &eol_num, buf, *len);
734 esc_len = 0;
735 }
736 continue;
737 }
738
739 switch (ichar) {
740
741 case 'D': /* <- key */
742 ichar = CTL_CH('b');
743 esc_len = 0;
744 break;
745 case 'C': /* -> key */
746 ichar = CTL_CH('f');
747 esc_len = 0;
748 break; /* pass off to ^F handler */
749 case 'H': /* Home key */
750 ichar = CTL_CH('a');
751 esc_len = 0;
752 break; /* pass off to ^A handler */
753 case 'A': /* up arrow */
754 ichar = CTL_CH('p');
755 esc_len = 0;
756 break; /* pass off to ^P handler */
757 case 'B': /* down arrow */
758 ichar = CTL_CH('n');
759 esc_len = 0;
760 break; /* pass off to ^N handler */
761 default:
762 esc_save[esc_len++] = ichar;
763 cread_add_str(esc_save, esc_len, insert,
764 &num, &eol_num, buf, *len);
765 esc_len = 0;
766 continue;
767 }
768 }
769
770 switch (ichar) {
771 case 0x1b:
772 if (esc_len == 0) {
773 esc_save[esc_len] = ichar;
774 esc_len = 1;
775 } else {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200776 puts("impossible condition #876\n");
Wolfgang Denk501090a2006-07-21 11:33:45 +0200777 esc_len = 0;
778 }
779 break;
780
781 case CTL_CH('a'):
782 BEGINNING_OF_LINE();
783 break;
784 case CTL_CH('c'): /* ^C - break */
785 *buf = '\0'; /* discard input */
786 return (-1);
787 case CTL_CH('f'):
788 if (num < eol_num) {
789 getcmd_putch(buf[num]);
790 num++;
791 }
792 break;
793 case CTL_CH('b'):
794 if (num) {
795 getcmd_putch(CTL_BACKSPACE);
796 num--;
797 }
798 break;
799 case CTL_CH('d'):
800 if (num < eol_num) {
801 wlen = eol_num - num - 1;
802 if (wlen) {
803 memmove(&buf[num], &buf[num+1], wlen);
804 putnstr(buf + num, wlen);
805 }
806
807 getcmd_putch(' ');
808 do {
809 getcmd_putch(CTL_BACKSPACE);
810 } while (wlen--);
811 eol_num--;
812 }
813 break;
814 case CTL_CH('k'):
815 ERASE_TO_EOL();
816 break;
817 case CTL_CH('e'):
818 REFRESH_TO_EOL();
819 break;
820 case CTL_CH('o'):
821 insert = !insert;
822 break;
823 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100824 case CTL_CH('u'):
Wolfgang Denk501090a2006-07-21 11:33:45 +0200825 BEGINNING_OF_LINE();
826 ERASE_TO_EOL();
827 break;
828 case DEL:
829 case DEL7:
830 case 8:
831 if (num) {
832 wlen = eol_num - num;
833 num--;
834 memmove(&buf[num], &buf[num+1], wlen);
835 getcmd_putch(CTL_BACKSPACE);
836 putnstr(buf + num, wlen);
837 getcmd_putch(' ');
838 do {
839 getcmd_putch(CTL_BACKSPACE);
840 } while (wlen--);
841 eol_num--;
842 }
843 break;
844 case CTL_CH('p'):
845 case CTL_CH('n'):
846 {
847 char * hline;
848
849 esc_len = 0;
850
851 if (ichar == CTL_CH('p'))
852 hline = hist_prev();
853 else
854 hline = hist_next();
855
856 if (!hline) {
857 getcmd_cbeep();
858 continue;
859 }
860
861 /* nuke the current line */
862 /* first, go home */
863 BEGINNING_OF_LINE();
864
865 /* erase to end of line */
866 ERASE_TO_EOL();
867
868 /* copy new line into place and display */
869 strcpy(buf, hline);
870 eol_num = strlen(buf);
871 REFRESH_TO_EOL();
872 continue;
873 }
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100874#ifdef CONFIG_AUTO_COMPLETE
875 case '\t': {
876 int num2, col;
877
878 /* do not autocomplete when in the middle */
879 if (num < eol_num) {
880 getcmd_cbeep();
881 break;
882 }
883
884 buf[num] = '\0';
885 col = strlen(prompt) + eol_num;
886 num2 = num;
887 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
888 col = num2 - num;
889 num += col;
890 eol_num += col;
891 }
892 break;
893 }
894#endif
Wolfgang Denk501090a2006-07-21 11:33:45 +0200895 default:
896 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
897 break;
898 }
899 }
900 *len = eol_num;
901 buf[eol_num] = '\0'; /* lose the newline */
902
903 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
904 cread_add_to_hist(buf);
905 hist_cur = hist_add_idx;
906
Peter Tyserf9239432009-10-25 15:12:53 -0500907 return 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200908}
909
910#endif /* CONFIG_CMDLINE_EDITING */
911
wdenkc6097192002-11-03 00:24:07 +0000912/****************************************************************************/
913
914/*
915 * Prompt for input and read a line.
916 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
917 * time out when time goes past endtime (timebase time in ticks).
918 * Return: number of read characters
919 * -1 if break
920 * -2 if timed out
921 */
922int readline (const char *const prompt)
923{
Peter Tyserecc55002009-10-25 15:12:54 -0500924 /*
925 * If console_buffer isn't 0-length the user will be prompted to modify
926 * it instead of entering it from scratch as desired.
927 */
928 console_buffer[0] = '\0';
929
Heiko Schocher9c348312012-01-16 21:13:05 +0000930 return readline_into_buffer(prompt, console_buffer, 0);
James Yang6636b622008-01-09 11:17:49 -0600931}
932
933
Heiko Schocher9c348312012-01-16 21:13:05 +0000934int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
James Yang6636b622008-01-09 11:17:49 -0600935{
936 char *p = buffer;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200937#ifdef CONFIG_CMDLINE_EDITING
Peter Tyser8804ae32010-09-29 13:30:56 -0500938 unsigned int len = CONFIG_SYS_CBSIZE;
Stefan Roesed8f961b2006-08-07 15:08:44 +0200939 int rc;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200940 static int initted = 0;
941
James Yang597f6c22008-05-05 10:22:53 -0500942 /*
943 * History uses a global array which is not
944 * writable until after relocation to RAM.
945 * Revert to non-history version if still
946 * running from flash.
947 */
948 if (gd->flags & GD_FLG_RELOC) {
949 if (!initted) {
950 hist_init();
951 initted = 1;
952 }
953
Peter Tysere491a712009-10-25 15:12:52 -0500954 if (prompt)
955 puts (prompt);
James Yang597f6c22008-05-05 10:22:53 -0500956
Heiko Schocher9c348312012-01-16 21:13:05 +0000957 rc = cread_line(prompt, p, &len, timeout);
James Yang597f6c22008-05-05 10:22:53 -0500958 return rc < 0 ? rc : len;
959
960 } else {
961#endif /* CONFIG_CMDLINE_EDITING */
Kumar Gala0ec59522008-01-10 02:22:05 -0600962 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +0000963 int n = 0; /* buffer index */
964 int plen = 0; /* prompt length */
965 int col; /* output column cnt */
966 char c;
967
968 /* print prompt */
969 if (prompt) {
970 plen = strlen (prompt);
971 puts (prompt);
972 }
973 col = plen;
974
975 for (;;) {
976#ifdef CONFIG_BOOT_RETRY_TIME
977 while (!tstc()) { /* while no incoming data */
978 if (retry_time >= 0 && get_ticks() > endtime)
979 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +0200980 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000981 }
982#endif
983 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
984
985#ifdef CONFIG_SHOW_ACTIVITY
986 while (!tstc()) {
987 extern void show_activity(int arg);
988 show_activity(0);
Jens Scharsig30dc1652010-04-09 19:02:38 +0200989 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000990 }
991#endif
992 c = getc();
993
994 /*
995 * Special character handling
996 */
997 switch (c) {
998 case '\r': /* Enter */
999 case '\n':
1000 *p = '\0';
1001 puts ("\r\n");
James Yang6636b622008-01-09 11:17:49 -06001002 return (p - p_buf);
wdenkc6097192002-11-03 00:24:07 +00001003
wdenk27aa8182004-03-23 22:37:33 +00001004 case '\0': /* nul */
1005 continue;
1006
wdenkc6097192002-11-03 00:24:07 +00001007 case 0x03: /* ^C - break */
James Yang6636b622008-01-09 11:17:49 -06001008 p_buf[0] = '\0'; /* discard input */
wdenkc6097192002-11-03 00:24:07 +00001009 return (-1);
1010
1011 case 0x15: /* ^U - erase line */
1012 while (col > plen) {
1013 puts (erase_seq);
1014 --col;
1015 }
James Yang6636b622008-01-09 11:17:49 -06001016 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001017 n = 0;
1018 continue;
1019
Wolfgang Denk1636d1c2007-06-22 23:59:00 +02001020 case 0x17: /* ^W - erase word */
James Yang6636b622008-01-09 11:17:49 -06001021 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001022 while ((n > 0) && (*p != ' ')) {
James Yang6636b622008-01-09 11:17:49 -06001023 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001024 }
1025 continue;
1026
1027 case 0x08: /* ^H - backspace */
1028 case 0x7F: /* DEL - backspace */
James Yang6636b622008-01-09 11:17:49 -06001029 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001030 continue;
1031
1032 default:
1033 /*
1034 * Must be a normal character then
1035 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001036 if (n < CONFIG_SYS_CBSIZE-2) {
wdenkc6097192002-11-03 00:24:07 +00001037 if (c == '\t') { /* expand TABs */
wdenk04a85b32004-04-15 18:22:41 +00001038#ifdef CONFIG_AUTO_COMPLETE
1039 /* if auto completion triggered just continue */
1040 *p = '\0';
1041 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang6636b622008-01-09 11:17:49 -06001042 p = p_buf + n; /* reset */
wdenk04a85b32004-04-15 18:22:41 +00001043 continue;
1044 }
1045#endif
wdenkc6097192002-11-03 00:24:07 +00001046 puts (tab_seq+(col&07));
1047 col += 8 - (col&07);
1048 } else {
1049 ++col; /* echo input */
1050 putc (c);
1051 }
1052 *p++ = c;
1053 ++n;
1054 } else { /* Buffer full */
1055 putc ('\a');
1056 }
1057 }
1058 }
James Yang597f6c22008-05-05 10:22:53 -05001059#ifdef CONFIG_CMDLINE_EDITING
1060 }
1061#endif
wdenkc6097192002-11-03 00:24:07 +00001062}
1063
1064/****************************************************************************/
1065
1066static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1067{
1068 char *s;
1069
1070 if (*np == 0) {
1071 return (p);
1072 }
1073
1074 if (*(--p) == '\t') { /* will retype the whole line */
1075 while (*colp > plen) {
1076 puts (erase_seq);
1077 (*colp)--;
1078 }
1079 for (s=buffer; s<p; ++s) {
1080 if (*s == '\t') {
1081 puts (tab_seq+((*colp) & 07));
1082 *colp += 8 - ((*colp) & 07);
1083 } else {
1084 ++(*colp);
1085 putc (*s);
1086 }
1087 }
1088 } else {
1089 puts (erase_seq);
1090 (*colp)--;
1091 }
1092 (*np)--;
1093 return (p);
1094}
1095
1096/****************************************************************************/
1097
1098int parse_line (char *line, char *argv[])
1099{
1100 int nargs = 0;
1101
1102#ifdef DEBUG_PARSER
1103 printf ("parse_line: \"%s\"\n", line);
1104#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001105 while (nargs < CONFIG_SYS_MAXARGS) {
wdenkc6097192002-11-03 00:24:07 +00001106
1107 /* skip any white space */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001108 while (isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001109 ++line;
wdenkc6097192002-11-03 00:24:07 +00001110
1111 if (*line == '\0') { /* end of line, no more args */
1112 argv[nargs] = NULL;
1113#ifdef DEBUG_PARSER
1114 printf ("parse_line: nargs=%d\n", nargs);
1115#endif
1116 return (nargs);
1117 }
1118
1119 argv[nargs++] = line; /* begin of argument string */
1120
1121 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001122 while (*line && !isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001123 ++line;
wdenkc6097192002-11-03 00:24:07 +00001124
1125 if (*line == '\0') { /* end of line, no more args */
1126 argv[nargs] = NULL;
1127#ifdef DEBUG_PARSER
1128 printf ("parse_line: nargs=%d\n", nargs);
1129#endif
1130 return (nargs);
1131 }
1132
1133 *line++ = '\0'; /* terminate current arg */
1134 }
1135
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001136 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
wdenkc6097192002-11-03 00:24:07 +00001137
1138#ifdef DEBUG_PARSER
1139 printf ("parse_line: nargs=%d\n", nargs);
1140#endif
1141 return (nargs);
1142}
1143
1144/****************************************************************************/
1145
Simon Glass7fed89e2012-02-14 19:59:22 +00001146#ifndef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +00001147static void process_macros (const char *input, char *output)
1148{
1149 char c, prev;
1150 const char *varname_start = NULL;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001151 int inputcnt = strlen (input);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001152 int outputcnt = CONFIG_SYS_CBSIZE;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001153 int state = 0; /* 0 = waiting for '$' */
1154
1155 /* 1 = waiting for '(' or '{' */
1156 /* 2 = waiting for ')' or '}' */
1157 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001158#ifdef DEBUG_PARSER
1159 char *output_start = output;
1160
Wolfgang Denk19973b62006-10-28 00:38:39 +02001161 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1162 input);
wdenkc6097192002-11-03 00:24:07 +00001163#endif
1164
Wolfgang Denk19973b62006-10-28 00:38:39 +02001165 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001166
1167 while (inputcnt && outputcnt) {
wdenk8bde7f72003-06-27 21:31:46 +00001168 c = *input++;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001169 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001170
Wolfgang Denk19973b62006-10-28 00:38:39 +02001171 if (state != 3) {
1172 /* remove one level of escape characters */
1173 if ((c == '\\') && (prev != '\\')) {
1174 if (inputcnt-- == 0)
1175 break;
1176 prev = c;
1177 c = *input++;
1178 }
wdenka25f8622003-01-02 23:57:29 +00001179 }
wdenkc6097192002-11-03 00:24:07 +00001180
Wolfgang Denk19973b62006-10-28 00:38:39 +02001181 switch (state) {
1182 case 0: /* Waiting for (unescaped) $ */
1183 if ((c == '\'') && (prev != '\\')) {
1184 state = 3;
1185 break;
1186 }
1187 if ((c == '$') && (prev != '\\')) {
1188 state++;
1189 } else {
wdenkc6097192002-11-03 00:24:07 +00001190 *(output++) = c;
1191 outputcnt--;
1192 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001193 break;
1194 case 1: /* Waiting for ( */
1195 if (c == '(' || c == '{') {
1196 state++;
1197 varname_start = input;
1198 } else {
1199 state = 0;
1200 *(output++) = '$';
1201 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001202
Wolfgang Denk19973b62006-10-28 00:38:39 +02001203 if (outputcnt) {
1204 *(output++) = c;
wdenkc6097192002-11-03 00:24:07 +00001205 outputcnt--;
1206 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001207 }
1208 break;
1209 case 2: /* Waiting for ) */
1210 if (c == ')' || c == '}') {
1211 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001212 char envname[CONFIG_SYS_CBSIZE], *envval;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001213 int envcnt = input - varname_start - 1; /* Varname # of chars */
1214
1215 /* Get the varname */
1216 for (i = 0; i < envcnt; i++) {
1217 envname[i] = varname_start[i];
1218 }
1219 envname[i] = 0;
1220
1221 /* Get its value */
1222 envval = getenv (envname);
1223
1224 /* Copy into the line if it exists */
1225 if (envval != NULL)
1226 while ((*envval) && outputcnt) {
1227 *(output++) = *(envval++);
1228 outputcnt--;
1229 }
1230 /* Look for another '$' */
1231 state = 0;
1232 }
1233 break;
1234 case 3: /* Waiting for ' */
1235 if ((c == '\'') && (prev != '\\')) {
1236 state = 0;
1237 } else {
1238 *(output++) = c;
1239 outputcnt--;
1240 }
1241 break;
wdenkc6097192002-11-03 00:24:07 +00001242 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001243 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001244 }
1245
1246 if (outputcnt)
1247 *output = 0;
Bartlomiej Sieka9160b962007-05-27 17:04:18 +02001248 else
1249 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001250
1251#ifdef DEBUG_PARSER
1252 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
Wolfgang Denk19973b62006-10-28 00:38:39 +02001253 strlen (output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001254#endif
1255}
1256
1257/****************************************************************************
1258 * returns:
1259 * 1 - command executed, repeatable
1260 * 0 - command executed but not repeatable, interrupted commands are
1261 * always considered not repeatable
1262 * -1 - not executed (unrecognized, bootd recursion or too many args)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001263 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
wdenkc6097192002-11-03 00:24:07 +00001264 * considered unrecognized)
1265 *
1266 * WARNING:
1267 *
1268 * We must create a temporary copy of the command since the command we get
1269 * may be the result from getenv(), which returns a pointer directly to
1270 * the environment data, which may change magicly when the command we run
1271 * creates or modifies environment variables (like "bootp" does).
1272 */
Simon Glass53071532012-02-14 19:59:21 +00001273static int builtin_run_command(const char *cmd, int flag)
wdenkc6097192002-11-03 00:24:07 +00001274{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001275 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
wdenkc6097192002-11-03 00:24:07 +00001276 char *token; /* start of token in cmdbuf */
1277 char *sep; /* end of token (separator) in cmdbuf */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001278 char finaltoken[CONFIG_SYS_CBSIZE];
wdenkc6097192002-11-03 00:24:07 +00001279 char *str = cmdbuf;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001280 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenkf07771c2003-05-28 08:06:31 +00001281 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001282 int repeatable = 1;
wdenkf07771c2003-05-28 08:06:31 +00001283 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001284
1285#ifdef DEBUG_PARSER
1286 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1287 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1288 puts ("\"\n");
1289#endif
1290
1291 clear_ctrlc(); /* forget any previous Control C */
1292
1293 if (!cmd || !*cmd) {
1294 return -1; /* empty command */
1295 }
1296
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001297 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
wdenkc6097192002-11-03 00:24:07 +00001298 puts ("## Command too long!\n");
1299 return -1;
1300 }
1301
1302 strcpy (cmdbuf, cmd);
1303
1304 /* Process separators and check for invalid
1305 * repeatable commands
1306 */
1307
1308#ifdef DEBUG_PARSER
1309 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1310#endif
1311 while (*str) {
1312
1313 /*
1314 * Find separator, or string end
1315 * Allow simple escape of ';' by writing "\;"
1316 */
wdenka25f8622003-01-02 23:57:29 +00001317 for (inquotes = 0, sep = str; *sep; sep++) {
1318 if ((*sep=='\'') &&
1319 (*(sep-1) != '\\'))
1320 inquotes=!inquotes;
1321
1322 if (!inquotes &&
1323 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001324 ( sep != str) && /* past string start */
1325 (*(sep-1) != '\\')) /* and NOT escaped */
1326 break;
1327 }
1328
1329 /*
1330 * Limit the token to data between separators
1331 */
1332 token = str;
1333 if (*sep) {
1334 str = sep + 1; /* start of command for next pass */
1335 *sep = '\0';
1336 }
1337 else
1338 str = sep; /* no more commands for next pass */
1339#ifdef DEBUG_PARSER
1340 printf ("token: \"%s\"\n", token);
1341#endif
1342
1343 /* find macros in this token and replace them */
1344 process_macros (token, finaltoken);
1345
1346 /* Extract arguments */
Wolfgang Denk1264b402006-03-12 02:20:55 +01001347 if ((argc = parse_line (finaltoken, argv)) == 0) {
1348 rc = -1; /* no command at all */
1349 continue;
1350 }
wdenkc6097192002-11-03 00:24:07 +00001351
Timo Ketola030fca52012-04-22 23:57:27 +00001352 if (cmd_process(flag, argc, argv, &repeatable))
1353 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001354
1355 /* Did the user stop this? */
1356 if (had_ctrlc ())
Detlev Zundel5afb2022007-05-23 18:47:48 +02001357 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001358 }
1359
wdenkf07771c2003-05-28 08:06:31 +00001360 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001361}
Simon Glass7fed89e2012-02-14 19:59:22 +00001362#endif
wdenkc6097192002-11-03 00:24:07 +00001363
Simon Glass53071532012-02-14 19:59:21 +00001364/*
1365 * Run a command using the selected parser.
1366 *
1367 * @param cmd Command to run
1368 * @param flag Execution flags (CMD_FLAG_...)
1369 * @return 0 on success, or != 0 on error.
1370 */
1371int run_command(const char *cmd, int flag)
1372{
1373#ifndef CONFIG_SYS_HUSH_PARSER
1374 /*
1375 * builtin_run_command can return 0 or 1 for success, so clean up
1376 * its result.
1377 */
1378 if (builtin_run_command(cmd, flag) == -1)
1379 return 1;
1380
1381 return 0;
1382#else
1383 return parse_string_outer(cmd,
1384 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1385#endif
1386}
1387
wdenkc6097192002-11-03 00:24:07 +00001388/****************************************************************************/
1389
Jon Loeligerc3517f92007-07-08 18:10:08 -05001390#if defined(CONFIG_CMD_RUN)
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001391int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +00001392{
1393 int i;
wdenkc6097192002-11-03 00:24:07 +00001394
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001395 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001396 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +00001397
1398 for (i=1; i<argc; ++i) {
wdenk3e386912003-04-05 00:53:31 +00001399 char *arg;
1400
1401 if ((arg = getenv (argv[i])) == NULL) {
1402 printf ("## Error: \"%s\" not defined\n", argv[i]);
1403 return 1;
1404 }
Jason Hobbsc8a20792011-08-31 05:37:24 +00001405
Simon Glass009dde12012-02-14 19:59:20 +00001406 if (run_command(arg, flag) != 0)
wdenk3e386912003-04-05 00:53:31 +00001407 return 1;
wdenkc6097192002-11-03 00:24:07 +00001408 }
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