blob: 48bed95386aaec840e249ef09b68bbf69e94f62d [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>
Che-Liang Chiou224b72e2012-10-25 16:31:07 +000033#include <fdtdec.h>
Simon Glassd51004a2012-03-30 21:30:55 +000034#include <malloc.h>
Andreas Bießmann09c2e902011-07-18 20:24:04 +020035#include <version.h>
Wolfgang Denkd9631ec2005-09-30 15:18:23 +020036#ifdef CONFIG_MODEM_SUPPORT
37#include <malloc.h> /* for free() prototype */
38#endif
wdenkc6097192002-11-03 00:24:07 +000039
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020040#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +000041#include <hush.h>
42#endif
43
Che-Liang Chiou224b72e2012-10-25 16:31:07 +000044#ifdef CONFIG_OF_CONTROL
45#include <fdtdec.h>
46#endif
47
wdenkbdccc4f2003-08-05 17:43:17 +000048#include <post.h>
Jason Hobbs4d91a6e2011-08-23 11:06:54 +000049#include <linux/ctype.h>
Heiko Schocher317d6c52012-01-16 21:13:35 +000050#include <menu.h>
wdenkbdccc4f2003-08-05 17:43:17 +000051
James Yang597f6c22008-05-05 10:22:53 -050052#if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
Wolfgang Denkd87080b2006-03-31 18:32:53 +020053DECLARE_GLOBAL_DATA_PTR;
54#endif
55
Heiko Schocherfad63402007-07-13 09:54:17 +020056/*
57 * Board-specific Platform code can reimplement show_boot_progress () if needed
58 */
59void inline __show_boot_progress (int val) {}
Emil Medve5e2c08c2009-05-12 13:48:32 -050060void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
Heiko Schocherfad63402007-07-13 09:54:17 +020061
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020062#if defined(CONFIG_UPDATE_TFTP)
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +000063int update_tftp (ulong addr);
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020064#endif /* CONFIG_UPDATE_TFTP */
wdenk8bde7f72003-06-27 21:31:46 +000065
wdenkc6097192002-11-03 00:24:07 +000066#define MAX_DELAY_STOP_STR 32
67
wdenkc6097192002-11-03 00:24:07 +000068#undef DEBUG_PARSER
69
John Schmoller6475b9f2010-03-12 09:49:23 -060070char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
wdenkc6097192002-11-03 00:24:07 +000071
Stefan Roese3ca91222006-07-27 16:11:19 +020072static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
Mike Frysinger82359ae2010-12-22 09:40:45 -050073static const char erase_seq[] = "\b \b"; /* erase sequence */
74static const char tab_seq[] = " "; /* used to expand TABs */
wdenkc6097192002-11-03 00:24:07 +000075
76#ifdef CONFIG_BOOT_RETRY_TIME
77static uint64_t endtime = 0; /* must be set, default is instant timeout */
78static int retry_time = -1; /* -1 so can call readline before main_loop */
79#endif
80
81#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
82
83#ifndef CONFIG_BOOT_RETRY_MIN
84#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
85#endif
86
87#ifdef CONFIG_MODEM_SUPPORT
88int do_mdm_init = 0;
89extern void mdm_init(void); /* defined in board.c */
90#endif
91
92/***************************************************************************
93 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
Jason Hobbsc8a20792011-08-31 05:37:24 +000094 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
wdenkc6097192002-11-03 00:24:07 +000095 */
96#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
97# if defined(CONFIG_AUTOBOOT_KEYED)
Jason Hobbsb41bc5a2011-08-23 11:06:50 +000098#ifndef CONFIG_MENU
99static inline
100#endif
101int abortboot(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000102{
103 int abort = 0;
104 uint64_t etime = endtick(bootdelay);
Wolfgang Denk19973b62006-10-28 00:38:39 +0200105 struct {
wdenkc6097192002-11-03 00:24:07 +0000106 char* str;
107 u_int len;
108 int retry;
109 }
Wolfgang Denk19973b62006-10-28 00:38:39 +0200110 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +0000111 { str: getenv ("bootdelaykey"), retry: 1 },
112 { str: getenv ("bootdelaykey2"), retry: 1 },
113 { str: getenv ("bootstopkey"), retry: 0 },
114 { str: getenv ("bootstopkey2"), retry: 0 },
115 };
116
117 char presskey [MAX_DELAY_STOP_STR];
118 u_int presskey_len = 0;
119 u_int presskey_max = 0;
120 u_int i;
121
Dirk Eibacha5aae0a2012-04-26 01:49:33 +0000122#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
123 if (bootdelay == 0)
124 return 0;
125#endif
126
wdenkc6097192002-11-03 00:24:07 +0000127# ifdef CONFIG_AUTOBOOT_PROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200128 printf(CONFIG_AUTOBOOT_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000129# endif
130
131# ifdef CONFIG_AUTOBOOT_DELAY_STR
132 if (delaykey[0].str == NULL)
133 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
134# endif
135# ifdef CONFIG_AUTOBOOT_DELAY_STR2
136 if (delaykey[1].str == NULL)
137 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
138# endif
139# ifdef CONFIG_AUTOBOOT_STOP_STR
140 if (delaykey[2].str == NULL)
141 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
142# endif
143# ifdef CONFIG_AUTOBOOT_STOP_STR2
144 if (delaykey[3].str == NULL)
145 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
146# endif
147
148 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
149 delaykey[i].len = delaykey[i].str == NULL ?
150 0 : strlen (delaykey[i].str);
151 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
152 MAX_DELAY_STOP_STR : delaykey[i].len;
153
154 presskey_max = presskey_max > delaykey[i].len ?
155 presskey_max : delaykey[i].len;
156
157# if DEBUG_BOOTKEYS
158 printf("%s key:<%s>\n",
159 delaykey[i].retry ? "delay" : "stop",
160 delaykey[i].str ? delaykey[i].str : "NULL");
161# endif
162 }
163
164 /* In order to keep up with incoming data, check timeout only
165 * when catch up.
166 */
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100167 do {
168 if (tstc()) {
169 if (presskey_len < presskey_max) {
170 presskey [presskey_len ++] = getc();
171 }
172 else {
173 for (i = 0; i < presskey_max - 1; i ++)
174 presskey [i] = presskey [i + 1];
175
176 presskey [i] = getc();
177 }
178 }
179
wdenkc6097192002-11-03 00:24:07 +0000180 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
181 if (delaykey[i].len > 0 &&
182 presskey_len >= delaykey[i].len &&
183 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk8bde7f72003-06-27 21:31:46 +0000184 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000185 delaykey[i].len) == 0) {
186# if DEBUG_BOOTKEYS
187 printf("got %skey\n",
188 delaykey[i].retry ? "delay" : "stop");
189# endif
190
191# ifdef CONFIG_BOOT_RETRY_TIME
192 /* don't retry auto boot */
193 if (! delaykey[i].retry)
194 retry_time = -1;
195# endif
196 abort = 1;
197 }
198 }
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100199 } while (!abort && get_ticks() <= etime);
wdenkc6097192002-11-03 00:24:07 +0000200
wdenkc6097192002-11-03 00:24:07 +0000201# if DEBUG_BOOTKEYS
202 if (!abort)
Ladislav Michlada4d402007-04-25 16:01:26 +0200203 puts("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000204# endif
205
dzu8cb81432003-10-24 13:14:45 +0000206#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200207 if (abort)
208 gd->flags &= ~GD_FLG_SILENT;
dzu8cb81432003-10-24 13:14:45 +0000209#endif
210
wdenkc6097192002-11-03 00:24:07 +0000211 return abort;
212}
213
214# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
215
wdenkc7de8292002-11-19 11:04:11 +0000216#ifdef CONFIG_MENUKEY
217static int menukey = 0;
218#endif
219
Jason Hobbsb41bc5a2011-08-23 11:06:50 +0000220#ifndef CONFIG_MENU
221static inline
222#endif
223int abortboot(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000224{
225 int abort = 0;
226
wdenkc7de8292002-11-19 11:04:11 +0000227#ifdef CONFIG_MENUPROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200228 printf(CONFIG_MENUPROMPT);
wdenkc7de8292002-11-19 11:04:11 +0000229#else
Joe Hershberger93d72122012-08-17 10:53:12 +0000230 if (bootdelay >= 0)
231 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenkc7de8292002-11-19 11:04:11 +0000232#endif
wdenkc6097192002-11-03 00:24:07 +0000233
234#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk8bde7f72003-06-27 21:31:46 +0000235 /*
236 * Check if key already pressed
237 * Don't check if bootdelay < 0
238 */
wdenkc6097192002-11-03 00:24:07 +0000239 if (bootdelay >= 0) {
240 if (tstc()) { /* we got a key press */
241 (void) getc(); /* consume input */
wdenk4b9206e2004-03-23 22:14:11 +0000242 puts ("\b\b\b 0");
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200243 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000244 }
wdenk8bde7f72003-06-27 21:31:46 +0000245 }
wdenkc6097192002-11-03 00:24:07 +0000246#endif
247
wdenkf72da342003-10-10 10:05:42 +0000248 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000249 int i;
250
251 --bootdelay;
252 /* delay 100 * 10ms */
253 for (i=0; !abort && i<100; ++i) {
254 if (tstc()) { /* we got a key press */
255 abort = 1; /* don't auto boot */
256 bootdelay = 0; /* no more delay */
wdenkc7de8292002-11-19 11:04:11 +0000257# ifdef CONFIG_MENUKEY
258 menukey = getc();
259# else
wdenkc6097192002-11-03 00:24:07 +0000260 (void) getc(); /* consume input */
wdenkc7de8292002-11-19 11:04:11 +0000261# endif
wdenkc6097192002-11-03 00:24:07 +0000262 break;
263 }
Ladislav Michlada4d402007-04-25 16:01:26 +0200264 udelay(10000);
wdenkc6097192002-11-03 00:24:07 +0000265 }
266
Ladislav Michlada4d402007-04-25 16:01:26 +0200267 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000268 }
269
Ladislav Michlada4d402007-04-25 16:01:26 +0200270 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000271
wdenkf72da342003-10-10 10:05:42 +0000272#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200273 if (abort)
274 gd->flags &= ~GD_FLG_SILENT;
wdenkf72da342003-10-10 10:05:42 +0000275#endif
276
wdenkc6097192002-11-03 00:24:07 +0000277 return abort;
278}
279# endif /* CONFIG_AUTOBOOT_KEYED */
280#endif /* CONFIG_BOOTDELAY >= 0 */
281
282/****************************************************************************/
283
284void main_loop (void)
285{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200286#ifndef CONFIG_SYS_HUSH_PARSER
287 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
wdenkc6097192002-11-03 00:24:07 +0000288 int len;
289 int rc = 1;
290 int flag;
291#endif
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000292#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) && \
293 defined(CONFIG_OF_CONTROL)
294 char *env;
295#endif
wdenkc6097192002-11-03 00:24:07 +0000296#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
297 char *s;
298 int bootdelay;
299#endif
300#ifdef CONFIG_PREBOOT
301 char *p;
302#endif
wdenkbdccc4f2003-08-05 17:43:17 +0000303#ifdef CONFIG_BOOTCOUNT_LIMIT
304 unsigned long bootcount = 0;
305 unsigned long bootlimit = 0;
306 char *bcs;
307 char bcs_set[16];
308#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000309
wdenkbdccc4f2003-08-05 17:43:17 +0000310#ifdef CONFIG_BOOTCOUNT_LIMIT
311 bootcount = bootcount_load();
312 bootcount++;
313 bootcount_store (bootcount);
314 sprintf (bcs_set, "%lu", bootcount);
315 setenv ("bootcount", bcs_set);
316 bcs = getenv ("bootlimit");
317 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
318#endif /* CONFIG_BOOTCOUNT_LIMIT */
319
wdenkc6097192002-11-03 00:24:07 +0000320#ifdef CONFIG_MODEM_SUPPORT
321 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
322 if (do_mdm_init) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200323 char *str = strdup(getenv("mdm_cmd"));
wdenkc6097192002-11-03 00:24:07 +0000324 setenv ("preboot", str); /* set or delete definition */
325 if (str != NULL)
326 free (str);
327 mdm_init(); /* wait for modem connection */
328 }
329#endif /* CONFIG_MODEM_SUPPORT */
330
stroese05875972003-04-04 15:44:49 +0000331#ifdef CONFIG_VERSION_VARIABLE
332 {
stroese155cb012003-07-11 08:00:33 +0000333 setenv ("ver", version_string); /* set version variable */
stroese05875972003-04-04 15:44:49 +0000334 }
335#endif /* CONFIG_VERSION_VARIABLE */
336
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200337#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000338 u_boot_hush_start ();
339#endif
340
Heiko Schocher81473f62008-10-15 09:40:28 +0200341#if defined(CONFIG_HUSH_INIT_VAR)
342 hush_init_var ();
343#endif
344
wdenkc6097192002-11-03 00:24:07 +0000345#ifdef CONFIG_PREBOOT
346 if ((p = getenv ("preboot")) != NULL) {
347# ifdef CONFIG_AUTOBOOT_KEYED
348 int prev = disable_ctrlc(1); /* disable Control C checking */
349# endif
350
Simon Glass3a8a02b2012-03-30 21:30:56 +0000351 run_command_list(p, -1, 0);
wdenkc6097192002-11-03 00:24:07 +0000352
353# ifdef CONFIG_AUTOBOOT_KEYED
354 disable_ctrlc(prev); /* restore Control C checking */
355# endif
356 }
357#endif /* CONFIG_PREBOOT */
358
Wolfgang Denk143cd212010-03-11 23:56:03 +0100359#if defined(CONFIG_UPDATE_TFTP)
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000360 update_tftp (0UL);
Wolfgang Denk143cd212010-03-11 23:56:03 +0100361#endif /* CONFIG_UPDATE_TFTP */
362
wdenkc6097192002-11-03 00:24:07 +0000363#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
364 s = getenv ("bootdelay");
365 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
366
wdenka6c7ad22002-12-03 21:28:10 +0000367 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000368
Heiko Schocher317d6c52012-01-16 21:13:35 +0000369#if defined(CONFIG_MENU_SHOW)
370 bootdelay = menu_show(bootdelay);
371#endif
wdenkc6097192002-11-03 00:24:07 +0000372# ifdef CONFIG_BOOT_RETRY_TIME
wdenk6dd652f2003-06-19 23:40:20 +0000373 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000374# endif /* CONFIG_BOOT_RETRY_TIME */
375
Yuri Tikhonovb428f6a2008-02-04 14:11:03 +0100376#ifdef CONFIG_POST
377 if (gd->flags & GD_FLG_POSTFAIL) {
378 s = getenv("failbootcmd");
379 }
380 else
381#endif /* CONFIG_POST */
wdenkbdccc4f2003-08-05 17:43:17 +0000382#ifdef CONFIG_BOOTCOUNT_LIMIT
383 if (bootlimit && (bootcount > bootlimit)) {
384 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
385 (unsigned)bootlimit);
386 s = getenv ("altbootcmd");
387 }
388 else
389#endif /* CONFIG_BOOTCOUNT_LIMIT */
390 s = getenv ("bootcmd");
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000391#ifdef CONFIG_OF_CONTROL
392 /* Allow the fdt to override the boot command */
393 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
394 if (env)
395 s = env;
396#endif /* CONFIG_OF_CONTROL */
wdenka6c7ad22002-12-03 21:28:10 +0000397
398 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
399
Joe Hershberger93d72122012-08-17 10:53:12 +0000400 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
wdenkc6097192002-11-03 00:24:07 +0000401# ifdef CONFIG_AUTOBOOT_KEYED
402 int prev = disable_ctrlc(1); /* disable Control C checking */
403# endif
404
Simon Glass3a8a02b2012-03-30 21:30:56 +0000405 run_command_list(s, -1, 0);
wdenkc6097192002-11-03 00:24:07 +0000406
407# ifdef CONFIG_AUTOBOOT_KEYED
408 disable_ctrlc(prev); /* restore Control C checking */
409# endif
410 }
wdenkc7de8292002-11-19 11:04:11 +0000411
412# ifdef CONFIG_MENUKEY
wdenka6c7ad22002-12-03 21:28:10 +0000413 if (menukey == CONFIG_MENUKEY) {
Jason Hobbs370d1e32011-06-23 08:27:30 +0000414 s = getenv("menucmd");
Jason Hobbsc8a20792011-08-31 05:37:24 +0000415 if (s)
Simon Glass3a8a02b2012-03-30 21:30:56 +0000416 run_command_list(s, -1, 0);
wdenkc7de8292002-11-19 11:04:11 +0000417 }
418#endif /* CONFIG_MENUKEY */
Wolfgang Denk953b7e62010-06-13 18:28:54 +0200419#endif /* CONFIG_BOOTDELAY */
wdenkc7de8292002-11-19 11:04:11 +0000420
wdenkc6097192002-11-03 00:24:07 +0000421 /*
422 * Main Loop for Monitor Command Processing
423 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200424#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000425 parse_file_outer();
426 /* This point is never reached */
427 for (;;);
428#else
429 for (;;) {
430#ifdef CONFIG_BOOT_RETRY_TIME
431 if (rc >= 0) {
432 /* Saw enough of a valid command to
433 * restart the timeout.
434 */
435 reset_cmd_timeout();
436 }
437#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200438 len = readline (CONFIG_SYS_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000439
440 flag = 0; /* assume no special flags for now */
441 if (len > 0)
442 strcpy (lastcommand, console_buffer);
443 else if (len == 0)
444 flag |= CMD_FLAG_REPEAT;
445#ifdef CONFIG_BOOT_RETRY_TIME
446 else if (len == -2) {
447 /* -2 means timed out, retry autoboot
448 */
wdenk4b9206e2004-03-23 22:14:11 +0000449 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000450# ifdef CONFIG_RESET_TO_RETRY
451 /* Reinit board to run initialization code again */
452 do_reset (NULL, 0, 0, NULL);
453# else
454 return; /* retry autoboot */
455# endif
456 }
457#endif
458
459 if (len == -1)
wdenk4b9206e2004-03-23 22:14:11 +0000460 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000461 else
Simon Glass53071532012-02-14 19:59:21 +0000462 rc = run_command(lastcommand, flag);
wdenkc6097192002-11-03 00:24:07 +0000463
464 if (rc <= 0) {
465 /* invalid command or not repeatable, forget it */
466 lastcommand[0] = 0;
467 }
468 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200469#endif /*CONFIG_SYS_HUSH_PARSER*/
wdenkc6097192002-11-03 00:24:07 +0000470}
471
wdenk6dd652f2003-06-19 23:40:20 +0000472#ifdef CONFIG_BOOT_RETRY_TIME
473/***************************************************************************
Wolfgang Denk19973b62006-10-28 00:38:39 +0200474 * initialize command line timeout
wdenk6dd652f2003-06-19 23:40:20 +0000475 */
476void init_cmd_timeout(void)
477{
478 char *s = getenv ("bootretry");
479
480 if (s != NULL)
wdenkb028f712003-12-07 21:39:28 +0000481 retry_time = (int)simple_strtol(s, NULL, 10);
wdenk6dd652f2003-06-19 23:40:20 +0000482 else
483 retry_time = CONFIG_BOOT_RETRY_TIME;
484
485 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
486 retry_time = CONFIG_BOOT_RETRY_MIN;
487}
488
wdenkc6097192002-11-03 00:24:07 +0000489/***************************************************************************
490 * reset command line timeout to retry_time seconds
491 */
wdenkc6097192002-11-03 00:24:07 +0000492void reset_cmd_timeout(void)
493{
494 endtime = endtick(retry_time);
495}
496#endif
497
Wolfgang Denk501090a2006-07-21 11:33:45 +0200498#ifdef CONFIG_CMDLINE_EDITING
499
500/*
501 * cmdline-editing related codes from vivi.
502 * Author: Janghoon Lyu <nandy@mizi.com>
503 */
504
Wolfgang Denk501090a2006-07-21 11:33:45 +0200505#define putnstr(str,n) do { \
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700506 printf ("%.*s", (int)n, str); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200507 } while (0)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200508
509#define CTL_CH(c) ((c) - 'a' + 1)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200510#define CTL_BACKSPACE ('\b')
511#define DEL ((char)255)
512#define DEL7 ((char)127)
513#define CREAD_HIST_CHAR ('!')
514
515#define getcmd_putch(ch) putc(ch)
516#define getcmd_getch() getc()
517#define getcmd_cbeep() getcmd_putch('\a')
518
519#define HIST_MAX 20
Peter Tyser8804ae32010-09-29 13:30:56 -0500520#define HIST_SIZE CONFIG_SYS_CBSIZE
Wolfgang Denk501090a2006-07-21 11:33:45 +0200521
Kim Phillips199adb62012-10-29 13:34:32 +0000522static int hist_max;
523static int hist_add_idx;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200524static int hist_cur = -1;
Kim Phillips199adb62012-10-29 13:34:32 +0000525static unsigned hist_num;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200526
Kim Phillips199adb62012-10-29 13:34:32 +0000527static char *hist_list[HIST_MAX];
528static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200529
530#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
531
532static void hist_init(void)
533{
534 int i;
535
536 hist_max = 0;
537 hist_add_idx = 0;
538 hist_cur = -1;
539 hist_num = 0;
540
541 for (i = 0; i < HIST_MAX; i++) {
542 hist_list[i] = hist_lines[i];
543 hist_list[i][0] = '\0';
544 }
545}
546
547static void cread_add_to_hist(char *line)
548{
549 strcpy(hist_list[hist_add_idx], line);
550
551 if (++hist_add_idx >= HIST_MAX)
552 hist_add_idx = 0;
553
554 if (hist_add_idx > hist_max)
555 hist_max = hist_add_idx;
556
557 hist_num++;
558}
559
560static char* hist_prev(void)
561{
562 char *ret;
563 int old_cur;
564
565 if (hist_cur < 0)
566 return NULL;
567
568 old_cur = hist_cur;
569 if (--hist_cur < 0)
570 hist_cur = hist_max;
571
572 if (hist_cur == hist_add_idx) {
573 hist_cur = old_cur;
574 ret = NULL;
575 } else
576 ret = hist_list[hist_cur];
577
578 return (ret);
579}
580
581static char* hist_next(void)
582{
583 char *ret;
584
585 if (hist_cur < 0)
586 return NULL;
587
588 if (hist_cur == hist_add_idx)
589 return NULL;
590
591 if (++hist_cur > hist_max)
592 hist_cur = 0;
593
594 if (hist_cur == hist_add_idx) {
595 ret = "";
596 } else
597 ret = hist_list[hist_cur];
598
599 return (ret);
600}
601
Stefan Roese3ca91222006-07-27 16:11:19 +0200602#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk501090a2006-07-21 11:33:45 +0200603static void cread_print_hist_list(void)
604{
605 int i;
606 unsigned long n;
607
608 n = hist_num - hist_max;
609
610 i = hist_add_idx + 1;
611 while (1) {
612 if (i > hist_max)
613 i = 0;
614 if (i == hist_add_idx)
615 break;
616 printf("%s\n", hist_list[i]);
617 n++;
618 i++;
619 }
620}
Stefan Roese3ca91222006-07-27 16:11:19 +0200621#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200622
623#define BEGINNING_OF_LINE() { \
624 while (num) { \
625 getcmd_putch(CTL_BACKSPACE); \
626 num--; \
627 } \
628}
629
630#define ERASE_TO_EOL() { \
631 if (num < eol_num) { \
Mike Frysinger8faba482010-07-23 05:28:15 -0400632 printf("%*s", (int)(eol_num - num), ""); \
633 do { \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200634 getcmd_putch(CTL_BACKSPACE); \
Mike Frysinger8faba482010-07-23 05:28:15 -0400635 } while (--eol_num > num); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200636 } \
637}
638
639#define REFRESH_TO_EOL() { \
640 if (num < eol_num) { \
641 wlen = eol_num - num; \
642 putnstr(buf + num, wlen); \
643 num = eol_num; \
644 } \
645}
646
647static void cread_add_char(char ichar, int insert, unsigned long *num,
648 unsigned long *eol_num, char *buf, unsigned long len)
649{
650 unsigned long wlen;
651
652 /* room ??? */
653 if (insert || *num == *eol_num) {
654 if (*eol_num > len - 1) {
655 getcmd_cbeep();
656 return;
657 }
658 (*eol_num)++;
659 }
660
661 if (insert) {
662 wlen = *eol_num - *num;
663 if (wlen > 1) {
664 memmove(&buf[*num+1], &buf[*num], wlen-1);
665 }
666
667 buf[*num] = ichar;
668 putnstr(buf + *num, wlen);
669 (*num)++;
670 while (--wlen) {
671 getcmd_putch(CTL_BACKSPACE);
672 }
673 } else {
674 /* echo the character */
675 wlen = 1;
676 buf[*num] = ichar;
677 putnstr(buf + *num, wlen);
678 (*num)++;
679 }
680}
681
682static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
683 unsigned long *eol_num, char *buf, unsigned long len)
684{
685 while (strsize--) {
686 cread_add_char(*str, insert, num, eol_num, buf, len);
687 str++;
688 }
689}
690
Heiko Schocher9c348312012-01-16 21:13:05 +0000691static int cread_line(const char *const prompt, char *buf, unsigned int *len,
692 int timeout)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200693{
694 unsigned long num = 0;
695 unsigned long eol_num = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200696 unsigned long wlen;
697 char ichar;
698 int insert = 1;
699 int esc_len = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200700 char esc_save[8];
Peter Tyserecc55002009-10-25 15:12:54 -0500701 int init_len = strlen(buf);
Heiko Schocher9c348312012-01-16 21:13:05 +0000702 int first = 1;
Peter Tyserecc55002009-10-25 15:12:54 -0500703
704 if (init_len)
705 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
Wolfgang Denk501090a2006-07-21 11:33:45 +0200706
707 while (1) {
Andreas Engel00ac50e2008-01-09 17:10:56 +0100708#ifdef CONFIG_BOOT_RETRY_TIME
709 while (!tstc()) { /* while no incoming data */
710 if (retry_time >= 0 && get_ticks() > endtime)
711 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +0200712 WATCHDOG_RESET();
Andreas Engel00ac50e2008-01-09 17:10:56 +0100713 }
714#endif
Heiko Schocher9c348312012-01-16 21:13:05 +0000715 if (first && timeout) {
716 uint64_t etime = endtick(timeout);
717
718 while (!tstc()) { /* while no incoming data */
719 if (get_ticks() >= etime)
720 return -2; /* timed out */
721 WATCHDOG_RESET();
722 }
723 first = 0;
724 }
Andreas Engel00ac50e2008-01-09 17:10:56 +0100725
Wolfgang Denk501090a2006-07-21 11:33:45 +0200726 ichar = getcmd_getch();
727
728 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200729 putc('\n');
Wolfgang Denk501090a2006-07-21 11:33:45 +0200730 break;
731 }
732
733 /*
734 * handle standard linux xterm esc sequences for arrow key, etc.
735 */
736 if (esc_len != 0) {
737 if (esc_len == 1) {
738 if (ichar == '[') {
739 esc_save[esc_len] = ichar;
740 esc_len = 2;
741 } else {
742 cread_add_str(esc_save, esc_len, insert,
743 &num, &eol_num, buf, *len);
744 esc_len = 0;
745 }
746 continue;
747 }
748
749 switch (ichar) {
750
751 case 'D': /* <- key */
752 ichar = CTL_CH('b');
753 esc_len = 0;
754 break;
755 case 'C': /* -> key */
756 ichar = CTL_CH('f');
757 esc_len = 0;
758 break; /* pass off to ^F handler */
759 case 'H': /* Home key */
760 ichar = CTL_CH('a');
761 esc_len = 0;
762 break; /* pass off to ^A handler */
763 case 'A': /* up arrow */
764 ichar = CTL_CH('p');
765 esc_len = 0;
766 break; /* pass off to ^P handler */
767 case 'B': /* down arrow */
768 ichar = CTL_CH('n');
769 esc_len = 0;
770 break; /* pass off to ^N handler */
771 default:
772 esc_save[esc_len++] = ichar;
773 cread_add_str(esc_save, esc_len, insert,
774 &num, &eol_num, buf, *len);
775 esc_len = 0;
776 continue;
777 }
778 }
779
780 switch (ichar) {
781 case 0x1b:
782 if (esc_len == 0) {
783 esc_save[esc_len] = ichar;
784 esc_len = 1;
785 } else {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200786 puts("impossible condition #876\n");
Wolfgang Denk501090a2006-07-21 11:33:45 +0200787 esc_len = 0;
788 }
789 break;
790
791 case CTL_CH('a'):
792 BEGINNING_OF_LINE();
793 break;
794 case CTL_CH('c'): /* ^C - break */
795 *buf = '\0'; /* discard input */
796 return (-1);
797 case CTL_CH('f'):
798 if (num < eol_num) {
799 getcmd_putch(buf[num]);
800 num++;
801 }
802 break;
803 case CTL_CH('b'):
804 if (num) {
805 getcmd_putch(CTL_BACKSPACE);
806 num--;
807 }
808 break;
809 case CTL_CH('d'):
810 if (num < eol_num) {
811 wlen = eol_num - num - 1;
812 if (wlen) {
813 memmove(&buf[num], &buf[num+1], wlen);
814 putnstr(buf + num, wlen);
815 }
816
817 getcmd_putch(' ');
818 do {
819 getcmd_putch(CTL_BACKSPACE);
820 } while (wlen--);
821 eol_num--;
822 }
823 break;
824 case CTL_CH('k'):
825 ERASE_TO_EOL();
826 break;
827 case CTL_CH('e'):
828 REFRESH_TO_EOL();
829 break;
830 case CTL_CH('o'):
831 insert = !insert;
832 break;
833 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100834 case CTL_CH('u'):
Wolfgang Denk501090a2006-07-21 11:33:45 +0200835 BEGINNING_OF_LINE();
836 ERASE_TO_EOL();
837 break;
838 case DEL:
839 case DEL7:
840 case 8:
841 if (num) {
842 wlen = eol_num - num;
843 num--;
844 memmove(&buf[num], &buf[num+1], wlen);
845 getcmd_putch(CTL_BACKSPACE);
846 putnstr(buf + num, wlen);
847 getcmd_putch(' ');
848 do {
849 getcmd_putch(CTL_BACKSPACE);
850 } while (wlen--);
851 eol_num--;
852 }
853 break;
854 case CTL_CH('p'):
855 case CTL_CH('n'):
856 {
857 char * hline;
858
859 esc_len = 0;
860
861 if (ichar == CTL_CH('p'))
862 hline = hist_prev();
863 else
864 hline = hist_next();
865
866 if (!hline) {
867 getcmd_cbeep();
868 continue;
869 }
870
871 /* nuke the current line */
872 /* first, go home */
873 BEGINNING_OF_LINE();
874
875 /* erase to end of line */
876 ERASE_TO_EOL();
877
878 /* copy new line into place and display */
879 strcpy(buf, hline);
880 eol_num = strlen(buf);
881 REFRESH_TO_EOL();
882 continue;
883 }
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100884#ifdef CONFIG_AUTO_COMPLETE
885 case '\t': {
886 int num2, col;
887
888 /* do not autocomplete when in the middle */
889 if (num < eol_num) {
890 getcmd_cbeep();
891 break;
892 }
893
894 buf[num] = '\0';
895 col = strlen(prompt) + eol_num;
896 num2 = num;
897 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
898 col = num2 - num;
899 num += col;
900 eol_num += col;
901 }
902 break;
903 }
904#endif
Wolfgang Denk501090a2006-07-21 11:33:45 +0200905 default:
906 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
907 break;
908 }
909 }
910 *len = eol_num;
911 buf[eol_num] = '\0'; /* lose the newline */
912
913 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
914 cread_add_to_hist(buf);
915 hist_cur = hist_add_idx;
916
Peter Tyserf9239432009-10-25 15:12:53 -0500917 return 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200918}
919
920#endif /* CONFIG_CMDLINE_EDITING */
921
wdenkc6097192002-11-03 00:24:07 +0000922/****************************************************************************/
923
924/*
925 * Prompt for input and read a line.
926 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
927 * time out when time goes past endtime (timebase time in ticks).
928 * Return: number of read characters
929 * -1 if break
930 * -2 if timed out
931 */
932int readline (const char *const prompt)
933{
Peter Tyserecc55002009-10-25 15:12:54 -0500934 /*
935 * If console_buffer isn't 0-length the user will be prompted to modify
936 * it instead of entering it from scratch as desired.
937 */
938 console_buffer[0] = '\0';
939
Heiko Schocher9c348312012-01-16 21:13:05 +0000940 return readline_into_buffer(prompt, console_buffer, 0);
James Yang6636b622008-01-09 11:17:49 -0600941}
942
943
Heiko Schocher9c348312012-01-16 21:13:05 +0000944int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
James Yang6636b622008-01-09 11:17:49 -0600945{
946 char *p = buffer;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200947#ifdef CONFIG_CMDLINE_EDITING
Peter Tyser8804ae32010-09-29 13:30:56 -0500948 unsigned int len = CONFIG_SYS_CBSIZE;
Stefan Roesed8f961b2006-08-07 15:08:44 +0200949 int rc;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200950 static int initted = 0;
951
James Yang597f6c22008-05-05 10:22:53 -0500952 /*
953 * History uses a global array which is not
954 * writable until after relocation to RAM.
955 * Revert to non-history version if still
956 * running from flash.
957 */
958 if (gd->flags & GD_FLG_RELOC) {
959 if (!initted) {
960 hist_init();
961 initted = 1;
962 }
963
Peter Tysere491a712009-10-25 15:12:52 -0500964 if (prompt)
965 puts (prompt);
James Yang597f6c22008-05-05 10:22:53 -0500966
Heiko Schocher9c348312012-01-16 21:13:05 +0000967 rc = cread_line(prompt, p, &len, timeout);
James Yang597f6c22008-05-05 10:22:53 -0500968 return rc < 0 ? rc : len;
969
970 } else {
971#endif /* CONFIG_CMDLINE_EDITING */
Kumar Gala0ec59522008-01-10 02:22:05 -0600972 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +0000973 int n = 0; /* buffer index */
974 int plen = 0; /* prompt length */
975 int col; /* output column cnt */
976 char c;
977
978 /* print prompt */
979 if (prompt) {
980 plen = strlen (prompt);
981 puts (prompt);
982 }
983 col = plen;
984
985 for (;;) {
986#ifdef CONFIG_BOOT_RETRY_TIME
987 while (!tstc()) { /* while no incoming data */
988 if (retry_time >= 0 && get_ticks() > endtime)
989 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +0200990 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000991 }
992#endif
993 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
994
995#ifdef CONFIG_SHOW_ACTIVITY
996 while (!tstc()) {
wdenkc6097192002-11-03 00:24:07 +0000997 show_activity(0);
Jens Scharsig30dc1652010-04-09 19:02:38 +0200998 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000999 }
1000#endif
1001 c = getc();
1002
1003 /*
1004 * Special character handling
1005 */
1006 switch (c) {
1007 case '\r': /* Enter */
1008 case '\n':
1009 *p = '\0';
1010 puts ("\r\n");
James Yang6636b622008-01-09 11:17:49 -06001011 return (p - p_buf);
wdenkc6097192002-11-03 00:24:07 +00001012
wdenk27aa8182004-03-23 22:37:33 +00001013 case '\0': /* nul */
1014 continue;
1015
wdenkc6097192002-11-03 00:24:07 +00001016 case 0x03: /* ^C - break */
James Yang6636b622008-01-09 11:17:49 -06001017 p_buf[0] = '\0'; /* discard input */
wdenkc6097192002-11-03 00:24:07 +00001018 return (-1);
1019
1020 case 0x15: /* ^U - erase line */
1021 while (col > plen) {
1022 puts (erase_seq);
1023 --col;
1024 }
James Yang6636b622008-01-09 11:17:49 -06001025 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001026 n = 0;
1027 continue;
1028
Wolfgang Denk1636d1c2007-06-22 23:59:00 +02001029 case 0x17: /* ^W - erase word */
James Yang6636b622008-01-09 11:17:49 -06001030 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001031 while ((n > 0) && (*p != ' ')) {
James Yang6636b622008-01-09 11:17:49 -06001032 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001033 }
1034 continue;
1035
1036 case 0x08: /* ^H - backspace */
1037 case 0x7F: /* DEL - backspace */
James Yang6636b622008-01-09 11:17:49 -06001038 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001039 continue;
1040
1041 default:
1042 /*
1043 * Must be a normal character then
1044 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001045 if (n < CONFIG_SYS_CBSIZE-2) {
wdenkc6097192002-11-03 00:24:07 +00001046 if (c == '\t') { /* expand TABs */
wdenk04a85b32004-04-15 18:22:41 +00001047#ifdef CONFIG_AUTO_COMPLETE
1048 /* if auto completion triggered just continue */
1049 *p = '\0';
1050 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang6636b622008-01-09 11:17:49 -06001051 p = p_buf + n; /* reset */
wdenk04a85b32004-04-15 18:22:41 +00001052 continue;
1053 }
1054#endif
wdenkc6097192002-11-03 00:24:07 +00001055 puts (tab_seq+(col&07));
1056 col += 8 - (col&07);
1057 } else {
1058 ++col; /* echo input */
1059 putc (c);
1060 }
1061 *p++ = c;
1062 ++n;
1063 } else { /* Buffer full */
1064 putc ('\a');
1065 }
1066 }
1067 }
James Yang597f6c22008-05-05 10:22:53 -05001068#ifdef CONFIG_CMDLINE_EDITING
1069 }
1070#endif
wdenkc6097192002-11-03 00:24:07 +00001071}
1072
1073/****************************************************************************/
1074
1075static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1076{
1077 char *s;
1078
1079 if (*np == 0) {
1080 return (p);
1081 }
1082
1083 if (*(--p) == '\t') { /* will retype the whole line */
1084 while (*colp > plen) {
1085 puts (erase_seq);
1086 (*colp)--;
1087 }
1088 for (s=buffer; s<p; ++s) {
1089 if (*s == '\t') {
1090 puts (tab_seq+((*colp) & 07));
1091 *colp += 8 - ((*colp) & 07);
1092 } else {
1093 ++(*colp);
1094 putc (*s);
1095 }
1096 }
1097 } else {
1098 puts (erase_seq);
1099 (*colp)--;
1100 }
1101 (*np)--;
1102 return (p);
1103}
1104
1105/****************************************************************************/
1106
1107int parse_line (char *line, char *argv[])
1108{
1109 int nargs = 0;
1110
1111#ifdef DEBUG_PARSER
1112 printf ("parse_line: \"%s\"\n", line);
1113#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001114 while (nargs < CONFIG_SYS_MAXARGS) {
wdenkc6097192002-11-03 00:24:07 +00001115
1116 /* skip any white space */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001117 while (isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001118 ++line;
wdenkc6097192002-11-03 00:24:07 +00001119
1120 if (*line == '\0') { /* end of line, no more args */
1121 argv[nargs] = NULL;
1122#ifdef DEBUG_PARSER
1123 printf ("parse_line: nargs=%d\n", nargs);
1124#endif
1125 return (nargs);
1126 }
1127
1128 argv[nargs++] = line; /* begin of argument string */
1129
1130 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001131 while (*line && !isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001132 ++line;
wdenkc6097192002-11-03 00:24:07 +00001133
1134 if (*line == '\0') { /* end of line, no more args */
1135 argv[nargs] = NULL;
1136#ifdef DEBUG_PARSER
1137 printf ("parse_line: nargs=%d\n", nargs);
1138#endif
1139 return (nargs);
1140 }
1141
1142 *line++ = '\0'; /* terminate current arg */
1143 }
1144
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001145 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
wdenkc6097192002-11-03 00:24:07 +00001146
1147#ifdef DEBUG_PARSER
1148 printf ("parse_line: nargs=%d\n", nargs);
1149#endif
1150 return (nargs);
1151}
1152
1153/****************************************************************************/
1154
Simon Glass7fed89e2012-02-14 19:59:22 +00001155#ifndef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +00001156static void process_macros (const char *input, char *output)
1157{
1158 char c, prev;
1159 const char *varname_start = NULL;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001160 int inputcnt = strlen (input);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001161 int outputcnt = CONFIG_SYS_CBSIZE;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001162 int state = 0; /* 0 = waiting for '$' */
1163
1164 /* 1 = waiting for '(' or '{' */
1165 /* 2 = waiting for ')' or '}' */
1166 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001167#ifdef DEBUG_PARSER
1168 char *output_start = output;
1169
Wolfgang Denk19973b62006-10-28 00:38:39 +02001170 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1171 input);
wdenkc6097192002-11-03 00:24:07 +00001172#endif
1173
Wolfgang Denk19973b62006-10-28 00:38:39 +02001174 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001175
1176 while (inputcnt && outputcnt) {
wdenk8bde7f72003-06-27 21:31:46 +00001177 c = *input++;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001178 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001179
Wolfgang Denk19973b62006-10-28 00:38:39 +02001180 if (state != 3) {
1181 /* remove one level of escape characters */
1182 if ((c == '\\') && (prev != '\\')) {
1183 if (inputcnt-- == 0)
1184 break;
1185 prev = c;
1186 c = *input++;
1187 }
wdenka25f8622003-01-02 23:57:29 +00001188 }
wdenkc6097192002-11-03 00:24:07 +00001189
Wolfgang Denk19973b62006-10-28 00:38:39 +02001190 switch (state) {
1191 case 0: /* Waiting for (unescaped) $ */
1192 if ((c == '\'') && (prev != '\\')) {
1193 state = 3;
1194 break;
1195 }
1196 if ((c == '$') && (prev != '\\')) {
1197 state++;
1198 } else {
wdenkc6097192002-11-03 00:24:07 +00001199 *(output++) = c;
1200 outputcnt--;
1201 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001202 break;
1203 case 1: /* Waiting for ( */
1204 if (c == '(' || c == '{') {
1205 state++;
1206 varname_start = input;
1207 } else {
1208 state = 0;
1209 *(output++) = '$';
1210 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001211
Wolfgang Denk19973b62006-10-28 00:38:39 +02001212 if (outputcnt) {
1213 *(output++) = c;
wdenkc6097192002-11-03 00:24:07 +00001214 outputcnt--;
1215 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001216 }
1217 break;
1218 case 2: /* Waiting for ) */
1219 if (c == ')' || c == '}') {
1220 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001221 char envname[CONFIG_SYS_CBSIZE], *envval;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001222 int envcnt = input - varname_start - 1; /* Varname # of chars */
1223
1224 /* Get the varname */
1225 for (i = 0; i < envcnt; i++) {
1226 envname[i] = varname_start[i];
1227 }
1228 envname[i] = 0;
1229
1230 /* Get its value */
1231 envval = getenv (envname);
1232
1233 /* Copy into the line if it exists */
1234 if (envval != NULL)
1235 while ((*envval) && outputcnt) {
1236 *(output++) = *(envval++);
1237 outputcnt--;
1238 }
1239 /* Look for another '$' */
1240 state = 0;
1241 }
1242 break;
1243 case 3: /* Waiting for ' */
1244 if ((c == '\'') && (prev != '\\')) {
1245 state = 0;
1246 } else {
1247 *(output++) = c;
1248 outputcnt--;
1249 }
1250 break;
wdenkc6097192002-11-03 00:24:07 +00001251 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001252 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001253 }
1254
1255 if (outputcnt)
1256 *output = 0;
Bartlomiej Sieka9160b962007-05-27 17:04:18 +02001257 else
1258 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001259
1260#ifdef DEBUG_PARSER
1261 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
Wolfgang Denk19973b62006-10-28 00:38:39 +02001262 strlen (output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001263#endif
1264}
1265
1266/****************************************************************************
1267 * returns:
1268 * 1 - command executed, repeatable
1269 * 0 - command executed but not repeatable, interrupted commands are
1270 * always considered not repeatable
1271 * -1 - not executed (unrecognized, bootd recursion or too many args)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001272 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
wdenkc6097192002-11-03 00:24:07 +00001273 * considered unrecognized)
1274 *
1275 * WARNING:
1276 *
1277 * We must create a temporary copy of the command since the command we get
1278 * may be the result from getenv(), which returns a pointer directly to
1279 * the environment data, which may change magicly when the command we run
1280 * creates or modifies environment variables (like "bootp" does).
1281 */
Simon Glass53071532012-02-14 19:59:21 +00001282static int builtin_run_command(const char *cmd, int flag)
wdenkc6097192002-11-03 00:24:07 +00001283{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001284 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
wdenkc6097192002-11-03 00:24:07 +00001285 char *token; /* start of token in cmdbuf */
1286 char *sep; /* end of token (separator) in cmdbuf */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001287 char finaltoken[CONFIG_SYS_CBSIZE];
wdenkc6097192002-11-03 00:24:07 +00001288 char *str = cmdbuf;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001289 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenkf07771c2003-05-28 08:06:31 +00001290 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001291 int repeatable = 1;
wdenkf07771c2003-05-28 08:06:31 +00001292 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001293
1294#ifdef DEBUG_PARSER
1295 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1296 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1297 puts ("\"\n");
1298#endif
1299
1300 clear_ctrlc(); /* forget any previous Control C */
1301
1302 if (!cmd || !*cmd) {
1303 return -1; /* empty command */
1304 }
1305
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001306 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
wdenkc6097192002-11-03 00:24:07 +00001307 puts ("## Command too long!\n");
1308 return -1;
1309 }
1310
1311 strcpy (cmdbuf, cmd);
1312
1313 /* Process separators and check for invalid
1314 * repeatable commands
1315 */
1316
1317#ifdef DEBUG_PARSER
1318 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1319#endif
1320 while (*str) {
1321
1322 /*
1323 * Find separator, or string end
1324 * Allow simple escape of ';' by writing "\;"
1325 */
wdenka25f8622003-01-02 23:57:29 +00001326 for (inquotes = 0, sep = str; *sep; sep++) {
1327 if ((*sep=='\'') &&
1328 (*(sep-1) != '\\'))
1329 inquotes=!inquotes;
1330
1331 if (!inquotes &&
1332 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001333 ( sep != str) && /* past string start */
1334 (*(sep-1) != '\\')) /* and NOT escaped */
1335 break;
1336 }
1337
1338 /*
1339 * Limit the token to data between separators
1340 */
1341 token = str;
1342 if (*sep) {
1343 str = sep + 1; /* start of command for next pass */
1344 *sep = '\0';
1345 }
1346 else
1347 str = sep; /* no more commands for next pass */
1348#ifdef DEBUG_PARSER
1349 printf ("token: \"%s\"\n", token);
1350#endif
1351
1352 /* find macros in this token and replace them */
1353 process_macros (token, finaltoken);
1354
1355 /* Extract arguments */
Wolfgang Denk1264b402006-03-12 02:20:55 +01001356 if ((argc = parse_line (finaltoken, argv)) == 0) {
1357 rc = -1; /* no command at all */
1358 continue;
1359 }
wdenkc6097192002-11-03 00:24:07 +00001360
Timo Ketola030fca52012-04-22 23:57:27 +00001361 if (cmd_process(flag, argc, argv, &repeatable))
1362 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001363
1364 /* Did the user stop this? */
1365 if (had_ctrlc ())
Detlev Zundel5afb2022007-05-23 18:47:48 +02001366 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001367 }
1368
wdenkf07771c2003-05-28 08:06:31 +00001369 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001370}
Simon Glass7fed89e2012-02-14 19:59:22 +00001371#endif
wdenkc6097192002-11-03 00:24:07 +00001372
Simon Glass53071532012-02-14 19:59:21 +00001373/*
1374 * Run a command using the selected parser.
1375 *
1376 * @param cmd Command to run
1377 * @param flag Execution flags (CMD_FLAG_...)
1378 * @return 0 on success, or != 0 on error.
1379 */
1380int run_command(const char *cmd, int flag)
1381{
1382#ifndef CONFIG_SYS_HUSH_PARSER
1383 /*
1384 * builtin_run_command can return 0 or 1 for success, so clean up
1385 * its result.
1386 */
1387 if (builtin_run_command(cmd, flag) == -1)
1388 return 1;
1389
1390 return 0;
1391#else
1392 return parse_string_outer(cmd,
1393 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1394#endif
1395}
1396
Simon Glassd51004a2012-03-30 21:30:55 +00001397#ifndef CONFIG_SYS_HUSH_PARSER
1398/**
1399 * Execute a list of command separated by ; or \n using the built-in parser.
1400 *
1401 * This function cannot take a const char * for the command, since if it
1402 * finds newlines in the string, it replaces them with \0.
1403 *
1404 * @param cmd String containing list of commands
1405 * @param flag Execution flags (CMD_FLAG_...)
1406 * @return 0 on success, or != 0 on error.
1407 */
1408static int builtin_run_command_list(char *cmd, int flag)
1409{
1410 char *line, *next;
1411 int rcode = 0;
1412
1413 /*
1414 * Break into individual lines, and execute each line; terminate on
1415 * error.
1416 */
1417 line = next = cmd;
1418 while (*next) {
1419 if (*next == '\n') {
1420 *next = '\0';
1421 /* run only non-empty commands */
1422 if (*line) {
1423 debug("** exec: \"%s\"\n", line);
1424 if (builtin_run_command(line, 0) < 0) {
1425 rcode = 1;
1426 break;
1427 }
1428 }
1429 line = next + 1;
1430 }
1431 ++next;
1432 }
1433 if (rcode == 0 && *line)
1434 rcode = (builtin_run_command(line, 0) >= 0);
1435
1436 return rcode;
1437}
1438#endif
1439
1440int run_command_list(const char *cmd, int len, int flag)
1441{
1442 int need_buff = 1;
1443 char *buff = (char *)cmd; /* cast away const */
1444 int rcode = 0;
1445
1446 if (len == -1) {
1447 len = strlen(cmd);
1448#ifdef CONFIG_SYS_HUSH_PARSER
1449 /* hush will never change our string */
1450 need_buff = 0;
1451#else
1452 /* the built-in parser will change our string if it sees \n */
1453 need_buff = strchr(cmd, '\n') != NULL;
1454#endif
1455 }
1456 if (need_buff) {
1457 buff = malloc(len + 1);
1458 if (!buff)
1459 return 1;
1460 memcpy(buff, cmd, len);
1461 buff[len] = '\0';
1462 }
1463#ifdef CONFIG_SYS_HUSH_PARSER
1464 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1465#else
1466 /*
1467 * This function will overwrite any \n it sees with a \0, which
1468 * is why it can't work with a const char *. Here we are making
1469 * using of internal knowledge of this function, to avoid always
1470 * doing a malloc() which is actually required only in a case that
1471 * is pretty rare.
1472 */
1473 rcode = builtin_run_command_list(buff, flag);
1474 if (need_buff)
1475 free(buff);
1476#endif
1477
1478 return rcode;
1479}
1480
wdenkc6097192002-11-03 00:24:07 +00001481/****************************************************************************/
1482
Jon Loeligerc3517f92007-07-08 18:10:08 -05001483#if defined(CONFIG_CMD_RUN)
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001484int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +00001485{
1486 int i;
wdenkc6097192002-11-03 00:24:07 +00001487
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001488 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001489 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +00001490
1491 for (i=1; i<argc; ++i) {
wdenk3e386912003-04-05 00:53:31 +00001492 char *arg;
1493
1494 if ((arg = getenv (argv[i])) == NULL) {
1495 printf ("## Error: \"%s\" not defined\n", argv[i]);
1496 return 1;
1497 }
Jason Hobbsc8a20792011-08-31 05:37:24 +00001498
Simon Glass009dde12012-02-14 19:59:20 +00001499 if (run_command(arg, flag) != 0)
wdenk3e386912003-04-05 00:53:31 +00001500 return 1;
wdenkc6097192002-11-03 00:24:07 +00001501 }
wdenk3e386912003-04-05 00:53:31 +00001502 return 0;
wdenkc6097192002-11-03 00:24:07 +00001503}
Jon Loeliger90253172007-07-10 11:02:44 -05001504#endif